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.
13 static const char sccsid
[] = "$Id: v_paragraph.c,v 10.9 2000/06/27 17:19:08 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:08 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
30 #define INTEXT_CHECK { \
31 if (len == 0 || v_isempty(p, len)) { \
38 * Historic documentation (USD:15-11, 4.2) said that formfeed \
39 * characters (^L) in the first column delimited paragraphs. \
40 * The historic vi code mentions formfeed characters, but never \
41 * implements them. It seems reasonable, do it. \
43 if (p[0] == '\014') { \
48 if (p[0] != '.' || len < 2) \
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]) && \
58 * v_paragraphf -- [count]}
59 * Move forward count paragraphs.
61 * Paragraphs are empty lines after text, formfeed characters, or values
62 * from the paragraph or section options.
64 * PUBLIC: int v_paragraphf __P((SCR *, VICMD *));
71 enum { P_INTEXT
, P_INBLANK
} pstate
;
73 db_recno_t cnt
, lastlno
, lno
;
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
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.
93 if (vp
->m_start
.cno
== 0)
96 vp
->m_stop
= vp
->m_start
;
98 if (nonblank(sp
, vp
->m_stop
.lno
, &vp
->m_stop
.cno
))
100 if (vp
->m_start
.cno
<= vp
->m_stop
.cno
)
104 /* Figure out what state we're currently in. */
105 lno
= vp
->m_start
.lno
;
106 if (db_get(sp
, lno
, 0, &p
, &len
))
110 * If we start in text, we want to switch states
111 * (2 * N - 1) times, in non-text, (2 * N) times.
113 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
115 if (len
== 0 || v_isempty(p
, len
))
125 if (db_get(sp
, ++lno
, 0, &p
, &len
))
132 if (len
== 0 || v_isempty(p
, len
))
140 * Non-motion commands move to the end of the range,
141 * delete and yank stay at the start. Ignore others.
142 * Adjust the end of the range for motion commands;
143 * historically, a motion component was to the end of
144 * the previous line, whereas the movement command was
145 * to the start of the new "paragraph".
147 found
: if (ISMOTION(vp
)) {
148 vp
->m_stop
.lno
= lastlno
;
149 vp
->m_stop
.cno
= lastlen
? lastlen
- 1 : 0;
150 vp
->m_final
= vp
->m_start
;
152 vp
->m_stop
.lno
= lno
;
154 vp
->m_final
= vp
->m_stop
;
164 * Adjust end of the range for motion commands; EOF is a movement
165 * sink. The } command historically moved to the end of the last
166 * line, not the beginning, from any position before the end of the
167 * last line. It also historically worked on empty files, so we
168 * have to make it okay.
170 eof
: if (vp
->m_start
.lno
== lno
|| vp
->m_start
.lno
== lno
- 1) {
171 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
177 if (vp
->m_start
.cno
== (len
? len
- 1 : 0)) {
184 * Non-motion commands move to the end of the range, delete
185 * and yank stay at the start. Ignore others.
187 * If deleting the line (which happens if deleting to EOF), then
188 * cursor movement is to the first nonblank.
190 if (ISMOTION(vp
) && ISCMD(vp
->rkp
, 'd')) {
191 F_CLR(vp
, VM_RCM_MASK
);
192 F_SET(vp
, VM_RCM_SETFNB
);
194 vp
->m_stop
.lno
= lno
- 1;
195 vp
->m_stop
.cno
= len
? len
- 1 : 0;
196 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
201 * v_paragraphb -- [count]{
202 * Move backward count paragraphs.
204 * PUBLIC: int v_paragraphb __P((SCR *, VICMD *));
211 enum { P_INTEXT
, P_INBLANK
} pstate
;
219 * Check for SOF. The historic vi didn't complain if users hit SOF
220 * repeatedly, unless it was part of a motion command. There is no
221 * question but that Emerson's editor of choice was vi.
223 * The { command historically moved to the beginning of the first
224 * line if invoked on the first line.
227 * If the starting cursor position is in the first column (backward
228 * paragraph movements did NOT historically pay attention to non-blank
229 * characters) i.e. the movement is cutting the entire line, the buffer
230 * is in line mode. Cuts from the beginning of the line also did not
231 * cut the current line, but started at the previous EOL.
233 * Correct for a left motion component while we're thinking about it.
235 lno
= vp
->m_start
.lno
;
238 if (vp
->m_start
.cno
== 0) {
239 if (vp
->m_start
.lno
== 1) {
240 v_sof(sp
, &vp
->m_start
);
248 if (vp
->m_start
.lno
<= 1)
251 /* Figure out what state we're currently in. */
252 if (db_get(sp
, lno
, 0, &p
, &len
))
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;
261 if (len
== 0 || v_isempty(p
, len
))
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)
277 if (db_get(sp
, --lno
, 0, &p
, &len
))
284 if (len
!= 0 && !v_isempty(p
, len
)) {
295 /* SOF is a movement sink. */
298 found
: vp
->m_stop
.lno
= lno
;
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
;
311 * Build the paragraph command search pattern.
313 * PUBLIC: int v_buildps __P((SCR *, char *, char *));
316 v_buildps(sp
, p_p
, s_p
)
325 * The vi paragraph command searches for either a paragraph or
326 * section option macro.
328 p_len
= p_p
== NULL
? 0 : strlen(p_p
);
329 s_len
= s_p
== NULL
? 0 : strlen(s_p
);
331 if (p_len
== 0 && s_len
== 0)
334 MALLOC_RET(sp
, p
, char *, p_len
+ s_len
+ 1);
341 memmove(p
, p_p
, p_len
+ 1);
343 memmove(p
+ p_len
, s_p
, s_len
+ 1);