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 * @(#)vi.c 8.1 (Berkeley) 6/4/93
40 * vi.c: Vi mode commands.
45 private el_action_t cv_action __P((EditLine *, int));
55 register char *cp, *kp;
57 if (el->el_chared.c_vcmd.action & DELETE) {
58 el->el_chared.c_vcmd.action = NOP;
59 el->el_chared.c_vcmd.pos = 0;
61 el->el_chared.c_undo.isize = 0;
62 el->el_chared.c_undo.dsize = 0;
63 kp = el->el_chared.c_undo.buf;
64 for (cp = el->el_line.buffer; cp < el->el_line.lastchar; cp++) {
66 el->el_chared.c_undo.dsize++;
69 el->el_chared.c_undo.action = INSERT;
70 el->el_chared.c_undo.ptr = el->el_line.buffer;
71 el->el_line.lastchar = el->el_line.buffer;
72 el->el_line.cursor = el->el_line.buffer;
74 el->el_map.current = el->el_map.key;
79 el->el_chared.c_vcmd.pos = el->el_line.cursor;
80 el->el_chared.c_vcmd.action = c;
85 * I don't think that this is needed. But we keep it for now
87 else if (el_chared.c_vcmd.action == NOP) {
88 el->el_chared.c_vcmd.pos = el->el_line.cursor;
89 el->el_chared.c_vcmd.action = c;
93 el->el_chared.c_vcmd.action = 0;
94 el->el_chared.c_vcmd.pos = 0;
102 * Paste previous deletion before or after the cursor
104 protected el_action_t
110 c_undo_t *un = &el->el_chared.c_undo;
112 (void) fprintf(el->el_errfile, "Paste: %x \"%s\" +%d -%d\n",
113 un->action, un->buf, un->isize, un->dsize);
118 if (!c && el->el_line.cursor < el->el_line.lastchar)
119 el->el_line.cursor++;
120 ptr = el->el_line.cursor;
122 c_insert(el, un->isize);
123 if (el->el_line.cursor + un->isize > el->el_line.lastchar)
125 (void) memcpy(ptr, un->buf, un->isize);
131 * Vi paste previous deletion to the right of the cursor
134 protected el_action_t
140 return cv_paste(el, 0);
145 * Vi paste previous deletion to the left of the cursor
148 protected el_action_t
154 return cv_paste(el, 1);
158 /* vi_prev_space_word():
159 * Vi move to the previous space delimited word
162 protected el_action_t
164 vi_prev_space_word(el, 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) {
186 * Vi move to the previous word
189 protected el_action_t
195 if (el->el_line.cursor == el->el_line.buffer)
198 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
200 el->el_state.argument,
203 if (el->el_chared.c_vcmd.action & DELETE) {
212 /* vi_next_space_word():
213 * Vi move to the next space delimited word
216 protected el_action_t
218 vi_next_space_word(el, c)
222 if (el->el_line.cursor == el->el_line.lastchar)
225 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
226 el->el_line.lastchar,
227 el->el_state.argument,
230 if (el->el_map.type == MAP_VI)
231 if (el->el_chared.c_vcmd.action & DELETE) {
240 * Vi move to the next word
243 protected el_action_t
249 if (el->el_line.cursor == el->el_line.lastchar)
252 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
253 el->el_line.lastchar,
254 el->el_state.argument,
257 if (el->el_map.type == MAP_VI)
258 if (el->el_chared.c_vcmd.action & DELETE) {
269 * Vi change case of character under the cursor and advance one character
272 protected el_action_t
273 vi_change_case(el, c)
277 if (el->el_line.cursor < el->el_line.lastchar) {
278 c = (unsigned char)*el->el_line.cursor;
280 *el->el_line.cursor++ = tolower(c);
282 *el->el_line.cursor++ = toupper(c);
284 el->el_line.cursor++;
293 * Vi change prefix command
296 protected el_action_t
298 vi_change_meta(el, c)
303 * Delete with insert == change: first we delete and then we leave in
306 return cv_action(el, DELETE|INSERT);
310 /* vi_insert_at_bol():
311 * Vi enter insert mode at the beginning of line
314 protected el_action_t
316 vi_insert_at_bol(el, c)
320 el->el_line.cursor = el->el_line.buffer;
321 el->el_chared.c_vcmd.ins = el->el_line.cursor;
323 el->el_chared.c_undo.ptr = el->el_line.cursor;
324 el->el_chared.c_undo.action = DELETE;
326 el->el_map.current = el->el_map.key;
331 /* vi_replace_char():
332 * Vi replace character under the cursor with the next character typed
335 protected el_action_t
337 vi_replace_char(el, c)
341 el->el_map.current = el->el_map.key;
342 el->el_state.inputmode = MODE_REPLACE_1;
343 el->el_chared.c_undo.action = CHANGE;
344 el->el_chared.c_undo.ptr = el->el_line.cursor;
345 el->el_chared.c_undo.isize = 0;
346 el->el_chared.c_undo.dsize = 0;
351 /* vi_replace_mode():
352 * Vi enter replace mode
355 protected el_action_t
357 vi_replace_mode(el, c)
361 el->el_map.current = el->el_map.key;
362 el->el_state.inputmode = MODE_REPLACE;
363 el->el_chared.c_undo.action = CHANGE;
364 el->el_chared.c_undo.ptr = el->el_line.cursor;
365 el->el_chared.c_undo.isize = 0;
366 el->el_chared.c_undo.dsize = 0;
371 /* vi_substitute_char():
372 * Vi replace character under the cursor and enter insert mode
375 protected el_action_t
377 vi_substitute_char(el, c)
381 c_delafter(el, el->el_state.argument);
382 el->el_map.current = el->el_map.key;
387 /* vi_substitute_line():
388 * Vi substitute entire line
391 protected el_action_t
393 vi_substitute_line(el, c)
397 (void) em_kill_line(el, 0);
398 el->el_map.current = el->el_map.key;
403 /* vi_change_to_eol():
404 * Vi change to end of line
407 protected el_action_t
409 vi_change_to_eol(el, c)
413 (void) ed_kill_line(el, 0);
414 el->el_map.current = el->el_map.key;
420 * Vi enter insert mode
423 protected el_action_t
429 el->el_map.current = el->el_map.key;
431 el->el_chared.c_vcmd.ins = el->el_line.cursor;
432 el->el_chared.c_undo.ptr = el->el_line.cursor;
433 el->el_chared.c_undo.action = DELETE;
440 * Vi enter insert mode after the cursor
443 protected el_action_t
451 el->el_map.current = el->el_map.key;
452 if (el->el_line.cursor < el->el_line.lastchar) {
453 el->el_line.cursor++;
454 if (el->el_line.cursor > el->el_line.lastchar)
455 el->el_line.cursor = el->el_line.lastchar;
461 el->el_chared.c_vcmd.ins = el->el_line.cursor;
462 el->el_chared.c_undo.ptr = el->el_line.cursor;
463 el->el_chared.c_undo.action = DELETE;
470 * Vi enter insert mode at end of line
473 protected el_action_t
479 el->el_map.current = el->el_map.key;
480 el->el_line.cursor = el->el_line.lastchar;
482 /* Mark where insertion begins */
483 el->el_chared.c_vcmd.ins = el->el_line.lastchar;
484 el->el_chared.c_undo.ptr = el->el_line.lastchar;
485 el->el_chared.c_undo.action = DELETE;
491 * Vi delete prefix command
494 protected el_action_t
496 vi_delete_meta(el, c)
500 return cv_action(el, DELETE);
505 * Vi move to the end of the current space delimited word
508 protected el_action_t
514 if (el->el_line.cursor == el->el_line.lastchar)
517 el->el_line.cursor = cv__endword(el->el_line.cursor, el->el_line.lastchar,
518 el->el_state.argument);
520 if (el->el_chared.c_vcmd.action & DELETE) {
521 el->el_line.cursor++;
531 * Vi move to the end of the current word
534 protected el_action_t
536 vi_to_end_word(el, c)
540 if (el->el_line.cursor == el->el_line.lastchar)
543 el->el_line.cursor = cv__endword(el->el_line.cursor, el->el_line.lastchar,
544 el->el_state.argument);
546 if (el->el_chared.c_vcmd.action & DELETE) {
547 el->el_line.cursor++;
557 * Vi undo last change
560 protected el_action_t
569 c_undo_t *un = &el->el_chared.c_undo;
572 (void) fprintf(el->el_errfile, "Undo: %x \"%s\" +%d -%d\n",
573 un->action, un->buf, un->isize, un->dsize);
575 switch (un->action) {
580 (void) memcpy(un->buf, un->ptr, un->dsize);
581 for (cp = un->ptr; cp <= el->el_line.lastchar; cp++)
584 el->el_line.lastchar -= un->dsize;
585 el->el_line.cursor = un->ptr;
588 un->isize = un->dsize;
593 size = un->isize - un->dsize;
606 el->el_line.cursor = cp;
608 while (size-- > 0 && cp < el->el_line.lastchar) {
616 for (; cp <= el->el_line.lastchar; cp++) {
620 el->el_line.lastchar -= size;
622 el->el_line.cursor = un->ptr;
624 un->dsize = un->isize;
632 el->el_line.cursor = un->ptr;
633 c_insert(el, un->isize);
634 memcpy(un->ptr, un->buf, un->isize);
636 un->dsize = un->isize;
644 el->el_line.cursor = un->ptr;
645 size = (int) (el->el_line.cursor - el->el_line.lastchar);
646 if (size < un->isize)
650 for(i = 0; i < size; i++) {
667 * Vi undo all changes
670 protected el_action_t
681 /* vi_command_mode():
682 * Vi enter command mode (use alternative key bindings)
685 protected el_action_t
687 vi_command_mode(el, c)
692 /* [Esc] cancels pending action */
693 el->el_chared.c_vcmd.ins = 0;
694 el->el_chared.c_vcmd.action = NOP;
695 el->el_chared.c_vcmd.pos = 0;
697 el->el_state.doingarg = 0;
698 size = el->el_chared.c_undo.ptr - el->el_line.cursor;
701 if (el->el_chared.c_undo.action == (INSERT|DELETE) ||
702 el->el_chared.c_undo.action == DELETE)
703 el->el_chared.c_undo.dsize = size;
705 el->el_chared.c_undo.isize = size;
707 el->el_state.inputmode = MODE_INSERT;
708 el->el_map.current = el->el_map.alt;
710 if (el->el_line.cursor > el->el_line.buffer)
711 el->el_line.cursor--;
717 * Vi move to the beginning of line
720 protected el_action_t
725 if (el->el_state.doingarg) {
726 if (el->el_state.argument > 1000000)
728 el->el_state.argument =
729 (el->el_state.argument * 10) + (c - '0');
733 el->el_line.cursor = el->el_line.buffer;
734 if (el->el_chared.c_vcmd.action & DELETE) {
743 /* vi_delete_prev_char():
744 * Vi move to previous character (backspace)
747 protected el_action_t
749 vi_delete_prev_char(el, c)
753 if (el->el_chared.c_vcmd.ins == 0)
756 if (el->el_chared.c_vcmd.ins >
757 el->el_line.cursor - el->el_state.argument)
760 c_delbefore(el, el->el_state.argument);
761 el->el_line.cursor -= el->el_state.argument;
764 } /* end v_del_char_prev */
768 * Vi list choices for completion or indicate end of file if empty line
771 protected el_action_t
773 vi_list_or_eof(el, c)
778 if (el->el_line.cursor == el->el_line.lastchar &&
779 el->el_line.cursor == el->el_line.buffer) {
781 term_overwrite(el, STReof, 4); /* then do a EOF */
788 *el->el_line.lastchar = '\0'; /* just in case */
789 return CC_LIST_CHOICES;
795 /* vi_kill_line_prev():
796 * Vi cut from beginning of line to cursor
799 protected el_action_t
801 vi_kill_line_prev(el, c)
807 cp = el->el_line.buffer;
808 kp = el->el_chared.c_kill.buf;
809 while (cp < el->el_line.cursor)
810 *kp++ = *cp++; /* copy it */
811 el->el_chared.c_kill.last = kp;
812 c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
813 el->el_line.cursor = el->el_line.buffer; /* zap! */
819 * Vi search history previous
822 protected el_action_t
824 vi_search_prev(el, c)
828 return cv_search(el, ED_SEARCH_PREV_HISTORY);
833 * Vi search history next
836 protected el_action_t
838 vi_search_next(el, c)
842 return cv_search(el, ED_SEARCH_NEXT_HISTORY);
846 /* vi_repeat_search_next():
847 * Vi repeat current search in the same search direction
850 protected el_action_t
852 vi_repeat_search_next(el, c)
856 if (el->el_search.patlen == 0)
859 return cv_repeat_srch(el, el->el_search.patdir);
863 /* vi_repeat_search_prev():
864 * Vi repeat current search in the opposite search direction
868 protected el_action_t
869 vi_repeat_search_prev(el, c)
873 if (el->el_search.patlen == 0)
876 return cv_repeat_srch(el,
877 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
878 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY);
883 * Vi move to the character specified next
886 protected el_action_t
894 if (el_getc(el, &ch) != 1)
895 return ed_end_of_file(el, 0);
897 el->el_search.chadir = CHAR_FWD;
898 el->el_search.chacha = ch;
900 return cv_csearch_fwd(el, ch, el->el_state.argument, 0);
906 * Vi move to the character specified previous
909 protected el_action_t
917 if (el_getc(el, &ch) != 1)
918 return ed_end_of_file(el, 0);
920 el->el_search.chadir = CHAR_BACK;
921 el->el_search.chacha = ch;
923 return cv_csearch_back(el, ch, el->el_state.argument, 0);
927 /* vi_to_next_char():
928 * Vi move up to the character specified next
931 protected el_action_t
933 vi_to_next_char(el, c)
939 if (el_getc(el, &ch) != 1)
940 return ed_end_of_file(el, 0);
942 return cv_csearch_fwd(el, ch, el->el_state.argument, 1);
947 /* vi_to_prev_char():
948 * Vi move up to the character specified previous
951 protected el_action_t
953 vi_to_prev_char(el, c)
958 if (el_getc(el, &ch) != 1)
959 return ed_end_of_file(el, 0);
961 return cv_csearch_back(el, ch, el->el_state.argument, 1);
965 /* vi_repeat_next_char():
966 * Vi repeat current character search in the same search direction
969 protected el_action_t
971 vi_repeat_next_char(el, c)
975 if (el->el_search.chacha == 0)
978 return el->el_search.chadir == CHAR_FWD ?
979 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
980 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);
984 /* vi_repeat_prev_char():
985 * Vi repeat current character search in the opposite search direction
988 protected el_action_t
990 vi_repeat_prev_char(el, c)
994 if (el->el_search.chacha == 0)
997 return el->el_search.chadir == CHAR_BACK ?
998 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
999 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);