1 /* $OpenBSD: v_ex.c,v 1.10 2009/11/14 17:44:53 jsg Exp $ */
2
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.
8 *
9 * See the LICENSE file for redistribution information.
10 */
11
12 #include "config.h"
13
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
17
18 #include <bitstring.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "../common/common.h"
26 #include "vi.h"
27
28 static int v_ecl(SCR *);
29 static int v_ecl_init(SCR *);
30 static int v_ecl_log(SCR *, TEXT *);
31 static int v_ex_done(SCR *, VICMD *);
32 static int v_exec_ex(SCR *, VICMD *, EXCMD *);
33
34 /*
35 * v_again -- &
36 * Repeat the previous substitution.
37 *
38 * PUBLIC: int v_again(SCR *, VICMD *);
39 */
40 int
v_again(sp,vp)41 v_again(sp, vp)
42 SCR *sp;
43 VICMD *vp;
44 {
45 ARGS *ap[2], a;
46 EXCMD cmd;
47
48 ex_cinit(&cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1, ap);
49 ex_cadd(&cmd, &a, "", 1);
50
51 return (v_exec_ex(sp, vp, &cmd));
52 }
53
54 /*
55 * v_exmode -- Q
56 * Switch the editor into EX mode.
57 *
58 * PUBLIC: int v_exmode(SCR *, VICMD *);
59 */
60 int
v_exmode(sp,vp)61 v_exmode(sp, vp)
62 SCR *sp;
63 VICMD *vp;
64 {
65 GS *gp;
66
67 gp = sp->gp;
68
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);
74 }
75 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
76
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);
81
82 /* Switch to ex mode. */
83 F_CLR(sp, SC_VI | SC_SCR_VI);
84 F_SET(sp, SC_EX);
85
86 /* Move out of the vi screen. */
87 (void)ex_puts(sp, "\n");
88
89 return (0);
90 }
91
92 /*
93 * v_join -- [count]J
94 * Join lines together.
95 *
96 * PUBLIC: int v_join(SCR *, VICMD *);
97 */
98 int
v_join(sp,vp)99 v_join(sp, vp)
100 SCR *sp;
101 VICMD *vp;
102 {
103 EXCMD cmd;
104 int lno;
105
106 /*
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.
114 */
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);
118
119 ex_cinit(&cmd, C_JOIN, 2, vp->m_start.lno, lno, 0, NULL);
120 return (v_exec_ex(sp, vp, &cmd));
121 }
122
123 /*
124 * v_shiftl -- [count]<motion
125 * Shift lines left.
126 *
127 * PUBLIC: int v_shiftl(SCR *, VICMD *);
128 */
129 int
v_shiftl(sp,vp)130 v_shiftl(sp, vp)
131 SCR *sp;
132 VICMD *vp;
133 {
134 ARGS *ap[2], a;
135 EXCMD cmd;
136
137 ex_cinit(&cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0, ap);
138 ex_cadd(&cmd, &a, "<", 1);
139 return (v_exec_ex(sp, vp, &cmd));
140 }
141
142 /*
143 * v_shiftr -- [count]>motion
144 * Shift lines right.
145 *
146 * PUBLIC: int v_shiftr(SCR *, VICMD *);
147 */
148 int
v_shiftr(sp,vp)149 v_shiftr(sp, vp)
150 SCR *sp;
151 VICMD *vp;
152 {
153 ARGS *ap[2], a;
154 EXCMD cmd;
155
156 ex_cinit(&cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0, ap);
157 ex_cadd(&cmd, &a, ">", 1);
158 return (v_exec_ex(sp, vp, &cmd));
159 }
160
161 /*
162 * v_suspend -- ^Z
163 * Suspend vi.
164 *
165 * PUBLIC: int v_suspend(SCR *, VICMD *);
166 */
167 int
v_suspend(sp,vp)168 v_suspend(sp, vp)
169 SCR *sp;
170 VICMD *vp;
171 {
172 ARGS *ap[2], a;
173 EXCMD cmd;
174
175 ex_cinit(&cmd, C_STOP, 0, OOBLNO, OOBLNO, 0, ap);
176 ex_cadd(&cmd, &a, "suspend", sizeof("suspend") - 1);
177 return (v_exec_ex(sp, vp, &cmd));
178 }
179
180 /*
181 * v_switch -- ^^
182 * Switch to the previous file.
183 *
184 * PUBLIC: int v_switch(SCR *, VICMD *);
185 */
186 int
v_switch(sp,vp)187 v_switch(sp, vp)
188 SCR *sp;
189 VICMD *vp;
190 {
191 ARGS *ap[2], a;
192 EXCMD cmd;
193 char *name;
194
195 /*
196 * Try the alternate file name, then the previous file
197 * name. Use the real name, not the user's current name.
198 */
199 if (sp->alt_name == NULL) {
200 msgq(sp, M_ERR, "180|No previous file to edit");
201 return (1);
202 }
203 if ((name = strdup(sp->alt_name)) == NULL) {
204 msgq(sp, M_SYSERR, NULL);
205 return (1);
206 }
207
208 /* If autowrite is set, write out the file. */
209 if (file_m1(sp, 0, FS_ALL)) {
210 free(name);
211 return (1);
212 }
213
214 ex_cinit(&cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0, ap);
215 ex_cadd(&cmd, &a, name, strlen(name));
216 return (v_exec_ex(sp, vp, &cmd));
217 }
218
219 /*
220 * v_tagpush -- ^[
221 * Do a tag search on the cursor keyword.
222 *
223 * PUBLIC: int v_tagpush(SCR *, VICMD *);
224 */
225 int
v_tagpush(sp,vp)226 v_tagpush(sp, vp)
227 SCR *sp;
228 VICMD *vp;
229 {
230 ARGS *ap[2], a;
231 EXCMD cmd;
232
233 ex_cinit(&cmd, C_TAG, 0, OOBLNO, 0, 0, ap);
234 ex_cadd(&cmd, &a, VIP(sp)->keyw, strlen(VIP(sp)->keyw));
235 return (v_exec_ex(sp, vp, &cmd));
236 }
237
238 /*
239 * v_tagpop -- ^T
240 * Pop the tags stack.
241 *
242 * PUBLIC: int v_tagpop(SCR *, VICMD *);
243 */
244 int
v_tagpop(sp,vp)245 v_tagpop(sp, vp)
246 SCR *sp;
247 VICMD *vp;
248 {
249 EXCMD cmd;
250
251 ex_cinit(&cmd, C_TAGPOP, 0, OOBLNO, 0, 0, NULL);
252 return (v_exec_ex(sp, vp, &cmd));
253 }
254
255 /*
256 * v_filter -- [count]!motion command(s)
257 * Run range through shell commands, replacing text.
258 *
259 * PUBLIC: int v_filter(SCR *, VICMD *);
260 */
261 int
v_filter(sp,vp)262 v_filter(sp, vp)
263 SCR *sp;
264 VICMD *vp;
265 {
266 EXCMD cmd;
267 TEXT *tp;
268
269 /*
270 * !!!
271 * Historical vi permitted "!!" in an empty file, and it's handled
272 * as a special case in the ex_bang routine. Don't modify this setup
273 * without understanding that one. In particular, note that we're
274 * manipulating the ex argument structures behind ex's back.
275 *
276 * !!!
277 * Historical vi did not permit the '!' command to be associated with
278 * a non-line oriented motion command, in general, although it did
279 * with search commands. So, !f; and !w would fail, but !/;<CR>
280 * would succeed, even if they all moved to the same location in the
281 * current line. I don't see any reason to disallow '!' using any of
282 * the possible motion commands.
283 *
284 * !!!
285 * Historical vi ran the last bang command if N or n was used as the
286 * search motion.
287 */
288 if (F_ISSET(vp, VC_ISDOT) ||
289 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
290 ex_cinit(&cmd, C_BANG,
291 2, vp->m_start.lno, vp->m_stop.lno, 0, NULL);
292 EXP(sp)->argsoff = 0; /* XXX */
293
294 if (argv_exp1(sp, &cmd, "!", 1, 1))
295 return (1);
296 cmd.argc = EXP(sp)->argsoff; /* XXX */
297 cmd.argv = EXP(sp)->args; /* XXX */
298 return (v_exec_ex(sp, vp, &cmd));
299 }
300
301 /* Get the command from the user. */
302 if (v_tcmd(sp, vp,
303 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
304 return (1);
305
306 /*
307 * Check to see if the user changed their mind.
308 *
309 * !!!
310 * Entering <escape> on an empty line was historically an error,
311 * this implementation doesn't bother.
312 */
313 tp = CIRCLEQ_FIRST(&sp->tiq);
314 if (tp->term != TERM_OK) {
315 vp->m_final.lno = sp->lno;
316 vp->m_final.cno = sp->cno;
317 return (0);
318 }
319
320 /* Home the cursor. */
321 vs_home(sp);
322
323 ex_cinit(&cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0, NULL);
324 EXP(sp)->argsoff = 0; /* XXX */
325
326 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
327 return (1);
328 cmd.argc = EXP(sp)->argsoff; /* XXX */
329 cmd.argv = EXP(sp)->args; /* XXX */
330 return (v_exec_ex(sp, vp, &cmd));
331 }
332
333 /*
334 * v_event_exec --
335 * Execute some command(s) based on an event.
336 *
337 * PUBLIC: int v_event_exec(SCR *, VICMD *);
338 */
339 int
v_event_exec(sp,vp)340 v_event_exec(sp, vp)
341 SCR *sp;
342 VICMD *vp;
343 {
344 EXCMD cmd;
345
346 switch (vp->ev.e_event) {
347 case E_QUIT:
348 ex_cinit(&cmd, C_QUIT, 0, OOBLNO, OOBLNO, 0, NULL);
349 break;
350 case E_WRITE:
351 ex_cinit(&cmd, C_WRITE, 0, OOBLNO, OOBLNO, 0, NULL);
352 break;
353 default:
354 abort();
355 }
356 return (v_exec_ex(sp, vp, &cmd));
357 }
358
359 /*
360 * v_exec_ex --
361 * Execute an ex command.
362 */
363 static int
v_exec_ex(sp,vp,exp)364 v_exec_ex(sp, vp, exp)
365 SCR *sp;
366 VICMD *vp;
367 EXCMD *exp;
368 {
369 int rval;
370
371 rval = exp->cmd->fn(sp, exp);
372 return (v_ex_done(sp, vp) || rval);
373 }
374
375 /*
376 * v_ex -- :
377 * Execute a colon command line.
378 *
379 * PUBLIC: int v_ex(SCR *, VICMD *);
380 */
381 int
v_ex(sp,vp)382 v_ex(sp, vp)
383 SCR *sp;
384 VICMD *vp;
385 {
386 GS *gp;
387 TEXT *tp;
388 int do_cedit, do_resolution, ifcontinue;
389
390 gp = sp->gp;
391
392 /*
393 * !!!
394 * If we put out more than a single line of messages, or ex trashes
395 * the screen, the user may continue entering ex commands. We find
396 * this out when we do the screen/message resolution. We can't enter
397 * completely into ex mode however, because the user can elect to
398 * return into vi mode by entering any key, i.e. we have to be in raw
399 * mode.
400 */
401 for (do_cedit = do_resolution = 0;;) {
402 /*
403 * !!!
404 * There may already be an ex command waiting to run. If
405 * so, we continue with it.
406 */
407 if (!EXCMD_RUNNING(gp)) {
408 /* Get a command. */
409 if (v_tcmd(sp, vp, ':',
410 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
411 return (1);
412 tp = CIRCLEQ_FIRST(&sp->tiq);
413
414 /*
415 * If the user entered a single <esc>, they want to
416 * edit their colon command history. If they already
417 * entered some text, move it into the edit history.
418 */
419 if (tp->term == TERM_CEDIT) {
420 if (tp->len > 1 && v_ecl_log(sp, tp))
421 return (1);
422 do_cedit = 1;
423 break;
424 }
425
426 /* If the user changed their mind, return. */
427 if (tp->term != TERM_OK)
428 break;
429
430 /* Log the command. */
431 if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
432 return (1);
433
434 /* Push a command on the command stack. */
435 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
436 return (1);
437 }
438
439 /* Home the cursor. */
440 vs_home(sp);
441
442 /*
443 * !!!
444 * If the editor wrote the screen behind curses back, put out
445 * a <newline> so that we don't overwrite the user's command
446 * with its output or the next want-to-continue? message. This
447 * doesn't belong here, but I can't find another place to put
448 * it. See, we resolved the output from the last ex command,
449 * and the user entered another one. This is the only place
450 * where we have control before the ex command writes output.
451 * We could get control in vs_msg(), but we have no way to know
452 * if command didn't put out any output when we try and resolve
453 * this command. This fixes a bug where combinations of ex
454 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
455 */
456 if (F_ISSET(sp, SC_SCR_EXWROTE))
457 (void)putchar('\n');
458
459 /* Call the ex parser. */
460 (void)ex_cmd(sp);
461
462 /* Flush ex messages. */
463 (void)ex_fflush(sp);
464
465 /* Resolve any messages. */
466 if (vs_ex_resolve(sp, &ifcontinue))
467 return (1);
468
469 /*
470 * Continue or return. If continuing, make sure that we
471 * eventually do resolution.
472 */
473 if (!ifcontinue)
474 break;
475 do_resolution = 1;
476
477 /* If we're continuing, it's a new command. */
478 ++sp->ccnt;
479 }
480
481 /*
482 * If the user previously continued an ex command, we have to do
483 * resolution to clean up the screen. Don't wait, we already did
484 * that.
485 */
486 if (do_resolution) {
487 F_SET(sp, SC_EX_WAIT_NO);
488 if (vs_ex_resolve(sp, &ifcontinue))
489 return (1);
490 }
491
492 /* Cleanup from the ex command. */
493 if (v_ex_done(sp, vp))
494 return (1);
495
496 /* The user may want to edit their colon command history. */
497 if (do_cedit)
498 return (v_ecl(sp));
499
500 return (0);
501 }
502
503 /*
504 * v_ex_done --
505 * Cleanup from an ex command.
506 */
507 static int
v_ex_done(sp,vp)508 v_ex_done(sp, vp)
509 SCR *sp;
510 VICMD *vp;
511 {
512 size_t len;
513
514 /*
515 * The only cursor modifications are real, however, the underlying
516 * line may have changed; don't trust anything. This code has been
517 * a remarkably fertile place for bugs. Do a reality check on a
518 * cursor value, and make sure it's okay. If necessary, change it.
519 * Ex keeps track of the line number, but it cares less about the
520 * column and it may have disappeared.
521 *
522 * Don't trust ANYTHING.
523 *
524 * XXX
525 * Ex will soon have to start handling the column correctly; see
526 * the POSIX 1003.2 standard.
527 */
528 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
529 sp->lno = 1;
530 sp->cno = 0;
531 } else if (sp->cno >= len)
532 sp->cno = len ? len - 1 : 0;
533
534 vp->m_final.lno = sp->lno;
535 vp->m_final.cno = sp->cno;
536
537 /*
538 * Don't re-adjust the cursor after executing an ex command,
539 * and ex movements are permanent.
540 */
541 F_CLR(vp, VM_RCM_MASK);
542 F_SET(vp, VM_RCM_SET);
543
544 return (0);
545 }
546
547 /*
548 * v_ecl --
549 * Start an edit window on the colon command-line commands.
550 */
551 static int
v_ecl(sp)552 v_ecl(sp)
553 SCR *sp;
554 {
555 GS *gp;
556 SCR *new;
557
558 /* Initialize the screen, if necessary. */
559 gp = sp->gp;
560 if (gp->ccl_sp == NULL && v_ecl_init(sp))
561 return (1);
562
563 /* Get a new screen. */
564 if (screen_init(gp, sp, &new))
565 return (1);
566 if (vs_split(sp, new, 1)) {
567 (void)screen_end(new);
568 return (1);
569 }
570
571 /* Attach to the screen. */
572 new->ep = gp->ccl_sp->ep;
573 ++new->ep->refcnt;
574
575 new->frp = gp->ccl_sp->frp;
576 new->frp->flags = sp->frp->flags;
577
578 /* Move the cursor to the end. */
579 (void)db_last(new, &new->lno);
580 if (new->lno == 0)
581 new->lno = 1;
582
583 /* Remember the originating window. */
584 sp->ccl_parent = sp;
585
586 /* It's a special window. */
587 F_SET(new, SC_COMEDIT);
588
589 /* Set up the switch. */
590 sp->nextdisp = new;
591 F_SET(sp, SC_SSWITCH);
592 return (0);
593 }
594
595 /*
596 * v_ecl_exec --
597 * Execute a command from a colon command-line window.
598 *
599 * PUBLIC: int v_ecl_exec(SCR *);
600 */
601 int
v_ecl_exec(sp)602 v_ecl_exec(sp)
603 SCR *sp;
604 {
605 size_t len;
606 char *p;
607
608 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
609 v_emsg(sp, NULL, VIM_EMPTY);
610 return (1);
611 }
612 if (len == 0) {
613 msgq(sp, M_BERR, "307|No ex command to execute");
614 return (1);
615 }
616
617 /* Push the command on the command stack. */
618 if (ex_run_str(sp, NULL, p, len, 0, 0))
619 return (1);
620
621 /* Set up the switch. */
622 sp->nextdisp = sp->ccl_parent;
623 F_SET(sp, SC_EXIT);
624 return (0);
625 }
626
627 /*
628 * v_ecl_log --
629 * Log a command into the colon command-line log file.
630 */
631 static int
v_ecl_log(sp,tp)632 v_ecl_log(sp, tp)
633 SCR *sp;
634 TEXT *tp;
635 {
636 EXF *save_ep;
637 recno_t lno;
638 int rval;
639
640 /* Initialize the screen, if necessary. */
641 if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
642 return (1);
643
644 /*
645 * Don't log colon command window commands into the colon command
646 * window...
647 */
648 if (sp->ep == sp->gp->ccl_sp->ep)
649 return (0);
650
651 /*
652 * XXX
653 * Swap the current EXF with the colon command file EXF. This
654 * isn't pretty, but too many routines "know" that sp->ep points
655 * to the current EXF.
656 */
657 save_ep = sp->ep;
658 sp->ep = sp->gp->ccl_sp->ep;
659 if (db_last(sp, &lno)) {
660 sp->ep = save_ep;
661 return (1);
662 }
663 rval = db_append(sp, 0, lno, tp->lb, tp->len);
664 sp->ep = save_ep;
665 return (rval);
666 }
667
668 /*
669 * v_ecl_init --
670 * Initialize the colon command-line log file.
671 */
672 static int
v_ecl_init(sp)673 v_ecl_init(sp)
674 SCR *sp;
675 {
676 FREF *frp;
677 GS *gp;
678
679 gp = sp->gp;
680
681 /* Get a temporary file. */
682 if ((frp = file_add(sp, NULL)) == NULL)
683 return (1);
684
685 /*
686 * XXX
687 * Create a screen -- the file initialization code wants one.
688 */
689 if (screen_init(gp, sp, &gp->ccl_sp))
690 return (1);
691 if (file_init(gp->ccl_sp, frp, NULL, 0)) {
692 (void)screen_end(gp->ccl_sp);
693 return (1);
694 }
695
696 /* The underlying file isn't recoverable. */
697 F_CLR(gp->ccl_sp->ep, F_RCV_ON);
698
699 return (0);
700 }
701