#define READLINE_LIBRARY
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif
#include <sys/types.h>
#include "posixstat.h"
#include <fcntl.h>
#if defined (HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#if defined (HAVE_STDLIB_H)
# include <stdlib.h>
#else
# include "ansi_stdlib.h"
#endif
#if defined (HAVE_LOCALE_H)
# include <locale.h>
#endif
#include <stdio.h>
#include "posixjmp.h"
#include "rldefs.h"
#if defined (__EMX__)
# define INCL_DOSPROCESS
# include <os2.h>
#endif
#include "readline.h"
#include "history.h"
#include "rlprivate.h"
#include "rlshell.h"
#include "xmalloc.h"
#ifndef RL_LIBRARY_VERSION
# define RL_LIBRARY_VERSION "4.1"
#endif
#define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
void _rl_free_history_entry __P((HIST_ENTRY *));
static char *readline_internal __P((void));
static void readline_initialize_everything __P((void));
static void start_using_history __P((void));
static void bind_arrow_keys __P((void));
static int rl_change_case __P((int, int));
static void readline_default_bindings __P((void));
char *rl_library_version = RL_LIBRARY_VERSION;
int rl_gnu_readline_p = 1;
Keymap _rl_keymap = emacs_standard_keymap;
int rl_editing_mode = emacs_mode;
int rl_dispatching;
int _rl_last_command_was_kill = 0;
int rl_numeric_arg = 1;
int rl_explicit_arg = 0;
int rl_arg_sign = 1;
static int rl_initialized;
static int running_in_emacs;
int rl_point;
int rl_mark;
int rl_end;
int rl_done;
Function *rl_last_func = (Function *)NULL;
procenv_t readline_top_level;
FILE *_rl_in_stream, *_rl_out_stream;
FILE *rl_instream = (FILE *)NULL;
FILE *rl_outstream = (FILE *)NULL;
int readline_echoing_p = 1;
char *rl_prompt;
int rl_visible_prompt_length = 0;
int rl_already_prompted = 0;
int rl_key_sequence_length = 0;
Function *rl_startup_hook = (Function *)NULL;
Function *rl_pre_input_hook = (Function *)NULL;
static char *the_line;
int _rl_eof_char = CTRL ('D');
int rl_pending_input = 0;
char *rl_terminal_name = (char *)NULL;
int _rl_horizontal_scroll_mode = 0;
int _rl_mark_modified_lines = 0;
int _rl_bell_preference = AUDIBLE_BELL;
char *_rl_comment_begin;
Keymap rl_executing_keymap;
int rl_erase_empty_line = 0;
int rl_num_chars_to_read;
char *rl_line_buffer = (char *)NULL;
int rl_line_buffer_len = 0;
unsigned char _rl_parsing_conditionalized_out = 0;
int _rl_convert_meta_chars_to_ascii = 1;
int _rl_output_meta_chars = 0;
int _rl_meta_flag = 0;
char *
readline (prompt)
char *prompt;
{
char *value;
rl_prompt = prompt;
if (rl_pending_input == EOF)
{
rl_pending_input = 0;
return ((char *)NULL);
}
rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
rl_initialize ();
(*rl_prep_term_function) (_rl_meta_flag);
#if defined (HANDLE_SIGNALS)
rl_set_signals ();
#endif
value = readline_internal ();
(*rl_deprep_term_function) ();
#if defined (HANDLE_SIGNALS)
rl_clear_signals ();
#endif
return (value);
}
#if defined (READLINE_CALLBACKS)
# define STATIC_CALLBACK
#else
# define STATIC_CALLBACK static
#endif
STATIC_CALLBACK void
readline_internal_setup ()
{
char *nprompt;
_rl_in_stream = rl_instream;
_rl_out_stream = rl_outstream;
if (rl_startup_hook)
(*rl_startup_hook) ();
if (readline_echoing_p == 0)
{
if (rl_prompt && rl_already_prompted == 0)
{
nprompt = _rl_strip_prompt (rl_prompt);
fprintf (_rl_out_stream, "%s", nprompt);
fflush (_rl_out_stream);
free (nprompt);
}
}
else
{
if (rl_prompt && rl_already_prompted)
rl_on_new_line_with_prompt ();
else
rl_on_new_line ();
(*rl_redisplay_function) ();
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
rl_vi_insertion_mode (1, 0);
#endif
}
if (rl_pre_input_hook)
(*rl_pre_input_hook) ();
}
STATIC_CALLBACK char *
readline_internal_teardown (eof)
int eof;
{
char *temp;
HIST_ENTRY *entry;
entry = current_history ();
if (entry && rl_undo_list)
{
temp = savestring (the_line);
rl_revert_line (1, 0);
entry = replace_history_entry (where_history (), the_line, (histdata_t)NULL);
_rl_free_history_entry (entry);
strcpy (the_line, temp);
free (temp);
}
if (rl_undo_list)
free_undo_list ();
return (eof ? (char *)NULL : savestring (the_line));
}
STATIC_CALLBACK int
#if defined (READLINE_CALLBACKS)
readline_internal_char ()
#else
readline_internal_charloop ()
#endif
{
static int lastc, eof_found;
int c, code, lk;
lastc = -1;
eof_found = 0;
#if !defined (READLINE_CALLBACKS)
while (rl_done == 0)
{
#endif
lk = _rl_last_command_was_kill;
code = setjmp (readline_top_level);
if (code)
(*rl_redisplay_function) ();
if (rl_pending_input == 0)
{
_rl_init_argument ();
rl_key_sequence_length = 0;
}
c = rl_read_key ();
if (c == EOF && rl_end)
c = NEWLINE;
if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
{
#if defined (READLINE_CALLBACKS)
return (rl_done = 1);
#else
eof_found = 1;
break;
#endif
}
lastc = c;
_rl_dispatch ((unsigned char)c, _rl_keymap);
if (rl_pending_input == 0 && lk == _rl_last_command_was_kill)
_rl_last_command_was_kill = 0;
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
rl_vi_check ();
#endif
if (rl_num_chars_to_read && rl_end >= rl_num_chars_to_read)
{
(*rl_redisplay_function) ();
rl_newline (1, '\n');
}
if (rl_done == 0)
(*rl_redisplay_function) ();
if (rl_erase_empty_line && rl_done && rl_last_func == rl_newline &&
rl_point == 0 && rl_end == 0)
_rl_erase_entire_line ();
#if defined (READLINE_CALLBACKS)
return 0;
#else
}
return (eof_found);
#endif
}
#if defined (READLINE_CALLBACKS)
static int
readline_internal_charloop ()
{
int eof = 1;
while (rl_done == 0)
eof = readline_internal_char ();
return (eof);
}
#endif
static char *
readline_internal ()
{
int eof;
readline_internal_setup ();
eof = readline_internal_charloop ();
return (readline_internal_teardown (eof));
}
void
_rl_init_line_state ()
{
rl_point = rl_end = 0;
the_line = rl_line_buffer;
the_line[0] = 0;
}
void
_rl_set_the_line ()
{
the_line = rl_line_buffer;
}
int
_rl_dispatch (key, map)
register int key;
Keymap map;
{
int r, newkey;
char *macro;
Function *func;
if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
{
if (map[ESC].type == ISKMAP)
{
if (_rl_defining_kbd_macro)
_rl_add_macro_char (ESC);
map = FUNCTION_TO_KEYMAP (map, ESC);
key = UNMETA (key);
rl_key_sequence_length += 2;
return (_rl_dispatch (key, map));
}
else
ding ();
return 0;
}
if (_rl_defining_kbd_macro)
_rl_add_macro_char (key);
r = 0;
switch (map[key].type)
{
case ISFUNC:
func = map[key].function;
if (func != (Function *)NULL)
{
if (func == rl_do_lowercase_version)
return (_rl_dispatch (_rl_to_lower (key), map));
rl_executing_keymap = map;
#if 0
_rl_suppress_redisplay = (map[key].function == rl_insert) && _rl_input_available ();
#endif
rl_dispatching = 1;
r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
rl_dispatching = 0;
if (!rl_pending_input && map[key].function != rl_digit_argument)
rl_last_func = map[key].function;
}
else
{
_rl_abort_internal ();
return -1;
}
break;
case ISKMAP:
if (map[key].function != (Function *)NULL)
{
rl_key_sequence_length++;
newkey = rl_read_key ();
r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
}
else
{
_rl_abort_internal ();
return -1;
}
break;
case ISMACR:
if (map[key].function != (Function *)NULL)
{
macro = savestring ((char *)map[key].function);
_rl_with_macro_input (macro);
return 0;
}
break;
}
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
_rl_vi_textmod_command (key))
_rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
#endif
return (r);
}
int
rl_initialize ()
{
if (!rl_initialized)
{
readline_initialize_everything ();
rl_initialized++;
}
_rl_init_line_state ();
rl_done = 0;
start_using_history ();
rl_reset_line_state ();
rl_last_func = (Function *)NULL;
_rl_parsing_conditionalized_out = 0;
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
_rl_vi_initialize_line ();
#endif
return 0;
}
#if 0
#if defined (__EMX__)
static void
_emx_build_environ ()
{
TIB *tibp;
PIB *pibp;
char *t, **tp;
int c;
DosGetInfoBlocks (&tibp, &pibp);
t = pibp->pib_pchenv;
for (c = 1; *t; c++)
t += strlen (t) + 1;
tp = environ = (char **)xmalloc ((c + 1) * sizeof (char *));
t = pibp->pib_pchenv;
while (*t)
{
*tp++ = t;
t += strlen (t) + 1;
}
*tp = 0;
}
#endif
#endif
static void
readline_initialize_everything ()
{
#if 0
#if defined (__EMX__)
if (environ == 0)
_emx_build_environ ();
#endif
#endif
running_in_emacs = get_env_value ("EMACS") != (char *)0;
if (!rl_instream)
rl_instream = stdin;
if (!rl_outstream)
rl_outstream = stdout;
_rl_in_stream = rl_instream;
_rl_out_stream = rl_outstream;
if (rl_line_buffer == 0)
rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
_rl_init_terminal_io ((char *)NULL);
readline_default_bindings ();
rl_initialize_funmap ();
_rl_init_eightbit ();
rl_read_init_file ((char *)NULL);
if (_rl_horizontal_scroll_mode && _rl_term_autowrap)
{
screenwidth--;
screenchars -= screenheight;
}
rl_set_keymap_from_edit_mode ();
bind_arrow_keys ();
if (_rl_enable_meta)
_rl_enable_meta_key ();
if (rl_completer_word_break_characters == (char *)NULL)
rl_completer_word_break_characters = rl_basic_word_break_characters;
}
static void
readline_default_bindings ()
{
rltty_set_default_bindings (_rl_keymap);
}
static void
bind_arrow_keys_internal ()
{
Function *f;
#if defined (__MSDOS__)
f = rl_function_of_keyseq ("\033[0A", _rl_keymap, (int *)NULL);
if (!f || f == rl_do_lowercase_version)
{
_rl_bind_if_unbound ("\033[0A", rl_get_previous_history);
_rl_bind_if_unbound ("\033[0B", rl_backward);
_rl_bind_if_unbound ("\033[0C", rl_forward);
_rl_bind_if_unbound ("\033[0D", rl_get_next_history);
}
#endif
f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
if (!f || f == rl_do_lowercase_version)
{
_rl_bind_if_unbound ("\033[A", rl_get_previous_history);
_rl_bind_if_unbound ("\033[B", rl_get_next_history);
_rl_bind_if_unbound ("\033[C", rl_forward);
_rl_bind_if_unbound ("\033[D", rl_backward);
}
f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
if (!f || f == rl_do_lowercase_version)
{
_rl_bind_if_unbound ("\033OA", rl_get_previous_history);
_rl_bind_if_unbound ("\033OB", rl_get_next_history);
_rl_bind_if_unbound ("\033OC", rl_forward);
_rl_bind_if_unbound ("\033OD", rl_backward);
}
}
static void
bind_arrow_keys ()
{
Keymap xkeymap;
xkeymap = _rl_keymap;
_rl_keymap = emacs_standard_keymap;
bind_arrow_keys_internal ();
#if defined (VI_MODE)
_rl_keymap = vi_movement_keymap;
bind_arrow_keys_internal ();
#endif
_rl_keymap = xkeymap;
}
static int
rl_digit_loop ()
{
int key, c, sawminus, sawdigits;
rl_save_prompt ();
sawminus = sawdigits = 0;
while (1)
{
if (rl_numeric_arg > 1000000)
{
sawdigits = rl_explicit_arg = rl_numeric_arg = 0;
ding ();
rl_restore_prompt ();
rl_clear_message ();
return 1;
}
rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
key = c = rl_read_key ();
if (_rl_keymap[c].type == ISFUNC &&
_rl_keymap[c].function == rl_universal_argument)
{
if (sawdigits == 0)
{
rl_numeric_arg *= 4;
continue;
}
else
{
key = rl_read_key ();
rl_restore_prompt ();
rl_clear_message ();
return (_rl_dispatch (key, _rl_keymap));
}
}
c = UNMETA (c);
if (_rl_digit_p (c))
{
rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) + c - '0' : c - '0';
sawdigits = rl_explicit_arg = 1;
}
else if (c == '-' && rl_explicit_arg == 0)
{
rl_numeric_arg = sawminus = 1;
rl_arg_sign = -1;
}
else
{
if (sawminus && rl_numeric_arg == 1 && rl_explicit_arg == 0)
rl_explicit_arg = 1;
rl_restore_prompt ();
rl_clear_message ();
return (_rl_dispatch (key, _rl_keymap));
}
}
return 0;
}
int
rl_digit_argument (ignore, key)
int ignore, key;
{
rl_pending_input = key;
return (rl_digit_loop ());
}
int
rl_discard_argument ()
{
ding ();
rl_clear_message ();
_rl_init_argument ();
return 0;
}
int
_rl_init_argument ()
{
rl_numeric_arg = rl_arg_sign = 1;
rl_explicit_arg = 0;
return 0;
}
int
rl_universal_argument (count, key)
int count, key;
{
rl_numeric_arg *= 4;
return (rl_digit_loop ());
}
int
rl_insert_text (string)
char *string;
{
register int i, l = strlen (string);
if (rl_end + l >= rl_line_buffer_len)
rl_extend_line_buffer (rl_end + l);
for (i = rl_end; i >= rl_point; i--)
the_line[i + l] = the_line[i];
strncpy (the_line + rl_point, string, l);
if (!_rl_doing_an_undo)
{
if ((l == 1) &&
rl_undo_list &&
(rl_undo_list->what == UNDO_INSERT) &&
(rl_undo_list->end == rl_point) &&
(rl_undo_list->end - rl_undo_list->start < 20))
rl_undo_list->end++;
else
rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
}
rl_point += l;
rl_end += l;
the_line[rl_end] = '\0';
return l;
}
int
rl_delete_text (from, to)
int from, to;
{
register char *text;
register int diff, i;
if (from > to)
SWAP (from, to);
if (to > rl_end)
{
to = rl_end;
if (from > to)
from = to;
}
text = rl_copy_text (from, to);
diff = to - from;
for (i = from; i < rl_end - diff; i++)
the_line[i] = the_line[i + diff];
if (_rl_doing_an_undo == 0)
rl_add_undo (UNDO_DELETE, from, to, text);
else
free (text);
rl_end -= diff;
the_line[rl_end] = '\0';
return (diff);
}
#define _RL_FIX_POINT(x) \
do { \
if (x > rl_end) \
x = rl_end; \
else if (x < 0) \
x = 0; \
} while (0)
void
_rl_fix_point (fix_mark_too)
int fix_mark_too;
{
_RL_FIX_POINT (rl_point);
if (fix_mark_too)
_RL_FIX_POINT (rl_mark);
}
#undef _RL_FIX_POINT
void
_rl_replace_text (text, start, end)
char *text;
int start, end;
{
rl_begin_undo_group ();
rl_delete_text (start, end + 1);
rl_point = start;
rl_insert_text (text);
rl_end_undo_group ();
}
int
rl_forward (count, key)
int count, key;
{
if (count < 0)
rl_backward (-count, key);
else if (count > 0)
{
int end = rl_point + count;
#if defined (VI_MODE)
int lend = rl_end - (rl_editing_mode == vi_mode);
#else
int lend = rl_end;
#endif
if (end > lend)
{
rl_point = lend;
ding ();
}
else
rl_point = end;
}
if (rl_end < 0)
rl_end = 0;
return 0;
}
int
rl_backward (count, key)
int count, key;
{
if (count < 0)
rl_forward (-count, key);
else if (count > 0)
{
if (rl_point < count)
{
rl_point = 0;
ding ();
}
else
rl_point -= count;
}
return 0;
}
int
rl_beg_of_line (count, key)
int count, key;
{
rl_point = 0;
return 0;
}
int
rl_end_of_line (count, key)
int count, key;
{
rl_point = rl_end;
return 0;
}
int
rl_forward_word (count, key)
int count, key;
{
int c;
if (count < 0)
{
rl_backward_word (-count, key);
return 0;
}
while (count)
{
if (rl_point == rl_end)
return 0;
c = the_line[rl_point];
if (alphabetic (c) == 0)
{
while (++rl_point < rl_end)
{
c = the_line[rl_point];
if (alphabetic (c))
break;
}
}
if (rl_point == rl_end)
return 0;
while (++rl_point < rl_end)
{
c = the_line[rl_point];
if (alphabetic (c) == 0)
break;
}
--count;
}
return 0;
}
int
rl_backward_word (count, key)
int count, key;
{
int c;
if (count < 0)
{
rl_forward_word (-count, key);
return 0;
}
while (count)
{
if (!rl_point)
return 0;
c = the_line[rl_point - 1];
if (alphabetic (c) == 0)
{
while (--rl_point)
{
c = the_line[rl_point - 1];
if (alphabetic (c))
break;
}
}
while (rl_point)
{
c = the_line[rl_point - 1];
if (alphabetic (c) == 0)
break;
else
--rl_point;
}
--count;
}
return 0;
}
int
rl_refresh_line (ignore1, ignore2)
int ignore1, ignore2;
{
int curr_line;
curr_line = _rl_current_display_line ();
_rl_move_vert (curr_line);
_rl_move_cursor_relative (0, the_line);
_rl_clear_to_eol (0);
rl_forced_update_display ();
rl_display_fixed = 1;
return 0;
}
int
rl_clear_screen (count, key)
int count, key;
{
if (rl_explicit_arg)
{
rl_refresh_line (count, key);
return 0;
}
_rl_clear_screen ();
rl_forced_update_display ();
rl_display_fixed = 1;
return 0;
}
int
rl_arrow_keys (count, c)
int count, c;
{
int ch;
ch = rl_read_key ();
switch (_rl_to_upper (ch))
{
case 'A':
rl_get_previous_history (count, ch);
break;
case 'B':
rl_get_next_history (count, ch);
break;
case 'C':
rl_forward (count, ch);
break;
case 'D':
rl_backward (count, ch);
break;
default:
ding ();
}
return 0;
}
int
rl_insert (count, c)
int count, c;
{
register int i;
char *string;
if (count <= 0)
return 0;
if (count > 1 && count <= 1024)
{
string = xmalloc (1 + count);
for (i = 0; i < count; i++)
string[i] = c;
string[i] = '\0';
rl_insert_text (string);
free (string);
return 0;
}
if (count > 1024)
{
int decreaser;
char str[1024+1];
for (i = 0; i < 1024; i++)
str[i] = c;
while (count)
{
decreaser = (count > 1024 ? 1024 : count);
str[decreaser] = '\0';
rl_insert_text (str);
count -= decreaser;
}
return 0;
}
if (_rl_any_typein ())
_rl_insert_typein (c);
else
{
char str[2];
str[1] = '\0';
str[0] = c;
rl_insert_text (str);
}
return 0;
}
int
rl_quoted_insert (count, key)
int count, key;
{
int c;
#if defined (HANDLE_SIGNALS)
_rl_disable_tty_signals ();
#endif
c = rl_read_key ();
#if defined (HANDLE_SIGNALS)
_rl_restore_tty_signals ();
#endif
return (rl_insert (count, c));
}
int
rl_tab_insert (count, key)
int count, key;
{
return (rl_insert (count, '\t'));
}
int
rl_newline (count, key)
int count, key;
{
rl_done = 1;
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
{
_rl_vi_done_inserting ();
_rl_vi_reset_last ();
}
#endif
if (rl_erase_empty_line && rl_point == 0 && rl_end == 0)
return 0;
if (readline_echoing_p)
_rl_update_final ();
return 0;
}
int
rl_do_lowercase_version (ignore1, ignore2)
int ignore1, ignore2;
{
return 0;
}
int
rl_rubout (count, key)
int count, key;
{
if (count < 0)
{
rl_delete (-count, key);
return 0;
}
if (!rl_point)
{
ding ();
return -1;
}
if (count > 1 || rl_explicit_arg)
{
int orig_point = rl_point;
rl_backward (count, key);
rl_kill_text (orig_point, rl_point);
}
else
{
int c = the_line[--rl_point];
rl_delete_text (rl_point, rl_point + 1);
if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
{
int l;
l = rl_character_len (c, rl_point);
_rl_erase_at_end_of_line (l);
}
}
return 0;
}
int
rl_delete (count, key)
int count, key;
{
if (count < 0)
return (rl_rubout (-count, key));
if (rl_point == rl_end)
{
ding ();
return -1;
}
if (count > 1 || rl_explicit_arg)
{
int orig_point = rl_point;
rl_forward (count, key);
rl_kill_text (orig_point, rl_point);
rl_point = orig_point;
return 0;
}
else
return (rl_delete_text (rl_point, rl_point + 1));
}
int
rl_rubout_or_delete (count, key)
int count, key;
{
if (rl_end != 0 && rl_point == rl_end)
return (rl_rubout (count, key));
else
return (rl_delete (count, key));
}
int
rl_delete_horizontal_space (count, ignore)
int count, ignore;
{
int start = rl_point;
while (rl_point && whitespace (the_line[rl_point - 1]))
rl_point--;
start = rl_point;
while (rl_point < rl_end && whitespace (the_line[rl_point]))
rl_point++;
if (start != rl_point)
{
rl_delete_text (start, rl_point);
rl_point = start;
}
return 0;
}
int
rl_delete_or_show_completions (count, key)
int count, key;
{
if (rl_end != 0 && rl_point == rl_end)
return (rl_possible_completions (count, key));
else
return (rl_delete (count, key));
}
#ifndef RL_COMMENT_BEGIN_DEFAULT
#define RL_COMMENT_BEGIN_DEFAULT "#"
#endif
int
rl_insert_comment (count, key)
int count, key;
{
rl_beg_of_line (1, key);
rl_insert_text (_rl_comment_begin ? _rl_comment_begin
: RL_COMMENT_BEGIN_DEFAULT);
(*rl_redisplay_function) ();
rl_newline (1, '\n');
return (0);
}
#define UpCase 1
#define DownCase 2
#define CapCase 3
int
rl_upcase_word (count, key)
int count, key;
{
return (rl_change_case (count, UpCase));
}
int
rl_downcase_word (count, key)
int count, key;
{
return (rl_change_case (count, DownCase));
}
int
rl_capitalize_word (count, key)
int count, key;
{
return (rl_change_case (count, CapCase));
}
static int
rl_change_case (count, op)
int count, op;
{
register int start, end;
int inword, c;
start = rl_point;
rl_forward_word (count, 0);
end = rl_point;
if (count < 0)
SWAP (start, end);
rl_modifying (start, end);
for (inword = 0; start < end; start++)
{
c = the_line[start];
switch (op)
{
case UpCase:
the_line[start] = _rl_to_upper (c);
break;
case DownCase:
the_line[start] = _rl_to_lower (c);
break;
case CapCase:
the_line[start] = (inword == 0) ? _rl_to_upper (c) : _rl_to_lower (c);
inword = alphabetic (the_line[start]);
break;
default:
ding ();
return -1;
}
}
rl_point = end;
return 0;
}
int
rl_transpose_words (count, key)
int count, key;
{
char *word1, *word2;
int w1_beg, w1_end, w2_beg, w2_end;
int orig_point = rl_point;
if (!count)
return 0;
rl_forward_word (count, key);
w2_end = rl_point;
rl_backward_word (1, key);
w2_beg = rl_point;
rl_backward_word (count, key);
w1_beg = rl_point;
rl_forward_word (1, key);
w1_end = rl_point;
if ((w1_beg == w2_beg) || (w2_beg < w1_end))
{
ding ();
rl_point = orig_point;
return -1;
}
word1 = rl_copy_text (w1_beg, w1_end);
word2 = rl_copy_text (w2_beg, w2_end);
rl_begin_undo_group ();
rl_point = w2_beg;
rl_delete_text (w2_beg, w2_end);
rl_insert_text (word1);
rl_point = w1_beg;
rl_delete_text (w1_beg, w1_end);
rl_insert_text (word2);
rl_point = w2_end;
rl_end_undo_group ();
free (word1);
free (word2);
return 0;
}
int
rl_transpose_chars (count, key)
int count, key;
{
char dummy[2];
if (!count)
return 0;
if (!rl_point || rl_end < 2)
{
ding ();
return -1;
}
rl_begin_undo_group ();
if (rl_point == rl_end)
{
--rl_point;
count = 1;
}
rl_point--;
dummy[0] = the_line[rl_point];
dummy[1] = '\0';
rl_delete_text (rl_point, rl_point + 1);
rl_point += count;
_rl_fix_point (0);
rl_insert_text (dummy);
rl_end_undo_group ();
return 0;
}
int
_rl_char_search_internal (count, dir, schar)
int count, dir, schar;
{
int pos, inc;
pos = rl_point;
inc = (dir < 0) ? -1 : 1;
while (count)
{
if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= rl_end))
{
ding ();
return -1;
}
pos += inc;
do
{
if (rl_line_buffer[pos] == schar)
{
count--;
if (dir < 0)
rl_point = (dir == BTO) ? pos + 1 : pos;
else
rl_point = (dir == FTO) ? pos - 1 : pos;
break;
}
}
while ((dir < 0) ? pos-- : ++pos < rl_end);
}
return (0);
}
static int
_rl_char_search (count, fdir, bdir)
int count, fdir, bdir;
{
int c;
c = rl_read_key ();
if (count < 0)
return (_rl_char_search_internal (-count, bdir, c));
else
return (_rl_char_search_internal (count, fdir, c));
}
int
rl_char_search (count, key)
int count, key;
{
return (_rl_char_search (count, FFIND, BFIND));
}
int
rl_backward_char_search (count, key)
int count, key;
{
return (_rl_char_search (count, BFIND, FFIND));
}
HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
static void
start_using_history ()
{
using_history ();
if (saved_line_for_history)
_rl_free_history_entry (saved_line_for_history);
saved_line_for_history = (HIST_ENTRY *)NULL;
}
void
_rl_free_history_entry (entry)
HIST_ENTRY *entry;
{
if (entry == 0)
return;
if (entry->line)
free (entry->line);
free (entry);
}
int
maybe_replace_line ()
{
HIST_ENTRY *temp;
temp = current_history ();
if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
{
temp = replace_history_entry (where_history (), the_line, (histdata_t)rl_undo_list);
free (temp->line);
free (temp);
}
return 0;
}
int
maybe_unsave_line ()
{
int line_len;
if (saved_line_for_history)
{
line_len = strlen (saved_line_for_history->line);
if (line_len >= rl_line_buffer_len)
rl_extend_line_buffer (line_len);
strcpy (the_line, saved_line_for_history->line);
rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
_rl_free_history_entry (saved_line_for_history);
saved_line_for_history = (HIST_ENTRY *)NULL;
rl_end = rl_point = strlen (the_line);
}
else
ding ();
return 0;
}
int
maybe_save_line ()
{
if (saved_line_for_history == 0)
{
saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
saved_line_for_history->line = savestring (the_line);
saved_line_for_history->data = (char *)rl_undo_list;
}
return 0;
}
int
rl_beginning_of_history (count, key)
int count, key;
{
return (rl_get_previous_history (1 + where_history (), key));
}
int
rl_end_of_history (count, key)
int count, key;
{
maybe_replace_line ();
using_history ();
maybe_unsave_line ();
return 0;
}
int
rl_get_next_history (count, key)
int count, key;
{
HIST_ENTRY *temp;
int line_len;
if (count < 0)
return (rl_get_previous_history (-count, key));
if (count == 0)
return 0;
maybe_replace_line ();
temp = (HIST_ENTRY *)NULL;
while (count)
{
temp = next_history ();
if (!temp)
break;
--count;
}
if (temp == 0)
maybe_unsave_line ();
else
{
line_len = strlen (temp->line);
if (line_len >= rl_line_buffer_len)
rl_extend_line_buffer (line_len);
strcpy (the_line, temp->line);
rl_undo_list = (UNDO_LIST *)temp->data;
rl_end = rl_point = strlen (the_line);
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
rl_point = 0;
#endif
}
return 0;
}
int
rl_get_previous_history (count, key)
int count, key;
{
HIST_ENTRY *old_temp, *temp;
int line_len;
if (count < 0)
return (rl_get_next_history (-count, key));
if (count == 0)
return 0;
maybe_save_line ();
maybe_replace_line ();
temp = old_temp = (HIST_ENTRY *)NULL;
while (count)
{
temp = previous_history ();
if (temp == 0)
break;
old_temp = temp;
--count;
}
if (!temp && old_temp)
temp = old_temp;
if (temp == 0)
ding ();
else
{
line_len = strlen (temp->line);
if (line_len >= rl_line_buffer_len)
rl_extend_line_buffer (line_len);
strcpy (the_line, temp->line);
rl_undo_list = (UNDO_LIST *)temp->data;
rl_end = rl_point = line_len;
#if defined (VI_MODE)
if (rl_editing_mode == vi_mode)
rl_point = 0;
#endif
}
return 0;
}
int
_rl_set_mark_at_pos (position)
int position;
{
if (position > rl_end)
return -1;
rl_mark = position;
return 0;
}
int
rl_set_mark (count, key)
int count, key;
{
return (_rl_set_mark_at_pos (rl_explicit_arg ? count : rl_point));
}
int
rl_exchange_point_and_mark (count, key)
int count, key;
{
if (rl_mark > rl_end)
rl_mark = -1;
if (rl_mark == -1)
{
ding ();
return -1;
}
else
SWAP (rl_point, rl_mark);
return 0;
}
int
rl_vi_editing_mode (count, key)
int count, key;
{
#if defined (VI_MODE)
rl_editing_mode = vi_mode;
rl_vi_insertion_mode (1, key);
#endif
return 0;
}
int
rl_emacs_editing_mode (count, key)
int count, key;
{
rl_editing_mode = emacs_mode;
_rl_keymap = emacs_standard_keymap;
return 0;
}