xref: /openbsd-src/usr.bin/vi/vi/v_paragraph.c (revision 57651b989732fe71e698cb13c8007c9650fe78b9)
1 /*	$OpenBSD: v_paragraph.c,v 1.10 2023/09/07 11:17:32 tobhe 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 <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "../common/common.h"
26 #include "vi.h"
27 
28 #define	INTEXT_CHECK {							\
29 	if (len == 0 || v_isempty(p, len)) {				\
30 		if (!--cnt)						\
31 			goto found;					\
32 		pstate = P_INBLANK;					\
33 	}								\
34 	/*								\
35 	 * !!!								\
36 	 * Historic documentation (USD:15-11, 4.2) said that formfeed	\
37 	 * characters (^L) in the first column delimited paragraphs.	\
38 	 * The historic vi code mentions formfeed characters, but never	\
39 	 * implements them.  It seems reasonable, do it.		\
40 	 */								\
41 	if (p[0] == '\014') {						\
42 		if (!--cnt)						\
43 			goto found;					\
44 		if (pstate == P_INTEXT && !--cnt)			\
45 			goto found;					\
46 		continue;						\
47 	}								\
48 	if (p[0] != '.' || len < 2)					\
49 		continue;						\
50 	for (lp = VIP(sp)->ps; *lp != '\0'; lp += 2)			\
51 		if (lp[0] == p[1] &&					\
52 		    (lp[1] == ' ' && len == 2 || lp[1] == p[2])) {	\
53 			if (!--cnt)					\
54 				goto found;				\
55 			if (pstate == P_INTEXT && !--cnt)		\
56 				goto found;				\
57 		}							\
58 }
59 
60 /*
61  * v_paragraphf -- [count]}
62  *	Move forward count paragraphs.
63  *
64  * Paragraphs are empty lines after text, formfeed characters, or values
65  * from the paragraph or section options.
66  *
67  * PUBLIC: int v_paragraphf(SCR *, VICMD *);
68  */
69 int
v_paragraphf(SCR * sp,VICMD * vp)70 v_paragraphf(SCR *sp, VICMD *vp)
71 {
72 	enum { P_INTEXT, P_INBLANK } pstate;
73 	size_t lastlen, len;
74 	recno_t cnt, lastlno, lno;
75 	int isempty;
76 	char *p, *lp;
77 
78 	/*
79 	 * !!!
80 	 * If the starting cursor position is at or before any non-blank
81 	 * characters in the line, i.e. the movement is cutting all of the
82 	 * line's text, the buffer is in line mode.  It's a lot easier to
83 	 * check here, because we know that the end is going to be the start
84 	 * or end of a line.
85 	 *
86 	 * This was historical practice in vi, with a single exception.  If
87 	 * the paragraph movement was from the start of the last line to EOF,
88 	 * then all the characters were deleted from the last line, but the
89 	 * line itself remained.  If somebody complains, don't pause, don't
90 	 * hesitate, just hit them.
91 	 */
92 	if (ISMOTION(vp)) {
93 		if (vp->m_start.cno == 0)
94 			F_SET(vp, VM_LMODE);
95 		else {
96 			vp->m_stop = vp->m_start;
97 			vp->m_stop.cno = 0;
98 			if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
99 				return (1);
100 			if (vp->m_start.cno <= vp->m_stop.cno)
101 				F_SET(vp, VM_LMODE);
102 		}
103 	}
104 
105 	/* Figure out what state we're currently in. */
106 	lno = vp->m_start.lno;
107 	if (db_get(sp, lno, 0, &p, &len))
108 		goto eof;
109 
110 	/*
111 	 * If we start in text, we want to switch states
112 	 * (2 * N - 1) times, in non-text, (2 * N) times.
113 	 */
114 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
115 	cnt *= 2;
116 	if (len == 0 || v_isempty(p, len))
117 		pstate = P_INBLANK;
118 	else {
119 		--cnt;
120 		pstate = P_INTEXT;
121 	}
122 
123 	for (;;) {
124 		lastlno = lno;
125 		lastlen = len;
126 		if (db_get(sp, ++lno, 0, &p, &len))
127 			goto eof;
128 		switch (pstate) {
129 		case P_INTEXT:
130 			INTEXT_CHECK;
131 			break;
132 		case P_INBLANK:
133 			if (len == 0 || v_isempty(p, len))
134 				break;
135 			if (--cnt) {
136 				pstate = P_INTEXT;
137 				break;
138 			}
139 			/*
140 			 * !!!
141 			 * Non-motion commands move to the end of the range,
142 			 * delete and yank stay at the start.  Ignore others.
143 			 * Adjust the end of the range for motion commands;
144 			 * historically, a motion component was to the end of
145 			 * the previous line, whereas the movement command was
146 			 * to the start of the new "paragraph".
147 			 */
148 found:			if (ISMOTION(vp)) {
149 				vp->m_stop.lno = lastlno;
150 				vp->m_stop.cno = lastlen ? lastlen - 1 : 0;
151 				vp->m_final = vp->m_start;
152 			} else {
153 				vp->m_stop.lno = lno;
154 				vp->m_stop.cno = 0;
155 				vp->m_final = vp->m_stop;
156 			}
157 			return (0);
158 		default:
159 			abort();
160 		}
161 	}
162 
163 	/*
164 	 * !!!
165 	 * Adjust end of the range for motion commands; EOF is a movement
166 	 * sink.  The } command historically moved to the end of the last
167 	 * line, not the beginning, from any position before the end of the
168 	 * last line.  It also historically worked on empty files, so we
169 	 * have to make it okay.
170 	 */
171 eof:	if (vp->m_start.lno == lno || vp->m_start.lno == lno - 1) {
172 		if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
173 			if (!isempty)
174 				return (1);
175 			vp->m_start.cno = 0;
176 			return (0);
177 		}
178 		if (vp->m_start.cno == (len ? len - 1 : 0)) {
179 			v_eof(sp, NULL);
180 			return (1);
181 		}
182 	}
183 	/*
184 	 * !!!
185 	 * Non-motion commands move to the end of the range, delete
186 	 * and yank stay at the start.  Ignore others.
187 	 *
188 	 * If deleting the line (which happens if deleting to EOF), then
189 	 * cursor movement is to the first nonblank.
190 	 */
191 	if (ISMOTION(vp) && ISCMD(vp->rkp, 'd')) {
192 		F_CLR(vp, VM_RCM_MASK);
193 		F_SET(vp, VM_RCM_SETFNB);
194 	}
195 	vp->m_stop.lno = lno - 1;
196 	vp->m_stop.cno = len ? len - 1 : 0;
197 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
198 	return (0);
199 }
200 
201 /*
202  * v_paragraphb -- [count]{
203  *	Move backward count paragraphs.
204  *
205  * PUBLIC: int v_paragraphb(SCR *, VICMD *);
206  */
207 int
v_paragraphb(SCR * sp,VICMD * vp)208 v_paragraphb(SCR *sp, VICMD *vp)
209 {
210 	enum { P_INTEXT, P_INBLANK } pstate;
211 	size_t len;
212 	recno_t cnt, lno;
213 	char *p, *lp;
214 
215 	/*
216 	 * !!!
217 	 * Check for SOF.  The historic vi didn't complain if users hit SOF
218 	 * repeatedly, unless it was part of a motion command.  There is no
219 	 * question but that Emerson's editor of choice was vi.
220 	 *
221 	 * The { command historically moved to the beginning of the first
222 	 * line if invoked on the first line.
223 	 *
224 	 * !!!
225 	 * If the starting cursor position is in the first column (backward
226 	 * paragraph movements did NOT historically pay attention to non-blank
227 	 * characters) i.e. the movement is cutting the entire line, the buffer
228 	 * is in line mode.  Cuts from the beginning of the line also did not
229 	 * cut the current line, but started at the previous EOL.
230 	 *
231 	 * Correct for a left motion component while we're thinking about it.
232 	 */
233 	lno = vp->m_start.lno;
234 
235 	if (ISMOTION(vp)) {
236 		if (vp->m_start.cno == 0) {
237 			if (vp->m_start.lno == 1) {
238 				v_sof(sp, &vp->m_start);
239 				return (1);
240 			} else
241 				--vp->m_start.lno;
242 			F_SET(vp, VM_LMODE);
243 		} else
244 			--vp->m_start.cno;
245 	}
246 
247 	if (vp->m_start.lno <= 1)
248 		goto sof;
249 
250 	/* Figure out what state we're currently in. */
251 	if (db_get(sp, lno, 0, &p, &len))
252 		goto sof;
253 
254 	/*
255 	 * If we start in text, we want to switch states
256 	 * (2 * N - 1) times, in non-text, (2 * N) times.
257 	 */
258 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
259 	cnt *= 2;
260 	if (len == 0 || v_isempty(p, len))
261 		pstate = P_INBLANK;
262 	else {
263 		--cnt;
264 		pstate = P_INTEXT;
265 
266 		/*
267 		 * !!!
268 		 * If the starting cursor is past the first column,
269 		 * the current line is checked for a paragraph.
270 		 */
271 		if (vp->m_start.cno > 0)
272 			++lno;
273 	}
274 
275 	for (;;) {
276 		if (db_get(sp, --lno, 0, &p, &len))
277 			goto sof;
278 		switch (pstate) {
279 		case P_INTEXT:
280 			INTEXT_CHECK;
281 			break;
282 		case P_INBLANK:
283 			if (len != 0 && !v_isempty(p, len)) {
284 				if (!--cnt)
285 					goto found;
286 				pstate = P_INTEXT;
287 			}
288 			break;
289 		default:
290 			abort();
291 		}
292 	}
293 
294 	/* SOF is a movement sink. */
295 sof:	lno = 1;
296 
297 found:	vp->m_stop.lno = lno;
298 	vp->m_stop.cno = 0;
299 
300 	/*
301 	 * All commands move to the end of the range.  (We already
302 	 * adjusted the start of the range for motion commands).
303 	 */
304 	vp->m_final = vp->m_stop;
305 	return (0);
306 }
307 
308 /*
309  * v_buildps --
310  *	Build the paragraph command search pattern.
311  *
312  * PUBLIC: int v_buildps(SCR *, char *, char *);
313  */
314 int
v_buildps(SCR * sp,char * p_p,char * s_p)315 v_buildps(SCR *sp, char *p_p, char *s_p)
316 {
317 	VI_PRIVATE *vip;
318 	size_t p_len, s_len;
319 	char *p;
320 
321 	/*
322 	 * The vi paragraph command searches for either a paragraph or
323 	 * section option macro.
324 	 */
325 	p_len = p_p == NULL ? 0 : strlen(p_p);
326 	s_len = s_p == NULL ? 0 : strlen(s_p);
327 
328 	if (p_len == 0 && s_len == 0)
329 		return (0);
330 
331 	MALLOC_RET(sp, p, p_len + s_len + 1);
332 
333 	vip = VIP(sp);
334 	free(vip->ps);
335 
336 	if (p_p != NULL)
337 		memmove(p, p_p, p_len + 1);
338 	if (s_p != NULL)
339 		memmove(p + p_len, s_p, s_len + 1);
340 	vip->ps = p;
341 	return (0);
342 }
343