2000-02-07 Jason Molenda (jsm@bugshack.cygnus.com)
[deliverable/binutils-gdb.git] / readline / readline.c
CommitLineData
d60d9f65
SS
1/* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 1, or
12 (at your option) any later version.
13
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 675 Mass Ave, Cambridge, MA 02139, USA. */
23#define READLINE_LIBRARY
24
25#if defined (HAVE_CONFIG_H)
26# include <config.h>
27#endif
28
29#include <sys/types.h>
30#include "posixstat.h"
31#include <fcntl.h>
32#if defined (HAVE_SYS_FILE_H)
33# include <sys/file.h>
34#endif /* HAVE_SYS_FILE_H */
35
36#if defined (HAVE_UNISTD_H)
37# include <unistd.h>
38#endif /* HAVE_UNISTD_H */
39
40#if defined (HAVE_STDLIB_H)
41# include <stdlib.h>
42#else
43# include "ansi_stdlib.h"
44#endif /* HAVE_STDLIB_H */
45
46#if defined (HAVE_LOCALE_H)
47# include <locale.h>
48#endif
49
50#include <signal.h>
51#include <stdio.h>
52#include "posixjmp.h"
53
54/* System-specific feature definitions and include files. */
55#include "rldefs.h"
56
57#if defined (__EMX__)
58# define INCL_DOSPROCESS
59# include <os2.h>
60#endif /* __EMX__ */
61
62/* Some standard library routines. */
63#include "readline.h"
64#include "history.h"
65
66#ifndef RL_LIBRARY_VERSION
c862e87b 67# define RL_LIBRARY_VERSION "4.0"
d60d9f65
SS
68#endif
69
70/* Evaluates its arguments multiple times. */
71#define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
72
73/* NOTE: Functions and variables prefixed with `_rl_' are
74 pseudo-global: they are global so they can be shared
75 between files in the readline library, but are not intended
76 to be visible to readline callers. */
77
78/* Variables and functions imported from terminal.c */
79extern int _rl_init_terminal_io ();
80extern void _rl_enable_meta_key ();
81#ifdef _MINIX
82extern void _rl_output_character_function ();
83#else
84extern int _rl_output_character_function ();
85#endif
d60d9f65
SS
86
87extern int _rl_enable_meta;
88extern int _rl_term_autowrap;
89extern int screenwidth, screenheight, screenchars;
90
91/* Variables and functions imported from rltty.c. */
92extern void rl_prep_terminal (), rl_deprep_terminal ();
93extern void rltty_set_default_bindings ();
94
95/* Functions imported from util.c. */
96extern void _rl_abort_internal ();
97extern void rl_extend_line_buffer ();
98extern int alphabetic ();
99
100/* Functions imported from bind.c. */
101extern void _rl_bind_if_unbound ();
d60d9f65
SS
102
103/* Functions imported from input.c. */
104extern int _rl_any_typein ();
105extern void _rl_insert_typein ();
106extern int rl_read_key ();
107
108/* Functions imported from nls.c */
109extern int _rl_init_eightbit ();
110
111/* Functions imported from shell.c */
112extern char *get_env_value ();
113
114/* External redisplay functions and variables from display.c */
115extern void _rl_move_vert ();
116extern void _rl_update_final ();
117extern void _rl_clear_to_eol ();
118extern void _rl_clear_screen ();
c862e87b 119extern void _rl_erase_entire_line ();
d60d9f65
SS
120
121extern void _rl_erase_at_end_of_line ();
122extern void _rl_move_cursor_relative ();
123
124extern int _rl_vis_botlin;
125extern int _rl_last_c_pos;
126extern int _rl_horizontal_scroll_mode;
127extern int rl_display_fixed;
128extern int _rl_suppress_redisplay;
129extern char *rl_display_prompt;
130
131/* Variables imported from complete.c. */
132extern char *rl_completer_word_break_characters;
133extern char *rl_basic_word_break_characters;
134extern int rl_completion_query_items;
135extern int rl_complete_with_tilde_expansion;
136
137/* Variables and functions from macro.c. */
138extern void _rl_add_macro_char ();
139extern void _rl_with_macro_input ();
140extern int _rl_next_macro_key ();
141extern int _rl_defining_kbd_macro;
142
143#if defined (VI_MODE)
144/* Functions imported from vi_mode.c. */
145extern void _rl_vi_set_last ();
146extern void _rl_vi_reset_last ();
147extern void _rl_vi_done_inserting ();
148extern int _rl_vi_textmod_command ();
149extern void _rl_vi_initialize_line ();
150#endif /* VI_MODE */
151
152extern UNDO_LIST *rl_undo_list;
153extern int _rl_doing_an_undo;
154
155/* Forward declarations used in this file. */
156void _rl_free_history_entry ();
157
158int _rl_dispatch ();
159int _rl_init_argument ();
160
161static char *readline_internal ();
162static void readline_initialize_everything ();
163static void start_using_history ();
164static void bind_arrow_keys ();
165
166#if !defined (__GO32__)
167static void readline_default_bindings ();
168#endif /* !__GO32__ */
169
170#if defined (__GO32__)
171# include <go32.h>
172# include <pc.h>
173# undef HANDLE_SIGNALS
174#endif /* __GO32__ */
175
176extern char *xmalloc (), *xrealloc ();
177
178/* **************************************************************** */
179/* */
180/* Line editing input utility */
181/* */
182/* **************************************************************** */
183
184char *rl_library_version = RL_LIBRARY_VERSION;
185
186/* A pointer to the keymap that is currently in use.
187 By default, it is the standard emacs keymap. */
188Keymap _rl_keymap = emacs_standard_keymap;
189
190/* The current style of editing. */
191int rl_editing_mode = emacs_mode;
192
193/* Non-zero if we called this function from _rl_dispatch(). It's present
194 so functions can find out whether they were called from a key binding
195 or directly from an application. */
196int rl_dispatching;
197
198/* Non-zero if the previous command was a kill command. */
199int _rl_last_command_was_kill = 0;
200
201/* The current value of the numeric argument specified by the user. */
202int rl_numeric_arg = 1;
203
204/* Non-zero if an argument was typed. */
205int rl_explicit_arg = 0;
206
207/* Temporary value used while generating the argument. */
208int rl_arg_sign = 1;
209
210/* Non-zero means we have been called at least once before. */
211static int rl_initialized;
212
213/* If non-zero, this program is running in an EMACS buffer. */
214static int running_in_emacs;
215
216/* The current offset in the current input line. */
217int rl_point;
218
219/* Mark in the current input line. */
220int rl_mark;
221
222/* Length of the current input line. */
223int rl_end;
224
225/* Make this non-zero to return the current input_line. */
226int rl_done;
227
228/* The last function executed by readline. */
229Function *rl_last_func = (Function *)NULL;
230
231/* Top level environment for readline_internal (). */
232procenv_t readline_top_level;
233
234/* The streams we interact with. */
235FILE *_rl_in_stream, *_rl_out_stream;
236
237/* The names of the streams that we do input and output to. */
238FILE *rl_instream = (FILE *)NULL;
239FILE *rl_outstream = (FILE *)NULL;
240
241/* Non-zero means echo characters as they are read. */
242int readline_echoing_p = 1;
243
244/* Current prompt. */
245char *rl_prompt;
246int rl_visible_prompt_length = 0;
247
248/* The number of characters read in order to type this complete command. */
249int rl_key_sequence_length = 0;
250
251/* If non-zero, then this is the address of a function to call just
c862e87b 252 before readline_internal_setup () prints the first prompt. */
d60d9f65
SS
253Function *rl_startup_hook = (Function *)NULL;
254
c862e87b
JM
255/* If non-zero, this is the address of a function to call just before
256 readline_internal_setup () returns and readline_internal starts
257 reading input characters. */
258Function *rl_pre_input_hook = (Function *)NULL;
259
d60d9f65
SS
260/* What we use internally. You should always refer to RL_LINE_BUFFER. */
261static char *the_line;
262
263/* The character that can generate an EOF. Really read from
264 the terminal driver... just defaulted here. */
265int _rl_eof_char = CTRL ('D');
266
267/* Non-zero makes this the next keystroke to read. */
268int rl_pending_input = 0;
269
270/* Pointer to a useful terminal name. */
271char *rl_terminal_name = (char *)NULL;
272
273/* Non-zero means to always use horizontal scrolling in line display. */
274int _rl_horizontal_scroll_mode = 0;
275
276/* Non-zero means to display an asterisk at the starts of history lines
277 which have been modified. */
278int _rl_mark_modified_lines = 0;
279
280/* The style of `bell' notification preferred. This can be set to NO_BELL,
281 AUDIBLE_BELL, or VISIBLE_BELL. */
282int _rl_bell_preference = AUDIBLE_BELL;
283
284/* String inserted into the line by rl_insert_comment (). */
285char *_rl_comment_begin;
286
287/* Keymap holding the function currently being executed. */
288Keymap rl_executing_keymap;
289
c862e87b
JM
290/* Non-zero means to erase entire line, including prompt, on empty input lines. */
291int rl_erase_empty_line = 0;
292
d60d9f65
SS
293/* Line buffer and maintenence. */
294char *rl_line_buffer = (char *)NULL;
295int rl_line_buffer_len = 0;
296
297/* Forward declarations used by the display and termcap code. */
298
299/* **************************************************************** */
300/* */
301/* `Forward' declarations */
302/* */
303/* **************************************************************** */
304
305/* Non-zero means do not parse any lines other than comments and
306 parser directives. */
307unsigned char _rl_parsing_conditionalized_out = 0;
308
309/* Non-zero means to convert characters with the meta bit set to
310 escape-prefixed characters so we can indirect through
311 emacs_meta_keymap or vi_escape_keymap. */
312int _rl_convert_meta_chars_to_ascii = 1;
313
314/* Non-zero means to output characters with the meta bit set directly
315 rather than as a meta-prefixed escape sequence. */
316int _rl_output_meta_chars = 0;
317
318/* **************************************************************** */
319/* */
320/* Top Level Functions */
321/* */
322/* **************************************************************** */
323
324/* Non-zero means treat 0200 bit in terminal input as Meta bit. */
325int _rl_meta_flag = 0; /* Forward declaration */
326
327/* Read a line of input. Prompt with PROMPT. An empty PROMPT means
328 none. A return value of NULL means that EOF was encountered. */
329char *
330readline (prompt)
331 char *prompt;
332{
333 char *value;
334
335 rl_prompt = prompt;
336
337 /* If we are at EOF return a NULL string. */
338 if (rl_pending_input == EOF)
339 {
340 rl_pending_input = 0;
341 return ((char *)NULL);
342 }
343
344 rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
345
346 rl_initialize ();
347 (*rl_prep_term_function) (_rl_meta_flag);
348
349#if defined (HANDLE_SIGNALS)
350 rl_set_signals ();
351#endif
352
353 value = readline_internal ();
354 (*rl_deprep_term_function) ();
355
356#if defined (HANDLE_SIGNALS)
357 rl_clear_signals ();
358#endif
359
360 return (value);
361}
362
363#if defined (READLINE_CALLBACKS)
364# define STATIC_CALLBACK
365#else
366# define STATIC_CALLBACK static
367#endif
368
369STATIC_CALLBACK void
370readline_internal_setup ()
371{
372 _rl_in_stream = rl_instream;
373 _rl_out_stream = rl_outstream;
374
375 if (rl_startup_hook)
376 (*rl_startup_hook) ();
377
378 if (readline_echoing_p == 0)
379 {
380 if (rl_prompt)
381 {
382 fprintf (_rl_out_stream, "%s", rl_prompt);
383 fflush (_rl_out_stream);
384 }
385 }
386 else
387 {
388 rl_on_new_line ();
389 (*rl_redisplay_function) ();
390#if defined (VI_MODE)
391 if (rl_editing_mode == vi_mode)
392 rl_vi_insertion_mode (1, 0);
393#endif /* VI_MODE */
394 }
c862e87b
JM
395
396 if (rl_pre_input_hook)
397 (*rl_pre_input_hook) ();
d60d9f65
SS
398}
399
400STATIC_CALLBACK char *
401readline_internal_teardown (eof)
402 int eof;
403{
404 char *temp;
405 HIST_ENTRY *entry;
406
407 /* Restore the original of this history line, iff the line that we
408 are editing was originally in the history, AND the line has changed. */
409 entry = current_history ();
410
411 if (entry && rl_undo_list)
412 {
413 temp = savestring (the_line);
414 rl_revert_line (1, 0);
c862e87b 415 entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
d60d9f65
SS
416 _rl_free_history_entry (entry);
417
418 strcpy (the_line, temp);
419 free (temp);
420 }
421
422 /* At any rate, it is highly likely that this line has an undo list. Get
423 rid of it now. */
424 if (rl_undo_list)
425 free_undo_list ();
426
427 return (eof ? (char *)NULL : savestring (the_line));
428}
429
430STATIC_CALLBACK int
431#if defined (READLINE_CALLBACKS)
432readline_internal_char ()
433#else
434readline_internal_charloop ()
435#endif
436{
437 static int lastc, eof_found;
438 int c, code, lk;
439
440 lastc = -1;
441 eof_found = 0;
442
443#if !defined (READLINE_CALLBACKS)
444 while (rl_done == 0)
445 {
446#endif
447 lk = _rl_last_command_was_kill;
448
449 code = setjmp (readline_top_level);
450
451 if (code)
452 (*rl_redisplay_function) ();
453
454 if (rl_pending_input == 0)
455 {
456 /* Then initialize the argument and number of keys read. */
457 _rl_init_argument ();
458 rl_key_sequence_length = 0;
459 }
460
461 c = rl_read_key ();
462
463 /* EOF typed to a non-blank line is a <NL>. */
464 if (c == EOF && rl_end)
465 c = NEWLINE;
466
467 /* The character _rl_eof_char typed to blank line, and not as the
468 previous character is interpreted as EOF. */
469 if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
470 {
471#if defined (READLINE_CALLBACKS)
472 return (rl_done = 1);
473#else
474 eof_found = 1;
475 break;
476#endif
477 }
478
479 lastc = c;
480 _rl_dispatch (c, _rl_keymap);
481
482 /* If there was no change in _rl_last_command_was_kill, then no kill
483 has taken place. Note that if input is pending we are reading
484 a prefix command, so nothing has changed yet. */
485 if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
486 _rl_last_command_was_kill = 0;
487
488#if defined (VI_MODE)
489 /* In vi mode, when you exit insert mode, the cursor moves back
490 over the previous character. We explicitly check for that here. */
491 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
492 rl_vi_check ();
493#endif /* VI_MODE */
494
495 if (rl_done == 0)
496 (*rl_redisplay_function) ();
497
c862e87b
JM
498 /* If the application writer has told us to erase the entire line if
499 the only character typed was something bound to rl_newline, do so. */
500 if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
501 rl_point == 0 && rl_end == 0)
502 _rl_erase_entire_line ();
503
d60d9f65
SS
504#if defined (READLINE_CALLBACKS)
505 return 0;
506#else
507 }
508
509 return (eof_found);
510#endif
511}
512
513#if defined (READLINE_CALLBACKS)
514static int
515readline_internal_charloop ()
516{
c862e87b 517 int eof = 1;
d60d9f65
SS
518
519 while (rl_done == 0)
520 eof = readline_internal_char ();
521 return (eof);
522}
523#endif /* READLINE_CALLBACKS */
524
525/* Read a line of input from the global rl_instream, doing output on
526 the global rl_outstream.
527 If rl_prompt is non-null, then that is our prompt. */
528static char *
529readline_internal ()
530{
531 int eof;
532
533 readline_internal_setup ();
534 eof = readline_internal_charloop ();
535 return (readline_internal_teardown (eof));
536}
537
538void
539_rl_init_line_state ()
540{
541 rl_point = rl_end = 0;
542 the_line = rl_line_buffer;
543 the_line[0] = 0;
544}
545
546void
547_rl_set_the_line ()
548{
549 the_line = rl_line_buffer;
550}
551
552/* Do the command associated with KEY in MAP.
553 If the associated command is really a keymap, then read
554 another key, and dispatch into that map. */
555int
556_rl_dispatch (key, map)
557 register int key;
558 Keymap map;
559{
560 int r, newkey;
561 char *macro;
562 Function *func;
563
564 if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
565 {
566 if (map[ESC].type == ISKMAP)
567 {
568 if (_rl_defining_kbd_macro)
569 _rl_add_macro_char (ESC);
570 map = FUNCTION_TO_KEYMAP (map, ESC);
571 key = UNMETA (key);
572 rl_key_sequence_length += 2;
573 return (_rl_dispatch (key, map));
574 }
575 else
576 ding ();
577 return 0;
578 }
579
580 if (_rl_defining_kbd_macro)
581 _rl_add_macro_char (key);
582
583 r = 0;
584 switch (map[key].type)
585 {
586 case ISFUNC:
587 func = map[key].function;
588 if (func != (Function *)NULL)
589 {
590 /* Special case rl_do_lowercase_version (). */
591 if (func == rl_do_lowercase_version)
592 return (_rl_dispatch (_rl_to_lower (key), map));
593
594 rl_executing_keymap = map;
595
596#if 0
597 _rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
598#endif
599
600 rl_dispatching = 1;
601 r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
602 rl_dispatching = 0;
603
604 /* If we have input pending, then the last command was a prefix
605 command. Don't change the state of rl_last_func. Otherwise,
606 remember the last command executed in this variable. */
607 if (!rl_pending_input && map[key].function != rl_digit_argument)
608 rl_last_func = map[key].function;
609 }
610 else
611 {
612 _rl_abort_internal ();
613 return -1;
614 }
615 break;
616
617 case ISKMAP:
618 if (map[key].function != (Function *)NULL)
619 {
620 rl_key_sequence_length++;
621 newkey = rl_read_key ();
622 r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
623 }
624 else
625 {
626 _rl_abort_internal ();
627 return -1;
628 }
629 break;
630
631 case ISMACR:
632 if (map[key].function != (Function *)NULL)
633 {
634 macro = savestring ((char *)map[key].function);
635 _rl_with_macro_input (macro);
636 return 0;
637 }
638 break;
639 }
640#if defined (VI_MODE)
641 if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
642 _rl_vi_textmod_command (key))
643 _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
644#endif
645 return (r);
646}
647
648/* **************************************************************** */
649/* */
650/* Initializations */
651/* */
652/* **************************************************************** */
653
654/* Initialize readline (and terminal if not already). */
655int
656rl_initialize ()
657{
658 /* If we have never been called before, initialize the
659 terminal and data structures. */
660 if (!rl_initialized)
661 {
662 readline_initialize_everything ();
663 rl_initialized++;
664 }
665
666 /* Initalize the current line information. */
667 _rl_init_line_state ();
668
669 /* We aren't done yet. We haven't even gotten started yet! */
670 rl_done = 0;
671
672 /* Tell the history routines what is going on. */
673 start_using_history ();
674
675 /* Make the display buffer match the state of the line. */
676 rl_reset_line_state ();
677
678 /* No such function typed yet. */
679 rl_last_func = (Function *)NULL;
680
681 /* Parsing of key-bindings begins in an enabled state. */
682 _rl_parsing_conditionalized_out = 0;
683
684#if defined (VI_MODE)
685 if (rl_editing_mode == vi_mode)
686 _rl_vi_initialize_line ();
687#endif
688
689 return 0;
690}
691
692#if defined (__EMX__)
693static void
694_emx_build_environ ()
695{
696 TIB *tibp;
697 PIB *pibp;
698 char *t, **tp;
699 int c;
700
701 DosGetInfoBlocks (&tibp, &pibp);
702 t = pibp->pib_pchenv;
703 for (c = 1; *t; c++)
704 t += strlen (t) + 1;
705 tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
706 t = pibp->pib_pchenv;
707 while (*t)
708 {
709 *tp++ = t;
710 t += strlen (t) + 1;
711 }
712 *tp = 0;
713}
714#endif /* __EMX__ */
715
716/* Initialize the entire state of the world. */
717static void
718readline_initialize_everything ()
719{
720#if defined (__EMX__)
721 if (environ == 0)
722 _emx_build_environ ();
723#endif
724
725 /* Find out if we are running in Emacs. */
726 running_in_emacs = get_env_value ("EMACS") != (char *)0;
727
728 /* Set up input and output if they are not already set up. */
729 if (!rl_instream)
730 rl_instream = stdin;
731
732 if (!rl_outstream)
733 rl_outstream = stdout;
734
735 /* Bind _rl_in_stream and _rl_out_stream immediately. These values
736 may change, but they may also be used before readline_internal ()
737 is called. */
738 _rl_in_stream = rl_instream;
739 _rl_out_stream = rl_outstream;
740
741 /* Allocate data structures. */
742 if (rl_line_buffer == 0)
743 rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
744
745 /* Initialize the terminal interface. */
746 _rl_init_terminal_io ((char *)NULL);
747
748#if !defined (__GO32__)
749 /* Bind tty characters to readline functions. */
750 readline_default_bindings ();
751#endif /* !__GO32__ */
752
753 /* Initialize the function names. */
754 rl_initialize_funmap ();
755
756 /* Decide whether we should automatically go into eight-bit mode. */
757 _rl_init_eightbit ();
758
759 /* Read in the init file. */
760 rl_read_init_file ((char *)NULL);
761
762 /* XXX */
763 if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
764 {
765 screenwidth--;
766 screenchars -= screenheight;
767 }
768
769 /* Override the effect of any `set keymap' assignments in the
770 inputrc file. */
771 rl_set_keymap_from_edit_mode ();
772
773 /* Try to bind a common arrow key prefix, if not already bound. */
774 bind_arrow_keys ();
775
776 /* Enable the meta key, if this terminal has one. */
777 if (_rl_enable_meta)
778 _rl_enable_meta_key ();
779
780 /* If the completion parser's default word break characters haven't
781 been set yet, then do so now. */
782 if (rl_completer_word_break_characters == (char *)NULL)
783 rl_completer_word_break_characters = rl_basic_word_break_characters;
784}
785
786/* If this system allows us to look at the values of the regular
787 input editing characters, then bind them to their readline
788 equivalents, iff the characters are not bound to keymaps. */
789static void
790readline_default_bindings ()
791{
792 rltty_set_default_bindings (_rl_keymap);
793}
794
795static void
796bind_arrow_keys_internal ()
797{
798 Function *f;
799
800 f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
801 if (!f || f == rl_do_lowercase_version)
802 {
803 _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
804 _rl_bind_if_unbound ("\033[B", rl_get_next_history);
805 _rl_bind_if_unbound ("\033[C", rl_forward);
806 _rl_bind_if_unbound ("\033[D", rl_backward);
807 }
808
809 f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
810 if (!f || f == rl_do_lowercase_version)
811 {
812 _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
813 _rl_bind_if_unbound ("\033OB", rl_get_next_history);
814 _rl_bind_if_unbound ("\033OC", rl_forward);
815 _rl_bind_if_unbound ("\033OD", rl_backward);
816 }
817}
818
819/* Try and bind the common arrow key prefix after giving termcap and
820 the inputrc file a chance to bind them and create `real' keymaps
821 for the arrow key prefix. */
822static void
823bind_arrow_keys ()
824{
825 Keymap xkeymap;
826
827 xkeymap = _rl_keymap;
828
829 _rl_keymap = emacs_standard_keymap;
830 bind_arrow_keys_internal ();
831
832#if defined (VI_MODE)
833 _rl_keymap = vi_movement_keymap;
834 bind_arrow_keys_internal ();
835#endif
836
837 _rl_keymap = xkeymap;
838}
839
840\f
841/* **************************************************************** */
842/* */
843/* Numeric Arguments */
844/* */
845/* **************************************************************** */
846
847/* Handle C-u style numeric args, as well as M--, and M-digits. */
848static int
849rl_digit_loop ()
850{
851 int key, c, sawminus, sawdigits;
852
c862e87b 853 rl_save_prompt ();
d60d9f65
SS
854
855 sawminus = sawdigits = 0;
856 while (1)
857 {
c862e87b
JM
858 if (rl_numeric_arg > 1000000)
859 {
860 sawdigits = rl_explicit_arg = rl_numeric_arg = 0;
861 ding ();
862 rl_restore_prompt ();
863 rl_clear_message ();
864 return 1;
865 }
d60d9f65
SS
866 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
867 key = c = rl_read_key ();
868
869 /* If we see a key bound to `universal-argument' after seeing digits,
870 it ends the argument but is otherwise ignored. */
871 if (_rl_keymap[c].type == ISFUNC &&
872 _rl_keymap[c].function == rl_universal_argument)
873 {
874 if (sawdigits == 0)
875 {
876 rl_numeric_arg *= 4;
877 continue;
878 }
879 else
880 {
881 key = rl_read_key ();
c862e87b 882 rl_restore_prompt ();
d60d9f65
SS
883 rl_clear_message ();
884 return (_rl_dispatch (key, _rl_keymap));
885 }
886 }
887
888 c = UNMETA (c);
889
890 if (_rl_digit_p (c))
891 {
892 rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
893 sawdigits = rl_explicit_arg = 1;
894 }
895 else if (c == '-' && rl_explicit_arg == 0)
896 {
897 rl_numeric_arg = sawminus = 1;
898 rl_arg_sign = -1;
899 }
900 else
901 {
902 /* Make M-- command equivalent to M--1 command. */
903 if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
904 rl_explicit_arg = 1;
c862e87b 905 rl_restore_prompt ();
d60d9f65
SS
906 rl_clear_message ();
907 return (_rl_dispatch (key, _rl_keymap));
908 }
909 }
910
911 return 0;
912}
913
914/* Add the current digit to the argument in progress. */
915int
916rl_digit_argument (ignore, key)
917 int ignore, key;
918{
919 rl_pending_input = key;
920 return (rl_digit_loop ());
921}
922
923/* What to do when you abort reading an argument. */
924int
925rl_discard_argument ()
926{
927 ding ();
928 rl_clear_message ();
929 _rl_init_argument ();
930 return 0;
931}
932
933/* Create a default argument. */
934int
935_rl_init_argument ()
936{
937 rl_numeric_arg = rl_arg_sign = 1;
938 rl_explicit_arg = 0;
939 return 0;
940}
941
942/* C-u, universal argument. Multiply the current argument by 4.
943 Read a key. If the key has nothing to do with arguments, then
944 dispatch on it. If the key is the abort character then abort. */
945int
946rl_universal_argument (count, key)
947 int count, key;
948{
949 rl_numeric_arg *= 4;
950 return (rl_digit_loop ());
951}
952
953/* **************************************************************** */
954/* */
955/* Insert and Delete */
956/* */
957/* **************************************************************** */
958
959/* Insert a string of text into the line at point. This is the only
960 way that you should do insertion. rl_insert () calls this
961 function. */
962int
963rl_insert_text (string)
964 char *string;
965{
966 register int i, l = strlen (string);
967
968 if (rl_end + l >= rl_line_buffer_len)
969 rl_extend_line_buffer (rl_end + l);
970
971 for (i = rl_end; i >= rl_point; i--)
972 the_line[i + l] = the_line[i];
973 strncpy (the_line + rl_point, string, l);
974
975 /* Remember how to undo this if we aren't undoing something. */
976 if (!_rl_doing_an_undo)
977 {
978 /* If possible and desirable, concatenate the undos. */
979 if ((l == 1) &&
980 rl_undo_list &&
981 (rl_undo_list->what == UNDO_INSERT) &&
982 (rl_undo_list->end == rl_point) &&
983 (rl_undo_list->end - rl_undo_list->start < 20))
984 rl_undo_list->end++;
985 else
986 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
987 }
988 rl_point += l;
989 rl_end += l;
990 the_line[rl_end] = '\0';
991 return l;
992}
993
994/* Delete the string between FROM and TO. FROM is
995 inclusive, TO is not. */
996int
997rl_delete_text (from, to)
998 int from, to;
999{
1000 register char *text;
1001 register int diff, i;
1002
1003 /* Fix it if the caller is confused. */
1004 if (from > to)
1005 SWAP (from, to);
1006
1007 /* fix boundaries */
1008 if (to > rl_end)
1009 {
1010 to = rl_end;
1011 if (from > to)
1012 from = to;
1013 }
1014
1015 text = rl_copy_text (from, to);
1016
1017 /* Some versions of strncpy() can't handle overlapping arguments. */
1018 diff = to - from;
1019 for (i = from; i < rl_end - diff; i++)
1020 the_line[i] = the_line[i + diff];
1021
1022 /* Remember how to undo this delete. */
1023 if (_rl_doing_an_undo == 0)
1024 rl_add_undo (UNDO_DELETE, from, to, text);
1025 else
1026 free (text);
1027
1028 rl_end -= diff;
1029 the_line[rl_end] = '\0';
1030 return (diff);
1031}
1032
1033/* Fix up point so that it is within the line boundaries after killing
1034 text. If FIX_MARK_TOO is non-zero, the mark is forced within line
1035 boundaries also. */
1036
1037#define _RL_FIX_POINT(x) \
1038 do { \
1039 if (x > rl_end) \
1040 x = rl_end; \
1041 else if (x < 0) \
1042 x = 0; \
1043 } while (0)
1044
1045void
1046_rl_fix_point (fix_mark_too)
1047 int fix_mark_too;
1048{
1049 _RL_FIX_POINT (rl_point);
1050 if (fix_mark_too)
1051 _RL_FIX_POINT (rl_mark);
1052}
1053#undef _RL_FIX_POINT
1054
1055void
1056_rl_replace_text (text, start, end)
1057 char *text;
1058 int start, end;
1059{
1060 rl_begin_undo_group ();
1061 rl_delete_text (start, end + 1);
1062 rl_point = start;
1063 rl_insert_text (text);
1064 rl_end_undo_group ();
1065}
1066
1067/* **************************************************************** */
1068/* */
1069/* Readline character functions */
1070/* */
1071/* **************************************************************** */
1072
1073/* This is not a gap editor, just a stupid line input routine. No hair
1074 is involved in writing any of the functions, and none should be. */
1075
1076/* Note that:
1077
1078 rl_end is the place in the string that we would place '\0';
1079 i.e., it is always safe to place '\0' there.
1080
1081 rl_point is the place in the string where the cursor is. Sometimes
1082 this is the same as rl_end.
1083
1084 Any command that is called interactively receives two arguments.
1085 The first is a count: the numeric arg pased to this command.
1086 The second is the key which invoked this command.
1087*/
1088
1089/* **************************************************************** */
1090/* */
1091/* Movement Commands */
1092/* */
1093/* **************************************************************** */
1094
1095/* Note that if you `optimize' the display for these functions, you cannot
1096 use said functions in other functions which do not do optimizing display.
1097 I.e., you will have to update the data base for rl_redisplay, and you
1098 might as well let rl_redisplay do that job. */
1099
1100/* Move forward COUNT characters. */
1101int
1102rl_forward (count, key)
1103 int count, key;
1104{
1105 if (count < 0)
1106 rl_backward (-count, key);
1107 else if (count > 0)
1108 {
1109 int end = rl_point + count;
1110#if defined (VI_MODE)
1111 int lend = rl_end - (rl_editing_mode == vi_mode);
1112#else
1113 int lend = rl_end;
1114#endif
1115
1116 if (end > lend)
1117 {
1118 rl_point = lend;
1119 ding ();
1120 }
1121 else
1122 rl_point = end;
1123 }
1124 return 0;
1125}
1126
1127/* Move backward COUNT characters. */
1128int
1129rl_backward (count, key)
1130 int count, key;
1131{
1132 if (count < 0)
1133 rl_forward (-count, key);
1134 else if (count > 0)
1135 {
1136 if (rl_point < count)
1137 {
1138 rl_point = 0;
1139 ding ();
1140 }
1141 else
1142 rl_point -= count;
1143 }
1144 return 0;
1145}
1146
1147/* Move to the beginning of the line. */
1148int
1149rl_beg_of_line (count, key)
1150 int count, key;
1151{
1152 rl_point = 0;
1153 return 0;
1154}
1155
1156/* Move to the end of the line. */
1157int
1158rl_end_of_line (count, key)
1159 int count, key;
1160{
1161 rl_point = rl_end;
1162 return 0;
1163}
1164
1165/* Move forward a word. We do what Emacs does. */
1166int
1167rl_forward_word (count, key)
1168 int count, key;
1169{
1170 int c;
1171
1172 if (count < 0)
1173 {
1174 rl_backward_word (-count, key);
1175 return 0;
1176 }
1177
1178 while (count)
1179 {
1180 if (rl_point == rl_end)
1181 return 0;
1182
1183 /* If we are not in a word, move forward until we are in one.
1184 Then, move forward until we hit a non-alphabetic character. */
1185 c = the_line[rl_point];
1186 if (alphabetic (c) == 0)
1187 {
1188 while (++rl_point < rl_end)
1189 {
1190 c = the_line[rl_point];
1191 if (alphabetic (c))
1192 break;
1193 }
1194 }
1195 if (rl_point == rl_end)
1196 return 0;
1197 while (++rl_point < rl_end)
1198 {
1199 c = the_line[rl_point];
1200 if (alphabetic (c) == 0)
1201 break;
1202 }
1203 --count;
1204 }
1205 return 0;
1206}
1207
1208/* Move backward a word. We do what Emacs does. */
1209int
1210rl_backward_word (count, key)
1211 int count, key;
1212{
1213 int c;
1214
1215 if (count < 0)
1216 {
1217 rl_forward_word (-count, key);
1218 return 0;
1219 }
1220
1221 while (count)
1222 {
1223 if (!rl_point)
1224 return 0;
1225
1226 /* Like rl_forward_word (), except that we look at the characters
1227 just before point. */
1228
1229 c = the_line[rl_point - 1];
1230 if (alphabetic (c) == 0)
1231 {
1232 while (--rl_point)
1233 {
1234 c = the_line[rl_point - 1];
1235 if (alphabetic (c))
1236 break;
1237 }
1238 }
1239
1240 while (rl_point)
1241 {
1242 c = the_line[rl_point - 1];
1243 if (alphabetic (c) == 0)
1244 break;
1245 else
1246 --rl_point;
1247 }
1248 --count;
1249 }
1250 return 0;
1251}
1252
1253/* Clear the current line. Numeric argument to C-l does this. */
1254int
c862e87b
JM
1255rl_refresh_line (ignore1, ignore2)
1256 int ignore1, ignore2;
d60d9f65
SS
1257{
1258 int curr_line, nleft;
1259
1260 /* Find out whether or not there might be invisible characters in the
1261 editing buffer. */
1262 if (rl_display_prompt == rl_prompt)
1263 nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1264 else
1265 nleft = _rl_last_c_pos - screenwidth;
1266
1267 if (nleft > 0)
1268 curr_line = 1 + nleft / screenwidth;
1269 else
1270 curr_line = 0;
1271
1272 _rl_move_vert (curr_line);
1273 _rl_move_cursor_relative (0, the_line); /* XXX is this right */
1274
1275#if defined (__GO32__)
1276 {
1277 int row, col, width, row_start;
1278
1279 ScreenGetCursor (&row, &col);
1280 width = ScreenCols ();
1281 row_start = ScreenPrimary + (row * width);
1282 memset (row_start + col, 0, (width - col) * 2);
1283 }
1284#else /* !__GO32__ */
1285 _rl_clear_to_eol (0); /* arg of 0 means to not use spaces */
1286#endif /* !__GO32__ */
1287
1288 rl_forced_update_display ();
1289 rl_display_fixed = 1;
1290
1291 return 0;
1292}
1293
1294/* C-l typed to a line without quoting clears the screen, and then reprints
1295 the prompt and the current input line. Given a numeric arg, redraw only
1296 the current line. */
1297int
1298rl_clear_screen (count, key)
1299 int count, key;
1300{
1301 if (rl_explicit_arg)
1302 {
c862e87b 1303 rl_refresh_line (count, key);
d60d9f65
SS
1304 return 0;
1305 }
1306
1307 _rl_clear_screen (); /* calls termcap function to clear screen */
1308 rl_forced_update_display ();
1309 rl_display_fixed = 1;
1310
1311 return 0;
1312}
1313
1314int
1315rl_arrow_keys (count, c)
1316 int count, c;
1317{
1318 int ch;
1319
1320 ch = rl_read_key ();
1321
1322 switch (_rl_to_upper (ch))
1323 {
1324 case 'A':
1325 rl_get_previous_history (count, ch);
1326 break;
1327
1328 case 'B':
1329 rl_get_next_history (count, ch);
1330 break;
1331
1332 case 'C':
1333 rl_forward (count, ch);
1334 break;
1335
1336 case 'D':
1337 rl_backward (count, ch);
1338 break;
1339
1340 default:
1341 ding ();
1342 }
1343 return 0;
1344}
1345
1346\f
1347/* **************************************************************** */
1348/* */
1349/* Text commands */
1350/* */
1351/* **************************************************************** */
1352
1353/* Insert the character C at the current location, moving point forward. */
1354int
1355rl_insert (count, c)
1356 int count, c;
1357{
1358 register int i;
1359 char *string;
1360
1361 if (count <= 0)
1362 return 0;
1363
1364 /* If we can optimize, then do it. But don't let people crash
1365 readline because of extra large arguments. */
1366 if (count > 1 && count <= 1024)
1367 {
1368 string = xmalloc (1 + count);
1369
1370 for (i = 0; i < count; i++)
1371 string[i] = c;
1372
1373 string[i] = '\0';
1374 rl_insert_text (string);
1375 free (string);
1376
1377 return 0;
1378 }
1379
1380 if (count > 1024)
1381 {
1382 int decreaser;
1383 char str[1024+1];
1384
1385 for (i = 0; i < 1024; i++)
1386 str[i] = c;
1387
1388 while (count)
1389 {
1390 decreaser = (count > 1024 ? 1024 : count);
1391 str[decreaser] = '\0';
1392 rl_insert_text (str);
1393 count -= decreaser;
1394 }
1395
1396 return 0;
1397 }
1398
1399 /* We are inserting a single character.
1400 If there is pending input, then make a string of all of the
1401 pending characters that are bound to rl_insert, and insert
1402 them all. */
1403 if (_rl_any_typein ())
1404 _rl_insert_typein (c);
1405 else
1406 {
1407 /* Inserting a single character. */
1408 char str[2];
1409
1410 str[1] = '\0';
1411 str[0] = c;
1412 rl_insert_text (str);
1413 }
1414 return 0;
1415}
1416
1417/* Insert the next typed character verbatim. */
1418int
1419rl_quoted_insert (count, key)
1420 int count, key;
1421{
1422 int c;
1423
1424 c = rl_read_key ();
1425 return (rl_insert (count, c));
1426}
1427
1428/* Insert a tab character. */
1429int
1430rl_tab_insert (count, key)
1431 int count, key;
1432{
1433 return (rl_insert (count, '\t'));
1434}
1435
1436/* What to do when a NEWLINE is pressed. We accept the whole line.
1437 KEY is the key that invoked this command. I guess it could have
1438 meaning in the future. */
1439int
1440rl_newline (count, key)
1441 int count, key;
1442{
1443 rl_done = 1;
1444
1445#if defined (VI_MODE)
1446 if (rl_editing_mode == vi_mode)
1447 {
1448 _rl_vi_done_inserting ();
1449 _rl_vi_reset_last ();
1450 }
1451#endif /* VI_MODE */
1452
c862e87b
JM
1453 /* If we've been asked to erase empty lines, suppress the final update,
1454 since _rl_update_final calls crlf(). */
1455 if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
1456 return 0;
1457
d60d9f65
SS
1458 if (readline_echoing_p)
1459 _rl_update_final ();
1460 return 0;
1461}
1462
1463/* What to do for some uppercase characters, like meta characters,
1464 and some characters appearing in emacs_ctlx_keymap. This function
1465 is just a stub, you bind keys to it and the code in _rl_dispatch ()
1466 is special cased. */
1467int
1468rl_do_lowercase_version (ignore1, ignore2)
1469 int ignore1, ignore2;
1470{
1471 return 0;
1472}
1473
1474/* Rubout the character behind point. */
1475int
1476rl_rubout (count, key)
1477 int count, key;
1478{
1479 if (count < 0)
1480 {
1481 rl_delete (-count, key);
1482 return 0;
1483 }
1484
1485 if (!rl_point)
1486 {
1487 ding ();
1488 return -1;
1489 }
1490
1491 if (count > 1 || rl_explicit_arg)
1492 {
1493 int orig_point = rl_point;
1494 rl_backward (count, key);
1495 rl_kill_text (orig_point, rl_point);
1496 }
1497 else
1498 {
1499 int c = the_line[--rl_point];
1500 rl_delete_text (rl_point, rl_point + 1);
1501
1502 if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
1503 {
1504 int l;
1505 l = rl_character_len (c, rl_point);
1506 _rl_erase_at_end_of_line (l);
1507 }
1508 }
1509 return 0;
1510}
1511
1512/* Delete the character under the cursor. Given a numeric argument,
1513 kill that many characters instead. */
1514int
1515rl_delete (count, key)
1516 int count, key;
1517{
1518 if (count < 0)
1519 return (rl_rubout (-count, key));
1520
1521 if (rl_point == rl_end)
1522 {
1523 ding ();
1524 return -1;
1525 }
1526
1527 if (count > 1 || rl_explicit_arg)
1528 {
1529 int orig_point = rl_point;
1530 rl_forward (count, key);
1531 rl_kill_text (orig_point, rl_point);
1532 rl_point = orig_point;
1533 return 0;
1534 }
1535 else
1536 return (rl_delete_text (rl_point, rl_point + 1));
d60d9f65
SS
1537}
1538
c862e87b
JM
1539/* Delete the character under the cursor, unless the insertion
1540 point is at the end of the line, in which case the character
1541 behind the cursor is deleted. COUNT is obeyed and may be used
1542 to delete forward or backward that many characters. */
1543int
1544rl_rubout_or_delete (count, key)
1545 int count, key;
1546{
1547 if (rl_end != 0 && rl_point == rl_end)
1548 return (rl_rubout (count, key));
1549 else
1550 return (rl_delete (count, key));
1551}
1552
d60d9f65
SS
1553/* Delete all spaces and tabs around point. */
1554int
1555rl_delete_horizontal_space (count, ignore)
1556 int count, ignore;
1557{
1558 int start = rl_point;
1559
1560 while (rl_point && whitespace (the_line[rl_point - 1]))
1561 rl_point--;
1562
1563 start = rl_point;
1564
1565 while (rl_point < rl_end && whitespace (the_line[rl_point]))
1566 rl_point++;
1567
1568 if (start != rl_point)
1569 {
1570 rl_delete_text (start, rl_point);
1571 rl_point = start;
1572 }
1573 return 0;
1574}
1575
c862e87b
JM
1576/* Like the tcsh editing function delete-char-or-list. The eof character
1577 is caught before this is invoked, so this really does the same thing as
1578 delete-char-or-list-or-eof, as long as it's bound to the eof character. */
1579int
1580rl_delete_or_show_completions (count, key)
1581 int count, key;
1582{
1583 if (rl_end != 0 && rl_point == rl_end)
1584 return (rl_possible_completions (count, key));
1585 else
1586 return (rl_delete (count, key));
1587}
1588
d60d9f65
SS
1589#ifndef RL_COMMENT_BEGIN_DEFAULT
1590#define RL_COMMENT_BEGIN_DEFAULT "#"
1591#endif
1592
1593/* Turn the current line into a comment in shell history.
1594 A K*rn shell style function. */
1595int
1596rl_insert_comment (count, key)
1597 int count, key;
1598{
1599 rl_beg_of_line (1, key);
1600 rl_insert_text (_rl_comment_begin ? _rl_comment_begin
1601 : RL_COMMENT_BEGIN_DEFAULT);
1602 (*rl_redisplay_function) ();
1603 rl_newline (1, '\n');
1604 return (0);
1605}
1606
1607/* **************************************************************** */
1608/* */
1609/* Changing Case */
1610/* */
1611/* **************************************************************** */
1612
1613/* The three kinds of things that we know how to do. */
1614#define UpCase 1
1615#define DownCase 2
1616#define CapCase 3
1617
1618static int rl_change_case ();
1619
1620/* Uppercase the word at point. */
1621int
1622rl_upcase_word (count, key)
1623 int count, key;
1624{
1625 return (rl_change_case (count, UpCase));
1626}
1627
1628/* Lowercase the word at point. */
1629int
1630rl_downcase_word (count, key)
1631 int count, key;
1632{
1633 return (rl_change_case (count, DownCase));
1634}
1635
1636/* Upcase the first letter, downcase the rest. */
1637int
1638rl_capitalize_word (count, key)
1639 int count, key;
1640{
1641 return (rl_change_case (count, CapCase));
1642}
1643
1644/* The meaty function.
1645 Change the case of COUNT words, performing OP on them.
1646 OP is one of UpCase, DownCase, or CapCase.
1647 If a negative argument is given, leave point where it started,
1648 otherwise, leave it where it moves to. */
1649static int
1650rl_change_case (count, op)
1651 int count, op;
1652{
1653 register int start, end;
1654 int inword, c;
1655
1656 start = rl_point;
1657 rl_forward_word (count, 0);
1658 end = rl_point;
1659
1660 if (count < 0)
1661 SWAP (start, end);
1662
1663 /* We are going to modify some text, so let's prepare to undo it. */
1664 rl_modifying (start, end);
1665
1666 for (inword = 0; start < end; start++)
1667 {
1668 c = the_line[start];
1669 switch (op)
1670 {
1671 case UpCase:
1672 the_line[start] = _rl_to_upper (c);
1673 break;
1674
1675 case DownCase:
1676 the_line[start] = _rl_to_lower (c);
1677 break;
1678
1679 case CapCase:
1680 the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
1681 inword = alphabetic (the_line[start]);
1682 break;
1683
1684 default:
1685 ding ();
1686 return -1;
1687 }
1688 }
1689 rl_point = end;
1690 return 0;
1691}
1692
1693/* **************************************************************** */
1694/* */
1695/* Transposition */
1696/* */
1697/* **************************************************************** */
1698
1699/* Transpose the words at point. */
1700int
1701rl_transpose_words (count, key)
1702 int count, key;
1703{
1704 char *word1, *word2;
1705 int w1_beg, w1_end, w2_beg, w2_end;
1706 int orig_point = rl_point;
1707
1708 if (!count)
1709 return 0;
1710
1711 /* Find the two words. */
1712 rl_forward_word (count, key);
1713 w2_end = rl_point;
1714 rl_backward_word (1, key);
1715 w2_beg = rl_point;
1716 rl_backward_word (count, key);
1717 w1_beg = rl_point;
1718 rl_forward_word (1, key);
1719 w1_end = rl_point;
1720
1721 /* Do some check to make sure that there really are two words. */
1722 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
1723 {
1724 ding ();
1725 rl_point = orig_point;
1726 return -1;
1727 }
1728
1729 /* Get the text of the words. */
1730 word1 = rl_copy_text (w1_beg, w1_end);
1731 word2 = rl_copy_text (w2_beg, w2_end);
1732
1733 /* We are about to do many insertions and deletions. Remember them
1734 as one operation. */
1735 rl_begin_undo_group ();
1736
1737 /* Do the stuff at word2 first, so that we don't have to worry
1738 about word1 moving. */
1739 rl_point = w2_beg;
1740 rl_delete_text (w2_beg, w2_end);
1741 rl_insert_text (word1);
1742
1743 rl_point = w1_beg;
1744 rl_delete_text (w1_beg, w1_end);
1745 rl_insert_text (word2);
1746
1747 /* This is exactly correct since the text before this point has not
1748 changed in length. */
1749 rl_point = w2_end;
1750
1751 /* I think that does it. */
1752 rl_end_undo_group ();
1753 free (word1);
1754 free (word2);
1755
1756 return 0;
1757}
1758
1759/* Transpose the characters at point. If point is at the end of the line,
1760 then transpose the characters before point. */
1761int
1762rl_transpose_chars (count, key)
1763 int count, key;
1764{
1765 char dummy[2];
1766
1767 if (!count)
1768 return 0;
1769
1770 if (!rl_point || rl_end < 2)
1771 {
1772 ding ();
1773 return -1;
1774 }
1775
1776 rl_begin_undo_group ();
1777
1778 if (rl_point == rl_end)
1779 {
1780 --rl_point;
1781 count = 1;
1782 }
1783 rl_point--;
1784
1785 dummy[0] = the_line[rl_point];
1786 dummy[1] = '\0';
1787
1788 rl_delete_text (rl_point, rl_point + 1);
1789
1790 rl_point += count;
1791 _rl_fix_point (0);
1792 rl_insert_text (dummy);
1793
1794 rl_end_undo_group ();
1795 return 0;
1796}
1797
1798/* **************************************************************** */
1799/* */
1800/* Character Searching */
1801/* */
1802/* **************************************************************** */
1803
1804int
1805_rl_char_search_internal (count, dir, schar)
1806 int count, dir, schar;
1807{
1808 int pos, inc;
1809
1810 pos = rl_point;
1811 inc = (dir < 0) ? -1 : 1;
1812 while (count)
1813 {
1814 if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
1815 {
1816 ding ();
1817 return -1;
1818 }
1819
1820 pos += inc;
1821 do
1822 {
1823 if (rl_line_buffer[pos] == schar)
1824 {
1825 count--;
1826 if (dir < 0)
1827 rl_point = (dir == BTO) ? pos + 1 : pos;
1828 else
1829 rl_point = (dir == FTO) ? pos - 1 : pos;
1830 break;
1831 }
1832 }
1833 while ((dir < 0) ? pos-- : ++pos < rl_end);
1834 }
1835 return (0);
1836}
1837
1838/* Search COUNT times for a character read from the current input stream.
1839 FDIR is the direction to search if COUNT is non-negative; otherwise
1840 the search goes in BDIR. */
1841static int
1842_rl_char_search (count, fdir, bdir)
1843 int count, fdir, bdir;
1844{
1845 int c;
1846
1847 c = rl_read_key ();
1848 if (count < 0)
1849 return (_rl_char_search_internal (-count, bdir, c));
1850 else
1851 return (_rl_char_search_internal (count, fdir, c));
1852}
1853
1854int
1855rl_char_search (count, key)
1856 int count, key;
1857{
1858 return (_rl_char_search (count, FFIND, BFIND));
1859}
1860
1861int
1862rl_backward_char_search (count, key)
1863 int count, key;
1864{
1865 return (_rl_char_search (count, BFIND, FFIND));
1866}
1867
1868/* **************************************************************** */
1869/* */
1870/* History Utilities */
1871/* */
1872/* **************************************************************** */
1873
1874/* We already have a history library, and that is what we use to control
1875 the history features of readline. This is our local interface to
1876 the history mechanism. */
1877
1878/* While we are editing the history, this is the saved
1879 version of the original line. */
1880HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
1881
1882/* Set the history pointer back to the last entry in the history. */
1883static void
1884start_using_history ()
1885{
1886 using_history ();
1887 if (saved_line_for_history)
1888 _rl_free_history_entry (saved_line_for_history);
1889
1890 saved_line_for_history = (HIST_ENTRY *)NULL;
1891}
1892
1893/* Free the contents (and containing structure) of a HIST_ENTRY. */
1894void
1895_rl_free_history_entry (entry)
1896 HIST_ENTRY *entry;
1897{
1898 if (entry == 0)
1899 return;
1900 if (entry->line)
1901 free (entry->line);
1902 free (entry);
1903}
1904
1905/* Perhaps put back the current line if it has changed. */
1906int
1907maybe_replace_line ()
1908{
1909 HIST_ENTRY *temp;
1910
1911 temp = current_history ();
1912 /* If the current line has changed, save the changes. */
1913 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
1914 {
c862e87b 1915 temp = replace_history_entry (where_history (), the_line, (histdata_t)rl_undo_list);
d60d9f65
SS
1916 free (temp->line);
1917 free (temp);
1918 }
1919 return 0;
1920}
1921
1922/* Put back the saved_line_for_history if there is one. */
1923int
1924maybe_unsave_line ()
1925{
1926 int line_len;
1927
1928 if (saved_line_for_history)
1929 {
1930 line_len = strlen (saved_line_for_history->line);
1931
1932 if (line_len >= rl_line_buffer_len)
1933 rl_extend_line_buffer (line_len);
1934
1935 strcpy (the_line, saved_line_for_history->line);
1936 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
1937 _rl_free_history_entry (saved_line_for_history);
1938 saved_line_for_history = (HIST_ENTRY *)NULL;
1939 rl_end = rl_point = strlen (the_line);
1940 }
1941 else
1942 ding ();
1943 return 0;
1944}
1945
1946/* Save the current line in saved_line_for_history. */
1947int
1948maybe_save_line ()
1949{
1950 if (saved_line_for_history == 0)
1951 {
1952 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
1953 saved_line_for_history->line = savestring (the_line);
1954 saved_line_for_history->data = (char *)rl_undo_list;
1955 }
1956 return 0;
1957}
1958
1959/* **************************************************************** */
1960/* */
1961/* History Commands */
1962/* */
1963/* **************************************************************** */
1964
1965/* Meta-< goes to the start of the history. */
1966int
1967rl_beginning_of_history (count, key)
1968 int count, key;
1969{
1970 return (rl_get_previous_history (1 + where_history (), key));
1971}
1972
1973/* Meta-> goes to the end of the history. (The current line). */
1974int
1975rl_end_of_history (count, key)
1976 int count, key;
1977{
1978 maybe_replace_line ();
1979 using_history ();
1980 maybe_unsave_line ();
1981 return 0;
1982}
1983
1984/* Move down to the next history line. */
1985int
1986rl_get_next_history (count, key)
1987 int count, key;
1988{
1989 HIST_ENTRY *temp;
1990 int line_len;
1991
1992 if (count < 0)
1993 return (rl_get_previous_history (-count, key));
1994
1995 if (count == 0)
1996 return 0;
1997
1998 maybe_replace_line ();
1999
2000 temp = (HIST_ENTRY *)NULL;
2001 while (count)
2002 {
2003 temp = next_history ();
2004 if (!temp)
2005 break;
2006 --count;
2007 }
2008
2009 if (temp == 0)
2010 maybe_unsave_line ();
2011 else
2012 {
2013 line_len = strlen (temp->line);
2014
2015 if (line_len >= rl_line_buffer_len)
2016 rl_extend_line_buffer (line_len);
2017
2018 strcpy (the_line, temp->line);
2019 rl_undo_list = (UNDO_LIST *)temp->data;
2020 rl_end = rl_point = strlen (the_line);
2021#if defined (VI_MODE)
2022 if (rl_editing_mode == vi_mode)
2023 rl_point = 0;
2024#endif /* VI_MODE */
2025 }
2026 return 0;
2027}
2028
2029/* Get the previous item out of our interactive history, making it the current
2030 line. If there is no previous history, just ding. */
2031int
2032rl_get_previous_history (count, key)
2033 int count, key;
2034{
2035 HIST_ENTRY *old_temp, *temp;
2036 int line_len;
2037
2038 if (count < 0)
2039 return (rl_get_next_history (-count, key));
2040
2041 if (count == 0)
2042 return 0;
2043
2044 /* If we don't have a line saved, then save this one. */
2045 maybe_save_line ();
2046
2047 /* If the current line has changed, save the changes. */
2048 maybe_replace_line ();
2049
2050 temp = old_temp = (HIST_ENTRY *)NULL;
2051 while (count)
2052 {
2053 temp = previous_history ();
2054 if (temp == 0)
2055 break;
2056
2057 old_temp = temp;
2058 --count;
2059 }
2060
2061 /* If there was a large argument, and we moved back to the start of the
2062 history, that is not an error. So use the last value found. */
2063 if (!temp && old_temp)
2064 temp = old_temp;
2065
2066 if (temp == 0)
2067 ding ();
2068 else
2069 {
2070 line_len = strlen (temp->line);
2071
2072 if (line_len >= rl_line_buffer_len)
2073 rl_extend_line_buffer (line_len);
2074
2075 strcpy (the_line, temp->line);
2076 rl_undo_list = (UNDO_LIST *)temp->data;
2077 rl_end = rl_point = line_len;
2078
2079#if defined (VI_MODE)
2080 if (rl_editing_mode == vi_mode)
2081 rl_point = 0;
2082#endif /* VI_MODE */
2083 }
2084 return 0;
2085}
2086
2087/* **************************************************************** */
2088/* */
2089/* The Mark and the Region. */
2090/* */
2091/* **************************************************************** */
2092
2093/* Set the mark at POSITION. */
2094int
2095_rl_set_mark_at_pos (position)
2096 int position;
2097{
2098 if (position > rl_end)
2099 return -1;
2100
2101 rl_mark = position;
2102 return 0;
2103}
2104
2105/* A bindable command to set the mark. */
2106int
2107rl_set_mark (count, key)
2108 int count, key;
2109{
2110 return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
2111}
2112
2113/* Exchange the position of mark and point. */
2114int
2115rl_exchange_point_and_mark (count, key)
2116 int count, key;
2117{
2118 if (rl_mark > rl_end)
2119 rl_mark = -1;
2120
2121 if (rl_mark == -1)
2122 {
2123 ding ();
2124 return -1;
2125 }
2126 else
2127 SWAP (rl_point, rl_mark);
2128
2129 return 0;
2130}
2131
2132/* **************************************************************** */
2133/* */
2134/* Editing Modes */
2135/* */
2136/* **************************************************************** */
2137/* How to toggle back and forth between editing modes. */
2138int
2139rl_vi_editing_mode (count, key)
2140 int count, key;
2141{
2142#if defined (VI_MODE)
2143 rl_editing_mode = vi_mode;
2144 rl_vi_insertion_mode (1, key);
2145#endif /* VI_MODE */
2146 return 0;
2147}
2148
2149int
2150rl_emacs_editing_mode (count, key)
2151 int count, key;
2152{
2153 rl_editing_mode = emacs_mode;
2154 _rl_keymap = emacs_standard_keymap;
2155 return 0;
2156}
This page took 0.105545 seconds and 4 git commands to generate.