Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / lib / readline / vi_mode.c
index c730296..b9c0ea8 100644 (file)
@@ -8,7 +8,7 @@
 
    The GNU Readline Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU General Public License
-   as published by the Free Software Foundation; either version 1, or
+   as published by the Free Software Foundation; either version 2, or
    (at your option) any later version.
 
    The GNU Readline Library is distributed in the hope that it will be
@@ -19,7 +19,7 @@
    The GNU General Public License is often shipped with GNU software, and
    is generally kept in a file called COPYING or LICENSE.  If you do not
    have a copy of the license, write to the Free Software Foundation,
-   675 Mass Ave, Cambridge, MA 02139, USA. */
+   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 #define READLINE_LIBRARY
 
 /* **************************************************************** */
@@ -54,6 +54,9 @@
 #include "readline.h"
 #include "history.h"
 
+#include "rlprivate.h"
+#include "xmalloc.h"
+
 #ifndef _rl_digit_p
 #define _rl_digit_p(c)  ((c) >= '0' && (c) <= '9')
 #endif
 #define exchange(x, y) do {int temp = x; x = y; y = temp;} while (0)
 #endif
 
-extern char *xmalloc (), *xrealloc ();
-
-/* Variables imported from readline.c */
-extern int rl_point, rl_end, rl_mark, rl_done;
-extern FILE *rl_instream;
-extern int rl_line_buffer_len, rl_explicit_arg, rl_numeric_arg;
-extern Keymap _rl_keymap;
-extern char *rl_prompt;
-extern char *rl_line_buffer;
-extern int rl_arg_sign;
-
-extern int _rl_doing_an_undo;
-extern int _rl_undo_group_level;
-
-extern void _rl_dispatch ();
-extern int _rl_char_search_internal ();
-
-extern void rl_extend_line_buffer ();
-extern int rl_vi_check ();
-
 /* Non-zero means enter insertion mode. */
 static int _rl_vi_doing_insert;
 
 /* Command keys which do movement for xxx_to commands. */
-static char *vi_motion = " hl^$0ftFt;,%wbeWBE|";
+static const char *vi_motion = " hl^$0ftFt;,%wbeWBE|";
 
 /* Keymap used for vi replace characters.  Created dynamically since
    rarely used. */
@@ -126,12 +109,12 @@ static int _rl_vi_last_key_before_insert;
 static int vi_redoing;
 
 /* Text modification commands.  These are the `redoable' commands. */
-static char *vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
+static const char *vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
 
 /* Arrays for the saved marks. */
 static int vi_mark_chars[27];
 
-static int rl_digit_loop1 ();
+static int rl_digit_loop1 __P((void));
 
 void
 _rl_vi_initialize_line ()
@@ -291,7 +274,7 @@ rl_vi_search (count, key)
       break;
 
     default:
-      ding ();
+      rl_ding ();
       break;
     }
   return (0);
@@ -347,14 +330,14 @@ rl_vi_prev_word (count, key)
 
   if (rl_point == 0)
     {
-      ding ();
+      rl_ding ();
       return (0);
     }
 
   if (_rl_uppercase_p (key))
-    rl_vi_bWord (count);
+    rl_vi_bWord (count, key);
   else
-    rl_vi_bword (count);
+    rl_vi_bword (count, key);
 
   return (0);
 }
@@ -369,14 +352,14 @@ rl_vi_next_word (count, key)
 
   if (rl_point >= (rl_end - 1))
     {
-      ding ();
+      rl_ding ();
       return (0);
     }
 
   if (_rl_uppercase_p (key))
-    rl_vi_fWord (count);
+    rl_vi_fWord (count, key);
   else
-    rl_vi_fword (count);
+    rl_vi_fword (count, key);
   return (0);
 }
 
@@ -387,21 +370,21 @@ rl_vi_end_word (count, key)
 {
   if (count < 0)
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
 
   if (_rl_uppercase_p (key))
-    rl_vi_eWord (count);
+    rl_vi_eWord (count, key);
   else
-    rl_vi_eword (count);
+    rl_vi_eword (count, key);
   return (0);
 }
 
 /* Move forward a word the way that 'W' does. */
 int
-rl_vi_fWord (count)
-     int count;
+rl_vi_fWord (count, ignore)
+     int count, ignore;
 {
   while (count-- && rl_point < (rl_end - 1))
     {
@@ -417,8 +400,8 @@ rl_vi_fWord (count)
 }
 
 int
-rl_vi_bWord (count)
-     int count;
+rl_vi_bWord (count, ignore)
+     int count, ignore;
 {
   while (count-- && rl_point > 0)
     {
@@ -441,8 +424,8 @@ rl_vi_bWord (count)
 }
 
 int
-rl_vi_eWord (count)
-     int count;
+rl_vi_eWord (count, ignore)
+     int count, ignore;
 {
   while (count-- && rl_point < (rl_end - 1))
     {
@@ -471,8 +454,8 @@ rl_vi_eWord (count)
 }
 
 int
-rl_vi_fword (count)
-     int count;
+rl_vi_fword (count, ignore)
+     int count, ignore;
 {
   while (count-- && rl_point < (rl_end - 1))
     {
@@ -497,8 +480,8 @@ rl_vi_fword (count)
 }
 
 int
-rl_vi_bword (count)
-     int count;
+rl_vi_bword (count, ignore)
+     int count, ignore;
 {
   while (count-- && rl_point > 0)
     {
@@ -536,8 +519,8 @@ rl_vi_bword (count)
 }
 
 int
-rl_vi_eword (count)
-     int count;
+rl_vi_eword (count, ignore)
+     int count, ignore;
 {
   while (count-- && rl_point < rl_end - 1)
     {
@@ -615,6 +598,13 @@ _rl_vi_save_insert (up)
 {
   int len, start, end;
 
+  if (up == 0)
+    {
+      if (vi_insert_buffer_size >= 1)
+       vi_insert_buffer[0] = '\0';
+      return;
+    }
+
   start = up->start;
   end = up->end;
   len = end - start + 1;
@@ -722,7 +712,7 @@ rl_vi_put (count, key)
   if (!_rl_uppercase_p (key) && (rl_point + 1 <= rl_end))
     rl_point++;
 
-  rl_yank ();
+  rl_yank (1, key);
   rl_backward (1, key);
   return (0);
 }
@@ -754,7 +744,9 @@ rl_vi_domove (key, nextkey)
   int old_end;
 
   rl_mark = rl_point;
+  RL_SETSTATE(RL_STATE_MOREINPUT);
   c = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
   *nextkey = c;
 
   if (!member (c, vi_motion))
@@ -765,7 +757,9 @@ rl_vi_domove (key, nextkey)
          rl_numeric_arg = _rl_digit_value (c);
          rl_digit_loop1 ();
          rl_numeric_arg *= save;
+         RL_SETSTATE(RL_STATE_MOREINPUT);
          c = rl_read_key ();   /* real command */
+         RL_UNSETSTATE(RL_STATE_MOREINPUT);
          *nextkey = c;
        }
       else if (key == c && (key == 'd' || key == 'y' || key == 'c'))
@@ -835,16 +829,28 @@ rl_vi_domove (key, nextkey)
 }
 
 /* A simplified loop for vi. Don't dispatch key at end.
-   Don't recognize minus sign? */
+   Don't recognize minus sign?
+   Should this do rl_save_prompt/rl_restore_prompt? */
 static int
 rl_digit_loop1 ()
 {
   int key, c;
 
+  RL_SETSTATE(RL_STATE_NUMERICARG);
   while (1)
     {
+      if (rl_numeric_arg > 1000000)
+       {
+         rl_explicit_arg = rl_numeric_arg = 0;
+         rl_ding ();
+         rl_clear_message ();
+         RL_UNSETSTATE(RL_STATE_NUMERICARG);
+         return 1;
+       }
       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
+      RL_SETSTATE(RL_STATE_MOREINPUT);
       key = c = rl_read_key ();
+      RL_UNSETSTATE(RL_STATE_MOREINPUT);
 
       if (_rl_keymap[c].type == ISFUNC &&
          _rl_keymap[c].function == rl_universal_argument)
@@ -869,6 +875,8 @@ rl_digit_loop1 ()
          break;
        }
     }
+
+  RL_UNSETSTATE(RL_STATE_NUMERICARG);
   return (0);
 }
 
@@ -885,7 +893,7 @@ rl_vi_delete_to (count, key)
 
   if (rl_vi_domove (key, &c))
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
 
@@ -913,7 +921,7 @@ rl_vi_change_to (count, key)
 
   if (rl_vi_domove (key, &c))
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
 
@@ -963,7 +971,7 @@ rl_vi_yank_to (count, key)
 
   if (rl_vi_domove (key, &c))
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
 
@@ -989,7 +997,7 @@ rl_vi_delete (count, key)
 
   if (rl_end == 0)
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
 
@@ -1036,7 +1044,11 @@ rl_vi_char_search (count, key)
       if (vi_redoing)
        target = _rl_vi_last_search_char;
       else
-       _rl_vi_last_search_char = target = rl_getc (rl_instream);
+       {
+         RL_SETSTATE(RL_STATE_MOREINPUT);
+         _rl_vi_last_search_char = target = rl_read_key ();
+         RL_UNSETSTATE(RL_STATE_MOREINPUT);
+       }
 
       switch (key)
         {
@@ -1078,7 +1090,7 @@ rl_vi_match (ignore, key)
       if (brack <= 0)
        {
          rl_point = pos;
-         ding ();
+         rl_ding ();
          return -1;
        }
     }
@@ -1099,7 +1111,7 @@ rl_vi_match (ignore, key)
            }
          else
            {
-             ding ();
+             rl_ding ();
              return -1;
            }
        }
@@ -1118,7 +1130,7 @@ rl_vi_match (ignore, key)
            }
          else
            {
-             ding ();
+             rl_ding ();
              return -1;
            }
        }
@@ -1152,7 +1164,11 @@ rl_vi_change_char (count, key)
   if (vi_redoing)
     c = _rl_vi_last_replacement;
   else
-    _rl_vi_last_replacement = c = rl_getc (rl_instream);
+    {
+      RL_SETSTATE(RL_STATE_MOREINPUT);
+      _rl_vi_last_replacement = c = rl_read_key ();
+      RL_UNSETSTATE(RL_STATE_MOREINPUT);
+    }
 
   if (c == '\033' || c == CTRL ('C'))
     return -1;
@@ -1248,7 +1264,7 @@ rl_vi_overstrike_delete (count, key)
     {
       if (vi_replace_count == 0)
        {
-         ding ();
+         rl_ding ();
          break;
        }
       s = rl_point;
@@ -1318,7 +1334,7 @@ rl_vi_possible_completions()
     }
   else if (rl_line_buffer[rl_point - 1] == ';')
     {
-      ding ();
+      rl_ding ();
       return (0);
     }
 
@@ -1336,10 +1352,13 @@ rl_vi_set_mark (count, key)
 {
   int ch;
 
+  RL_SETSTATE(RL_STATE_MOREINPUT);
   ch = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
   if (_rl_lowercase_p (ch) == 0)
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
   ch -= 'a';
@@ -1353,7 +1372,10 @@ rl_vi_goto_mark (count, key)
 {
   int ch;
 
+  RL_SETSTATE(RL_STATE_MOREINPUT);
   ch = rl_read_key ();
+  RL_UNSETSTATE(RL_STATE_MOREINPUT);
+
   if (ch == '`')
     {
       rl_point = rl_mark;
@@ -1361,14 +1383,14 @@ rl_vi_goto_mark (count, key)
     }
   else if (_rl_lowercase_p (ch) == 0)
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
 
   ch -= 'a';
   if (vi_mark_chars[ch] == -1)
     {
-      ding ();
+      rl_ding ();
       return -1;
     }
   rl_point = vi_mark_chars[ch];