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