gdb/readline: Fix date in last ChangeLog entry
[deliverable/binutils-gdb.git] / readline / undo.c
index 25c287b5a2df3df04c7d9fd98f1978949c6d7016..ae65d38043a7acff72e94785aa669401696260dd 100644 (file)
@@ -1,25 +1,24 @@
-/* readline.c -- a general facility for reading lines of input
-   with emacs style editing and completion. */
+/* undo.c - manage list of changes to lines, offering opportunity to undo them */
 
-/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2017 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.
+   This file is part of the GNU Readline Library (Readline), a library
+   for reading lines of text with interactive input and history editing.      
 
-   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 2, or
+   Readline 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 3 of the License, or
    (at your option) any later version.
 
-   The GNU Readline Library is distributed in the hope that it will be
-   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
-   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   Readline is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
-   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,
-   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Readline.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 #define READLINE_LIBRARY
 
 #if defined (HAVE_CONFIG_H)
 #include "rlprivate.h"
 #include "xmalloc.h"
 
+extern void _hs_replace_history_data PARAMS((int, histdata_t *, histdata_t *));
+
+extern HIST_ENTRY *_rl_saved_line_for_history;
+
 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
    the undo list. */
 int _rl_doing_an_undo = 0;
@@ -66,54 +69,115 @@ UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
 /*                                                                 */
 /* **************************************************************** */
 
-/* Remember how to undo something.  Concatenate some undos if that
-   seems right. */
-void
-rl_add_undo (what, start, end, text)
-     enum undo_code what;
-     int start, end;
-     char *text;
+static UNDO_LIST *
+alloc_undo_entry (enum undo_code what, int start, int end, char *text)
 {
-  UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
+  UNDO_LIST *temp;
+
+  temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
   temp->what = what;
   temp->start = start;
   temp->end = end;
   temp->text = text;
+
+  temp->next = (UNDO_LIST *)NULL;
+  return temp;
+}
+
+/* Remember how to undo something.  Concatenate some undos if that
+   seems right. */
+void
+rl_add_undo (enum undo_code what, int start, int end, char *text)
+{
+  UNDO_LIST *temp;
+
+  temp = alloc_undo_entry (what, start, end, text);
   temp->next = rl_undo_list;
   rl_undo_list = temp;
 }
 
-/* Free the existing undo list. */
+/* Free an UNDO_LIST */
 void
-rl_free_undo_list ()
+_rl_free_undo_list (UNDO_LIST *ul)
 {
-  while (rl_undo_list)
+  UNDO_LIST *release;
+
+  while (ul)
     {
-      UNDO_LIST *release = rl_undo_list;
-      rl_undo_list = rl_undo_list->next;
+      release = ul;
+      ul = ul->next;
 
       if (release->what == UNDO_DELETE)
-       free (release->text);
+       xfree (release->text);
 
-      free (release);
+      xfree (release);
     }
+}
+
+/* Free the existing undo list. */
+void
+rl_free_undo_list (void)
+{
+  UNDO_LIST *release, *orig_list;
+
+  orig_list = rl_undo_list;
+  _rl_free_undo_list (rl_undo_list);
   rl_undo_list = (UNDO_LIST *)NULL;
+  _hs_replace_history_data (-1, (histdata_t *)orig_list, (histdata_t *)NULL);
+}
+
+UNDO_LIST *
+_rl_copy_undo_entry (UNDO_LIST *entry)
+{
+  UNDO_LIST *new;
+
+  new = alloc_undo_entry (entry->what, entry->start, entry->end, (char *)NULL);
+  new->text = entry->text ? savestring (entry->text) : 0;
+  return new;
+}
+
+UNDO_LIST *
+_rl_copy_undo_list (UNDO_LIST *head)
+{
+  UNDO_LIST *list, *new, *roving, *c;
+
+  if (head == 0)
+    return head;
+
+  list = head;
+  new = 0;
+  while (list)
+    {
+      c = _rl_copy_undo_entry (list);
+      if (new == 0)
+       roving = new = c;
+      else
+       {
+         roving->next = c;
+         roving = roving->next;
+       }
+      list = list->next;
+    }
+
+  roving->next = 0;
+  return new;
 }
 
 /* Undo the next thing in the list.  Return 0 if there
    is nothing to undo, or non-zero if there was. */
 int
-rl_do_undo ()
+rl_do_undo (void)
 {
-  UNDO_LIST *release;
+  UNDO_LIST *release, *search;
   int waiting_for_begin, start, end;
+  HIST_ENTRY *cur, *temp;
 
 #define TRANS(i) ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))
 
   start = end = waiting_for_begin = 0;
   do
     {
-      if (!rl_undo_list)
+      if (rl_undo_list == 0)
        return (0);
 
       _rl_doing_an_undo = 1;
@@ -133,7 +197,7 @@ rl_do_undo ()
        case UNDO_DELETE:
          rl_point = start;
          rl_insert_text (rl_undo_list->text);
-         free (rl_undo_list->text);
+         xfree (rl_undo_list->text);
          break;
 
        /* Undoing inserts means deleting some text. */
@@ -161,7 +225,44 @@ rl_do_undo ()
 
       release = rl_undo_list;
       rl_undo_list = rl_undo_list->next;
-      free (release);
+      release->next = 0;       /* XXX */
+
+      /* If we are editing a history entry, make sure the change is replicated
+        in the history entry's line */
+      cur = current_history ();
+      if (cur && cur->data && (UNDO_LIST *)cur->data == release)
+       {
+         temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list);
+         xfree (temp->line);
+         FREE (temp->timestamp);
+         xfree (temp);
+       }
+
+      /* Make sure there aren't any history entries with that undo list */
+      _hs_replace_history_data (-1, (histdata_t *)release, (histdata_t *)rl_undo_list);
+
+      /* And make sure this list isn't anywhere in the saved line for history */
+      if (_rl_saved_line_for_history && _rl_saved_line_for_history->data)
+       {
+         /* Brute force; no finesse here */
+         search = (UNDO_LIST *)_rl_saved_line_for_history->data;
+         if (search == release)
+           _rl_saved_line_for_history->data = rl_undo_list;
+         else
+           {
+             while (search->next)
+               {
+                 if (search->next == release)
+                   {
+                     search->next = rl_undo_list;
+                     break;
+                   }
+                 search = search->next;
+               }
+           }
+       }
+
+      xfree (release);
     }
   while (waiting_for_begin);
 
@@ -170,8 +271,7 @@ rl_do_undo ()
 #undef TRANS
 
 int
-_rl_fix_last_undo_of_type (type, start, end)
-     int type, start, end;
+_rl_fix_last_undo_of_type (int type, int start, int end)
 {
   UNDO_LIST *rl;
 
@@ -189,7 +289,7 @@ _rl_fix_last_undo_of_type (type, start, end)
 
 /* Begin a group.  Subsequent undos are undone as an atomic operation. */
 int
-rl_begin_undo_group ()
+rl_begin_undo_group (void)
 {
   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
   _rl_undo_group_level++;
@@ -198,7 +298,7 @@ rl_begin_undo_group ()
 
 /* End an undo group started with rl_begin_undo_group (). */
 int
-rl_end_undo_group ()
+rl_end_undo_group (void)
 {
   rl_add_undo (UNDO_END, 0, 0, 0);
   _rl_undo_group_level--;
@@ -207,8 +307,7 @@ rl_end_undo_group ()
 
 /* Save an undo entry for the text from START to END. */
 int
-rl_modifying (start, end)
-     int start, end;
+rl_modifying (int start, int end)
 {
   if (start > end)
     {
@@ -228,23 +327,26 @@ rl_modifying (start, end)
 
 /* Revert the current line to its previous state. */
 int
-rl_revert_line (count, key)
-     int count, key;
+rl_revert_line (int count, int key)
 {
-  if (!rl_undo_list)
+  if (rl_undo_list == 0)
     rl_ding ();
   else
     {
       while (rl_undo_list)
        rl_do_undo ();
+#if defined (VI_MODE)
+      if (rl_editing_mode == vi_mode)
+       rl_point = rl_mark = 0;         /* rl_end should be set correctly */
+#endif
     }
+    
   return 0;
 }
 
 /* Do some undoing of things that were done. */
 int
-rl_undo_command (count, key)
-     int count, key;
+rl_undo_command (int count, int key)
 {
   if (count < 0)
     return 0;  /* Nothing to do. */
This page took 0.046467 seconds and 4 git commands to generate.