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