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.
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 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.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
*));
37 * Repeat the previous substitution.
39 * PUBLIC: int v_again __P((SCR *, VICMD *));
42 v_again(SCR
*sp
, VICMD
*vp
)
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
));
54 * Switch the editor into EX mode.
56 * PUBLIC: int v_exmode __P((SCR *, VICMD *));
59 v_exmode(SCR
*sp
, VICMD
*vp
)
65 /* Try and switch screens -- the screen may not permit it. */
66 if (gp
->scr_screen(sp
, SC_EX
)) {
68 "207|The Q command requires the ex terminal interface");
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
);
82 /* Move out of the vi screen. */
83 (void)ex_puts(sp
, "\n");
90 * Join lines together.
92 * PUBLIC: int v_join __P((SCR *, VICMD *));
95 v_join(SCR
*sp
, VICMD
*vp
)
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
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
121 * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
124 v_shiftl(SCR
*sp
, VICMD
*vp
)
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
138 * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
141 v_shiftr(SCR
*sp
, VICMD
*vp
)
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
));
155 * PUBLIC: int v_suspend __P((SCR *, VICMD *));
158 v_suspend(SCR
*sp
, VICMD
*vp
)
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
));
170 * Switch to the previous file.
172 * PUBLIC: int v_switch __P((SCR *, VICMD *));
175 v_switch(SCR
*sp
, VICMD
*vp
)
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");
191 /* If autowrite is set, write out the file. */
192 if (file_m1(sp
, 0, FS_ALL
))
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
));
203 * Do a tag search on the cursor keyword.
205 * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
208 v_tagpush(SCR
*sp
, VICMD
*vp
)
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
));
219 * Pop the tags stack.
221 * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
224 v_tagpop(SCR
*sp
, VICMD
*vp
)
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
)
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.
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.
260 * Historical vi ran the last bang command if N or n was used as the
263 if (F_ISSET(vp
, VC_ISDOT
) ||
264 ISCMD(vp
->rkp
, 'N') || ISCMD(vp
->rkp
, 'n')) {
265 static CHAR_T bang
[] = {'!', 0};
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))
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. */
279 '!', TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_FILEC
| TXT_PROMPT
))
283 * Check to see if the user changed their mind.
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
;
296 /* Home the cursor. */
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))
304 cmd
.argc
= EXP(sp
)->argsoff
; /* XXX */
305 cmd
.argv
= EXP(sp
)->args
; /* XXX */
306 return (v_exec_ex(sp
, vp
, &cmd
));
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
)
320 rval
= exp
->cmd
->fn(sp
, exp
);
321 return (v_ex_done(sp
, vp
) || rval
);
326 * Execute a colon command line.
328 * PUBLIC: int v_ex __P((SCR *, VICMD *));
331 v_ex(SCR
*sp
, VICMD
*vp
)
335 int do_cedit
, do_resolution
, ifcontinue
;
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
348 for (do_cedit
= do_resolution
= 0;;) {
351 * There may already be an ex command waiting to run. If
352 * so, we continue with it.
354 if (!EXCMD_RUNNING(wp
)) {
356 if (v_tcmd(sp
, vp
, ':',
357 TXT_BS
| TXT_CEDIT
| TXT_FILEC
| TXT_PROMPT
))
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
))
373 /* If the user didn't enter anything, return. */
374 if (tp
->term
== TERM_BS
)
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))
386 /* Home the cursor. */
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
))
406 /* Call the ex parser. */
409 /* Flush ex messages. */
412 /* Resolve any messages. */
413 if (vs_ex_resolve(sp
, &ifcontinue
))
417 * Continue or return. If continuing, make sure that we
418 * eventually do resolution.
424 /* If we're continuing, it's a new command. */
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
434 F_SET(sp
, SC_EX_WAIT_NO
);
435 if (vs_ex_resolve(sp
, &ifcontinue
))
439 /* Cleanup from the ex command. */
440 if (v_ex_done(sp
, vp
))
443 /* The user may want to edit their colon command history. */
452 * Cleanup from an ex command.
455 v_ex_done(SCR
*sp
, VICMD
*vp
)
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.
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
)) {
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
);
494 * Start an edit window on the colon command-line commands.
503 /* Initialize the screen, if necessary. */
506 if (wp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
509 /* Get a new screen. */
510 if (screen_init(gp
, sp
, &new))
512 if (vs_split(sp
, new, 1)) {
513 (void)screen_end(new);
517 /* Attach to the screen. */
518 new->ep
= wp
->ccl_sp
->ep
;
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
);
530 /* Remember the originating window. */
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. */
541 F_SET(sp
, SC_SSWITCH
);
547 * Execute a command from a colon command-line window.
549 * PUBLIC: int v_ecl_exec __P((SCR *));
557 if (db_get(sp
, sp
->lno
, 0, &p
, &len
) && sp
->lno
== 1) {
558 v_emsg(sp
, NULL
, VIM_EMPTY
);
562 msgq(sp
, M_BERR
, "307|No ex command to execute");
566 /* Push the command on the command stack. */
567 if (ex_run_str(sp
, NULL
, p
, len
, 0, 0))
570 /* Set up the switch. */
571 sp
->nextdisp
= sp
->ccl_parent
;
578 * Log a command into the colon command-line log file.
581 v_ecl_log(SCR
*sp
, TEXT
*tp
)
589 /* Initialize the screen, if necessary. */
590 if (sp
->wp
->ccl_sp
== NULL
&& v_ecl_init(sp
))
593 ccl_sp
= sp
->wp
->ccl_sp
;
596 * Don't log colon command window commands into the colon command
599 if (sp
->ep
== ccl_sp
->ep
)
602 if (db_last(ccl_sp
, &lno
)) {
605 /* Don't log line that is identical to previous one */
607 !db_get(ccl_sp
, lno
, 0, &p
, &len
) &&
609 !MEMCMP(tp
->lb
, p
, len
))
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 ? */
623 * Initialize the colon command-line log file.
635 /* Get a temporary file. */
636 if ((frp
= file_add(sp
, NULL
)) == NULL
)
641 * Create a screen -- the file initialization code wants one.
643 if (screen_init(gp
, sp
, &wp
->ccl_sp
))
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
);
652 /* The underlying file isn't recoverable. */
653 F_CLR(wp
->ccl_sp
->ep
, F_RCV_ON
);