Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / v_ex.c
bloba9bef7298d22865020742c41dffb8c9e2900d2ea
1 /* $NetBSD: v_ex.c,v 1.3 2008/12/05 22:51:43 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "vi.h"
32 static int v_ecl __P((SCR *));
33 static int v_ecl_init __P((SCR *));
34 static int v_ecl_log __P((SCR *, TEXT *));
35 static int v_ex_done __P((SCR *, VICMD *));
38 * v_again -- &
39 * Repeat the previous substitution.
41 * PUBLIC: int v_again __P((SCR *, VICMD *));
43 int
44 v_again(SCR *sp, 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(SCR *sp, VICMD *vp)
63 GS *gp;
65 gp = sp->gp;
67 /* Try and switch screens -- the screen may not permit it. */
68 if (gp->scr_screen(sp, SC_EX)) {
69 msgq(sp, M_ERR,
70 "207|The Q command requires the ex terminal interface");
71 return (1);
73 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
75 /* Save the current cursor position. */
76 sp->frp->lno = sp->lno;
77 sp->frp->cno = sp->cno;
78 F_SET(sp->frp, FR_CURSORSET);
80 /* Switch to ex mode. */
81 F_CLR(sp, SC_VI | SC_SCR_VI);
82 F_SET(sp, SC_EX);
84 /* Move out of the vi screen. */
85 (void)ex_puts(sp, "\n");
87 return (0);
91 * v_join -- [count]J
92 * Join lines together.
94 * PUBLIC: int v_join __P((SCR *, VICMD *));
96 int
97 v_join(SCR *sp, VICMD *vp)
99 EXCMD cmd;
100 int lno;
103 * YASC.
104 * The general rule is that '#J' joins # lines, counting the current
105 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
106 * current and next lines. This doesn't map well into the ex command
107 * (which takes two line numbers), so we handle it here. Note that
108 * we never test for EOF -- historically going past the end of file
109 * worked just fine.
111 lno = vp->m_start.lno + 1;
112 if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
113 lno = vp->m_start.lno + (vp->count - 1);
115 ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
116 return (v_exec_ex(sp, vp, &cmd));
120 * v_shiftl -- [count]<motion
121 * Shift lines left.
123 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
126 v_shiftl(SCR *sp, VICMD *vp)
128 EXCMD cmd;
129 static CHAR_T lt[] = {'<', 0};
131 ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
132 argv_exp0(sp, &cmd, lt, 2);
133 return (v_exec_ex(sp, vp, &cmd));
137 * v_shiftr -- [count]>motion
138 * Shift lines right.
140 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
143 v_shiftr(SCR *sp, VICMD *vp)
145 EXCMD cmd;
146 static CHAR_T gt[] = {'>', 0};
148 ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
149 argv_exp0(sp, &cmd, gt, 2);
150 return (v_exec_ex(sp, vp, &cmd));
154 * v_suspend -- ^Z
155 * Suspend vi.
157 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
160 v_suspend(SCR *sp, VICMD *vp)
162 EXCMD cmd;
163 static CHAR_T suspend[] = {'s', 'u', 's', 'p', 'e', 'n', 'd', 0};
165 ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
166 argv_exp0(sp, &cmd, suspend, sizeof(suspend)/sizeof(CHAR_T));
167 return (v_exec_ex(sp, vp, &cmd));
171 * v_switch -- ^^
172 * Switch to the previous file.
174 * PUBLIC: int v_switch __P((SCR *, VICMD *));
177 v_switch(SCR *sp, VICMD *vp)
179 EXCMD cmd;
180 char *name;
181 const CHAR_T *wp;
182 size_t wlen;
185 * Try the alternate file name, then the previous file
186 * name. Use the real name, not the user's current name.
188 if ((name = sp->alt_name) == NULL) {
189 msgq(sp, M_ERR, "180|No previous file to edit");
190 return (1);
193 /* If autowrite is set, write out the file. */
194 if (file_m1(sp, 0, FS_ALL))
195 return (1);
197 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
198 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
199 argv_exp0(sp, &cmd, wp, wlen);
200 return (v_exec_ex(sp, vp, &cmd));
204 * v_tagpush -- ^[
205 * Do a tag search on the cursor keyword.
207 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
210 v_tagpush(SCR *sp, VICMD *vp)
212 EXCMD cmd;
214 #ifdef GTAGS
215 if (O_ISSET(sp, O_GTAGSMODE) && vp->m_start.cno == 0)
216 ex_cinit(sp, &cmd, C_RTAG, 0, OOBLNO, 0, 0);
217 else
218 #endif
219 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
220 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1);
221 return (v_exec_ex(sp, vp, &cmd));
225 * v_tagpop -- ^T
226 * Pop the tags stack.
228 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
231 v_tagpop(SCR *sp, VICMD *vp)
233 EXCMD cmd;
235 ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
236 return (v_exec_ex(sp, vp, &cmd));
240 * v_filter -- [count]!motion command(s)
241 * Run range through shell commands, replacing text.
243 * PUBLIC: int v_filter __P((SCR *, VICMD *));
246 v_filter(SCR *sp, VICMD *vp)
248 EXCMD cmd;
249 TEXT *tp;
252 * !!!
253 * Historical vi permitted "!!" in an empty file, and it's handled
254 * as a special case in the ex_bang routine. Don't modify this setup
255 * without understanding that one. In particular, note that we're
256 * manipulating the ex argument structures behind ex's back.
258 * !!!
259 * Historical vi did not permit the '!' command to be associated with
260 * a non-line oriented motion command, in general, although it did
261 * with search commands. So, !f; and !w would fail, but !/;<CR>
262 * would succeed, even if they all moved to the same location in the
263 * current line. I don't see any reason to disallow '!' using any of
264 * the possible motion commands.
266 * !!!
267 * Historical vi ran the last bang command if N or n was used as the
268 * search motion.
270 if (F_ISSET(vp, VC_ISDOT) ||
271 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
272 static CHAR_T bang[] = {'!', 0};
273 ex_cinit(sp,
274 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
275 EXP(sp)->argsoff = 0; /* XXX */
277 if (argv_exp1(sp, &cmd, bang, 1, 1))
278 return (1);
279 cmd.argc = EXP(sp)->argsoff; /* XXX */
280 cmd.argv = EXP(sp)->args; /* XXX */
281 return (v_exec_ex(sp, vp, &cmd));
284 /* Get the command from the user. */
285 if (v_tcmd(sp, vp,
286 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
287 return (1);
290 * Check to see if the user changed their mind.
292 * !!!
293 * Entering <escape> on an empty line was historically an error,
294 * this implementation doesn't bother.
296 tp = sp->tiq.cqh_first;
297 if (tp->term != TERM_OK) {
298 vp->m_final.lno = sp->lno;
299 vp->m_final.cno = sp->cno;
300 return (0);
303 /* Home the cursor. */
304 vs_home(sp);
306 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
307 EXP(sp)->argsoff = 0; /* XXX */
309 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
310 return (1);
311 cmd.argc = EXP(sp)->argsoff; /* XXX */
312 cmd.argv = EXP(sp)->args; /* XXX */
313 return (v_exec_ex(sp, vp, &cmd));
317 * v_exec_ex --
318 * Execute an ex command.
320 * PUBLIC: int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
323 v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
325 int rval;
327 rval = exp->cmd->fn(sp, exp);
328 return (v_ex_done(sp, vp) || rval);
332 * v_ex -- :
333 * Execute a colon command line.
335 * PUBLIC: int v_ex __P((SCR *, VICMD *));
338 v_ex(SCR *sp, VICMD *vp)
340 WIN *wp;
341 TEXT *tp;
342 int do_cedit, do_resolution, ifcontinue;
344 wp = sp->wp;
347 * !!!
348 * If we put out more than a single line of messages, or ex trashes
349 * the screen, the user may continue entering ex commands. We find
350 * this out when we do the screen/message resolution. We can't enter
351 * completely into ex mode however, because the user can elect to
352 * return into vi mode by entering any key, i.e. we have to be in raw
353 * mode.
355 for (do_cedit = do_resolution = 0;;) {
357 * !!!
358 * There may already be an ex command waiting to run. If
359 * so, we continue with it.
361 if (!EXCMD_RUNNING(wp)) {
362 /* Get a command. */
363 if (v_tcmd(sp, vp, ':',
364 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
365 return (1);
366 tp = sp->tiq.cqh_first;
369 * If the user entered a single <esc>, they want to
370 * edit their colon command history. If they already
371 * entered some text, move it into the edit history.
373 if (tp->term == TERM_CEDIT) {
374 if (tp->len > 1 && v_ecl_log(sp, tp))
375 return (1);
376 do_cedit = 1;
377 break;
380 /* If the user didn't enter anything, return. */
381 if (tp->term == TERM_BS)
382 break;
384 /* Log the command. */
385 if (O_STR(sp, O_CEDIT) != NULL)
386 (void)v_ecl_log(sp, tp);
388 /* Push a command on the command stack. */
389 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
390 return (1);
393 /* Home the cursor. */
394 vs_home(sp);
397 * !!!
398 * If the editor wrote the screen behind curses back, put out
399 * a <newline> so that we don't overwrite the user's command
400 * with its output or the next want-to-continue? message. This
401 * doesn't belong here, but I can't find another place to put
402 * it. See, we resolved the output from the last ex command,
403 * and the user entered another one. This is the only place
404 * where we have control before the ex command writes output.
405 * We could get control in vs_msg(), but we have no way to know
406 * if command didn't put out any output when we try and resolve
407 * this command. This fixes a bug where combinations of ex
408 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
410 if (F_ISSET(sp, SC_SCR_EXWROTE))
411 (void)putchar('\n');
413 /* Call the ex parser. */
414 (void)ex_cmd(sp);
416 /* Flush ex messages. */
417 (void)ex_fflush(sp);
419 /* Resolve any messages. */
420 if (vs_ex_resolve(sp, &ifcontinue))
421 return (1);
424 * Continue or return. If continuing, make sure that we
425 * eventually do resolution.
427 if (!ifcontinue)
428 break;
429 do_resolution = 1;
431 /* If we're continuing, it's a new command. */
432 ++sp->ccnt;
436 * If the user previously continued an ex command, we have to do
437 * resolution to clean up the screen. Don't wait, we already did
438 * that.
440 if (do_resolution) {
441 F_SET(sp, SC_EX_WAIT_NO);
442 if (vs_ex_resolve(sp, &ifcontinue))
443 return (1);
446 /* Cleanup from the ex command. */
447 if (v_ex_done(sp, vp))
448 return (1);
450 /* The user may want to edit their colon command history. */
451 if (do_cedit)
452 return (v_ecl(sp));
454 return (0);
458 * v_ex_done --
459 * Cleanup from an ex command.
461 static int
462 v_ex_done(SCR *sp, VICMD *vp)
464 size_t len;
467 * The only cursor modifications are real, however, the underlying
468 * line may have changed; don't trust anything. This code has been
469 * a remarkably fertile place for bugs. Do a reality check on a
470 * cursor value, and make sure it's okay. If necessary, change it.
471 * Ex keeps track of the line number, but it cares less about the
472 * column and it may have disappeared.
474 * Don't trust ANYTHING.
476 * XXX
477 * Ex will soon have to start handling the column correctly; see
478 * the POSIX 1003.2 standard.
480 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
481 sp->lno = 1;
482 sp->cno = 0;
483 } else if (sp->cno >= len)
484 sp->cno = len ? len - 1 : 0;
486 vp->m_final.lno = sp->lno;
487 vp->m_final.cno = sp->cno;
490 * Don't re-adjust the cursor after executing an ex command,
491 * and ex movements are permanent.
493 F_CLR(vp, VM_RCM_MASK);
494 F_SET(vp, VM_RCM_SET);
496 return (0);
500 * v_ecl --
501 * Start an edit window on the colon command-line commands.
503 static int
504 v_ecl(SCR *sp)
506 GS *gp;
507 WIN *wp;
508 SCR *new;
510 /* Initialize the screen, if necessary. */
511 gp = sp->gp;
512 wp = sp->wp;
513 if (wp->ccl_sp == NULL && v_ecl_init(sp))
514 return (1);
516 /* Get a new screen. */
517 if (screen_init(gp, sp, &new))
518 return (1);
519 if (vs_split(sp, new, 1)) {
520 (void)screen_end(new);
521 return (1);
524 /* Attach to the screen. */
525 new->ep = wp->ccl_sp->ep;
526 ++new->ep->refcnt;
527 CIRCLEQ_INSERT_HEAD(&new->ep->scrq, new, eq);
529 new->frp = wp->ccl_sp->frp;
530 new->frp->flags = sp->frp->flags;
531 new->conv = wp->ccl_sp->conv;
533 /* Move the cursor to the end. */
534 (void)db_last(new, &new->lno);
535 if (new->lno == 0)
536 new->lno = 1;
538 /* Remember the originating window. */
539 sp->ccl_parent = sp;
541 /* It's a special window. */
542 F_SET(new, SC_COMEDIT);
544 /* Don't encode on writing to DB. */
545 o_set(new, O_FILEENCODING, OS_STRDUP, "WCHAR_T", 0);
547 /* Set up the switch. */
548 sp->nextdisp = new;
549 F_SET(sp, SC_SSWITCH);
550 return (0);
554 * v_ecl_exec --
555 * Execute a command from a colon command-line window.
557 * PUBLIC: int v_ecl_exec __P((SCR *));
560 v_ecl_exec(SCR *sp)
562 size_t len;
563 CHAR_T *p;
565 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
566 v_emsg(sp, NULL, VIM_EMPTY);
567 return (1);
569 if (len == 0) {
570 msgq(sp, M_BERR, "307|No ex command to execute");
571 return (1);
574 /* Push the command on the command stack. */
575 if (ex_run_str(sp, NULL, p, len, 0, 0))
576 return (1);
578 /* Set up the switch. */
579 sp->nextdisp = sp->ccl_parent;
580 F_SET(sp, SC_EXIT);
581 return (0);
585 * v_ecl_log --
586 * Log a command into the colon command-line log file.
588 static int
589 v_ecl_log(SCR *sp, TEXT *tp)
591 db_recno_t lno;
592 int rval;
593 CHAR_T *p;
594 size_t len;
595 SCR *ccl_sp;
597 /* Initialize the screen, if necessary. */
598 if (sp->wp->ccl_sp == NULL && v_ecl_init(sp))
599 return (1);
601 ccl_sp = sp->wp->ccl_sp;
604 * Don't log colon command window commands into the colon command
605 * window...
607 if (sp->ep == ccl_sp->ep)
608 return (0);
610 if (db_last(ccl_sp, &lno)) {
611 return (1);
613 /* Don't log line that is identical to previous one */
614 if (lno > 0 &&
615 !db_get(ccl_sp, lno, 0, &p, &len) &&
616 len == tp->len &&
617 !MEMCMP(tp->lb, p, len))
618 rval = 0;
619 else {
620 rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len);
621 /* XXXX end "transaction" on ccl */
622 /* Is this still necessary now that we no longer hijack sp ? */
623 log_cursor(ccl_sp);
626 return (rval);
630 * v_ecl_init --
631 * Initialize the colon command-line log file.
633 static int
634 v_ecl_init(SCR *sp)
636 FREF *frp;
637 GS *gp;
638 WIN *wp;
640 gp = sp->gp;
641 wp = sp->wp;
643 /* Get a temporary file. */
644 if ((frp = file_add(sp, NULL)) == NULL)
645 return (1);
648 * XXX
649 * Create a screen -- the file initialization code wants one.
651 if (screen_init(gp, sp, &wp->ccl_sp))
652 return (1);
653 conv_enc(wp->ccl_sp, O_FILEENCODING, "WCHAR_T");
654 if (file_init(wp->ccl_sp, frp, NULL, 0)) {
655 (void)screen_end(wp->ccl_sp);
656 wp->ccl_sp = 0;
657 return (1);
660 /* The underlying file isn't recoverable. */
661 F_CLR(wp->ccl_sp->ep, F_RCV_ON);
663 return (0);