home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / readline / readline.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  50KB  |  2,116 lines

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