xref: /openbsd/usr.bin/vi/ex/ex_visual.c (revision 133306f0)
1 /*	$OpenBSD: ex_visual.c,v 1.5 2001/01/29 01:58:45 niklas Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1992, 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 #ifndef lint
15 static const char sccsid[] = "@(#)ex_visual.c	10.13 (Berkeley) 6/28/96";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
21 
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include "../common/common.h"
30 #include "../vi/vi.h"
31 
32 /*
33  * ex_visual -- :[line] vi[sual] [^-.+] [window_size] [flags]
34  *	Switch to visual mode.
35  *
36  * PUBLIC: int ex_visual __P((SCR *, EXCMD *));
37  */
38 int
39 ex_visual(sp, cmdp)
40 	SCR *sp;
41 	EXCMD *cmdp;
42 {
43 	SCR *tsp;
44 	size_t len;
45 	int pos;
46 	char buf[256];
47 
48 	/* If open option off, disallow visual command. */
49 	if (!O_ISSET(sp, O_OPEN)) {
50 		msgq(sp, M_ERR,
51 	    "175|The visual command requires that the open option be set");
52 		return (1);
53 	}
54 
55 	/* Move to the address. */
56 	sp->lno = cmdp->addr1.lno == 0 ? 1 : cmdp->addr1.lno;
57 
58 	/*
59 	 * Push a command based on the line position flags.  If no
60 	 * flag specified, the line goes at the top of the screen.
61 	 */
62 	switch (FL_ISSET(cmdp->iflags,
63 	    E_C_CARAT | E_C_DASH | E_C_DOT | E_C_PLUS)) {
64 	case E_C_CARAT:
65 		pos = '^';
66 		break;
67 	case E_C_DASH:
68 		pos = '-';
69 		break;
70 	case E_C_DOT:
71 		pos = '.';
72 		break;
73 	case E_C_PLUS:
74 		pos = '+';
75 		break;
76 	default:
77 		sp->frp->lno = sp->lno;
78 		sp->frp->cno = 0;
79 		(void)nonblank(sp, sp->lno, &sp->cno);
80 		F_SET(sp->frp, FR_CURSORSET);
81 		goto nopush;
82 	}
83 
84 	if (FL_ISSET(cmdp->iflags, E_C_COUNT))
85 		len = snprintf(buf, sizeof(buf),
86 		     "%luz%c%lu", sp->lno, pos, cmdp->count);
87 	else
88 		len = snprintf(buf, sizeof(buf), "%luz%c", sp->lno, pos);
89 	if (len >= sizeof(buf))
90 		len = sizeof(buf) - 1;
91 	(void)v_event_push(sp, NULL, buf, len, CH_NOMAP | CH_QUOTED);
92 
93 	/*
94 	 * !!!
95 	 * Historically, if no line address was specified, the [p#l] flags
96 	 * caused the cursor to be moved to the last line of the file, which
97 	 * was then positioned as described above.  This seems useless, so
98 	 * I haven't implemented it.
99 	 */
100 	switch (FL_ISSET(cmdp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT)) {
101 	case E_C_HASH:
102 		O_SET(sp, O_NUMBER);
103 		break;
104 	case E_C_LIST:
105 		O_SET(sp, O_LIST);
106 		break;
107 	case E_C_PRINT:
108 		break;
109 	}
110 
111 nopush:	/*
112 	 * !!!
113 	 * You can call the visual part of the editor from within an ex
114 	 * global command.
115 	 *
116 	 * XXX
117 	 * Historically, undoing a visual session was a single undo command,
118 	 * i.e. you could undo all of the changes you made in visual mode.
119 	 * We don't get this right; I'm waiting for the new logging code to
120 	 * be available.
121 	 *
122 	 * It's explicit, don't have to wait for the user, unless there's
123 	 * already a reason to wait.
124 	 */
125 	if (!F_ISSET(sp, SC_SCR_EXWROTE))
126 		F_SET(sp, SC_EX_WAIT_NO);
127 
128 	if (F_ISSET(sp, SC_EX_GLOBAL)) {
129 		/*
130 		 * When the vi screen(s) exit, we don't want to lose our hold
131 		 * on this screen or this file, otherwise we're going to fail
132 		 * fairly spectacularly.
133 		 */
134 		++sp->refcnt;
135 		++sp->ep->refcnt;
136 
137 		/*
138 		 * Fake up a screen pointer -- vi doesn't get to change our
139 		 * underlying file, regardless.
140 		 */
141 		tsp = sp;
142 		if (vi(&tsp))
143 			return (1);
144 
145 		/*
146 		 * !!!
147 		 * Historically, if the user exited the vi screen(s) using an
148 		 * ex quit command (e.g. :wq, :q) ex/vi exited, it was only if
149 		 * they exited vi using the Q command that ex continued.  Some
150 		 * early versions of nvi continued in ex regardless, but users
151 		 * didn't like the semantic.
152 		 *
153 		 * Reset the screen.
154 		 */
155 		if (ex_init(sp))
156 			return (1);
157 
158 		/* Move out of the vi screen. */
159 		(void)ex_puts(sp, "\n");
160 	} else {
161 		F_CLR(sp, SC_EX | SC_SCR_EX);
162 		F_SET(sp, SC_VI);
163 	}
164 	return (0);
165 }
166