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