* test case for PR 18230, over from d30v branch
[binutils-gdb.git] / readline / readline.c
blob3d28df82360b89e65ad8950ef513a7ce4a5dfab2
1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright 1987, 1989, 1991, 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. */
24 #include "sysdep.h"
25 #include <stdio.h>
26 #include <fcntl.h>
27 #ifndef NO_SYS_FILE
28 #include <sys/file.h>
29 #endif
30 #include <signal.h>
32 /* This is needed to include support for TIOCGWINSZ and window resizing. */
33 #if defined (OSF1) || defined (BSD386) || defined (_386BSD) || defined (AIX)
34 # include <sys/ioctl.h>
35 #endif /* OSF1 */
37 #if defined (HAVE_UNISTD_H)
38 # include <unistd.h>
39 #endif
41 #include <errno.h>
42 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
43 #if !defined (errno)
44 extern int errno;
45 #endif /* !errno */
47 extern char * getenv ();
49 #include <setjmp.h>
50 #include <sys/stat.h>
52 /* System-specific feature definitions and include files. */
53 #include "rldefs.h"
55 /* Some standard library routines. */
56 #include "readline.h"
57 #include "history.h"
59 /* NOTE: Functions and variables prefixed with `_rl_' are
60 pseudo-global: they are global so they can be shared
61 between files in the readline library, but are not intended
62 to be visible to readline callers. */
64 /* Functions imported from other files in the library. */
65 extern char *tgetstr ();
66 extern void rl_prep_terminal (), rl_deprep_terminal ();
67 extern void rl_vi_set_last ();
68 extern Function *rl_function_of_keyseq ();
69 extern char *tilde_expand ();
71 /* External redisplay functions and variables from display.c */
72 extern void rl_redisplay ();
73 extern void _rl_move_vert ();
75 extern void _rl_erase_at_end_of_line ();
76 extern void _rl_move_cursor_relative ();
78 extern int _rl_vis_botlin;
79 extern int _rl_last_c_pos;
80 extern int rl_display_fixed;
82 /* Variables imported from complete.c. */
83 extern char *rl_completer_word_break_characters;
84 extern char *rl_basic_word_break_characters;
85 extern Function *rl_symbolic_link_hook;
86 extern int rl_completion_query_items;
87 extern int rl_complete_with_tilde_expansion;
89 /* Forward declarations used in this file. */
90 void rl_dispatch ();
91 void free_history_entry ();
92 int _rl_output_character_function ();
93 void _rl_set_screen_size ();
94 void free_undo_list (), rl_add_undo ();
96 #if !defined (__GO32__)
97 static void readline_default_bindings ();
98 #endif /* !__GO32__ */
100 #if defined (__GO32__)
101 # include <sys/pc.h>
102 # undef HANDLE_SIGNALS
103 #endif /* __GO32__ */
105 #if defined (STATIC_MALLOC)
106 static char *xmalloc (), *xrealloc ();
107 #else
108 extern char *xmalloc (), *xrealloc ();
109 #endif /* STATIC_MALLOC */
112 /* **************************************************************** */
113 /* */
114 /* Line editing input utility */
115 /* */
116 /* **************************************************************** */
118 static char *LibraryVersion = "2.0 (Cygnus)";
120 /* A pointer to the keymap that is currently in use.
121 By default, it is the standard emacs keymap. */
122 Keymap _rl_keymap = emacs_standard_keymap;
124 /* The current style of editing. */
125 int rl_editing_mode = emacs_mode;
127 /* Non-zero if the previous command was a kill command. */
128 static int last_command_was_kill = 0;
130 /* The current value of the numeric argument specified by the user. */
131 int rl_numeric_arg = 1;
133 /* Non-zero if an argument was typed. */
134 int rl_explicit_arg = 0;
136 /* Temporary value used while generating the argument. */
137 int rl_arg_sign = 1;
139 /* Non-zero means we have been called at least once before. */
140 static int rl_initialized = 0;
142 /* If non-zero, this program is running in an EMACS buffer. */
143 static char *running_in_emacs = (char *)NULL;
145 /* The current offset in the current input line. */
146 int rl_point;
148 /* Mark in the current input line. */
149 int rl_mark;
151 /* Length of the current input line. */
152 int rl_end;
154 /* Make this non-zero to return the current input_line. */
155 int rl_done;
157 /* The last function executed by readline. */
158 Function *rl_last_func = (Function *)NULL;
160 /* Top level environment for readline_internal (). */
161 static jmp_buf readline_top_level;
163 /* The streams we interact with. */
164 static FILE *in_stream, *out_stream;
166 /* The names of the streams that we do input and output to. */
167 FILE *rl_instream = (FILE *)NULL;
168 FILE *rl_outstream = (FILE *)NULL;
170 /* Non-zero means echo characters as they are read. */
171 int readline_echoing_p = 1;
173 /* Current prompt. */
174 char *rl_prompt;
176 /* The number of characters read in order to type this complete command. */
177 int rl_key_sequence_length = 0;
179 /* If non-zero, then this is the address of a function to call just
180 before readline_internal () prints the first prompt. */
181 Function *rl_startup_hook = (Function *)NULL;
183 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
184 static char *the_line;
186 /* The character that can generate an EOF. Really read from
187 the terminal driver... just defaulted here. */
188 int _rl_eof_char = CTRL ('D');
190 /* Non-zero makes this the next keystroke to read. */
191 int rl_pending_input = 0;
193 /* Pointer to a useful terminal name. */
194 char *rl_terminal_name = (char *)NULL;
196 /* Non-zero means to always use horizontal scrolling in line display. */
197 int _rl_horizontal_scroll_mode = 0;
199 /* Non-zero means to display an asterisk at the starts of history lines
200 which have been modified. */
201 int _rl_mark_modified_lines = 0;
203 /* Non-zero means to use a visible bell if one is available rather than
204 simply ringing the terminal bell. */
205 int _rl_prefer_visible_bell = 0;
207 /* Line buffer and maintenence. */
208 char *rl_line_buffer = (char *)NULL;
209 int rl_line_buffer_len = 0;
210 #define DEFAULT_BUFFER_SIZE 256
213 /* **************************************************************** */
214 /* */
215 /* `Forward' declarations */
216 /* */
217 /* **************************************************************** */
219 /* Non-zero means do not parse any lines other than comments and
220 parser directives. */
221 unsigned char _rl_parsing_conditionalized_out = 0;
223 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
224 static int defining_kbd_macro = 0;
226 /* Non-zero means to convert characters with the meta bit set to
227 escape-prefixed characters so we can indirect through
228 emacs_meta_keymap or vi_escape_keymap. */
229 int _rl_convert_meta_chars_to_ascii = 1;
231 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
232 the undo list. */
233 static int doing_an_undo = 0;
235 /* **************************************************************** */
236 /* */
237 /* Top Level Functions */
238 /* */
239 /* **************************************************************** */
241 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
242 int _rl_meta_flag = 0; /* Forward declaration */
244 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
245 none. A return value of NULL means that EOF was encountered. */
246 char *
247 readline (prompt)
248 char *prompt;
250 char *readline_internal ();
251 char *value;
253 rl_prompt = prompt;
255 /* If we are at EOF return a NULL string. */
256 if (rl_pending_input == EOF)
258 rl_pending_input = 0;
259 return ((char *)NULL);
262 rl_initialize ();
263 rl_prep_terminal (_rl_meta_flag);
265 #if defined (HANDLE_SIGNALS)
266 rl_set_signals ();
267 #endif
269 value = readline_internal ();
270 rl_deprep_terminal ();
272 #if defined (HANDLE_SIGNALS)
273 rl_clear_signals ();
274 #endif
276 return (value);
279 /* Read a line of input from the global rl_instream, doing output on
280 the global rl_outstream.
281 If rl_prompt is non-null, then that is our prompt. */
282 char *
283 readline_internal ()
285 int lastc, c, eof_found;
287 in_stream = rl_instream;
288 out_stream = rl_outstream;
290 lastc = -1;
291 eof_found = 0;
293 if (rl_startup_hook)
294 (*rl_startup_hook) ();
296 if (!readline_echoing_p)
298 if (rl_prompt)
300 fprintf (out_stream, "%s", rl_prompt);
301 fflush (out_stream);
304 else
306 rl_on_new_line ();
307 rl_redisplay ();
308 #if defined (VI_MODE)
309 if (rl_editing_mode == vi_mode)
310 rl_vi_insertion_mode ();
311 #endif /* VI_MODE */
314 while (!rl_done)
316 int lk = last_command_was_kill;
317 int code;
319 code = setjmp (readline_top_level);
321 if (code)
322 rl_redisplay ();
324 if (!rl_pending_input)
326 /* Then initialize the argument and number of keys read. */
327 rl_init_argument ();
328 rl_key_sequence_length = 0;
331 c = rl_read_key ();
333 /* EOF typed to a non-blank line is a <NL>. */
334 if (c == EOF && rl_end)
335 c = NEWLINE;
337 /* The character _rl_eof_char typed to blank line, and not as the
338 previous character is interpreted as EOF. */
339 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
341 eof_found = 1;
342 break;
345 lastc = c;
346 rl_dispatch (c, _rl_keymap);
348 /* If there was no change in last_command_was_kill, then no kill
349 has taken place. Note that if input is pending we are reading
350 a prefix command, so nothing has changed yet. */
351 if (!rl_pending_input)
353 if (lk == last_command_was_kill)
354 last_command_was_kill = 0;
357 #if defined (VI_MODE)
358 /* In vi mode, when you exit insert mode, the cursor moves back
359 over the previous character. We explicitly check for that here. */
360 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
361 rl_vi_check ();
362 #endif /* VI_MODE */
364 if (!rl_done)
365 rl_redisplay ();
368 /* Restore the original of this history line, iff the line that we
369 are editing was originally in the history, AND the line has changed. */
371 HIST_ENTRY *entry = current_history ();
373 if (entry && rl_undo_list)
375 char *temp = savestring (the_line);
376 rl_revert_line ();
377 entry = replace_history_entry (where_history (), the_line,
378 (HIST_ENTRY *)NULL);
379 free_history_entry (entry);
381 strcpy (the_line, temp);
382 free (temp);
386 /* At any rate, it is highly likely that this line has an undo list. Get
387 rid of it now. */
388 if (rl_undo_list)
389 free_undo_list ();
391 if (eof_found)
392 return (char *)NULL;
393 else
394 return (savestring (the_line));
397 /* **************************************************************** */
398 /* */
399 /* Character Input Buffering */
400 /* */
401 /* **************************************************************** */
403 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
404 static unsigned char ibuffer[512];
406 /* Non-null means it is a pointer to a function to run while waiting for
407 character input. */
408 Function *rl_event_hook = (Function *)NULL;
410 #define any_typein (push_index != pop_index)
412 /* Add KEY to the buffer of characters to be read. */
413 rl_stuff_char (key)
414 int key;
416 if (key == EOF)
418 key = NEWLINE;
419 rl_pending_input = EOF;
421 ibuffer[push_index++] = key;
422 if (push_index >= ibuffer_len)
423 push_index = 0;
426 /* Return the amount of space available in the
427 buffer for stuffing characters. */
429 ibuffer_space ()
431 if (pop_index > push_index)
432 return (pop_index - push_index);
433 else
434 return (ibuffer_len - (push_index - pop_index));
437 /* Get a key from the buffer of characters to be read.
438 Return the key in KEY.
439 Result is KEY if there was a key, or 0 if there wasn't. */
441 rl_get_char (key)
442 int *key;
444 if (push_index == pop_index)
445 return (0);
447 *key = ibuffer[pop_index++];
449 if (pop_index >= ibuffer_len)
450 pop_index = 0;
452 return (1);
455 /* Stuff KEY into the *front* of the input buffer.
456 Returns non-zero if successful, zero if there is
457 no space left in the buffer. */
459 rl_unget_char (key)
460 int key;
462 if (ibuffer_space ())
464 pop_index--;
465 if (pop_index < 0)
466 pop_index = ibuffer_len - 1;
467 ibuffer[pop_index] = key;
468 return (1);
470 return (0);
473 /* If a character is available to be read, then read it
474 and stuff it into IBUFFER. Otherwise, just return. */
475 void
476 rl_gather_tyi ()
478 #if defined (__GO32__)
479 char input;
481 if (isatty (0))
483 int i = rl_getc ();
485 if (i != EOF)
486 rl_stuff_char (i);
488 else if (kbhit () && ibuffer_space ())
489 rl_stuff_char (getkey ());
490 #else /* !__GO32__ */
492 int tty = fileno (in_stream);
493 register int tem, result = -1;
494 int chars_avail;
495 char input;
497 #if defined (FIONREAD)
498 result = ioctl (tty, FIONREAD, &chars_avail);
499 #endif
501 #if defined (O_NDELAY)
502 if (result == -1)
504 int flags;
506 flags = fcntl (tty, F_GETFL, 0);
508 fcntl (tty, F_SETFL, (flags | O_NDELAY));
509 chars_avail = read (tty, &input, 1);
511 fcntl (tty, F_SETFL, flags);
512 if (chars_avail == -1 && errno == EAGAIN)
513 return;
515 #endif /* O_NDELAY */
517 /* If there's nothing available, don't waste time trying to read
518 something. */
519 if (chars_avail == 0)
520 return;
522 tem = ibuffer_space ();
524 if (chars_avail > tem)
525 chars_avail = tem;
527 /* One cannot read all of the available input. I can only read a single
528 character at a time, or else programs which require input can be
529 thwarted. If the buffer is larger than one character, I lose.
530 Damn! */
531 if (tem < ibuffer_len)
532 chars_avail = 0;
534 if (result != -1)
536 while (chars_avail--)
537 rl_stuff_char (rl_getc (in_stream));
539 else
541 if (chars_avail)
542 rl_stuff_char (input);
544 #endif /* !__GO32__ */
547 static int next_macro_key ();
548 /* Read a key, including pending input. */
550 rl_read_key ()
552 int c;
554 rl_key_sequence_length++;
556 if (rl_pending_input)
558 c = rl_pending_input;
559 rl_pending_input = 0;
561 else
563 /* If input is coming from a macro, then use that. */
564 if (c = next_macro_key ())
565 return (c);
567 /* If the user has an event function, then call it periodically. */
568 if (rl_event_hook)
570 while (rl_event_hook && !rl_get_char (&c))
572 (*rl_event_hook) ();
573 rl_gather_tyi ();
576 else
578 if (!rl_get_char (&c))
579 c = rl_getc (in_stream);
583 return (c);
586 /* Found later in this file. */
587 static void add_macro_char (), with_macro_input ();
589 /* Do the command associated with KEY in MAP.
590 If the associated command is really a keymap, then read
591 another key, and dispatch into that map. */
592 void
593 rl_dispatch (key, map)
594 register int key;
595 Keymap map;
597 #if defined (VI_MODE)
598 extern int _rl_vi_last_command, _rl_vi_last_repeat, _rl_vi_last_arg_sign;
599 #endif
601 if (defining_kbd_macro)
602 add_macro_char (key);
604 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
606 if (map[ESC].type == ISKMAP)
608 #if defined (CRAY)
609 map = (Keymap)((int)map[ESC].function);
610 #else
611 map = (Keymap)map[ESC].function;
612 #endif
613 key = UNMETA (key);
614 rl_key_sequence_length += 2;
615 rl_dispatch (key, map);
617 else
618 ding ();
619 return;
622 switch (map[key].type)
624 case ISFUNC:
626 Function *func = map[key].function;
628 if (func != (Function *)NULL)
630 /* Special case rl_do_lowercase_version (). */
631 if (func == rl_do_lowercase_version)
633 rl_dispatch (to_lower (key), map);
634 return;
637 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
639 /* If we have input pending, then the last command was a prefix
640 command. Don't change the state of rl_last_func. Otherwise,
641 remember the last command executed in this variable. */
642 if (!rl_pending_input)
643 rl_last_func = map[key].function;
645 else
647 rl_abort ();
648 return;
651 break;
653 case ISKMAP:
654 if (map[key].function != (Function *)NULL)
656 int newkey;
658 rl_key_sequence_length++;
659 newkey = rl_read_key ();
660 #if defined (CRAY)
661 /* If you cast map[key].function to type (Keymap) on a Cray,
662 the compiler takes the value of may[key].function and
663 divides it by 4 to convert between pointer types (pointers
664 to functions and pointers to structs are different sizes).
665 This is not what is wanted. */
666 rl_dispatch (newkey, (Keymap)((int)map[key].function));
667 #else
668 rl_dispatch (newkey, (Keymap)map[key].function);
669 #endif /* !CRAY */
671 else
673 rl_abort ();
674 return;
676 break;
678 case ISMACR:
679 if (map[key].function != (Function *)NULL)
681 char *macro;
683 macro = savestring ((char *)map[key].function);
684 with_macro_input (macro);
685 return;
687 break;
689 #if defined (VI_MODE)
690 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
691 rl_vi_textmod_command (key))
693 _rl_vi_last_command = key;
694 _rl_vi_last_repeat = rl_numeric_arg;
695 _rl_vi_last_arg_sign = rl_arg_sign;
697 #endif
701 /* **************************************************************** */
702 /* */
703 /* Hacking Keyboard Macros */
704 /* */
705 /* **************************************************************** */
707 /* The currently executing macro string. If this is non-zero,
708 then it is a malloc ()'ed string where input is coming from. */
709 static char *executing_macro = (char *)NULL;
711 /* The offset in the above string to the next character to be read. */
712 static int executing_macro_index = 0;
714 /* The current macro string being built. Characters get stuffed
715 in here by add_macro_char (). */
716 static char *current_macro = (char *)NULL;
718 /* The size of the buffer allocated to current_macro. */
719 static int current_macro_size = 0;
721 /* The index at which characters are being added to current_macro. */
722 static int current_macro_index = 0;
724 /* A structure used to save nested macro strings.
725 It is a linked list of string/index for each saved macro. */
726 struct saved_macro {
727 struct saved_macro *next;
728 char *string;
729 int index;
732 /* The list of saved macros. */
733 struct saved_macro *macro_list = (struct saved_macro *)NULL;
735 /* Forward declarations of static functions. Thank you C. */
736 static void push_executing_macro (), pop_executing_macro ();
738 /* This one has to be declared earlier in the file. */
739 /* static void add_macro_char (); */
741 /* Set up to read subsequent input from STRING.
742 STRING is free ()'ed when we are done with it. */
743 static void
744 with_macro_input (string)
745 char *string;
747 push_executing_macro ();
748 executing_macro = string;
749 executing_macro_index = 0;
752 /* Return the next character available from a macro, or 0 if
753 there are no macro characters. */
754 static int
755 next_macro_key ()
757 if (!executing_macro)
758 return (0);
760 if (!executing_macro[executing_macro_index])
762 pop_executing_macro ();
763 return (next_macro_key ());
766 return (executing_macro[executing_macro_index++]);
769 /* Save the currently executing macro on a stack of saved macros. */
770 static void
771 push_executing_macro ()
773 struct saved_macro *saver;
775 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
776 saver->next = macro_list;
777 saver->index = executing_macro_index;
778 saver->string = executing_macro;
780 macro_list = saver;
783 /* Discard the current macro, replacing it with the one
784 on the top of the stack of saved macros. */
785 static void
786 pop_executing_macro ()
788 if (executing_macro)
789 free (executing_macro);
791 executing_macro = (char *)NULL;
792 executing_macro_index = 0;
794 if (macro_list)
796 struct saved_macro *disposer = macro_list;
797 executing_macro = macro_list->string;
798 executing_macro_index = macro_list->index;
799 macro_list = macro_list->next;
800 free (disposer);
804 /* Add a character to the macro being built. */
805 static void
806 add_macro_char (c)
807 int c;
809 if (current_macro_index + 1 >= current_macro_size)
811 if (!current_macro)
812 current_macro = (char *)xmalloc (current_macro_size = 25);
813 else
814 current_macro =
815 (char *)xrealloc (current_macro, current_macro_size += 25);
818 current_macro[current_macro_index++] = c;
819 current_macro[current_macro_index] = '\0';
822 /* Begin defining a keyboard macro.
823 Keystrokes are recorded as they are executed.
824 End the definition with rl_end_kbd_macro ().
825 If a numeric argument was explicitly typed, then append this
826 definition to the end of the existing macro, and start by
827 re-executing the existing macro. */
828 rl_start_kbd_macro (ignore1, ignore2)
829 int ignore1, ignore2;
831 if (defining_kbd_macro)
832 rl_abort ();
834 if (rl_explicit_arg)
836 if (current_macro)
837 with_macro_input (savestring (current_macro));
839 else
840 current_macro_index = 0;
842 defining_kbd_macro = 1;
845 /* Stop defining a keyboard macro.
846 A numeric argument says to execute the macro right now,
847 that many times, counting the definition as the first time. */
848 rl_end_kbd_macro (count, ignore)
849 int count, ignore;
851 if (!defining_kbd_macro)
852 rl_abort ();
854 current_macro_index -= (rl_key_sequence_length - 1);
855 current_macro[current_macro_index] = '\0';
857 defining_kbd_macro = 0;
859 rl_call_last_kbd_macro (--count, 0);
862 /* Execute the most recently defined keyboard macro.
863 COUNT says how many times to execute it. */
864 rl_call_last_kbd_macro (count, ignore)
865 int count, ignore;
867 if (!current_macro)
868 rl_abort ();
870 if (defining_kbd_macro)
872 ding (); /* no recursive macros */
873 current_macro[--current_macro_index] = '\0'; /* erase this char */
874 return 0;
877 while (count--)
878 with_macro_input (savestring (current_macro));
881 void
882 _rl_kill_kbd_macro ()
884 if (current_macro)
886 free (current_macro);
887 current_macro = (char *) NULL;
889 current_macro_size = current_macro_index = 0;
891 if (executing_macro)
893 free (executing_macro);
894 executing_macro = (char *) NULL;
896 executing_macro_index = 0;
898 defining_kbd_macro = 0;
902 /* **************************************************************** */
903 /* */
904 /* Initializations */
905 /* */
906 /* **************************************************************** */
908 /* Initliaze readline (and terminal if not already). */
909 rl_initialize ()
911 /* If we have never been called before, initialize the
912 terminal and data structures. */
913 if (!rl_initialized)
915 readline_initialize_everything ();
916 rl_initialized++;
919 /* Initalize the current line information. */
920 rl_point = rl_end = 0;
921 the_line = rl_line_buffer;
922 the_line[0] = 0;
924 /* We aren't done yet. We haven't even gotten started yet! */
925 rl_done = 0;
927 /* Tell the history routines what is going on. */
928 start_using_history ();
930 /* Make the display buffer match the state of the line. */
931 rl_reset_line_state ();
933 /* No such function typed yet. */
934 rl_last_func = (Function *)NULL;
936 /* Parsing of key-bindings begins in an enabled state. */
937 _rl_parsing_conditionalized_out = 0;
940 /* Initialize the entire state of the world. */
941 readline_initialize_everything ()
943 /* Find out if we are running in Emacs. */
944 running_in_emacs = getenv ("EMACS");
946 /* Set up input and output if they are not already set up. */
947 if (!rl_instream)
948 rl_instream = stdin;
950 if (!rl_outstream)
951 rl_outstream = stdout;
953 /* Bind in_stream and out_stream immediately. These values may change,
954 but they may also be used before readline_internal () is called. */
955 in_stream = rl_instream;
956 out_stream = rl_outstream;
958 /* Allocate data structures. */
959 if (!rl_line_buffer)
960 rl_line_buffer =
961 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
963 /* Initialize the terminal interface. */
964 init_terminal_io ((char *)NULL);
966 #if !defined (__GO32__)
967 /* Bind tty characters to readline functions. */
968 readline_default_bindings ();
969 #endif /* !__GO32__ */
971 /* Initialize the function names. */
972 rl_initialize_funmap ();
974 /* Read in the init file. */
975 rl_read_init_file ((char *)NULL);
977 /* If the completion parser's default word break characters haven't
978 been set yet, then do so now. */
980 if (rl_completer_word_break_characters == (char *)NULL)
981 rl_completer_word_break_characters = rl_basic_word_break_characters;
985 /* If this system allows us to look at the values of the regular
986 input editing characters, then bind them to their readline
987 equivalents, iff the characters are not bound to keymaps. */
988 #if !defined (__GO32__)
989 static void
990 readline_default_bindings ()
992 rltty_set_default_bindings (_rl_keymap);
994 #endif /* !__GO32__ */
997 /* **************************************************************** */
998 /* */
999 /* Numeric Arguments */
1000 /* */
1001 /* **************************************************************** */
1003 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1005 /* Add the current digit to the argument in progress. */
1006 rl_digit_argument (ignore, key)
1007 int ignore, key;
1009 rl_pending_input = key;
1010 rl_digit_loop ();
1013 /* What to do when you abort reading an argument. */
1014 rl_discard_argument ()
1016 ding ();
1017 rl_clear_message ();
1018 rl_init_argument ();
1021 /* Create a default argument. */
1022 rl_init_argument ()
1024 rl_numeric_arg = rl_arg_sign = 1;
1025 rl_explicit_arg = 0;
1028 /* C-u, universal argument. Multiply the current argument by 4.
1029 Read a key. If the key has nothing to do with arguments, then
1030 dispatch on it. If the key is the abort character then abort. */
1031 rl_universal_argument ()
1033 rl_numeric_arg *= 4;
1034 rl_digit_loop ();
1037 rl_digit_loop ()
1039 int key, c;
1040 while (1)
1042 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1043 key = c = rl_read_key ();
1045 if (_rl_keymap[c].type == ISFUNC &&
1046 _rl_keymap[c].function == rl_universal_argument)
1048 rl_numeric_arg *= 4;
1049 continue;
1051 c = UNMETA (c);
1052 if (numeric (c))
1054 if (rl_explicit_arg)
1055 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1056 else
1057 rl_numeric_arg = (c - '0');
1058 rl_explicit_arg = 1;
1060 else
1062 if (c == '-' && !rl_explicit_arg)
1064 rl_numeric_arg = 1;
1065 rl_arg_sign = -1;
1067 else
1069 rl_clear_message ();
1070 rl_dispatch (key, _rl_keymap);
1071 return;
1077 /* **************************************************************** */
1078 /* */
1079 /* Terminal and Termcap */
1080 /* */
1081 /* **************************************************************** */
1083 static char *term_buffer = (char *)NULL;
1084 static char *term_string_buffer = (char *)NULL;
1086 /* Non-zero means this terminal can't really do anything. */
1087 int dumb_term = 0;
1088 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1089 Unfortunately, PC is a global variable used by the termcap library. */
1090 #undef PC
1092 #if !defined (__linux__)
1093 char PC;
1094 char *BC, *UP;
1095 #endif /* __linux__ */
1097 /* Some strings to control terminal actions. These are output by tputs (). */
1098 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1100 int screenwidth, screenheight;
1102 /* Non-zero if we determine that the terminal can do character insertion. */
1103 int terminal_can_insert = 0;
1105 /* How to insert characters. */
1106 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1108 /* How to delete characters. */
1109 char *term_dc, *term_DC;
1111 #if defined (HACK_TERMCAP_MOTION)
1112 char *term_forward_char;
1113 #endif /* HACK_TERMCAP_MOTION */
1115 /* How to go up a line. */
1116 char *term_up;
1118 /* True if we have funny auto-line-wrap ("am" and "xn"). */
1119 int term_xn;
1121 /* A visible bell, if the terminal can be made to flash the screen. */
1122 char *visible_bell;
1124 /* Non-zero means that this terminal has a meta key. */
1125 int term_has_meta;
1127 /* The string to write to turn on the meta key, if this term has one. */
1128 char *term_mm;
1130 /* The string to write to turn off the meta key, if this term has one. */
1131 char *term_mo;
1133 /* The key sequences output by the arrow keys, if this terminal has any. */
1134 char *term_ku, *term_kd, *term_kr, *term_kl;
1136 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1137 has changed. */
1138 rl_reset_terminal (terminal_name)
1139 char *terminal_name;
1141 init_terminal_io (terminal_name);
1144 /* Set readline's idea of the screen size. TTY is a file descriptor open
1145 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1146 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1147 non-null serve to check whether or not we have initialized termcap. */
1148 void
1149 _rl_set_screen_size (tty, ignore_env)
1150 int tty, ignore_env;
1152 #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
1153 struct winsize window_size;
1154 #endif /* TIOCGWINSZ */
1156 #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
1157 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1159 screenwidth = (int) window_size.ws_col;
1160 screenheight = (int) window_size.ws_row;
1162 #endif /* TIOCGWINSZ */
1164 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1165 is unset. */
1166 if (screenwidth <= 0)
1168 char *sw;
1170 if (!ignore_env && (sw = getenv ("COLUMNS")))
1171 screenwidth = atoi (sw);
1173 if (screenwidth <= 0 && term_string_buffer)
1174 screenwidth = tgetnum ("co");
1177 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1178 is unset. */
1179 if (screenheight <= 0)
1181 char *sh;
1183 if (!ignore_env && (sh = getenv ("LINES")))
1184 screenheight = atoi (sh);
1186 if (screenheight <= 0 && term_string_buffer)
1187 screenheight = tgetnum ("li");
1190 /* If all else fails, default to 80x24 terminal. */
1191 if (screenwidth <= 0)
1192 screenwidth = 80;
1194 if (screenheight <= 0)
1195 screenheight = 24;
1197 #if defined (SHELL)
1198 /* If we're being compiled as part of bash, set the environment
1199 variables $LINES and $COLUMNS to new values. */
1200 set_lines_and_columns (screenheight, screenwidth);
1201 #endif
1203 /* If we don't have xn (most modern terminals do),
1204 don't use the last column. */
1205 if (!term_xn)
1206 screenwidth--;
1209 init_terminal_io (terminal_name)
1210 char *terminal_name;
1212 #if defined (__GO32__)
1213 screenwidth = ScreenCols ();
1214 screenheight = ScreenRows ();
1215 term_cr = "\r";
1216 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1217 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1219 /* Does the __GO32__ have a meta key? I don't know. */
1220 term_has_meta = 0;
1221 term_mm = term_mo = (char *)NULL;
1223 /* It probably has arrow keys, but I don't know what they are. */
1224 term_ku = term_kd = term_kr = term_kl = (char *)NULL;
1226 #if defined (HACK_TERMCAP_MOTION)
1227 term_forward_char = (char *)NULL;
1228 #endif /* HACK_TERMCAP_MOTION */
1229 terminal_can_insert = term_xn = 0;
1230 return;
1231 #else /* !__GO32__ */
1233 char *term, *buffer;
1234 int tty;
1236 term = terminal_name ? terminal_name : getenv ("TERM");
1238 if (!term_string_buffer)
1239 term_string_buffer = (char *)xmalloc (2048);
1241 if (!term_buffer)
1242 term_buffer = (char *)xmalloc (2048);
1244 buffer = term_string_buffer;
1246 term_clrpag = term_cr = term_clreol = (char *)NULL;
1248 if (!term)
1249 term = "dumb";
1251 if (tgetent (term_buffer, term) <= 0)
1253 dumb_term = 1;
1254 screenwidth = 79;
1255 screenheight = 24;
1256 term_cr = "\r";
1257 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1258 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1259 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
1260 #if defined (HACK_TERMCAP_MOTION)
1261 term_forward_char = (char *)NULL;
1262 #endif
1263 terminal_can_insert = term_xn = 0;
1264 return;
1267 BC = tgetstr ("pc", &buffer);
1268 PC = buffer ? *buffer : 0;
1270 term_backspace = tgetstr ("le", &buffer);
1272 term_cr = tgetstr ("cr", &buffer);
1273 term_clreol = tgetstr ("ce", &buffer);
1274 term_clrpag = tgetstr ("cl", &buffer);
1276 if (!term_cr)
1277 term_cr = "\r";
1279 #if defined (HACK_TERMCAP_MOTION)
1280 term_forward_char = tgetstr ("nd", &buffer);
1281 #endif /* HACK_TERMCAP_MOTION */
1283 if (rl_instream)
1284 tty = fileno (rl_instream);
1285 else
1286 tty = 0;
1288 screenwidth = screenheight = 0;
1290 term_xn = tgetflag ("am") && tgetflag ("xn");
1292 _rl_set_screen_size (tty, 0);
1294 term_im = tgetstr ("im", &buffer);
1295 term_ei = tgetstr ("ei", &buffer);
1296 term_IC = tgetstr ("IC", &buffer);
1297 term_ic = tgetstr ("ic", &buffer);
1299 /* "An application program can assume that the terminal can do
1300 character insertion if *any one of* the capabilities `IC',
1301 `im', `ic' or `ip' is provided." But we can't do anything if
1302 only `ip' is provided, so... */
1303 terminal_can_insert = (term_IC || term_im || term_ic);
1305 term_up = tgetstr ("up", &buffer);
1306 term_dc = tgetstr ("dc", &buffer);
1307 term_DC = tgetstr ("DC", &buffer);
1309 visible_bell = tgetstr ("vb", &buffer);
1311 /* Check to see if this terminal has a meta key. */
1312 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1313 if (term_has_meta)
1315 term_mm = tgetstr ("mm", &buffer);
1316 term_mo = tgetstr ("mo", &buffer);
1318 else
1320 term_mm = (char *)NULL;
1321 term_mo = (char *)NULL;
1324 /* Attempt to find and bind the arrow keys. Do not override already
1325 bound keys in an overzealous attempt, however. */
1326 term_ku = tgetstr ("ku", &buffer);
1327 term_kd = tgetstr ("kd", &buffer);
1328 term_kr = tgetstr ("kr", &buffer);
1329 term_kl = tgetstr ("kl", &buffer);
1331 if (term_ku)
1333 Function *func;
1335 func = rl_function_of_keyseq (term_ku, _rl_keymap, (int *)NULL);
1337 if (!func || func == rl_do_lowercase_version)
1338 rl_set_key (term_ku, rl_get_previous_history, _rl_keymap);
1341 if (term_kd)
1343 Function *func;
1345 func = rl_function_of_keyseq (term_kd, _rl_keymap, (int *)NULL);
1347 if (!func || func == rl_do_lowercase_version)
1348 rl_set_key (term_kd, rl_get_next_history, _rl_keymap);
1351 if (term_kr)
1353 Function *func;
1355 func = rl_function_of_keyseq (term_kr, _rl_keymap, (int *)NULL);
1357 if (!func || func == rl_do_lowercase_version)
1358 rl_set_key (term_kr, rl_forward, _rl_keymap);
1361 if (term_kl)
1363 Function *func;
1365 func = rl_function_of_keyseq (term_kl, _rl_keymap, (int *)NULL);
1367 if (!func || func == rl_do_lowercase_version)
1368 rl_set_key (term_kl, rl_backward, _rl_keymap);
1370 #endif /* !__GO32__ */
1371 return 0;
1374 /* A function for the use of tputs () */
1376 _rl_output_character_function (c)
1377 int c;
1379 return putc (c, out_stream);
1382 /* Write COUNT characters from STRING to the output stream. */
1383 void
1384 _rl_output_some_chars (string, count)
1385 char *string;
1386 int count;
1388 fwrite (string, 1, count, out_stream);
1391 /* Move the cursor back. */
1392 backspace (count)
1393 int count;
1395 register int i;
1397 #if !defined (__GO32__)
1398 if (term_backspace)
1399 for (i = 0; i < count; i++)
1400 tputs (term_backspace, 1, _rl_output_character_function);
1401 else
1402 #endif /* !__GO32__ */
1403 for (i = 0; i < count; i++)
1404 putc ('\b', out_stream);
1407 /* Move to the start of the next line. */
1408 crlf ()
1410 #if defined (NEW_TTY_DRIVER)
1411 tputs (term_cr, 1, _rl_output_character_function);
1412 #endif /* NEW_TTY_DRIVER */
1413 putc ('\n', out_stream);
1417 /* **************************************************************** */
1418 /* */
1419 /* Utility Functions */
1420 /* */
1421 /* **************************************************************** */
1423 /* Return 0 if C is not a member of the class of characters that belong
1424 in words, or 1 if it is. */
1426 int allow_pathname_alphabetic_chars = 0;
1427 char *pathname_alphabetic_chars = "/-_=~.#$";
1430 alphabetic (c)
1431 int c;
1433 if (pure_alphabetic (c) || (numeric (c)))
1434 return (1);
1436 if (allow_pathname_alphabetic_chars)
1437 return (strchr (pathname_alphabetic_chars, c) != NULL);
1438 else
1439 return (0);
1442 /* Return non-zero if C is a numeric character. */
1444 numeric (c)
1445 int c;
1447 return (c >= '0' && c <= '9');
1450 /* Ring the terminal bell. */
1452 ding ()
1454 if (readline_echoing_p)
1456 #if !defined (__GO32__)
1457 if (_rl_prefer_visible_bell && visible_bell)
1458 tputs (visible_bell, 1, _rl_output_character_function);
1459 else
1460 #endif /* !__GO32__ */
1462 fprintf (stderr, "\007");
1463 fflush (stderr);
1466 return (-1);
1469 /* How to abort things. */
1470 rl_abort ()
1472 ding ();
1473 rl_clear_message ();
1474 rl_init_argument ();
1475 rl_pending_input = 0;
1477 defining_kbd_macro = 0;
1478 while (executing_macro)
1479 pop_executing_macro ();
1481 rl_last_func = (Function *)NULL;
1482 longjmp (readline_top_level, 1);
1485 /* Return a copy of the string between FROM and TO.
1486 FROM is inclusive, TO is not. */
1487 char *
1488 rl_copy_text (from, to)
1489 int from, to;
1491 register int length;
1492 char *copy;
1494 /* Fix it if the caller is confused. */
1495 if (from > to)
1497 int t = from;
1498 from = to;
1499 to = t;
1502 length = to - from;
1503 copy = (char *)xmalloc (1 + length);
1504 strncpy (copy, the_line + from, length);
1505 copy[length] = '\0';
1506 return (copy);
1509 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1510 LEN characters. */
1511 void
1512 rl_extend_line_buffer (len)
1513 int len;
1515 while (len >= rl_line_buffer_len)
1516 rl_line_buffer =
1517 (char *)xrealloc
1518 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
1520 the_line = rl_line_buffer;
1524 /* **************************************************************** */
1525 /* */
1526 /* Insert and Delete */
1527 /* */
1528 /* **************************************************************** */
1530 /* Insert a string of text into the line at point. This is the only
1531 way that you should do insertion. rl_insert () calls this
1532 function. */
1533 rl_insert_text (string)
1534 char *string;
1536 register int i, l = strlen (string);
1538 if (rl_end + l >= rl_line_buffer_len)
1539 rl_extend_line_buffer (rl_end + l);
1541 for (i = rl_end; i >= rl_point; i--)
1542 the_line[i + l] = the_line[i];
1543 strncpy (the_line + rl_point, string, l);
1545 /* Remember how to undo this if we aren't undoing something. */
1546 if (!doing_an_undo)
1548 /* If possible and desirable, concatenate the undos. */
1549 if ((strlen (string) == 1) &&
1550 rl_undo_list &&
1551 (rl_undo_list->what == UNDO_INSERT) &&
1552 (rl_undo_list->end == rl_point) &&
1553 (rl_undo_list->end - rl_undo_list->start < 20))
1554 rl_undo_list->end++;
1555 else
1556 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1558 rl_point += l;
1559 rl_end += l;
1560 the_line[rl_end] = '\0';
1563 /* Delete the string between FROM and TO. FROM is
1564 inclusive, TO is not. */
1565 rl_delete_text (from, to)
1566 int from, to;
1568 register char *text;
1570 /* Fix it if the caller is confused. */
1571 if (from > to)
1573 int t = from;
1574 from = to;
1575 to = t;
1577 text = rl_copy_text (from, to);
1578 strncpy (the_line + from, the_line + to, rl_end - to);
1580 /* Remember how to undo this delete. */
1581 if (!doing_an_undo)
1582 rl_add_undo (UNDO_DELETE, from, to, text);
1583 else
1584 free (text);
1586 rl_end -= (to - from);
1587 the_line[rl_end] = '\0';
1591 /* **************************************************************** */
1592 /* */
1593 /* Readline character functions */
1594 /* */
1595 /* **************************************************************** */
1597 /* This is not a gap editor, just a stupid line input routine. No hair
1598 is involved in writing any of the functions, and none should be. */
1600 /* Note that:
1602 rl_end is the place in the string that we would place '\0';
1603 i.e., it is always safe to place '\0' there.
1605 rl_point is the place in the string where the cursor is. Sometimes
1606 this is the same as rl_end.
1608 Any command that is called interactively receives two arguments.
1609 The first is a count: the numeric arg pased to this command.
1610 The second is the key which invoked this command.
1614 /* **************************************************************** */
1615 /* */
1616 /* Movement Commands */
1617 /* */
1618 /* **************************************************************** */
1620 /* Note that if you `optimize' the display for these functions, you cannot
1621 use said functions in other functions which do not do optimizing display.
1622 I.e., you will have to update the data base for rl_redisplay, and you
1623 might as well let rl_redisplay do that job. */
1625 /* Move forward COUNT characters. */
1626 rl_forward (count)
1627 int count;
1629 if (count < 0)
1630 rl_backward (-count);
1631 else
1632 while (count)
1634 #if defined (VI_MODE)
1635 if (rl_point >= (rl_end - (rl_editing_mode == vi_mode)))
1636 #else
1637 if (rl_point == rl_end)
1638 #endif /* VI_MODE */
1640 ding ();
1641 return 0;
1643 else
1644 rl_point++;
1645 --count;
1647 return 0;
1650 /* Move backward COUNT characters. */
1651 rl_backward (count)
1652 int count;
1654 if (count < 0)
1655 rl_forward (-count);
1656 else
1657 while (count)
1659 if (!rl_point)
1661 ding ();
1662 return 0;
1664 else
1665 --rl_point;
1666 --count;
1668 return 0;
1671 /* Move to the beginning of the line. */
1672 rl_beg_of_line ()
1674 rl_point = 0;
1675 return 0;
1678 /* Move to the end of the line. */
1679 rl_end_of_line ()
1681 rl_point = rl_end;
1682 return 0;
1685 /* Move forward a word. We do what Emacs does. */
1686 rl_forward_word (count)
1687 int count;
1689 int c;
1691 if (count < 0)
1693 rl_backward_word (-count);
1694 return 0;
1697 while (count)
1699 if (rl_point == rl_end)
1700 return 0;
1702 /* If we are not in a word, move forward until we are in one.
1703 Then, move forward until we hit a non-alphabetic character. */
1704 c = the_line[rl_point];
1705 if (!alphabetic (c))
1707 while (++rl_point < rl_end)
1709 c = the_line[rl_point];
1710 if (alphabetic (c)) break;
1713 if (rl_point == rl_end) return;
1714 while (++rl_point < rl_end)
1716 c = the_line[rl_point];
1717 if (!alphabetic (c)) break;
1719 --count;
1721 return 0;
1724 /* Move backward a word. We do what Emacs does. */
1725 rl_backward_word (count)
1726 int count;
1728 int c;
1730 if (count < 0)
1732 rl_forward_word (-count);
1733 return 0;
1736 while (count)
1738 if (!rl_point)
1739 return 0;
1741 /* Like rl_forward_word (), except that we look at the characters
1742 just before point. */
1744 c = the_line[rl_point - 1];
1745 if (!alphabetic (c))
1747 while (--rl_point)
1749 c = the_line[rl_point - 1];
1750 if (alphabetic (c)) break;
1754 while (rl_point)
1756 c = the_line[rl_point - 1];
1757 if (!alphabetic (c))
1758 break;
1759 else --rl_point;
1761 --count;
1763 return 0;
1766 /* Clear the current line. Numeric argument to C-l does this. */
1767 rl_refresh_line ()
1769 int curr_line = _rl_last_c_pos / screenwidth;
1771 _rl_move_vert (curr_line);
1772 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1774 #if defined (__GO32__)
1776 int row, col, width, row_start;
1778 ScreenGetCursor (&row, &col);
1779 width = ScreenCols ();
1780 row_start = ScreenPrimary + (row * width);
1781 memset (row_start + col, 0, (width - col) * 2);
1783 #else /* !__GO32__ */
1784 if (term_clreol)
1785 tputs (term_clreol, 1, _rl_output_character_function);
1786 #endif /* !__GO32__ */
1788 rl_forced_update_display ();
1789 rl_display_fixed = 1;
1791 return 0;
1794 /* C-l typed to a line without quoting clears the screen, and then reprints
1795 the prompt and the current input line. Given a numeric arg, redraw only
1796 the current line. */
1797 rl_clear_screen ()
1799 if (rl_explicit_arg)
1801 rl_refresh_line ();
1802 return 0;
1805 #if !defined (__GO32__)
1806 if (term_clrpag)
1807 tputs (term_clrpag, 1, _rl_output_character_function);
1808 else
1809 #endif /* !__GO32__ */
1810 crlf ();
1812 rl_forced_update_display ();
1813 rl_display_fixed = 1;
1815 return 0;
1818 rl_arrow_keys (count, c)
1819 int count, c;
1821 int ch;
1823 ch = rl_read_key ();
1825 switch (to_upper (ch))
1827 case 'A':
1828 rl_get_previous_history (count);
1829 break;
1831 case 'B':
1832 rl_get_next_history (count);
1833 break;
1835 case 'C':
1836 rl_forward (count);
1837 break;
1839 case 'D':
1840 rl_backward (count);
1841 break;
1843 default:
1844 ding ();
1846 return 0;
1850 /* **************************************************************** */
1851 /* */
1852 /* Text commands */
1853 /* */
1854 /* **************************************************************** */
1856 /* Insert the character C at the current location, moving point forward. */
1857 rl_insert (count, c)
1858 int count, c;
1860 register int i;
1861 char *string;
1863 if (count <= 0)
1864 return 0;
1866 /* If we can optimize, then do it. But don't let people crash
1867 readline because of extra large arguments. */
1868 if (count > 1 && count < 1024)
1870 string = (char *)alloca (1 + count);
1872 for (i = 0; i < count; i++)
1873 string[i] = c;
1875 string[i] = '\0';
1876 rl_insert_text (string);
1877 return 0;
1880 if (count > 1024)
1882 int decreaser;
1884 string = (char *)alloca (1024 + 1);
1886 for (i = 0; i < 1024; i++)
1887 string[i] = c;
1889 while (count)
1891 decreaser = (count > 1024 ? 1024 : count);
1892 string[decreaser] = '\0';
1893 rl_insert_text (string);
1894 count -= decreaser;
1896 return 0;
1899 /* We are inserting a single character.
1900 If there is pending input, then make a string of all of the
1901 pending characters that are bound to rl_insert, and insert
1902 them all. */
1903 if (any_typein)
1905 int key = 0, t;
1907 i = 0;
1908 string = (char *)alloca (ibuffer_len + 1);
1909 string[i++] = c;
1911 while ((t = rl_get_char (&key)) &&
1912 (_rl_keymap[key].type == ISFUNC &&
1913 _rl_keymap[key].function == rl_insert))
1914 string[i++] = key;
1916 if (t)
1917 rl_unget_char (key);
1919 string[i] = '\0';
1920 rl_insert_text (string);
1922 else
1924 /* Inserting a single character. */
1925 string = (char *)alloca (2);
1927 string[1] = '\0';
1928 string[0] = c;
1929 rl_insert_text (string);
1931 return 0;
1934 /* Insert the next typed character verbatim. */
1935 rl_quoted_insert (count)
1936 int count;
1938 int c;
1940 c = rl_read_key ();
1941 return (rl_insert (count, c));
1945 /* Insert a tab character. */
1946 rl_tab_insert (count)
1947 int count;
1949 return (rl_insert (count, '\t'));
1952 /* What to do when a NEWLINE is pressed. We accept the whole line.
1953 KEY is the key that invoked this command. I guess it could have
1954 meaning in the future. */
1955 rl_newline (count, key)
1956 int count, key;
1958 rl_done = 1;
1960 #if defined (VI_MODE)
1962 extern int _rl_vi_doing_insert;
1963 if (_rl_vi_doing_insert)
1965 rl_end_undo_group ();
1966 _rl_vi_doing_insert = 0;
1969 rl_vi_set_last ();
1971 #endif /* VI_MODE */
1973 if (readline_echoing_p)
1975 _rl_move_vert (_rl_vis_botlin);
1976 _rl_vis_botlin = 0;
1977 crlf ();
1978 fflush (out_stream);
1979 rl_display_fixed++;
1981 return 0;
1984 rl_clean_up_for_exit ()
1986 if (readline_echoing_p)
1988 _rl_move_vert (_rl_vis_botlin);
1989 _rl_vis_botlin = 0;
1990 fflush (out_stream);
1991 rl_restart_output ();
1993 return 0;
1996 /* What to do for some uppercase characters, like meta characters,
1997 and some characters appearing in emacs_ctlx_keymap. This function
1998 is just a stub, you bind keys to it and the code in rl_dispatch ()
1999 is special cased. */
2000 rl_do_lowercase_version (ignore1, ignore2)
2001 int ignore1, ignore2;
2003 return 0;
2006 /* Rubout the character behind point. */
2007 rl_rubout (count)
2008 int count;
2010 if (count < 0)
2012 rl_delete (-count);
2013 return 0;
2016 if (!rl_point)
2018 ding ();
2019 return -1;
2022 if (count > 1 || rl_explicit_arg)
2024 int orig_point = rl_point;
2025 rl_backward (count);
2026 rl_kill_text (orig_point, rl_point);
2028 else
2030 int c = the_line[--rl_point];
2031 rl_delete_text (rl_point, rl_point + 1);
2033 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
2035 int l;
2036 l = rl_character_len (c, rl_point);
2037 _rl_erase_at_end_of_line (l);
2040 return 0;
2043 /* Delete the character under the cursor. Given a numeric argument,
2044 kill that many characters instead. */
2045 rl_delete (count, invoking_key)
2046 int count, invoking_key;
2048 if (count < 0)
2050 return (rl_rubout (-count));
2053 if (rl_point == rl_end)
2055 ding ();
2056 return -1;
2059 if (count > 1 || rl_explicit_arg)
2061 int orig_point = rl_point;
2062 rl_forward (count);
2063 rl_kill_text (orig_point, rl_point);
2064 rl_point = orig_point;
2065 return 0;
2067 else
2068 return (rl_delete_text (rl_point, rl_point + 1));
2072 /* Delete all spaces and tabs around point. */
2073 rl_delete_horizontal_space (count, ignore)
2074 int count, ignore;
2076 int start = rl_point;
2078 while (rl_point && whitespace (the_line[rl_point - 1]))
2079 rl_point--;
2081 start = rl_point;
2083 while (rl_point < rl_end && whitespace (the_line[rl_point]))
2084 rl_point++;
2086 if (start != rl_point)
2088 rl_delete_text (start, rl_point);
2089 rl_point = start;
2091 return 0;
2095 /* **************************************************************** */
2096 /* */
2097 /* Kill commands */
2098 /* */
2099 /* **************************************************************** */
2101 /* The next two functions mimic unix line editing behaviour, except they
2102 save the deleted text on the kill ring. This is safer than not saving
2103 it, and since we have a ring, nobody should get screwed. */
2105 /* This does what C-w does in Unix. We can't prevent people from
2106 using behaviour that they expect. */
2107 rl_unix_word_rubout ()
2109 if (!rl_point)
2110 ding ();
2111 else
2113 int orig_point = rl_point;
2115 while (rl_point && whitespace (the_line[rl_point - 1]))
2116 rl_point--;
2118 while (rl_point && !whitespace (the_line[rl_point - 1]))
2119 rl_point--;
2121 rl_kill_text (rl_point, orig_point);
2123 return 0;
2126 /* Here is C-u doing what Unix does. You don't *have* to use these
2127 key-bindings. We have a choice of killing the entire line, or
2128 killing from where we are to the start of the line. We choose the
2129 latter, because if you are a Unix weenie, then you haven't backspaced
2130 into the line at all, and if you aren't, then you know what you are
2131 doing. */
2132 rl_unix_line_discard ()
2134 if (!rl_point)
2135 ding ();
2136 else
2138 rl_kill_text (rl_point, 0);
2139 rl_point = 0;
2141 return 0;
2145 /* **************************************************************** */
2146 /* */
2147 /* Commands For Typos */
2148 /* */
2149 /* **************************************************************** */
2151 /* Random and interesting things in here. */
2153 /* **************************************************************** */
2154 /* */
2155 /* Changing Case */
2156 /* */
2157 /* **************************************************************** */
2159 /* The three kinds of things that we know how to do. */
2160 #define UpCase 1
2161 #define DownCase 2
2162 #define CapCase 3
2164 static int rl_change_case ();
2166 /* Uppercase the word at point. */
2167 rl_upcase_word (count)
2168 int count;
2170 return (rl_change_case (count, UpCase));
2173 /* Lowercase the word at point. */
2174 rl_downcase_word (count)
2175 int count;
2177 return (rl_change_case (count, DownCase));
2180 /* Upcase the first letter, downcase the rest. */
2181 rl_capitalize_word (count)
2182 int count;
2184 return (rl_change_case (count, CapCase));
2187 /* The meaty function.
2188 Change the case of COUNT words, performing OP on them.
2189 OP is one of UpCase, DownCase, or CapCase.
2190 If a negative argument is given, leave point where it started,
2191 otherwise, leave it where it moves to. */
2192 static int
2193 rl_change_case (count, op)
2194 int count, op;
2196 register int start = rl_point, end;
2197 int state = 0;
2199 rl_forward_word (count);
2200 end = rl_point;
2202 if (count < 0)
2204 int temp = start;
2205 start = end;
2206 end = temp;
2209 /* We are going to modify some text, so let's prepare to undo it. */
2210 rl_modifying (start, end);
2212 for (; start < end; start++)
2214 switch (op)
2216 case UpCase:
2217 the_line[start] = to_upper (the_line[start]);
2218 break;
2220 case DownCase:
2221 the_line[start] = to_lower (the_line[start]);
2222 break;
2224 case CapCase:
2225 if (state == 0)
2227 the_line[start] = to_upper (the_line[start]);
2228 state = 1;
2230 else
2232 the_line[start] = to_lower (the_line[start]);
2234 if (!pure_alphabetic (the_line[start]))
2235 state = 0;
2236 break;
2238 default:
2239 abort ();
2240 return -1;
2243 rl_point = end;
2244 return 0;
2247 /* **************************************************************** */
2248 /* */
2249 /* Transposition */
2250 /* */
2251 /* **************************************************************** */
2253 /* Transpose the words at point. */
2254 rl_transpose_words (count)
2255 int count;
2257 char *word1, *word2;
2258 int w1_beg, w1_end, w2_beg, w2_end;
2259 int orig_point = rl_point;
2261 if (!count)
2262 return 0;
2264 /* Find the two words. */
2265 rl_forward_word (count);
2266 w2_end = rl_point;
2267 rl_backward_word (1);
2268 w2_beg = rl_point;
2269 rl_backward_word (count);
2270 w1_beg = rl_point;
2271 rl_forward_word (1);
2272 w1_end = rl_point;
2274 /* Do some check to make sure that there really are two words. */
2275 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2277 ding ();
2278 rl_point = orig_point;
2279 return -1;
2282 /* Get the text of the words. */
2283 word1 = rl_copy_text (w1_beg, w1_end);
2284 word2 = rl_copy_text (w2_beg, w2_end);
2286 /* We are about to do many insertions and deletions. Remember them
2287 as one operation. */
2288 rl_begin_undo_group ();
2290 /* Do the stuff at word2 first, so that we don't have to worry
2291 about word1 moving. */
2292 rl_point = w2_beg;
2293 rl_delete_text (w2_beg, w2_end);
2294 rl_insert_text (word1);
2296 rl_point = w1_beg;
2297 rl_delete_text (w1_beg, w1_end);
2298 rl_insert_text (word2);
2300 /* This is exactly correct since the text before this point has not
2301 changed in length. */
2302 rl_point = w2_end;
2304 /* I think that does it. */
2305 rl_end_undo_group ();
2306 free (word1);
2307 free (word2);
2309 return 0;
2312 /* Transpose the characters at point. If point is at the end of the line,
2313 then transpose the characters before point. */
2314 rl_transpose_chars (count)
2315 int count;
2317 char dummy[2];
2319 if (!count)
2320 return 0;
2322 if (!rl_point || rl_end < 2)
2324 ding ();
2325 return -1;
2328 rl_begin_undo_group ();
2330 if (rl_point == rl_end)
2332 --rl_point;
2333 count = 1;
2335 rl_point--;
2337 dummy[0] = the_line[rl_point];
2338 dummy[1] = '\0';
2340 rl_delete_text (rl_point, rl_point + 1);
2342 rl_point += count;
2343 if (rl_point > rl_end)
2344 rl_point = rl_end;
2345 else if (rl_point < 0)
2346 rl_point = 0;
2347 rl_insert_text (dummy);
2349 rl_end_undo_group ();
2350 return 0;
2353 /* **************************************************************** */
2354 /* */
2355 /* Undo, and Undoing */
2356 /* */
2357 /* **************************************************************** */
2359 /* The current undo list for THE_LINE. */
2360 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2362 /* Remember how to undo something. Concatenate some undos if that
2363 seems right. */
2364 void
2365 rl_add_undo (what, start, end, text)
2366 enum undo_code what;
2367 int start, end;
2368 char *text;
2370 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
2371 temp->what = what;
2372 temp->start = start;
2373 temp->end = end;
2374 temp->text = text;
2375 temp->next = rl_undo_list;
2376 rl_undo_list = temp;
2379 /* Free the existing undo list. */
2380 void
2381 free_undo_list ()
2383 while (rl_undo_list)
2385 UNDO_LIST *release = rl_undo_list;
2386 rl_undo_list = rl_undo_list->next;
2388 if (release->what == UNDO_DELETE)
2389 free (release->text);
2391 free (release);
2393 rl_undo_list = (UNDO_LIST *)NULL;
2396 /* Undo the next thing in the list. Return 0 if there
2397 is nothing to undo, or non-zero if there was. */
2399 rl_do_undo ()
2401 UNDO_LIST *release;
2402 int waiting_for_begin = 0;
2404 undo_thing:
2405 if (!rl_undo_list)
2406 return (0);
2408 doing_an_undo = 1;
2410 switch (rl_undo_list->what) {
2412 /* Undoing deletes means inserting some text. */
2413 case UNDO_DELETE:
2414 rl_point = rl_undo_list->start;
2415 rl_insert_text (rl_undo_list->text);
2416 free (rl_undo_list->text);
2417 break;
2419 /* Undoing inserts means deleting some text. */
2420 case UNDO_INSERT:
2421 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
2422 rl_point = rl_undo_list->start;
2423 break;
2425 /* Undoing an END means undoing everything 'til we get to
2426 a BEGIN. */
2427 case UNDO_END:
2428 waiting_for_begin++;
2429 break;
2431 /* Undoing a BEGIN means that we are done with this group. */
2432 case UNDO_BEGIN:
2433 if (waiting_for_begin)
2434 waiting_for_begin--;
2435 else
2436 #if 0
2437 abort ();
2438 #else
2439 ding ();
2440 #endif
2441 break;
2444 doing_an_undo = 0;
2446 release = rl_undo_list;
2447 rl_undo_list = rl_undo_list->next;
2448 free (release);
2450 if (waiting_for_begin)
2451 goto undo_thing;
2453 return (1);
2456 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2457 rl_begin_undo_group ()
2459 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2460 return 0;
2463 /* End an undo group started with rl_begin_undo_group (). */
2464 rl_end_undo_group ()
2466 rl_add_undo (UNDO_END, 0, 0, 0);
2467 return 0;
2470 /* Save an undo entry for the text from START to END. */
2471 rl_modifying (start, end)
2472 int start, end;
2474 if (start > end)
2476 int t = start;
2477 start = end;
2478 end = t;
2481 if (start != end)
2483 char *temp = rl_copy_text (start, end);
2484 rl_begin_undo_group ();
2485 rl_add_undo (UNDO_DELETE, start, end, temp);
2486 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
2487 rl_end_undo_group ();
2489 return 0;
2492 /* Revert the current line to its previous state. */
2493 rl_revert_line ()
2495 if (!rl_undo_list)
2496 ding ();
2497 else
2499 while (rl_undo_list)
2500 rl_do_undo ();
2502 return 0;
2505 /* Do some undoing of things that were done. */
2506 rl_undo_command (count)
2507 int count;
2509 if (count < 0)
2510 return 0; /* Nothing to do. */
2512 while (count)
2514 if (rl_do_undo ())
2515 count--;
2516 else
2518 ding ();
2519 break;
2522 return 0;
2525 /* **************************************************************** */
2526 /* */
2527 /* History Utilities */
2528 /* */
2529 /* **************************************************************** */
2531 /* We already have a history library, and that is what we use to control
2532 the history features of readline. However, this is our local interface
2533 to the history mechanism. */
2535 /* While we are editing the history, this is the saved
2536 version of the original line. */
2537 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
2539 /* Set the history pointer back to the last entry in the history. */
2540 start_using_history ()
2542 using_history ();
2543 if (saved_line_for_history)
2544 free_history_entry (saved_line_for_history);
2546 saved_line_for_history = (HIST_ENTRY *)NULL;
2547 return 0;
2550 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2551 void
2552 free_history_entry (entry)
2553 HIST_ENTRY *entry;
2555 if (!entry)
2556 return;
2557 if (entry->line)
2558 free (entry->line);
2559 free (entry);
2562 /* Perhaps put back the current line if it has changed. */
2563 maybe_replace_line ()
2565 HIST_ENTRY *temp = current_history ();
2567 /* If the current line has changed, save the changes. */
2568 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2570 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2571 free (temp->line);
2572 free (temp);
2574 return 0;
2577 /* Put back the saved_line_for_history if there is one. */
2578 maybe_unsave_line ()
2580 if (saved_line_for_history)
2582 int line_len;
2584 line_len = strlen (saved_line_for_history->line);
2586 if (line_len >= rl_line_buffer_len)
2587 rl_extend_line_buffer (line_len);
2589 strcpy (the_line, saved_line_for_history->line);
2590 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
2591 free_history_entry (saved_line_for_history);
2592 saved_line_for_history = (HIST_ENTRY *)NULL;
2593 rl_end = rl_point = strlen (the_line);
2595 else
2596 ding ();
2597 return 0;
2600 /* Save the current line in saved_line_for_history. */
2601 maybe_save_line ()
2603 if (!saved_line_for_history)
2605 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
2606 saved_line_for_history->line = savestring (the_line);
2607 saved_line_for_history->data = (char *)rl_undo_list;
2609 return 0;
2612 /* **************************************************************** */
2613 /* */
2614 /* History Commands */
2615 /* */
2616 /* **************************************************************** */
2618 /* Meta-< goes to the start of the history. */
2619 rl_beginning_of_history ()
2621 return (rl_get_previous_history (1 + where_history ()));
2624 /* Meta-> goes to the end of the history. (The current line). */
2625 rl_end_of_history ()
2627 maybe_replace_line ();
2628 using_history ();
2629 maybe_unsave_line ();
2630 return 0;
2633 /* Move down to the next history line. */
2634 rl_get_next_history (count)
2635 int count;
2637 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2639 if (count < 0)
2640 return (rl_get_previous_history (-count));
2642 if (!count)
2643 return 0;
2645 maybe_replace_line ();
2647 while (count)
2649 temp = next_history ();
2650 if (!temp)
2651 break;
2652 --count;
2655 if (!temp)
2656 maybe_unsave_line ();
2657 else
2659 int line_len;
2661 line_len = strlen (temp->line);
2663 if (line_len >= rl_line_buffer_len)
2664 rl_extend_line_buffer (line_len);
2666 strcpy (the_line, temp->line);
2667 rl_undo_list = (UNDO_LIST *)temp->data;
2668 rl_end = rl_point = strlen (the_line);
2669 #if defined (VI_MODE)
2670 if (rl_editing_mode == vi_mode)
2671 rl_point = 0;
2672 #endif /* VI_MODE */
2674 return 0;
2677 /* Get the previous item out of our interactive history, making it the current
2678 line. If there is no previous history, just ding. */
2679 rl_get_previous_history (count)
2680 int count;
2682 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2683 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2685 if (count < 0)
2686 return (rl_get_next_history (-count));
2688 if (!count)
2689 return 0;
2691 /* If we don't have a line saved, then save this one. */
2692 maybe_save_line ();
2694 /* If the current line has changed, save the changes. */
2695 maybe_replace_line ();
2697 while (count)
2699 temp = previous_history ();
2700 if (!temp)
2701 break;
2702 else
2703 old_temp = temp;
2704 --count;
2707 /* If there was a large argument, and we moved back to the start of the
2708 history, that is not an error. So use the last value found. */
2709 if (!temp && old_temp)
2710 temp = old_temp;
2712 if (!temp)
2713 ding ();
2714 else
2716 int line_len;
2718 line_len = strlen (temp->line);
2720 if (line_len >= rl_line_buffer_len)
2721 rl_extend_line_buffer (line_len);
2723 strcpy (the_line, temp->line);
2724 rl_undo_list = (UNDO_LIST *)temp->data;
2725 rl_end = rl_point = line_len;
2727 #if defined (VI_MODE)
2728 if (rl_editing_mode == vi_mode)
2729 rl_point = 0;
2730 #endif /* VI_MODE */
2732 return 0;
2735 /* Make C be the next command to be executed. */
2736 rl_execute_next (c)
2737 int c;
2739 rl_pending_input = c;
2740 return 0;
2743 /* **************************************************************** */
2744 /* */
2745 /* The Mark and the Region. */
2746 /* */
2747 /* **************************************************************** */
2749 /* Set the mark at POSITION. */
2750 rl_set_mark (position)
2751 int position;
2753 if (position > rl_end)
2754 return -1;
2756 rl_mark = position;
2757 return 0;
2760 /* Exchange the position of mark and point. */
2761 rl_exchange_mark_and_point ()
2763 if (rl_mark > rl_end)
2764 rl_mark = -1;
2766 if (rl_mark == -1)
2768 ding ();
2769 return -1;
2771 else
2773 int temp = rl_point;
2775 rl_point = rl_mark;
2776 rl_mark = temp;
2778 return 0;
2782 /* **************************************************************** */
2783 /* */
2784 /* Killing Mechanism */
2785 /* */
2786 /* **************************************************************** */
2788 /* What we assume for a max number of kills. */
2789 #define DEFAULT_MAX_KILLS 10
2791 /* The real variable to look at to find out when to flush kills. */
2792 int rl_max_kills = DEFAULT_MAX_KILLS;
2794 /* Where to store killed text. */
2795 char **rl_kill_ring = (char **)NULL;
2797 /* Where we are in the kill ring. */
2798 int rl_kill_index = 0;
2800 /* How many slots we have in the kill ring. */
2801 int rl_kill_ring_length = 0;
2803 /* How to say that you only want to save a certain amount
2804 of kill material. */
2805 rl_set_retained_kills (num)
2806 int num;
2808 return 0;
2811 /* The way to kill something. This appends or prepends to the last
2812 kill, if the last command was a kill command. if FROM is less
2813 than TO, then the text is appended, otherwise prepended. If the
2814 last command was not a kill command, then a new slot is made for
2815 this kill. */
2816 rl_kill_text (from, to)
2817 int from, to;
2819 int slot;
2820 char *text = rl_copy_text (from, to);
2822 /* Is there anything to kill? */
2823 if (from == to)
2825 free (text);
2826 last_command_was_kill++;
2827 return 0;
2830 /* Delete the copied text from the line. */
2831 rl_delete_text (from, to);
2833 /* First, find the slot to work with. */
2834 if (!last_command_was_kill)
2836 /* Get a new slot. */
2837 if (!rl_kill_ring)
2839 /* If we don't have any defined, then make one. */
2840 rl_kill_ring = (char **)
2841 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
2842 slot = 1;
2844 else
2846 /* We have to add a new slot on the end, unless we have
2847 exceeded the max limit for remembering kills. */
2848 slot = rl_kill_ring_length;
2849 if (slot == rl_max_kills)
2851 register int i;
2852 free (rl_kill_ring[0]);
2853 for (i = 0; i < slot; i++)
2854 rl_kill_ring[i] = rl_kill_ring[i + 1];
2856 else
2858 rl_kill_ring =
2859 (char **)
2860 xrealloc (rl_kill_ring,
2861 ((slot = (rl_kill_ring_length += 1)) + 1)
2862 * sizeof (char *));
2865 slot--;
2867 else
2869 slot = rl_kill_ring_length - 1;
2872 /* If the last command was a kill, prepend or append. */
2873 if (last_command_was_kill && rl_editing_mode != vi_mode)
2875 char *old = rl_kill_ring[slot];
2876 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
2878 if (from < to)
2880 strcpy (new, old);
2881 strcat (new, text);
2883 else
2885 strcpy (new, text);
2886 strcat (new, old);
2888 free (old);
2889 free (text);
2890 rl_kill_ring[slot] = new;
2892 else
2894 rl_kill_ring[slot] = text;
2896 rl_kill_index = slot;
2897 last_command_was_kill++;
2898 return 0;
2901 /* Now REMEMBER! In order to do prepending or appending correctly, kill
2902 commands always make rl_point's original position be the FROM argument,
2903 and rl_point's extent be the TO argument. */
2905 /* **************************************************************** */
2906 /* */
2907 /* Killing Commands */
2908 /* */
2909 /* **************************************************************** */
2911 /* Delete the word at point, saving the text in the kill ring. */
2912 rl_kill_word (count)
2913 int count;
2915 int orig_point = rl_point;
2917 if (count < 0)
2918 return (rl_backward_kill_word (-count));
2919 else
2921 rl_forward_word (count);
2923 if (rl_point != orig_point)
2924 rl_kill_text (orig_point, rl_point);
2926 rl_point = orig_point;
2928 return 0;
2931 /* Rubout the word before point, placing it on the kill ring. */
2932 rl_backward_kill_word (count)
2933 int count;
2935 int orig_point = rl_point;
2937 if (count < 0)
2938 return (rl_kill_word (-count));
2939 else
2941 rl_backward_word (count);
2943 if (rl_point != orig_point)
2944 rl_kill_text (orig_point, rl_point);
2948 /* Kill from here to the end of the line. If DIRECTION is negative, kill
2949 back to the line start instead. */
2950 rl_kill_line (direction)
2951 int direction;
2953 int orig_point = rl_point;
2955 if (direction < 0)
2956 return (rl_backward_kill_line (1));
2957 else
2959 rl_end_of_line ();
2960 if (orig_point != rl_point)
2961 rl_kill_text (orig_point, rl_point);
2962 rl_point = orig_point;
2964 return 0;
2967 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
2968 forwards to the line end instead. */
2969 rl_backward_kill_line (direction)
2970 int direction;
2972 int orig_point = rl_point;
2974 if (direction < 0)
2975 return (rl_kill_line (1));
2976 else
2978 if (!rl_point)
2979 ding ();
2980 else
2982 rl_beg_of_line ();
2983 rl_kill_text (orig_point, rl_point);
2986 return 0;
2989 /* Yank back the last killed text. This ignores arguments. */
2990 rl_yank ()
2992 if (!rl_kill_ring)
2994 rl_abort ();
2995 return -1;
2998 rl_set_mark (rl_point);
2999 rl_insert_text (rl_kill_ring[rl_kill_index]);
3000 return 0;
3003 /* If the last command was yank, or yank_pop, and the text just
3004 before point is identical to the current kill item, then
3005 delete that text from the line, rotate the index down, and
3006 yank back some other text. */
3007 rl_yank_pop ()
3009 int l;
3011 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
3012 !rl_kill_ring)
3014 rl_abort ();
3015 return -1;
3018 l = strlen (rl_kill_ring[rl_kill_index]);
3019 if (((rl_point - l) >= 0) &&
3020 (strncmp (the_line + (rl_point - l),
3021 rl_kill_ring[rl_kill_index], l) == 0))
3023 rl_delete_text ((rl_point - l), rl_point);
3024 rl_point -= l;
3025 rl_kill_index--;
3026 if (rl_kill_index < 0)
3027 rl_kill_index = rl_kill_ring_length - 1;
3028 rl_yank ();
3029 return 0;
3031 else
3033 rl_abort ();
3034 return -1;
3038 /* Yank the COUNTth argument from the previous history line. */
3039 rl_yank_nth_arg (count, ignore)
3040 int count;
3042 register HIST_ENTRY *entry = previous_history ();
3043 char *arg;
3045 if (entry)
3046 next_history ();
3047 else
3049 ding ();
3050 return -1;
3053 arg = history_arg_extract (count, count, entry->line);
3054 if (!arg || !*arg)
3056 ding ();
3057 return -1;
3060 rl_begin_undo_group ();
3062 #if defined (VI_MODE)
3063 /* Vi mode always inserts a space before yanking the argument, and it
3064 inserts it right *after* rl_point. */
3065 if (rl_editing_mode == vi_mode)
3066 rl_point++;
3067 #endif /* VI_MODE */
3069 #if 0
3070 if (rl_point && the_line[rl_point - 1] != ' ')
3071 rl_insert_text (" ");
3072 #endif
3074 rl_insert_text (arg);
3075 free (arg);
3077 rl_end_undo_group ();
3078 return 0;
3081 /* How to toggle back and forth between editing modes. */
3082 rl_vi_editing_mode ()
3084 #if defined (VI_MODE)
3085 rl_editing_mode = vi_mode;
3086 rl_vi_insertion_mode ();
3087 return 0;
3088 #endif /* VI_MODE */
3091 rl_emacs_editing_mode ()
3093 rl_editing_mode = emacs_mode;
3094 _rl_keymap = emacs_standard_keymap;
3095 return 0;
3099 /* **************************************************************** */
3100 /* */
3101 /* USG (System V) Support */
3102 /* */
3103 /* **************************************************************** */
3106 rl_getc (stream)
3107 FILE *stream;
3109 int result;
3110 unsigned char c;
3112 #if defined (__GO32__)
3113 if (isatty (0))
3114 return (getkey ());
3115 #endif /* __GO32__ */
3117 while (1)
3119 result = read (fileno (stream), &c, sizeof (unsigned char));
3121 if (result == sizeof (unsigned char))
3122 return (c);
3124 /* If zero characters are returned, then the file that we are
3125 reading from is empty! Return EOF in that case. */
3126 if (result == 0)
3127 return (EOF);
3129 #if defined (EWOULDBLOCK)
3130 if (errno == EWOULDBLOCK)
3132 int flags;
3134 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3135 return (EOF);
3136 if (flags & O_NDELAY)
3138 flags &= ~O_NDELAY;
3139 fcntl (fileno (stream), F_SETFL, flags);
3140 continue;
3142 continue;
3144 #endif /* EWOULDBLOCK */
3146 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3147 if (errno == EAGAIN)
3149 int flags;
3151 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3152 return (EOF);
3153 if (flags & O_NONBLOCK)
3155 flags &= ~O_NONBLOCK;
3156 fcntl (fileno (stream), F_SETFL, flags);
3157 continue;
3160 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3162 #if !defined (__GO32__)
3163 /* If the error that we received was SIGINT, then try again,
3164 this is simply an interrupted system call to read ().
3165 Otherwise, some error ocurred, also signifying EOF. */
3166 if (errno != EINTR)
3167 return (EOF);
3168 #endif /* !__GO32__ */
3172 char *
3173 _rl_savestring (str)
3174 char *str;
3176 char *copy = (char*) xmalloc (strlen (str) + 1);
3177 strcpy (copy, str);
3178 return copy;
3181 #if defined (STATIC_MALLOC)
3183 /* **************************************************************** */
3184 /* */
3185 /* xmalloc and xrealloc () */
3186 /* */
3187 /* **************************************************************** */
3189 static void memory_error_and_abort ();
3191 static char *
3192 xmalloc (bytes)
3193 int bytes;
3195 char *temp = (char *)malloc (bytes);
3197 if (!temp)
3198 memory_error_and_abort ();
3199 return (temp);
3202 static char *
3203 xrealloc (pointer, bytes)
3204 char *pointer;
3205 int bytes;
3207 char *temp;
3209 if (!pointer)
3210 temp = (char *)malloc (bytes);
3211 else
3212 temp = (char *)realloc (pointer, bytes);
3214 if (!temp)
3215 memory_error_and_abort ();
3217 return (temp);
3220 static void
3221 memory_error_and_abort ()
3223 fprintf (stderr, "readline: Out of virtual memory!\n");
3224 abort ();
3226 #endif /* STATIC_MALLOC */
3229 /* **************************************************************** */
3230 /* */
3231 /* Testing Readline */
3232 /* */
3233 /* **************************************************************** */
3235 #if defined (TEST)
3237 main ()
3239 HIST_ENTRY **history_list ();
3240 char *temp = (char *)NULL;
3241 char *prompt = "readline% ";
3242 int done = 0;
3244 while (!done)
3246 temp = readline (prompt);
3248 /* Test for EOF. */
3249 if (!temp)
3250 exit (1);
3252 /* If there is anything on the line, print it and remember it. */
3253 if (*temp)
3255 fprintf (stderr, "%s\r\n", temp);
3256 add_history (temp);
3259 /* Check for `command' that we handle. */
3260 if (strcmp (temp, "quit") == 0)
3261 done = 1;
3263 if (strcmp (temp, "list") == 0)
3265 HIST_ENTRY **list = history_list ();
3266 register int i;
3267 if (list)
3269 for (i = 0; list[i]; i++)
3271 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3272 free (list[i]->line);
3274 free (list);
3277 free (temp);
3281 #endif /* TEST */
3285 * Local variables:
3286 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
3287 * end: