Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / v_paragraph.c
blob22cb46df20f9f3a749e65d238bf8eb3289371dae
1 /* $NetBSD: v_paragraph.c,v 1.1.1.2 2008/05/18 14:31:43 aymeric Exp $ */
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.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: v_paragraph.c,v 10.10 2001/06/25 15:19:32 skimo Exp (Berkeley) Date: 2001/06/25 15:19:32";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "vi.h"
32 #define INTEXT_CHECK { \
33 if (len == 0 || v_isempty(p, len)) { \
34 if (!--cnt) \
35 goto found; \
36 pstate = P_INBLANK; \
37 } \
38 /* \
39 * !!! \
40 * Historic documentation (USD:15-11, 4.2) said that formfeed \
41 * characters (^L) in the first column delimited paragraphs. \
42 * The historic vi code mentions formfeed characters, but never \
43 * implements them. It seems reasonable, do it. \
44 */ \
45 if (p[0] == '\014') { \
46 if (!--cnt) \
47 goto found; \
48 continue; \
49 } \
50 if (p[0] != '.' || len < 2) \
51 continue; \
52 for (lp = VIP(sp)->ps; *lp != '\0'; lp += 2) \
53 if (lp[0] == p[1] && \
54 ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) && \
55 !--cnt) \
56 goto found; \
60 * v_paragraphf -- [count]}
61 * Move forward count paragraphs.
63 * Paragraphs are empty lines after text, formfeed characters, or values
64 * from the paragraph or section options.
66 * PUBLIC: int v_paragraphf __P((SCR *, VICMD *));
68 int
69 v_paragraphf(SCR *sp, VICMD *vp)
71 enum { P_INTEXT, P_INBLANK } pstate;
72 size_t lastlen, len;
73 db_recno_t cnt, lastlno, lno;
74 int isempty;
75 CHAR_T *p;
76 char *lp;
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.
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.
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);
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;
111 * If we start in text, we want to switch states
112 * (2 * N - 1) times, in non-text, (2 * N) times.
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;
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;
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".
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;
157 return (0);
158 default:
159 abort();
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.
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);
178 if (vp->m_start.cno == (len ? len - 1 : 0)) {
179 v_eof(sp, NULL);
180 return (1);
184 * !!!
185 * Non-motion commands move to the end of the range, delete
186 * and yank stay at the start. Ignore others.
188 * If deleting the line (which happens if deleting to EOF), then
189 * cursor movement is to the first nonblank.
191 if (ISMOTION(vp) && ISCMD(vp->rkp, 'd')) {
192 F_CLR(vp, VM_RCM_MASK);
193 F_SET(vp, VM_RCM_SETFNB);
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);
202 * v_paragraphb -- [count]{
203 * Move backward count paragraphs.
205 * PUBLIC: int v_paragraphb __P((SCR *, VICMD *));
208 v_paragraphb(SCR *sp, VICMD *vp)
210 enum { P_INTEXT, P_INBLANK } pstate;
211 size_t len;
212 db_recno_t cnt, lno;
213 CHAR_T *p;
214 char *lp;
217 * !!!
218 * Check for SOF. The historic vi didn't complain if users hit SOF
219 * repeatedly, unless it was part of a motion command. There is no
220 * question but that Emerson's editor of choice was vi.
222 * The { command historically moved to the beginning of the first
223 * line if invoked on the first line.
225 * !!!
226 * If the starting cursor position is in the first column (backward
227 * paragraph movements did NOT historically pay attention to non-blank
228 * characters) i.e. the movement is cutting the entire line, the buffer
229 * is in line mode. Cuts from the beginning of the line also did not
230 * cut the current line, but started at the previous EOL.
232 * Correct for a left motion component while we're thinking about it.
234 lno = vp->m_start.lno;
236 if (ISMOTION(vp)) {
237 if (vp->m_start.cno == 0) {
238 if (vp->m_start.lno == 1) {
239 v_sof(sp, &vp->m_start);
240 return (1);
241 } else
242 --vp->m_start.lno;
243 F_SET(vp, VM_LMODE);
244 } else
245 --vp->m_start.cno;
248 if (vp->m_start.lno <= 1)
249 goto sof;
251 /* Figure out what state we're currently in. */
252 if (db_get(sp, lno, 0, &p, &len))
253 goto sof;
256 * If we start in text, we want to switch states
257 * (2 * N - 1) times, in non-text, (2 * N) times.
259 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
260 cnt *= 2;
261 if (len == 0 || v_isempty(p, len))
262 pstate = P_INBLANK;
263 else {
264 --cnt;
265 pstate = P_INTEXT;
268 * !!!
269 * If the starting cursor is past the first column,
270 * the current line is checked for a paragraph.
272 if (vp->m_start.cno > 0)
273 ++lno;
276 for (;;) {
277 if (db_get(sp, --lno, 0, &p, &len))
278 goto sof;
279 switch (pstate) {
280 case P_INTEXT:
281 INTEXT_CHECK;
282 break;
283 case P_INBLANK:
284 if (len != 0 && !v_isempty(p, len)) {
285 if (!--cnt)
286 goto found;
287 pstate = P_INTEXT;
289 break;
290 default:
291 abort();
295 /* SOF is a movement sink. */
296 sof: lno = 1;
298 found: vp->m_stop.lno = lno;
299 vp->m_stop.cno = 0;
302 * All commands move to the end of the range. (We already
303 * adjusted the start of the range for motion commands).
305 vp->m_final = vp->m_stop;
306 return (0);
310 * v_buildps --
311 * Build the paragraph command search pattern.
313 * PUBLIC: int v_buildps __P((SCR *, const char *, const char *));
316 v_buildps(SCR *sp, const char *p_p, const char *s_p)
318 VI_PRIVATE *vip;
319 size_t p_len, s_len;
320 char *p;
323 * The vi paragraph command searches for either a paragraph or
324 * section option macro.
326 p_len = p_p == NULL ? 0 : strlen(p_p);
327 s_len = s_p == NULL ? 0 : strlen(s_p);
329 if (p_len == 0 && s_len == 0)
330 return (0);
332 MALLOC_RET(sp, p, char *, p_len + s_len + 1);
334 vip = VIP(sp);
335 if (vip->ps != NULL)
336 free(vip->ps);
338 if (p_p != NULL)
339 memmove(p, p_p, p_len + 1);
340 if (s_p != NULL)
341 memmove(p + p_len, s_p, s_len + 1);
342 vip->ps = p;
343 return (0);