4187119d |
1 | /* readline.c -- a general facility for reading lines of input |
2 | with emacs style editing and completion. */ |
3 | |
4 | /* Copyright (C) 1987,1989 Free Software Foundation, Inc. |
5 | |
6 | This file contains the Readline Library (the Library), a set of |
7 | routines for providing Emacs style line input to programs that ask |
8 | for it. |
9 | |
10 | The Library is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by |
12 | the Free Software Foundation; either version 1, or (at your option) |
13 | any later version. |
14 | |
15 | The Library is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | General Public License for more details. |
19 | |
20 | The GNU General Public License is often shipped with GNU software, and |
21 | is generally kept in a file called COPYING or LICENSE. If you do not |
22 | have a copy of the license, write to the Free Software Foundation, |
23 | 675 Mass Ave, Cambridge, MA 02139, USA. */ |
24 | |
25 | /* Remove these declarations when we have a complete libgnu.a. */ |
26 | #define STATIC_MALLOC |
27 | #ifndef STATIC_MALLOC |
28 | extern char *xmalloc (), *xrealloc (); |
29 | #else |
30 | static char *xmalloc (), *xrealloc (); |
31 | #endif |
32 | |
33 | #include <stdio.h> |
34 | #include <sys/types.h> |
35 | #include <fcntl.h> |
36 | #include <sys/file.h> |
37 | #include <signal.h> |
38 | |
39 | #ifdef __GNUC__ |
40 | #define alloca __builtin_alloca |
41 | #else |
42 | #if defined (sparc) && defined (sun) |
43 | #include <alloca.h> |
44 | #endif |
45 | #endif |
46 | |
47 | #define NEW_TTY_DRIVER |
48 | #if defined (SYSV) || defined (hpux) |
49 | #undef NEW_TTY_DRIVER |
50 | #include <termio.h> |
51 | #else |
52 | #include <sgtty.h> |
53 | #endif |
54 | |
55 | #include <errno.h> |
56 | extern int errno; |
57 | |
58 | #include <setjmp.h> |
59 | |
60 | /* These next are for filename completion. Perhaps this belongs |
61 | in a different place. */ |
62 | #include <sys/stat.h> |
63 | |
64 | #include <pwd.h> |
65 | #ifdef SYSV |
66 | struct passwd *getpwuid (), *getpwent (); |
67 | #endif |
68 | |
69 | #define HACK_TERMCAP_MOTION |
70 | |
71 | #ifndef SYSV |
72 | #include <sys/dir.h> |
73 | #else /* SYSV */ |
74 | #ifdef hpux |
75 | #include <ndir.h> |
76 | #else |
77 | #include <dirent.h> |
78 | #define direct dirent |
79 | #define d_namlen d_reclen |
80 | #endif /* hpux */ |
81 | #endif /* SYSV */ |
82 | |
83 | /* Some standard library routines. */ |
84 | #include "readline.h" |
85 | #include "history.h" |
86 | |
87 | #ifndef digit |
88 | #define digit(c) ((c) >= '0' && (c) <= '9') |
89 | #endif |
90 | |
91 | #ifndef isletter |
92 | #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z')) |
93 | #endif |
94 | |
95 | #ifndef digit_value |
96 | #define digit_value(c) ((c) - '0') |
97 | #endif |
98 | |
99 | #ifndef member |
100 | char *index (); |
101 | #define member(c, s) ((c) ? index ((s), (c)) : 0) |
102 | #endif |
103 | |
104 | #ifndef isident |
105 | #define isident(c) ((isletter(c) || digit(c) || c == '_')) |
106 | #endif |
107 | |
108 | #ifndef exchange |
109 | #define exchange(x, y) {int temp = x; x = y; y = temp;} |
110 | #endif |
111 | |
112 | static update_line (); |
113 | static void output_character_function (); |
114 | static delete_chars (); |
115 | static start_insert (); |
116 | static end_insert (); |
117 | |
118 | #ifdef VOID_SIGHANDLER |
119 | #define sighandler void |
120 | #else |
121 | #define sighandler int |
122 | #endif |
123 | |
124 | /* This typedef is equivalant to the one for Function; it allows us |
125 | to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */ |
126 | typedef sighandler SigHandler (); |
127 | |
128 | #ifdef SIGWINCH |
129 | static sighandler rl_handle_sigwinch (); |
130 | static SigHandler *old_sigwinch = (SigHandler *)NULL; |
131 | #endif |
132 | |
133 | /* If on, then readline handles signals in a way that doesn't screw. */ |
134 | #define HANDLE_SIGNALS |
135 | |
136 | #if defined (SYSV) |
137 | #ifdef HANDLE_SIGNALS |
138 | #undef HANDLE_SIGNALS |
139 | #endif |
140 | #endif |
141 | |
1c997a4a |
142 | /* Stupid comparison routine for qsort () ing strings. */ |
143 | static int |
144 | compare_strings (s1, s2) |
145 | char **s1, **s2; |
146 | { |
147 | return (strcmp (*s1, *s2)); |
148 | } |
149 | |
4187119d |
150 | \f |
151 | /* **************************************************************** */ |
152 | /* */ |
153 | /* Line editing input utility */ |
154 | /* */ |
155 | /* **************************************************************** */ |
156 | |
157 | /* A pointer to the keymap that is currently in use. |
158 | By default, it is the standard emacs keymap. */ |
159 | Keymap keymap = emacs_standard_keymap; |
160 | |
161 | #define vi_mode 0 |
162 | #define emacs_mode 1 |
163 | |
164 | /* The current style of editing. */ |
165 | int rl_editing_mode = emacs_mode; |
166 | |
167 | /* Non-zero if the previous command was a kill command. */ |
168 | static int last_command_was_kill = 0; |
169 | |
170 | /* The current value of the numeric argument specified by the user. */ |
171 | int rl_numeric_arg = 1; |
172 | |
173 | /* Non-zero if an argument was typed. */ |
174 | int rl_explicit_arg = 0; |
175 | |
176 | /* Temporary value used while generating the argument. */ |
177 | static int arg_sign = 1; |
178 | |
179 | /* Non-zero means we have been called at least once before. */ |
180 | static int rl_initialized = 0; |
181 | |
182 | /* If non-zero, this program is running in an EMACS buffer. */ |
183 | static char *running_in_emacs = (char *)NULL; |
184 | |
185 | /* The current offset in the current input line. */ |
186 | int rl_point; |
187 | |
188 | /* Mark in the current input line. */ |
189 | int rl_mark; |
190 | |
191 | /* Length of the current input line. */ |
192 | int rl_end; |
193 | |
194 | /* Make this non-zero to return the current input_line. */ |
195 | int rl_done; |
196 | |
197 | /* The last function executed by readline. */ |
198 | Function *rl_last_func = (Function *)NULL; |
199 | |
200 | /* Top level environment for readline_internal (). */ |
201 | static jmp_buf readline_top_level; |
202 | |
203 | /* The streams we interact with. */ |
204 | static FILE *in_stream, *out_stream; |
205 | |
206 | /* The names of the streams that we do input and output to. */ |
207 | FILE *rl_instream = stdin, *rl_outstream = stdout; |
208 | |
209 | /* Non-zero means echo characters as they are read. */ |
210 | int readline_echoing_p = 1; |
211 | |
212 | /* Current prompt. */ |
213 | char *rl_prompt; |
214 | |
215 | /* The number of characters read in order to type this complete command. */ |
216 | int rl_key_sequence_length = 0; |
217 | |
218 | /* If non-zero, then this is the address of a function to call just |
219 | before readline_internal () prints the first prompt. */ |
220 | Function *rl_startup_hook = (Function *)NULL; |
221 | |
222 | /* What we use internally. You should always refer to RL_LINE_BUFFER. */ |
223 | static char *the_line; |
224 | |
225 | /* The character that can generate an EOF. Really read from |
226 | the terminal driver... just defaulted here. */ |
227 | static int eof_char = CTRL ('D'); |
228 | |
229 | /* Non-zero makes this the next keystroke to read. */ |
230 | int rl_pending_input = 0; |
231 | |
232 | /* Pointer to a useful terminal name. */ |
233 | char *rl_terminal_name = (char *)NULL; |
234 | |
235 | /* Line buffer and maintenence. */ |
236 | char *rl_line_buffer = (char *)NULL; |
237 | static int rl_line_buffer_len = 0; |
238 | #define DEFAULT_BUFFER_SIZE 256 |
239 | |
240 | \f |
241 | /* **************************************************************** */ |
242 | /* */ |
243 | /* Top Level Functions */ |
244 | /* */ |
245 | /* **************************************************************** */ |
246 | |
247 | /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means |
248 | none. A return value of NULL means that EOF was encountered. */ |
249 | char * |
250 | readline (prompt) |
251 | char *prompt; |
252 | { |
253 | static rl_prep_terminal (), rl_deprep_terminal (); |
254 | char *readline_internal (); |
255 | char *value; |
256 | |
257 | rl_prompt = prompt; |
258 | |
259 | /* If we are at EOF return a NULL string. */ |
260 | if (rl_pending_input == EOF) |
261 | { |
262 | rl_pending_input = 0; |
263 | return ((char *)NULL); |
264 | } |
265 | |
266 | rl_initialize (); |
267 | rl_prep_terminal (); |
268 | |
269 | #ifdef SIGWINCH |
270 | old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch); |
271 | #endif |
272 | |
273 | #ifdef HANDLE_SIGNALS |
274 | rl_set_signals (); |
275 | #endif |
276 | |
277 | value = readline_internal (); |
278 | rl_deprep_terminal (); |
279 | |
280 | #ifdef SIGWINCH |
281 | signal (SIGWINCH, old_sigwinch); |
282 | #endif |
283 | |
284 | #ifdef HANDLE_SIGNALS |
285 | rl_clear_signals (); |
286 | #endif |
287 | |
288 | return (value); |
289 | } |
290 | |
291 | /* Read a line of input from the global rl_instream, doing output on |
292 | the global rl_outstream. |
293 | If rl_prompt is non-null, then that is our prompt. */ |
294 | char * |
295 | readline_internal () |
296 | { |
297 | int lastc, c, eof_found; |
298 | |
299 | in_stream = rl_instream; out_stream = rl_outstream; |
300 | lastc = eof_found = 0; |
301 | |
302 | if (rl_startup_hook) |
303 | (*rl_startup_hook) (); |
304 | |
305 | if (!readline_echoing_p) |
306 | { |
307 | if (rl_prompt) |
308 | fprintf (out_stream, "%s", rl_prompt); |
309 | } |
310 | else |
311 | { |
312 | rl_on_new_line (); |
313 | rl_redisplay (); |
314 | #ifdef VI_MODE |
315 | if (rl_editing_mode == vi_mode) |
316 | rl_vi_insertion_mode (); |
317 | #endif /* VI_MODE */ |
318 | } |
319 | |
320 | while (!rl_done) |
321 | { |
322 | int lk = last_command_was_kill; |
323 | int code = setjmp (readline_top_level); |
324 | |
325 | if (code) |
326 | rl_redisplay (); |
327 | |
328 | if (!rl_pending_input) |
329 | { |
330 | /* Then initialize the argument and number of keys read. */ |
331 | rl_init_argument (); |
332 | rl_key_sequence_length = 0; |
333 | } |
334 | |
335 | c = rl_read_key (); |
336 | |
337 | /* EOF typed to a non-blank line is a <NL>. */ |
338 | if (c == EOF && rl_end) |
339 | c = NEWLINE; |
340 | |
341 | /* The character eof_char typed to blank line, and not as the |
342 | previous character is interpreted as EOF. */ |
343 | if (((c == eof_char && lastc != c) || c == EOF) && !rl_end) |
344 | { |
345 | eof_found = 1; |
346 | break; |
347 | } |
348 | |
349 | lastc = c; |
350 | rl_dispatch (c, keymap); |
351 | |
352 | /* If there was no change in last_command_was_kill, then no kill |
353 | has taken place. Note that if input is pending we are reading |
354 | a prefix command, so nothing has changed yet. */ |
355 | if (!rl_pending_input) |
356 | { |
357 | if (lk == last_command_was_kill) |
358 | last_command_was_kill = 0; |
359 | } |
360 | |
361 | #ifdef VI_MODE |
362 | /* In vi mode, when you exit insert mode, the cursor moves back |
363 | over the previous character. We explicitly check for that here. */ |
364 | if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap) |
365 | rl_vi_check (); |
366 | #endif |
367 | |
368 | if (!rl_done) |
369 | rl_redisplay (); |
370 | } |
371 | |
372 | /* Restore the original of this history line, iff the line that we |
373 | are editing was originally in the history, AND the line has changed. */ |
374 | { |
375 | HIST_ENTRY *entry = current_history (); |
376 | |
377 | if (entry && rl_undo_list) |
378 | { |
379 | char *temp = savestring (the_line); |
380 | rl_revert_line (); |
381 | entry = replace_history_entry (where_history (), the_line, |
382 | (HIST_ENTRY *)NULL); |
383 | free_history_entry (entry); |
384 | |
385 | strcpy (the_line, temp); |
386 | free (temp); |
387 | } |
388 | } |
389 | |
390 | /* At any rate, it is highly likely that this line has an undo list. Get |
391 | rid of it now. */ |
392 | if (rl_undo_list) |
393 | free_undo_list (); |
394 | |
395 | if (eof_found) |
396 | return (char *)NULL; |
397 | else |
398 | return (savestring (the_line)); |
399 | } |
400 | |
1c997a4a |
401 | \f |
402 | /* Variables for keyboard macros. */ |
403 | |
404 | /* The currently executing macro string. If this is non-zero, |
405 | then it is a malloc ()'ed string where input is coming from. */ |
406 | static char *executing_macro = (char *)NULL; |
407 | |
408 | /* The offset in the above string to the next character to be read. */ |
409 | static int executing_macro_index = 0; |
410 | |
411 | /* Non-zero means to save keys that we dispatch on in a kbd macro. */ |
412 | static int defining_kbd_macro = 0; |
413 | |
414 | /* The current macro string being built. Characters get stuffed |
415 | in here by add_macro_char (). */ |
416 | static char *current_macro = (char *)NULL; |
417 | |
418 | /* The size of the buffer allocated to current_macro. */ |
419 | static int current_macro_size = 0; |
420 | |
421 | /* The index at which characters are being added to current_macro. */ |
422 | static int current_macro_index = 0; |
423 | |
424 | /* A structure used to save nested macro strings. |
425 | It is a linked list of string/index for each saved macro. */ |
426 | struct saved_macro { |
427 | struct saved_macro *next; |
428 | char *string; |
429 | int index; |
430 | }; |
431 | |
432 | /* The list of saved macros. */ |
433 | struct saved_macro *macro_list = (struct saved_macro *)NULL; |
434 | |
4187119d |
435 | \f |
436 | /* **************************************************************** */ |
437 | /* */ |
438 | /* Signal Handling */ |
439 | /* */ |
440 | /* **************************************************************** */ |
441 | |
442 | #ifdef SIGWINCH |
443 | static sighandler |
444 | rl_handle_sigwinch (sig, code, scp) |
445 | int sig, code; |
446 | struct sigcontext *scp; |
447 | { |
448 | char *term = rl_terminal_name, *getenv (); |
449 | |
450 | if (readline_echoing_p) |
451 | { |
452 | if (!term) |
453 | term = getenv ("TERM"); |
454 | if (!term) |
455 | term = "dumb"; |
456 | rl_reset_terminal (term); |
457 | #ifdef NEVER |
458 | crlf (); |
459 | rl_forced_update_display (); |
460 | #endif |
461 | } |
462 | |
463 | if (old_sigwinch && |
464 | old_sigwinch != (SigHandler *)SIG_IGN && |
465 | old_sigwinch != (SigHandler *)SIG_DFL) |
466 | (*old_sigwinch)(sig, code, scp); |
467 | } |
468 | #endif /* SIGWINCH */ |
469 | |
470 | #ifdef HANDLE_SIGNALS |
471 | /* Interrupt handling. */ |
472 | static SigHandler *old_int = (SigHandler *)NULL, |
473 | *old_tstp = (SigHandler *)NULL, |
474 | *old_ttou = (SigHandler *)NULL, |
475 | *old_ttin = (SigHandler *)NULL, |
476 | *old_cont = (SigHandler *)NULL; |
477 | |
478 | /* Handle an interrupt character. */ |
479 | static sighandler |
480 | rl_signal_handler (sig, code, scp) |
481 | int sig, code; |
482 | struct sigcontext *scp; |
483 | { |
484 | static rl_prep_terminal (), rl_deprep_terminal (); |
485 | |
486 | switch (sig) |
487 | { |
488 | case SIGINT: |
489 | free_undo_list (); |
490 | rl_clear_message (); |
491 | rl_init_argument (); |
492 | #ifdef SIGWINCH |
493 | signal (SIGWINCH, old_sigwinch); |
494 | #endif |
495 | |
496 | #ifdef SIGTSTP |
497 | case SIGTSTP: |
498 | case SIGTTOU: |
499 | case SIGTTIN: |
500 | #endif |
501 | |
502 | rl_clean_up_for_exit (); |
503 | rl_deprep_terminal (); |
504 | rl_clear_signals (); |
505 | rl_pending_input = 0; |
506 | |
507 | kill (getpid (), sig); |
508 | sigsetmask (0); |
509 | |
510 | rl_prep_terminal (); |
511 | rl_set_signals (); |
512 | } |
513 | } |
514 | |
515 | rl_set_signals () |
516 | { |
4187119d |
517 | old_int = (SigHandler *)signal (SIGINT, rl_signal_handler); |
1c997a4a |
518 | |
4187119d |
519 | if (old_int == (SigHandler *)SIG_IGN) |
520 | signal (SIGINT, SIG_IGN); |
521 | |
522 | #ifdef SIGTSTP |
523 | old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler); |
524 | if (old_tstp == (SigHandler *)SIG_IGN) |
525 | signal (SIGTSTP, SIG_IGN); |
526 | #endif |
527 | #ifdef SIGTTOU |
528 | old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler); |
529 | old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler); |
530 | #endif |
531 | } |
532 | |
533 | rl_clear_signals () |
534 | { |
535 | signal (SIGINT, old_int); |
536 | |
537 | #ifdef SIGTSTP |
538 | signal (SIGTSTP, old_tstp); |
539 | #endif |
540 | #ifdef SIGTTOU |
541 | signal (SIGTTOU, old_ttou); |
542 | signal (SIGTTIN, old_ttin); |
543 | #endif |
544 | } |
545 | #endif /* HANDLE_SIGNALS */ |
546 | |
547 | |
548 | |
549 | /* **************************************************************** */ |
550 | /* */ |
551 | /* Character Input Buffering */ |
552 | /* */ |
553 | /* **************************************************************** */ |
554 | |
555 | /* If the terminal was in xoff state when we got to it, then xon_char |
556 | contains the character that is supposed to start it again. */ |
557 | static int xon_char, xoff_state; |
558 | static int pop_index = 0, push_index = 0, ibuffer_len = 511; |
559 | static unsigned char ibuffer[512]; |
560 | |
561 | /* Non-null means it is a pointer to a function to run while waiting for |
562 | character input. */ |
563 | Function *rl_event_hook = (Function *)NULL; |
564 | |
565 | #define any_typein (push_index != pop_index) |
566 | |
567 | /* Add KEY to the buffer of characters to be read. */ |
568 | rl_stuff_char (key) |
569 | int key; |
570 | { |
571 | if (key == EOF) |
572 | { |
573 | key = NEWLINE; |
574 | rl_pending_input = EOF; |
575 | } |
576 | ibuffer[push_index++] = key; |
577 | if (push_index >= ibuffer_len) |
578 | push_index = 0; |
579 | } |
580 | |
581 | /* Return the amount of space available in the |
582 | buffer for stuffing characters. */ |
583 | int |
584 | ibuffer_space () |
585 | { |
586 | if (pop_index > push_index) |
587 | return (pop_index - push_index); |
588 | else |
589 | return (ibuffer_len - (push_index - pop_index)); |
590 | } |
591 | |
592 | /* Get a key from the buffer of characters to be read. |
593 | Return the key in KEY. |
594 | Result is KEY if there was a key, or 0 if there wasn't. */ |
595 | int |
596 | rl_get_char (key) |
597 | int *key; |
598 | { |
599 | if (push_index == pop_index) |
600 | return (0); |
601 | |
602 | *key = ibuffer[pop_index++]; |
603 | |
604 | if (pop_index >= ibuffer_len) |
605 | pop_index = 0; |
606 | |
607 | return (1); |
608 | } |
609 | |
610 | /* Stuff KEY into the *front* of the input buffer. |
611 | Returns non-zero if successful, zero if there is |
612 | no space left in the buffer. */ |
613 | int |
614 | rl_unget_char (key) |
615 | int key; |
616 | { |
617 | if (ibuffer_space ()) |
618 | { |
619 | pop_index--; |
620 | if (pop_index < 0) |
621 | pop_index = ibuffer_len - 1; |
622 | ibuffer[pop_index] = key; |
623 | return (1); |
624 | } |
625 | return (0); |
626 | } |
627 | |
628 | /* If a character is available to be read, then read it |
629 | and stuff it into IBUFFER. Otherwise, just return. */ |
630 | rl_gather_tyi () |
631 | { |
632 | int tty = fileno (in_stream); |
633 | register int tem, result = -1; |
634 | long chars_avail; |
635 | char input; |
636 | |
637 | #ifdef FIONREAD |
638 | result = ioctl (tty, FIONREAD, &chars_avail); |
639 | #endif |
640 | |
641 | if (result == -1) |
642 | { |
643 | fcntl (tty, F_SETFL, O_NDELAY); |
644 | chars_avail = read (tty, &input, 1); |
645 | fcntl (tty, F_SETFL, 0); |
646 | if (chars_avail == -1 && errno == EAGAIN) |
647 | return; |
648 | } |
649 | |
650 | tem = ibuffer_space (); |
651 | |
652 | if (chars_avail > tem) |
653 | chars_avail = tem; |
654 | |
655 | /* One cannot read all of the available input. I can only read a single |
656 | character at a time, or else programs which require input can be |
657 | thwarted. If the buffer is larger than one character, I lose. |
658 | Damn! */ |
659 | if (tem < ibuffer_len) |
660 | chars_avail = 0; |
661 | |
662 | if (result != -1) |
663 | { |
664 | while (chars_avail--) |
665 | rl_stuff_char (rl_getc (in_stream)); |
666 | } |
667 | else |
668 | { |
669 | if (chars_avail) |
670 | rl_stuff_char (input); |
671 | } |
672 | } |
673 | |
674 | /* Read a key, including pending input. */ |
675 | int |
676 | rl_read_key () |
677 | { |
678 | int c; |
679 | |
680 | rl_key_sequence_length++; |
681 | |
682 | if (rl_pending_input) |
683 | { |
684 | c = rl_pending_input; |
685 | rl_pending_input = 0; |
686 | } |
687 | else |
688 | { |
689 | static int next_macro_key (); |
690 | |
691 | /* If input is coming from a macro, then use that. */ |
692 | if (c = next_macro_key ()) |
693 | return (c); |
694 | |
695 | /* If the user has an event function, then call it periodically. */ |
696 | if (rl_event_hook) |
697 | { |
698 | while (rl_event_hook && !rl_get_char (&c)) |
699 | { |
700 | (*rl_event_hook) (); |
701 | rl_gather_tyi (); |
702 | } |
703 | } |
704 | else |
705 | { |
706 | if (!rl_get_char (&c)) |
707 | c = rl_getc (in_stream); |
708 | } |
709 | } |
710 | #ifdef TIOCSTART |
711 | /* Ugh. But I can't think of a better way. */ |
712 | if (xoff_state && c == xon_char) |
713 | { |
714 | ioctl (fileno (in_stream), TIOCSTART, 0); |
715 | xoff_state = 0; |
716 | return rl_read_key (); |
717 | } |
718 | #endif /* TIOCSTART */ |
719 | return (c); |
720 | } |
721 | |
722 | /* Do the command associated with KEY in MAP. |
723 | If the associated command is really a keymap, then read |
724 | another key, and dispatch into that map. */ |
725 | rl_dispatch (key, map) |
726 | register int key; |
727 | Keymap map; |
728 | { |
4187119d |
729 | if (defining_kbd_macro) |
1c997a4a |
730 | { |
731 | static add_macro_char (); |
732 | |
733 | add_macro_char (key); |
734 | } |
4187119d |
735 | |
736 | if (key > 127 && key < 256) |
737 | { |
738 | if (map[ESC].type == ISKMAP) |
739 | { |
740 | map = (Keymap)map[ESC].function; |
741 | key -= 128; |
742 | rl_dispatch (key, map); |
743 | } |
744 | else |
745 | ding (); |
746 | return; |
747 | } |
748 | |
749 | switch (map[key].type) |
750 | { |
751 | case ISFUNC: |
752 | { |
753 | Function *func = map[key].function; |
754 | |
755 | if (func != (Function *)NULL) |
756 | { |
757 | /* Special case rl_do_lowercase_version (). */ |
758 | if (func == rl_do_lowercase_version) |
759 | { |
760 | rl_dispatch (to_lower (key), map); |
761 | return; |
762 | } |
763 | |
764 | (*map[key].function)(rl_numeric_arg * arg_sign, key); |
765 | } |
766 | else |
767 | { |
768 | ding (); |
769 | return; |
770 | } |
771 | } |
772 | break; |
773 | |
774 | case ISKMAP: |
775 | if (map[key].function != (Function *)NULL) |
776 | { |
777 | int newkey; |
778 | |
779 | rl_key_sequence_length++; |
780 | newkey = rl_read_key (); |
781 | rl_dispatch (newkey, (Keymap)map[key].function); |
782 | } |
783 | else |
784 | { |
785 | ding (); |
786 | return; |
787 | } |
788 | break; |
789 | |
790 | case ISMACR: |
791 | if (map[key].function != (Function *)NULL) |
792 | { |
793 | static with_macro_input (); |
794 | char *macro = savestring ((char *)map[key].function); |
795 | |
796 | with_macro_input (macro); |
797 | return; |
798 | } |
799 | break; |
800 | } |
801 | |
802 | /* If we have input pending, then the last command was a prefix |
803 | command. Don't change the state of rl_last_func. */ |
804 | if (!rl_pending_input) |
805 | rl_last_func = map[key].function; |
806 | } |
807 | |
808 | \f |
809 | /* **************************************************************** */ |
810 | /* */ |
811 | /* Hacking Keyboard Macros */ |
812 | /* */ |
813 | /* **************************************************************** */ |
814 | |
4187119d |
815 | /* Set up to read subsequent input from STRING. |
816 | STRING is free ()'ed when we are done with it. */ |
817 | static |
818 | with_macro_input (string) |
819 | char *string; |
820 | { |
1c997a4a |
821 | static push_executing_macro (); |
822 | |
4187119d |
823 | push_executing_macro (); |
824 | executing_macro = string; |
825 | executing_macro_index = 0; |
826 | } |
827 | |
828 | /* Return the next character available from a macro, or 0 if |
829 | there are no macro characters. */ |
830 | static int |
831 | next_macro_key () |
832 | { |
833 | if (!executing_macro) |
834 | return (0); |
835 | |
836 | if (!executing_macro[executing_macro_index]) |
837 | { |
1c997a4a |
838 | static pop_executing_macro (); |
839 | |
4187119d |
840 | pop_executing_macro (); |
841 | return (next_macro_key ()); |
842 | } |
843 | |
844 | return (executing_macro[executing_macro_index++]); |
845 | } |
846 | |
847 | /* Save the currently executing macro on a stack of saved macros. */ |
848 | static |
849 | push_executing_macro () |
850 | { |
851 | struct saved_macro *saver; |
852 | |
853 | saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro)); |
854 | saver->next = macro_list; |
855 | saver->index = executing_macro_index; |
856 | saver->string = executing_macro; |
857 | |
858 | macro_list = saver; |
859 | } |
860 | |
861 | /* Discard the current macro, replacing it with the one |
862 | on the top of the stack of saved macros. */ |
863 | static |
864 | pop_executing_macro () |
865 | { |
866 | if (executing_macro) |
867 | free (executing_macro); |
868 | |
869 | executing_macro = (char *)NULL; |
870 | executing_macro_index = 0; |
871 | |
872 | if (macro_list) |
873 | { |
874 | struct saved_macro *disposer = macro_list; |
875 | executing_macro = macro_list->string; |
876 | executing_macro_index = macro_list->index; |
877 | macro_list = macro_list->next; |
878 | free (disposer); |
879 | } |
880 | } |
881 | |
882 | /* Add a character to the macro being built. */ |
883 | static |
884 | add_macro_char (c) |
885 | int c; |
886 | { |
887 | if (current_macro_index + 1 >= current_macro_size) |
888 | { |
889 | if (!current_macro) |
890 | current_macro = (char *)xmalloc (current_macro_size = 25); |
891 | else |
892 | current_macro = |
893 | (char *)xrealloc (current_macro, current_macro_size += 25); |
894 | } |
895 | |
896 | current_macro[current_macro_index++] = c; |
897 | current_macro[current_macro_index] = '\0'; |
898 | } |
899 | |
900 | /* Begin defining a keyboard macro. |
901 | Keystrokes are recorded as they are executed. |
902 | End the definition with rl_end_kbd_macro (). |
903 | If a numeric argument was explicitly typed, then append this |
904 | definition to the end of the existing macro, and start by |
905 | re-executing the existing macro. */ |
906 | rl_start_kbd_macro (ignore1, ignore2) |
907 | int ignore1, ignore2; |
908 | { |
909 | if (defining_kbd_macro) |
910 | rl_abort (); |
911 | |
912 | if (rl_explicit_arg) |
913 | { |
914 | if (current_macro) |
915 | with_macro_input (savestring (current_macro)); |
916 | } |
917 | else |
918 | current_macro_index = 0; |
919 | |
920 | defining_kbd_macro = 1; |
921 | } |
922 | |
923 | /* Stop defining a keyboard macro. |
924 | A numeric argument says to execute the macro right now, |
925 | that many times, counting the definition as the first time. */ |
926 | rl_end_kbd_macro (count, ignore) |
927 | int count, ignore; |
928 | { |
929 | if (!defining_kbd_macro) |
930 | rl_abort (); |
931 | |
932 | current_macro_index -= (rl_key_sequence_length - 1); |
933 | current_macro[current_macro_index] = '\0'; |
934 | |
935 | defining_kbd_macro = 0; |
936 | |
937 | rl_call_last_kbd_macro (--count, 0); |
938 | } |
939 | |
940 | /* Execute the most recently defined keyboard macro. |
941 | COUNT says how many times to execute it. */ |
942 | rl_call_last_kbd_macro (count, ignore) |
943 | int count, ignore; |
944 | { |
945 | if (!current_macro) |
946 | rl_abort (); |
947 | |
948 | while (count--) |
949 | with_macro_input (savestring (current_macro)); |
950 | } |
951 | |
1c997a4a |
952 | \f |
953 | /* Non-zero means do not parse any lines other than comments and |
954 | parser directives. */ |
955 | static unsigned char parsing_conditionalized_out = 0; |
4187119d |
956 | \f |
957 | /* **************************************************************** */ |
958 | /* */ |
959 | /* Initializations */ |
960 | /* */ |
961 | /* **************************************************************** */ |
962 | |
963 | /* Initliaze readline (and terminal if not already). */ |
964 | rl_initialize () |
965 | { |
966 | extern char *rl_display_prompt; |
967 | |
968 | /* If we have never been called before, initialize the |
969 | terminal and data structures. */ |
970 | if (!rl_initialized) |
971 | { |
972 | readline_initialize_everything (); |
973 | rl_initialized++; |
974 | } |
975 | |
976 | /* Initalize the current line information. */ |
977 | rl_point = rl_end = 0; |
978 | the_line = rl_line_buffer; |
979 | the_line[0] = 0; |
980 | |
981 | /* We aren't done yet. We haven't even gotten started yet! */ |
982 | rl_done = 0; |
983 | |
984 | /* Tell the history routines what is going on. */ |
985 | start_using_history (); |
986 | |
987 | /* Make the display buffer match the state of the line. */ |
988 | { |
989 | extern char *rl_display_prompt; |
990 | extern int forced_display; |
991 | |
992 | rl_on_new_line (); |
993 | |
994 | rl_display_prompt = rl_prompt ? rl_prompt : ""; |
995 | forced_display = 1; |
996 | } |
997 | |
998 | /* No such function typed yet. */ |
999 | rl_last_func = (Function *)NULL; |
1000 | |
1001 | /* Parsing of key-bindings begins in an enabled state. */ |
1002 | { |
4187119d |
1003 | parsing_conditionalized_out = 0; |
1004 | } |
1005 | } |
1006 | |
1007 | /* Initialize the entire state of the world. */ |
1008 | readline_initialize_everything () |
1009 | { |
1010 | /* Find out if we are running in Emacs. */ |
1011 | running_in_emacs = (char *)getenv ("EMACS"); |
1012 | |
1013 | /* Allocate data structures. */ |
1014 | if (!rl_line_buffer) |
1015 | rl_line_buffer = |
1016 | (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE); |
1017 | |
1018 | /* Initialize the terminal interface. */ |
1019 | init_terminal_io ((char *)NULL); |
1020 | |
1021 | /* Bind tty characters to readline functions. */ |
1022 | readline_default_bindings (); |
1023 | |
1024 | /* Initialize the function names. */ |
1025 | rl_initialize_funmap (); |
1026 | |
1027 | /* Read in the init file. */ |
1028 | rl_read_init_file ((char *)NULL); |
1029 | |
1030 | /* If the completion parser's default word break characters haven't |
1031 | been set yet, then do so now. */ |
1032 | { |
1033 | extern char *rl_completer_word_break_characters; |
1034 | extern char *rl_basic_word_break_characters; |
1035 | |
1036 | if (rl_completer_word_break_characters == (char *)NULL) |
1037 | rl_completer_word_break_characters = rl_basic_word_break_characters; |
1038 | } |
1039 | } |
1040 | |
1041 | /* If this system allows us to look at the values of the regular |
1042 | input editing characters, then bind them to their readline |
1043 | equivalents. */ |
1044 | readline_default_bindings () |
1045 | { |
1046 | #ifdef TIOCGETP |
1047 | struct sgttyb ttybuff; |
1048 | int tty = fileno (rl_instream); |
1049 | |
1050 | if (ioctl (tty, TIOCGETP, &ttybuff) != -1) |
1051 | { |
1052 | int erase = ttybuff.sg_erase, kill = ttybuff.sg_kill; |
1053 | |
1054 | if (erase != -1 && keymap[erase].type == ISFUNC) |
1055 | keymap[erase].function = rl_rubout; |
1056 | |
1057 | if (kill != -1 && keymap[kill].type == ISFUNC) |
1058 | keymap[kill].function = rl_unix_line_discard; |
1059 | } |
1060 | |
1061 | #ifdef TIOCGLTC |
1062 | { |
1063 | struct ltchars lt; |
1064 | |
1065 | if (ioctl (tty, TIOCGLTC, <) != -1) |
1066 | { |
1067 | int erase = lt.t_werasc, nextc = lt.t_lnextc; |
1068 | |
1069 | if (erase != -1 && keymap[erase].type == ISFUNC) |
1070 | keymap[erase].function = rl_unix_word_rubout; |
1071 | |
1072 | if (nextc != -1 && keymap[nextc].type == ISFUNC) |
1073 | keymap[nextc].function = rl_quoted_insert; |
1074 | } |
1075 | } |
1076 | #endif /* TIOCGLTC */ |
1077 | #endif /* TIOCGETP */ |
1078 | } |
1079 | |
1080 | \f |
1081 | /* **************************************************************** */ |
1082 | /* */ |
1083 | /* Numeric Arguments */ |
1084 | /* */ |
1085 | /* **************************************************************** */ |
1086 | |
1087 | /* Handle C-u style numeric args, as well as M--, and M-digits. */ |
1088 | |
1089 | /* Add the current digit to the argument in progress. */ |
1090 | rl_digit_argument (ignore, key) |
1091 | int ignore, key; |
1092 | { |
1093 | rl_pending_input = key; |
1094 | rl_digit_loop (); |
1095 | } |
1096 | |
1097 | /* What to do when you abort reading an argument. */ |
1098 | rl_discard_argument () |
1099 | { |
1100 | ding (); |
1101 | rl_clear_message (); |
1102 | rl_init_argument (); |
1103 | } |
1104 | |
1105 | /* Create a default argument. */ |
1106 | rl_init_argument () |
1107 | { |
1108 | rl_numeric_arg = arg_sign = 1; |
1109 | rl_explicit_arg = 0; |
1110 | } |
1111 | |
1112 | /* C-u, universal argument. Multiply the current argument by 4. |
1113 | Read a key. If the key has nothing to do with arguments, then |
1114 | dispatch on it. If the key is the abort character then abort. */ |
1115 | rl_universal_argument () |
1116 | { |
1117 | rl_numeric_arg *= 4; |
1118 | rl_digit_loop (); |
1119 | } |
1120 | |
1121 | rl_digit_loop () |
1122 | { |
1123 | int key, c; |
1124 | while (1) |
1125 | { |
1126 | rl_message ("(arg: %d) ", arg_sign * rl_numeric_arg); |
1127 | key = c = rl_read_key (); |
1128 | |
1129 | if (keymap[c].type == ISFUNC && |
1130 | keymap[c].function == rl_universal_argument) |
1131 | { |
1132 | rl_numeric_arg *= 4; |
1133 | continue; |
1134 | } |
1135 | c = UNMETA (c); |
1136 | if (numeric (c)) |
1137 | { |
1138 | if (rl_explicit_arg) |
1139 | rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0'); |
1140 | else |
1141 | rl_numeric_arg = (c - '0'); |
1142 | rl_explicit_arg = 1; |
1143 | } |
1144 | else |
1145 | { |
1146 | if (c == '-' && !rl_explicit_arg) |
1147 | { |
1148 | rl_numeric_arg = 1; |
1149 | arg_sign = -1; |
1150 | } |
1151 | else |
1152 | { |
1153 | rl_clear_message (); |
1154 | rl_dispatch (key, keymap); |
1155 | return; |
1156 | } |
1157 | } |
1158 | } |
1159 | } |
1160 | |
1161 | \f |
1162 | /* **************************************************************** */ |
1163 | /* */ |
1164 | /* Display stuff */ |
1165 | /* */ |
1166 | /* **************************************************************** */ |
1167 | |
1168 | /* This is the stuff that is hard for me. I never seem to write good |
1169 | display routines in C. Let's see how I do this time. */ |
1170 | |
1171 | /* (PWP) Well... Good for a simple line updater, but totally ignores |
1172 | the problems of input lines longer than the screen width. |
1173 | |
1174 | update_line and the code that calls it makes a multiple line, |
1175 | automatically wrapping line update. Carefull attention needs |
1176 | to be paid to the vertical position variables. |
1177 | |
1178 | handling of terminals with autowrap on (incl. DEC braindamage) |
1179 | could be improved a bit. Right now I just cheat and decrement |
1180 | screenwidth by one. */ |
1181 | |
1182 | /* Keep two buffers; one which reflects the current contents of the |
1183 | screen, and the other to draw what we think the new contents should |
1184 | be. Then compare the buffers, and make whatever changes to the |
1185 | screen itself that we should. Finally, make the buffer that we |
1186 | just drew into be the one which reflects the current contents of the |
1187 | screen, and place the cursor where it belongs. |
1188 | |
1189 | Commands that want to can fix the display themselves, and then let |
1190 | this function know that the display has been fixed by setting the |
1191 | RL_DISPLAY_FIXED variable. This is good for efficiency. */ |
1192 | |
1193 | /* Termcap variables: */ |
1194 | extern char *term_up, *term_dc, *term_cr; |
1195 | extern int screenheight, screenwidth, terminal_can_insert; |
1196 | |
1197 | /* What YOU turn on when you have handled all redisplay yourself. */ |
1198 | int rl_display_fixed = 0; |
1199 | |
1200 | /* The visible cursor position. If you print some text, adjust this. */ |
1201 | int last_c_pos = 0; |
1202 | int last_v_pos = 0; |
1203 | |
1204 | /* The last left edge of text that was displayed. This is used when |
1205 | doing horizontal scrolling. It shifts in thirds of a screenwidth. */ |
1206 | static int last_lmargin = 0; |
1207 | |
1208 | /* The line display buffers. One is the line currently displayed on |
1209 | the screen. The other is the line about to be displayed. */ |
1210 | static char *visible_line = (char *)NULL; |
1211 | static char *invisible_line = (char *)NULL; |
1212 | |
1213 | /* Number of lines currently on screen minus 1. */ |
1214 | int vis_botlin = 0; |
1215 | |
1216 | /* A buffer for `modeline' messages. */ |
1217 | char msg_buf[128]; |
1218 | |
1219 | /* Non-zero forces the redisplay even if we thought it was unnecessary. */ |
1220 | int forced_display = 0; |
1221 | |
1222 | /* The stuff that gets printed out before the actual text of the line. |
1223 | This is usually pointing to rl_prompt. */ |
1224 | char *rl_display_prompt = (char *)NULL; |
1225 | |
1226 | /* Default and initial buffer size. Can grow. */ |
1227 | static int line_size = 1024; |
1228 | |
1229 | /* Non-zero means to always use horizontal scrolling in line display. */ |
1230 | int horizontal_scroll_mode = 0; |
1231 | |
1232 | /* I really disagree with this, but my boss (among others) insists that we |
1233 | support compilers that don't work. I don't think we are gaining by doing |
1234 | so; what is the advantage in producing better code if we can't use it? */ |
1235 | /* The following two declarations belong inside the |
1236 | function block, not here. */ |
1237 | static void move_cursor_relative (); |
1238 | static void output_some_chars (); |
1239 | |
1240 | /* Basic redisplay algorithm. */ |
1241 | rl_redisplay () |
1242 | { |
1243 | register int in, out, c, linenum; |
1244 | register char *line = invisible_line; |
1245 | int c_pos = 0; |
1246 | int inv_botlin = 0; /* Number of lines in newly drawn buffer. */ |
1247 | |
1248 | extern int readline_echoing_p; |
1249 | |
1250 | if (!readline_echoing_p) |
1251 | return; |
1252 | |
1253 | if (!rl_display_prompt) |
1254 | rl_display_prompt = ""; |
1255 | |
1256 | if (!invisible_line) |
1257 | { |
1258 | visible_line = (char *)xmalloc (line_size); |
1259 | invisible_line = (char *)xmalloc (line_size); |
1260 | line = invisible_line; |
1261 | for (in = 0; in < line_size; in++) |
1262 | { |
1263 | visible_line[in] = 0; |
1264 | invisible_line[in] = 1; |
1265 | } |
1266 | rl_on_new_line (); |
1267 | } |
1268 | |
1269 | /* Draw the line into the buffer. */ |
1270 | c_pos = -1; |
1271 | |
1272 | /* Mark the line as modified or not. We only do this for history |
1273 | lines. */ |
1274 | out = 0; |
1275 | if (current_history () && rl_undo_list) |
1276 | { |
1277 | line[out++] = '*'; |
1278 | line[out] = '\0'; |
1279 | } |
1280 | |
1281 | /* If someone thought that the redisplay was handled, but the currently |
1282 | visible line has a different modification state than the one about |
1283 | to become visible, then correct the callers misconception. */ |
1284 | if (visible_line[0] != invisible_line[0]) |
1285 | rl_display_fixed = 0; |
1286 | |
1287 | strncpy (line + out, rl_display_prompt, strlen (rl_display_prompt)); |
1288 | out += strlen (rl_display_prompt); |
1289 | line[out] = '\0'; |
1290 | |
1291 | for (in = 0; in < rl_end; in++) |
1292 | { |
1293 | c = the_line[in]; |
1294 | |
1295 | if (out + 1 >= line_size) |
1296 | { |
1297 | line_size *= 2; |
1298 | visible_line = (char *)xrealloc (visible_line, line_size); |
1299 | invisible_line = (char *)xrealloc (invisible_line, line_size); |
1300 | line = invisible_line; |
1301 | } |
1302 | |
1303 | if (in == rl_point) |
1304 | c_pos = out; |
1305 | |
1306 | if (c > 127) |
1307 | { |
1308 | line[out++] = 'M'; |
1309 | line[out++] = '-'; |
1310 | line[out++] = c - 128; |
1311 | } |
1312 | #define DISPLAY_TABS |
1313 | #ifdef DISPLAY_TABS |
1314 | else if (c == '\t') |
1315 | { |
1316 | register int newout = (out | (int)7) + 1; |
1317 | while (out < newout) |
1318 | line[out++] = ' '; |
1319 | } |
1320 | #endif |
1321 | else if (c < 32) |
1322 | { |
1323 | line[out++] = 'C'; |
1324 | line[out++] = '-'; |
1325 | line[out++] = c + 64; |
1326 | } |
1327 | else |
1328 | line[out++] = c; |
1329 | } |
1330 | line[out] = '\0'; |
1331 | if (c_pos < 0) |
1332 | c_pos = out; |
1333 | |
1334 | /* PWP: now is when things get a bit hairy. The visible and invisible |
1335 | line buffers are really multiple lines, which would wrap every |
1336 | (screenwidth - 1) characters. Go through each in turn, finding |
1337 | the changed region and updating it. The line order is top to bottom. */ |
1338 | |
1339 | /* If we can move the cursor up and down, then use multiple lines, |
1340 | otherwise, let long lines display in a single terminal line, and |
1341 | horizontally scroll it. */ |
1342 | |
1343 | if (!horizontal_scroll_mode && term_up && *term_up) |
1344 | { |
1345 | int total_screen_chars = (screenwidth * screenheight); |
1346 | |
1347 | if (!rl_display_fixed || forced_display) |
1348 | { |
1349 | forced_display = 0; |
1350 | |
1351 | /* If we have more than a screenful of material to display, then |
1352 | only display a screenful. We should display the last screen, |
1353 | not the first. I'll fix this in a minute. */ |
1354 | if (out >= total_screen_chars) |
1355 | out = total_screen_chars - 1; |
1356 | |
1357 | /* Number of screen lines to display. */ |
1358 | inv_botlin = out / screenwidth; |
1359 | |
1360 | /* For each line in the buffer, do the updating display. */ |
1361 | for (linenum = 0; linenum <= inv_botlin; linenum++) |
1362 | update_line (linenum > vis_botlin ? "" |
1363 | : &visible_line[linenum * screenwidth], |
1364 | &invisible_line[linenum * screenwidth], |
1365 | linenum); |
1366 | |
1367 | /* We may have deleted some lines. If so, clear the left over |
1368 | blank ones at the bottom out. */ |
1369 | if (vis_botlin > inv_botlin) |
1370 | { |
1371 | char *tt; |
1372 | for (; linenum <= vis_botlin; linenum++) |
1373 | { |
1374 | tt = &visible_line[linenum * screenwidth]; |
1375 | move_vert (linenum); |
1376 | move_cursor_relative (0, tt); |
1377 | clear_to_eol ((linenum == vis_botlin)? |
1378 | strlen (tt) : screenwidth); |
1379 | } |
1380 | } |
1381 | vis_botlin = inv_botlin; |
1382 | |
1383 | /* Move the cursor where it should be. */ |
1384 | move_vert (c_pos / screenwidth); |
1385 | move_cursor_relative (c_pos % screenwidth, |
1386 | &invisible_line[(c_pos / screenwidth) * screenwidth]); |
1387 | } |
1388 | } |
1389 | else /* Do horizontal scrolling. */ |
1390 | { |
1391 | int lmargin; |
1392 | |
1393 | /* Always at top line. */ |
1394 | last_v_pos = 0; |
1395 | |
1396 | /* If the display position of the cursor would be off the edge |
1397 | of the screen, start the display of this line at an offset that |
1398 | leaves the cursor on the screen. */ |
1399 | if (c_pos - last_lmargin > screenwidth - 2) |
1400 | lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3); |
1401 | else if (c_pos - last_lmargin < 1) |
1402 | lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3); |
1403 | else |
1404 | lmargin = last_lmargin; |
1405 | |
1406 | /* If the first character on the screen isn't the first character |
1407 | in the display line, indicate this with a special character. */ |
1408 | if (lmargin > 0) |
1409 | line[lmargin] = '<'; |
1410 | |
1411 | if (lmargin + screenwidth < out) |
1412 | line[lmargin + screenwidth - 1] = '>'; |
1413 | |
1414 | if (!rl_display_fixed || forced_display || lmargin != last_lmargin) |
1415 | { |
1416 | forced_display = 0; |
1417 | update_line (&visible_line[last_lmargin], |
1418 | &invisible_line[lmargin], 0); |
1419 | |
1420 | move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]); |
1421 | last_lmargin = lmargin; |
1422 | } |
1423 | } |
1424 | fflush (out_stream); |
1425 | |
1426 | /* Swap visible and non-visible lines. */ |
1427 | { |
1428 | char *temp = visible_line; |
1429 | visible_line = invisible_line; |
1430 | invisible_line = temp; |
1431 | rl_display_fixed = 0; |
1432 | } |
1433 | } |
1434 | |
1435 | /* PWP: update_line() is based on finding the middle difference of each |
1436 | line on the screen; vis: |
1437 | |
1438 | /old first difference |
1439 | /beginning of line | /old last same /old EOL |
1440 | v v v v |
1441 | old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as |
1442 | new: eddie> Oh, my little buggy says to me, as lurgid as |
1443 | ^ ^ ^ ^ |
1444 | \beginning of line | \new last same \new end of line |
1445 | \new first difference |
1446 | |
1447 | All are character pointers for the sake of speed. Special cases for |
1448 | no differences, as well as for end of line additions must be handeled. |
1449 | |
1450 | Could be made even smarter, but this works well enough */ |
1451 | static |
1452 | update_line (old, new, current_line) |
1453 | register char *old, *new; |
1454 | int current_line; |
1455 | { |
1456 | register char *ofd, *ols, *oe, *nfd, *nls, *ne; |
1457 | int lendiff, wsatend; |
1458 | |
1459 | /* Find first difference. */ |
1460 | for (ofd = old, nfd = new; |
1461 | (ofd - old < screenwidth) && *ofd && (*ofd == *nfd); |
1462 | ofd++, nfd++) |
1463 | ; |
1464 | |
1465 | /* Move to the end of the screen line. */ |
1466 | for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++); |
1467 | for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++); |
1468 | |
1469 | /* If no difference, continue to next line. */ |
1470 | if (ofd == oe && nfd == ne) |
1471 | return; |
1472 | |
1473 | wsatend = 1; /* flag for trailing whitespace */ |
1474 | ols = oe - 1; /* find last same */ |
1475 | nls = ne - 1; |
1476 | while ((*ols == *nls) && (ols > ofd) && (nls > nfd)) |
1477 | { |
1478 | if (*ols != ' ') |
1479 | wsatend = 0; |
1480 | ols--; |
1481 | nls--; |
1482 | } |
1483 | |
1484 | if (wsatend) |
1485 | { |
1486 | ols = oe; |
1487 | nls = ne; |
1488 | } |
1489 | else if (*ols != *nls) |
1490 | { |
1491 | if (*ols) /* don't step past the NUL */ |
1492 | ols++; |
1493 | if (*nls) |
1494 | nls++; |
1495 | } |
1496 | |
1497 | move_vert (current_line); |
1498 | move_cursor_relative (ofd - old, old); |
1499 | |
1500 | /* if (len (new) > len (old)) */ |
1501 | lendiff = (nls - nfd) - (ols - ofd); |
1502 | |
1503 | /* Insert (diff(len(old),len(new)) ch */ |
1504 | if (lendiff > 0) |
1505 | { |
1506 | if (terminal_can_insert) |
1507 | { |
1508 | extern char *term_IC; |
1509 | |
1510 | /* Sometimes it is cheaper to print the characters rather than |
1511 | use the terminal's capabilities. */ |
1512 | if ((2 * (ne - nfd)) < lendiff && (!term_IC || !*term_IC)) |
1513 | { |
1514 | output_some_chars (nfd, (ne - nfd)); |
1515 | last_c_pos += (ne - nfd); |
1516 | } |
1517 | else |
1518 | { |
1519 | if (*ols) |
1520 | { |
1521 | start_insert (lendiff); |
1522 | output_some_chars (nfd, lendiff); |
1523 | last_c_pos += lendiff; |
1524 | end_insert (); |
1525 | } |
1526 | else |
1527 | { |
1528 | /* At the end of a line the characters do not have to |
1529 | be "inserted". They can just be placed on the screen. */ |
1530 | output_some_chars (nfd, lendiff); |
1531 | last_c_pos += lendiff; |
1532 | } |
1533 | /* Copy (new) chars to screen from first diff to last match. */ |
1534 | if (((nls - nfd) - lendiff) > 0) |
1535 | { |
1536 | output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff)); |
1537 | last_c_pos += ((nls - nfd) - lendiff); |
1538 | } |
1539 | } |
1540 | } |
1541 | else |
1542 | { /* cannot insert chars, write to EOL */ |
1543 | output_some_chars (nfd, (ne - nfd)); |
1544 | last_c_pos += (ne - nfd); |
1545 | } |
1546 | } |
1547 | else /* Delete characters from line. */ |
1548 | { |
1549 | /* If possible and inexpensive to use terminal deletion, then do so. */ |
1550 | if (term_dc && (2 * (ne - nfd)) >= (-lendiff)) |
1551 | { |
1552 | if (lendiff) |
1553 | delete_chars (-lendiff); /* delete (diff) characters */ |
1554 | |
1555 | /* Copy (new) chars to screen from first diff to last match */ |
1556 | if ((nls - nfd) > 0) |
1557 | { |
1558 | output_some_chars (nfd, (nls - nfd)); |
1559 | last_c_pos += (nls - nfd); |
1560 | } |
1561 | } |
1562 | /* Otherwise, print over the existing material. */ |
1563 | else |
1564 | { |
1565 | output_some_chars (nfd, (ne - nfd)); |
1566 | last_c_pos += (ne - nfd); |
1567 | clear_to_eol ((oe - old) - (ne - new)); |
1568 | } |
1569 | } |
1570 | } |
1571 | |
1572 | /* (PWP) tell the update routines that we have moved onto a |
1573 | new (empty) line. */ |
1574 | rl_on_new_line () |
1575 | { |
1576 | if (visible_line) |
1577 | visible_line[0] = '\0'; |
1578 | |
1579 | last_c_pos = last_v_pos = 0; |
1580 | vis_botlin = last_lmargin = 0; |
1581 | } |
1582 | |
1583 | /* Actually update the display, period. */ |
1584 | rl_forced_update_display () |
1585 | { |
1586 | if (visible_line) |
1587 | { |
1588 | register char *temp = visible_line; |
1589 | |
1590 | while (*temp) *temp++ = '\0'; |
1591 | } |
1592 | rl_on_new_line (); |
1593 | forced_display++; |
1594 | rl_redisplay (); |
1595 | } |
1596 | |
1597 | /* Move the cursor from last_c_pos to NEW, which are buffer indices. |
1598 | DATA is the contents of the screen line of interest; i.e., where |
1599 | the movement is being done. */ |
1600 | static void |
1601 | move_cursor_relative (new, data) |
1602 | int new; |
1603 | char *data; |
1604 | { |
1605 | register int i; |
1606 | static void output_character_function (); |
1607 | |
1608 | /* It may be faster to output a CR, and then move forwards instead |
1609 | of moving backwards. */ |
1610 | if (new + 1 < last_c_pos - new) |
1611 | { |
1612 | tputs (term_cr, 1, output_character_function); |
1613 | last_c_pos = 0; |
1614 | } |
1615 | |
1616 | if (last_c_pos == new) return; |
1617 | |
1618 | if (last_c_pos < new) |
1619 | { |
1620 | /* Move the cursor forward. We do it by printing the command |
1621 | to move the cursor forward if there is one, else print that |
1622 | portion of the output buffer again. Which is cheaper? */ |
1623 | |
1624 | /* The above comment is left here for posterity. It is faster |
1625 | to print one character (non-control) than to print a control |
1626 | sequence telling the terminal to move forward one character. |
1627 | That kind of control is for people who don't know what the |
1628 | data is underneath the cursor. */ |
1629 | #ifdef HACK_TERMCAP_MOTION |
1630 | extern char *term_forward_char; |
1631 | |
1632 | if (term_forward_char) |
1633 | for (i = last_c_pos; i < new; i++) |
1634 | tputs (term_forward_char, 1, output_character_function); |
1635 | else |
1636 | for (i = last_c_pos; i < new; i++) |
1637 | putc (data[i], out_stream); |
1638 | #else |
1639 | for (i = last_c_pos; i < new; i++) |
1640 | putc (data[i], out_stream); |
1641 | #endif /* HACK_TERMCAP_MOTION */ |
1642 | } |
1643 | else |
1644 | backspace (last_c_pos - new); |
1645 | last_c_pos = new; |
1646 | } |
1647 | |
1648 | /* PWP: move the cursor up or down. */ |
1649 | move_vert (to) |
1650 | int to; |
1651 | { |
1652 | void output_character_function (); |
1653 | register int delta, i; |
1654 | |
1655 | if (last_v_pos == to) return; |
1656 | |
1657 | if (to > screenheight) |
1658 | return; |
1659 | |
1660 | if ((delta = to - last_v_pos) > 0) |
1661 | { |
1662 | for (i = 0; i < delta; i++) |
1663 | putc ('\n', out_stream); |
1664 | tputs (term_cr, 1, output_character_function); |
1665 | last_c_pos = 0; /* because crlf() will do \r\n */ |
1666 | } |
1667 | else |
1668 | { /* delta < 0 */ |
1669 | if (term_up && *term_up) |
1670 | for (i = 0; i < -delta; i++) |
1671 | tputs (term_up, 1, output_character_function); |
1672 | } |
1673 | last_v_pos = to; /* now to is here */ |
1674 | } |
1675 | |
1676 | /* Physically print C on out_stream. This is for functions which know |
1677 | how to optimize the display. */ |
1678 | rl_show_char (c) |
1679 | int c; |
1680 | { |
1681 | if (c > 127) |
1682 | { |
1683 | fprintf (out_stream, "M-"); |
1684 | c -= 128; |
1685 | } |
1686 | |
1687 | #ifdef DISPLAY_TABS |
1688 | if (c < 32 && c != '\t') |
1689 | #else |
1690 | if (c < 32) |
1691 | #endif |
1692 | { |
1693 | |
1694 | c += 64; |
1695 | } |
1696 | |
1697 | putc (c, out_stream); |
1698 | fflush (out_stream); |
1699 | } |
1700 | |
1701 | #ifdef DISPLAY_TABS |
1702 | int |
1703 | rl_character_len (c, pos) |
1704 | register int c, pos; |
1705 | { |
1706 | if (c < ' ' || c > 126) |
1707 | { |
1708 | if (c == '\t') |
1709 | return (((pos | (int)7) + 1) - pos); |
1710 | else |
1711 | return (3); |
1712 | } |
1713 | else |
1714 | return (1); |
1715 | } |
1716 | #else |
1717 | int |
1718 | rl_character_len (c) |
1719 | int c; |
1720 | { |
1721 | if (c < ' ' || c > 126) |
1722 | return (3); |
1723 | else |
1724 | return (1); |
1725 | } |
1726 | #endif /* DISPLAY_TAB */ |
1727 | |
1728 | /* How to print things in the "echo-area". The prompt is treated as a |
1729 | mini-modeline. */ |
1730 | rl_message (string, arg1, arg2) |
1731 | char *string; |
1732 | { |
1733 | sprintf (msg_buf, string, arg1, arg2); |
1734 | rl_display_prompt = msg_buf; |
1735 | rl_redisplay (); |
1736 | } |
1737 | |
1738 | /* How to clear things from the "echo-area". */ |
1739 | rl_clear_message () |
1740 | { |
1741 | rl_display_prompt = rl_prompt; |
1742 | rl_redisplay (); |
1743 | } |
1744 | \f |
1745 | /* **************************************************************** */ |
1746 | /* */ |
1747 | /* Terminal and Termcap */ |
1748 | /* */ |
1749 | /* **************************************************************** */ |
1750 | |
1751 | static char *term_buffer = (char *)NULL; |
1752 | static char *term_string_buffer = (char *)NULL; |
1753 | |
1754 | /* Non-zero means this terminal can't really do anything. */ |
1755 | int dumb_term = 0; |
1756 | |
1757 | char PC; |
1758 | char *BC, *UP; |
1759 | |
1760 | /* Some strings to control terminal actions. These are output by tputs (). */ |
1761 | char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace; |
1762 | |
1763 | int screenwidth, screenheight; |
1764 | |
1765 | /* Non-zero if we determine that the terminal can do character insertion. */ |
1766 | int terminal_can_insert = 0; |
1767 | |
1768 | /* How to insert characters. */ |
1769 | char *term_im, *term_ei, *term_ic, *term_ip, *term_IC; |
1770 | |
1771 | /* How to delete characters. */ |
1772 | char *term_dc, *term_DC; |
1773 | |
1774 | #ifdef HACK_TERMCAP_MOTION |
1775 | char *term_forward_char; |
1776 | #endif /* HACK_TERMCAP_MOTION */ |
1777 | |
1778 | /* How to go up a line. */ |
1779 | char *term_up; |
1780 | |
1781 | /* Re-initialize the terminal considering that the TERM/TERMCAP variable |
1782 | has changed. */ |
1783 | rl_reset_terminal (terminal_name) |
1784 | char *terminal_name; |
1785 | { |
1786 | init_terminal_io (terminal_name); |
1787 | } |
1788 | |
1789 | init_terminal_io (terminal_name) |
1790 | char *terminal_name; |
1791 | { |
1792 | char *term = (terminal_name? terminal_name : (char *)getenv ("TERM")); |
1793 | char *tgetstr (), *buffer; |
1794 | |
1795 | |
1796 | if (!term_string_buffer) |
1797 | term_string_buffer = (char *)xmalloc (2048); |
1798 | |
1799 | if (!term_buffer) |
1800 | term_buffer = (char *)xmalloc (2048); |
1801 | |
1802 | buffer = term_string_buffer; |
1803 | |
1804 | term_clrpag = term_cr = term_clreol = (char *)NULL; |
1805 | |
1806 | if (!term) |
1807 | term = "dumb"; |
1808 | |
1809 | if (tgetent (term_buffer, term) < 0) |
1810 | { |
1811 | dumb_term = 1; |
1812 | return; |
1813 | } |
1814 | |
1815 | BC = tgetstr ("pc", &buffer); |
1816 | PC = buffer ? *buffer : 0; |
1817 | |
1818 | term_backspace = tgetstr ("le", &buffer); |
1819 | |
1820 | term_cr = tgetstr ("cr", &buffer); |
1821 | term_clreol = tgetstr ("ce", &buffer); |
1822 | term_clrpag = tgetstr ("cl", &buffer); |
1823 | |
1824 | if (!term_cr) |
1825 | term_cr = "\r"; |
1826 | |
1827 | #ifdef HACK_TERMCAP_MOTION |
1828 | term_forward_char = tgetstr ("nd", &buffer); |
1829 | #endif /* HACK_TERMCAP_MOTION */ |
1830 | |
1831 | screenwidth = tgetnum ("co"); |
1832 | if (screenwidth <= 0) |
1833 | screenwidth = 80; |
1834 | screenwidth--; /* PWP: avoid autowrap bugs */ |
1835 | |
1836 | screenheight = tgetnum ("li"); |
1837 | if (screenheight <= 0) |
1838 | screenheight = 24; |
1839 | |
1840 | term_im = tgetstr ("im", &buffer); |
1841 | term_ei = tgetstr ("ei", &buffer); |
1842 | term_IC = tgetstr ("IC", &buffer); |
1843 | term_ic = tgetstr ("ic", &buffer); |
1844 | term_ip = tgetstr ("ip", &buffer); |
1845 | term_IC = tgetstr ("IC", &buffer); |
1846 | |
1847 | /* "An application program can assume that the terminal can do |
1848 | character insertion if *any one of* the capabilities `IC', |
1849 | `im', `ic' or `ip' is provided." */ |
1850 | terminal_can_insert = (term_IC || term_im || term_ic || term_ip); |
1851 | |
1852 | term_up = tgetstr ("up", &buffer); |
1853 | term_dc = tgetstr ("dc", &buffer); |
1854 | term_DC = tgetstr ("DC", &buffer); |
1855 | } |
1856 | |
1857 | /* A function for the use of tputs () */ |
1858 | static void |
1859 | output_character_function (c) |
1860 | int c; |
1861 | { |
1862 | putc (c, out_stream); |
1863 | } |
1864 | |
1865 | /* Write COUNT characters from STRING to the output stream. */ |
1866 | static void |
1867 | output_some_chars (string, count) |
1868 | char *string; |
1869 | int count; |
1870 | { |
1871 | fwrite (string, 1, count, out_stream); |
1872 | } |
1873 | |
1874 | |
1875 | /* Delete COUNT characters from the display line. */ |
1876 | static |
1877 | delete_chars (count) |
1878 | int count; |
1879 | { |
1880 | if (count > screenwidth) |
1881 | return; |
1882 | |
1883 | if (term_DC && *term_DC) |
1884 | { |
1885 | char *tgoto (), *buffer; |
1886 | buffer = tgoto (term_DC, 0, count); |
1887 | tputs (buffer, 1, output_character_function); |
1888 | } |
1889 | else |
1890 | { |
1891 | if (term_dc && *term_dc) |
1892 | while (count--) |
1893 | tputs (term_dc, 1, output_character_function); |
1894 | } |
1895 | } |
1896 | |
1897 | /* Prepare to insert by inserting COUNT blank spaces. */ |
1898 | static |
1899 | start_insert (count) |
1900 | int count; |
1901 | { |
1902 | if (term_im && *term_im) |
1903 | tputs (term_im, 1, output_character_function); |
1904 | |
1905 | if (term_IC && *term_IC && |
1906 | (count > 1 || !term_ic || !*term_ic)) |
1907 | { |
1908 | char *tgoto (), *buffer; |
1909 | buffer = tgoto (term_IC, 0, count); |
1910 | tputs (buffer, 1, output_character_function); |
1911 | } |
1912 | else |
1913 | { |
1914 | if (term_ic && *term_ic) |
1915 | while (count--) |
1916 | tputs (term_ic, 1, output_character_function); |
1917 | } |
1918 | } |
1919 | |
1920 | /* We are finished doing our insertion. Send ending string. */ |
1921 | static |
1922 | end_insert () |
1923 | { |
1924 | if (term_ei && *term_ei) |
1925 | tputs (term_ei, 1, output_character_function); |
1926 | } |
1927 | |
1928 | /* Move the cursor back. */ |
1929 | backspace (count) |
1930 | int count; |
1931 | { |
1932 | register int i; |
1933 | |
1934 | if (term_backspace) |
1935 | for (i = 0; i < count; i++) |
1936 | tputs (term_backspace, 1, output_character_function); |
1937 | else |
1938 | for (i = 0; i < count; i++) |
1939 | putc ('\b', out_stream); |
1940 | } |
1941 | |
1942 | /* Move to the start of the next line. */ |
1943 | crlf () |
1944 | { |
1945 | tputs (term_cr, 1, output_character_function); |
1946 | putc ('\n', out_stream); |
1947 | } |
1948 | |
1949 | /* Clear to the end of the line. COUNT is the minimum |
1950 | number of character spaces to clear, */ |
1951 | clear_to_eol (count) |
1952 | int count; |
1953 | { |
1954 | if (term_clreol) { |
1955 | tputs (term_clreol, 1, output_character_function); |
1956 | } else { |
1957 | register int i; |
1958 | /* Do one more character space. */ |
1959 | count++; |
1960 | for (i = 0; i < count; i++) |
1961 | putc (' ', out_stream); |
1962 | backspace (count); |
1963 | } |
1964 | } |
1965 | |
1966 | \f |
1967 | /* **************************************************************** */ |
1968 | /* */ |
1969 | /* Saving and Restoring the TTY */ |
1970 | /* */ |
1971 | /* **************************************************************** */ |
1972 | |
1973 | #ifdef NEW_TTY_DRIVER |
1974 | |
1975 | /* Standard flags, including ECHO. */ |
1976 | static int original_tty_flags = 0; |
1977 | |
1978 | /* Local mode flags, like LPASS8. */ |
1979 | static int local_mode_flags = 0; |
1980 | |
1981 | /* Terminal characters. This has C-s and C-q in it. */ |
1982 | static struct tchars original_tchars; |
1983 | |
1984 | /* Local special characters. This has the interrupt characters in it. */ |
1985 | static struct ltchars original_ltchars; |
1986 | |
1987 | /* We use this to get and set the tty_flags. */ |
1988 | static struct sgttyb the_ttybuff; |
1989 | |
1990 | /* Put the terminal in CBREAK mode so that we can detect key presses. */ |
1991 | static |
1992 | rl_prep_terminal () |
1993 | { |
1994 | int tty = fileno (rl_instream); |
1995 | |
1996 | /* We always get the latest tty values. Maybe stty changed them. */ |
1997 | |
1998 | ioctl (tty, TIOCGETP, &the_ttybuff); |
1999 | original_tty_flags = the_ttybuff.sg_flags; |
2000 | |
2001 | readline_echoing_p = (original_tty_flags & ECHO); |
2002 | |
2003 | /* If this terminal doesn't care how the 8th bit is used, |
2004 | then we can use it for the meta-key. |
2005 | We check by seeing if BOTH odd and even parity are allowed. */ |
2006 | if ((the_ttybuff.sg_flags & (ODDP | EVENP)) == (ODDP | EVENP)) |
2007 | { |
2008 | #ifdef PASS8 |
2009 | the_ttybuff.sg_flags |= PASS8; |
2010 | #endif |
2011 | |
2012 | #if defined (TIOCLGET) && defined (LPASS8) |
2013 | { |
2014 | int flags; |
2015 | ioctl (tty, TIOCLGET, &flags); |
2016 | local_mode_flags = flags; |
2017 | flags |= LPASS8; |
2018 | ioctl (tty, TIOCLSET, &flags); |
2019 | } |
2020 | #endif |
2021 | } |
2022 | |
2023 | #ifdef TIOCGETC |
2024 | { |
2025 | struct tchars temp; |
2026 | |
2027 | ioctl (tty, TIOCGETC, &original_tchars); |
2028 | bcopy (&original_tchars, &temp, sizeof (struct tchars)); |
2029 | |
2030 | /* Get rid of C-s and C-q. |
2031 | We remember the value of startc (C-q) so that if the terminal is in |
2032 | xoff state, the user can xon it by pressing that character. */ |
2033 | xon_char = temp.t_startc; |
2034 | temp.t_stopc = -1; |
2035 | temp.t_startc = -1; |
2036 | |
2037 | /* If there is an XON character, bind it to restart the output. */ |
2038 | if (xon_char != -1) |
2039 | rl_bind_key (xon_char, rl_restart_output); |
2040 | |
2041 | /* If there is an EOF char, bind eof_char to it. */ |
2042 | if (temp.t_eofc != -1) |
2043 | eof_char = temp.t_eofc; |
2044 | |
2045 | #ifdef NEVER |
2046 | /* Get rid of C-\ and C-c. */ |
2047 | temp.t_intrc = temp.t_quitc = -1; |
2048 | #endif |
2049 | |
2050 | ioctl (tty, TIOCSETC, &temp); |
2051 | } |
2052 | #endif /* TIOCGETC */ |
2053 | |
2054 | #ifdef TIOCGLTC |
2055 | { |
2056 | struct ltchars temp; |
2057 | |
2058 | ioctl (tty, TIOCGLTC, &original_ltchars); |
2059 | bcopy (&original_ltchars, &temp, sizeof (struct ltchars)); |
2060 | |
2061 | /* Make the interrupt keys go away. Just enough to make people happy. */ |
2062 | temp.t_dsuspc = -1; /* C-y */ |
2063 | temp.t_lnextc = -1; /* C-v */ |
2064 | |
2065 | ioctl (tty, TIOCSLTC, &temp); |
2066 | } |
2067 | #endif /* TIOCGLTC */ |
2068 | |
2069 | the_ttybuff.sg_flags &= ~ECHO; |
2070 | the_ttybuff.sg_flags |= CBREAK; |
2071 | ioctl (tty, TIOCSETN, &the_ttybuff); |
2072 | } |
2073 | |
2074 | /* Restore the terminal to its original state. */ |
2075 | static |
2076 | rl_deprep_terminal () |
2077 | { |
2078 | int tty = fileno (rl_instream); |
2079 | |
2080 | #if defined (TIOCLGET) && defined (LPASS8) |
7a67dd45 |
2081 | if ((the_ttybuff.sg_flags & (ODDP | EVENP)) == (ODDP | EVENP)) |
2082 | ioctl (tty, TIOCLSET, &local_mode_flags); |
4187119d |
2083 | #endif |
2084 | |
2085 | #ifdef TIOCSLTC |
2086 | ioctl (tty, TIOCSLTC, &original_ltchars); |
2087 | #endif |
2088 | |
2089 | #ifdef TIOCSETC |
2090 | ioctl (tty, TIOCSETC, &original_tchars); |
2091 | #endif |
2092 | |
2093 | the_ttybuff.sg_flags = original_tty_flags; |
2094 | ioctl (tty, TIOCSETN, &the_ttybuff); |
2095 | readline_echoing_p = 1; |
2096 | } |
2097 | |
2098 | #else /* !defined (NEW_TTY_DRIVER) */ |
2099 | static struct termio otio; |
2100 | |
2101 | static |
2102 | rl_prep_terminal () |
2103 | { |
2104 | int tty = fileno (rl_instream); |
2105 | struct termio tio; |
2106 | |
2107 | ioctl (tty, TCGETA, &tio); |
2108 | ioctl (tty, TCGETA, &otio); |
2109 | |
2110 | readline_echoing_p = (tio.c_lflag & ECHO); |
2111 | |
2112 | tio.c_lflag &= ~(ICANON|ECHO); |
2113 | tio.c_iflag &= ~(IXON|ISTRIP|INPCK); |
2114 | |
2115 | #ifndef HANDLE_SIGNALS |
2116 | tio.c_lflag &= ~ISIG; |
2117 | #endif |
2118 | |
2119 | tio.c_cc[VEOF] = 1; /* really: MIN */ |
2120 | tio.c_cc[VEOL] = 0; /* really: TIME */ |
2121 | ioctl (tty, TCSETAW,&tio); |
2122 | } |
2123 | |
2124 | static |
2125 | rl_deprep_terminal () |
2126 | { |
2127 | int tty = fileno (rl_instream); |
2128 | ioctl (tty, TCSETAW, &otio); |
2129 | } |
2130 | #endif /* NEW_TTY_DRIVER */ |
2131 | |
2132 | \f |
2133 | /* **************************************************************** */ |
2134 | /* */ |
2135 | /* Utility Functions */ |
2136 | /* */ |
2137 | /* **************************************************************** */ |
2138 | |
2139 | /* Return 0 if C is not a member of the class of characters that belong |
2140 | in words, or 1 if it is. */ |
2141 | |
2142 | int allow_pathname_alphabetic_chars = 0; |
2143 | char *pathname_alphabetic_chars = "/-_=~.#$"; |
2144 | |
2145 | int |
2146 | alphabetic (c) |
2147 | int c; |
2148 | { |
2149 | char *rindex (); |
2150 | if (pure_alphabetic (c) || (numeric (c))) |
2151 | return (1); |
2152 | |
2153 | if (allow_pathname_alphabetic_chars) |
2154 | return ((int)rindex (pathname_alphabetic_chars, c)); |
2155 | else |
2156 | return (0); |
2157 | } |
2158 | |
2159 | /* Return non-zero if C is a numeric character. */ |
2160 | int |
2161 | numeric (c) |
2162 | int c; |
2163 | { |
2164 | return (c >= '0' && c <= '9'); |
2165 | } |
2166 | |
2167 | /* Ring the terminal bell. */ |
2168 | int |
2169 | ding () |
2170 | { |
2171 | if (readline_echoing_p) |
2172 | { |
2173 | fprintf (stderr, "\007"); |
2174 | fflush (stderr); |
2175 | } |
2176 | return (-1); |
2177 | } |
2178 | |
2179 | /* How to abort things. */ |
2180 | rl_abort () |
2181 | { |
2182 | ding (); |
2183 | rl_clear_message (); |
2184 | rl_init_argument (); |
2185 | rl_pending_input = 0; |
2186 | |
2187 | defining_kbd_macro = 0; |
2188 | while (executing_macro) |
2189 | pop_executing_macro (); |
2190 | |
2191 | longjmp (readline_top_level, 1); |
2192 | } |
2193 | |
2194 | /* Return a copy of the string between FROM and TO. |
2195 | FROM is inclusive, TO is not. */ |
2196 | char * |
2197 | rl_copy (from, to) |
2198 | int from, to; |
2199 | { |
2200 | register int length; |
2201 | char *copy; |
2202 | |
2203 | /* Fix it if the caller is confused. */ |
2204 | if (from > to) { |
2205 | int t = from; |
2206 | from = to; |
2207 | to = t; |
2208 | } |
2209 | |
2210 | length = to - from; |
2211 | copy = (char *)xmalloc (1 + length); |
2212 | strncpy (copy, the_line + from, length); |
2213 | copy[length] = '\0'; |
2214 | return (copy); |
2215 | } |
2216 | |
2217 | \f |
2218 | /* **************************************************************** */ |
2219 | /* */ |
2220 | /* Insert and Delete */ |
2221 | /* */ |
2222 | /* **************************************************************** */ |
2223 | |
2224 | |
2225 | /* Insert a string of text into the line at point. This is the only |
2226 | way that you should do insertion. rl_insert () calls this |
2227 | function. */ |
2228 | rl_insert_text (string) |
2229 | char *string; |
2230 | { |
2231 | extern int doing_an_undo; |
2232 | register int i, l = strlen (string); |
2233 | while (rl_end + l >= rl_line_buffer_len) |
2234 | { |
2235 | rl_line_buffer = |
2236 | (char *)xrealloc (rl_line_buffer, |
2237 | rl_line_buffer_len += DEFAULT_BUFFER_SIZE); |
2238 | the_line = rl_line_buffer; |
2239 | } |
2240 | |
2241 | for (i = rl_end; i >= rl_point; i--) |
2242 | the_line[i + l] = the_line[i]; |
2243 | strncpy (the_line + rl_point, string, l); |
2244 | |
2245 | /* Remember how to undo this if we aren't undoing something. */ |
2246 | if (!doing_an_undo) |
2247 | { |
2248 | /* If possible and desirable, concatenate the undos. */ |
2249 | if ((strlen (string) == 1) && |
2250 | rl_undo_list && |
2251 | (rl_undo_list->what == UNDO_INSERT) && |
2252 | (rl_undo_list->end == rl_point) && |
2253 | (rl_undo_list->end - rl_undo_list->start < 20)) |
2254 | rl_undo_list->end++; |
2255 | else |
2256 | rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL); |
2257 | } |
2258 | rl_point += l; |
2259 | rl_end += l; |
2260 | the_line[rl_end] = '\0'; |
2261 | } |
2262 | |
2263 | /* Delete the string between FROM and TO. FROM is |
2264 | inclusive, TO is not. */ |
2265 | rl_delete_text (from, to) |
2266 | int from, to; |
2267 | { |
2268 | extern int doing_an_undo; |
2269 | register char *text; |
2270 | |
2271 | /* Fix it if the caller is confused. */ |
2272 | if (from > to) { |
2273 | int t = from; |
2274 | from = to; |
2275 | to = t; |
2276 | } |
2277 | text = rl_copy (from, to); |
2278 | strncpy (the_line + from, the_line + to, rl_end - to); |
2279 | |
2280 | /* Remember how to undo this delete. */ |
2281 | if (!doing_an_undo) |
2282 | rl_add_undo (UNDO_DELETE, from, to, text); |
2283 | else |
2284 | free (text); |
2285 | |
2286 | rl_end -= (to - from); |
2287 | the_line[rl_end] = '\0'; |
2288 | } |
2289 | |
2290 | \f |
2291 | /* **************************************************************** */ |
2292 | /* */ |
2293 | /* Readline character functions */ |
2294 | /* */ |
2295 | /* **************************************************************** */ |
2296 | |
2297 | /* This is not a gap editor, just a stupid line input routine. No hair |
2298 | is involved in writing any of the functions, and none should be. */ |
2299 | |
2300 | /* Note that: |
2301 | |
2302 | rl_end is the place in the string that we would place '\0'; |
2303 | i.e., it is always safe to place '\0' there. |
2304 | |
2305 | rl_point is the place in the string where the cursor is. Sometimes |
2306 | this is the same as rl_end. |
2307 | |
2308 | Any command that is called interactively receives two arguments. |
2309 | The first is a count: the numeric arg pased to this command. |
2310 | The second is the key which invoked this command. |
2311 | */ |
2312 | |
2313 | \f |
2314 | /* **************************************************************** */ |
2315 | /* */ |
2316 | /* Movement Commands */ |
2317 | /* */ |
2318 | /* **************************************************************** */ |
2319 | |
2320 | /* Note that if you `optimize' the display for these functions, you cannot |
2321 | use said functions in other functions which do not do optimizing display. |
2322 | I.e., you will have to update the data base for rl_redisplay, and you |
2323 | might as well let rl_redisplay do that job. */ |
2324 | |
2325 | /* Move forward COUNT characters. */ |
2326 | rl_forward (count) |
2327 | int count; |
2328 | { |
2329 | if (count < 0) |
2330 | rl_backward (-count); |
2331 | else |
2332 | while (count) |
2333 | { |
2334 | #ifdef VI_MODE |
2335 | if (rl_point == (rl_end - (rl_editing_mode == vi_mode))) |
2336 | #else |
2337 | if (rl_point == rl_end) |
2338 | #endif |
2339 | { |
2340 | ding (); |
2341 | return; |
2342 | } |
2343 | else |
2344 | rl_point++; |
2345 | --count; |
2346 | } |
2347 | } |
2348 | |
2349 | /* Move backward COUNT characters. */ |
2350 | rl_backward (count) |
2351 | int count; |
2352 | { |
2353 | if (count < 0) |
2354 | rl_forward (-count); |
2355 | else |
2356 | while (count) |
2357 | { |
2358 | if (!rl_point) |
2359 | { |
2360 | ding (); |
2361 | return; |
2362 | } |
2363 | else |
2364 | --rl_point; |
2365 | --count; |
2366 | } |
2367 | } |
2368 | |
2369 | /* Move to the beginning of the line. */ |
2370 | rl_beg_of_line () |
2371 | { |
2372 | rl_point = 0; |
2373 | } |
2374 | |
2375 | /* Move to the end of the line. */ |
2376 | rl_end_of_line () |
2377 | { |
2378 | rl_point = rl_end; |
2379 | } |
2380 | |
2381 | /* Move forward a word. We do what Emacs does. */ |
2382 | rl_forward_word (count) |
2383 | int count; |
2384 | { |
2385 | int c; |
2386 | |
2387 | if (count < 0) |
2388 | { |
2389 | rl_backward_word (-count); |
2390 | return; |
2391 | } |
2392 | |
2393 | while (count) |
2394 | { |
2395 | if (rl_point == rl_end) |
2396 | return; |
2397 | |
2398 | /* If we are not in a word, move forward until we are in one. |
2399 | Then, move forward until we hit a non-alphabetic character. */ |
2400 | c = the_line[rl_point]; |
2401 | if (!alphabetic (c)) |
2402 | { |
2403 | while (++rl_point < rl_end) |
2404 | { |
2405 | c = the_line[rl_point]; |
2406 | if (alphabetic (c)) break; |
2407 | } |
2408 | } |
2409 | if (rl_point == rl_end) return; |
2410 | while (++rl_point < rl_end) |
2411 | { |
2412 | c = the_line[rl_point]; |
2413 | if (!alphabetic (c)) break; |
2414 | } |
2415 | --count; |
2416 | } |
2417 | } |
2418 | |
2419 | /* Move backward a word. We do what Emacs does. */ |
2420 | rl_backward_word (count) |
2421 | int count; |
2422 | { |
2423 | int c; |
2424 | |
2425 | if (count < 0) |
2426 | { |
2427 | rl_forward_word (-count); |
2428 | return; |
2429 | } |
2430 | |
2431 | while (count) |
2432 | { |
2433 | if (!rl_point) |
2434 | return; |
2435 | |
2436 | /* Like rl_forward_word (), except that we look at the characters |
2437 | just before point. */ |
2438 | |
2439 | c = the_line[rl_point - 1]; |
2440 | if (!alphabetic (c)) |
2441 | { |
2442 | while (--rl_point) |
2443 | { |
2444 | c = the_line[rl_point - 1]; |
2445 | if (alphabetic (c)) break; |
2446 | } |
2447 | } |
2448 | |
2449 | while (rl_point) |
2450 | { |
2451 | c = the_line[rl_point - 1]; |
2452 | if (!alphabetic (c)) |
2453 | break; |
2454 | else --rl_point; |
2455 | } |
2456 | --count; |
2457 | } |
2458 | } |
2459 | |
2460 | /* Clear the current line. Numeric argument to C-l does this. */ |
2461 | rl_refresh_line () |
2462 | { |
2463 | int curr_line = last_c_pos / screenwidth; |
2464 | |
2465 | move_vert(curr_line); |
2466 | move_cursor_relative (0, the_line); /* XXX is this right */ |
2467 | rl_forced_update_display (); |
2468 | rl_display_fixed = 1; |
2469 | } |
2470 | |
2471 | /* C-l typed to a line without quoting clears the screen, and then reprints |
2472 | the prompt and the current input line. Given a numeric arg, redraw only |
2473 | the current line. */ |
2474 | rl_clear_screen () |
2475 | { |
2476 | extern char *term_clrpag; |
2477 | static void output_character_function (); |
2478 | |
2479 | if (rl_explicit_arg) |
2480 | { |
2481 | rl_refresh_line (); |
2482 | return; |
2483 | } |
2484 | |
2485 | if (term_clrpag) |
2486 | tputs (term_clrpag, 1, output_character_function); |
2487 | else |
2488 | crlf (); |
2489 | |
2490 | rl_forced_update_display (); |
2491 | rl_display_fixed = 1; |
2492 | } |
2493 | |
2494 | \f |
2495 | /* **************************************************************** */ |
2496 | /* */ |
2497 | /* Text commands */ |
2498 | /* */ |
2499 | /* **************************************************************** */ |
2500 | |
2501 | /* Insert the character C at the current location, moving point forward. */ |
2502 | rl_insert (count, c) |
2503 | int count, c; |
2504 | { |
2505 | register int i; |
2506 | char *string; |
2507 | |
2508 | if (count <= 0) |
2509 | return; |
2510 | |
2511 | /* If we can optimize, then do it. But don't let people crash |
2512 | readline because of extra large arguments. */ |
2513 | if (count > 1 && count < 1024) |
2514 | { |
2515 | string = (char *)alloca (1 + count); |
2516 | |
2517 | for (i = 0; i < count; i++) |
2518 | string[i] = c; |
2519 | |
2520 | string[i] = '\0'; |
2521 | rl_insert_text (string); |
2522 | return; |
2523 | } |
2524 | |
2525 | if (count > 1024) |
2526 | { |
2527 | int descreaser; |
2528 | |
2529 | string = (char *)alloca (1024 + 1); |
2530 | |
2531 | for (i = 0; i < 1024; i++) |
2532 | string[i] = c; |
2533 | |
2534 | while (count) |
2535 | { |
2536 | descreaser = (count > 1024 ? 1024 : count); |
2537 | string[descreaser] = '\0'; |
2538 | rl_insert_text (string); |
2539 | count -= descreaser; |
2540 | } |
2541 | return; |
2542 | } |
2543 | |
2544 | /* We are inserting a single character. |
2545 | If there is pending input, then make a string of all of the |
2546 | pending characters that are bound to rl_insert, and insert |
2547 | them all. */ |
2548 | if (any_typein) |
2549 | { |
2550 | int slen, key = 0, t; |
2551 | |
2552 | i = 0; |
2553 | string = (char *)alloca (ibuffer_len + 1); |
2554 | string[i++] = c; |
2555 | |
2556 | while ((t = rl_get_char (&key)) && |
2557 | (keymap[key].type == ISFUNC && |
2558 | keymap[key].function == rl_insert)) |
2559 | string[i++] = key; |
2560 | |
2561 | if (t) |
2562 | rl_unget_char (key); |
2563 | |
2564 | string[i] = '\0'; |
2565 | rl_insert_text (string); |
2566 | return; |
2567 | } |
2568 | else |
2569 | { |
2570 | /* Inserting a single character. */ |
2571 | string = (char *)alloca (2); |
2572 | |
2573 | string[1] = '\0'; |
2574 | string[0] = c; |
2575 | rl_insert_text (string); |
2576 | } |
2577 | } |
2578 | |
2579 | /* Insert the next typed character verbatim. */ |
2580 | rl_quoted_insert (count) |
2581 | int count; |
2582 | { |
2583 | int c = rl_read_key (in_stream); |
2584 | rl_insert (count, c); |
2585 | } |
2586 | |
2587 | /* Insert a tab character. */ |
2588 | rl_tab_insert (count) |
2589 | int count; |
2590 | { |
2591 | rl_insert (count, '\t'); |
2592 | } |
2593 | |
1c997a4a |
2594 | #ifdef VI_MODE |
2595 | /* Non-zero means enter insertion mode. */ |
2596 | static vi_doing_insert = 0; |
2597 | #endif |
2598 | |
4187119d |
2599 | /* What to do when a NEWLINE is pressed. We accept the whole line. |
2600 | KEY is the key that invoked this command. I guess it could have |
2601 | meaning in the future. */ |
2602 | rl_newline (count, key) |
2603 | int count, key; |
2604 | { |
2605 | |
2606 | rl_done = 1; |
2607 | |
2608 | #ifdef VI_MODE |
2609 | { |
4187119d |
2610 | if (vi_doing_insert) |
2611 | { |
2612 | rl_end_undo_group (); |
2613 | vi_doing_insert = 0; |
2614 | } |
2615 | } |
2616 | #endif /* VI_MODE */ |
2617 | |
2618 | if (readline_echoing_p) |
2619 | { |
2620 | move_vert (vis_botlin); |
2621 | vis_botlin = 0; |
2622 | crlf (); |
2623 | fflush (out_stream); |
2624 | rl_display_fixed++; |
2625 | } |
2626 | } |
2627 | |
2628 | rl_clean_up_for_exit () |
2629 | { |
2630 | if (readline_echoing_p) |
2631 | { |
2632 | move_vert (vis_botlin); |
2633 | vis_botlin = 0; |
2634 | fflush (out_stream); |
2635 | rl_restart_output (); |
2636 | } |
2637 | } |
2638 | |
2639 | /* What to do for some uppercase characters, like meta characters, |
2640 | and some characters appearing in emacs_ctlx_keymap. This function |
2641 | is just a stub, you bind keys to it and the code in rl_dispatch () |
2642 | is special cased. */ |
2643 | rl_do_lowercase_version (ignore1, ignore2) |
2644 | int ignore1, ignore2; |
2645 | { |
2646 | } |
2647 | |
2648 | /* Rubout the character behind point. */ |
2649 | rl_rubout (count) |
2650 | int count; |
2651 | { |
2652 | if (count < 0) |
2653 | { |
2654 | rl_delete (-count); |
2655 | return; |
2656 | } |
2657 | |
2658 | if (!rl_point) |
2659 | { |
2660 | ding (); |
2661 | return; |
2662 | } |
2663 | |
2664 | if (count > 1) |
2665 | { |
2666 | int orig_point = rl_point; |
2667 | rl_backward (count); |
2668 | rl_kill_text (orig_point, rl_point); |
2669 | } |
2670 | else |
2671 | { |
2672 | int c = the_line[--rl_point]; |
2673 | rl_delete_text (rl_point, rl_point + 1); |
2674 | |
2675 | if (rl_point == rl_end && alphabetic (c) && last_c_pos) |
2676 | { |
2677 | backspace (1); |
2678 | putc (' ', out_stream); |
2679 | backspace (1); |
2680 | last_c_pos--; |
2681 | rl_display_fixed++; |
2682 | } |
2683 | } |
2684 | } |
2685 | |
2686 | /* Delete the character under the cursor. Given a numeric argument, |
2687 | kill that many characters instead. */ |
2688 | rl_delete (count, invoking_key) |
2689 | int count; |
2690 | { |
2691 | if (count < 0) |
2692 | { |
2693 | rl_rubout (-count); |
2694 | return; |
2695 | } |
2696 | |
2697 | if (rl_point == rl_end) |
2698 | { |
2699 | ding (); |
2700 | return; |
2701 | } |
2702 | |
2703 | if (count > 1) |
2704 | { |
2705 | int orig_point = rl_point; |
2706 | rl_forward (count); |
2707 | rl_kill_text (orig_point, rl_point); |
2708 | rl_point = orig_point; |
2709 | } |
2710 | else |
2711 | rl_delete_text (rl_point, rl_point + 1); |
2712 | } |
2713 | |
2714 | \f |
2715 | /* **************************************************************** */ |
2716 | /* */ |
2717 | /* Kill commands */ |
2718 | /* */ |
2719 | /* **************************************************************** */ |
2720 | |
2721 | /* The next two functions mimic unix line editing behaviour, except they |
2722 | save the deleted text on the kill ring. This is safer than not saving |
2723 | it, and since we have a ring, nobody should get screwed. */ |
2724 | |
2725 | /* This does what C-w does in Unix. We can't prevent people from |
2726 | using behaviour that they expect. */ |
2727 | rl_unix_word_rubout () |
2728 | { |
2729 | if (!rl_point) ding (); |
2730 | else { |
2731 | int orig_point = rl_point; |
2732 | while (rl_point && whitespace (the_line[rl_point - 1])) |
2733 | rl_point--; |
2734 | while (rl_point && !whitespace (the_line[rl_point - 1])) |
2735 | rl_point--; |
2736 | rl_kill_text (rl_point, orig_point); |
2737 | } |
2738 | } |
2739 | |
2740 | /* Here is C-u doing what Unix does. You don't *have* to use these |
2741 | key-bindings. We have a choice of killing the entire line, or |
2742 | killing from where we are to the start of the line. We choose the |
2743 | latter, because if you are a Unix weenie, then you haven't backspaced |
2744 | into the line at all, and if you aren't, then you know what you are |
2745 | doing. */ |
2746 | rl_unix_line_discard () |
2747 | { |
2748 | if (!rl_point) ding (); |
2749 | else { |
2750 | rl_kill_text (rl_point, 0); |
2751 | rl_point = 0; |
2752 | } |
2753 | } |
2754 | |
2755 | \f |
2756 | |
2757 | /* **************************************************************** */ |
2758 | /* */ |
2759 | /* Commands For Typos */ |
2760 | /* */ |
2761 | /* **************************************************************** */ |
2762 | |
2763 | /* Random and interesting things in here. */ |
2764 | |
2765 | |
2766 | /* **************************************************************** */ |
2767 | /* */ |
2768 | /* Changing Case */ |
2769 | /* */ |
2770 | /* **************************************************************** */ |
2771 | |
2772 | /* The three kinds of things that we know how to do. */ |
2773 | #define UpCase 1 |
2774 | #define DownCase 2 |
2775 | #define CapCase 3 |
2776 | |
2777 | /* Uppercase the word at point. */ |
2778 | rl_upcase_word (count) |
2779 | int count; |
2780 | { |
2781 | rl_change_case (count, UpCase); |
2782 | } |
2783 | |
2784 | /* Lowercase the word at point. */ |
2785 | rl_downcase_word (count) |
2786 | int count; |
2787 | { |
2788 | rl_change_case (count, DownCase); |
2789 | } |
2790 | |
2791 | /* Upcase the first letter, downcase the rest. */ |
2792 | rl_capitalize_word (count) |
2793 | int count; |
2794 | { |
2795 | rl_change_case (count, CapCase); |
2796 | } |
2797 | |
2798 | /* The meaty function. |
2799 | Change the case of COUNT words, performing OP on them. |
2800 | OP is one of UpCase, DownCase, or CapCase. |
2801 | If a negative argument is given, leave point where it started, |
2802 | otherwise, leave it where it moves to. */ |
2803 | rl_change_case (count, op) |
2804 | int count, op; |
2805 | { |
2806 | register int start = rl_point, end; |
2807 | int state = 0; |
2808 | |
2809 | rl_forward_word (count); |
2810 | end = rl_point; |
2811 | |
2812 | if (count < 0) |
2813 | { |
2814 | int temp = start; |
2815 | start = end; |
2816 | end = temp; |
2817 | } |
2818 | |
2819 | /* We are going to modify some text, so let's prepare to undo it. */ |
2820 | rl_modifying (start, end); |
2821 | |
2822 | for (; start < end; start++) |
2823 | { |
2824 | switch (op) |
2825 | { |
2826 | case UpCase: |
2827 | the_line[start] = to_upper (the_line[start]); |
2828 | break; |
2829 | |
2830 | case DownCase: |
2831 | the_line[start] = to_lower (the_line[start]); |
2832 | break; |
2833 | |
2834 | case CapCase: |
2835 | if (state == 0) |
2836 | { |
2837 | the_line[start] = to_upper (the_line[start]); |
2838 | state = 1; |
2839 | } |
2840 | else |
2841 | { |
2842 | the_line[start] = to_lower (the_line[start]); |
2843 | } |
2844 | if (!pure_alphabetic (the_line[start])) |
2845 | state = 0; |
2846 | break; |
2847 | |
2848 | default: |
2849 | abort (); |
2850 | } |
2851 | } |
2852 | rl_point = end; |
2853 | } |
2854 | |
2855 | /* **************************************************************** */ |
2856 | /* */ |
2857 | /* Transposition */ |
2858 | /* */ |
2859 | /* **************************************************************** */ |
2860 | |
2861 | /* Transpose the words at point. */ |
2862 | rl_transpose_words (count) |
2863 | int count; |
2864 | { |
2865 | char *word1, *word2; |
2866 | int w1_beg, w1_end, w2_beg, w2_end; |
2867 | int orig_point = rl_point; |
2868 | |
2869 | if (!count) return; |
2870 | |
2871 | /* Find the two words. */ |
2872 | rl_forward_word (count); |
2873 | w2_end = rl_point; |
2874 | rl_backward_word (1); |
2875 | w2_beg = rl_point; |
2876 | rl_backward_word (count); |
2877 | w1_beg = rl_point; |
2878 | rl_forward_word (1); |
2879 | w1_end = rl_point; |
2880 | |
2881 | /* Do some check to make sure that there really are two words. */ |
2882 | if ((w1_beg == w2_beg) || (w2_beg < w1_end)) |
2883 | { |
2884 | ding (); |
2885 | rl_point = orig_point; |
2886 | return; |
2887 | } |
2888 | |
2889 | /* Get the text of the words. */ |
2890 | word1 = rl_copy (w1_beg, w1_end); |
2891 | word2 = rl_copy (w2_beg, w2_end); |
2892 | |
2893 | /* We are about to do many insertions and deletions. Remember them |
2894 | as one operation. */ |
2895 | rl_begin_undo_group (); |
2896 | |
2897 | /* Do the stuff at word2 first, so that we don't have to worry |
2898 | about word1 moving. */ |
2899 | rl_point = w2_beg; |
2900 | rl_delete_text (w2_beg, w2_end); |
2901 | rl_insert_text (word1); |
2902 | |
2903 | rl_point = w1_beg; |
2904 | rl_delete_text (w1_beg, w1_end); |
2905 | rl_insert_text (word2); |
2906 | |
2907 | /* This is exactly correct since the text before this point has not |
2908 | changed in length. */ |
2909 | rl_point = w2_end; |
2910 | |
2911 | /* I think that does it. */ |
2912 | rl_end_undo_group (); |
2913 | free (word1); free (word2); |
2914 | } |
2915 | |
2916 | /* Transpose the characters at point. If point is at the end of the line, |
2917 | then transpose the characters before point. */ |
2918 | rl_transpose_chars (count) |
2919 | int count; |
2920 | { |
2921 | if (!count) |
2922 | return; |
2923 | |
2924 | if (!rl_point || rl_end < 2) { |
2925 | ding (); |
2926 | return; |
2927 | } |
2928 | |
2929 | while (count) { |
2930 | if (rl_point == rl_end) { |
2931 | int t = the_line[rl_point - 1]; |
2932 | the_line[rl_point - 1] = the_line[rl_point - 2]; |
2933 | the_line[rl_point - 2] = t; |
2934 | } else { |
2935 | int t = the_line[rl_point]; |
2936 | the_line[rl_point] = the_line[rl_point - 1]; |
2937 | the_line[rl_point - 1] = t; |
2938 | if (count < 0 && rl_point) |
2939 | rl_point--; |
2940 | else |
2941 | rl_point++; |
2942 | } |
2943 | if (count < 0) |
2944 | count++; |
2945 | else |
2946 | count--; |
2947 | } |
2948 | } |
2949 | |
2950 | \f |
2951 | /* **************************************************************** */ |
2952 | /* */ |
2953 | /* Bogus Flow Control */ |
2954 | /* */ |
2955 | /* **************************************************************** */ |
2956 | |
2957 | rl_restart_output (count, key) |
2958 | int count, key; |
2959 | { |
2960 | int fildes = fileno (stdin); |
2961 | #ifdef TIOCSTART |
2962 | ioctl (fildes, TIOCSTART, 0); |
2963 | #endif /* TIOCSTART */ |
2964 | } |
2965 | |
2966 | /* **************************************************************** */ |
2967 | /* */ |
2968 | /* Completion matching, from readline's point of view. */ |
2969 | /* */ |
2970 | /* **************************************************************** */ |
2971 | |
2972 | /* Pointer to the generator function for completion_matches (). |
2973 | NULL means to use filename_entry_function (), the default filename |
2974 | completer. */ |
2975 | Function *rl_completion_entry_function = (Function *)NULL; |
2976 | |
2977 | /* Pointer to alternative function to create matches. |
2978 | Function is called with TEXT, START, and END. |
2979 | START and END are indices in RL_LINE_BUFFER saying what the boundaries |
2980 | of TEXT are. |
2981 | If this function exists and returns NULL then call the value of |
2982 | rl_completion_entry_function to try to match, otherwise use the |
2983 | array of strings returned. */ |
2984 | Function *rl_attempted_completion_function = (Function *)NULL; |
2985 | |
2986 | /* Complete the word at or before point. You have supplied the function |
2987 | that does the initial simple matching selection algorithm (see |
2988 | completion_matches ()). The default is to do filename completion. */ |
2989 | rl_complete (ignore, invoking_key) |
2990 | int ignore, invoking_key; |
2991 | { |
2992 | rl_complete_internal (TAB); |
2993 | if (running_in_emacs) |
2994 | printf ("%s", the_line); |
2995 | } |
2996 | |
2997 | /* List the possible completions. See description of rl_complete (). */ |
2998 | rl_possible_completions () |
2999 | { |
3000 | rl_complete_internal ('?'); |
3001 | } |
3002 | |
3003 | /* The user must press "y" or "n". Non-zero return means "y" pressed. */ |
3004 | get_y_or_n () |
3005 | { |
3006 | int c; |
3007 | loop: |
3008 | c = rl_read_key (in_stream); |
3009 | if (c == 'y' || c == 'Y') return (1); |
3010 | if (c == 'n' || c == 'N') return (0); |
3011 | if (c == ABORT_CHAR) rl_abort (); |
3012 | ding (); goto loop; |
3013 | } |
3014 | |
3015 | /* Up to this many items will be displayed in response to a |
3016 | possible-completions call. After that, we ask the user if |
3017 | she is sure she wants to see them all. */ |
3018 | int rl_completion_query_items = 100; |
3019 | |
3020 | /* The basic list of characters that signal a break between words for the |
3021 | completer routine. The contents of this variable is what breaks words |
3022 | in the shell, i.e. " \t\n\"\\'`@$><=" */ |
3023 | char *rl_basic_word_break_characters = " \t\n\"\\'`@$><="; |
3024 | |
3025 | /* The list of characters that signal a break between words for |
3026 | rl_complete_internal. The default list is the contents of |
3027 | rl_basic_word_break_characters. */ |
3028 | char *rl_completer_word_break_characters = (char *)NULL; |
3029 | |
3030 | /* List of characters that are word break characters, but should be left |
3031 | in TEXT when it is passed to the completion function. The shell uses |
3032 | this to help determine what kind of completing to do. */ |
3033 | char *rl_special_prefixes = (char *)NULL; |
3034 | |
3035 | /* If non-zero, then disallow duplicates in the matches. */ |
3036 | int rl_ignore_completion_duplicates = 1; |
3037 | |
3038 | /* Non-zero means that the results of the matches are to be treated |
3039 | as filenames. This is ALWAYS zero on entry, and can only be changed |
3040 | within a completion entry finder function. */ |
3041 | int rl_filename_completion_desired = 0; |
3042 | |
3043 | /* Complete the word at or before point. |
3044 | WHAT_TO_DO says what to do with the completion. |
3045 | `?' means list the possible completions. |
3046 | TAB means do standard completion. |
3047 | `*' means insert all of the possible completions. */ |
3048 | rl_complete_internal (what_to_do) |
3049 | int what_to_do; |
3050 | { |
3051 | char *filename_completion_function (); |
3052 | char **completion_matches (), **matches; |
3053 | Function *our_func; |
3054 | int start, end, delimiter = 0; |
3055 | char *text; |
3056 | |
3057 | if (rl_completion_entry_function) |
3058 | our_func = rl_completion_entry_function; |
3059 | else |
3060 | our_func = (int (*)())filename_completion_function; |
3061 | |
3062 | /* Only the completion entry function can change this. */ |
3063 | rl_filename_completion_desired = 0; |
3064 | |
3065 | /* We now look backwards for the start of a filename/variable word. */ |
3066 | end = rl_point; |
3067 | if (rl_point) |
3068 | { |
3069 | while (--rl_point && |
3070 | !rindex (rl_completer_word_break_characters, the_line[rl_point])); |
3071 | |
3072 | /* If we are at a word break, then advance past it. */ |
3073 | if (rindex (rl_completer_word_break_characters, (the_line[rl_point]))) |
3074 | { |
3075 | /* If the character that caused the word break was a quoting |
3076 | character, then remember it as the delimiter. */ |
3077 | if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1) |
3078 | delimiter = the_line[rl_point]; |
3079 | |
3080 | /* If the character isn't needed to determine something special |
3081 | about what kind of completion to perform, then advance past it. */ |
3082 | |
3083 | if (!rl_special_prefixes || |
3084 | !rindex (rl_special_prefixes, the_line[rl_point])) |
3085 | rl_point++; |
3086 | } |
3087 | } |
3088 | |
3089 | start = rl_point; |
3090 | rl_point = end; |
3091 | text = rl_copy (start, end); |
3092 | |
3093 | /* If the user wants to TRY to complete, but then wants to give |
3094 | up and use the default completion function, they set the |
3095 | variable rl_attempted_completion_function. */ |
3096 | if (rl_attempted_completion_function) |
3097 | { |
3098 | matches = |
3099 | (char **)(*rl_attempted_completion_function) (text, start, end); |
3100 | |
3101 | if (matches) |
3102 | goto after_usual_completion; |
3103 | } |
3104 | |
3105 | matches = completion_matches (text, our_func, start, end); |
3106 | |
3107 | after_usual_completion: |
3108 | free (text); |
3109 | |
3110 | if (!matches) |
3111 | ding (); |
3112 | else |
3113 | { |
3114 | register int i; |
3115 | |
3116 | some_matches: |
3117 | |
3118 | /* It seems to me that in all the cases we handle we would like |
3119 | to ignore duplicate possiblilities. Scan for the text to |
3120 | insert being identical to the other completions. */ |
3121 | if (rl_ignore_completion_duplicates) |
3122 | { |
3123 | char *lowest_common; |
1c997a4a |
3124 | int j, newlen = 0; |
4187119d |
3125 | |
3126 | /* Sort the items. */ |
3127 | /* It is safe to sort this array, because the lowest common |
3128 | denominator found in matches[0] will remain in place. */ |
3129 | for (i = 0; matches[i]; i++); |
3130 | qsort (matches, i, sizeof (char *), compare_strings); |
3131 | |
3132 | /* Remember the lowest common denimator for it may be unique. */ |
3133 | lowest_common = savestring (matches[0]); |
3134 | |
3135 | for (i = 0; matches[i + 1]; i++) |
3136 | { |
3137 | if (strcmp (matches[i], matches[i + 1]) == 0) |
3138 | { |
3139 | free (matches[i]); |
3140 | matches[i] = (char *)-1; |
3141 | } |
3142 | else |
3143 | newlen++; |
3144 | } |
3145 | |
3146 | /* We have marked all the dead slots with (char *)-1. |
3147 | Copy all the non-dead entries into a new array. */ |
3148 | { |
3149 | char **temp_array = |
3150 | (char **)malloc ((3 + newlen) * sizeof (char *)); |
3151 | |
3152 | for (i = 1, j = 1; matches[i]; i++) |
3153 | if (matches[i] != (char *)-1) |
3154 | temp_array[j++] = matches[i]; |
3155 | temp_array[j] = (char *)NULL; |
3156 | |
3157 | if (matches[0] != (char *)-1) |
3158 | free (matches[0]); |
3159 | free (matches); |
3160 | |
3161 | matches = temp_array; |
3162 | } |
3163 | |
3164 | /* Place the lowest common denominator back in [0]. */ |
3165 | matches[0] = lowest_common; |
3166 | |
3167 | /* If there is one string left, and it is identical to the |
3168 | lowest common denominator, then the LCD is the string to |
3169 | insert. */ |
3170 | if (j == 2 && strcmp (matches[0], matches[1]) == 0) |
3171 | { |
3172 | free (matches[1]); |
3173 | matches[1] = (char *)NULL; |
3174 | } |
3175 | } |
3176 | |
3177 | switch (what_to_do) |
3178 | { |
3179 | case TAB: |
3180 | rl_delete_text (start, rl_point); |
3181 | rl_point = start; |
3182 | rl_insert_text (matches[0]); |
3183 | |
3184 | /* If there are more matches, ring the bell to indicate. |
3185 | If this was the only match, and we are hacking files, |
3186 | check the file to see if it was a directory. If so, |
3187 | add a '/' to the name. If not, and we are at the end |
3188 | of the line, then add a space. */ |
3189 | if (matches[1]) |
3190 | { |
3191 | ding (); /* There are other matches remaining. */ |
3192 | } |
3193 | else |
3194 | { |
3195 | char temp_string[2]; |
3196 | |
3197 | temp_string[0] = delimiter ? delimiter : ' '; |
3198 | temp_string[1] = '\0'; |
3199 | |
3200 | if (rl_filename_completion_desired) |
3201 | { |
3202 | struct stat finfo; |
3203 | char *tilde_expand (); |
3204 | char *filename = tilde_expand (matches[0]); |
3205 | |
3206 | if ((stat (filename, &finfo) == 0) && |
3207 | ((finfo.st_mode & S_IFMT) == S_IFDIR)) |
3208 | { |
3209 | if (the_line[rl_point] != '/') |
3210 | rl_insert_text ("/"); |
3211 | } |
3212 | else |
3213 | { |
3214 | if (rl_point == rl_end) |
3215 | rl_insert_text (temp_string); |
3216 | } |
3217 | free (filename); |
3218 | } |
3219 | else |
3220 | { |
3221 | if (rl_point == rl_end) |
3222 | rl_insert_text (temp_string); |
3223 | } |
3224 | } |
3225 | break; |
3226 | |
3227 | case '*': |
3228 | { |
3229 | int i = 1; |
3230 | |
3231 | rl_delete_text (start, rl_point); |
3232 | rl_point = start; |
3233 | rl_begin_undo_group (); |
3234 | if (matches[1]) |
3235 | { |
3236 | while (matches[i]) |
3237 | { |
3238 | rl_insert_text (matches[i++]); |
3239 | rl_insert_text (" "); |
3240 | } |
3241 | } |
3242 | else |
3243 | { |
3244 | rl_insert_text (matches[0]); |
3245 | rl_insert_text (" "); |
3246 | } |
3247 | rl_end_undo_group (); |
3248 | } |
3249 | break; |
3250 | |
3251 | |
3252 | case '?': |
3253 | { |
3254 | int len, count, limit, max = 0; |
3255 | int j, k, l; |
3256 | |
3257 | /* Handle simple case first. What if there is only one answer? */ |
3258 | if (!matches[1]) |
3259 | { |
3260 | char *rindex (), *temp; |
3261 | |
3262 | if (rl_filename_completion_desired) |
3263 | temp = rindex (matches[0], '/'); |
3264 | else |
3265 | temp = (char *)NULL; |
3266 | |
3267 | if (!temp) |
3268 | temp = matches[0]; |
3269 | else |
3270 | temp++; |
3271 | |
3272 | crlf (); |
3273 | fprintf (out_stream, "%s", temp); |
3274 | crlf (); |
3275 | goto restart; |
3276 | } |
3277 | |
3278 | /* There is more than one answer. Find out how many there are, |
3279 | and find out what the maximum printed length of a single entry |
3280 | is. */ |
3281 | for (i = 1; matches[i]; i++) |
3282 | { |
3283 | char *rindex (), *temp = (char *)NULL; |
3284 | |
3285 | /* If we are hacking filenames, then only count the characters |
3286 | after the last slash in the pathname. */ |
3287 | if (rl_filename_completion_desired) |
3288 | temp = rindex (matches[i], '/'); |
3289 | else |
3290 | temp = (char *)NULL; |
3291 | |
3292 | if (!temp) |
3293 | temp = matches[i]; |
3294 | else |
3295 | temp++; |
3296 | |
3297 | if (strlen (temp) > max) |
3298 | max = strlen (temp); |
3299 | } |
3300 | |
3301 | len = i; |
3302 | |
3303 | /* If there are many items, then ask the user if she |
3304 | really wants to see them all. */ |
3305 | if (len >= rl_completion_query_items) |
3306 | { |
3307 | crlf (); |
3308 | fprintf (out_stream, |
3309 | "There are %d possibilities. Do you really", len); |
3310 | crlf (); |
3311 | fprintf (out_stream, "wish to see them all? (y or n)"); |
3312 | fflush (out_stream); |
3313 | if (!get_y_or_n ()) |
3314 | { |
3315 | crlf (); |
3316 | goto restart; |
3317 | } |
3318 | } |
3319 | /* How many items of MAX length can we fit in the screen window? */ |
3320 | max += 2; |
3321 | limit = screenwidth / max; |
3322 | if (limit != 1 && (limit * max == screenwidth)) |
3323 | limit--; |
3324 | |
3325 | /* How many iterations of the printing loop? */ |
3326 | count = (len + (limit - 1)) / limit; |
3327 | |
3328 | /* Watch out for special case. If LEN is less than LIMIT, then |
3329 | just do the inner printing loop. */ |
3330 | if (len < limit) count = 1; |
3331 | |
3332 | /* Sort the items if they are not already sorted. */ |
3333 | if (!rl_ignore_completion_duplicates) |
3334 | { |
4187119d |
3335 | qsort (matches, len, sizeof (char *), compare_strings); |
3336 | } |
3337 | |
3338 | /* Print the sorted items, up-and-down alphabetically, like |
3339 | ls might. */ |
3340 | crlf (); |
3341 | |
3342 | for (i = 1; i < count + 1; i++) |
3343 | { |
3344 | for (j = 0, l = i; j < limit; j++) |
3345 | { |
3346 | if (l > len || !matches[l]) |
3347 | { |
3348 | break; |
3349 | } |
3350 | else |
3351 | { |
3352 | char *rindex (), *temp = (char *)NULL; |
3353 | |
3354 | if (rl_filename_completion_desired) |
3355 | temp = rindex (matches[l], '/'); |
3356 | else |
3357 | temp = (char *)NULL; |
3358 | |
3359 | if (!temp) |
3360 | temp = matches[l]; |
3361 | else |
3362 | temp++; |
3363 | |
3364 | fprintf (out_stream, "%s", temp); |
3365 | for (k = 0; k < max - strlen (temp); k++) |
3366 | putc (' ', out_stream); |
3367 | } |
3368 | l += count; |
3369 | } |
3370 | crlf (); |
3371 | } |
3372 | restart: |
3373 | |
3374 | rl_on_new_line (); |
3375 | } |
3376 | break; |
3377 | |
3378 | default: |
3379 | abort (); |
3380 | } |
3381 | |
3382 | for (i = 0; matches[i]; i++) |
3383 | free (matches[i]); |
3384 | free (matches); |
3385 | } |
3386 | } |
3387 | |
4187119d |
3388 | /* A completion function for usernames. |
3389 | TEXT contains a partial username preceded by a random |
3390 | character (usually `~'). */ |
3391 | char * |
3392 | username_completion_function (text, state) |
3393 | int state; |
3394 | char *text; |
3395 | { |
3396 | static char *username = (char *)NULL; |
3397 | static struct passwd *entry; |
3398 | static int namelen; |
3399 | |
3400 | if (!state) |
3401 | { |
3402 | if (username) |
3403 | free (username); |
3404 | username = savestring (&text[1]); |
3405 | namelen = strlen (username); |
3406 | setpwent (); |
3407 | } |
3408 | |
3409 | while (entry = getpwent ()) |
3410 | { |
3411 | if (strncmp (username, entry->pw_name, namelen) == 0) |
3412 | break; |
3413 | } |
3414 | |
3415 | if (!entry) |
3416 | { |
3417 | endpwent (); |
3418 | return ((char *)NULL); |
3419 | } |
3420 | else |
3421 | { |
3422 | char *value = (char *)xmalloc (2 + strlen (entry->pw_name)); |
3423 | *value = *text; |
3424 | strcpy (value + 1, entry->pw_name); |
3425 | rl_filename_completion_desired = 1; |
3426 | return (value); |
3427 | } |
3428 | } |
3429 | |
3430 | /* If non-null, this contains the address of a function to call if the |
3431 | standard meaning for expanding a tilde fails. The function is called |
3432 | with the text (sans tilde, as in "foo"), and returns a malloc()'ed string |
3433 | which is the expansion, or a NULL pointer if there is no expansion. */ |
3434 | Function *rl_tilde_expander = (Function *)NULL; |
3435 | |
3436 | /* Expand FILENAME if it begins with a tilde. This always returns |
3437 | a new string. */ |
3438 | char * |
3439 | tilde_expand (filename) |
3440 | char *filename; |
3441 | { |
3442 | char *dirname = filename ? savestring (filename) : (char *)NULL; |
3443 | |
3444 | if (dirname && *dirname == '~') |
3445 | { |
3446 | char *temp_name; |
3447 | if (!dirname[1] || dirname[1] == '/') |
3448 | { |
3449 | /* Prepend $HOME to the rest of the string. */ |
3450 | char *temp_home = (char *)getenv ("HOME"); |
3451 | |
3452 | temp_name = (char *)alloca (1 + strlen (&dirname[1]) |
3453 | + (temp_home? strlen (temp_home) : 0)); |
3454 | temp_name[0] = '\0'; |
3455 | if (temp_home) |
3456 | strcpy (temp_name, temp_home); |
3457 | strcat (temp_name, &dirname[1]); |
3458 | free (dirname); |
3459 | dirname = savestring (temp_name); |
3460 | } |
3461 | else |
3462 | { |
3463 | struct passwd *getpwnam (), *user_entry; |
3464 | char *username = (char *)alloca (257); |
3465 | int i, c; |
3466 | |
3467 | for (i = 1; c = dirname[i]; i++) |
3468 | { |
3469 | if (c == '/') break; |
3470 | else username[i - 1] = c; |
3471 | } |
3472 | username[i - 1] = '\0'; |
3473 | |
3474 | if (!(user_entry = getpwnam (username))) |
3475 | { |
3476 | /* If the calling program has a special syntax for |
3477 | expanding tildes, and we couldn't find a standard |
3478 | expansion, then let them try. */ |
3479 | if (rl_tilde_expander) |
3480 | { |
3481 | char *expansion; |
3482 | |
3483 | expansion = (char *)(*rl_tilde_expander) (username); |
3484 | |
3485 | if (expansion) |
3486 | { |
3487 | temp_name = (char *)alloca (1 + strlen (expansion) |
3488 | + strlen (&dirname[i])); |
3489 | strcpy (temp_name, expansion); |
3490 | strcat (temp_name, &dirname[i]); |
3491 | free (expansion); |
3492 | goto return_name; |
3493 | } |
3494 | } |
3495 | /* |
3496 | * We shouldn't report errors. |
3497 | */ |
3498 | } |
3499 | else |
3500 | { |
3501 | temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir) |
3502 | + strlen (&dirname[i])); |
3503 | strcpy (temp_name, user_entry->pw_dir); |
3504 | strcat (temp_name, &dirname[i]); |
3505 | return_name: |
3506 | free (dirname); |
3507 | dirname = savestring (temp_name); |
3508 | } |
3509 | } |
3510 | } |
3511 | return (dirname); |
3512 | } |
3513 | |
3514 | \f |
3515 | /* **************************************************************** */ |
3516 | /* */ |
3517 | /* Undo, and Undoing */ |
3518 | /* */ |
3519 | /* **************************************************************** */ |
3520 | |
3521 | /* Non-zero tells rl_delete_text and rl_insert_text to not add to |
3522 | the undo list. */ |
3523 | int doing_an_undo = 0; |
3524 | |
3525 | /* The current undo list for THE_LINE. */ |
3526 | UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL; |
3527 | |
3528 | /* Remember how to undo something. Concatenate some undos if that |
3529 | seems right. */ |
3530 | rl_add_undo (what, start, end, text) |
3531 | enum undo_code what; |
3532 | int start, end; |
3533 | char *text; |
3534 | { |
3535 | UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST)); |
3536 | temp->what = what; |
3537 | temp->start = start; |
3538 | temp->end = end; |
3539 | temp->text = text; |
3540 | temp->next = rl_undo_list; |
3541 | rl_undo_list = temp; |
3542 | } |
3543 | |
3544 | /* Free the existing undo list. */ |
3545 | free_undo_list () |
3546 | { |
3547 | while (rl_undo_list) { |
3548 | UNDO_LIST *release = rl_undo_list; |
3549 | rl_undo_list = rl_undo_list->next; |
3550 | |
3551 | if (release->what == UNDO_DELETE) |
3552 | free (release->text); |
3553 | |
3554 | free (release); |
3555 | } |
3556 | } |
3557 | |
3558 | /* Undo the next thing in the list. Return 0 if there |
3559 | is nothing to undo, or non-zero if there was. */ |
3560 | int |
3561 | rl_do_undo () |
3562 | { |
3563 | UNDO_LIST *release; |
3564 | int waiting_for_begin = 0; |
3565 | |
3566 | undo_thing: |
3567 | if (!rl_undo_list) |
3568 | return (0); |
3569 | |
3570 | doing_an_undo = 1; |
3571 | |
3572 | switch (rl_undo_list->what) { |
3573 | |
3574 | /* Undoing deletes means inserting some text. */ |
3575 | case UNDO_DELETE: |
3576 | rl_point = rl_undo_list->start; |
3577 | rl_insert_text (rl_undo_list->text); |
3578 | free (rl_undo_list->text); |
3579 | break; |
3580 | |
3581 | /* Undoing inserts means deleting some text. */ |
3582 | case UNDO_INSERT: |
3583 | rl_delete_text (rl_undo_list->start, rl_undo_list->end); |
3584 | rl_point = rl_undo_list->start; |
3585 | break; |
3586 | |
3587 | /* Undoing an END means undoing everything 'til we get to |
3588 | a BEGIN. */ |
3589 | case UNDO_END: |
3590 | waiting_for_begin++; |
3591 | break; |
3592 | |
3593 | /* Undoing a BEGIN means that we are done with this group. */ |
3594 | case UNDO_BEGIN: |
3595 | if (waiting_for_begin) |
3596 | waiting_for_begin--; |
3597 | else |
3598 | abort (); |
3599 | break; |
3600 | } |
3601 | |
3602 | doing_an_undo = 0; |
3603 | |
3604 | release = rl_undo_list; |
3605 | rl_undo_list = rl_undo_list->next; |
3606 | free (release); |
3607 | |
3608 | if (waiting_for_begin) |
3609 | goto undo_thing; |
3610 | |
3611 | return (1); |
3612 | } |
3613 | |
3614 | /* Begin a group. Subsequent undos are undone as an atomic operation. */ |
3615 | rl_begin_undo_group () |
3616 | { |
3617 | rl_add_undo (UNDO_BEGIN, 0, 0, 0); |
3618 | } |
3619 | |
3620 | /* End an undo group started with rl_begin_undo_group (). */ |
3621 | rl_end_undo_group () |
3622 | { |
3623 | rl_add_undo (UNDO_END, 0, 0, 0); |
3624 | } |
3625 | |
3626 | /* Save an undo entry for the text from START to END. */ |
3627 | rl_modifying (start, end) |
3628 | int start, end; |
3629 | { |
3630 | if (start > end) |
3631 | { |
3632 | int t = start; |
3633 | start = end; |
3634 | end = t; |
3635 | } |
3636 | |
3637 | if (start != end) |
3638 | { |
3639 | char *temp = rl_copy (start, end); |
3640 | rl_begin_undo_group (); |
3641 | rl_add_undo (UNDO_DELETE, start, end, temp); |
3642 | rl_add_undo (UNDO_INSERT, start, end, (char *)NULL); |
3643 | rl_end_undo_group (); |
3644 | } |
3645 | } |
3646 | |
3647 | /* Revert the current line to its previous state. */ |
3648 | rl_revert_line () |
3649 | { |
3650 | if (!rl_undo_list) ding (); |
3651 | else { |
3652 | while (rl_undo_list) |
3653 | rl_do_undo (); |
3654 | } |
3655 | } |
3656 | |
3657 | /* Do some undoing of things that were done. */ |
3658 | rl_undo_command (count) |
3659 | { |
3660 | if (count < 0) return; /* Nothing to do. */ |
3661 | |
3662 | while (count) |
3663 | { |
3664 | if (rl_do_undo ()) |
3665 | { |
3666 | count--; |
3667 | } |
3668 | else |
3669 | { |
3670 | ding (); |
3671 | break; |
3672 | } |
3673 | } |
3674 | } |
3675 | \f |
3676 | /* **************************************************************** */ |
3677 | /* */ |
3678 | /* History Utilities */ |
3679 | /* */ |
3680 | /* **************************************************************** */ |
3681 | |
3682 | /* We already have a history library, and that is what we use to control |
3683 | the history features of readline. However, this is our local interface |
3684 | to the history mechanism. */ |
3685 | |
3686 | /* While we are editing the history, this is the saved |
3687 | version of the original line. */ |
3688 | HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL; |
3689 | |
3690 | /* Set the history pointer back to the last entry in the history. */ |
3691 | start_using_history () |
3692 | { |
3693 | using_history (); |
3694 | if (saved_line_for_history) |
3695 | free_history_entry (saved_line_for_history); |
3696 | |
3697 | saved_line_for_history = (HIST_ENTRY *)NULL; |
3698 | } |
3699 | |
3700 | /* Free the contents (and containing structure) of a HIST_ENTRY. */ |
3701 | free_history_entry (entry) |
3702 | HIST_ENTRY *entry; |
3703 | { |
3704 | if (!entry) return; |
3705 | if (entry->line) |
3706 | free (entry->line); |
3707 | free (entry); |
3708 | } |
3709 | |
3710 | /* Perhaps put back the current line if it has changed. */ |
3711 | maybe_replace_line () |
3712 | { |
3713 | HIST_ENTRY *temp = current_history (); |
3714 | |
3715 | /* If the current line has changed, save the changes. */ |
3716 | if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list)) { |
3717 | temp = replace_history_entry (where_history (), the_line, rl_undo_list); |
3718 | free (temp->line); |
3719 | free (temp); |
3720 | } |
3721 | } |
3722 | |
3723 | /* Put back the saved_line_for_history if there is one. */ |
3724 | maybe_unsave_line () |
3725 | { |
3726 | if (saved_line_for_history) { |
3727 | strcpy (the_line, saved_line_for_history->line); |
3728 | rl_undo_list = (UNDO_LIST *)saved_line_for_history->data; |
3729 | free_history_entry (saved_line_for_history); |
3730 | saved_line_for_history = (HIST_ENTRY *)NULL; |
3731 | rl_end = rl_point = strlen (the_line); |
3732 | } else { |
3733 | ding (); |
3734 | } |
3735 | } |
3736 | |
3737 | /* Save the current line in saved_line_for_history. */ |
3738 | maybe_save_line () |
3739 | { |
3740 | if (!saved_line_for_history) { |
3741 | saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY)); |
3742 | saved_line_for_history->line = savestring (the_line); |
3743 | saved_line_for_history->data = (char *)rl_undo_list; |
3744 | } |
3745 | } |
3746 | |
3747 | |
3748 | \f |
3749 | /* **************************************************************** */ |
3750 | /* */ |
3751 | /* History Commands */ |
3752 | /* */ |
3753 | /* **************************************************************** */ |
3754 | |
3755 | /* Meta-< goes to the start of the history. */ |
3756 | rl_beginning_of_history () |
3757 | { |
3758 | rl_get_previous_history (1 + where_history ()); |
3759 | } |
3760 | |
3761 | /* Meta-> goes to the end of the history. (The current line). */ |
3762 | rl_end_of_history () |
3763 | { |
3764 | maybe_replace_line (); |
3765 | using_history (); |
3766 | maybe_unsave_line (); |
3767 | } |
3768 | |
3769 | /* Move down to the next history line. */ |
3770 | rl_get_next_history (count) |
3771 | int count; |
3772 | { |
3773 | HIST_ENTRY *temp = (HIST_ENTRY *)NULL; |
3774 | |
3775 | if (count < 0) |
3776 | { |
3777 | rl_get_previous_history (-count); |
3778 | return; |
3779 | } |
3780 | |
3781 | if (!count) |
3782 | return; |
3783 | |
3784 | maybe_replace_line (); |
3785 | |
3786 | while (count) |
3787 | { |
3788 | temp = next_history (); |
3789 | if (!temp) |
3790 | break; |
3791 | --count; |
3792 | } |
3793 | |
3794 | if (!temp) |
3795 | maybe_unsave_line (); |
3796 | else |
3797 | { |
3798 | strcpy (the_line, temp->line); |
3799 | rl_undo_list = (UNDO_LIST *)temp->data; |
3800 | rl_end = rl_point = strlen (the_line); |
3801 | } |
3802 | } |
3803 | |
3804 | /* Get the previous item out of our interactive history, making it the current |
3805 | line. If there is no previous history, just ding. */ |
3806 | rl_get_previous_history (count) |
3807 | int count; |
3808 | { |
3809 | HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL; |
3810 | HIST_ENTRY *temp = (HIST_ENTRY *)NULL; |
3811 | |
3812 | if (count < 0) |
3813 | { |
3814 | rl_get_next_history (-count); |
3815 | return; |
3816 | } |
3817 | |
3818 | if (!count) |
3819 | return; |
3820 | |
3821 | /* If we don't have a line saved, then save this one. */ |
3822 | maybe_save_line (); |
3823 | |
3824 | /* If the current line has changed, save the changes. */ |
3825 | maybe_replace_line (); |
3826 | |
3827 | while (count) |
3828 | { |
3829 | temp = previous_history (); |
3830 | if (!temp) |
3831 | break; |
3832 | else |
3833 | old_temp = temp; |
3834 | --count; |
3835 | } |
3836 | |
3837 | /* If there was a large argument, and we moved back to the start of the |
3838 | history, that is not an error. So use the last value found. */ |
3839 | if (!temp && old_temp) |
3840 | temp = old_temp; |
3841 | |
3842 | if (!temp) |
3843 | ding (); |
3844 | else |
3845 | { |
3846 | strcpy (the_line, temp->line); |
3847 | rl_undo_list = (UNDO_LIST *)temp->data; |
3848 | rl_end = rl_point = strlen (the_line); |
3849 | #ifdef VI_MODE |
3850 | if (rl_editing_mode == vi_mode) |
3851 | rl_point = 0; |
3852 | #endif /* VI_MODE */ |
3853 | } |
3854 | } |
3855 | |
3856 | /* There is a command in ksh which yanks into this line, the last word |
3857 | of the previous line. Here it is. We left it on M-. */ |
3858 | rl_yank_previous_last_arg (ignore) |
3859 | int ignore; |
3860 | { |
3861 | } |
3862 | |
3863 | |
3864 | \f |
3865 | /* **************************************************************** */ |
3866 | /* */ |
3867 | /* I-Search and Searching */ |
3868 | /* */ |
3869 | /* **************************************************************** */ |
3870 | |
3871 | /* Search backwards through the history looking for a string which is typed |
3872 | interactively. Start with the current line. */ |
3873 | rl_reverse_search_history (sign, key) |
3874 | int sign; |
3875 | int key; |
3876 | { |
3877 | rl_search_history (-sign, key); |
3878 | } |
3879 | |
3880 | /* Search forwards through the history looking for a string which is typed |
3881 | interactively. Start with the current line. */ |
3882 | rl_forward_search_history (sign, key) |
3883 | int sign; |
3884 | int key; |
3885 | { |
3886 | rl_search_history (sign, key); |
3887 | } |
3888 | |
3889 | /* Display the current state of the search in the echo-area. |
3890 | SEARCH_STRING contains the string that is being searched for, |
3891 | DIRECTION is zero for forward, or 1 for reverse, |
3892 | WHERE is the history list number of the current line. If it is |
3893 | -1, then this line is the starting one. */ |
3894 | rl_display_search (search_string, reverse_p, where) |
3895 | char *search_string; |
3896 | int reverse_p, where; |
3897 | { |
3898 | char *message = (char *)NULL; |
3899 | |
3900 | message = |
3901 | (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30); |
3902 | |
3903 | *message = '\0'; |
3904 | |
3905 | #ifdef NEVER |
3906 | if (where != -1) |
3907 | sprintf (message, "[%d]", where + history_base); |
3908 | #endif |
3909 | |
3910 | strcat (message, "("); |
3911 | |
3912 | if (reverse_p) |
3913 | strcat (message, "reverse-"); |
3914 | |
3915 | strcat (message, "i-search)`"); |
3916 | |
3917 | if (search_string) |
3918 | strcat (message, search_string); |
3919 | |
3920 | strcat (message, "': "); |
3921 | rl_message (message, 0, 0); |
3922 | rl_redisplay (); |
3923 | } |
3924 | |
3925 | /* Search through the history looking for an interactively typed string. |
3926 | This is analogous to i-search. We start the search in the current line. |
3927 | DIRECTION is which direction to search; > 0 means forward, < 0 means |
3928 | backwards. */ |
3929 | rl_search_history (direction, invoking_key) |
3930 | int direction; |
3931 | int invoking_key; |
3932 | { |
3933 | /* The string that the user types in to search for. */ |
3934 | char *search_string = (char *)alloca (128); |
3935 | |
3936 | /* The current length of SEARCH_STRING. */ |
3937 | int search_string_index; |
3938 | |
3939 | /* The list of lines to search through. */ |
3940 | char **lines; |
3941 | |
3942 | /* The length of LINES. */ |
3943 | int hlen; |
3944 | |
3945 | /* Where we get LINES from. */ |
3946 | HIST_ENTRY **hlist = history_list (); |
3947 | |
3948 | int orig_point = rl_point; |
3949 | int orig_line = where_history (); |
3950 | int last_found_line = orig_line; |
3951 | int c, done = 0; |
3952 | register int i = 0; |
3953 | |
3954 | |
3955 | /* The line currently being searched. */ |
3956 | char *sline; |
3957 | |
3958 | /* Offset in that line. */ |
3959 | int index; |
3960 | |
3961 | /* Non-zero if we are doing a reverse search. */ |
3962 | int reverse = (direction < 0); |
3963 | |
3964 | /* Create an arrary of pointers to the lines that we want to search. */ |
3965 | |
3966 | maybe_replace_line (); |
3967 | if (hlist) |
3968 | for (i = 0; hlist[i]; i++); |
3969 | |
3970 | /* Allocate space for this many lines, +1 for the current input line, |
3971 | and remember those lines. */ |
3972 | lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *)); |
3973 | for (i = 0; i < hlen; i++) |
3974 | lines[i] = hlist[i]->line; |
3975 | |
3976 | if (saved_line_for_history) |
3977 | lines[i] = saved_line_for_history->line; |
3978 | else |
3979 | { |
3980 | /* So I have to type it in this way instead. */ |
3981 | lines[i] = (char *)alloca (1 + strlen (the_line)); |
3982 | strcpy (lines[i], &the_line[0]); |
3983 | } |
3984 | |
3985 | hlen++; |
3986 | |
3987 | /* The line where we start the search. */ |
3988 | i = orig_line; |
3989 | |
3990 | /* Initialize search parameters. */ |
3991 | *search_string = '\0'; |
3992 | search_string_index = 0; |
3993 | |
3994 | rl_display_search (search_string, reverse, -1); |
3995 | |
3996 | sline = the_line; |
3997 | index = rl_point; |
3998 | |
3999 | while (!done) |
4000 | { |
4001 | c = rl_read_key (in_stream); |
4002 | |
4003 | /* Hack C to Do What I Mean. */ |
4004 | { |
4005 | Function *f = (Function *)NULL; |
4006 | |
4007 | if (keymap[c].type == ISFUNC) |
4008 | f = keymap[c].function; |
4009 | |
4010 | if (f == rl_reverse_search_history) |
4011 | c = reverse ? -1 : -2; |
4012 | else if (f == rl_forward_search_history) |
4013 | c = !reverse ? -1 : -2; |
4014 | } |
4015 | |
4016 | switch (c) |
4017 | { |
4018 | case ESC: |
4019 | done = 1; |
4020 | continue; |
4021 | |
4022 | /* case invoking_key: */ |
4023 | case -1: |
4024 | goto search_again; |
4025 | |
4026 | /* switch directions */ |
4027 | case -2: |
4028 | direction = -direction; |
4029 | reverse = (direction < 0); |
4030 | |
4031 | goto do_search; |
4032 | |
4033 | case CTRL ('G'): |
4034 | strcpy (the_line, lines[orig_line]); |
4035 | rl_point = orig_point; |
4036 | rl_end = strlen (the_line); |
4037 | rl_clear_message (); |
4038 | return; |
4039 | |
4040 | default: |
4041 | if (c < 32 || c > 126) |
4042 | { |
4043 | rl_execute_next (c); |
4044 | done = 1; |
4045 | continue; |
4046 | } |
4047 | else |
4048 | { |
4049 | search_string[search_string_index++] = c; |
4050 | search_string[search_string_index] = '\0'; |
4051 | goto do_search; |
4052 | |
4053 | search_again: |
4054 | |
4055 | if (!search_string_index) |
4056 | continue; |
4057 | else |
4058 | { |
4059 | if (reverse) |
4060 | --index; |
4061 | else |
4062 | if (index != strlen (sline)) |
4063 | ++index; |
4064 | else |
4065 | ding (); |
4066 | } |
4067 | do_search: |
4068 | |
4069 | while (1) |
4070 | { |
4071 | if (reverse) |
4072 | { |
4073 | while (index >= 0) |
4074 | if (strncmp |
4075 | (search_string, |
4076 | sline + index, |
4077 | search_string_index) == 0) |
4078 | goto string_found; |
4079 | else |
4080 | index--; |
4081 | } |
4082 | else |
4083 | { |
4084 | register int limit = |
4085 | (strlen (sline) - search_string_index) + 1; |
4086 | |
4087 | while (index < limit) |
4088 | { |
4089 | if (strncmp (search_string, |
4090 | sline + index, |
4091 | search_string_index) == 0) |
4092 | goto string_found; |
4093 | index++; |
4094 | } |
4095 | } |
4096 | |
4097 | next_line: |
4098 | i += direction; |
4099 | |
4100 | /* At limit for direction? */ |
4101 | if ((reverse && i < 0) || |
4102 | (!reverse && i == hlen)) |
4103 | goto search_failed; |
4104 | |
4105 | sline = lines[i]; |
4106 | if (reverse) |
4107 | index = strlen (sline); |
4108 | else |
4109 | index = 0; |
4110 | |
4111 | /* If the search string is longer than the current |
4112 | line, no match. */ |
4113 | if (search_string_index > strlen (sline)) |
4114 | goto next_line; |
4115 | |
4116 | /* Start actually searching. */ |
4117 | if (reverse) |
4118 | index -= search_string_index; |
4119 | } |
4120 | |
4121 | search_failed: |
4122 | /* We cannot find the search string. Ding the bell. */ |
4123 | ding (); |
4124 | i = last_found_line; |
4125 | break; |
4126 | |
4127 | string_found: |
4128 | /* We have found the search string. Just display it. But don't |
4129 | actually move there in the history list until the user accepts |
4130 | the location. */ |
4131 | strcpy (the_line, lines[i]); |
4132 | rl_point = index; |
4133 | rl_end = strlen (the_line); |
4134 | last_found_line = i; |
4135 | rl_display_search (search_string, reverse, |
4136 | (i == orig_line) ? -1 : i); |
4137 | } |
4138 | } |
4139 | continue; |
4140 | } |
4141 | /* The user has won. They found the string that they wanted. Now all |
4142 | we have to do is place them there. */ |
4143 | { |
4144 | int now = last_found_line; |
4145 | |
4146 | /* First put back the original state. */ |
4147 | strcpy (the_line, lines[orig_line]); |
4148 | |
4149 | if (now < orig_line) |
4150 | rl_get_previous_history (orig_line - now); |
4151 | else |
4152 | rl_get_next_history (now - orig_line); |
4153 | |
4154 | rl_point = index; |
4155 | rl_clear_message (); |
4156 | } |
4157 | } |
4158 | |
4159 | /* Make C be the next command to be executed. */ |
4160 | rl_execute_next (c) |
4161 | int c; |
4162 | { |
4163 | rl_pending_input = c; |
4164 | } |
4165 | \f |
4166 | /* **************************************************************** */ |
4167 | /* */ |
4168 | /* Killing Mechanism */ |
4169 | /* */ |
4170 | /* **************************************************************** */ |
4171 | |
4172 | /* What we assume for a max number of kills. */ |
4173 | #define DEFAULT_MAX_KILLS 10 |
4174 | |
4175 | /* The real variable to look at to find out when to flush kills. */ |
4176 | int rl_max_kills = DEFAULT_MAX_KILLS; |
4177 | |
4178 | /* Where to store killed text. */ |
4179 | char **rl_kill_ring = (char **)NULL; |
4180 | |
4181 | /* Where we are in the kill ring. */ |
4182 | int rl_kill_index = 0; |
4183 | |
4184 | /* How many slots we have in the kill ring. */ |
4185 | int rl_kill_ring_length = 0; |
4186 | |
4187 | /* How to say that you only want to save a certain amount |
4188 | of kill material. */ |
4189 | rl_set_retained_kills (num) |
4190 | int num; |
4191 | {} |
4192 | |
4193 | /* The way to kill something. This appends or prepends to the last |
4194 | kill, if the last command was a kill command. if FROM is less |
4195 | than TO, then the text is appended, otherwise prepended. If the |
4196 | last command was not a kill command, then a new slot is made for |
4197 | this kill. */ |
4198 | rl_kill_text (from, to) |
4199 | int from, to; |
4200 | { |
4201 | int slot; |
4202 | char *text = rl_copy (from, to); |
4203 | |
4204 | /* Is there anything to kill? */ |
4205 | if (from == to) { |
4206 | free (text); |
4207 | last_command_was_kill++; |
4208 | return; |
4209 | } |
4210 | |
4211 | /* Delete the copied text from the line. */ |
4212 | rl_delete_text (from, to); |
4213 | |
4214 | /* First, find the slot to work with. */ |
4215 | if (!last_command_was_kill) { |
4216 | |
4217 | /* Get a new slot. */ |
4218 | if (!rl_kill_ring) { |
4219 | |
4220 | /* If we don't have any defined, then make one. */ |
4221 | rl_kill_ring = |
4222 | (char **)xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *)); |
4223 | slot = 1; |
4224 | |
4225 | } else { |
4226 | |
4227 | /* We have to add a new slot on the end, unless we have exceeded |
4228 | the max limit for remembering kills. */ |
4229 | slot = rl_kill_ring_length; |
4230 | if (slot == rl_max_kills) { |
4231 | register int i; |
4232 | free (rl_kill_ring[0]); |
4233 | for (i = 0; i < slot; i++) |
4234 | rl_kill_ring[i] = rl_kill_ring[i + 1]; |
4235 | } else { |
4236 | rl_kill_ring = |
4237 | (char **)xrealloc (rl_kill_ring, |
4238 | ((slot = (rl_kill_ring_length += 1)) + 1) |
4239 | * sizeof (char *)); |
4240 | } |
4241 | } |
4242 | slot--; |
4243 | } else { |
4244 | slot = rl_kill_ring_length - 1; |
4245 | } |
4246 | |
4247 | /* If the last command was a kill, prepend or append. */ |
4248 | if (last_command_was_kill) { |
4249 | char *old = rl_kill_ring[slot]; |
4250 | char *new = (char *)xmalloc (1 + strlen (old) + strlen (text)); |
4251 | |
4252 | if (from < to) { |
4253 | strcpy (new, old); |
4254 | strcat (new, text); |
4255 | } else { |
4256 | strcpy (new, text); |
4257 | strcat (new, old); |
4258 | } |
4259 | free (old); |
4260 | free (text); |
4261 | rl_kill_ring[slot] = new; |
4262 | } else { |
4263 | rl_kill_ring[slot] = text; |
4264 | } |
4265 | rl_kill_index = slot; |
4266 | last_command_was_kill++; |
4267 | } |
4268 | |
4269 | /* Now REMEMBER! In order to do prepending or appending correctly, kill |
4270 | commands always make rl_point's original position be the FROM argument, |
4271 | and rl_point's extent be the TO argument. */ |
4272 | |
4273 | |
4274 | /* **************************************************************** */ |
4275 | /* */ |
4276 | /* Killing Commands */ |
4277 | /* */ |
4278 | /* **************************************************************** */ |
4279 | |
4280 | /* Delete the word at point, saving the text in the kill ring. */ |
4281 | rl_kill_word (count) |
4282 | int count; |
4283 | { |
4284 | int orig_point = rl_point; |
4285 | |
4286 | if (count < 0) |
4287 | rl_backward_kill_word (-count); |
4288 | else |
4289 | { |
4290 | rl_forward_word (count); |
4291 | |
4292 | if (rl_point != orig_point) |
4293 | rl_kill_text (orig_point, rl_point); |
4294 | |
4295 | rl_point = orig_point; |
4296 | } |
4297 | } |
4298 | |
4299 | /* Rubout the word before point, placing it on the kill ring. */ |
4300 | rl_backward_kill_word (count) |
4301 | int count; |
4302 | { |
4303 | int orig_point = rl_point; |
4304 | |
4305 | if (count < 0) |
4306 | rl_kill_word (-count); |
4307 | else |
4308 | { |
4309 | rl_backward_word (count); |
4310 | |
4311 | if (rl_point != orig_point) |
4312 | rl_kill_text (orig_point, rl_point); |
4313 | } |
4314 | } |
4315 | |
4316 | /* Kill from here to the end of the line. If DIRECTION is negative, kill |
4317 | back to the line start instead. */ |
4318 | rl_kill_line (direction) |
4319 | int direction; |
4320 | { |
4321 | int orig_point = rl_point; |
4322 | |
4323 | if (direction < 0) |
4324 | rl_backward_kill_line (1); |
4325 | else |
4326 | { |
4327 | rl_end_of_line (); |
4328 | if (orig_point != rl_point) |
4329 | rl_kill_text (orig_point, rl_point); |
4330 | rl_point = orig_point; |
4331 | } |
4332 | } |
4333 | |
4334 | /* Kill backwards to the start of the line. If DIRECTION is negative, kill |
4335 | forwards to the line end instead. */ |
4336 | rl_backward_kill_line (direction) |
4337 | int direction; |
4338 | { |
4339 | int orig_point = rl_point; |
4340 | |
4341 | if (direction < 0) |
4342 | rl_kill_line (1); |
4343 | else |
4344 | { |
4345 | if (!rl_point) |
4346 | ding (); |
4347 | else |
4348 | { |
4349 | rl_beg_of_line (); |
4350 | rl_kill_text (orig_point, rl_point); |
4351 | } |
4352 | } |
4353 | } |
4354 | |
4355 | /* Yank back the last killed text. This ignores arguments. */ |
4356 | rl_yank () |
4357 | { |
4358 | if (!rl_kill_ring) rl_abort (); |
4359 | rl_insert_text (rl_kill_ring[rl_kill_index]); |
4360 | } |
4361 | |
4362 | /* If the last command was yank, or yank_pop, and the text just |
4363 | before point is identical to the current kill item, then |
4364 | delete that text from the line, rotate the index down, and |
4365 | yank back some other text. */ |
4366 | rl_yank_pop () |
4367 | { |
4368 | int l; |
4369 | |
4370 | if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) || |
4371 | !rl_kill_ring) |
4372 | { |
4373 | rl_abort (); |
4374 | } |
4375 | |
4376 | l = strlen (rl_kill_ring[rl_kill_index]); |
4377 | if (((rl_point - l) >= 0) && |
4378 | (strncmp (the_line + (rl_point - l), |
4379 | rl_kill_ring[rl_kill_index], l) == 0)) |
4380 | { |
4381 | rl_delete_text ((rl_point - l), rl_point); |
4382 | rl_point -= l; |
4383 | rl_kill_index--; |
4384 | if (rl_kill_index < 0) |
4385 | rl_kill_index = rl_kill_ring_length - 1; |
4386 | rl_yank (); |
4387 | } |
4388 | else |
4389 | rl_abort (); |
4390 | |
4391 | } |
4392 | |
4393 | /* Yank the COUNTth argument from the previous history line. */ |
4394 | rl_yank_nth_arg (count, ignore) |
4395 | int count; |
4396 | { |
4397 | register HIST_ENTRY *entry = previous_history (); |
4398 | char *arg; |
4399 | |
4400 | if (entry) |
4401 | next_history (); |
4402 | else |
4403 | { |
4404 | ding (); |
4405 | return; |
4406 | } |
4407 | |
4408 | arg = history_arg_extract (count, count, entry->line); |
4409 | if (!arg || !*arg) |
4410 | { |
4411 | ding (); |
4412 | return; |
4413 | } |
4414 | |
4415 | rl_begin_undo_group (); |
4416 | if (rl_point && the_line[rl_point - 1] != ' ') |
4417 | rl_insert_text (" "); |
4418 | rl_insert_text (arg); |
4419 | free (arg); |
4420 | rl_end_undo_group (); |
4421 | } |
4422 | |
4423 | /* Vi Mode. */ |
4424 | #ifdef VI_MODE |
4425 | #include "vi_mode.c" |
4426 | #endif /* VI_MODE */ |
4427 | |
4428 | /* How to toggle back and forth between editing modes. */ |
4429 | rl_vi_editing_mode () |
4430 | { |
4431 | #ifdef VI_MODE |
4432 | rl_editing_mode = vi_mode; |
4433 | rl_vi_insertion_mode (); |
4434 | #endif /* VI_MODE */ |
4435 | } |
4436 | |
4437 | rl_emacs_editing_mode () |
4438 | { |
4439 | rl_editing_mode = emacs_mode; |
4440 | keymap = emacs_standard_keymap; |
4441 | } |
4442 | |
4443 | \f |
4444 | /* **************************************************************** */ |
4445 | /* */ |
4446 | /* Completion */ |
4447 | /* */ |
4448 | /* **************************************************************** */ |
4449 | |
4450 | /* Non-zero means that case is not significant in completion. */ |
4451 | int completion_case_fold = 0; |
4452 | |
4453 | /* Return an array of (char *) which is a list of completions for TEXT. |
4454 | If there are no completions, return a NULL pointer. |
4455 | The first entry in the returned array is the substitution for TEXT. |
4456 | The remaining entries are the possible completions. |
4457 | The array is terminated with a NULL pointer. |
4458 | |
4459 | ENTRY_FUNCTION is a function of two args, and returns a (char *). |
4460 | The first argument is TEXT. |
4461 | The second is a state argument; it should be zero on the first call, and |
4462 | non-zero on subsequent calls. It returns a NULL pointer to the caller |
4463 | when there are no more matches. |
4464 | */ |
4465 | char ** |
4466 | completion_matches (text, entry_function) |
4467 | char *text; |
4468 | char *(*entry_function) (); |
4469 | { |
4470 | /* Number of slots in match_list. */ |
4471 | int match_list_size; |
4472 | |
4473 | /* The list of matches. */ |
4474 | char **match_list = |
4475 | (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *)); |
4476 | |
4477 | /* Number of matches actually found. */ |
4478 | int matches = 0; |
4479 | |
4480 | /* Temporary string binder. */ |
4481 | char *string; |
4482 | |
4483 | match_list[1] = (char *)NULL; |
4484 | |
4485 | while (string = (*entry_function) (text, matches)) |
4486 | { |
4487 | if (matches + 1 == match_list_size) |
4488 | match_list = |
4489 | (char **)xrealloc (match_list, |
4490 | ((match_list_size += 10) + 1) * sizeof (char *)); |
4491 | |
4492 | match_list[++matches] = string; |
4493 | match_list[matches + 1] = (char *)NULL; |
4494 | } |
4495 | |
4496 | /* If there were any matches, then look through them finding out the |
4497 | lowest common denominator. That then becomes match_list[0]. */ |
4498 | if (matches) |
4499 | { |
4500 | register int i = 1; |
4501 | int low = 100000; /* Count of max-matched characters. */ |
4502 | |
4503 | /* If only one match, just use that. */ |
4504 | if (matches == 1) |
4505 | { |
4506 | match_list[0] = match_list[1]; |
4507 | match_list[1] = (char *)NULL; |
4508 | } |
4509 | else |
4510 | { |
4511 | /* Otherwise, compare each member of the list with |
4512 | the next, finding out where they stop matching. */ |
4513 | |
4514 | while (i < matches) |
4515 | { |
4516 | register int c1, c2, si; |
4517 | |
4518 | if (completion_case_fold) |
4519 | { |
4520 | for (si = 0; |
4521 | (c1 = to_lower(match_list[i][si])) && |
4522 | (c2 = to_lower(match_list[i + 1][si])); |
4523 | si++) |
4524 | if (c1 != c2) break; |
4525 | } |
4526 | else |
4527 | { |
4528 | for (si = 0; |
4529 | (c1 = match_list[i][si]) && |
4530 | (c2 = match_list[i + 1][si]); |
4531 | si++) |
4532 | if (c1 != c2) break; |
4533 | } |
4534 | |
4535 | if (low > si) low = si; |
4536 | i++; |
4537 | } |
4538 | match_list[0] = (char *)xmalloc (low + 1); |
4539 | strncpy (match_list[0], match_list[1], low); |
4540 | match_list[0][low] = '\0'; |
4541 | } |
4542 | } |
4543 | else /* There were no matches. */ |
4544 | { |
4545 | free (match_list); |
4546 | match_list = (char **)NULL; |
4547 | } |
4548 | return (match_list); |
4549 | } |
4550 | |
4551 | /* Okay, now we write the entry_function for filename completion. In the |
4552 | general case. Note that completion in the shell is a little different |
4553 | because of all the pathnames that must be followed when looking up the |
4554 | completion for a command. */ |
4555 | char * |
4556 | filename_completion_function (text, state) |
4557 | int state; |
4558 | char *text; |
4559 | { |
4560 | static DIR *directory; |
4561 | static char *filename = (char *)NULL; |
4562 | static char *dirname = (char *)NULL; |
4563 | static char *users_dirname = (char *)NULL; |
4564 | static int filename_len; |
4565 | |
4566 | struct direct *entry = (struct direct *)NULL; |
4567 | |
4568 | /* If we don't have any state, then do some initialization. */ |
4569 | if (!state) |
4570 | { |
4571 | char *rindex (), *temp; |
4572 | |
4573 | if (dirname) free (dirname); |
4574 | if (filename) free (filename); |
4575 | if (users_dirname) free (users_dirname); |
4576 | |
4577 | filename = savestring (text); |
4578 | if (!*text) text = "."; |
4579 | dirname = savestring (text); |
4580 | |
4581 | temp = rindex (dirname, '/'); |
4582 | |
4583 | if (temp) |
4584 | { |
4585 | strcpy (filename, ++temp); |
4586 | *temp = '\0'; |
4587 | } |
4588 | else |
4589 | strcpy (dirname, "."); |
4590 | |
4591 | /* We aren't done yet. We also support the "~user" syntax. */ |
4592 | |
4593 | /* Save the version of the directory that the user typed. */ |
4594 | users_dirname = savestring (dirname); |
4595 | { |
4596 | char *tilde_expand (), *temp_dirname = tilde_expand (dirname); |
4597 | free (dirname); |
4598 | dirname = temp_dirname; |
4599 | #ifdef SHELL |
4600 | { |
4601 | extern int follow_symbolic_links; |
4602 | char *make_absolute (); |
4603 | |
4604 | if (follow_symbolic_links && (strcmp (dirname, ".") != 0)) |
4605 | { |
4606 | temp_dirname = make_absolute (dirname, get_working_directory ("")); |
4607 | |
4608 | if (temp_dirname) |
4609 | { |
4610 | free (dirname); |
4611 | dirname = temp_dirname; |
4612 | } |
4613 | } |
4614 | } |
4615 | #endif /* SHELL */ |
4616 | } |
4617 | directory = opendir (dirname); |
4618 | filename_len = strlen (filename); |
4619 | |
4620 | rl_filename_completion_desired = 1; |
4621 | } |
4622 | |
4623 | /* At this point we should entertain the possibility of hacking wildcarded |
4624 | filenames, like /usr/man*\/te<TAB>. If the directory name contains |
4625 | globbing characters, then build an array of directories to glob on, and |
4626 | glob on the first one. */ |
4627 | |
4628 | /* Now that we have some state, we can read the directory. */ |
4629 | |
4630 | while (directory && (entry = readdir (directory))) |
4631 | { |
4632 | /* Special case for no filename. |
4633 | All entries except "." and ".." match. */ |
4634 | if (!filename_len) |
4635 | { |
4636 | if ((strcmp (entry->d_name, ".") != 0) && |
4637 | (strcmp (entry->d_name, "..") != 0)) |
4638 | break; |
4639 | } |
4640 | else |
4641 | { |
4642 | /* Otherwise, if these match upto the length of filename, then |
4643 | it is a match. */ |
4644 | #ifdef TMB_SYSV |
4645 | if ((strlen (entry->d_name) >= filename_len) && |
4646 | (strncmp (filename, entry->d_name, filename_len) == 0)) |
4647 | #else |
4648 | if ((entry->d_namlen >= filename_len) && |
4649 | (strncmp (filename, entry->d_name, filename_len) == 0)) |
4650 | #endif /* TMB_SYSV */ |
4651 | { |
4652 | break; |
4653 | } |
4654 | } |
4655 | } |
4656 | |
4657 | if (!entry) |
4658 | { |
4659 | if (directory) |
4660 | { |
4661 | closedir (directory); |
4662 | directory = (DIR *)NULL; |
4663 | } |
4664 | return (char *)NULL; |
4665 | } |
4666 | else |
4667 | { |
4668 | char *temp; |
4669 | |
4670 | if (dirname && (strcmp (dirname, ".") != 0)) |
4671 | { |
4672 | #ifdef TMB_SYSV |
4673 | temp = (char *)xmalloc (1 + strlen (users_dirname) |
4674 | + strlen (entry->d_name)); |
4675 | #else |
4676 | temp = (char *)xmalloc (1 + strlen (users_dirname) |
4677 | + entry->d_namlen); |
4678 | #endif /* TMB_SYSV */ |
4679 | strcpy (temp, users_dirname); |
4680 | strcat (temp, entry->d_name); |
4681 | } |
4682 | else |
4683 | { |
4684 | temp = (savestring (entry->d_name)); |
4685 | } |
4686 | return (temp); |
4687 | } |
4688 | } |
4689 | |
4690 | \f |
4691 | /* **************************************************************** */ |
4692 | /* */ |
4693 | /* Binding keys */ |
4694 | /* */ |
4695 | /* **************************************************************** */ |
4696 | |
4697 | /* rl_add_defun (char *name, Function *function, int key) |
4698 | Add NAME to the list of named functions. Make FUNCTION |
4699 | be the function that gets called. |
4700 | If KEY is not -1, then bind it. */ |
4701 | rl_add_defun (name, function, key) |
4702 | char *name; |
4703 | Function *function; |
4704 | int key; |
4705 | { |
4706 | if (key != -1) |
4707 | rl_bind_key (key, function); |
4708 | rl_add_funmap_entry (name, function); |
4709 | } |
4710 | |
4711 | /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */ |
4712 | int |
4713 | rl_bind_key (key, function) |
4714 | int key; |
4715 | Function *function; |
4716 | { |
4717 | if (key < 0) |
4718 | return (key); |
4719 | |
4720 | if (key > 127 && key < 256) |
4721 | { |
4722 | if (keymap[ESC].type == ISKMAP) |
4723 | { |
4724 | Keymap escmap = (Keymap)keymap[ESC].function; |
4725 | |
4726 | key -= 128; |
4727 | escmap[key].type = ISFUNC; |
4728 | escmap[key].function = function; |
4729 | return (0); |
4730 | } |
4731 | return (key); |
4732 | } |
4733 | |
4734 | keymap[key].type = ISFUNC; |
4735 | keymap[key].function = function; |
4736 | return (0); |
4737 | } |
4738 | |
4739 | /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid |
4740 | KEY. */ |
4741 | int |
4742 | rl_bind_key_in_map (key, function, map) |
4743 | int key; |
4744 | Function *function; |
4745 | Keymap map; |
4746 | { |
4747 | int result; |
4748 | Keymap oldmap = keymap; |
4749 | |
4750 | keymap = map; |
4751 | result = rl_bind_key (key, function); |
4752 | keymap = oldmap; |
4753 | return (result); |
4754 | } |
4755 | |
4756 | /* Make KEY do nothing in the currently selected keymap. |
4757 | Returns non-zero in case of error. */ |
4758 | int |
4759 | rl_unbind_key (key) |
4760 | int key; |
4761 | { |
4762 | return (rl_bind_key (key, (Function *)NULL)); |
4763 | } |
4764 | |
4765 | /* Make KEY do nothing in MAP. |
4766 | Returns non-zero in case of error. */ |
4767 | int |
4768 | rl_unbind_key_in_map (key, map) |
4769 | int key; |
4770 | Keymap map; |
4771 | { |
4772 | return (rl_bind_key_in_map (key, (Function *)NULL, map)); |
4773 | } |
4774 | |
4775 | /* Bind the key sequence represented by the string KEYSEQ to |
4776 | FUNCTION. This makes new keymaps as necessary. The initial |
4777 | place to do bindings is in MAP. */ |
4778 | rl_set_key (keyseq, function, map) |
4779 | char *keyseq; |
4780 | Function *function; |
4781 | Keymap map; |
4782 | { |
4783 | rl_generic_bind (ISFUNC, keyseq, function, map); |
4784 | } |
4785 | |
4786 | /* Bind the key sequence represented by the string KEYSEQ to |
4787 | the string of characters MACRO. This makes new keymaps as |
4788 | necessary. The initial place to do bindings is in MAP. */ |
4789 | rl_macro_bind (keyseq, macro, map) |
4790 | char *keyseq, *macro; |
4791 | Keymap map; |
4792 | { |
4793 | char *macro_keys = (char *)xmalloc (2 * (strlen (macro))); |
4794 | int macro_keys_len; |
4795 | |
4796 | if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len)) |
4797 | { |
4798 | free (macro_keys); |
4799 | return; |
4800 | } |
4801 | rl_generic_bind (ISMACR, keyseq, macro_keys, map); |
4802 | } |
4803 | |
4804 | /* Bind the key sequence represented by the string KEYSEQ to |
4805 | the arbitrary pointer DATA. TYPE says what kind of data is |
4806 | pointed to by DATA, right now this can be a function (ISFUNC), |
4807 | a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps |
4808 | as necessary. The initial place to do bindings is in MAP. */ |
4809 | rl_generic_bind (type, keyseq, data, map) |
4810 | int type; |
4811 | char *keyseq, *data; |
4812 | Keymap map; |
4813 | { |
4814 | char *keys; |
4815 | int keys_len; |
4816 | register int i; |
4817 | |
4818 | /* If no keys to bind to, exit right away. */ |
4819 | if (!keyseq || !*keyseq) |
4820 | { |
4821 | if (type == ISMACR) |
4822 | free (data); |
4823 | return; |
4824 | } |
4825 | |
4826 | keys = (char *)alloca (1 + (2 * strlen (keyseq))); |
4827 | |
4828 | /* Translate the ASCII representation of KEYSEQ into an array |
4829 | of characters. Stuff the characters into ARRAY, and the |
4830 | length of ARRAY into LENGTH. */ |
4831 | if (rl_translate_keyseq (keyseq, keys, &keys_len)) |
4832 | return; |
4833 | |
4834 | /* Bind keys, making new keymaps as necessary. */ |
4835 | for (i = 0; i < keys_len; i++) |
4836 | { |
4837 | if (i + 1 < keys_len) |
4838 | { |
4839 | if (map[keys[i]].type != ISKMAP) |
4840 | { |
4841 | if (map[i].type == ISMACR) |
4842 | free ((char *)map[i].function); |
4843 | |
4844 | map[keys[i]].type = ISKMAP; |
4845 | map[keys[i]].function = (Function *)rl_make_bare_keymap (); |
4846 | } |
4847 | map = (Keymap)map[keys[i]].function; |
4848 | } |
4849 | else |
4850 | { |
4851 | if (map[keys[i]].type == ISMACR) |
4852 | free ((char *)map[keys[i]].function); |
4853 | |
4854 | map[keys[i]].function = (Function *)data; |
4855 | map[keys[i]].type = type; |
4856 | } |
4857 | } |
4858 | } |
4859 | |
4860 | /* Translate the ASCII representation of SEQ, stuffing the |
4861 | values into ARRAY, an array of characters. LEN gets the |
4862 | final length of ARRAY. Return non-zero if there was an |
4863 | error parsing SEQ. */ |
4864 | rl_translate_keyseq (seq, array, len) |
4865 | char *seq, *array; |
4866 | int *len; |
4867 | { |
4868 | register int i, c, l = 0; |
4869 | |
4870 | for (i = 0; c = seq[i]; i++) |
4871 | { |
4872 | if (c == '\\') |
4873 | { |
4874 | c = seq[++i]; |
4875 | |
4876 | if (!c) |
4877 | break; |
4878 | |
4879 | if (((c == 'C' || c == 'M') && seq[i + 1] == '-') || |
4880 | (c == 'e')) |
4881 | { |
4882 | /* Handle special case of backwards define. */ |
4883 | if (strncmp (&seq[i], "C-\\M-", 5) == 0) |
4884 | { |
4885 | array[l++] = ESC; |
4886 | i += 5; |
4887 | array[l++] = CTRL (to_upper (seq[i])); |
4888 | if (!seq[i]) |
4889 | i--; |
4890 | continue; |
4891 | } |
4892 | |
4893 | switch (c) |
4894 | { |
4895 | case 'M': |
4896 | i++; |
4897 | array[l++] = ESC; |
4898 | break; |
4899 | |
4900 | case 'C': |
4901 | i += 2; |
4902 | array[l++] = CTRL (to_upper (seq[i])); |
4903 | break; |
4904 | |
4905 | case 'e': |
4906 | array[l++] = ESC; |
4907 | } |
4908 | |
4909 | continue; |
4910 | } |
4911 | } |
4912 | array[l++] = c; |
4913 | } |
4914 | |
4915 | array[l] = '\0'; |
4916 | *len = l; |
4917 | return (0); |
4918 | } |
4919 | |
4920 | /* Return a pointer to the function that STRING represents. |
4921 | If STRING doesn't have a matching function, then a NULL pointer |
4922 | is returned. */ |
4923 | Function * |
4924 | rl_named_function (string) |
4925 | char *string; |
4926 | { |
4927 | register int i; |
1c997a4a |
4928 | static int stricmp (); |
4187119d |
4929 | |
4930 | for (i = 0; funmap[i]; i++) |
4931 | if (stricmp (funmap[i]->name, string) == 0) |
4932 | return (funmap[i]->function); |
4933 | return ((Function *)NULL); |
4934 | } |
4935 | |
4936 | /* The last key bindings file read. */ |
4937 | static char *last_readline_init_file = "~/.inputrc"; |
4938 | |
4939 | /* Re-read the current keybindings file. */ |
4940 | rl_re_read_init_file (count, ignore) |
4941 | int count, ignore; |
4942 | { |
4943 | rl_read_init_file (last_readline_init_file); |
4944 | } |
4945 | |
4946 | /* Do key bindings from a file. If FILENAME is NULL it defaults |
4947 | to `~/.inputrc'. If the file existed and could be opened and |
4948 | read, 0 is returned, otherwise errno is returned. */ |
4949 | int |
4950 | rl_read_init_file (filename) |
4951 | char *filename; |
4952 | { |
4953 | extern int errno; |
4954 | int line_size, line_index; |
4955 | char *line = (char *)xmalloc (line_size = 100); |
4956 | char *openname; |
4957 | FILE *file; |
4958 | |
4959 | int c; |
4960 | |
4961 | /* Default the filename. */ |
4962 | if (!filename) |
4963 | filename = "~/.inputrc"; |
4964 | |
4965 | openname = tilde_expand (filename); |
4966 | |
4967 | /* Open the file. */ |
4968 | file = fopen (openname, "r"); |
4969 | free (openname); |
4970 | |
4971 | if (!file) |
4972 | return (errno); |
4973 | |
4974 | last_readline_init_file = filename; |
4975 | |
4976 | /* Loop reading lines from the file. Lines that start with `#' are |
4977 | comments, all other lines are commands for readline initialization. */ |
4978 | while ((c = rl_getc (file)) != EOF) |
4979 | { |
4980 | /* If comment, flush to EOL. */ |
4981 | if (c == '#') |
4982 | { |
4983 | while ((c = rl_getc (file)) != EOF && c != '\n'); |
4984 | if (c == EOF) |
4985 | goto function_exit; |
4986 | continue; |
4987 | } |
4988 | |
4989 | /* Otherwise, this is the start of a line. Read the |
4990 | line from the file. */ |
4991 | line_index = 0; |
4992 | while (c != EOF && c != '\n') |
4993 | { |
4994 | line[line_index++] = c; |
4995 | if (line_index == line_size) |
4996 | line = (char *)xrealloc (line, line_size += 100); |
4997 | c = rl_getc (file); |
4998 | } |
4999 | line[line_index] = '\0'; |
5000 | |
5001 | /* Parse the line. */ |
5002 | rl_parse_and_bind (line); |
5003 | } |
5004 | |
5005 | function_exit: |
5006 | |
5007 | free (line); |
5008 | /* Close up the file and exit. */ |
5009 | fclose (file); |
5010 | return (0); |
5011 | } |
5012 | |
5013 | |
5014 | /* **************************************************************** */ |
5015 | /* */ |
5016 | /* Parser Directives */ |
5017 | /* */ |
5018 | /* **************************************************************** */ |
5019 | |
5020 | /* Conditionals. */ |
5021 | |
5022 | /* Calling programs set this to have their argv[0]. */ |
5023 | char *rl_readline_name = "other"; |
5024 | |
4187119d |
5025 | /* Stack of previous values of parsing_conditionalized_out. */ |
5026 | static unsigned char *if_stack = (unsigned char *)NULL; |
5027 | static int if_stack_depth = 0; |
5028 | static int if_stack_size = 0; |
5029 | |
5030 | /* Push parsing_conditionalized_out, and set parser state based on ARGS. */ |
5031 | parser_if (args) |
5032 | char *args; |
5033 | { |
5034 | register int i; |
1c997a4a |
5035 | static int stricmp (); |
4187119d |
5036 | |
5037 | /* Push parser state. */ |
5038 | if (if_stack_depth + 1 >= if_stack_size) |
5039 | { |
5040 | if (!if_stack) |
5041 | if_stack = (unsigned char *)xmalloc (if_stack_size = 20); |
5042 | else |
5043 | if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20); |
5044 | } |
5045 | if_stack[if_stack_depth++] = parsing_conditionalized_out; |
5046 | |
5047 | /* We only check to see if the first word in ARGS is the same as the |
5048 | value stored in rl_readline_name. */ |
5049 | |
5050 | /* Isolate first argument. */ |
5051 | for (i = 0; args[i] && !whitespace (args[i]); i++); |
5052 | |
5053 | if (args[i]) |
5054 | args[i++] = '\0'; |
5055 | |
5056 | if (stricmp (args, rl_readline_name) == 0) |
5057 | parsing_conditionalized_out = 0; |
5058 | else |
5059 | parsing_conditionalized_out = 1; |
5060 | } |
5061 | |
5062 | /* Invert the current parser state if there is anything on the stack. */ |
5063 | parser_else (args) |
5064 | char *args; |
5065 | { |
5066 | if (if_stack_depth) |
5067 | parsing_conditionalized_out = !parsing_conditionalized_out; |
5068 | else |
5069 | { |
5070 | /* *** What, no error message? *** */ |
5071 | } |
5072 | } |
5073 | |
5074 | /* Terminate a conditional, popping the value of |
5075 | parsing_conditionalized_out from the stack. */ |
5076 | parser_endif (args) |
5077 | char *args; |
5078 | { |
5079 | if (if_stack_depth) |
5080 | parsing_conditionalized_out = if_stack[--if_stack_depth]; |
5081 | else |
5082 | { |
5083 | /* *** What, no error message? *** */ |
5084 | } |
5085 | } |
5086 | |
5087 | /* Associate textual names with actual functions. */ |
5088 | static struct { |
5089 | char *name; |
5090 | Function *function; |
5091 | } parser_directives [] = { |
5092 | { "if", parser_if }, |
5093 | { "endif", parser_endif }, |
5094 | { "else", parser_else }, |
5095 | { (char *)0x0, (Function *)0x0 } |
5096 | }; |
5097 | |
5098 | /* Handle a parser directive. STATEMENT is the line of the directive |
5099 | without any leading `$'. */ |
5100 | static int |
5101 | handle_parser_directive (statement) |
5102 | char *statement; |
5103 | { |
5104 | register int i; |
5105 | char *directive, *args; |
1c997a4a |
5106 | static int stricmp (); |
4187119d |
5107 | |
5108 | /* Isolate the actual directive. */ |
5109 | |
5110 | /* Skip whitespace. */ |
5111 | for (i = 0; whitespace (statement[i]); i++); |
5112 | |
5113 | directive = &statement[i]; |
5114 | |
5115 | for (; statement[i] && !whitespace (statement[i]); i++); |
5116 | |
5117 | if (statement[i]) |
5118 | statement[i++] = '\0'; |
5119 | |
5120 | for (; statement[i] && whitespace (statement[i]); i++); |
5121 | |
5122 | args = &statement[i]; |
5123 | |
5124 | /* Lookup the command, and act on it. */ |
5125 | for (i = 0; parser_directives[i].name; i++) |
5126 | if (stricmp (directive, parser_directives[i].name) == 0) |
5127 | { |
5128 | (*parser_directives[i].function) (args); |
5129 | return (0); |
5130 | } |
5131 | |
5132 | /* *** Should an error message be output? */ |
5133 | return (1); |
5134 | } |
5135 | |
5136 | /* Read the binding command from STRING and perform it. |
5137 | A key binding command looks like: Keyname: function-name\0, |
5138 | a variable binding command looks like: set variable value. |
5139 | A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */ |
5140 | rl_parse_and_bind (string) |
5141 | char *string; |
5142 | { |
5143 | extern char *possible_control_prefixes[], *possible_meta_prefixes[]; |
5144 | char *rindex (), *funname, *kname; |
1c997a4a |
5145 | static int substring_member_of_array (), stricmp (); |
4187119d |
5146 | register int c; |
5147 | int key, i; |
5148 | |
5149 | if (!string || !*string || *string == '#') |
5150 | return; |
5151 | |
5152 | /* If this is a parser directive, act on it. */ |
5153 | if (*string == '$') |
5154 | { |
5155 | handle_parser_directive (&string[1]); |
5156 | return; |
5157 | } |
5158 | |
5159 | /* If we are supposed to be skipping parsing right now, then do it. */ |
5160 | if (parsing_conditionalized_out) |
5161 | return; |
5162 | |
5163 | i = 0; |
5164 | /* If this keyname is a complex key expression surrounded by quotes, |
5165 | advance to after the matching close quote. */ |
5166 | if (*string == '"') |
5167 | { |
5168 | for (i = 1; c = string[i]; i++) |
5169 | { |
5170 | if (c == '"' && string[i - 1] != '\\') |
5171 | break; |
5172 | } |
5173 | } |
5174 | |
5175 | /* Advance to the colon (:) or whitespace which separates the two objects. */ |
5176 | for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ ); |
5177 | |
5178 | /* Mark the end of the command (or keyname). */ |
5179 | if (string[i]) |
5180 | string[i++] = '\0'; |
5181 | |
5182 | /* If this is a command to set a variable, then do that. */ |
5183 | if (stricmp (string, "set") == 0) |
5184 | { |
5185 | char *var = string + i; |
5186 | char *value; |
5187 | |
5188 | /* Make VAR point to start of variable name. */ |
5189 | while (*var && whitespace (*var)) var++; |
5190 | |
5191 | /* Make value point to start of value string. */ |
5192 | value = var; |
5193 | while (*value && !whitespace (*value)) value++; |
5194 | if (*value) |
5195 | *value++ = '\0'; |
5196 | while (*value && whitespace (*value)) value++; |
5197 | |
5198 | rl_variable_bind (var, value); |
5199 | return; |
5200 | } |
5201 | |
5202 | /* Skip any whitespace between keyname and funname. */ |
5203 | for (; string[i] && whitespace (string[i]); i++); |
5204 | funname = &string[i]; |
5205 | |
5206 | /* Now isolate funname. |
5207 | For straight function names just look for whitespace, since |
5208 | that will signify the end of the string. But this could be a |
5209 | macro definition. In that case, the string is quoted, so skip |
5210 | to the matching delimiter. */ |
5211 | if (*funname == '\'' || *funname == '"') |
5212 | { |
5213 | int delimiter = string[i++]; |
5214 | |
5215 | for (; c = string[i]; i++) |
5216 | { |
5217 | if (c == delimiter && string[i - 1] != '\\') |
5218 | break; |
5219 | } |
5220 | if (c) |
5221 | i++; |
5222 | } |
5223 | |
5224 | /* Advance to the end of the string. */ |
5225 | for (; string[i] && !whitespace (string[i]); i++); |
5226 | |
5227 | /* No extra whitespace at the end of the string. */ |
5228 | string[i] = '\0'; |
5229 | |
5230 | /* If this is a new-style key-binding, then do the binding with |
5231 | rl_set_key (). Otherwise, let the older code deal with it. */ |
5232 | if (*string == '"') |
5233 | { |
5234 | char *seq = (char *)alloca (1 + strlen (string)); |
5235 | register int j, k = 0; |
5236 | |
5237 | for (j = 1; string[j]; j++) |
5238 | { |
5239 | if (string[j] == '"' && string[j - 1] != '\\') |
5240 | break; |
5241 | |
5242 | seq[k++] = string[j]; |
5243 | } |
5244 | seq[k] = '\0'; |
5245 | |
5246 | /* Binding macro? */ |
5247 | if (*funname == '\'' || *funname == '"') |
5248 | { |
5249 | j = strlen (funname); |
5250 | |
5251 | if (j && funname[j - 1] == *funname) |
5252 | funname[j - 1] = '\0'; |
5253 | |
5254 | rl_macro_bind (seq, &funname[1], keymap); |
5255 | } |
5256 | else |
5257 | rl_set_key (seq, rl_named_function (funname), keymap); |
5258 | |
5259 | return; |
5260 | } |
5261 | |
5262 | /* Get the actual character we want to deal with. */ |
5263 | kname = rindex (string, '-'); |
5264 | if (!kname) |
5265 | kname = string; |
5266 | else |
5267 | kname++; |
5268 | |
5269 | key = glean_key_from_name (kname); |
5270 | |
5271 | /* Add in control and meta bits. */ |
5272 | if (substring_member_of_array (string, possible_control_prefixes)) |
5273 | key = CTRL (to_upper (key)); |
5274 | |
5275 | if (substring_member_of_array (string, possible_meta_prefixes)) |
5276 | key = META (key); |
5277 | |
5278 | /* Temporary. Handle old-style keyname with macro-binding. */ |
5279 | if (*funname == '\'' || *funname == '"') |
5280 | { |
5281 | char seq[2]; |
5282 | int fl = strlen (funname); |
5283 | |
5284 | seq[0] = key; seq[1] = '\0'; |
5285 | if (fl && funname[fl - 1] == *funname) |
5286 | funname[fl - 1] = '\0'; |
5287 | |
5288 | rl_macro_bind (seq, &funname[1], keymap); |
5289 | } |
5290 | else |
5291 | rl_bind_key (key, rl_named_function (funname)); |
5292 | } |
5293 | |
5294 | rl_variable_bind (name, value) |
5295 | char *name, *value; |
5296 | { |
1c997a4a |
5297 | static int strnicmp (), stricmp (); |
4187119d |
5298 | |
5299 | if (stricmp (name, "editing-mode") == 0) |
5300 | { |
5301 | if (strnicmp (value, "vi", 2) == 0) |
5302 | { |
5303 | #ifdef VI_MODE |
5304 | keymap = vi_insertion_keymap; |
5305 | rl_editing_mode = vi_mode; |
5306 | #endif /* VI_MODE */ |
5307 | } |
5308 | else if (strnicmp (value, "emacs", 5) == 0) |
5309 | { |
5310 | keymap = emacs_standard_keymap; |
5311 | rl_editing_mode = emacs_mode; |
5312 | } |
5313 | } |
5314 | else if (stricmp (name, "horizontal-scroll-mode") == 0) |
5315 | { |
5316 | if (!*value || stricmp (value, "On") == 0) |
5317 | horizontal_scroll_mode = 1; |
5318 | else |
5319 | horizontal_scroll_mode = 0; |
5320 | } |
5321 | } |
5322 | |
5323 | /* Return the character which matches NAME. |
5324 | For example, `Space' returns ' '. */ |
5325 | |
5326 | typedef struct { |
5327 | char *name; |
5328 | int value; |
5329 | } assoc_list; |
5330 | |
5331 | assoc_list name_key_alist[] = { |
5332 | { "Space", ' ' }, |
5333 | { "SPC", ' ' }, |
5334 | { "Rubout", 0x7f }, |
5335 | { "DEL", 0x7f }, |
5336 | { "Tab", 0x09 }, |
5337 | { "Newline", '\n' }, |
5338 | { "Return", '\r' }, |
5339 | { "RET", '\r' }, |
5340 | { "LFD", '\n' }, |
5341 | { "Escape", '\033' }, |
5342 | { "ESC", '\033' }, |
5343 | |
5344 | { (char *)0x0, 0 } |
5345 | }; |
5346 | |
5347 | int |
5348 | glean_key_from_name (name) |
5349 | char *name; |
5350 | { |
5351 | register int i; |
1c997a4a |
5352 | static int stricmp (); |
4187119d |
5353 | |
5354 | for (i = 0; name_key_alist[i].name; i++) |
5355 | if (stricmp (name, name_key_alist[i].name) == 0) |
5356 | return (name_key_alist[i].value); |
5357 | |
5358 | return (*name); |
5359 | } |
5360 | |
5361 | \f |
5362 | /* **************************************************************** */ |
5363 | /* */ |
5364 | /* String Utility Functions */ |
5365 | /* */ |
5366 | /* **************************************************************** */ |
5367 | |
5368 | /* Return non-zero if any members of ARRAY are a substring in STRING. */ |
5369 | static int |
5370 | substring_member_of_array (string, array) |
5371 | char *string, **array; |
5372 | { |
5373 | static char *strindex (); |
5374 | |
5375 | while (*array) |
5376 | { |
5377 | if (strindex (string, *array)) |
5378 | return (1); |
5379 | array++; |
5380 | } |
5381 | return (0); |
5382 | } |
5383 | |
5384 | /* Whoops, Unix doesn't have strnicmp. */ |
5385 | |
5386 | /* Compare at most COUNT characters from string1 to string2. Case |
5387 | doesn't matter. */ |
5388 | static int |
5389 | strnicmp (string1, string2, count) |
5390 | char *string1, *string2; |
5391 | { |
5392 | register char ch1, ch2; |
5393 | |
5394 | while (count) { |
5395 | ch1 = *string1++; |
5396 | ch2 = *string2++; |
5397 | if (to_upper(ch1) == to_upper(ch2)) |
5398 | count--; |
5399 | else break; |
5400 | } |
5401 | return (count); |
5402 | } |
5403 | |
5404 | /* strcmp (), but caseless. */ |
5405 | static int |
5406 | stricmp (string1, string2) |
5407 | char *string1, *string2; |
5408 | { |
5409 | register char ch1, ch2; |
5410 | |
5411 | while (*string1 && *string2) { |
5412 | ch1 = *string1++; |
5413 | ch2 = *string2++; |
5414 | if (to_upper(ch1) != to_upper(ch2)) |
5415 | return (1); |
5416 | } |
5417 | return (*string1 | *string2); |
5418 | } |
5419 | |
5420 | /* Determine if s2 occurs in s1. If so, return a pointer to the |
5421 | match in s1. The compare is case insensitive. */ |
5422 | static char * |
5423 | strindex (s1, s2) |
5424 | register char *s1, *s2; |
5425 | { |
5426 | register int i, l = strlen (s2); |
5427 | register int len = strlen (s1); |
5428 | |
5429 | for (i = 0; (len - i) >= l; i++) |
5430 | if (strnicmp (&s1[i], s2, l) == 0) |
5431 | return (s1 + i); |
5432 | return ((char *)NULL); |
5433 | } |
5434 | |
5435 | \f |
5436 | /* **************************************************************** */ |
5437 | /* */ |
5438 | /* SYSV Support */ |
5439 | /* */ |
5440 | /* **************************************************************** */ |
5441 | |
5442 | /* Since system V reads input differently than we do, I have to |
5443 | make a special version of getc for that. */ |
5444 | |
5445 | #ifdef SYSV |
5446 | |
5447 | extern int errno; |
5448 | #include <sys/errno.h> |
5449 | |
5450 | int |
5451 | rl_getc (stream) |
5452 | FILE *stream; |
5453 | { |
5454 | int result; |
5455 | unsigned char c; |
5456 | |
5457 | while (1) |
5458 | { |
5459 | result = read (fileno (stream), &c, sizeof (char)); |
5460 | if (result == sizeof (char)) |
5461 | return (c); |
5462 | |
5463 | if (errno != EINTR) |
5464 | return (EOF); |
5465 | } |
5466 | } |
5467 | #else |
5468 | int |
5469 | rl_getc (stream) |
5470 | FILE *stream; |
5471 | { |
5472 | return (getc (stream)); |
5473 | } |
5474 | #endif |
5475 | |
5476 | #ifdef STATIC_MALLOC |
5477 | \f |
5478 | /* **************************************************************** */ |
5479 | /* */ |
5480 | /* xmalloc and xrealloc () */ |
5481 | /* */ |
5482 | /* **************************************************************** */ |
5483 | |
5484 | static char * |
5485 | xmalloc (bytes) |
5486 | int bytes; |
5487 | { |
5488 | static memory_error_and_abort (); |
5489 | char *temp = (char *)malloc (bytes); |
5490 | |
5491 | if (!temp) |
5492 | memory_error_and_abort (); |
5493 | return (temp); |
5494 | } |
5495 | |
5496 | static char * |
5497 | xrealloc (pointer, bytes) |
5498 | char *pointer; |
5499 | int bytes; |
5500 | { |
5501 | static memory_error_and_abort (); |
5502 | char *temp = (char *)realloc (pointer, bytes); |
5503 | |
5504 | if (!temp) |
5505 | memory_error_and_abort (); |
5506 | return (temp); |
5507 | } |
5508 | |
5509 | static |
5510 | memory_error_and_abort () |
5511 | { |
5512 | fprintf (stderr, "readline: Out of virtual memory!\n"); |
5513 | abort (); |
5514 | } |
5515 | #endif /* STATIC_MALLOC */ |
5516 | |
5517 | \f |
5518 | /* **************************************************************** */ |
5519 | /* */ |
5520 | /* Testing Readline */ |
5521 | /* */ |
5522 | /* **************************************************************** */ |
5523 | |
5524 | #ifdef TEST |
5525 | |
5526 | main () |
5527 | { |
5528 | HIST_ENTRY **history_list (); |
5529 | char *temp = (char *)NULL; |
5530 | char *prompt = "readline% "; |
5531 | int done = 0; |
5532 | |
5533 | while (!done) |
5534 | { |
5535 | temp = readline (prompt); |
5536 | |
5537 | /* Test for EOF. */ |
5538 | if (!temp) |
5539 | exit (1); |
5540 | |
5541 | /* If there is anything on the line, print it and remember it. */ |
5542 | if (*temp) |
5543 | { |
5544 | fprintf (stderr, "%s\r\n", temp); |
5545 | add_history (temp); |
5546 | } |
5547 | |
5548 | /* Check for `command' that we handle. */ |
5549 | if (strcmp (temp, "quit") == 0) |
5550 | done = 1; |
5551 | |
5552 | if (strcmp (temp, "list") == 0) { |
5553 | HIST_ENTRY **list = history_list (); |
5554 | register int i; |
5555 | if (list) { |
5556 | for (i = 0; list[i]; i++) { |
5557 | fprintf (stderr, "%d: %s\r\n", i, list[i]->line); |
5558 | free (list[i]->line); |
5559 | } |
5560 | free (list); |
5561 | } |
5562 | } |
5563 | free (temp); |
5564 | } |
5565 | } |
5566 | |
5567 | #endif /* TEST */ |
5568 | |
5569 | \f |
5570 | /* |
5571 | * Local variables: |
5572 | * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap" |
5573 | * end: |
5574 | */ |