121308Sache/* readline.c -- a general facility for reading lines of input
221308Sache   with emacs style editing and completion. */
321308Sache
4157184Sache/* Copyright (C) 1987-2005 Free Software Foundation, Inc.
521308Sache
621308Sache   This file is part of the GNU Readline Library, a library for
721308Sache   reading lines of text with interactive input and history editing.
821308Sache
921308Sache   The GNU Readline Library is free software; you can redistribute it
1021308Sache   and/or modify it under the terms of the GNU General Public License
1158310Sache   as published by the Free Software Foundation; either version 2, or
1221308Sache   (at your option) any later version.
1321308Sache
1421308Sache   The GNU Readline Library is distributed in the hope that it will be
1521308Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1621308Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1721308Sache   GNU General Public License for more details.
1821308Sache
1921308Sache   The GNU General Public License is often shipped with GNU software, and
2021308Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
2121308Sache   have a copy of the license, write to the Free Software Foundation,
2258310Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
2321308Sache#define READLINE_LIBRARY
2421308Sache
2521308Sache#if defined (HAVE_CONFIG_H)
2621308Sache#  include <config.h>
2721308Sache#endif
2821308Sache
2921308Sache#include <sys/types.h>
3021308Sache#include "posixstat.h"
3121308Sache#include <fcntl.h>
3221308Sache#if defined (HAVE_SYS_FILE_H)
3321308Sache#  include <sys/file.h>
3421308Sache#endif /* HAVE_SYS_FILE_H */
3521308Sache
3621308Sache#if defined (HAVE_UNISTD_H)
3721308Sache#  include <unistd.h>
3821308Sache#endif /* HAVE_UNISTD_H */
3921308Sache
4021308Sache#if defined (HAVE_STDLIB_H)
4121308Sache#  include <stdlib.h>
4221308Sache#else
4321308Sache#  include "ansi_stdlib.h"
4421308Sache#endif /* HAVE_STDLIB_H */
4521308Sache
4621308Sache#if defined (HAVE_LOCALE_H)
4721308Sache#  include <locale.h>
4821308Sache#endif
4921308Sache
5021308Sache#include <stdio.h>
5126497Sache#include "posixjmp.h"
52165670Sache#include <errno.h>
5321308Sache
54165670Sache#if !defined (errno)
55165670Sacheextern int errno;
56165670Sache#endif /* !errno */
57165670Sache
5821308Sache/* System-specific feature definitions and include files. */
5921308Sache#include "rldefs.h"
60119610Sache#include "rlmbutil.h"
6121308Sache
6226497Sache#if defined (__EMX__)
6326497Sache#  define INCL_DOSPROCESS
6426497Sache#  include <os2.h>
6526497Sache#endif /* __EMX__ */
6621308Sache
6721308Sache/* Some standard library routines. */
6821308Sache#include "readline.h"
6921308Sache#include "history.h"
7021308Sache
7158310Sache#include "rlprivate.h"
7258310Sache#include "rlshell.h"
7358310Sache#include "xmalloc.h"
7458310Sache
7526497Sache#ifndef RL_LIBRARY_VERSION
76157184Sache#  define RL_LIBRARY_VERSION "5.1"
7726497Sache#endif
7826497Sache
79119610Sache#ifndef RL_READLINE_VERSION
80157184Sache#  define RL_READLINE_VERSION	0x0501
81119610Sache#endif
8221308Sache
83119610Sacheextern void _rl_free_history_entry PARAMS((HIST_ENTRY *));
84119610Sache
8521308Sache/* Forward declarations used in this file. */
86119610Sachestatic char *readline_internal PARAMS((void));
87119610Sachestatic void readline_initialize_everything PARAMS((void));
8821308Sache
89119610Sachestatic void bind_arrow_keys_internal PARAMS((Keymap));
90119610Sachestatic void bind_arrow_keys PARAMS((void));
9121308Sache
92119610Sachestatic void readline_default_bindings PARAMS((void));
93136644Sachestatic void reset_default_bindings PARAMS((void));
9421308Sache
95157184Sachestatic int _rl_subseq_result PARAMS((int, Keymap, int, int));
96157184Sachestatic int _rl_subseq_getchar PARAMS((int));
97157184Sache
9821308Sache/* **************************************************************** */
9921308Sache/*								    */
10021308Sache/*			Line editing input utility		    */
10121308Sache/*								    */
10221308Sache/* **************************************************************** */
10321308Sache
10475406Sacheconst char *rl_library_version = RL_LIBRARY_VERSION;
10521308Sache
106119610Sacheint rl_readline_version = RL_READLINE_VERSION;
107119610Sache
10875406Sache/* True if this is `real' readline as opposed to some stub substitute. */
10958310Sacheint rl_gnu_readline_p = 1;
11058310Sache
11121308Sache/* A pointer to the keymap that is currently in use.
11221308Sache   By default, it is the standard emacs keymap. */
11321308SacheKeymap _rl_keymap = emacs_standard_keymap;
11421308Sache
115157184Sache
11621308Sache/* The current style of editing. */
11721308Sacheint rl_editing_mode = emacs_mode;
11821308Sache
119119610Sache/* The current insert mode:  input (the default) or overwrite */
120119610Sacheint rl_insert_mode = RL_IM_DEFAULT;
121119610Sache
12226497Sache/* Non-zero if we called this function from _rl_dispatch().  It's present
12326497Sache   so functions can find out whether they were called from a key binding
12426497Sache   or directly from an application. */
12526497Sacheint rl_dispatching;
12626497Sache
12721308Sache/* Non-zero if the previous command was a kill command. */
12821308Sacheint _rl_last_command_was_kill = 0;
12921308Sache
13021308Sache/* The current value of the numeric argument specified by the user. */
13121308Sacheint rl_numeric_arg = 1;
13221308Sache
13321308Sache/* Non-zero if an argument was typed. */
13421308Sacheint rl_explicit_arg = 0;
13521308Sache
13621308Sache/* Temporary value used while generating the argument. */
13721308Sacheint rl_arg_sign = 1;
13821308Sache
13921308Sache/* Non-zero means we have been called at least once before. */
14021308Sachestatic int rl_initialized;
14121308Sache
14275406Sache#if 0
14321308Sache/* If non-zero, this program is running in an EMACS buffer. */
14421308Sachestatic int running_in_emacs;
14575406Sache#endif
14621308Sache
14775406Sache/* Flags word encapsulating the current readline state. */
14875406Sacheint rl_readline_state = RL_STATE_NONE;
14975406Sache
15021308Sache/* The current offset in the current input line. */
15121308Sacheint rl_point;
15221308Sache
15321308Sache/* Mark in the current input line. */
15421308Sacheint rl_mark;
15521308Sache
15621308Sache/* Length of the current input line. */
15721308Sacheint rl_end;
15821308Sache
15921308Sache/* Make this non-zero to return the current input_line. */
16021308Sacheint rl_done;
16121308Sache
16221308Sache/* The last function executed by readline. */
16375406Sacherl_command_func_t *rl_last_func = (rl_command_func_t *)NULL;
16421308Sache
16521308Sache/* Top level environment for readline_internal (). */
16626497Sacheprocenv_t readline_top_level;
16721308Sache
16821308Sache/* The streams we interact with. */
16921308SacheFILE *_rl_in_stream, *_rl_out_stream;
17021308Sache
17121308Sache/* The names of the streams that we do input and output to. */
17221308SacheFILE *rl_instream = (FILE *)NULL;
17321308SacheFILE *rl_outstream = (FILE *)NULL;
17421308Sache
175119610Sache/* Non-zero means echo characters as they are read.  Defaults to no echo;
176119610Sache   set to 1 if there is a controlling terminal, we can get its attributes,
177119610Sache   and the attributes include `echo'.  Look at rltty.c:prepare_terminal_settings
178119610Sache   for the code that sets it. */
179119610Sacheint readline_echoing_p = 0;
18021308Sache
18121308Sache/* Current prompt. */
18275406Sachechar *rl_prompt = (char *)NULL;
18321308Sacheint rl_visible_prompt_length = 0;
18421308Sache
18558310Sache/* Set to non-zero by calling application if it has already printed rl_prompt
18658310Sache   and does not want readline to do it the first time. */
18758310Sacheint rl_already_prompted = 0;
18858310Sache
18921308Sache/* The number of characters read in order to type this complete command. */
19021308Sacheint rl_key_sequence_length = 0;
19121308Sache
19221308Sache/* If non-zero, then this is the address of a function to call just
19347558Sache   before readline_internal_setup () prints the first prompt. */
19475406Sacherl_hook_func_t *rl_startup_hook = (rl_hook_func_t *)NULL;
19521308Sache
19647558Sache/* If non-zero, this is the address of a function to call just before
19747558Sache   readline_internal_setup () returns and readline_internal starts
19847558Sache   reading input characters. */
19975406Sacherl_hook_func_t *rl_pre_input_hook = (rl_hook_func_t *)NULL;
20047558Sache
20121308Sache/* What we use internally.  You should always refer to RL_LINE_BUFFER. */
20221308Sachestatic char *the_line;
20321308Sache
20421308Sache/* The character that can generate an EOF.  Really read from
20521308Sache   the terminal driver... just defaulted here. */
20621308Sacheint _rl_eof_char = CTRL ('D');
20721308Sache
20821308Sache/* Non-zero makes this the next keystroke to read. */
20921308Sacheint rl_pending_input = 0;
21021308Sache
21121308Sache/* Pointer to a useful terminal name. */
21275406Sacheconst char *rl_terminal_name = (const char *)NULL;
21321308Sache
21421308Sache/* Non-zero means to always use horizontal scrolling in line display. */
21521308Sacheint _rl_horizontal_scroll_mode = 0;
21621308Sache
21721308Sache/* Non-zero means to display an asterisk at the starts of history lines
21821308Sache   which have been modified. */
21921308Sacheint _rl_mark_modified_lines = 0;
22021308Sache
22121308Sache/* The style of `bell' notification preferred.  This can be set to NO_BELL,
22221308Sache   AUDIBLE_BELL, or VISIBLE_BELL. */
22321308Sacheint _rl_bell_preference = AUDIBLE_BELL;
22421308Sache
22521308Sache/* String inserted into the line by rl_insert_comment (). */
22621308Sachechar *_rl_comment_begin;
22721308Sache
22821308Sache/* Keymap holding the function currently being executed. */
22921308SacheKeymap rl_executing_keymap;
23021308Sache
231157184Sache/* Keymap we're currently using to dispatch. */
232157184SacheKeymap _rl_dispatching_keymap;
233157184Sache
23447558Sache/* Non-zero means to erase entire line, including prompt, on empty input lines. */
23547558Sacheint rl_erase_empty_line = 0;
23647558Sache
23758310Sache/* Non-zero means to read only this many characters rather than up to a
23858310Sache   character bound to accept-line. */
23958310Sacheint rl_num_chars_to_read;
24058310Sache
24121308Sache/* Line buffer and maintenence. */
24221308Sachechar *rl_line_buffer = (char *)NULL;
24321308Sacheint rl_line_buffer_len = 0;
24421308Sache
245157184Sache/* Key sequence `contexts' */
246157184Sache_rl_keyseq_cxt *_rl_kscxt = 0;
247157184Sache
248119610Sache/* Forward declarations used by the display, termcap, and history code. */
24921308Sache
25021308Sache/* **************************************************************** */
25121308Sache/*								    */
25221308Sache/*			`Forward' declarations  		    */
25321308Sache/*								    */
25421308Sache/* **************************************************************** */
25521308Sache
25621308Sache/* Non-zero means do not parse any lines other than comments and
25721308Sache   parser directives. */
25821308Sacheunsigned char _rl_parsing_conditionalized_out = 0;
25921308Sache
26021308Sache/* Non-zero means to convert characters with the meta bit set to
26121308Sache   escape-prefixed characters so we can indirect through
26221308Sache   emacs_meta_keymap or vi_escape_keymap. */
26321308Sacheint _rl_convert_meta_chars_to_ascii = 1;
26421308Sache
26521308Sache/* Non-zero means to output characters with the meta bit set directly
26621308Sache   rather than as a meta-prefixed escape sequence. */
26721308Sacheint _rl_output_meta_chars = 0;
26821308Sache
269157184Sache/* Non-zero means to look at the termios special characters and bind
270157184Sache   them to equivalent readline functions at startup. */
271157184Sacheint _rl_bind_stty_chars = 1;
272157184Sache
27321308Sache/* **************************************************************** */
27421308Sache/*								    */
27521308Sache/*			Top Level Functions			    */
27621308Sache/*								    */
27721308Sache/* **************************************************************** */
27821308Sache
27921308Sache/* Non-zero means treat 0200 bit in terminal input as Meta bit. */
28021308Sacheint _rl_meta_flag = 0;	/* Forward declaration */
28121308Sache
28275406Sache/* Set up the prompt and expand it.  Called from readline() and
28375406Sache   rl_callback_handler_install (). */
28475406Sacheint
28575406Sacherl_set_prompt (prompt)
28675406Sache     const char *prompt;
28775406Sache{
28875406Sache  FREE (rl_prompt);
28975406Sache  rl_prompt = prompt ? savestring (prompt) : (char *)NULL;
290157191Sache  rl_display_prompt = rl_prompt ? rl_prompt : "";
29175406Sache
292119610Sache  rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
29375406Sache  return 0;
29475406Sache}
29575406Sache
29621308Sache/* Read a line of input.  Prompt with PROMPT.  An empty PROMPT means
29721308Sache   none.  A return value of NULL means that EOF was encountered. */
29821308Sachechar *
29921308Sachereadline (prompt)
30075406Sache     const char *prompt;
30121308Sache{
30221308Sache  char *value;
30321308Sache
30421308Sache  /* If we are at EOF return a NULL string. */
30521308Sache  if (rl_pending_input == EOF)
30621308Sache    {
30775406Sache      rl_clear_pending_input ();
30821308Sache      return ((char *)NULL);
30921308Sache    }
31021308Sache
31175406Sache  rl_set_prompt (prompt);
31221308Sache
31321308Sache  rl_initialize ();
314157184Sache  if (rl_prep_term_function)
315157184Sache    (*rl_prep_term_function) (_rl_meta_flag);
31621308Sache
31721308Sache#if defined (HANDLE_SIGNALS)
31821308Sache  rl_set_signals ();
31921308Sache#endif
32021308Sache
32121308Sache  value = readline_internal ();
322157184Sache  if (rl_deprep_term_function)
323157184Sache    (*rl_deprep_term_function) ();
32421308Sache
32521308Sache#if defined (HANDLE_SIGNALS)
32621308Sache  rl_clear_signals ();
32721308Sache#endif
32821308Sache
32921308Sache  return (value);
33021308Sache}
33121308Sache
33221308Sache#if defined (READLINE_CALLBACKS)
33321308Sache#  define STATIC_CALLBACK
33421308Sache#else
33521308Sache#  define STATIC_CALLBACK static
33621308Sache#endif
33721308Sache
33821308SacheSTATIC_CALLBACK void
33921308Sachereadline_internal_setup ()
34021308Sache{
34158310Sache  char *nprompt;
34258310Sache
34321308Sache  _rl_in_stream = rl_instream;
34421308Sache  _rl_out_stream = rl_outstream;
34521308Sache
34621308Sache  if (rl_startup_hook)
34721308Sache    (*rl_startup_hook) ();
34821308Sache
349119610Sache  /* If we're not echoing, we still want to at least print a prompt, because
350119610Sache     rl_redisplay will not do it for us.  If the calling application has a
351119610Sache     custom redisplay function, though, let that function handle it. */
352119610Sache  if (readline_echoing_p == 0 && rl_redisplay_function == rl_redisplay)
35321308Sache    {
35458310Sache      if (rl_prompt && rl_already_prompted == 0)
35521308Sache	{
35658310Sache	  nprompt = _rl_strip_prompt (rl_prompt);
35758310Sache	  fprintf (_rl_out_stream, "%s", nprompt);
35821308Sache	  fflush (_rl_out_stream);
35958310Sache	  free (nprompt);
36021308Sache	}
36121308Sache    }
36221308Sache  else
36321308Sache    {
36458310Sache      if (rl_prompt && rl_already_prompted)
36558310Sache	rl_on_new_line_with_prompt ();
36658310Sache      else
36758310Sache	rl_on_new_line ();
36821308Sache      (*rl_redisplay_function) ();
369119610Sache    }
370119610Sache
37121308Sache#if defined (VI_MODE)
372119610Sache  if (rl_editing_mode == vi_mode)
373136644Sache    rl_vi_insertion_mode (1, 'i');
37421308Sache#endif /* VI_MODE */
37547558Sache
37647558Sache  if (rl_pre_input_hook)
37747558Sache    (*rl_pre_input_hook) ();
37821308Sache}
37921308Sache
38021308SacheSTATIC_CALLBACK char *
38121308Sachereadline_internal_teardown (eof)
38221308Sache     int eof;
38321308Sache{
38421308Sache  char *temp;
38521308Sache  HIST_ENTRY *entry;
38621308Sache
38721308Sache  /* Restore the original of this history line, iff the line that we
38821308Sache     are editing was originally in the history, AND the line has changed. */
38921308Sache  entry = current_history ();
39021308Sache
39121308Sache  if (entry && rl_undo_list)
39221308Sache    {
39321308Sache      temp = savestring (the_line);
39421308Sache      rl_revert_line (1, 0);
39547558Sache      entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
39621308Sache      _rl_free_history_entry (entry);
39721308Sache
39821308Sache      strcpy (the_line, temp);
39921308Sache      free (temp);
40021308Sache    }
40121308Sache
40221308Sache  /* At any rate, it is highly likely that this line has an undo list.  Get
40321308Sache     rid of it now. */
40421308Sache  if (rl_undo_list)
40575406Sache    rl_free_undo_list ();
40621308Sache
407119610Sache  /* Restore normal cursor, if available. */
408119610Sache  _rl_set_insert_mode (RL_IM_INSERT, 0);
409119610Sache
41021308Sache  return (eof ? (char *)NULL : savestring (the_line));
41121308Sache}
41221308Sache
413157184Sachevoid
414157184Sache_rl_internal_char_cleanup ()
415157184Sache{
416157184Sache#if defined (VI_MODE)
417157184Sache  /* In vi mode, when you exit insert mode, the cursor moves back
418157184Sache     over the previous character.  We explicitly check for that here. */
419157184Sache  if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
420157184Sache    rl_vi_check ();
421157184Sache#endif /* VI_MODE */
422157184Sache
423157184Sache  if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
424157184Sache    {
425157184Sache      (*rl_redisplay_function) ();
426157184Sache      _rl_want_redisplay = 0;
427157184Sache      rl_newline (1, '\n');
428157184Sache    }
429157184Sache
430157184Sache  if (rl_done == 0)
431157184Sache    {
432157184Sache      (*rl_redisplay_function) ();
433157184Sache      _rl_want_redisplay = 0;
434157184Sache    }
435157184Sache
436157184Sache  /* If the application writer has told us to erase the entire line if
437157184Sache     the only character typed was something bound to rl_newline, do so. */
438157184Sache  if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
439157184Sache      rl_point == 0 && rl_end == 0)
440157184Sache    _rl_erase_entire_line ();
441157184Sache}
442157184Sache
44321308SacheSTATIC_CALLBACK int
44421308Sache#if defined (READLINE_CALLBACKS)
44521308Sachereadline_internal_char ()
44621308Sache#else
44721308Sachereadline_internal_charloop ()
44821308Sache#endif
44921308Sache{
45021308Sache  static int lastc, eof_found;
45121308Sache  int c, code, lk;
45221308Sache
45321308Sache  lastc = -1;
45421308Sache  eof_found = 0;
45521308Sache
45621308Sache#if !defined (READLINE_CALLBACKS)
45721308Sache  while (rl_done == 0)
45821308Sache    {
45921308Sache#endif
46021308Sache      lk = _rl_last_command_was_kill;
46121308Sache
46221308Sache      code = setjmp (readline_top_level);
46321308Sache
46421308Sache      if (code)
465157184Sache	{
466157184Sache	  (*rl_redisplay_function) ();
467157184Sache	  _rl_want_redisplay = 0;
468157184Sache	  /* If we get here, we're not being called from something dispatched
469157184Sache	     from _rl_callback_read_char(), which sets up its own value of
470157184Sache	     readline_top_level (saving and restoring the old, of course), so
471157184Sache	     we can just return here. */
472157184Sache	  if (RL_ISSTATE (RL_STATE_CALLBACK))
473157184Sache	    return (0);
474157184Sache	}
47521308Sache
47621308Sache      if (rl_pending_input == 0)
47721308Sache	{
47821308Sache	  /* Then initialize the argument and number of keys read. */
479157184Sache	  _rl_reset_argument ();
48021308Sache	  rl_key_sequence_length = 0;
48121308Sache	}
48221308Sache
48375406Sache      RL_SETSTATE(RL_STATE_READCMD);
48421308Sache      c = rl_read_key ();
48575406Sache      RL_UNSETSTATE(RL_STATE_READCMD);
48621308Sache
487165670Sache      /* look at input.c:rl_getc() for the circumstances under which this will
488165670Sache	 be returned; punt immediately on read error without converting it to
489165670Sache	 a newline. */
490165670Sache      if (c == READERR)
491165670Sache	{
492165670Sache#if defined (READLINE_CALLBACKS)
493165670Sache	  RL_SETSTATE(RL_STATE_DONE);
494165670Sache	  return (rl_done = 1);
495165670Sache#else
496165670Sache	  eof_found = 1;
497165670Sache	  break;
498165670Sache#endif
499165670Sache	}
500165670Sache
50121308Sache      /* EOF typed to a non-blank line is a <NL>. */
50221308Sache      if (c == EOF && rl_end)
50321308Sache	c = NEWLINE;
50421308Sache
50521308Sache      /* The character _rl_eof_char typed to blank line, and not as the
50621308Sache	 previous character is interpreted as EOF. */
50721308Sache      if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
50821308Sache	{
50921308Sache#if defined (READLINE_CALLBACKS)
51075406Sache	  RL_SETSTATE(RL_STATE_DONE);
51121308Sache	  return (rl_done = 1);
51221308Sache#else
51321308Sache	  eof_found = 1;
51421308Sache	  break;
51521308Sache#endif
51621308Sache	}
51721308Sache
51821308Sache      lastc = c;
51958310Sache      _rl_dispatch ((unsigned char)c, _rl_keymap);
52021308Sache
52121308Sache      /* If there was no change in _rl_last_command_was_kill, then no kill
52221308Sache	 has taken place.  Note that if input is pending we are reading
52321308Sache	 a prefix command, so nothing has changed yet. */
52421308Sache      if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
52521308Sache	_rl_last_command_was_kill = 0;
52621308Sache
527157184Sache      _rl_internal_char_cleanup ();
52821308Sache
52921308Sache#if defined (READLINE_CALLBACKS)
53021308Sache      return 0;
53121308Sache#else
53221308Sache    }
53321308Sache
53421308Sache  return (eof_found);
53521308Sache#endif
53621308Sache}
53721308Sache
53821308Sache#if defined (READLINE_CALLBACKS)
53921308Sachestatic int
54021308Sachereadline_internal_charloop ()
54121308Sache{
54247558Sache  int eof = 1;
54321308Sache
54421308Sache  while (rl_done == 0)
54521308Sache    eof = readline_internal_char ();
54621308Sache  return (eof);
54721308Sache}
54821308Sache#endif /* READLINE_CALLBACKS */
54921308Sache
55021308Sache/* Read a line of input from the global rl_instream, doing output on
55121308Sache   the global rl_outstream.
55221308Sache   If rl_prompt is non-null, then that is our prompt. */
55321308Sachestatic char *
55421308Sachereadline_internal ()
55521308Sache{
55621308Sache  int eof;
55721308Sache
55821308Sache  readline_internal_setup ();
55921308Sache  eof = readline_internal_charloop ();
56021308Sache  return (readline_internal_teardown (eof));
56121308Sache}
56221308Sache
56321308Sachevoid
56426497Sache_rl_init_line_state ()
56526497Sache{
566119610Sache  rl_point = rl_end = rl_mark = 0;
56726497Sache  the_line = rl_line_buffer;
56826497Sache  the_line[0] = 0;
56926497Sache}
57026497Sache
57126497Sachevoid
57221308Sache_rl_set_the_line ()
57321308Sache{
57421308Sache  the_line = rl_line_buffer;
57521308Sache}
57621308Sache
577157184Sache#if defined (READLINE_CALLBACKS)
578157184Sache_rl_keyseq_cxt *
579157184Sache_rl_keyseq_cxt_alloc ()
580157184Sache{
581157184Sache  _rl_keyseq_cxt *cxt;
582157184Sache
583157184Sache  cxt = (_rl_keyseq_cxt *)xmalloc (sizeof (_rl_keyseq_cxt));
584157184Sache
585157184Sache  cxt->flags = cxt->subseq_arg = cxt->subseq_retval = 0;
586157184Sache
587157184Sache  cxt->okey = 0;
588157184Sache  cxt->ocxt = _rl_kscxt;
589157184Sache  cxt->childval = 42;		/* sentinel value */
590157184Sache
591157184Sache  return cxt;
592157184Sache}
593157184Sache
594157184Sachevoid
595157184Sache_rl_keyseq_cxt_dispose (cxt)
596157184Sache    _rl_keyseq_cxt *cxt;
597157184Sache{
598157184Sache  free (cxt);
599157184Sache}
600157184Sache
601157184Sachevoid
602157184Sache_rl_keyseq_chain_dispose ()
603157184Sache{
604157184Sache  _rl_keyseq_cxt *cxt;
605157184Sache
606157184Sache  while (_rl_kscxt)
607157184Sache    {
608157184Sache      cxt = _rl_kscxt;
609157184Sache      _rl_kscxt = _rl_kscxt->ocxt;
610157184Sache      _rl_keyseq_cxt_dispose (cxt);
611157184Sache    }
612157184Sache}
613157184Sache#endif
614157184Sache
615157184Sachestatic int
616157184Sache_rl_subseq_getchar (key)
617157184Sache     int key;
618157184Sache{
619157184Sache  int k;
620157184Sache
621157184Sache  if (key == ESC)
622157184Sache    RL_SETSTATE(RL_STATE_METANEXT);
623157184Sache  RL_SETSTATE(RL_STATE_MOREINPUT);
624157184Sache  k = rl_read_key ();
625157184Sache  RL_UNSETSTATE(RL_STATE_MOREINPUT);
626157184Sache  if (key == ESC)
627157184Sache    RL_UNSETSTATE(RL_STATE_METANEXT);
628157184Sache
629157184Sache  return k;
630157184Sache}
631157184Sache
632157184Sache#if defined (READLINE_CALLBACKS)
633157184Sacheint
634157184Sache_rl_dispatch_callback (cxt)
635157184Sache     _rl_keyseq_cxt *cxt;
636157184Sache{
637157184Sache  int nkey, r;
638157184Sache
639157184Sache  /* For now */
640157184Sache#if 1
641157184Sache  /* The first time this context is used, we want to read input and dispatch
642157184Sache     on it.  When traversing the chain of contexts back `up', we want to use
643157184Sache     the value from the next context down.  We're simulating recursion using
644157184Sache     a chain of contexts. */
645157184Sache  if ((cxt->flags & KSEQ_DISPATCHED) == 0)
646157184Sache    {
647157184Sache      nkey = _rl_subseq_getchar (cxt->okey);
648173403Sache      if (nkey < 0)
649173403Sache	{
650173403Sache	  _rl_abort_internal ();
651173403Sache	  return -1;
652173403Sache	}
653157184Sache      r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
654157184Sache      cxt->flags |= KSEQ_DISPATCHED;
655157184Sache    }
656157184Sache  else
657157184Sache    r = cxt->childval;
658157184Sache#else
659157184Sache  r = _rl_dispatch_subseq (nkey, cxt->dmap, cxt->subseq_arg);
660157184Sache#endif
661157184Sache
662157184Sache  /* For now */
663157184Sache  r = _rl_subseq_result (r, cxt->oldmap, cxt->okey, (cxt->flags & KSEQ_SUBSEQ));
664157184Sache
665157184Sache  if (r == 0)			/* success! */
666157184Sache    {
667157184Sache      _rl_keyseq_chain_dispose ();
668157184Sache      RL_UNSETSTATE (RL_STATE_MULTIKEY);
669157184Sache      return r;
670157184Sache    }
671157184Sache
672157184Sache  if (r != -3)			/* magic value that says we added to the chain */
673157184Sache    _rl_kscxt = cxt->ocxt;
674157184Sache  if (_rl_kscxt)
675157184Sache    _rl_kscxt->childval = r;
676157184Sache  if (r != -3)
677157184Sache    _rl_keyseq_cxt_dispose (cxt);
678157184Sache
679157184Sache  return r;
680157184Sache}
681157184Sache#endif /* READLINE_CALLBACKS */
682157184Sache
68321308Sache/* Do the command associated with KEY in MAP.
68421308Sache   If the associated command is really a keymap, then read
68521308Sache   another key, and dispatch into that map. */
68621308Sacheint
68721308Sache_rl_dispatch (key, map)
68821308Sache     register int key;
68921308Sache     Keymap map;
69021308Sache{
691157184Sache  _rl_dispatching_keymap = map;
692119610Sache  return _rl_dispatch_subseq (key, map, 0);
693119610Sache}
694119610Sache
695119610Sacheint
696119610Sache_rl_dispatch_subseq (key, map, got_subseq)
697119610Sache     register int key;
698119610Sache     Keymap map;
699119610Sache     int got_subseq;
700119610Sache{
70121308Sache  int r, newkey;
70221308Sache  char *macro;
70375406Sache  rl_command_func_t *func;
704157184Sache#if defined (READLINE_CALLBACKS)
705157184Sache  _rl_keyseq_cxt *cxt;
706157184Sache#endif
70721308Sache
70821308Sache  if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
70921308Sache    {
71021308Sache      if (map[ESC].type == ISKMAP)
71121308Sache	{
712119610Sache	  if (RL_ISSTATE (RL_STATE_MACRODEF))
71321308Sache	    _rl_add_macro_char (ESC);
71421308Sache	  map = FUNCTION_TO_KEYMAP (map, ESC);
71521308Sache	  key = UNMETA (key);
71621308Sache	  rl_key_sequence_length += 2;
71721308Sache	  return (_rl_dispatch (key, map));
71821308Sache	}
71921308Sache      else
72075406Sache	rl_ding ();
72121308Sache      return 0;
72221308Sache    }
72321308Sache
724119610Sache  if (RL_ISSTATE (RL_STATE_MACRODEF))
72521308Sache    _rl_add_macro_char (key);
72621308Sache
72721308Sache  r = 0;
72821308Sache  switch (map[key].type)
72921308Sache    {
73021308Sache    case ISFUNC:
73121308Sache      func = map[key].function;
73275406Sache      if (func)
73321308Sache	{
73421308Sache	  /* Special case rl_do_lowercase_version (). */
73521308Sache	  if (func == rl_do_lowercase_version)
73621308Sache	    return (_rl_dispatch (_rl_to_lower (key), map));
73721308Sache
73821308Sache	  rl_executing_keymap = map;
73921308Sache
74026497Sache	  rl_dispatching = 1;
74175406Sache	  RL_SETSTATE(RL_STATE_DISPATCHING);
742157191Sache	  (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
74375406Sache	  RL_UNSETSTATE(RL_STATE_DISPATCHING);
74426497Sache	  rl_dispatching = 0;
74521308Sache
74621308Sache	  /* If we have input pending, then the last command was a prefix
74721308Sache	     command.  Don't change the state of rl_last_func.  Otherwise,
74821308Sache	     remember the last command executed in this variable. */
74975406Sache	  if (rl_pending_input == 0 && map[key].function != rl_digit_argument)
75021308Sache	    rl_last_func = map[key].function;
75121308Sache	}
752119610Sache      else if (map[ANYOTHERKEY].function)
753119610Sache	{
754119610Sache	  /* OK, there's no function bound in this map, but there is a
755119610Sache	     shadow function that was overridden when the current keymap
756119610Sache	     was created.  Return -2 to note  that. */
757119610Sache	  _rl_unget_char  (key);
758119610Sache	  return -2;
759119610Sache	}
760119610Sache      else if (got_subseq)
761119610Sache	{
762119610Sache	  /* Return -1 to note that we're in a subsequence, but  we don't
763119610Sache	     have a matching key, nor was one overridden.  This means
764119610Sache	     we need to back up the recursion chain and find the last
765119610Sache	     subsequence that is bound to a function. */
766119610Sache	  _rl_unget_char (key);
767119610Sache	  return -1;
768119610Sache	}
76921308Sache      else
77021308Sache	{
771157184Sache#if defined (READLINE_CALLBACKS)
772157184Sache	  RL_UNSETSTATE (RL_STATE_MULTIKEY);
773157184Sache	  _rl_keyseq_chain_dispose ();
774157184Sache#endif
77521308Sache	  _rl_abort_internal ();
77621308Sache	  return -1;
77721308Sache	}
77821308Sache      break;
77921308Sache
78021308Sache    case ISKMAP:
78175406Sache      if (map[key].function != 0)
78221308Sache	{
783119610Sache#if defined (VI_MODE)
784119610Sache	  /* The only way this test will be true is if a subsequence has been
785119610Sache	     bound starting with ESC, generally the arrow keys.  What we do is
786119610Sache	     check whether there's input in the queue, which there generally
787119610Sache	     will be if an arrow key has been pressed, and, if there's not,
788119610Sache	     just dispatch to (what we assume is) rl_vi_movement_mode right
789119610Sache	     away.  This is essentially an input test with a zero timeout. */
790119610Sache	  if (rl_editing_mode == vi_mode && key == ESC && map == vi_insertion_keymap
791119610Sache	      && _rl_input_queued (0) == 0)
792119610Sache	    return (_rl_dispatch (ANYOTHERKEY, FUNCTION_TO_KEYMAP (map, key)));
793119610Sache#endif
794119610Sache
79521308Sache	  rl_key_sequence_length++;
796157184Sache	  _rl_dispatching_keymap = FUNCTION_TO_KEYMAP (map, key);
79775406Sache
798157184Sache	  /* Allocate new context here.  Use linked contexts (linked through
799157184Sache	     cxt->ocxt) to simulate recursion */
800157184Sache#if defined (READLINE_CALLBACKS)
801157184Sache	  if (RL_ISSTATE (RL_STATE_CALLBACK))
802157184Sache	    {
803157184Sache	      /* Return 0 only the first time, to indicate success to
804157184Sache		 _rl_callback_read_char.  The rest of the time, we're called
805157184Sache		 from _rl_dispatch_callback, so we return 3 to indicate
806157184Sache		 special handling is necessary. */
807157184Sache	      r = RL_ISSTATE (RL_STATE_MULTIKEY) ? -3 : 0;
808157184Sache	      cxt = _rl_keyseq_cxt_alloc ();
80975406Sache
810157184Sache	      if (got_subseq)
811157184Sache		cxt->flags |= KSEQ_SUBSEQ;
812157184Sache	      cxt->okey = key;
813157184Sache	      cxt->oldmap = map;
814157184Sache	      cxt->dmap = _rl_dispatching_keymap;
815157184Sache	      cxt->subseq_arg = got_subseq || cxt->dmap[ANYOTHERKEY].function;
816157184Sache
817157184Sache	      RL_SETSTATE (RL_STATE_MULTIKEY);
818157184Sache	      _rl_kscxt = cxt;
819157184Sache
820157184Sache	      return r;		/* don't indicate immediate success */
821157184Sache	    }
822157184Sache#endif
823157184Sache
824157184Sache	  newkey = _rl_subseq_getchar (key);
825119610Sache	  if (newkey < 0)
826119610Sache	    {
827119610Sache	      _rl_abort_internal ();
828119610Sache	      return -1;
829119610Sache	    }
830119610Sache
831157184Sache	  r = _rl_dispatch_subseq (newkey, _rl_dispatching_keymap, got_subseq || map[ANYOTHERKEY].function);
832157184Sache	  return _rl_subseq_result (r, map, key, got_subseq);
83321308Sache	}
83421308Sache      else
83521308Sache	{
83621308Sache	  _rl_abort_internal ();
83721308Sache	  return -1;
83821308Sache	}
83921308Sache      break;
84021308Sache
84121308Sache    case ISMACR:
84275406Sache      if (map[key].function != 0)
84321308Sache	{
84421308Sache	  macro = savestring ((char *)map[key].function);
84521308Sache	  _rl_with_macro_input (macro);
84621308Sache	  return 0;
84721308Sache	}
84821308Sache      break;
84921308Sache    }
85021308Sache#if defined (VI_MODE)
85121308Sache  if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
852125759Sache      key != ANYOTHERKEY &&
85321308Sache      _rl_vi_textmod_command (key))
85421308Sache    _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
85521308Sache#endif
856157184Sache
85721308Sache  return (r);
85821308Sache}
85921308Sache
860157184Sachestatic int
861157184Sache_rl_subseq_result (r, map, key, got_subseq)
862157184Sache     int r;
863157184Sache     Keymap map;
864157184Sache     int key, got_subseq;
865157184Sache{
866157184Sache  Keymap m;
867157184Sache  int type, nt;
868157184Sache  rl_command_func_t *func, *nf;
869157184Sache
870157184Sache  if (r == -2)
871157184Sache    /* We didn't match anything, and the keymap we're indexed into
872157184Sache       shadowed a function previously bound to that prefix.  Call
873157184Sache       the function.  The recursive call to _rl_dispatch_subseq has
874157184Sache       already taken care of pushing any necessary input back onto
875157184Sache       the input queue with _rl_unget_char. */
876157184Sache    {
877157184Sache      m = _rl_dispatching_keymap;
878157184Sache      type = m[ANYOTHERKEY].type;
879157184Sache      func = m[ANYOTHERKEY].function;
880157184Sache      if (type == ISFUNC && func == rl_do_lowercase_version)
881157184Sache	r = _rl_dispatch (_rl_to_lower (key), map);
882157184Sache      else if (type == ISFUNC && func == rl_insert)
883157184Sache	{
884157184Sache	  /* If the function that was shadowed was self-insert, we
885157184Sache	     somehow need a keymap with map[key].func == self-insert.
886157184Sache	     Let's use this one. */
887157184Sache	  nt = m[key].type;
888157184Sache	  nf = m[key].function;
889157184Sache
890157184Sache	  m[key].type = type;
891157184Sache	  m[key].function = func;
892157184Sache	  r = _rl_dispatch (key, m);
893157184Sache	  m[key].type = nt;
894157184Sache	  m[key].function = nf;
895157184Sache	}
896157184Sache      else
897157184Sache	r = _rl_dispatch (ANYOTHERKEY, m);
898157184Sache    }
899157184Sache  else if (r && map[ANYOTHERKEY].function)
900157184Sache    {
901157184Sache      /* We didn't match (r is probably -1), so return something to
902157184Sache	 tell the caller that it should try ANYOTHERKEY for an
903157184Sache	 overridden function. */
904157184Sache      _rl_unget_char (key);
905157184Sache      _rl_dispatching_keymap = map;
906157184Sache      return -2;
907157184Sache    }
908157184Sache  else if (r && got_subseq)
909157184Sache    {
910157184Sache      /* OK, back up the chain. */
911157184Sache      _rl_unget_char (key);
912157184Sache      _rl_dispatching_keymap = map;
913157184Sache      return -1;
914157184Sache    }
915157184Sache
916157184Sache  return r;
917157184Sache}
918157184Sache
91921308Sache/* **************************************************************** */
92021308Sache/*								    */
92121308Sache/*			Initializations 			    */
92221308Sache/*								    */
92321308Sache/* **************************************************************** */
92421308Sache
92526497Sache/* Initialize readline (and terminal if not already). */
92621308Sacheint
92721308Sacherl_initialize ()
92821308Sache{
92921308Sache  /* If we have never been called before, initialize the
93021308Sache     terminal and data structures. */
93121308Sache  if (!rl_initialized)
93221308Sache    {
93375406Sache      RL_SETSTATE(RL_STATE_INITIALIZING);
93421308Sache      readline_initialize_everything ();
93575406Sache      RL_UNSETSTATE(RL_STATE_INITIALIZING);
93621308Sache      rl_initialized++;
93775406Sache      RL_SETSTATE(RL_STATE_INITIALIZED);
93821308Sache    }
93921308Sache
94021308Sache  /* Initalize the current line information. */
94126497Sache  _rl_init_line_state ();
94221308Sache
94321308Sache  /* We aren't done yet.  We haven't even gotten started yet! */
94421308Sache  rl_done = 0;
94575406Sache  RL_UNSETSTATE(RL_STATE_DONE);
94621308Sache
94721308Sache  /* Tell the history routines what is going on. */
948119610Sache  _rl_start_using_history ();
94921308Sache
95021308Sache  /* Make the display buffer match the state of the line. */
95121308Sache  rl_reset_line_state ();
95221308Sache
95321308Sache  /* No such function typed yet. */
95475406Sache  rl_last_func = (rl_command_func_t *)NULL;
95521308Sache
95621308Sache  /* Parsing of key-bindings begins in an enabled state. */
95721308Sache  _rl_parsing_conditionalized_out = 0;
95821308Sache
95921308Sache#if defined (VI_MODE)
96021308Sache  if (rl_editing_mode == vi_mode)
96121308Sache    _rl_vi_initialize_line ();
96221308Sache#endif
96321308Sache
964119610Sache  /* Each line starts in insert mode (the default). */
965119610Sache  _rl_set_insert_mode (RL_IM_DEFAULT, 1);
966119610Sache
96721308Sache  return 0;
96821308Sache}
96921308Sache
97058310Sache#if 0
97126497Sache#if defined (__EMX__)
97226497Sachestatic void
97326497Sache_emx_build_environ ()
97426497Sache{
97526497Sache  TIB *tibp;
97626497Sache  PIB *pibp;
97726497Sache  char *t, **tp;
97826497Sache  int c;
97926497Sache
98026497Sache  DosGetInfoBlocks (&tibp, &pibp);
98126497Sache  t = pibp->pib_pchenv;
98226497Sache  for (c = 1; *t; c++)
98326497Sache    t += strlen (t) + 1;
98426497Sache  tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
98526497Sache  t = pibp->pib_pchenv;
98626497Sache  while (*t)
98726497Sache    {
98826497Sache      *tp++ = t;
98926497Sache      t += strlen (t) + 1;
99026497Sache    }
99126497Sache  *tp = 0;
99226497Sache}
99326497Sache#endif /* __EMX__ */
99458310Sache#endif
99526497Sache
99621308Sache/* Initialize the entire state of the world. */
99721308Sachestatic void
99821308Sachereadline_initialize_everything ()
99921308Sache{
100058310Sache#if 0
100126497Sache#if defined (__EMX__)
100226497Sache  if (environ == 0)
100326497Sache    _emx_build_environ ();
100426497Sache#endif
100558310Sache#endif
100626497Sache
100775406Sache#if 0
100875406Sache  /* Find out if we are running in Emacs -- UNUSED. */
100975406Sache  running_in_emacs = sh_get_env_value ("EMACS") != (char *)0;
101075406Sache#endif
101121308Sache
101221308Sache  /* Set up input and output if they are not already set up. */
101321308Sache  if (!rl_instream)
101421308Sache    rl_instream = stdin;
101521308Sache
101621308Sache  if (!rl_outstream)
101721308Sache    rl_outstream = stdout;
101821308Sache
101921308Sache  /* Bind _rl_in_stream and _rl_out_stream immediately.  These values
102021308Sache     may change, but they may also be used before readline_internal ()
102121308Sache     is called. */
102221308Sache  _rl_in_stream = rl_instream;
102321308Sache  _rl_out_stream = rl_outstream;
102421308Sache
102521308Sache  /* Allocate data structures. */
102626497Sache  if (rl_line_buffer == 0)
1027119610Sache    rl_line_buffer = (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
102821308Sache
102921308Sache  /* Initialize the terminal interface. */
103075406Sache  if (rl_terminal_name == 0)
103175406Sache    rl_terminal_name = sh_get_env_value ("TERM");
103275406Sache  _rl_init_terminal_io (rl_terminal_name);
103321308Sache
103421308Sache  /* Bind tty characters to readline functions. */
103521308Sache  readline_default_bindings ();
103621308Sache
103721308Sache  /* Initialize the function names. */
103821308Sache  rl_initialize_funmap ();
103921308Sache
104021308Sache  /* Decide whether we should automatically go into eight-bit mode. */
104121308Sache  _rl_init_eightbit ();
104221308Sache
104321308Sache  /* Read in the init file. */
104421308Sache  rl_read_init_file ((char *)NULL);
104521308Sache
104621308Sache  /* XXX */
104721308Sache  if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
104821308Sache    {
104975406Sache      _rl_screenwidth--;
105075406Sache      _rl_screenchars -= _rl_screenheight;
105121308Sache    }
105221308Sache
105321308Sache  /* Override the effect of any `set keymap' assignments in the
105421308Sache     inputrc file. */
105521308Sache  rl_set_keymap_from_edit_mode ();
105621308Sache
105721308Sache  /* Try to bind a common arrow key prefix, if not already bound. */
105821308Sache  bind_arrow_keys ();
105921308Sache
106021308Sache  /* Enable the meta key, if this terminal has one. */
106121308Sache  if (_rl_enable_meta)
106221308Sache    _rl_enable_meta_key ();
106321308Sache
106421308Sache  /* If the completion parser's default word break characters haven't
106521308Sache     been set yet, then do so now. */
106621308Sache  if (rl_completer_word_break_characters == (char *)NULL)
1067136644Sache    rl_completer_word_break_characters = (char *)rl_basic_word_break_characters;
106821308Sache}
106921308Sache
107021308Sache/* If this system allows us to look at the values of the regular
107121308Sache   input editing characters, then bind them to their readline
107221308Sache   equivalents, iff the characters are not bound to keymaps. */
107321308Sachestatic void
107421308Sachereadline_default_bindings ()
107521308Sache{
1076157184Sache  if (_rl_bind_stty_chars)
1077157184Sache    rl_tty_set_default_bindings (_rl_keymap);
107821308Sache}
107921308Sache
1080136644Sache/* Reset the default bindings for the terminal special characters we're
1081136644Sache   interested in back to rl_insert and read the new ones. */
1082136644Sachestatic void
1083136644Sachereset_default_bindings ()
1084136644Sache{
1085157184Sache  if (_rl_bind_stty_chars)
1086157184Sache    {
1087157184Sache      rl_tty_unset_default_bindings (_rl_keymap);
1088157184Sache      rl_tty_set_default_bindings (_rl_keymap);
1089157184Sache    }
1090136644Sache}
1091136644Sache
1092119610Sache/* Bind some common arrow key sequences in MAP. */
109321308Sachestatic void
1094119610Sachebind_arrow_keys_internal (map)
1095119610Sache     Keymap map;
109621308Sache{
1097119610Sache  Keymap xkeymap;
109821308Sache
1099119610Sache  xkeymap = _rl_keymap;
1100119610Sache  _rl_keymap = map;
1101119610Sache
110258310Sache#if defined (__MSDOS__)
1103136644Sache  rl_bind_keyseq_if_unbound ("\033[0A", rl_get_previous_history);
1104136644Sache  rl_bind_keyseq_if_unbound ("\033[0B", rl_backward_char);
1105136644Sache  rl_bind_keyseq_if_unbound ("\033[0C", rl_forward_char);
1106136644Sache  rl_bind_keyseq_if_unbound ("\033[0D", rl_get_next_history);
110758310Sache#endif
110821308Sache
1109136644Sache  rl_bind_keyseq_if_unbound ("\033[A", rl_get_previous_history);
1110136644Sache  rl_bind_keyseq_if_unbound ("\033[B", rl_get_next_history);
1111136644Sache  rl_bind_keyseq_if_unbound ("\033[C", rl_forward_char);
1112136644Sache  rl_bind_keyseq_if_unbound ("\033[D", rl_backward_char);
1113136644Sache  rl_bind_keyseq_if_unbound ("\033[H", rl_beg_of_line);
1114136644Sache  rl_bind_keyseq_if_unbound ("\033[F", rl_end_of_line);
1115119610Sache
1116136644Sache  rl_bind_keyseq_if_unbound ("\033OA", rl_get_previous_history);
1117136644Sache  rl_bind_keyseq_if_unbound ("\033OB", rl_get_next_history);
1118136644Sache  rl_bind_keyseq_if_unbound ("\033OC", rl_forward_char);
1119136644Sache  rl_bind_keyseq_if_unbound ("\033OD", rl_backward_char);
1120136644Sache  rl_bind_keyseq_if_unbound ("\033OH", rl_beg_of_line);
1121136644Sache  rl_bind_keyseq_if_unbound ("\033OF", rl_end_of_line);
1122119610Sache
1123157184Sache#if defined (__MINGW32__)
1124157184Sache  rl_bind_keyseq_if_unbound ("\340H", rl_get_previous_history);
1125157184Sache  rl_bind_keyseq_if_unbound ("\340P", rl_get_next_history);
1126157184Sache  rl_bind_keyseq_if_unbound ("\340M", rl_forward_char);
1127157184Sache  rl_bind_keyseq_if_unbound ("\340K", rl_backward_char);
1128157184Sache#endif
1129157184Sache
1130119610Sache  _rl_keymap = xkeymap;
113121308Sache}
113221308Sache
1133119610Sache/* Try and bind the common arrow key prefixes after giving termcap and
113421308Sache   the inputrc file a chance to bind them and create `real' keymaps
113521308Sache   for the arrow key prefix. */
113621308Sachestatic void
113721308Sachebind_arrow_keys ()
113821308Sache{
1139119610Sache  bind_arrow_keys_internal (emacs_standard_keymap);
114021308Sache
114121308Sache#if defined (VI_MODE)
1142119610Sache  bind_arrow_keys_internal (vi_movement_keymap);
1143119610Sache  bind_arrow_keys_internal (vi_insertion_keymap);
114421308Sache#endif
114521308Sache}
114621308Sache
114721308Sache/* **************************************************************** */
114821308Sache/*								    */
1149119610Sache/*		Saving and Restoring Readline's state		    */
115021308Sache/*								    */
115121308Sache/* **************************************************************** */
115221308Sache
115321308Sacheint
1154119610Sacherl_save_state (sp)
1155119610Sache     struct readline_state *sp;
115621308Sache{
1157119610Sache  if (sp == 0)
1158119610Sache    return -1;
115921308Sache
1160119610Sache  sp->point = rl_point;
1161119610Sache  sp->end = rl_end;
1162119610Sache  sp->mark = rl_mark;
1163119610Sache  sp->buffer = rl_line_buffer;
1164119610Sache  sp->buflen = rl_line_buffer_len;
1165119610Sache  sp->ul = rl_undo_list;
1166119610Sache  sp->prompt = rl_prompt;
116721308Sache
1168119610Sache  sp->rlstate = rl_readline_state;
1169119610Sache  sp->done = rl_done;
1170119610Sache  sp->kmap = _rl_keymap;
117121308Sache
1172119610Sache  sp->lastfunc = rl_last_func;
1173119610Sache  sp->insmode = rl_insert_mode;
1174119610Sache  sp->edmode = rl_editing_mode;
1175119610Sache  sp->kseqlen = rl_key_sequence_length;
1176119610Sache  sp->inf = rl_instream;
1177119610Sache  sp->outf = rl_outstream;
1178119610Sache  sp->pendingin = rl_pending_input;
1179119610Sache  sp->macro = rl_executing_macro;
118021308Sache
1181119610Sache  sp->catchsigs = rl_catch_signals;
1182119610Sache  sp->catchsigwinch = rl_catch_sigwinch;
118321308Sache
118421308Sache  return (0);
118521308Sache}
118621308Sache
118721308Sacheint
1188119610Sacherl_restore_state (sp)
1189119610Sache     struct readline_state *sp;
119021308Sache{
1191119610Sache  if (sp == 0)
1192119610Sache    return -1;
119321308Sache
1194119610Sache  rl_point = sp->point;
1195119610Sache  rl_end = sp->end;
1196119610Sache  rl_mark = sp->mark;
1197119610Sache  the_line = rl_line_buffer = sp->buffer;
1198119610Sache  rl_line_buffer_len = sp->buflen;
1199119610Sache  rl_undo_list = sp->ul;
1200119610Sache  rl_prompt = sp->prompt;
120121308Sache
1202119610Sache  rl_readline_state = sp->rlstate;
1203119610Sache  rl_done = sp->done;
1204119610Sache  _rl_keymap = sp->kmap;
120521308Sache
1206119610Sache  rl_last_func = sp->lastfunc;
1207119610Sache  rl_insert_mode = sp->insmode;
1208119610Sache  rl_editing_mode = sp->edmode;
1209119610Sache  rl_key_sequence_length = sp->kseqlen;
1210119610Sache  rl_instream = sp->inf;
1211119610Sache  rl_outstream = sp->outf;
1212119610Sache  rl_pending_input = sp->pendingin;
1213119610Sache  rl_executing_macro = sp->macro;
121421308Sache
1215119610Sache  rl_catch_signals = sp->catchsigs;
1216119610Sache  rl_catch_sigwinch = sp->catchsigwinch;
121721308Sache
121821308Sache  return (0);
121921308Sache}
1220