]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - readline/readline.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / readline / readline.c
1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4 /* Copyright 1987, 1989, 1991, 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
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>
31
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 */
36
37 #if defined (HAVE_UNISTD_H)
38 # include <unistd.h>
39 #endif
40 \f
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 */
46
47 extern char * getenv ();
48
49 #include <setjmp.h>
50 #include <sys/stat.h>
51
52 /* System-specific feature definitions and include files. */
53 #include "rldefs.h"
54
55 /* Some standard library routines. */
56 #include "readline.h"
57 #include "history.h"
58
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. */
63
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 ();
70
71 /* External redisplay functions and variables from display.c */
72 extern void rl_redisplay ();
73 extern void _rl_move_vert ();
74
75 extern void _rl_erase_at_end_of_line ();
76 extern void _rl_move_cursor_relative ();
77
78 extern int _rl_vis_botlin;
79 extern int _rl_last_c_pos;
80 extern int rl_display_fixed;
81
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;
88
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 ();
95
96 #if !defined (__GO32__)
97 static void readline_default_bindings ();
98 #endif /* !__GO32__ */
99
100 #if defined (__GO32__)
101 # include <sys/pc.h>
102 # undef HANDLE_SIGNALS
103 #endif /* __GO32__ */
104
105 #if defined (STATIC_MALLOC)
106 static char *xmalloc (), *xrealloc ();
107 #else
108 extern char *xmalloc (), *xrealloc ();
109 #endif /* STATIC_MALLOC */
110
111 \f
112 /* **************************************************************** */
113 /* */
114 /* Line editing input utility */
115 /* */
116 /* **************************************************************** */
117
118 static char *LibraryVersion = "2.0 (Cygnus)";
119
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;
123
124 /* The current style of editing. */
125 int rl_editing_mode = emacs_mode;
126
127 /* Non-zero if the previous command was a kill command. */
128 static int last_command_was_kill = 0;
129
130 /* The current value of the numeric argument specified by the user. */
131 int rl_numeric_arg = 1;
132
133 /* Non-zero if an argument was typed. */
134 int rl_explicit_arg = 0;
135
136 /* Temporary value used while generating the argument. */
137 int rl_arg_sign = 1;
138
139 /* Non-zero means we have been called at least once before. */
140 static int rl_initialized = 0;
141
142 /* If non-zero, this program is running in an EMACS buffer. */
143 static char *running_in_emacs = (char *)NULL;
144
145 /* The current offset in the current input line. */
146 int rl_point;
147
148 /* Mark in the current input line. */
149 int rl_mark;
150
151 /* Length of the current input line. */
152 int rl_end;
153
154 /* Make this non-zero to return the current input_line. */
155 int rl_done;
156
157 /* The last function executed by readline. */
158 Function *rl_last_func = (Function *)NULL;
159
160 /* Top level environment for readline_internal (). */
161 static jmp_buf readline_top_level;
162
163 /* The streams we interact with. */
164 static FILE *in_stream, *out_stream;
165
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;
169
170 /* Non-zero means echo characters as they are read. */
171 int readline_echoing_p = 1;
172
173 /* Current prompt. */
174 char *rl_prompt;
175
176 /* The number of characters read in order to type this complete command. */
177 int rl_key_sequence_length = 0;
178
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;
182
183 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
184 static char *the_line;
185
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');
189
190 /* Non-zero makes this the next keystroke to read. */
191 int rl_pending_input = 0;
192
193 /* Pointer to a useful terminal name. */
194 char *rl_terminal_name = (char *)NULL;
195
196 /* Non-zero means to always use horizontal scrolling in line display. */
197 int _rl_horizontal_scroll_mode = 0;
198
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;
202
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;
206
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
211
212 \f
213 /* **************************************************************** */
214 /* */
215 /* `Forward' declarations */
216 /* */
217 /* **************************************************************** */
218
219 /* Non-zero means do not parse any lines other than comments and
220 parser directives. */
221 unsigned char _rl_parsing_conditionalized_out = 0;
222
223 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
224 static int defining_kbd_macro = 0;
225
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;
230
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;
234 \f
235 /* **************************************************************** */
236 /* */
237 /* Top Level Functions */
238 /* */
239 /* **************************************************************** */
240
241 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
242 int _rl_meta_flag = 0; /* Forward declaration */
243
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;
249 {
250 char *readline_internal ();
251 char *value;
252
253 rl_prompt = prompt;
254
255 /* If we are at EOF return a NULL string. */
256 if (rl_pending_input == EOF)
257 {
258 rl_pending_input = 0;
259 return ((char *)NULL);
260 }
261
262 rl_initialize ();
263 rl_prep_terminal (_rl_meta_flag);
264
265 #if defined (HANDLE_SIGNALS)
266 rl_set_signals ();
267 #endif
268
269 value = readline_internal ();
270 rl_deprep_terminal ();
271
272 #if defined (HANDLE_SIGNALS)
273 rl_clear_signals ();
274 #endif
275
276 return (value);
277 }
278
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 ()
284 {
285 int lastc, c, eof_found;
286
287 in_stream = rl_instream;
288 out_stream = rl_outstream;
289
290 lastc = -1;
291 eof_found = 0;
292
293 if (rl_startup_hook)
294 (*rl_startup_hook) ();
295
296 if (!readline_echoing_p)
297 {
298 if (rl_prompt)
299 {
300 fprintf (out_stream, "%s", rl_prompt);
301 fflush (out_stream);
302 }
303 }
304 else
305 {
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 */
312 }
313
314 while (!rl_done)
315 {
316 int lk = last_command_was_kill;
317 int code;
318
319 code = setjmp (readline_top_level);
320
321 if (code)
322 rl_redisplay ();
323
324 if (!rl_pending_input)
325 {
326 /* Then initialize the argument and number of keys read. */
327 rl_init_argument ();
328 rl_key_sequence_length = 0;
329 }
330
331 c = rl_read_key ();
332
333 /* EOF typed to a non-blank line is a <NL>. */
334 if (c == EOF && rl_end)
335 c = NEWLINE;
336
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)
340 {
341 eof_found = 1;
342 break;
343 }
344
345 lastc = c;
346 rl_dispatch (c, _rl_keymap);
347
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)
352 {
353 if (lk == last_command_was_kill)
354 last_command_was_kill = 0;
355 }
356
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 */
363
364 if (!rl_done)
365 rl_redisplay ();
366 }
367
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. */
370 {
371 HIST_ENTRY *entry = current_history ();
372
373 if (entry && rl_undo_list)
374 {
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);
380
381 strcpy (the_line, temp);
382 free (temp);
383 }
384 }
385
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 ();
390
391 if (eof_found)
392 return (char *)NULL;
393 else
394 return (savestring (the_line));
395 }
396 \f
397 /* **************************************************************** */
398 /* */
399 /* Character Input Buffering */
400 /* */
401 /* **************************************************************** */
402
403 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
404 static unsigned char ibuffer[512];
405
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;
409
410 #define any_typein (push_index != pop_index)
411
412 /* Add KEY to the buffer of characters to be read. */
413 rl_stuff_char (key)
414 int key;
415 {
416 if (key == EOF)
417 {
418 key = NEWLINE;
419 rl_pending_input = EOF;
420 }
421 ibuffer[push_index++] = key;
422 if (push_index >= ibuffer_len)
423 push_index = 0;
424 }
425
426 /* Return the amount of space available in the
427 buffer for stuffing characters. */
428 int
429 ibuffer_space ()
430 {
431 if (pop_index > push_index)
432 return (pop_index - push_index);
433 else
434 return (ibuffer_len - (push_index - pop_index));
435 }
436
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. */
440 int
441 rl_get_char (key)
442 int *key;
443 {
444 if (push_index == pop_index)
445 return (0);
446
447 *key = ibuffer[pop_index++];
448
449 if (pop_index >= ibuffer_len)
450 pop_index = 0;
451
452 return (1);
453 }
454
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. */
458 int
459 rl_unget_char (key)
460 int key;
461 {
462 if (ibuffer_space ())
463 {
464 pop_index--;
465 if (pop_index < 0)
466 pop_index = ibuffer_len - 1;
467 ibuffer[pop_index] = key;
468 return (1);
469 }
470 return (0);
471 }
472
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 ()
477 {
478 #if defined (__GO32__)
479 char input;
480
481 if (isatty (0))
482 {
483 int i = rl_getc ();
484
485 if (i != EOF)
486 rl_stuff_char (i);
487 }
488 else if (kbhit () && ibuffer_space ())
489 rl_stuff_char (getkey ());
490 #else /* !__GO32__ */
491
492 int tty = fileno (in_stream);
493 register int tem, result = -1;
494 int chars_avail;
495 char input;
496
497 #if defined (FIONREAD)
498 result = ioctl (tty, FIONREAD, &chars_avail);
499 #endif
500
501 #if defined (O_NDELAY)
502 if (result == -1)
503 {
504 int flags;
505
506 flags = fcntl (tty, F_GETFL, 0);
507
508 fcntl (tty, F_SETFL, (flags | O_NDELAY));
509 chars_avail = read (tty, &input, 1);
510
511 fcntl (tty, F_SETFL, flags);
512 if (chars_avail == -1 && errno == EAGAIN)
513 return;
514 }
515 #endif /* O_NDELAY */
516
517 /* If there's nothing available, don't waste time trying to read
518 something. */
519 if (chars_avail == 0)
520 return;
521
522 tem = ibuffer_space ();
523
524 if (chars_avail > tem)
525 chars_avail = tem;
526
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;
533
534 if (result != -1)
535 {
536 while (chars_avail--)
537 rl_stuff_char (rl_getc (in_stream));
538 }
539 else
540 {
541 if (chars_avail)
542 rl_stuff_char (input);
543 }
544 #endif /* !__GO32__ */
545 }
546
547 static int next_macro_key ();
548 /* Read a key, including pending input. */
549 int
550 rl_read_key ()
551 {
552 int c;
553
554 rl_key_sequence_length++;
555
556 if (rl_pending_input)
557 {
558 c = rl_pending_input;
559 rl_pending_input = 0;
560 }
561 else
562 {
563 /* If input is coming from a macro, then use that. */
564 if (c = next_macro_key ())
565 return (c);
566
567 /* If the user has an event function, then call it periodically. */
568 if (rl_event_hook)
569 {
570 while (rl_event_hook && !rl_get_char (&c))
571 {
572 (*rl_event_hook) ();
573 rl_gather_tyi ();
574 }
575 }
576 else
577 {
578 if (!rl_get_char (&c))
579 c = rl_getc (in_stream);
580 }
581 }
582
583 return (c);
584 }
585
586 /* Found later in this file. */
587 static void add_macro_char (), with_macro_input ();
588
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;
596 {
597 #if defined (VI_MODE)
598 extern int _rl_vi_last_command, _rl_vi_last_repeat, _rl_vi_last_arg_sign;
599 #endif
600
601 if (defining_kbd_macro)
602 add_macro_char (key);
603
604 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
605 {
606 if (map[ESC].type == ISKMAP)
607 {
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);
616 }
617 else
618 ding ();
619 return;
620 }
621
622 switch (map[key].type)
623 {
624 case ISFUNC:
625 {
626 Function *func = map[key].function;
627
628 if (func != (Function *)NULL)
629 {
630 /* Special case rl_do_lowercase_version (). */
631 if (func == rl_do_lowercase_version)
632 {
633 rl_dispatch (to_lower (key), map);
634 return;
635 }
636
637 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
638
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;
644 }
645 else
646 {
647 rl_abort ();
648 return;
649 }
650 }
651 break;
652
653 case ISKMAP:
654 if (map[key].function != (Function *)NULL)
655 {
656 int newkey;
657
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 */
670 }
671 else
672 {
673 rl_abort ();
674 return;
675 }
676 break;
677
678 case ISMACR:
679 if (map[key].function != (Function *)NULL)
680 {
681 char *macro;
682
683 macro = savestring ((char *)map[key].function);
684 with_macro_input (macro);
685 return;
686 }
687 break;
688 }
689 #if defined (VI_MODE)
690 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
691 rl_vi_textmod_command (key))
692 {
693 _rl_vi_last_command = key;
694 _rl_vi_last_repeat = rl_numeric_arg;
695 _rl_vi_last_arg_sign = rl_arg_sign;
696 }
697 #endif
698 }
699
700 \f
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 index;
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->index = 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->index;
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 = (char *)xmalloc (current_macro_size = 25);
813 else
814 current_macro =
815 (char *)xrealloc (current_macro, current_macro_size += 25);
816 }
817
818 current_macro[current_macro_index++] = c;
819 current_macro[current_macro_index] = '\0';
820 }
821
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;
830 {
831 if (defining_kbd_macro)
832 rl_abort ();
833
834 if (rl_explicit_arg)
835 {
836 if (current_macro)
837 with_macro_input (savestring (current_macro));
838 }
839 else
840 current_macro_index = 0;
841
842 defining_kbd_macro = 1;
843 }
844
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;
850 {
851 if (!defining_kbd_macro)
852 rl_abort ();
853
854 current_macro_index -= (rl_key_sequence_length - 1);
855 current_macro[current_macro_index] = '\0';
856
857 defining_kbd_macro = 0;
858
859 rl_call_last_kbd_macro (--count, 0);
860 }
861
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;
866 {
867 if (!current_macro)
868 rl_abort ();
869
870 if (defining_kbd_macro)
871 {
872 ding (); /* no recursive macros */
873 current_macro[--current_macro_index] = '\0'; /* erase this char */
874 return 0;
875 }
876
877 while (count--)
878 with_macro_input (savestring (current_macro));
879 }
880
881 void
882 _rl_kill_kbd_macro ()
883 {
884 if (current_macro)
885 {
886 free (current_macro);
887 current_macro = (char *) NULL;
888 }
889 current_macro_size = current_macro_index = 0;
890
891 if (executing_macro)
892 {
893 free (executing_macro);
894 executing_macro = (char *) NULL;
895 }
896 executing_macro_index = 0;
897
898 defining_kbd_macro = 0;
899 }
900
901 \f
902 /* **************************************************************** */
903 /* */
904 /* Initializations */
905 /* */
906 /* **************************************************************** */
907
908 /* Initliaze readline (and terminal if not already). */
909 rl_initialize ()
910 {
911 /* If we have never been called before, initialize the
912 terminal and data structures. */
913 if (!rl_initialized)
914 {
915 readline_initialize_everything ();
916 rl_initialized++;
917 }
918
919 /* Initalize the current line information. */
920 rl_point = rl_end = 0;
921 the_line = rl_line_buffer;
922 the_line[0] = 0;
923
924 /* We aren't done yet. We haven't even gotten started yet! */
925 rl_done = 0;
926
927 /* Tell the history routines what is going on. */
928 start_using_history ();
929
930 /* Make the display buffer match the state of the line. */
931 rl_reset_line_state ();
932
933 /* No such function typed yet. */
934 rl_last_func = (Function *)NULL;
935
936 /* Parsing of key-bindings begins in an enabled state. */
937 _rl_parsing_conditionalized_out = 0;
938 }
939
940 /* Initialize the entire state of the world. */
941 readline_initialize_everything ()
942 {
943 /* Find out if we are running in Emacs. */
944 running_in_emacs = getenv ("EMACS");
945
946 /* Set up input and output if they are not already set up. */
947 if (!rl_instream)
948 rl_instream = stdin;
949
950 if (!rl_outstream)
951 rl_outstream = stdout;
952
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;
957
958 /* Allocate data structures. */
959 if (!rl_line_buffer)
960 rl_line_buffer =
961 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
962
963 /* Initialize the terminal interface. */
964 init_terminal_io ((char *)NULL);
965
966 #if !defined (__GO32__)
967 /* Bind tty characters to readline functions. */
968 readline_default_bindings ();
969 #endif /* !__GO32__ */
970
971 /* Initialize the function names. */
972 rl_initialize_funmap ();
973
974 /* Read in the init file. */
975 rl_read_init_file ((char *)NULL);
976
977 /* If the completion parser's default word break characters haven't
978 been set yet, then do so now. */
979 {
980 if (rl_completer_word_break_characters == (char *)NULL)
981 rl_completer_word_break_characters = rl_basic_word_break_characters;
982 }
983 }
984
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 ()
991 {
992 rltty_set_default_bindings (_rl_keymap);
993 }
994 #endif /* !__GO32__ */
995
996 \f
997 /* **************************************************************** */
998 /* */
999 /* Numeric Arguments */
1000 /* */
1001 /* **************************************************************** */
1002
1003 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1004
1005 /* Add the current digit to the argument in progress. */
1006 rl_digit_argument (ignore, key)
1007 int ignore, key;
1008 {
1009 rl_pending_input = key;
1010 rl_digit_loop ();
1011 }
1012
1013 /* What to do when you abort reading an argument. */
1014 rl_discard_argument ()
1015 {
1016 ding ();
1017 rl_clear_message ();
1018 rl_init_argument ();
1019 }
1020
1021 /* Create a default argument. */
1022 rl_init_argument ()
1023 {
1024 rl_numeric_arg = rl_arg_sign = 1;
1025 rl_explicit_arg = 0;
1026 }
1027
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 ()
1032 {
1033 rl_numeric_arg *= 4;
1034 rl_digit_loop ();
1035 }
1036
1037 rl_digit_loop ()
1038 {
1039 int key, c;
1040 while (1)
1041 {
1042 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1043 key = c = rl_read_key ();
1044
1045 if (_rl_keymap[c].type == ISFUNC &&
1046 _rl_keymap[c].function == rl_universal_argument)
1047 {
1048 rl_numeric_arg *= 4;
1049 continue;
1050 }
1051 c = UNMETA (c);
1052 if (numeric (c))
1053 {
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;
1059 }
1060 else
1061 {
1062 if (c == '-' && !rl_explicit_arg)
1063 {
1064 rl_numeric_arg = 1;
1065 rl_arg_sign = -1;
1066 }
1067 else
1068 {
1069 rl_clear_message ();
1070 rl_dispatch (key, _rl_keymap);
1071 return;
1072 }
1073 }
1074 }
1075 }
1076 \f
1077 /* **************************************************************** */
1078 /* */
1079 /* Terminal and Termcap */
1080 /* */
1081 /* **************************************************************** */
1082
1083 static char *term_buffer = (char *)NULL;
1084 static char *term_string_buffer = (char *)NULL;
1085
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
1091
1092 #if !defined (__linux__)
1093 char PC;
1094 char *BC, *UP;
1095 #endif /* __linux__ */
1096
1097 /* Some strings to control terminal actions. These are output by tputs (). */
1098 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1099
1100 int screenwidth, screenheight;
1101
1102 /* Non-zero if we determine that the terminal can do character insertion. */
1103 int terminal_can_insert = 0;
1104
1105 /* How to insert characters. */
1106 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1107
1108 /* How to delete characters. */
1109 char *term_dc, *term_DC;
1110
1111 #if defined (HACK_TERMCAP_MOTION)
1112 char *term_forward_char;
1113 #endif /* HACK_TERMCAP_MOTION */
1114
1115 /* How to go up a line. */
1116 char *term_up;
1117
1118 /* True if we have funny auto-line-wrap ("am" and "xn"). */
1119 int term_xn;
1120
1121 /* A visible bell, if the terminal can be made to flash the screen. */
1122 char *visible_bell;
1123
1124 /* Non-zero means that this terminal has a meta key. */
1125 int term_has_meta;
1126
1127 /* The string to write to turn on the meta key, if this term has one. */
1128 char *term_mm;
1129
1130 /* The string to write to turn off the meta key, if this term has one. */
1131 char *term_mo;
1132
1133 /* The key sequences output by the arrow keys, if this terminal has any. */
1134 char *term_ku, *term_kd, *term_kr, *term_kl;
1135
1136 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1137 has changed. */
1138 rl_reset_terminal (terminal_name)
1139 char *terminal_name;
1140 {
1141 init_terminal_io (terminal_name);
1142 }
1143
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;
1151 {
1152 #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
1153 struct winsize window_size;
1154 #endif /* TIOCGWINSZ */
1155
1156 #if defined (TIOCGWINSZ) && !defined (TIOCGWINSZ_BROKEN)
1157 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1158 {
1159 screenwidth = (int) window_size.ws_col;
1160 screenheight = (int) window_size.ws_row;
1161 }
1162 #endif /* TIOCGWINSZ */
1163
1164 /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1165 is unset. */
1166 if (screenwidth <= 0)
1167 {
1168 char *sw;
1169
1170 if (!ignore_env && (sw = getenv ("COLUMNS")))
1171 screenwidth = atoi (sw);
1172
1173 if (screenwidth <= 0 && term_string_buffer)
1174 screenwidth = tgetnum ("co");
1175 }
1176
1177 /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1178 is unset. */
1179 if (screenheight <= 0)
1180 {
1181 char *sh;
1182
1183 if (!ignore_env && (sh = getenv ("LINES")))
1184 screenheight = atoi (sh);
1185
1186 if (screenheight <= 0 && term_string_buffer)
1187 screenheight = tgetnum ("li");
1188 }
1189
1190 /* If all else fails, default to 80x24 terminal. */
1191 if (screenwidth <= 0)
1192 screenwidth = 80;
1193
1194 if (screenheight <= 0)
1195 screenheight = 24;
1196
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
1202
1203 /* If we don't have xn (most modern terminals do),
1204 don't use the last column. */
1205 if (!term_xn)
1206 screenwidth--;
1207 }
1208
1209 init_terminal_io (terminal_name)
1210 char *terminal_name;
1211 {
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;
1218
1219 /* Does the __GO32__ have a meta key? I don't know. */
1220 term_has_meta = 0;
1221 term_mm = term_mo = (char *)NULL;
1222
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;
1225
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__ */
1232
1233 char *term, *buffer;
1234 int tty;
1235
1236 term = terminal_name ? terminal_name : getenv ("TERM");
1237
1238 if (!term_string_buffer)
1239 term_string_buffer = (char *)xmalloc (2048);
1240
1241 if (!term_buffer)
1242 term_buffer = (char *)xmalloc (2048);
1243
1244 buffer = term_string_buffer;
1245
1246 term_clrpag = term_cr = term_clreol = (char *)NULL;
1247
1248 if (!term)
1249 term = "dumb";
1250
1251 if (tgetent (term_buffer, term) <= 0)
1252 {
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;
1265 }
1266
1267 BC = tgetstr ("pc", &buffer);
1268 PC = buffer ? *buffer : 0;
1269
1270 term_backspace = tgetstr ("le", &buffer);
1271
1272 term_cr = tgetstr ("cr", &buffer);
1273 term_clreol = tgetstr ("ce", &buffer);
1274 term_clrpag = tgetstr ("cl", &buffer);
1275
1276 if (!term_cr)
1277 term_cr = "\r";
1278
1279 #if defined (HACK_TERMCAP_MOTION)
1280 term_forward_char = tgetstr ("nd", &buffer);
1281 #endif /* HACK_TERMCAP_MOTION */
1282
1283 if (rl_instream)
1284 tty = fileno (rl_instream);
1285 else
1286 tty = 0;
1287
1288 screenwidth = screenheight = 0;
1289
1290 term_xn = tgetflag ("am") && tgetflag ("xn");
1291
1292 _rl_set_screen_size (tty, 0);
1293
1294 term_im = tgetstr ("im", &buffer);
1295 term_ei = tgetstr ("ei", &buffer);
1296 term_IC = tgetstr ("IC", &buffer);
1297 term_ic = tgetstr ("ic", &buffer);
1298
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);
1304
1305 term_up = tgetstr ("up", &buffer);
1306 term_dc = tgetstr ("dc", &buffer);
1307 term_DC = tgetstr ("DC", &buffer);
1308
1309 visible_bell = tgetstr ("vb", &buffer);
1310
1311 /* Check to see if this terminal has a meta key. */
1312 term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1313 if (term_has_meta)
1314 {
1315 term_mm = tgetstr ("mm", &buffer);
1316 term_mo = tgetstr ("mo", &buffer);
1317 }
1318 else
1319 {
1320 term_mm = (char *)NULL;
1321 term_mo = (char *)NULL;
1322 }
1323
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);
1330
1331 if (term_ku)
1332 {
1333 Function *func;
1334
1335 func = rl_function_of_keyseq (term_ku, _rl_keymap, (int *)NULL);
1336
1337 if (!func || func == rl_do_lowercase_version)
1338 rl_set_key (term_ku, rl_get_previous_history, _rl_keymap);
1339 }
1340
1341 if (term_kd)
1342 {
1343 Function *func;
1344
1345 func = rl_function_of_keyseq (term_kd, _rl_keymap, (int *)NULL);
1346
1347 if (!func || func == rl_do_lowercase_version)
1348 rl_set_key (term_kd, rl_get_next_history, _rl_keymap);
1349 }
1350
1351 if (term_kr)
1352 {
1353 Function *func;
1354
1355 func = rl_function_of_keyseq (term_kr, _rl_keymap, (int *)NULL);
1356
1357 if (!func || func == rl_do_lowercase_version)
1358 rl_set_key (term_kr, rl_forward, _rl_keymap);
1359 }
1360
1361 if (term_kl)
1362 {
1363 Function *func;
1364
1365 func = rl_function_of_keyseq (term_kl, _rl_keymap, (int *)NULL);
1366
1367 if (!func || func == rl_do_lowercase_version)
1368 rl_set_key (term_kl, rl_backward, _rl_keymap);
1369 }
1370 #endif /* !__GO32__ */
1371 return 0;
1372 }
1373
1374 /* A function for the use of tputs () */
1375 int
1376 _rl_output_character_function (c)
1377 int c;
1378 {
1379 return putc (c, out_stream);
1380 }
1381
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;
1387 {
1388 fwrite (string, 1, count, out_stream);
1389 }
1390
1391 /* Move the cursor back. */
1392 backspace (count)
1393 int count;
1394 {
1395 register int i;
1396
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);
1405 }
1406
1407 /* Move to the start of the next line. */
1408 crlf ()
1409 {
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);
1414 }
1415
1416 \f
1417 /* **************************************************************** */
1418 /* */
1419 /* Utility Functions */
1420 /* */
1421 /* **************************************************************** */
1422
1423 /* Return 0 if C is not a member of the class of characters that belong
1424 in words, or 1 if it is. */
1425
1426 int allow_pathname_alphabetic_chars = 0;
1427 char *pathname_alphabetic_chars = "/-_=~.#$";
1428
1429 int
1430 alphabetic (c)
1431 int c;
1432 {
1433 if (pure_alphabetic (c) || (numeric (c)))
1434 return (1);
1435
1436 if (allow_pathname_alphabetic_chars)
1437 return (strchr (pathname_alphabetic_chars, c) != NULL);
1438 else
1439 return (0);
1440 }
1441
1442 /* Return non-zero if C is a numeric character. */
1443 int
1444 numeric (c)
1445 int c;
1446 {
1447 return (c >= '0' && c <= '9');
1448 }
1449
1450 /* Ring the terminal bell. */
1451 int
1452 ding ()
1453 {
1454 if (readline_echoing_p)
1455 {
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__ */
1461 {
1462 fprintf (stderr, "\007");
1463 fflush (stderr);
1464 }
1465 }
1466 return (-1);
1467 }
1468
1469 /* How to abort things. */
1470 rl_abort ()
1471 {
1472 ding ();
1473 rl_clear_message ();
1474 rl_init_argument ();
1475 rl_pending_input = 0;
1476
1477 defining_kbd_macro = 0;
1478 while (executing_macro)
1479 pop_executing_macro ();
1480
1481 rl_last_func = (Function *)NULL;
1482 longjmp (readline_top_level, 1);
1483 }
1484
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;
1490 {
1491 register int length;
1492 char *copy;
1493
1494 /* Fix it if the caller is confused. */
1495 if (from > to)
1496 {
1497 int t = from;
1498 from = to;
1499 to = t;
1500 }
1501
1502 length = to - from;
1503 copy = (char *)xmalloc (1 + length);
1504 strncpy (copy, the_line + from, length);
1505 copy[length] = '\0';
1506 return (copy);
1507 }
1508
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;
1514 {
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);
1519
1520 the_line = rl_line_buffer;
1521 }
1522
1523 \f
1524 /* **************************************************************** */
1525 /* */
1526 /* Insert and Delete */
1527 /* */
1528 /* **************************************************************** */
1529
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;
1535 {
1536 register int i, l = strlen (string);
1537
1538 if (rl_end + l >= rl_line_buffer_len)
1539 rl_extend_line_buffer (rl_end + l);
1540
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);
1544
1545 /* Remember how to undo this if we aren't undoing something. */
1546 if (!doing_an_undo)
1547 {
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);
1557 }
1558 rl_point += l;
1559 rl_end += l;
1560 the_line[rl_end] = '\0';
1561 }
1562
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;
1567 {
1568 register char *text;
1569
1570 /* Fix it if the caller is confused. */
1571 if (from > to)
1572 {
1573 int t = from;
1574 from = to;
1575 to = t;
1576 }
1577 text = rl_copy_text (from, to);
1578 strncpy (the_line + from, the_line + to, rl_end - to);
1579
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);
1585
1586 rl_end -= (to - from);
1587 the_line[rl_end] = '\0';
1588 }
1589
1590 \f
1591 /* **************************************************************** */
1592 /* */
1593 /* Readline character functions */
1594 /* */
1595 /* **************************************************************** */
1596
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. */
1599
1600 /* Note that:
1601
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.
1604
1605 rl_point is the place in the string where the cursor is. Sometimes
1606 this is the same as rl_end.
1607
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.
1611 */
1612
1613 \f
1614 /* **************************************************************** */
1615 /* */
1616 /* Movement Commands */
1617 /* */
1618 /* **************************************************************** */
1619
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. */
1624
1625 /* Move forward COUNT characters. */
1626 rl_forward (count)
1627 int count;
1628 {
1629 if (count < 0)
1630 rl_backward (-count);
1631 else
1632 while (count)
1633 {
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 */
1639 {
1640 ding ();
1641 return 0;
1642 }
1643 else
1644 rl_point++;
1645 --count;
1646 }
1647 return 0;
1648 }
1649
1650 /* Move backward COUNT characters. */
1651 rl_backward (count)
1652 int count;
1653 {
1654 if (count < 0)
1655 rl_forward (-count);
1656 else
1657 while (count)
1658 {
1659 if (!rl_point)
1660 {
1661 ding ();
1662 return 0;
1663 }
1664 else
1665 --rl_point;
1666 --count;
1667 }
1668 return 0;
1669 }
1670
1671 /* Move to the beginning of the line. */
1672 rl_beg_of_line ()
1673 {
1674 rl_point = 0;
1675 return 0;
1676 }
1677
1678 /* Move to the end of the line. */
1679 rl_end_of_line ()
1680 {
1681 rl_point = rl_end;
1682 return 0;
1683 }
1684
1685 /* Move forward a word. We do what Emacs does. */
1686 rl_forward_word (count)
1687 int count;
1688 {
1689 int c;
1690
1691 if (count < 0)
1692 {
1693 rl_backward_word (-count);
1694 return 0;
1695 }
1696
1697 while (count)
1698 {
1699 if (rl_point == rl_end)
1700 return 0;
1701
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))
1706 {
1707 while (++rl_point < rl_end)
1708 {
1709 c = the_line[rl_point];
1710 if (alphabetic (c)) break;
1711 }
1712 }
1713 if (rl_point == rl_end) return;
1714 while (++rl_point < rl_end)
1715 {
1716 c = the_line[rl_point];
1717 if (!alphabetic (c)) break;
1718 }
1719 --count;
1720 }
1721 return 0;
1722 }
1723
1724 /* Move backward a word. We do what Emacs does. */
1725 rl_backward_word (count)
1726 int count;
1727 {
1728 int c;
1729
1730 if (count < 0)
1731 {
1732 rl_forward_word (-count);
1733 return 0;
1734 }
1735
1736 while (count)
1737 {
1738 if (!rl_point)
1739 return 0;
1740
1741 /* Like rl_forward_word (), except that we look at the characters
1742 just before point. */
1743
1744 c = the_line[rl_point - 1];
1745 if (!alphabetic (c))
1746 {
1747 while (--rl_point)
1748 {
1749 c = the_line[rl_point - 1];
1750 if (alphabetic (c)) break;
1751 }
1752 }
1753
1754 while (rl_point)
1755 {
1756 c = the_line[rl_point - 1];
1757 if (!alphabetic (c))
1758 break;
1759 else --rl_point;
1760 }
1761 --count;
1762 }
1763 return 0;
1764 }
1765
1766 /* Clear the current line. Numeric argument to C-l does this. */
1767 rl_refresh_line ()
1768 {
1769 int curr_line = _rl_last_c_pos / screenwidth;
1770
1771 _rl_move_vert (curr_line);
1772 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1773
1774 #if defined (__GO32__)
1775 {
1776 int row, col, width, row_start;
1777
1778 ScreenGetCursor (&row, &col);
1779 width = ScreenCols ();
1780 row_start = ScreenPrimary + (row * width);
1781 memset (row_start + col, 0, (width - col) * 2);
1782 }
1783 #else /* !__GO32__ */
1784 if (term_clreol)
1785 tputs (term_clreol, 1, _rl_output_character_function);
1786 #endif /* !__GO32__ */
1787
1788 rl_forced_update_display ();
1789 rl_display_fixed = 1;
1790
1791 return 0;
1792 }
1793
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 ()
1798 {
1799 if (rl_explicit_arg)
1800 {
1801 rl_refresh_line ();
1802 return 0;
1803 }
1804
1805 #if !defined (__GO32__)
1806 if (term_clrpag)
1807 tputs (term_clrpag, 1, _rl_output_character_function);
1808 else
1809 #endif /* !__GO32__ */
1810 crlf ();
1811
1812 rl_forced_update_display ();
1813 rl_display_fixed = 1;
1814
1815 return 0;
1816 }
1817
1818 rl_arrow_keys (count, c)
1819 int count, c;
1820 {
1821 int ch;
1822
1823 ch = rl_read_key ();
1824
1825 switch (to_upper (ch))
1826 {
1827 case 'A':
1828 rl_get_previous_history (count);
1829 break;
1830
1831 case 'B':
1832 rl_get_next_history (count);
1833 break;
1834
1835 case 'C':
1836 rl_forward (count);
1837 break;
1838
1839 case 'D':
1840 rl_backward (count);
1841 break;
1842
1843 default:
1844 ding ();
1845 }
1846 return 0;
1847 }
1848
1849 \f
1850 /* **************************************************************** */
1851 /* */
1852 /* Text commands */
1853 /* */
1854 /* **************************************************************** */
1855
1856 /* Insert the character C at the current location, moving point forward. */
1857 rl_insert (count, c)
1858 int count, c;
1859 {
1860 register int i;
1861 char *string;
1862
1863 if (count <= 0)
1864 return 0;
1865
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)
1869 {
1870 string = (char *)alloca (1 + count);
1871
1872 for (i = 0; i < count; i++)
1873 string[i] = c;
1874
1875 string[i] = '\0';
1876 rl_insert_text (string);
1877 return 0;
1878 }
1879
1880 if (count > 1024)
1881 {
1882 int decreaser;
1883
1884 string = (char *)alloca (1024 + 1);
1885
1886 for (i = 0; i < 1024; i++)
1887 string[i] = c;
1888
1889 while (count)
1890 {
1891 decreaser = (count > 1024 ? 1024 : count);
1892 string[decreaser] = '\0';
1893 rl_insert_text (string);
1894 count -= decreaser;
1895 }
1896 return 0;
1897 }
1898
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)
1904 {
1905 int key = 0, t;
1906
1907 i = 0;
1908 string = (char *)alloca (ibuffer_len + 1);
1909 string[i++] = c;
1910
1911 while ((t = rl_get_char (&key)) &&
1912 (_rl_keymap[key].type == ISFUNC &&
1913 _rl_keymap[key].function == rl_insert))
1914 string[i++] = key;
1915
1916 if (t)
1917 rl_unget_char (key);
1918
1919 string[i] = '\0';
1920 rl_insert_text (string);
1921 }
1922 else
1923 {
1924 /* Inserting a single character. */
1925 string = (char *)alloca (2);
1926
1927 string[1] = '\0';
1928 string[0] = c;
1929 rl_insert_text (string);
1930 }
1931 return 0;
1932 }
1933
1934 /* Insert the next typed character verbatim. */
1935 rl_quoted_insert (count)
1936 int count;
1937 {
1938 int c;
1939
1940 c = rl_read_key ();
1941 return (rl_insert (count, c));
1942
1943 }
1944
1945 /* Insert a tab character. */
1946 rl_tab_insert (count)
1947 int count;
1948 {
1949 return (rl_insert (count, '\t'));
1950 }
1951
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;
1957 {
1958 rl_done = 1;
1959
1960 #if defined (VI_MODE)
1961 {
1962 extern int _rl_vi_doing_insert;
1963 if (_rl_vi_doing_insert)
1964 {
1965 rl_end_undo_group ();
1966 _rl_vi_doing_insert = 0;
1967 }
1968 }
1969 rl_vi_set_last ();
1970
1971 #endif /* VI_MODE */
1972
1973 if (readline_echoing_p)
1974 {
1975 _rl_move_vert (_rl_vis_botlin);
1976 _rl_vis_botlin = 0;
1977 crlf ();
1978 fflush (out_stream);
1979 rl_display_fixed++;
1980 }
1981 return 0;
1982 }
1983
1984 rl_clean_up_for_exit ()
1985 {
1986 if (readline_echoing_p)
1987 {
1988 _rl_move_vert (_rl_vis_botlin);
1989 _rl_vis_botlin = 0;
1990 fflush (out_stream);
1991 rl_restart_output ();
1992 }
1993 return 0;
1994 }
1995
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;
2002 {
2003 return 0;
2004 }
2005
2006 /* Rubout the character behind point. */
2007 rl_rubout (count)
2008 int count;
2009 {
2010 if (count < 0)
2011 {
2012 rl_delete (-count);
2013 return 0;
2014 }
2015
2016 if (!rl_point)
2017 {
2018 ding ();
2019 return -1;
2020 }
2021
2022 if (count > 1 || rl_explicit_arg)
2023 {
2024 int orig_point = rl_point;
2025 rl_backward (count);
2026 rl_kill_text (orig_point, rl_point);
2027 }
2028 else
2029 {
2030 int c = the_line[--rl_point];
2031 rl_delete_text (rl_point, rl_point + 1);
2032
2033 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
2034 {
2035 int l;
2036 l = rl_character_len (c, rl_point);
2037 _rl_erase_at_end_of_line (l);
2038 }
2039 }
2040 return 0;
2041 }
2042
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;
2047 {
2048 if (count < 0)
2049 {
2050 return (rl_rubout (-count));
2051 }
2052
2053 if (rl_point == rl_end)
2054 {
2055 ding ();
2056 return -1;
2057 }
2058
2059 if (count > 1 || rl_explicit_arg)
2060 {
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;
2066 }
2067 else
2068 return (rl_delete_text (rl_point, rl_point + 1));
2069
2070 }
2071
2072 /* Delete all spaces and tabs around point. */
2073 rl_delete_horizontal_space (count, ignore)
2074 int count, ignore;
2075 {
2076 int start = rl_point;
2077
2078 while (rl_point && whitespace (the_line[rl_point - 1]))
2079 rl_point--;
2080
2081 start = rl_point;
2082
2083 while (rl_point < rl_end && whitespace (the_line[rl_point]))
2084 rl_point++;
2085
2086 if (start != rl_point)
2087 {
2088 rl_delete_text (start, rl_point);
2089 rl_point = start;
2090 }
2091 return 0;
2092 }
2093
2094 \f
2095 /* **************************************************************** */
2096 /* */
2097 /* Kill commands */
2098 /* */
2099 /* **************************************************************** */
2100
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. */
2104
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 ()
2108 {
2109 if (!rl_point)
2110 ding ();
2111 else
2112 {
2113 int orig_point = rl_point;
2114
2115 while (rl_point && whitespace (the_line[rl_point - 1]))
2116 rl_point--;
2117
2118 while (rl_point && !whitespace (the_line[rl_point - 1]))
2119 rl_point--;
2120
2121 rl_kill_text (rl_point, orig_point);
2122 }
2123 return 0;
2124 }
2125
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 ()
2133 {
2134 if (!rl_point)
2135 ding ();
2136 else
2137 {
2138 rl_kill_text (rl_point, 0);
2139 rl_point = 0;
2140 }
2141 return 0;
2142 }
2143
2144 \f
2145 /* **************************************************************** */
2146 /* */
2147 /* Commands For Typos */
2148 /* */
2149 /* **************************************************************** */
2150
2151 /* Random and interesting things in here. */
2152
2153 /* **************************************************************** */
2154 /* */
2155 /* Changing Case */
2156 /* */
2157 /* **************************************************************** */
2158
2159 /* The three kinds of things that we know how to do. */
2160 #define UpCase 1
2161 #define DownCase 2
2162 #define CapCase 3
2163
2164 static int rl_change_case ();
2165
2166 /* Uppercase the word at point. */
2167 rl_upcase_word (count)
2168 int count;
2169 {
2170 return (rl_change_case (count, UpCase));
2171 }
2172
2173 /* Lowercase the word at point. */
2174 rl_downcase_word (count)
2175 int count;
2176 {
2177 return (rl_change_case (count, DownCase));
2178 }
2179
2180 /* Upcase the first letter, downcase the rest. */
2181 rl_capitalize_word (count)
2182 int count;
2183 {
2184 return (rl_change_case (count, CapCase));
2185 }
2186
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;
2195 {
2196 register int start = rl_point, end;
2197 int state = 0;
2198
2199 rl_forward_word (count);
2200 end = rl_point;
2201
2202 if (count < 0)
2203 {
2204 int temp = start;
2205 start = end;
2206 end = temp;
2207 }
2208
2209 /* We are going to modify some text, so let's prepare to undo it. */
2210 rl_modifying (start, end);
2211
2212 for (; start < end; start++)
2213 {
2214 switch (op)
2215 {
2216 case UpCase:
2217 the_line[start] = to_upper (the_line[start]);
2218 break;
2219
2220 case DownCase:
2221 the_line[start] = to_lower (the_line[start]);
2222 break;
2223
2224 case CapCase:
2225 if (state == 0)
2226 {
2227 the_line[start] = to_upper (the_line[start]);
2228 state = 1;
2229 }
2230 else
2231 {
2232 the_line[start] = to_lower (the_line[start]);
2233 }
2234 if (!pure_alphabetic (the_line[start]))
2235 state = 0;
2236 break;
2237
2238 default:
2239 abort ();
2240 return -1;
2241 }
2242 }
2243 rl_point = end;
2244 return 0;
2245 }
2246
2247 /* **************************************************************** */
2248 /* */
2249 /* Transposition */
2250 /* */
2251 /* **************************************************************** */
2252
2253 /* Transpose the words at point. */
2254 rl_transpose_words (count)
2255 int count;
2256 {
2257 char *word1, *word2;
2258 int w1_beg, w1_end, w2_beg, w2_end;
2259 int orig_point = rl_point;
2260
2261 if (!count)
2262 return 0;
2263
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;
2273
2274 /* Do some check to make sure that there really are two words. */
2275 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2276 {
2277 ding ();
2278 rl_point = orig_point;
2279 return -1;
2280 }
2281
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);
2285
2286 /* We are about to do many insertions and deletions. Remember them
2287 as one operation. */
2288 rl_begin_undo_group ();
2289
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);
2295
2296 rl_point = w1_beg;
2297 rl_delete_text (w1_beg, w1_end);
2298 rl_insert_text (word2);
2299
2300 /* This is exactly correct since the text before this point has not
2301 changed in length. */
2302 rl_point = w2_end;
2303
2304 /* I think that does it. */
2305 rl_end_undo_group ();
2306 free (word1);
2307 free (word2);
2308
2309 return 0;
2310 }
2311
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;
2316 {
2317 char dummy[2];
2318
2319 if (!count)
2320 return 0;
2321
2322 if (!rl_point || rl_end < 2)
2323 {
2324 ding ();
2325 return -1;
2326 }
2327
2328 rl_begin_undo_group ();
2329
2330 if (rl_point == rl_end)
2331 {
2332 --rl_point;
2333 count = 1;
2334 }
2335 rl_point--;
2336
2337 dummy[0] = the_line[rl_point];
2338 dummy[1] = '\0';
2339
2340 rl_delete_text (rl_point, rl_point + 1);
2341
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);
2348
2349 rl_end_undo_group ();
2350 return 0;
2351 }
2352 \f
2353 /* **************************************************************** */
2354 /* */
2355 /* Undo, and Undoing */
2356 /* */
2357 /* **************************************************************** */
2358
2359 /* The current undo list for THE_LINE. */
2360 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2361
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;
2369 {
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;
2377 }
2378
2379 /* Free the existing undo list. */
2380 void
2381 free_undo_list ()
2382 {
2383 while (rl_undo_list)
2384 {
2385 UNDO_LIST *release = rl_undo_list;
2386 rl_undo_list = rl_undo_list->next;
2387
2388 if (release->what == UNDO_DELETE)
2389 free (release->text);
2390
2391 free (release);
2392 }
2393 rl_undo_list = (UNDO_LIST *)NULL;
2394 }
2395
2396 /* Undo the next thing in the list. Return 0 if there
2397 is nothing to undo, or non-zero if there was. */
2398 int
2399 rl_do_undo ()
2400 {
2401 UNDO_LIST *release;
2402 int waiting_for_begin = 0;
2403
2404 undo_thing:
2405 if (!rl_undo_list)
2406 return (0);
2407
2408 doing_an_undo = 1;
2409
2410 switch (rl_undo_list->what) {
2411
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;
2418
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;
2424
2425 /* Undoing an END means undoing everything 'til we get to
2426 a BEGIN. */
2427 case UNDO_END:
2428 waiting_for_begin++;
2429 break;
2430
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;
2442 }
2443
2444 doing_an_undo = 0;
2445
2446 release = rl_undo_list;
2447 rl_undo_list = rl_undo_list->next;
2448 free (release);
2449
2450 if (waiting_for_begin)
2451 goto undo_thing;
2452
2453 return (1);
2454 }
2455
2456 /* Begin a group. Subsequent undos are undone as an atomic operation. */
2457 rl_begin_undo_group ()
2458 {
2459 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2460 return 0;
2461 }
2462
2463 /* End an undo group started with rl_begin_undo_group (). */
2464 rl_end_undo_group ()
2465 {
2466 rl_add_undo (UNDO_END, 0, 0, 0);
2467 return 0;
2468 }
2469
2470 /* Save an undo entry for the text from START to END. */
2471 rl_modifying (start, end)
2472 int start, end;
2473 {
2474 if (start > end)
2475 {
2476 int t = start;
2477 start = end;
2478 end = t;
2479 }
2480
2481 if (start != end)
2482 {
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 ();
2488 }
2489 return 0;
2490 }
2491
2492 /* Revert the current line to its previous state. */
2493 rl_revert_line ()
2494 {
2495 if (!rl_undo_list)
2496 ding ();
2497 else
2498 {
2499 while (rl_undo_list)
2500 rl_do_undo ();
2501 }
2502 return 0;
2503 }
2504
2505 /* Do some undoing of things that were done. */
2506 rl_undo_command (count)
2507 int count;
2508 {
2509 if (count < 0)
2510 return 0; /* Nothing to do. */
2511
2512 while (count)
2513 {
2514 if (rl_do_undo ())
2515 count--;
2516 else
2517 {
2518 ding ();
2519 break;
2520 }
2521 }
2522 return 0;
2523 }
2524 \f
2525 /* **************************************************************** */
2526 /* */
2527 /* History Utilities */
2528 /* */
2529 /* **************************************************************** */
2530
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. */
2534
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;
2538
2539 /* Set the history pointer back to the last entry in the history. */
2540 start_using_history ()
2541 {
2542 using_history ();
2543 if (saved_line_for_history)
2544 free_history_entry (saved_line_for_history);
2545
2546 saved_line_for_history = (HIST_ENTRY *)NULL;
2547 return 0;
2548 }
2549
2550 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2551 void
2552 free_history_entry (entry)
2553 HIST_ENTRY *entry;
2554 {
2555 if (!entry)
2556 return;
2557 if (entry->line)
2558 free (entry->line);
2559 free (entry);
2560 }
2561
2562 /* Perhaps put back the current line if it has changed. */
2563 maybe_replace_line ()
2564 {
2565 HIST_ENTRY *temp = current_history ();
2566
2567 /* If the current line has changed, save the changes. */
2568 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2569 {
2570 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2571 free (temp->line);
2572 free (temp);
2573 }
2574 return 0;
2575 }
2576
2577 /* Put back the saved_line_for_history if there is one. */
2578 maybe_unsave_line ()
2579 {
2580 if (saved_line_for_history)
2581 {
2582 int line_len;
2583
2584 line_len = strlen (saved_line_for_history->line);
2585
2586 if (line_len >= rl_line_buffer_len)
2587 rl_extend_line_buffer (line_len);
2588
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);
2594 }
2595 else
2596 ding ();
2597 return 0;
2598 }
2599
2600 /* Save the current line in saved_line_for_history. */
2601 maybe_save_line ()
2602 {
2603 if (!saved_line_for_history)
2604 {
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;
2608 }
2609 return 0;
2610 }
2611 \f
2612 /* **************************************************************** */
2613 /* */
2614 /* History Commands */
2615 /* */
2616 /* **************************************************************** */
2617
2618 /* Meta-< goes to the start of the history. */
2619 rl_beginning_of_history ()
2620 {
2621 return (rl_get_previous_history (1 + where_history ()));
2622 }
2623
2624 /* Meta-> goes to the end of the history. (The current line). */
2625 rl_end_of_history ()
2626 {
2627 maybe_replace_line ();
2628 using_history ();
2629 maybe_unsave_line ();
2630 return 0;
2631 }
2632
2633 /* Move down to the next history line. */
2634 rl_get_next_history (count)
2635 int count;
2636 {
2637 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2638
2639 if (count < 0)
2640 return (rl_get_previous_history (-count));
2641
2642 if (!count)
2643 return 0;
2644
2645 maybe_replace_line ();
2646
2647 while (count)
2648 {
2649 temp = next_history ();
2650 if (!temp)
2651 break;
2652 --count;
2653 }
2654
2655 if (!temp)
2656 maybe_unsave_line ();
2657 else
2658 {
2659 int line_len;
2660
2661 line_len = strlen (temp->line);
2662
2663 if (line_len >= rl_line_buffer_len)
2664 rl_extend_line_buffer (line_len);
2665
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 */
2673 }
2674 return 0;
2675 }
2676
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;
2681 {
2682 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2683 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2684
2685 if (count < 0)
2686 return (rl_get_next_history (-count));
2687
2688 if (!count)
2689 return 0;
2690
2691 /* If we don't have a line saved, then save this one. */
2692 maybe_save_line ();
2693
2694 /* If the current line has changed, save the changes. */
2695 maybe_replace_line ();
2696
2697 while (count)
2698 {
2699 temp = previous_history ();
2700 if (!temp)
2701 break;
2702 else
2703 old_temp = temp;
2704 --count;
2705 }
2706
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;
2711
2712 if (!temp)
2713 ding ();
2714 else
2715 {
2716 int line_len;
2717
2718 line_len = strlen (temp->line);
2719
2720 if (line_len >= rl_line_buffer_len)
2721 rl_extend_line_buffer (line_len);
2722
2723 strcpy (the_line, temp->line);
2724 rl_undo_list = (UNDO_LIST *)temp->data;
2725 rl_end = rl_point = line_len;
2726
2727 #if defined (VI_MODE)
2728 if (rl_editing_mode == vi_mode)
2729 rl_point = 0;
2730 #endif /* VI_MODE */
2731 }
2732 return 0;
2733 }
2734
2735 /* Make C be the next command to be executed. */
2736 rl_execute_next (c)
2737 int c;
2738 {
2739 rl_pending_input = c;
2740 return 0;
2741 }
2742
2743 /* **************************************************************** */
2744 /* */
2745 /* The Mark and the Region. */
2746 /* */
2747 /* **************************************************************** */
2748
2749 /* Set the mark at POSITION. */
2750 rl_set_mark (position)
2751 int position;
2752 {
2753 if (position > rl_end)
2754 return -1;
2755
2756 rl_mark = position;
2757 return 0;
2758 }
2759
2760 /* Exchange the position of mark and point. */
2761 rl_exchange_mark_and_point ()
2762 {
2763 if (rl_mark > rl_end)
2764 rl_mark = -1;
2765
2766 if (rl_mark == -1)
2767 {
2768 ding ();
2769 return -1;
2770 }
2771 else
2772 {
2773 int temp = rl_point;
2774
2775 rl_point = rl_mark;
2776 rl_mark = temp;
2777 }
2778 return 0;
2779 }
2780
2781 \f
2782 /* **************************************************************** */
2783 /* */
2784 /* Killing Mechanism */
2785 /* */
2786 /* **************************************************************** */
2787
2788 /* What we assume for a max number of kills. */
2789 #define DEFAULT_MAX_KILLS 10
2790
2791 /* The real variable to look at to find out when to flush kills. */
2792 int rl_max_kills = DEFAULT_MAX_KILLS;
2793
2794 /* Where to store killed text. */
2795 char **rl_kill_ring = (char **)NULL;
2796
2797 /* Where we are in the kill ring. */
2798 int rl_kill_index = 0;
2799
2800 /* How many slots we have in the kill ring. */
2801 int rl_kill_ring_length = 0;
2802
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;
2807 {
2808 return 0;
2809 }
2810
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;
2818 {
2819 int slot;
2820 char *text = rl_copy_text (from, to);
2821
2822 /* Is there anything to kill? */
2823 if (from == to)
2824 {
2825 free (text);
2826 last_command_was_kill++;
2827 return 0;
2828 }
2829
2830 /* Delete the copied text from the line. */
2831 rl_delete_text (from, to);
2832
2833 /* First, find the slot to work with. */
2834 if (!last_command_was_kill)
2835 {
2836 /* Get a new slot. */
2837 if (!rl_kill_ring)
2838 {
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;
2843 }
2844 else
2845 {
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)
2850 {
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];
2855 }
2856 else
2857 {
2858 rl_kill_ring =
2859 (char **)
2860 xrealloc (rl_kill_ring,
2861 ((slot = (rl_kill_ring_length += 1)) + 1)
2862 * sizeof (char *));
2863 }
2864 }
2865 slot--;
2866 }
2867 else
2868 {
2869 slot = rl_kill_ring_length - 1;
2870 }
2871
2872 /* If the last command was a kill, prepend or append. */
2873 if (last_command_was_kill && rl_editing_mode != vi_mode)
2874 {
2875 char *old = rl_kill_ring[slot];
2876 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
2877
2878 if (from < to)
2879 {
2880 strcpy (new, old);
2881 strcat (new, text);
2882 }
2883 else
2884 {
2885 strcpy (new, text);
2886 strcat (new, old);
2887 }
2888 free (old);
2889 free (text);
2890 rl_kill_ring[slot] = new;
2891 }
2892 else
2893 {
2894 rl_kill_ring[slot] = text;
2895 }
2896 rl_kill_index = slot;
2897 last_command_was_kill++;
2898 return 0;
2899 }
2900
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. */
2904
2905 /* **************************************************************** */
2906 /* */
2907 /* Killing Commands */
2908 /* */
2909 /* **************************************************************** */
2910
2911 /* Delete the word at point, saving the text in the kill ring. */
2912 rl_kill_word (count)
2913 int count;
2914 {
2915 int orig_point = rl_point;
2916
2917 if (count < 0)
2918 return (rl_backward_kill_word (-count));
2919 else
2920 {
2921 rl_forward_word (count);
2922
2923 if (rl_point != orig_point)
2924 rl_kill_text (orig_point, rl_point);
2925
2926 rl_point = orig_point;
2927 }
2928 return 0;
2929 }
2930
2931 /* Rubout the word before point, placing it on the kill ring. */
2932 rl_backward_kill_word (count)
2933 int count;
2934 {
2935 int orig_point = rl_point;
2936
2937 if (count < 0)
2938 return (rl_kill_word (-count));
2939 else
2940 {
2941 rl_backward_word (count);
2942
2943 if (rl_point != orig_point)
2944 rl_kill_text (orig_point, rl_point);
2945 }
2946 }
2947
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;
2952 {
2953 int orig_point = rl_point;
2954
2955 if (direction < 0)
2956 return (rl_backward_kill_line (1));
2957 else
2958 {
2959 rl_end_of_line ();
2960 if (orig_point != rl_point)
2961 rl_kill_text (orig_point, rl_point);
2962 rl_point = orig_point;
2963 }
2964 return 0;
2965 }
2966
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;
2971 {
2972 int orig_point = rl_point;
2973
2974 if (direction < 0)
2975 return (rl_kill_line (1));
2976 else
2977 {
2978 if (!rl_point)
2979 ding ();
2980 else
2981 {
2982 rl_beg_of_line ();
2983 rl_kill_text (orig_point, rl_point);
2984 }
2985 }
2986 return 0;
2987 }
2988
2989 /* Yank back the last killed text. This ignores arguments. */
2990 rl_yank ()
2991 {
2992 if (!rl_kill_ring)
2993 {
2994 rl_abort ();
2995 return -1;
2996 }
2997
2998 rl_set_mark (rl_point);
2999 rl_insert_text (rl_kill_ring[rl_kill_index]);
3000 return 0;
3001 }
3002
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 ()
3008 {
3009 int l;
3010
3011 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
3012 !rl_kill_ring)
3013 {
3014 rl_abort ();
3015 return -1;
3016 }
3017
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))
3022 {
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;
3030 }
3031 else
3032 {
3033 rl_abort ();
3034 return -1;
3035 }
3036 }
3037
3038 /* Yank the COUNTth argument from the previous history line. */
3039 rl_yank_nth_arg (count, ignore)
3040 int count;
3041 {
3042 register HIST_ENTRY *entry = previous_history ();
3043 char *arg;
3044
3045 if (entry)
3046 next_history ();
3047 else
3048 {
3049 ding ();
3050 return -1;
3051 }
3052
3053 arg = history_arg_extract (count, count, entry->line);
3054 if (!arg || !*arg)
3055 {
3056 ding ();
3057 return -1;
3058 }
3059
3060 rl_begin_undo_group ();
3061
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 */
3068
3069 #if 0
3070 if (rl_point && the_line[rl_point - 1] != ' ')
3071 rl_insert_text (" ");
3072 #endif
3073
3074 rl_insert_text (arg);
3075 free (arg);
3076
3077 rl_end_undo_group ();
3078 return 0;
3079 }
3080
3081 /* How to toggle back and forth between editing modes. */
3082 rl_vi_editing_mode ()
3083 {
3084 #if defined (VI_MODE)
3085 rl_editing_mode = vi_mode;
3086 rl_vi_insertion_mode ();
3087 return 0;
3088 #endif /* VI_MODE */
3089 }
3090
3091 rl_emacs_editing_mode ()
3092 {
3093 rl_editing_mode = emacs_mode;
3094 _rl_keymap = emacs_standard_keymap;
3095 return 0;
3096 }
3097
3098 \f
3099 /* **************************************************************** */
3100 /* */
3101 /* USG (System V) Support */
3102 /* */
3103 /* **************************************************************** */
3104
3105 int
3106 rl_getc (stream)
3107 FILE *stream;
3108 {
3109 int result;
3110 unsigned char c;
3111
3112 #if defined (__GO32__)
3113 if (isatty (0))
3114 return (getkey ());
3115 #endif /* __GO32__ */
3116
3117 while (1)
3118 {
3119 result = read (fileno (stream), &c, sizeof (unsigned char));
3120
3121 if (result == sizeof (unsigned char))
3122 return (c);
3123
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);
3128
3129 #if defined (EWOULDBLOCK)
3130 if (errno == EWOULDBLOCK)
3131 {
3132 int flags;
3133
3134 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3135 return (EOF);
3136 if (flags & O_NDELAY)
3137 {
3138 flags &= ~O_NDELAY;
3139 fcntl (fileno (stream), F_SETFL, flags);
3140 continue;
3141 }
3142 continue;
3143 }
3144 #endif /* EWOULDBLOCK */
3145
3146 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3147 if (errno == EAGAIN)
3148 {
3149 int flags;
3150
3151 if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3152 return (EOF);
3153 if (flags & O_NONBLOCK)
3154 {
3155 flags &= ~O_NONBLOCK;
3156 fcntl (fileno (stream), F_SETFL, flags);
3157 continue;
3158 }
3159 }
3160 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3161
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__ */
3169 }
3170 }
3171
3172 char *
3173 _rl_savestring (str)
3174 char *str;
3175 {
3176 char *copy = (char*) xmalloc (strlen (str) + 1);
3177 strcpy (copy, str);
3178 return copy;
3179 }
3180
3181 #if defined (STATIC_MALLOC)
3182 \f
3183 /* **************************************************************** */
3184 /* */
3185 /* xmalloc and xrealloc () */
3186 /* */
3187 /* **************************************************************** */
3188
3189 static void memory_error_and_abort ();
3190
3191 static char *
3192 xmalloc (bytes)
3193 int bytes;
3194 {
3195 char *temp = (char *)malloc (bytes);
3196
3197 if (!temp)
3198 memory_error_and_abort ();
3199 return (temp);
3200 }
3201
3202 static char *
3203 xrealloc (pointer, bytes)
3204 char *pointer;
3205 int bytes;
3206 {
3207 char *temp;
3208
3209 if (!pointer)
3210 temp = (char *)malloc (bytes);
3211 else
3212 temp = (char *)realloc (pointer, bytes);
3213
3214 if (!temp)
3215 memory_error_and_abort ();
3216
3217 return (temp);
3218 }
3219
3220 static void
3221 memory_error_and_abort ()
3222 {
3223 fprintf (stderr, "readline: Out of virtual memory!\n");
3224 abort ();
3225 }
3226 #endif /* STATIC_MALLOC */
3227
3228 \f
3229 /* **************************************************************** */
3230 /* */
3231 /* Testing Readline */
3232 /* */
3233 /* **************************************************************** */
3234
3235 #if defined (TEST)
3236
3237 main ()
3238 {
3239 HIST_ENTRY **history_list ();
3240 char *temp = (char *)NULL;
3241 char *prompt = "readline% ";
3242 int done = 0;
3243
3244 while (!done)
3245 {
3246 temp = readline (prompt);
3247
3248 /* Test for EOF. */
3249 if (!temp)
3250 exit (1);
3251
3252 /* If there is anything on the line, print it and remember it. */
3253 if (*temp)
3254 {
3255 fprintf (stderr, "%s\r\n", temp);
3256 add_history (temp);
3257 }
3258
3259 /* Check for `command' that we handle. */
3260 if (strcmp (temp, "quit") == 0)
3261 done = 1;
3262
3263 if (strcmp (temp, "list") == 0)
3264 {
3265 HIST_ENTRY **list = history_list ();
3266 register int i;
3267 if (list)
3268 {
3269 for (i = 0; list[i]; i++)
3270 {
3271 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3272 free (list[i]->line);
3273 }
3274 free (list);
3275 }
3276 }
3277 free (temp);
3278 }
3279 }
3280
3281 #endif /* TEST */
3282
3283 \f
3284 /*
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:
3288 */