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