2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
23 #include "../common/common.h"
26 #define INTEXT_CHECK do { \
27 if (len == 0 || v_isempty(p, len)) { \
34 * Historic documentation (USD:15-11, 4.2) said that formfeed \
35 * characters (^L) in the first column delimited paragraphs. \
36 * The historic vi code mentions formfeed characters, but never \
37 * implements them. It seems reasonable, do it. \
39 if (p[0] == '\014') { \
44 if (p[0] != '.' || len < 2) \
46 for (lp = VIP(sp)->ps; *lp != '\0'; lp += 2) \
47 if (lp[0] == p[1] && \
48 (lp[1] == ' ' && len == 2 || lp[1] == p[2]) && \
54 * v_paragraphf -- [count]}
55 * Move forward count paragraphs.
57 * Paragraphs are empty lines after text, formfeed characters, or values
58 * from the paragraph or section options.
60 * PUBLIC: int v_paragraphf(SCR *, VICMD *);
63 v_paragraphf(SCR *sp, VICMD *vp)
65 enum { P_INTEXT, P_INBLANK } pstate;
67 recno_t cnt, lastlno, lno;
74 * If the starting cursor position is at or before any non-blank
75 * characters in the line, i.e. the movement is cutting all of the
76 * line's text, the buffer is in line mode. It's a lot easier to
77 * check here, because we know that the end is going to be the start
80 * This was historical practice in vi, with a single exception. If
81 * the paragraph movement was from the start of the last line to EOF,
82 * then all the characters were deleted from the last line, but the
83 * line itself remained. If somebody complains, don't pause, don't
84 * hesitate, just hit them.
87 if (vp->m_start.cno == 0)
90 vp->m_stop = vp->m_start;
92 if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
94 if (vp->m_start.cno <= vp->m_stop.cno)
99 /* Figure out what state we're currently in. */
100 lno = vp->m_start.lno;
101 if (db_get(sp, lno, 0, &p, &len))
105 * If we start in text, we want to switch states
106 * (2 * N - 1) times, in non-text, (2 * N) times.
108 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
110 if (len == 0 || v_isempty(p, len))
120 if (db_get(sp, ++lno, 0, &p, &len))
127 if (len == 0 || v_isempty(p, len))
135 * Non-motion commands move to the end of the range,
136 * delete and yank stay at the start. Ignore others.
137 * Adjust the end of the range for motion commands;
138 * historically, a motion component was to the end of
139 * the previous line, whereas the movement command was
140 * to the start of the new "paragraph".
142 found: if (ISMOTION(vp)) {
143 vp->m_stop.lno = lastlno;
144 vp->m_stop.cno = lastlen ? lastlen - 1 : 0;
145 vp->m_final = vp->m_start;
147 vp->m_stop.lno = lno;
149 vp->m_final = vp->m_stop;
159 * Adjust end of the range for motion commands; EOF is a movement
160 * sink. The } command historically moved to the end of the last
161 * line, not the beginning, from any position before the end of the
162 * last line. It also historically worked on empty files, so we
163 * have to make it okay.
165 eof: if (vp->m_start.lno == lno || vp->m_start.lno == lno - 1) {
166 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
172 if (vp->m_start.cno == (len ? len - 1 : 0)) {
179 * Non-motion commands move to the end of the range, delete
180 * and yank stay at the start. Ignore others.
182 * If deleting the line (which happens if deleting to EOF), then
183 * cursor movement is to the first nonblank.
185 if (ISMOTION(vp) && ISCMD(vp->rkp, 'd')) {
186 F_CLR(vp, VM_RCM_MASK);
187 F_SET(vp, VM_RCM_SETFNB);
189 vp->m_stop.lno = lno - 1;
190 vp->m_stop.cno = len ? len - 1 : 0;
191 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
196 * v_paragraphb -- [count]{
197 * Move backward count paragraphs.
199 * PUBLIC: int v_paragraphb(SCR *, VICMD *);
202 v_paragraphb(SCR *sp, VICMD *vp)
204 enum { P_INTEXT, P_INBLANK } pstate;
212 * Check for SOF. The historic vi didn't complain if users hit SOF
213 * repeatedly, unless it was part of a motion command. There is no
214 * question but that Emerson's editor of choice was vi.
216 * The { command historically moved to the beginning of the first
217 * line if invoked on the first line.
220 * If the starting cursor position is in the first column (backward
221 * paragraph movements did NOT historically pay attention to non-blank
222 * characters) i.e. the movement is cutting the entire line, the buffer
223 * is in line mode. Cuts from the beginning of the line also did not
224 * cut the current line, but started at the previous EOL.
226 * Correct for a left motion component while we're thinking about it.
228 lno = vp->m_start.lno;
231 if (vp->m_start.cno == 0) {
232 if (vp->m_start.lno == 1) {
233 v_sof(sp, &vp->m_start);
242 if (vp->m_start.lno <= 1)
245 /* Figure out what state we're currently in. */
246 if (db_get(sp, lno, 0, &p, &len))
250 * If we start in text, we want to switch states
251 * (2 * N - 1) times, in non-text, (2 * N) times.
253 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
255 if (len == 0 || v_isempty(p, len))
263 * If the starting cursor is past the first column,
264 * the current line is checked for a paragraph.
266 if (vp->m_start.cno > 0)
271 if (db_get(sp, --lno, 0, &p, &len))
278 if (len != 0 && !v_isempty(p, len)) {
289 /* SOF is a movement sink. */
292 found: vp->m_stop.lno = lno;
296 * All commands move to the end of the range. (We already
297 * adjusted the start of the range for motion commands).
299 vp->m_final = vp->m_stop;
305 * Build the paragraph command search pattern.
307 * PUBLIC: int v_buildps(SCR *, char *, char *);
310 v_buildps(SCR *sp, char *p_p, char *s_p)
317 * The vi paragraph command searches for either a paragraph or
318 * section option macro.
320 p_len = p_p == NULL ? 0 : strlen(p_p);
321 s_len = s_p == NULL ? 0 : strlen(s_p);
323 if (p_len == 0 && s_len == 0)
326 MALLOC_RET(sp, p, p_len + s_len + 1);
332 memmove(p, p_p, p_len + 1);
334 memmove(p + p_len, s_p, s_len + 1);