1 /* $NetBSD: v_section.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: v_section.c,v 10.10 2001/06/25 15:19:35 skimo Exp (Berkeley) Date: 2001/06/25 15:19:35 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
26 #include "../common/common.h"
31 * In historic vi, the section commands ignored empty lines, unlike the
32 * paragraph commands, which was probably okay. However, they also moved
33 * to the start of the last line when there where no more sections instead
34 * of the end of the last line like the paragraph commands. I've changed
35 * the latter behavior to match the paragraph commands.
37 * In historic vi, a section was defined as the first character(s) of the
38 * line matching, which could be followed by anything. This implementation
39 * follows that historic practice.
42 * The historic vi documentation (USD:15-10) claimed:
43 * The section commands interpret a preceding count as a different
44 * window size in which to redraw the screen at the new location,
45 * and this window size is the base size for newly drawn windows
46 * until another size is specified. This is very useful if you are
47 * on a slow terminal ...
49 * I can't get the 4BSD vi to do this, it just beeps at me. For now, a
50 * count to the section commands simply repeats the command.
54 * v_sectionf -- [count]]]
55 * Move forward count sections/functions.
58 * Using ]] as a motion command was a bit special, historically. It could
59 * match } as well as the usual { and section values. If it matched a { or
60 * a section, it did NOT include the matched line. If it matched a }, it
61 * did include the line. No clue why.
63 * PUBLIC: int v_sectionf __P((SCR *, VICMD *));
66 v_sectionf(SCR
*sp
, VICMD
*vp
)
71 const char *list
, *lp
;
73 /* Get the macro list. */
74 if ((list
= O_STR(sp
, O_SECTIONS
)) == NULL
)
79 * If the starting cursor position is at or before any non-blank
80 * characters in the line, i.e. the movement is cutting all of the
81 * line's text, the buffer is in line mode. It's a lot easier to
82 * check here, because we know that the end is going to be the start
86 if (vp
->m_start
.cno
== 0)
89 vp
->m_stop
= vp
->m_start
;
91 if (nonblank(sp
, vp
->m_stop
.lno
, &vp
->m_stop
.cno
))
93 if (vp
->m_start
.cno
<= vp
->m_stop
.cno
)
98 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
99 for (lno
= vp
->m_start
.lno
; !db_get(sp
, ++lno
, 0, &p
, &len
);) {
102 if (p
[0] == '{' || (ISMOTION(vp
) && p
[0] == '}')) {
112 * Historic documentation (USD:15-11, 4.2) said that formfeed
113 * characters (^L) in the first column delimited sections.
114 * The historic code mentions formfeed characters, but never
115 * implements them. Seems reasonable, do it.
117 if (p
[0] == '\014') {
122 if (p
[0] != '.' || len
< 2)
124 for (lp
= list
; *lp
!= '\0'; lp
+= 2 * sizeof(*lp
))
126 ((lp
[1] == ' ' && len
== 2) || lp
[1] == p
[2]) &&
130 * If not cutting this line, adjust to the end
131 * of the previous one. Otherwise, position to
134 adjust1
: if (ISMOTION(vp
))
137 adjust2
: vp
->m_stop
.lno
= lno
;
143 /* If moving forward, reached EOF, check to see if we started there. */
144 if (vp
->m_start
.lno
== lno
- 1) {
149 ret1
: if (db_get(sp
, --lno
, DBG_FATAL
, NULL
, &len
))
151 vp
->m_stop
.lno
= lno
;
152 vp
->m_stop
.cno
= len
? len
- 1 : 0;
155 * Non-motion commands go to the end of the range. Delete and
156 * yank stay at the start of the range. Ignore others.
158 ret2
: if (ISMOTION(vp
)) {
159 vp
->m_final
= vp
->m_start
;
160 if (F_ISSET(vp
, VM_LMODE
))
163 vp
->m_final
= vp
->m_stop
;
168 * v_sectionb -- [count][[
169 * Move backward count sections/functions.
171 * PUBLIC: int v_sectionb __P((SCR *, VICMD *));
174 v_sectionb(SCR
*sp
, VICMD
*vp
)
179 const char *list
, *lp
;
181 /* An empty file or starting from line 1 is always illegal. */
182 if (vp
->m_start
.lno
<= 1) {
187 /* Get the macro list. */
188 if ((list
= O_STR(sp
, O_SECTIONS
)) == NULL
)
191 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
192 for (lno
= vp
->m_start
.lno
; !db_get(sp
, --lno
, 0, &p
, &len
);) {
202 * Historic documentation (USD:15-11, 4.2) said that formfeed
203 * characters (^L) in the first column delimited sections.
204 * The historic code mentions formfeed characters, but never
205 * implements them. Seems reasonable, do it.
207 if (p
[0] == '\014') {
212 if (p
[0] != '.' || len
< 2)
214 for (lp
= list
; *lp
!= '\0'; lp
+= 2 * sizeof(*lp
))
216 ((lp
[1] == ' ' && len
== 2) || lp
[1] == p
[2]) &&
218 adjust1
: vp
->m_stop
.lno
= lno
;
225 * If moving backward, reached SOF, which is a movement sink.
226 * We already checked for starting there.
232 * All commands move to the end of the range.
235 * Historic practice is the section cut was in line mode if it started
236 * from column 0 and was in the backward direction. Otherwise, left
237 * motion commands adjust the starting point to the character before
238 * the current one. What makes this worse is that if it cut to line
239 * mode it also went to the first non-<blank>.
241 ret1
: if (vp
->m_start
.cno
== 0) {
242 F_CLR(vp
, VM_RCM_MASK
);
243 F_SET(vp
, VM_RCM_SETFNB
);
250 vp
->m_final
= vp
->m_stop
;