fix -pthread typo
[nvi.git] / vi / v_ex.c
blobe135a85df99f7b11458fe80769280304ef2ea3ba
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.52 2001/05/11 20:09:38 skimo Exp $ (Berkeley) $Date: 2001/05/11 20:09:38 $";
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;
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));
55 * v_exmode -- Q
56 * Switch the editor into EX mode.
58 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
60 int
61 v_exmode(sp, vp)
62 SCR *sp;
63 VICMD *vp;
65 GS *gp;
67 gp = sp->gp;
69 /* Try and switch screens -- the screen may not permit it. */
70 if (gp->scr_screen(sp, SC_EX)) {
71 msgq(sp, M_ERR,
72 "207|The Q command requires the ex terminal interface");
73 return (1);
75 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
77 /* Save the current cursor position. */
78 sp->frp->lno = sp->lno;
79 sp->frp->cno = sp->cno;
80 F_SET(sp->frp, FR_CURSORSET);
82 /* Switch to ex mode. */
83 F_CLR(sp, SC_VI | SC_SCR_VI);
84 F_SET(sp, SC_EX);
86 /* Move out of the vi screen. */
87 (void)ex_puts(sp, "\n");
89 return (0);
93 * v_join -- [count]J
94 * Join lines together.
96 * PUBLIC: int v_join __P((SCR *, VICMD *));
98 int
99 v_join(sp, vp)
100 SCR *sp;
101 VICMD *vp;
103 EXCMD cmd;
104 int lno;
107 * YASC.
108 * The general rule is that '#J' joins # lines, counting the current
109 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
110 * current and next lines. This doesn't map well into the ex command
111 * (which takes two line numbers), so we handle it here. Note that
112 * we never test for EOF -- historically going past the end of file
113 * worked just fine.
115 lno = vp->m_start.lno + 1;
116 if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
117 lno = vp->m_start.lno + (vp->count - 1);
119 ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
120 return (v_exec_ex(sp, vp, &cmd));
124 * v_shiftl -- [count]<motion
125 * Shift lines left.
127 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
130 v_shiftl(sp, vp)
131 SCR *sp;
132 VICMD *vp;
134 EXCMD cmd;
135 static CHAR_T lt[] = {'<', 0};
137 ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
138 argv_exp0(sp, &cmd, lt, 2);
139 return (v_exec_ex(sp, vp, &cmd));
143 * v_shiftr -- [count]>motion
144 * Shift lines right.
146 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
149 v_shiftr(sp, vp)
150 SCR *sp;
151 VICMD *vp;
153 EXCMD cmd;
154 static CHAR_T gt[] = {'>', 0};
156 ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
157 argv_exp0(sp, &cmd, gt, 2);
158 return (v_exec_ex(sp, vp, &cmd));
162 * v_suspend -- ^Z
163 * Suspend vi.
165 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
168 v_suspend(sp, vp)
169 SCR *sp;
170 VICMD *vp;
172 EXCMD cmd;
173 static CHAR_T suspend[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
175 ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
176 argv_exp0(sp, &cmd, suspend, sizeof(suspend)/sizeof(CHAR_T));
177 return (v_exec_ex(sp, vp, &cmd));
181 * v_switch -- ^^
182 * Switch to the previous file.
184 * PUBLIC: int v_switch __P((SCR *, VICMD *));
187 v_switch(sp, vp)
188 SCR *sp;
189 VICMD *vp;
191 EXCMD cmd;
192 char *name;
193 CHAR_T *wp;
194 size_t wlen;
197 * Try the alternate file name, then the previous file
198 * name. Use the real name, not the user's current name.
200 if ((name = sp->alt_name) == NULL) {
201 msgq(sp, M_ERR, "180|No previous file to edit");
202 return (1);
205 /* If autowrite is set, write out the file. */
206 if (file_m1(sp, 0, FS_ALL))
207 return (1);
209 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
210 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
211 argv_exp0(sp, &cmd, wp, wlen);
212 return (v_exec_ex(sp, vp, &cmd));
216 * v_tagpush -- ^[
217 * Do a tag search on the cursor keyword.
219 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
222 v_tagpush(sp, vp)
223 SCR *sp;
224 VICMD *vp;
226 EXCMD cmd;
228 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
229 argv_exp0(sp, &cmd, VIP(sp)->keyw, v_strlen(VIP(sp)->keyw) + 1);
230 return (v_exec_ex(sp, vp, &cmd));
234 * v_tagpop -- ^T
235 * Pop the tags stack.
237 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
240 v_tagpop(sp, vp)
241 SCR *sp;
242 VICMD *vp;
244 EXCMD cmd;
246 ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
247 return (v_exec_ex(sp, vp, &cmd));
251 * v_filter -- [count]!motion command(s)
252 * Run range through shell commands, replacing text.
254 * PUBLIC: int v_filter __P((SCR *, VICMD *));
257 v_filter(sp, vp)
258 SCR *sp;
259 VICMD *vp;
261 EXCMD cmd;
262 TEXT *tp;
265 * !!!
266 * Historical vi permitted "!!" in an empty file, and it's handled
267 * as a special case in the ex_bang routine. Don't modify this setup
268 * without understanding that one. In particular, note that we're
269 * manipulating the ex argument structures behind ex's back.
271 * !!!
272 * Historical vi did not permit the '!' command to be associated with
273 * a non-line oriented motion command, in general, although it did
274 * with search commands. So, !f; and !w would fail, but !/;<CR>
275 * would succeed, even if they all moved to the same location in the
276 * current line. I don't see any reason to disallow '!' using any of
277 * the possible motion commands.
279 * !!!
280 * Historical vi ran the last bang command if N or n was used as the
281 * search motion.
283 if (F_ISSET(vp, VC_ISDOT) ||
284 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
285 static CHAR_T bang[] = {'!', 0};
286 ex_cinit(sp,
287 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
288 EXP(sp)->argsoff = 0; /* XXX */
290 if (argv_exp1(sp, &cmd, bang, 1, 1))
291 return (1);
292 cmd.argc = EXP(sp)->argsoff; /* XXX */
293 cmd.argv = EXP(sp)->args; /* XXX */
294 return (v_exec_ex(sp, vp, &cmd));
297 /* Get the command from the user. */
298 if (v_tcmd(sp, vp,
299 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
300 return (1);
303 * Check to see if the user changed their mind.
305 * !!!
306 * Entering <escape> on an empty line was historically an error,
307 * this implementation doesn't bother.
309 tp = sp->tiq.cqh_first;
310 if (tp->term != TERM_OK) {
311 vp->m_final.lno = sp->lno;
312 vp->m_final.cno = sp->cno;
313 return (0);
316 /* Home the cursor. */
317 vs_home(sp);
319 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
320 EXP(sp)->argsoff = 0; /* XXX */
322 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
323 return (1);
324 cmd.argc = EXP(sp)->argsoff; /* XXX */
325 cmd.argv = EXP(sp)->args; /* XXX */
326 return (v_exec_ex(sp, vp, &cmd));
330 * v_exec_ex --
331 * Execute an ex command.
333 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
336 v_exec_ex(sp, vp, exp)
337 SCR *sp;
338 VICMD *vp;
339 EXCMD *exp;
341 int rval;
343 rval = exp->cmd->fn(sp, exp);
344 return (v_ex_done(sp, vp) || rval);
348 * v_ex -- :
349 * Execute a colon command line.
351 * PUBLIC: int v_ex __P((SCR *, VICMD *));
354 v_ex(sp, vp)
355 SCR *sp;
356 VICMD *vp;
358 WIN *wp;
359 TEXT *tp;
360 int do_cedit, do_resolution, ifcontinue;
362 wp = sp->wp;
365 * !!!
366 * If we put out more than a single line of messages, or ex trashes
367 * the screen, the user may continue entering ex commands. We find
368 * this out when we do the screen/message resolution. We can't enter
369 * completely into ex mode however, because the user can elect to
370 * return into vi mode by entering any key, i.e. we have to be in raw
371 * mode.
373 for (do_cedit = do_resolution = 0;;) {
375 * !!!
376 * There may already be an ex command waiting to run. If
377 * so, we continue with it.
379 if (!EXCMD_RUNNING(wp)) {
380 /* Get a command. */
381 if (v_tcmd(sp, vp, ':',
382 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
383 return (1);
384 tp = sp->tiq.cqh_first;
387 * If the user entered a single <esc>, they want to
388 * edit their colon command history. If they already
389 * entered some text, move it into the edit history.
391 if (tp->term == TERM_CEDIT) {
392 if (tp->len > 1 && v_ecl_log(sp, tp))
393 return (1);
394 do_cedit = 1;
395 break;
398 /* If the user didn't enter anything, return. */
399 if (tp->term == TERM_BS)
400 break;
402 /* Log the command. */
403 if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
404 return (1);
406 /* Push a command on the command stack. */
407 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
408 return (1);
411 /* Home the cursor. */
412 vs_home(sp);
415 * !!!
416 * If the editor wrote the screen behind curses back, put out
417 * a <newline> so that we don't overwrite the user's command
418 * with its output or the next want-to-continue? message. This
419 * doesn't belong here, but I can't find another place to put
420 * it. See, we resolved the output from the last ex command,
421 * and the user entered another one. This is the only place
422 * where we have control before the ex command writes output.
423 * We could get control in vs_msg(), but we have no way to know
424 * if command didn't put out any output when we try and resolve
425 * this command. This fixes a bug where combinations of ex
426 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
428 if (F_ISSET(sp, SC_SCR_EXWROTE))
429 (void)putchar('\n');
431 /* Call the ex parser. */
432 (void)ex_cmd(sp);
434 /* Flush ex messages. */
435 (void)ex_fflush(sp);
437 /* Resolve any messages. */
438 if (vs_ex_resolve(sp, &ifcontinue))
439 return (1);
442 * Continue or return. If continuing, make sure that we
443 * eventually do resolution.
445 if (!ifcontinue)
446 break;
447 do_resolution = 1;
449 /* If we're continuing, it's a new command. */
450 ++sp->ccnt;
454 * If the user previously continued an ex command, we have to do
455 * resolution to clean up the screen. Don't wait, we already did
456 * that.
458 if (do_resolution) {
459 F_SET(sp, SC_EX_WAIT_NO);
460 if (vs_ex_resolve(sp, &ifcontinue))
461 return (1);
464 /* Cleanup from the ex command. */
465 if (v_ex_done(sp, vp))
466 return (1);
468 /* The user may want to edit their colon command history. */
469 if (do_cedit)
470 return (v_ecl(sp));
472 return (0);
476 * v_ex_done --
477 * Cleanup from an ex command.
479 static int
480 v_ex_done(sp, vp)
481 SCR *sp;
482 VICMD *vp;
484 size_t len;
487 * The only cursor modifications are real, however, the underlying
488 * line may have changed; don't trust anything. This code has been
489 * a remarkably fertile place for bugs. Do a reality check on a
490 * cursor value, and make sure it's okay. If necessary, change it.
491 * Ex keeps track of the line number, but it cares less about the
492 * column and it may have disappeared.
494 * Don't trust ANYTHING.
496 * XXX
497 * Ex will soon have to start handling the column correctly; see
498 * the POSIX 1003.2 standard.
500 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
501 sp->lno = 1;
502 sp->cno = 0;
503 } else if (sp->cno >= len)
504 sp->cno = len ? len - 1 : 0;
506 vp->m_final.lno = sp->lno;
507 vp->m_final.cno = sp->cno;
510 * Don't re-adjust the cursor after executing an ex command,
511 * and ex movements are permanent.
513 F_CLR(vp, VM_RCM_MASK);
514 F_SET(vp, VM_RCM_SET);
516 return (0);
520 * v_ecl --
521 * Start an edit window on the colon command-line commands.
523 static int
524 v_ecl(sp)
525 SCR *sp;
527 GS *gp;
528 SCR *new;
530 /* Initialize the screen, if necessary. */
531 gp = sp->gp;
532 if (gp->ccl_sp == NULL && v_ecl_init(sp))
533 return (1);
535 /* Get a new screen. */
536 if (screen_init(gp, sp, &new))
537 return (1);
538 if (vs_split(sp, new, 1)) {
539 (void)screen_end(new);
540 return (1);
543 /* Attach to the screen. */
544 new->ep = gp->ccl_sp->ep;
545 ++new->ep->refcnt;
547 new->frp = gp->ccl_sp->frp;
548 new->frp->flags = sp->frp->flags;
550 /* Move the cursor to the end. */
551 (void)db_last(new, &new->lno);
552 if (new->lno == 0)
553 new->lno = 1;
555 /* Remember the originating window. */
556 sp->ccl_parent = sp;
558 /* It's a special window. */
559 F_SET(new, SC_COMEDIT);
561 /* Don't encode on writing to DB. */
562 o_set(new, O_FILEENCODING, OS_STRDUP, "WCHAR_T", 0);
564 /* Set up the switch. */
565 sp->nextdisp = new;
566 F_SET(sp, SC_SSWITCH);
567 return (0);
571 * v_ecl_exec --
572 * Execute a command from a colon command-line window.
574 * PUBLIC: int v_ecl_exec __P((SCR *));
577 v_ecl_exec(sp)
578 SCR *sp;
580 size_t len;
581 CHAR_T *p;
583 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
584 v_emsg(sp, NULL, VIM_EMPTY);
585 return (1);
587 if (len == 0) {
588 msgq(sp, M_BERR, "307|No ex command to execute");
589 return (1);
592 /* Push the command on the command stack. */
593 if (ex_run_str(sp, NULL, p, len, 0, 0))
594 return (1);
596 /* Set up the switch. */
597 sp->nextdisp = sp->ccl_parent;
598 F_SET(sp, SC_EXIT);
599 return (0);
603 * v_ecl_log --
604 * Log a command into the colon command-line log file.
606 static int
607 v_ecl_log(sp, tp)
608 SCR *sp;
609 TEXT *tp;
611 EXF *save_ep;
612 char *save_enc;
613 db_recno_t lno;
614 int rval;
615 CHAR_T *p;
616 size_t len;
618 /* Initialize the screen, if necessary. */
619 if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
620 return (1);
623 * Don't log colon command window commands into the colon command
624 * window...
626 if (sp->ep == sp->gp->ccl_sp->ep)
627 return (0);
630 * XXX
631 * Swap the current EXF with the colon command file EXF. This
632 * isn't pretty, but too many routines "know" that sp->ep points
633 * to the current EXF.
635 * XXXX
636 * Temporarily change fileencoding as well.
638 save_ep = sp->ep;
639 sp->ep = sp->gp->ccl_sp->ep;
641 save_enc = O_STR(sp, O_FILEENCODING);
642 o_set(sp, O_FILEENCODING, OS_STR | OS_NOFREE, "WCHAR_T", 0);
644 if (db_last(sp, &lno)) {
645 sp->ep = save_ep;
646 return (1);
648 /* Don't log line that is identical to previous one */
649 if (lno > 0 &&
650 !db_get(sp, lno, 0, &p, &len) &&
651 len == tp->len &&
652 !MEMCMP(tp->lb, p, len))
653 rval = 0;
654 else {
655 rval = db_append(sp, 0, lno, tp->lb, tp->len);
656 /* XXXX end "transaction" on ccl */
657 log_cursor(sp);
659 sp->ep = save_ep;
661 o_set(sp, O_FILEENCODING, OS_STR | OS_NOFREE, save_enc, 0);
663 return (rval);
667 * v_ecl_init --
668 * Initialize the colon command-line log file.
670 static int
671 v_ecl_init(sp)
672 SCR *sp;
674 FREF *frp;
675 GS *gp;
677 gp = sp->gp;
679 /* Get a temporary file. */
680 if ((frp = file_add(sp, NULL)) == NULL)
681 return (1);
684 * XXX
685 * Create a screen -- the file initialization code wants one.
687 if (screen_init(gp, sp, &gp->ccl_sp))
688 return (1);
689 if (file_init(gp->ccl_sp, frp, NULL, 0)) {
690 (void)screen_end(gp->ccl_sp);
691 return (1);
694 /* The underlying file isn't recoverable. */
695 F_CLR(gp->ccl_sp->ep, F_RCV_ON);
697 return (0);