readline.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:49k
源码类别:

MySQL数据库

开发平台:

Visual C++

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