2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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 $
43 * vi.c: Vi mode commands.
47 private el_action_t cv_action(EditLine *, int);
48 private el_action_t cv_paste(EditLine *, int);
54 cv_action(EditLine *el, int c)
57 if (el->el_chared.c_vcmd.action != NOP) {
58 /* 'cc', 'dd' and (possibly) friends */
59 if (c != el->el_chared.c_vcmd.action)
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;
71 el->el_map.current = el->el_map.key;
75 el->el_chared.c_vcmd.pos = el->el_line.cursor;
76 el->el_chared.c_vcmd.action = c;
81 * Paste previous deletion before or after the cursor
84 cv_paste(EditLine *el, int c)
87 c_kill_t *k = &el->el_chared.c_kill;
88 int len = k->last - k->buf;
90 if (k->buf == NULL || len == 0)
93 (void) fprintf(el->el_errfile, "Paste: \"%.*s\"\n", len, k->buf);
98 if (!c && el->el_line.cursor < el->el_line.lastchar)
100 ptr = el->el_line.cursor;
103 if (el->el_line.cursor + len > el->el_line.lastchar)
105 (void) memcpy(ptr, k->buf, len +0u);
111 * Vi paste previous deletion to the right of the cursor
114 protected el_action_t
116 vi_paste_next(EditLine *el, int c __attribute__((__unused__)))
119 return (cv_paste(el, 0));
124 * Vi paste previous deletion to the left of the cursor
127 protected el_action_t
129 vi_paste_prev(EditLine *el, int c __attribute__((__unused__)))
132 return (cv_paste(el, 1));
136 /* vi_prev_big_word():
137 * Vi move to the previous space delimited word
140 protected el_action_t
142 vi_prev_big_word(EditLine *el, int c)
145 if (el->el_line.cursor == el->el_line.buffer)
148 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
150 el->el_state.argument,
153 if (el->el_chared.c_vcmd.action != NOP) {
162 * Vi move to the previous word
165 protected el_action_t
167 vi_prev_word(EditLine *el, int c __attribute__((__unused__)))
170 if (el->el_line.cursor == el->el_line.buffer)
173 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
175 el->el_state.argument,
178 if (el->el_chared.c_vcmd.action != NOP) {
186 /* vi_next_big_word():
187 * Vi move to the next space delimited word
190 protected el_action_t
192 vi_next_big_word(EditLine *el, int c)
195 if (el->el_line.cursor >= el->el_line.lastchar - 1)
198 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
199 el->el_line.lastchar, el->el_state.argument, cv__isWord);
201 if (el->el_map.type == MAP_VI)
202 if (el->el_chared.c_vcmd.action != NOP) {
211 * Vi move to the next word
214 protected el_action_t
216 vi_next_word(EditLine *el, int c __attribute__((__unused__)))
219 if (el->el_line.cursor >= el->el_line.lastchar - 1)
222 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
223 el->el_line.lastchar, el->el_state.argument, cv__isword);
225 if (el->el_map.type == MAP_VI)
226 if (el->el_chared.c_vcmd.action != NOP) {
235 * Vi change case of character under the cursor and advance one character
238 protected el_action_t
239 vi_change_case(EditLine *el, int c)
243 if (el->el_line.cursor >= el->el_line.lastchar)
246 for (i = 0; i < el->el_state.argument; i++) {
248 c = *(unsigned char *)el->el_line.cursor;
250 *el->el_line.cursor = tolower(c);
252 *el->el_line.cursor = toupper(c);
254 if (++el->el_line.cursor >= el->el_line.lastchar) {
255 el->el_line.cursor--;
266 * Vi change prefix command
269 protected el_action_t
271 vi_change_meta(EditLine *el, int c __attribute__((__unused__)))
275 * Delete with insert == change: first we delete and then we leave in
278 return (cv_action(el, DELETE | INSERT));
282 /* vi_insert_at_bol():
283 * Vi enter insert mode at the beginning of line
286 protected el_action_t
288 vi_insert_at_bol(EditLine *el, int c __attribute__((__unused__)))
291 el->el_line.cursor = el->el_line.buffer;
293 el->el_map.current = el->el_map.key;
298 /* vi_replace_char():
299 * Vi replace character under the cursor with the next character typed
302 protected el_action_t
304 vi_replace_char(EditLine *el, int c __attribute__((__unused__)))
307 if (el->el_line.cursor >= el->el_line.lastchar)
310 el->el_map.current = el->el_map.key;
311 el->el_state.inputmode = MODE_REPLACE_1;
317 /* vi_replace_mode():
318 * Vi enter replace mode
321 protected el_action_t
323 vi_replace_mode(EditLine *el, int c __attribute__((__unused__)))
326 el->el_map.current = el->el_map.key;
327 el->el_state.inputmode = MODE_REPLACE;
333 /* vi_substitute_char():
334 * Vi replace character under the cursor and enter insert mode
337 protected el_action_t
339 vi_substitute_char(EditLine *el, int c __attribute__((__unused__)))
342 c_delafter(el, el->el_state.argument);
343 el->el_map.current = el->el_map.key;
348 /* vi_substitute_line():
349 * Vi substitute entire line
352 protected el_action_t
354 vi_substitute_line(EditLine *el, int c __attribute__((__unused__)))
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;
366 /* vi_change_to_eol():
367 * Vi change to end of line
370 protected el_action_t
372 vi_change_to_eol(EditLine *el, int c __attribute__((__unused__)))
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;
385 * Vi enter insert mode
388 protected el_action_t
390 vi_insert(EditLine *el, int c __attribute__((__unused__)))
393 el->el_map.current = el->el_map.key;
400 * Vi enter insert mode after the cursor
403 protected el_action_t
405 vi_add(EditLine *el, int c __attribute__((__unused__)))
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;
425 * Vi enter insert mode at end of line
428 protected el_action_t
430 vi_add_at_eol(EditLine *el, int c __attribute__((__unused__)))
433 el->el_map.current = el->el_map.key;
434 el->el_line.cursor = el->el_line.lastchar;
441 * Vi delete prefix command
444 protected el_action_t
446 vi_delete_meta(EditLine *el, int c __attribute__((__unused__)))
449 return (cv_action(el, DELETE));
453 /* vi_end_big_word():
454 * Vi move to the end of the current space delimited word
457 protected el_action_t
459 vi_end_big_word(EditLine *el, int c)
462 if (el->el_line.cursor == el->el_line.lastchar)
465 el->el_line.cursor = cv__endword(el->el_line.cursor,
466 el->el_line.lastchar, el->el_state.argument, cv__isWord);
468 if (el->el_chared.c_vcmd.action != NOP) {
469 el->el_line.cursor++;
478 * Vi move to the end of the current word
481 protected el_action_t
483 vi_end_word(EditLine *el, int c __attribute__((__unused__)))
486 if (el->el_line.cursor == el->el_line.lastchar)
489 el->el_line.cursor = cv__endword(el->el_line.cursor,
490 el->el_line.lastchar, el->el_state.argument, cv__isword);
492 if (el->el_chared.c_vcmd.action != NOP) {
493 el->el_line.cursor++;
502 * Vi undo last change
505 protected el_action_t
507 vi_undo(EditLine *el, int c __attribute__((__unused__)))
509 c_undo_t un = el->el_chared.c_undo;
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;
527 /* vi_command_mode():
528 * Vi enter command mode (use alternative key bindings)
531 protected el_action_t
533 vi_command_mode(EditLine *el, int c __attribute__((__unused__)))
536 /* [Esc] cancels pending action */
537 el->el_chared.c_vcmd.action = NOP;
538 el->el_chared.c_vcmd.pos = 0;
540 el->el_state.doingarg = 0;
542 el->el_state.inputmode = MODE_INSERT;
543 el->el_map.current = el->el_map.alt;
545 if (el->el_line.cursor > el->el_line.buffer)
546 el->el_line.cursor--;
553 * Vi move to the beginning of line
556 protected el_action_t
557 vi_zero(EditLine *el, int c)
560 if (el->el_state.doingarg)
561 return ed_argument_digit(el, c);
563 el->el_line.cursor = el->el_line.buffer;
564 if (el->el_chared.c_vcmd.action != NOP) {
572 /* vi_delete_prev_char():
573 * Vi move to previous character (backspace)
574 * [^H] in insert mode only
576 protected el_action_t
578 vi_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
581 if (el->el_line.cursor <= el->el_line.buffer)
585 el->el_line.cursor--;
591 * Vi list choices for completion or indicate end of file if empty line
594 protected el_action_t
596 vi_list_or_eof(EditLine *el, int c __attribute__((__unused__)))
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 */
606 * Here we could list completions, but it is an
615 *el->el_line.lastchar = '\0'; /* just in case */
616 return (CC_LIST_CHOICES);
619 * Just complain for now.
628 /* vi_kill_line_prev():
629 * Vi cut from beginning of line to cursor
632 protected el_action_t
634 vi_kill_line_prev(EditLine *el, int c __attribute__((__unused__)))
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! */
650 * Vi search history previous
653 protected el_action_t
655 vi_search_prev(EditLine *el, int c __attribute__((__unused__)))
658 return (cv_search(el, ED_SEARCH_PREV_HISTORY));
663 * Vi search history next
666 protected el_action_t
668 vi_search_next(EditLine *el, int c __attribute__((__unused__)))
671 return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
675 /* vi_repeat_search_next():
676 * Vi repeat current search in the same search direction
679 protected el_action_t
681 vi_repeat_search_next(EditLine *el, int c __attribute__((__unused__)))
684 if (el->el_search.patlen == 0)
687 return (cv_repeat_srch(el, el->el_search.patdir));
691 /* vi_repeat_search_prev():
692 * Vi repeat current search in the opposite search direction
696 protected el_action_t
697 vi_repeat_search_prev(EditLine *el, int c __attribute__((__unused__)))
700 if (el->el_search.patlen == 0)
703 return (cv_repeat_srch(el,
704 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
705 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
710 * Vi move to the character specified next
713 protected el_action_t
715 vi_next_char(EditLine *el, int c __attribute__((__unused__)))
717 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 0);
722 * Vi move to the character specified previous
725 protected el_action_t
727 vi_prev_char(EditLine *el, int c __attribute__((__unused__)))
729 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 0);
733 /* vi_to_next_char():
734 * Vi move up to the character specified next
737 protected el_action_t
739 vi_to_next_char(EditLine *el, int c __attribute__((__unused__)))
741 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 1);
745 /* vi_to_prev_char():
746 * Vi move up to the character specified previous
749 protected el_action_t
751 vi_to_prev_char(EditLine *el, int c __attribute__((__unused__)))
753 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 1);
757 /* vi_repeat_next_char():
758 * Vi repeat current character search in the same search direction
761 protected el_action_t
763 vi_repeat_next_char(EditLine *el, int c __attribute__((__unused__)))
766 return cv_csearch(el, el->el_search.chadir, el->el_search.chacha,
767 el->el_state.argument, el->el_search.chatflg);
771 /* vi_repeat_prev_char():
772 * Vi repeat current character search in the opposite search direction
775 protected el_action_t
777 vi_repeat_prev_char(EditLine *el, int c __attribute__((__unused__)))
780 int dir = el->el_search.chadir;
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;
790 * Vi go to matching () {} or []
793 protected el_action_t
795 vi_match(EditLine *el, int c)
797 const char match_chars[] = "()[]{}";
802 *el->el_line.lastchar = '\0'; /* just in case */
804 i = strcspn(el->el_line.cursor, match_chars);
805 o_ch = el->el_line.cursor[i];
808 delta = strchr(match_chars, o_ch) - match_chars;
809 c_ch = match_chars[delta ^ 1];
811 delta = 1 - (delta & 1) * 2;
813 for (cp = &el->el_line.cursor[i]; count; ) {
815 if (cp < el->el_line.buffer || cp >= el->el_line.lastchar)
819 else if (*cp == c_ch)
823 el->el_line.cursor = cp;
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. */
829 el->el_line.cursor++;
837 * Vi undo all changes to line
840 protected el_action_t
842 vi_undo_line(EditLine *el, int c)
850 * Vi go to specified column
852 * NB netbsd vi goes to screen column 'n', posix says nth character
854 protected el_action_t
856 vi_to_column(EditLine *el, int c)
859 el->el_line.cursor = el->el_line.buffer;
860 el->el_state.argument--;
861 return ed_next_char(el, 0);
865 * Vi yank to end of line
868 protected el_action_t
870 vi_yank_end(EditLine *el, int c)
873 cv_yank(el, el->el_line.cursor,
874 el->el_line.lastchar - el->el_line.cursor);
882 protected el_action_t
884 vi_yank(EditLine *el, int c)
887 return cv_action(el, YANK);
891 * Vi comment out current command
894 protected el_action_t
896 vi_comment_out(EditLine *el, int c)
899 el->el_line.cursor = el->el_line.buffer;
901 *el->el_line.cursor = '#';
903 return ed_newline(el, 0);
907 * Vi include shell alias
909 * NB: posix impiles that we should enter insert mode, however
910 * this is against historical precedent...
912 protected el_action_t
914 vi_alias(EditLine *el, int c)
919 extern char *get_alias_text(const char *);
920 __weak_extern(get_alias_text);
922 if (get_alias_text == 0) {
928 if (el_getc(el, &alias_name[1]) != 1)
931 alias_text = get_alias_text(alias_name);
932 if (alias_text != NULL)
933 el_push(el, alias_text);
940 /* vi_to_history_line():
941 * Vi go to specified history file line.
944 protected el_action_t
946 vi_to_history_line(EditLine *el, int c)
948 int sv_event_no = el->el_history.eventno;
952 if (el->el_history.eventno == 0) {
953 (void) strncpy(el->el_history.buf, el->el_line.buffer,
955 el->el_history.last = el->el_history.buf +
956 (el->el_line.lastchar - el->el_line.buffer);
959 /* Lack of a 'count' means oldest, not 1 */
960 if (!el->el_state.doingarg) {
961 el->el_history.eventno = 0x7fffffff;
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.
969 el->el_history.eventno = 1;
970 if (hist_get(el) == 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;
980 if (rval == CC_ERROR)
981 el->el_history.eventno = sv_event_no;
986 * Vi edit history line with vi
989 protected el_action_t
991 vi_histedit(EditLine *el, int c)
996 char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
999 if (el->el_state.doingarg) {
1000 if (vi_to_history_line(el, 0) == CC_ERROR)
1004 fd = mkstemp(tempfile);
1007 cp = el->el_line.buffer;
1008 write(fd, cp, el->el_line.lastchar - cp +0u);
1018 execlp("vi", "vi", tempfile, NULL);
1022 while (waitpid(pid, &st, 0) != pid)
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')
1028 el->el_line.cursor = cp;
1029 el->el_line.lastchar = cp + st;
1035 /* return CC_REFRESH; */
1036 return ed_newline(el, 0);
1039 /* vi_history_word():
1040 * Vi append word from previous input line
1042 * Who knows where this one came from!
1043 * '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
1045 protected el_action_t
1047 vi_history_word(EditLine *el, int c)
1049 const char *wp = HIST_FIRST(el);
1050 const char *wep, *wsp;
1060 while (isspace((unsigned char)*wp))
1065 while (*wp && !isspace((unsigned char)*wp))
1068 } while ((!el->el_state.doingarg || --el->el_state.argument > 0) && *wp != 0);
1070 if (wsp == 0 || (el->el_state.doingarg && el->el_state.argument != 0))
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;
1082 while (wsp < wep && cp < lim)
1084 el->el_line.cursor = cp;
1086 el->el_map.current = el->el_map.key;
1091 * Vi redo last non-motion command
1094 protected el_action_t
1096 vi_redo(EditLine *el, int c)
1098 c_redo_t *r = &el->el_chared.c_redo;
1100 if (!el->el_state.doingarg && r->count) {
1101 el->el_state.doingarg = 1;
1102 el->el_state.argument = r->count;
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)
1110 r->pos = r->lim - 1;
1112 el_push(el, r->buf);
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);