home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / readline / vi_mode.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  31KB  |  1,382 lines

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