1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
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 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
52 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
54 int _rl_doing_an_undo = 0;
56 /* How many unclosed undo groups we currently have. */
57 int _rl_undo_group_level = 0;
59 /* The current undo list for THE_LINE. */
60 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
62 /* **************************************************************** */
64 /* Undo, and Undoing */
66 /* **************************************************************** */
68 /* Remember how to undo something. Concatenate some undos if that
71 rl_add_undo (what, start, end, text)
76 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
81 temp->next = rl_undo_list;
85 /* Free the existing undo list. */
91 UNDO_LIST *release = rl_undo_list;
92 rl_undo_list = rl_undo_list->next;
94 if (release->what == UNDO_DELETE)
99 rl_undo_list = (UNDO_LIST *)NULL;
102 /* Undo the next thing in the list. Return 0 if there
103 is nothing to undo, or non-zero if there was. */
108 int waiting_for_begin = 0;
111 #define TRANS(i) ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))
118 _rl_doing_an_undo = 1;
120 /* To better support vi-mode, a start or end value of -1 means
121 rl_point, and a value of -2 means rl_end. */
122 if (rl_undo_list->what == UNDO_DELETE || rl_undo_list->what == UNDO_INSERT)
124 start = TRANS (rl_undo_list->start);
125 end = TRANS (rl_undo_list->end);
128 switch (rl_undo_list->what)
130 /* Undoing deletes means inserting some text. */
133 rl_insert_text (rl_undo_list->text);
134 free (rl_undo_list->text);
137 /* Undoing inserts means deleting some text. */
139 rl_delete_text (start, end);
143 /* Undoing an END means undoing everything 'til we get to a BEGIN. */
148 /* Undoing a BEGIN means that we are done with this group. */
150 if (waiting_for_begin)
157 _rl_doing_an_undo = 0;
159 release = rl_undo_list;
160 rl_undo_list = rl_undo_list->next;
163 while (waiting_for_begin);
170 _rl_fix_last_undo_of_type (type, start, end)
171 int type, start, end;
175 for (rl = rl_undo_list; rl; rl = rl->next)
177 if (rl->what == type)
187 /* Begin a group. Subsequent undos are undone as an atomic operation. */
189 rl_begin_undo_group ()
191 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
192 _rl_undo_group_level++;
196 /* End an undo group started with rl_begin_undo_group (). */
200 rl_add_undo (UNDO_END, 0, 0, 0);
201 _rl_undo_group_level--;
205 /* Save an undo entry for the text from START to END. */
207 rl_modifying (start, end)
217 char *temp = rl_copy_text (start, end);
218 rl_begin_undo_group ();
219 rl_add_undo (UNDO_DELETE, start, end, temp);
220 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
221 rl_end_undo_group ();
226 /* Revert the current line to its previous state. */
228 rl_revert_line (count, key)
241 /* Do some undoing of things that were done. */
243 rl_undo_command (count, key)
247 return 0; /* Nothing to do. */