xref: /openbsd/usr.bin/vi/vi/v_section.c (revision cca36db2)
1 /*	$OpenBSD: v_section.c,v 1.6 2009/10/27 23:59:48 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1992, 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
17 
18 #include <bitstring.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include "../common/common.h"
24 #include "vi.h"
25 
26 /*
27  * !!!
28  * In historic vi, the section commands ignored empty lines, unlike the
29  * paragraph commands, which was probably okay.  However, they also moved
30  * to the start of the last line when there where no more sections instead
31  * of the end of the last line like the paragraph commands.  I've changed
32  * the latter behavior to match the paragraph commands.
33  *
34  * In historic vi, a section was defined as the first character(s) of the
35  * line matching, which could be followed by anything.  This implementation
36  * follows that historic practice.
37  *
38  * !!!
39  * The historic vi documentation (USD:15-10) claimed:
40  *	The section commands interpret a preceding count as a different
41  *	window size in which to redraw the screen at the new location,
42  *	and this window size is the base size for newly drawn windows
43  *	until another size is specified.  This is very useful if you are
44  *	on a slow terminal ...
45  *
46  * I can't get the 4BSD vi to do this, it just beeps at me.  For now, a
47  * count to the section commands simply repeats the command.
48  */
49 
50 /*
51  * v_sectionf -- [count]]]
52  *	Move forward count sections/functions.
53  *
54  * !!!
55  * Using ]] as a motion command was a bit special, historically.  It could
56  * match } as well as the usual { and section values.  If it matched a { or
57  * a section, it did NOT include the matched line.  If it matched a }, it
58  * did include the line.  No clue why.
59  *
60  * PUBLIC: int v_sectionf(SCR *, VICMD *);
61  */
62 int
63 v_sectionf(sp, vp)
64 	SCR *sp;
65 	VICMD *vp;
66 {
67 	recno_t cnt, lno;
68 	size_t len;
69 	char *p, *list, *lp;
70 
71 	/* Get the macro list. */
72 	if ((list = O_STR(sp, O_SECTIONS)) == NULL)
73 		return (1);
74 
75 	/*
76 	 * !!!
77 	 * If the starting cursor position is at or before any non-blank
78 	 * characters in the line, i.e. the movement is cutting all of the
79 	 * line's text, the buffer is in line mode.  It's a lot easier to
80 	 * check here, because we know that the end is going to be the start
81 	 * or end of a line.
82 	 */
83 	if (ISMOTION(vp)) {
84 		if (vp->m_start.cno == 0)
85 			F_SET(vp, VM_LMODE);
86 		else {
87 			vp->m_stop = vp->m_start;
88 			vp->m_stop.cno = 0;
89 			if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
90 				return (1);
91 			if (vp->m_start.cno <= vp->m_stop.cno)
92 				F_SET(vp, VM_LMODE);
93 		}
94 	}
95 
96 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
97 	for (lno = vp->m_start.lno; !db_get(sp, ++lno, 0, &p, &len);) {
98 		if (len == 0)
99 			continue;
100 		if (p[0] == '{' || (ISMOTION(vp) && p[0] == '}')) {
101 			if (!--cnt) {
102 				if (p[0] == '{')
103 					goto adjust1;
104 				goto adjust2;
105 			}
106 			continue;
107 		}
108 		/*
109 		 * !!!
110 		 * Historic documentation (USD:15-11, 4.2) said that formfeed
111 		 * characters (^L) in the first column delimited sections.
112 		 * The historic code mentions formfeed characters, but never
113 		 * implements them.  Seems reasonable, do it.
114 		 */
115 		if (p[0] == '\014') {
116 			if (!--cnt)
117 				goto adjust1;
118 			continue;
119 		}
120 		if (p[0] != '.' || len < 2)
121 			continue;
122 		for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
123 			if (lp[0] == p[1] &&
124 			    ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
125 			    !--cnt) {
126 				/*
127 				 * !!!
128 				 * If not cutting this line, adjust to the end
129 				 * of the previous one.  Otherwise, position to
130 				 * column 0.
131 				 */
132 adjust1:			if (ISMOTION(vp))
133 					goto ret1;
134 
135 adjust2:			vp->m_stop.lno = lno;
136 				vp->m_stop.cno = 0;
137 				goto ret2;
138 			}
139 	}
140 
141 	/* If moving forward, reached EOF, check to see if we started there. */
142 	if (vp->m_start.lno == lno - 1) {
143 		v_eof(sp, NULL);
144 		return (1);
145 	}
146 
147 ret1:	if (db_get(sp, --lno, DBG_FATAL, NULL, &len))
148 		return (1);
149 	vp->m_stop.lno = lno;
150 	vp->m_stop.cno = len ? len - 1 : 0;
151 
152 	/*
153 	 * Non-motion commands go to the end of the range.  Delete and
154 	 * yank stay at the start of the range.  Ignore others.
155 	 */
156 ret2:	if (ISMOTION(vp)) {
157 		vp->m_final = vp->m_start;
158 		if (F_ISSET(vp, VM_LMODE))
159 			vp->m_final.cno = 0;
160 	} else
161 		vp->m_final = vp->m_stop;
162 	return (0);
163 }
164 
165 /*
166  * v_sectionb -- [count][[
167  *	Move backward count sections/functions.
168  *
169  * PUBLIC: int v_sectionb(SCR *, VICMD *);
170  */
171 int
172 v_sectionb(sp, vp)
173 	SCR *sp;
174 	VICMD *vp;
175 {
176 	size_t len;
177 	recno_t cnt, lno;
178 	char *p, *list, *lp;
179 
180 	/* An empty file or starting from line 1 is always illegal. */
181 	if (vp->m_start.lno <= 1) {
182 		v_sof(sp, NULL);
183 		return (1);
184 	}
185 
186 	/* Get the macro list. */
187 	if ((list = O_STR(sp, O_SECTIONS)) == NULL)
188 		return (1);
189 
190 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
191 	for (lno = vp->m_start.lno; !db_get(sp, --lno, 0, &p, &len);) {
192 		if (len == 0)
193 			continue;
194 		if (p[0] == '{') {
195 			if (!--cnt)
196 				goto adjust1;
197 			continue;
198 		}
199 		/*
200 		 * !!!
201 		 * Historic documentation (USD:15-11, 4.2) said that formfeed
202 		 * characters (^L) in the first column delimited sections.
203 		 * The historic code mentions formfeed characters, but never
204 		 * implements them.  Seems reasonable, do it.
205 		 */
206 		if (p[0] == '\014') {
207 			if (!--cnt)
208 				goto adjust1;
209 			continue;
210 		}
211 		if (p[0] != '.' || len < 2)
212 			continue;
213 		for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
214 			if (lp[0] == p[1] &&
215 			    ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
216 			    !--cnt) {
217 adjust1:			vp->m_stop.lno = lno;
218 				vp->m_stop.cno = 0;
219 				goto ret1;
220 			}
221 	}
222 
223 	/*
224 	 * If moving backward, reached SOF, which is a movement sink.
225 	 * We already checked for starting there.
226 	 */
227 	vp->m_stop.lno = 1;
228 	vp->m_stop.cno = 0;
229 
230 	/*
231 	 * All commands move to the end of the range.
232 	 *
233 	 * !!!
234 	 * Historic practice is the section cut was in line mode if it started
235 	 * from column 0 and was in the backward direction.  Otherwise, left
236 	 * motion commands adjust the starting point to the character before
237 	 * the current one.  What makes this worse is that if it cut to line
238 	 * mode it also went to the first non-<blank>.
239 	 */
240 ret1:	if (vp->m_start.cno == 0) {
241 		F_CLR(vp, VM_RCM_MASK);
242 		F_SET(vp, VM_RCM_SETFNB);
243 
244 		--vp->m_start.lno;
245 		F_SET(vp, VM_LMODE);
246 	} else
247 		--vp->m_start.cno;
248 
249 	vp->m_final = vp->m_stop;
250 	return (0);
251 }
252