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 * $NetBSD: vi.c,v 1.24 2005/08/10 12:46:24 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * vi.c: Vi mode commands.
48 private el_action_t cv_action(EditLine *, int);
49 private el_action_t cv_paste(EditLine *, int);
55 cv_action(EditLine *el, int c)
58 if (el->el_chared.c_vcmd.action != NOP) {
59 /* 'cc', 'dd' and (possibly) friends */
60 if (c != el->el_chared.c_vcmd.action)
65 cv_yank(el, el->el_line.buffer,
66 el->el_line.lastchar - el->el_line.buffer);
67 el->el_chared.c_vcmd.action = NOP;
68 el->el_chared.c_vcmd.pos = 0;
70 el->el_line.lastchar = el->el_line.buffer;
71 el->el_line.cursor = el->el_line.buffer;
74 el->el_map.current = el->el_map.key;
78 el->el_chared.c_vcmd.pos = el->el_line.cursor;
79 el->el_chared.c_vcmd.action = c;
84 * Paste previous deletion before or after the cursor
87 cv_paste(EditLine *el, int c)
89 c_kill_t *k = &el->el_chared.c_kill;
90 int len = k->last - k->buf;
92 if (k->buf == NULL || len == 0)
95 (void) fprintf(el->el_errfile, "Paste: \"%.*s\"\n", len, k->buf);
100 if (!c && el->el_line.cursor < el->el_line.lastchar)
101 el->el_line.cursor++;
104 if (el->el_line.cursor + len > el->el_line.lastchar)
106 (void) memcpy(el->el_line.cursor, k->buf, len +0u);
113 * Vi paste previous deletion to the right of the cursor
116 protected el_action_t
118 vi_paste_next(EditLine *el, int c __unused)
121 return (cv_paste(el, 0));
126 * Vi paste previous deletion to the left of the cursor
129 protected el_action_t
131 vi_paste_prev(EditLine *el, int c __unused)
134 return (cv_paste(el, 1));
138 /* vi_prev_big_word():
139 * Vi move to the previous space delimited word
142 protected el_action_t
144 vi_prev_big_word(EditLine *el, int c)
147 if (el->el_line.cursor == el->el_line.buffer)
150 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
152 el->el_state.argument,
155 if (el->el_chared.c_vcmd.action != NOP) {
164 * Vi move to the previous word
167 protected el_action_t
169 vi_prev_word(EditLine *el, int c __unused)
172 if (el->el_line.cursor == el->el_line.buffer)
175 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
177 el->el_state.argument,
180 if (el->el_chared.c_vcmd.action != NOP) {
188 /* vi_next_big_word():
189 * Vi move to the next space delimited word
192 protected el_action_t
194 vi_next_big_word(EditLine *el, int c)
197 if (el->el_line.cursor >= el->el_line.lastchar - 1)
200 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
201 el->el_line.lastchar, el->el_state.argument, cv__isWord);
203 if (el->el_map.type == MAP_VI)
204 if (el->el_chared.c_vcmd.action != NOP) {
213 * Vi move to the next word
216 protected el_action_t
218 vi_next_word(EditLine *el, int c __unused)
221 if (el->el_line.cursor >= el->el_line.lastchar - 1)
224 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
225 el->el_line.lastchar, el->el_state.argument, cv__isword);
227 if (el->el_map.type == MAP_VI)
228 if (el->el_chared.c_vcmd.action != NOP) {
237 * Vi change case of character under the cursor and advance one character
240 protected el_action_t
241 vi_change_case(EditLine *el, int c)
245 if (el->el_line.cursor >= el->el_line.lastchar)
248 for (i = 0; i < el->el_state.argument; i++) {
250 c = *(unsigned char *)el->el_line.cursor;
252 *el->el_line.cursor = tolower(c);
254 *el->el_line.cursor = toupper(c);
256 if (++el->el_line.cursor >= el->el_line.lastchar) {
257 el->el_line.cursor--;
268 * Vi change prefix command
271 protected el_action_t
273 vi_change_meta(EditLine *el, int c __unused)
277 * Delete with insert == change: first we delete and then we leave in
280 return (cv_action(el, DELETE | INSERT));
284 /* vi_insert_at_bol():
285 * Vi enter insert mode at the beginning of line
288 protected el_action_t
290 vi_insert_at_bol(EditLine *el, int c __unused)
293 el->el_line.cursor = el->el_line.buffer;
295 el->el_map.current = el->el_map.key;
300 /* vi_replace_char():
301 * Vi replace character under the cursor with the next character typed
304 protected el_action_t
306 vi_replace_char(EditLine *el, int c __unused)
309 if (el->el_line.cursor >= el->el_line.lastchar)
312 el->el_map.current = el->el_map.key;
313 el->el_state.inputmode = MODE_REPLACE_1;
319 /* vi_replace_mode():
320 * Vi enter replace mode
323 protected el_action_t
325 vi_replace_mode(EditLine *el, int c __unused)
328 el->el_map.current = el->el_map.key;
329 el->el_state.inputmode = MODE_REPLACE;
335 /* vi_substitute_char():
336 * Vi replace character under the cursor and enter insert mode
339 protected el_action_t
341 vi_substitute_char(EditLine *el, int c __unused)
344 c_delafter(el, el->el_state.argument);
345 el->el_map.current = el->el_map.key;
350 /* vi_substitute_line():
351 * Vi substitute entire line
354 protected el_action_t
356 vi_substitute_line(EditLine *el, int c __unused)
360 cv_yank(el, el->el_line.buffer,
361 el->el_line.lastchar - el->el_line.buffer);
362 (void) em_kill_line(el, 0);
363 el->el_map.current = el->el_map.key;
368 /* vi_change_to_eol():
369 * Vi change to end of line
372 protected el_action_t
374 vi_change_to_eol(EditLine *el, int c __unused)
378 cv_yank(el, el->el_line.cursor,
379 el->el_line.lastchar - el->el_line.cursor);
380 (void) ed_kill_line(el, 0);
381 el->el_map.current = el->el_map.key;
387 * Vi enter insert mode
390 protected el_action_t
392 vi_insert(EditLine *el, int c __unused)
395 el->el_map.current = el->el_map.key;
402 * Vi enter insert mode after the cursor
405 protected el_action_t
407 vi_add(EditLine *el, int c __unused)
411 el->el_map.current = el->el_map.key;
412 if (el->el_line.cursor < el->el_line.lastchar) {
413 el->el_line.cursor++;
414 if (el->el_line.cursor > el->el_line.lastchar)
415 el->el_line.cursor = el->el_line.lastchar;
427 * Vi enter insert mode at end of line
430 protected el_action_t
432 vi_add_at_eol(EditLine *el, int c __unused)
435 el->el_map.current = el->el_map.key;
436 el->el_line.cursor = el->el_line.lastchar;
443 * Vi delete prefix command
446 protected el_action_t
448 vi_delete_meta(EditLine *el, int c __unused)
451 return (cv_action(el, DELETE));
455 /* vi_end_big_word():
456 * Vi move to the end of the current space delimited word
459 protected el_action_t
461 vi_end_big_word(EditLine *el, int c)
464 if (el->el_line.cursor == el->el_line.lastchar)
467 el->el_line.cursor = cv__endword(el->el_line.cursor,
468 el->el_line.lastchar, el->el_state.argument, cv__isWord);
470 if (el->el_chared.c_vcmd.action != NOP) {
471 el->el_line.cursor++;
480 * Vi move to the end of the current word
483 protected el_action_t
485 vi_end_word(EditLine *el, int c __unused)
488 if (el->el_line.cursor == el->el_line.lastchar)
491 el->el_line.cursor = cv__endword(el->el_line.cursor,
492 el->el_line.lastchar, el->el_state.argument, cv__isword);
494 if (el->el_chared.c_vcmd.action != NOP) {
495 el->el_line.cursor++;
504 * Vi undo last change
507 protected el_action_t
509 vi_undo(EditLine *el, int c __unused)
511 c_undo_t un = el->el_chared.c_undo;
516 /* switch line buffer and undo buffer */
517 el->el_chared.c_undo.buf = el->el_line.buffer;
518 el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
519 el->el_chared.c_undo.cursor = el->el_line.cursor - el->el_line.buffer;
520 el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
521 el->el_line.buffer = un.buf;
522 el->el_line.cursor = un.buf + un.cursor;
523 el->el_line.lastchar = un.buf + un.len;
529 /* vi_command_mode():
530 * Vi enter command mode (use alternative key bindings)
533 protected el_action_t
535 vi_command_mode(EditLine *el, int c __unused)
538 /* [Esc] cancels pending action */
539 el->el_chared.c_vcmd.action = NOP;
540 el->el_chared.c_vcmd.pos = 0;
542 el->el_state.doingarg = 0;
544 el->el_state.inputmode = MODE_INSERT;
545 el->el_map.current = el->el_map.alt;
547 if (el->el_line.cursor > el->el_line.buffer)
548 el->el_line.cursor--;
555 * Vi move to the beginning of line
558 protected el_action_t
559 vi_zero(EditLine *el, int c)
562 if (el->el_state.doingarg)
563 return ed_argument_digit(el, c);
565 el->el_line.cursor = el->el_line.buffer;
566 if (el->el_chared.c_vcmd.action != NOP) {
574 /* vi_delete_prev_char():
575 * Vi move to previous character (backspace)
576 * [^H] in insert mode only
578 protected el_action_t
580 vi_delete_prev_char(EditLine *el, int c __unused)
583 if (el->el_line.cursor <= el->el_line.buffer)
587 el->el_line.cursor--;
593 * Vi list choices for completion or indicate end of file if empty line
596 protected el_action_t
598 vi_list_or_eof(EditLine *el, int c __unused)
601 if (el->el_line.cursor == el->el_line.lastchar) {
602 if (el->el_line.cursor == el->el_line.buffer) {
603 term_overwrite(el, STReof, 4); /* then do a EOF */
608 * Here we could list completions, but it is an
617 *el->el_line.lastchar = '\0'; /* just in case */
618 return (CC_LIST_CHOICES);
621 * Just complain for now.
630 /* vi_kill_line_prev():
631 * Vi cut from beginning of line to cursor
634 protected el_action_t
636 vi_kill_line_prev(EditLine *el, int c __unused)
640 cp = el->el_line.buffer;
641 kp = el->el_chared.c_kill.buf;
642 while (cp < el->el_line.cursor)
643 *kp++ = *cp++; /* copy it */
644 el->el_chared.c_kill.last = kp;
645 c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
646 el->el_line.cursor = el->el_line.buffer; /* zap! */
652 * Vi search history previous
655 protected el_action_t
657 vi_search_prev(EditLine *el, int c __unused)
660 return (cv_search(el, ED_SEARCH_PREV_HISTORY));
665 * Vi search history next
668 protected el_action_t
670 vi_search_next(EditLine *el, int c __unused)
673 return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
677 /* vi_repeat_search_next():
678 * Vi repeat current search in the same search direction
681 protected el_action_t
683 vi_repeat_search_next(EditLine *el, int c __unused)
686 if (el->el_search.patlen == 0)
689 return (cv_repeat_srch(el, el->el_search.patdir));
693 /* vi_repeat_search_prev():
694 * Vi repeat current search in the opposite search direction
698 protected el_action_t
699 vi_repeat_search_prev(EditLine *el, int c __unused)
702 if (el->el_search.patlen == 0)
705 return (cv_repeat_srch(el,
706 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
707 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
712 * Vi move to the character specified next
715 protected el_action_t
717 vi_next_char(EditLine *el, int c __unused)
719 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 0);
724 * Vi move to the character specified previous
727 protected el_action_t
729 vi_prev_char(EditLine *el, int c __unused)
731 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 0);
735 /* vi_to_next_char():
736 * Vi move up to the character specified next
739 protected el_action_t
741 vi_to_next_char(EditLine *el, int c __unused)
743 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 1);
747 /* vi_to_prev_char():
748 * Vi move up to the character specified previous
751 protected el_action_t
753 vi_to_prev_char(EditLine *el, int c __unused)
755 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 1);
759 /* vi_repeat_next_char():
760 * Vi repeat current character search in the same search direction
763 protected el_action_t
765 vi_repeat_next_char(EditLine *el, int c __unused)
768 return cv_csearch(el, el->el_search.chadir, el->el_search.chacha,
769 el->el_state.argument, el->el_search.chatflg);
773 /* vi_repeat_prev_char():
774 * Vi repeat current character search in the opposite search direction
777 protected el_action_t
779 vi_repeat_prev_char(EditLine *el, int c __unused)
782 int dir = el->el_search.chadir;
784 r = cv_csearch(el, -dir, el->el_search.chacha,
785 el->el_state.argument, el->el_search.chatflg);
786 el->el_search.chadir = dir;
792 * Vi go to matching () {} or []
795 protected el_action_t
797 vi_match(EditLine *el, int c)
799 const char match_chars[] = "()[]{}";
804 *el->el_line.lastchar = '\0'; /* just in case */
806 i = strcspn(el->el_line.cursor, match_chars);
807 o_ch = el->el_line.cursor[i];
810 delta = strchr(match_chars, o_ch) - match_chars;
811 c_ch = match_chars[delta ^ 1];
813 delta = 1 - (delta & 1) * 2;
815 for (cp = &el->el_line.cursor[i]; count; ) {
817 if (cp < el->el_line.buffer || cp >= el->el_line.lastchar)
821 else if (*cp == c_ch)
825 el->el_line.cursor = cp;
827 if (el->el_chared.c_vcmd.action != NOP) {
828 /* NB posix says char under cursor should NOT be deleted
829 for -ve delta - this is different to netbsd vi. */
831 el->el_line.cursor++;
839 * Vi undo all changes to line
842 protected el_action_t
844 vi_undo_line(EditLine *el, int c)
852 * Vi go to specified column
854 * NB netbsd vi goes to screen column 'n', posix says nth character
856 protected el_action_t
858 vi_to_column(EditLine *el, int c)
861 el->el_line.cursor = el->el_line.buffer;
862 el->el_state.argument--;
863 return ed_next_char(el, 0);
867 * Vi yank to end of line
870 protected el_action_t
872 vi_yank_end(EditLine *el, int c)
875 cv_yank(el, el->el_line.cursor,
876 el->el_line.lastchar - el->el_line.cursor);
884 protected el_action_t
886 vi_yank(EditLine *el, int c)
889 return cv_action(el, YANK);
893 * Vi comment out current command
896 protected el_action_t
898 vi_comment_out(EditLine *el, int c)
901 el->el_line.cursor = el->el_line.buffer;
903 *el->el_line.cursor = '#';
905 return ed_newline(el, 0);
909 * Vi include shell alias
911 * NB: posix implies that we should enter insert mode, however
912 * this is against historical precedent...
914 protected el_action_t
916 vi_alias(EditLine *el, int c)
921 extern char *get_alias_text(const char *);
922 __weak_extern(get_alias_text);
924 if (get_alias_text == 0) {
930 if (el_getc(el, &alias_name[1]) != 1)
933 alias_text = get_alias_text(alias_name);
934 if (alias_text != NULL)
935 el_push(el, alias_text);
942 /* vi_to_history_line():
943 * Vi go to specified history file line.
946 protected el_action_t
948 vi_to_history_line(EditLine *el, int c)
950 int sv_event_no = el->el_history.eventno;
954 if (el->el_history.eventno == 0) {
955 (void) strncpy(el->el_history.buf, el->el_line.buffer,
957 el->el_history.last = el->el_history.buf +
958 (el->el_line.lastchar - el->el_line.buffer);
961 /* Lack of a 'count' means oldest, not 1 */
962 if (!el->el_state.doingarg) {
963 el->el_history.eventno = 0x7fffffff;
966 /* This is brain dead, all the rest of this code counts
967 * upwards going into the past. Here we need count in the
968 * other direction (to match the output of fc -l).
969 * I could change the world, but this seems to suffice.
971 el->el_history.eventno = 1;
972 if (hist_get(el) == CC_ERROR)
974 el->el_history.eventno = 1 + el->el_history.ev.num
975 - el->el_state.argument;
976 if (el->el_history.eventno < 0) {
977 el->el_history.eventno = sv_event_no;
982 if (rval == CC_ERROR)
983 el->el_history.eventno = sv_event_no;
988 * Vi edit history line with vi
991 protected el_action_t
993 vi_histedit(EditLine *el, int c)
998 char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
1001 if (el->el_state.doingarg) {
1002 if (vi_to_history_line(el, 0) == CC_ERROR)
1006 fd = mkstemp(tempfile);
1009 cp = el->el_line.buffer;
1010 write(fd, cp, el->el_line.lastchar - cp +0u);
1020 execlp("vi", "vi", tempfile, NULL);
1024 while (waitpid(pid, &st, 0) != pid)
1026 lseek(fd, 0ll, SEEK_SET);
1027 st = read(fd, cp, el->el_line.limit - cp +0u);
1028 if (st > 0 && cp[st - 1] == '\n')
1030 el->el_line.cursor = cp;
1031 el->el_line.lastchar = cp + st;
1037 /* return CC_REFRESH; */
1038 return ed_newline(el, 0);
1041 /* vi_history_word():
1042 * Vi append word from previous input line
1044 * Who knows where this one came from!
1045 * '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
1047 protected el_action_t
1049 vi_history_word(EditLine *el, int c)
1051 const char *wp = HIST_FIRST(el);
1052 const char *wep, *wsp;
1062 while (isspace((unsigned char)*wp))
1067 while (*wp && !isspace((unsigned char)*wp))
1070 } while ((!el->el_state.doingarg || --el->el_state.argument > 0) && *wp != 0);
1072 if (wsp == 0 || (el->el_state.doingarg && el->el_state.argument != 0))
1077 if (el->el_line.cursor < el->el_line.lastchar)
1078 el->el_line.cursor++;
1079 c_insert(el, len + 1);
1080 cp = el->el_line.cursor;
1081 lim = el->el_line.limit;
1084 while (wsp < wep && cp < lim)
1086 el->el_line.cursor = cp;
1088 el->el_map.current = el->el_map.key;
1093 * Vi redo last non-motion command
1096 protected el_action_t
1098 vi_redo(EditLine *el, int c)
1100 c_redo_t *r = &el->el_chared.c_redo;
1102 if (!el->el_state.doingarg && r->count) {
1103 el->el_state.doingarg = 1;
1104 el->el_state.argument = r->count;
1107 el->el_chared.c_vcmd.pos = el->el_line.cursor;
1108 el->el_chared.c_vcmd.action = r->action;
1109 if (r->pos != r->buf) {
1110 if (r->pos + 1 > r->lim)
1112 r->pos = r->lim - 1;
1114 el_push(el, r->buf);
1117 el->el_state.thiscmd = r->cmd;
1118 el->el_state.thisch = r->ch;
1119 return (*el->el_map.func[r->cmd])(el, r->ch);