v_ex.c revision 208612
1207753Smm/*-
2207753Smm * Copyright (c) 1992, 1993, 1994
3207753Smm *	The Regents of the University of California.  All rights reserved.
4207753Smm * Copyright (c) 1992, 1993, 1994, 1995, 1996
5207753Smm *	Keith Bostic.  All rights reserved.
6207753Smm *
7207753Smm * See the LICENSE file for redistribution information.
8207753Smm */
9207753Smm
10207753Smm#include "config.h"
11207753Smm
12207753Smm#ifndef lint
13207753Smmstatic const char sccsid[] = "@(#)v_ex.c	10.42 (Berkeley) 6/28/96";
14207753Smm#endif /* not lint */
15207753Smm
16207753Smm#include <sys/types.h>
17207753Smm#include <sys/queue.h>
18278433Srpaulo#include <sys/time.h>
19207753Smm
20207753Smm#include <bitstring.h>
21207753Smm#include <limits.h>
22207753Smm#include <stdio.h>
23207753Smm#include <stdlib.h>
24207753Smm#include <string.h>
25207753Smm#include <unistd.h>
26207753Smm
27207753Smm#include "../common/common.h"
28207753Smm#include "vi.h"
29207753Smm
30207753Smmstatic int v_ecl __P((SCR *));
31207753Smmstatic int v_ecl_init __P((SCR *));
32207753Smmstatic int v_ecl_log __P((SCR *, TEXT *));
33207753Smmstatic int v_ex_done __P((SCR *, VICMD *));
34207753Smmstatic int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
35207753Smm
36207753Smm/*
37207753Smm * v_again -- &
38207753Smm *	Repeat the previous substitution.
39207753Smm *
40207753Smm * PUBLIC: int v_again __P((SCR *, VICMD *));
41207753Smm */
42207753Smmint
43207753Smmv_again(sp, vp)
44207753Smm	SCR *sp;
45207753Smm	VICMD *vp;
46207753Smm{
47207753Smm	ARGS *ap[2], a;
48207753Smm	EXCMD cmd;
49207753Smm
50207753Smm	ex_cinit(&cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1, ap);
51207753Smm	ex_cadd(&cmd, &a, "", 1);
52207753Smm
53207753Smm	return (v_exec_ex(sp, vp, &cmd));
54207753Smm}
55207753Smm
56207753Smm/*
57207753Smm * v_exmode -- Q
58207753Smm *	Switch the editor into EX mode.
59207753Smm *
60207753Smm * PUBLIC: int v_exmode __P((SCR *, VICMD *));
61207753Smm */
62207753Smmint
63207753Smmv_exmode(sp, vp)
64207753Smm	SCR *sp;
65207753Smm	VICMD *vp;
66207753Smm{
67207753Smm	GS *gp;
68207753Smm
69207753Smm	gp = sp->gp;
70207753Smm
71207753Smm	/* Try and switch screens -- the screen may not permit it. */
72207753Smm	if (gp->scr_screen(sp, SC_EX)) {
73207753Smm		msgq(sp, M_ERR,
74207753Smm		    "207|The Q command requires the ex terminal interface");
75207753Smm		return (1);
76207753Smm	}
77207753Smm	(void)gp->scr_attr(sp, SA_ALTERNATE, 0);
78207753Smm
79207753Smm	/* Save the current cursor position. */
80207753Smm	sp->frp->lno = sp->lno;
81207753Smm	sp->frp->cno = sp->cno;
82207753Smm	F_SET(sp->frp, FR_CURSORSET);
83207753Smm
84207753Smm	/* Switch to ex mode. */
85207753Smm	F_CLR(sp, SC_VI | SC_SCR_VI);
86207753Smm	F_SET(sp, SC_EX);
87207753Smm
88207753Smm	/* Move out of the vi screen. */
89207753Smm	(void)ex_puts(sp, "\n");
90207753Smm
91207753Smm	return (0);
92}
93
94/*
95 * v_join -- [count]J
96 *	Join lines together.
97 *
98 * PUBLIC: int v_join __P((SCR *, VICMD *));
99 */
100int
101v_join(sp, vp)
102	SCR *sp;
103	VICMD *vp;
104{
105	EXCMD cmd;
106	int lno;
107
108	/*
109	 * YASC.
110	 * The general rule is that '#J' joins # lines, counting the current
111	 * line.  However, 'J' and '1J' are the same as '2J', i.e. join the
112	 * current and next lines.  This doesn't map well into the ex command
113	 * (which takes two line numbers), so we handle it here.  Note that
114	 * we never test for EOF -- historically going past the end of file
115	 * worked just fine.
116	 */
117	lno = vp->m_start.lno + 1;
118	if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
119		lno = vp->m_start.lno + (vp->count - 1);
120
121	ex_cinit(&cmd, C_JOIN, 2, vp->m_start.lno, lno, 0, NULL);
122	return (v_exec_ex(sp, vp, &cmd));
123}
124
125/*
126 * v_shiftl -- [count]<motion
127 *	Shift lines left.
128 *
129 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
130 */
131int
132v_shiftl(sp, vp)
133	SCR *sp;
134	VICMD *vp;
135{
136	ARGS *ap[2], a;
137	EXCMD cmd;
138
139	ex_cinit(&cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0, ap);
140	ex_cadd(&cmd, &a, "<", 1);
141	return (v_exec_ex(sp, vp, &cmd));
142}
143
144/*
145 * v_shiftr -- [count]>motion
146 *	Shift lines right.
147 *
148 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
149 */
150int
151v_shiftr(sp, vp)
152	SCR *sp;
153	VICMD *vp;
154{
155	ARGS *ap[2], a;
156	EXCMD cmd;
157
158	ex_cinit(&cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0, ap);
159	ex_cadd(&cmd, &a, ">", 1);
160	return (v_exec_ex(sp, vp, &cmd));
161}
162
163/*
164 * v_suspend -- ^Z
165 *	Suspend vi.
166 *
167 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
168 */
169int
170v_suspend(sp, vp)
171	SCR *sp;
172	VICMD *vp;
173{
174	ARGS *ap[2], a;
175	EXCMD cmd;
176
177	ex_cinit(&cmd, C_STOP, 0, OOBLNO, OOBLNO, 0, ap);
178	ex_cadd(&cmd, &a, "suspend", sizeof("suspend") - 1);
179	return (v_exec_ex(sp, vp, &cmd));
180}
181
182/*
183 * v_switch -- ^^
184 *	Switch to the previous file.
185 *
186 * PUBLIC: int v_switch __P((SCR *, VICMD *));
187 */
188int
189v_switch(sp, vp)
190	SCR *sp;
191	VICMD *vp;
192{
193	ARGS *ap[2], a;
194	EXCMD cmd;
195	char *name;
196
197	/*
198	 * Try the alternate file name, then the previous file
199	 * name.  Use the real name, not the user's current name.
200	 */
201	if ((name = sp->alt_name) == NULL) {
202		msgq(sp, M_ERR, "180|No previous file to edit");
203		return (1);
204	}
205
206	/* If autowrite is set, write out the file. */
207	if (file_m1(sp, 0, FS_ALL))
208		return (1);
209
210	ex_cinit(&cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0, ap);
211	ex_cadd(&cmd, &a, name, strlen(name));
212	return (v_exec_ex(sp, vp, &cmd));
213}
214
215/*
216 * v_tagpush -- ^[
217 *	Do a tag search on the cursor keyword.
218 *
219 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
220 */
221int
222v_tagpush(sp, vp)
223	SCR *sp;
224	VICMD *vp;
225{
226	ARGS *ap[2], a;
227	EXCMD cmd;
228
229#ifdef GTAGS
230	if (O_ISSET(sp, O_GTAGSMODE) && vp->m_start.cno == 0)
231		ex_cinit(&cmd, C_RTAG, 0, OOBLNO, 0, 0, ap);
232	else
233#endif
234	ex_cinit(&cmd, C_TAG, 0, OOBLNO, 0, 0, ap);
235	ex_cadd(&cmd, &a, VIP(sp)->keyw, strlen(VIP(sp)->keyw));
236	return (v_exec_ex(sp, vp, &cmd));
237}
238
239/*
240 * v_tagpop -- ^T
241 *	Pop the tags stack.
242 *
243 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
244 */
245int
246v_tagpop(sp, vp)
247	SCR *sp;
248	VICMD *vp;
249{
250	EXCMD cmd;
251
252	ex_cinit(&cmd, C_TAGPOP, 0, OOBLNO, 0, 0, NULL);
253	return (v_exec_ex(sp, vp, &cmd));
254}
255
256/*
257 * v_filter -- [count]!motion command(s)
258 *	Run range through shell commands, replacing text.
259 *
260 * PUBLIC: int v_filter __P((SCR *, VICMD *));
261 */
262int
263v_filter(sp, vp)
264	SCR *sp;
265	VICMD *vp;
266{
267	EXCMD cmd;
268	TEXT *tp;
269
270	/*
271	 * !!!
272	 * Historical vi permitted "!!" in an empty file, and it's handled
273	 * as a special case in the ex_bang routine.  Don't modify this setup
274	 * without understanding that one.  In particular, note that we're
275	 * manipulating the ex argument structures behind ex's back.
276	 *
277	 * !!!
278	 * Historical vi did not permit the '!' command to be associated with
279	 * a non-line oriented motion command, in general, although it did
280	 * with search commands.  So, !f; and !w would fail, but !/;<CR>
281	 * would succeed, even if they all moved to the same location in the
282	 * current line.  I don't see any reason to disallow '!' using any of
283	 * the possible motion commands.
284	 *
285	 * !!!
286	 * Historical vi ran the last bang command if N or n was used as the
287	 * search motion.
288	 */
289	if (F_ISSET(vp, VC_ISDOT) ||
290	    ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
291		ex_cinit(&cmd, C_BANG,
292		    2, vp->m_start.lno, vp->m_stop.lno, 0, NULL);
293		EXP(sp)->argsoff = 0;			/* XXX */
294
295		if (argv_exp1(sp, &cmd, "!", 1, 1))
296			return (1);
297		cmd.argc = EXP(sp)->argsoff;		/* XXX */
298		cmd.argv = EXP(sp)->args;		/* XXX */
299		return (v_exec_ex(sp, vp, &cmd));
300	}
301
302	/* Get the command from the user. */
303	if (v_tcmd(sp, vp,
304	    '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
305		return (1);
306
307	/*
308	 * Check to see if the user changed their mind.
309	 *
310	 * !!!
311	 * Entering <escape> on an empty line was historically an error,
312	 * this implementation doesn't bother.
313	 */
314	tp = sp->tiq.cqh_first;
315	if (tp->term != TERM_OK) {
316		vp->m_final.lno = sp->lno;
317		vp->m_final.cno = sp->cno;
318		return (0);
319	}
320
321	/* Home the cursor. */
322	vs_home(sp);
323
324	ex_cinit(&cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0, NULL);
325	EXP(sp)->argsoff = 0;			/* XXX */
326
327	if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
328		return (1);
329	cmd.argc = EXP(sp)->argsoff;		/* XXX */
330	cmd.argv = EXP(sp)->args;		/* XXX */
331	return (v_exec_ex(sp, vp, &cmd));
332}
333
334/*
335 * v_event_exec --
336 *	Execute some command(s) based on an event.
337 *
338 * PUBLIC: int v_event_exec __P((SCR *, VICMD *));
339 */
340int
341v_event_exec(sp, vp)
342	SCR *sp;
343	VICMD *vp;
344{
345	EXCMD cmd;
346
347	switch (vp->ev.e_event) {
348	case E_QUIT:
349		ex_cinit(&cmd, C_QUIT, 0, OOBLNO, OOBLNO, 0, NULL);
350		break;
351	case E_WRITE:
352		ex_cinit(&cmd, C_WRITE, 0, OOBLNO, OOBLNO, 0, NULL);
353		break;
354	default:
355		abort();
356	}
357	return (v_exec_ex(sp, vp, &cmd));
358}
359
360/*
361 * v_exec_ex --
362 *	Execute an ex command.
363 */
364static int
365v_exec_ex(sp, vp, exp)
366	SCR *sp;
367	VICMD *vp;
368	EXCMD *exp;
369{
370	int rval;
371
372	rval = exp->cmd->fn(sp, exp);
373	return (v_ex_done(sp, vp) || rval);
374}
375
376/*
377 * v_ex -- :
378 *	Execute a colon command line.
379 *
380 * PUBLIC: int v_ex __P((SCR *, VICMD *));
381 */
382int
383v_ex(sp, vp)
384	SCR *sp;
385	VICMD *vp;
386{
387	GS *gp;
388	TEXT *tp;
389	int do_cedit, do_resolution, ifcontinue;
390
391	gp = sp->gp;
392
393	/*
394	 * !!!
395	 * If we put out more than a single line of messages, or ex trashes
396	 * the screen, the user may continue entering ex commands.  We find
397	 * this out when we do the screen/message resolution.  We can't enter
398	 * completely into ex mode however, because the user can elect to
399	 * return into vi mode by entering any key, i.e. we have to be in raw
400	 * mode.
401	 */
402	for (do_cedit = do_resolution = 0;;) {
403		/*
404		 * !!!
405		 * There may already be an ex command waiting to run.  If
406		 * so, we continue with it.
407		 */
408		if (!EXCMD_RUNNING(gp)) {
409			/* Get a command. */
410			if (v_tcmd(sp, vp, ':',
411			    TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
412				return (1);
413			tp = sp->tiq.cqh_first;
414
415			/*
416			 * If the user entered a single <esc>, they want to
417			 * edit their colon command history.  If they already
418			 * entered some text, move it into the edit history.
419			 */
420			if (tp->term == TERM_CEDIT) {
421				if (tp->len > 1 && v_ecl_log(sp, tp))
422					return (1);
423				do_cedit = 1;
424				break;
425			}
426
427			/* If the user didn't enter anything, return. */
428			if (tp->term == TERM_BS)
429				break;
430
431			/* If the user changed their mind, return. */
432			if (tp->term != TERM_OK)
433				break;
434
435			/* Log the command. */
436			if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
437				return (1);
438
439			/* Push a command on the command stack. */
440			if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
441				return (1);
442		}
443
444		/* Home the cursor. */
445		vs_home(sp);
446
447		/*
448		 * !!!
449		 * If the editor wrote the screen behind curses back, put out
450		 * a <newline> so that we don't overwrite the user's command
451		 * with its output or the next want-to-continue? message.  This
452		 * doesn't belong here, but I can't find another place to put
453		 * it.  See, we resolved the output from the last ex command,
454		 * and the user entered another one.  This is the only place
455		 * where we have control before the ex command writes output.
456		 * We could get control in vs_msg(), but we have no way to know
457		 * if command didn't put out any output when we try and resolve
458		 * this command.  This fixes a bug where combinations of ex
459		 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
460		 */
461		if (F_ISSET(sp, SC_SCR_EXWROTE))
462			(void)putchar('\n');
463
464		/* Call the ex parser. */
465		(void)ex_cmd(sp);
466
467		/* Flush ex messages. */
468		(void)ex_fflush(sp);
469
470		/* Resolve any messages. */
471		if (vs_ex_resolve(sp, &ifcontinue))
472			return (1);
473
474		/*
475		 * Continue or return.  If continuing, make sure that we
476		 * eventually do resolution.
477		 */
478		if (!ifcontinue)
479			break;
480		do_resolution = 1;
481
482		/* If we're continuing, it's a new command. */
483		++sp->ccnt;
484	}
485
486	/*
487	 * If the user previously continued an ex command, we have to do
488	 * resolution to clean up the screen.  Don't wait, we already did
489	 * that.
490	 */
491	if (do_resolution) {
492		F_SET(sp, SC_EX_WAIT_NO);
493		if (vs_ex_resolve(sp, &ifcontinue))
494			return (1);
495	}
496
497	/* Cleanup from the ex command. */
498	if (v_ex_done(sp, vp))
499		return (1);
500
501	/* The user may want to edit their colon command history. */
502	if (do_cedit)
503		return (v_ecl(sp));
504
505	return (0);
506}
507
508/*
509 * v_ex_done --
510 *	Cleanup from an ex command.
511 */
512static int
513v_ex_done(sp, vp)
514	SCR *sp;
515	VICMD *vp;
516{
517	size_t len;
518
519	/*
520	 * The only cursor modifications are real, however, the underlying
521	 * line may have changed; don't trust anything.  This code has been
522	 * a remarkably fertile place for bugs.  Do a reality check on a
523	 * cursor value, and make sure it's okay.  If necessary, change it.
524	 * Ex keeps track of the line number, but it cares less about the
525	 * column and it may have disappeared.
526	 *
527	 * Don't trust ANYTHING.
528	 *
529	 * XXX
530	 * Ex will soon have to start handling the column correctly; see
531	 * the POSIX 1003.2 standard.
532	 */
533	if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
534		sp->lno = 1;
535		sp->cno = 0;
536	} else if (sp->cno >= len)
537		sp->cno = len ? len - 1 : 0;
538
539	vp->m_final.lno = sp->lno;
540	vp->m_final.cno = sp->cno;
541
542	/*
543	 * Don't re-adjust the cursor after executing an ex command,
544	 * and ex movements are permanent.
545	 */
546	F_CLR(vp, VM_RCM_MASK);
547	F_SET(vp, VM_RCM_SET);
548
549	return (0);
550}
551
552/*
553 * v_ecl --
554 *	Start an edit window on the colon command-line commands.
555 */
556static int
557v_ecl(sp)
558	SCR *sp;
559{
560	GS *gp;
561	SCR *new;
562
563	/* Initialize the screen, if necessary. */
564	gp = sp->gp;
565	if (gp->ccl_sp == NULL && v_ecl_init(sp))
566		return (1);
567
568	/* Get a new screen. */
569	if (screen_init(gp, sp, &new))
570		return (1);
571	if (vs_split(sp, new, 1)) {
572		(void)screen_end(new);
573		return (1);
574	}
575
576	/* Attach to the screen. */
577	new->ep = gp->ccl_sp->ep;
578	++new->ep->refcnt;
579
580	new->frp = gp->ccl_sp->frp;
581	new->frp->flags = sp->frp->flags;
582
583	/* Move the cursor to the end. */
584	(void)db_last(new, &new->lno);
585	if (new->lno == 0)
586		new->lno = 1;
587
588	/* Remember the originating window. */
589	sp->ccl_parent = sp;
590
591	/* It's a special window. */
592	F_SET(new, SC_COMEDIT);
593
594	/* Set up the switch. */
595	sp->nextdisp = new;
596	F_SET(sp, SC_SSWITCH);
597	return (0);
598}
599
600/*
601 * v_ecl_exec --
602 *	Execute a command from a colon command-line window.
603 *
604 * PUBLIC: int v_ecl_exec __P((SCR *));
605 */
606int
607v_ecl_exec(sp)
608	SCR *sp;
609{
610	size_t len;
611	char *p;
612
613	if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
614		v_emsg(sp, NULL, VIM_EMPTY);
615		return (1);
616	}
617	if (len == 0) {
618		msgq(sp, M_BERR, "307|No ex command to execute");
619		return (1);
620	}
621
622	/* Push the command on the command stack. */
623	if (ex_run_str(sp, NULL, p, len, 0, 0))
624		return (1);
625
626	/* Set up the switch. */
627	sp->nextdisp = sp->ccl_parent;
628	F_SET(sp, SC_EXIT);
629	return (0);
630}
631
632/*
633 * v_ecl_log --
634 *	Log a command into the colon command-line log file.
635 */
636static int
637v_ecl_log(sp, tp)
638	SCR *sp;
639	TEXT *tp;
640{
641	EXF *save_ep;
642	recno_t lno;
643	int rval;
644
645	/* Initialize the screen, if necessary. */
646	if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
647		return (1);
648
649	/*
650	 * Don't log colon command window commands into the colon command
651	 * window...
652	 */
653	if (sp->ep == sp->gp->ccl_sp->ep)
654		return (0);
655
656	/*
657	 * XXX
658	 * Swap the current EXF with the colon command file EXF.  This
659	 * isn't pretty, but too many routines "know" that sp->ep points
660	 * to the current EXF.
661	 */
662	save_ep = sp->ep;
663	sp->ep = sp->gp->ccl_sp->ep;
664	if (db_last(sp, &lno)) {
665		sp->ep = save_ep;
666		return (1);
667	}
668	rval = db_append(sp, 0, lno, tp->lb, tp->len);
669	sp->ep = save_ep;
670	return (rval);
671}
672
673/*
674 * v_ecl_init --
675 *	Initialize the colon command-line log file.
676 */
677static int
678v_ecl_init(sp)
679	SCR *sp;
680{
681	FREF *frp;
682	GS *gp;
683
684	gp = sp->gp;
685
686	/* Get a temporary file. */
687	if ((frp = file_add(sp, NULL)) == NULL)
688		return (1);
689
690	/*
691	 * XXX
692	 * Create a screen -- the file initialization code wants one.
693	 */
694	if (screen_init(gp, sp, &gp->ccl_sp))
695		return (1);
696	if (file_init(gp->ccl_sp, frp, NULL, 0)) {
697		(void)screen_end(gp->ccl_sp);
698		return (1);
699	}
700
701	/* The underlying file isn't recoverable. */
702	F_CLR(gp->ccl_sp->ep, F_RCV_ON);
703
704	return (0);
705}
706