From 3fd16970f3a6b4d33d6e6397e5570e9ee0e2a368 Mon Sep 17 00:00:00 2001 From: shugo Date: Thu, 19 May 2005 14:36:20 +0000 Subject: * ext/readline/readline.c: supported libedit. fixed: [ruby-core:4858] * ext/readline/extconf.rb: added new option --enable-libedit. * test/readline/test_readline.rb: added assertions for Readline::HISTORY. * lib/irb/input-method.rb: do not use Readline::HISTORY.pop. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8489 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/readline/readline.c | 191 ++++++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 95 deletions(-) (limited to 'ext/readline/readline.c') diff --git a/ext/readline/readline.c b/ext/readline/readline.c index 4b859dd08d..f93e77ff0d 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -3,8 +3,16 @@ #include #include +#include +#ifdef HAVE_READLINE_READLINE_H #include +#endif +#ifdef HAVE_READLINE_HISTORY_H #include +#endif +#ifdef HAVE_EDITLINE_READLINE_H +#include +#endif #include "ruby.h" #include "rubyio.h" @@ -22,12 +30,16 @@ static VALUE mReadline; #define COMPLETION_CASE_FOLD "completion_case_fold" static ID completion_proc, completion_case_fold; -#ifndef READLINE_42_OR_LATER +#ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION # define rl_filename_completion_function filename_completion_function # define rl_username_completion_function username_completion_function # define rl_completion_matches completion_matches #endif +static int readline_event(void); +static char **readline_attempted_completion_function(const char *text, + int start, int end); + static int readline_event() { @@ -63,10 +75,10 @@ readline_readline(argc, argv, self) buff = (char*)rb_protect((VALUE(*)_((VALUE)))readline, (VALUE)prompt, &status); if (status) { -#if defined READLINE_40_OR_LATER +#if defined HAVE_RL_CLEANUP_AFTER_SIGNAL /* restore terminal mode and signal handler*/ rl_cleanup_after_signal(); -#elif defined READLINE_21_OR_LATER +#elif defined HAVE_RL_DEPREP_TERM_FUNCTION /* restore terminal mode */ (*rl_deprep_term_function)(); #else @@ -124,7 +136,7 @@ readline_s_get_completion_case_fold(self) static char ** readline_attempted_completion_function(text, start, end) - char *text; + const char *text; int start; int end; { @@ -136,7 +148,9 @@ readline_attempted_completion_function(text, start, end) proc = rb_attr_get(mReadline, completion_proc); if (NIL_P(proc)) return NULL; +#ifdef HAVE_RL_ATTEMPTED_COMPLETION_OVER rl_attempted_completion_over = 1; +#endif case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold)); ary = rb_funcall(proc, rb_intern("call"), 1, rb_tainted_str_new2(text)); if (TYPE(ary) != T_ARRAY) @@ -209,7 +223,7 @@ static VALUE readline_s_set_completion_append_character(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER rb_secure(4); if (NIL_P(str)) { rl_completion_append_character = '\0'; @@ -226,14 +240,14 @@ readline_s_set_completion_append_character(self, str) return self; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_COMPLETION_APPEND_CHARACTER */ } static VALUE readline_s_get_completion_append_character(self) VALUE self; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER VALUE str; rb_secure(4); @@ -246,14 +260,14 @@ readline_s_get_completion_append_character(self) return str; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_COMPLETION_APPEND_CHARACTER */ } static VALUE readline_s_set_basic_word_break_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS static char *basic_word_break_characters = NULL; rb_secure(4); @@ -273,28 +287,28 @@ readline_s_set_basic_word_break_characters(self, str) return self; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_BASIC_WORD_BREAK_CHARACTERS */ } static VALUE readline_s_get_basic_word_break_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS rb_secure(4); if (rl_basic_word_break_characters == NULL) return Qnil; return rb_tainted_str_new2(rl_basic_word_break_characters); #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_BASIC_WORD_BREAK_CHARACTERS */ } static VALUE readline_s_set_completer_word_break_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS static char *completer_word_break_characters = NULL; rb_secure(4); @@ -314,28 +328,28 @@ readline_s_set_completer_word_break_characters(self, str) return self; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS */ } static VALUE readline_s_get_completer_word_break_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS rb_secure(4); if (rl_completer_word_break_characters == NULL) return Qnil; return rb_tainted_str_new2(rl_completer_word_break_characters); #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS */ } static VALUE readline_s_set_basic_quote_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS static char *basic_quote_characters = NULL; rb_secure(4); @@ -355,28 +369,28 @@ readline_s_set_basic_quote_characters(self, str) return self; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_BASIC_QUOTE_CHARACTERS */ } static VALUE readline_s_get_basic_quote_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS rb_secure(4); if (rl_basic_quote_characters == NULL) return Qnil; return rb_tainted_str_new2(rl_basic_quote_characters); #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_BASIC_QUOTE_CHARACTERS */ } static VALUE readline_s_set_completer_quote_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS static char *completer_quote_characters = NULL; rb_secure(4); @@ -396,28 +410,28 @@ readline_s_set_completer_quote_characters(self, str) return self; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_COMPLETER_QUOTE_CHARACTERS */ } static VALUE readline_s_get_completer_quote_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS rb_secure(4); if (rl_completer_quote_characters == NULL) return Qnil; return rb_tainted_str_new2(rl_completer_quote_characters); #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_COMPLETER_QUOTE_CHARACTERS */ } static VALUE readline_s_set_filename_quote_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS static char *filename_quote_characters = NULL; rb_secure(4); @@ -437,39 +451,21 @@ readline_s_set_filename_quote_characters(self, str) return self; #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ +#endif /* HAVE_RL_FILENAME_QUOTE_CHARACTERS */ } static VALUE readline_s_get_filename_quote_characters(self, str) VALUE self, str; { -#ifdef READLINE_21_OR_LATER +#ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS rb_secure(4); if (rl_filename_quote_characters == NULL) return Qnil; return rb_tainted_str_new2(rl_filename_quote_characters); #else rb_notimplement(); -#endif /* READLINE_21_OR_LATER */ -} - -static VALUE -rb_remove_history(index) - int index; -{ - HIST_ENTRY *entry; - VALUE val; - - rb_secure(4); - entry = remove_history(index); - if (entry) { - val = rb_tainted_str_new2(entry->line); - free(entry->line); - free(entry); - return val; - } - return Qnil; +#endif /* HAVE_RL_FILENAME_QUOTE_CHARACTERS */ } static VALUE @@ -484,19 +480,19 @@ hist_get(self, index) VALUE self; VALUE index; { - HISTORY_STATE *state; + HIST_ENTRY *entry; int i; rb_secure(4); - state = history_get_history_state(); i = NUM2INT(index); if (i < 0) { - i += state->length; + i += history_length; } - if (i < 0 || i > state->length - 1) { + entry = history_get(history_base + i); + if (entry == NULL) { rb_raise(rb_eIndexError, "invalid index"); } - return rb_tainted_str_new2(state->entries[i]->line); + return rb_tainted_str_new2(entry->line); } static VALUE @@ -505,21 +501,23 @@ hist_set(self, index, str) VALUE index; VALUE str; { - HISTORY_STATE *state; - VALUE s = str; +#ifdef HAVE_REPLACE_HISTORY_ENTRY + HIST_ENTRY *entry; int i; rb_secure(4); - state = history_get_history_state(); i = NUM2INT(index); + SafeStringValue(str); if (i < 0) { - i += state->length; + i += history_length; } - if (i < 0 || i > state->length - 1) { + entry = replace_history_entry(i, RSTRING(str)->ptr, NULL); + if (entry == NULL) { rb_raise(rb_eIndexError, "invalid index"); } - SafeStringValue(str); - replace_history_entry(i, RSTRING(str)->ptr, NULL); +#else + rb_notimplement(); +#endif return str; } @@ -551,16 +549,35 @@ hist_push_method(argc, argv, self) return self; } +static VALUE +rb_remove_history(index) + int index; +{ +#ifdef HAVE_REMOVE_HISTORY + HIST_ENTRY *entry; + VALUE val; + + rb_secure(4); + entry = remove_history(index); + if (entry) { + val = rb_tainted_str_new2(entry->line); + free(entry->line); + free(entry); + return val; + } +#else + rb_notimplement(); +#endif + return Qnil; +} + static VALUE hist_pop(self) VALUE self; { - HISTORY_STATE *state; - rb_secure(4); - state = history_get_history_state(); - if (state->length > 0) { - return rb_remove_history(state->length - 1); + if (history_length > 0) { + return rb_remove_history(history_length - 1); } else { return Qnil; } @@ -570,11 +587,8 @@ static VALUE hist_shift(self) VALUE self; { - HISTORY_STATE *state; - rb_secure(4); - state = history_get_history_state(); - if (state->length > 0) { + if (history_length > 0) { return rb_remove_history(0); } else { return Qnil; @@ -585,13 +599,15 @@ static VALUE hist_each(self) VALUE self; { - HISTORY_STATE *state; + HIST_ENTRY *entry; int i; rb_secure(4); - state = history_get_history_state(); - for (i = 0; i < state->length; i++) { - rb_yield(rb_tainted_str_new2(state->entries[i]->line)); + for (i = 0; i < history_length; i++) { + entry = history_get(history_base + i); + if (entry == NULL) + break; + rb_yield(rb_tainted_str_new2(entry->line)); } return self; } @@ -600,25 +616,16 @@ static VALUE hist_length(self) VALUE self; { - HISTORY_STATE *state; - rb_secure(4); - state = history_get_history_state(); - return INT2NUM(state->length); + return INT2NUM(history_length); } static VALUE hist_empty_p(self) VALUE self; { - HISTORY_STATE *state; - rb_secure(4); - state = history_get_history_state(); - if (state->length == 0) - return Qtrue; - else - return Qfalse; + return history_length == 0 ? Qtrue : Qfalse; } static VALUE @@ -626,15 +633,13 @@ hist_delete_at(self, index) VALUE self; VALUE index; { - HISTORY_STATE *state; int i; rb_secure(4); - state = history_get_history_state(); i = NUM2INT(index); if (i < 0) - i += state->length; - if (i < 0 || i > state->length - 1) { + i += history_length; + if (i < 0 || i > history_length - 1) { rb_raise(rb_eIndexError, "invalid index"); } return rb_remove_history(i); @@ -759,7 +764,6 @@ Init_readline() rb_define_singleton_method(history,"each", hist_each, 0); rb_define_singleton_method(history,"length", hist_length, 0); rb_define_singleton_method(history,"size", hist_length, 0); - rb_define_singleton_method(history,"empty?", hist_empty_p, 0); rb_define_singleton_method(history,"delete_at", hist_delete_at, 1); rb_define_const(mReadline, "HISTORY", history); @@ -773,21 +777,18 @@ Init_readline() rb_define_singleton_method(ucomp, "call", username_completion_proc_call, 1); rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp); -#if defined READLINE_21_OR_LATER +#if defined HAVE_RL_LIBRARY_VERSION rb_define_const(mReadline, "VERSION", rb_str_new2(rl_library_version)); #else rb_define_const(mReadline, "VERSION", rb_str_new2("2.0 or before version")); #endif -#if defined READLINE_42_OR_LATER - rl_attempted_completion_function - = (rl_completion_func_t *)readline_attempted_completion_function; - rl_event_hook = (rl_hook_func_t *)readline_event; -#else - rl_attempted_completion_function - = (CPPFunction *) readline_attempted_completion_function; + rl_attempted_completion_function = readline_attempted_completion_function; +#ifdef HAVE_RL_EVENT_HOOK rl_event_hook = readline_event; #endif +#ifdef HAVE_RL_CLEAR_SIGNALS rl_clear_signals(); +#endif } -- cgit v1.2.3