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