Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / v_section.c
blob392984ec10041443a214a8924ce95ced42489590
1 /* $NetBSD: v_section.c,v 1.1.1.2 2008/05/18 14:31:44 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "vi.h"
31 * !!!
32 * In historic vi, the section commands ignored empty lines, unlike the
33 * paragraph commands, which was probably okay. However, they also moved
34 * to the start of the last line when there where no more sections instead
35 * of the end of the last line like the paragraph commands. I've changed
36 * the latter behavior to match the paragraph commands.
38 * In historic vi, a section was defined as the first character(s) of the
39 * line matching, which could be followed by anything. This implementation
40 * follows that historic practice.
42 * !!!
43 * The historic vi documentation (USD:15-10) claimed:
44 * The section commands interpret a preceding count as a different
45 * window size in which to redraw the screen at the new location,
46 * and this window size is the base size for newly drawn windows
47 * until another size is specified. This is very useful if you are
48 * on a slow terminal ...
50 * I can't get the 4BSD vi to do this, it just beeps at me. For now, a
51 * count to the section commands simply repeats the command.
55 * v_sectionf -- [count]]]
56 * Move forward count sections/functions.
58 * !!!
59 * Using ]] as a motion command was a bit special, historically. It could
60 * match } as well as the usual { and section values. If it matched a { or
61 * a section, it did NOT include the matched line. If it matched a }, it
62 * did include the line. No clue why.
64 * PUBLIC: int v_sectionf __P((SCR *, VICMD *));
66 int
67 v_sectionf(SCR *sp, VICMD *vp)
69 db_recno_t cnt, lno;
70 size_t len;
71 CHAR_T *p;
72 const char *list, *lp;
74 /* Get the macro list. */
75 if ((list = O_STR(sp, O_SECTIONS)) == NULL)
76 return (1);
79 * !!!
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
84 * or end of a line.
86 if (ISMOTION(vp)) {
87 if (vp->m_start.cno == 0)
88 F_SET(vp, VM_LMODE);
89 else {
90 vp->m_stop = vp->m_start;
91 vp->m_stop.cno = 0;
92 if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
93 return (1);
94 if (vp->m_start.cno <= vp->m_stop.cno)
95 F_SET(vp, VM_LMODE);
99 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
100 for (lno = vp->m_start.lno; !db_get(sp, ++lno, 0, &p, &len);) {
101 if (len == 0)
102 continue;
103 if (p[0] == '{' || (ISMOTION(vp) && p[0] == '}')) {
104 if (!--cnt) {
105 if (p[0] == '{')
106 goto adjust1;
107 goto adjust2;
109 continue;
112 * !!!
113 * Historic documentation (USD:15-11, 4.2) said that formfeed
114 * characters (^L) in the first column delimited sections.
115 * The historic code mentions formfeed characters, but never
116 * implements them. Seems reasonable, do it.
118 if (p[0] == '\014') {
119 if (!--cnt)
120 goto adjust1;
121 continue;
123 if (p[0] != '.' || len < 2)
124 continue;
125 for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
126 if (lp[0] == p[1] &&
127 ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
128 !--cnt) {
130 * !!!
131 * If not cutting this line, adjust to the end
132 * of the previous one. Otherwise, position to
133 * column 0.
135 adjust1: if (ISMOTION(vp))
136 goto ret1;
138 adjust2: vp->m_stop.lno = lno;
139 vp->m_stop.cno = 0;
140 goto ret2;
144 /* If moving forward, reached EOF, check to see if we started there. */
145 if (vp->m_start.lno == lno - 1) {
146 v_eof(sp, NULL);
147 return (1);
150 ret1: if (db_get(sp, --lno, DBG_FATAL, NULL, &len))
151 return (1);
152 vp->m_stop.lno = lno;
153 vp->m_stop.cno = len ? len - 1 : 0;
156 * Non-motion commands go to the end of the range. Delete and
157 * yank stay at the start of the range. Ignore others.
159 ret2: if (ISMOTION(vp)) {
160 vp->m_final = vp->m_start;
161 if (F_ISSET(vp, VM_LMODE))
162 vp->m_final.cno = 0;
163 } else
164 vp->m_final = vp->m_stop;
165 return (0);
169 * v_sectionb -- [count][[
170 * Move backward count sections/functions.
172 * PUBLIC: int v_sectionb __P((SCR *, VICMD *));
175 v_sectionb(SCR *sp, VICMD *vp)
177 size_t len;
178 db_recno_t cnt, lno;
179 CHAR_T *p;
180 const char *list, *lp;
182 /* An empty file or starting from line 1 is always illegal. */
183 if (vp->m_start.lno <= 1) {
184 v_sof(sp, NULL);
185 return (1);
188 /* Get the macro list. */
189 if ((list = O_STR(sp, O_SECTIONS)) == NULL)
190 return (1);
192 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
193 for (lno = vp->m_start.lno; !db_get(sp, --lno, 0, &p, &len);) {
194 if (len == 0)
195 continue;
196 if (p[0] == '{') {
197 if (!--cnt)
198 goto adjust1;
199 continue;
202 * !!!
203 * Historic documentation (USD:15-11, 4.2) said that formfeed
204 * characters (^L) in the first column delimited sections.
205 * The historic code mentions formfeed characters, but never
206 * implements them. Seems reasonable, do it.
208 if (p[0] == '\014') {
209 if (!--cnt)
210 goto adjust1;
211 continue;
213 if (p[0] != '.' || len < 2)
214 continue;
215 for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
216 if (lp[0] == p[1] &&
217 ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
218 !--cnt) {
219 adjust1: vp->m_stop.lno = lno;
220 vp->m_stop.cno = 0;
221 goto ret1;
226 * If moving backward, reached SOF, which is a movement sink.
227 * We already checked for starting there.
229 vp->m_stop.lno = 1;
230 vp->m_stop.cno = 0;
233 * All commands move to the end of the range.
235 * !!!
236 * Historic practice is the section cut was in line mode if it started
237 * from column 0 and was in the backward direction. Otherwise, left
238 * motion commands adjust the starting point to the character before
239 * the current one. What makes this worse is that if it cut to line
240 * mode it also went to the first non-<blank>.
242 ret1: if (vp->m_start.cno == 0) {
243 F_CLR(vp, VM_RCM_MASK);
244 F_SET(vp, VM_RCM_SETFNB);
246 --vp->m_start.lno;
247 F_SET(vp, VM_LMODE);
248 } else
249 --vp->m_start.cno;
251 vp->m_final = vp->m_stop;
252 return (0);