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