]>
Commit | Line | Data |
---|---|---|
7117c2d2 JA |
1 | /* misc.c -- miscellaneous bindable readline functions. */ |
2 | ||
74091dd4 | 3 | /* Copyright (C) 1987-2022 Free Software Foundation, Inc. |
7117c2d2 | 4 | |
3185942a JA |
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. | |
7117c2d2 | 7 | |
3185942a JA |
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 | |
7117c2d2 JA |
11 | (at your option) any later version. |
12 | ||
3185942a JA |
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 | |
7117c2d2 JA |
16 | GNU General Public License for more details. |
17 | ||
3185942a JA |
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/>. | |
20 | */ | |
21 | ||
7117c2d2 JA |
22 | #define READLINE_LIBRARY |
23 | ||
24 | #if defined (HAVE_CONFIG_H) | |
25 | # include <config.h> | |
26 | #endif | |
27 | ||
28 | #if defined (HAVE_UNISTD_H) | |
29 | # include <unistd.h> | |
30 | #endif /* HAVE_UNISTD_H */ | |
31 | ||
32 | #if defined (HAVE_STDLIB_H) | |
33 | # include <stdlib.h> | |
34 | #else | |
35 | # include "ansi_stdlib.h" | |
36 | #endif /* HAVE_STDLIB_H */ | |
37 | ||
38 | #if defined (HAVE_LOCALE_H) | |
39 | # include <locale.h> | |
40 | #endif | |
41 | ||
42 | #include <stdio.h> | |
43 | ||
44 | /* System-specific feature definitions and include files. */ | |
45 | #include "rldefs.h" | |
46 | #include "rlmbutil.h" | |
47 | ||
48 | /* Some standard library routines. */ | |
49 | #include "readline.h" | |
50 | #include "history.h" | |
51 | ||
52 | #include "rlprivate.h" | |
74091dd4 | 53 | #include "histlib.h" |
7117c2d2 JA |
54 | #include "rlshell.h" |
55 | #include "xmalloc.h" | |
56 | ||
74091dd4 CR |
57 | static int rl_digit_loop (void); |
58 | static void _rl_history_set_point (void); | |
7117c2d2 JA |
59 | |
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; | |
63 | ||
95732b49 JA |
64 | _rl_arg_cxt _rl_argcxt; |
65 | ||
7117c2d2 JA |
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; | |
69 | ||
70 | /* **************************************************************** */ | |
71 | /* */ | |
72 | /* Numeric Arguments */ | |
73 | /* */ | |
74 | /* **************************************************************** */ | |
75 | ||
95732b49 | 76 | int |
d233b485 | 77 | _rl_arg_overflow (void) |
7117c2d2 | 78 | { |
95732b49 JA |
79 | if (rl_numeric_arg > 1000000) |
80 | { | |
81 | _rl_argcxt = 0; | |
82 | rl_explicit_arg = rl_numeric_arg = 0; | |
83 | rl_ding (); | |
84 | rl_restore_prompt (); | |
85 | rl_clear_message (); | |
86 | RL_UNSETSTATE(RL_STATE_NUMERICARG); | |
87 | return 1; | |
88 | } | |
89 | return 0; | |
90 | } | |
7117c2d2 | 91 | |
95732b49 | 92 | void |
d233b485 | 93 | _rl_arg_init (void) |
95732b49 | 94 | { |
7117c2d2 | 95 | rl_save_prompt (); |
95732b49 | 96 | _rl_argcxt = 0; |
7117c2d2 | 97 | RL_SETSTATE(RL_STATE_NUMERICARG); |
95732b49 | 98 | } |
7117c2d2 | 99 | |
95732b49 | 100 | int |
d233b485 | 101 | _rl_arg_getchar (void) |
95732b49 JA |
102 | { |
103 | int c; | |
7117c2d2 | 104 | |
95732b49 JA |
105 | rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg); |
106 | RL_SETSTATE(RL_STATE_MOREINPUT); | |
107 | c = rl_read_key (); | |
108 | RL_UNSETSTATE(RL_STATE_MOREINPUT); | |
7117c2d2 | 109 | |
95732b49 JA |
110 | return c; |
111 | } | |
7117c2d2 | 112 | |
95732b49 JA |
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. */ | |
116 | int | |
d233b485 | 117 | _rl_arg_dispatch (_rl_arg_cxt cxt, int c) |
95732b49 JA |
118 | { |
119 | int key, r; | |
120 | ||
121 | key = c; | |
122 | ||
123 | /* If we see a key bound to `universal-argument' after seeing digits, | |
124 | it ends the argument but is otherwise ignored. */ | |
a0c0a00f | 125 | if (c >= 0 && _rl_keymap[c].type == ISFUNC && _rl_keymap[c].function == rl_universal_argument) |
95732b49 JA |
126 | { |
127 | if ((cxt & NUM_SAWDIGITS) == 0) | |
7117c2d2 | 128 | { |
95732b49 JA |
129 | rl_numeric_arg *= 4; |
130 | return 1; | |
7117c2d2 | 131 | } |
95732b49 JA |
132 | else if (RL_ISSTATE (RL_STATE_CALLBACK)) |
133 | { | |
134 | _rl_argcxt |= NUM_READONE; | |
135 | return 0; /* XXX */ | |
136 | } | |
7117c2d2 JA |
137 | else |
138 | { | |
8868edaf | 139 | key = _rl_bracketed_read_key (); |
7117c2d2 JA |
140 | rl_restore_prompt (); |
141 | rl_clear_message (); | |
142 | RL_UNSETSTATE(RL_STATE_NUMERICARG); | |
f1be666c JA |
143 | if (key < 0) |
144 | return -1; | |
7117c2d2 JA |
145 | return (_rl_dispatch (key, _rl_keymap)); |
146 | } | |
147 | } | |
148 | ||
95732b49 | 149 | c = UNMETA (c); |
7117c2d2 | 150 | |
95732b49 JA |
151 | if (_rl_digit_p (c)) |
152 | { | |
153 | r = _rl_digit_value (c); | |
154 | rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + r : r; | |
155 | rl_explicit_arg = 1; | |
156 | _rl_argcxt |= NUM_SAWDIGITS; | |
157 | } | |
158 | else if (c == '-' && rl_explicit_arg == 0) | |
159 | { | |
160 | rl_numeric_arg = 1; | |
161 | _rl_argcxt |= NUM_SAWMINUS; | |
162 | rl_arg_sign = -1; | |
163 | } | |
164 | else | |
165 | { | |
166 | /* Make M-- command equivalent to M--1 command. */ | |
167 | if ((_rl_argcxt & NUM_SAWMINUS) && rl_numeric_arg == 1 && rl_explicit_arg == 0) | |
168 | rl_explicit_arg = 1; | |
169 | rl_restore_prompt (); | |
170 | rl_clear_message (); | |
171 | RL_UNSETSTATE(RL_STATE_NUMERICARG); | |
172 | ||
173 | r = _rl_dispatch (key, _rl_keymap); | |
174 | if (RL_ISSTATE (RL_STATE_CALLBACK)) | |
175 | { | |
176 | /* At worst, this will cause an extra redisplay. Otherwise, | |
177 | we have to wait until the next character comes in. */ | |
178 | if (rl_done == 0) | |
179 | (*rl_redisplay_function) (); | |
180 | r = 0; | |
181 | } | |
182 | return r; | |
183 | } | |
184 | ||
185 | return 1; | |
7117c2d2 JA |
186 | } |
187 | ||
95732b49 JA |
188 | /* Handle C-u style numeric args, as well as M--, and M-digits. */ |
189 | static int | |
d233b485 | 190 | rl_digit_loop (void) |
7117c2d2 | 191 | { |
95732b49 JA |
192 | int c, r; |
193 | ||
194 | while (1) | |
195 | { | |
196 | if (_rl_arg_overflow ()) | |
197 | return 1; | |
198 | ||
199 | c = _rl_arg_getchar (); | |
200 | ||
201 | if (c < 0) | |
202 | { | |
203 | _rl_abort_internal (); | |
204 | return -1; | |
205 | } | |
206 | ||
207 | r = _rl_arg_dispatch (_rl_argcxt, c); | |
208 | if (r <= 0 || (RL_ISSTATE (RL_STATE_NUMERICARG) == 0)) | |
209 | break; | |
210 | } | |
0628567a JA |
211 | |
212 | return r; | |
7117c2d2 JA |
213 | } |
214 | ||
215 | /* Create a default argument. */ | |
95732b49 | 216 | void |
d233b485 | 217 | _rl_reset_argument (void) |
7117c2d2 JA |
218 | { |
219 | rl_numeric_arg = rl_arg_sign = 1; | |
220 | rl_explicit_arg = 0; | |
95732b49 JA |
221 | _rl_argcxt = 0; |
222 | } | |
223 | ||
224 | /* Start a numeric argument with initial value KEY */ | |
225 | int | |
d233b485 | 226 | rl_digit_argument (int ignore, int key) |
95732b49 JA |
227 | { |
228 | _rl_arg_init (); | |
229 | if (RL_ISSTATE (RL_STATE_CALLBACK)) | |
230 | { | |
231 | _rl_arg_dispatch (_rl_argcxt, key); | |
232 | rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg); | |
233 | return 0; | |
234 | } | |
235 | else | |
236 | { | |
237 | rl_execute_next (key); | |
238 | return (rl_digit_loop ()); | |
239 | } | |
7117c2d2 JA |
240 | } |
241 | ||
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. */ | |
245 | int | |
d233b485 | 246 | rl_universal_argument (int count, int key) |
7117c2d2 | 247 | { |
95732b49 | 248 | _rl_arg_init (); |
7117c2d2 | 249 | rl_numeric_arg *= 4; |
95732b49 JA |
250 | |
251 | return (RL_ISSTATE (RL_STATE_CALLBACK) ? 0 : rl_digit_loop ()); | |
252 | } | |
253 | ||
254 | int | |
d233b485 | 255 | _rl_arg_callback (_rl_arg_cxt cxt) |
95732b49 JA |
256 | { |
257 | int c, r; | |
258 | ||
259 | c = _rl_arg_getchar (); | |
a0c0a00f CR |
260 | if (c < 0) |
261 | return (1); /* EOF */ | |
95732b49 JA |
262 | |
263 | if (_rl_argcxt & NUM_READONE) | |
264 | { | |
265 | _rl_argcxt &= ~NUM_READONE; | |
266 | rl_restore_prompt (); | |
267 | rl_clear_message (); | |
268 | RL_UNSETSTATE(RL_STATE_NUMERICARG); | |
269 | rl_execute_next (c); | |
270 | return 0; | |
271 | } | |
272 | ||
273 | r = _rl_arg_dispatch (cxt, c); | |
d233b485 CR |
274 | if (r > 0) |
275 | rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg); | |
95732b49 JA |
276 | return (r != 1); |
277 | } | |
278 | ||
279 | /* What to do when you abort reading an argument. */ | |
280 | int | |
d233b485 | 281 | rl_discard_argument (void) |
95732b49 JA |
282 | { |
283 | rl_ding (); | |
284 | rl_clear_message (); | |
285 | _rl_reset_argument (); | |
286 | ||
287 | return 0; | |
7117c2d2 JA |
288 | } |
289 | ||
290 | /* **************************************************************** */ | |
291 | /* */ | |
292 | /* History Utilities */ | |
293 | /* */ | |
294 | /* **************************************************************** */ | |
295 | ||
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. */ | |
299 | ||
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; | |
303 | ||
304 | /* Set the history pointer back to the last entry in the history. */ | |
305 | void | |
d233b485 | 306 | _rl_start_using_history (void) |
7117c2d2 JA |
307 | { |
308 | using_history (); | |
309 | if (_rl_saved_line_for_history) | |
74091dd4 | 310 | _rl_free_saved_history_line (); |
7117c2d2 | 311 | _rl_saved_line_for_history = (HIST_ENTRY *)NULL; |
74091dd4 | 312 | _rl_history_search_pos = -99; /* some random invalid history position */ |
7117c2d2 JA |
313 | } |
314 | ||
315 | /* Free the contents (and containing structure) of a HIST_ENTRY. */ | |
316 | void | |
d233b485 | 317 | _rl_free_history_entry (HIST_ENTRY *entry) |
7117c2d2 JA |
318 | { |
319 | if (entry == 0) | |
320 | return; | |
95732b49 JA |
321 | |
322 | FREE (entry->line); | |
323 | FREE (entry->timestamp); | |
324 | ||
495aee44 | 325 | xfree (entry); |
7117c2d2 JA |
326 | } |
327 | ||
328 | /* Perhaps put back the current line if it has changed. */ | |
329 | int | |
d233b485 | 330 | rl_maybe_replace_line (void) |
7117c2d2 JA |
331 | { |
332 | HIST_ENTRY *temp; | |
333 | ||
334 | temp = current_history (); | |
335 | /* If the current line has changed, save the changes. */ | |
336 | if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list)) | |
337 | { | |
338 | temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list); | |
495aee44 | 339 | xfree (temp->line); |
95732b49 | 340 | FREE (temp->timestamp); |
495aee44 | 341 | xfree (temp); |
7117c2d2 JA |
342 | } |
343 | return 0; | |
344 | } | |
345 | ||
346 | /* Restore the _rl_saved_line_for_history if there is one. */ | |
347 | int | |
d233b485 | 348 | rl_maybe_unsave_line (void) |
7117c2d2 JA |
349 | { |
350 | if (_rl_saved_line_for_history) | |
351 | { | |
b80f6443 JA |
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. */ | |
7117c2d2 JA |
354 | rl_replace_line (_rl_saved_line_for_history->line, 0); |
355 | rl_undo_list = (UNDO_LIST *)_rl_saved_line_for_history->data; | |
74091dd4 CR |
356 | |
357 | /* Doesn't free `data'. */ | |
7117c2d2 JA |
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 */ | |
361 | } | |
362 | else | |
363 | rl_ding (); | |
364 | return 0; | |
365 | } | |
366 | ||
367 | /* Save the current line in _rl_saved_line_for_history. */ | |
368 | int | |
d233b485 | 369 | rl_maybe_save_line (void) |
7117c2d2 JA |
370 | { |
371 | if (_rl_saved_line_for_history == 0) | |
372 | { | |
373 | _rl_saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY)); | |
374 | _rl_saved_line_for_history->line = savestring (rl_line_buffer); | |
95732b49 | 375 | _rl_saved_line_for_history->timestamp = (char *)NULL; |
7117c2d2 JA |
376 | _rl_saved_line_for_history->data = (char *)rl_undo_list; |
377 | } | |
b80f6443 | 378 | |
7117c2d2 JA |
379 | return 0; |
380 | } | |
381 | ||
382 | int | |
d233b485 | 383 | _rl_free_saved_history_line (void) |
7117c2d2 | 384 | { |
74091dd4 CR |
385 | UNDO_LIST *orig; |
386 | ||
7117c2d2 JA |
387 | if (_rl_saved_line_for_history) |
388 | { | |
74091dd4 CR |
389 | if (rl_undo_list && rl_undo_list == (UNDO_LIST *)_rl_saved_line_for_history->data) |
390 | rl_undo_list = 0; | |
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); | |
7117c2d2 JA |
397 | _rl_free_history_entry (_rl_saved_line_for_history); |
398 | _rl_saved_line_for_history = (HIST_ENTRY *)NULL; | |
399 | } | |
400 | return 0; | |
401 | } | |
402 | ||
403 | static void | |
d233b485 | 404 | _rl_history_set_point (void) |
7117c2d2 JA |
405 | { |
406 | rl_point = (_rl_history_preserve_point && _rl_history_saved_point != -1) | |
407 | ? _rl_history_saved_point | |
408 | : rl_end; | |
409 | if (rl_point > rl_end) | |
410 | rl_point = rl_end; | |
411 | ||
412 | #if defined (VI_MODE) | |
b80f6443 | 413 | if (rl_editing_mode == vi_mode && _rl_keymap != vi_insertion_keymap) |
7117c2d2 JA |
414 | rl_point = 0; |
415 | #endif /* VI_MODE */ | |
416 | ||
417 | if (rl_editing_mode == emacs_mode) | |
418 | rl_mark = (rl_point == rl_end ? 0 : rl_end); | |
419 | } | |
420 | ||
421 | void | |
d233b485 | 422 | rl_replace_from_history (HIST_ENTRY *entry, int flags) |
7117c2d2 | 423 | { |
b80f6443 JA |
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. */ | |
7117c2d2 JA |
426 | rl_replace_line (entry->line, 0); |
427 | rl_undo_list = (UNDO_LIST *)entry->data; | |
428 | rl_point = rl_end; | |
429 | rl_mark = 0; | |
430 | ||
431 | #if defined (VI_MODE) | |
432 | if (rl_editing_mode == vi_mode) | |
433 | { | |
434 | rl_point = 0; | |
435 | rl_mark = rl_end; | |
436 | } | |
437 | #endif | |
3185942a JA |
438 | } |
439 | ||
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. */ | |
445 | void | |
8868edaf | 446 | _rl_revert_previous_lines (void) |
3185942a JA |
447 | { |
448 | int hpos; | |
449 | HIST_ENTRY *entry; | |
450 | UNDO_LIST *ul, *saved_undo_list; | |
451 | char *lbuf; | |
452 | ||
453 | lbuf = savestring (rl_line_buffer); | |
454 | saved_undo_list = rl_undo_list; | |
455 | hpos = where_history (); | |
456 | ||
457 | entry = (hpos == history_length) ? previous_history () : current_history (); | |
458 | while (entry) | |
459 | { | |
460 | if (ul = (UNDO_LIST *)entry->data) | |
461 | { | |
462 | if (ul == saved_undo_list) | |
463 | saved_undo_list = 0; | |
464 | /* Set up rl_line_buffer and other variables from history entry */ | |
465 | rl_replace_from_history (entry, 0); /* entry->line is now current */ | |
00062383 | 466 | entry->data = 0; /* entry->data is now current undo list */ |
3185942a JA |
467 | /* Undo all changes to this history entry */ |
468 | while (rl_undo_list) | |
469 | rl_do_undo (); | |
470 | /* And copy the reverted line back to the history entry, preserving | |
471 | the timestamp. */ | |
472 | FREE (entry->line); | |
473 | entry->line = savestring (rl_line_buffer); | |
3185942a JA |
474 | } |
475 | entry = previous_history (); | |
476 | } | |
477 | ||
478 | /* Restore history state */ | |
479 | rl_undo_list = saved_undo_list; /* may have been set to null */ | |
480 | history_set_pos (hpos); | |
481 | ||
482 | /* reset the line buffer */ | |
483 | rl_replace_line (lbuf, 0); | |
484 | _rl_set_the_line (); | |
485 | ||
486 | /* and clean up */ | |
495aee44 | 487 | xfree (lbuf); |
7117c2d2 JA |
488 | } |
489 | ||
8868edaf CR |
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() */ | |
492 | void | |
493 | _rl_revert_all_lines (void) | |
494 | { | |
495 | int pos; | |
496 | ||
497 | pos = where_history (); | |
498 | using_history (); | |
499 | _rl_revert_previous_lines (); | |
500 | history_set_pos (pos); | |
501 | } | |
502 | ||
ac50fbac CR |
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. */ | |
507 | void | |
d233b485 | 508 | rl_clear_history (void) |
ac50fbac CR |
509 | { |
510 | HIST_ENTRY **hlist, *hent; | |
511 | register int i; | |
512 | UNDO_LIST *ul, *saved_undo_list; | |
513 | ||
514 | saved_undo_list = rl_undo_list; | |
515 | hlist = history_list (); /* direct pointer, not copy */ | |
516 | ||
517 | for (i = 0; i < history_length; i++) | |
518 | { | |
519 | hent = hlist[i]; | |
520 | if (ul = (UNDO_LIST *)hent->data) | |
521 | { | |
522 | if (ul == saved_undo_list) | |
523 | saved_undo_list = 0; | |
524 | _rl_free_undo_list (ul); | |
525 | hent->data = 0; | |
526 | } | |
527 | _rl_free_history_entry (hent); | |
528 | } | |
529 | ||
530 | history_offset = history_length = 0; | |
531 | rl_undo_list = saved_undo_list; /* should be NULL */ | |
532 | } | |
533 | ||
7117c2d2 JA |
534 | /* **************************************************************** */ |
535 | /* */ | |
536 | /* History Commands */ | |
537 | /* */ | |
538 | /* **************************************************************** */ | |
539 | ||
540 | /* Meta-< goes to the start of the history. */ | |
541 | int | |
d233b485 | 542 | rl_beginning_of_history (int count, int key) |
7117c2d2 JA |
543 | { |
544 | return (rl_get_previous_history (1 + where_history (), key)); | |
545 | } | |
546 | ||
547 | /* Meta-> goes to the end of the history. (The current line). */ | |
548 | int | |
d233b485 | 549 | rl_end_of_history (int count, int key) |
7117c2d2 JA |
550 | { |
551 | rl_maybe_replace_line (); | |
552 | using_history (); | |
553 | rl_maybe_unsave_line (); | |
554 | return 0; | |
555 | } | |
556 | ||
557 | /* Move down to the next history line. */ | |
558 | int | |
d233b485 | 559 | rl_get_next_history (int count, int key) |
7117c2d2 JA |
560 | { |
561 | HIST_ENTRY *temp; | |
562 | ||
563 | if (count < 0) | |
564 | return (rl_get_previous_history (-count, key)); | |
565 | ||
566 | if (count == 0) | |
567 | return 0; | |
568 | ||
569 | rl_maybe_replace_line (); | |
570 | ||
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; | |
574 | ||
575 | temp = (HIST_ENTRY *)NULL; | |
576 | while (count) | |
577 | { | |
578 | temp = next_history (); | |
579 | if (!temp) | |
580 | break; | |
581 | --count; | |
582 | } | |
583 | ||
584 | if (temp == 0) | |
585 | rl_maybe_unsave_line (); | |
586 | else | |
587 | { | |
588 | rl_replace_from_history (temp, 0); | |
589 | _rl_history_set_point (); | |
590 | } | |
591 | return 0; | |
592 | } | |
593 | ||
594 | /* Get the previous item out of our interactive history, making it the current | |
595 | line. If there is no previous history, just ding. */ | |
596 | int | |
d233b485 | 597 | rl_get_previous_history (int count, int key) |
7117c2d2 JA |
598 | { |
599 | HIST_ENTRY *old_temp, *temp; | |
b0852fb5 | 600 | int had_saved_line; |
7117c2d2 JA |
601 | |
602 | if (count < 0) | |
603 | return (rl_get_next_history (-count, key)); | |
604 | ||
d233b485 | 605 | if (count == 0 || history_list () == 0) |
7117c2d2 JA |
606 | return 0; |
607 | ||
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; | |
611 | ||
612 | /* If we don't have a line saved, then save this one. */ | |
b0852fb5 | 613 | had_saved_line = _rl_saved_line_for_history != 0; |
7117c2d2 JA |
614 | rl_maybe_save_line (); |
615 | ||
616 | /* If the current line has changed, save the changes. */ | |
617 | rl_maybe_replace_line (); | |
618 | ||
619 | temp = old_temp = (HIST_ENTRY *)NULL; | |
620 | while (count) | |
621 | { | |
622 | temp = previous_history (); | |
623 | if (temp == 0) | |
624 | break; | |
625 | ||
626 | old_temp = temp; | |
627 | --count; | |
628 | } | |
629 | ||
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) | |
633 | temp = old_temp; | |
634 | ||
635 | if (temp == 0) | |
d233b485 | 636 | { |
b0852fb5 | 637 | if (had_saved_line == 0) |
74091dd4 | 638 | _rl_free_saved_history_line (); |
d233b485 CR |
639 | rl_ding (); |
640 | } | |
7117c2d2 JA |
641 | else |
642 | { | |
643 | rl_replace_from_history (temp, 0); | |
644 | _rl_history_set_point (); | |
645 | } | |
b80f6443 | 646 | |
7117c2d2 JA |
647 | return 0; |
648 | } | |
649 | ||
74091dd4 CR |
650 | /* With an argument, move back that many history lines, else move to the |
651 | beginning of history. */ | |
652 | int | |
653 | rl_fetch_history (int count, int c) | |
654 | { | |
655 | int wanted, nhist; | |
656 | ||
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'. */ | |
662 | if (rl_explicit_arg) | |
663 | { | |
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; | |
667 | ||
668 | if (wanted <= 0 || wanted >= nhist) | |
669 | { | |
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) | |
673 | rl_ding (); | |
674 | else | |
675 | rl_beginning_of_history (0, 0); | |
676 | } | |
677 | else | |
678 | rl_get_previous_history (wanted, c); | |
679 | } | |
680 | else | |
681 | rl_beginning_of_history (count, 0); | |
682 | ||
683 | return (0); | |
684 | } | |
685 | ||
8868edaf CR |
686 | /* The equivalent of the Korn shell C-o operate-and-get-next-history-line |
687 | editing command. */ | |
688 | ||
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; | |
692 | ||
693 | #define HISTORY_FULL() (history_is_stifled () && history_length >= history_max_entries) | |
694 | ||
695 | static int | |
696 | set_saved_history () | |
697 | { | |
698 | int absolute_offset, count; | |
699 | ||
700 | if (saved_history_logical_offset >= 0) | |
701 | { | |
702 | absolute_offset = saved_history_logical_offset - history_base; | |
703 | count = where_history () - absolute_offset; | |
704 | rl_get_previous_history (count, 0); | |
705 | } | |
706 | saved_history_logical_offset = -1; | |
707 | _rl_internal_startup_hook = _rl_saved_internal_startup_hook; | |
708 | ||
709 | return (0); | |
710 | } | |
711 | ||
712 | int | |
74091dd4 | 713 | rl_operate_and_get_next (int count, int c) |
8868edaf CR |
714 | { |
715 | /* Accept the current line. */ | |
716 | rl_newline (1, c); | |
717 | ||
718 | saved_history_logical_offset = rl_explicit_arg ? count : where_history () + history_base + 1; | |
719 | ||
8868edaf CR |
720 | _rl_saved_internal_startup_hook = _rl_internal_startup_hook; |
721 | _rl_internal_startup_hook = set_saved_history; | |
722 | ||
723 | return 0; | |
724 | } | |
725 | ||
7117c2d2 JA |
726 | /* **************************************************************** */ |
727 | /* */ | |
728 | /* Editing Modes */ | |
729 | /* */ | |
730 | /* **************************************************************** */ | |
731 | /* How to toggle back and forth between editing modes. */ | |
732 | int | |
d233b485 | 733 | rl_vi_editing_mode (int count, int key) |
7117c2d2 JA |
734 | { |
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; | |
3185942a | 738 | rl_vi_insert_mode (1, key); |
7117c2d2 JA |
739 | #endif /* VI_MODE */ |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
744 | int | |
d233b485 | 745 | rl_emacs_editing_mode (int count, int key) |
7117c2d2 JA |
746 | { |
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; | |
ac50fbac CR |
750 | |
751 | if (_rl_show_mode_in_prompt) | |
752 | _rl_reset_prompt (); | |
753 | ||
7117c2d2 JA |
754 | return 0; |
755 | } | |
756 | ||
757 | /* Function for the rest of the library to use to set insert/overwrite mode. */ | |
758 | void | |
d233b485 | 759 | _rl_set_insert_mode (int im, int force) |
7117c2d2 JA |
760 | { |
761 | #ifdef CURSOR_MODE | |
762 | _rl_set_cursor (im, force); | |
763 | #endif | |
764 | ||
765 | rl_insert_mode = im; | |
766 | } | |
767 | ||
768 | /* Toggle overwrite mode. A positive explicit argument selects overwrite | |
769 | mode. A negative or zero explicit argument selects insert mode. */ | |
770 | int | |
d233b485 | 771 | rl_overwrite_mode (int count, int key) |
7117c2d2 JA |
772 | { |
773 | if (rl_explicit_arg == 0) | |
774 | _rl_set_insert_mode (rl_insert_mode ^ 1, 0); | |
775 | else if (count > 0) | |
776 | _rl_set_insert_mode (RL_IM_OVERWRITE, 0); | |
777 | else | |
778 | _rl_set_insert_mode (RL_IM_INSERT, 0); | |
779 | ||
780 | return 0; | |
781 | } |