1 /* misc.c -- miscellaneous bindable readline functions. */
3 /* Copyright (C) 1987-2022 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 #if defined (HAVE_UNISTD_H)
30 #endif /* HAVE_UNISTD_H */
32 #if defined (HAVE_STDLIB_H)
35 # include "ansi_stdlib.h"
36 #endif /* HAVE_STDLIB_H */
38 #if defined (HAVE_LOCALE_H)
44 /* System-specific feature definitions and include files. */
48 /* Some standard library routines. */
52 #include "rlprivate.h"
57 static int rl_digit_loop (void);
58 static void _rl_history_set_point (void);
60 /* If non-zero, rl_get_previous_history and rl_get_next_history attempt
61 to preserve the value of rl_point from line to line. */
62 int _rl_history_preserve_point
= 0;
64 _rl_arg_cxt _rl_argcxt
;
66 /* Saved target point for when _rl_history_preserve_point is set. Special
67 value of -1 means that point is at the end of the line. */
68 int _rl_history_saved_point
= -1;
70 /* **************************************************************** */
72 /* Numeric Arguments */
74 /* **************************************************************** */
77 _rl_arg_overflow (void)
79 if (rl_numeric_arg
> 1000000)
82 rl_explicit_arg
= rl_numeric_arg
= 0;
86 RL_UNSETSTATE(RL_STATE_NUMERICARG
);
97 RL_SETSTATE(RL_STATE_NUMERICARG
);
101 _rl_arg_getchar (void)
105 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
106 RL_SETSTATE(RL_STATE_MOREINPUT
);
108 RL_UNSETSTATE(RL_STATE_MOREINPUT
);
113 /* Process C as part of the current numeric argument. Return -1 if the
114 argument should be aborted, 0 if we should not read any more chars, and
115 1 if we should continue to read chars. */
117 _rl_arg_dispatch (_rl_arg_cxt cxt
, int c
)
123 /* If we see a key bound to `universal-argument' after seeing digits,
124 it ends the argument but is otherwise ignored. */
125 if (c
>= 0 && _rl_keymap
[c
].type
== ISFUNC
&& _rl_keymap
[c
].function
== rl_universal_argument
)
127 if ((cxt
& NUM_SAWDIGITS
) == 0)
132 else if (RL_ISSTATE (RL_STATE_CALLBACK
))
134 _rl_argcxt
|= NUM_READONE
;
139 key
= _rl_bracketed_read_key ();
140 rl_restore_prompt ();
142 RL_UNSETSTATE(RL_STATE_NUMERICARG
);
145 return (_rl_dispatch (key
, _rl_keymap
));
153 r
= _rl_digit_value (c
);
154 rl_numeric_arg
= rl_explicit_arg
? (rl_numeric_arg
* 10) + r
: r
;
156 _rl_argcxt
|= NUM_SAWDIGITS
;
158 else if (c
== '-' && rl_explicit_arg
== 0)
161 _rl_argcxt
|= NUM_SAWMINUS
;
166 /* Make M-- command equivalent to M--1 command. */
167 if ((_rl_argcxt
& NUM_SAWMINUS
) && rl_numeric_arg
== 1 && rl_explicit_arg
== 0)
169 rl_restore_prompt ();
171 RL_UNSETSTATE(RL_STATE_NUMERICARG
);
173 r
= _rl_dispatch (key
, _rl_keymap
);
174 if (RL_ISSTATE (RL_STATE_CALLBACK
))
176 /* At worst, this will cause an extra redisplay. Otherwise,
177 we have to wait until the next character comes in. */
179 (*rl_redisplay_function
) ();
188 /* Handle C-u style numeric args, as well as M--, and M-digits. */
196 if (_rl_arg_overflow ())
199 c
= _rl_arg_getchar ();
203 _rl_abort_internal ();
207 r
= _rl_arg_dispatch (_rl_argcxt
, c
);
208 if (r
<= 0 || (RL_ISSTATE (RL_STATE_NUMERICARG
) == 0))
215 /* Create a default argument. */
217 _rl_reset_argument (void)
219 rl_numeric_arg
= rl_arg_sign
= 1;
224 /* Start a numeric argument with initial value KEY */
226 rl_digit_argument (int ignore
, int key
)
229 if (RL_ISSTATE (RL_STATE_CALLBACK
))
231 _rl_arg_dispatch (_rl_argcxt
, key
);
232 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
237 rl_execute_next (key
);
238 return (rl_digit_loop ());
242 /* C-u, universal argument. Multiply the current argument by 4.
243 Read a key. If the key has nothing to do with arguments, then
244 dispatch on it. If the key is the abort character then abort. */
246 rl_universal_argument (int count
, int key
)
251 return (RL_ISSTATE (RL_STATE_CALLBACK
) ? 0 : rl_digit_loop ());
255 _rl_arg_callback (_rl_arg_cxt cxt
)
259 c
= _rl_arg_getchar ();
261 return (1); /* EOF */
263 if (_rl_argcxt
& NUM_READONE
)
265 _rl_argcxt
&= ~NUM_READONE
;
266 rl_restore_prompt ();
268 RL_UNSETSTATE(RL_STATE_NUMERICARG
);
273 r
= _rl_arg_dispatch (cxt
, c
);
275 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
279 /* What to do when you abort reading an argument. */
281 rl_discard_argument (void)
285 _rl_reset_argument ();
290 /* **************************************************************** */
292 /* History Utilities */
294 /* **************************************************************** */
296 /* We already have a history library, and that is what we use to control
297 the history features of readline. This is our local interface to
298 the history mechanism. */
300 /* While we are editing the history, this is the saved
301 version of the original line. */
302 HIST_ENTRY
*_rl_saved_line_for_history
= (HIST_ENTRY
*)NULL
;
304 /* Set the history pointer back to the last entry in the history. */
306 _rl_start_using_history (void)
309 if (_rl_saved_line_for_history
)
310 _rl_free_saved_history_line ();
311 _rl_saved_line_for_history
= (HIST_ENTRY
*)NULL
;
312 _rl_history_search_pos
= -99; /* some random invalid history position */
315 /* Free the contents (and containing structure) of a HIST_ENTRY. */
317 _rl_free_history_entry (HIST_ENTRY
*entry
)
323 FREE (entry
->timestamp
);
328 /* Perhaps put back the current line if it has changed. */
330 rl_maybe_replace_line (void)
334 temp
= current_history ();
335 /* If the current line has changed, save the changes. */
336 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
338 temp
= replace_history_entry (where_history (), rl_line_buffer
, (histdata_t
)rl_undo_list
);
340 FREE (temp
->timestamp
);
346 /* Restore the _rl_saved_line_for_history if there is one. */
348 rl_maybe_unsave_line (void)
350 if (_rl_saved_line_for_history
)
352 /* Can't call with `1' because rl_undo_list might point to an undo
353 list from a history entry, as in rl_replace_from_history() below. */
354 rl_replace_line (_rl_saved_line_for_history
->line
, 0);
355 rl_undo_list
= (UNDO_LIST
*)_rl_saved_line_for_history
->data
;
357 /* Doesn't free `data'. */
358 _rl_free_history_entry (_rl_saved_line_for_history
);
359 _rl_saved_line_for_history
= (HIST_ENTRY
*)NULL
;
360 rl_point
= rl_end
; /* rl_replace_line sets rl_end */
367 /* Save the current line in _rl_saved_line_for_history. */
369 rl_maybe_save_line (void)
371 if (_rl_saved_line_for_history
== 0)
373 _rl_saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
374 _rl_saved_line_for_history
->line
= savestring (rl_line_buffer
);
375 _rl_saved_line_for_history
->timestamp
= (char *)NULL
;
376 _rl_saved_line_for_history
->data
= (char *)rl_undo_list
;
383 _rl_free_saved_history_line (void)
387 if (_rl_saved_line_for_history
)
389 if (rl_undo_list
&& rl_undo_list
== (UNDO_LIST
*)_rl_saved_line_for_history
->data
)
391 /* Have to free this separately because _rl_free_history entry can't:
392 it doesn't know whether or not this has application data. Only the
393 callers that know this is _rl_saved_line_for_history can know that
394 it's an undo list. */
395 if (_rl_saved_line_for_history
->data
)
396 _rl_free_undo_list ((UNDO_LIST
*)_rl_saved_line_for_history
->data
);
397 _rl_free_history_entry (_rl_saved_line_for_history
);
398 _rl_saved_line_for_history
= (HIST_ENTRY
*)NULL
;
404 _rl_history_set_point (void)
406 rl_point
= (_rl_history_preserve_point
&& _rl_history_saved_point
!= -1)
407 ? _rl_history_saved_point
409 if (rl_point
> rl_end
)
412 #if defined (VI_MODE)
413 if (rl_editing_mode
== vi_mode
&& _rl_keymap
!= vi_insertion_keymap
)
417 if (rl_editing_mode
== emacs_mode
)
418 rl_mark
= (rl_point
== rl_end
? 0 : rl_end
);
422 rl_replace_from_history (HIST_ENTRY
*entry
, int flags
)
424 /* Can't call with `1' because rl_undo_list might point to an undo list
425 from a history entry, just like we're setting up here. */
426 rl_replace_line (entry
->line
, 0);
427 rl_undo_list
= (UNDO_LIST
*)entry
->data
;
431 #if defined (VI_MODE)
432 if (rl_editing_mode
== vi_mode
)
440 /* Process and free undo lists attached to each history entry prior to the
441 current entry, inclusive, reverting each line to its saved state. This
442 is destructive, and state about the current line is lost. This is not
443 intended to be called while actively editing, and the current line is
444 not assumed to have been added to the history list. */
446 _rl_revert_previous_lines (void)
450 UNDO_LIST
*ul
, *saved_undo_list
;
453 lbuf
= savestring (rl_line_buffer
);
454 saved_undo_list
= rl_undo_list
;
455 hpos
= where_history ();
457 entry
= (hpos
== history_length
) ? previous_history () : current_history ();
460 if (ul
= (UNDO_LIST
*)entry
->data
)
462 if (ul
== saved_undo_list
)
464 /* Set up rl_line_buffer and other variables from history entry */
465 rl_replace_from_history (entry
, 0); /* entry->line is now current */
466 entry
->data
= 0; /* entry->data is now current undo list */
467 /* Undo all changes to this history entry */
470 /* And copy the reverted line back to the history entry, preserving
473 entry
->line
= savestring (rl_line_buffer
);
475 entry
= previous_history ();
478 /* Restore history state */
479 rl_undo_list
= saved_undo_list
; /* may have been set to null */
480 history_set_pos (hpos
);
482 /* reset the line buffer */
483 rl_replace_line (lbuf
, 0);
490 /* Revert all lines in the history by making sure we are at the end of the
491 history before calling _rl_revert_previous_lines() */
493 _rl_revert_all_lines (void)
497 pos
= where_history ();
499 _rl_revert_previous_lines ();
500 history_set_pos (pos
);
503 /* Free the history list, including private readline data and take care
504 of pointer aliases to history data. Resets rl_undo_list if it points
505 to an UNDO_LIST * saved as some history entry's data member. This
506 should not be called while editing is active. */
508 rl_clear_history (void)
510 HIST_ENTRY
**hlist
, *hent
;
512 UNDO_LIST
*ul
, *saved_undo_list
;
514 saved_undo_list
= rl_undo_list
;
515 hlist
= history_list (); /* direct pointer, not copy */
517 for (i
= 0; i
< history_length
; i
++)
520 if (ul
= (UNDO_LIST
*)hent
->data
)
522 if (ul
== saved_undo_list
)
524 _rl_free_undo_list (ul
);
527 _rl_free_history_entry (hent
);
530 history_offset
= history_length
= 0;
531 rl_undo_list
= saved_undo_list
; /* should be NULL */
534 /* **************************************************************** */
536 /* History Commands */
538 /* **************************************************************** */
540 /* Meta-< goes to the start of the history. */
542 rl_beginning_of_history (int count
, int key
)
544 return (rl_get_previous_history (1 + where_history (), key
));
547 /* Meta-> goes to the end of the history. (The current line). */
549 rl_end_of_history (int count
, int key
)
551 rl_maybe_replace_line ();
553 rl_maybe_unsave_line ();
557 /* Move down to the next history line. */
559 rl_get_next_history (int count
, int key
)
564 return (rl_get_previous_history (-count
, key
));
569 rl_maybe_replace_line ();
571 /* either not saved by rl_newline or at end of line, so set appropriately. */
572 if (_rl_history_saved_point
== -1 && (rl_point
|| rl_end
))
573 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
575 temp
= (HIST_ENTRY
*)NULL
;
578 temp
= next_history ();
585 rl_maybe_unsave_line ();
588 rl_replace_from_history (temp
, 0);
589 _rl_history_set_point ();
594 /* Get the previous item out of our interactive history, making it the current
595 line. If there is no previous history, just ding. */
597 rl_get_previous_history (int count
, int key
)
599 HIST_ENTRY
*old_temp
, *temp
;
603 return (rl_get_next_history (-count
, key
));
605 if (count
== 0 || history_list () == 0)
608 /* either not saved by rl_newline or at end of line, so set appropriately. */
609 if (_rl_history_saved_point
== -1 && (rl_point
|| rl_end
))
610 _rl_history_saved_point
= (rl_point
== rl_end
) ? -1 : rl_point
;
612 /* If we don't have a line saved, then save this one. */
613 had_saved_line
= _rl_saved_line_for_history
!= 0;
614 rl_maybe_save_line ();
616 /* If the current line has changed, save the changes. */
617 rl_maybe_replace_line ();
619 temp
= old_temp
= (HIST_ENTRY
*)NULL
;
622 temp
= previous_history ();
630 /* If there was a large argument, and we moved back to the start of the
631 history, that is not an error. So use the last value found. */
632 if (!temp
&& old_temp
)
637 if (had_saved_line
== 0)
638 _rl_free_saved_history_line ();
643 rl_replace_from_history (temp
, 0);
644 _rl_history_set_point ();
650 /* With an argument, move back that many history lines, else move to the
651 beginning of history. */
653 rl_fetch_history (int count
, int c
)
657 /* Giving an argument of n means we want the nth command in the history
658 file. The command number is interpreted the same way that the bash
659 `history' command does it -- that is, giving an argument count of 450
660 to this command would get the command listed as number 450 in the
661 output of `history'. */
664 nhist
= history_base
+ where_history ();
665 /* Negative arguments count back from the end of the history list. */
666 wanted
= (count
>= 0) ? nhist
- count
: -count
;
668 if (wanted
<= 0 || wanted
>= nhist
)
670 /* In vi mode, we don't change the line with an out-of-range
671 argument, as for the `G' command. */
672 if (rl_editing_mode
== vi_mode
)
675 rl_beginning_of_history (0, 0);
678 rl_get_previous_history (wanted
, c
);
681 rl_beginning_of_history (count
, 0);
686 /* The equivalent of the Korn shell C-o operate-and-get-next-history-line
689 /* This could stand to be global to the readline library */
690 static rl_hook_func_t
*_rl_saved_internal_startup_hook
= 0;
691 static int saved_history_logical_offset
= -1;
693 #define HISTORY_FULL() (history_is_stifled () && history_length >= history_max_entries)
698 int absolute_offset
, count
;
700 if (saved_history_logical_offset
>= 0)
702 absolute_offset
= saved_history_logical_offset
- history_base
;
703 count
= where_history () - absolute_offset
;
704 rl_get_previous_history (count
, 0);
706 saved_history_logical_offset
= -1;
707 _rl_internal_startup_hook
= _rl_saved_internal_startup_hook
;
713 rl_operate_and_get_next (int count
, int c
)
715 /* Accept the current line. */
718 saved_history_logical_offset
= rl_explicit_arg
? count
: where_history () + history_base
+ 1;
720 _rl_saved_internal_startup_hook
= _rl_internal_startup_hook
;
721 _rl_internal_startup_hook
= set_saved_history
;
726 /* **************************************************************** */
730 /* **************************************************************** */
731 /* How to toggle back and forth between editing modes. */
733 rl_vi_editing_mode (int count
, int key
)
735 #if defined (VI_MODE)
736 _rl_set_insert_mode (RL_IM_INSERT
, 1); /* vi mode ignores insert mode */
737 rl_editing_mode
= vi_mode
;
738 rl_vi_insert_mode (1, key
);
745 rl_emacs_editing_mode (int count
, int key
)
747 rl_editing_mode
= emacs_mode
;
748 _rl_set_insert_mode (RL_IM_INSERT
, 1); /* emacs mode default is insert mode */
749 _rl_keymap
= emacs_standard_keymap
;
751 if (_rl_show_mode_in_prompt
)
757 /* Function for the rest of the library to use to set insert/overwrite mode. */
759 _rl_set_insert_mode (int im
, int force
)
762 _rl_set_cursor (im
, force
);
768 /* Toggle overwrite mode. A positive explicit argument selects overwrite
769 mode. A negative or zero explicit argument selects insert mode. */
771 rl_overwrite_mode (int count
, int key
)
773 if (rl_explicit_arg
== 0)
774 _rl_set_insert_mode (rl_insert_mode
^ 1, 0);
776 _rl_set_insert_mode (RL_IM_OVERWRITE
, 0);
778 _rl_set_insert_mode (RL_IM_INSERT
, 0);