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.45 2000/04/21 19:00:40 skimo Exp $ (Berkeley) $Date: 2000/04/21 19:00:40 $";
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 *));
48 ex_cinit(sp
, &cmd
, C_SUBAGAIN
, 2, vp
->m_start
.lno
, vp
->m_start
.lno
, 1);
49 argv_exp0(sp
, &cmd
, "", 1);
50 return (v_exec_ex(sp
, vp
, &cmd
));
55 * Switch the editor into EX mode.
57 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
68 /* Try and switch screens -- the screen may not permit it. */
69 if (gp
->scr_screen(sp
, SC_EX
)) {
71 "207|The Q command requires the ex terminal interface");
74 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
76 /* Save the current cursor position. */
77 sp
->frp
->lno
= sp
->lno
;
78 sp
->frp
->cno
= sp
->cno
;
79 F_SET(sp
->frp
, FR_CURSORSET
);
81 /* Switch to ex mode. */
82 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
85 /* Move out of the vi screen. */
86 (void)ex_puts(sp
, "\n");
93 * Join lines together.
95 * PUBLIC: int v_join __P((SCR *, VICMD *));
107 * The general rule is that '#J' joins # lines, counting the current
108 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
109 * current and next lines. This doesn't map well into the ex command
110 * (which takes two line numbers), so we handle it here. Note that
111 * we never test for EOF -- historically going past the end of file
114 lno
= vp
->m_start
.lno
+ 1;
115 if (F_ISSET(vp
, VC_C1SET
) && vp
->count
> 2)
116 lno
= vp
->m_start
.lno
+ (vp
->count
- 1);
118 ex_cinit(sp
, &cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0);
119 return (v_exec_ex(sp
, vp
, &cmd
));
123 * v_shiftl -- [count]<motion
126 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
135 ex_cinit(sp
, &cmd
, C_SHIFTL
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
136 argv_exp0(sp
, &cmd
, "<", 2);
137 return (v_exec_ex(sp
, vp
, &cmd
));
141 * v_shiftr -- [count]>motion
144 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
153 ex_cinit(sp
, &cmd
, C_SHIFTR
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
154 argv_exp0(sp
, &cmd
, ">", 2);
155 return (v_exec_ex(sp
, vp
, &cmd
));
162 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
171 ex_cinit(sp
, &cmd
, C_STOP
, 0, OOBLNO
, OOBLNO
, 0);
172 argv_exp0(sp
, &cmd
, "suspend", sizeof("suspend"));
173 return (v_exec_ex(sp
, vp
, &cmd
));
178 * Switch to the previous file.
180 * PUBLIC: int v_switch __P((SCR *, VICMD *));
191 * Try the alternate file name, then the previous file
192 * name. Use the real name, not the user's current name.
194 if ((name
= sp
->alt_name
) == NULL
) {
195 msgq(sp
, M_ERR
, "180|No previous file to edit");
199 /* If autowrite is set, write out the file. */
200 if (file_m1(sp
, 0, FS_ALL
))
203 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
204 argv_exp0(sp
, &cmd
, name
, strlen(name
) + 1);
205 return (v_exec_ex(sp
, vp
, &cmd
));
210 * Do a tag search on the cursor keyword.
212 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
221 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, 0, 0);
222 argv_exp0(sp
, &cmd
, VIP(sp
)->keyw
, strlen(VIP(sp
)->keyw
) + 1);
223 return (v_exec_ex(sp
, vp
, &cmd
));
228 * Pop the tags stack.
230 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
239 ex_cinit(sp
, &cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0);
240 return (v_exec_ex(sp
, vp
, &cmd
));
244 * v_filter -- [count]!motion command(s)
245 * Run range through shell commands, replacing text.
247 * PUBLIC: int v_filter __P((SCR *, VICMD *));
259 * Historical vi permitted "!!" in an empty file, and it's handled
260 * as a special case in the ex_bang routine. Don't modify this setup
261 * without understanding that one. In particular, note that we're
262 * manipulating the ex argument structures behind ex's back.
265 * Historical vi did not permit the '!' command to be associated with
266 * a non-line oriented motion command, in general, although it did
267 * with search commands. So, !f; and !w would fail, but !/;<CR>
268 * would succeed, even if they all moved to the same location in the
269 * current line. I don't see any reason to disallow '!' using any of
270 * the possible motion commands.
273 * Historical vi ran the last bang command if N or n was used as the
276 if (F_ISSET(vp
, VC_ISDOT
) ||
277 ISCMD(vp
->rkp
, 'N') || ISCMD(vp
->rkp
, 'n')) {
279 &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
280 EXP(sp
)->argsoff
= 0; /* XXX */
282 if (argv_exp1(sp
, &cmd
, "!", 1, 1))
284 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
285 cmd
.argv
= EXP(sp
)->args
; /* XXX */
286 return (v_exec_ex(sp
, vp
, &cmd
));
289 /* Get the command from the user. */
291 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
295 * Check to see if the user changed their mind.
298 * Entering <escape> on an empty line was historically an error,
299 * this implementation doesn't bother.
301 tp
= sp
->tiq
.cqh_first
;
302 if (tp
->term
!= TERM_OK
) {
303 vp
->m_final
.lno
= sp
->lno
;
304 vp
->m_final
.cno
= sp
->cno
;
308 /* Home the cursor. */
311 ex_cinit(sp
, &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
312 EXP(sp
)->argsoff
= 0; /* XXX */
314 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
316 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
317 cmd
.argv
= EXP(sp
)->args
; /* XXX */
318 return (v_exec_ex(sp
, vp
, &cmd
));
323 * Execute an ex command.
325 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
328 v_exec_ex(sp
, vp
, exp
)
335 rval
= exp
->cmd
->fn(sp
, exp
);
336 return (v_ex_done(sp
, vp
) || rval
);
341 * Execute a colon command line.
343 * PUBLIC: int v_ex __P((SCR *, VICMD *));
352 int do_cedit
, do_resolution
, ifcontinue
;
358 * If we put out more than a single line of messages, or ex trashes
359 * the screen, the user may continue entering ex commands. We find
360 * this out when we do the screen/message resolution. We can't enter
361 * completely into ex mode however, because the user can elect to
362 * return into vi mode by entering any key, i.e. we have to be in raw
365 for (do_cedit
= do_resolution
= 0;;) {
368 * There may already be an ex command waiting to run. If
369 * so, we continue with it.
371 if (!EXCMD_RUNNING(gp
)) {
373 if (v_tcmd(sp
, vp
, ':',
374 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
376 tp
= sp
->tiq
.cqh_first
;
379 * If the user entered a single <esc>, they want to
380 * edit their colon command history. If they already
381 * entered some text, move it into the edit history.
383 if (tp
->term
== TERM_CEDIT
) {
384 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
390 /* If the user didn't enter anything, return. */
391 if (tp
->term
== TERM_BS
)
394 /* Log the command. */
395 if (O_STR(sp
, O_CEDIT
) != NULL
&& v_ecl_log(sp
, tp
))
398 /* Push a command on the command stack. */
399 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
403 /* Home the cursor. */
408 * If the editor wrote the screen behind curses back, put out
409 * a <newline> so that we don't overwrite the user's command
410 * with its output or the next want-to-continue? message. This
411 * doesn't belong here, but I can't find another place to put
412 * it. See, we resolved the output from the last ex command,
413 * and the user entered another one. This is the only place
414 * where we have control before the ex command writes output.
415 * We could get control in vs_msg(), but we have no way to know
416 * if command didn't put out any output when we try and resolve
417 * this command. This fixes a bug where combinations of ex
418 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
420 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
423 /* Call the ex parser. */
426 /* Flush ex messages. */
429 /* Resolve any messages. */
430 if (vs_ex_resolve(sp
, &ifcontinue
))
434 * Continue or return. If continuing, make sure that we
435 * eventually do resolution.
441 /* If we're continuing, it's a new command. */
446 * If the user previously continued an ex command, we have to do
447 * resolution to clean up the screen. Don't wait, we already did
451 F_SET(sp
, SC_EX_WAIT_NO
);
452 if (vs_ex_resolve(sp
, &ifcontinue
))
456 /* Cleanup from the ex command. */
457 if (v_ex_done(sp
, vp
))
460 /* The user may want to edit their colon command history. */
469 * Cleanup from an ex command.
479 * The only cursor modifications are real, however, the underlying
480 * line may have changed; don't trust anything. This code has been
481 * a remarkably fertile place for bugs. Do a reality check on a
482 * cursor value, and make sure it's okay. If necessary, change it.
483 * Ex keeps track of the line number, but it cares less about the
484 * column and it may have disappeared.
486 * Don't trust ANYTHING.
489 * Ex will soon have to start handling the column correctly; see
490 * the POSIX 1003.2 standard.
492 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
495 } else if (sp
->cno
>= len
)
496 sp
->cno
= len
? len
- 1 : 0;
498 vp
->m_final
.lno
= sp
->lno
;
499 vp
->m_final
.cno
= sp
->cno
;
502 * Don't re-adjust the cursor after executing an ex command,
503 * and ex movements are permanent.
505 F_CLR(vp
, VM_RCM_MASK
);
506 F_SET(vp
, VM_RCM_SET
);
513 * Start an edit window on the colon command-line commands.
522 /* Initialize the screen, if necessary. */
524 if (gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
527 /* Get a new screen. */
528 if (screen_init(gp
, sp
, &new))
530 if (vs_split(sp
, new, 1)) {
531 (void)screen_end(new);
535 /* Attach to the screen. */
536 new->ep
= gp
->ccl_sp
->ep
;
539 new->frp
= gp
->ccl_sp
->frp
;
540 new->frp
->flags
= sp
->frp
->flags
;
542 /* Move the cursor to the end. */
543 (void)db_last(new, &new->lno
);
547 /* Remember the originating window. */
550 /* It's a special window. */
551 F_SET(new, SC_COMEDIT
);
553 /* Set up the switch. */
555 F_SET(sp
, SC_SSWITCH
);
561 * Execute a command from a colon command-line window.
563 * PUBLIC: int v_ecl_exec __P((SCR *));
572 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
573 v_emsg(sp
, NULL
, VIM_EMPTY
);
577 msgq(sp
, M_BERR
, "307|No ex command to execute");
581 /* Push the command on the command stack. */
582 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
585 /* Set up the switch. */
586 sp
->nextdisp
= sp
->ccl_parent
;
593 * Log a command into the colon command-line log file.
604 /* Initialize the screen, if necessary. */
605 if (sp
->gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
609 * Don't log colon command window commands into the colon command
612 if (sp
->ep
== sp
->gp
->ccl_sp
->ep
)
617 * Swap the current EXF with the colon command file EXF. This
618 * isn't pretty, but too many routines "know" that sp->ep points
619 * to the current EXF.
622 sp
->ep
= sp
->gp
->ccl_sp
->ep
;
623 if (db_last(sp
, &lno
)) {
627 rval
= db_append(sp
, 0, lno
, tp
->lb
, tp
->len
);
634 * Initialize the colon command-line log file.
645 /* Get a temporary file. */
646 if ((frp
= file_add(sp
, NULL
)) == NULL
)
651 * Create a screen -- the file initialization code wants one.
653 if (screen_init(gp
, sp
, &gp
->ccl_sp
))
655 if (file_init(gp
->ccl_sp
, frp
, NULL
, 0)) {
656 (void)screen_end(gp
->ccl_sp
);
660 /* The underlying file isn't recoverable. */
661 F_CLR(gp
->ccl_sp
->ep
, F_RCV_ON
);