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