1 /* $NetBSD: v_ex.c,v 1.3 2008/12/05 22:51:43 christos Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
29 #include "../common/common.h"
32 static int v_ecl
__P((SCR
*));
33 static int v_ecl_init
__P((SCR
*));
34 static int v_ecl_log
__P((SCR
*, TEXT
*));
35 static int v_ex_done
__P((SCR
*, VICMD
*));
39 * Repeat the previous substitution.
41 * PUBLIC: int v_again __P((SCR *, VICMD *));
44 v_again(SCR
*sp
, VICMD
*vp
)
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 *));
61 v_exmode(SCR
*sp
, VICMD
*vp
)
67 /* Try and switch screens -- the screen may not permit it. */
68 if (gp
->scr_screen(sp
, SC_EX
)) {
70 "207|The Q command requires the ex terminal interface");
73 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
75 /* Save the current cursor position. */
76 sp
->frp
->lno
= sp
->lno
;
77 sp
->frp
->cno
= sp
->cno
;
78 F_SET(sp
->frp
, FR_CURSORSET
);
80 /* Switch to ex mode. */
81 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
84 /* Move out of the vi screen. */
85 (void)ex_puts(sp
, "\n");
92 * Join lines together.
94 * PUBLIC: int v_join __P((SCR *, VICMD *));
97 v_join(SCR
*sp
, VICMD
*vp
)
104 * The general rule is that '#J' joins # lines, counting the current
105 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
106 * current and next lines. This doesn't map well into the ex command
107 * (which takes two line numbers), so we handle it here. Note that
108 * we never test for EOF -- historically going past the end of file
111 lno
= vp
->m_start
.lno
+ 1;
112 if (F_ISSET(vp
, VC_C1SET
) && vp
->count
> 2)
113 lno
= vp
->m_start
.lno
+ (vp
->count
- 1);
115 ex_cinit(sp
, &cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0);
116 return (v_exec_ex(sp
, vp
, &cmd
));
120 * v_shiftl -- [count]<motion
123 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
126 v_shiftl(SCR
*sp
, VICMD
*vp
)
129 static CHAR_T lt
[] = {'<', 0};
131 ex_cinit(sp
, &cmd
, C_SHIFTL
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
132 argv_exp0(sp
, &cmd
, lt
, 2);
133 return (v_exec_ex(sp
, vp
, &cmd
));
137 * v_shiftr -- [count]>motion
140 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
143 v_shiftr(SCR
*sp
, VICMD
*vp
)
146 static CHAR_T gt
[] = {'>', 0};
148 ex_cinit(sp
, &cmd
, C_SHIFTR
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
149 argv_exp0(sp
, &cmd
, gt
, 2);
150 return (v_exec_ex(sp
, vp
, &cmd
));
157 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
160 v_suspend(SCR
*sp
, VICMD
*vp
)
163 static CHAR_T suspend
[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
165 ex_cinit(sp
, &cmd
, C_STOP
, 0, OOBLNO
, OOBLNO
, 0);
166 argv_exp0(sp
, &cmd
, suspend
, sizeof(suspend
)/sizeof(CHAR_T
));
167 return (v_exec_ex(sp
, vp
, &cmd
));
172 * Switch to the previous file.
174 * PUBLIC: int v_switch __P((SCR *, VICMD *));
177 v_switch(SCR
*sp
, VICMD
*vp
)
185 * Try the alternate file name, then the previous file
186 * name. Use the real name, not the user's current name.
188 if ((name
= sp
->alt_name
) == NULL
) {
189 msgq(sp
, M_ERR
, "180|No previous file to edit");
193 /* If autowrite is set, write out the file. */
194 if (file_m1(sp
, 0, FS_ALL
))
197 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
198 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
199 argv_exp0(sp
, &cmd
, wp
, wlen
);
200 return (v_exec_ex(sp
, vp
, &cmd
));
205 * Do a tag search on the cursor keyword.
207 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
210 v_tagpush(SCR
*sp
, VICMD
*vp
)
215 if (O_ISSET(sp
, O_GTAGSMODE
) && vp
->m_start
.cno
== 0)
216 ex_cinit(sp
, &cmd
, C_RTAG
, 0, OOBLNO
, 0, 0);
219 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, 0, 0);
220 argv_exp0(sp
, &cmd
, VIP(sp
)->keyw
, STRLEN(VIP(sp
)->keyw
) + 1);
221 return (v_exec_ex(sp
, vp
, &cmd
));
226 * Pop the tags stack.
228 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
231 v_tagpop(SCR
*sp
, VICMD
*vp
)
235 ex_cinit(sp
, &cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0);
236 return (v_exec_ex(sp
, vp
, &cmd
));
240 * v_filter -- [count]!motion command(s)
241 * Run range through shell commands, replacing text.
243 * PUBLIC: int v_filter __P((SCR *, VICMD *));
246 v_filter(SCR
*sp
, VICMD
*vp
)
253 * Historical vi permitted "!!" in an empty file, and it's handled
254 * as a special case in the ex_bang routine. Don't modify this setup
255 * without understanding that one. In particular, note that we're
256 * manipulating the ex argument structures behind ex's back.
259 * Historical vi did not permit the '!' command to be associated with
260 * a non-line oriented motion command, in general, although it did
261 * with search commands. So, !f; and !w would fail, but !/;<CR>
262 * would succeed, even if they all moved to the same location in the
263 * current line. I don't see any reason to disallow '!' using any of
264 * the possible motion commands.
267 * Historical vi ran the last bang command if N or n was used as the
270 if (F_ISSET(vp
, VC_ISDOT
) ||
271 ISCMD(vp
->rkp
, 'N') || ISCMD(vp
->rkp
, 'n')) {
272 static CHAR_T bang
[] = {'!', 0};
274 &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
275 EXP(sp
)->argsoff
= 0; /* XXX */
277 if (argv_exp1(sp
, &cmd
, bang
, 1, 1))
279 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
280 cmd
.argv
= EXP(sp
)->args
; /* XXX */
281 return (v_exec_ex(sp
, vp
, &cmd
));
284 /* Get the command from the user. */
286 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
290 * Check to see if the user changed their mind.
293 * Entering <escape> on an empty line was historically an error,
294 * this implementation doesn't bother.
296 tp
= sp
->tiq
.cqh_first
;
297 if (tp
->term
!= TERM_OK
) {
298 vp
->m_final
.lno
= sp
->lno
;
299 vp
->m_final
.cno
= sp
->cno
;
303 /* Home the cursor. */
306 ex_cinit(sp
, &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
307 EXP(sp
)->argsoff
= 0; /* XXX */
309 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
311 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
312 cmd
.argv
= EXP(sp
)->args
; /* XXX */
313 return (v_exec_ex(sp
, vp
, &cmd
));
318 * Execute an ex command.
320 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
323 v_exec_ex(SCR
*sp
, VICMD
*vp
, EXCMD
*exp
)
327 rval
= exp
->cmd
->fn(sp
, exp
);
328 return (v_ex_done(sp
, vp
) || rval
);
333 * Execute a colon command line.
335 * PUBLIC: int v_ex __P((SCR *, VICMD *));
338 v_ex(SCR
*sp
, VICMD
*vp
)
342 int do_cedit
, do_resolution
, ifcontinue
;
348 * If we put out more than a single line of messages, or ex trashes
349 * the screen, the user may continue entering ex commands. We find
350 * this out when we do the screen/message resolution. We can't enter
351 * completely into ex mode however, because the user can elect to
352 * return into vi mode by entering any key, i.e. we have to be in raw
355 for (do_cedit
= do_resolution
= 0;;) {
358 * There may already be an ex command waiting to run. If
359 * so, we continue with it.
361 if (!EXCMD_RUNNING(wp
)) {
363 if (v_tcmd(sp
, vp
, ':',
364 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
366 tp
= sp
->tiq
.cqh_first
;
369 * If the user entered a single <esc>, they want to
370 * edit their colon command history. If they already
371 * entered some text, move it into the edit history.
373 if (tp
->term
== TERM_CEDIT
) {
374 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
380 /* If the user didn't enter anything, return. */
381 if (tp
->term
== TERM_BS
)
384 /* Log the command. */
385 if (O_STR(sp
, O_CEDIT
) != NULL
)
386 (void)v_ecl_log(sp
, tp
);
388 /* Push a command on the command stack. */
389 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
393 /* Home the cursor. */
398 * If the editor wrote the screen behind curses back, put out
399 * a <newline> so that we don't overwrite the user's command
400 * with its output or the next want-to-continue? message. This
401 * doesn't belong here, but I can't find another place to put
402 * it. See, we resolved the output from the last ex command,
403 * and the user entered another one. This is the only place
404 * where we have control before the ex command writes output.
405 * We could get control in vs_msg(), but we have no way to know
406 * if command didn't put out any output when we try and resolve
407 * this command. This fixes a bug where combinations of ex
408 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
410 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
413 /* Call the ex parser. */
416 /* Flush ex messages. */
419 /* Resolve any messages. */
420 if (vs_ex_resolve(sp
, &ifcontinue
))
424 * Continue or return. If continuing, make sure that we
425 * eventually do resolution.
431 /* If we're continuing, it's a new command. */
436 * If the user previously continued an ex command, we have to do
437 * resolution to clean up the screen. Don't wait, we already did
441 F_SET(sp
, SC_EX_WAIT_NO
);
442 if (vs_ex_resolve(sp
, &ifcontinue
))
446 /* Cleanup from the ex command. */
447 if (v_ex_done(sp
, vp
))
450 /* The user may want to edit their colon command history. */
459 * Cleanup from an ex command.
462 v_ex_done(SCR
*sp
, VICMD
*vp
)
467 * The only cursor modifications are real, however, the underlying
468 * line may have changed; don't trust anything. This code has been
469 * a remarkably fertile place for bugs. Do a reality check on a
470 * cursor value, and make sure it's okay. If necessary, change it.
471 * Ex keeps track of the line number, but it cares less about the
472 * column and it may have disappeared.
474 * Don't trust ANYTHING.
477 * Ex will soon have to start handling the column correctly; see
478 * the POSIX 1003.2 standard.
480 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
483 } else if (sp
->cno
>= len
)
484 sp
->cno
= len
? len
- 1 : 0;
486 vp
->m_final
.lno
= sp
->lno
;
487 vp
->m_final
.cno
= sp
->cno
;
490 * Don't re-adjust the cursor after executing an ex command,
491 * and ex movements are permanent.
493 F_CLR(vp
, VM_RCM_MASK
);
494 F_SET(vp
, VM_RCM_SET
);
501 * Start an edit window on the colon command-line commands.
510 /* Initialize the screen, if necessary. */
513 if (wp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
516 /* Get a new screen. */
517 if (screen_init(gp
, sp
, &new))
519 if (vs_split(sp
, new, 1)) {
520 (void)screen_end(new);
524 /* Attach to the screen. */
525 new->ep
= wp
->ccl_sp
->ep
;
527 CIRCLEQ_INSERT_HEAD(&new->ep
->scrq
, new, eq
);
529 new->frp
= wp
->ccl_sp
->frp
;
530 new->frp
->flags
= sp
->frp
->flags
;
531 new->conv
= wp
->ccl_sp
->conv
;
533 /* Move the cursor to the end. */
534 (void)db_last(new, &new->lno
);
538 /* Remember the originating window. */
541 /* It's a special window. */
542 F_SET(new, SC_COMEDIT
);
544 /* Don't encode on writing to DB. */
545 o_set(new, O_FILEENCODING
, OS_STRDUP
, "WCHAR_T", 0);
547 /* Set up the switch. */
549 F_SET(sp
, SC_SSWITCH
);
555 * Execute a command from a colon command-line window.
557 * PUBLIC: int v_ecl_exec __P((SCR *));
565 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
566 v_emsg(sp
, NULL
, VIM_EMPTY
);
570 msgq(sp
, M_BERR
, "307|No ex command to execute");
574 /* Push the command on the command stack. */
575 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
578 /* Set up the switch. */
579 sp
->nextdisp
= sp
->ccl_parent
;
586 * Log a command into the colon command-line log file.
589 v_ecl_log(SCR
*sp
, TEXT
*tp
)
597 /* Initialize the screen, if necessary. */
598 if (sp
->wp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
601 ccl_sp
= sp
->wp
->ccl_sp
;
604 * Don't log colon command window commands into the colon command
607 if (sp
->ep
== ccl_sp
->ep
)
610 if (db_last(ccl_sp
, &lno
)) {
613 /* Don't log line that is identical to previous one */
615 !db_get(ccl_sp
, lno
, 0, &p
, &len
) &&
617 !MEMCMP(tp
->lb
, p
, len
))
620 rval
= db_append(ccl_sp
, 0, lno
, tp
->lb
, tp
->len
);
621 /* XXXX end "transaction" on ccl */
622 /* Is this still necessary now that we no longer hijack sp ? */
631 * Initialize the colon command-line log file.
643 /* Get a temporary file. */
644 if ((frp
= file_add(sp
, NULL
)) == NULL
)
649 * Create a screen -- the file initialization code wants one.
651 if (screen_init(gp
, sp
, &wp
->ccl_sp
))
653 conv_enc(wp
->ccl_sp
, O_FILEENCODING
, "WCHAR_T");
654 if (file_init(wp
->ccl_sp
, frp
, NULL
, 0)) {
655 (void)screen_end(wp
->ccl_sp
);
660 /* The underlying file isn't recoverable. */
661 F_CLR(wp
->ccl_sp
->ep
, F_RCV_ON
);