new libedit: Silence -Wold-style-definition
[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.27 2006/10/22 07:48:13 mrg Exp $$
34  * $DragonFly: src/lib/libedit/vi.c,v 1.7 2007/05/05 00:27:40 pavalos 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                 if (!(c & YANK)) {
69                         el->el_line.lastchar = el->el_line.buffer;
70                         el->el_line.cursor = el->el_line.buffer;
71                 }
72                 if (c & INSERT)
73                         el->el_map.current = el->el_map.key;
74
75                 return (CC_REFRESH);
76         }
77         el->el_chared.c_vcmd.pos = el->el_line.cursor;
78         el->el_chared.c_vcmd.action = c;
79         return (CC_ARGHACK);
80 }
81
82 /* cv_paste():
83  *      Paste previous deletion before or after the cursor
84  */
85 private el_action_t
86 cv_paste(EditLine *el, int c)
87 {
88         c_kill_t *k = &el->el_chared.c_kill;
89         int len = k->last - k->buf;
90
91         if (k->buf == NULL || len == 0)
92                 return (CC_ERROR);
93 #ifdef DEBUG_PASTE
94         (void) fprintf(el->el_errfile, "Paste: \"%.*s\"\n", len, k->buf);
95 #endif
96
97         cv_undo(el);
98
99         if (!c && el->el_line.cursor < el->el_line.lastchar)
100                 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(el->el_line.cursor, k->buf, len +0u);
106
107         return (CC_REFRESH);
108 }
109
110
111 /* vi_paste_next():
112  *      Vi paste previous deletion to the right of the cursor
113  *      [p]
114  */
115 protected el_action_t
116 /*ARGSUSED*/
117 vi_paste_next(EditLine *el, int c __attribute__((__unused__)))
118 {
119
120         return (cv_paste(el, 0));
121 }
122
123
124 /* vi_paste_prev():
125  *      Vi paste previous deletion to the left of the cursor
126  *      [P]
127  */
128 protected el_action_t
129 /*ARGSUSED*/
130 vi_paste_prev(EditLine *el, int c __attribute__((__unused__)))
131 {
132
133         return (cv_paste(el, 1));
134 }
135
136
137 /* vi_prev_big_word():
138  *      Vi move to the previous space delimited word
139  *      [B]
140  */
141 protected el_action_t
142 /*ARGSUSED*/
143 vi_prev_big_word(EditLine *el, int c)
144 {
145
146         if (el->el_line.cursor == el->el_line.buffer)
147                 return (CC_ERROR);
148
149         el->el_line.cursor = cv_prev_word(el->el_line.cursor,
150             el->el_line.buffer,
151             el->el_state.argument,
152             cv__isWord);
153
154         if (el->el_chared.c_vcmd.action != NOP) {
155                 cv_delfini(el);
156                 return (CC_REFRESH);
157         }
158         return (CC_CURSOR);
159 }
160
161
162 /* vi_prev_word():
163  *      Vi move to the previous word
164  *      [b]
165  */
166 protected el_action_t
167 /*ARGSUSED*/
168 vi_prev_word(EditLine *el, int c __attribute__((__unused__)))
169 {
170
171         if (el->el_line.cursor == el->el_line.buffer)
172                 return (CC_ERROR);
173
174         el->el_line.cursor = cv_prev_word(el->el_line.cursor,
175             el->el_line.buffer,
176             el->el_state.argument,
177             cv__isword);
178
179         if (el->el_chared.c_vcmd.action != NOP) {
180                 cv_delfini(el);
181                 return (CC_REFRESH);
182         }
183         return (CC_CURSOR);
184 }
185
186
187 /* vi_next_big_word():
188  *      Vi move to the next space delimited word
189  *      [W]
190  */
191 protected el_action_t
192 /*ARGSUSED*/
193 vi_next_big_word(EditLine *el, int c)
194 {
195
196         if (el->el_line.cursor >= el->el_line.lastchar - 1)
197                 return (CC_ERROR);
198
199         el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
200             el->el_line.lastchar, el->el_state.argument, cv__isWord);
201
202         if (el->el_map.type == MAP_VI)
203                 if (el->el_chared.c_vcmd.action != NOP) {
204                         cv_delfini(el);
205                         return (CC_REFRESH);
206                 }
207         return (CC_CURSOR);
208 }
209
210
211 /* vi_next_word():
212  *      Vi move to the next word
213  *      [w]
214  */
215 protected el_action_t
216 /*ARGSUSED*/
217 vi_next_word(EditLine *el, int c __attribute__((__unused__)))
218 {
219
220         if (el->el_line.cursor >= el->el_line.lastchar - 1)
221                 return (CC_ERROR);
222
223         el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
224             el->el_line.lastchar, el->el_state.argument, cv__isword);
225
226         if (el->el_map.type == MAP_VI)
227                 if (el->el_chared.c_vcmd.action != NOP) {
228                         cv_delfini(el);
229                         return (CC_REFRESH);
230                 }
231         return (CC_CURSOR);
232 }
233
234
235 /* vi_change_case():
236  *      Vi change case of character under the cursor and advance one character
237  *      [~]
238  */
239 protected el_action_t
240 vi_change_case(EditLine *el, int c)
241 {
242         int i;
243
244         if (el->el_line.cursor >= el->el_line.lastchar)
245                 return (CC_ERROR);
246         cv_undo(el);
247         for (i = 0; i < el->el_state.argument; i++) {
248
249                 c = *(unsigned char *)el->el_line.cursor;
250                 if (isupper(c))
251                         *el->el_line.cursor = tolower(c);
252                 else if (islower(c))
253                         *el->el_line.cursor = toupper(c);
254
255                 if (++el->el_line.cursor >= el->el_line.lastchar) {
256                         el->el_line.cursor--;
257                         re_fastaddc(el);
258                         break;
259                 }
260                 re_fastaddc(el);
261         }
262         return CC_NORM;
263 }
264
265
266 /* vi_change_meta():
267  *      Vi change prefix command
268  *      [c]
269  */
270 protected el_action_t
271 /*ARGSUSED*/
272 vi_change_meta(EditLine *el, int c __attribute__((__unused__)))
273 {
274
275         /*
276          * Delete with insert == change: first we delete and then we leave in
277          * insert mode.
278          */
279         return (cv_action(el, DELETE | INSERT));
280 }
281
282
283 /* vi_insert_at_bol():
284  *      Vi enter insert mode at the beginning of line
285  *      [I]
286  */
287 protected el_action_t
288 /*ARGSUSED*/
289 vi_insert_at_bol(EditLine *el, int c __attribute__((__unused__)))
290 {
291
292         el->el_line.cursor = el->el_line.buffer;
293         cv_undo(el);
294         el->el_map.current = el->el_map.key;
295         return (CC_CURSOR);
296 }
297
298
299 /* vi_replace_char():
300  *      Vi replace character under the cursor with the next character typed
301  *      [r]
302  */
303 protected el_action_t
304 /*ARGSUSED*/
305 vi_replace_char(EditLine *el, int c __attribute__((__unused__)))
306 {
307
308         if (el->el_line.cursor >= el->el_line.lastchar)
309                 return CC_ERROR;
310
311         el->el_map.current = el->el_map.key;
312         el->el_state.inputmode = MODE_REPLACE_1;
313         cv_undo(el);
314         return (CC_ARGHACK);
315 }
316
317
318 /* vi_replace_mode():
319  *      Vi enter replace mode
320  *      [R]
321  */
322 protected el_action_t
323 /*ARGSUSED*/
324 vi_replace_mode(EditLine *el, int c __attribute__((__unused__)))
325 {
326
327         el->el_map.current = el->el_map.key;
328         el->el_state.inputmode = MODE_REPLACE;
329         cv_undo(el);
330         return (CC_NORM);
331 }
332
333
334 /* vi_substitute_char():
335  *      Vi replace character under the cursor and enter insert mode
336  *      [s]
337  */
338 protected el_action_t
339 /*ARGSUSED*/
340 vi_substitute_char(EditLine *el, int c __attribute__((__unused__)))
341 {
342
343         c_delafter(el, el->el_state.argument);
344         el->el_map.current = el->el_map.key;
345         return (CC_REFRESH);
346 }
347
348
349 /* vi_substitute_line():
350  *      Vi substitute entire line
351  *      [S]
352  */
353 protected el_action_t
354 /*ARGSUSED*/
355 vi_substitute_line(EditLine *el, int c __attribute__((__unused__)))
356 {
357
358         cv_undo(el);
359         cv_yank(el, el->el_line.buffer,
360                     el->el_line.lastchar - el->el_line.buffer);
361         (void) em_kill_line(el, 0);
362         el->el_map.current = el->el_map.key;
363         return (CC_REFRESH);
364 }
365
366
367 /* vi_change_to_eol():
368  *      Vi change to end of line
369  *      [C]
370  */
371 protected el_action_t
372 /*ARGSUSED*/
373 vi_change_to_eol(EditLine *el, int c __attribute__((__unused__)))
374 {
375
376         cv_undo(el);
377         cv_yank(el, el->el_line.cursor,
378                     el->el_line.lastchar - el->el_line.cursor);
379         (void) ed_kill_line(el, 0);
380         el->el_map.current = el->el_map.key;
381         return (CC_REFRESH);
382 }
383
384
385 /* vi_insert():
386  *      Vi enter insert mode
387  *      [i]
388  */
389 protected el_action_t
390 /*ARGSUSED*/
391 vi_insert(EditLine *el, int c __attribute__((__unused__)))
392 {
393
394         el->el_map.current = el->el_map.key;
395         cv_undo(el);
396         return (CC_NORM);
397 }
398
399
400 /* vi_add():
401  *      Vi enter insert mode after the cursor
402  *      [a]
403  */
404 protected el_action_t
405 /*ARGSUSED*/
406 vi_add(EditLine *el, int c __attribute__((__unused__)))
407 {
408         int ret;
409
410         el->el_map.current = el->el_map.key;
411         if (el->el_line.cursor < el->el_line.lastchar) {
412                 el->el_line.cursor++;
413                 if (el->el_line.cursor > el->el_line.lastchar)
414                         el->el_line.cursor = el->el_line.lastchar;
415                 ret = CC_CURSOR;
416         } else
417                 ret = CC_NORM;
418
419         cv_undo(el);
420
421         return (ret);
422 }
423
424
425 /* vi_add_at_eol():
426  *      Vi enter insert mode at end of line
427  *      [A]
428  */
429 protected el_action_t
430 /*ARGSUSED*/
431 vi_add_at_eol(EditLine *el, int c __attribute__((__unused__)))
432 {
433
434         el->el_map.current = el->el_map.key;
435         el->el_line.cursor = el->el_line.lastchar;
436         cv_undo(el);
437         return (CC_CURSOR);
438 }
439
440
441 /* vi_delete_meta():
442  *      Vi delete prefix command
443  *      [d]
444  */
445 protected el_action_t
446 /*ARGSUSED*/
447 vi_delete_meta(EditLine *el, int c __attribute__((__unused__)))
448 {
449
450         return (cv_action(el, DELETE));
451 }
452
453
454 /* vi_end_big_word():
455  *      Vi move to the end of the current space delimited word
456  *      [E]
457  */
458 protected el_action_t
459 /*ARGSUSED*/
460 vi_end_big_word(EditLine *el, int c)
461 {
462
463         if (el->el_line.cursor == el->el_line.lastchar)
464                 return (CC_ERROR);
465
466         el->el_line.cursor = cv__endword(el->el_line.cursor,
467             el->el_line.lastchar, el->el_state.argument, cv__isWord);
468
469         if (el->el_chared.c_vcmd.action != NOP) {
470                 el->el_line.cursor++;
471                 cv_delfini(el);
472                 return (CC_REFRESH);
473         }
474         return (CC_CURSOR);
475 }
476
477
478 /* vi_end_word():
479  *      Vi move to the end of the current word
480  *      [e]
481  */
482 protected el_action_t
483 /*ARGSUSED*/
484 vi_end_word(EditLine *el, int c __attribute__((__unused__)))
485 {
486
487         if (el->el_line.cursor == el->el_line.lastchar)
488                 return (CC_ERROR);
489
490         el->el_line.cursor = cv__endword(el->el_line.cursor,
491             el->el_line.lastchar, el->el_state.argument, cv__isword);
492
493         if (el->el_chared.c_vcmd.action != NOP) {
494                 el->el_line.cursor++;
495                 cv_delfini(el);
496                 return (CC_REFRESH);
497         }
498         return (CC_CURSOR);
499 }
500
501
502 /* vi_undo():
503  *      Vi undo last change
504  *      [u]
505  */
506 protected el_action_t
507 /*ARGSUSED*/
508 vi_undo(EditLine *el, int c __attribute__((__unused__)))
509 {
510         c_undo_t un = el->el_chared.c_undo;
511
512         if (un.len == -1)
513                 return CC_ERROR;
514
515         /* switch line buffer and undo buffer */
516         el->el_chared.c_undo.buf = el->el_line.buffer;
517         el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
518         el->el_chared.c_undo.cursor = el->el_line.cursor - el->el_line.buffer;
519         el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
520         el->el_line.buffer = un.buf;
521         el->el_line.cursor = un.buf + un.cursor;
522         el->el_line.lastchar = un.buf + un.len;
523
524         return (CC_REFRESH);
525 }
526
527
528 /* vi_command_mode():
529  *      Vi enter command mode (use alternative key bindings)
530  *      [<ESC>]
531  */
532 protected el_action_t
533 /*ARGSUSED*/
534 vi_command_mode(EditLine *el, int c __attribute__((__unused__)))
535 {
536
537         /* [Esc] cancels pending action */
538         el->el_chared.c_vcmd.action = NOP;
539         el->el_chared.c_vcmd.pos = 0;
540
541         el->el_state.doingarg = 0;
542
543         el->el_state.inputmode = MODE_INSERT;
544         el->el_map.current = el->el_map.alt;
545 #ifdef VI_MOVE
546         if (el->el_line.cursor > el->el_line.buffer)
547                 el->el_line.cursor--;
548 #endif
549         return (CC_CURSOR);
550 }
551
552
553 /* vi_zero():
554  *      Vi move to the beginning of line
555  *      [0]
556  */
557 protected el_action_t
558 vi_zero(EditLine *el, int c)
559 {
560
561         if (el->el_state.doingarg)
562                 return ed_argument_digit(el, c);
563
564         el->el_line.cursor = el->el_line.buffer;
565         if (el->el_chared.c_vcmd.action != NOP) {
566                 cv_delfini(el);
567                 return (CC_REFRESH);
568         }
569         return (CC_CURSOR);
570 }
571
572
573 /* vi_delete_prev_char():
574  *      Vi move to previous character (backspace)
575  *      [^H] in insert mode only
576  */
577 protected el_action_t
578 /*ARGSUSED*/
579 vi_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
580 {
581
582         if (el->el_line.cursor <= el->el_line.buffer)
583                 return (CC_ERROR);
584
585         c_delbefore1(el);
586         el->el_line.cursor--;
587         return (CC_REFRESH);
588 }
589
590
591 /* vi_list_or_eof():
592  *      Vi list choices for completion or indicate end of file if empty line
593  *      [^D]
594  */
595 protected el_action_t
596 /*ARGSUSED*/
597 vi_list_or_eof(EditLine *el, int c)
598 {
599
600         if (el->el_line.cursor == el->el_line.lastchar) {
601                 if (el->el_line.cursor == el->el_line.buffer) {
602                         term_writec(el, c);     /* then do a EOF */
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  *      [#]
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 implies that we should enter insert mode, however
910  * this is against historical precedent...
911  */
912 #ifdef __weak_extern
913 extern char *get_alias_text(const char *) __weak_extern(get_alias_text);
914 #endif
915 protected el_action_t
916 /*ARGSUSED*/
917 vi_alias(EditLine *el, int c)
918 {
919 #ifdef __weak_extern
920         char alias_name[3];
921         char *alias_text;
922
923         if (get_alias_text == 0) {
924                 return CC_ERROR;
925         }
926
927         alias_name[0] = '_';
928         alias_name[2] = 0;
929         if (el_getc(el, &alias_name[1]) != 1)
930                 return CC_ERROR;
931
932         alias_text = get_alias_text(alias_name);
933         if (alias_text != NULL)
934                 el_push(el, alias_text);
935         return CC_NORM;
936 #else
937         return CC_ERROR;
938 #endif
939 }
940
941 /* vi_to_history_line():
942  *      Vi go to specified history file line.
943  *      [G]
944  */
945 protected el_action_t
946 /*ARGSUSED*/
947 vi_to_history_line(EditLine *el, int c)
948 {
949         int sv_event_no = el->el_history.eventno;
950         el_action_t rval;
951
952
953         if (el->el_history.eventno == 0) {
954                  (void) strncpy(el->el_history.buf, el->el_line.buffer,
955                      EL_BUFSIZ);
956                  el->el_history.last = el->el_history.buf +
957                          (el->el_line.lastchar - el->el_line.buffer);
958         }
959
960         /* Lack of a 'count' means oldest, not 1 */
961         if (!el->el_state.doingarg) {
962                 el->el_history.eventno = 0x7fffffff;
963                 hist_get(el);
964         } else {
965                 /* This is brain dead, all the rest of this code counts
966                  * upwards going into the past.  Here we need count in the
967                  * other direction (to match the output of fc -l).
968                  * I could change the world, but this seems to suffice.
969                  */
970                 el->el_history.eventno = 1;
971                 if (hist_get(el) == CC_ERROR)
972                         return CC_ERROR;
973                 el->el_history.eventno = 1 + el->el_history.ev.num 
974                                         - el->el_state.argument;
975                 if (el->el_history.eventno < 0) {
976                         el->el_history.eventno = sv_event_no;
977                         return CC_ERROR;
978                 }
979         }
980         rval = hist_get(el);
981         if (rval == CC_ERROR)
982                 el->el_history.eventno = sv_event_no;
983         return rval;
984 }
985
986 /* vi_histedit():
987  *      Vi edit history line with vi
988  *      [v]
989  */
990 protected el_action_t
991 /*ARGSUSED*/
992 vi_histedit(EditLine *el, int c)
993 {
994         int fd;
995         pid_t pid;
996         int st;
997         char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
998         char *cp;
999
1000         if (el->el_state.doingarg) {
1001                 if (vi_to_history_line(el, 0) == CC_ERROR)
1002                         return CC_ERROR;
1003         }
1004
1005         fd = mkstemp(tempfile);
1006         if (fd < 0)
1007                 return CC_ERROR;
1008         cp = el->el_line.buffer;
1009         write(fd, cp, el->el_line.lastchar - cp +0u);
1010         write(fd, "\n", 1);
1011         pid = fork();
1012         switch (pid) {
1013         case -1:
1014                 close(fd);
1015                 unlink(tempfile);
1016                 return CC_ERROR;
1017         case 0:
1018                 close(fd);
1019                 execlp("vi", "vi", tempfile, NULL);
1020                 exit(0);
1021                 /*NOTREACHED*/
1022         default:
1023                 while (waitpid(pid, &st, 0) != pid)
1024                         continue;
1025                 lseek(fd, 0ll, SEEK_SET);
1026                 st = read(fd, cp, el->el_line.limit - cp +0u);
1027                 if (st > 0 && cp[st - 1] == '\n')
1028                         st--;
1029                 el->el_line.cursor = cp;
1030                 el->el_line.lastchar = cp + st;
1031                 break;
1032         }
1033
1034         close(fd);
1035         unlink(tempfile);
1036         /* return CC_REFRESH; */
1037         return ed_newline(el, 0);
1038 }
1039
1040 /* vi_history_word():
1041  *      Vi append word from previous input line
1042  *      [_]
1043  * Who knows where this one came from!
1044  * '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
1045  */
1046 protected el_action_t
1047 /*ARGSUSED*/
1048 vi_history_word(EditLine *el, int c)
1049 {
1050         const char *wp = HIST_FIRST(el);
1051         const char *wep, *wsp;
1052         int len;
1053         char *cp;
1054         const char *lim;
1055
1056         if (wp == NULL)
1057                 return CC_ERROR;
1058
1059         wep = wsp = 0;
1060         do {
1061                 while (isspace((unsigned char)*wp))
1062                         wp++;
1063                 if (*wp == 0)
1064                         break;
1065                 wsp = wp;
1066                 while (*wp && !isspace((unsigned char)*wp))
1067                         wp++;
1068                 wep = wp;
1069         } while ((!el->el_state.doingarg || --el->el_state.argument > 0) && *wp != 0);
1070
1071         if (wsp == 0 || (el->el_state.doingarg && el->el_state.argument != 0))
1072                 return CC_ERROR;
1073
1074         cv_undo(el);
1075         len = wep - wsp;
1076         if (el->el_line.cursor < el->el_line.lastchar)
1077                 el->el_line.cursor++;
1078         c_insert(el, len + 1);
1079         cp = el->el_line.cursor;
1080         lim = el->el_line.limit;
1081         if (cp < lim)
1082                 *cp++ = ' ';
1083         while (wsp < wep && cp < lim)
1084                 *cp++ = *wsp++;
1085         el->el_line.cursor = cp;
1086
1087         el->el_map.current = el->el_map.key;
1088         return CC_REFRESH;
1089 }
1090
1091 /* vi_redo():
1092  *      Vi redo last non-motion command
1093  *      [.]
1094  */
1095 protected el_action_t
1096 /*ARGSUSED*/
1097 vi_redo(EditLine *el, int c)
1098 {
1099         c_redo_t *r = &el->el_chared.c_redo;
1100
1101         if (!el->el_state.doingarg && r->count) {
1102                 el->el_state.doingarg = 1;
1103                 el->el_state.argument = r->count;
1104         }
1105
1106         el->el_chared.c_vcmd.pos = el->el_line.cursor;
1107         el->el_chared.c_vcmd.action = r->action;
1108         if (r->pos != r->buf) {
1109                 if (r->pos + 1 > r->lim)
1110                         /* sanity */
1111                         r->pos = r->lim - 1;
1112                 r->pos[0] = 0;
1113                 el_push(el, r->buf);
1114         }
1115
1116         el->el_state.thiscmd = r->cmd;
1117         el->el_state.thisch = r->ch;
1118         return  (*el->el_map.func[r->cmd])(el, r->ch);
1119 }