Imported from ../bash-3.0.16.tar.gz.
[platform/upstream/bash.git] / lib / readline / vi_mode.c
index 09ccecf..de723a1 100644 (file)
@@ -1,7 +1,7 @@
 /* vi_mode.c -- A vi emulation mode for Bash.
    Derived from code written by Jeff Sparkes (jsparkes@bnr.ca).  */
 
-/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
 
    This file is part of the GNU Readline Library, a library for
    reading lines of text with interactive input and history editing.
@@ -51,6 +51,8 @@
 
 /* Some standard library routines. */
 #include "rldefs.h"
+#include "rlmbutil.h"
+
 #include "readline.h"
 #include "history.h"
 
@@ -61,9 +63,7 @@
 #define member(c, s) ((c) ? (char *)strchr ((s), (c)) != (char *)NULL : 0)
 #endif
 
-#ifndef exchange
-#define exchange(x, y) do {int temp = x; x = y; y = temp;} while (0)
-#endif
+int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */
 
 /* Non-zero means enter insertion mode. */
 static int _rl_vi_doing_insert;
@@ -85,11 +85,14 @@ static int vi_continued_command;
 static char *vi_insert_buffer;
 static int vi_insert_buffer_size;
 
-static int _rl_vi_last_command = 'i';  /* default `.' puts you in insert mode */
 static int _rl_vi_last_repeat = 1;
 static int _rl_vi_last_arg_sign = 1;
 static int _rl_vi_last_motion;
+#if defined (HANDLE_MULTIBYTE)
+static char _rl_vi_last_search_mbchar[MB_LEN_MAX];
+#else
 static int _rl_vi_last_search_char;
+#endif
 static int _rl_vi_last_replacement;
 
 static int _rl_vi_last_key_before_insert;
@@ -133,6 +136,16 @@ _rl_vi_set_last (key, repeat, sign)
   _rl_vi_last_arg_sign = sign;
 }
 
+/* A convenience function that calls _rl_vi_set_last to save the last command
+   information and enters insertion mode. */
+void
+rl_vi_start_inserting (key, repeat, sign)
+     int key, repeat, sign;
+{
+  _rl_vi_set_last (key, repeat, sign);
+  rl_vi_insertion_mode (1, key);
+}
+
 /* Is the command C a VI mode text modification command? */
 int
 _rl_vi_textmod_command (c)
@@ -158,12 +171,15 @@ int
 rl_vi_redo (count, c)
      int count, c;
 {
+  int r;
+
   if (!rl_explicit_arg)
     {
       rl_numeric_arg = _rl_vi_last_repeat;
       rl_arg_sign = _rl_vi_last_arg_sign;
     }
 
+  r = 0;
   vi_redoing = 1;
   /* If we're redoing an insert with `i', stuff in the inserted text
      and do not go into insertion mode. */
@@ -175,10 +191,10 @@ rl_vi_redo (count, c)
        rl_point--;
     }
   else
-    _rl_dispatch (_rl_vi_last_command, _rl_keymap);
+    r = _rl_dispatch (_rl_vi_last_command, _rl_keymap);
   vi_redoing = 0;
 
-  return (0);
+  return (r);
 }
 
 /* A placeholder for further expansion. */
@@ -256,10 +272,12 @@ rl_vi_search (count, key)
   switch (key)
     {
     case '?':
+      _rl_free_saved_history_line ();
       rl_noninc_forward_search (count, key);
       break;
 
     case '/':
+      _rl_free_saved_history_line ();
       rl_noninc_reverse_search (count, key);
       break;
 
@@ -292,10 +310,8 @@ rl_vi_complete (ignore, key)
     rl_complete (0, key);
 
   if (key == '*' || key == '\\')
-    {
-      _rl_vi_set_last (key, 1, rl_arg_sign);
-      rl_vi_insertion_mode (1, key);
-    }
+    rl_vi_start_inserting (key, 1, rl_arg_sign);
+
   return (0);
 }
 
@@ -305,8 +321,7 @@ rl_vi_tilde_expand (ignore, key)
      int ignore, key;
 {
   rl_tilde_expand (0, key);
-  _rl_vi_set_last (key, 1, rl_arg_sign);       /* XXX */
-  rl_vi_insertion_mode (1, key);
+  rl_vi_start_inserting (key, 1, rl_arg_sign);
   return (0);
 }
 
@@ -424,7 +439,8 @@ rl_vi_eWord (count, ignore)
 
       /* Move to the next non-whitespace character (to the start of the
         next word). */
-      while (++rl_point < rl_end && whitespace (rl_line_buffer[rl_point]));
+      while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
+       rl_point++;
 
       if (rl_point && rl_point < rl_end)
        {
@@ -547,7 +563,17 @@ rl_vi_append_mode (count, key)
      int count, key;
 {
   if (rl_point < rl_end)
-    rl_point++;
+    {
+      if (MB_CUR_MAX == 1 || rl_byte_oriented)
+       rl_point++;
+      else
+        {
+          int point = rl_point;
+          rl_forward_char (1, key);
+          if (point == rl_point)
+            rl_point = rl_end;
+        }
+    }
   rl_vi_insertion_mode (1, key);
   return (0);
 }
@@ -612,6 +638,7 @@ _rl_vi_done_inserting ()
 {
   if (_rl_vi_doing_insert)
     {
+      /* The `C', `s', and `S' commands set this. */
       rl_end_undo_group ();
       /* Now, the text between rl_undo_list->next->start and
         rl_undo_list->next->end is what was inserted while in insert
@@ -624,7 +651,7 @@ _rl_vi_done_inserting ()
     }
   else
     {
-      if (_rl_vi_last_key_before_insert == 'i' && rl_undo_list)
+      if ((_rl_vi_last_key_before_insert == 'i' || _rl_vi_last_key_before_insert == 'a') && rl_undo_list)
         _rl_vi_save_insert (rl_undo_list);
       /* XXX - Other keys probably need to be checked. */
       else if (_rl_vi_last_key_before_insert == 'C')
@@ -640,7 +667,7 @@ rl_vi_movement_mode (count, key)
      int count, key;
 {
   if (rl_point > 0)
-    rl_backward (1, key);
+    rl_backward_char (1, key);
 
   _rl_keymap = vi_movement_keymap;
   _rl_vi_done_inserting ();
@@ -657,16 +684,73 @@ rl_vi_arg_digit (count, c)
     return (rl_digit_argument (count, c));
 }
 
+/* Change the case of the next COUNT characters. */
+#if defined (HANDLE_MULTIBYTE)
+static int
+_rl_vi_change_mbchar_case (count)
+     int count;
+{
+  wchar_t wc;
+  char mb[MB_LEN_MAX+1];
+  int mblen, p;
+  mbstate_t ps;
+
+  memset (&ps, 0, sizeof (mbstate_t));
+  if (_rl_adjust_point (rl_line_buffer, rl_point, &ps) > 0)
+    count--;
+  while (count-- && rl_point < rl_end)
+    {
+      mbrtowc (&wc, rl_line_buffer + rl_point, rl_end - rl_point, &ps);
+      if (iswupper (wc))
+       wc = towlower (wc);
+      else if (iswlower (wc))
+       wc = towupper (wc);
+      else
+       {
+         /* Just skip over chars neither upper nor lower case */
+         rl_forward_char (1, 0);
+         continue;
+       }
+
+      /* Vi is kind of strange here. */
+      if (wc)
+       {
+         p = rl_point;
+         mblen = wcrtomb (mb, wc, &ps);
+         if (mblen >= 0)
+           mb[mblen] = '\0';
+         rl_begin_undo_group ();
+         rl_vi_delete (1, 0);
+         if (rl_point < p)     /* Did we retreat at EOL? */
+           rl_point++; /* XXX - should we advance more than 1 for mbchar? */
+         rl_insert_text (mb);
+         rl_end_undo_group ();
+         rl_vi_check ();
+       }
+      else
+        rl_forward_char (1, 0);
+    }
+
+  return 0;
+}
+#endif
+
 int
 rl_vi_change_case (count, ignore)
      int count, ignore;
 {
-  char c = 0;
+  int c, p;
 
   /* Don't try this on an empty line. */
   if (rl_point >= rl_end)
     return (0);
 
+  c = 0;
+#if defined (HANDLE_MULTIBYTE)
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    return (_rl_vi_change_mbchar_case (count));
+#endif
+
   while (count-- && rl_point < rl_end)
     {
       if (_rl_uppercase_p (rl_line_buffer[rl_point]))
@@ -676,21 +760,24 @@ rl_vi_change_case (count, ignore)
       else
        {
          /* Just skip over characters neither upper nor lower case. */
-         rl_forward (1, c);
+         rl_forward_char (1, c);
          continue;
        }
 
       /* Vi is kind of strange here. */
       if (c)
        {
+         p = rl_point;
          rl_begin_undo_group ();
-         rl_delete (1, c);
-         rl_insert (1, c);
+         rl_vi_delete (1, c);
+         if (rl_point < p)     /* Did we retreat at EOL? */
+           rl_point++;
+         _rl_insert_char (1, c);
          rl_end_undo_group ();
          rl_vi_check ();
         }
       else
-       rl_forward (1, c);
+       rl_forward_char (1, c);
     }
   return (0);
 }
@@ -700,10 +787,12 @@ rl_vi_put (count, key)
      int count, key;
 {
   if (!_rl_uppercase_p (key) && (rl_point + 1 <= rl_end))
-    rl_point++;
+    rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
+
+  while (count--)
+    rl_yank (1, key);
 
-  rl_yank (1, key);
-  rl_backward (1, key);
+  rl_backward_char (1, key);
   return (0);
 }
 
@@ -711,7 +800,12 @@ int
 rl_vi_check ()
 {
   if (rl_point && rl_point == rl_end)
-    rl_point--;
+    {
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
+      else
+        rl_point--;
+    }
   return (0);
 }
 
@@ -745,6 +839,7 @@ rl_vi_domove (key, nextkey)
        {
          save = rl_numeric_arg;
          rl_numeric_arg = _rl_digit_value (c);
+         rl_explicit_arg = 1;
          rl_digit_loop1 ();
          rl_numeric_arg *= save;
          RL_SETSTATE(RL_STATE_MOREINPUT);
@@ -813,7 +908,7 @@ rl_vi_domove (key, nextkey)
     }
 
   if (rl_mark < rl_point)
-    exchange (rl_point, rl_mark);
+    SWAP (rl_point, rl_mark);
 
   return (0);
 }
@@ -943,8 +1038,7 @@ rl_vi_change_to (count, key)
       /* `C' does not save the text inserted for undoing or redoing. */
       if (_rl_uppercase_p (key) == 0)
         _rl_vi_doing_insert = 1;
-      _rl_vi_set_last (key, count, rl_arg_sign);
-      rl_vi_insertion_mode (1, key);
+      rl_vi_start_inserting (key, rl_numeric_arg, rl_arg_sign);
     }
 
   return (0);
@@ -991,7 +1085,10 @@ rl_vi_delete (count, key)
       return -1;
     }
 
-  end = rl_point + count;
+  if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+    end = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
+  else
+    end = rl_point + count;
 
   if (end >= rl_end)
     end = rl_end;
@@ -999,7 +1096,7 @@ rl_vi_delete (count, key)
   rl_kill_text (rl_point, end);
   
   if (rl_point > 0 && rl_point == rl_end)
-    rl_backward (1, key);
+    rl_backward_char (1, key);
   return (0);
 }
 
@@ -1024,7 +1121,12 @@ int
 rl_vi_char_search (count, key)
      int count, key;
 {
+#if defined (HANDLE_MULTIBYTE)
+  static char *target;
+  static int mb_len;
+#else
   static char target;
+#endif
   static int orig_dir, dir;
 
   if (key == ';' || key == ',')
@@ -1032,12 +1134,21 @@ rl_vi_char_search (count, key)
   else
     {
       if (vi_redoing)
+#if defined (HANDLE_MULTIBYTE)
+       target = _rl_vi_last_search_mbchar;
+#else
        target = _rl_vi_last_search_char;
+#endif
       else
        {
+#if defined (HANDLE_MULTIBYTE)
+         mb_len = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX);
+         target = _rl_vi_last_search_mbchar;
+#else
          RL_SETSTATE(RL_STATE_MOREINPUT);
          _rl_vi_last_search_char = target = rl_read_key ();
          RL_UNSETSTATE(RL_STATE_MOREINPUT);
+#endif
        }
 
       switch (key)
@@ -1060,7 +1171,11 @@ rl_vi_char_search (count, key)
         }
     }
 
+#if defined (HANDLE_MULTIBYTE)
+   return (_rl_char_search_internal (count, dir, target, mb_len));
+#else
   return (_rl_char_search_internal (count, dir, target));
+#endif
 }
 
 /* Match brackets */
@@ -1068,14 +1183,25 @@ int
 rl_vi_match (ignore, key)
      int ignore, key;
 {
-  int count = 1, brack, pos;
+  int count = 1, brack, pos, tmp, pre;
 
   pos = rl_point;
   if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
     {
-      while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
-            rl_point < rl_end - 1)
-       rl_forward (1, key);
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
+           {
+             pre = rl_point;
+             rl_forward_char (1, key);
+             if (pre == rl_point)
+               break;
+           }
+       }
+      else
+       while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
+               rl_point < rl_end - 1)
+         rl_forward_char (1, key);
 
       if (brack <= 0)
        {
@@ -1091,7 +1217,16 @@ rl_vi_match (ignore, key)
     {
       while (count)
        {
-         if (--pos >= 0)
+         tmp = pos;
+         if (MB_CUR_MAX == 1 || rl_byte_oriented)
+           pos--;
+         else
+           {
+             pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY);
+             if (tmp == pos)
+               pos--;
+           }
+         if (pos >= 0)
            {
              int b = rl_vi_bracktype (rl_line_buffer[pos]);
              if (b == -brack)
@@ -1110,7 +1245,12 @@ rl_vi_match (ignore, key)
     {                  /* brack > 0 */
       while (count)
        {
-         if (++pos < rl_end)
+         if (MB_CUR_MAX == 1 || rl_byte_oriented)
+           pos++;
+         else
+           pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY);
+
+         if (pos < rl_end)
            {
              int b = rl_vi_bracktype (rl_line_buffer[pos]);
              if (b == -brack)
@@ -1145,11 +1285,16 @@ rl_vi_bracktype (c)
     }
 }
 
+/* XXX - think about reading an entire mbchar with _rl_read_mbchar and
+   inserting it in one bunch instead of the loop below (like in
+   rl_vi_char_search or _rl_vi_change_mbchar_case).  Set c to mbchar[0]
+   for test against 033 or ^C.  Make sure that _rl_read_mbchar does
+   this right. */
 int
 rl_vi_change_char (count, key)
      int count, key;
 {
-  int c;
+  int c, p;
 
   if (vi_redoing)
     c = _rl_vi_last_replacement;
@@ -1163,17 +1308,33 @@ rl_vi_change_char (count, key)
   if (c == '\033' || c == CTRL ('C'))
     return -1;
 
+  rl_begin_undo_group ();
   while (count-- && rl_point < rl_end)
     {
-      rl_begin_undo_group ();
-
-      rl_delete (1, c);
-      rl_insert (1, c);
-      if (count == 0)
-       rl_backward (1, c);
-
-      rl_end_undo_group ();
+      p = rl_point;
+      rl_vi_delete (1, c);
+#if defined (HANDLE_MULTIBYTE)
+      if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+       {
+         if (rl_point < p)             /* Did we retreat at EOL? */
+           rl_point++;
+         while (_rl_insert_char (1, c))
+           {
+             RL_SETSTATE (RL_STATE_MOREINPUT);
+             c = rl_read_key ();
+             RL_UNSETSTATE (RL_STATE_MOREINPUT);
+           }
+       }
+      else
+#endif
+       {
+         if (rl_point < p)             /* Did we retreat at EOL? */
+           rl_point++;
+         _rl_insert_char (1, c);
+       }
     }
+  rl_end_undo_group ();
+
   return (0);
 }
 
@@ -1181,66 +1342,29 @@ int
 rl_vi_subst (count, key)
      int count, key;
 {
-  rl_begin_undo_group ();
+  /* If we are redoing, rl_vi_change_to will stuff the last motion char */
+  if (vi_redoing == 0)
+    rl_stuff_char ((key == 'S') ? 'c' : 'l');  /* `S' == `cc', `s' == `cl' */
 
-  if (_rl_uppercase_p (key))
-    {
-      rl_beg_of_line (1, key);
-      rl_kill_line (1, key);
-    }
-  else
-    rl_delete_text (rl_point, rl_point+count);
-
-  rl_end_undo_group ();
-
-  _rl_vi_set_last (key, count, rl_arg_sign);
-
-  if (vi_redoing)
-    {
-      int o = _rl_doing_an_undo;
-
-      _rl_doing_an_undo = 1;
-      if (vi_insert_buffer && *vi_insert_buffer)
-       rl_insert_text (vi_insert_buffer);
-      _rl_doing_an_undo = o;
-    }
-  else
-    {
-      rl_begin_undo_group ();
-      _rl_vi_doing_insert = 1;
-      rl_vi_insertion_mode (1, key);
-    }
-
-  return (0);
+  return (rl_vi_change_to (count, 'c'));
 }
 
 int
 rl_vi_overstrike (count, key)
      int count, key;
 {
-  int i;
-
   if (_rl_vi_doing_insert == 0)
     {
       _rl_vi_doing_insert = 1;
       rl_begin_undo_group ();
     }
 
-  for (i = 0; i < count; i++)
+  if (count > 0)
     {
-      vi_replace_count++;
-      rl_begin_undo_group ();
-
-      if (rl_point < rl_end)
-       {
-         rl_delete (1, key);
-         rl_insert (1, key);
-       }
-      else
-       rl_insert (1, key);
-
-      rl_end_undo_group ();
+      _rl_overwrite_char (count, key);
+      vi_replace_count += count;
     }
+
   return (0);
 }
 
@@ -1263,7 +1387,7 @@ rl_vi_overstrike_delete (count, key)
        vi_replace_count--;
 
       if (rl_point == s)
-       rl_backward (1, key);
+       rl_backward_char (1, key);
     }
 
   if (vi_replace_count == 0 && _rl_vi_doing_insert)