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