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
37 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
39 #endif /* not lint && not SCCSID */
42 * vi.c: Vi mode commands.
47 private el_action_t cv_action __P((EditLine *, int));
57 register char *cp, *kp;
59 if (el->el_chared.c_vcmd.action & DELETE) {
60 el->el_chared.c_vcmd.action = NOP;
61 el->el_chared.c_vcmd.pos = 0;
63 el->el_chared.c_undo.isize = 0;
64 el->el_chared.c_undo.dsize = 0;
65 kp = el->el_chared.c_undo.buf;
66 for (cp = el->el_line.buffer; cp < el->el_line.lastchar; cp++) {
68 el->el_chared.c_undo.dsize++;
71 el->el_chared.c_undo.action = INSERT;
72 el->el_chared.c_undo.ptr = el->el_line.buffer;
73 el->el_line.lastchar = el->el_line.buffer;
74 el->el_line.cursor = el->el_line.buffer;
76 el->el_map.current = el->el_map.key;
81 el->el_chared.c_vcmd.pos = el->el_line.cursor;
82 el->el_chared.c_vcmd.action = c;
87 * I don't think that this is needed. But we keep it for now
89 else if (el_chared.c_vcmd.action == NOP) {
90 el->el_chared.c_vcmd.pos = el->el_line.cursor;
91 el->el_chared.c_vcmd.action = c;
95 el->el_chared.c_vcmd.action = 0;
96 el->el_chared.c_vcmd.pos = 0;
104 * Paste previous deletion before or after the cursor
106 protected el_action_t
112 c_undo_t *un = &el->el_chared.c_undo;
114 (void) fprintf(el->el_errfile, "Paste: %x \"%s\" +%d -%d\n",
115 un->action, un->buf, un->isize, un->dsize);
120 if (!c && el->el_line.cursor < el->el_line.lastchar)
121 el->el_line.cursor++;
122 ptr = el->el_line.cursor;
124 c_insert(el, un->isize);
125 if (el->el_line.cursor + un->isize > el->el_line.lastchar)
127 (void) memcpy(ptr, un->buf, un->isize);
133 * Vi paste previous deletion to the right of the cursor
136 protected el_action_t
142 return cv_paste(el, 0);
147 * Vi paste previous deletion to the left of the cursor
150 protected el_action_t
156 return cv_paste(el, 1);
160 /* vi_prev_space_word():
161 * Vi move to the previous space delimited word
164 protected el_action_t
166 vi_prev_space_word(el, c)
170 if (el->el_line.cursor == el->el_line.buffer)
173 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
175 el->el_state.argument,
178 if (el->el_chared.c_vcmd.action & DELETE) {
188 * Vi move to the previous word
191 protected el_action_t
197 if (el->el_line.cursor == el->el_line.buffer)
200 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
202 el->el_state.argument,
205 if (el->el_chared.c_vcmd.action & DELETE) {
214 /* vi_next_space_word():
215 * Vi move to the next space delimited word
218 protected el_action_t
220 vi_next_space_word(el, c)
224 if (el->el_line.cursor == el->el_line.lastchar)
227 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
228 el->el_line.lastchar,
229 el->el_state.argument,
232 if (el->el_map.type == MAP_VI)
233 if (el->el_chared.c_vcmd.action & DELETE) {
242 * Vi move to the next word
245 protected el_action_t
251 if (el->el_line.cursor == el->el_line.lastchar)
254 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
255 el->el_line.lastchar,
256 el->el_state.argument,
259 if (el->el_map.type == MAP_VI)
260 if (el->el_chared.c_vcmd.action & DELETE) {
271 * Vi change case of character under the cursor and advance one character
274 protected el_action_t
275 vi_change_case(el, c)
279 if (el->el_line.cursor < el->el_line.lastchar) {
280 c = (unsigned char)*el->el_line.cursor;
282 *el->el_line.cursor++ = tolower(c);
284 *el->el_line.cursor++ = toupper(c);
286 el->el_line.cursor++;
295 * Vi change prefix command
298 protected el_action_t
300 vi_change_meta(el, c)
305 * Delete with insert == change: first we delete and then we leave in
308 return cv_action(el, DELETE|INSERT);
312 /* vi_insert_at_bol():
313 * Vi enter insert mode at the beginning of line
316 protected el_action_t
318 vi_insert_at_bol(el, c)
322 el->el_line.cursor = el->el_line.buffer;
323 el->el_chared.c_vcmd.ins = el->el_line.cursor;
325 el->el_chared.c_undo.ptr = el->el_line.cursor;
326 el->el_chared.c_undo.action = DELETE;
328 el->el_map.current = el->el_map.key;
333 /* vi_replace_char():
334 * Vi replace character under the cursor with the next character typed
337 protected el_action_t
339 vi_replace_char(el, c)
343 el->el_map.current = el->el_map.key;
344 el->el_state.inputmode = MODE_REPLACE_1;
345 el->el_chared.c_undo.action = CHANGE;
346 el->el_chared.c_undo.ptr = el->el_line.cursor;
347 el->el_chared.c_undo.isize = 0;
348 el->el_chared.c_undo.dsize = 0;
353 /* vi_replace_mode():
354 * Vi enter replace mode
357 protected el_action_t
359 vi_replace_mode(el, c)
363 el->el_map.current = el->el_map.key;
364 el->el_state.inputmode = MODE_REPLACE;
365 el->el_chared.c_undo.action = CHANGE;
366 el->el_chared.c_undo.ptr = el->el_line.cursor;
367 el->el_chared.c_undo.isize = 0;
368 el->el_chared.c_undo.dsize = 0;
373 /* vi_substitute_char():
374 * Vi replace character under the cursor and enter insert mode
377 protected el_action_t
379 vi_substitute_char(el, c)
383 c_delafter(el, el->el_state.argument);
384 el->el_map.current = el->el_map.key;
389 /* vi_substitute_line():
390 * Vi substitute entire line
393 protected el_action_t
395 vi_substitute_line(el, c)
399 (void) em_kill_line(el, 0);
400 el->el_map.current = el->el_map.key;
405 /* vi_change_to_eol():
406 * Vi change to end of line
409 protected el_action_t
411 vi_change_to_eol(el, c)
415 (void) ed_kill_line(el, 0);
416 el->el_map.current = el->el_map.key;
422 * Vi enter insert mode
425 protected el_action_t
431 el->el_map.current = el->el_map.key;
433 el->el_chared.c_vcmd.ins = el->el_line.cursor;
434 el->el_chared.c_undo.ptr = el->el_line.cursor;
435 el->el_chared.c_undo.action = DELETE;
442 * Vi enter insert mode after the cursor
445 protected el_action_t
453 el->el_map.current = el->el_map.key;
454 if (el->el_line.cursor < el->el_line.lastchar) {
455 el->el_line.cursor++;
456 if (el->el_line.cursor > el->el_line.lastchar)
457 el->el_line.cursor = el->el_line.lastchar;
463 el->el_chared.c_vcmd.ins = el->el_line.cursor;
464 el->el_chared.c_undo.ptr = el->el_line.cursor;
465 el->el_chared.c_undo.action = DELETE;
472 * Vi enter insert mode at end of line
475 protected el_action_t
481 el->el_map.current = el->el_map.key;
482 el->el_line.cursor = el->el_line.lastchar;
484 /* Mark where insertion begins */
485 el->el_chared.c_vcmd.ins = el->el_line.lastchar;
486 el->el_chared.c_undo.ptr = el->el_line.lastchar;
487 el->el_chared.c_undo.action = DELETE;
493 * Vi delete prefix command
496 protected el_action_t
498 vi_delete_meta(el, c)
502 return cv_action(el, DELETE);
507 * Vi move to the end of the current space delimited word
510 protected el_action_t
516 if (el->el_line.cursor == el->el_line.lastchar)
519 el->el_line.cursor = cv__endword(el->el_line.cursor, el->el_line.lastchar,
520 el->el_state.argument);
522 if (el->el_chared.c_vcmd.action & DELETE) {
523 el->el_line.cursor++;
533 * Vi move to the end of the current word
536 protected el_action_t
538 vi_to_end_word(el, c)
542 if (el->el_line.cursor == el->el_line.lastchar)
545 el->el_line.cursor = cv__endword(el->el_line.cursor, el->el_line.lastchar,
546 el->el_state.argument);
548 if (el->el_chared.c_vcmd.action & DELETE) {
549 el->el_line.cursor++;
559 * Vi undo last change
562 protected el_action_t
571 c_undo_t *un = &el->el_chared.c_undo;
574 (void) fprintf(el->el_errfile, "Undo: %x \"%s\" +%d -%d\n",
575 un->action, un->buf, un->isize, un->dsize);
577 switch (un->action) {
582 (void) memcpy(un->buf, un->ptr, un->dsize);
583 for (cp = un->ptr; cp <= el->el_line.lastchar; cp++)
586 el->el_line.lastchar -= un->dsize;
587 el->el_line.cursor = un->ptr;
590 un->isize = un->dsize;
595 size = un->isize - un->dsize;
608 el->el_line.cursor = cp;
610 while (size-- > 0 && cp < el->el_line.lastchar) {
618 for (; cp <= el->el_line.lastchar; cp++) {
622 el->el_line.lastchar -= size;
624 el->el_line.cursor = un->ptr;
626 un->dsize = un->isize;
634 el->el_line.cursor = un->ptr;
635 c_insert(el, un->isize);
636 memcpy(un->ptr, un->buf, un->isize);
638 un->dsize = un->isize;
646 el->el_line.cursor = un->ptr;
647 size = (int) (el->el_line.cursor - el->el_line.lastchar);
648 if (size < un->isize)
652 for(i = 0; i < size; i++) {
669 * Vi undo all changes
672 protected el_action_t
683 /* vi_command_mode():
684 * Vi enter command mode (use alternative key bindings)
687 protected el_action_t
689 vi_command_mode(el, c)
694 /* [Esc] cancels pending action */
695 el->el_chared.c_vcmd.ins = 0;
696 el->el_chared.c_vcmd.action = NOP;
697 el->el_chared.c_vcmd.pos = 0;
699 el->el_state.doingarg = 0;
700 size = el->el_chared.c_undo.ptr - el->el_line.cursor;
703 if (el->el_chared.c_undo.action == (INSERT|DELETE) ||
704 el->el_chared.c_undo.action == DELETE)
705 el->el_chared.c_undo.dsize = size;
707 el->el_chared.c_undo.isize = size;
709 el->el_state.inputmode = MODE_INSERT;
710 el->el_map.current = el->el_map.alt;
712 if (el->el_line.cursor > el->el_line.buffer)
713 el->el_line.cursor--;
719 * Vi move to the beginning of line
722 protected el_action_t
727 if (el->el_state.doingarg) {
728 if (el->el_state.argument > 1000000)
730 el->el_state.argument =
731 (el->el_state.argument * 10) + (c - '0');
735 el->el_line.cursor = el->el_line.buffer;
736 if (el->el_chared.c_vcmd.action & DELETE) {
745 /* vi_delete_prev_char():
746 * Vi move to previous character (backspace)
749 protected el_action_t
751 vi_delete_prev_char(el, c)
755 if (el->el_chared.c_vcmd.ins == 0)
758 if (el->el_chared.c_vcmd.ins >
759 el->el_line.cursor - el->el_state.argument)
762 c_delbefore(el, el->el_state.argument);
763 el->el_line.cursor -= el->el_state.argument;
766 } /* end v_del_char_prev */
770 * Vi list choices for completion or indicate end of file if empty line
773 protected el_action_t
775 vi_list_or_eof(el, c)
780 if (el->el_line.cursor == el->el_line.lastchar &&
781 el->el_line.cursor == el->el_line.buffer) {
783 term_overwrite(el, STReof, 4); /* then do a EOF */
790 *el->el_line.lastchar = '\0'; /* just in case */
791 return CC_LIST_CHOICES;
797 /* vi_kill_line_prev():
798 * Vi cut from beginning of line to cursor
801 protected el_action_t
803 vi_kill_line_prev(el, c)
809 cp = el->el_line.buffer;
810 kp = el->el_chared.c_kill.buf;
811 while (cp < el->el_line.cursor)
812 *kp++ = *cp++; /* copy it */
813 el->el_chared.c_kill.last = kp;
814 c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
815 el->el_line.cursor = el->el_line.buffer; /* zap! */
821 * Vi search history previous
824 protected el_action_t
826 vi_search_prev(el, c)
830 return cv_search(el, ED_SEARCH_PREV_HISTORY);
835 * Vi search history next
838 protected el_action_t
840 vi_search_next(el, c)
844 return cv_search(el, ED_SEARCH_NEXT_HISTORY);
848 /* vi_repeat_search_next():
849 * Vi repeat current search in the same search direction
852 protected el_action_t
854 vi_repeat_search_next(el, c)
858 if (el->el_search.patlen == 0)
861 return cv_repeat_srch(el, el->el_search.patdir);
865 /* vi_repeat_search_prev():
866 * Vi repeat current search in the opposite search direction
870 protected el_action_t
871 vi_repeat_search_prev(el, c)
875 if (el->el_search.patlen == 0)
878 return cv_repeat_srch(el,
879 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
880 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY);
885 * Vi move to the character specified next
888 protected el_action_t
896 if (el_getc(el, &ch) != 1)
897 return ed_end_of_file(el, 0);
899 el->el_search.chadir = CHAR_FWD;
900 el->el_search.chacha = ch;
902 return cv_csearch_fwd(el, ch, el->el_state.argument, 0);
908 * Vi move to the character specified previous
911 protected el_action_t
919 if (el_getc(el, &ch) != 1)
920 return ed_end_of_file(el, 0);
922 el->el_search.chadir = CHAR_BACK;
923 el->el_search.chacha = ch;
925 return cv_csearch_back(el, ch, el->el_state.argument, 0);
929 /* vi_to_next_char():
930 * Vi move up to the character specified next
933 protected el_action_t
935 vi_to_next_char(el, c)
941 if (el_getc(el, &ch) != 1)
942 return ed_end_of_file(el, 0);
944 return cv_csearch_fwd(el, ch, el->el_state.argument, 1);
949 /* vi_to_prev_char():
950 * Vi move up to the character specified previous
953 protected el_action_t
955 vi_to_prev_char(el, c)
960 if (el_getc(el, &ch) != 1)
961 return ed_end_of_file(el, 0);
963 return cv_csearch_back(el, ch, el->el_state.argument, 1);
967 /* vi_repeat_next_char():
968 * Vi repeat current character search in the same search direction
971 protected el_action_t
973 vi_repeat_next_char(el, c)
977 if (el->el_search.chacha == 0)
980 return el->el_search.chadir == CHAR_FWD ?
981 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
982 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);
986 /* vi_repeat_prev_char():
987 * Vi repeat current character search in the opposite search direction
990 protected el_action_t
992 vi_repeat_prev_char(el, c)
996 if (el->el_search.chacha == 0)
999 return el->el_search.chadir == CHAR_BACK ?
1000 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
1001 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);