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.43 1996/12/04 09:48:21 bostic Exp $ (Berkeley) $Date: 1996/12/04 09:48:21 $";
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 *));
49 ex_cinit(&cmd
, C_SUBAGAIN
, 2, vp
->m_start
.lno
, vp
->m_start
.lno
, 1, ap
);
50 ex_cadd(&cmd
, &a
, "", 1);
52 return (v_exec_ex(sp
, vp
, &cmd
));
57 * Switch the editor into EX mode.
59 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
70 /* Try and switch screens -- the screen may not permit it. */
71 if (gp
->scr_screen(sp
, SC_EX
)) {
73 "207|The Q command requires the ex terminal interface");
76 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
78 /* Save the current cursor position. */
79 sp
->frp
->lno
= sp
->lno
;
80 sp
->frp
->cno
= sp
->cno
;
81 F_SET(sp
->frp
, FR_CURSORSET
);
83 /* Switch to ex mode. */
84 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
87 /* Move out of the vi screen. */
88 (void)ex_puts(sp
, "\n");
95 * Join lines together.
97 * PUBLIC: int v_join __P((SCR *, VICMD *));
109 * The general rule is that '#J' joins # lines, counting the current
110 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
111 * current and next lines. This doesn't map well into the ex command
112 * (which takes two line numbers), so we handle it here. Note that
113 * we never test for EOF -- historically going past the end of file
116 lno
= vp
->m_start
.lno
+ 1;
117 if (F_ISSET(vp
, VC_C1SET
) && vp
->count
> 2)
118 lno
= vp
->m_start
.lno
+ (vp
->count
- 1);
120 ex_cinit(&cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0, NULL
);
121 return (v_exec_ex(sp
, vp
, &cmd
));
125 * v_shiftl -- [count]<motion
128 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
138 ex_cinit(&cmd
, C_SHIFTL
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0, ap
);
139 ex_cadd(&cmd
, &a
, "<", 1);
140 return (v_exec_ex(sp
, vp
, &cmd
));
144 * v_shiftr -- [count]>motion
147 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
157 ex_cinit(&cmd
, C_SHIFTR
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0, ap
);
158 ex_cadd(&cmd
, &a
, ">", 1);
159 return (v_exec_ex(sp
, vp
, &cmd
));
166 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
176 ex_cinit(&cmd
, C_STOP
, 0, OOBLNO
, OOBLNO
, 0, ap
);
177 ex_cadd(&cmd
, &a
, "suspend", sizeof("suspend") - 1);
178 return (v_exec_ex(sp
, vp
, &cmd
));
183 * Switch to the previous file.
185 * 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(&cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0, ap
);
210 ex_cadd(&cmd
, &a
, name
, strlen(name
));
211 return (v_exec_ex(sp
, vp
, &cmd
));
216 * Do a tag search on the cursor keyword.
218 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
228 ex_cinit(&cmd
, C_TAG
, 0, OOBLNO
, 0, 0, ap
);
229 ex_cadd(&cmd
, &a
, VIP(sp
)->keyw
, strlen(VIP(sp
)->keyw
));
230 return (v_exec_ex(sp
, vp
, &cmd
));
235 * Pop the tags stack.
237 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
246 ex_cinit(&cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0, NULL
);
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 ex_cinit(&cmd
, C_BANG
,
286 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0, NULL
);
287 EXP(sp
)->argsoff
= 0; /* XXX */
289 if (argv_exp1(sp
, &cmd
, "!", 1, 1))
291 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
292 cmd
.argv
= EXP(sp
)->args
; /* XXX */
293 return (v_exec_ex(sp
, vp
, &cmd
));
296 /* Get the command from the user. */
298 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
302 * Check to see if the user changed their mind.
305 * Entering <escape> on an empty line was historically an error,
306 * this implementation doesn't bother.
308 tp
= sp
->tiq
.cqh_first
;
309 if (tp
->term
!= TERM_OK
) {
310 vp
->m_final
.lno
= sp
->lno
;
311 vp
->m_final
.cno
= sp
->cno
;
315 /* Home the cursor. */
318 ex_cinit(&cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0, NULL
);
319 EXP(sp
)->argsoff
= 0; /* XXX */
321 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
323 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
324 cmd
.argv
= EXP(sp
)->args
; /* XXX */
325 return (v_exec_ex(sp
, vp
, &cmd
));
330 * Execute an ex command.
332 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
335 v_exec_ex(sp
, vp
, exp
)
342 rval
= exp
->cmd
->fn(sp
, exp
);
343 return (v_ex_done(sp
, vp
) || rval
);
348 * Execute a colon command line.
350 * PUBLIC: int v_ex __P((SCR *, VICMD *));
359 int do_cedit
, do_resolution
, ifcontinue
;
365 * If we put out more than a single line of messages, or ex trashes
366 * the screen, the user may continue entering ex commands. We find
367 * this out when we do the screen/message resolution. We can't enter
368 * completely into ex mode however, because the user can elect to
369 * return into vi mode by entering any key, i.e. we have to be in raw
372 for (do_cedit
= do_resolution
= 0;;) {
375 * There may already be an ex command waiting to run. If
376 * so, we continue with it.
378 if (!EXCMD_RUNNING(gp
)) {
380 if (v_tcmd(sp
, vp
, ':',
381 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
383 tp
= sp
->tiq
.cqh_first
;
386 * If the user entered a single <esc>, they want to
387 * edit their colon command history. If they already
388 * entered some text, move it into the edit history.
390 if (tp
->term
== TERM_CEDIT
) {
391 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
397 /* If the user didn't enter anything, return. */
398 if (tp
->term
== TERM_BS
)
401 /* Log the command. */
402 if (O_STR(sp
, O_CEDIT
) != NULL
&& v_ecl_log(sp
, tp
))
405 /* Push a command on the command stack. */
406 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
410 /* Home the cursor. */
415 * If the editor wrote the screen behind curses back, put out
416 * a <newline> so that we don't overwrite the user's command
417 * with its output or the next want-to-continue? message. This
418 * doesn't belong here, but I can't find another place to put
419 * it. See, we resolved the output from the last ex command,
420 * and the user entered another one. This is the only place
421 * where we have control before the ex command writes output.
422 * We could get control in vs_msg(), but we have no way to know
423 * if command didn't put out any output when we try and resolve
424 * this command. This fixes a bug where combinations of ex
425 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
427 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
430 /* Call the ex parser. */
433 /* Flush ex messages. */
436 /* Resolve any messages. */
437 if (vs_ex_resolve(sp
, &ifcontinue
))
441 * Continue or return. If continuing, make sure that we
442 * eventually do resolution.
448 /* If we're continuing, it's a new command. */
453 * If the user previously continued an ex command, we have to do
454 * resolution to clean up the screen. Don't wait, we already did
458 F_SET(sp
, SC_EX_WAIT_NO
);
459 if (vs_ex_resolve(sp
, &ifcontinue
))
463 /* Cleanup from the ex command. */
464 if (v_ex_done(sp
, vp
))
467 /* The user may want to edit their colon command history. */
476 * Cleanup from an ex command.
486 * The only cursor modifications are real, however, the underlying
487 * line may have changed; don't trust anything. This code has been
488 * a remarkably fertile place for bugs. Do a reality check on a
489 * cursor value, and make sure it's okay. If necessary, change it.
490 * Ex keeps track of the line number, but it cares less about the
491 * column and it may have disappeared.
493 * Don't trust ANYTHING.
496 * Ex will soon have to start handling the column correctly; see
497 * the POSIX 1003.2 standard.
499 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
502 } else if (sp
->cno
>= len
)
503 sp
->cno
= len
? len
- 1 : 0;
505 vp
->m_final
.lno
= sp
->lno
;
506 vp
->m_final
.cno
= sp
->cno
;
509 * Don't re-adjust the cursor after executing an ex command,
510 * and ex movements are permanent.
512 F_CLR(vp
, VM_RCM_MASK
);
513 F_SET(vp
, VM_RCM_SET
);
520 * Start an edit window on the colon command-line commands.
529 /* Initialize the screen, if necessary. */
531 if (gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
534 /* Get a new screen. */
535 if (screen_init(gp
, sp
, &new))
537 if (vs_split(sp
, new, 1)) {
538 (void)screen_end(new);
542 /* Attach to the screen. */
543 new->ep
= gp
->ccl_sp
->ep
;
546 new->frp
= gp
->ccl_sp
->frp
;
547 new->frp
->flags
= sp
->frp
->flags
;
549 /* Move the cursor to the end. */
550 (void)db_last(new, &new->lno
);
554 /* Remember the originating window. */
557 /* It's a special window. */
558 F_SET(new, SC_COMEDIT
);
560 /* Set up the switch. */
562 F_SET(sp
, SC_SSWITCH
);
568 * Execute a command from a colon command-line window.
570 * PUBLIC: int v_ecl_exec __P((SCR *));
579 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
580 v_emsg(sp
, NULL
, VIM_EMPTY
);
584 msgq(sp
, M_BERR
, "307|No ex command to execute");
588 /* Push the command on the command stack. */
589 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
592 /* Set up the switch. */
593 sp
->nextdisp
= sp
->ccl_parent
;
600 * Log a command into the colon command-line log file.
611 /* Initialize the screen, if necessary. */
612 if (sp
->gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
616 * Don't log colon command window commands into the colon command
619 if (sp
->ep
== sp
->gp
->ccl_sp
->ep
)
624 * Swap the current EXF with the colon command file EXF. This
625 * isn't pretty, but too many routines "know" that sp->ep points
626 * to the current EXF.
629 sp
->ep
= sp
->gp
->ccl_sp
->ep
;
630 if (db_last(sp
, &lno
)) {
634 rval
= db_append(sp
, 0, lno
, tp
->lb
, tp
->len
);
641 * Initialize the colon command-line log file.
652 /* Get a temporary file. */
653 if ((frp
= file_add(sp
, NULL
)) == NULL
)
658 * Create a screen -- the file initialization code wants one.
660 if (screen_init(gp
, sp
, &gp
->ccl_sp
))
662 if (file_init(gp
->ccl_sp
, frp
, NULL
, 0)) {
663 (void)screen_end(gp
->ccl_sp
);
667 /* The underlying file isn't recoverable. */
668 F_CLR(gp
->ccl_sp
->ep
, F_RCV_ON
);