1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
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.
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.
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
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
30 #include "posixstat.h"
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
40 #if defined (HAVE_STDLIB_H)
43 # include "ansi_stdlib.h"
44 #endif /* HAVE_STDLIB_H */
46 #if defined (HAVE_LOCALE_H)
54 /* System-specific feature definitions and include files. */
58 # define INCL_DOSPROCESS
62 /* Some standard library routines. */
66 #ifndef RL_LIBRARY_VERSION
67 # define RL_LIBRARY_VERSION "2.1-bash"
70 /* Evaluates its arguments multiple times. */
71 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
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. */
78 /* Variables and functions imported from terminal.c */
79 extern int _rl_init_terminal_io ();
80 extern void _rl_enable_meta_key ();
81 extern int _rl_output_character_function ();
82 extern void _rl_get_screen_size ();
84 extern int _rl_enable_meta
;
85 extern int _rl_term_autowrap
;
86 extern int screenwidth
, screenheight
, screenchars
;
88 /* Variables and functions imported from rltty.c. */
89 extern void rl_prep_terminal (), rl_deprep_terminal ();
90 extern void rltty_set_default_bindings ();
92 /* Functions imported from util.c. */
93 extern void _rl_abort_internal ();
94 extern void rl_extend_line_buffer ();
95 extern int alphabetic ();
97 /* Functions imported from bind.c. */
98 extern void _rl_bind_if_unbound ();
99 extern int rl_set_keymap_from_edit_mode ();
101 /* Functions imported from input.c. */
102 extern int _rl_any_typein ();
103 extern void _rl_insert_typein ();
104 extern int rl_read_key ();
106 /* Functions imported from nls.c */
107 extern int _rl_init_eightbit ();
109 /* Functions imported from shell.c */
110 extern char *get_env_value ();
112 /* External redisplay functions and variables from display.c */
113 extern void _rl_move_vert ();
114 extern void _rl_update_final ();
115 extern void _rl_clear_to_eol ();
116 extern void _rl_clear_screen ();
118 extern void _rl_save_prompt ();
119 extern void _rl_restore_prompt ();
121 extern void _rl_erase_at_end_of_line ();
122 extern void _rl_move_cursor_relative ();
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
;
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
;
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
;
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 ();
152 extern UNDO_LIST
*rl_undo_list
;
153 extern int _rl_doing_an_undo
;
155 /* Forward declarations used in this file. */
156 void _rl_free_history_entry ();
159 int _rl_init_argument ();
161 static char *readline_internal ();
162 static void readline_initialize_everything ();
163 static void start_using_history ();
164 static void bind_arrow_keys ();
166 #if !defined (__GO32__)
167 static void readline_default_bindings ();
168 #endif /* !__GO32__ */
170 #if defined (__GO32__)
173 # undef HANDLE_SIGNALS
174 #endif /* __GO32__ */
176 extern char *xmalloc (), *xrealloc ();
178 /* **************************************************************** */
180 /* Line editing input utility */
182 /* **************************************************************** */
184 char *rl_library_version
= RL_LIBRARY_VERSION
;
186 /* A pointer to the keymap that is currently in use.
187 By default, it is the standard emacs keymap. */
188 Keymap _rl_keymap
= emacs_standard_keymap
;
190 /* The current style of editing. */
191 int rl_editing_mode
= emacs_mode
;
193 /* Non-zero if we called this function from _rl_dispatch(). It's present
194 so functions can find out whether they were called from a key binding
195 or directly from an application. */
198 /* Non-zero if the previous command was a kill command. */
199 int _rl_last_command_was_kill
= 0;
201 /* The current value of the numeric argument specified by the user. */
202 int rl_numeric_arg
= 1;
204 /* Non-zero if an argument was typed. */
205 int rl_explicit_arg
= 0;
207 /* Temporary value used while generating the argument. */
210 /* Non-zero means we have been called at least once before. */
211 static int rl_initialized
;
213 /* If non-zero, this program is running in an EMACS buffer. */
214 static int running_in_emacs
;
216 /* The current offset in the current input line. */
219 /* Mark in the current input line. */
222 /* Length of the current input line. */
225 /* Make this non-zero to return the current input_line. */
228 /* The last function executed by readline. */
229 Function
*rl_last_func
= (Function
*)NULL
;
231 /* Top level environment for readline_internal (). */
232 procenv_t readline_top_level
;
234 /* The streams we interact with. */
235 FILE *_rl_in_stream
, *_rl_out_stream
;
237 /* The names of the streams that we do input and output to. */
238 FILE *rl_instream
= (FILE *)NULL
;
239 FILE *rl_outstream
= (FILE *)NULL
;
241 /* Non-zero means echo characters as they are read. */
242 int readline_echoing_p
= 1;
244 /* Current prompt. */
246 int rl_visible_prompt_length
= 0;
248 /* The number of characters read in order to type this complete command. */
249 int rl_key_sequence_length
= 0;
251 /* If non-zero, then this is the address of a function to call just
252 before readline_internal () prints the first prompt. */
253 Function
*rl_startup_hook
= (Function
*)NULL
;
255 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
256 static char *the_line
;
258 /* The character that can generate an EOF. Really read from
259 the terminal driver... just defaulted here. */
260 int _rl_eof_char
= CTRL ('D');
262 /* Non-zero makes this the next keystroke to read. */
263 int rl_pending_input
= 0;
265 /* Pointer to a useful terminal name. */
266 char *rl_terminal_name
= (char *)NULL
;
268 /* Non-zero means to always use horizontal scrolling in line display. */
269 int _rl_horizontal_scroll_mode
= 0;
271 /* Non-zero means to display an asterisk at the starts of history lines
272 which have been modified. */
273 int _rl_mark_modified_lines
= 0;
275 /* The style of `bell' notification preferred. This can be set to NO_BELL,
276 AUDIBLE_BELL, or VISIBLE_BELL. */
277 int _rl_bell_preference
= AUDIBLE_BELL
;
279 /* String inserted into the line by rl_insert_comment (). */
280 char *_rl_comment_begin
;
282 /* Keymap holding the function currently being executed. */
283 Keymap rl_executing_keymap
;
285 /* Line buffer and maintenence. */
286 char *rl_line_buffer
= (char *)NULL
;
287 int rl_line_buffer_len
= 0;
289 /* Forward declarations used by the display and termcap code. */
291 /* **************************************************************** */
293 /* `Forward' declarations */
295 /* **************************************************************** */
297 /* Non-zero means do not parse any lines other than comments and
298 parser directives. */
299 unsigned char _rl_parsing_conditionalized_out
= 0;
301 /* Non-zero means to convert characters with the meta bit set to
302 escape-prefixed characters so we can indirect through
303 emacs_meta_keymap or vi_escape_keymap. */
304 int _rl_convert_meta_chars_to_ascii
= 1;
306 /* Non-zero means to output characters with the meta bit set directly
307 rather than as a meta-prefixed escape sequence. */
308 int _rl_output_meta_chars
= 0;
310 /* **************************************************************** */
312 /* Top Level Functions */
314 /* **************************************************************** */
316 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
317 int _rl_meta_flag
= 0; /* Forward declaration */
319 /* Read a line of input. Prompt with PROMPT. An empty PROMPT means
320 none. A return value of NULL means that EOF was encountered. */
329 /* If we are at EOF return a NULL string. */
330 if (rl_pending_input
== EOF
)
332 rl_pending_input
= 0;
333 return ((char *)NULL
);
336 rl_visible_prompt_length
= rl_expand_prompt (rl_prompt
);
339 (*rl_prep_term_function
) (_rl_meta_flag
);
341 #if defined (HANDLE_SIGNALS)
345 value
= readline_internal ();
346 (*rl_deprep_term_function
) ();
348 #if defined (HANDLE_SIGNALS)
355 #if defined (READLINE_CALLBACKS)
356 # define STATIC_CALLBACK
358 # define STATIC_CALLBACK static
362 readline_internal_setup ()
364 _rl_in_stream
= rl_instream
;
365 _rl_out_stream
= rl_outstream
;
368 (*rl_startup_hook
) ();
370 if (readline_echoing_p
== 0)
374 fprintf (_rl_out_stream
, "%s", rl_prompt
);
375 fflush (_rl_out_stream
);
381 (*rl_redisplay_function
) ();
382 #if defined (VI_MODE)
383 if (rl_editing_mode
== vi_mode
)
384 rl_vi_insertion_mode (1, 0);
389 STATIC_CALLBACK
char *
390 readline_internal_teardown (eof
)
396 /* Restore the original of this history line, iff the line that we
397 are editing was originally in the history, AND the line has changed. */
398 entry
= current_history ();
400 if (entry
&& rl_undo_list
)
402 temp
= savestring (the_line
);
403 rl_revert_line (1, 0);
404 entry
= replace_history_entry (where_history (), the_line
, (HIST_ENTRY
*)NULL
);
405 _rl_free_history_entry (entry
);
407 strcpy (the_line
, temp
);
411 /* At any rate, it is highly likely that this line has an undo list. Get
416 return (eof
? (char *)NULL
: savestring (the_line
));
420 #if defined (READLINE_CALLBACKS)
421 readline_internal_char ()
423 readline_internal_charloop ()
426 static int lastc
, eof_found
;
432 #if !defined (READLINE_CALLBACKS)
436 lk
= _rl_last_command_was_kill
;
438 code
= setjmp (readline_top_level
);
441 (*rl_redisplay_function
) ();
443 if (rl_pending_input
== 0)
445 /* Then initialize the argument and number of keys read. */
446 _rl_init_argument ();
447 rl_key_sequence_length
= 0;
452 /* EOF typed to a non-blank line is a <NL>. */
453 if (c
== EOF
&& rl_end
)
456 /* The character _rl_eof_char typed to blank line, and not as the
457 previous character is interpreted as EOF. */
458 if (((c
== _rl_eof_char
&& lastc
!= c
) || c
== EOF
) && !rl_end
)
460 #if defined (READLINE_CALLBACKS)
461 return (rl_done
= 1);
469 _rl_dispatch (c
, _rl_keymap
);
471 /* If there was no change in _rl_last_command_was_kill, then no kill
472 has taken place. Note that if input is pending we are reading
473 a prefix command, so nothing has changed yet. */
474 if (rl_pending_input
== 0 && lk
== _rl_last_command_was_kill
)
475 _rl_last_command_was_kill
= 0;
477 #if defined (VI_MODE)
478 /* In vi mode, when you exit insert mode, the cursor moves back
479 over the previous character. We explicitly check for that here. */
480 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
)
485 (*rl_redisplay_function
) ();
487 #if defined (READLINE_CALLBACKS)
496 #if defined (READLINE_CALLBACKS)
498 readline_internal_charloop ()
503 eof
= readline_internal_char ();
506 #endif /* READLINE_CALLBACKS */
508 /* Read a line of input from the global rl_instream, doing output on
509 the global rl_outstream.
510 If rl_prompt is non-null, then that is our prompt. */
516 readline_internal_setup ();
517 eof
= readline_internal_charloop ();
518 return (readline_internal_teardown (eof
));
522 _rl_init_line_state ()
524 rl_point
= rl_end
= 0;
525 the_line
= rl_line_buffer
;
532 the_line
= rl_line_buffer
;
535 /* Do the command associated with KEY in MAP.
536 If the associated command is really a keymap, then read
537 another key, and dispatch into that map. */
539 _rl_dispatch (key
, map
)
547 if (META_CHAR (key
) && _rl_convert_meta_chars_to_ascii
)
549 if (map
[ESC
].type
== ISKMAP
)
551 if (_rl_defining_kbd_macro
)
552 _rl_add_macro_char (ESC
);
553 map
= FUNCTION_TO_KEYMAP (map
, ESC
);
555 rl_key_sequence_length
+= 2;
556 return (_rl_dispatch (key
, map
));
563 if (_rl_defining_kbd_macro
)
564 _rl_add_macro_char (key
);
567 switch (map
[key
].type
)
570 func
= map
[key
].function
;
571 if (func
!= (Function
*)NULL
)
573 /* Special case rl_do_lowercase_version (). */
574 if (func
== rl_do_lowercase_version
)
575 return (_rl_dispatch (_rl_to_lower (key
), map
));
577 rl_executing_keymap
= map
;
580 _rl_suppress_redisplay
= (map
[key
].function
== rl_insert
) && _rl_input_available ();
584 r
= (*map
[key
].function
)(rl_numeric_arg
* rl_arg_sign
, key
);
587 /* If we have input pending, then the last command was a prefix
588 command. Don't change the state of rl_last_func. Otherwise,
589 remember the last command executed in this variable. */
590 if (!rl_pending_input
&& map
[key
].function
!= rl_digit_argument
)
591 rl_last_func
= map
[key
].function
;
595 _rl_abort_internal ();
601 if (map
[key
].function
!= (Function
*)NULL
)
603 rl_key_sequence_length
++;
604 newkey
= rl_read_key ();
605 r
= _rl_dispatch (newkey
, FUNCTION_TO_KEYMAP (map
, key
));
609 _rl_abort_internal ();
615 if (map
[key
].function
!= (Function
*)NULL
)
617 macro
= savestring ((char *)map
[key
].function
);
618 _rl_with_macro_input (macro
);
623 #if defined (VI_MODE)
624 if (rl_editing_mode
== vi_mode
&& _rl_keymap
== vi_movement_keymap
&&
625 _rl_vi_textmod_command (key
))
626 _rl_vi_set_last (key
, rl_numeric_arg
, rl_arg_sign
);
631 /* **************************************************************** */
633 /* Initializations */
635 /* **************************************************************** */
637 /* Initialize readline (and terminal if not already). */
641 /* If we have never been called before, initialize the
642 terminal and data structures. */
645 readline_initialize_everything ();
649 /* Initalize the current line information. */
650 _rl_init_line_state ();
652 /* We aren't done yet. We haven't even gotten started yet! */
655 /* Tell the history routines what is going on. */
656 start_using_history ();
658 /* Make the display buffer match the state of the line. */
659 rl_reset_line_state ();
661 /* No such function typed yet. */
662 rl_last_func
= (Function
*)NULL
;
664 /* Parsing of key-bindings begins in an enabled state. */
665 _rl_parsing_conditionalized_out
= 0;
667 #if defined (VI_MODE)
668 if (rl_editing_mode
== vi_mode
)
669 _rl_vi_initialize_line ();
675 #if defined (__EMX__)
677 _emx_build_environ ()
684 DosGetInfoBlocks (&tibp
, &pibp
);
685 t
= pibp
->pib_pchenv
;
688 tp
= environ
= (char **)xmalloc ((c
+ 1) * sizeof (char *));
689 t
= pibp
->pib_pchenv
;
699 /* Initialize the entire state of the world. */
701 readline_initialize_everything ()
703 #if defined (__EMX__)
705 _emx_build_environ ();
708 /* Find out if we are running in Emacs. */
709 running_in_emacs
= get_env_value ("EMACS") != (char *)0;
711 /* Set up input and output if they are not already set up. */
716 rl_outstream
= stdout
;
718 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
719 may change, but they may also be used before readline_internal ()
721 _rl_in_stream
= rl_instream
;
722 _rl_out_stream
= rl_outstream
;
724 /* Allocate data structures. */
725 if (rl_line_buffer
== 0)
726 rl_line_buffer
= xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
728 /* Initialize the terminal interface. */
729 _rl_init_terminal_io ((char *)NULL
);
731 #if !defined (__GO32__)
732 /* Bind tty characters to readline functions. */
733 readline_default_bindings ();
734 #endif /* !__GO32__ */
736 /* Initialize the function names. */
737 rl_initialize_funmap ();
739 /* Decide whether we should automatically go into eight-bit mode. */
740 _rl_init_eightbit ();
742 /* Read in the init file. */
743 rl_read_init_file ((char *)NULL
);
746 if (_rl_horizontal_scroll_mode
&& _rl_term_autowrap
)
749 screenchars
-= screenheight
;
752 /* Override the effect of any `set keymap' assignments in the
754 rl_set_keymap_from_edit_mode ();
756 /* Try to bind a common arrow key prefix, if not already bound. */
759 /* Enable the meta key, if this terminal has one. */
761 _rl_enable_meta_key ();
763 /* If the completion parser's default word break characters haven't
764 been set yet, then do so now. */
765 if (rl_completer_word_break_characters
== (char *)NULL
)
766 rl_completer_word_break_characters
= rl_basic_word_break_characters
;
769 /* If this system allows us to look at the values of the regular
770 input editing characters, then bind them to their readline
771 equivalents, iff the characters are not bound to keymaps. */
773 readline_default_bindings ()
775 rltty_set_default_bindings (_rl_keymap
);
779 bind_arrow_keys_internal ()
783 f
= rl_function_of_keyseq ("\033[A", _rl_keymap
, (int *)NULL
);
784 if (!f
|| f
== rl_do_lowercase_version
)
786 _rl_bind_if_unbound ("\033[A", rl_get_previous_history
);
787 _rl_bind_if_unbound ("\033[B", rl_get_next_history
);
788 _rl_bind_if_unbound ("\033[C", rl_forward
);
789 _rl_bind_if_unbound ("\033[D", rl_backward
);
792 f
= rl_function_of_keyseq ("\033OA", _rl_keymap
, (int *)NULL
);
793 if (!f
|| f
== rl_do_lowercase_version
)
795 _rl_bind_if_unbound ("\033OA", rl_get_previous_history
);
796 _rl_bind_if_unbound ("\033OB", rl_get_next_history
);
797 _rl_bind_if_unbound ("\033OC", rl_forward
);
798 _rl_bind_if_unbound ("\033OD", rl_backward
);
802 /* Try and bind the common arrow key prefix after giving termcap and
803 the inputrc file a chance to bind them and create `real' keymaps
804 for the arrow key prefix. */
810 xkeymap
= _rl_keymap
;
812 _rl_keymap
= emacs_standard_keymap
;
813 bind_arrow_keys_internal ();
815 #if defined (VI_MODE)
816 _rl_keymap
= vi_movement_keymap
;
817 bind_arrow_keys_internal ();
820 _rl_keymap
= xkeymap
;
824 /* **************************************************************** */
826 /* Numeric Arguments */
828 /* **************************************************************** */
830 /* Handle C-u style numeric args, as well as M--, and M-digits. */
834 int key
, c
, sawminus
, sawdigits
;
838 sawminus
= sawdigits
= 0;
841 rl_message ("(arg: %d) ", rl_arg_sign
* rl_numeric_arg
);
842 key
= c
= rl_read_key ();
844 /* If we see a key bound to `universal-argument' after seeing digits,
845 it ends the argument but is otherwise ignored. */
846 if (_rl_keymap
[c
].type
== ISFUNC
&&
847 _rl_keymap
[c
].function
== rl_universal_argument
)
856 key
= rl_read_key ();
857 _rl_restore_prompt ();
859 return (_rl_dispatch (key
, _rl_keymap
));
867 rl_numeric_arg
= rl_explicit_arg
? (rl_numeric_arg
* 10) + c
- '0' : c
- '0';
868 sawdigits
= rl_explicit_arg
= 1;
870 else if (c
== '-' && rl_explicit_arg
== 0)
872 rl_numeric_arg
= sawminus
= 1;
877 /* Make M-- command equivalent to M--1 command. */
878 if (sawminus
&& rl_numeric_arg
== 1 && rl_explicit_arg
== 0)
880 _rl_restore_prompt ();
882 return (_rl_dispatch (key
, _rl_keymap
));
889 /* Add the current digit to the argument in progress. */
891 rl_digit_argument (ignore
, key
)
894 rl_pending_input
= key
;
895 return (rl_digit_loop ());
898 /* What to do when you abort reading an argument. */
900 rl_discard_argument ()
904 _rl_init_argument ();
908 /* Create a default argument. */
912 rl_numeric_arg
= rl_arg_sign
= 1;
917 /* C-u, universal argument. Multiply the current argument by 4.
918 Read a key. If the key has nothing to do with arguments, then
919 dispatch on it. If the key is the abort character then abort. */
921 rl_universal_argument (count
, key
)
925 return (rl_digit_loop ());
928 /* **************************************************************** */
930 /* Insert and Delete */
932 /* **************************************************************** */
934 /* Insert a string of text into the line at point. This is the only
935 way that you should do insertion. rl_insert () calls this
938 rl_insert_text (string
)
941 register int i
, l
= strlen (string
);
943 if (rl_end
+ l
>= rl_line_buffer_len
)
944 rl_extend_line_buffer (rl_end
+ l
);
946 for (i
= rl_end
; i
>= rl_point
; i
--)
947 the_line
[i
+ l
] = the_line
[i
];
948 strncpy (the_line
+ rl_point
, string
, l
);
950 /* Remember how to undo this if we aren't undoing something. */
951 if (!_rl_doing_an_undo
)
953 /* If possible and desirable, concatenate the undos. */
956 (rl_undo_list
->what
== UNDO_INSERT
) &&
957 (rl_undo_list
->end
== rl_point
) &&
958 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
961 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
965 the_line
[rl_end
] = '\0';
969 /* Delete the string between FROM and TO. FROM is
970 inclusive, TO is not. */
972 rl_delete_text (from
, to
)
976 register int diff
, i
;
978 /* Fix it if the caller is confused. */
990 text
= rl_copy_text (from
, to
);
992 /* Some versions of strncpy() can't handle overlapping arguments. */
994 for (i
= from
; i
< rl_end
- diff
; i
++)
995 the_line
[i
] = the_line
[i
+ diff
];
997 /* Remember how to undo this delete. */
998 if (_rl_doing_an_undo
== 0)
999 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
1004 the_line
[rl_end
] = '\0';
1008 /* Fix up point so that it is within the line boundaries after killing
1009 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1012 #define _RL_FIX_POINT(x) \
1021 _rl_fix_point (fix_mark_too
)
1024 _RL_FIX_POINT (rl_point
);
1026 _RL_FIX_POINT (rl_mark
);
1028 #undef _RL_FIX_POINT
1030 /* **************************************************************** */
1032 /* Readline character functions */
1034 /* **************************************************************** */
1036 /* This is not a gap editor, just a stupid line input routine. No hair
1037 is involved in writing any of the functions, and none should be. */
1041 rl_end is the place in the string that we would place '\0';
1042 i.e., it is always safe to place '\0' there.
1044 rl_point is the place in the string where the cursor is. Sometimes
1045 this is the same as rl_end.
1047 Any command that is called interactively receives two arguments.
1048 The first is a count: the numeric arg pased to this command.
1049 The second is the key which invoked this command.
1052 /* **************************************************************** */
1054 /* Movement Commands */
1056 /* **************************************************************** */
1058 /* Note that if you `optimize' the display for these functions, you cannot
1059 use said functions in other functions which do not do optimizing display.
1060 I.e., you will have to update the data base for rl_redisplay, and you
1061 might as well let rl_redisplay do that job. */
1063 /* Move forward COUNT characters. */
1065 rl_forward (count
, key
)
1069 rl_backward (-count
, key
);
1072 int end
= rl_point
+ count
;
1073 #if defined (VI_MODE)
1074 int lend
= rl_end
- (rl_editing_mode
== vi_mode
);
1090 /* Move backward COUNT characters. */
1092 rl_backward (count
, key
)
1096 rl_forward (-count
, key
);
1099 if (rl_point
< count
)
1110 /* Move to the beginning of the line. */
1112 rl_beg_of_line (count
, key
)
1119 /* Move to the end of the line. */
1121 rl_end_of_line (count
, key
)
1128 /* Move forward a word. We do what Emacs does. */
1130 rl_forward_word (count
, key
)
1137 rl_backward_word (-count
, key
);
1143 if (rl_point
== rl_end
)
1146 /* If we are not in a word, move forward until we are in one.
1147 Then, move forward until we hit a non-alphabetic character. */
1148 c
= the_line
[rl_point
];
1149 if (alphabetic (c
) == 0)
1151 while (++rl_point
< rl_end
)
1153 c
= the_line
[rl_point
];
1158 if (rl_point
== rl_end
)
1160 while (++rl_point
< rl_end
)
1162 c
= the_line
[rl_point
];
1163 if (alphabetic (c
) == 0)
1171 /* Move backward a word. We do what Emacs does. */
1173 rl_backward_word (count
, key
)
1180 rl_forward_word (-count
, key
);
1189 /* Like rl_forward_word (), except that we look at the characters
1190 just before point. */
1192 c
= the_line
[rl_point
- 1];
1193 if (alphabetic (c
) == 0)
1197 c
= the_line
[rl_point
- 1];
1205 c
= the_line
[rl_point
- 1];
1206 if (alphabetic (c
) == 0)
1216 /* Clear the current line. Numeric argument to C-l does this. */
1220 int curr_line
, nleft
;
1222 /* Find out whether or not there might be invisible characters in the
1224 if (rl_display_prompt
== rl_prompt
)
1225 nleft
= _rl_last_c_pos
- screenwidth
- rl_visible_prompt_length
;
1227 nleft
= _rl_last_c_pos
- screenwidth
;
1230 curr_line
= 1 + nleft
/ screenwidth
;
1234 _rl_move_vert (curr_line
);
1235 _rl_move_cursor_relative (0, the_line
); /* XXX is this right */
1237 #if defined (__GO32__)
1239 int row
, col
, width
, row_start
;
1241 ScreenGetCursor (&row
, &col
);
1242 width
= ScreenCols ();
1243 row_start
= ScreenPrimary
+ (row
* width
);
1244 memset (row_start
+ col
, 0, (width
- col
) * 2);
1246 #else /* !__GO32__ */
1247 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1248 #endif /* !__GO32__ */
1250 rl_forced_update_display ();
1251 rl_display_fixed
= 1;
1256 /* C-l typed to a line without quoting clears the screen, and then reprints
1257 the prompt and the current input line. Given a numeric arg, redraw only
1258 the current line. */
1260 rl_clear_screen (count
, key
)
1263 if (rl_explicit_arg
)
1269 _rl_clear_screen (); /* calls termcap function to clear screen */
1270 rl_forced_update_display ();
1271 rl_display_fixed
= 1;
1277 rl_arrow_keys (count
, c
)
1282 ch
= rl_read_key ();
1284 switch (_rl_to_upper (ch
))
1287 rl_get_previous_history (count
, ch
);
1291 rl_get_next_history (count
, ch
);
1295 rl_forward (count
, ch
);
1299 rl_backward (count
, ch
);
1309 /* **************************************************************** */
1313 /* **************************************************************** */
1315 /* Insert the character C at the current location, moving point forward. */
1317 rl_insert (count
, c
)
1326 /* If we can optimize, then do it. But don't let people crash
1327 readline because of extra large arguments. */
1328 if (count
> 1 && count
<= 1024)
1330 string
= xmalloc (1 + count
);
1332 for (i
= 0; i
< count
; i
++)
1336 rl_insert_text (string
);
1347 for (i
= 0; i
< 1024; i
++)
1352 decreaser
= (count
> 1024 ? 1024 : count
);
1353 str
[decreaser
] = '\0';
1354 rl_insert_text (str
);
1361 /* We are inserting a single character.
1362 If there is pending input, then make a string of all of the
1363 pending characters that are bound to rl_insert, and insert
1365 if (_rl_any_typein ())
1366 _rl_insert_typein (c
);
1369 /* Inserting a single character. */
1374 rl_insert_text (str
);
1379 /* Insert the next typed character verbatim. */
1381 rl_quoted_insert (count
, key
)
1387 return (rl_insert (count
, c
));
1390 /* Insert a tab character. */
1392 rl_tab_insert (count
, key
)
1395 return (rl_insert (count
, '\t'));
1398 /* What to do when a NEWLINE is pressed. We accept the whole line.
1399 KEY is the key that invoked this command. I guess it could have
1400 meaning in the future. */
1402 rl_newline (count
, key
)
1407 #if defined (VI_MODE)
1408 if (rl_editing_mode
== vi_mode
)
1410 _rl_vi_done_inserting ();
1411 _rl_vi_reset_last ();
1413 #endif /* VI_MODE */
1415 if (readline_echoing_p
)
1416 _rl_update_final ();
1420 /* What to do for some uppercase characters, like meta characters,
1421 and some characters appearing in emacs_ctlx_keymap. This function
1422 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1423 is special cased. */
1425 rl_do_lowercase_version (ignore1
, ignore2
)
1426 int ignore1
, ignore2
;
1431 /* Rubout the character behind point. */
1433 rl_rubout (count
, key
)
1438 rl_delete (-count
, key
);
1448 if (count
> 1 || rl_explicit_arg
)
1450 int orig_point
= rl_point
;
1451 rl_backward (count
, key
);
1452 rl_kill_text (orig_point
, rl_point
);
1456 int c
= the_line
[--rl_point
];
1457 rl_delete_text (rl_point
, rl_point
+ 1);
1459 if (rl_point
== rl_end
&& isprint (c
) && _rl_last_c_pos
)
1462 l
= rl_character_len (c
, rl_point
);
1463 _rl_erase_at_end_of_line (l
);
1469 /* Delete the character under the cursor. Given a numeric argument,
1470 kill that many characters instead. */
1472 rl_delete (count
, key
)
1476 return (rl_rubout (-count
, key
));
1478 if (rl_point
== rl_end
)
1484 if (count
> 1 || rl_explicit_arg
)
1486 int orig_point
= rl_point
;
1487 rl_forward (count
, key
);
1488 rl_kill_text (orig_point
, rl_point
);
1489 rl_point
= orig_point
;
1493 return (rl_delete_text (rl_point
, rl_point
+ 1));
1497 /* Delete all spaces and tabs around point. */
1499 rl_delete_horizontal_space (count
, ignore
)
1502 int start
= rl_point
;
1504 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
1509 while (rl_point
< rl_end
&& whitespace (the_line
[rl_point
]))
1512 if (start
!= rl_point
)
1514 rl_delete_text (start
, rl_point
);
1520 #ifndef RL_COMMENT_BEGIN_DEFAULT
1521 #define RL_COMMENT_BEGIN_DEFAULT "#"
1524 /* Turn the current line into a comment in shell history.
1525 A K*rn shell style function. */
1527 rl_insert_comment (count
, key
)
1530 rl_beg_of_line (1, key
);
1531 rl_insert_text (_rl_comment_begin
? _rl_comment_begin
1532 : RL_COMMENT_BEGIN_DEFAULT
);
1533 (*rl_redisplay_function
) ();
1534 rl_newline (1, '\n');
1538 /* **************************************************************** */
1542 /* **************************************************************** */
1544 /* The three kinds of things that we know how to do. */
1549 static int rl_change_case ();
1551 /* Uppercase the word at point. */
1553 rl_upcase_word (count
, key
)
1556 return (rl_change_case (count
, UpCase
));
1559 /* Lowercase the word at point. */
1561 rl_downcase_word (count
, key
)
1564 return (rl_change_case (count
, DownCase
));
1567 /* Upcase the first letter, downcase the rest. */
1569 rl_capitalize_word (count
, key
)
1572 return (rl_change_case (count
, CapCase
));
1575 /* The meaty function.
1576 Change the case of COUNT words, performing OP on them.
1577 OP is one of UpCase, DownCase, or CapCase.
1578 If a negative argument is given, leave point where it started,
1579 otherwise, leave it where it moves to. */
1581 rl_change_case (count
, op
)
1584 register int start
, end
;
1588 rl_forward_word (count
, 0);
1594 /* We are going to modify some text, so let's prepare to undo it. */
1595 rl_modifying (start
, end
);
1597 for (inword
= 0; start
< end
; start
++)
1599 c
= the_line
[start
];
1603 the_line
[start
] = _rl_to_upper (c
);
1607 the_line
[start
] = _rl_to_lower (c
);
1611 the_line
[start
] = (inword
== 0) ? _rl_to_upper (c
) : _rl_to_lower (c
);
1612 inword
= alphabetic (the_line
[start
]);
1624 /* **************************************************************** */
1628 /* **************************************************************** */
1630 /* Transpose the words at point. */
1632 rl_transpose_words (count
, key
)
1635 char *word1
, *word2
;
1636 int w1_beg
, w1_end
, w2_beg
, w2_end
;
1637 int orig_point
= rl_point
;
1642 /* Find the two words. */
1643 rl_forward_word (count
, key
);
1645 rl_backward_word (1, key
);
1647 rl_backward_word (count
, key
);
1649 rl_forward_word (1, key
);
1652 /* Do some check to make sure that there really are two words. */
1653 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
1656 rl_point
= orig_point
;
1660 /* Get the text of the words. */
1661 word1
= rl_copy_text (w1_beg
, w1_end
);
1662 word2
= rl_copy_text (w2_beg
, w2_end
);
1664 /* We are about to do many insertions and deletions. Remember them
1665 as one operation. */
1666 rl_begin_undo_group ();
1668 /* Do the stuff at word2 first, so that we don't have to worry
1669 about word1 moving. */
1671 rl_delete_text (w2_beg
, w2_end
);
1672 rl_insert_text (word1
);
1675 rl_delete_text (w1_beg
, w1_end
);
1676 rl_insert_text (word2
);
1678 /* This is exactly correct since the text before this point has not
1679 changed in length. */
1682 /* I think that does it. */
1683 rl_end_undo_group ();
1690 /* Transpose the characters at point. If point is at the end of the line,
1691 then transpose the characters before point. */
1693 rl_transpose_chars (count
, key
)
1701 if (!rl_point
|| rl_end
< 2)
1707 rl_begin_undo_group ();
1709 if (rl_point
== rl_end
)
1716 dummy
[0] = the_line
[rl_point
];
1719 rl_delete_text (rl_point
, rl_point
+ 1);
1723 rl_insert_text (dummy
);
1725 rl_end_undo_group ();
1729 /* **************************************************************** */
1731 /* Character Searching */
1733 /* **************************************************************** */
1736 _rl_char_search_internal (count
, dir
, schar
)
1737 int count
, dir
, schar
;
1742 inc
= (dir
< 0) ? -1 : 1;
1745 if ((dir
< 0 && pos
<= 0) || (dir
> 0 && pos
>= rl_end
))
1754 if (rl_line_buffer
[pos
] == schar
)
1758 rl_point
= (dir
== BTO
) ? pos
+ 1 : pos
;
1760 rl_point
= (dir
== FTO
) ? pos
- 1 : pos
;
1764 while ((dir
< 0) ? pos
-- : ++pos
< rl_end
);
1769 /* Search COUNT times for a character read from the current input stream.
1770 FDIR is the direction to search if COUNT is non-negative; otherwise
1771 the search goes in BDIR. */
1773 _rl_char_search (count
, fdir
, bdir
)
1774 int count
, fdir
, bdir
;
1780 return (_rl_char_search_internal (-count
, bdir
, c
));
1782 return (_rl_char_search_internal (count
, fdir
, c
));
1786 rl_char_search (count
, key
)
1789 return (_rl_char_search (count
, FFIND
, BFIND
));
1793 rl_backward_char_search (count
, key
)
1796 return (_rl_char_search (count
, BFIND
, FFIND
));
1799 /* **************************************************************** */
1801 /* History Utilities */
1803 /* **************************************************************** */
1805 /* We already have a history library, and that is what we use to control
1806 the history features of readline. This is our local interface to
1807 the history mechanism. */
1809 /* While we are editing the history, this is the saved
1810 version of the original line. */
1811 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1813 /* Set the history pointer back to the last entry in the history. */
1815 start_using_history ()
1818 if (saved_line_for_history
)
1819 _rl_free_history_entry (saved_line_for_history
);
1821 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1824 /* Free the contents (and containing structure) of a HIST_ENTRY. */
1826 _rl_free_history_entry (entry
)
1836 /* Perhaps put back the current line if it has changed. */
1838 maybe_replace_line ()
1842 temp
= current_history ();
1843 /* If the current line has changed, save the changes. */
1844 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
))
1846 temp
= replace_history_entry (where_history (), the_line
, rl_undo_list
);
1853 /* Put back the saved_line_for_history if there is one. */
1855 maybe_unsave_line ()
1859 if (saved_line_for_history
)
1861 line_len
= strlen (saved_line_for_history
->line
);
1863 if (line_len
>= rl_line_buffer_len
)
1864 rl_extend_line_buffer (line_len
);
1866 strcpy (the_line
, saved_line_for_history
->line
);
1867 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
1868 _rl_free_history_entry (saved_line_for_history
);
1869 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
1870 rl_end
= rl_point
= strlen (the_line
);
1877 /* Save the current line in saved_line_for_history. */
1881 if (saved_line_for_history
== 0)
1883 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
1884 saved_line_for_history
->line
= savestring (the_line
);
1885 saved_line_for_history
->data
= (char *)rl_undo_list
;
1890 /* **************************************************************** */
1892 /* History Commands */
1894 /* **************************************************************** */
1896 /* Meta-< goes to the start of the history. */
1898 rl_beginning_of_history (count
, key
)
1901 return (rl_get_previous_history (1 + where_history (), key
));
1904 /* Meta-> goes to the end of the history. (The current line). */
1906 rl_end_of_history (count
, key
)
1909 maybe_replace_line ();
1911 maybe_unsave_line ();
1915 /* Move down to the next history line. */
1917 rl_get_next_history (count
, key
)
1924 return (rl_get_previous_history (-count
, key
));
1929 maybe_replace_line ();
1931 temp
= (HIST_ENTRY
*)NULL
;
1934 temp
= next_history ();
1941 maybe_unsave_line ();
1944 line_len
= strlen (temp
->line
);
1946 if (line_len
>= rl_line_buffer_len
)
1947 rl_extend_line_buffer (line_len
);
1949 strcpy (the_line
, temp
->line
);
1950 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
1951 rl_end
= rl_point
= strlen (the_line
);
1952 #if defined (VI_MODE)
1953 if (rl_editing_mode
== vi_mode
)
1955 #endif /* VI_MODE */
1960 /* Get the previous item out of our interactive history, making it the current
1961 line. If there is no previous history, just ding. */
1963 rl_get_previous_history (count
, key
)
1966 HIST_ENTRY
*old_temp
, *temp
;
1970 return (rl_get_next_history (-count
, key
));
1975 /* If we don't have a line saved, then save this one. */
1978 /* If the current line has changed, save the changes. */
1979 maybe_replace_line ();
1981 temp
= old_temp
= (HIST_ENTRY
*)NULL
;
1984 temp
= previous_history ();
1992 /* If there was a large argument, and we moved back to the start of the
1993 history, that is not an error. So use the last value found. */
1994 if (!temp
&& old_temp
)
2001 line_len
= strlen (temp
->line
);
2003 if (line_len
>= rl_line_buffer_len
)
2004 rl_extend_line_buffer (line_len
);
2006 strcpy (the_line
, temp
->line
);
2007 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
2008 rl_end
= rl_point
= line_len
;
2010 #if defined (VI_MODE)
2011 if (rl_editing_mode
== vi_mode
)
2013 #endif /* VI_MODE */
2018 /* **************************************************************** */
2020 /* The Mark and the Region. */
2022 /* **************************************************************** */
2024 /* Set the mark at POSITION. */
2026 _rl_set_mark_at_pos (position
)
2029 if (position
> rl_end
)
2036 /* A bindable command to set the mark. */
2038 rl_set_mark (count
, key
)
2041 return (_rl_set_mark_at_pos (rl_explicit_arg
? count
: rl_point
));
2044 /* Exchange the position of mark and point. */
2046 rl_exchange_point_and_mark (count
, key
)
2049 if (rl_mark
> rl_end
)
2058 SWAP (rl_point
, rl_mark
);
2063 /* **************************************************************** */
2067 /* **************************************************************** */
2068 /* How to toggle back and forth between editing modes. */
2070 rl_vi_editing_mode (count
, key
)
2073 #if defined (VI_MODE)
2074 rl_editing_mode
= vi_mode
;
2075 rl_vi_insertion_mode (1, key
);
2076 #endif /* VI_MODE */
2081 rl_emacs_editing_mode (count
, key
)
2084 rl_editing_mode
= emacs_mode
;
2085 _rl_keymap
= emacs_standard_keymap
;