1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987-2012 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library (Readline), a library
7 for reading lines of text with interactive input and history editing.
9 Readline is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Readline is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Readline. If not, see <http://www.gnu.org/licenses/>.
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
31 #if defined (HAVE_UNISTD_H)
32 # include <unistd.h> /* for _POSIX_VERSION */
33 #endif /* HAVE_UNISTD_H */
35 #if defined (HAVE_STDLIB_H)
38 # include "ansi_stdlib.h"
39 #endif /* HAVE_STDLIB_H */
43 /* System-specific feature definitions and include files. */
46 /* Some standard library routines. */
50 #include "rlprivate.h"
53 extern void replace_history_data PARAMS((int, histdata_t *, histdata_t *));
55 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
57 int _rl_doing_an_undo = 0;
59 /* How many unclosed undo groups we currently have. */
60 int _rl_undo_group_level = 0;
62 /* The current undo list for THE_LINE. */
63 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
65 /* **************************************************************** */
67 /* Undo, and Undoing */
69 /* **************************************************************** */
72 alloc_undo_entry (what, start, end, text)
79 temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
85 temp->next = (UNDO_LIST *)NULL;
89 /* Remember how to undo something. Concatenate some undos if that
92 rl_add_undo (what, start, end, text)
99 temp = alloc_undo_entry (what, start, end, text);
100 temp->next = rl_undo_list;
104 /* Free an UNDO_LIST */
106 _rl_free_undo_list (ul)
116 if (release->what == UNDO_DELETE)
117 xfree (release->text);
123 /* Free the existing undo list. */
127 UNDO_LIST *release, *orig_list;
129 orig_list = rl_undo_list;
130 _rl_free_undo_list (rl_undo_list);
131 rl_undo_list = (UNDO_LIST *)NULL;
132 replace_history_data (-1, (histdata_t *)orig_list, (histdata_t *)NULL);
136 _rl_copy_undo_entry (entry)
141 new = alloc_undo_entry (entry->what, entry->start, entry->end, (char *)NULL);
142 new->text = entry->text ? savestring (entry->text) : 0;
147 _rl_copy_undo_list (head)
150 UNDO_LIST *list, *new, *roving, *c;
159 c = _rl_copy_undo_entry (list);
165 roving = roving->next;
174 /* Undo the next thing in the list. Return 0 if there
175 is nothing to undo, or non-zero if there was. */
180 int waiting_for_begin, start, end;
181 HIST_ENTRY *cur, *temp;
183 #define TRANS(i) ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))
185 start = end = waiting_for_begin = 0;
188 if (rl_undo_list == 0)
191 _rl_doing_an_undo = 1;
192 RL_SETSTATE(RL_STATE_UNDOING);
194 /* To better support vi-mode, a start or end value of -1 means
195 rl_point, and a value of -2 means rl_end. */
196 if (rl_undo_list->what == UNDO_DELETE || rl_undo_list->what == UNDO_INSERT)
198 start = TRANS (rl_undo_list->start);
199 end = TRANS (rl_undo_list->end);
202 switch (rl_undo_list->what)
204 /* Undoing deletes means inserting some text. */
207 rl_insert_text (rl_undo_list->text);
208 xfree (rl_undo_list->text);
211 /* Undoing inserts means deleting some text. */
213 rl_delete_text (start, end);
217 /* Undoing an END means undoing everything 'til we get to a BEGIN. */
222 /* Undoing a BEGIN means that we are done with this group. */
224 if (waiting_for_begin)
231 _rl_doing_an_undo = 0;
232 RL_UNSETSTATE(RL_STATE_UNDOING);
234 release = rl_undo_list;
235 rl_undo_list = rl_undo_list->next;
237 /* If we are editing a history entry, make sure the change is replicated
238 in the history entry's line */
239 cur = current_history ();
240 if (cur && cur->data && (UNDO_LIST *)cur->data == release)
242 temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list);
244 FREE (temp->timestamp);
248 replace_history_data (-1, (histdata_t *)release, (histdata_t *)rl_undo_list);
252 while (waiting_for_begin);
259 _rl_fix_last_undo_of_type (type, start, end)
260 int type, start, end;
264 for (rl = rl_undo_list; rl; rl = rl->next)
266 if (rl->what == type)
276 /* Begin a group. Subsequent undos are undone as an atomic operation. */
278 rl_begin_undo_group ()
280 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
281 _rl_undo_group_level++;
285 /* End an undo group started with rl_begin_undo_group (). */
289 rl_add_undo (UNDO_END, 0, 0, 0);
290 _rl_undo_group_level--;
294 /* Save an undo entry for the text from START to END. */
296 rl_modifying (start, end)
306 char *temp = rl_copy_text (start, end);
307 rl_begin_undo_group ();
308 rl_add_undo (UNDO_DELETE, start, end, temp);
309 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
310 rl_end_undo_group ();
315 /* Revert the current line to its previous state. */
317 rl_revert_line (count, key)
320 if (rl_undo_list == 0)
326 #if defined (VI_MODE)
327 if (rl_editing_mode == vi_mode)
328 rl_point = rl_mark = 0; /* rl_end should be set correctly */
335 /* Do some undoing of things that were done. */
337 rl_undo_command (count, key)
341 return 0; /* Nothing to do. */