Debian Codegraph @

libsrc/readline/vi_mode.c

raw
1 /* vi_mode.c -- A vi emulation mode for Bash.
2 Derived from code written by Jeff Sparkes ([email protected]). */
3
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
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 1, or
12 (at your option) any later version.
13
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.
18
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 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
24
25 /* **************************************************************** */
26 /* */
27 /* VI Emulation Mode */
28 /* */
29
30
31 /* **************************************************************** */
32 /*
33
34 .VERSION
35 051021 last modif
36
37 */
38
39 #include "rlconf.h"
40
41 #if defined (VI_MODE)
42
43 #include <sys/types.h>
44
45 #if defined (HAVE_STDLIB_H)
46 # include <stdlib.h>
47 #else
48 # include "ansi_stdlib.h"
49 #endif /* HAVE_STDLIB_H */
50
51 #if defined (HAVE_UNISTD_H)
52 # include <unistd.h>
53 #endif
54
55 #include <stdio.h>
56
57 /* Some standard library routines. */
58 #include "rldefs.h"
59 #include "readline.h"
60 #include "history.h"
61
62 #ifndef digit
63 #define digit(c) ((c) >= '0' && (c) <= '9')
64 #endif
65
66 #ifndef isletter
67 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
68 #endif
69
70 #ifndef digit_value
71 #define digit_value(c) ((c) - '0')
72 #endif
73
74 #ifndef member
75 #define member(c, s) ((c) ? (char *)strchr ((s), (c)) : 0)
76 #endif
77
78 #ifndef isident
79 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
80 #endif
81
82 #ifndef exchange
83 #define exchange(x, y) {int temp = x; x = y; y = temp;}
84 #endif
85
86 #ifndef VI_COMMENT_BEGIN_DEFAULT
87 #define VI_COMMENT_BEGIN_DEFAULT "#"
88 #endif
89
90 #if defined (STATIC_MALLOC)
91 static char *xmalloc (), *xrealloc ();
92 #else
93 extern char *xmalloc (), *xrealloc ();
94 #endif /* STATIC_MALLOC */
95
96 /* Variables imported from readline.c */
97 extern int rl_point, rl_end, rl_mark, rl_done;
98 extern FILE *rl_instream;
99 extern int rl_line_buffer_len, rl_explicit_arg, rl_numeric_arg;
100 extern Keymap _rl_keymap;
101 extern char *rl_prompt;
102 extern char *rl_line_buffer;
103 extern int rl_arg_sign;
104
105 extern void _rl_dispatch ();
106
107 extern void rl_extend_line_buffer ();
108 extern int rl_vi_check ();
109
110 /* Non-zero means enter insertion mode. */
111 static int _rl_vi_doing_insert = 0;
112
113 /* String inserted into the line by rl_vi_comment (). */
114 char *rl_vi_comment_begin = (char *)NULL;
115
116 /* *** UNCLEAN *** */
117 /* Command keys which do movement for xxx_to commands. */
118 static char *vi_motion = " hl^$0ftFt;,%wbeWBE|";
119
120 /* Keymap used for vi replace characters. Created dynamically since
121 rarely used. */
122 static Keymap vi_replace_map = (Keymap)NULL;
123
124 /* The number of characters inserted in the last replace operation. */
125 static int vi_replace_count = 0;
126
127 /* If non-zero, we have text inserted after a c[motion] command that put
128 us implicitly into insert mode. Some people want this text to be
129 attached to the command so that it is `redoable' with `.'. */
130 static int vi_continued_command = 0;
131
132 static int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */
133 static int _rl_vi_last_repeat = 1;
134 static int _rl_vi_last_arg_sign = 1;
135 static int _rl_vi_last_motion = 0;
136 static int _rl_vi_last_search_char = 0;
137 static int _rl_vi_last_replacement = 0;
138
139 static int vi_redoing = 0;
140
141 /* Text modification commands. These are the `redoable' commands. */
142 static char *vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
143
144 static int rl_digit_loop1 ();
145
146 void
147 _rl_vi_reset_last ()
148 {
149 _rl_vi_last_command = 'i';
150 _rl_vi_last_repeat = 1;
151 _rl_vi_last_arg_sign = 1;
152 _rl_vi_last_motion = 0;
153 }
154
155 void
156 _rl_vi_set_last (key, repeat, sign)
157 int key, repeat, sign;
158 {
159 _rl_vi_last_command = key;
160 _rl_vi_last_repeat = repeat;
161 _rl_vi_last_arg_sign = sign;
162 }
163
164 /* Is the command C a VI mode text modification command? */
165 int
166 rl_vi_textmod_command (c)
167 int c;
168 {
169 return (member (c, vi_textmod) != (char *)NULL);
170 }
171
172 /* Bound to `.'. Called from command mode, so we know that we have to
173 redo a text modification command. The default for _rl_vi_last_command
174 puts you back into insert mode. */
175 rl_vi_redo (count, c)
176 int count, c;
177 {
178 if (!rl_explicit_arg)
179 {
180 rl_numeric_arg = _rl_vi_last_repeat;
181 rl_arg_sign = _rl_vi_last_arg_sign;
182 }
183
184 vi_redoing = 1;
185 _rl_dispatch (_rl_vi_last_command, _rl_keymap);
186 vi_redoing = 0;
187
188 return (0);
189 }
190
191 /* Yank the nth arg from the previous line into this line at point. */
192 rl_vi_yank_arg (count)
193 int count;
194 {
195 /* Readline thinks that the first word on a line is the 0th, while vi
196 thinks the first word on a line is the 1st. Compensate. */
197 if (rl_explicit_arg)
198 rl_yank_nth_arg (count - 1, 0);
199 else
200 rl_yank_nth_arg ('$', 0);
201
202 return (0);
203 }
204
205 /* With an argument, move back that many history lines, else move to the
206 beginning of history. */
207 rl_vi_fetch_history (count, c)
208 int count, c;
209 {
210 int current = where_history ();
211
212 /* Giving an argument of n means we want the nth command in the history
213 file. The command number is interpreted the same way that the bash
214 `history' command does it -- that is, giving an argument count of 450
215 to this command would get the command listed as number 450 in the
216 output of `history'. */
217 if (rl_explicit_arg)
218 {
219 int wanted = history_base + current - count;
220 if (wanted <= 0)
221 rl_beginning_of_history (0, 0);
222 else
223 rl_get_previous_history (wanted);
224 }
225 else
226 rl_beginning_of_history (count, 0);
227 return (0);
228 }
229
230 /* Search again for the last thing searched for. */
231 rl_vi_search_again (count, key)
232 int count, key;
233 {
234 switch (key)
235 {
236 case 'n':
237 rl_noninc_reverse_search_again (count, key);
238 break;
239
240 case 'N':
241 rl_noninc_forward_search_again (count, key);
242 break;
243 }
244 return (0);
245 }
246
247 /* Do a vi style search. */
248 rl_vi_search (count, key)
249 int count, key;
250 {
251 switch (key)
252 {
253 case '?':
254 rl_noninc_forward_search (count, key);
255 break;
256
257 case '/':
258 rl_noninc_reverse_search (count, key);
259 break;
260
261 default:
262 ding ();
263 break;
264 }
265 return (0);
266 }
267
268 /* Completion, from vi's point of view. */
269 rl_vi_complete (ignore, key)
270 int ignore, key;
271 {
272 if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
273 {
274 if (!whitespace (rl_line_buffer[rl_point + 1]))
275 rl_vi_end_word (1, 'E');
276 rl_point++;
277 }
278
279 if (key == '*')
280 rl_complete_internal ('*'); /* Expansion and replacement. */
281 else if (key == '=')
282 rl_complete_internal ('?'); /* List possible completions. */
283 else if (key == '\\')
284 rl_complete_internal (TAB); /* Standard Readline completion. */
285 else
286 rl_complete (0, key);
287
288 if (key == '*' || key == '\\')
289 {
290 _rl_vi_set_last (key, 1, rl_arg_sign);
291 rl_vi_insertion_mode ();
292 }
293 return (0);
294 }
295
296 /* Tilde expansion for vi mode. */
297 rl_vi_tilde_expand (ignore, key)
298 int ignore, key;
299 {
300 rl_tilde_expand (0, key);
301 _rl_vi_set_last (key, 1, rl_arg_sign); /* XXX */
302 rl_vi_insertion_mode ();
303 return (0);
304 }
305
306 /* Previous word in vi mode. */
307 rl_vi_prev_word (count, key)
308 int count, key;
309 {
310 if (count < 0)
311 return (rl_vi_next_word (-count, key));
312
313 if (rl_point == 0)
314 {
315 ding ();
316 return (0);
317 }
318
319 if (uppercase_p (key))
320 rl_vi_bWord (count);
321 else
322 rl_vi_bword (count);
323
324 return (0);
325 }
326
327 /* Next word in vi mode. */
328 rl_vi_next_word (count, key)
329 int count, key;
330 {
331 if (count < 0)
332 return (rl_vi_prev_word (-count, key));
333
334 if (rl_point >= (rl_end - 1))
335 {
336 ding ();
337 return (0);
338 }
339
340 if (uppercase_p (key))
341 rl_vi_fWord (count);
342 else
343 rl_vi_fword (count);
344 return (0);
345 }
346
347 /* Move to the end of the ?next? word. */
348 rl_vi_end_word (count, key)
349 int count, key;
350 {
351 if (count < 0)
352 {
353 ding ();
354 return -1;
355 }
356
357 if (uppercase_p (key))
358 rl_vi_eWord (count);
359 else
360 rl_vi_eword (count);
361 return (0);
362 }
363
364 /* Move forward a word the way that 'W' does. */
365 rl_vi_fWord (count)
366 int count;
367 {
368 while (count-- && rl_point < (rl_end - 1))
369 {
370 /* Skip until whitespace. */
371 while (!whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
372 rl_point++;
373
374 /* Now skip whitespace. */
375 while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
376 rl_point++;
377 }
378 return (0);
379 }
380
381 rl_vi_bWord (count)
382 int count;
383 {
384 while (count-- && rl_point > 0)
385 {
386 /* If we are at the start of a word, move back to whitespace so
387 we will go back to the start of the previous word. */
388 if (!whitespace (rl_line_buffer[rl_point]) &&
389 whitespace (rl_line_buffer[rl_point - 1]))
390 rl_point--;
391
392 while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
393 rl_point--;
394
395 if (rl_point > 0)
396 {
397 while (--rl_point >= 0 && !whitespace (rl_line_buffer[rl_point]));
398 rl_point++;
399 }
400 }
401 return (0);
402 }
403
404 rl_vi_eWord (count)
405 int count;
406 {
407 while (count-- && rl_point < (rl_end - 1))
408 {
409 if (!whitespace (rl_line_buffer[rl_point]))
410 rl_point++;
411
412 /* Move to the next non-whitespace character (to the start of the
413 next word). */
414 while (++rl_point < rl_end && whitespace (rl_line_buffer[rl_point]));
415
416 if (rl_point && rl_point < rl_end)
417 {
418 /* Skip whitespace. */
419 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
420 rl_point++;
421
422 /* Skip until whitespace. */
423 while (rl_point < rl_end && !whitespace (rl_line_buffer[rl_point]))
424 rl_point++;
425
426 /* Move back to the last character of the word. */
427 rl_point--;
428 }
429 }
430 return (0);
431 }
432
433 rl_vi_fword (count)
434 int count;
435 {
436 while (count-- && rl_point < (rl_end - 1))
437 {
438 /* Move to white space (really non-identifer). */
439 if (isident (rl_line_buffer[rl_point]))
440 {
441 while (isident (rl_line_buffer[rl_point]) && rl_point < rl_end)
442 rl_point++;
443 }
444 else /* if (!whitespace (rl_line_buffer[rl_point])) */
445 {
446 while (!isident (rl_line_buffer[rl_point]) &&
447 !whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
448 rl_point++;
449 }
450
451 /* Move past whitespace. */
452 while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
453 rl_point++;
454 }
455 return (0);
456 }
457
458 rl_vi_bword (count)
459 int count;
460 {
461 while (count-- && rl_point > 0)
462 {
463 int last_is_ident;
464
465 /* If we are at the start of a word, move back to whitespace
466 so we will go back to the start of the previous word. */
467 if (!whitespace (rl_line_buffer[rl_point]) &&
468 whitespace (rl_line_buffer[rl_point - 1]))
469 rl_point--;
470
471 /* If this character and the previous character are `opposite', move
472 back so we don't get messed up by the rl_point++ down there in
473 the while loop. Without this code, words like `l;' screw up the
474 function. */
475 last_is_ident = isident (rl_line_buffer[rl_point - 1]);
476 if ((isident (rl_line_buffer[rl_point]) && !last_is_ident) ||
477 (!isident (rl_line_buffer[rl_point]) && last_is_ident))
478 rl_point--;
479
480 while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
481 rl_point--;
482
483 if (rl_point > 0)
484 {
485 if (isident (rl_line_buffer[rl_point]))
486 while (--rl_point >= 0 && isident (rl_line_buffer[rl_point]));
487 else
488 while (--rl_point >= 0 && !isident (rl_line_buffer[rl_point]) &&
489 !whitespace (rl_line_buffer[rl_point]));
490 rl_point++;
491 }
492 }
493 return (0);
494 }
495
496 rl_vi_eword (count)
497 int count;
498 {
499 while (count-- && rl_point < rl_end - 1)
500 {
501 if (!whitespace (rl_line_buffer[rl_point]))
502 rl_point++;
503
504 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
505 rl_point++;
506
507 if (rl_point < rl_end)
508 {
509 if (isident (rl_line_buffer[rl_point]))
510 while (++rl_point < rl_end && isident (rl_line_buffer[rl_point]));
511 else
512 while (++rl_point < rl_end && !isident (rl_line_buffer[rl_point])
513 && !whitespace (rl_line_buffer[rl_point]));
514 }
515 rl_point--;
516 }
517 return (0);
518 }
519
520 rl_vi_insert_beg ()
521 {
522 rl_beg_of_line ();
523 rl_vi_insertion_mode ();
524 return (0);
525 }
526
527 rl_vi_append_mode ()
528 {
529 if (rl_point < rl_end)
530 rl_point++;
531 rl_vi_insertion_mode ();
532 return (0);
533 }
534
535 rl_vi_append_eol ()
536 {
537 rl_end_of_line ();
538 rl_vi_append_mode ();
539 return (0);
540 }
541
542 /* What to do in the case of C-d. */
543 rl_vi_eof_maybe (count, c)
544 int count, c;
545 {
546 return (rl_newline (1, '\n'));
547 }
548
549 /* Insertion mode stuff. */
550
551 /* Switching from one mode to the other really just involves
552 switching keymaps. */
553 rl_vi_insertion_mode ()
554 {
555 _rl_keymap = vi_insertion_keymap;
556 return (0);
557 }
558
559 void
560 _rl_vi_done_inserting ()
561 {
562 if (_rl_vi_doing_insert)
563 {
564 rl_end_undo_group ();
565 /* Now, the text between rl_undo_list->next->start and
566 rl_undo_list->next->end is what was inserted while in insert
567 mode. */
568 _rl_vi_doing_insert = 0;
569 vi_continued_command = 1;
570 }
571 else
572 vi_continued_command = 0;
573 }
574
575 rl_vi_movement_mode ()
576 {
577 if (rl_point > 0)
578 rl_backward (1);
579
580 #if 0
581 _rl_vi_reset_last ();
582 #endif
583
584 _rl_keymap = vi_movement_keymap;
585 _rl_vi_done_inserting ();
586 return (0);
587 }
588
589 rl_vi_arg_digit (count, c)
590 int count, c;
591 {
592 if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
593 return (rl_beg_of_line ());
594 else
595 return (rl_digit_argument (count, c));
596 }
597
598 rl_vi_change_case (count, ignore)
599 int count, ignore;
600 {
601 char c = 0;
602
603 /* Don't try this on an empty line. */
604 if (rl_point >= rl_end)
605 return (0);
606
607 while (count-- && rl_point < rl_end)
608 {
609 if (uppercase_p (rl_line_buffer[rl_point]))
610 c = to_lower (rl_line_buffer[rl_point]);
611 else if (lowercase_p (rl_line_buffer[rl_point]))
612 c = to_upper (rl_line_buffer[rl_point]);
613 else
614 {
615 /* Just skip over characters neither upper nor lower case. */
616 rl_forward (1);
617 continue;
618 }
619
620 /* Vi is kind of strange here. */
621 if (c)
622 {
623 rl_begin_undo_group ();
624 rl_delete (1, c);
625 rl_insert (1, c);
626 rl_end_undo_group ();
627 rl_vi_check ();
628 }
629 else
630 rl_forward (1);
631 }
632 return (0);
633 }
634
635 rl_vi_put (count, key)
636 int count, key;
637 {
638 if (!uppercase_p (key) && (rl_point + 1 <= rl_end))
639 rl_point++;
640
641 rl_yank ();
642 rl_backward (1);
643 return (0);
644 }
645
646 rl_vi_check ()
647 {
648 if (rl_point && rl_point == rl_end)
649 rl_point--;
650 return (0);
651 }
652
653 rl_vi_column (count)
654 int count;
655
656 {
657 if (count > rl_end)
658 rl_end_of_line ();
659 else
660 rl_point = count - 1;
661 return (0);
662 }
663
664 int
665 rl_vi_domove (key, nextkey)
666 int key, *nextkey;
667 {
668 int c, save;
669 int old_end;
670
671 rl_mark = rl_point;
672 c = rl_read_key ();
673 *nextkey = c;
674
675 if (!member (c, vi_motion))
676 {
677 if (digit (c))
678 {
679 save = rl_numeric_arg;
680 rl_numeric_arg = digit_value (c);
681 rl_digit_loop1 ();
682 rl_numeric_arg *= save;
683 c = rl_read_key (); /* real command */
684 *nextkey = c;
685 }
686 else if (key == c && (key == 'd' || key == 'y' || key == 'c'))
687 {
688 rl_mark = rl_end;
689 rl_beg_of_line ();
690 _rl_vi_last_motion = c;
691 return (0);
692 }
693 else
694 return (-1);
695 }
696
697 _rl_vi_last_motion = c;
698
699 /* Append a blank character temporarily so that the motion routines
700 work right at the end of the line. */
701 old_end = rl_end;
702 rl_line_buffer[rl_end++] = ' ';
703 rl_line_buffer[rl_end] = '\0';
704
705 _rl_dispatch (c, _rl_keymap);
706
707 /* Remove the blank that we added. */
708 rl_end = old_end;
709 rl_line_buffer[rl_end] = '\0';
710 if (rl_point > rl_end)
711 rl_point = rl_end;
712
713 /* No change in position means the command failed. */
714 if (rl_mark == rl_point)
715 return (-1);
716
717 /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
718 word. If we are not at the end of the line, and we are on a
719 non-whitespace character, move back one (presumably to whitespace). */
720 if ((to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
721 !whitespace (rl_line_buffer[rl_point]))
722 rl_point--;
723
724 /* If cw or cW, back up to the end of a word, so the behaviour of ce
725 or cE is the actual result. Brute-force, no subtlety. */
726 if (key == 'c' && rl_point >= rl_mark && (to_upper (c) == 'W'))
727 {
728 /* Don't move farther back than where we started. */
729 while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
730 rl_point--;
731
732 /* Posix.2 says that if cw or cW moves the cursor towards the end of
733 the line, the character under the cursor should be deleted. */
734 if (rl_point == rl_mark)
735 rl_point++;
736 else
737 {
738 /* Move past the end of the word so that the kill doesn't
739 remove the last letter of the previous word. Only do this
740 if we are not at the end of the line. */
741 if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point]))
742 rl_point++;
743 }
744 }
745
746 if (rl_mark < rl_point)
747 exchange (rl_point, rl_mark);
748
749 return (0);
750 }
751
752 /* A simplified loop for vi. Don't dispatch key at end.
753 Don't recognize minus sign? */
754 static int
755 rl_digit_loop1 ()
756 {
757 int key, c;
758
759 while (1)
760 {
761 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
762 key = c = rl_read_key ();
763
764 if (_rl_keymap[c].type == ISFUNC &&
765 _rl_keymap[c].function == rl_universal_argument)
766 {
767 rl_numeric_arg *= 4;
768 continue;
769 }
770
771 c = UNMETA (c);
772 if (numeric (c))
773 {
774 if (rl_explicit_arg)
775 rl_numeric_arg = (rl_numeric_arg * 10) + digit_value (c);
776 else
777 rl_numeric_arg = digit_value (c);
778 rl_explicit_arg = 1;
779 }
780 else
781 {
782 rl_clear_message ();
783 rl_stuff_char (key);
784 break;
785 }
786 }
787 return (0);
788 }
789
790 rl_vi_delete_to (count, key)
791 int count, key;
792 {
793 int c;
794
795 if (uppercase_p (key))
796 rl_stuff_char ('$');
797 else if (vi_redoing)
798 rl_stuff_char (_rl_vi_last_motion);
799
800 if (rl_vi_domove (key, &c))
801 {
802 ding ();
803 return -1;
804 }
805
806 /* These are the motion commands that do not require adjusting the
807 mark. */
808 if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end))
809 rl_mark++;
810
811 rl_kill_text (rl_point, rl_mark);
812 return (0);
813 }
814
815 rl_vi_change_to (count, key)
816 int count, key;
817 {
818 int c, start_pos;
819
820 if (uppercase_p (key))
821 rl_stuff_char ('$');
822 else if (vi_redoing)
823 rl_stuff_char (_rl_vi_last_motion);
824
825 start_pos = rl_point;
826
827 if (rl_vi_domove (key, &c))
828 {
829 ding ();
830 return -1;
831 }
832
833 /* These are the motion commands that do not require adjusting the
834 mark. c[wW] are handled by special-case code in rl_vi_domove(),
835 and already leave the mark at the correct location. */
836 if ((strchr (" l|hwW^0%bB", c) == 0) && (rl_mark < rl_end))
837 rl_mark++;
838
839 /* The cursor never moves with c[wW]. */
840 if ((to_upper (c) == 'W') && rl_point < start_pos)
841 rl_point = start_pos;
842
843 rl_kill_text (rl_point, rl_mark);
844
845 rl_begin_undo_group ();
846 _rl_vi_doing_insert = 1;
847 _rl_vi_set_last (key, count, rl_arg_sign);
848 rl_vi_insertion_mode ();
849
850 return (0);
851 }
852
853 rl_vi_yank_to (count, key)
854 int count, key;
855 {
856 int c, save = rl_point;
857
858 if (uppercase_p (key))
859 rl_stuff_char ('$');
860
861 if (rl_vi_domove (key, &c))
862 {
863 ding ();
864 return -1;
865 }
866
867 /* These are the motion commands that do not require adjusting the
868 mark. */
869 if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end))
870 rl_mark++;
871
872 rl_begin_undo_group ();
873 rl_kill_text (rl_point, rl_mark);
874 rl_end_undo_group ();
875 rl_do_undo ();
876 rl_point = save;
877
878 return (0);
879 }
880
881 rl_vi_delete (count)
882 int count;
883 {
884 int end;
885
886 if (rl_end == 0)
887 {
888 ding ();
889 return -1;
890 }
891
892 end = rl_point + count;
893
894 if (end >= rl_end)
895 end = rl_end;
896
897 rl_kill_text (rl_point, end);
898
899 if (rl_point > 0 && rl_point == rl_end)
900 rl_backward (1);
901 return (0);
902 }
903
904 /* Turn the current line into a comment in shell history.
905 A K*rn shell style function. */
906 rl_vi_comment ()
907 {
908 rl_beg_of_line ();
909
910 if (rl_vi_comment_begin != (char *)NULL)
911 rl_insert_text (rl_vi_comment_begin);
912 else
913 rl_insert_text (VI_COMMENT_BEGIN_DEFAULT); /* Default. */
914
915 rl_redisplay ();
916 rl_newline (1, '\010');
917 return (0);
918 }
919
920 rl_vi_first_print ()
921 {
922 return (rl_back_to_indent ());
923 }
924
925 rl_back_to_indent (ignore1, ignore2)
926 int ignore1, ignore2;
927 {
928 rl_beg_of_line ();
929 while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
930 rl_point++;
931 return (0);
932 }
933
934 /* NOTE: it is necessary that opposite directions are inverses */
935 #define FTO 1 /* forward to */
936 #define BTO -1 /* backward to */
937 #define FFIND 2 /* forward find */
938 #define BFIND -2 /* backward find */
939
940 rl_vi_char_search (count, key)
941 int count, key;
942 {
943 static char target;
944 static int orig_dir, dir;
945 int pos;
946
947 if (key == ';' || key == ',')
948 dir = (key == ';' ? orig_dir : -orig_dir);
949 else
950 {
951 if (vi_redoing)
952 target = _rl_vi_last_search_char;
953 else
954 _rl_vi_last_search_char = target = rl_getc (rl_instream);
955
956 switch (key)
957 {
958 case 't':
959 orig_dir = dir = FTO;
960 break;
961
962 case 'T':
963 orig_dir = dir = BTO;
964 break;
965
966 case 'f':
967 orig_dir = dir = FFIND;
968 break;
969
970 case 'F':
971 orig_dir = dir = BFIND;
972 break;
973 }
974 }
975
976 pos = rl_point;
977
978 while (count--)
979 {
980 if (dir < 0)
981 {
982 if (pos == 0)
983 {
984 ding ();
985 return -1;
986 }
987
988 pos--;
989 do
990 {
991 if (rl_line_buffer[pos] == target)
992 {
993 if (dir == BTO)
994 rl_point = pos + 1;
995 else
996 rl_point = pos;
997 break;
998 }
999 }
1000 while (pos--);
1001
1002 if (pos < 0)
1003 {
1004 ding ();
1005 return -1;
1006 }
1007 }
1008 else
1009 { /* dir > 0 */
1010 if (pos >= rl_end)
1011 {
1012 ding ();
1013 return -1;
1014 }
1015
1016 pos++;
1017 do
1018 {
1019 if (rl_line_buffer[pos] == target)
1020 {
1021 if (dir == FTO)
1022 rl_point = pos - 1;
1023 else
1024 rl_point = pos;
1025 break;
1026 }
1027 }
1028 while (++pos < rl_end);
1029
1030 if (pos >= (rl_end - 1))
1031 {
1032 ding ();
1033 return -1;
1034 }
1035 }
1036 }
1037 return (0);
1038 }
1039
1040 /* Match brackets */
1041 rl_vi_match ()
1042 {
1043 int count = 1, brack, pos;
1044
1045 pos = rl_point;
1046 if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
1047 {
1048 while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
1049 rl_point < rl_end - 1)
1050 rl_forward (1);
1051
1052 if (brack <= 0)
1053 {
1054 rl_point = pos;
1055 ding ();
1056 return -1;
1057 }
1058 }
1059
1060 pos = rl_point;
1061
1062 if (brack < 0)
1063 {
1064 while (count)
1065 {
1066 if (--pos >= 0)
1067 {
1068 int b = rl_vi_bracktype (rl_line_buffer[pos]);
1069 if (b == -brack)
1070 count--;
1071 else if (b == brack)
1072 count++;
1073 }
1074 else
1075 {
1076 ding ();
1077 return -1;
1078 }
1079 }
1080 }
1081 else
1082 { /* brack > 0 */
1083 while (count)
1084 {
1085 if (++pos < rl_end)
1086 {
1087 int b = rl_vi_bracktype (rl_line_buffer[pos]);
1088 if (b == -brack)
1089 count--;
1090 else if (b == brack)
1091 count++;
1092 }
1093 else
1094 {
1095 ding ();
1096 return -1;
1097 }
1098 }
1099 }
1100 rl_point = pos;
1101 return (0);
1102 }
1103
1104 int
1105 rl_vi_bracktype (c)
1106 int c;
1107 {
1108 switch (c)
1109 {
1110 case '(': return 1;
1111 case ')': return -1;
1112 case '[': return 2;
1113 case ']': return -2;
1114 case '{': return 3;
1115 case '}': return -3;
1116 default: return 0;
1117 }
1118 }
1119
1120 rl_vi_change_char (count, key)
1121 int count, key;
1122 {
1123 int c;
1124
1125 if (vi_redoing)
1126 c = _rl_vi_last_replacement;
1127 else
1128 _rl_vi_last_replacement = c = rl_getc (rl_instream);
1129
1130 if (c == '\033' || c == CTRL ('C'))
1131 return -1;
1132
1133 while (count-- && rl_point < rl_end)
1134 {
1135 rl_begin_undo_group ();
1136
1137 rl_delete (1, c);
1138 rl_insert (1, c);
1139 if (count == 0)
1140 rl_backward (1);
1141
1142 rl_end_undo_group ();
1143 }
1144 return (0);
1145 }
1146
1147 rl_vi_subst (count, key)
1148 int count, key;
1149 {
1150 rl_begin_undo_group ();
1151
1152 if (uppercase_p (key))
1153 {
1154 rl_beg_of_line ();
1155 rl_kill_line (1);
1156 }
1157 else
1158 rl_delete (count, key);
1159
1160 rl_end_undo_group ();
1161
1162 _rl_vi_set_last (key, count, rl_arg_sign);
1163
1164 rl_begin_undo_group ();
1165 _rl_vi_doing_insert = 1;
1166 rl_vi_insertion_mode ();
1167
1168 return (0);
1169 }
1170
1171 rl_vi_overstrike (count, key)
1172 int count, key;
1173 {
1174 int i;
1175
1176 if (_rl_vi_doing_insert == 0)
1177 {
1178 _rl_vi_doing_insert = 1;
1179 rl_begin_undo_group ();
1180 }
1181
1182 for (i = 0; i < count; i++)
1183 {
1184 vi_replace_count++;
1185 rl_begin_undo_group ();
1186
1187 if (rl_point < rl_end)
1188 {
1189 rl_delete (1, key);
1190 rl_insert (1, key);
1191 }
1192 else
1193 rl_insert (1, key);
1194
1195 rl_end_undo_group ();
1196 }
1197 return (0);
1198 }
1199
1200 rl_vi_overstrike_delete (count)
1201 int count;
1202 {
1203 int i, s;
1204
1205 for (i = 0; i < count; i++)
1206 {
1207 if (vi_replace_count == 0)
1208 {
1209 ding ();
1210 break;
1211 }
1212 s = rl_point;
1213
1214 if (rl_do_undo ())
1215 vi_replace_count--;
1216
1217 if (rl_point == s)
1218 rl_backward (1);
1219 }
1220
1221 if (vi_replace_count == 0 && _rl_vi_doing_insert)
1222 {
1223 rl_end_undo_group ();
1224 rl_do_undo ();
1225 _rl_vi_doing_insert = 0;
1226 }
1227 return (0);
1228 }
1229
1230 rl_vi_replace (count, key)
1231 int count, key;
1232 {
1233 int i;
1234
1235 vi_replace_count = 0;
1236
1237 if (!vi_replace_map)
1238 {
1239 vi_replace_map = rl_make_bare_keymap ();
1240
1241 for (i = ' '; i < 127; i++)
1242 vi_replace_map[i].function = rl_vi_overstrike;
1243
1244 vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
1245 vi_replace_map[ESC].function = rl_vi_movement_mode;
1246 vi_replace_map[RETURN].function = rl_newline;
1247 vi_replace_map[NEWLINE].function = rl_newline;
1248
1249 /* If the normal vi insertion keymap has ^H bound to erase, do the
1250 same here. Probably should remove the assignment to RUBOUT up
1251 there, but I don't think it will make a difference in real life. */
1252 if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC &&
1253 vi_insertion_keymap[CTRL ('H')].function == rl_rubout)
1254 vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete;
1255
1256 }
1257 _rl_keymap = vi_replace_map;
1258 return (0);
1259 }
1260
1261 #if 0
1262 /* Try to complete the word we are standing on or the word that ends with
1263 the previous character. A space matches everything. Word delimiters are
1264 space and ;. */
1265 rl_vi_possible_completions()
1266 {
1267 int save_pos = rl_point;
1268
1269 if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';')
1270 {
1271 while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' &&
1272 rl_line_buffer[rl_point] != ';')
1273 rl_point++;
1274 }
1275 else if (rl_line_buffer[rl_point - 1] == ';')
1276 {
1277 ding ();
1278 return (0);
1279 }
1280
1281 rl_possible_completions ();
1282 rl_point = save_pos;
1283
1284 return (0);
1285 }
1286 #endif
1287
1288 #if defined (STATIC_MALLOC)
1289
1290 /* **************************************************************** */
1291 /* */
1292 /* xmalloc and xrealloc () */
1293 /* */
1294 /* **************************************************************** */
1295
1296 static void memory_error_and_abort ();
1297
1298 static char *
1299 xmalloc (bytes)
1300 int bytes;
1301 {
1302 char *temp = (char *)malloc ((size_t)bytes);
1303
1304 if (!temp)
1305 memory_error_and_abort ();
1306 return (temp);
1307 }
1308
1309 static char *
1310 xrealloc (pointer, bytes)
1311 char *pointer;
1312 int bytes;
1313 {
1314 char *temp;
1315
1316 if (!pointer)
1317 temp = (char *)xmalloc (bytes);
1318 else
1319 temp = (char *)realloc (pointer, bytes);
1320
1321 if (!temp)
1322 memory_error_and_abort ();
1323
1324 return (temp);
1325 }
1326
1327 static void
1328 memory_error_and_abort ()
1329 {
1330 fprintf (stderr, "readline: Out of virtual memory!\n");
1331 abort ();
1332 }
1333 #endif /* STATIC_MALLOC */
1334
1335 #endif /* VI_MODE */