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
[] = "$Id: v_ex.c,v 10.48 2000/07/14 14:29:23 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:23 $";
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
*));
37 * Repeat the previous substitution.
39 * PUBLIC: int v_again __P((SCR *, VICMD *));
47 static CHAR_T nul
[] = { 0 };
49 ex_cinit(sp
, &cmd
, C_SUBAGAIN
, 2, vp
->m_start
.lno
, vp
->m_start
.lno
, 1);
50 argv_exp0(sp
, &cmd
, nul
, 1);
51 return (v_exec_ex(sp
, vp
, &cmd
));
56 * Switch the editor into EX mode.
58 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
69 /* Try and switch screens -- the screen may not permit it. */
70 if (gp
->scr_screen(sp
, SC_EX
)) {
72 "207|The Q command requires the ex terminal interface");
75 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
77 /* Save the current cursor position. */
78 sp
->frp
->lno
= sp
->lno
;
79 sp
->frp
->cno
= sp
->cno
;
80 F_SET(sp
->frp
, FR_CURSORSET
);
82 /* Switch to ex mode. */
83 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
86 /* Move out of the vi screen. */
87 (void)ex_puts(sp
, "\n");
94 * Join lines together.
96 * PUBLIC: int v_join __P((SCR *, VICMD *));
108 * The general rule is that '#J' joins # lines, counting the current
109 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
110 * current and next lines. This doesn't map well into the ex command
111 * (which takes two line numbers), so we handle it here. Note that
112 * we never test for EOF -- historically going past the end of file
115 lno
= vp
->m_start
.lno
+ 1;
116 if (F_ISSET(vp
, VC_C1SET
) && vp
->count
> 2)
117 lno
= vp
->m_start
.lno
+ (vp
->count
- 1);
119 ex_cinit(sp
, &cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0);
120 return (v_exec_ex(sp
, vp
, &cmd
));
124 * v_shiftl -- [count]<motion
127 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
135 static CHAR_T lt
[] = {'<', 0};
137 ex_cinit(sp
, &cmd
, C_SHIFTL
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
138 argv_exp0(sp
, &cmd
, lt
, 2);
139 return (v_exec_ex(sp
, vp
, &cmd
));
143 * v_shiftr -- [count]>motion
146 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
154 static CHAR_T gt
[] = {'>', 0};
156 ex_cinit(sp
, &cmd
, C_SHIFTR
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
157 argv_exp0(sp
, &cmd
, gt
, 2);
158 return (v_exec_ex(sp
, vp
, &cmd
));
165 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
173 static CHAR_T suspend
[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
175 ex_cinit(sp
, &cmd
, C_STOP
, 0, OOBLNO
, OOBLNO
, 0);
176 argv_exp0(sp
, &cmd
, suspend
, sizeof(suspend
)/sizeof(CHAR_T
));
177 return (v_exec_ex(sp
, vp
, &cmd
));
182 * Switch to the previous file.
184 * PUBLIC: int v_switch __P((SCR *, VICMD *));
197 * Try the alternate file name, then the previous file
198 * name. Use the real name, not the user's current name.
200 if ((name
= sp
->alt_name
) == NULL
) {
201 msgq(sp
, M_ERR
, "180|No previous file to edit");
205 /* If autowrite is set, write out the file. */
206 if (file_m1(sp
, 0, FS_ALL
))
209 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
210 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
211 argv_exp0(sp
, &cmd
, wp
, wlen
);
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 *));
228 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, 0, 0);
229 argv_exp0(sp
, &cmd
, VIP(sp
)->keyw
, v_strlen(VIP(sp
)->keyw
) + 1);
230 return (v_exec_ex(sp
, vp
, &cmd
));
235 * Pop the tags stack.
237 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
246 ex_cinit(sp
, &cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0);
247 return (v_exec_ex(sp
, vp
, &cmd
));
251 * v_filter -- [count]!motion command(s)
252 * Run range through shell commands, replacing text.
254 * PUBLIC: int v_filter __P((SCR *, VICMD *));
266 * Historical vi permitted "!!" in an empty file, and it's handled
267 * as a special case in the ex_bang routine. Don't modify this setup
268 * without understanding that one. In particular, note that we're
269 * manipulating the ex argument structures behind ex's back.
272 * Historical vi did not permit the '!' command to be associated with
273 * a non-line oriented motion command, in general, although it did
274 * with search commands. So, !f; and !w would fail, but !/;<CR>
275 * would succeed, even if they all moved to the same location in the
276 * current line. I don't see any reason to disallow '!' using any of
277 * the possible motion commands.
280 * Historical vi ran the last bang command if N or n was used as the
283 if (F_ISSET(vp
, VC_ISDOT
) ||
284 ISCMD(vp
->rkp
, 'N') || ISCMD(vp
->rkp
, 'n')) {
285 static CHAR_T bang
[] = {'!', 0};
287 &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
288 EXP(sp
)->argsoff
= 0; /* XXX */
290 if (argv_exp1(sp
, &cmd
, bang
, 1, 1))
292 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
293 cmd
.argv
= EXP(sp
)->args
; /* XXX */
294 return (v_exec_ex(sp
, vp
, &cmd
));
297 /* Get the command from the user. */
299 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
303 * Check to see if the user changed their mind.
306 * Entering <escape> on an empty line was historically an error,
307 * this implementation doesn't bother.
309 tp
= sp
->tiq
.cqh_first
;
310 if (tp
->term
!= TERM_OK
) {
311 vp
->m_final
.lno
= sp
->lno
;
312 vp
->m_final
.cno
= sp
->cno
;
316 /* Home the cursor. */
319 ex_cinit(sp
, &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
320 EXP(sp
)->argsoff
= 0; /* XXX */
322 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
324 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
325 cmd
.argv
= EXP(sp
)->args
; /* XXX */
326 return (v_exec_ex(sp
, vp
, &cmd
));
331 * Execute an ex command.
333 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
336 v_exec_ex(sp
, vp
, exp
)
343 rval
= exp
->cmd
->fn(sp
, exp
);
344 return (v_ex_done(sp
, vp
) || rval
);
349 * Execute a colon command line.
351 * PUBLIC: int v_ex __P((SCR *, VICMD *));
360 int do_cedit
, do_resolution
, ifcontinue
;
366 * If we put out more than a single line of messages, or ex trashes
367 * the screen, the user may continue entering ex commands. We find
368 * this out when we do the screen/message resolution. We can't enter
369 * completely into ex mode however, because the user can elect to
370 * return into vi mode by entering any key, i.e. we have to be in raw
373 for (do_cedit
= do_resolution
= 0;;) {
376 * There may already be an ex command waiting to run. If
377 * so, we continue with it.
379 if (!EXCMD_RUNNING(wp
)) {
381 if (v_tcmd(sp
, vp
, ':',
382 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
384 tp
= sp
->tiq
.cqh_first
;
387 * If the user entered a single <esc>, they want to
388 * edit their colon command history. If they already
389 * entered some text, move it into the edit history.
391 if (tp
->term
== TERM_CEDIT
) {
392 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
398 /* If the user didn't enter anything, return. */
399 if (tp
->term
== TERM_BS
)
402 /* Log the command. */
403 if (O_STR(sp
, O_CEDIT
) != NULL
&& v_ecl_log(sp
, tp
))
406 /* Push a command on the command stack. */
407 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
411 /* Home the cursor. */
416 * If the editor wrote the screen behind curses back, put out
417 * a <newline> so that we don't overwrite the user's command
418 * with its output or the next want-to-continue? message. This
419 * doesn't belong here, but I can't find another place to put
420 * it. See, we resolved the output from the last ex command,
421 * and the user entered another one. This is the only place
422 * where we have control before the ex command writes output.
423 * We could get control in vs_msg(), but we have no way to know
424 * if command didn't put out any output when we try and resolve
425 * this command. This fixes a bug where combinations of ex
426 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
428 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
431 /* Call the ex parser. */
434 /* Flush ex messages. */
437 /* Resolve any messages. */
438 if (vs_ex_resolve(sp
, &ifcontinue
))
442 * Continue or return. If continuing, make sure that we
443 * eventually do resolution.
449 /* If we're continuing, it's a new command. */
454 * If the user previously continued an ex command, we have to do
455 * resolution to clean up the screen. Don't wait, we already did
459 F_SET(sp
, SC_EX_WAIT_NO
);
460 if (vs_ex_resolve(sp
, &ifcontinue
))
464 /* Cleanup from the ex command. */
465 if (v_ex_done(sp
, vp
))
468 /* The user may want to edit their colon command history. */
477 * Cleanup from an ex command.
487 * The only cursor modifications are real, however, the underlying
488 * line may have changed; don't trust anything. This code has been
489 * a remarkably fertile place for bugs. Do a reality check on a
490 * cursor value, and make sure it's okay. If necessary, change it.
491 * Ex keeps track of the line number, but it cares less about the
492 * column and it may have disappeared.
494 * Don't trust ANYTHING.
497 * Ex will soon have to start handling the column correctly; see
498 * the POSIX 1003.2 standard.
500 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
503 } else if (sp
->cno
>= len
)
504 sp
->cno
= len
? len
- 1 : 0;
506 vp
->m_final
.lno
= sp
->lno
;
507 vp
->m_final
.cno
= sp
->cno
;
510 * Don't re-adjust the cursor after executing an ex command,
511 * and ex movements are permanent.
513 F_CLR(vp
, VM_RCM_MASK
);
514 F_SET(vp
, VM_RCM_SET
);
521 * Start an edit window on the colon command-line commands.
530 /* Initialize the screen, if necessary. */
532 if (gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
535 /* Get a new screen. */
536 if (screen_init(gp
, sp
, &new))
538 if (vs_split(sp
, new, 1)) {
539 (void)screen_end(new);
543 /* Attach to the screen. */
544 new->ep
= gp
->ccl_sp
->ep
;
547 new->frp
= gp
->ccl_sp
->frp
;
548 new->frp
->flags
= sp
->frp
->flags
;
550 /* Move the cursor to the end. */
551 (void)db_last(new, &new->lno
);
555 /* Remember the originating window. */
558 /* It's a special window. */
559 F_SET(new, SC_COMEDIT
);
561 /* Set up the switch. */
563 F_SET(sp
, SC_SSWITCH
);
569 * Execute a command from a colon command-line window.
571 * PUBLIC: int v_ecl_exec __P((SCR *));
580 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
581 v_emsg(sp
, NULL
, VIM_EMPTY
);
585 msgq(sp
, M_BERR
, "307|No ex command to execute");
589 /* Push the command on the command stack. */
590 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
593 /* Set up the switch. */
594 sp
->nextdisp
= sp
->ccl_parent
;
601 * Log a command into the colon command-line log file.
612 /* Initialize the screen, if necessary. */
613 if (sp
->gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
617 * Don't log colon command window commands into the colon command
620 if (sp
->ep
== sp
->gp
->ccl_sp
->ep
)
625 * Swap the current EXF with the colon command file EXF. This
626 * isn't pretty, but too many routines "know" that sp->ep points
627 * to the current EXF.
630 sp
->ep
= sp
->gp
->ccl_sp
->ep
;
631 if (db_last(sp
, &lno
)) {
635 rval
= db_append(sp
, 0, lno
, tp
->lb
, tp
->len
);
642 * Initialize the colon command-line log file.
653 /* Get a temporary file. */
654 if ((frp
= file_add(sp
, NULL
)) == NULL
)
659 * Create a screen -- the file initialization code wants one.
661 if (screen_init(gp
, sp
, &gp
->ccl_sp
))
663 if (file_init(gp
->ccl_sp
, frp
, NULL
, 0)) {
664 (void)screen_end(gp
->ccl_sp
);
668 /* The underlying file isn't recoverable. */
669 F_CLR(gp
->ccl_sp
->ep
, F_RCV_ON
);