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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
23 #include "../common/common.h"
26 static int v_ecl(SCR *);
27 static int v_ecl_init(SCR *);
28 static int v_ecl_log(SCR *, TEXT *);
29 static int v_ex_done(SCR *, VICMD *);
30 static int v_exec_ex(SCR *, VICMD *, EXCMD *);
34 * Repeat the previous substitution.
36 * PUBLIC: int v_again(SCR *, VICMD *);
39 v_again(SCR *sp, VICMD *vp)
43 ex_cinit(sp, &cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1);
44 argv_exp0(sp, &cmd, L(""), 1);
45 return (v_exec_ex(sp, vp, &cmd));
50 * Switch the editor into EX mode.
52 * PUBLIC: int v_exmode(SCR *, VICMD *);
55 v_exmode(SCR *sp, VICMD *vp)
61 /* Try and switch screens -- the screen may not permit it. */
62 if (gp->scr_screen(sp, SC_EX)) {
64 "207|The Q command requires the ex terminal interface");
67 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
69 /* Save the current cursor position. */
70 sp->frp->lno = sp->lno;
71 sp->frp->cno = sp->cno;
72 F_SET(sp->frp, FR_CURSORSET);
74 /* Switch to ex mode. */
75 F_CLR(sp, SC_VI | SC_SCR_VI);
78 /* Move out of the vi screen. */
79 (void)ex_puts(sp, "\n");
86 * Join lines together.
88 * PUBLIC: int v_join(SCR *, VICMD *);
91 v_join(SCR *sp, VICMD *vp)
98 * The general rule is that '#J' joins # lines, counting the current
99 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
100 * current and next lines. This doesn't map well into the ex command
101 * (which takes two line numbers), so we handle it here. Note that
102 * we never test for EOF -- historically going past the end of file
105 lno = vp->m_start.lno + 1;
106 if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
107 lno = vp->m_start.lno + (vp->count - 1);
109 ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
110 return (v_exec_ex(sp, vp, &cmd));
114 * v_shiftl -- [count]<motion
117 * PUBLIC: int v_shiftl(SCR *, VICMD *);
120 v_shiftl(SCR *sp, VICMD *vp)
124 ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
125 argv_exp0(sp, &cmd, L("<"), 2);
126 return (v_exec_ex(sp, vp, &cmd));
130 * v_shiftr -- [count]>motion
133 * PUBLIC: int v_shiftr(SCR *, VICMD *);
136 v_shiftr(SCR *sp, VICMD *vp)
140 ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
141 argv_exp0(sp, &cmd, L(">"), 2);
142 return (v_exec_ex(sp, vp, &cmd));
149 * PUBLIC: int v_suspend(SCR *, VICMD *);
152 v_suspend(SCR *sp, VICMD *vp)
156 ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
157 argv_exp0(sp, &cmd, L("suspend"), SIZE(L("suspend")));
158 return (v_exec_ex(sp, vp, &cmd));
163 * Switch to the previous file.
165 * PUBLIC: int v_switch(SCR *, VICMD *);
168 v_switch(SCR *sp, VICMD *vp)
176 * Try the alternate file name, then the previous file
177 * name. Use the real name, not the user's current name.
179 if ((name = sp->alt_name) == NULL) {
180 msgq(sp, M_ERR, "180|No previous file to edit");
184 /* If autowrite is set, write out the file. */
185 if (file_m1(sp, 0, FS_ALL))
188 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
189 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
190 argv_exp0(sp, &cmd, wp, wlen);
191 return (v_exec_ex(sp, vp, &cmd));
196 * Do a tag search on the cursor keyword.
198 * PUBLIC: int v_tagpush(SCR *, VICMD *);
201 v_tagpush(SCR *sp, VICMD *vp)
205 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
206 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1);
207 return (v_exec_ex(sp, vp, &cmd));
212 * Pop the tags stack.
214 * PUBLIC: int v_tagpop(SCR *, VICMD *);
217 v_tagpop(SCR *sp, VICMD *vp)
221 ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
222 return (v_exec_ex(sp, vp, &cmd));
226 * v_filter -- [count]!motion command(s)
227 * Run range through shell commands, replacing text.
229 * PUBLIC: int v_filter(SCR *, VICMD *);
232 v_filter(SCR *sp, VICMD *vp)
239 * Historical vi permitted "!!" in an empty file, and it's handled
240 * as a special case in the ex_bang routine. Don't modify this setup
241 * without understanding that one. In particular, note that we're
242 * manipulating the ex argument structures behind ex's back.
245 * Historical vi did not permit the '!' command to be associated with
246 * a non-line oriented motion command, in general, although it did
247 * with search commands. So, !f; and !w would fail, but !/;<CR>
248 * would succeed, even if they all moved to the same location in the
249 * current line. I don't see any reason to disallow '!' using any of
250 * the possible motion commands.
253 * Historical vi ran the last bang command if N or n was used as the
256 if (F_ISSET(vp, VC_ISDOT) ||
257 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
259 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
260 EXP(sp)->argsoff = 0; /* XXX */
262 if (argv_exp1(sp, &cmd, L("!"), 1, 1))
264 cmd.argc = EXP(sp)->argsoff; /* XXX */
265 cmd.argv = EXP(sp)->args; /* XXX */
266 return (v_exec_ex(sp, vp, &cmd));
269 /* Get the command from the user. */
271 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
275 * Check to see if the user changed their mind.
278 * Entering <escape> on an empty line was historically an error,
279 * this implementation doesn't bother.
281 tp = TAILQ_FIRST(sp->tiq);
282 if (tp->term != TERM_OK) {
283 vp->m_final.lno = sp->lno;
284 vp->m_final.cno = sp->cno;
288 /* Home the cursor. */
291 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
292 EXP(sp)->argsoff = 0; /* XXX */
294 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
296 cmd.argc = EXP(sp)->argsoff; /* XXX */
297 cmd.argv = EXP(sp)->args; /* XXX */
298 return (v_exec_ex(sp, vp, &cmd));
303 * Execute an ex command.
306 v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
310 rval = exp->cmd->fn(sp, exp);
311 return (v_ex_done(sp, vp) || rval);
316 * Execute a colon command line.
318 * PUBLIC: int v_ex(SCR *, VICMD *);
321 v_ex(SCR *sp, VICMD *vp)
325 int do_cedit, do_resolution, ifcontinue;
331 * If we put out more than a single line of messages, or ex trashes
332 * the screen, the user may continue entering ex commands. We find
333 * this out when we do the screen/message resolution. We can't enter
334 * completely into ex mode however, because the user can elect to
335 * return into vi mode by entering any key, i.e. we have to be in raw
338 for (do_cedit = do_resolution = 0;;) {
341 * There may already be an ex command waiting to run. If
342 * so, we continue with it.
344 if (!EXCMD_RUNNING(gp)) {
346 if (v_tcmd(sp, vp, ':',
347 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
349 tp = TAILQ_FIRST(sp->tiq);
352 * If the user entered a single <esc>, they want to
353 * edit their colon command history. If they already
354 * entered some text, move it into the edit history.
356 if (tp->term == TERM_CEDIT) {
357 if (tp->len > 1 && v_ecl_log(sp, tp))
363 /* If the user didn't enter anything, return. */
364 if (tp->term == TERM_BS)
367 /* If the user changed their mind, return. */
368 if (tp->term != TERM_OK)
371 /* Log the command. */
372 if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
375 /* Push a command on the command stack. */
376 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
380 /* Home the cursor. */
385 * If the editor wrote the screen behind curses back, put out
386 * a <newline> so that we don't overwrite the user's command
387 * with its output or the next want-to-continue? message. This
388 * doesn't belong here, but I can't find another place to put
389 * it. See, we resolved the output from the last ex command,
390 * and the user entered another one. This is the only place
391 * where we have control before the ex command writes output.
392 * We could get control in vs_msg(), but we have no way to know
393 * if command didn't put out any output when we try and resolve
394 * this command. This fixes a bug where combinations of ex
395 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
397 if (F_ISSET(sp, SC_SCR_EXWROTE))
400 /* Call the ex parser. */
403 /* Flush ex messages. */
406 /* Resolve any messages. */
407 if (vs_ex_resolve(sp, &ifcontinue))
411 * Continue or return. If continuing, make sure that we
412 * eventually do resolution.
418 /* If we're continuing, it's a new command. */
423 * If the user previously continued an ex command, we have to do
424 * resolution to clean up the screen. Don't wait, we already did
428 F_SET(sp, SC_EX_WAIT_NO);
429 if (vs_ex_resolve(sp, &ifcontinue))
433 /* Cleanup from the ex command. */
434 if (v_ex_done(sp, vp))
437 /* The user may want to edit their colon command history. */
446 * Cleanup from an ex command.
449 v_ex_done(SCR *sp, VICMD *vp)
454 * The only cursor modifications are real, however, the underlying
455 * line may have changed; don't trust anything. This code has been
456 * a remarkably fertile place for bugs. Do a reality check on a
457 * cursor value, and make sure it's okay. If necessary, change it.
458 * Ex keeps track of the line number, but it cares less about the
459 * column and it may have disappeared.
461 * Don't trust ANYTHING.
464 * Ex will soon have to start handling the column correctly; see
465 * the POSIX 1003.2 standard.
467 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
470 } else if (sp->cno >= len)
471 sp->cno = len ? len - 1 : 0;
473 vp->m_final.lno = sp->lno;
474 vp->m_final.cno = sp->cno;
477 * Don't re-adjust the cursor after executing an ex command,
478 * and ex movements are permanent.
480 F_CLR(vp, VM_RCM_MASK);
481 F_SET(vp, VM_RCM_SET);
488 * Start an edit window on the colon command-line commands.
496 /* Initialize the screen, if necessary. */
498 if (gp->ccl_sp == NULL && v_ecl_init(sp))
501 /* Get a new screen. */
502 if (screen_init(gp, sp, &new))
504 if (vs_split(sp, new, 1)) {
505 (void)screen_end(new);
509 /* Attach to the screen. */
510 new->ep = gp->ccl_sp->ep;
513 new->frp = gp->ccl_sp->frp;
514 new->frp->flags = sp->frp->flags;
516 /* Move the cursor to the end. */
517 (void)db_last(new, &new->lno);
521 /* Remember the originating window. */
524 /* It's a special window. */
525 F_SET(new, SC_COMEDIT);
527 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
528 /* Bypass iconv on writing to DB. */
529 o_set(new, O_FILEENCODING, OS_STRDUP, codeset(), 0);
532 /* Set up the switch. */
534 F_SET(sp, SC_SSWITCH);
540 * Execute a command from a colon command-line window.
542 * PUBLIC: int v_ecl_exec(SCR *);
550 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
551 v_emsg(sp, NULL, VIM_EMPTY);
555 msgq(sp, M_BERR, "307|No ex command to execute");
559 /* Push the command on the command stack. */
560 if (ex_run_str(sp, NULL, p, len, 0, 0))
563 /* Set up the switch. */
564 sp->nextdisp = sp->ccl_parent;
571 * Log a command into the colon command-line log file.
574 v_ecl_log(SCR *sp, TEXT *tp)
582 /* Initialize the screen, if necessary. */
583 if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
586 ccl_sp = sp->gp->ccl_sp;
589 * Don't log colon command window commands into the colon command
592 if (sp->ep == ccl_sp->ep)
595 if (db_last(ccl_sp, &lno)) {
598 /* Don't log line that is identical to previous one */
600 !db_get(ccl_sp, lno, 0, &p, &len) &&
602 !MEMCMP(tp->lb, p, len))
605 rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len);
606 /* XXXX end "transaction" on ccl */
607 /* Is this still necessary now that we no longer hijack sp ? */
616 * Initialize the colon command-line log file.
626 /* Get a temporary file. */
627 if ((frp = file_add(sp, NULL)) == NULL)
632 * Create a screen -- the file initialization code wants one.
634 if (screen_init(gp, sp, &gp->ccl_sp))
636 if (file_init(gp->ccl_sp, frp, NULL, 0)) {
637 (void)screen_end(gp->ccl_sp);
642 /* The underlying file isn't recoverable. */
643 F_CLR(gp->ccl_sp->ep, F_RCV_ON);