xref: /netbsd-src/external/bsd/nvi/dist/ex/ex_visual.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: ex_visual.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #ifndef lint
14 static const char sccsid[] = "Id: ex_visual.c,v 10.16 2001/08/29 11:04:13 skimo Exp  (Berkeley) Date: 2001/08/29 11:04:13 ";
15 #endif /* not lint */
16 
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
20 
21 #include <bitstring.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "../common/common.h"
29 #include "../vi/vi.h"
30 
31 /*
32  * ex_visual -- :[line] vi[sual] [^-.+] [window_size] [flags]
33  *	Switch to visual mode.
34  *
35  * PUBLIC: int ex_visual __P((SCR *, EXCMD *));
36  */
37 int
38 ex_visual(SCR *sp, EXCMD *cmdp)
39 {
40 	SCR *tsp;
41 	size_t len;
42 	int pos;
43 	char buf[256];
44 	size_t wlen;
45 	const CHAR_T *wp;
46 
47 	/* If open option off, disallow visual command. */
48 	if (!O_ISSET(sp, O_OPEN)) {
49 		msgq(sp, M_ERR,
50 	    "175|The visual command requires that the open option be set");
51 		return (1);
52 	}
53 
54 	/* Move to the address. */
55 	sp->lno = cmdp->addr1.lno == 0 ? 1 : cmdp->addr1.lno;
56 
57 	/*
58 	 * Push a command based on the line position flags.  If no
59 	 * flag specified, the line goes at the top of the screen.
60 	 */
61 	switch (FL_ISSET(cmdp->iflags,
62 	    E_C_CARAT | E_C_DASH | E_C_DOT | E_C_PLUS)) {
63 	case E_C_CARAT:
64 		pos = '^';
65 		break;
66 	case E_C_DASH:
67 		pos = '-';
68 		break;
69 	case E_C_DOT:
70 		pos = '.';
71 		break;
72 	case E_C_PLUS:
73 		pos = '+';
74 		break;
75 	default:
76 		sp->frp->lno = sp->lno;
77 		sp->frp->cno = 0;
78 		(void)nonblank(sp, sp->lno, &sp->cno);
79 		F_SET(sp->frp, FR_CURSORSET);
80 		goto nopush;
81 	}
82 
83 	if (FL_ISSET(cmdp->iflags, E_C_COUNT))
84 		len = snprintf(buf, sizeof(buf),
85 		    "%luz%c%lu", (unsigned long)sp->lno, pos, cmdp->count);
86 	else
87 		len = snprintf(buf, sizeof(buf),
88 		    "%luz%c", (unsigned long)sp->lno, pos);
89 	CHAR2INT(sp, buf, len, wp, wlen);
90 	(void)v_event_push(sp, NULL, wp, wlen, CH_NOMAP | CH_QUOTED);
91 
92 	/*
93 	 * !!!
94 	 * Historically, if no line address was specified, the [p#l] flags
95 	 * caused the cursor to be moved to the last line of the file, which
96 	 * was then positioned as described above.  This seems useless, so
97 	 * I haven't implemented it.
98 	 */
99 	switch (FL_ISSET(cmdp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT)) {
100 	case E_C_HASH:
101 		O_SET(sp, O_NUMBER);
102 		break;
103 	case E_C_LIST:
104 		O_SET(sp, O_LIST);
105 		break;
106 	case E_C_PRINT:
107 		break;
108 	}
109 
110 nopush:	/*
111 	 * !!!
112 	 * You can call the visual part of the editor from within an ex
113 	 * global command.
114 	 *
115 	 * XXX
116 	 * Historically, undoing a visual session was a single undo command,
117 	 * i.e. you could undo all of the changes you made in visual mode.
118 	 * We don't get this right; I'm waiting for the new logging code to
119 	 * be available.
120 	 *
121 	 * It's explicit, don't have to wait for the user, unless there's
122 	 * already a reason to wait.
123 	 */
124 	if (!F_ISSET(sp, SC_SCR_EXWROTE))
125 		F_SET(sp, SC_EX_WAIT_NO);
126 
127 	if (F_ISSET(sp, SC_EX_GLOBAL)) {
128 		/*
129 		 * When the vi screen(s) exit, we don't want to lose our hold
130 		 * on this screen or this file, otherwise we're going to fail
131 		 * fairly spectacularly.
132 		 */
133 		++sp->refcnt;
134 		++sp->ep->refcnt;
135 		/* XXXX where is this decremented ? */
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