Bash  5.0-beta2
Bash - Bourne Again shell
undo.c File Reference
#include <sys/types.h>
#include "ansi_stdlib.h"
#include <stdio.h>
#include "rldefs.h"
#include "readline.h"
#include "history.h"
#include "rlprivate.h"
#include "xmalloc.h"

Go to the source code of this file.

Macros

#define READLINE_LIBRARY
 
#define TRANS(i)   ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))
 

Functions

void _hs_replace_history_data ()
 
static UNDO_LISTalloc_undo_entry (enum undo_code what, int start, int end, char *text)
 
void rl_add_undo (enum undo_code what, int start, int end, char *text)
 
void _rl_free_undo_list (UNDO_LIST *ul)
 
void rl_free_undo_list (void)
 
UNDO_LIST_rl_copy_undo_entry (UNDO_LIST *entry)
 
UNDO_LIST_rl_copy_undo_list (UNDO_LIST *head)
 
int rl_do_undo (void)
 
int _rl_fix_last_undo_of_type (int type, int start, int end)
 
int rl_begin_undo_group (void)
 
int rl_end_undo_group (void)
 
int rl_modifying (int start, int end)
 
int rl_revert_line (int count, int key)
 
int rl_undo_command (int count, int key)
 

Variables

HIST_ENTRY_rl_saved_line_for_history
 
int _rl_doing_an_undo = 0
 
int _rl_undo_group_level = 0
 
UNDO_LISTrl_undo_list = (UNDO_LIST *)NULL
 

Macro Definition Documentation

◆ READLINE_LIBRARY

#define READLINE_LIBRARY

Definition at line 22 of file undo.c.

◆ TRANS

#define TRANS (   i)    ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))

Referenced by rl_do_undo().

Function Documentation

◆ _hs_replace_history_data()

void _hs_replace_history_data ( )

Referenced by rl_do_undo(), and rl_free_undo_list().

Here is the caller graph for this function:

◆ alloc_undo_entry()

static UNDO_LIST* alloc_undo_entry ( enum undo_code  what,
int  start,
int  end,
char *  text 
)
static

Definition at line 73 of file undo.c.

References undo_list::end, undo_list::next, NULL, undo_list::start, temp, undo_list::text, undo_list::what, and xmalloc().

Referenced by _rl_copy_undo_entry(), and rl_add_undo().

74 {
75  UNDO_LIST *temp;
76 
77  temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
78  temp->what = what;
79  temp->start = start;
80  temp->end = end;
81  temp->text = text;
82 
83  temp->next = (UNDO_LIST *)NULL;
84  return temp;
85 }
struct undo_list * next
Definition: readline.h:58
char * text
Definition: readline.h:60
int end
Definition: readline.h:59
#define NULL
Definition: general.h:53
enum undo_code what
Definition: readline.h:61
static char * xmalloc()
temp
Definition: subst.c:10040
int start
Definition: readline.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rl_add_undo()

void rl_add_undo ( enum undo_code  what,
int  start,
int  end,
char *  text 
)

Definition at line 90 of file undo.c.

References alloc_undo_entry(), undo_list::next, rl_undo_list, and temp.

Referenced by rl_begin_undo_group(), rl_delete_text(), rl_end_undo_group(), rl_insert_text(), and rl_modifying().

91 {
92  UNDO_LIST *temp;
93 
94  temp = alloc_undo_entry (what, start, end, text);
95  temp->next = rl_undo_list;
97 }
struct undo_list * next
Definition: readline.h:58
UNDO_LIST * rl_undo_list
Definition: undo.c:64
static UNDO_LIST * alloc_undo_entry(enum undo_code what, int start, int end, char *text)
Definition: undo.c:73
temp
Definition: subst.c:10040
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _rl_free_undo_list()

void _rl_free_undo_list ( UNDO_LIST ul)

Definition at line 101 of file undo.c.

References undo_list::next, undo_list::text, UNDO_DELETE, undo_list::what, and xfree().

Referenced by rl_free_undo_list().

102 {
103  UNDO_LIST *release;
104 
105  while (ul)
106  {
107  release = ul;
108  ul = ul->next;
109 
110  if (release->what == UNDO_DELETE)
111  xfree (release->text);
112 
113  xfree (release);
114  }
115 }
struct undo_list * next
Definition: readline.h:58
char * text
Definition: readline.h:60
enum undo_code what
Definition: readline.h:61
void xfree(char *string)
Definition: xmalloc.c:89
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rl_free_undo_list()

void rl_free_undo_list ( void  )

Definition at line 119 of file undo.c.

References _hs_replace_history_data(), _rl_free_undo_list(), NULL, and rl_undo_list.

Referenced by _rl_nsearch_dosearch(), free_undo_list(), make_history_line_current(), readline_internal_teardown(), rl_free_line_state(), rl_replace_line(), and rl_vi_movement_mode().

120 {
121  UNDO_LIST *release, *orig_list;
122 
123  orig_list = rl_undo_list;
126  _hs_replace_history_data (-1, (histdata_t *)orig_list, (histdata_t *)NULL);
127 }
UNDO_LIST * rl_undo_list
Definition: undo.c:64
void _hs_replace_history_data()
void _rl_free_undo_list(UNDO_LIST *ul)
Definition: undo.c:101
#define NULL
Definition: general.h:53
char * histdata_t
Definition: history.h:42
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _rl_copy_undo_entry()

UNDO_LIST* _rl_copy_undo_entry ( UNDO_LIST entry)

Definition at line 130 of file undo.c.

References alloc_undo_entry(), undo_list::end, NULL, savestring(), undo_list::start, undo_list::text, and undo_list::what.

Referenced by _rl_copy_undo_list().

131 {
132  UNDO_LIST *new;
133 
134  new = alloc_undo_entry (entry->what, entry->start, entry->end, (char *)NULL);
135  new->text = entry->text ? savestring (entry->text) : 0;
136  return new;
137 }
static UNDO_LIST * alloc_undo_entry(enum undo_code what, int start, int end, char *text)
Definition: undo.c:73
char * savestring(const char *s)
Definition: savestring.c:33
char * text
Definition: readline.h:60
int end
Definition: readline.h:59
#define NULL
Definition: general.h:53
enum undo_code what
Definition: readline.h:61
int start
Definition: readline.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _rl_copy_undo_list()

UNDO_LIST* _rl_copy_undo_list ( UNDO_LIST head)

Definition at line 140 of file undo.c.

References _rl_copy_undo_entry(), c, list, and undo_list::next.

141 {
142  UNDO_LIST *list, *new, *roving, *c;
143 
144  if (head == 0)
145  return head;
146 
147  list = head;
148  new = 0;
149  while (list)
150  {
151  c = _rl_copy_undo_entry (list);
152  if (new == 0)
153  roving = new = c;
154  else
155  {
156  roving->next = c;
157  roving = roving->next;
158  }
159  list = list->next;
160  }
161 
162  roving->next = 0;
163  return new;
164 }
struct undo_list * next
Definition: readline.h:58
list
Definition: subst.c:10212
register GCHAR c
Definition: glob_loop.c:26
UNDO_LIST * _rl_copy_undo_entry(UNDO_LIST *entry)
Definition: undo.c:130
Here is the call graph for this function:

◆ rl_do_undo()

int rl_do_undo ( void  )

Definition at line 169 of file undo.c.

References _hs_replace_history_data(), current_history(), _hist_entry::data, undo_list::end, FREE, if(), _hist_entry::line, undo_list::next, replace_history_entry(), rl_delete_text(), rl_ding(), rl_insert_text(), rl_line_buffer, rl_point, RL_SETSTATE, RL_STATE_UNDOING, rl_undo_list, RL_UNSETSTATE, undo_list::start, temp, undo_list::text, _hist_entry::timestamp, TRANS, UNDO_BEGIN, UNDO_DELETE, UNDO_END, UNDO_INSERT, undo_list::what, where_history(), and xfree().

Referenced by _rl_revert_all_lines(), rl_revert_line(), rl_undo_command(), rl_vi_overstrike_delete(), rl_yank_last_arg(), and vi_yank_dispatch().

170 {
171  UNDO_LIST *release, *search;
172  int waiting_for_begin, start, end;
173  HIST_ENTRY *cur, *temp;
174 
175 #define TRANS(i) ((i) == -1 ? rl_point : ((i) == -2 ? rl_end : (i)))
176 
177  start = end = waiting_for_begin = 0;
178  do
179  {
180  if (rl_undo_list == 0)
181  return (0);
182 
183  _rl_doing_an_undo = 1;
185 
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. */
189  {
190  start = TRANS (rl_undo_list->start);
191  end = TRANS (rl_undo_list->end);
192  }
193 
194  switch (rl_undo_list->what)
195  {
196  /* Undoing deletes means inserting some text. */
197  case UNDO_DELETE:
198  rl_point = start;
201  break;
202 
203  /* Undoing inserts means deleting some text. */
204  case UNDO_INSERT:
205  rl_delete_text (start, end);
206  rl_point = start;
207  break;
208 
209  /* Undoing an END means undoing everything 'til we get to a BEGIN. */
210  case UNDO_END:
211  waiting_for_begin++;
212  break;
213 
214  /* Undoing a BEGIN means that we are done with this group. */
215  case UNDO_BEGIN:
216  if (waiting_for_begin)
217  waiting_for_begin--;
218  else
219  rl_ding ();
220  break;
221  }
222 
223  _rl_doing_an_undo = 0;
225 
226  release = rl_undo_list;
228  release->next = 0; /* XXX */
229 
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)
234  {
236  xfree (temp->line);
237  FREE (temp->timestamp);
238  xfree (temp);
239  }
240 
241  /* Make sure there aren't any history entries with that undo list */
243 
244  /* And make sure this list isn't anywhere in the saved line for history */
246  {
247  /* Brute force; no finesse here */
249  if (search == release)
251  else
252  {
253  while (search->next)
254  {
255  if (search->next == release)
256  {
257  search->next = rl_undo_list;
258  break;
259  }
260  search = search->next;
261  }
262  }
263  }
264 
265  xfree (release);
266  }
267  while (waiting_for_begin);
268 
269  return (1);
270 }
int rl_delete_text(int from, int to)
Definition: text.c:123
struct undo_list * next
Definition: readline.h:58
UNDO_LIST * rl_undo_list
Definition: undo.c:64
#define RL_UNSETSTATE(x)
Definition: readline.h:901
int where_history(void)
Definition: history.c:153
char * timestamp
Definition: history.h:48
HIST_ENTRY * _rl_saved_line_for_history
Definition: misc.c:306
void _hs_replace_history_data()
#define TRANS(i)
#define RL_STATE_UNDOING
Definition: readline.h:888
char * rl_line_buffer
Definition: readline.c:251
int _rl_doing_an_undo
Definition: undo.c:58
int rl_insert_text(const char *string)
Definition: text.c:86
HIST_ENTRY * replace_history_entry(int which, const char *line, histdata_t data)
Definition: history.c:381
histdata_t data
Definition: history.h:49
char * text
Definition: readline.h:60
int end
Definition: readline.h:59
int rl_point
Definition: readline.c:157
enum undo_code what
Definition: readline.h:61
Definition: history.h:46
#define FREE(s)
Definition: general.h:172
if(return(GMATCH(string==0||pattern==0)
Definition: sm_loop.c:42
HIST_ENTRY * current_history(void)
Definition: history.c:181
#define RL_SETSTATE(x)
Definition: readline.h:900
int rl_ding()
Definition: terminal.c:681
void xfree(char *string)
Definition: xmalloc.c:89
char * histdata_t
Definition: history.h:42
char * line
Definition: history.h:47
temp
Definition: subst.c:10040
int start
Definition: readline.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _rl_fix_last_undo_of_type()

int _rl_fix_last_undo_of_type ( int  type,
int  start,
int  end 
)

Definition at line 274 of file undo.c.

References undo_list::end, undo_list::next, undo_list::start, and undo_list::what.

275 {
276  UNDO_LIST *rl;
277 
278  for (rl = rl_undo_list; rl; rl = rl->next)
279  {
280  if (rl->what == type)
281  {
282  rl->start = start;
283  rl->end = end;
284  return 0;
285  }
286  }
287  return 1;
288 }
struct undo_list * next
Definition: readline.h:58
UNDO_LIST * rl_undo_list
Definition: undo.c:64
int end
Definition: readline.h:59
enum undo_code what
Definition: readline.h:61
int start
Definition: readline.h:59

◆ rl_begin_undo_group()

int rl_begin_undo_group ( void  )

Definition at line 292 of file undo.c.

References rl_add_undo(), and UNDO_BEGIN.

Referenced by _rl_overwrite_char(), _rl_overwrite_rubout(), _rl_replace_text(), _rl_vi_change_char(), _rl_vi_replace_insert(), _rl_vi_stuff_insert(), insert_all_matches(), rl_kill_full_line(), rl_modifying(), rl_transpose_chars(), rl_transpose_words(), rl_vi_arg_digit(), rl_vi_change_case(), rl_vi_overstrike(), rl_vi_start_inserting(), rl_yank_nth_arg_internal(), vi_change_dispatch(), and vi_yank_dispatch().

293 {
294  rl_add_undo (UNDO_BEGIN, 0, 0, 0);
296  return 0;
297 }
void rl_add_undo(enum undo_code what, int start, int end, char *text)
Definition: undo.c:90
int _rl_undo_group_level
Definition: undo.c:61
Here is the call graph for this function:

◆ rl_end_undo_group()

int rl_end_undo_group ( void  )

Definition at line 301 of file undo.c.

References rl_add_undo(), and UNDO_END.

Referenced by _rl_overwrite_char(), _rl_overwrite_rubout(), _rl_replace_text(), _rl_vi_change_char(), _rl_vi_done_inserting(), _rl_vi_replace_insert(), _rl_vi_stuff_insert(), insert_all_matches(), rl_kill_full_line(), rl_modifying(), rl_transpose_chars(), rl_transpose_words(), rl_vi_arg_digit(), rl_vi_change_case(), rl_vi_overstrike_delete(), rl_yank_nth_arg_internal(), vi_change_dispatch(), and vi_yank_dispatch().

302 {
303  rl_add_undo (UNDO_END, 0, 0, 0);
305  return 0;
306 }
void rl_add_undo(enum undo_code what, int start, int end, char *text)
Definition: undo.c:90
int _rl_undo_group_level
Definition: undo.c:61
Here is the call graph for this function:

◆ rl_modifying()

int rl_modifying ( int  start,
int  end 
)

Definition at line 310 of file undo.c.

References NULL, rl_add_undo(), rl_begin_undo_group(), rl_copy_text(), rl_end_undo_group(), SWAP, temp, UNDO_DELETE, and UNDO_INSERT.

Referenced by invert_case_line(), and rl_change_case().

311 {
312  if (start > end)
313  {
314  SWAP (start, end);
315  }
316 
317  if (start != end)
318  {
319  char *temp = rl_copy_text (start, end);
321  rl_add_undo (UNDO_DELETE, start, end, temp);
322  rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
324  }
325  return 0;
326 }
#define SWAP(s, e)
Definition: rldefs.h:160
int rl_end_undo_group(void)
Definition: undo.c:301
char * rl_copy_text(int from, int to)
Definition: util.c:145
void rl_add_undo(enum undo_code what, int start, int end, char *text)
Definition: undo.c:90
#define NULL
Definition: general.h:53
int rl_begin_undo_group(void)
Definition: undo.c:292
temp
Definition: subst.c:10040
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rl_revert_line()

int rl_revert_line ( int  count,
int  key 
)

Definition at line 330 of file undo.c.

References rl_ding(), rl_do_undo(), rl_editing_mode, rl_mark, rl_point, and vi_mode.

Referenced by readline_internal_teardown().

331 {
332  if (rl_undo_list == 0)
333  rl_ding ();
334  else
335  {
336  while (rl_undo_list)
337  rl_do_undo ();
338 #if defined (VI_MODE)
339  if (rl_editing_mode == vi_mode)
340  rl_point = rl_mark = 0; /* rl_end should be set correctly */
341 #endif
342  }
343 
344  return 0;
345 }
UNDO_LIST * rl_undo_list
Definition: undo.c:64
int rl_mark
Definition: readline.c:160
int rl_editing_mode
Definition: readline.c:123
int rl_point
Definition: readline.c:157
int rl_do_undo(void)
Definition: undo.c:169
int rl_ding()
Definition: terminal.c:681
#define vi_mode
Definition: rldefs.h:94
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rl_undo_command()

int rl_undo_command ( int  count,
int  key 
)

Definition at line 349 of file undo.c.

References rl_ding(), and rl_do_undo().

Referenced by rl_vi_undo().

350 {
351  if (count < 0)
352  return 0; /* Nothing to do. */
353 
354  while (count)
355  {
356  if (rl_do_undo ())
357  count--;
358  else
359  {
360  rl_ding ();
361  break;
362  }
363  }
364  return 0;
365 }
int rl_do_undo(void)
Definition: undo.c:169
int rl_ding()
Definition: terminal.c:681
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ _rl_saved_line_for_history

HIST_ENTRY* _rl_saved_line_for_history

Definition at line 306 of file misc.c.

◆ _rl_doing_an_undo

int _rl_doing_an_undo = 0

Definition at line 58 of file undo.c.

Referenced by rl_delete_text(), and rl_insert_text().

◆ _rl_undo_group_level

int _rl_undo_group_level = 0

Definition at line 61 of file undo.c.

Referenced by _rl_vi_done_inserting().

◆ rl_undo_list