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