text input: fix problem with autoindenting and ^^D
[nvi.git] / ex / ex_z.c
blobfe941a4b21bf14c7e59bbf412848094df429693e
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_z.c,v 10.12 2001/06/25 15:19:22 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:22 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "../common/common.h"
28 * ex_z -- :[line] z [^-.+=] [count] [flags]
29 * Adjust window.
31 * PUBLIC: int ex_z __P((SCR *, EXCMD *));
33 int
34 ex_z(SCR *sp, EXCMD *cmdp)
36 MARK abs;
37 db_recno_t cnt, equals, lno;
38 int eofcheck;
40 NEEDFILE(sp, cmdp);
43 * !!!
44 * If no count specified, use either two times the size of the
45 * scrolling region, or the size of the window option. POSIX
46 * 1003.2 claims that the latter is correct, but historic ex/vi
47 * documentation and practice appear to use the scrolling region.
48 * I'm using the window size as it means that the entire screen
49 * is used instead of losing a line to roundoff. Note, we drop
50 * a line from the cnt if using the window size to leave room for
51 * the next ex prompt.
53 if (FL_ISSET(cmdp->iflags, E_C_COUNT))
54 cnt = cmdp->count;
55 else
56 #ifdef HISTORIC_PRACTICE
57 cnt = O_VAL(sp, O_SCROLL) * 2;
58 #else
59 cnt = O_VAL(sp, O_WINDOW) - 1;
60 #endif
62 equals = 0;
63 eofcheck = 0;
64 lno = cmdp->addr1.lno;
66 switch (FL_ISSET(cmdp->iflags,
67 E_C_CARAT | E_C_DASH | E_C_DOT | E_C_EQUAL | E_C_PLUS)) {
68 case E_C_CARAT: /* Display cnt * 2 before the line. */
69 eofcheck = 1;
70 if (lno > cnt * 2)
71 cmdp->addr1.lno = (lno - cnt * 2) + 1;
72 else
73 cmdp->addr1.lno = 1;
74 cmdp->addr2.lno = (cmdp->addr1.lno + cnt) - 1;
75 break;
76 case E_C_DASH: /* Line goes at the bottom of the screen. */
77 cmdp->addr1.lno = lno > cnt ? (lno - cnt) + 1 : 1;
78 cmdp->addr2.lno = lno;
79 break;
80 case E_C_DOT: /* Line goes in the middle of the screen. */
82 * !!!
83 * Historically, the "middleness" of the line overrode the
84 * count, so that "3z.19" or "3z.20" would display the first
85 * 12 lines of the file, i.e. (N - 1) / 2 lines before and
86 * after the specified line.
88 eofcheck = 1;
89 cnt = (cnt - 1) / 2;
90 cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
91 cmdp->addr2.lno = lno + cnt;
94 * !!!
95 * Historically, z. set the absolute cursor mark.
97 abs.lno = sp->lno;
98 abs.cno = sp->cno;
99 (void)mark_set(sp, ABSMARK1, &abs, 1);
100 break;
101 case E_C_EQUAL: /* Center with hyphens. */
103 * !!!
104 * Strangeness. The '=' flag is like the '.' flag (see the
105 * above comment, it applies here as well) but with a special
106 * little hack. Print out lines of hyphens before and after
107 * the specified line. Additionally, the cursor remains set
108 * on that line.
110 eofcheck = 1;
111 cnt = (cnt - 1) / 2;
112 cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
113 cmdp->addr2.lno = lno - 1;
114 if (ex_pr(sp, cmdp))
115 return (1);
116 (void)ex_puts(sp, "----------------------------------------\n");
117 cmdp->addr2.lno = cmdp->addr1.lno = equals = lno;
118 if (ex_pr(sp, cmdp))
119 return (1);
120 (void)ex_puts(sp, "----------------------------------------\n");
121 cmdp->addr1.lno = lno + 1;
122 cmdp->addr2.lno = (lno + cnt) - 1;
123 break;
124 default:
125 /* If no line specified, move to the next one. */
126 if (F_ISSET(cmdp, E_ADDR_DEF))
127 ++lno;
128 /* FALLTHROUGH */
129 case E_C_PLUS: /* Line goes at the top of the screen. */
130 eofcheck = 1;
131 cmdp->addr1.lno = lno;
132 cmdp->addr2.lno = (lno + cnt) - 1;
133 break;
136 if (eofcheck) {
137 if (db_last(sp, &lno))
138 return (1);
139 if (cmdp->addr2.lno > lno)
140 cmdp->addr2.lno = lno;
143 if (ex_pr(sp, cmdp))
144 return (1);
145 if (equals)
146 sp->lno = equals;
147 return (0);