vi.c revision 1.28.2.1 1 /* $NetBSD: vi.c,v 1.28.2.1 2009/05/13 19:18:29 jym Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "config.h"
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/wait.h>
39
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: vi.c,v 1.28.2.1 2009/05/13 19:18:29 jym Exp $");
45 #endif
46 #endif /* not lint && not SCCSID */
47
48 /*
49 * vi.c: Vi mode commands.
50 */
51 #include "el.h"
52
53 private el_action_t cv_action(EditLine *, int);
54 private el_action_t cv_paste(EditLine *, int);
55
56 /* cv_action():
57 * Handle vi actions.
58 */
59 private el_action_t
60 cv_action(EditLine *el, int c)
61 {
62
63 if (el->el_chared.c_vcmd.action != NOP) {
64 /* 'cc', 'dd' and (possibly) friends */
65 if (c != el->el_chared.c_vcmd.action)
66 return CC_ERROR;
67
68 if (!(c & YANK))
69 cv_undo(el);
70 cv_yank(el, el->el_line.buffer,
71 (int)(el->el_line.lastchar - el->el_line.buffer));
72 el->el_chared.c_vcmd.action = NOP;
73 el->el_chared.c_vcmd.pos = 0;
74 if (!(c & YANK)) {
75 el->el_line.lastchar = el->el_line.buffer;
76 el->el_line.cursor = el->el_line.buffer;
77 }
78 if (c & INSERT)
79 el->el_map.current = el->el_map.key;
80
81 return (CC_REFRESH);
82 }
83 el->el_chared.c_vcmd.pos = el->el_line.cursor;
84 el->el_chared.c_vcmd.action = c;
85 return (CC_ARGHACK);
86 }
87
88 /* cv_paste():
89 * Paste previous deletion before or after the cursor
90 */
91 private el_action_t
92 cv_paste(EditLine *el, int c)
93 {
94 c_kill_t *k = &el->el_chared.c_kill;
95 size_t len = (size_t)(k->last - k->buf);
96
97 if (k->buf == NULL || len == 0)
98 return (CC_ERROR);
99 #ifdef DEBUG_PASTE
100 (void) fprintf(el->el_errfile, "Paste: \"%.*s\"\n", (int)len, k->buf);
101 #endif
102
103 cv_undo(el);
104
105 if (!c && el->el_line.cursor < el->el_line.lastchar)
106 el->el_line.cursor++;
107
108 c_insert(el, (int)len);
109 if (el->el_line.cursor + len > el->el_line.lastchar)
110 return (CC_ERROR);
111 (void) memcpy(el->el_line.cursor, k->buf, len);
112
113 return (CC_REFRESH);
114 }
115
116
117 /* vi_paste_next():
118 * Vi paste previous deletion to the right of the cursor
119 * [p]
120 */
121 protected el_action_t
122 /*ARGSUSED*/
123 vi_paste_next(EditLine *el, int c __attribute__((__unused__)))
124 {
125
126 return (cv_paste(el, 0));
127 }
128
129
130 /* vi_paste_prev():
131 * Vi paste previous deletion to the left of the cursor
132 * [P]
133 */
134 protected el_action_t
135 /*ARGSUSED*/
136 vi_paste_prev(EditLine *el, int c __attribute__((__unused__)))
137 {
138
139 return (cv_paste(el, 1));
140 }
141
142
143 /* vi_prev_big_word():
144 * Vi move to the previous space delimited word
145 * [B]
146 */
147 protected el_action_t
148 /*ARGSUSED*/
149 vi_prev_big_word(EditLine *el, int c)
150 {
151
152 if (el->el_line.cursor == el->el_line.buffer)
153 return (CC_ERROR);
154
155 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
156 el->el_line.buffer,
157 el->el_state.argument,
158 cv__isWord);
159
160 if (el->el_chared.c_vcmd.action != NOP) {
161 cv_delfini(el);
162 return (CC_REFRESH);
163 }
164 return (CC_CURSOR);
165 }
166
167
168 /* vi_prev_word():
169 * Vi move to the previous word
170 * [b]
171 */
172 protected el_action_t
173 /*ARGSUSED*/
174 vi_prev_word(EditLine *el, int c __attribute__((__unused__)))
175 {
176
177 if (el->el_line.cursor == el->el_line.buffer)
178 return (CC_ERROR);
179
180 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
181 el->el_line.buffer,
182 el->el_state.argument,
183 cv__isword);
184
185 if (el->el_chared.c_vcmd.action != NOP) {
186 cv_delfini(el);
187 return (CC_REFRESH);
188 }
189 return (CC_CURSOR);
190 }
191
192
193 /* vi_next_big_word():
194 * Vi move to the next space delimited word
195 * [W]
196 */
197 protected el_action_t
198 /*ARGSUSED*/
199 vi_next_big_word(EditLine *el, int c)
200 {
201
202 if (el->el_line.cursor >= el->el_line.lastchar - 1)
203 return (CC_ERROR);
204
205 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
206 el->el_line.lastchar, el->el_state.argument, cv__isWord);
207
208 if (el->el_map.type == MAP_VI)
209 if (el->el_chared.c_vcmd.action != NOP) {
210 cv_delfini(el);
211 return (CC_REFRESH);
212 }
213 return (CC_CURSOR);
214 }
215
216
217 /* vi_next_word():
218 * Vi move to the next word
219 * [w]
220 */
221 protected el_action_t
222 /*ARGSUSED*/
223 vi_next_word(EditLine *el, int c __attribute__((__unused__)))
224 {
225
226 if (el->el_line.cursor >= el->el_line.lastchar - 1)
227 return (CC_ERROR);
228
229 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
230 el->el_line.lastchar, el->el_state.argument, cv__isword);
231
232 if (el->el_map.type == MAP_VI)
233 if (el->el_chared.c_vcmd.action != NOP) {
234 cv_delfini(el);
235 return (CC_REFRESH);
236 }
237 return (CC_CURSOR);
238 }
239
240
241 /* vi_change_case():
242 * Vi change case of character under the cursor and advance one character
243 * [~]
244 */
245 protected el_action_t
246 vi_change_case(EditLine *el, int c)
247 {
248 int i;
249
250 if (el->el_line.cursor >= el->el_line.lastchar)
251 return (CC_ERROR);
252 cv_undo(el);
253 for (i = 0; i < el->el_state.argument; i++) {
254
255 c = *(unsigned char *)el->el_line.cursor;
256 if (isupper(c))
257 *el->el_line.cursor = tolower(c);
258 else if (islower(c))
259 *el->el_line.cursor = toupper(c);
260
261 if (++el->el_line.cursor >= el->el_line.lastchar) {
262 el->el_line.cursor--;
263 re_fastaddc(el);
264 break;
265 }
266 re_fastaddc(el);
267 }
268 return CC_NORM;
269 }
270
271
272 /* vi_change_meta():
273 * Vi change prefix command
274 * [c]
275 */
276 protected el_action_t
277 /*ARGSUSED*/
278 vi_change_meta(EditLine *el, int c __attribute__((__unused__)))
279 {
280
281 /*
282 * Delete with insert == change: first we delete and then we leave in
283 * insert mode.
284 */
285 return (cv_action(el, DELETE | INSERT));
286 }
287
288
289 /* vi_insert_at_bol():
290 * Vi enter insert mode at the beginning of line
291 * [I]
292 */
293 protected el_action_t
294 /*ARGSUSED*/
295 vi_insert_at_bol(EditLine *el, int c __attribute__((__unused__)))
296 {
297
298 el->el_line.cursor = el->el_line.buffer;
299 cv_undo(el);
300 el->el_map.current = el->el_map.key;
301 return (CC_CURSOR);
302 }
303
304
305 /* vi_replace_char():
306 * Vi replace character under the cursor with the next character typed
307 * [r]
308 */
309 protected el_action_t
310 /*ARGSUSED*/
311 vi_replace_char(EditLine *el, int c __attribute__((__unused__)))
312 {
313
314 if (el->el_line.cursor >= el->el_line.lastchar)
315 return CC_ERROR;
316
317 el->el_map.current = el->el_map.key;
318 el->el_state.inputmode = MODE_REPLACE_1;
319 cv_undo(el);
320 return (CC_ARGHACK);
321 }
322
323
324 /* vi_replace_mode():
325 * Vi enter replace mode
326 * [R]
327 */
328 protected el_action_t
329 /*ARGSUSED*/
330 vi_replace_mode(EditLine *el, int c __attribute__((__unused__)))
331 {
332
333 el->el_map.current = el->el_map.key;
334 el->el_state.inputmode = MODE_REPLACE;
335 cv_undo(el);
336 return (CC_NORM);
337 }
338
339
340 /* vi_substitute_char():
341 * Vi replace character under the cursor and enter insert mode
342 * [s]
343 */
344 protected el_action_t
345 /*ARGSUSED*/
346 vi_substitute_char(EditLine *el, int c __attribute__((__unused__)))
347 {
348
349 c_delafter(el, el->el_state.argument);
350 el->el_map.current = el->el_map.key;
351 return (CC_REFRESH);
352 }
353
354
355 /* vi_substitute_line():
356 * Vi substitute entire line
357 * [S]
358 */
359 protected el_action_t
360 /*ARGSUSED*/
361 vi_substitute_line(EditLine *el, int c __attribute__((__unused__)))
362 {
363
364 cv_undo(el);
365 cv_yank(el, el->el_line.buffer,
366 (int)(el->el_line.lastchar - el->el_line.buffer));
367 (void) em_kill_line(el, 0);
368 el->el_map.current = el->el_map.key;
369 return (CC_REFRESH);
370 }
371
372
373 /* vi_change_to_eol():
374 * Vi change to end of line
375 * [C]
376 */
377 protected el_action_t
378 /*ARGSUSED*/
379 vi_change_to_eol(EditLine *el, int c __attribute__((__unused__)))
380 {
381
382 cv_undo(el);
383 cv_yank(el, el->el_line.cursor,
384 (int)(el->el_line.lastchar - el->el_line.cursor));
385 (void) ed_kill_line(el, 0);
386 el->el_map.current = el->el_map.key;
387 return (CC_REFRESH);
388 }
389
390
391 /* vi_insert():
392 * Vi enter insert mode
393 * [i]
394 */
395 protected el_action_t
396 /*ARGSUSED*/
397 vi_insert(EditLine *el, int c __attribute__((__unused__)))
398 {
399
400 el->el_map.current = el->el_map.key;
401 cv_undo(el);
402 return (CC_NORM);
403 }
404
405
406 /* vi_add():
407 * Vi enter insert mode after the cursor
408 * [a]
409 */
410 protected el_action_t
411 /*ARGSUSED*/
412 vi_add(EditLine *el, int c __attribute__((__unused__)))
413 {
414 int ret;
415
416 el->el_map.current = el->el_map.key;
417 if (el->el_line.cursor < el->el_line.lastchar) {
418 el->el_line.cursor++;
419 if (el->el_line.cursor > el->el_line.lastchar)
420 el->el_line.cursor = el->el_line.lastchar;
421 ret = CC_CURSOR;
422 } else
423 ret = CC_NORM;
424
425 cv_undo(el);
426
427 return (ret);
428 }
429
430
431 /* vi_add_at_eol():
432 * Vi enter insert mode at end of line
433 * [A]
434 */
435 protected el_action_t
436 /*ARGSUSED*/
437 vi_add_at_eol(EditLine *el, int c __attribute__((__unused__)))
438 {
439
440 el->el_map.current = el->el_map.key;
441 el->el_line.cursor = el->el_line.lastchar;
442 cv_undo(el);
443 return (CC_CURSOR);
444 }
445
446
447 /* vi_delete_meta():
448 * Vi delete prefix command
449 * [d]
450 */
451 protected el_action_t
452 /*ARGSUSED*/
453 vi_delete_meta(EditLine *el, int c __attribute__((__unused__)))
454 {
455
456 return (cv_action(el, DELETE));
457 }
458
459
460 /* vi_end_big_word():
461 * Vi move to the end of the current space delimited word
462 * [E]
463 */
464 protected el_action_t
465 /*ARGSUSED*/
466 vi_end_big_word(EditLine *el, int c)
467 {
468
469 if (el->el_line.cursor == el->el_line.lastchar)
470 return (CC_ERROR);
471
472 el->el_line.cursor = cv__endword(el->el_line.cursor,
473 el->el_line.lastchar, el->el_state.argument, cv__isWord);
474
475 if (el->el_chared.c_vcmd.action != NOP) {
476 el->el_line.cursor++;
477 cv_delfini(el);
478 return (CC_REFRESH);
479 }
480 return (CC_CURSOR);
481 }
482
483
484 /* vi_end_word():
485 * Vi move to the end of the current word
486 * [e]
487 */
488 protected el_action_t
489 /*ARGSUSED*/
490 vi_end_word(EditLine *el, int c __attribute__((__unused__)))
491 {
492
493 if (el->el_line.cursor == el->el_line.lastchar)
494 return (CC_ERROR);
495
496 el->el_line.cursor = cv__endword(el->el_line.cursor,
497 el->el_line.lastchar, el->el_state.argument, cv__isword);
498
499 if (el->el_chared.c_vcmd.action != NOP) {
500 el->el_line.cursor++;
501 cv_delfini(el);
502 return (CC_REFRESH);
503 }
504 return (CC_CURSOR);
505 }
506
507
508 /* vi_undo():
509 * Vi undo last change
510 * [u]
511 */
512 protected el_action_t
513 /*ARGSUSED*/
514 vi_undo(EditLine *el, int c __attribute__((__unused__)))
515 {
516 c_undo_t un = el->el_chared.c_undo;
517
518 if (un.len == -1)
519 return CC_ERROR;
520
521 /* switch line buffer and undo buffer */
522 el->el_chared.c_undo.buf = el->el_line.buffer;
523 el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
524 el->el_chared.c_undo.cursor =
525 (int)(el->el_line.cursor - el->el_line.buffer);
526 el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
527 el->el_line.buffer = un.buf;
528 el->el_line.cursor = un.buf + un.cursor;
529 el->el_line.lastchar = un.buf + un.len;
530
531 return (CC_REFRESH);
532 }
533
534
535 /* vi_command_mode():
536 * Vi enter command mode (use alternative key bindings)
537 * [<ESC>]
538 */
539 protected el_action_t
540 /*ARGSUSED*/
541 vi_command_mode(EditLine *el, int c __attribute__((__unused__)))
542 {
543
544 /* [Esc] cancels pending action */
545 el->el_chared.c_vcmd.action = NOP;
546 el->el_chared.c_vcmd.pos = 0;
547
548 el->el_state.doingarg = 0;
549
550 el->el_state.inputmode = MODE_INSERT;
551 el->el_map.current = el->el_map.alt;
552 #ifdef VI_MOVE
553 if (el->el_line.cursor > el->el_line.buffer)
554 el->el_line.cursor--;
555 #endif
556 return (CC_CURSOR);
557 }
558
559
560 /* vi_zero():
561 * Vi move to the beginning of line
562 * [0]
563 */
564 protected el_action_t
565 vi_zero(EditLine *el, int c)
566 {
567
568 if (el->el_state.doingarg)
569 return ed_argument_digit(el, c);
570
571 el->el_line.cursor = el->el_line.buffer;
572 if (el->el_chared.c_vcmd.action != NOP) {
573 cv_delfini(el);
574 return (CC_REFRESH);
575 }
576 return (CC_CURSOR);
577 }
578
579
580 /* vi_delete_prev_char():
581 * Vi move to previous character (backspace)
582 * [^H] in insert mode only
583 */
584 protected el_action_t
585 /*ARGSUSED*/
586 vi_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
587 {
588
589 if (el->el_line.cursor <= el->el_line.buffer)
590 return (CC_ERROR);
591
592 c_delbefore1(el);
593 el->el_line.cursor--;
594 return (CC_REFRESH);
595 }
596
597
598 /* vi_list_or_eof():
599 * Vi list choices for completion or indicate end of file if empty line
600 * [^D]
601 */
602 protected el_action_t
603 /*ARGSUSED*/
604 vi_list_or_eof(EditLine *el, int c)
605 {
606
607 if (el->el_line.cursor == el->el_line.lastchar) {
608 if (el->el_line.cursor == el->el_line.buffer) {
609 term_writec(el, c); /* then do a EOF */
610 return (CC_EOF);
611 } else {
612 /*
613 * Here we could list completions, but it is an
614 * error right now
615 */
616 term_beep(el);
617 return (CC_ERROR);
618 }
619 } else {
620 #ifdef notyet
621 re_goto_bottom(el);
622 *el->el_line.lastchar = '\0'; /* just in case */
623 return (CC_LIST_CHOICES);
624 #else
625 /*
626 * Just complain for now.
627 */
628 term_beep(el);
629 return (CC_ERROR);
630 #endif
631 }
632 }
633
634
635 /* vi_kill_line_prev():
636 * Vi cut from beginning of line to cursor
637 * [^U]
638 */
639 protected el_action_t
640 /*ARGSUSED*/
641 vi_kill_line_prev(EditLine *el, int c __attribute__((__unused__)))
642 {
643 char *kp, *cp;
644
645 cp = el->el_line.buffer;
646 kp = el->el_chared.c_kill.buf;
647 while (cp < el->el_line.cursor)
648 *kp++ = *cp++; /* copy it */
649 el->el_chared.c_kill.last = kp;
650 c_delbefore(el, (int)(el->el_line.cursor - el->el_line.buffer));
651 el->el_line.cursor = el->el_line.buffer; /* zap! */
652 return (CC_REFRESH);
653 }
654
655
656 /* vi_search_prev():
657 * Vi search history previous
658 * [?]
659 */
660 protected el_action_t
661 /*ARGSUSED*/
662 vi_search_prev(EditLine *el, int c __attribute__((__unused__)))
663 {
664
665 return (cv_search(el, ED_SEARCH_PREV_HISTORY));
666 }
667
668
669 /* vi_search_next():
670 * Vi search history next
671 * [/]
672 */
673 protected el_action_t
674 /*ARGSUSED*/
675 vi_search_next(EditLine *el, int c __attribute__((__unused__)))
676 {
677
678 return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
679 }
680
681
682 /* vi_repeat_search_next():
683 * Vi repeat current search in the same search direction
684 * [n]
685 */
686 protected el_action_t
687 /*ARGSUSED*/
688 vi_repeat_search_next(EditLine *el, int c __attribute__((__unused__)))
689 {
690
691 if (el->el_search.patlen == 0)
692 return (CC_ERROR);
693 else
694 return (cv_repeat_srch(el, el->el_search.patdir));
695 }
696
697
698 /* vi_repeat_search_prev():
699 * Vi repeat current search in the opposite search direction
700 * [N]
701 */
702 /*ARGSUSED*/
703 protected el_action_t
704 vi_repeat_search_prev(EditLine *el, int c __attribute__((__unused__)))
705 {
706
707 if (el->el_search.patlen == 0)
708 return (CC_ERROR);
709 else
710 return (cv_repeat_srch(el,
711 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
712 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
713 }
714
715
716 /* vi_next_char():
717 * Vi move to the character specified next
718 * [f]
719 */
720 protected el_action_t
721 /*ARGSUSED*/
722 vi_next_char(EditLine *el, int c __attribute__((__unused__)))
723 {
724 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 0);
725 }
726
727
728 /* vi_prev_char():
729 * Vi move to the character specified previous
730 * [F]
731 */
732 protected el_action_t
733 /*ARGSUSED*/
734 vi_prev_char(EditLine *el, int c __attribute__((__unused__)))
735 {
736 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 0);
737 }
738
739
740 /* vi_to_next_char():
741 * Vi move up to the character specified next
742 * [t]
743 */
744 protected el_action_t
745 /*ARGSUSED*/
746 vi_to_next_char(EditLine *el, int c __attribute__((__unused__)))
747 {
748 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 1);
749 }
750
751
752 /* vi_to_prev_char():
753 * Vi move up to the character specified previous
754 * [T]
755 */
756 protected el_action_t
757 /*ARGSUSED*/
758 vi_to_prev_char(EditLine *el, int c __attribute__((__unused__)))
759 {
760 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 1);
761 }
762
763
764 /* vi_repeat_next_char():
765 * Vi repeat current character search in the same search direction
766 * [;]
767 */
768 protected el_action_t
769 /*ARGSUSED*/
770 vi_repeat_next_char(EditLine *el, int c __attribute__((__unused__)))
771 {
772
773 return cv_csearch(el, el->el_search.chadir, el->el_search.chacha,
774 el->el_state.argument, el->el_search.chatflg);
775 }
776
777
778 /* vi_repeat_prev_char():
779 * Vi repeat current character search in the opposite search direction
780 * [,]
781 */
782 protected el_action_t
783 /*ARGSUSED*/
784 vi_repeat_prev_char(EditLine *el, int c __attribute__((__unused__)))
785 {
786 el_action_t r;
787 int dir = el->el_search.chadir;
788
789 r = cv_csearch(el, -dir, el->el_search.chacha,
790 el->el_state.argument, el->el_search.chatflg);
791 el->el_search.chadir = dir;
792 return r;
793 }
794
795
796 /* vi_match():
797 * Vi go to matching () {} or []
798 * [%]
799 */
800 protected el_action_t
801 /*ARGSUSED*/
802 vi_match(EditLine *el, int c)
803 {
804 const char match_chars[] = "()[]{}";
805 char *cp;
806 size_t delta, i, count;
807 char o_ch, c_ch;
808
809 *el->el_line.lastchar = '\0'; /* just in case */
810
811 i = strcspn(el->el_line.cursor, match_chars);
812 o_ch = el->el_line.cursor[i];
813 if (o_ch == 0)
814 return CC_ERROR;
815 delta = strchr(match_chars, o_ch) - match_chars;
816 c_ch = match_chars[delta ^ 1];
817 count = 1;
818 delta = 1 - (delta & 1) * 2;
819
820 for (cp = &el->el_line.cursor[i]; count; ) {
821 cp += delta;
822 if (cp < el->el_line.buffer || cp >= el->el_line.lastchar)
823 return CC_ERROR;
824 if (*cp == o_ch)
825 count++;
826 else if (*cp == c_ch)
827 count--;
828 }
829
830 el->el_line.cursor = cp;
831
832 if (el->el_chared.c_vcmd.action != NOP) {
833 /* NB posix says char under cursor should NOT be deleted
834 for -ve delta - this is different to netbsd vi. */
835 if (delta > 0)
836 el->el_line.cursor++;
837 cv_delfini(el);
838 return (CC_REFRESH);
839 }
840 return (CC_CURSOR);
841 }
842
843 /* vi_undo_line():
844 * Vi undo all changes to line
845 * [U]
846 */
847 protected el_action_t
848 /*ARGSUSED*/
849 vi_undo_line(EditLine *el, int c)
850 {
851
852 cv_undo(el);
853 return hist_get(el);
854 }
855
856 /* vi_to_column():
857 * Vi go to specified column
858 * [|]
859 * NB netbsd vi goes to screen column 'n', posix says nth character
860 */
861 protected el_action_t
862 /*ARGSUSED*/
863 vi_to_column(EditLine *el, int c)
864 {
865
866 el->el_line.cursor = el->el_line.buffer;
867 el->el_state.argument--;
868 return ed_next_char(el, 0);
869 }
870
871 /* vi_yank_end():
872 * Vi yank to end of line
873 * [Y]
874 */
875 protected el_action_t
876 /*ARGSUSED*/
877 vi_yank_end(EditLine *el, int c)
878 {
879
880 cv_yank(el, el->el_line.cursor,
881 (int)(el->el_line.lastchar - el->el_line.cursor));
882 return CC_REFRESH;
883 }
884
885 /* vi_yank():
886 * Vi yank
887 * [y]
888 */
889 protected el_action_t
890 /*ARGSUSED*/
891 vi_yank(EditLine *el, int c)
892 {
893
894 return cv_action(el, YANK);
895 }
896
897 /* vi_comment_out():
898 * Vi comment out current command
899 * [#]
900 */
901 protected el_action_t
902 /*ARGSUSED*/
903 vi_comment_out(EditLine *el, int c)
904 {
905
906 el->el_line.cursor = el->el_line.buffer;
907 c_insert(el, 1);
908 *el->el_line.cursor = '#';
909 re_refresh(el);
910 return ed_newline(el, 0);
911 }
912
913 /* vi_alias():
914 * Vi include shell alias
915 * [@]
916 * NB: posix implies that we should enter insert mode, however
917 * this is against historical precedent...
918 */
919 #ifdef __weak_reference
920 extern char *get_alias_text(const char *) __weak_reference(get_alias_text);
921 #endif
922 protected el_action_t
923 /*ARGSUSED*/
924 vi_alias(EditLine *el, int c)
925 {
926 #ifdef __weak_reference
927 char alias_name[3];
928 char *alias_text;
929
930 if (get_alias_text == 0) {
931 return CC_ERROR;
932 }
933
934 alias_name[0] = '_';
935 alias_name[2] = 0;
936 if (el_getc(el, &alias_name[1]) != 1)
937 return CC_ERROR;
938
939 alias_text = get_alias_text(alias_name);
940 if (alias_text != NULL)
941 el_push(el, alias_text);
942 return CC_NORM;
943 #else
944 return CC_ERROR;
945 #endif
946 }
947
948 /* vi_to_history_line():
949 * Vi go to specified history file line.
950 * [G]
951 */
952 protected el_action_t
953 /*ARGSUSED*/
954 vi_to_history_line(EditLine *el, int c)
955 {
956 int sv_event_no = el->el_history.eventno;
957 el_action_t rval;
958
959
960 if (el->el_history.eventno == 0) {
961 (void) strncpy(el->el_history.buf, el->el_line.buffer,
962 EL_BUFSIZ);
963 el->el_history.last = el->el_history.buf +
964 (el->el_line.lastchar - el->el_line.buffer);
965 }
966
967 /* Lack of a 'count' means oldest, not 1 */
968 if (!el->el_state.doingarg) {
969 el->el_history.eventno = 0x7fffffff;
970 hist_get(el);
971 } else {
972 /* This is brain dead, all the rest of this code counts
973 * upwards going into the past. Here we need count in the
974 * other direction (to match the output of fc -l).
975 * I could change the world, but this seems to suffice.
976 */
977 el->el_history.eventno = 1;
978 if (hist_get(el) == CC_ERROR)
979 return CC_ERROR;
980 el->el_history.eventno = 1 + el->el_history.ev.num
981 - el->el_state.argument;
982 if (el->el_history.eventno < 0) {
983 el->el_history.eventno = sv_event_no;
984 return CC_ERROR;
985 }
986 }
987 rval = hist_get(el);
988 if (rval == CC_ERROR)
989 el->el_history.eventno = sv_event_no;
990 return rval;
991 }
992
993 /* vi_histedit():
994 * Vi edit history line with vi
995 * [v]
996 */
997 protected el_action_t
998 /*ARGSUSED*/
999 vi_histedit(EditLine *el, int c)
1000 {
1001 int fd;
1002 pid_t pid;
1003 ssize_t st;
1004 int status;
1005 char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
1006 char *cp;
1007
1008 if (el->el_state.doingarg) {
1009 if (vi_to_history_line(el, 0) == CC_ERROR)
1010 return CC_ERROR;
1011 }
1012
1013 fd = mkstemp(tempfile);
1014 if (fd < 0)
1015 return CC_ERROR;
1016 cp = el->el_line.buffer;
1017 write(fd, cp, (size_t)(el->el_line.lastchar - cp));
1018 write(fd, "\n", 1);
1019 pid = fork();
1020 switch (pid) {
1021 case -1:
1022 close(fd);
1023 unlink(tempfile);
1024 return CC_ERROR;
1025 case 0:
1026 close(fd);
1027 execlp("vi", "vi", tempfile, (char *)NULL);
1028 exit(0);
1029 /*NOTREACHED*/
1030 default:
1031 while (waitpid(pid, &status, 0) != pid)
1032 continue;
1033 lseek(fd, (off_t)0, SEEK_SET);
1034 st = read(fd, cp, (size_t)(el->el_line.limit - cp));
1035 if (st > 0 && cp[st - 1] == '\n')
1036 st--;
1037 el->el_line.cursor = cp;
1038 el->el_line.lastchar = cp + st;
1039 break;
1040 }
1041
1042 close(fd);
1043 unlink(tempfile);
1044 /* return CC_REFRESH; */
1045 return ed_newline(el, 0);
1046 }
1047
1048 /* vi_history_word():
1049 * Vi append word from previous input line
1050 * [_]
1051 * Who knows where this one came from!
1052 * '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
1053 */
1054 protected el_action_t
1055 /*ARGSUSED*/
1056 vi_history_word(EditLine *el, int c)
1057 {
1058 const char *wp = HIST_FIRST(el);
1059 const char *wep, *wsp;
1060 int len;
1061 char *cp;
1062 const char *lim;
1063
1064 if (wp == NULL)
1065 return CC_ERROR;
1066
1067 wep = wsp = 0;
1068 do {
1069 while (isspace((unsigned char)*wp))
1070 wp++;
1071 if (*wp == 0)
1072 break;
1073 wsp = wp;
1074 while (*wp && !isspace((unsigned char)*wp))
1075 wp++;
1076 wep = wp;
1077 } while ((!el->el_state.doingarg || --el->el_state.argument > 0) && *wp != 0);
1078
1079 if (wsp == 0 || (el->el_state.doingarg && el->el_state.argument != 0))
1080 return CC_ERROR;
1081
1082 cv_undo(el);
1083 len = (int)(wep - wsp);
1084 if (el->el_line.cursor < el->el_line.lastchar)
1085 el->el_line.cursor++;
1086 c_insert(el, len + 1);
1087 cp = el->el_line.cursor;
1088 lim = el->el_line.limit;
1089 if (cp < lim)
1090 *cp++ = ' ';
1091 while (wsp < wep && cp < lim)
1092 *cp++ = *wsp++;
1093 el->el_line.cursor = cp;
1094
1095 el->el_map.current = el->el_map.key;
1096 return CC_REFRESH;
1097 }
1098
1099 /* vi_redo():
1100 * Vi redo last non-motion command
1101 * [.]
1102 */
1103 protected el_action_t
1104 /*ARGSUSED*/
1105 vi_redo(EditLine *el, int c)
1106 {
1107 c_redo_t *r = &el->el_chared.c_redo;
1108
1109 if (!el->el_state.doingarg && r->count) {
1110 el->el_state.doingarg = 1;
1111 el->el_state.argument = r->count;
1112 }
1113
1114 el->el_chared.c_vcmd.pos = el->el_line.cursor;
1115 el->el_chared.c_vcmd.action = r->action;
1116 if (r->pos != r->buf) {
1117 if (r->pos + 1 > r->lim)
1118 /* sanity */
1119 r->pos = r->lim - 1;
1120 r->pos[0] = 0;
1121 el_push(el, r->buf);
1122 }
1123
1124 el->el_state.thiscmd = r->cmd;
1125 el->el_state.thisch = r->ch;
1126 return (*el->el_map.func[r->cmd])(el, r->ch);
1127 }
1128