1 /* vi_mode.c -- A vi emulation mode for Bash.
2 Derived from code written by Jeff Sparkes (jsparkes@bnr.ca). */
4 /* Copyright (C) 1988, 1991 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library (the Library), a set of
7 routines for providing Emacs style line input to programs that ask
10 The Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 1, or (at your option)
15 The Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 The GNU General Public License is often shipped with GNU software, and
21 is generally kept in a file called COPYING or LICENSE. If you do not
22 have a copy of the license, write to the Free Software Foundation,
23 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* **************************************************************** */
27 /* VI Emulation Mode */
29 /* **************************************************************** */
34 #if defined (__GNUC__)
35 # define alloca __builtin_alloca
37 # if defined (sparc) || defined (HAVE_ALLOCA_H)
42 /* Some standard library routines. */
47 #define digit(c) ((c) >= '0' && (c) <= '9')
51 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
55 #define digit_value(c) ((c) - '0')
59 #define member(c, s) ((c) ? index ((s), (c)) : 0)
63 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
67 #define exchange(x, y) {int temp = x; x = y; y = temp;}
70 /* Variables imported from readline.c */
71 extern int rl_point
, rl_end
, rl_mark
, rl_done
;
72 extern FILE *rl_instream
;
73 extern int rl_line_buffer_len
, rl_explicit_arg
, rl_numeric_arg
;
75 extern char *rl_prompt
;
76 extern char *rl_line_buffer
;
77 extern int rl_arg_sign
;
79 extern char *xmalloc (), *xrealloc ();
80 extern void rl_extend_line_buffer ();
82 /* Last string searched for from `/' or `?'. */
83 static char *vi_last_search
= (char *)NULL
;
84 static int vi_histpos
;
86 /* Non-zero means enter insertion mode. */
87 int vi_doing_insert
= 0;
89 /* String inserted into the line by rl_vi_comment (). */
90 char *rl_vi_comment_begin
= (char *)NULL
;
93 /* Command keys which do movement for xxx_to commands. */
94 static char *vi_motion
= " hl^$0ftFt;,%wbeWBE|";
96 /* Keymap used for vi replace characters. Created dynamically since
98 static Keymap vi_replace_map
= (Keymap
)NULL
;
100 /* The number of characters inserted in the last replace operation. */
101 static int vi_replace_count
= 0;
103 /* Yank the nth arg from the previous line into this line at point. */
104 rl_vi_yank_arg (count
)
107 /* Readline thinks that the first word on a line is the 0th, while vi
108 thinks the first word on a line is the 1st. Compensate. */
110 rl_yank_nth_arg (count
- 1, 0);
112 rl_yank_nth_arg ('$', 0);
115 /* With an argument, move back that many history lines, else move to the
116 beginning of history. */
117 rl_vi_fetch_history (count
, c
)
120 extern int rl_explicit_arg
;
121 int current
= where_history ();
123 /* Giving an argument of n means we want the nth command in the history
124 file. The command number is interpreted the same way that the bash
125 `history' command does it -- that is, giving an argument count of 450
126 to this command would get the command listed as number 450 in the
127 output of `history'. */
130 int wanted
= history_base
+ current
- count
;
132 rl_beginning_of_history (0, 0);
134 rl_get_previous_history (wanted
);
137 rl_beginning_of_history (count
, 0);
140 /* Search again for the last thing searched for. */
141 rl_vi_search_again (ignore
, key
)
147 rl_vi_dosearch (vi_last_search
, -1);
151 rl_vi_dosearch (vi_last_search
, 1);
156 /* Do a vi style search. */
157 rl_vi_search (count
, key
)
160 int dir
, c
, save_pos
;
178 vi_histpos
= where_history ();
182 /* Reuse the line input buffer to read the search string. */
183 rl_line_buffer
[0] = 0;
184 rl_end
= rl_point
= 0;
185 p
= (char *)alloca (2 + (rl_prompt
? strlen (rl_prompt
) : 0));
187 sprintf (p
, "%s%c", rl_prompt
? rl_prompt
: "", key
);
189 rl_message (p
, 0, 0);
191 while (c
= rl_read_key ())
199 maybe_unsave_line ();
207 rl_dispatch (c
, keymap
);
217 maybe_unsave_line ();
231 free (vi_last_search
);
233 vi_last_search
= savestring (rl_line_buffer
);
234 rl_vi_dosearch (rl_line_buffer
, dir
);
237 /* Search for STRING in the history list. DIR is < 0 for searching
238 backwards. POS is an absolute index into the history list at
239 which point to begin searching. If the first character of STRING
240 is `^', the string must match a prefix of a history line, otherwise
241 a full substring match is performed. */
243 vi_history_search_pos (string
, dir
, pos
)
247 int ret
, old
= where_history ();
249 history_set_pos (pos
);
252 ret
= history_search_prefix (string
+ 1, dir
);
254 ret
= history_search (string
, dir
);
258 history_set_pos (old
);
262 ret
= where_history ();
263 history_set_pos (old
);
267 rl_vi_dosearch (string
, dir
)
271 int old
, save
= vi_histpos
;
274 if (string
== 0 || *string
== 0 || vi_histpos
< 0)
280 if ((save
= vi_history_search_pos (string
, dir
, vi_histpos
+ dir
)) == -1)
282 maybe_unsave_line ();
291 old
= where_history ();
292 history_set_pos (vi_histpos
);
293 h
= current_history ();
294 history_set_pos (old
);
297 int line_len
= strlen (h
->line
);
299 if (line_len
>= rl_line_buffer_len
)
300 rl_extend_line_buffer (line_len
);
301 strcpy (rl_line_buffer
, h
->line
);
304 rl_undo_list
= (UNDO_LIST
*)h
->data
;
305 rl_end
= strlen (rl_line_buffer
);
310 /* Completion, from vi's point of view. */
311 rl_vi_complete (ignore
, key
)
314 if ((rl_point
< rl_end
) && (!whitespace (rl_line_buffer
[rl_point
])))
316 if (!whitespace (rl_line_buffer
[rl_point
+ 1]))
317 rl_vi_end_word (1, 'E');
322 rl_complete_internal ('*'); /* Expansion and replacement. */
324 rl_complete_internal ('?'); /* List possible completions. */
325 else if (key
== '\\')
326 rl_complete_internal (TAB
); /* Standard Readline completion. */
328 rl_complete (0, key
);
331 /* Previous word in vi mode. */
332 rl_vi_prev_word (count
, key
)
337 rl_vi_next_word (-count
, key
);
347 if (uppercase_p (key
))
353 /* Next word in vi mode. */
354 rl_vi_next_word (count
, key
)
359 rl_vi_prev_word (-count
, key
);
363 if (rl_point
>= (rl_end
- 1))
369 if (uppercase_p (key
))
375 /* Move to the end of the ?next? word. */
376 rl_vi_end_word (count
, key
)
385 if (uppercase_p (key
))
391 /* Move forward a word the way that 'W' does. */
395 while (count
-- && rl_point
< (rl_end
- 1))
397 /* Skip until whitespace. */
398 while (!whitespace (rl_line_buffer
[rl_point
]) && rl_point
< rl_end
)
401 /* Now skip whitespace. */
402 while (whitespace (rl_line_buffer
[rl_point
]) && rl_point
< rl_end
)
410 while (count
-- && rl_point
> 0)
412 /* If we are at the start of a word, move back to whitespace so
413 we will go back to the start of the previous word. */
414 if (!whitespace (rl_line_buffer
[rl_point
]) &&
415 whitespace (rl_line_buffer
[rl_point
- 1]))
418 while (rl_point
> 0 && whitespace (rl_line_buffer
[rl_point
]))
423 while (--rl_point
>= 0 && !whitespace (rl_line_buffer
[rl_point
]));
432 while (count
-- && rl_point
< (rl_end
- 1))
434 if (!whitespace (rl_line_buffer
[rl_point
]))
437 /* Move to the next non-whitespace character (to the start of the
439 while (++rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]));
441 if (rl_point
&& rl_point
< rl_end
)
443 /* Skip whitespace. */
444 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
447 /* Skip until whitespace. */
448 while (rl_point
< rl_end
&& !whitespace (rl_line_buffer
[rl_point
]))
451 /* Move back to the last character of the word. */
460 while (count
-- && rl_point
< (rl_end
- 1))
462 /* Move to white space (really non-identifer). */
463 if (isident (rl_line_buffer
[rl_point
]))
465 while (isident (rl_line_buffer
[rl_point
]) && rl_point
< rl_end
)
468 else /* if (!whitespace (rl_line_buffer[rl_point])) */
470 while (!isident (rl_line_buffer
[rl_point
]) &&
471 !whitespace (rl_line_buffer
[rl_point
]) && rl_point
< rl_end
)
475 /* Move past whitespace. */
476 while (whitespace (rl_line_buffer
[rl_point
]) && rl_point
< rl_end
)
484 while (count
-- && rl_point
> 0)
488 /* If we are at the start of a word, move back to whitespace
489 so we will go back to the start of the previous word. */
490 if (!whitespace (rl_line_buffer
[rl_point
]) &&
491 whitespace (rl_line_buffer
[rl_point
- 1]))
494 /* If this character and the previous character are `opposite', move
495 back so we don't get messed up by the rl_point++ down there in
496 the while loop. Without this code, words like `l;' screw up the
498 last_is_ident
= isident (rl_line_buffer
[rl_point
- 1]);
499 if ((isident (rl_line_buffer
[rl_point
]) && !last_is_ident
) ||
500 (!isident (rl_line_buffer
[rl_point
]) && last_is_ident
))
503 while (rl_point
> 0 && whitespace (rl_line_buffer
[rl_point
]))
508 if (isident (rl_line_buffer
[rl_point
]))
509 while (--rl_point
>= 0 && isident (rl_line_buffer
[rl_point
]));
511 while (--rl_point
>= 0 && !isident (rl_line_buffer
[rl_point
]) &&
512 !whitespace (rl_line_buffer
[rl_point
]));
521 while (count
-- && rl_point
< rl_end
- 1)
523 if (!whitespace (rl_line_buffer
[rl_point
]))
526 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
529 if (rl_point
< rl_end
)
531 if (isident (rl_line_buffer
[rl_point
]))
532 while (++rl_point
< rl_end
&& isident (rl_line_buffer
[rl_point
]));
534 while (++rl_point
< rl_end
&& !isident (rl_line_buffer
[rl_point
])
535 && !whitespace (rl_line_buffer
[rl_point
]));
544 rl_vi_insertion_mode ();
550 if (rl_point
< rl_end
)
552 rl_vi_insertion_mode ();
559 rl_vi_append_mode ();
563 /* What to do in the case of C-d. */
564 rl_vi_eof_maybe (count
, c
)
567 rl_newline (1, '\n');
570 /* Insertion mode stuff. */
572 /* Switching from one mode to the other really just involves
573 switching keymaps. */
574 rl_vi_insertion_mode ()
576 keymap
= vi_insertion_keymap
;
579 rl_vi_movement_mode ()
584 keymap
= vi_movement_keymap
;
585 vi_done_inserting ();
592 rl_end_undo_group ();
597 rl_vi_arg_digit (count
, c
)
600 if (c
== '0' && rl_numeric_arg
== 1 && !rl_explicit_arg
)
603 rl_digit_argument (count
, c
);
606 rl_vi_change_case (count
, ignore
)
611 /* Don't try this on an empty line. */
612 if (rl_point
>= rl_end
)
615 while (count
-- && rl_point
< rl_end
)
617 if (uppercase_p (rl_line_buffer
[rl_point
]))
618 c
= to_lower (rl_line_buffer
[rl_point
]);
619 else if (lowercase_p (rl_line_buffer
[rl_point
]))
620 c
= to_upper (rl_line_buffer
[rl_point
]);
622 /* Vi is kind of strange here. */
625 rl_begin_undo_group ();
628 rl_end_undo_group ();
636 rl_vi_put (count
, key
)
639 if (!uppercase_p (key
) && (rl_point
+ 1 <= rl_end
))
648 if (rl_point
&& rl_point
== rl_end
)
657 rl_point
= count
- 1;
661 rl_vi_domove (key
, nextkey
)
665 int old_end
, added_blank
;
673 if (!member (c
, vi_motion
))
677 save
= rl_numeric_arg
;
678 rl_numeric_arg
= digit_value (c
);
680 rl_numeric_arg
*= save
;
681 c
= rl_read_key (); /* real command */
684 else if ((key
== 'd' && c
== 'd') ||
685 (key
== 'y' && c
== 'y') ||
686 (key
== 'c' && c
== 'c'))
696 /* Append a blank character temporarily so that the motion routines
697 work right at the end of the line. */
699 rl_line_buffer
[rl_end
++] = ' '; /* This looks pretty bogus to me??? */
700 rl_line_buffer
[rl_end
] = '\0';
703 rl_dispatch (c
, keymap
);
705 /* Remove the blank that we added. */
707 rl_line_buffer
[rl_end
] = '\0';
708 if (rl_point
> rl_end
)
709 rl_point
= rl_end
- 1;
711 /* No change in position means the command failed. */
712 if (rl_mark
== rl_point
)
715 /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
716 word. If we are not at the end of the line, and we are on a
717 non-whitespace character, move back one (presumably to whitespace). */
718 if ((c
== 'w' || c
== 'W') && (rl_point
< rl_end
) &&
719 !whitespace (rl_line_buffer
[rl_point
]))
722 /* If cw or cW, back up to the end of a word, so the behaviour of ce
723 or cE is the actual result. Brute-force, no subtlety. Do the same
724 thing for dw or dW. */
725 if (key
== 'c' && (to_upper (c
) == 'W'))
727 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
]))
731 if (rl_mark
< rl_point
)
732 exchange (rl_point
, rl_mark
);
737 /* A simplified loop for vi. Don't dispatch key at end.
738 Don't recognize minus sign? */
745 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
, 0);
746 key
= c
= rl_read_key ();
748 if (keymap
[c
].type
== ISFUNC
&&
749 keymap
[c
].function
== rl_universal_argument
)
759 rl_numeric_arg
= (rl_numeric_arg
* 10) + digit_value (c
);
761 rl_numeric_arg
= digit_value (c
);
773 rl_vi_delete_to (count
, key
)
778 if (uppercase_p (key
))
781 if (rl_vi_domove (key
, &c
))
787 if ((c
!= 'l') && (c
!= '|') && (c
!= 'h') && rl_mark
< rl_end
)
790 rl_kill_text (rl_point
, rl_mark
);
793 rl_vi_change_to (count
, key
)
798 if (uppercase_p (key
))
801 if (rl_vi_domove (key
, &c
))
807 if ((c
!= 'l') && (c
!= '|') && (c
!= 'h') && rl_mark
< rl_end
)
810 rl_begin_undo_group ();
812 rl_kill_text (rl_point
, rl_mark
);
813 rl_vi_insertion_mode ();
816 rl_vi_yank_to (count
, key
)
819 int c
, save
= rl_point
;
821 if (uppercase_p (key
))
824 if (rl_vi_domove (key
, &c
))
830 if ((c
!= 'l') && (c
!= '|') && (c
!= 'h') && rl_mark
< rl_end
)
833 rl_begin_undo_group ();
834 rl_kill_text (rl_point
, rl_mark
);
835 rl_end_undo_group ();
850 end
= rl_point
+ count
;
855 rl_kill_text (rl_point
, end
);
857 if (rl_point
> 0 && rl_point
== rl_end
)
861 /* Turn the current line into a comment in shell history.
862 A K*rn shell style function. */
867 if (rl_vi_comment_begin
!= (char *)NULL
)
868 rl_insert_text (rl_vi_comment_begin
);
870 rl_insert_text (": "); /* Default. */
873 rl_newline (1, '\010');
878 rl_back_to_indent ();
881 rl_back_to_indent (ignore1
, ignore2
)
882 int ignore1
, ignore2
;
885 while (rl_point
< rl_end
&& whitespace (rl_line_buffer
[rl_point
]))
889 /* NOTE: it is necessary that opposite directions are inverses */
890 #define FTO 1 /* forward to */
891 #define BTO -1 /* backward to */
892 #define FFIND 2 /* forward find */
893 #define BFIND -2 /* backward find */
895 rl_vi_char_search (count
, key
)
899 static int orig_dir
, dir
;
902 if (key
== ';' || key
== ',')
903 dir
= (key
== ';' ? orig_dir
: -orig_dir
);
906 target
= rl_getc (rl_instream
);
911 orig_dir
= dir
= FTO
;
915 orig_dir
= dir
= BTO
;
919 orig_dir
= dir
= FFIND
;
923 orig_dir
= dir
= BFIND
;
943 if (rl_line_buffer
[pos
] == target
)
971 if (rl_line_buffer
[pos
] == target
)
980 while (++pos
< rl_end
);
982 if (pos
>= (rl_end
- 1))
994 int count
= 1, brack
, pos
;
997 if ((brack
= rl_vi_bracktype (rl_line_buffer
[rl_point
])) == 0)
999 while ((brack
= rl_vi_bracktype (rl_line_buffer
[rl_point
])) == 0 &&
1000 rl_point
< rl_end
- 1)
1019 int b
= rl_vi_bracktype (rl_line_buffer
[pos
]);
1022 else if (b
== brack
)
1038 int b
= rl_vi_bracktype (rl_line_buffer
[pos
]);
1041 else if (b
== brack
)
1061 case ')': return -1;
1063 case ']': return -2;
1065 case '}': return -3;
1070 rl_vi_change_char (count
, key
)
1075 c
= rl_getc (rl_instream
);
1077 if (c
== '\033' || c
== CTRL ('C'))
1080 while (count
-- && rl_point
< rl_end
)
1082 rl_begin_undo_group ();
1089 rl_end_undo_group ();
1093 rl_vi_subst (count
, key
)
1096 rl_begin_undo_group ();
1097 vi_doing_insert
= 1;
1099 if (uppercase_p (key
))
1105 rl_delete (count
, key
);
1107 rl_vi_insertion_mode ();
1110 rl_vi_overstrike (count
, key
)
1115 if (vi_doing_insert
== 0)
1117 vi_doing_insert
= 1;
1118 rl_begin_undo_group ();
1121 for (i
= 0; i
< count
; i
++)
1124 rl_begin_undo_group ();
1126 if (rl_point
< rl_end
)
1134 rl_end_undo_group ();
1138 rl_vi_overstrike_delete (count
)
1143 for (i
= 0; i
< count
; i
++)
1145 if (vi_replace_count
== 0)
1159 if (vi_replace_count
== 0 && vi_doing_insert
)
1161 rl_end_undo_group ();
1163 vi_doing_insert
= 0;
1167 rl_vi_replace (count
, key
)
1172 vi_replace_count
= 0;
1174 if (!vi_replace_map
)
1176 vi_replace_map
= rl_make_bare_keymap ();
1178 for (i
= ' '; i
< 127; i
++)
1179 vi_replace_map
[i
].function
= rl_vi_overstrike
;
1181 vi_replace_map
[RUBOUT
].function
= rl_vi_overstrike_delete
;
1182 vi_replace_map
[ESC
].function
= rl_vi_movement_mode
;
1183 vi_replace_map
[RETURN
].function
= rl_newline
;
1184 vi_replace_map
[NEWLINE
].function
= rl_newline
;
1186 /* If the normal vi insertion keymap has ^H bound to erase, do the
1187 same here. Probably should remove the assignment to RUBOUT up
1188 there, but I don't think it will make a difference in real life. */
1189 if (vi_insertion_keymap
[CTRL ('H')].type
== ISFUNC
&&
1190 vi_insertion_keymap
[CTRL ('H')].function
== rl_rubout
)
1191 vi_replace_map
[CTRL ('H')].function
= rl_vi_overstrike_delete
;
1194 keymap
= vi_replace_map
;
1198 * Try to complete the word we are standing on or the word that ends with
1199 * the previous character. A space matches everything.
1200 * Word delimiters are space and ;.
1202 rl_vi_possible_completions()
1204 int save_pos
= rl_point
;
1206 if (!index (" ;", rl_line_buffer
[rl_point
]))
1208 while (!index(" ;", rl_line_buffer
[++rl_point
]))
1211 else if (rl_line_buffer
[rl_point
-1] == ';')
1217 rl_possible_completions ();
1218 rl_point
= save_pos
;
1223 #endif /* VI_MODE */
This page took 0.054919 seconds and 4 git commands to generate.