OSDN Git Service

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