]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/nvi/vi/v_section.c
Update nvi to 2.2.0
[FreeBSD/FreeBSD.git] / contrib / nvi / vi / v_section.c
1 /*-
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15
16 #include <bitstring.h>
17 #include <limits.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "../common/common.h"
22 #include "vi.h"
23
24 /*
25  * !!!
26  * In historic vi, the section commands ignored empty lines, unlike the
27  * paragraph commands, which was probably okay.  However, they also moved
28  * to the start of the last line when there where no more sections instead
29  * of the end of the last line like the paragraph commands.  I've changed
30  * the latter behavior to match the paragraph commands.
31  *
32  * In historic vi, a section was defined as the first character(s) of the
33  * line matching, which could be followed by anything.  This implementation
34  * follows that historic practice.
35  *
36  * !!!
37  * The historic vi documentation (USD:15-10) claimed:
38  *      The section commands interpret a preceding count as a different
39  *      window size in which to redraw the screen at the new location,
40  *      and this window size is the base size for newly drawn windows
41  *      until another size is specified.  This is very useful if you are
42  *      on a slow terminal ...
43  *
44  * I can't get the 4BSD vi to do this, it just beeps at me.  For now, a
45  * count to the section commands simply repeats the command.
46  */
47
48 /*
49  * v_sectionf -- [count]]]
50  *      Move forward count sections/functions.
51  *
52  * !!!
53  * Using ]] as a motion command was a bit special, historically.  It could
54  * match } as well as the usual { and section values.  If it matched a { or
55  * a section, it did NOT include the matched line.  If it matched a }, it
56  * did include the line.  No clue why.
57  *
58  * PUBLIC: int v_sectionf(SCR *, VICMD *);
59  */
60 int
61 v_sectionf(SCR *sp, VICMD *vp)
62 {
63         recno_t cnt, lno;
64         size_t len;
65         CHAR_T *p;
66         char *list, *lp;
67
68         /* Get the macro list. */
69         if ((list = O_STR(sp, O_SECTIONS)) == NULL)
70                 return (1);
71
72         /*
73          * !!!
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
78          * or end of a line.
79          */
80         if (ISMOTION(vp))
81                 if (vp->m_start.cno == 0)
82                         F_SET(vp, VM_LMODE);
83                 else {
84                         vp->m_stop = vp->m_start;
85                         vp->m_stop.cno = 0;
86                         if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
87                                 return (1);
88                         if (vp->m_start.cno <= vp->m_stop.cno)
89                                 F_SET(vp, VM_LMODE);
90                 }
91
92         cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
93         for (lno = vp->m_start.lno; !db_get(sp, ++lno, 0, &p, &len);) {
94                 if (len == 0)
95                         continue;
96                 if (p[0] == '{' || (ISMOTION(vp) && p[0] == '}')) {
97                         if (!--cnt) {
98                                 if (p[0] == '{')
99                                         goto adjust1;
100                                 goto adjust2;
101                         }
102                         continue;
103                 }
104                 /*
105                  * !!!
106                  * Historic documentation (USD:15-11, 4.2) said that formfeed
107                  * characters (^L) in the first column delimited sections.
108                  * The historic code mentions formfeed characters, but never
109                  * implements them.  Seems reasonable, do it.
110                  */
111                 if (p[0] == '\014') {
112                         if (!--cnt)
113                                 goto adjust1;
114                         continue;
115                 }
116                 if (p[0] != '.' || len < 2)
117                         continue;
118                 for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
119                         if (lp[0] == p[1] &&
120                             ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
121                             !--cnt) {
122                                 /*
123                                  * !!!
124                                  * If not cutting this line, adjust to the end
125                                  * of the previous one.  Otherwise, position to
126                                  * column 0.
127                                  */
128 adjust1:                        if (ISMOTION(vp))
129                                         goto ret1;
130
131 adjust2:                        vp->m_stop.lno = lno;
132                                 vp->m_stop.cno = 0;
133                                 goto ret2;
134                         }
135         }
136
137         /* If moving forward, reached EOF, check to see if we started there. */
138         if (vp->m_start.lno == lno - 1) {
139                 v_eof(sp, NULL);
140                 return (1);
141         }
142
143 ret1:   if (db_get(sp, --lno, DBG_FATAL, NULL, &len))
144                 return (1);
145         vp->m_stop.lno = lno;
146         vp->m_stop.cno = len ? len - 1 : 0;
147
148         /*
149          * Non-motion commands go to the end of the range.  Delete and
150          * yank stay at the start of the range.  Ignore others.
151          */
152 ret2:   if (ISMOTION(vp)) {
153                 vp->m_final = vp->m_start;
154                 if (F_ISSET(vp, VM_LMODE))
155                         vp->m_final.cno = 0;
156         } else
157                 vp->m_final = vp->m_stop;
158         return (0);
159 }
160
161 /*
162  * v_sectionb -- [count][[
163  *      Move backward count sections/functions.
164  *
165  * PUBLIC: int v_sectionb(SCR *, VICMD *);
166  */
167 int
168 v_sectionb(SCR *sp, VICMD *vp)
169 {
170         size_t len;
171         recno_t cnt, lno;
172         CHAR_T *p;
173         char *list, *lp;
174
175         /* An empty file or starting from line 1 is always illegal. */
176         if (vp->m_start.lno <= 1) {
177                 v_sof(sp, NULL);
178                 return (1);
179         }
180
181         /* Get the macro list. */
182         if ((list = O_STR(sp, O_SECTIONS)) == NULL)
183                 return (1);
184
185         cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
186         for (lno = vp->m_start.lno; !db_get(sp, --lno, 0, &p, &len);) {
187                 if (len == 0)
188                         continue;
189                 if (p[0] == '{') {
190                         if (!--cnt)
191                                 goto adjust1;
192                         continue;
193                 }
194                 /*
195                  * !!!
196                  * Historic documentation (USD:15-11, 4.2) said that formfeed
197                  * characters (^L) in the first column delimited sections.
198                  * The historic code mentions formfeed characters, but never
199                  * implements them.  Seems reasonable, do it.
200                  */
201                 if (p[0] == '\014') {
202                         if (!--cnt)
203                                 goto adjust1;
204                         continue;
205                 }
206                 if (p[0] != '.' || len < 2)
207                         continue;
208                 for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
209                         if (lp[0] == p[1] &&
210                             ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
211                             !--cnt) {
212 adjust1:                        vp->m_stop.lno = lno;
213                                 vp->m_stop.cno = 0;
214                                 goto ret1;
215                         }
216         }
217
218         /*
219          * If moving backward, reached SOF, which is a movement sink.
220          * We already checked for starting there.
221          */
222         vp->m_stop.lno = 1;
223         vp->m_stop.cno = 0;
224
225         /*
226          * All commands move to the end of the range.
227          *
228          * !!!
229          * Historic practice is the section cut was in line mode if it started
230          * from column 0 and was in the backward direction.  Otherwise, left
231          * motion commands adjust the starting point to the character before
232          * the current one.  What makes this worse is that if it cut to line
233          * mode it also went to the first non-<blank>.
234          */
235 ret1:   if (vp->m_start.cno == 0) {
236                 F_CLR(vp, VM_RCM_MASK);
237                 F_SET(vp, VM_RCM_SETFNB);
238
239                 --vp->m_start.lno;
240                 F_SET(vp, VM_LMODE);
241         } else
242                 --vp->m_start.cno;
243
244         vp->m_final = vp->m_stop;
245         return (0);
246 }