optionally use bundled db
[nvi.git] / vi / v_ex.c
blob0043d1ceef917c14c9e5a309d5a2956552c2b219
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.60 2003/07/19 21:04:00 skimo Exp $ (Berkeley) $Date: 2003/07/19 21:04:00 $";
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(SCR *sp, VICMD *vp)
44 EXCMD cmd;
45 static CHAR_T nul[] = { 0 };
47 ex_cinit(sp, &cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1);
48 argv_exp0(sp, &cmd, nul, 1);
49 return (v_exec_ex(sp, vp, &cmd));
53 * v_exmode -- Q
54 * Switch the editor into EX mode.
56 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
58 int
59 v_exmode(SCR *sp, VICMD *vp)
61 GS *gp;
63 gp = sp->gp;
65 /* Try and switch screens -- the screen may not permit it. */
66 if (gp->scr_screen(sp, SC_EX)) {
67 msgq(sp, M_ERR,
68 "207|The Q command requires the ex terminal interface");
69 return (1);
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);
80 F_SET(sp, SC_EX);
82 /* Move out of the vi screen. */
83 (void)ex_puts(sp, "\n");
85 return (0);
89 * v_join -- [count]J
90 * Join lines together.
92 * PUBLIC: int v_join __P((SCR *, VICMD *));
94 int
95 v_join(SCR *sp, VICMD *vp)
97 EXCMD cmd;
98 int lno;
101 * YASC.
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
107 * worked just fine.
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
119 * Shift lines left.
121 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
124 v_shiftl(SCR *sp, VICMD *vp)
126 EXCMD cmd;
127 static CHAR_T lt[] = {'<', 0};
129 ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
130 argv_exp0(sp, &cmd, lt, 2);
131 return (v_exec_ex(sp, vp, &cmd));
135 * v_shiftr -- [count]>motion
136 * Shift lines right.
138 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
141 v_shiftr(SCR *sp, VICMD *vp)
143 EXCMD cmd;
144 static CHAR_T gt[] = {'>', 0};
146 ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
147 argv_exp0(sp, &cmd, gt, 2);
148 return (v_exec_ex(sp, vp, &cmd));
152 * v_suspend -- ^Z
153 * Suspend vi.
155 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
158 v_suspend(SCR *sp, VICMD *vp)
160 EXCMD cmd;
161 static CHAR_T suspend[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
163 ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
164 argv_exp0(sp, &cmd, suspend, sizeof(suspend)/sizeof(CHAR_T));
165 return (v_exec_ex(sp, vp, &cmd));
169 * v_switch -- ^^
170 * Switch to the previous file.
172 * PUBLIC: int v_switch __P((SCR *, VICMD *));
175 v_switch(SCR *sp, VICMD *vp)
177 EXCMD cmd;
178 char *name;
179 CHAR_T *wp;
180 size_t wlen;
183 * Try the alternate file name, then the previous file
184 * name. Use the real name, not the user's current name.
186 if ((name = sp->alt_name) == NULL) {
187 msgq(sp, M_ERR, "180|No previous file to edit");
188 return (1);
191 /* If autowrite is set, write out the file. */
192 if (file_m1(sp, 0, FS_ALL))
193 return (1);
195 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
196 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
197 argv_exp0(sp, &cmd, wp, wlen);
198 return (v_exec_ex(sp, vp, &cmd));
202 * v_tagpush -- ^[
203 * Do a tag search on the cursor keyword.
205 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
208 v_tagpush(SCR *sp, VICMD *vp)
210 EXCMD cmd;
212 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
213 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1);
214 return (v_exec_ex(sp, vp, &cmd));
218 * v_tagpop -- ^T
219 * Pop the tags stack.
221 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
224 v_tagpop(SCR *sp, VICMD *vp)
226 EXCMD cmd;
228 ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
229 return (v_exec_ex(sp, vp, &cmd));
233 * v_filter -- [count]!motion command(s)
234 * Run range through shell commands, replacing text.
236 * PUBLIC: int v_filter __P((SCR *, VICMD *));
239 v_filter(SCR *sp, VICMD *vp)
241 EXCMD cmd;
242 TEXT *tp;
245 * !!!
246 * Historical vi permitted "!!" in an empty file, and it's handled
247 * as a special case in the ex_bang routine. Don't modify this setup
248 * without understanding that one. In particular, note that we're
249 * manipulating the ex argument structures behind ex's back.
251 * !!!
252 * Historical vi did not permit the '!' command to be associated with
253 * a non-line oriented motion command, in general, although it did
254 * with search commands. So, !f; and !w would fail, but !/;<CR>
255 * would succeed, even if they all moved to the same location in the
256 * current line. I don't see any reason to disallow '!' using any of
257 * the possible motion commands.
259 * !!!
260 * Historical vi ran the last bang command if N or n was used as the
261 * search motion.
263 if (F_ISSET(vp, VC_ISDOT) ||
264 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
265 static CHAR_T bang[] = {'!', 0};
266 ex_cinit(sp,
267 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
268 EXP(sp)->argsoff = 0; /* XXX */
270 if (argv_exp1(sp, &cmd, bang, 1, 1))
271 return (1);
272 cmd.argc = EXP(sp)->argsoff; /* XXX */
273 cmd.argv = EXP(sp)->args; /* XXX */
274 return (v_exec_ex(sp, vp, &cmd));
277 /* Get the command from the user. */
278 if (v_tcmd(sp, vp,
279 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
280 return (1);
283 * Check to see if the user changed their mind.
285 * !!!
286 * Entering <escape> on an empty line was historically an error,
287 * this implementation doesn't bother.
289 tp = sp->tiq.cqh_first;
290 if (tp->term != TERM_OK) {
291 vp->m_final.lno = sp->lno;
292 vp->m_final.cno = sp->cno;
293 return (0);
296 /* Home the cursor. */
297 vs_home(sp);
299 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
300 EXP(sp)->argsoff = 0; /* XXX */
302 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
303 return (1);
304 cmd.argc = EXP(sp)->argsoff; /* XXX */
305 cmd.argv = EXP(sp)->args; /* XXX */
306 return (v_exec_ex(sp, vp, &cmd));
310 * v_exec_ex --
311 * Execute an ex command.
313 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
316 v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
318 int rval;
320 rval = exp->cmd->fn(sp, exp);
321 return (v_ex_done(sp, vp) || rval);
325 * v_ex -- :
326 * Execute a colon command line.
328 * PUBLIC: int v_ex __P((SCR *, VICMD *));
331 v_ex(SCR *sp, VICMD *vp)
333 WIN *wp;
334 TEXT *tp;
335 int do_cedit, do_resolution, ifcontinue;
337 wp = sp->wp;
340 * !!!
341 * If we put out more than a single line of messages, or ex trashes
342 * the screen, the user may continue entering ex commands. We find
343 * this out when we do the screen/message resolution. We can't enter
344 * completely into ex mode however, because the user can elect to
345 * return into vi mode by entering any key, i.e. we have to be in raw
346 * mode.
348 for (do_cedit = do_resolution = 0;;) {
350 * !!!
351 * There may already be an ex command waiting to run. If
352 * so, we continue with it.
354 if (!EXCMD_RUNNING(wp)) {
355 /* Get a command. */
356 if (v_tcmd(sp, vp, ':',
357 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
358 return (1);
359 tp = sp->tiq.cqh_first;
362 * If the user entered a single <esc>, they want to
363 * edit their colon command history. If they already
364 * entered some text, move it into the edit history.
366 if (tp->term == TERM_CEDIT) {
367 if (tp->len > 1 && v_ecl_log(sp, tp))
368 return (1);
369 do_cedit = 1;
370 break;
373 /* If the user didn't enter anything, return. */
374 if (tp->term == TERM_BS)
375 break;
377 /* Log the command. */
378 if (O_STR(sp, O_CEDIT) != NULL)
379 (void)v_ecl_log(sp, tp);
381 /* Push a command on the command stack. */
382 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
383 return (1);
386 /* Home the cursor. */
387 vs_home(sp);
390 * !!!
391 * If the editor wrote the screen behind curses back, put out
392 * a <newline> so that we don't overwrite the user's command
393 * with its output or the next want-to-continue? message. This
394 * doesn't belong here, but I can't find another place to put
395 * it. See, we resolved the output from the last ex command,
396 * and the user entered another one. This is the only place
397 * where we have control before the ex command writes output.
398 * We could get control in vs_msg(), but we have no way to know
399 * if command didn't put out any output when we try and resolve
400 * this command. This fixes a bug where combinations of ex
401 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
403 if (F_ISSET(sp, SC_SCR_EXWROTE))
404 (void)putchar('\n');
406 /* Call the ex parser. */
407 (void)ex_cmd(sp);
409 /* Flush ex messages. */
410 (void)ex_fflush(sp);
412 /* Resolve any messages. */
413 if (vs_ex_resolve(sp, &ifcontinue))
414 return (1);
417 * Continue or return. If continuing, make sure that we
418 * eventually do resolution.
420 if (!ifcontinue)
421 break;
422 do_resolution = 1;
424 /* If we're continuing, it's a new command. */
425 ++sp->ccnt;
429 * If the user previously continued an ex command, we have to do
430 * resolution to clean up the screen. Don't wait, we already did
431 * that.
433 if (do_resolution) {
434 F_SET(sp, SC_EX_WAIT_NO);
435 if (vs_ex_resolve(sp, &ifcontinue))
436 return (1);
439 /* Cleanup from the ex command. */
440 if (v_ex_done(sp, vp))
441 return (1);
443 /* The user may want to edit their colon command history. */
444 if (do_cedit)
445 return (v_ecl(sp));
447 return (0);
451 * v_ex_done --
452 * Cleanup from an ex command.
454 static int
455 v_ex_done(SCR *sp, VICMD *vp)
457 size_t len;
460 * The only cursor modifications are real, however, the underlying
461 * line may have changed; don't trust anything. This code has been
462 * a remarkably fertile place for bugs. Do a reality check on a
463 * cursor value, and make sure it's okay. If necessary, change it.
464 * Ex keeps track of the line number, but it cares less about the
465 * column and it may have disappeared.
467 * Don't trust ANYTHING.
469 * XXX
470 * Ex will soon have to start handling the column correctly; see
471 * the POSIX 1003.2 standard.
473 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
474 sp->lno = 1;
475 sp->cno = 0;
476 } else if (sp->cno >= len)
477 sp->cno = len ? len - 1 : 0;
479 vp->m_final.lno = sp->lno;
480 vp->m_final.cno = sp->cno;
483 * Don't re-adjust the cursor after executing an ex command,
484 * and ex movements are permanent.
486 F_CLR(vp, VM_RCM_MASK);
487 F_SET(vp, VM_RCM_SET);
489 return (0);
493 * v_ecl --
494 * Start an edit window on the colon command-line commands.
496 static int
497 v_ecl(SCR *sp)
499 GS *gp;
500 WIN *wp;
501 SCR *new;
503 /* Initialize the screen, if necessary. */
504 gp = sp->gp;
505 wp = sp->wp;
506 if (wp->ccl_sp == NULL && v_ecl_init(sp))
507 return (1);
509 /* Get a new screen. */
510 if (screen_init(gp, sp, &new))
511 return (1);
512 if (vs_split(sp, new, 1)) {
513 (void)screen_end(new);
514 return (1);
517 /* Attach to the screen. */
518 new->ep = wp->ccl_sp->ep;
519 ++new->ep->refcnt;
520 CIRCLEQ_INSERT_HEAD(&new->ep->scrq, new, eq);
522 new->frp = wp->ccl_sp->frp;
523 new->frp->flags = sp->frp->flags;
525 /* Move the cursor to the end. */
526 (void)db_last(new, &new->lno);
527 if (new->lno == 0)
528 new->lno = 1;
530 /* Remember the originating window. */
531 sp->ccl_parent = sp;
533 /* It's a special window. */
534 F_SET(new, SC_COMEDIT);
536 /* Don't encode on writing to DB. */
537 o_set(new, O_FILEENCODING, OS_STRDUP, "WCHAR_T", 0);
539 /* Set up the switch. */
540 sp->nextdisp = new;
541 F_SET(sp, SC_SSWITCH);
542 return (0);
546 * v_ecl_exec --
547 * Execute a command from a colon command-line window.
549 * PUBLIC: int v_ecl_exec __P((SCR *));
552 v_ecl_exec(SCR *sp)
554 size_t len;
555 CHAR_T *p;
557 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
558 v_emsg(sp, NULL, VIM_EMPTY);
559 return (1);
561 if (len == 0) {
562 msgq(sp, M_BERR, "307|No ex command to execute");
563 return (1);
566 /* Push the command on the command stack. */
567 if (ex_run_str(sp, NULL, p, len, 0, 0))
568 return (1);
570 /* Set up the switch. */
571 sp->nextdisp = sp->ccl_parent;
572 F_SET(sp, SC_EXIT);
573 return (0);
577 * v_ecl_log --
578 * Log a command into the colon command-line log file.
580 static int
581 v_ecl_log(SCR *sp, TEXT *tp)
583 db_recno_t lno;
584 int rval;
585 CHAR_T *p;
586 size_t len;
587 SCR *ccl_sp;
589 /* Initialize the screen, if necessary. */
590 if (sp->wp->ccl_sp == NULL && v_ecl_init(sp))
591 return (1);
593 ccl_sp = sp->wp->ccl_sp;
596 * Don't log colon command window commands into the colon command
597 * window...
599 if (sp->ep == ccl_sp->ep)
600 return (0);
602 if (db_last(ccl_sp, &lno)) {
603 return (1);
605 /* Don't log line that is identical to previous one */
606 if (lno > 0 &&
607 !db_get(ccl_sp, lno, 0, &p, &len) &&
608 len == tp->len &&
609 !MEMCMP(tp->lb, p, len))
610 rval = 0;
611 else {
612 rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len);
613 /* XXXX end "transaction" on ccl */
614 /* Is this still necessary now that we no longer hijack sp ? */
615 log_cursor(ccl_sp);
618 return (rval);
622 * v_ecl_init --
623 * Initialize the colon command-line log file.
625 static int
626 v_ecl_init(SCR *sp)
628 FREF *frp;
629 GS *gp;
630 WIN *wp;
632 gp = sp->gp;
633 wp = sp->wp;
635 /* Get a temporary file. */
636 if ((frp = file_add(sp, NULL)) == NULL)
637 return (1);
640 * XXX
641 * Create a screen -- the file initialization code wants one.
643 if (screen_init(gp, sp, &wp->ccl_sp))
644 return (1);
645 conv_enc(wp->ccl_sp, O_FILEENCODING, "WCHAR_T");
646 if (file_init(wp->ccl_sp, frp, NULL, 0)) {
647 (void)screen_end(wp->ccl_sp);
648 wp->ccl_sp = 0;
649 return (1);
652 /* The underlying file isn't recoverable. */
653 F_CLR(wp->ccl_sp->ep, F_RCV_ON);
655 return (0);