home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / readline / readline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-09  |  73.0 KB  |  3,305 lines

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