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