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