1 /* vi_mode.c -- A vi emulation mode for Bash.
2 Derived from code written by Jeff Sparkes (jsparkes@bnr.ca). */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 /* **************************************************************** */
27 /* VI Emulation Mode */
29 /* **************************************************************** */
34 #if defined (HAVE_CONFIG_H)
38 #include <sys/types.h>
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_UNISTD_H)
52 /* Some standard library routines. */
59 #include "rlprivate.h"
63 #define member(c, s) ((c) ? (char *)strchr ((s), (c)) != (char *)NULL : 0)
66 /* Non-zero means enter insertion mode. */
67 static int _rl_vi_doing_insert;
69 /* Command keys which do movement for xxx_to commands. */
70 static const char *vi_motion = " hl^$0ftFT;,%wbeWBE|";
72 /* Keymap used for vi replace characters. Created dynamically since
74 static Keymap vi_replace_map;
76 /* The number of characters inserted in the last replace operation. */
77 static int vi_replace_count;
79 /* If non-zero, we have text inserted after a c[motion] command that put
80 us implicitly into insert mode. Some people want this text to be
81 attached to the command so that it is `redoable' with `.'. */
82 static int vi_continued_command;
83 static char *vi_insert_buffer;
84 static int vi_insert_buffer_size;
86 static int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */
87 static int _rl_vi_last_repeat = 1;
88 static int _rl_vi_last_arg_sign = 1;
89 static int _rl_vi_last_motion;
90 #if defined (HANDLE_MULTIBYTE)
91 static char _rl_vi_last_search_mbchar[MB_LEN_MAX];
93 static int _rl_vi_last_search_char;
95 static int _rl_vi_last_replacement;
97 static int _rl_vi_last_key_before_insert;
99 static int vi_redoing;
101 /* Text modification commands. These are the `redoable' commands. */
102 static const char *vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
104 /* Arrays for the saved marks. */
105 static int vi_mark_chars['z' - 'a' + 1];
107 static void _rl_vi_stuff_insert PARAMS((int));
108 static void _rl_vi_save_insert PARAMS((UNDO_LIST *));
109 static int rl_digit_loop1 PARAMS((void));
112 _rl_vi_initialize_line ()
116 for (i = 0; i < sizeof (vi_mark_chars) / sizeof (int); i++)
117 vi_mark_chars[i] = -1;
123 _rl_vi_last_command = 'i';
124 _rl_vi_last_repeat = 1;
125 _rl_vi_last_arg_sign = 1;
126 _rl_vi_last_motion = 0;
130 _rl_vi_set_last (key, repeat, sign)
131 int key, repeat, sign;
133 _rl_vi_last_command = key;
134 _rl_vi_last_repeat = repeat;
135 _rl_vi_last_arg_sign = sign;
138 /* Is the command C a VI mode text modification command? */
140 _rl_vi_textmod_command (c)
143 return (member (c, vi_textmod));
147 _rl_vi_stuff_insert (count)
150 rl_begin_undo_group ();
152 rl_insert_text (vi_insert_buffer);
153 rl_end_undo_group ();
156 /* Bound to `.'. Called from command mode, so we know that we have to
157 redo a text modification command. The default for _rl_vi_last_command
158 puts you back into insert mode. */
160 rl_vi_redo (count, c)
165 if (!rl_explicit_arg)
167 rl_numeric_arg = _rl_vi_last_repeat;
168 rl_arg_sign = _rl_vi_last_arg_sign;
173 /* If we're redoing an insert with `i', stuff in the inserted text
174 and do not go into insertion mode. */
175 if (_rl_vi_last_command == 'i' && vi_insert_buffer && *vi_insert_buffer)
177 _rl_vi_stuff_insert (count);
178 /* And back up point over the last character inserted. */
183 r = _rl_dispatch (_rl_vi_last_command, _rl_keymap);
189 /* A placeholder for further expansion. */
191 rl_vi_undo (count, key)
194 return (rl_undo_command (count, key));
197 /* Yank the nth arg from the previous line into this line at point. */
199 rl_vi_yank_arg (count, key)
202 /* Readline thinks that the first word on a line is the 0th, while vi
203 thinks the first word on a line is the 1st. Compensate. */
205 rl_yank_nth_arg (count - 1, 0);
207 rl_yank_nth_arg ('$', 0);
212 /* With an argument, move back that many history lines, else move to the
213 beginning of history. */
215 rl_vi_fetch_history (count, c)
220 /* Giving an argument of n means we want the nth command in the history
221 file. The command number is interpreted the same way that the bash
222 `history' command does it -- that is, giving an argument count of 450
223 to this command would get the command listed as number 450 in the
224 output of `history'. */
227 wanted = history_base + where_history () - count;
229 rl_beginning_of_history (0, 0);
231 rl_get_previous_history (wanted, c);
234 rl_beginning_of_history (count, 0);
238 /* Search again for the last thing searched for. */
240 rl_vi_search_again (count, key)
246 rl_noninc_reverse_search_again (count, key);
250 rl_noninc_forward_search_again (count, key);
256 /* Do a vi style search. */
258 rl_vi_search (count, key)
264 rl_noninc_forward_search (count, key);
268 rl_noninc_reverse_search (count, key);
278 /* Completion, from vi's point of view. */
280 rl_vi_complete (ignore, key)
283 if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
285 if (!whitespace (rl_line_buffer[rl_point + 1]))
286 rl_vi_end_word (1, 'E');
291 rl_complete_internal ('*'); /* Expansion and replacement. */
293 rl_complete_internal ('?'); /* List possible completions. */
294 else if (key == '\\')
295 rl_complete_internal (TAB); /* Standard Readline completion. */
297 rl_complete (0, key);
299 if (key == '*' || key == '\\')
301 _rl_vi_set_last (key, 1, rl_arg_sign);
302 rl_vi_insertion_mode (1, key);
307 /* Tilde expansion for vi mode. */
309 rl_vi_tilde_expand (ignore, key)
312 rl_tilde_expand (0, key);
313 _rl_vi_set_last (key, 1, rl_arg_sign); /* XXX */
314 rl_vi_insertion_mode (1, key);
318 /* Previous word in vi mode. */
320 rl_vi_prev_word (count, key)
324 return (rl_vi_next_word (-count, key));
332 if (_rl_uppercase_p (key))
333 rl_vi_bWord (count, key);
335 rl_vi_bword (count, key);
340 /* Next word in vi mode. */
342 rl_vi_next_word (count, key)
346 return (rl_vi_prev_word (-count, key));
348 if (rl_point >= (rl_end - 1))
354 if (_rl_uppercase_p (key))
355 rl_vi_fWord (count, key);
357 rl_vi_fword (count, key);
361 /* Move to the end of the ?next? word. */
363 rl_vi_end_word (count, key)
372 if (_rl_uppercase_p (key))
373 rl_vi_eWord (count, key);
375 rl_vi_eword (count, key);
379 /* Move forward a word the way that 'W' does. */
381 rl_vi_fWord (count, ignore)
384 while (count-- && rl_point < (rl_end - 1))
386 /* Skip until whitespace. */
387 while (!whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
390 /* Now skip whitespace. */
391 while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
398 rl_vi_bWord (count, ignore)
401 while (count-- && rl_point > 0)
403 /* If we are at the start of a word, move back to whitespace so
404 we will go back to the start of the previous word. */
405 if (!whitespace (rl_line_buffer[rl_point]) &&
406 whitespace (rl_line_buffer[rl_point - 1]))
409 while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
414 while (--rl_point >= 0 && !whitespace (rl_line_buffer[rl_point]));
422 rl_vi_eWord (count, ignore)
425 while (count-- && rl_point < (rl_end - 1))
427 if (!whitespace (rl_line_buffer[rl_point]))
430 /* Move to the next non-whitespace character (to the start of the
432 while (++rl_point < rl_end && whitespace (rl_line_buffer[rl_point]));
434 if (rl_point && rl_point < rl_end)
436 /* Skip whitespace. */
437 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
440 /* Skip until whitespace. */
441 while (rl_point < rl_end && !whitespace (rl_line_buffer[rl_point]))
444 /* Move back to the last character of the word. */
452 rl_vi_fword (count, ignore)
455 while (count-- && rl_point < (rl_end - 1))
457 /* Move to white space (really non-identifer). */
458 if (_rl_isident (rl_line_buffer[rl_point]))
460 while (_rl_isident (rl_line_buffer[rl_point]) && rl_point < rl_end)
463 else /* if (!whitespace (rl_line_buffer[rl_point])) */
465 while (!_rl_isident (rl_line_buffer[rl_point]) &&
466 !whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
470 /* Move past whitespace. */
471 while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
478 rl_vi_bword (count, ignore)
481 while (count-- && rl_point > 0)
485 /* If we are at the start of a word, move back to whitespace
486 so we will go back to the start of the previous word. */
487 if (!whitespace (rl_line_buffer[rl_point]) &&
488 whitespace (rl_line_buffer[rl_point - 1]))
491 /* If this character and the previous character are `opposite', move
492 back so we don't get messed up by the rl_point++ down there in
493 the while loop. Without this code, words like `l;' screw up the
495 last_is_ident = _rl_isident (rl_line_buffer[rl_point - 1]);
496 if ((_rl_isident (rl_line_buffer[rl_point]) && !last_is_ident) ||
497 (!_rl_isident (rl_line_buffer[rl_point]) && last_is_ident))
500 while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
505 if (_rl_isident (rl_line_buffer[rl_point]))
506 while (--rl_point >= 0 && _rl_isident (rl_line_buffer[rl_point]));
508 while (--rl_point >= 0 && !_rl_isident (rl_line_buffer[rl_point]) &&
509 !whitespace (rl_line_buffer[rl_point]));
517 rl_vi_eword (count, ignore)
520 while (count-- && rl_point < rl_end - 1)
522 if (!whitespace (rl_line_buffer[rl_point]))
525 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
528 if (rl_point < rl_end)
530 if (_rl_isident (rl_line_buffer[rl_point]))
531 while (++rl_point < rl_end && _rl_isident (rl_line_buffer[rl_point]));
533 while (++rl_point < rl_end && !_rl_isident (rl_line_buffer[rl_point])
534 && !whitespace (rl_line_buffer[rl_point]));
542 rl_vi_insert_beg (count, key)
545 rl_beg_of_line (1, key);
546 rl_vi_insertion_mode (1, key);
551 rl_vi_append_mode (count, key)
554 if (rl_point < rl_end)
556 if (MB_CUR_MAX == 1 || rl_byte_oriented)
560 int point = rl_point;
561 rl_forward_char (1, key);
562 if (point == rl_point)
566 rl_vi_insertion_mode (1, key);
571 rl_vi_append_eol (count, key)
574 rl_end_of_line (1, key);
575 rl_vi_append_mode (1, key);
579 /* What to do in the case of C-d. */
581 rl_vi_eof_maybe (count, c)
584 return (rl_newline (1, '\n'));
587 /* Insertion mode stuff. */
589 /* Switching from one mode to the other really just involves
590 switching keymaps. */
592 rl_vi_insertion_mode (count, key)
595 _rl_keymap = vi_insertion_keymap;
596 _rl_vi_last_key_before_insert = key;
601 _rl_vi_save_insert (up)
608 if (vi_insert_buffer_size >= 1)
609 vi_insert_buffer[0] = '\0';
615 len = end - start + 1;
616 if (len >= vi_insert_buffer_size)
618 vi_insert_buffer_size += (len + 32) - (len % 32);
619 vi_insert_buffer = (char *)xrealloc (vi_insert_buffer, vi_insert_buffer_size);
621 strncpy (vi_insert_buffer, rl_line_buffer + start, len - 1);
622 vi_insert_buffer[len-1] = '\0';
626 _rl_vi_done_inserting ()
628 if (_rl_vi_doing_insert)
630 /* The `C', `s', and `S' commands set this. */
631 rl_end_undo_group ();
632 /* Now, the text between rl_undo_list->next->start and
633 rl_undo_list->next->end is what was inserted while in insert
634 mode. It gets copied to VI_INSERT_BUFFER because it depends
635 on absolute indices into the line which may change (though they
636 probably will not). */
637 _rl_vi_doing_insert = 0;
638 _rl_vi_save_insert (rl_undo_list->next);
639 vi_continued_command = 1;
643 if (_rl_vi_last_key_before_insert == 'i' && rl_undo_list)
644 _rl_vi_save_insert (rl_undo_list);
645 /* XXX - Other keys probably need to be checked. */
646 else if (_rl_vi_last_key_before_insert == 'C')
647 rl_end_undo_group ();
648 while (_rl_undo_group_level > 0)
649 rl_end_undo_group ();
650 vi_continued_command = 0;
655 rl_vi_movement_mode (count, key)
659 rl_backward_char (1, key);
661 _rl_keymap = vi_movement_keymap;
662 _rl_vi_done_inserting ();
667 rl_vi_arg_digit (count, c)
670 if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
671 return (rl_beg_of_line (1, c));
673 return (rl_digit_argument (count, c));
676 /* Change the case of the next COUNT characters. */
677 #if defined (HANDLE_MULTIBYTE)
679 _rl_vi_change_mbchar_case (count)
686 memset (&ps, 0, sizeof (mbstate_t));
687 if (_rl_adjust_point (rl_line_buffer, rl_point, &ps) > 0)
689 while (count-- && rl_point < rl_end)
691 mbrtowc (&wc, rl_line_buffer + rl_point, rl_end - rl_point, &ps);
694 else if (iswlower (wc))
698 /* Just skip over chars neither upper nor lower case */
699 rl_forward_char (1, 0);
703 /* Vi is kind of strange here. */
707 rl_begin_undo_group ();
710 rl_end_undo_group ();
714 rl_forward_char (1, 0);
722 rl_vi_change_case (count, ignore)
727 /* Don't try this on an empty line. */
728 if (rl_point >= rl_end)
731 #if defined (HANDLE_MULTIBYTE)
732 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
733 return (_rl_vi_change_mbchar_case (count));
736 while (count-- && rl_point < rl_end)
738 if (_rl_uppercase_p (rl_line_buffer[rl_point]))
739 c = _rl_to_lower (rl_line_buffer[rl_point]);
740 else if (_rl_lowercase_p (rl_line_buffer[rl_point]))
741 c = _rl_to_upper (rl_line_buffer[rl_point]);
744 /* Just skip over characters neither upper nor lower case. */
745 rl_forward_char (1, c);
749 /* Vi is kind of strange here. */
752 rl_begin_undo_group ();
754 _rl_insert_char (1, c);
755 rl_end_undo_group ();
759 rl_forward_char (1, c);
765 rl_vi_put (count, key)
768 if (!_rl_uppercase_p (key) && (rl_point + 1 <= rl_end))
769 rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
772 rl_backward_char (1, key);
779 if (rl_point && rl_point == rl_end)
781 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
782 rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
790 rl_vi_column (count, key)
794 rl_end_of_line (1, key);
796 rl_point = count - 1;
801 rl_vi_domove (key, nextkey)
808 RL_SETSTATE(RL_STATE_MOREINPUT);
810 RL_UNSETSTATE(RL_STATE_MOREINPUT);
813 if (!member (c, vi_motion))
817 save = rl_numeric_arg;
818 rl_numeric_arg = _rl_digit_value (c);
820 rl_numeric_arg *= save;
821 RL_SETSTATE(RL_STATE_MOREINPUT);
822 c = rl_read_key (); /* real command */
823 RL_UNSETSTATE(RL_STATE_MOREINPUT);
826 else if (key == c && (key == 'd' || key == 'y' || key == 'c'))
829 rl_beg_of_line (1, c);
830 _rl_vi_last_motion = c;
837 _rl_vi_last_motion = c;
839 /* Append a blank character temporarily so that the motion routines
840 work right at the end of the line. */
842 rl_line_buffer[rl_end++] = ' ';
843 rl_line_buffer[rl_end] = '\0';
845 _rl_dispatch (c, _rl_keymap);
847 /* Remove the blank that we added. */
849 rl_line_buffer[rl_end] = '\0';
850 if (rl_point > rl_end)
853 /* No change in position means the command failed. */
854 if (rl_mark == rl_point)
857 /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
858 word. If we are not at the end of the line, and we are on a
859 non-whitespace character, move back one (presumably to whitespace). */
860 if ((_rl_to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
861 !whitespace (rl_line_buffer[rl_point]))
864 /* If cw or cW, back up to the end of a word, so the behaviour of ce
865 or cE is the actual result. Brute-force, no subtlety. */
866 if (key == 'c' && rl_point >= rl_mark && (_rl_to_upper (c) == 'W'))
868 /* Don't move farther back than where we started. */
869 while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
872 /* Posix.2 says that if cw or cW moves the cursor towards the end of
873 the line, the character under the cursor should be deleted. */
874 if (rl_point == rl_mark)
878 /* Move past the end of the word so that the kill doesn't
879 remove the last letter of the previous word. Only do this
880 if we are not at the end of the line. */
881 if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point]))
886 if (rl_mark < rl_point)
887 SWAP (rl_point, rl_mark);
892 /* A simplified loop for vi. Don't dispatch key at end.
893 Don't recognize minus sign?
894 Should this do rl_save_prompt/rl_restore_prompt? */
900 RL_SETSTATE(RL_STATE_NUMERICARG);
903 if (rl_numeric_arg > 1000000)
905 rl_explicit_arg = rl_numeric_arg = 0;
908 RL_UNSETSTATE(RL_STATE_NUMERICARG);
911 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
912 RL_SETSTATE(RL_STATE_MOREINPUT);
913 key = c = rl_read_key ();
914 RL_UNSETSTATE(RL_STATE_MOREINPUT);
916 if (c >= 0 && _rl_keymap[c].type == ISFUNC &&
917 _rl_keymap[c].function == rl_universal_argument)
927 rl_numeric_arg = (rl_numeric_arg * 10) + _rl_digit_value (c);
929 rl_numeric_arg = _rl_digit_value (c);
940 RL_UNSETSTATE(RL_STATE_NUMERICARG);
945 rl_vi_delete_to (count, key)
950 if (_rl_uppercase_p (key))
953 rl_stuff_char (_rl_vi_last_motion);
955 if (rl_vi_domove (key, &c))
961 /* These are the motion commands that do not require adjusting the
963 if ((strchr (" l|h^0bB", c) == 0) && (rl_mark < rl_end))
966 rl_kill_text (rl_point, rl_mark);
971 rl_vi_change_to (count, key)
976 if (_rl_uppercase_p (key))
979 rl_stuff_char (_rl_vi_last_motion);
981 start_pos = rl_point;
983 if (rl_vi_domove (key, &c))
989 /* These are the motion commands that do not require adjusting the
990 mark. c[wW] are handled by special-case code in rl_vi_domove(),
991 and already leave the mark at the correct location. */
992 if ((strchr (" l|hwW^0bB", c) == 0) && (rl_mark < rl_end))
995 /* The cursor never moves with c[wW]. */
996 if ((_rl_to_upper (c) == 'W') && rl_point < start_pos)
997 rl_point = start_pos;
1001 if (vi_insert_buffer && *vi_insert_buffer)
1002 rl_begin_undo_group ();
1003 rl_delete_text (rl_point, rl_mark);
1004 if (vi_insert_buffer && *vi_insert_buffer)
1006 rl_insert_text (vi_insert_buffer);
1007 rl_end_undo_group ();
1012 rl_begin_undo_group (); /* to make the `u' command work */
1013 rl_kill_text (rl_point, rl_mark);
1014 /* `C' does not save the text inserted for undoing or redoing. */
1015 if (_rl_uppercase_p (key) == 0)
1016 _rl_vi_doing_insert = 1;
1017 _rl_vi_set_last (key, count, rl_arg_sign);
1018 rl_vi_insertion_mode (1, key);
1025 rl_vi_yank_to (count, key)
1028 int c, save = rl_point;
1030 if (_rl_uppercase_p (key))
1031 rl_stuff_char ('$');
1033 if (rl_vi_domove (key, &c))
1039 /* These are the motion commands that do not require adjusting the
1041 if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end))
1044 rl_begin_undo_group ();
1045 rl_kill_text (rl_point, rl_mark);
1046 rl_end_undo_group ();
1054 rl_vi_delete (count, key)
1065 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1066 end = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
1068 end = rl_point + count;
1073 rl_kill_text (rl_point, end);
1075 if (rl_point > 0 && rl_point == rl_end)
1076 rl_backward_char (1, key);
1081 rl_vi_back_to_indent (count, key)
1084 rl_beg_of_line (1, key);
1085 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
1091 rl_vi_first_print (count, key)
1094 return (rl_vi_back_to_indent (1, key));
1098 rl_vi_char_search (count, key)
1101 #if defined (HANDLE_MULTIBYTE)
1102 static char *target;
1107 static int orig_dir, dir;
1109 if (key == ';' || key == ',')
1110 dir = key == ';' ? orig_dir : -orig_dir;
1114 #if defined (HANDLE_MULTIBYTE)
1115 target = _rl_vi_last_search_mbchar;
1117 target = _rl_vi_last_search_char;
1121 #if defined (HANDLE_MULTIBYTE)
1122 mb_len = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX);
1123 target = _rl_vi_last_search_mbchar;
1125 RL_SETSTATE(RL_STATE_MOREINPUT);
1126 _rl_vi_last_search_char = target = rl_read_key ();
1127 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1134 orig_dir = dir = FTO;
1138 orig_dir = dir = BTO;
1142 orig_dir = dir = FFIND;
1146 orig_dir = dir = BFIND;
1151 #if defined (HANDLE_MULTIBYTE)
1152 return (_rl_char_search_internal (count, dir, target, mb_len));
1154 return (_rl_char_search_internal (count, dir, target));
1158 /* Match brackets */
1160 rl_vi_match (ignore, key)
1163 int count = 1, brack, pos, tmp, pre;
1166 if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
1168 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1170 while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
1173 rl_forward_char (1, key);
1174 if (pre == rl_point)
1179 while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
1180 rl_point < rl_end - 1)
1181 rl_forward_char (1, key);
1198 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1202 pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY);
1208 int b = rl_vi_bracktype (rl_line_buffer[pos]);
1211 else if (b == brack)
1225 if (MB_CUR_MAX == 1 || rl_byte_oriented)
1228 pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY);
1232 int b = rl_vi_bracktype (rl_line_buffer[pos]);
1235 else if (b == brack)
1256 case ')': return -1;
1258 case ']': return -2;
1260 case '}': return -3;
1265 /* XXX - think about reading an entire mbchar with _rl_read_mbchar and
1266 inserting it in one bunch instead of the loop below (like in
1267 rl_vi_char_search or _rl_vi_change_mbchar_case. Set c to mbchar[0]
1268 for test against 033 or ^C. Make sure that _rl_read_mbchar does
1271 rl_vi_change_char (count, key)
1277 c = _rl_vi_last_replacement;
1280 RL_SETSTATE(RL_STATE_MOREINPUT);
1281 _rl_vi_last_replacement = c = rl_read_key ();
1282 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1285 if (c == '\033' || c == CTRL ('C'))
1288 while (count-- && rl_point < rl_end)
1290 rl_begin_undo_group ();
1293 #if defined (HANDLE_MULTIBYTE)
1294 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
1295 while (_rl_insert_char (1, c))
1297 RL_SETSTATE (RL_STATE_MOREINPUT);
1299 RL_UNSETSTATE (RL_STATE_MOREINPUT);
1303 _rl_insert_char (1, c);
1305 rl_backward_char (1, c);
1307 rl_end_undo_group ();
1313 rl_vi_subst (count, key)
1316 /* If we are redoing, rl_vi_change_to will stuff the last motion char */
1317 if (vi_redoing == 0)
1318 rl_stuff_char ((key == 'S') ? 'c' : ' '); /* `S' == `cc', `s' == `c ' */
1320 return (rl_vi_change_to (count, 'c'));
1324 rl_vi_overstrike (count, key)
1327 if (_rl_vi_doing_insert == 0)
1329 _rl_vi_doing_insert = 1;
1330 rl_begin_undo_group ();
1335 _rl_overwrite_char (count, key);
1336 vi_replace_count += count;
1343 rl_vi_overstrike_delete (count, key)
1348 for (i = 0; i < count; i++)
1350 if (vi_replace_count == 0)
1361 rl_backward_char (1, key);
1364 if (vi_replace_count == 0 && _rl_vi_doing_insert)
1366 rl_end_undo_group ();
1368 _rl_vi_doing_insert = 0;
1374 rl_vi_replace (count, key)
1379 vi_replace_count = 0;
1381 if (!vi_replace_map)
1383 vi_replace_map = rl_make_bare_keymap ();
1385 for (i = ' '; i < KEYMAP_SIZE; i++)
1386 vi_replace_map[i].function = rl_vi_overstrike;
1388 vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
1389 vi_replace_map[ESC].function = rl_vi_movement_mode;
1390 vi_replace_map[RETURN].function = rl_newline;
1391 vi_replace_map[NEWLINE].function = rl_newline;
1393 /* If the normal vi insertion keymap has ^H bound to erase, do the
1394 same here. Probably should remove the assignment to RUBOUT up
1395 there, but I don't think it will make a difference in real life. */
1396 if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC &&
1397 vi_insertion_keymap[CTRL ('H')].function == rl_rubout)
1398 vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete;
1401 _rl_keymap = vi_replace_map;
1406 /* Try to complete the word we are standing on or the word that ends with
1407 the previous character. A space matches everything. Word delimiters are
1410 rl_vi_possible_completions()
1412 int save_pos = rl_point;
1414 if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';')
1416 while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' &&
1417 rl_line_buffer[rl_point] != ';')
1420 else if (rl_line_buffer[rl_point - 1] == ';')
1426 rl_possible_completions ();
1427 rl_point = save_pos;
1433 /* Functions to save and restore marks. */
1435 rl_vi_set_mark (count, key)
1440 RL_SETSTATE(RL_STATE_MOREINPUT);
1441 ch = rl_read_key ();
1442 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1444 if (ch < 'a' || ch > 'z')
1450 vi_mark_chars[ch] = rl_point;
1455 rl_vi_goto_mark (count, key)
1460 RL_SETSTATE(RL_STATE_MOREINPUT);
1461 ch = rl_read_key ();
1462 RL_UNSETSTATE(RL_STATE_MOREINPUT);
1469 else if (ch < 'a' || ch > 'z')
1476 if (vi_mark_chars[ch] == -1)
1481 rl_point = vi_mark_chars[ch];
1485 #endif /* VI_MODE */