d85789d590921d837956651e92890f0ae0f3811b
[platform/upstream/bash.git] / lib / readline / readline.c
1 /* readline.c -- a general facility for reading lines of input
2    with emacs style editing and completion. */
3
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6    This file is part of the GNU Readline Library, a library for
7    reading lines of text with interactive input and history editing.
8
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.
13
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.
18
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
24
25 #if defined (HAVE_CONFIG_H)
26 #  include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #include "posixstat.h"
31 #include <fcntl.h>
32 #if defined (HAVE_SYS_FILE_H)
33 #  include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
35
36 #if defined (HAVE_UNISTD_H)
37 #  include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39
40 #if defined (HAVE_STDLIB_H)
41 #  include <stdlib.h>
42 #else
43 #  include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
45
46 #if defined (HAVE_LOCALE_H)
47 #  include <locale.h>
48 #endif
49
50 #include <signal.h>
51 #include <stdio.h>
52 #include <setjmp.h>
53
54 /* System-specific feature definitions and include files. */
55 #include "rldefs.h"
56
57 #include "tcap.h"
58
59 /* Some standard library routines. */
60 #include "readline.h"
61 #include "history.h"
62
63 #define SWAP(s, e)  do { int t; t = s; s = e; e = t; } while (0)
64
65 /* NOTE: Functions and variables prefixed with `_rl_' are
66    pseudo-global: they are global so they can be shared
67    between files in the readline library, but are not intended
68    to be visible to readline callers. */
69
70 /* Variables and functions imported from terminal.c */
71 extern int _rl_init_terminal_io ();
72 extern void _rl_enable_meta_key ();
73 extern int _rl_output_character_function ();
74 extern void _rl_get_screen_size ();
75
76 extern int _rl_enable_meta;
77 extern int _rl_term_autowrap;
78 extern char *term_backspace, *term_clreol, *term_clrpag;
79 extern int screenwidth, screenheight, screenchars;
80
81 /* Variables and functions imported from rltty.c. */
82 extern void rl_prep_terminal (), rl_deprep_terminal ();
83 extern void rltty_set_default_bindings ();
84
85 /* Functions imported from util.c. */
86 extern void _rl_abort_internal ();
87 extern void rl_extend_line_buffer ();
88 extern int alphabetic ();
89
90 /* Functions imported from bind.c. */
91 extern void _rl_bind_if_unbound ();
92 extern int rl_set_keymap_from_edit_mode ();
93
94 /* Functions imported from input.c. */
95 extern int _rl_any_typein ();
96 extern void _rl_insert_typein ();
97 extern int rl_read_key ();
98
99 /* Functions imported from nls.c */
100 extern int _rl_init_eightbit ();
101
102 /* External redisplay functions and variables from display.c */
103 extern void _rl_move_vert ();
104 extern void _rl_update_final ();
105
106 extern void _rl_erase_at_end_of_line ();
107 extern void _rl_move_cursor_relative ();
108
109 extern int _rl_vis_botlin;
110 extern int _rl_last_c_pos;
111 extern int _rl_horizontal_scroll_mode;
112 extern int rl_display_fixed;
113 extern int _rl_suppress_redisplay;
114 extern char *rl_display_prompt;
115
116 /* Variables imported from complete.c. */
117 extern char *rl_completer_word_break_characters;
118 extern char *rl_basic_word_break_characters;
119 extern int rl_completion_query_items;
120 extern int rl_complete_with_tilde_expansion;
121
122 /* Variables and functions from macro.c. */
123 extern void _rl_add_macro_char ();
124 extern void _rl_with_macro_input ();
125 extern int _rl_next_macro_key ();
126 extern int _rl_defining_kbd_macro;
127
128 #if defined (VI_MODE)
129 /* Functions imported from vi_mode.c. */
130 extern void _rl_vi_set_last ();
131 extern void _rl_vi_reset_last ();
132 extern void _rl_vi_done_inserting ();
133 extern int _rl_vi_textmod_command ();
134 extern void _rl_vi_initialize_line ();
135 #endif /* VI_MODE */
136
137 extern UNDO_LIST *rl_undo_list;
138 extern int _rl_doing_an_undo;
139
140 /* Forward declarations used in this file. */
141 void _rl_free_history_entry ();
142
143 int _rl_dispatch ();
144 int _rl_init_argument ();
145
146 static char *readline_internal ();
147 static void readline_initialize_everything ();
148 static void start_using_history ();
149 static void bind_arrow_keys ();
150
151 #if !defined (__GO32__)
152 static void readline_default_bindings ();
153 #endif /* !__GO32__ */
154
155 #if defined (__GO32__)
156 #  include <pc.h>
157 #  undef HANDLE_SIGNALS
158 #endif /* __GO32__ */
159
160 extern char *xmalloc (), *xrealloc ();
161
162 /* **************************************************************** */
163 /*                                                                  */
164 /*                      Line editing input utility                  */
165 /*                                                                  */
166 /* **************************************************************** */
167
168 char *rl_library_version = "2.1";
169
170 /* A pointer to the keymap that is currently in use.
171    By default, it is the standard emacs keymap. */
172 Keymap _rl_keymap = emacs_standard_keymap;
173
174 /* The current style of editing. */
175 int rl_editing_mode = emacs_mode;
176
177 /* Non-zero if the previous command was a kill command. */
178 int _rl_last_command_was_kill = 0;
179
180 /* The current value of the numeric argument specified by the user. */
181 int rl_numeric_arg = 1;
182
183 /* Non-zero if an argument was typed. */
184 int rl_explicit_arg = 0;
185
186 /* Temporary value used while generating the argument. */
187 int rl_arg_sign = 1;
188
189 /* Non-zero means we have been called at least once before. */
190 static int rl_initialized;
191
192 /* If non-zero, this program is running in an EMACS buffer. */
193 static int running_in_emacs;
194
195 /* The current offset in the current input line. */
196 int rl_point;
197
198 /* Mark in the current input line. */
199 int rl_mark;
200
201 /* Length of the current input line. */
202 int rl_end;
203
204 /* Make this non-zero to return the current input_line. */
205 int rl_done;
206
207 /* The last function executed by readline. */
208 Function *rl_last_func = (Function *)NULL;
209
210 /* Top level environment for readline_internal (). */
211 jmp_buf readline_top_level;
212
213 /* The streams we interact with. */
214 FILE *_rl_in_stream, *_rl_out_stream;
215
216 /* The names of the streams that we do input and output to. */
217 FILE *rl_instream = (FILE *)NULL;
218 FILE *rl_outstream = (FILE *)NULL;
219
220 /* Non-zero means echo characters as they are read. */
221 int readline_echoing_p = 1;
222
223 /* Current prompt. */
224 char *rl_prompt;
225 int rl_visible_prompt_length = 0;
226
227 /* The number of characters read in order to type this complete command. */
228 int rl_key_sequence_length = 0;
229
230 /* If non-zero, then this is the address of a function to call just
231    before readline_internal () prints the first prompt. */
232 Function *rl_startup_hook = (Function *)NULL;
233
234 /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
235 static char *the_line;
236
237 /* The character that can generate an EOF.  Really read from
238    the terminal driver... just defaulted here. */
239 int _rl_eof_char = CTRL ('D');
240
241 /* Non-zero makes this the next keystroke to read. */
242 int rl_pending_input = 0;
243
244 /* Pointer to a useful terminal name. */
245 char *rl_terminal_name = (char *)NULL;
246
247 /* Non-zero means to always use horizontal scrolling in line display. */
248 int _rl_horizontal_scroll_mode = 0;
249
250 /* Non-zero means to display an asterisk at the starts of history lines
251    which have been modified. */
252 int _rl_mark_modified_lines = 0;  
253
254 /* The style of `bell' notification preferred.  This can be set to NO_BELL,
255    AUDIBLE_BELL, or VISIBLE_BELL. */
256 int _rl_bell_preference = AUDIBLE_BELL;
257      
258 /* String inserted into the line by rl_insert_comment (). */
259 char *_rl_comment_begin;
260
261 /* Keymap holding the function currently being executed. */
262 Keymap rl_executing_keymap;
263
264 /* Line buffer and maintenence. */
265 char *rl_line_buffer = (char *)NULL;
266 int rl_line_buffer_len = 0;
267
268 /* Forward declarations used by the display and termcap code. */
269
270 /* **************************************************************** */
271 /*                                                                  */
272 /*                      `Forward' declarations                      */
273 /*                                                                  */
274 /* **************************************************************** */
275
276 /* Non-zero means do not parse any lines other than comments and
277    parser directives. */
278 unsigned char _rl_parsing_conditionalized_out = 0;
279
280 /* Non-zero means to convert characters with the meta bit set to
281    escape-prefixed characters so we can indirect through
282    emacs_meta_keymap or vi_escape_keymap. */
283 int _rl_convert_meta_chars_to_ascii = 1;
284
285 /* Non-zero means to output characters with the meta bit set directly
286    rather than as a meta-prefixed escape sequence. */
287 int _rl_output_meta_chars = 0;
288
289 /* **************************************************************** */
290 /*                                                                  */
291 /*                      Top Level Functions                         */
292 /*                                                                  */
293 /* **************************************************************** */
294
295 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
296 int _rl_meta_flag = 0;  /* Forward declaration */
297
298 /* Read a line of input.  Prompt with PROMPT.  An empty PROMPT means
299    none.  A return value of NULL means that EOF was encountered. */
300 char *
301 readline (prompt)
302      char *prompt;
303 {
304   char *value;
305
306   rl_prompt = prompt;
307
308   /* If we are at EOF return a NULL string. */
309   if (rl_pending_input == EOF)
310     {
311       rl_pending_input = 0;
312       return ((char *)NULL);
313     }
314
315   rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
316
317   rl_initialize ();
318   (*rl_prep_term_function) (_rl_meta_flag);
319
320 #if defined (HANDLE_SIGNALS)
321   rl_set_signals ();
322 #endif
323
324   value = readline_internal ();
325   (*rl_deprep_term_function) ();
326
327 #if defined (HANDLE_SIGNALS)
328   rl_clear_signals ();
329 #endif
330
331   return (value);
332 }
333
334 #if defined (READLINE_CALLBACKS)
335 #  define STATIC_CALLBACK
336 #else
337 #  define STATIC_CALLBACK static
338 #endif
339
340 STATIC_CALLBACK void
341 readline_internal_setup ()
342 {
343   _rl_in_stream = rl_instream;
344   _rl_out_stream = rl_outstream;
345
346   if (rl_startup_hook)
347     (*rl_startup_hook) ();
348
349   if (readline_echoing_p == 0)
350     {
351       if (rl_prompt)
352         {
353           fprintf (_rl_out_stream, "%s", rl_prompt);
354           fflush (_rl_out_stream);
355         }
356     }
357   else
358     {
359       rl_on_new_line ();
360       (*rl_redisplay_function) ();
361 #if defined (VI_MODE)
362       if (rl_editing_mode == vi_mode)
363         rl_vi_insertion_mode ();
364 #endif /* VI_MODE */
365     }
366 }
367
368 STATIC_CALLBACK char *
369 readline_internal_teardown (eof)
370      int eof;
371 {
372   char *temp;
373   HIST_ENTRY *entry;
374
375   /* Restore the original of this history line, iff the line that we
376      are editing was originally in the history, AND the line has changed. */
377   entry = current_history ();
378
379   if (entry && rl_undo_list)
380     {
381       temp = savestring (the_line);
382       rl_revert_line (1, 0);
383       entry = replace_history_entry (where_history (), the_line, (HIST_ENTRY *)NULL);
384       _rl_free_history_entry (entry);
385
386       strcpy (the_line, temp);
387       free (temp);
388     }
389
390   /* At any rate, it is highly likely that this line has an undo list.  Get
391      rid of it now. */
392   if (rl_undo_list)
393     free_undo_list ();
394
395   return (eof ? (char *)NULL : savestring (the_line));
396 }
397
398 STATIC_CALLBACK int
399 #if defined (READLINE_CALLBACKS)
400 readline_internal_char ()
401 #else
402 readline_internal_charloop ()
403 #endif
404 {
405   static int lastc, eof_found;
406   int c, code, lk;
407
408   lastc = -1;
409   eof_found = 0;
410
411 #if !defined (READLINE_CALLBACKS)
412   while (rl_done == 0)
413     {
414 #endif
415       lk = _rl_last_command_was_kill;
416
417       code = setjmp (readline_top_level);
418
419       if (code)
420         (*rl_redisplay_function) ();
421
422       if (rl_pending_input == 0)
423         {
424           /* Then initialize the argument and number of keys read. */
425           _rl_init_argument ();
426           rl_key_sequence_length = 0;
427         }
428
429       c = rl_read_key ();
430
431       /* EOF typed to a non-blank line is a <NL>. */
432       if (c == EOF && rl_end)
433         c = NEWLINE;
434
435       /* The character _rl_eof_char typed to blank line, and not as the
436          previous character is interpreted as EOF. */
437       if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
438         {
439 #if defined (READLINE_CALLBACKS)
440           return (rl_done = 1);
441 #else
442           eof_found = 1;
443           break;
444 #endif
445         }
446
447       lastc = c;
448       _rl_dispatch (c, _rl_keymap);
449
450       /* If there was no change in _rl_last_command_was_kill, then no kill
451          has taken place.  Note that if input is pending we are reading
452          a prefix command, so nothing has changed yet. */
453       if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
454         _rl_last_command_was_kill = 0;
455
456 #if defined (VI_MODE)
457       /* In vi mode, when you exit insert mode, the cursor moves back
458          over the previous character.  We explicitly check for that here. */
459       if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
460         rl_vi_check ();
461 #endif /* VI_MODE */
462
463       if (rl_done == 0)
464         (*rl_redisplay_function) ();
465
466 #if defined (READLINE_CALLBACKS)
467       return 0;
468 #else
469     }
470
471   return (eof_found);
472 #endif
473 }
474
475 #if defined (READLINE_CALLBACKS)
476 static int
477 readline_internal_charloop ()
478 {
479   int eof;
480
481   while (rl_done == 0)
482     eof = readline_internal_char ();
483   return (eof);
484 }
485 #endif /* READLINE_CALLBACKS */
486
487 /* Read a line of input from the global rl_instream, doing output on
488    the global rl_outstream.
489    If rl_prompt is non-null, then that is our prompt. */
490 static char *
491 readline_internal ()
492 {
493   int eof;
494
495   readline_internal_setup ();
496   eof = readline_internal_charloop ();
497   return (readline_internal_teardown (eof));
498 }
499
500 void
501 _rl_set_the_line ()
502 {
503   the_line = rl_line_buffer;
504 }
505
506 /* Do the command associated with KEY in MAP.
507    If the associated command is really a keymap, then read
508    another key, and dispatch into that map. */
509 int
510 _rl_dispatch (key, map)
511      register int key;
512      Keymap map;
513 {
514   int r, newkey;
515   char *macro;
516   Function *func;
517
518   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
519     {
520       if (map[ESC].type == ISKMAP)
521         {
522           if (_rl_defining_kbd_macro)
523             _rl_add_macro_char (ESC);
524           map = FUNCTION_TO_KEYMAP (map, ESC);
525           key = UNMETA (key);
526           rl_key_sequence_length += 2;
527           return (_rl_dispatch (key, map));
528         }
529       else
530         ding ();
531       return 0;
532     }
533
534   if (_rl_defining_kbd_macro)
535     _rl_add_macro_char (key);
536
537   r = 0;
538   switch (map[key].type)
539     {
540     case ISFUNC:
541       func = map[key].function;
542       if (func != (Function *)NULL)
543         {
544           /* Special case rl_do_lowercase_version (). */
545           if (func == rl_do_lowercase_version)
546             return (_rl_dispatch (_rl_to_lower (key), map));
547
548           rl_executing_keymap = map;
549
550 #if 0
551           _rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
552 #endif
553
554           r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
555
556           /* If we have input pending, then the last command was a prefix
557              command.  Don't change the state of rl_last_func.  Otherwise,
558              remember the last command executed in this variable. */
559           if (!rl_pending_input)
560             rl_last_func = map[key].function;
561         }
562       else
563         {
564           _rl_abort_internal ();
565           return -1;
566         }
567       break;
568
569     case ISKMAP:
570       if (map[key].function != (Function *)NULL)
571         {
572           rl_key_sequence_length++;
573           newkey = rl_read_key ();
574           r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
575         }
576       else
577         {
578           _rl_abort_internal ();
579           return -1;
580         }
581       break;
582
583     case ISMACR:
584       if (map[key].function != (Function *)NULL)
585         {
586           macro = savestring ((char *)map[key].function);
587           _rl_with_macro_input (macro);
588           return 0;
589         }
590       break;
591     }
592 #if defined (VI_MODE)
593   if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
594       _rl_vi_textmod_command (key))
595     _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
596 #endif
597   return (r);
598 }
599
600 /* **************************************************************** */
601 /*                                                                  */
602 /*                      Initializations                             */
603 /*                                                                  */
604 /* **************************************************************** */
605
606 /* Initliaze readline (and terminal if not already). */
607 int
608 rl_initialize ()
609 {
610   /* If we have never been called before, initialize the
611      terminal and data structures. */
612   if (!rl_initialized)
613     {
614       readline_initialize_everything ();
615       rl_initialized++;
616     }
617
618   /* Initalize the current line information. */
619   rl_point = rl_end = 0;
620   the_line = rl_line_buffer;
621   the_line[0] = 0;
622
623   /* We aren't done yet.  We haven't even gotten started yet! */
624   rl_done = 0;
625
626   /* Tell the history routines what is going on. */
627   start_using_history ();
628
629   /* Make the display buffer match the state of the line. */
630   rl_reset_line_state ();
631
632   /* No such function typed yet. */
633   rl_last_func = (Function *)NULL;
634
635   /* Parsing of key-bindings begins in an enabled state. */
636   _rl_parsing_conditionalized_out = 0;
637
638 #if defined (VI_MODE)
639   if (rl_editing_mode == vi_mode)
640     _rl_vi_initialize_line ();
641 #endif
642
643   return 0;
644 }
645
646 /* Initialize the entire state of the world. */
647 static void
648 readline_initialize_everything ()
649 {
650   /* Find out if we are running in Emacs. */
651   running_in_emacs = getenv ("EMACS") != (char *)0;
652
653   /* Set up input and output if they are not already set up. */
654   if (!rl_instream)
655     rl_instream = stdin;
656
657   if (!rl_outstream)
658     rl_outstream = stdout;
659
660   /* Bind _rl_in_stream and _rl_out_stream immediately.  These values
661      may change, but they may also be used before readline_internal ()
662      is called. */
663   _rl_in_stream = rl_instream;
664   _rl_out_stream = rl_outstream;
665
666   /* Allocate data structures. */
667   if (!rl_line_buffer)
668     rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
669
670   /* Initialize the terminal interface. */
671   _rl_init_terminal_io ((char *)NULL);
672
673 #if !defined (__GO32__)
674   /* Bind tty characters to readline functions. */
675   readline_default_bindings ();
676 #endif /* !__GO32__ */
677
678   /* Initialize the function names. */
679   rl_initialize_funmap ();
680
681   /* Decide whether we should automatically go into eight-bit mode. */
682   _rl_init_eightbit ();
683       
684   /* Read in the init file. */
685   rl_read_init_file ((char *)NULL);
686
687   /* XXX */
688   if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
689     {
690       screenwidth--;
691       screenchars -= screenheight;
692     }
693
694   /* Override the effect of any `set keymap' assignments in the
695      inputrc file. */
696   rl_set_keymap_from_edit_mode ();
697
698   /* Try to bind a common arrow key prefix, if not already bound. */
699   bind_arrow_keys ();
700
701   /* Enable the meta key, if this terminal has one. */
702   if (_rl_enable_meta)
703     _rl_enable_meta_key ();
704
705   /* If the completion parser's default word break characters haven't
706      been set yet, then do so now. */
707   if (rl_completer_word_break_characters == (char *)NULL)
708     rl_completer_word_break_characters = rl_basic_word_break_characters;
709 }
710
711 /* If this system allows us to look at the values of the regular
712    input editing characters, then bind them to their readline
713    equivalents, iff the characters are not bound to keymaps. */
714 static void
715 readline_default_bindings ()
716 {
717   rltty_set_default_bindings (_rl_keymap);
718 }
719
720 static void
721 bind_arrow_keys_internal ()
722 {
723   Function *f;
724
725   f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
726   if (!f || f == rl_do_lowercase_version)
727     {
728       _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
729       _rl_bind_if_unbound ("\033[B", rl_get_next_history);
730       _rl_bind_if_unbound ("\033[C", rl_forward);
731       _rl_bind_if_unbound ("\033[D", rl_backward);
732     }
733
734   f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
735   if (!f || f == rl_do_lowercase_version)
736     {
737       _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
738       _rl_bind_if_unbound ("\033OB", rl_get_next_history);
739       _rl_bind_if_unbound ("\033OC", rl_forward);
740       _rl_bind_if_unbound ("\033OD", rl_backward);
741     }
742 }
743
744 /* Try and bind the common arrow key prefix after giving termcap and
745    the inputrc file a chance to bind them and create `real' keymaps
746    for the arrow key prefix. */
747 static void
748 bind_arrow_keys ()
749 {
750   Keymap xkeymap;
751
752   xkeymap = _rl_keymap;
753
754   _rl_keymap = emacs_standard_keymap;
755   bind_arrow_keys_internal ();
756
757 #if defined (VI_MODE)
758   _rl_keymap = vi_movement_keymap;
759   bind_arrow_keys_internal ();
760 #endif
761
762   _rl_keymap = xkeymap;
763 }
764
765 \f
766 /* **************************************************************** */
767 /*                                                                  */
768 /*                      Numeric Arguments                           */
769 /*                                                                  */
770 /* **************************************************************** */
771
772 /* Handle C-u style numeric args, as well as M--, and M-digits. */
773 static int
774 rl_digit_loop ()
775 {
776   int key, c, sawminus;
777
778   _rl_save_prompt ();
779
780   sawminus = 0;
781   while (1)
782     {
783       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
784       key = c = rl_read_key ();
785
786       if (_rl_keymap[c].type == ISFUNC &&
787           _rl_keymap[c].function == rl_universal_argument)
788         {
789           rl_numeric_arg *= 4;
790           continue;
791         }
792       c = UNMETA (c);
793       if (_rl_digit_p (c))
794         {
795           rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
796           rl_explicit_arg = 1;
797         }
798       else
799         {
800           if (c == '-' && rl_explicit_arg == 0)
801             {
802               rl_numeric_arg = sawminus = 1;
803               rl_arg_sign = -1;
804             }
805           else
806             {
807               /* Make M-- command equivalent to M--1 command. */
808               if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
809                 rl_explicit_arg = 1;
810               _rl_restore_prompt ();
811               rl_clear_message ();
812               return (_rl_dispatch (key, _rl_keymap));
813             }
814         }
815     }
816
817   return 0;
818 }
819
820 /* Add the current digit to the argument in progress. */
821 int
822 rl_digit_argument (ignore, key)
823      int ignore, key;
824 {
825   rl_pending_input = key;
826   return (rl_digit_loop ());
827 }
828
829 /* What to do when you abort reading an argument. */
830 int
831 rl_discard_argument ()
832 {
833   ding ();
834   rl_clear_message ();
835   _rl_init_argument ();
836   return 0;
837 }
838
839 /* Create a default argument. */
840 int
841 _rl_init_argument ()
842 {
843   rl_numeric_arg = rl_arg_sign = 1;
844   rl_explicit_arg = 0;
845   return 0;
846 }
847
848 /* C-u, universal argument.  Multiply the current argument by 4.
849    Read a key.  If the key has nothing to do with arguments, then
850    dispatch on it.  If the key is the abort character then abort. */
851 int
852 rl_universal_argument (count, key)
853      int count, key;
854 {
855   rl_numeric_arg *= 4;
856   return (rl_digit_loop ());
857 }
858
859 /* **************************************************************** */
860 /*                                                                  */
861 /*                      Insert and Delete                           */
862 /*                                                                  */
863 /* **************************************************************** */
864
865 /* Insert a string of text into the line at point.  This is the only
866    way that you should do insertion.  rl_insert () calls this
867    function. */
868 int
869 rl_insert_text (string)
870      char *string;
871 {
872   register int i, l = strlen (string);
873
874   if (rl_end + l >= rl_line_buffer_len)
875     rl_extend_line_buffer (rl_end + l);
876
877   for (i = rl_end; i >= rl_point; i--)
878     the_line[i + l] = the_line[i];
879   strncpy (the_line + rl_point, string, l);
880
881   /* Remember how to undo this if we aren't undoing something. */
882   if (!_rl_doing_an_undo)
883     {
884       /* If possible and desirable, concatenate the undos. */
885       if ((l == 1) &&
886           rl_undo_list &&
887           (rl_undo_list->what == UNDO_INSERT) &&
888           (rl_undo_list->end == rl_point) &&
889           (rl_undo_list->end - rl_undo_list->start < 20))
890         rl_undo_list->end++;
891       else
892         rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
893     }
894   rl_point += l;
895   rl_end += l;
896   the_line[rl_end] = '\0';
897   return l;
898 }
899
900 /* Delete the string between FROM and TO.  FROM is
901    inclusive, TO is not. */
902 int
903 rl_delete_text (from, to)
904      int from, to;
905 {
906   register char *text;
907   register int diff, i;
908
909   /* Fix it if the caller is confused. */
910   if (from > to)
911     SWAP (from, to);
912
913   /* fix boundaries */
914   if (to > rl_end)
915     {
916       to = rl_end;
917       if (from > to)
918         from = to;
919     }
920
921   text = rl_copy_text (from, to);
922
923   /* Some versions of strncpy() can't handle overlapping arguments. */
924   diff = to - from;
925   for (i = from; i < rl_end - diff; i++)
926     the_line[i] = the_line[i + diff];
927
928   /* Remember how to undo this delete. */
929   if (_rl_doing_an_undo == 0)
930     rl_add_undo (UNDO_DELETE, from, to, text);
931   else
932     free (text);
933
934   rl_end -= diff;
935   the_line[rl_end] = '\0';
936   return (diff);
937 }
938
939 /* **************************************************************** */
940 /*                                                                  */
941 /*                      Readline character functions                */
942 /*                                                                  */
943 /* **************************************************************** */
944
945 /* This is not a gap editor, just a stupid line input routine.  No hair
946    is involved in writing any of the functions, and none should be. */
947
948 /* Note that:
949
950    rl_end is the place in the string that we would place '\0';
951    i.e., it is always safe to place '\0' there.
952
953    rl_point is the place in the string where the cursor is.  Sometimes
954    this is the same as rl_end.
955
956    Any command that is called interactively receives two arguments.
957    The first is a count: the numeric arg pased to this command.
958    The second is the key which invoked this command.
959 */
960
961 /* **************************************************************** */
962 /*                                                                  */
963 /*                      Movement Commands                           */
964 /*                                                                  */
965 /* **************************************************************** */
966
967 /* Note that if you `optimize' the display for these functions, you cannot
968    use said functions in other functions which do not do optimizing display.
969    I.e., you will have to update the data base for rl_redisplay, and you
970    might as well let rl_redisplay do that job. */
971
972 /* Move forward COUNT characters. */
973 int
974 rl_forward (count, key)
975      int count, key;
976 {
977   if (count < 0)
978     rl_backward (-count, key);
979   else if (count > 0)
980     {
981       int end = rl_point + count;
982 #if defined (VI_MODE)
983       int lend = rl_end - (rl_editing_mode == vi_mode);
984 #else
985       int lend = rl_end;
986 #endif
987
988       if (end > lend)
989         {
990           rl_point = lend;
991           ding ();
992         }
993       else
994         rl_point = end;
995     }
996   return 0;
997 }
998
999 /* Move backward COUNT characters. */
1000 int
1001 rl_backward (count, key)
1002      int count, key;
1003 {
1004   if (count < 0)
1005     rl_forward (-count, key);
1006   else if (count > 0)
1007     {
1008       if (rl_point < count)
1009         {
1010           rl_point = 0;
1011           ding ();
1012         }
1013       else
1014         rl_point -= count;
1015     }
1016   return 0;
1017 }
1018
1019 /* Move to the beginning of the line. */
1020 int
1021 rl_beg_of_line (count, key)
1022      int count, key;
1023 {
1024   rl_point = 0;
1025   return 0;
1026 }
1027
1028 /* Move to the end of the line. */
1029 int
1030 rl_end_of_line (count, key)
1031      int count, key;
1032 {
1033   rl_point = rl_end;
1034   return 0;
1035 }
1036
1037 /* Move forward a word.  We do what Emacs does. */
1038 int
1039 rl_forward_word (count, key)
1040      int count, key;
1041 {
1042   int c;
1043
1044   if (count < 0)
1045     {
1046       rl_backward_word (-count, key);
1047       return 0;
1048     }
1049
1050   while (count)
1051     {
1052       if (rl_point == rl_end)
1053         return 0;
1054
1055       /* If we are not in a word, move forward until we are in one.
1056          Then, move forward until we hit a non-alphabetic character. */
1057       c = the_line[rl_point];
1058       if (alphabetic (c) == 0)
1059         {
1060           while (++rl_point < rl_end)
1061             {
1062               c = the_line[rl_point];
1063               if (alphabetic (c))
1064                 break;
1065             }
1066         }
1067       if (rl_point == rl_end)
1068         return 0;
1069       while (++rl_point < rl_end)
1070         {
1071           c = the_line[rl_point];
1072           if (alphabetic (c) == 0)
1073             break;
1074         }
1075       --count;
1076     }
1077   return 0;
1078 }
1079
1080 /* Move backward a word.  We do what Emacs does. */
1081 int
1082 rl_backward_word (count, key)
1083      int count, key;
1084 {
1085   int c;
1086
1087   if (count < 0)
1088     {
1089       rl_forward_word (-count, key);
1090       return 0;
1091     }
1092
1093   while (count)
1094     {
1095       if (!rl_point)
1096         return 0;
1097
1098       /* Like rl_forward_word (), except that we look at the characters
1099          just before point. */
1100
1101       c = the_line[rl_point - 1];
1102       if (alphabetic (c) == 0)
1103         {
1104           while (--rl_point)
1105             {
1106               c = the_line[rl_point - 1];
1107               if (alphabetic (c))
1108                 break;
1109             }
1110         }
1111
1112       while (rl_point)
1113         {
1114           c = the_line[rl_point - 1];
1115           if (alphabetic (c) == 0)
1116             break;
1117           else
1118             --rl_point;
1119         }
1120       --count;
1121     }
1122   return 0;
1123 }
1124
1125 /* Clear the current line.  Numeric argument to C-l does this. */
1126 int
1127 rl_refresh_line ()
1128 {
1129   int curr_line, nleft;
1130
1131   /* Find out whether or not there might be invisible characters in the
1132      editing buffer. */
1133   if (rl_display_prompt == rl_prompt)
1134     nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1135   else
1136     nleft = _rl_last_c_pos - screenwidth;
1137
1138   if (nleft > 0)
1139     curr_line = 1 + nleft / screenwidth;
1140   else
1141     curr_line = 0;
1142
1143   _rl_move_vert (curr_line);
1144   _rl_move_cursor_relative (0, the_line);   /* XXX is this right */
1145
1146 #if defined (__GO32__)
1147   {
1148     int row, col, width, row_start;
1149
1150     ScreenGetCursor (&row, &col);
1151     width = ScreenCols ();
1152     row_start = ScreenPrimary + (row * width);
1153     memset (row_start + col, 0, (width - col) * 2);
1154   }
1155 #else /* !__GO32__ */
1156   if (term_clreol)
1157     tputs (term_clreol, 1, _rl_output_character_function);
1158 #endif /* !__GO32__ */
1159
1160   rl_forced_update_display ();
1161   rl_display_fixed = 1;
1162
1163   return 0;
1164 }
1165
1166 /* C-l typed to a line without quoting clears the screen, and then reprints
1167    the prompt and the current input line.  Given a numeric arg, redraw only
1168    the current line. */
1169 int
1170 rl_clear_screen (count, key)
1171      int count, key;
1172 {
1173   if (rl_explicit_arg)
1174     {
1175       rl_refresh_line ();
1176       return 0;
1177     }
1178
1179 #if !defined (__GO32__)
1180   if (term_clrpag)
1181     tputs (term_clrpag, 1, _rl_output_character_function);
1182   else
1183 #endif /* !__GO32__ */
1184     crlf ();
1185
1186   rl_forced_update_display ();
1187   rl_display_fixed = 1;
1188
1189   return 0;
1190 }
1191
1192 int
1193 rl_arrow_keys (count, c)
1194      int count, c;
1195 {
1196   int ch;
1197
1198   ch = rl_read_key ();
1199
1200   switch (_rl_to_upper (ch))
1201     {
1202     case 'A':
1203       rl_get_previous_history (count, ch);
1204       break;
1205
1206     case 'B':
1207       rl_get_next_history (count, ch);
1208       break;
1209
1210     case 'C':
1211       rl_forward (count, ch);
1212       break;
1213
1214     case 'D':
1215       rl_backward (count, ch);
1216       break;
1217
1218     default:
1219       ding ();
1220     }
1221   return 0;
1222 }
1223
1224 \f
1225 /* **************************************************************** */
1226 /*                                                                  */
1227 /*                      Text commands                               */
1228 /*                                                                  */
1229 /* **************************************************************** */
1230
1231 /* Insert the character C at the current location, moving point forward. */
1232 int
1233 rl_insert (count, c)
1234      int count, c;
1235 {
1236   register int i;
1237   char *string;
1238
1239   if (count <= 0)
1240     return 0;
1241
1242   /* If we can optimize, then do it.  But don't let people crash
1243      readline because of extra large arguments. */
1244   if (count > 1 && count <= 1024)
1245     {
1246       string = xmalloc (1 + count);
1247
1248       for (i = 0; i < count; i++)
1249         string[i] = c;
1250
1251       string[i] = '\0';
1252       rl_insert_text (string);
1253       free (string);
1254
1255       return 0;
1256     }
1257
1258   if (count > 1024)
1259     {
1260       int decreaser;
1261       char str[1024+1];
1262
1263       for (i = 0; i < 1024; i++)
1264         str[i] = c;
1265
1266       while (count)
1267         {
1268           decreaser = (count > 1024 ? 1024 : count);
1269           str[decreaser] = '\0';
1270           rl_insert_text (str);
1271           count -= decreaser;
1272         }
1273
1274       return 0;
1275     }
1276
1277   /* We are inserting a single character.
1278      If there is pending input, then make a string of all of the
1279      pending characters that are bound to rl_insert, and insert
1280      them all. */
1281   if (_rl_any_typein ())
1282     _rl_insert_typein (c);
1283   else
1284     {
1285       /* Inserting a single character. */
1286       char str[2];
1287
1288       str[1] = '\0';
1289       str[0] = c;
1290       rl_insert_text (str);
1291     }
1292   return 0;
1293 }
1294
1295 /* Insert the next typed character verbatim. */
1296 int
1297 rl_quoted_insert (count, key)
1298      int count, key;
1299 {
1300   int c;
1301
1302   c = rl_read_key ();
1303   return (rl_insert (count, c));  
1304 }
1305
1306 /* Insert a tab character. */
1307 int
1308 rl_tab_insert (count, key)
1309      int count, key;
1310 {
1311   return (rl_insert (count, '\t'));
1312 }
1313
1314 /* What to do when a NEWLINE is pressed.  We accept the whole line.
1315    KEY is the key that invoked this command.  I guess it could have
1316    meaning in the future. */
1317 int
1318 rl_newline (count, key)
1319      int count, key;
1320 {
1321   rl_done = 1;
1322
1323 #if defined (VI_MODE)
1324   _rl_vi_done_inserting ();
1325   _rl_vi_reset_last ();
1326 #endif /* VI_MODE */
1327
1328   if (readline_echoing_p)
1329     _rl_update_final ();
1330   return 0;
1331 }
1332
1333 /* What to do for some uppercase characters, like meta characters,
1334    and some characters appearing in emacs_ctlx_keymap.  This function
1335    is just a stub, you bind keys to it and the code in _rl_dispatch ()
1336    is special cased. */
1337 int
1338 rl_do_lowercase_version (ignore1, ignore2)
1339      int ignore1, ignore2;
1340 {
1341   return 0;
1342 }
1343
1344 /* Rubout the character behind point. */
1345 int
1346 rl_rubout (count, key)
1347      int count, key;
1348 {
1349   if (count < 0)
1350     {
1351       rl_delete (-count, key);
1352       return 0;
1353     }
1354
1355   if (!rl_point)
1356     {
1357       ding ();
1358       return -1;
1359     }
1360
1361   if (count > 1 || rl_explicit_arg)
1362     {
1363       int orig_point = rl_point;
1364       rl_backward (count, key);
1365       rl_kill_text (orig_point, rl_point);
1366     }
1367   else
1368     {
1369       int c = the_line[--rl_point];
1370       rl_delete_text (rl_point, rl_point + 1);
1371
1372       if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
1373         {
1374           int l;
1375           l = rl_character_len (c, rl_point);
1376           _rl_erase_at_end_of_line (l);
1377         }
1378     }
1379   return 0;
1380 }
1381
1382 /* Delete the character under the cursor.  Given a numeric argument,
1383    kill that many characters instead. */
1384 int
1385 rl_delete (count, key)
1386      int count, key;
1387 {
1388   if (count < 0)
1389     return (rl_rubout (-count, key));
1390
1391   if (rl_point == rl_end)
1392     {
1393       ding ();
1394       return -1;
1395     }
1396
1397   if (count > 1 || rl_explicit_arg)
1398     {
1399       int orig_point = rl_point;
1400       rl_forward (count, key);
1401       rl_kill_text (orig_point, rl_point);
1402       rl_point = orig_point;
1403       return 0;
1404     }
1405   else
1406     return (rl_delete_text (rl_point, rl_point + 1));
1407   
1408 }
1409
1410 /* Delete all spaces and tabs around point. */
1411 int
1412 rl_delete_horizontal_space (count, ignore)
1413      int count, ignore;
1414 {
1415   int start = rl_point;
1416
1417   while (rl_point && whitespace (the_line[rl_point - 1]))
1418     rl_point--;
1419
1420   start = rl_point;
1421
1422   while (rl_point < rl_end && whitespace (the_line[rl_point]))
1423     rl_point++;
1424
1425   if (start != rl_point)
1426     {
1427       rl_delete_text (start, rl_point);
1428       rl_point = start;
1429     }
1430   return 0;
1431 }
1432
1433 #ifndef RL_COMMENT_BEGIN_DEFAULT
1434 #define RL_COMMENT_BEGIN_DEFAULT "#"
1435 #endif
1436
1437 /* Turn the current line into a comment in shell history.
1438    A K*rn shell style function. */
1439 int
1440 rl_insert_comment (count, key)
1441      int count, key;
1442 {
1443   rl_beg_of_line (1, key);
1444   rl_insert_text (_rl_comment_begin ? _rl_comment_begin
1445                                     : RL_COMMENT_BEGIN_DEFAULT);
1446   (*rl_redisplay_function) ();
1447   rl_newline (1, '\n');
1448   return (0);
1449 }
1450
1451 /* **************************************************************** */
1452 /*                                                                  */
1453 /*                      Changing Case                               */
1454 /*                                                                  */
1455 /* **************************************************************** */
1456
1457 /* The three kinds of things that we know how to do. */
1458 #define UpCase 1
1459 #define DownCase 2
1460 #define CapCase 3
1461
1462 static int rl_change_case ();
1463
1464 /* Uppercase the word at point. */
1465 int
1466 rl_upcase_word (count, key)
1467      int count, key;
1468 {
1469   return (rl_change_case (count, UpCase));
1470 }
1471
1472 /* Lowercase the word at point. */
1473 int
1474 rl_downcase_word (count, key)
1475      int count, key;
1476 {
1477   return (rl_change_case (count, DownCase));
1478 }
1479
1480 /* Upcase the first letter, downcase the rest. */
1481 int
1482 rl_capitalize_word (count, key)
1483      int count, key;
1484 {
1485  return (rl_change_case (count, CapCase));
1486 }
1487
1488 /* The meaty function.
1489    Change the case of COUNT words, performing OP on them.
1490    OP is one of UpCase, DownCase, or CapCase.
1491    If a negative argument is given, leave point where it started,
1492    otherwise, leave it where it moves to. */
1493 static int
1494 rl_change_case (count, op)
1495      int count, op;
1496 {
1497   register int start, end;
1498   int inword, c;
1499
1500   start = rl_point;
1501   rl_forward_word (count, 0);
1502   end = rl_point;
1503
1504   if (count < 0)
1505     SWAP (start, end);
1506
1507   /* We are going to modify some text, so let's prepare to undo it. */
1508   rl_modifying (start, end);
1509
1510   for (inword = 0; start < end; start++)
1511     {
1512       c = the_line[start];
1513       switch (op)
1514         {
1515         case UpCase:
1516           the_line[start] = _rl_to_upper (c);
1517           break;
1518
1519         case DownCase:
1520           the_line[start] = _rl_to_lower (c);
1521           break;
1522
1523         case CapCase:
1524           the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
1525           inword = alphabetic (the_line[start]);
1526           break;
1527
1528         default:
1529           ding ();
1530           return -1;
1531         }
1532     }
1533   rl_point = end;
1534   return 0;
1535 }
1536
1537 /* **************************************************************** */
1538 /*                                                                  */
1539 /*                      Transposition                               */
1540 /*                                                                  */
1541 /* **************************************************************** */
1542
1543 /* Transpose the words at point. */
1544 int
1545 rl_transpose_words (count, key)
1546      int count, key;
1547 {
1548   char *word1, *word2;
1549   int w1_beg, w1_end, w2_beg, w2_end;
1550   int orig_point = rl_point;
1551
1552   if (!count)
1553     return 0;
1554
1555   /* Find the two words. */
1556   rl_forward_word (count, key);
1557   w2_end = rl_point;
1558   rl_backward_word (1, key);
1559   w2_beg = rl_point;
1560   rl_backward_word (count, key);
1561   w1_beg = rl_point;
1562   rl_forward_word (1, key);
1563   w1_end = rl_point;
1564
1565   /* Do some check to make sure that there really are two words. */
1566   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1567     {
1568       ding ();
1569       rl_point = orig_point;
1570       return -1;
1571     }
1572
1573   /* Get the text of the words. */
1574   word1 = rl_copy_text (w1_beg, w1_end);
1575   word2 = rl_copy_text (w2_beg, w2_end);
1576
1577   /* We are about to do many insertions and deletions.  Remember them
1578      as one operation. */
1579   rl_begin_undo_group ();
1580
1581   /* Do the stuff at word2 first, so that we don't have to worry
1582      about word1 moving. */
1583   rl_point = w2_beg;
1584   rl_delete_text (w2_beg, w2_end);
1585   rl_insert_text (word1);
1586
1587   rl_point = w1_beg;
1588   rl_delete_text (w1_beg, w1_end);
1589   rl_insert_text (word2);
1590
1591   /* This is exactly correct since the text before this point has not
1592      changed in length. */
1593   rl_point = w2_end;
1594
1595   /* I think that does it. */
1596   rl_end_undo_group ();
1597   free (word1);
1598   free (word2);
1599
1600   return 0;
1601 }
1602
1603 /* Transpose the characters at point.  If point is at the end of the line,
1604    then transpose the characters before point. */
1605 int
1606 rl_transpose_chars (count, key)
1607      int count, key;
1608 {
1609   char dummy[2];
1610
1611   if (!count)
1612     return 0;
1613
1614   if (!rl_point || rl_end < 2)
1615     {
1616       ding ();
1617       return -1;
1618     }
1619
1620   rl_begin_undo_group ();
1621
1622   if (rl_point == rl_end)
1623     {
1624       --rl_point;
1625       count = 1;
1626     }
1627   rl_point--;
1628
1629   dummy[0] = the_line[rl_point];
1630   dummy[1] = '\0';
1631
1632   rl_delete_text (rl_point, rl_point + 1);
1633
1634   rl_point += count;
1635   if (rl_point > rl_end)
1636     rl_point = rl_end;
1637   else if (rl_point < 0)
1638     rl_point = 0;
1639   rl_insert_text (dummy);
1640
1641   rl_end_undo_group ();
1642   return 0;
1643 }
1644
1645 /* **************************************************************** */
1646 /*                                                                  */
1647 /*                      Character Searching                         */
1648 /*                                                                  */
1649 /* **************************************************************** */
1650
1651 int
1652 _rl_char_search_internal (count, dir, schar)
1653      int count, dir, schar;
1654 {
1655   int pos, inc;
1656
1657   pos = rl_point;
1658   inc = (dir < 0) ? -1 : 1;
1659   while (count)
1660     {
1661       if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1662         {
1663           ding ();
1664           return -1;
1665         }
1666
1667       pos += inc;
1668       do
1669         {
1670           if (rl_line_buffer[pos] == schar)
1671             {
1672               count--;
1673               if (dir < 0)
1674                 rl_point = (dir == BTO) ? pos + 1 : pos;
1675               else
1676                 rl_point = (dir == FTO) ? pos - 1 : pos;
1677               break;
1678             }
1679         }
1680       while ((dir < 0) ? pos-- : ++pos < rl_end);
1681     }
1682   return (0);
1683 }
1684
1685 /* Search COUNT times for a character read from the current input stream.
1686    FDIR is the direction to search if COUNT is non-negative; otherwise
1687    the search goes in BDIR. */
1688 static int
1689 _rl_char_search (count, fdir, bdir)
1690      int count, fdir, bdir;
1691 {
1692   int c;
1693
1694   c = rl_read_key ();
1695   if (count < 0)
1696     return (_rl_char_search_internal (-count, bdir, c));
1697   else
1698     return (_rl_char_search_internal (count, fdir, c));
1699 }
1700
1701 int
1702 rl_char_search (count, key)
1703      int count, key;
1704 {
1705   return (_rl_char_search (count, FFIND, BFIND));
1706 }
1707
1708 int
1709 rl_backward_char_search (count, key)
1710      int count, key;
1711 {
1712   return (_rl_char_search (count, BFIND, FFIND));
1713 }
1714
1715 /* **************************************************************** */
1716 /*                                                                  */
1717 /*                      History Utilities                           */
1718 /*                                                                  */
1719 /* **************************************************************** */
1720
1721 /* We already have a history library, and that is what we use to control
1722    the history features of readline.  This is our local interface to
1723    the history mechanism. */
1724
1725 /* While we are editing the history, this is the saved
1726    version of the original line. */
1727 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
1728
1729 /* Set the history pointer back to the last entry in the history. */
1730 static void
1731 start_using_history ()
1732 {
1733   using_history ();
1734   if (saved_line_for_history)
1735     _rl_free_history_entry (saved_line_for_history);
1736
1737   saved_line_for_history = (HIST_ENTRY *)NULL;
1738 }
1739
1740 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1741 void
1742 _rl_free_history_entry (entry)
1743      HIST_ENTRY *entry;
1744 {
1745   if (entry == 0)
1746     return;
1747   if (entry->line)
1748     free (entry->line);
1749   free (entry);
1750 }
1751
1752 /* Perhaps put back the current line if it has changed. */
1753 int
1754 maybe_replace_line ()
1755 {
1756   HIST_ENTRY *temp;
1757
1758   temp = current_history ();
1759   /* If the current line has changed, save the changes. */
1760   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
1761     {
1762       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
1763       free (temp->line);
1764       free (temp);
1765     }
1766   return 0;
1767 }
1768
1769 /* Put back the saved_line_for_history if there is one. */
1770 int
1771 maybe_unsave_line ()
1772 {
1773   int line_len;
1774
1775   if (saved_line_for_history)
1776     {
1777       line_len = strlen (saved_line_for_history->line);
1778
1779       if (line_len >= rl_line_buffer_len)
1780         rl_extend_line_buffer (line_len);
1781
1782       strcpy (the_line, saved_line_for_history->line);
1783       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
1784       _rl_free_history_entry (saved_line_for_history);
1785       saved_line_for_history = (HIST_ENTRY *)NULL;
1786       rl_end = rl_point = strlen (the_line);
1787     }
1788   else
1789     ding ();
1790   return 0;
1791 }
1792
1793 /* Save the current line in saved_line_for_history. */
1794 int
1795 maybe_save_line ()
1796 {
1797   if (saved_line_for_history == 0)
1798     {
1799       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
1800       saved_line_for_history->line = savestring (the_line);
1801       saved_line_for_history->data = (char *)rl_undo_list;
1802     }
1803   return 0;
1804 }
1805
1806 /* **************************************************************** */
1807 /*                                                                  */
1808 /*                      History Commands                            */
1809 /*                                                                  */
1810 /* **************************************************************** */
1811
1812 /* Meta-< goes to the start of the history. */
1813 int
1814 rl_beginning_of_history (count, key)
1815      int count, key;
1816 {
1817   return (rl_get_previous_history (1 + where_history (), key));
1818 }
1819
1820 /* Meta-> goes to the end of the history.  (The current line). */
1821 int
1822 rl_end_of_history (count, key)
1823      int count, key;
1824 {
1825   maybe_replace_line ();
1826   using_history ();
1827   maybe_unsave_line ();
1828   return 0;
1829 }
1830
1831 /* Move down to the next history line. */
1832 int
1833 rl_get_next_history (count, key)
1834      int count, key;
1835 {
1836   HIST_ENTRY *temp;
1837   int line_len;
1838
1839   if (count < 0)
1840     return (rl_get_previous_history (-count, key));
1841
1842   if (count == 0)
1843     return 0;
1844
1845   maybe_replace_line ();
1846
1847   temp = (HIST_ENTRY *)NULL;
1848   while (count)
1849     {
1850       temp = next_history ();
1851       if (!temp)
1852         break;
1853       --count;
1854     }
1855
1856   if (temp == 0)
1857     maybe_unsave_line ();
1858   else
1859     {
1860       line_len = strlen (temp->line);
1861
1862       if (line_len >= rl_line_buffer_len)
1863         rl_extend_line_buffer (line_len);
1864
1865       strcpy (the_line, temp->line);
1866       rl_undo_list = (UNDO_LIST *)temp->data;
1867       rl_end = rl_point = strlen (the_line);
1868 #if defined (VI_MODE)
1869       if (rl_editing_mode == vi_mode)
1870         rl_point = 0;
1871 #endif /* VI_MODE */
1872     }
1873   return 0;
1874 }
1875
1876 /* Get the previous item out of our interactive history, making it the current
1877    line.  If there is no previous history, just ding. */
1878 int
1879 rl_get_previous_history (count, key)
1880      int count, key;
1881 {
1882   HIST_ENTRY *old_temp, *temp;
1883   int line_len;
1884
1885   if (count < 0)
1886     return (rl_get_next_history (-count, key));
1887
1888   if (count == 0)
1889     return 0;
1890
1891   /* If we don't have a line saved, then save this one. */
1892   maybe_save_line ();
1893
1894   /* If the current line has changed, save the changes. */
1895   maybe_replace_line ();
1896
1897   temp = old_temp = (HIST_ENTRY *)NULL;
1898   while (count)
1899     {
1900       temp = previous_history ();
1901       if (temp == 0)
1902         break;
1903
1904       old_temp = temp;
1905       --count;
1906     }
1907
1908   /* If there was a large argument, and we moved back to the start of the
1909      history, that is not an error.  So use the last value found. */
1910   if (!temp && old_temp)
1911     temp = old_temp;
1912
1913   if (temp == 0)
1914     ding ();
1915   else
1916     {
1917       line_len = strlen (temp->line);
1918
1919       if (line_len >= rl_line_buffer_len)
1920         rl_extend_line_buffer (line_len);
1921
1922       strcpy (the_line, temp->line);
1923       rl_undo_list = (UNDO_LIST *)temp->data;
1924       rl_end = rl_point = line_len;
1925
1926 #if defined (VI_MODE)
1927       if (rl_editing_mode == vi_mode)
1928         rl_point = 0;
1929 #endif /* VI_MODE */
1930     }
1931   return 0;
1932 }
1933
1934 /* **************************************************************** */
1935 /*                                                                  */
1936 /*                 The Mark and the Region.                         */
1937 /*                                                                  */
1938 /* **************************************************************** */
1939
1940 /* Set the mark at POSITION. */
1941 int
1942 _rl_set_mark_at_pos (position)
1943      int position;
1944 {
1945   if (position > rl_end)
1946     return -1;
1947
1948   rl_mark = position;
1949   return 0;
1950 }
1951
1952 /* A bindable command to set the mark. */
1953 int
1954 rl_set_mark (count, key)
1955      int count, key;
1956 {
1957   return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
1958 }
1959
1960 /* Exchange the position of mark and point. */
1961 int
1962 rl_exchange_point_and_mark (count, key)
1963      int count, key;
1964 {
1965   if (rl_mark > rl_end)
1966     rl_mark = -1;
1967
1968   if (rl_mark == -1)
1969     {
1970       ding ();
1971       return -1;
1972     }
1973   else
1974     SWAP (rl_point, rl_mark);
1975
1976   return 0;
1977 }
1978
1979 /* **************************************************************** */
1980 /*                                                                  */
1981 /*                          Editing Modes                           */
1982 /*                                                                  */
1983 /* **************************************************************** */
1984 /* How to toggle back and forth between editing modes. */
1985 int
1986 rl_vi_editing_mode (count, key)
1987      int count, key;
1988 {
1989 #if defined (VI_MODE)
1990   rl_editing_mode = vi_mode;
1991   rl_vi_insertion_mode ();
1992 #endif /* VI_MODE */
1993   return 0;
1994 }
1995
1996 int
1997 rl_emacs_editing_mode (count, key)
1998      int count, key;
1999 {
2000   rl_editing_mode = emacs_mode;
2001   _rl_keymap = emacs_standard_keymap;
2002   return 0;
2003 }