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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $NetBSD: vi.c,v 1.7 1999/07/02 15:21:28 simonb Exp $
39 #if !defined(lint) && !defined(SCCSID)
40 static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
41 #endif /* not lint && not SCCSID */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * vi.c: Vi mode commands.
51 private el_action_t cv_action(EditLine *, int);
52 private el_action_t cv_paste(EditLine *, int);
58 cv_action(EditLine *el, int c)
62 if (el->el_chared.c_vcmd.action & DELETE) {
63 el->el_chared.c_vcmd.action = NOP;
64 el->el_chared.c_vcmd.pos = 0;
66 el->el_chared.c_undo.isize = 0;
67 el->el_chared.c_undo.dsize = 0;
68 kp = el->el_chared.c_undo.buf;
69 for (cp = el->el_line.buffer; cp < el->el_line.lastchar; cp++) {
71 el->el_chared.c_undo.dsize++;
74 el->el_chared.c_undo.action = INSERT;
75 el->el_chared.c_undo.ptr = el->el_line.buffer;
76 el->el_line.lastchar = el->el_line.buffer;
77 el->el_line.cursor = el->el_line.buffer;
79 el->el_map.current = el->el_map.key;
83 el->el_chared.c_vcmd.pos = el->el_line.cursor;
84 el->el_chared.c_vcmd.action = c;
89 * I don't think that this is needed. But we keep it for now
92 if (el_chared.c_vcmd.action == NOP) {
93 el->el_chared.c_vcmd.pos = el->el_line.cursor;
94 el->el_chared.c_vcmd.action = c;
97 el->el_chared.c_vcmd.action = 0;
98 el->el_chared.c_vcmd.pos = 0;
106 * Paste previous deletion before or after the cursor
109 cv_paste(EditLine *el, int c)
112 c_undo_t *un = &el->el_chared.c_undo;
115 (void) fprintf(el->el_errfile, "Paste: %x \"%s\" +%d -%d\n",
116 un->action, un->buf, un->isize, un->dsize);
121 if (!c && el->el_line.cursor < el->el_line.lastchar)
122 el->el_line.cursor++;
123 ptr = el->el_line.cursor;
125 c_insert(el, (int) un->isize);
126 if (el->el_line.cursor + un->isize > el->el_line.lastchar)
128 (void) memcpy(ptr, un->buf, un->isize);
134 * Vi paste previous deletion to the right of the cursor
137 protected el_action_t
139 vi_paste_next(EditLine *el, int c)
142 return (cv_paste(el, 0));
147 * Vi paste previous deletion to the left of the cursor
150 protected el_action_t
152 vi_paste_prev(EditLine *el, int c)
155 return (cv_paste(el, 1));
159 /* vi_prev_space_word():
160 * Vi move to the previous space delimited word
163 protected el_action_t
165 vi_prev_space_word(EditLine *el, int c)
168 if (el->el_line.cursor == el->el_line.buffer)
171 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
173 el->el_state.argument,
176 if (el->el_chared.c_vcmd.action & DELETE) {
185 * Vi move to the previous word
188 protected el_action_t
190 vi_prev_word(EditLine *el, int c)
193 if (el->el_line.cursor == el->el_line.buffer)
196 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
198 el->el_state.argument,
201 if (el->el_chared.c_vcmd.action & DELETE) {
209 /* vi_next_space_word():
210 * Vi move to the next space delimited word
213 protected el_action_t
215 vi_next_space_word(EditLine *el, int c)
218 if (el->el_line.cursor == el->el_line.lastchar)
221 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
222 el->el_line.lastchar,
223 el->el_state.argument,
226 if (el->el_map.type == MAP_VI)
227 if (el->el_chared.c_vcmd.action & DELETE) {
236 * Vi move to the next word
239 protected el_action_t
241 vi_next_word(EditLine *el, int c)
244 if (el->el_line.cursor == el->el_line.lastchar)
247 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
248 el->el_line.lastchar,
249 el->el_state.argument,
252 if (el->el_map.type == MAP_VI)
253 if (el->el_chared.c_vcmd.action & DELETE) {
262 * Vi change case of character under the cursor and advance one character
265 protected el_action_t
266 vi_change_case(EditLine *el, int c)
269 if (el->el_line.cursor < el->el_line.lastchar) {
270 c = (unsigned char)*el->el_line.cursor;
272 *el->el_line.cursor++ = tolower(c);
274 *el->el_line.cursor++ = toupper(c);
276 el->el_line.cursor++;
285 * Vi change prefix command
288 protected el_action_t
290 vi_change_meta(EditLine *el, int c)
294 * Delete with insert == change: first we delete and then we leave in
297 return (cv_action(el, DELETE | INSERT));
301 /* vi_insert_at_bol():
302 * Vi enter insert mode at the beginning of line
305 protected el_action_t
307 vi_insert_at_bol(EditLine *el, int c)
310 el->el_line.cursor = el->el_line.buffer;
311 el->el_chared.c_vcmd.ins = el->el_line.cursor;
313 el->el_chared.c_undo.ptr = el->el_line.cursor;
314 el->el_chared.c_undo.action = DELETE;
316 el->el_map.current = el->el_map.key;
321 /* vi_replace_char():
322 * Vi replace character under the cursor with the next character typed
325 protected el_action_t
327 vi_replace_char(EditLine *el, int c)
330 el->el_map.current = el->el_map.key;
331 el->el_state.inputmode = MODE_REPLACE_1;
332 el->el_chared.c_undo.action = CHANGE;
333 el->el_chared.c_undo.ptr = el->el_line.cursor;
334 el->el_chared.c_undo.isize = 0;
335 el->el_chared.c_undo.dsize = 0;
340 /* vi_replace_mode():
341 * Vi enter replace mode
344 protected el_action_t
346 vi_replace_mode(EditLine *el, int c)
349 el->el_map.current = el->el_map.key;
350 el->el_state.inputmode = MODE_REPLACE;
351 el->el_chared.c_undo.action = CHANGE;
352 el->el_chared.c_undo.ptr = el->el_line.cursor;
353 el->el_chared.c_undo.isize = 0;
354 el->el_chared.c_undo.dsize = 0;
359 /* vi_substitute_char():
360 * Vi replace character under the cursor and enter insert mode
363 protected el_action_t
365 vi_substitute_char(EditLine *el, int c)
368 c_delafter(el, el->el_state.argument);
369 el->el_map.current = el->el_map.key;
374 /* vi_substitute_line():
375 * Vi substitute entire line
378 protected el_action_t
380 vi_substitute_line(EditLine *el, int c)
383 (void) em_kill_line(el, 0);
384 el->el_map.current = el->el_map.key;
389 /* vi_change_to_eol():
390 * Vi change to end of line
393 protected el_action_t
395 vi_change_to_eol(EditLine *el, int c)
398 (void) ed_kill_line(el, 0);
399 el->el_map.current = el->el_map.key;
405 * Vi enter insert mode
408 protected el_action_t
410 vi_insert(EditLine *el, int c)
413 el->el_map.current = el->el_map.key;
415 el->el_chared.c_vcmd.ins = el->el_line.cursor;
416 el->el_chared.c_undo.ptr = el->el_line.cursor;
417 el->el_chared.c_undo.action = DELETE;
424 * Vi enter insert mode after the cursor
427 protected el_action_t
429 vi_add(EditLine *el, int c)
433 el->el_map.current = el->el_map.key;
434 if (el->el_line.cursor < el->el_line.lastchar) {
435 el->el_line.cursor++;
436 if (el->el_line.cursor > el->el_line.lastchar)
437 el->el_line.cursor = el->el_line.lastchar;
442 el->el_chared.c_vcmd.ins = el->el_line.cursor;
443 el->el_chared.c_undo.ptr = el->el_line.cursor;
444 el->el_chared.c_undo.action = DELETE;
451 * Vi enter insert mode at end of line
454 protected el_action_t
456 vi_add_at_eol(EditLine *el, int c)
459 el->el_map.current = el->el_map.key;
460 el->el_line.cursor = el->el_line.lastchar;
462 /* Mark where insertion begins */
463 el->el_chared.c_vcmd.ins = el->el_line.lastchar;
464 el->el_chared.c_undo.ptr = el->el_line.lastchar;
465 el->el_chared.c_undo.action = DELETE;
471 * Vi delete prefix command
474 protected el_action_t
476 vi_delete_meta(EditLine *el, int c)
479 return (cv_action(el, DELETE));
484 * Vi move to the end of the current space delimited word
487 protected el_action_t
489 vi_end_word(EditLine *el, int c)
492 if (el->el_line.cursor == el->el_line.lastchar)
495 el->el_line.cursor = cv__endword(el->el_line.cursor,
496 el->el_line.lastchar, el->el_state.argument);
498 if (el->el_chared.c_vcmd.action & DELETE) {
499 el->el_line.cursor++;
508 * Vi move to the end of the current word
511 protected el_action_t
513 vi_to_end_word(EditLine *el, int c)
516 if (el->el_line.cursor == el->el_line.lastchar)
519 el->el_line.cursor = cv__endword(el->el_line.cursor,
520 el->el_line.lastchar, el->el_state.argument);
522 if (el->el_chared.c_vcmd.action & DELETE) {
523 el->el_line.cursor++;
532 * Vi undo last change
535 protected el_action_t
537 vi_undo(EditLine *el, int c)
542 c_undo_t *un = &el->el_chared.c_undo;
545 (void) fprintf(el->el_errfile, "Undo: %x \"%s\" +%d -%d\n",
546 un->action, un->buf, un->isize, un->dsize);
548 switch (un->action) {
553 (void) memcpy(un->buf, un->ptr, un->dsize);
554 for (cp = un->ptr; cp <= el->el_line.lastchar; cp++)
557 el->el_line.lastchar -= un->dsize;
558 el->el_line.cursor = un->ptr;
561 un->isize = un->dsize;
565 case DELETE | INSERT:
566 size = un->isize - un->dsize;
579 el->el_line.cursor = cp;
581 while (size-- > 0 && cp < el->el_line.lastchar) {
586 } else if (size < 0) {
588 for (; cp <= el->el_line.lastchar; cp++) {
592 el->el_line.lastchar -= size;
594 el->el_line.cursor = un->ptr;
596 un->dsize = un->isize;
604 el->el_line.cursor = un->ptr;
605 c_insert(el, (int) un->isize);
606 (void) memcpy(un->ptr, un->buf, un->isize);
608 un->dsize = un->isize;
616 el->el_line.cursor = un->ptr;
617 size = (int) (el->el_line.cursor - el->el_line.lastchar);
618 if (size < un->isize)
622 for (i = 0; i < size; i++) {
639 * Vi undo all changes
642 protected el_action_t
644 vi_undo_line(EditLine *el, int c)
651 /* vi_command_mode():
652 * Vi enter command mode (use alternative key bindings)
655 protected el_action_t
657 vi_command_mode(EditLine *el, int c)
661 /* [Esc] cancels pending action */
662 el->el_chared.c_vcmd.ins = 0;
663 el->el_chared.c_vcmd.action = NOP;
664 el->el_chared.c_vcmd.pos = 0;
666 el->el_state.doingarg = 0;
667 size = el->el_chared.c_undo.ptr - el->el_line.cursor;
670 if (el->el_chared.c_undo.action == (INSERT | DELETE) ||
671 el->el_chared.c_undo.action == DELETE)
672 el->el_chared.c_undo.dsize = size;
674 el->el_chared.c_undo.isize = size;
676 el->el_state.inputmode = MODE_INSERT;
677 el->el_map.current = el->el_map.alt;
679 if (el->el_line.cursor > el->el_line.buffer)
680 el->el_line.cursor--;
687 * Vi move to the beginning of line
690 protected el_action_t
691 vi_zero(EditLine *el, int c)
694 if (el->el_state.doingarg) {
695 if (el->el_state.argument > 1000000)
697 el->el_state.argument =
698 (el->el_state.argument * 10) + (c - '0');
701 el->el_line.cursor = el->el_line.buffer;
702 if (el->el_chared.c_vcmd.action & DELETE) {
711 /* vi_delete_prev_char():
712 * Vi move to previous character (backspace)
715 protected el_action_t
717 vi_delete_prev_char(EditLine *el, int c)
720 if (el->el_chared.c_vcmd.ins == 0)
723 if (el->el_chared.c_vcmd.ins >
724 el->el_line.cursor - el->el_state.argument)
727 c_delbefore(el, el->el_state.argument);
728 el->el_line.cursor -= el->el_state.argument;
735 * Vi list choices for completion or indicate end of file if empty line
738 protected el_action_t
740 vi_list_or_eof(EditLine *el, int c)
744 if (el->el_line.cursor == el->el_line.lastchar &&
745 el->el_line.cursor == el->el_line.buffer) {
747 term_overwrite(el, STReof, 4); /* then do a EOF */
753 *el->el_line.lastchar = '\0'; /* just in case */
754 return (CC_LIST_CHOICES);
760 /* vi_kill_line_prev():
761 * Vi cut from beginning of line to cursor
764 protected el_action_t
766 vi_kill_line_prev(EditLine *el, int c)
770 cp = el->el_line.buffer;
771 kp = el->el_chared.c_kill.buf;
772 while (cp < el->el_line.cursor)
773 *kp++ = *cp++; /* copy it */
774 el->el_chared.c_kill.last = kp;
775 c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
776 el->el_line.cursor = el->el_line.buffer; /* zap! */
782 * Vi search history previous
785 protected el_action_t
787 vi_search_prev(EditLine *el, int c)
790 return (cv_search(el, ED_SEARCH_PREV_HISTORY));
795 * Vi search history next
798 protected el_action_t
800 vi_search_next(EditLine *el, int c)
803 return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
807 /* vi_repeat_search_next():
808 * Vi repeat current search in the same search direction
811 protected el_action_t
813 vi_repeat_search_next(EditLine *el, int c)
816 if (el->el_search.patlen == 0)
819 return (cv_repeat_srch(el, el->el_search.patdir));
823 /* vi_repeat_search_prev():
824 * Vi repeat current search in the opposite search direction
828 protected el_action_t
829 vi_repeat_search_prev(EditLine *el, int c)
832 if (el->el_search.patlen == 0)
835 return (cv_repeat_srch(el,
836 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
837 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
842 * Vi move to the character specified next
845 protected el_action_t
847 vi_next_char(EditLine *el, int c)
851 if (el_getc(el, &ch) != 1)
852 return (ed_end_of_file(el, 0));
854 el->el_search.chadir = CHAR_FWD;
855 el->el_search.chacha = ch;
857 return (cv_csearch_fwd(el, ch, el->el_state.argument, 0));
863 * Vi move to the character specified previous
866 protected el_action_t
868 vi_prev_char(EditLine *el, int c)
872 if (el_getc(el, &ch) != 1)
873 return (ed_end_of_file(el, 0));
875 el->el_search.chadir = CHAR_BACK;
876 el->el_search.chacha = ch;
878 return (cv_csearch_back(el, ch, el->el_state.argument, 0));
882 /* vi_to_next_char():
883 * Vi move up to the character specified next
886 protected el_action_t
888 vi_to_next_char(EditLine *el, int c)
892 if (el_getc(el, &ch) != 1)
893 return (ed_end_of_file(el, 0));
895 return (cv_csearch_fwd(el, ch, el->el_state.argument, 1));
900 /* vi_to_prev_char():
901 * Vi move up to the character specified previous
904 protected el_action_t
906 vi_to_prev_char(EditLine *el, int c)
910 if (el_getc(el, &ch) != 1)
911 return (ed_end_of_file(el, 0));
913 return (cv_csearch_back(el, ch, el->el_state.argument, 1));
917 /* vi_repeat_next_char():
918 * Vi repeat current character search in the same search direction
921 protected el_action_t
923 vi_repeat_next_char(EditLine *el, int c)
926 if (el->el_search.chacha == 0)
929 return (el->el_search.chadir == CHAR_FWD
930 ? cv_csearch_fwd(el, el->el_search.chacha,
931 el->el_state.argument, 0)
932 : cv_csearch_back(el, el->el_search.chacha,
933 el->el_state.argument, 0));
937 /* vi_repeat_prev_char():
938 * Vi repeat current character search in the opposite search direction
941 protected el_action_t
943 vi_repeat_prev_char(EditLine *el, int c)
946 if (el->el_search.chacha == 0)
949 return el->el_search.chadir == CHAR_BACK ?
950 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
951 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);