add NEXINIT as alternative to EXINIT
[nvi.git] / vi / v_section.c
blobc20d8c052a1e9e6a005293b7e4f7b39e20355341
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: v_section.c,v 8.3 1993/08/31 17:15:26 bostic Exp $ (Berkeley) $Date: 1993/08/31 17:15:26 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <string.h>
16 #include "vi.h"
17 #include "vcmd.h"
20 * In historic vi, the section commands ignored empty lines, unlike the
21 * paragraph commands, which was probably okay. However, they also moved
22 * to the start of the last line when there where no more sections instead
23 * of the end of the last line like the paragraph commands. I've changed
24 * the latter behaviore to match the paragraphs command.
26 * In historic vi, a "function" was defined as the first character of the
27 * line being an open brace, which could be followed by anything. This
28 * implementation follows that historic practice.
31 /* Macro to do a check on each line. */
32 #define CHECK { \
33 if (len == 0) \
34 continue; \
35 if (p[0] == '{') { \
36 if (!--cnt) { \
37 rp->cno = 0; \
38 rp->lno = lno; \
39 return (0); \
40 } \
41 continue; \
42 } \
43 if (p[0] != '.' || len < 3) \
44 continue; \
45 for (lp = list; *lp; lp += 2) \
46 if (lp[0] == p[1] && \
47 (lp[1] == ' ' || lp[1] == p[2]) && !--cnt) { \
48 rp->cno = 0; \
49 rp->lno = lno; \
50 return (0); \
51 } \
55 * v_sectionf -- [count]]]
56 * Move forward count sections/functions.
58 int
59 v_sectionf(sp, ep, vp, fm, tm, rp)
60 SCR *sp;
61 EXF *ep;
62 VICMDARG *vp;
63 MARK *fm, *tm, *rp;
65 size_t len;
66 recno_t cnt, lno;
67 char *p, *list, *lp;
69 /* Get macro list. */
70 if ((list = O_STR(sp, O_SECTIONS)) == NULL)
71 return (1);
73 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
74 for (lno = fm->lno; (p = file_gline(sp, ep, ++lno, &len)) != NULL;)
75 CHECK;
77 /* EOF is a movement sink. */
78 if (fm->lno != lno - 1) {
79 rp->lno = lno - 1;
80 rp->cno = len ? len - 1 : 0;
81 return (0);
83 v_eof(sp, ep, NULL);
84 return (1);
88 * v_sectionb -- [count][[
89 * Move backward count sections/functions.
91 int
92 v_sectionb(sp, ep, vp, fm, tm, rp)
93 SCR *sp;
94 EXF *ep;
95 VICMDARG *vp;
96 MARK *fm, *tm, *rp;
98 size_t len;
99 recno_t cnt, lno;
100 char *p, *list, *lp;
102 /* Check for SOF. */
103 if (fm->lno <= 1) {
104 v_sof(sp, NULL);
105 return (1);
108 /* Get macro list. */
109 if ((list = O_STR(sp, O_SECTIONS)) == NULL)
110 return (1);
112 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
113 for (lno = fm->lno; (p = file_gline(sp, ep, --lno, &len)) != NULL;)
114 CHECK;
116 /* SOF is a movement sink. */
117 rp->lno = 1;
118 return (0);