1 /* misc.c -- miscellaneous bindable readline functions. */
3 /* Copyright (C) 1987-2004 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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"
56 static int rl_digit_loop PARAMS((void));
57 static void _rl_history_set_point PARAMS((void));
59 /* Forward declarations used in this file */
60 void _rl_free_history_entry PARAMS((HIST_ENTRY *));
62 /* If non-zero, rl_get_previous_history and rl_get_next_history attempt
63 to preserve the value of rl_point from line to line. */
64 int _rl_history_preserve_point = 0;
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 /* **************************************************************** */
76 /* Handle C-u style numeric args, as well as M--, and M-digits. */
80 int key, c, sawminus, sawdigits;
84 RL_SETSTATE(RL_STATE_NUMERICARG);
85 sawminus = sawdigits = 0;
88 if (rl_numeric_arg > 1000000)
90 sawdigits = rl_explicit_arg = rl_numeric_arg = 0;
94 RL_UNSETSTATE(RL_STATE_NUMERICARG);
97 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
98 RL_SETSTATE(RL_STATE_MOREINPUT);
99 key = c = rl_read_key ();
100 RL_UNSETSTATE(RL_STATE_MOREINPUT);
104 _rl_abort_internal ();
108 /* If we see a key bound to `universal-argument' after seeing digits,
109 it ends the argument but is otherwise ignored. */
110 if (_rl_keymap[c].type == ISFUNC &&
111 _rl_keymap[c].function == rl_universal_argument)
120 RL_SETSTATE(RL_STATE_MOREINPUT);
121 key = rl_read_key ();
122 RL_UNSETSTATE(RL_STATE_MOREINPUT);
123 rl_restore_prompt ();
125 RL_UNSETSTATE(RL_STATE_NUMERICARG);
126 return (_rl_dispatch (key, _rl_keymap));
134 rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
135 sawdigits = rl_explicit_arg = 1;
137 else if (c == '-' && rl_explicit_arg == 0)
139 rl_numeric_arg = sawminus = 1;
144 /* Make M-- command equivalent to M--1 command. */
145 if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
147 rl_restore_prompt ();
149 RL_UNSETSTATE(RL_STATE_NUMERICARG);
150 return (_rl_dispatch (key, _rl_keymap));
157 /* Add the current digit to the argument in progress. */
159 rl_digit_argument (ignore, key)
162 rl_execute_next (key);
163 return (rl_digit_loop ());
166 /* What to do when you abort reading an argument. */
168 rl_discard_argument ()
172 _rl_init_argument ();
176 /* Create a default argument. */
180 rl_numeric_arg = rl_arg_sign = 1;
185 /* C-u, universal argument. Multiply the current argument by 4.
186 Read a key. If the key has nothing to do with arguments, then
187 dispatch on it. If the key is the abort character then abort. */
189 rl_universal_argument (count, key)
193 return (rl_digit_loop ());
196 /* **************************************************************** */
198 /* History Utilities */
200 /* **************************************************************** */
202 /* We already have a history library, and that is what we use to control
203 the history features of readline. This is our local interface to
204 the history mechanism. */
206 /* While we are editing the history, this is the saved
207 version of the original line. */
208 HIST_ENTRY *_rl_saved_line_for_history = (HIST_ENTRY *)NULL;
210 /* Set the history pointer back to the last entry in the history. */
212 _rl_start_using_history ()
215 if (_rl_saved_line_for_history)
216 _rl_free_history_entry (_rl_saved_line_for_history);
218 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
221 /* Free the contents (and containing structure) of a HIST_ENTRY. */
223 _rl_free_history_entry (entry)
233 /* Perhaps put back the current line if it has changed. */
235 rl_maybe_replace_line ()
239 temp = current_history ();
240 /* If the current line has changed, save the changes. */
241 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
243 temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list);
250 /* Restore the _rl_saved_line_for_history if there is one. */
252 rl_maybe_unsave_line ()
254 if (_rl_saved_line_for_history)
256 /* Can't call with `1' because rl_undo_list might point to an undo
257 list from a history entry, as in rl_replace_from_history() below. */
258 rl_replace_line (_rl_saved_line_for_history->line, 0);
259 rl_undo_list = (UNDO_LIST *)_rl_saved_line_for_history->data;
260 _rl_free_history_entry (_rl_saved_line_for_history);
261 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
262 rl_point = rl_end; /* rl_replace_line sets rl_end */
269 /* Save the current line in _rl_saved_line_for_history. */
271 rl_maybe_save_line ()
273 if (_rl_saved_line_for_history == 0)
275 _rl_saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
276 _rl_saved_line_for_history->line = savestring (rl_line_buffer);
277 _rl_saved_line_for_history->data = (char *)rl_undo_list;
284 _rl_free_saved_history_line ()
286 if (_rl_saved_line_for_history)
288 _rl_free_history_entry (_rl_saved_line_for_history);
289 _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
295 _rl_history_set_point ()
297 rl_point = (_rl_history_preserve_point && _rl_history_saved_point != -1)
298 ? _rl_history_saved_point
300 if (rl_point > rl_end)
303 #if defined (VI_MODE)
304 if (rl_editing_mode == vi_mode && _rl_keymap != vi_insertion_keymap)
308 if (rl_editing_mode == emacs_mode)
309 rl_mark = (rl_point == rl_end ? 0 : rl_end);
313 rl_replace_from_history (entry, flags)
315 int flags; /* currently unused */
317 /* Can't call with `1' because rl_undo_list might point to an undo list
318 from a history entry, just like we're setting up here. */
319 rl_replace_line (entry->line, 0);
320 rl_undo_list = (UNDO_LIST *)entry->data;
324 #if defined (VI_MODE)
325 if (rl_editing_mode == vi_mode)
333 /* **************************************************************** */
335 /* History Commands */
337 /* **************************************************************** */
339 /* Meta-< goes to the start of the history. */
341 rl_beginning_of_history (count, key)
344 return (rl_get_previous_history (1 + where_history (), key));
347 /* Meta-> goes to the end of the history. (The current line). */
349 rl_end_of_history (count, key)
352 rl_maybe_replace_line ();
354 rl_maybe_unsave_line ();
358 /* Move down to the next history line. */
360 rl_get_next_history (count, key)
366 return (rl_get_previous_history (-count, key));
371 rl_maybe_replace_line ();
373 /* either not saved by rl_newline or at end of line, so set appropriately. */
374 if (_rl_history_saved_point == -1 && (rl_point || rl_end))
375 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
377 temp = (HIST_ENTRY *)NULL;
380 temp = next_history ();
387 rl_maybe_unsave_line ();
390 rl_replace_from_history (temp, 0);
391 _rl_history_set_point ();
396 /* Get the previous item out of our interactive history, making it the current
397 line. If there is no previous history, just ding. */
399 rl_get_previous_history (count, key)
402 HIST_ENTRY *old_temp, *temp;
405 return (rl_get_next_history (-count, key));
410 /* either not saved by rl_newline or at end of line, so set appropriately. */
411 if (_rl_history_saved_point == -1 && (rl_point || rl_end))
412 _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
414 /* If we don't have a line saved, then save this one. */
415 rl_maybe_save_line ();
417 /* If the current line has changed, save the changes. */
418 rl_maybe_replace_line ();
420 temp = old_temp = (HIST_ENTRY *)NULL;
423 temp = previous_history ();
431 /* If there was a large argument, and we moved back to the start of the
432 history, that is not an error. So use the last value found. */
433 if (!temp && old_temp)
440 rl_replace_from_history (temp, 0);
441 _rl_history_set_point ();
447 /* **************************************************************** */
451 /* **************************************************************** */
452 /* How to toggle back and forth between editing modes. */
454 rl_vi_editing_mode (count, key)
457 #if defined (VI_MODE)
458 _rl_set_insert_mode (RL_IM_INSERT, 1); /* vi mode ignores insert mode */
459 rl_editing_mode = vi_mode;
460 rl_vi_insertion_mode (1, key);
467 rl_emacs_editing_mode (count, key)
470 rl_editing_mode = emacs_mode;
471 _rl_set_insert_mode (RL_IM_INSERT, 1); /* emacs mode default is insert mode */
472 _rl_keymap = emacs_standard_keymap;
476 /* Function for the rest of the library to use to set insert/overwrite mode. */
478 _rl_set_insert_mode (im, force)
482 _rl_set_cursor (im, force);
488 /* Toggle overwrite mode. A positive explicit argument selects overwrite
489 mode. A negative or zero explicit argument selects insert mode. */
491 rl_overwrite_mode (count, key)
494 if (rl_explicit_arg == 0)
495 _rl_set_insert_mode (rl_insert_mode ^ 1, 0);
497 _rl_set_insert_mode (RL_IM_OVERWRITE, 0);
499 _rl_set_insert_mode (RL_IM_INSERT, 0);