1 /* undo.c - manage list of changes to lines, offering opportunity to undo them */
3 /* Copyright (C) 1987-2017 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
31 # include <unistd.h> /* for _POSIX_VERSION */
32 #endif /* HAVE_UNISTD_H */
34 #if defined (HAVE_STDLIB_H)
37 # include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
42 /* System-specific feature definitions and include files. */
45 /* Some standard library routines. */
49 #include "rlprivate.h"
52 extern void _hs_replace_history_data PARAMS((int, histdata_t *, histdata_t *));
54 extern HIST_ENTRY *_rl_saved_line_for_history;
56 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
58 int _rl_doing_an_undo = 0;
60 /* How many unclosed undo groups we currently have. */
61 int _rl_undo_group_level = 0;
63 /* The current undo list for THE_LINE. */
64 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
66 /* **************************************************************** */
68 /* Undo, and Undoing */
70 /* **************************************************************** */
73 alloc_undo_entry (enum undo_code what, int start, int end, char *text)
77 temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
83 temp->next = (UNDO_LIST *)NULL;
87 /* Remember how to undo something. Concatenate some undos if that
90 rl_add_undo (enum undo_code what, int start, int end, char *text)
94 temp = alloc_undo_entry (what, start, end, text);
95 temp->next = rl_undo_list;
99 /* Free an UNDO_LIST */
101 _rl_free_undo_list (UNDO_LIST *ul)
110 if (release->what == UNDO_DELETE)
111 xfree (release->text);
117 /* Free the existing undo list. */
119 rl_free_undo_list (void)
121 UNDO_LIST *release, *orig_list;
123 orig_list = rl_undo_list;
124 _rl_free_undo_list (rl_undo_list);
125 rl_undo_list = (UNDO_LIST *)NULL;
126 _hs_replace_history_data (-1, (histdata_t *)orig_list, (histdata_t *)NULL);
130 _rl_copy_undo_entry (UNDO_LIST *entry)
134 new = alloc_undo_entry (entry->what, entry->start, entry->end, (char *)NULL);
135 new->text = entry->text ? savestring (entry->text) : 0;
140 _rl_copy_undo_list (UNDO_LIST *head)
142 UNDO_LIST *list, *new, *roving, *c;
151 c = _rl_copy_undo_entry (list);
157 roving = roving->next;
166 /* Undo the next thing in the list. Return 0 if there
167 is nothing to undo, or non-zero if there was. */
171 UNDO_LIST *release, *search;
172 int waiting_for_begin, start, end;
173 HIST_ENTRY *cur, *temp;
175 #define TRANS(i) ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))
177 start = end = waiting_for_begin = 0;
180 if (rl_undo_list == 0)
183 _rl_doing_an_undo = 1;
184 RL_SETSTATE(RL_STATE_UNDOING);
186 /* To better support vi-mode, a start or end value of -1 means
187 rl_point, and a value of -2 means rl_end. */
188 if (rl_undo_list->what == UNDO_DELETE || rl_undo_list->what == UNDO_INSERT)
190 start = TRANS (rl_undo_list->start);
191 end = TRANS (rl_undo_list->end);
194 switch (rl_undo_list->what)
196 /* Undoing deletes means inserting some text. */
199 rl_insert_text (rl_undo_list->text);
200 xfree (rl_undo_list->text);
203 /* Undoing inserts means deleting some text. */
205 rl_delete_text (start, end);
209 /* Undoing an END means undoing everything 'til we get to a BEGIN. */
214 /* Undoing a BEGIN means that we are done with this group. */
216 if (waiting_for_begin)
223 _rl_doing_an_undo = 0;
224 RL_UNSETSTATE(RL_STATE_UNDOING);
226 release = rl_undo_list;
227 rl_undo_list = rl_undo_list->next;
228 release->next = 0; /* XXX */
230 /* If we are editing a history entry, make sure the change is replicated
231 in the history entry's line */
232 cur = current_history ();
233 if (cur && cur->data && (UNDO_LIST *)cur->data == release)
235 temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list);
237 FREE (temp->timestamp);
241 /* Make sure there aren't any history entries with that undo list */
242 _hs_replace_history_data (-1, (histdata_t *)release, (histdata_t *)rl_undo_list);
244 /* And make sure this list isn't anywhere in the saved line for history */
245 if (_rl_saved_line_for_history && _rl_saved_line_for_history->data)
247 /* Brute force; no finesse here */
248 search = (UNDO_LIST *)_rl_saved_line_for_history->data;
249 if (search == release)
250 _rl_saved_line_for_history->data = rl_undo_list;
255 if (search->next == release)
257 search->next = rl_undo_list;
260 search = search->next;
267 while (waiting_for_begin);
274 _rl_fix_last_undo_of_type (int type, int start, int end)
278 for (rl = rl_undo_list; rl; rl = rl->next)
280 if (rl->what == type)
290 /* Begin a group. Subsequent undos are undone as an atomic operation. */
292 rl_begin_undo_group (void)
294 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
295 _rl_undo_group_level++;
299 /* End an undo group started with rl_begin_undo_group (). */
301 rl_end_undo_group (void)
303 rl_add_undo (UNDO_END, 0, 0, 0);
304 _rl_undo_group_level--;
308 /* Save an undo entry for the text from START to END. */
310 rl_modifying (int start, int end)
319 char *temp = rl_copy_text (start, end);
320 rl_begin_undo_group ();
321 rl_add_undo (UNDO_DELETE, start, end, temp);
322 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
323 rl_end_undo_group ();
328 /* Revert the current line to its previous state. */
330 rl_revert_line (int count, int key)
332 if (rl_undo_list == 0)
338 #if defined (VI_MODE)
339 if (rl_editing_mode == vi_mode)
340 rl_point = rl_mark = 0; /* rl_end should be set correctly */
347 /* Do some undoing of things that were done. */
349 rl_undo_command (int count, int key)
352 return 0; /* Nothing to do. */