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
37 * $DragonFly: src/lib/libedit/vi.c,v 1.5 2004/10/25 19:38:45 drhodus Exp $
41 * vi.c: Vi mode commands.
46 private el_action_t cv_action (EditLine *, int);
58 if (el->el_chared.c_vcmd.action & DELETE) {
59 el->el_chared.c_vcmd.action = NOP;
60 el->el_chared.c_vcmd.pos = 0;
62 el->el_chared.c_undo.isize = 0;
63 el->el_chared.c_undo.dsize = 0;
64 kp = el->el_chared.c_undo.buf;
65 for (cp = el->el_line.buffer; cp < el->el_line.lastchar; cp++) {
67 el->el_chared.c_undo.dsize++;
70 el->el_chared.c_undo.action = INSERT;
71 el->el_chared.c_undo.ptr = el->el_line.buffer;
72 el->el_line.lastchar = el->el_line.buffer;
73 el->el_line.cursor = el->el_line.buffer;
75 el->el_map.current = el->el_map.key;
80 el->el_chared.c_vcmd.pos = el->el_line.cursor;
81 el->el_chared.c_vcmd.action = c;
86 * I don't think that this is needed. But we keep it for now
88 else if (el_chared.c_vcmd.action == NOP) {
89 el->el_chared.c_vcmd.pos = el->el_line.cursor;
90 el->el_chared.c_vcmd.action = c;
94 el->el_chared.c_vcmd.action = 0;
95 el->el_chared.c_vcmd.pos = 0;
103 * Paste previous deletion before or after the cursor
105 protected el_action_t
111 c_undo_t *un = &el->el_chared.c_undo;
113 (void) fprintf(el->el_errfile, "Paste: %x \"%s\" +%d -%d\n",
114 un->action, un->buf, un->isize, un->dsize);
119 if (!c && el->el_line.cursor < el->el_line.lastchar)
120 el->el_line.cursor++;
121 ptr = el->el_line.cursor;
123 c_insert(el, un->isize);
124 if (el->el_line.cursor + un->isize > el->el_line.lastchar)
126 (void) memcpy(ptr, un->buf, un->isize);
132 * Vi paste previous deletion to the right of the cursor
135 protected el_action_t
141 return cv_paste(el, 0);
146 * Vi paste previous deletion to the left of the cursor
149 protected el_action_t
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(el, c)
169 if (el->el_line.cursor == el->el_line.buffer)
172 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
174 el->el_state.argument,
177 if (el->el_chared.c_vcmd.action & DELETE) {
187 * Vi move to the previous word
190 protected el_action_t
196 if (el->el_line.cursor == el->el_line.buffer)
199 el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
201 el->el_state.argument,
204 if (el->el_chared.c_vcmd.action & DELETE) {
213 /* vi_next_space_word():
214 * Vi move to the next space delimited word
217 protected el_action_t
219 vi_next_space_word(el, c)
223 if (el->el_line.cursor == el->el_line.lastchar)
226 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
227 el->el_line.lastchar,
228 el->el_state.argument,
231 if (el->el_map.type == MAP_VI)
232 if (el->el_chared.c_vcmd.action & DELETE) {
241 * Vi move to the next word
244 protected el_action_t
250 if (el->el_line.cursor == el->el_line.lastchar)
253 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
254 el->el_line.lastchar,
255 el->el_state.argument,
258 if (el->el_map.type == MAP_VI)
259 if (el->el_chared.c_vcmd.action & DELETE) {
270 * Vi change case of character under the cursor and advance one character
273 protected el_action_t
274 vi_change_case(el, c)
278 if (el->el_line.cursor < el->el_line.lastchar) {
279 c = (unsigned char)*el->el_line.cursor;
281 *el->el_line.cursor++ = tolower(c);
283 *el->el_line.cursor++ = toupper(c);
285 el->el_line.cursor++;
294 * Vi change prefix command
297 protected el_action_t
299 vi_change_meta(el, c)
304 * Delete with insert == change: first we delete and then we leave in
307 return cv_action(el, DELETE|INSERT);
311 /* vi_insert_at_bol():
312 * Vi enter insert mode at the beginning of line
315 protected el_action_t
317 vi_insert_at_bol(el, c)
321 el->el_line.cursor = el->el_line.buffer;
322 el->el_chared.c_vcmd.ins = el->el_line.cursor;
324 el->el_chared.c_undo.ptr = el->el_line.cursor;
325 el->el_chared.c_undo.action = DELETE;
327 el->el_map.current = el->el_map.key;
332 /* vi_replace_char():
333 * Vi replace character under the cursor with the next character typed
336 protected el_action_t
338 vi_replace_char(el, c)
342 el->el_map.current = el->el_map.key;
343 el->el_state.inputmode = MODE_REPLACE_1;
344 el->el_chared.c_undo.action = CHANGE;
345 el->el_chared.c_undo.ptr = el->el_line.cursor;
346 el->el_chared.c_undo.isize = 0;
347 el->el_chared.c_undo.dsize = 0;
352 /* vi_replace_mode():
353 * Vi enter replace mode
356 protected el_action_t
358 vi_replace_mode(el, c)
362 el->el_map.current = el->el_map.key;
363 el->el_state.inputmode = MODE_REPLACE;
364 el->el_chared.c_undo.action = CHANGE;
365 el->el_chared.c_undo.ptr = el->el_line.cursor;
366 el->el_chared.c_undo.isize = 0;
367 el->el_chared.c_undo.dsize = 0;
372 /* vi_substitute_char():
373 * Vi replace character under the cursor and enter insert mode
376 protected el_action_t
378 vi_substitute_char(el, c)
382 c_delafter(el, el->el_state.argument);
383 el->el_map.current = el->el_map.key;
388 /* vi_substitute_line():
389 * Vi substitute entire line
392 protected el_action_t
394 vi_substitute_line(el, c)
398 (void) em_kill_line(el, 0);
399 el->el_map.current = el->el_map.key;
404 /* vi_change_to_eol():
405 * Vi change to end of line
408 protected el_action_t
410 vi_change_to_eol(el, c)
414 (void) ed_kill_line(el, 0);
415 el->el_map.current = el->el_map.key;
421 * Vi enter insert mode
424 protected el_action_t
430 el->el_map.current = el->el_map.key;
432 el->el_chared.c_vcmd.ins = el->el_line.cursor;
433 el->el_chared.c_undo.ptr = el->el_line.cursor;
434 el->el_chared.c_undo.action = DELETE;
441 * Vi enter insert mode after the cursor
444 protected el_action_t
452 el->el_map.current = el->el_map.key;
453 if (el->el_line.cursor < el->el_line.lastchar) {
454 el->el_line.cursor++;
455 if (el->el_line.cursor > el->el_line.lastchar)
456 el->el_line.cursor = el->el_line.lastchar;
462 el->el_chared.c_vcmd.ins = el->el_line.cursor;
463 el->el_chared.c_undo.ptr = el->el_line.cursor;
464 el->el_chared.c_undo.action = DELETE;
471 * Vi enter insert mode at end of line
474 protected el_action_t
480 el->el_map.current = el->el_map.key;
481 el->el_line.cursor = el->el_line.lastchar;
483 /* Mark where insertion begins */
484 el->el_chared.c_vcmd.ins = el->el_line.lastchar;
485 el->el_chared.c_undo.ptr = el->el_line.lastchar;
486 el->el_chared.c_undo.action = DELETE;
492 * Vi delete prefix command
495 protected el_action_t
497 vi_delete_meta(el, c)
501 return cv_action(el, DELETE);
506 * Vi move to the end of the current space delimited word
509 protected el_action_t
515 if (el->el_line.cursor == el->el_line.lastchar)
518 el->el_line.cursor = cv__endword(el->el_line.cursor, el->el_line.lastchar,
519 el->el_state.argument);
521 if (el->el_chared.c_vcmd.action & DELETE) {
522 el->el_line.cursor++;
532 * Vi move to the end of the current word
535 protected el_action_t
537 vi_to_end_word(el, c)
541 if (el->el_line.cursor == el->el_line.lastchar)
544 el->el_line.cursor = cv__endword(el->el_line.cursor, el->el_line.lastchar,
545 el->el_state.argument);
547 if (el->el_chared.c_vcmd.action & DELETE) {
548 el->el_line.cursor++;
558 * Vi undo last change
561 protected el_action_t
570 c_undo_t *un = &el->el_chared.c_undo;
573 (void) fprintf(el->el_errfile, "Undo: %x \"%s\" +%d -%d\n",
574 un->action, un->buf, un->isize, un->dsize);
576 switch (un->action) {
581 (void) memcpy(un->buf, un->ptr, un->dsize);
582 for (cp = un->ptr; cp <= el->el_line.lastchar; cp++)
585 el->el_line.lastchar -= un->dsize;
586 el->el_line.cursor = un->ptr;
589 un->isize = un->dsize;
594 size = un->isize - un->dsize;
607 el->el_line.cursor = cp;
609 while (size-- > 0 && cp < el->el_line.lastchar) {
617 for (; cp <= el->el_line.lastchar; cp++) {
621 el->el_line.lastchar -= size;
623 el->el_line.cursor = un->ptr;
625 un->dsize = un->isize;
633 el->el_line.cursor = un->ptr;
634 c_insert(el, un->isize);
635 memcpy(un->ptr, un->buf, un->isize);
637 un->dsize = un->isize;
645 el->el_line.cursor = un->ptr;
646 size = (int) (el->el_line.cursor - el->el_line.lastchar);
647 if (size < un->isize)
651 for(i = 0; i < size; i++) {
668 * Vi undo all changes
671 protected el_action_t
682 /* vi_command_mode():
683 * Vi enter command mode (use alternative key bindings)
686 protected el_action_t
688 vi_command_mode(el, c)
693 /* [Esc] cancels pending action */
694 el->el_chared.c_vcmd.ins = 0;
695 el->el_chared.c_vcmd.action = NOP;
696 el->el_chared.c_vcmd.pos = 0;
698 el->el_state.doingarg = 0;
699 size = el->el_chared.c_undo.ptr - el->el_line.cursor;
702 if (el->el_chared.c_undo.action == (INSERT|DELETE) ||
703 el->el_chared.c_undo.action == DELETE)
704 el->el_chared.c_undo.dsize = size;
706 el->el_chared.c_undo.isize = size;
708 el->el_state.inputmode = MODE_INSERT;
709 el->el_map.current = el->el_map.alt;
711 if (el->el_line.cursor > el->el_line.buffer)
712 el->el_line.cursor--;
718 * Vi move to the beginning of line
721 protected el_action_t
726 if (el->el_state.doingarg) {
727 if (el->el_state.argument > 1000000)
729 el->el_state.argument =
730 (el->el_state.argument * 10) + (c - '0');
734 el->el_line.cursor = el->el_line.buffer;
735 if (el->el_chared.c_vcmd.action & DELETE) {
744 /* vi_delete_prev_char():
745 * Vi move to previous character (backspace)
748 protected el_action_t
750 vi_delete_prev_char(el, c)
754 if (el->el_chared.c_vcmd.ins == 0)
757 if (el->el_chared.c_vcmd.ins >
758 el->el_line.cursor - el->el_state.argument)
761 c_delbefore(el, el->el_state.argument);
762 el->el_line.cursor -= el->el_state.argument;
765 } /* end v_del_char_prev */
769 * Vi list choices for completion or indicate end of file if empty line
772 protected el_action_t
774 vi_list_or_eof(el, c)
779 if (el->el_line.cursor == el->el_line.lastchar &&
780 el->el_line.cursor == el->el_line.buffer) {
782 term_overwrite(el, STReof, 4); /* then do a EOF */
789 *el->el_line.lastchar = '\0'; /* just in case */
790 return CC_LIST_CHOICES;
796 /* vi_kill_line_prev():
797 * Vi cut from beginning of line to cursor
800 protected el_action_t
802 vi_kill_line_prev(el, c)
808 cp = el->el_line.buffer;
809 kp = el->el_chared.c_kill.buf;
810 while (cp < el->el_line.cursor)
811 *kp++ = *cp++; /* copy it */
812 el->el_chared.c_kill.last = kp;
813 c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
814 el->el_line.cursor = el->el_line.buffer; /* zap! */
820 * Vi search history previous
823 protected el_action_t
825 vi_search_prev(el, c)
829 return cv_search(el, ED_SEARCH_PREV_HISTORY);
834 * Vi search history next
837 protected el_action_t
839 vi_search_next(el, c)
843 return cv_search(el, ED_SEARCH_NEXT_HISTORY);
847 /* vi_repeat_search_next():
848 * Vi repeat current search in the same search direction
851 protected el_action_t
853 vi_repeat_search_next(el, c)
857 if (el->el_search.patlen == 0)
860 return cv_repeat_srch(el, el->el_search.patdir);
864 /* vi_repeat_search_prev():
865 * Vi repeat current search in the opposite search direction
869 protected el_action_t
870 vi_repeat_search_prev(el, c)
874 if (el->el_search.patlen == 0)
877 return cv_repeat_srch(el,
878 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
879 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY);
884 * Vi move to the character specified next
887 protected el_action_t
895 if (el_getc(el, &ch) != 1)
896 return ed_end_of_file(el, 0);
898 el->el_search.chadir = CHAR_FWD;
899 el->el_search.chacha = ch;
901 return cv_csearch_fwd(el, ch, el->el_state.argument, 0);
907 * Vi move to the character specified previous
910 protected el_action_t
918 if (el_getc(el, &ch) != 1)
919 return ed_end_of_file(el, 0);
921 el->el_search.chadir = CHAR_BACK;
922 el->el_search.chacha = ch;
924 return cv_csearch_back(el, ch, el->el_state.argument, 0);
928 /* vi_to_next_char():
929 * Vi move up to the character specified next
932 protected el_action_t
934 vi_to_next_char(el, c)
940 if (el_getc(el, &ch) != 1)
941 return ed_end_of_file(el, 0);
943 return cv_csearch_fwd(el, ch, el->el_state.argument, 1);
948 /* vi_to_prev_char():
949 * Vi move up to the character specified previous
952 protected el_action_t
954 vi_to_prev_char(el, c)
959 if (el_getc(el, &ch) != 1)
960 return ed_end_of_file(el, 0);
962 return cv_csearch_back(el, ch, el->el_state.argument, 1);
966 /* vi_repeat_next_char():
967 * Vi repeat current character search in the same search direction
970 protected el_action_t
972 vi_repeat_next_char(el, c)
976 if (el->el_search.chacha == 0)
979 return el->el_search.chadir == CHAR_FWD ?
980 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
981 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);
985 /* vi_repeat_prev_char():
986 * Vi repeat current character search in the opposite search direction
989 protected el_action_t
991 vi_repeat_prev_char(el, c)
995 if (el->el_search.chacha == 0)
998 return el->el_search.chadir == CHAR_BACK ?
999 cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
1000 cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);