tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / vi / v_ex.c
blob02b19429eaf7011f1ecce226fd5fe6640c5242ee
1 /* $NetBSD: v_ex.c,v 1.5 2013/12/01 02:34:54 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "../common/common.h"
29 #include "vi.h"
31 static int v_ecl __P((SCR *));
32 static int v_ecl_init __P((SCR *));
33 static int v_ecl_log __P((SCR *, TEXT *));
34 static int v_ex_done __P((SCR *, VICMD *));
37 * v_again -- &
38 * Repeat the previous substitution.
40 * PUBLIC: int v_again __P((SCR *, VICMD *));
42 int
43 v_again(SCR *sp, VICMD *vp)
45 EXCMD cmd;
46 static CHAR_T nul[] = { 0 };
48 ex_cinit(sp, &cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1);
49 argv_exp0(sp, &cmd, nul, 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(SCR *sp, VICMD *vp)
62 GS *gp;
64 gp = sp->gp;
66 /* Try and switch screens -- the screen may not permit it. */
67 if (gp->scr_screen(sp, SC_EX)) {
68 msgq(sp, M_ERR,
69 "207|The Q command requires the ex terminal interface");
70 return (1);
72 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
74 /* Save the current cursor position. */
75 sp->frp->lno = sp->lno;
76 sp->frp->cno = sp->cno;
77 F_SET(sp->frp, FR_CURSORSET);
79 /* Switch to ex mode. */
80 F_CLR(sp, SC_VI | SC_SCR_VI);
81 F_SET(sp, SC_EX);
83 /* Move out of the vi screen. */
84 (void)ex_puts(sp, "\n");
86 return (0);
90 * v_join -- [count]J
91 * Join lines together.
93 * PUBLIC: int v_join __P((SCR *, VICMD *));
95 int
96 v_join(SCR *sp, VICMD *vp)
98 EXCMD cmd;
99 int lno;
102 * YASC.
103 * The general rule is that '#J' joins # lines, counting the current
104 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
105 * current and next lines. This doesn't map well into the ex command
106 * (which takes two line numbers), so we handle it here. Note that
107 * we never test for EOF -- historically going past the end of file
108 * worked just fine.
110 lno = vp->m_start.lno + 1;
111 if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
112 lno = vp->m_start.lno + (vp->count - 1);
114 ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
115 return (v_exec_ex(sp, vp, &cmd));
119 * v_shiftl -- [count]<motion
120 * Shift lines left.
122 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
125 v_shiftl(SCR *sp, VICMD *vp)
127 EXCMD cmd;
128 static CHAR_T lt[] = {'<', 0};
130 ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
131 argv_exp0(sp, &cmd, lt, 2);
132 return (v_exec_ex(sp, vp, &cmd));
136 * v_shiftr -- [count]>motion
137 * Shift lines right.
139 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
142 v_shiftr(SCR *sp, VICMD *vp)
144 EXCMD cmd;
145 static CHAR_T gt[] = {'>', 0};
147 ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
148 argv_exp0(sp, &cmd, gt, 2);
149 return (v_exec_ex(sp, vp, &cmd));
153 * v_suspend -- ^Z
154 * Suspend vi.
156 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
159 v_suspend(SCR *sp, VICMD *vp)
161 EXCMD cmd;
162 static CHAR_T suspend[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
164 ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
165 argv_exp0(sp, &cmd, suspend, sizeof(suspend)/sizeof(CHAR_T));
166 return (v_exec_ex(sp, vp, &cmd));
170 * v_switch -- ^^
171 * Switch to the previous file.
173 * PUBLIC: int v_switch __P((SCR *, VICMD *));
176 v_switch(SCR *sp, VICMD *vp)
178 EXCMD cmd;
179 char *name;
180 const CHAR_T *wp;
181 size_t wlen;
184 * Try the alternate file name, then the previous file
185 * name. Use the real name, not the user's current name.
187 if ((name = sp->alt_name) == NULL) {
188 msgq(sp, M_ERR, "180|No previous file to edit");
189 return (1);
192 /* If autowrite is set, write out the file. */
193 if (file_m1(sp, 0, FS_ALL))
194 return (1);
196 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
197 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
198 argv_exp0(sp, &cmd, wp, wlen);
199 return (v_exec_ex(sp, vp, &cmd));
203 * v_tagpush -- ^[
204 * Do a tag search on the cursor keyword.
206 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
209 v_tagpush(SCR *sp, VICMD *vp)
211 EXCMD cmd;
213 #ifdef GTAGS
214 if (O_ISSET(sp, O_GTAGSMODE) && vp->m_start.cno == 0)
215 ex_cinit(sp, &cmd, C_RTAG, 0, OOBLNO, 0, 0);
216 else
217 #endif
218 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
219 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1);
220 return (v_exec_ex(sp, vp, &cmd));
224 * v_tagpop -- ^T
225 * Pop the tags stack.
227 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
230 v_tagpop(SCR *sp, VICMD *vp)
232 EXCMD cmd;
234 ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
235 return (v_exec_ex(sp, vp, &cmd));
239 * v_filter -- [count]!motion command(s)
240 * Run range through shell commands, replacing text.
242 * PUBLIC: int v_filter __P((SCR *, VICMD *));
245 v_filter(SCR *sp, VICMD *vp)
247 EXCMD cmd;
248 TEXT *tp;
251 * !!!
252 * Historical vi permitted "!!" in an empty file, and it's handled
253 * as a special case in the ex_bang routine. Don't modify this setup
254 * without understanding that one. In particular, note that we're
255 * manipulating the ex argument structures behind ex's back.
257 * !!!
258 * Historical vi did not permit the '!' command to be associated with
259 * a non-line oriented motion command, in general, although it did
260 * with search commands. So, !f; and !w would fail, but !/;<CR>
261 * would succeed, even if they all moved to the same location in the
262 * current line. I don't see any reason to disallow '!' using any of
263 * the possible motion commands.
265 * !!!
266 * Historical vi ran the last bang command if N or n was used as the
267 * search motion.
269 if (F_ISSET(vp, VC_ISDOT) ||
270 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
271 static CHAR_T bang[] = {'!', 0};
272 ex_cinit(sp,
273 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
274 EXP(sp)->argsoff = 0; /* XXX */
276 if (argv_exp1(sp, &cmd, bang, 1, 1))
277 return (1);
278 cmd.argc = EXP(sp)->argsoff; /* XXX */
279 cmd.argv = EXP(sp)->args; /* XXX */
280 return (v_exec_ex(sp, vp, &cmd));
283 /* Get the command from the user. */
284 if (v_tcmd(sp, vp,
285 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
286 return (1);
289 * Check to see if the user changed their mind.
291 * !!!
292 * Entering <escape> on an empty line was historically an error,
293 * this implementation doesn't bother.
295 tp = TAILQ_FIRST(&sp->tiq);
296 if (tp->term != TERM_OK) {
297 vp->m_final.lno = sp->lno;
298 vp->m_final.cno = sp->cno;
299 return (0);
302 /* Home the cursor. */
303 vs_home(sp);
305 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
306 EXP(sp)->argsoff = 0; /* XXX */
308 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
309 return (1);
310 cmd.argc = EXP(sp)->argsoff; /* XXX */
311 cmd.argv = EXP(sp)->args; /* XXX */
312 return (v_exec_ex(sp, vp, &cmd));
316 * v_exec_ex --
317 * Execute an ex command.
319 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
322 v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
324 int rval;
326 rval = exp->cmd->fn(sp, exp);
327 return (v_ex_done(sp, vp) || rval);
331 * v_ex -- :
332 * Execute a colon command line.
334 * PUBLIC: int v_ex __P((SCR *, VICMD *));
337 v_ex(SCR *sp, VICMD *vp)
339 WIN *wp;
340 TEXT *tp;
341 int do_cedit, do_resolution, ifcontinue;
343 wp = sp->wp;
346 * !!!
347 * If we put out more than a single line of messages, or ex trashes
348 * the screen, the user may continue entering ex commands. We find
349 * this out when we do the screen/message resolution. We can't enter
350 * completely into ex mode however, because the user can elect to
351 * return into vi mode by entering any key, i.e. we have to be in raw
352 * mode.
354 for (do_cedit = do_resolution = 0;;) {
356 * !!!
357 * There may already be an ex command waiting to run. If
358 * so, we continue with it.
360 if (!EXCMD_RUNNING(wp)) {
361 /* Get a command. */
362 if (v_tcmd(sp, vp, ':',
363 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
364 return (1);
365 tp = TAILQ_FIRST(&sp->tiq);
368 * If the user entered a single <esc>, they want to
369 * edit their colon command history. If they already
370 * entered some text, move it into the edit history.
372 if (tp->term == TERM_CEDIT) {
373 if (tp->len > 1 && v_ecl_log(sp, tp))
374 return (1);
375 do_cedit = 1;
376 break;
379 /* If the user didn't enter anything, return. */
380 if (tp->term == TERM_BS)
381 break;
383 /* Log the command. */
384 if (O_STR(sp, O_CEDIT) != NULL)
385 (void)v_ecl_log(sp, tp);
387 /* Push a command on the command stack. */
388 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
389 return (1);
392 /* Home the cursor. */
393 vs_home(sp);
396 * !!!
397 * If the editor wrote the screen behind curses back, put out
398 * a <newline> so that we don't overwrite the user's command
399 * with its output or the next want-to-continue? message. This
400 * doesn't belong here, but I can't find another place to put
401 * it. See, we resolved the output from the last ex command,
402 * and the user entered another one. This is the only place
403 * where we have control before the ex command writes output.
404 * We could get control in vs_msg(), but we have no way to know
405 * if command didn't put out any output when we try and resolve
406 * this command. This fixes a bug where combinations of ex
407 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
409 if (F_ISSET(sp, SC_SCR_EXWROTE))
410 (void)putchar('\n');
412 /* Call the ex parser. */
413 (void)ex_cmd(sp);
415 /* Flush ex messages. */
416 (void)ex_fflush(sp);
418 /* Resolve any messages. */
419 if (vs_ex_resolve(sp, &ifcontinue))
420 return (1);
423 * Continue or return. If continuing, make sure that we
424 * eventually do resolution.
426 if (!ifcontinue)
427 break;
428 do_resolution = 1;
430 /* If we're continuing, it's a new command. */
431 ++sp->ccnt;
435 * If the user previously continued an ex command, we have to do
436 * resolution to clean up the screen. Don't wait, we already did
437 * that.
439 if (do_resolution) {
440 F_SET(sp, SC_EX_WAIT_NO);
441 if (vs_ex_resolve(sp, &ifcontinue))
442 return (1);
445 /* Cleanup from the ex command. */
446 if (v_ex_done(sp, vp))
447 return (1);
449 /* The user may want to edit their colon command history. */
450 if (do_cedit)
451 return (v_ecl(sp));
453 return (0);
457 * v_ex_done --
458 * Cleanup from an ex command.
460 static int
461 v_ex_done(SCR *sp, VICMD *vp)
463 size_t len;
466 * The only cursor modifications are real, however, the underlying
467 * line may have changed; don't trust anything. This code has been
468 * a remarkably fertile place for bugs. Do a reality check on a
469 * cursor value, and make sure it's okay. If necessary, change it.
470 * Ex keeps track of the line number, but it cares less about the
471 * column and it may have disappeared.
473 * Don't trust ANYTHING.
475 * XXX
476 * Ex will soon have to start handling the column correctly; see
477 * the POSIX 1003.2 standard.
479 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
480 sp->lno = 1;
481 sp->cno = 0;
482 } else if (sp->cno >= len)
483 sp->cno = len ? len - 1 : 0;
485 vp->m_final.lno = sp->lno;
486 vp->m_final.cno = sp->cno;
489 * Don't re-adjust the cursor after executing an ex command,
490 * and ex movements are permanent.
492 F_CLR(vp, VM_RCM_MASK);
493 F_SET(vp, VM_RCM_SET);
495 return (0);
499 * v_ecl --
500 * Start an edit window on the colon command-line commands.
502 static int
503 v_ecl(SCR *sp)
505 GS *gp;
506 WIN *wp;
507 SCR *new;
509 /* Initialize the screen, if necessary. */
510 gp = sp->gp;
511 wp = sp->wp;
512 if (wp->ccl_sp == NULL && v_ecl_init(sp))
513 return (1);
515 /* Get a new screen. */
516 if (screen_init(gp, sp, &new))
517 return (1);
518 if (vs_split(sp, new, 1)) {
519 (void)screen_end(new);
520 return (1);
523 /* Attach to the screen. */
524 new->ep = wp->ccl_sp->ep;
525 ++new->ep->refcnt;
526 TAILQ_INSERT_HEAD(&new->ep->scrq, new, eq);
528 new->frp = wp->ccl_sp->frp;
529 new->frp->flags = sp->frp->flags;
530 new->conv = wp->ccl_sp->conv;
532 /* Move the cursor to the end. */
533 (void)db_last(new, &new->lno);
534 if (new->lno == 0)
535 new->lno = 1;
537 /* Remember the originating window. */
538 sp->ccl_parent = sp;
540 /* It's a special window. */
541 F_SET(new, SC_COMEDIT);
543 /* Don't encode on writing to DB. */
544 o_set(new, O_FILEENCODING, OS_STRDUP, "WCHAR_T", 0);
546 /* Set up the switch. */
547 sp->nextdisp = new;
548 F_SET(sp, SC_SSWITCH);
549 return (0);
553 * v_ecl_exec --
554 * Execute a command from a colon command-line window.
556 * PUBLIC: int v_ecl_exec __P((SCR *));
559 v_ecl_exec(SCR *sp)
561 size_t len;
562 CHAR_T *p;
564 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
565 v_emsg(sp, NULL, VIM_EMPTY);
566 return (1);
568 if (len == 0) {
569 msgq(sp, M_BERR, "307|No ex command to execute");
570 return (1);
573 /* Push the command on the command stack. */
574 if (ex_run_str(sp, NULL, p, len, 0, 0))
575 return (1);
577 /* Set up the switch. */
578 sp->nextdisp = sp->ccl_parent;
579 F_SET(sp, SC_EXIT);
580 return (0);
584 * v_ecl_log --
585 * Log a command into the colon command-line log file.
587 static int
588 v_ecl_log(SCR *sp, TEXT *tp)
590 db_recno_t lno;
591 int rval;
592 CHAR_T *p;
593 size_t len;
594 SCR *ccl_sp;
596 /* Initialize the screen, if necessary. */
597 if (sp->wp->ccl_sp == NULL && v_ecl_init(sp))
598 return (1);
600 ccl_sp = sp->wp->ccl_sp;
603 * Don't log colon command window commands into the colon command
604 * window...
606 if (sp->ep == ccl_sp->ep)
607 return (0);
609 if (db_last(ccl_sp, &lno)) {
610 return (1);
612 /* Don't log line that is identical to previous one */
613 if (lno > 0 &&
614 !db_get(ccl_sp, lno, 0, &p, &len) &&
615 len == tp->len &&
616 !MEMCMP(tp->lb, p, len))
617 rval = 0;
618 else {
619 rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len);
620 /* XXXX end "transaction" on ccl */
621 /* Is this still necessary now that we no longer hijack sp ? */
622 log_cursor(ccl_sp);
625 return (rval);
629 * v_ecl_init --
630 * Initialize the colon command-line log file.
632 static int
633 v_ecl_init(SCR *sp)
635 FREF *frp;
636 GS *gp;
637 WIN *wp;
639 gp = sp->gp;
640 wp = sp->wp;
642 /* Get a temporary file. */
643 if ((frp = file_add(sp, NULL)) == NULL)
644 return (1);
647 * XXX
648 * Create a screen -- the file initialization code wants one.
650 if (screen_init(gp, sp, &wp->ccl_sp))
651 return (1);
652 conv_enc(wp->ccl_sp, O_FILEENCODING, "WCHAR_T");
653 if (file_init(wp->ccl_sp, frp, NULL, 0)) {
654 (void)screen_end(wp->ccl_sp);
655 wp->ccl_sp = 0;
656 return (1);
659 /* The underlying file isn't recoverable. */
660 F_CLR(wp->ccl_sp->ep, F_RCV_ON);
662 return (0);