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