2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "@(#)v_ex.c 10.42 (Berkeley) 6/28/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
30 static int v_ecl
__P((SCR
*));
31 static int v_ecl_init
__P((SCR
*));
32 static int v_ecl_log
__P((SCR
*, TEXT
*));
33 static int v_ex_done
__P((SCR
*, VICMD
*));
34 static int v_exec_ex
__P((SCR
*, VICMD
*, EXCMD
*));
38 * Repeat the previous substitution.
40 * PUBLIC: int v_again __P((SCR *, VICMD *));
50 ex_cinit(&cmd
, C_SUBAGAIN
, 2, vp
->m_start
.lno
, vp
->m_start
.lno
, 1, ap
);
51 ex_cadd(&cmd
, &a
, "", 1);
53 return (v_exec_ex(sp
, vp
, &cmd
));
58 * Switch the editor into EX mode.
60 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
71 /* Try and switch screens -- the screen may not permit it. */
72 if (gp
->scr_screen(sp
, SC_EX
)) {
74 "207|The Q command requires the ex terminal interface");
77 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
79 /* Save the current cursor position. */
80 sp
->frp
->lno
= sp
->lno
;
81 sp
->frp
->cno
= sp
->cno
;
82 F_SET(sp
->frp
, FR_CURSORSET
);
84 /* Switch to ex mode. */
85 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
88 /* Move out of the vi screen. */
89 (void)ex_puts(sp
, "\n");
96 * Join lines together.
98 * PUBLIC: int v_join __P((SCR *, VICMD *));
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
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);
121 ex_cinit(&cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0, NULL
);
122 return (v_exec_ex(sp
, vp
, &cmd
));
126 * v_shiftl -- [count]<motion
129 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
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
));
145 * v_shiftr -- [count]>motion
148 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
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
));
167 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
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
));
184 * Switch to the previous file.
186 * PUBLIC: int v_switch __P((SCR *, VICMD *));
198 * Try the alternate file name, then the previous file
199 * name. Use the real name, not the user's current name.
201 if ((name
= sp
->alt_name
) == NULL
) {
202 msgq(sp
, M_ERR
, "180|No previous file to edit");
206 /* If autowrite is set, write out the file. */
207 if (file_m1(sp
, 0, FS_ALL
))
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
));
217 * Do a tag search on the cursor keyword.
219 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
230 if (O_ISSET(sp
, O_GTAGSMODE
) && vp
->m_start
.cno
== 0)
231 ex_cinit(&cmd
, C_RTAG
, 0, OOBLNO
, 0, 0, ap
);
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
));
241 * Pop the tags stack.
243 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
252 ex_cinit(&cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0, NULL
);
253 return (v_exec_ex(sp
, vp
, &cmd
));
257 * v_filter -- [count]!motion command(s)
258 * Run range through shell commands, replacing text.
260 * PUBLIC: int v_filter __P((SCR *, VICMD *));
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.
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.
286 * Historical vi ran the last bang command if N or n was used as the
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 */
295 if (argv_exp1(sp
, &cmd
, "!", 1, 1))
297 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
298 cmd
.argv
= EXP(sp
)->args
; /* XXX */
299 return (v_exec_ex(sp
, vp
, &cmd
));
302 /* Get the command from the user. */
304 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
308 * Check to see if the user changed their mind.
311 * Entering <escape> on an empty line was historically an error,
312 * this implementation doesn't bother.
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
;
321 /* Home the cursor. */
324 ex_cinit(&cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0, NULL
);
325 EXP(sp
)->argsoff
= 0; /* XXX */
327 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
329 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
330 cmd
.argv
= EXP(sp
)->args
; /* XXX */
331 return (v_exec_ex(sp
, vp
, &cmd
));
336 * Execute some command(s) based on an event.
338 * PUBLIC: int v_event_exec __P((SCR *, VICMD *));
347 switch (vp
->ev
.e_event
) {
349 ex_cinit(&cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0, NULL
);
352 ex_cinit(&cmd
, C_WRITE
, 0, OOBLNO
, OOBLNO
, 0, NULL
);
357 return (v_exec_ex(sp
, vp
, &cmd
));
362 * Execute an ex command.
365 v_exec_ex(sp
, vp
, exp
)
372 rval
= exp
->cmd
->fn(sp
, exp
);
373 return (v_ex_done(sp
, vp
) || rval
);
378 * Execute a colon command line.
380 * PUBLIC: int v_ex __P((SCR *, VICMD *));
389 int do_cedit
, do_resolution
, ifcontinue
;
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
402 for (do_cedit
= do_resolution
= 0;;) {
405 * There may already be an ex command waiting to run. If
406 * so, we continue with it.
408 if (!EXCMD_RUNNING(gp
)) {
410 if (v_tcmd(sp
, vp
, ':',
411 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
413 tp
= sp
->tiq
.cqh_first
;
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.
420 if (tp
->term
== TERM_CEDIT
) {
421 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
427 /* If the user didn't enter anything, return. */
428 if (tp
->term
== TERM_BS
)
431 /* Log the command. */
432 if (O_STR(sp
, O_CEDIT
) != NULL
&& v_ecl_log(sp
, tp
))
435 /* Push a command on the command stack. */
436 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
440 /* Home the cursor. */
445 * If the editor wrote the screen behind curses back, put out
446 * a <newline> so that we don't overwrite the user's command
447 * with its output or the next want-to-continue? message. This
448 * doesn't belong here, but I can't find another place to put
449 * it. See, we resolved the output from the last ex command,
450 * and the user entered another one. This is the only place
451 * where we have control before the ex command writes output.
452 * We could get control in vs_msg(), but we have no way to know
453 * if command didn't put out any output when we try and resolve
454 * this command. This fixes a bug where combinations of ex
455 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
457 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
460 /* Call the ex parser. */
463 /* Flush ex messages. */
466 /* Resolve any messages. */
467 if (vs_ex_resolve(sp
, &ifcontinue
))
471 * Continue or return. If continuing, make sure that we
472 * eventually do resolution.
478 /* If we're continuing, it's a new command. */
483 * If the user previously continued an ex command, we have to do
484 * resolution to clean up the screen. Don't wait, we already did
488 F_SET(sp
, SC_EX_WAIT_NO
);
489 if (vs_ex_resolve(sp
, &ifcontinue
))
493 /* Cleanup from the ex command. */
494 if (v_ex_done(sp
, vp
))
497 /* The user may want to edit their colon command history. */
506 * Cleanup from an ex command.
516 * The only cursor modifications are real, however, the underlying
517 * line may have changed; don't trust anything. This code has been
518 * a remarkably fertile place for bugs. Do a reality check on a
519 * cursor value, and make sure it's okay. If necessary, change it.
520 * Ex keeps track of the line number, but it cares less about the
521 * column and it may have disappeared.
523 * Don't trust ANYTHING.
526 * Ex will soon have to start handling the column correctly; see
527 * the POSIX 1003.2 standard.
529 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
532 } else if (sp
->cno
>= len
)
533 sp
->cno
= len
? len
- 1 : 0;
535 vp
->m_final
.lno
= sp
->lno
;
536 vp
->m_final
.cno
= sp
->cno
;
539 * Don't re-adjust the cursor after executing an ex command,
540 * and ex movements are permanent.
542 F_CLR(vp
, VM_RCM_MASK
);
543 F_SET(vp
, VM_RCM_SET
);
550 * Start an edit window on the colon command-line commands.
559 /* Initialize the screen, if necessary. */
561 if (gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
564 /* Get a new screen. */
565 if (screen_init(gp
, sp
, &new))
567 if (vs_split(sp
, new, 1)) {
568 (void)screen_end(new);
572 /* Attach to the screen. */
573 new->ep
= gp
->ccl_sp
->ep
;
576 new->frp
= gp
->ccl_sp
->frp
;
577 new->frp
->flags
= sp
->frp
->flags
;
579 /* Move the cursor to the end. */
580 (void)db_last(new, &new->lno
);
584 /* Remember the originating window. */
587 /* It's a special window. */
588 F_SET(new, SC_COMEDIT
);
590 /* Set up the switch. */
592 F_SET(sp
, SC_SSWITCH
);
598 * Execute a command from a colon command-line window.
600 * PUBLIC: int v_ecl_exec __P((SCR *));
609 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
610 v_emsg(sp
, NULL
, VIM_EMPTY
);
614 msgq(sp
, M_BERR
, "307|No ex command to execute");
618 /* Push the command on the command stack. */
619 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
622 /* Set up the switch. */
623 sp
->nextdisp
= sp
->ccl_parent
;
630 * Log a command into the colon command-line log file.
641 /* Initialize the screen, if necessary. */
642 if (sp
->gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
646 * Don't log colon command window commands into the colon command
649 if (sp
->ep
== sp
->gp
->ccl_sp
->ep
)
654 * Swap the current EXF with the colon command file EXF. This
655 * isn't pretty, but too many routines "know" that sp->ep points
656 * to the current EXF.
659 sp
->ep
= sp
->gp
->ccl_sp
->ep
;
660 if (db_last(sp
, &lno
)) {
664 rval
= db_append(sp
, 0, lno
, tp
->lb
, tp
->len
);
671 * Initialize the colon command-line log file.
682 /* Get a temporary file. */
683 if ((frp
= file_add(sp
, NULL
)) == NULL
)
688 * Create a screen -- the file initialization code wants one.
690 if (screen_init(gp
, sp
, &gp
->ccl_sp
))
692 if (file_init(gp
->ccl_sp
, frp
, NULL
, 0)) {
693 (void)screen_end(gp
->ccl_sp
);
697 /* The underlying file isn't recoverable. */
698 F_CLR(gp
->ccl_sp
->ep
, F_RCV_ON
);