Debian Codegraph @

libsrc/readline/readline.c

raw
1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
13
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #if !defined (NO_SYS_FILE)
29 # include <sys/file.h>
30 #endif /* !NO_SYS_FILE */
31 #include <signal.h>
32
33 #if defined (HAVE_UNISTD_H)
34 # include <unistd.h>
35 #endif /* HAVE_UNISTD_H */
36
37 #if defined (HAVE_STDLIB_H)
38 # include <stdlib.h>
39 #else
40 # include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
42
43 #include <errno.h>
44 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
45 #if !defined (errno)
46 extern int errno;
47 #endif /* !errno */
48
49 #include <setjmp.h>
50
51 #include "posixstat.h"
52
53 /* System-specific feature definitions and include files. */
54 #include "rldefs.h"
55
56 #if defined (WINSIZE_IN_IOCTL_H)
57 # include <sys/ioctl.h>
58 #endif /* WINSIZE_IN_IOCTL_H */
59
60 /* Some standard library routines. */
61 #include "readline.h"
62 #include "history.h"
63
64 /* NOTE: Functions and variables prefixed with `_rl_' are
65 pseudo-global: they are global so they can be shared
66 between files in the readline library, but are not intended
67 to be visible to readline callers. */
68
69 /* Functions imported from other files in the library. */
70 extern char *tgetstr ();
71 extern void rl_prep_terminal (), rl_deprep_terminal ();
72
73 extern void _rl_bind_if_unbound ();
74
75 /* External redisplay functions and variables from display.c */
76 extern void _rl_move_vert ();
77
78 extern void _rl_erase_at_end_of_line ();
79 extern void _rl_move_cursor_relative ();
80
81 extern int _rl_vis_botlin;
82 extern int _rl_last_c_pos;
83 extern int rl_display_fixed;
84 extern char *rl_display_prompt;
85
86 /* Variables imported from complete.c. */
87 extern char *rl_completer_word_break_characters;
88 extern char *rl_basic_word_break_characters;
89 extern int rl_completion_query_items;
90 extern int rl_complete_with_tilde_expansion;
91
92 #if defined (VI_MODE)
93 extern void _rl_vi_set_last ();
94 extern void _rl_vi_reset_last ();
95 extern void _rl_vi_done_inserting ();
96 #endif /* VI_MODE */
97
98 /* Forward declarations used in this file. */
99 void _rl_free_history_entry ();
100
101 int _rl_dispatch ();
102 void _rl_set_screen_size ();
103 int _rl_output_character_function ();
104
105 static char *readline_internal ();
106 static void readline_initialize_everything ();
107 static int init_terminal_io ();
108 static void start_using_history ();
109
110 #if !defined (__GO32__)
111 static void readline_default_bindings ();
112 #endif /* !__GO32__ */
113
114 #if defined (__GO32__)
115 # include <sys/pc.h>
116 # undef HANDLE_SIGNALS
117 #endif /* __GO32__ */
118
119 #if defined (STATIC_MALLOC)
120 static char *xmalloc (), *xrealloc ();
121 #else
122 extern char *xmalloc (), *xrealloc ();
123 #endif /* STATIC_MALLOC */
124
125
126 /* **************************************************************** */
127 /* */
128 /* Line editing input utility */
129 /* */
130 /* **************************************************************** */
131
132 static char *LibraryVersion = "2.0";
133
134 /* A pointer to the keymap that is currently in use.
135 By default, it is the standard emacs keymap. */
136 Keymap _rl_keymap = emacs_standard_keymap;
137
138 /* The current style of editing. */
139 int rl_editing_mode = emacs_mode;
140
141 /* Non-zero if the previous command was a kill command. */
142 static int last_command_was_kill = 0;
143
144 /* The current value of the numeric argument specified by the user. */
145 int rl_numeric_arg = 1;
146
147 /* Non-zero if an argument was typed. */
148 int rl_explicit_arg = 0;
149
150 /* Temporary value used while generating the argument. */
151 int rl_arg_sign = 1;
152
153 /* Non-zero means we have been called at least once before. */
154 static int rl_initialized = 0;
155
156 /* If non-zero, this program is running in an EMACS buffer. */
157 static int running_in_emacs = 0;
158
159 /* The current offset in the current input line. */
160 int rl_point;
161
162 /* Mark in the current input line. */
163 int rl_mark;
164
165 /* Length of the current input line. */
166 int rl_end;
167
168 /* Make this non-zero to return the current input_line. */
169 int rl_done;
170
171 /* The last function executed by readline. */
172 Function *rl_last_func = (Function *)NULL;
173
174 /* Top level environment for readline_internal (). */
175 static jmp_buf readline_top_level;
176
177 /* The streams we interact with. */
178 static FILE *in_stream, *out_stream;
179
180 /* The names of the streams that we do input and output to. */
181 FILE *rl_instream = (FILE *)NULL;
182 FILE *rl_outstream = (FILE *)NULL;
183
184 /* Non-zero means echo characters as they are read. */
185 int readline_echoing_p = 1;
186
187 /* Current prompt. */
188 char *rl_prompt;
189 int rl_visible_prompt_length = 0;
190
191 /* The number of characters read in order to type this complete command. */
192 int rl_key_sequence_length = 0;
193
194 /* If non-zero, then this is the address of a function to call just
195 before readline_internal () prints the first prompt. */
196 Function *rl_startup_hook = (Function *)NULL;
197
198 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
199 static char *the_line;
200
201 /* The character that can generate an EOF. Really read from
202 the terminal driver... just defaulted here. */
203 int _rl_eof_char = CTRL ('D');
204
205 /* Non-zero makes this the next keystroke to read. */
206 int rl_pending_input = 0;
207
208 /* Pointer to a useful terminal name. */
209 char *rl_terminal_name = (char *)NULL;
210
211 /* Non-zero means to always use horizontal scrolling in line display. */
212 int _rl_horizontal_scroll_mode = 0;
213
214 /* Non-zero means to display an asterisk at the starts of history lines
215 which have been modified. */
216 int _rl_mark_modified_lines = 0;
217
218 /* The style of `bell' notification preferred. This can be set to NO_BELL,
219 AUDIBLE_BELL, or VISIBLE_BELL. */
220 int _rl_bell_preference = AUDIBLE_BELL;
221
222 /* Line buffer and maintenence. */
223 char *rl_line_buffer = (char *)NULL;
224 int rl_line_buffer_len = 0;
225 #define DEFAULT_BUFFER_SIZE 256
226
227
228 /* **************************************************************** */
229 /* */
230 /* `Forward' declarations */
231 /* */
232 /* **************************************************************** */
233
234 /* Non-zero means do not parse any lines other than comments and
235 parser directives. */
236 unsigned char _rl_parsing_conditionalized_out = 0;
237
238 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
239 static int defining_kbd_macro = 0;
240
241 /* Non-zero means to convert characters with the meta bit set to
242 escape-prefixed characters so we can indirect through
243 emacs_meta_keymap or vi_escape_keymap. */
244 int _rl_convert_meta_chars_to_ascii = 1;
245
246 /* Non-zero means to output characters with the meta bit set directly
247 rather than as a meta-prefixed escape sequence. */
248 int _rl_output_meta_chars = 0;
249
250 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
251 the undo list. */
252 static int doing_an_undo = 0;
253
254 /* **************************************************************** */
255 /* */
256 /* Top Level Functions */
257 /* */
258 /* **************************************************************** */
259
260 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
261 int _rl_meta_flag = 0; /* Forward declaration */
262
263 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
264 none. A return value of NULL means that EOF was encountered. */
265 char *
266 readline (prompt)
267 char *prompt;
268 {
269 char *value;
270
271 rl_prompt = prompt;
272
273 /* If we are at EOF return a NULL string. */
274 if (rl_pending_input == EOF)
275 {
276 rl_pending_input = 0;
277 return ((char *)NULL);
278 }
279
280 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
281
282 rl_initialize ();
283 rl_prep_terminal (_rl_meta_flag);
284
285 #if defined (HANDLE_SIGNALS)
286 rl_set_signals ();
287 #endif
288
289 value = readline_internal ();
290 rl_deprep_terminal ();
291
292 #if defined (HANDLE_SIGNALS)
293 rl_clear_signals ();
294 #endif
295
296 return (value);
297 }
298
299 /* Read a line of input from the global rl_instream, doing output on
300 the global rl_outstream.
301 If rl_prompt is non-null, then that is our prompt. */
302 static char *
303 readline_internal ()
304 {
305 int lastc, c, eof_found;
306
307 in_stream = rl_instream;
308 out_stream = rl_outstream;
309
310 lastc = -1;
311 eof_found = 0;
312
313 if (rl_startup_hook)
314 (*rl_startup_hook) ();
315
316 if (!readline_echoing_p)
317 {
318 if (rl_prompt)
319 {
320 fprintf (out_stream, "%s", rl_prompt);
321 fflush (out_stream);
322 }
323 }
324 else
325 {
326 rl_on_new_line ();
327 rl_redisplay ();
328 #if defined (VI_MODE)
329 if (rl_editing_mode == vi_mode)
330 rl_vi_insertion_mode ();
331 #endif /* VI_MODE */
332 }
333
334 while (!rl_done)
335 {
336 int lk = last_command_was_kill;
337 int code;
338
339 code = setjmp (readline_top_level);
340
341 if (code)
342 rl_redisplay ();
343
344 if (!rl_pending_input)
345 {
346 /* Then initialize the argument and number of keys read. */
347 rl_init_argument ();
348 rl_key_sequence_length = 0;
349 }
350
351 c = rl_read_key ();
352
353 /* EOF typed to a non-blank line is a <NL>. */
354 if (c == EOF && rl_end)
355 c = NEWLINE;
356
357 /* The character _rl_eof_char typed to blank line, and not as the
358 previous character is interpreted as EOF. */
359 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
360 {
361 eof_found = 1;
362 break;
363 }
364
365 lastc = c;
366 _rl_dispatch (c, _rl_keymap);
367
368 /* If there was no change in last_command_was_kill, then no kill
369 has taken place. Note that if input is pending we are reading
370 a prefix command, so nothing has changed yet. */
371 if (!rl_pending_input)
372 {
373 if (lk == last_command_was_kill)
374 last_command_was_kill = 0;
375 }
376
377 #if defined (VI_MODE)
378 /* In vi mode, when you exit insert mode, the cursor moves back
379 over the previous character. We explicitly check for that here. */
380 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
381 rl_vi_check ();
382 #endif /* VI_MODE */
383
384 if (!rl_done)
385 rl_redisplay ();
386 }
387
388 /* Restore the original of this history line, iff the line that we
389 are editing was originally in the history, AND the line has changed. */
390 {
391 HIST_ENTRY *entry = current_history ();
392
393 if (entry && rl_undo_list)
394 {
395 char *temp = savestring (the_line);
396 rl_revert_line ();
397 entry = replace_history_entry (where_history (), the_line,
398 (HIST_ENTRY *)NULL);
399 _rl_free_history_entry (entry);
400
401 strcpy (the_line, temp);
402 free (temp);
403 }
404 }
405
406 /* At any rate, it is highly likely that this line has an undo list. Get
407 rid of it now. */
408 if (rl_undo_list)
409 free_undo_list ();
410
411 if (eof_found)
412 return (char *)NULL;
413 else
414 return (savestring (the_line));
415 }
416
417 /* **************************************************************** */
418 /* */
419 /* Character Input Buffering */
420 /* */
421 /* **************************************************************** */
422
423 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
424 static unsigned char ibuffer[512];
425
426 /* Non-null means it is a pointer to a function to run while waiting for
427 character input. */
428 Function *rl_event_hook = (Function *)NULL;
429
430 #define any_typein (push_index != pop_index)
431
432 /* Add KEY to the buffer of characters to be read. */
433 rl_stuff_char (key)
434 int key;
435 {
436 if (key == EOF)
437 {
438 key = NEWLINE;
439 rl_pending_input = EOF;
440 }
441 ibuffer[push_index++] = key;
442 if (push_index >= ibuffer_len)
443 push_index = 0;
444 return push_index;
445 }
446
447 /* Return the amount of space available in the
448 buffer for stuffing characters. */
449 int
450 ibuffer_space ()
451 {
452 if (pop_index > push_index)
453 return (pop_index - push_index);
454 else
455 return (ibuffer_len - (push_index - pop_index));
456 }
457
458 /* Get a key from the buffer of characters to be read.
459 Return the key in KEY.
460 Result is KEY if there was a key, or 0 if there wasn't. */
461 int
462 rl_get_char (key)
463 int *key;
464 {
465 if (push_index == pop_index)
466 return (0);
467
468 *key = ibuffer[pop_index++];
469
470 if (pop_index >= ibuffer_len)
471 pop_index = 0;
472
473 return (1);
474 }
475
476 /* Stuff KEY into the *front* of the input buffer.
477 Returns non-zero if successful, zero if there is
478 no space left in the buffer. */
479 int
480 rl_unget_char (key)
481 int key;
482 {
483 if (ibuffer_space ())
484 {
485 pop_index--;
486 if (pop_index < 0)
487 pop_index = ibuffer_len - 1;
488 ibuffer[pop_index] = key;
489 return (1);
490 }
491 return (0);
492 }
493
494 /* If a character is available to be read, then read it
495 and stuff it into IBUFFER. Otherwise, just return. */
496 void
497 rl_gather_tyi ()
498 {
499 #if defined (__GO32__)
500 char input;
501
502 if (isatty (0))
503 {
504 int i = rl_getc ();
505
506 if (i != EOF)
507 rl_stuff_char (i);
508 }
509 else if (kbhit () && ibuffer_space ())
510 rl_stuff_char (getkey ());
511 #else /* !__GO32__ */
512
513 int tty = fileno (in_stream);
514 register int tem, result = -1;
515 int chars_avail;
516 char input;
517
518 #if defined (FIONREAD)
519 result = ioctl (tty, FIONREAD, &chars_avail);
520 #endif
521
522 #if defined (O_NDELAY)
523 if (result == -1)
524 {
525 int flags;
526
527 flags = fcntl (tty, F_GETFL, 0);
528
529 fcntl (tty, F_SETFL, (flags | O_NDELAY));
530 chars_avail = read (tty, &input, 1);
531
532 fcntl (tty, F_SETFL, flags);
533 if (chars_avail == -1 && errno == EAGAIN)
534 return;
535 }
536 #endif /* O_NDELAY */
537
538 /* If there's nothing available, don't waste time trying to read
539 something. */
540 if (chars_avail == 0)
541 return;
542
543 tem = ibuffer_space ();
544
545 if (chars_avail > tem)
546 chars_avail = tem;
547
548 /* One cannot read all of the available input. I can only read a single
549 character at a time, or else programs which require input can be
550 thwarted. If the buffer is larger than one character, I lose.
551 Damn! */
552 if (tem < ibuffer_len)
553 chars_avail = 0;
554
555 if (result != -1)
556 {
557 while (chars_avail--)
558 rl_stuff_char (rl_getc (in_stream));
559 }
560 else
561 {
562 if (chars_avail)
563 rl_stuff_char (input);
564 }
565 #endif /* !__GO32__ */
566 }
567
568 static int next_macro_key ();
569 /* Read a key, including pending input. */
570 int
571 rl_read_key ()
572 {
573 int c;
574
575 rl_key_sequence_length++;
576
577 if (rl_pending_input)
578 {
579 c = rl_pending_input;
580 rl_pending_input = 0;
581 }
582 else
583 {
584 /* If input is coming from a macro, then use that. */
585 if (c = next_macro_key ())
586 return (c);
587
588 /* If the user has an event function, then call it periodically. */
589 if (rl_event_hook)
590 {
591 while (rl_event_hook && !rl_get_char (&c))
592 {
593 (*rl_event_hook) ();
594 rl_gather_tyi ();
595 }
596 }
597 else
598 {
599 if (!rl_get_char (&c))
600 c = rl_getc (in_stream);
601 }
602 }
603
604 return (c);
605 }
606
607 /* Found later in this file. */
608 static void add_macro_char (), with_macro_input ();
609
610 /* Do the command associated with KEY in MAP.
611 If the associated command is really a keymap, then read
612 another key, and dispatch into that map. */
613 int
614 _rl_dispatch (key, map)
615 register int key;
616 Keymap map;
617 {
618 int r = 0;
619
620 if (defining_kbd_macro)
621 add_macro_char (key);
622
623 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
624 {
625 if (map[ESC].type == ISKMAP)
626 {
627 map = FUNCTION_TO_KEYMAP (map, ESC);
628 key = UNMETA (key);
629 rl_key_sequence_length += 2;
630 return (_rl_dispatch (key, map));
631 }
632 else
633 ding ();
634 return 0;
635 }
636
637 switch (map[key].type)
638 {
639 case ISFUNC:
640 {
641 Function *func = map[key].function;
642
643 if (func != (Function *)NULL)
644 {
645 /* Special case rl_do_lowercase_version (). */
646 if (func == rl_do_lowercase_version)
647 return (_rl_dispatch (to_lower (key), map));
648
649 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
650
651 /* If we have input pending, then the last command was a prefix
652 command. Don't change the state of rl_last_func. Otherwise,
653 remember the last command executed in this variable. */
654 if (!rl_pending_input)
655 rl_last_func = map[key].function;
656 }
657 else
658 {
659 rl_abort ();
660 return -1;
661 }
662 }
663 break;
664
665 case ISKMAP:
666 if (map[key].function != (Function *)NULL)
667 {
668 int newkey;
669
670 rl_key_sequence_length++;
671 newkey = rl_read_key ();
672 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
673 }
674 else
675 {
676 rl_abort ();
677 return -1;
678 }
679 break;
680
681 case ISMACR:
682 if (map[key].function != (Function *)NULL)
683 {
684 char *macro;
685
686 macro = savestring ((char *)map[key].function);
687 with_macro_input (macro);
688 return 0;
689 }
690 break;
691 }
692 #if defined (VI_MODE)
693 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
694 rl_vi_textmod_command (key))
695 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
696 #endif
697 return (r);
698 }
699
700
701 /* **************************************************************** */
702 /* */
703 /* Hacking Keyboard Macros */
704 /* */
705 /* **************************************************************** */
706
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;
710
711 /* The offset in the above string to the next character to be read. */
712 static int executing_macro_index = 0;
713
714 /* The current macro string being built. Characters get stuffed
715 in here by add_macro_char (). */
716 static char *current_macro = (char *)NULL;
717
718 /* The size of the buffer allocated to current_macro. */
719 static int current_macro_size = 0;
720
721 /* The index at which characters are being added to current_macro. */
722 static int current_macro_index = 0;
723
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 sindex;
730 };
731
732 /* The list of saved macros. */
733 struct saved_macro *macro_list = (struct saved_macro *)NULL;
734
735 /* Forward declarations of static functions. Thank you C. */
736 static void push_executing_macro (), pop_executing_macro ();
737
738 /* This one has to be declared earlier in the file. */
739 /* static void add_macro_char (); */
740
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;
746 {
747 push_executing_macro ();
748 executing_macro = string;
749 executing_macro_index = 0;
750 }
751
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 ()
756 {
757 if (!executing_macro)
758 return (0);
759
760 if (!executing_macro[executing_macro_index])
761 {
762 pop_executing_macro ();
763 return (next_macro_key ());
764 }
765
766 return (executing_macro[executing_macro_index++]);
767 }
768
769 /* Save the currently executing macro on a stack of saved macros. */
770 static void
771 push_executing_macro ()
772 {
773 struct saved_macro *saver;
774
775 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
776 saver->next = macro_list;
777 saver->sindex = executing_macro_index;
778 saver->string = executing_macro;
779
780 macro_list = saver;
781 }
782
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 ()
787 {
788 if (executing_macro)
789 free (executing_macro);
790
791 executing_macro = (char *)NULL;
792 executing_macro_index = 0;
793
794 if (macro_list)
795 {
796 struct saved_macro *disposer = macro_list;
797 executing_macro = macro_list->string;
798 executing_macro_index = macro_list->sindex;
799 macro_list = macro_list->next;
800 free (disposer);
801 }
802 }
803
804 /* Add a character to the macro being built. */
805 static void
806 add_macro_char (c)
807 int c;
808 {
809 if (current_macro_index + 1 >= current_macro_size)
810 {
811 if (!current_macro)
812 current_macro = xmalloc (current_macro_size = 25);
813 else
814 current_macro = xrealloc (current_macro, current_macro_size += 25);
815 }
816
817 current_macro[current_macro_index++] = c;
818 current_macro[current_macro_index] = '\0';
819 }
820
821 /* Begin defining a keyboard macro.
822 Keystrokes are recorded as they are executed.
823 End the definition with rl_end_kbd_macro ().
824 If a numeric argument was explicitly typed, then append this
825 definition to the end of the existing macro, and start by
826 re-executing the existing macro. */
827 rl_start_kbd_macro (ignore1, ignore2)
828 int ignore1, ignore2;
829 {
830 if (defining_kbd_macro)
831 {
832 rl_abort ();
833 return -1;
834 }
835
836 if (rl_explicit_arg)
837 {
838 if (current_macro)
839 with_macro_input (savestring (current_macro));
840 }
841 else
842 current_macro_index = 0;
843
844 defining_kbd_macro = 1;
845 return 0;
846 }
847
848 /* Stop defining a keyboard macro.
849 A numeric argument says to execute the macro right now,
850 that many times, counting the definition as the first time. */
851 rl_end_kbd_macro (count, ignore)
852 int count, ignore;
853 {
854 if (!defining_kbd_macro)
855 {
856 rl_abort ();
857 return -1;
858 }
859
860 current_macro_index -= (rl_key_sequence_length - 1);
861 current_macro[current_macro_index] = '\0';
862
863 defining_kbd_macro = 0;
864
865 return (rl_call_last_kbd_macro (--count, 0));
866 }
867
868 /* Execute the most recently defined keyboard macro.
869 COUNT says how many times to execute it. */
870 rl_call_last_kbd_macro (count, ignore)
871 int count, ignore;
872 {
873 if (!current_macro)
874 rl_abort ();
875
876 if (defining_kbd_macro)
877 {
878 ding (); /* no recursive macros */
879 current_macro[--current_macro_index] = '\0'; /* erase this char */
880 return 0;
881 }
882
883 while (count--)
884 with_macro_input (savestring (current_macro));
885 return 0;
886 }
887
888 void
889 _rl_kill_kbd_macro ()
890 {
891 if (current_macro)
892 {
893 free (current_macro);
894 current_macro = (char *) NULL;
895 }
896 current_macro_size = current_macro_index = 0;
897
898 if (executing_macro)
899 {
900 free (executing_macro);
901 executing_macro = (char *) NULL;
902 }
903 executing_macro_index = 0;
904
905 defining_kbd_macro = 0;
906 }
907
908 /* **************************************************************** */
909 /* */
910 /* Initializations */
911 /* */
912 /* **************************************************************** */
913
914 /* Initliaze readline (and terminal if not already). */
915 rl_initialize ()
916 {
917 /* If we have never been called before, initialize the
918 terminal and data structures. */
919 if (!rl_initialized)
920 {
921 readline_initialize_everything ();
922 rl_initialized++;
923 }
924
925 /* Initalize the current line information. */
926 rl_point = rl_end = 0;
927 the_line = rl_line_buffer;
928 the_line[0] = 0;
929
930 /* We aren't done yet. We haven't even gotten started yet! */
931 rl_done = 0;
932
933 /* Tell the history routines what is going on. */
934 start_using_history ();
935
936 /* Make the display buffer match the state of the line. */
937 rl_reset_line_state ();
938
939 /* No such function typed yet. */
940 rl_last_func = (Function *)NULL;
941
942 /* Parsing of key-bindings begins in an enabled state. */
943 _rl_parsing_conditionalized_out = 0;
944
945 return 0;
946 }
947
948 /* Initialize the entire state of the world. */
949 static void
950 readline_initialize_everything ()
951 {
952 /* Find out if we are running in Emacs. */
953 running_in_emacs = getenv ("EMACS") != (char *)0;
954
955 /* Set up input and output if they are not already set up. */
956 if (!rl_instream)
957 rl_instream = stdin;
958
959 if (!rl_outstream)
960 rl_outstream = stdout;
961
962 /* Bind in_stream and out_stream immediately. These values may change,
963 but they may also be used before readline_internal () is called. */
964 in_stream = rl_instream;
965 out_stream = rl_outstream;
966
967 /* Allocate data structures. */
968 if (!rl_line_buffer)
969 rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
970
971 /* Initialize the terminal interface. */
972 init_terminal_io ((char *)NULL);
973
974 #if !defined (__GO32__)
975 /* Bind tty characters to readline functions. */
976 readline_default_bindings ();
977 #endif /* !__GO32__ */
978
979 /* Initialize the function names. */
980 rl_initialize_funmap ();
981
982 /* Read in the init file. */
983 rl_read_init_file ((char *)NULL);
984
985 /* Override the effect of any `set keymap' assignments in the
986 inputrc file. */
987 rl_set_keymap_from_edit_mode ();
988
989 /* If the completion parser's default word break characters haven't
990 been set yet, then do so now. */
991 if (rl_completer_word_break_characters == (char *)NULL)
992 rl_completer_word_break_characters = rl_basic_word_break_characters;
993 }
994
995 /* If this system allows us to look at the values of the regular
996 input editing characters, then bind them to their readline
997 equivalents, iff the characters are not bound to keymaps. */
998 static void
999 readline_default_bindings ()
1000 {
1001 rltty_set_default_bindings (_rl_keymap);
1002 }
1003
1004
1005 /* **************************************************************** */
1006 /* */
1007 /* Numeric Arguments */
1008 /* */
1009 /* **************************************************************** */
1010
1011 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1012 static int
1013 rl_digit_loop ()
1014 {
1015 int key, c;
1016
1017 while (1)
1018 {
1019 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1020 key = c = rl_read_key ();
1021
1022 if (_rl_keymap[c].type == ISFUNC &&
1023 _rl_keymap[c].function == rl_universal_argument)
1024 {
1025 rl_numeric_arg *= 4;
1026 continue;
1027 }
1028 c = UNMETA (c);
1029 if (numeric (c))
1030 {
1031 if (rl_explicit_arg)
1032 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1033 else
1034 rl_numeric_arg = (c - '0');
1035 rl_explicit_arg = 1;
1036 }
1037 else
1038 {
1039 if (c == '-' && !rl_explicit_arg)
1040 {
1041 rl_numeric_arg = 1;
1042 rl_arg_sign = -1;
1043 }
1044 else
1045 {
1046 rl_clear_message ();
1047 return (_rl_dispatch (key, _rl_keymap));
1048 }
1049 }
1050 }
1051 return 0;
1052 }
1053
1054 /* Add the current digit to the argument in progress. */
1055 rl_digit_argument (ignore, key)
1056 int ignore, key;
1057 {
1058 rl_pending_input = key;
1059 return (rl_digit_loop ());
1060 }
1061
1062 /* What to do when you abort reading an argument. */
1063 rl_discard_argument ()
1064 {
1065 ding ();
1066 rl_clear_message ();
1067 rl_init_argument ();
1068 return 0;
1069 }
1070
1071 /* Create a default argument. */
1072 rl_init_argument ()
1073 {
1074 rl_numeric_arg = rl_arg_sign = 1;
1075 rl_explicit_arg = 0;
1076 return 0;
1077 }
1078
1079 /* C-u, universal argument. Multiply the current argument by 4.
1080 Read a key. If the key has nothing to do with arguments, then
1081 dispatch on it. If the key is the abort character then abort. */
1082 rl_universal_argument ()
1083 {
1084 rl_numeric_arg *= 4;
1085 return (rl_digit_loop ());
1086 }
1087
1088 /* **************************************************************** */
1089 /* */
1090 /* Terminal and Termcap */
1091 /* */
1092 /* **************************************************************** */
1093
1094 static char *term_buffer = (char *)NULL;
1095 static char *term_string_buffer = (char *)NULL;
1096
1097 static int tcap_initialized = 0;
1098
1099 /* Non-zero means this terminal can't really do anything. */
1100 int dumb_term = 0;
1101 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1102 Unfortunately, PC is a global variable used by the termcap library. */
1103 #undef PC
1104
1105 #if !defined (__linux__)
1106 char PC;
1107 char *BC, *UP;
1108 #endif /* __linux__ */
1109
1110 /* Some strings to control terminal actions. These are output by tputs (). */
1111 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1112 char *term_pc;
1113
1114 int screenwidth, screenheight, screenchars;
1115
1116 /* Non-zero if we determine that the terminal can do character insertion. */
1117 int terminal_can_insert = 0;
1118
1119 /* How to insert characters. */
1120 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1121
1122 /* How to delete characters. */
1123 char *term_dc, *term_DC;
1124
1125 #if defined (HACK_TERMCAP_MOTION)
1126 char *term_forward_char;
1127 #endif /* HACK_TERMCAP_MOTION */
1128
1129 /* How to go up a line. */
1130 char *term_up;
1131
1132 /* A visible bell, if the terminal can be made to flash the screen. */
1133 char *visible_bell;
1134
1135 /* Non-zero means that this terminal has a meta key. */
1136 int term_has_meta;
1137
1138 /* The string to write to turn on the meta key, if this term has one. */
1139 char *term_mm;
1140
1141 /* The string to write to turn off the meta key, if this term has one. */
1142 char *term_mo;
1143
1144 /* The key sequences output by the arrow keys, if this terminal has any. */
1145 char *term_ku, *term_kd, *term_kr, *term_kl;
1146
1147 /* How to initialize and reset the arrow keys, if this terminal has any. */
1148 char *term_ks, *term_ke;
1149
1150 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1151 has changed. */
1152 rl_reset_terminal (terminal_name)
1153 char *terminal_name;
1154 {
1155 init_terminal_io (terminal_name);
1156 return 0;
1157 }
1158
1159 /* Set readline's idea of the screen size. TTY is a file descriptor open
1160 to the terminal. If IGNORE_ENV is true, we do not pay attention to the
1161 values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being
1162 non-null serve to check whether or not we have initialized termcap. */
1163 void
1164 _rl_set_screen_size (tty, ignore_env)
1165 int tty, ignore_env;
1166 {
1167 #if defined (TIOCGWINSZ)
1168 struct winsize window_size;
1169 #endif /* TIOCGWINSZ */
1170
1171 #if defined (TIOCGWINSZ)
1172 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1173 {
1174 screenwidth = (int) window_size.ws_col;
1175 screenheight = (int) window_size.ws_row;
1176 }
1177 #endif /* TIOCGWINSZ */
1178
1179 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1180 is unset. */
1181 if (screenwidth <= 0)
1182 {
1183 char *sw;
1184
1185 if (!ignore_env && (sw = getenv ("COLUMNS")))
1186 screenwidth = atoi (sw);
1187
1188 if (screenwidth <= 0 && term_string_buffer)
1189 screenwidth = tgetnum ("co");
1190 }
1191
1192 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1193 is unset. */
1194 if (screenheight <= 0)
1195 {
1196 char *sh;
1197
1198 if (!ignore_env && (sh = getenv ("LINES")))
1199 screenheight = atoi (sh);
1200
1201 if (screenheight <= 0 && term_string_buffer)
1202 screenheight = tgetnum ("li");
1203 }
1204
1205 /* If all else fails, default to 80x24 terminal. */
1206 if (screenwidth <= 1)
1207 screenwidth = 80;
1208
1209 if (screenheight <= 0)
1210 screenheight = 24;
1211
1212 #if defined (SHELL)
1213 /* If we're being compiled as part of bash, set the environment
1214 variables $LINES and $COLUMNS to new values. */
1215 set_lines_and_columns (screenheight, screenwidth);
1216 #endif
1217
1218 screenwidth--;
1219
1220 screenchars = screenwidth * screenheight;
1221 }
1222
1223 struct _tc_string {
1224 char *tc_var;
1225 char **tc_value;
1226 };
1227
1228 /* This should be kept sorted, just in case we decide to change the
1229 search algorithm to something smarter. */
1230 static struct _tc_string tc_strings[] =
1231 {
1232 "DC", &term_DC,
1233 "IC", &term_IC,
1234 "ce", &term_clreol,
1235 "cl", &term_clrpag,
1236 "cr", &term_cr,
1237 "dc", &term_dc,
1238 "ei", &term_ei,
1239 "ic", &term_ic,
1240 "im", &term_im,
1241 "kd", &term_kd,
1242 "kl", &term_kl,
1243 "kr", &term_kr,
1244 "ku", &term_ku,
1245 "ks", &term_ks,
1246 "ke", &term_ke,
1247 "le", &term_backspace,
1248 "mm", &term_mm,
1249 "mo", &term_mo,
1250 #if defined (HACK_TERMCAP_MOTION)
1251 "nd", &term_forward_char,
1252 #endif
1253 "pc", &term_pc,
1254 "up", &term_up,
1255 "vb", &visible_bell,
1256 };
1257
1258 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
1259
1260 /* Read the desired terminal capability strings into BP. The capabilities
1261 are described in the TC_STRINGS table. */
1262 static void
1263 get_term_capabilities (bp)
1264 char **bp;
1265 {
1266 register int i;
1267
1268 for (i = 0; i < NUM_TC_STRINGS; i++)
1269 *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
1270 tcap_initialized = 1;
1271 }
1272
1273 static int
1274 init_terminal_io (terminal_name)
1275 char *terminal_name;
1276 {
1277 #if defined (__GO32__)
1278 screenwidth = ScreenCols ();
1279 screenheight = ScreenRows ();
1280 screenchars = screenwidth * screenheight;
1281 term_cr = "\r";
1282 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1283 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1284
1285 /* Does the __GO32__ have a meta key? I don't know. */
1286 term_has_meta = 0;
1287 term_mm = term_mo = (char *)NULL;
1288
1289 /* It probably has arrow keys, but I don't know what they are. */
1290 term_ku = term_kd = term_kr = term_kl = (char *)NULL;
1291
1292 #if defined (HACK_TERMCAP_MOTION)
1293 term_forward_char = (char *)NULL;
1294 #endif /* HACK_TERMCAP_MOTION */
1295 terminal_can_insert = 0;
1296 return;
1297 #else /* !__GO32__ */
1298
1299 char *term, *buffer;
1300 int tty;
1301 Keymap xkeymap;
1302
1303 term = terminal_name ? terminal_name : getenv ("TERM");
1304
1305 if (!term_string_buffer)
1306 term_string_buffer = xmalloc (2048);
1307
1308 if (!term_buffer)
1309 term_buffer = xmalloc (2048);
1310
1311 buffer = term_string_buffer;
1312
1313 term_clrpag = term_cr = term_clreol = (char *)NULL;
1314
1315 if (!term)
1316 term = "dumb";
1317
1318 if (tgetent (term_buffer, term) <= 0)
1319 {
1320 dumb_term = 1;
1321 screenwidth = 79;
1322 screenheight = 24;
1323 screenchars = 79 * 24;
1324 term_cr = "\r";
1325 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1326 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1327 term_ku = term_kd = term_kl = term_kr = (char *)NULL;
1328 #if defined (HACK_TERMCAP_MOTION)
1329 term_forward_char = (char *)NULL;
1330 #endif
1331 terminal_can_insert = 0;
1332 return 0;
1333 }
1334
1335 get_term_capabilities (&buffer);
1336
1337 /* Set up the variables that the termcap library expects the application
1338 to provide. */
1339 PC = term_pc ? *term_pc : 0;
1340 BC = term_backspace;
1341 UP = term_up;
1342
1343 if (!term_cr)
1344 term_cr = "\r";
1345
1346 if (rl_instream)
1347 tty = fileno (rl_instream);
1348 else
1349 tty = 0;
1350
1351 screenwidth = screenheight = 0;
1352
1353 _rl_set_screen_size (tty, 0);
1354
1355 /* "An application program can assume that the terminal can do
1356 character insertion if *any one of* the capabilities `IC',
1357 `im', `ic' or `ip' is provided." But we can't do anything if
1358 only `ip' is provided, so... */
1359 terminal_can_insert = (term_IC || term_im || term_ic);
1360
1361 /* Check to see if this terminal has a meta key and clear the capability
1362 variables if there is none. */
1363 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1364 if (!term_has_meta)
1365 {
1366 term_mm = (char *)NULL;
1367 term_mo = (char *)NULL;
1368 }
1369
1370 /* Attempt to find and bind the arrow keys. Do not override already
1371 bound keys in an overzealous attempt, however. */
1372 xkeymap = _rl_keymap;
1373
1374 _rl_keymap = emacs_standard_keymap;
1375 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
1376 _rl_bind_if_unbound (term_kd, rl_get_next_history);
1377 _rl_bind_if_unbound (term_kr, rl_forward);
1378 _rl_bind_if_unbound (term_kl, rl_backward);
1379
1380 #if defined (VI_MODE)
1381 _rl_keymap = vi_movement_keymap;
1382 _rl_bind_if_unbound (term_ku, rl_get_previous_history);
1383 _rl_bind_if_unbound (term_kd, rl_get_next_history);
1384 _rl_bind_if_unbound (term_kr, rl_forward);
1385 _rl_bind_if_unbound (term_kl, rl_backward);
1386 #endif /* VI_MODE */
1387
1388 _rl_keymap = xkeymap;
1389
1390 #endif /* !__GO32__ */
1391 return 0;
1392 }
1393
1394 char *
1395 rl_get_termcap (cap)
1396 char *cap;
1397 {
1398 register int i;
1399
1400 if (tcap_initialized == 0)
1401 return ((char *)NULL);
1402 for (i = 0; i < NUM_TC_STRINGS; i++)
1403 {
1404 if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
1405 return *(tc_strings[i].tc_value);
1406 }
1407 return ((char *)NULL);
1408 }
1409
1410 /* A function for the use of tputs () */
1411 int
1412 _rl_output_character_function (c)
1413 int c;
1414 {
1415 return putc (c, out_stream);
1416 }
1417
1418 /* Write COUNT characters from STRING to the output stream. */
1419 void
1420 _rl_output_some_chars (string, count)
1421 char *string;
1422 int count;
1423 {
1424 fwrite (string, 1, count, out_stream);
1425 }
1426
1427 /* Move the cursor back. */
1428 backspace (count)
1429 int count;
1430 {
1431 register int i;
1432
1433 #if !defined (__GO32__)
1434 if (term_backspace)
1435 for (i = 0; i < count; i++)
1436 tputs (term_backspace, 1, _rl_output_character_function);
1437 else
1438 #endif /* !__GO32__ */
1439 for (i = 0; i < count; i++)
1440 putc ('\b', out_stream);
1441 return 0;
1442 }
1443
1444 /* Move to the start of the next line. */
1445 crlf ()
1446 {
1447 #if defined (NEW_TTY_DRIVER)
1448 tputs (term_cr, 1, _rl_output_character_function);
1449 #endif /* NEW_TTY_DRIVER */
1450 putc ('\n', out_stream);
1451 return 0;
1452 }
1453
1454
1455 /* **************************************************************** */
1456 /* */
1457 /* Utility Functions */
1458 /* */
1459 /* **************************************************************** */
1460
1461 /* Return 0 if C is not a member of the class of characters that belong
1462 in words, or 1 if it is. */
1463
1464 int allow_pathname_alphabetic_chars = 0;
1465 char *pathname_alphabetic_chars = "/-_=~.#$";
1466
1467 int
1468 alphabetic (c)
1469 int c;
1470 {
1471 if (pure_alphabetic (c) || (numeric (c)))
1472 return (1);
1473
1474 if (allow_pathname_alphabetic_chars)
1475 return (strchr (pathname_alphabetic_chars, c) != NULL);
1476 else
1477 return (0);
1478 }
1479
1480 /* Return non-zero if C is a numeric character. */
1481 int
1482 numeric (c)
1483 int c;
1484 {
1485 return (c >= '0' && c <= '9');
1486 }
1487
1488 /* Ring the terminal bell. */
1489 int
1490 ding ()
1491 {
1492 if (readline_echoing_p)
1493 {
1494 #if !defined (__GO32__)
1495 switch (_rl_bell_preference)
1496 {
1497 case NO_BELL:
1498 default:
1499 break;
1500 case VISIBLE_BELL:
1501 if (visible_bell)
1502 {
1503 tputs (visible_bell, 1, _rl_output_character_function);
1504 break;
1505 }
1506 /* FALLTHROUGH */
1507 case AUDIBLE_BELL:
1508 fprintf (stderr, "\007");
1509 fflush (stderr);
1510 break;
1511 }
1512 #else /* __GO32__ */
1513 fprintf (stderr, "\007");
1514 fflush (stderr);
1515 #endif /* __GO32__ */
1516 return (0);
1517 }
1518 return (-1);
1519 }
1520
1521 /* How to abort things. */
1522 rl_abort ()
1523 {
1524 ding ();
1525 rl_clear_message ();
1526 rl_init_argument ();
1527 rl_pending_input = 0;
1528
1529 defining_kbd_macro = 0;
1530 while (executing_macro)
1531 pop_executing_macro ();
1532
1533 rl_last_func = (Function *)NULL;
1534 longjmp (readline_top_level, 1);
1535 }
1536
1537 /* Return a copy of the string between FROM and TO.
1538 FROM is inclusive, TO is not. */
1539 char *
1540 rl_copy_text (from, to)
1541 int from, to;
1542 {
1543 register int length;
1544 char *copy;
1545
1546 /* Fix it if the caller is confused. */
1547 if (from > to)
1548 {
1549 int t = from;
1550 from = to;
1551 to = t;
1552 }
1553
1554 length = to - from;
1555 copy = xmalloc (1 + length);
1556 strncpy (copy, the_line + from, length);
1557 copy[length] = '\0';
1558 return (copy);
1559 }
1560
1561 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1562 LEN characters. */
1563 void
1564 rl_extend_line_buffer (len)
1565 int len;
1566 {
1567 while (len >= rl_line_buffer_len)
1568 {
1569 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
1570 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
1571 }
1572
1573 the_line = rl_line_buffer;
1574 }
1575
1576
1577 /* **************************************************************** */
1578 /* */
1579 /* Insert and Delete */
1580 /* */
1581 /* **************************************************************** */
1582
1583 /* Insert a string of text into the line at point. This is the only
1584 way that you should do insertion. rl_insert () calls this
1585 function. */
1586 rl_insert_text (string)
1587 char *string;
1588 {
1589 register int i, l = strlen (string);
1590
1591 if (rl_end + l >= rl_line_buffer_len)
1592 rl_extend_line_buffer (rl_end + l);
1593
1594 for (i = rl_end; i >= rl_point; i--)
1595 the_line[i + l] = the_line[i];
1596 strncpy (the_line + rl_point, string, l);
1597
1598 /* Remember how to undo this if we aren't undoing something. */
1599 if (!doing_an_undo)
1600 {
1601 /* If possible and desirable, concatenate the undos. */
1602 if ((l == 1) &&
1603 rl_undo_list &&
1604 (rl_undo_list->what == UNDO_INSERT) &&
1605 (rl_undo_list->end == rl_point) &&
1606 (rl_undo_list->end - rl_undo_list->start < 20))
1607 rl_undo_list->end++;
1608 else
1609 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1610 }
1611 rl_point += l;
1612 rl_end += l;
1613 the_line[rl_end] = '\0';
1614 return l;
1615 }
1616
1617 /* Delete the string between FROM and TO. FROM is
1618 inclusive, TO is not. */
1619 rl_delete_text (from, to)
1620 int from, to;
1621 {
1622 register char *text;
1623 register int diff, i;
1624
1625 /* Fix it if the caller is confused. */
1626 if (from > to)
1627 {
1628 int t = from;
1629 from = to;
1630 to = t;
1631 }
1632 text = rl_copy_text (from, to);
1633
1634 /* Some versions of strncpy() can't handle overlapping arguments. */
1635 diff = to - from;
1636 for (i = from; i < rl_end - diff; i++)
1637 the_line[i] = the_line[i + diff];
1638
1639 /* Remember how to undo this delete. */
1640 if (!doing_an_undo)
1641 rl_add_undo (UNDO_DELETE, from, to, text);
1642 else
1643 free (text);
1644
1645 rl_end -= diff;
1646 the_line[rl_end] = '\0';
1647 return (diff);
1648 }
1649
1650
1651 /* **************************************************************** */
1652 /* */
1653 /* Readline character functions */
1654 /* */
1655 /* **************************************************************** */
1656
1657 /* This is not a gap editor, just a stupid line input routine. No hair
1658 is involved in writing any of the functions, and none should be. */
1659
1660 /* Note that:
1661
1662 rl_end is the place in the string that we would place '\0';
1663 i.e., it is always safe to place '\0' there.
1664
1665 rl_point is the place in the string where the cursor is. Sometimes
1666 this is the same as rl_end.
1667
1668 Any command that is called interactively receives two arguments.
1669 The first is a count: the numeric arg pased to this command.
1670 The second is the key which invoked this command.
1671 */
1672
1673
1674 /* **************************************************************** */
1675 /* */
1676 /* Movement Commands */
1677 /* */
1678 /* **************************************************************** */
1679
1680 /* Note that if you `optimize' the display for these functions, you cannot
1681 use said functions in other functions which do not do optimizing display.
1682 I.e., you will have to update the data base for rl_redisplay, and you
1683 might as well let rl_redisplay do that job. */
1684
1685 /* Move forward COUNT characters. */
1686 rl_forward (count)
1687 int count;
1688 {
1689 if (count < 0)
1690 rl_backward (-count);
1691 else if (count > 0)
1692 {
1693 int end = rl_point + count;
1694 #if defined (VI_MODE)
1695 int lend = rl_end - (rl_editing_mode == vi_mode);
1696 #else
1697 int lend = rl_end;
1698 #endif
1699
1700 if (end > lend)
1701 {
1702 rl_point = lend;
1703 ding ();
1704 }
1705 else
1706 rl_point = end;
1707 }
1708 return 0;
1709 }
1710
1711 /* Move backward COUNT characters. */
1712 rl_backward (count)
1713 int count;
1714 {
1715 if (count < 0)
1716 rl_forward (-count);
1717 else if (count > 0)
1718 {
1719 if (rl_point < count)
1720 {
1721 rl_point = 0;
1722 ding ();
1723 }
1724 else
1725 rl_point -= count;
1726 }
1727 return 0;
1728 }
1729
1730 /* Move to the beginning of the line. */
1731 rl_beg_of_line ()
1732 {
1733 rl_point = 0;
1734 return 0;
1735 }
1736
1737 /* Move to the end of the line. */
1738 rl_end_of_line ()
1739 {
1740 rl_point = rl_end;
1741 return 0;
1742 }
1743
1744 /* Move forward a word. We do what Emacs does. */
1745 rl_forward_word (count)
1746 int count;
1747 {
1748 int c;
1749
1750 if (count < 0)
1751 {
1752 rl_backward_word (-count);
1753 return 0;
1754 }
1755
1756 while (count)
1757 {
1758 if (rl_point == rl_end)
1759 return 0;
1760
1761 /* If we are not in a word, move forward until we are in one.
1762 Then, move forward until we hit a non-alphabetic character. */
1763 c = the_line[rl_point];
1764 if (!alphabetic (c))
1765 {
1766 while (++rl_point < rl_end)
1767 {
1768 c = the_line[rl_point];
1769 if (alphabetic (c))
1770 break;
1771 }
1772 }
1773 if (rl_point == rl_end)
1774 return 0;
1775 while (++rl_point < rl_end)
1776 {
1777 c = the_line[rl_point];
1778 if (!alphabetic (c))
1779 break;
1780 }
1781 --count;
1782 }
1783 return 0;
1784 }
1785
1786 /* Move backward a word. We do what Emacs does. */
1787 rl_backward_word (count)
1788 int count;
1789 {
1790 int c;
1791
1792 if (count < 0)
1793 {
1794 rl_forward_word (-count);
1795 return 0;
1796 }
1797
1798 while (count)
1799 {
1800 if (!rl_point)
1801 return 0;
1802
1803 /* Like rl_forward_word (), except that we look at the characters
1804 just before point. */
1805
1806 c = the_line[rl_point - 1];
1807 if (!alphabetic (c))
1808 {
1809 while (--rl_point)
1810 {
1811 c = the_line[rl_point - 1];
1812 if (alphabetic (c))
1813 break;
1814 }
1815 }
1816
1817 while (rl_point)
1818 {
1819 c = the_line[rl_point - 1];
1820 if (!alphabetic (c))
1821 break;
1822 else
1823 --rl_point;
1824 }
1825 --count;
1826 }
1827 return 0;
1828 }
1829
1830 /* Clear the current line. Numeric argument to C-l does this. */
1831 rl_refresh_line ()
1832 {
1833 int curr_line, nleft;
1834
1835 /* Find out whether or not there might be invisible characters in the
1836 editing buffer. */
1837 if (rl_display_prompt == rl_prompt)
1838 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1839 else
1840 nleft = _rl_last_c_pos - screenwidth;
1841
1842 if (nleft > 0)
1843 curr_line = 1 + nleft / screenwidth;
1844 else
1845 curr_line = 0;
1846
1847 _rl_move_vert (curr_line);
1848 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1849
1850 #if defined (__GO32__)
1851 {
1852 int row, col, width, row_start;
1853
1854 ScreenGetCursor (&row, &col);
1855 width = ScreenCols ();
1856 row_start = ScreenPrimary + (row * width);
1857 memset (row_start + col, 0, (width - col) * 2);
1858 }
1859 #else /* !__GO32__ */
1860 if (term_clreol)
1861 tputs (term_clreol, 1, _rl_output_character_function);
1862 #endif /* !__GO32__ */
1863
1864 rl_forced_update_display ();
1865 rl_display_fixed = 1;
1866
1867 return 0;
1868 }
1869
1870 /* C-l typed to a line without quoting clears the screen, and then reprints
1871 the prompt and the current input line. Given a numeric arg, redraw only
1872 the current line. */
1873 rl_clear_screen ()
1874 {
1875 if (rl_explicit_arg)
1876 {
1877 rl_refresh_line ();
1878 return 0;
1879 }
1880
1881 #if !defined (__GO32__)
1882 if (term_clrpag)
1883 tputs (term_clrpag, 1, _rl_output_character_function);
1884 else
1885 #endif /* !__GO32__ */
1886 crlf ();
1887
1888 rl_forced_update_display ();
1889 rl_display_fixed = 1;
1890
1891 return 0;
1892 }
1893
1894 rl_arrow_keys (count, c)
1895 int count, c;
1896 {
1897 int ch;
1898
1899 ch = rl_read_key ();
1900
1901 switch (to_upper (ch))
1902 {
1903 case 'A':
1904 rl_get_previous_history (count);
1905 break;
1906
1907 case 'B':
1908 rl_get_next_history (count);
1909 break;
1910
1911 case 'C':
1912 rl_forward (count);
1913 break;
1914
1915 case 'D':
1916 rl_backward (count);
1917 break;
1918
1919 default:
1920 ding ();
1921 }
1922 return 0;
1923 }
1924
1925
1926 /* **************************************************************** */
1927 /* */
1928 /* Text commands */
1929 /* */
1930 /* **************************************************************** */
1931
1932 /* Insert the character C at the current location, moving point forward. */
1933 rl_insert (count, c)
1934 int count, c;
1935 {
1936 register int i;
1937 char *string;
1938
1939 if (count <= 0)
1940 return 0;
1941
1942 /* If we can optimize, then do it. But don't let people crash
1943 readline because of extra large arguments. */
1944 if (count > 1 && count < 1024)
1945 {
1946 string = xmalloc (1 + count);
1947
1948 for (i = 0; i < count; i++)
1949 string[i] = c;
1950
1951 string[i] = '\0';
1952 rl_insert_text (string);
1953 free (string);
1954
1955 return 0;
1956 }
1957
1958 if (count > 1024)
1959 {
1960 int decreaser;
1961 char str[1024+1];
1962
1963 for (i = 0; i < 1024; i++)
1964 str[i] = c;
1965
1966 while (count)
1967 {
1968 decreaser = (count > 1024 ? 1024 : count);
1969 str[decreaser] = '\0';
1970 rl_insert_text (str);
1971 count -= decreaser;
1972 }
1973
1974 return 0;
1975 }
1976
1977 /* We are inserting a single character.
1978 If there is pending input, then make a string of all of the
1979 pending characters that are bound to rl_insert, and insert
1980 them all. */
1981 if (any_typein)
1982 {
1983 int key = 0, t;
1984
1985 i = 0;
1986 string = xmalloc (ibuffer_len + 1);
1987 string[i++] = c;
1988
1989 while ((t = rl_get_char (&key)) &&
1990 (_rl_keymap[key].type == ISFUNC &&
1991 _rl_keymap[key].function == rl_insert))
1992 string[i++] = key;
1993
1994 if (t)
1995 rl_unget_char (key);
1996
1997 string[i] = '\0';
1998 rl_insert_text (string);
1999 free (string);
2000 }
2001 else
2002 {
2003 /* Inserting a single character. */
2004 char str[2];
2005
2006 str[1] = '\0';
2007 str[0] = c;
2008 rl_insert_text (str);
2009 }
2010 return 0;
2011 }
2012
2013 /* Insert the next typed character verbatim. */
2014 rl_quoted_insert (count)
2015 int count;
2016 {
2017 int c;
2018
2019 c = rl_read_key ();
2020 return (rl_insert (count, c));
2021
2022 }
2023
2024 /* Insert a tab character. */
2025 rl_tab_insert (count)
2026 int count;
2027 {
2028 return (rl_insert (count, '\t'));
2029 }
2030
2031 /* What to do when a NEWLINE is pressed. We accept the whole line.
2032 KEY is the key that invoked this command. I guess it could have
2033 meaning in the future. */
2034 rl_newline (count, key)
2035 int count, key;
2036 {
2037 rl_done = 1;
2038
2039 #if defined (VI_MODE)
2040 _rl_vi_done_inserting ();
2041 _rl_vi_reset_last ();
2042
2043 #endif /* VI_MODE */
2044
2045 if (readline_echoing_p)
2046 {
2047 _rl_move_vert (_rl_vis_botlin);
2048 _rl_vis_botlin = 0;
2049 crlf ();
2050 fflush (out_stream);
2051 rl_display_fixed++;
2052 }
2053 return 0;
2054 }
2055
2056 rl_clean_up_for_exit ()
2057 {
2058 if (readline_echoing_p)
2059 {
2060 _rl_move_vert (_rl_vis_botlin);
2061 _rl_vis_botlin = 0;
2062 fflush (out_stream);
2063 rl_restart_output ();
2064 }
2065 return 0;
2066 }
2067
2068 /* What to do for some uppercase characters, like meta characters,
2069 and some characters appearing in emacs_ctlx_keymap. This function
2070 is just a stub, you bind keys to it and the code in _rl_dispatch ()
2071 is special cased. */
2072 rl_do_lowercase_version (ignore1, ignore2)
2073 int ignore1, ignore2;
2074 {
2075 return 0;
2076 }
2077
2078 /* Rubout the character behind point. */
2079 rl_rubout (count)
2080 int count;
2081 {
2082 if (count < 0)
2083 {
2084 rl_delete (-count);
2085 return 0;
2086 }
2087
2088 if (!rl_point)
2089 {
2090 ding ();
2091 return -1;
2092 }
2093
2094 if (count > 1 || rl_explicit_arg)
2095 {
2096 int orig_point = rl_point;
2097 rl_backward (count);
2098 rl_kill_text (orig_point, rl_point);
2099 }
2100 else
2101 {
2102 int c = the_line[--rl_point];
2103 rl_delete_text (rl_point, rl_point + 1);
2104
2105 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
2106 {
2107 int l;
2108 l = rl_character_len (c, rl_point);
2109 _rl_erase_at_end_of_line (l);
2110 }
2111 }
2112 return 0;
2113 }
2114
2115 /* Delete the character under the cursor. Given a numeric argument,
2116 kill that many characters instead. */
2117 rl_delete (count, invoking_key)
2118 int count, invoking_key;
2119 {
2120 if (count < 0)
2121 {
2122 return (rl_rubout (-count));
2123 }
2124
2125 if (rl_point == rl_end)
2126 {
2127 ding ();
2128 return -1;
2129 }
2130
2131 if (count > 1 || rl_explicit_arg)
2132 {
2133 int orig_point = rl_point;
2134 rl_forward (count);
2135 rl_kill_text (orig_point, rl_point);
2136 rl_point = orig_point;
2137 return 0;
2138 }
2139 else
2140 return (rl_delete_text (rl_point, rl_point + 1));
2141
2142 }
2143
2144 /* Delete all spaces and tabs around point. */
2145 rl_delete_horizontal_space (count, ignore)
2146 int count, ignore;
2147 {
2148 int start = rl_point;
2149
2150 while (rl_point && whitespace (the_line[rl_point - 1]))
2151 rl_point--;
2152
2153 start = rl_point;
2154
2155 while (rl_point < rl_end && whitespace (the_line[rl_point]))
2156 rl_point++;
2157
2158 if (start != rl_point)
2159 {
2160 rl_delete_text (start, rl_point);
2161 rl_point = start;
2162 }
2163 return 0;
2164 }
2165
2166
2167 /* **************************************************************** */
2168 /* */
2169 /* Kill commands */
2170 /* */
2171 /* **************************************************************** */
2172
2173 /* The next two functions mimic unix line editing behaviour, except they
2174 save the deleted text on the kill ring. This is safer than not saving
2175 it, and since we have a ring, nobody should get screwed. */
2176
2177 /* This does what C-w does in Unix. We can't prevent people from
2178 using behaviour that they expect. */
2179 rl_unix_word_rubout ()
2180 {
2181 if (!rl_point)
2182 ding ();
2183 else
2184 {
2185 int orig_point = rl_point;
2186
2187 while (rl_point && whitespace (the_line[rl_point - 1]))
2188 rl_point--;
2189
2190 while (rl_point && !whitespace (the_line[rl_point - 1]))
2191 rl_point--;
2192
2193 rl_kill_text (orig_point, rl_point);
2194 }
2195 return 0;
2196 }
2197
2198 /* Here is C-u doing what Unix does. You don't *have* to use these
2199 key-bindings. We have a choice of killing the entire line, or
2200 killing from where we are to the start of the line. We choose the
2201 latter, because if you are a Unix weenie, then you haven't backspaced
2202 into the line at all, and if you aren't, then you know what you are
2203 doing. */
2204 rl_unix_line_discard ()
2205 {
2206 if (!rl_point)
2207 ding ();
2208 else
2209 {
2210 rl_kill_text (rl_point, 0);
2211 rl_point = 0;
2212 }
2213 return 0;
2214 }
2215
2216
2217 /* **************************************************************** */
2218 /* */
2219 /* Commands For Typos */
2220 /* */
2221 /* **************************************************************** */
2222
2223 /* Random and interesting things in here. */
2224
2225 /* **************************************************************** */
2226 /* */
2227 /* Changing Case */
2228 /* */
2229 /* **************************************************************** */
2230
2231 /* The three kinds of things that we know how to do. */
2232 #define UpCase 1
2233 #define DownCase 2
2234 #define CapCase 3
2235
2236 static int rl_change_case ();
2237
2238 /* Uppercase the word at point. */
2239 rl_upcase_word (count)
2240 int count;
2241 {
2242 return (rl_change_case (count, UpCase));
2243 }
2244
2245 /* Lowercase the word at point. */
2246 rl_downcase_word (count)
2247 int count;
2248 {
2249 return (rl_change_case (count, DownCase));
2250 }
2251
2252 /* Upcase the first letter, downcase the rest. */
2253 rl_capitalize_word (count)
2254 int count;
2255 {
2256 return (rl_change_case (count, CapCase));
2257 }
2258
2259 /* The meaty function.
2260 Change the case of COUNT words, performing OP on them.
2261 OP is one of UpCase, DownCase, or CapCase.
2262 If a negative argument is given, leave point where it started,
2263 otherwise, leave it where it moves to. */
2264 static int
2265 rl_change_case (count, op)
2266 int count, op;
2267 {
2268 register int start = rl_point, end;
2269 int state = 0;
2270
2271 rl_forward_word (count);
2272 end = rl_point;
2273
2274 if (count < 0)
2275 {
2276 int temp = start;
2277 start = end;
2278 end = temp;
2279 }
2280
2281 /* We are going to modify some text, so let's prepare to undo it. */
2282 rl_modifying (start, end);
2283
2284 for (; start < end; start++)
2285 {
2286 switch (op)
2287 {
2288 case UpCase:
2289 the_line[start] = to_upper (the_line[start]);
2290 break;
2291
2292 case DownCase:
2293 the_line[start] = to_lower (the_line[start]);
2294 break;
2295
2296 case CapCase:
2297 if (state == 0)
2298 {
2299 the_line[start] = to_upper (the_line[start]);
2300 state = 1;
2301 }
2302 else
2303 {
2304 the_line[start] = to_lower (the_line[start]);
2305 }
2306 if (!pure_alphabetic (the_line[start]))
2307 state = 0;
2308 break;
2309
2310 default:
2311 abort ();
2312 return -1;
2313 }
2314 }
2315 rl_point = end;
2316 return 0;
2317 }
2318
2319 /* **************************************************************** */
2320 /* */
2321 /* Transposition */
2322 /* */
2323 /* **************************************************************** */
2324
2325 /* Transpose the words at point. */
2326 rl_transpose_words (count)
2327 int count;
2328 {
2329 char *word1, *word2;
2330 int w1_beg, w1_end, w2_beg, w2_end;
2331 int orig_point = rl_point;
2332
2333 if (!count)
2334 return 0;
2335
2336 /* Find the two words. */
2337 rl_forward_word (count);
2338 w2_end = rl_point;
2339 rl_backward_word (1);
2340 w2_beg = rl_point;
2341 rl_backward_word (count);
2342 w1_beg = rl_point;
2343 rl_forward_word (1);
2344 w1_end = rl_point;
2345
2346 /* Do some check to make sure that there really are two words. */
2347 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2348 {
2349 ding ();
2350 rl_point = orig_point;
2351 return -1;
2352 }
2353
2354 /* Get the text of the words. */
2355 word1 = rl_copy_text (w1_beg, w1_end);
2356 word2 = rl_copy_text (w2_beg, w2_end);
2357
2358 /* We are about to do many insertions and deletions. Remember them
2359 as one operation. */
2360 rl_begin_undo_group ();
2361
2362 /* Do the stuff at word2 first, so that we don't have to worry
2363 about word1 moving. */
2364 rl_point = w2_beg;
2365 rl_delete_text (w2_beg, w2_end);
2366 rl_insert_text (word1);
2367
2368 rl_point = w1_beg;
2369 rl_delete_text (w1_beg, w1_end);
2370 rl_insert_text (word2);
2371
2372 /* This is exactly correct since the text before this point has not
2373 changed in length. */
2374 rl_point = w2_end;
2375
2376 /* I think that does it. */
2377 rl_end_undo_group ();
2378 free (word1);
2379 free (word2);
2380
2381 return 0;
2382 }
2383
2384 /* Transpose the characters at point. If point is at the end of the line,
2385 then transpose the characters before point. */
2386 rl_transpose_chars (count)
2387 int count;
2388 {
2389 char dummy[2];
2390
2391 if (!count)
2392 return 0;
2393
2394 if (!rl_point || rl_end < 2)
2395 {
2396 ding ();
2397 return -1;
2398 }
2399
2400 rl_begin_undo_group ();
2401
2402 if (rl_point == rl_end)
2403 {
2404 --rl_point;
2405 count = 1;
2406 }
2407 rl_point--;
2408
2409 dummy[0] = the_line[rl_point];
2410 dummy[1] = '\0';
2411
2412 rl_delete_text (rl_point, rl_point + 1);
2413
2414 rl_point += count;
2415 if (rl_point > rl_end)
2416 rl_point = rl_end;
2417 else if (rl_point < 0)
2418 rl_point = 0;
2419 rl_insert_text (dummy);
2420
2421 rl_end_undo_group ();
2422 return 0;
2423 }
2424
2425 /* **************************************************************** */
2426 /* */
2427 /* Undo, and Undoing */
2428 /* */
2429 /* **************************************************************** */
2430
2431 /* The current undo list for THE_LINE. */
2432 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2433
2434 /* Remember how to undo something. Concatenate some undos if that
2435 seems right. */
2436 void
2437 rl_add_undo (what, start, end, text)
2438 enum undo_code what;
2439 int start, end;
2440 char *text;
2441 {
2442 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
2443 temp->what = what;
2444 temp->start = start;
2445 temp->end = end;
2446 temp->text = text;
2447 temp->next = rl_undo_list;
2448 rl_undo_list = temp;
2449 }
2450
2451 /* Free the existing undo list. */
2452 void
2453 free_undo_list ()
2454 {
2455 while (rl_undo_list)
2456 {
2457 UNDO_LIST *release = rl_undo_list;
2458 rl_undo_list = rl_undo_list->next;
2459
2460 if (release->what == UNDO_DELETE)
2461 free (release->text);
2462
2463 free (release);
2464 }
2465 rl_undo_list = (UNDO_LIST *)NULL;
2466 }
2467
2468 /* Undo the next thing in the list. Return 0 if there
2469 is nothing to undo, or non-zero if there was. */
2470 int
2471 rl_do_undo ()
2472 {
2473 UNDO_LIST *release;
2474 int waiting_for_begin = 0;
2475
2476 undo_thing:
2477 if (!rl_undo_list)
2478 return (0);
2479
2480 doing_an_undo = 1;
2481
2482 switch (rl_undo_list->what) {
2483
2484 /* Undoing deletes means inserting some text. */
2485 case UNDO_DELETE:
2486 rl_point = rl_undo_list->start;
2487 rl_insert_text (rl_undo_list->text);
2488 free (rl_undo_list->text);
2489 break;
2490
2491 /* Undoing inserts means deleting some text. */
2492 case UNDO_INSERT:
2493 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
2494 rl_point = rl_undo_list->start;
2495 break;
2496
2497 /* Undoing an END means undoing everything 'til we get to
2498 a BEGIN. */
2499 case UNDO_END:
2500 waiting_for_begin++;
2501 break;
2502
2503 /* Undoing a BEGIN means that we are done with this group. */
2504 case UNDO_BEGIN:
2505 if (waiting_for_begin)
2506 waiting_for_begin--;
2507 else
2508 ding ();
2509 break;
2510 }
2511
2512 doing_an_undo = 0;
2513
2514 release = rl_undo_list;
2515 rl_undo_list = rl_undo_list->next;
2516 free (release);
2517
2518 if (waiting_for_begin)
2519 goto undo_thing;
2520
2521 return (1);
2522 }
2523
2524 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2525 rl_begin_undo_group ()
2526 {
2527 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2528 return 0;
2529 }
2530
2531 /* End an undo group started with rl_begin_undo_group (). */
2532 rl_end_undo_group ()
2533 {
2534 rl_add_undo (UNDO_END, 0, 0, 0);
2535 return 0;
2536 }
2537
2538 /* Save an undo entry for the text from START to END. */
2539 rl_modifying (start, end)
2540 int start, end;
2541 {
2542 if (start > end)
2543 {
2544 int t = start;
2545 start = end;
2546 end = t;
2547 }
2548
2549 if (start != end)
2550 {
2551 char *temp = rl_copy_text (start, end);
2552 rl_begin_undo_group ();
2553 rl_add_undo (UNDO_DELETE, start, end, temp);
2554 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
2555 rl_end_undo_group ();
2556 }
2557 return 0;
2558 }
2559
2560 /* Revert the current line to its previous state. */
2561 rl_revert_line ()
2562 {
2563 if (!rl_undo_list)
2564 ding ();
2565 else
2566 {
2567 while (rl_undo_list)
2568 rl_do_undo ();
2569 }
2570 return 0;
2571 }
2572
2573 /* Do some undoing of things that were done. */
2574 rl_undo_command (count)
2575 int count;
2576 {
2577 if (count < 0)
2578 return 0; /* Nothing to do. */
2579
2580 while (count)
2581 {
2582 if (rl_do_undo ())
2583 count--;
2584 else
2585 {
2586 ding ();
2587 break;
2588 }
2589 }
2590 return 0;
2591 }
2592
2593 /* **************************************************************** */
2594 /* */
2595 /* History Utilities */
2596 /* */
2597 /* **************************************************************** */
2598
2599 /* We already have a history library, and that is what we use to control
2600 the history features of readline. However, this is our local interface
2601 to the history mechanism. */
2602
2603 /* While we are editing the history, this is the saved
2604 version of the original line. */
2605 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
2606
2607 /* Set the history pointer back to the last entry in the history. */
2608 static void
2609 start_using_history ()
2610 {
2611 using_history ();
2612 if (saved_line_for_history)
2613 _rl_free_history_entry (saved_line_for_history);
2614
2615 saved_line_for_history = (HIST_ENTRY *)NULL;
2616 }
2617
2618 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2619 void
2620 _rl_free_history_entry (entry)
2621 HIST_ENTRY *entry;
2622 {
2623 if (!entry)
2624 return;
2625 if (entry->line)
2626 free (entry->line);
2627 free (entry);
2628 }
2629
2630 /* Perhaps put back the current line if it has changed. */
2631 maybe_replace_line ()
2632 {
2633 HIST_ENTRY *temp = current_history ();
2634
2635 /* If the current line has changed, save the changes. */
2636 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2637 {
2638 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2639 free (temp->line);
2640 free (temp);
2641 }
2642 return 0;
2643 }
2644
2645 /* Put back the saved_line_for_history if there is one. */
2646 maybe_unsave_line ()
2647 {
2648 if (saved_line_for_history)
2649 {
2650 int line_len;
2651
2652 line_len = strlen (saved_line_for_history->line);
2653
2654 if (line_len >= rl_line_buffer_len)
2655 rl_extend_line_buffer (line_len);
2656
2657 strcpy (the_line, saved_line_for_history->line);
2658 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
2659 _rl_free_history_entry (saved_line_for_history);
2660 saved_line_for_history = (HIST_ENTRY *)NULL;
2661 rl_end = rl_point = strlen (the_line);
2662 }
2663 else
2664 ding ();
2665 return 0;
2666 }
2667
2668 /* Save the current line in saved_line_for_history. */
2669 maybe_save_line ()
2670 {
2671 if (!saved_line_for_history)
2672 {
2673 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
2674 saved_line_for_history->line = savestring (the_line);
2675 saved_line_for_history->data = (char *)rl_undo_list;
2676 }
2677 return 0;
2678 }
2679
2680 /* **************************************************************** */
2681 /* */
2682 /* History Commands */
2683 /* */
2684 /* **************************************************************** */
2685
2686 /* Meta-< goes to the start of the history. */
2687 rl_beginning_of_history ()
2688 {
2689 return (rl_get_previous_history (1 + where_history ()));
2690 }
2691
2692 /* Meta-> goes to the end of the history. (The current line). */
2693 rl_end_of_history ()
2694 {
2695 maybe_replace_line ();
2696 using_history ();
2697 maybe_unsave_line ();
2698 return 0;
2699 }
2700
2701 /* Move down to the next history line. */
2702 rl_get_next_history (count)
2703 int count;
2704 {
2705 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2706
2707 if (count < 0)
2708 return (rl_get_previous_history (-count));
2709
2710 if (!count)
2711 return 0;
2712
2713 maybe_replace_line ();
2714
2715 while (count)
2716 {
2717 temp = next_history ();
2718 if (!temp)
2719 break;
2720 --count;
2721 }
2722
2723 if (!temp)
2724 maybe_unsave_line ();
2725 else
2726 {
2727 int line_len;
2728
2729 line_len = strlen (temp->line);
2730
2731 if (line_len >= rl_line_buffer_len)
2732 rl_extend_line_buffer (line_len);
2733
2734 strcpy (the_line, temp->line);
2735 rl_undo_list = (UNDO_LIST *)temp->data;
2736 rl_end = rl_point = strlen (the_line);
2737 #if defined (VI_MODE)
2738 if (rl_editing_mode == vi_mode)
2739 rl_point = 0;
2740 #endif /* VI_MODE */
2741 }
2742 return 0;
2743 }
2744
2745 /* Get the previous item out of our interactive history, making it the current
2746 line. If there is no previous history, just ding. */
2747 rl_get_previous_history (count)
2748 int count;
2749 {
2750 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2751 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2752
2753 if (count < 0)
2754 return (rl_get_next_history (-count));
2755
2756 if (!count)
2757 return 0;
2758
2759 /* If we don't have a line saved, then save this one. */
2760 maybe_save_line ();
2761
2762 /* If the current line has changed, save the changes. */
2763 maybe_replace_line ();
2764
2765 while (count)
2766 {
2767 temp = previous_history ();
2768 if (!temp)
2769 break;
2770 else
2771 old_temp = temp;
2772 --count;
2773 }
2774
2775 /* If there was a large argument, and we moved back to the start of the
2776 history, that is not an error. So use the last value found. */
2777 if (!temp && old_temp)
2778 temp = old_temp;
2779
2780 if (!temp)
2781 ding ();
2782 else
2783 {
2784 int line_len;
2785
2786 line_len = strlen (temp->line);
2787
2788 if (line_len >= rl_line_buffer_len)
2789 rl_extend_line_buffer (line_len);
2790
2791 strcpy (the_line, temp->line);
2792 rl_undo_list = (UNDO_LIST *)temp->data;
2793 rl_end = rl_point = line_len;
2794
2795 #if defined (VI_MODE)
2796 if (rl_editing_mode == vi_mode)
2797 rl_point = 0;
2798 #endif /* VI_MODE */
2799 }
2800 return 0;
2801 }
2802
2803 /* Make C be the next command to be executed. */
2804 rl_execute_next (c)
2805 int c;
2806 {
2807 rl_pending_input = c;
2808 return 0;
2809 }
2810
2811 /* **************************************************************** */
2812 /* */
2813 /* The Mark and the Region. */
2814 /* */
2815 /* **************************************************************** */
2816
2817 /* Set the mark at POSITION. */
2818 rl_set_mark (position)
2819 int position;
2820 {
2821 if (position > rl_end)
2822 return -1;
2823
2824 rl_mark = position;
2825 return 0;
2826 }
2827
2828 /* Exchange the position of mark and point. */
2829 rl_exchange_mark_and_point ()
2830 {
2831 if (rl_mark > rl_end)
2832 rl_mark = -1;
2833
2834 if (rl_mark == -1)
2835 {
2836 ding ();
2837 return -1;
2838 }
2839 else
2840 {
2841 int temp = rl_point;
2842
2843 rl_point = rl_mark;
2844 rl_mark = temp;
2845 }
2846 return 0;
2847 }
2848
2849
2850 /* **************************************************************** */
2851 /* */
2852 /* Killing Mechanism */
2853 /* */
2854 /* **************************************************************** */
2855
2856 /* What we assume for a max number of kills. */
2857 #define DEFAULT_MAX_KILLS 10
2858
2859 /* The real variable to look at to find out when to flush kills. */
2860 int rl_max_kills = DEFAULT_MAX_KILLS;
2861
2862 /* Where to store killed text. */
2863 char **rl_kill_ring = (char **)NULL;
2864
2865 /* Where we are in the kill ring. */
2866 int rl_kill_index = 0;
2867
2868 /* How many slots we have in the kill ring. */
2869 int rl_kill_ring_length = 0;
2870
2871 /* How to say that you only want to save a certain amount
2872 of kill material. */
2873 rl_set_retained_kills (num)
2874 int num;
2875 {
2876 return 0;
2877 }
2878
2879 /* The way to kill something. This appends or prepends to the last
2880 kill, if the last command was a kill command. if FROM is less
2881 than TO, then the text is appended, otherwise prepended. If the
2882 last command was not a kill command, then a new slot is made for
2883 this kill. */
2884 rl_kill_text (from, to)
2885 int from, to;
2886 {
2887 int slot;
2888 char *text;
2889
2890 /* Is there anything to kill? */
2891 if (from == to)
2892 {
2893 last_command_was_kill++;
2894 return 0;
2895 }
2896
2897 text = rl_copy_text (from, to);
2898
2899 /* Delete the copied text from the line. */
2900 rl_delete_text (from, to);
2901
2902 /* First, find the slot to work with. */
2903 if (!last_command_was_kill)
2904 {
2905 /* Get a new slot. */
2906 if (!rl_kill_ring)
2907 {
2908 /* If we don't have any defined, then make one. */
2909 rl_kill_ring = (char **)
2910 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
2911 rl_kill_ring[slot = 0] = (char *)NULL;
2912 }
2913 else
2914 {
2915 /* We have to add a new slot on the end, unless we have
2916 exceeded the max limit for remembering kills. */
2917 slot = rl_kill_ring_length;
2918 if (slot == rl_max_kills)
2919 {
2920 register int i;
2921 free (rl_kill_ring[0]);
2922 for (i = 0; i < slot; i++)
2923 rl_kill_ring[i] = rl_kill_ring[i + 1];
2924 }
2925 else
2926 {
2927 slot = rl_kill_ring_length += 1;
2928 rl_kill_ring = (char **)xrealloc (rl_kill_ring, slot * sizeof (char *));
2929 }
2930 rl_kill_ring[--slot] = (char *)NULL;
2931 }
2932 }
2933 else
2934 slot = rl_kill_ring_length - 1;
2935
2936 /* If the last command was a kill, prepend or append. */
2937 if (last_command_was_kill && rl_editing_mode != vi_mode)
2938 {
2939 char *old = rl_kill_ring[slot];
2940 char *new = xmalloc (1 + strlen (old) + strlen (text));
2941
2942 if (from < to)
2943 {
2944 strcpy (new, old);
2945 strcat (new, text);
2946 }
2947 else
2948 {
2949 strcpy (new, text);
2950 strcat (new, old);
2951 }
2952 free (old);
2953 free (text);
2954 rl_kill_ring[slot] = new;
2955 }
2956 else
2957 {
2958 rl_kill_ring[slot] = text;
2959 }
2960 rl_kill_index = slot;
2961 last_command_was_kill++;
2962 return 0;
2963 }
2964
2965 /* Now REMEMBER! In order to do prepending or appending correctly, kill
2966 commands always make rl_point's original position be the FROM argument,
2967 and rl_point's extent be the TO argument. */
2968
2969 /* **************************************************************** */
2970 /* */
2971 /* Killing Commands */
2972 /* */
2973 /* **************************************************************** */
2974
2975 /* Delete the word at point, saving the text in the kill ring. */
2976 rl_kill_word (count)
2977 int count;
2978 {
2979 int orig_point = rl_point;
2980
2981 if (count < 0)
2982 return (rl_backward_kill_word (-count));
2983 else
2984 {
2985 rl_forward_word (count);
2986
2987 if (rl_point != orig_point)
2988 rl_kill_text (orig_point, rl_point);
2989
2990 rl_point = orig_point;
2991 }
2992 return 0;
2993 }
2994
2995 /* Rubout the word before point, placing it on the kill ring. */
2996 rl_backward_kill_word (count, ignore)
2997 int count, ignore;
2998 {
2999 int orig_point = rl_point;
3000
3001 if (count < 0)
3002 return (rl_kill_word (-count));
3003 else
3004 {
3005 rl_backward_word (count);
3006
3007 if (rl_point != orig_point)
3008 rl_kill_text (orig_point, rl_point);
3009 }
3010 return 0;
3011 }
3012
3013 /* Kill from here to the end of the line. If DIRECTION is negative, kill
3014 back to the line start instead. */
3015 rl_kill_line (direction)
3016 int direction;
3017 {
3018 int orig_point = rl_point;
3019
3020 if (direction < 0)
3021 return (rl_backward_kill_line (1));
3022 else
3023 {
3024 rl_end_of_line ();
3025 if (orig_point != rl_point)
3026 rl_kill_text (orig_point, rl_point);
3027 rl_point = orig_point;
3028 }
3029 return 0;
3030 }
3031
3032 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
3033 forwards to the line end instead. */
3034 rl_backward_kill_line (direction)
3035 int direction;
3036 {
3037 int orig_point = rl_point;
3038
3039 if (direction < 0)
3040 return (rl_kill_line (1));
3041 else
3042 {
3043 if (!rl_point)
3044 ding ();
3045 else
3046 {
3047 rl_beg_of_line ();
3048 rl_kill_text (orig_point, rl_point);
3049 }
3050 }
3051 return 0;
3052 }
3053
3054 /* Kill the whole line, no matter where point is. */
3055 rl_kill_full_line (count, ignore)
3056 int count, ignore;
3057 {
3058 rl_begin_undo_group ();
3059 rl_point = 0;
3060 rl_kill_text (rl_point, rl_end);
3061 rl_end_undo_group ();
3062 return 0;
3063 }
3064
3065 /* Yank back the last killed text. This ignores arguments. */
3066 rl_yank (count, ignore)
3067 int count, ignore;
3068 {
3069 if (!rl_kill_ring)
3070 {
3071 rl_abort ();
3072 return -1;
3073 }
3074
3075 rl_set_mark (rl_point);
3076 rl_insert_text (rl_kill_ring[rl_kill_index]);
3077 return 0;
3078 }
3079
3080 /* If the last command was yank, or yank_pop, and the text just
3081 before point is identical to the current kill item, then
3082 delete that text from the line, rotate the index down, and
3083 yank back some other text. */
3084 rl_yank_pop (count, key)
3085 int count, key;
3086 {
3087 int l;
3088
3089 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
3090 !rl_kill_ring)
3091 {
3092 rl_abort ();
3093 return -1;
3094 }
3095
3096 l = strlen (rl_kill_ring[rl_kill_index]);
3097 if (((rl_point - l) >= 0) &&
3098 (strncmp (the_line + (rl_point - l),
3099 rl_kill_ring[rl_kill_index], l) == 0))
3100 {
3101 rl_delete_text ((rl_point - l), rl_point);
3102 rl_point -= l;
3103 rl_kill_index--;
3104 if (rl_kill_index < 0)
3105 rl_kill_index = rl_kill_ring_length - 1;
3106 rl_yank (1, 0);
3107 return 0;
3108 }
3109 else
3110 {
3111 rl_abort ();
3112 return -1;
3113 }
3114 }
3115
3116 /* Yank the COUNTth argument from the previous history line. */
3117 rl_yank_nth_arg (count, ignore)
3118 int count, ignore;
3119 {
3120 register HIST_ENTRY *entry = previous_history ();
3121 char *arg;
3122
3123 if (entry)
3124 next_history ();
3125 else
3126 {
3127 ding ();
3128 return -1;
3129 }
3130
3131 arg = history_arg_extract (count, count, entry->line);
3132 if (!arg || !*arg)
3133 {
3134 ding ();
3135 return -1;
3136 }
3137
3138 rl_begin_undo_group ();
3139
3140 #if defined (VI_MODE)
3141 /* Vi mode always inserts a space before yanking the argument, and it
3142 inserts it right *after* rl_point. */
3143 if (rl_editing_mode == vi_mode)
3144 {
3145 rl_vi_append_mode ();
3146 rl_insert_text (" ");
3147 }
3148 #endif /* VI_MODE */
3149
3150 rl_insert_text (arg);
3151 free (arg);
3152
3153 rl_end_undo_group ();
3154 return 0;
3155 }
3156
3157 /* How to toggle back and forth between editing modes. */
3158 rl_vi_editing_mode (count, key)
3159 int count, key;
3160 {
3161 #if defined (VI_MODE)
3162 rl_editing_mode = vi_mode;
3163 rl_vi_insertion_mode ();
3164 return 0;
3165 #endif /* VI_MODE */
3166 }
3167
3168 rl_emacs_editing_mode (count, key)
3169 int count, key;
3170 {
3171 rl_editing_mode = emacs_mode;
3172 _rl_keymap = emacs_standard_keymap;
3173 return 0;
3174 }
3175
3176
3177 /* **************************************************************** */
3178 /* */
3179 /* USG (System V) Support */
3180 /* */
3181 /* **************************************************************** */
3182
3183 int
3184 rl_getc (stream)
3185 FILE *stream;
3186 {
3187 int result;
3188 unsigned char c;
3189
3190 #if defined (__GO32__)
3191 if (isatty (0))
3192 return (getkey () & 0x7F);
3193 #endif /* __GO32__ */
3194
3195 while (1)
3196 {
3197 result = read (fileno (stream), &c, sizeof (unsigned char));
3198
3199 if (result == sizeof (unsigned char))
3200 return (c);
3201
3202 /* If zero characters are returned, then the file that we are
3203 reading from is empty! Return EOF in that case. */
3204 if (result == 0)
3205 return (EOF);
3206
3207 #if defined (EWOULDBLOCK)
3208 if (errno == EWOULDBLOCK)
3209 {
3210 int flags;
3211
3212 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3213 return (EOF);
3214 if (flags & O_NDELAY)
3215 {
3216 flags &= ~O_NDELAY;
3217 fcntl (fileno (stream), F_SETFL, flags);
3218 continue;
3219 }
3220 continue;
3221 }
3222 #endif /* EWOULDBLOCK */
3223
3224 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3225 if (errno == EAGAIN)
3226 {
3227 int flags;
3228
3229 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3230 return (EOF);
3231 if (flags & O_NONBLOCK)
3232 {
3233 flags &= ~O_NONBLOCK;
3234 fcntl (fileno (stream), F_SETFL, flags);
3235 continue;
3236 }
3237 }
3238 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3239
3240 #if !defined (__GO32__)
3241 /* If the error that we received was SIGINT, then try again,
3242 this is simply an interrupted system call to read ().
3243 Otherwise, some error ocurred, also signifying EOF. */
3244 if (errno != EINTR)
3245 return (EOF);
3246 #endif /* !__GO32__ */
3247 }
3248 }
3249
3250 #if !defined (SHELL)
3251 #ifdef savestring
3252 #undef savestring
3253 #endif
3254 /* Backwards compatibilty, now that savestring has been removed from
3255 all `public' readline header files. */
3256 char * savestring (s)
3257 char *s;
3258 {
3259 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
3260 }
3261 #endif
3262
3263 #if defined (STATIC_MALLOC)
3264
3265 /* **************************************************************** */
3266 /* */
3267 /* xmalloc and xrealloc () */
3268 /* */
3269 /* **************************************************************** */
3270
3271 static void memory_error_and_abort ();
3272
3273 static char *
3274 xmalloc (bytes)
3275 int bytes;
3276 {
3277 char *temp = (char *)malloc (bytes);
3278
3279 if (!temp)
3280 memory_error_and_abort ();
3281 return (temp);
3282 }
3283
3284 static char *
3285 xrealloc (pointer, bytes)
3286 char *pointer;
3287 int bytes;
3288 {
3289 char *temp;
3290
3291 if (!pointer)
3292 temp = (char *)malloc (bytes);
3293 else
3294 temp = (char *)realloc (pointer, bytes);
3295
3296 if (!temp)
3297 memory_error_and_abort ();
3298
3299 return (temp);
3300 }
3301
3302 static void
3303 memory_error_and_abort ()
3304 {
3305 fprintf (stderr, "readline: Out of virtual memory!\n");
3306 abort ();
3307 }
3308 #endif /* STATIC_MALLOC */
3309
3310
3311 /* **************************************************************** */
3312 /* */
3313 /* Testing Readline */
3314 /* */
3315 /* **************************************************************** */
3316
3317 #if defined (TEST)
3318
3319 main ()
3320 {
3321 HIST_ENTRY **history_list ();
3322 char *temp = (char *)NULL;
3323 char *prompt = "readline% ";
3324 int done = 0;
3325
3326 while (!done)
3327 {
3328 temp = readline (prompt);
3329
3330 /* Test for EOF. */
3331 if (!temp)
3332 exit (1);
3333
3334 /* If there is anything on the line, print it and remember it. */
3335 if (*temp)
3336 {
3337 fprintf (stderr, "%s\r\n", temp);
3338 add_history (temp);
3339 }
3340
3341 /* Check for `command' that we handle. */
3342 if (strcmp (temp, "quit") == 0)
3343 done = 1;
3344
3345 if (strcmp (temp, "list") == 0)
3346 {
3347 HIST_ENTRY **list = history_list ();
3348 register int i;
3349 if (list)
3350 {
3351 for (i = 0; list[i]; i++)
3352 {
3353 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3354 free (list[i]->line);
3355 }
3356 free (list);
3357 }
3358 }
3359 free (temp);
3360 }
3361 }
3362
3363 #endif /* TEST */
3364
3365
3366 /*
3367 * Local variables:
3368 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
3369 * end:
3370 */