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