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