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.61 2011/12/22 18:41:53 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
30 static int v_ecl(SCR
*);
31 static int v_ecl_init(SCR
*);
32 static int v_ecl_log(SCR
*, TEXT
*);
33 static int v_ex_done(SCR
*, VICMD
*);
34 static int v_exec_ex(SCR
*, VICMD
*, EXCMD
*);
38 * Repeat the previous substitution.
40 * PUBLIC: int v_again(SCR *, VICMD *);
43 v_again(SCR
*sp
, VICMD
*vp
)
47 ex_cinit(sp
, &cmd
, C_SUBAGAIN
, 2, vp
->m_start
.lno
, vp
->m_start
.lno
, 1);
48 argv_exp0(sp
, &cmd
, L(""), 1);
49 return (v_exec_ex(sp
, vp
, &cmd
));
54 * Switch the editor into EX mode.
56 * PUBLIC: int v_exmode(SCR *, VICMD *);
59 v_exmode(SCR
*sp
, VICMD
*vp
)
65 /* Try and switch screens -- the screen may not permit it. */
66 if (gp
->scr_screen(sp
, SC_EX
)) {
68 "207|The Q command requires the ex terminal interface");
71 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 0);
73 /* Save the current cursor position. */
74 sp
->frp
->lno
= sp
->lno
;
75 sp
->frp
->cno
= sp
->cno
;
76 F_SET(sp
->frp
, FR_CURSORSET
);
78 /* Switch to ex mode. */
79 F_CLR(sp
, SC_VI
| SC_SCR_VI
);
82 /* Move out of the vi screen. */
83 (void)ex_puts(sp
, "\n");
90 * Join lines together.
92 * PUBLIC: int v_join(SCR *, VICMD *);
95 v_join(SCR
*sp
, VICMD
*vp
)
102 * The general rule is that '#J' joins # lines, counting the current
103 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
104 * current and next lines. This doesn't map well into the ex command
105 * (which takes two line numbers), so we handle it here. Note that
106 * we never test for EOF -- historically going past the end of file
109 lno
= vp
->m_start
.lno
+ 1;
110 if (F_ISSET(vp
, VC_C1SET
) && vp
->count
> 2)
111 lno
= vp
->m_start
.lno
+ (vp
->count
- 1);
113 ex_cinit(sp
, &cmd
, C_JOIN
, 2, vp
->m_start
.lno
, lno
, 0);
114 return (v_exec_ex(sp
, vp
, &cmd
));
118 * v_shiftl -- [count]<motion
121 * PUBLIC: int v_shiftl(SCR *, VICMD *);
124 v_shiftl(SCR
*sp
, VICMD
*vp
)
128 ex_cinit(sp
, &cmd
, C_SHIFTL
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
129 argv_exp0(sp
, &cmd
, L("<"), 2);
130 return (v_exec_ex(sp
, vp
, &cmd
));
134 * v_shiftr -- [count]>motion
137 * PUBLIC: int v_shiftr(SCR *, VICMD *);
140 v_shiftr(SCR
*sp
, VICMD
*vp
)
144 ex_cinit(sp
, &cmd
, C_SHIFTR
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
145 argv_exp0(sp
, &cmd
, L(">"), 2);
146 return (v_exec_ex(sp
, vp
, &cmd
));
153 * PUBLIC: int v_suspend(SCR *, VICMD *);
156 v_suspend(SCR
*sp
, VICMD
*vp
)
160 ex_cinit(sp
, &cmd
, C_STOP
, 0, OOBLNO
, OOBLNO
, 0);
161 argv_exp0(sp
, &cmd
, L("suspend"), SIZE(L("suspend")));
162 return (v_exec_ex(sp
, vp
, &cmd
));
167 * Switch to the previous file.
169 * PUBLIC: int v_switch(SCR *, VICMD *);
172 v_switch(SCR
*sp
, VICMD
*vp
)
180 * Try the alternate file name, then the previous file
181 * name. Use the real name, not the user's current name.
183 if ((name
= sp
->alt_name
) == NULL
) {
184 msgq(sp
, M_ERR
, "180|No previous file to edit");
188 /* If autowrite is set, write out the file. */
189 if (file_m1(sp
, 0, FS_ALL
))
192 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
193 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
194 argv_exp0(sp
, &cmd
, wp
, wlen
);
195 return (v_exec_ex(sp
, vp
, &cmd
));
200 * Do a tag search on the cursor keyword.
202 * PUBLIC: int v_tagpush(SCR *, VICMD *);
205 v_tagpush(SCR
*sp
, VICMD
*vp
)
209 ex_cinit(sp
, &cmd
, C_TAG
, 0, OOBLNO
, 0, 0);
210 argv_exp0(sp
, &cmd
, VIP(sp
)->keyw
, STRLEN(VIP(sp
)->keyw
) + 1);
211 return (v_exec_ex(sp
, vp
, &cmd
));
216 * Pop the tags stack.
218 * PUBLIC: int v_tagpop(SCR *, VICMD *);
221 v_tagpop(SCR
*sp
, VICMD
*vp
)
225 ex_cinit(sp
, &cmd
, C_TAGPOP
, 0, OOBLNO
, 0, 0);
226 return (v_exec_ex(sp
, vp
, &cmd
));
230 * v_filter -- [count]!motion command(s)
231 * Run range through shell commands, replacing text.
233 * PUBLIC: int v_filter(SCR *, VICMD *);
236 v_filter(SCR
*sp
, VICMD
*vp
)
243 * Historical vi permitted "!!" in an empty file, and it's handled
244 * as a special case in the ex_bang routine. Don't modify this setup
245 * without understanding that one. In particular, note that we're
246 * manipulating the ex argument structures behind ex's back.
249 * Historical vi did not permit the '!' command to be associated with
250 * a non-line oriented motion command, in general, although it did
251 * with search commands. So, !f; and !w would fail, but !/;<CR>
252 * would succeed, even if they all moved to the same location in the
253 * current line. I don't see any reason to disallow '!' using any of
254 * the possible motion commands.
257 * Historical vi ran the last bang command if N or n was used as the
260 if (F_ISSET(vp
, VC_ISDOT
) ||
261 ISCMD(vp
->rkp
, 'N') || ISCMD(vp
->rkp
, 'n')) {
263 &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
264 EXP(sp
)->argsoff
= 0; /* XXX */
266 if (argv_exp1(sp
, &cmd
, L("!"), 1, 1))
268 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
269 cmd
.argv
= EXP(sp
)->args
; /* XXX */
270 return (v_exec_ex(sp
, vp
, &cmd
));
273 /* Get the command from the user. */
275 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
279 * Check to see if the user changed their mind.
282 * Entering <escape> on an empty line was historically an error,
283 * this implementation doesn't bother.
285 tp
= TAILQ_FIRST(sp
->tiq
);
286 if (tp
->term
!= TERM_OK
) {
287 vp
->m_final
.lno
= sp
->lno
;
288 vp
->m_final
.cno
= sp
->cno
;
292 /* Home the cursor. */
295 ex_cinit(sp
, &cmd
, C_BANG
, 2, vp
->m_start
.lno
, vp
->m_stop
.lno
, 0);
296 EXP(sp
)->argsoff
= 0; /* XXX */
298 if (argv_exp1(sp
, &cmd
, tp
->lb
+ 1, tp
->len
- 1, 1))
300 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
301 cmd
.argv
= EXP(sp
)->args
; /* XXX */
302 return (v_exec_ex(sp
, vp
, &cmd
));
307 * Execute an ex command.
310 v_exec_ex(SCR
*sp
, VICMD
*vp
, EXCMD
*exp
)
314 rval
= exp
->cmd
->fn(sp
, exp
);
315 return (v_ex_done(sp
, vp
) || rval
);
320 * Execute a colon command line.
322 * PUBLIC: int v_ex(SCR *, VICMD *);
325 v_ex(SCR
*sp
, VICMD
*vp
)
329 int do_cedit
, do_resolution
, ifcontinue
;
335 * If we put out more than a single line of messages, or ex trashes
336 * the screen, the user may continue entering ex commands. We find
337 * this out when we do the screen/message resolution. We can't enter
338 * completely into ex mode however, because the user can elect to
339 * return into vi mode by entering any key, i.e. we have to be in raw
342 for (do_cedit
= do_resolution
= 0;;) {
345 * There may already be an ex command waiting to run. If
346 * so, we continue with it.
348 if (!EXCMD_RUNNING(gp
)) {
350 if (v_tcmd(sp
, vp
, ':',
351 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
353 tp
= TAILQ_FIRST(sp
->tiq
);
356 * If the user entered a single <esc>, they want to
357 * edit their colon command history. If they already
358 * entered some text, move it into the edit history.
360 if (tp
->term
== TERM_CEDIT
) {
361 if (tp
->len
> 1 && v_ecl_log(sp
, tp
))
367 /* If the user didn't enter anything, return. */
368 if (tp
->term
== TERM_BS
)
371 /* If the user changed their mind, return. */
372 if (tp
->term
!= TERM_OK
)
375 /* Log the command. */
376 if (O_STR(sp
, O_CEDIT
) != NULL
&& v_ecl_log(sp
, tp
))
379 /* Push a command on the command stack. */
380 if (ex_run_str(sp
, NULL
, tp
->lb
, tp
->len
, 0, 1))
384 /* Home the cursor. */
389 * If the editor wrote the screen behind curses back, put out
390 * a <newline> so that we don't overwrite the user's command
391 * with its output or the next want-to-continue? message. This
392 * doesn't belong here, but I can't find another place to put
393 * it. See, we resolved the output from the last ex command,
394 * and the user entered another one. This is the only place
395 * where we have control before the ex command writes output.
396 * We could get control in vs_msg(), but we have no way to know
397 * if command didn't put out any output when we try and resolve
398 * this command. This fixes a bug where combinations of ex
399 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
401 if (F_ISSET(sp
, SC_SCR_EXWROTE
))
404 /* Call the ex parser. */
407 /* Flush ex messages. */
410 /* Resolve any messages. */
411 if (vs_ex_resolve(sp
, &ifcontinue
))
415 * Continue or return. If continuing, make sure that we
416 * eventually do resolution.
422 /* If we're continuing, it's a new command. */
427 * If the user previously continued an ex command, we have to do
428 * resolution to clean up the screen. Don't wait, we already did
432 F_SET(sp
, SC_EX_WAIT_NO
);
433 if (vs_ex_resolve(sp
, &ifcontinue
))
437 /* Cleanup from the ex command. */
438 if (v_ex_done(sp
, vp
))
441 /* The user may want to edit their colon command history. */
450 * Cleanup from an ex command.
453 v_ex_done(SCR
*sp
, VICMD
*vp
)
458 * The only cursor modifications are real, however, the underlying
459 * line may have changed; don't trust anything. This code has been
460 * a remarkably fertile place for bugs. Do a reality check on a
461 * cursor value, and make sure it's okay. If necessary, change it.
462 * Ex keeps track of the line number, but it cares less about the
463 * column and it may have disappeared.
465 * Don't trust ANYTHING.
468 * Ex will soon have to start handling the column correctly; see
469 * the POSIX 1003.2 standard.
471 if (db_eget(sp
, sp
->lno
, NULL
, &len
, NULL
)) {
474 } else if (sp
->cno
>= len
)
475 sp
->cno
= len
? len
- 1 : 0;
477 vp
->m_final
.lno
= sp
->lno
;
478 vp
->m_final
.cno
= sp
->cno
;
481 * Don't re-adjust the cursor after executing an ex command,
482 * and ex movements are permanent.
484 F_CLR(vp
, VM_RCM_MASK
);
485 F_SET(vp
, VM_RCM_SET
);
492 * Start an edit window on the colon command-line commands.
500 /* Initialize the screen, if necessary. */
502 if (gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
505 /* Get a new screen. */
506 if (screen_init(gp
, sp
, &new))
508 if (vs_split(sp
, new, 1)) {
509 (void)screen_end(new);
513 /* Attach to the screen. */
514 new->ep
= gp
->ccl_sp
->ep
;
517 new->frp
= gp
->ccl_sp
->frp
;
518 new->frp
->flags
= sp
->frp
->flags
;
520 /* Move the cursor to the end. */
521 (void)db_last(new, &new->lno
);
525 /* Remember the originating window. */
528 /* It's a special window. */
529 F_SET(new, SC_COMEDIT
);
531 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
532 /* Bypass iconv on writing to DB. */
533 o_set(new, O_FILEENCODING
, OS_STRDUP
, codeset(), 0);
536 /* Set up the switch. */
538 F_SET(sp
, SC_SSWITCH
);
544 * Execute a command from a colon command-line window.
546 * PUBLIC: int v_ecl_exec(SCR *);
554 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
555 v_emsg(sp
, NULL
, VIM_EMPTY
);
559 msgq(sp
, M_BERR
, "307|No ex command to execute");
563 /* Push the command on the command stack. */
564 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
567 /* Set up the switch. */
568 sp
->nextdisp
= sp
->ccl_parent
;
575 * Log a command into the colon command-line log file.
578 v_ecl_log(SCR
*sp
, TEXT
*tp
)
586 /* Initialize the screen, if necessary. */
587 if (sp
->gp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
590 ccl_sp
= sp
->gp
->ccl_sp
;
593 * Don't log colon command window commands into the colon command
596 if (sp
->ep
== ccl_sp
->ep
)
599 if (db_last(ccl_sp
, &lno
)) {
602 /* Don't log line that is identical to previous one */
604 !db_get(ccl_sp
, lno
, 0, &p
, &len
) &&
606 !MEMCMP(tp
->lb
, p
, len
))
609 rval
= db_append(ccl_sp
, 0, lno
, tp
->lb
, tp
->len
);
610 /* XXXX end "transaction" on ccl */
611 /* Is this still necessary now that we no longer hijack sp ? */
620 * Initialize the colon command-line log file.
630 /* Get a temporary file. */
631 if ((frp
= file_add(sp
, NULL
)) == NULL
)
636 * Create a screen -- the file initialization code wants one.
638 if (screen_init(gp
, sp
, &gp
->ccl_sp
))
640 if (file_init(gp
->ccl_sp
, frp
, NULL
, 0)) {
641 (void)screen_end(gp
->ccl_sp
);
646 /* The underlying file isn't recoverable. */
647 F_CLR(gp
->ccl_sp
->ep
, F_RCV_ON
);