*** empty log message ***
[nvi.git] / vi / v_ex.c
blobe7cb6f92fd18f4ef0064b2935877035b7b721d98
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_ex.c,v 10.47 2000/06/27 17:19:07 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:07 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "../common/common.h"
28 #include "vi.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 *));
36 * v_again -- &
37 * Repeat the previous substitution.
39 * PUBLIC: int v_again __P((SCR *, VICMD *));
41 int
42 v_again(sp, vp)
43 SCR *sp;
44 VICMD *vp;
46 EXCMD cmd;
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));
54 * v_exmode -- Q
55 * Switch the editor into EX mode.
57 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
59 int
60 v_exmode(sp, vp)
61 SCR *sp;
62 VICMD *vp;
64 GS *gp;
66 gp = sp->gp;
68 /* Try and switch screens -- the screen may not permit it. */
69 if (gp->scr_screen(sp, SC_EX)) {
70 msgq(sp, M_ERR,
71 "207|The Q command requires the ex terminal interface");
72 return (1);
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);
83 F_SET(sp, SC_EX);
85 /* Move out of the vi screen. */
86 (void)ex_puts(sp, "\n");
88 return (0);
92 * v_join -- [count]J
93 * Join lines together.
95 * PUBLIC: int v_join __P((SCR *, VICMD *));
97 int
98 v_join(sp, vp)
99 SCR *sp;
100 VICMD *vp;
102 EXCMD cmd;
103 int lno;
106 * YASC.
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
112 * worked just fine.
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
124 * Shift lines left.
126 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
129 v_shiftl(sp, vp)
130 SCR *sp;
131 VICMD *vp;
133 EXCMD cmd;
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
142 * Shift lines right.
144 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
147 v_shiftr(sp, vp)
148 SCR *sp;
149 VICMD *vp;
151 EXCMD cmd;
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));
159 * v_suspend -- ^Z
160 * Suspend vi.
162 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
165 v_suspend(sp, vp)
166 SCR *sp;
167 VICMD *vp;
169 EXCMD cmd;
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));
177 * v_switch -- ^^
178 * Switch to the previous file.
180 * PUBLIC: int v_switch __P((SCR *, VICMD *));
183 v_switch(sp, vp)
184 SCR *sp;
185 VICMD *vp;
187 EXCMD cmd;
188 char *name;
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");
196 return (1);
199 /* If autowrite is set, write out the file. */
200 if (file_m1(sp, 0, FS_ALL))
201 return (1);
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));
209 * v_tagpush -- ^[
210 * Do a tag search on the cursor keyword.
212 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
215 v_tagpush(sp, vp)
216 SCR *sp;
217 VICMD *vp;
219 EXCMD cmd;
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));
227 * v_tagpop -- ^T
228 * Pop the tags stack.
230 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
233 v_tagpop(sp, vp)
234 SCR *sp;
235 VICMD *vp;
237 EXCMD cmd;
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 *));
250 v_filter(sp, vp)
251 SCR *sp;
252 VICMD *vp;
254 EXCMD cmd;
255 TEXT *tp;
258 * !!!
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.
264 * !!!
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.
272 * !!!
273 * Historical vi ran the last bang command if N or n was used as the
274 * search motion.
276 if (F_ISSET(vp, VC_ISDOT) ||
277 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
278 ex_cinit(sp,
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))
283 return (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. */
290 if (v_tcmd(sp, vp,
291 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
292 return (1);
295 * Check to see if the user changed their mind.
297 * !!!
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;
305 return (0);
308 /* Home the cursor. */
309 vs_home(sp);
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))
315 return (1);
316 cmd.argc = EXP(sp)->argsoff; /* XXX */
317 cmd.argv = EXP(sp)->args; /* XXX */
318 return (v_exec_ex(sp, vp, &cmd));
322 * v_exec_ex --
323 * Execute an ex command.
325 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
328 v_exec_ex(sp, vp, exp)
329 SCR *sp;
330 VICMD *vp;
331 EXCMD *exp;
333 int rval;
335 rval = exp->cmd->fn(sp, exp);
336 return (v_ex_done(sp, vp) || rval);
340 * v_ex -- :
341 * Execute a colon command line.
343 * PUBLIC: int v_ex __P((SCR *, VICMD *));
346 v_ex(sp, vp)
347 SCR *sp;
348 VICMD *vp;
350 WIN *wp;
351 TEXT *tp;
352 int do_cedit, do_resolution, ifcontinue;
354 wp = sp->wp;
357 * !!!
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
363 * mode.
365 for (do_cedit = do_resolution = 0;;) {
367 * !!!
368 * There may already be an ex command waiting to run. If
369 * so, we continue with it.
371 if (!EXCMD_RUNNING(wp)) {
372 /* Get a command. */
373 if (v_tcmd(sp, vp, ':',
374 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
375 return (1);
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))
385 return (1);
386 do_cedit = 1;
387 break;
390 /* If the user didn't enter anything, return. */
391 if (tp->term == TERM_BS)
392 break;
394 /* Log the command. */
395 if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
396 return (1);
398 /* Push a command on the command stack. */
399 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
400 return (1);
403 /* Home the cursor. */
404 vs_home(sp);
407 * !!!
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))
421 (void)putchar('\n');
423 /* Call the ex parser. */
424 (void)ex_cmd(sp);
426 /* Flush ex messages. */
427 (void)ex_fflush(sp);
429 /* Resolve any messages. */
430 if (vs_ex_resolve(sp, &ifcontinue))
431 return (1);
434 * Continue or return. If continuing, make sure that we
435 * eventually do resolution.
437 if (!ifcontinue)
438 break;
439 do_resolution = 1;
441 /* If we're continuing, it's a new command. */
442 ++sp->ccnt;
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
448 * that.
450 if (do_resolution) {
451 F_SET(sp, SC_EX_WAIT_NO);
452 if (vs_ex_resolve(sp, &ifcontinue))
453 return (1);
456 /* Cleanup from the ex command. */
457 if (v_ex_done(sp, vp))
458 return (1);
460 /* The user may want to edit their colon command history. */
461 if (do_cedit)
462 return (v_ecl(sp));
464 return (0);
468 * v_ex_done --
469 * Cleanup from an ex command.
471 static int
472 v_ex_done(sp, vp)
473 SCR *sp;
474 VICMD *vp;
476 size_t len;
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.
488 * XXX
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)) {
493 sp->lno = 1;
494 sp->cno = 0;
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);
508 return (0);
512 * v_ecl --
513 * Start an edit window on the colon command-line commands.
515 static int
516 v_ecl(sp)
517 SCR *sp;
519 GS *gp;
520 SCR *new;
522 /* Initialize the screen, if necessary. */
523 gp = sp->gp;
524 if (gp->ccl_sp == NULL && v_ecl_init(sp))
525 return (1);
527 /* Get a new screen. */
528 if (screen_init(gp, sp, &new))
529 return (1);
530 if (vs_split(sp, new, 1)) {
531 (void)screen_end(new);
532 return (1);
535 /* Attach to the screen. */
536 new->ep = gp->ccl_sp->ep;
537 ++new->ep->refcnt;
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);
544 if (new->lno == 0)
545 new->lno = 1;
547 /* Remember the originating window. */
548 sp->ccl_parent = sp;
550 /* It's a special window. */
551 F_SET(new, SC_COMEDIT);
553 /* Set up the switch. */
554 sp->nextdisp = new;
555 F_SET(sp, SC_SSWITCH);
556 return (0);
560 * v_ecl_exec --
561 * Execute a command from a colon command-line window.
563 * PUBLIC: int v_ecl_exec __P((SCR *));
566 v_ecl_exec(sp)
567 SCR *sp;
569 size_t len;
570 CHAR_T *p;
572 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
573 v_emsg(sp, NULL, VIM_EMPTY);
574 return (1);
576 if (len == 0) {
577 msgq(sp, M_BERR, "307|No ex command to execute");
578 return (1);
581 /* Push the command on the command stack. */
582 if (ex_run_str(sp, NULL, p, len, 0, 0))
583 return (1);
585 /* Set up the switch. */
586 sp->nextdisp = sp->ccl_parent;
587 F_SET(sp, SC_EXIT);
588 return (0);
592 * v_ecl_log --
593 * Log a command into the colon command-line log file.
595 static int
596 v_ecl_log(sp, tp)
597 SCR *sp;
598 TEXT *tp;
600 EXF *save_ep;
601 db_recno_t lno;
602 int rval;
604 /* Initialize the screen, if necessary. */
605 if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
606 return (1);
609 * Don't log colon command window commands into the colon command
610 * window...
612 if (sp->ep == sp->gp->ccl_sp->ep)
613 return (0);
616 * XXX
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.
621 save_ep = sp->ep;
622 sp->ep = sp->gp->ccl_sp->ep;
623 if (db_last(sp, &lno)) {
624 sp->ep = save_ep;
625 return (1);
627 rval = db_append(sp, 0, lno, tp->lb, tp->len);
628 sp->ep = save_ep;
629 return (rval);
633 * v_ecl_init --
634 * Initialize the colon command-line log file.
636 static int
637 v_ecl_init(sp)
638 SCR *sp;
640 FREF *frp;
641 GS *gp;
643 gp = sp->gp;
645 /* Get a temporary file. */
646 if ((frp = file_add(sp, NULL)) == NULL)
647 return (1);
650 * XXX
651 * Create a screen -- the file initialization code wants one.
653 if (screen_init(gp, sp, &gp->ccl_sp))
654 return (1);
655 if (file_init(gp->ccl_sp, frp, NULL, 0)) {
656 (void)screen_end(gp->ccl_sp);
657 return (1);
660 /* The underlying file isn't recoverable. */
661 F_CLR(gp->ccl_sp->ep, F_RCV_ON);
663 return (0);