1 /* $NetBSD: v_ex.c,v 1.5 2013/12/01 02:34:54 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: v_ex.c,v 10.60 2003/07/19 21:04:00 skimo Exp (Berkeley) Date: 2003/07/19 21:04:00 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
28 #include "../common/common.h"
31 static int v_ecl
__P((SCR
*));
32 static int v_ecl_init
__P((SCR
*));
33 static int v_ecl_log
__P((SCR
*, TEXT
*));
34 static int v_ex_done
__P((SCR
*, VICMD
*));
38 * Repeat the previous substitution.
40 * PUBLIC: int v_again __P((SCR *, VICMD *));
43 v_again(SCR
*sp
, VICMD
*vp
)
46 static CHAR_T nul
[] = { 0 };
48 ex_cinit(sp
, &cmd
, C_SUBAGAIN
, 2, vp
->m_start
.lno
, vp
->m_start
.lno
, 1);
49 argv_exp0(sp
, &cmd
, nul
, 1);
50 return (v_exec_ex(sp
, vp
, &cmd
));
55 * Switch the editor into EX mode.
57 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
60 v_exmode(SCR
*sp
, VICMD
*vp
)
66 /* Try and switch screens -- the screen may not permit it. */
67 if (gp
->scr_screen(sp
, SC_EX
)) {
69 "207|The Q command requires the ex terminal interface");
72 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
74 /* Save the current cursor position. */
75 sp
->frp
->lno
= sp
->lno
;
76 sp
->frp
->cno
= sp
->cno
;
77 F_SET(sp
->frp
, FR_CURSORSET
);
79 /* Switch to ex mode. */
80 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
83 /* Move out of the vi screen. */
84 (void)ex_puts(sp
, "\n");
91 * Join lines together.
93 * PUBLIC: int v_join __P((SCR *, VICMD *));
96 v_join(SCR
*sp
, VICMD
*vp
)
103 * The general rule is that '#J' joins # lines, counting the current
104 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
105 * current and next lines. This doesn't map well into the ex command
106 * (which takes two line numbers), so we handle it here. Note that
107 * we never test for EOF -- historically going past the end of file
110 lno
= vp
->m_start
.lno
+ 1;
111 if (F_ISSET(vp
, VC_C1SET
) && vp
->count
> 2)
112 lno
= vp
->m_start
.lno
+ (vp
->count
- 1);
114 ex_cinit(sp
, &cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0);
115 return (v_exec_ex(sp
, vp
, &cmd
));
119 * v_shiftl -- [count]<motion
122 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
125 v_shiftl(SCR
*sp
, VICMD
*vp
)
128 static CHAR_T lt
[] = {'<', 0};
130 ex_cinit(sp
, &cmd
, C_SHIFTL
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
131 argv_exp0(sp
, &cmd
, lt
, 2);
132 return (v_exec_ex(sp
, vp
, &cmd
));
136 * v_shiftr -- [count]>motion
139 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
142 v_shiftr(SCR
*sp
, VICMD
*vp
)
145 static CHAR_T gt
[] = {'>', 0};
147 ex_cinit(sp
, &cmd
, C_SHIFTR
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
148 argv_exp0(sp
, &cmd
, gt
, 2);
149 return (v_exec_ex(sp
, vp
, &cmd
));
156 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
159 v_suspend(SCR
*sp
, VICMD
*vp
)
162 static CHAR_T suspend
[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
164 ex_cinit(sp
, &cmd
, C_STOP
, 0, OOBLNO
, OOBLNO
, 0);
165 argv_exp0(sp
, &cmd
, suspend
, sizeof(suspend
)/sizeof(CHAR_T
));
166 return (v_exec_ex(sp
, vp
, &cmd
));
171 * Switch to the previous file.
173 * PUBLIC: int v_switch __P((SCR *, VICMD *));
176 v_switch(SCR
*sp
, VICMD
*vp
)
184 * Try the alternate file name, then the previous file
185 * name. Use the real name, not the user's current name.
187 if ((name
= sp
->alt_name
) == NULL
) {
188 msgq(sp
, M_ERR
, "180|No previous file to edit");
192 /* If autowrite is set, write out the file. */
193 if (file_m1(sp
, 0, FS_ALL
))
196 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
197 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
198 argv_exp0(sp
, &cmd
, wp
, wlen
);
199 return (v_exec_ex(sp
, vp
, &cmd
));
204 * Do a tag search on the cursor keyword.
206 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
209 v_tagpush(SCR
*sp
, VICMD
*vp
)
214 if (O_ISSET(sp
, O_GTAGSMODE
) && vp
->m_start
.cno
== 0)
215 ex_cinit(sp
, &cmd
, C_RTAG
, 0, OOBLNO
, 0, 0);
218 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, 0, 0);
219 argv_exp0(sp
, &cmd
, VIP(sp
)->keyw
, STRLEN(VIP(sp
)->keyw
) + 1);
220 return (v_exec_ex(sp
, vp
, &cmd
));
225 * Pop the tags stack.
227 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
230 v_tagpop(SCR
*sp
, VICMD
*vp
)
234 ex_cinit(sp
, &cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0);
235 return (v_exec_ex(sp
, vp
, &cmd
));
239 * v_filter -- [count]!motion command(s)
240 * Run range through shell commands, replacing text.
242 * PUBLIC: int v_filter __P((SCR *, VICMD *));
245 v_filter(SCR
*sp
, VICMD
*vp
)
252 * Historical vi permitted "!!" in an empty file, and it's handled
253 * as a special case in the ex_bang routine. Don't modify this setup
254 * without understanding that one. In particular, note that we're
255 * manipulating the ex argument structures behind ex's back.
258 * Historical vi did not permit the '!' command to be associated with
259 * a non-line oriented motion command, in general, although it did
260 * with search commands. So, !f; and !w would fail, but !/;<CR>
261 * would succeed, even if they all moved to the same location in the
262 * current line. I don't see any reason to disallow '!' using any of
263 * the possible motion commands.
266 * Historical vi ran the last bang command if N or n was used as the
269 if (F_ISSET(vp
, VC_ISDOT
) ||
270 ISCMD(vp
->rkp
, 'N') || ISCMD(vp
->rkp
, 'n')) {
271 static CHAR_T bang
[] = {'!', 0};
273 &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
274 EXP(sp
)->argsoff
= 0; /* XXX */
276 if (argv_exp1(sp
, &cmd
, bang
, 1, 1))
278 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
279 cmd
.argv
= EXP(sp
)->args
; /* XXX */
280 return (v_exec_ex(sp
, vp
, &cmd
));
283 /* Get the command from the user. */
285 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
289 * Check to see if the user changed their mind.
292 * Entering <escape> on an empty line was historically an error,
293 * this implementation doesn't bother.
295 tp
= TAILQ_FIRST(&sp
->tiq
);
296 if (tp
->term
!= TERM_OK
) {
297 vp
->m_final
.lno
= sp
->lno
;
298 vp
->m_final
.cno
= sp
->cno
;
302 /* Home the cursor. */
305 ex_cinit(sp
, &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
306 EXP(sp
)->argsoff
= 0; /* XXX */
308 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
310 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
311 cmd
.argv
= EXP(sp
)->args
; /* XXX */
312 return (v_exec_ex(sp
, vp
, &cmd
));
317 * Execute an ex command.
319 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
322 v_exec_ex(SCR
*sp
, VICMD
*vp
, EXCMD
*exp
)
326 rval
= exp
->cmd
->fn(sp
, exp
);
327 return (v_ex_done(sp
, vp
) || rval
);
332 * Execute a colon command line.
334 * PUBLIC: int v_ex __P((SCR *, VICMD *));
337 v_ex(SCR
*sp
, VICMD
*vp
)
341 int do_cedit
, do_resolution
, ifcontinue
;
347 * If we put out more than a single line of messages, or ex trashes
348 * the screen, the user may continue entering ex commands. We find
349 * this out when we do the screen/message resolution. We can't enter
350 * completely into ex mode however, because the user can elect to
351 * return into vi mode by entering any key, i.e. we have to be in raw
354 for (do_cedit
= do_resolution
= 0;;) {
357 * There may already be an ex command waiting to run. If
358 * so, we continue with it.
360 if (!EXCMD_RUNNING(wp
)) {
362 if (v_tcmd(sp
, vp
, ':',
363 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
365 tp
= TAILQ_FIRST(&sp
->tiq
);
368 * If the user entered a single <esc>, they want to
369 * edit their colon command history. If they already
370 * entered some text, move it into the edit history.
372 if (tp
->term
== TERM_CEDIT
) {
373 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
379 /* If the user didn't enter anything, return. */
380 if (tp
->term
== TERM_BS
)
383 /* Log the command. */
384 if (O_STR(sp
, O_CEDIT
) != NULL
)
385 (void)v_ecl_log(sp
, tp
);
387 /* Push a command on the command stack. */
388 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
392 /* Home the cursor. */
397 * If the editor wrote the screen behind curses back, put out
398 * a <newline> so that we don't overwrite the user's command
399 * with its output or the next want-to-continue? message. This
400 * doesn't belong here, but I can't find another place to put
401 * it. See, we resolved the output from the last ex command,
402 * and the user entered another one. This is the only place
403 * where we have control before the ex command writes output.
404 * We could get control in vs_msg(), but we have no way to know
405 * if command didn't put out any output when we try and resolve
406 * this command. This fixes a bug where combinations of ex
407 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
409 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
412 /* Call the ex parser. */
415 /* Flush ex messages. */
418 /* Resolve any messages. */
419 if (vs_ex_resolve(sp
, &ifcontinue
))
423 * Continue or return. If continuing, make sure that we
424 * eventually do resolution.
430 /* If we're continuing, it's a new command. */
435 * If the user previously continued an ex command, we have to do
436 * resolution to clean up the screen. Don't wait, we already did
440 F_SET(sp
, SC_EX_WAIT_NO
);
441 if (vs_ex_resolve(sp
, &ifcontinue
))
445 /* Cleanup from the ex command. */
446 if (v_ex_done(sp
, vp
))
449 /* The user may want to edit their colon command history. */
458 * Cleanup from an ex command.
461 v_ex_done(SCR
*sp
, VICMD
*vp
)
466 * The only cursor modifications are real, however, the underlying
467 * line may have changed; don't trust anything. This code has been
468 * a remarkably fertile place for bugs. Do a reality check on a
469 * cursor value, and make sure it's okay. If necessary, change it.
470 * Ex keeps track of the line number, but it cares less about the
471 * column and it may have disappeared.
473 * Don't trust ANYTHING.
476 * Ex will soon have to start handling the column correctly; see
477 * the POSIX 1003.2 standard.
479 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
482 } else if (sp
->cno
>= len
)
483 sp
->cno
= len
? len
- 1 : 0;
485 vp
->m_final
.lno
= sp
->lno
;
486 vp
->m_final
.cno
= sp
->cno
;
489 * Don't re-adjust the cursor after executing an ex command,
490 * and ex movements are permanent.
492 F_CLR(vp
, VM_RCM_MASK
);
493 F_SET(vp
, VM_RCM_SET
);
500 * Start an edit window on the colon command-line commands.
509 /* Initialize the screen, if necessary. */
512 if (wp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
515 /* Get a new screen. */
516 if (screen_init(gp
, sp
, &new))
518 if (vs_split(sp
, new, 1)) {
519 (void)screen_end(new);
523 /* Attach to the screen. */
524 new->ep
= wp
->ccl_sp
->ep
;
526 TAILQ_INSERT_HEAD(&new->ep
->scrq
, new, eq
);
528 new->frp
= wp
->ccl_sp
->frp
;
529 new->frp
->flags
= sp
->frp
->flags
;
530 new->conv
= wp
->ccl_sp
->conv
;
532 /* Move the cursor to the end. */
533 (void)db_last(new, &new->lno
);
537 /* Remember the originating window. */
540 /* It's a special window. */
541 F_SET(new, SC_COMEDIT
);
543 /* Don't encode on writing to DB. */
544 o_set(new, O_FILEENCODING
, OS_STRDUP
, "WCHAR_T", 0);
546 /* Set up the switch. */
548 F_SET(sp
, SC_SSWITCH
);
554 * Execute a command from a colon command-line window.
556 * PUBLIC: int v_ecl_exec __P((SCR *));
564 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
565 v_emsg(sp
, NULL
, VIM_EMPTY
);
569 msgq(sp
, M_BERR
, "307|No ex command to execute");
573 /* Push the command on the command stack. */
574 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
577 /* Set up the switch. */
578 sp
->nextdisp
= sp
->ccl_parent
;
585 * Log a command into the colon command-line log file.
588 v_ecl_log(SCR
*sp
, TEXT
*tp
)
596 /* Initialize the screen, if necessary. */
597 if (sp
->wp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
600 ccl_sp
= sp
->wp
->ccl_sp
;
603 * Don't log colon command window commands into the colon command
606 if (sp
->ep
== ccl_sp
->ep
)
609 if (db_last(ccl_sp
, &lno
)) {
612 /* Don't log line that is identical to previous one */
614 !db_get(ccl_sp
, lno
, 0, &p
, &len
) &&
616 !MEMCMP(tp
->lb
, p
, len
))
619 rval
= db_append(ccl_sp
, 0, lno
, tp
->lb
, tp
->len
);
620 /* XXXX end "transaction" on ccl */
621 /* Is this still necessary now that we no longer hijack sp ? */
630 * Initialize the colon command-line log file.
642 /* Get a temporary file. */
643 if ((frp
= file_add(sp
, NULL
)) == NULL
)
648 * Create a screen -- the file initialization code wants one.
650 if (screen_init(gp
, sp
, &wp
->ccl_sp
))
652 conv_enc(wp
->ccl_sp
, O_FILEENCODING
, "WCHAR_T");
653 if (file_init(wp
->ccl_sp
, frp
, NULL
, 0)) {
654 (void)screen_end(wp
->ccl_sp
);
659 /* The underlying file isn't recoverable. */
660 F_CLR(wp
->ccl_sp
->ep
, F_RCV_ON
);