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