summaryrefslogtreecommitdiff
path: root/ext/readline
diff options
context:
space:
mode:
authorkouji <kouji@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-01 15:04:37 +0000
committerkouji <kouji@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-01 15:04:37 +0000
commit7eb66d1c7b982e0888b25423ad732ea3effff18e (patch)
treea2552ea797c8dbabfa9edd0801fbcddfd0546d08 /ext/readline
parent95cd2c11b994cd20f0a23c340127b0a424e24bdf (diff)
* ext/readline/extconf.rb: checked to have clear_history in
readline library. * ext/readline/readline.c (hist_get, hist_each, Init_readline): The offset specified for the argument of history_get() might be different in GNU Readline and libedit. If use libedit, it was corrected that the computational method of the offset specified for the argument of history_get() when the Readline module was initialized was decided. (hist_get, hist_set): If use libedit, accesses first an input content in history when specifies the negative offset for the argument of history_get() or replace_history_entry(). Then checks the offset is negative in ruby. (rb_remove_history): When compiling, it corrects it to warning when libedit is used. (hist_clear, Init_readline): added Readline::HISTORY.clear method. [ruby-dev:35551] * test/readline/test_readline_history.rb: added unit test for Readline::HISTORY. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18313 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/readline')
-rw-r--r--ext/readline/extconf.rb1
-rw-r--r--ext/readline/readline.c57
2 files changed, 52 insertions, 6 deletions
diff --git a/ext/readline/extconf.rb b/ext/readline/extconf.rb
index cd500ceec8..84fb9b14a6 100644
--- a/ext/readline/extconf.rb
+++ b/ext/readline/extconf.rb
@@ -66,4 +66,5 @@ have_readline_func("rl_vi_editing_mode")
have_readline_func("rl_emacs_editing_mode")
have_readline_func("replace_history_entry")
have_readline_func("remove_history")
+have_readline_func("clear_history")
create_makefile("readline")
diff --git a/ext/readline/readline.c b/ext/readline/readline.c
index 6481e4854f..4e9792e663 100644
--- a/ext/readline/readline.c
+++ b/ext/readline/readline.c
@@ -29,6 +29,8 @@
static VALUE mReadline;
+#define EDIT_LINE_LIBRARY_VERSION "EditLine wrapper"
+
#define COMPLETION_PROC "completion_proc"
#define COMPLETION_CASE_FOLD "completion_case_fold"
static ID completion_proc, completion_case_fold;
@@ -43,6 +45,8 @@ static ID completion_proc, completion_case_fold;
# define rl_completion_matches completion_matches
#endif
+static int (*history_get_offset_func)(int);
+
static char **readline_attempted_completion_function(const char *text,
int start, int end);
@@ -505,10 +509,22 @@ hist_to_s(VALUE self)
return rb_str_new2("HISTORY");
}
+static int
+history_get_offset_history_base(int offset)
+{
+ return history_base + offset;
+}
+
+static int
+history_get_offset_0(int offset)
+{
+ return offset;
+}
+
static VALUE
hist_get(VALUE self, VALUE index)
{
- HIST_ENTRY *entry;
+ HIST_ENTRY *entry = NULL;
int i;
rb_secure(4);
@@ -516,7 +532,9 @@ hist_get(VALUE self, VALUE index)
if (i < 0) {
i += history_length;
}
- entry = history_get(history_base + i);
+ if (i >= 0) {
+ entry = history_get(history_get_offset_func(i));
+ }
if (entry == NULL) {
rb_raise(rb_eIndexError, "invalid index");
}
@@ -527,7 +545,7 @@ static VALUE
hist_set(VALUE self, VALUE index, VALUE str)
{
#ifdef HAVE_REPLACE_HISTORY_ENTRY
- HIST_ENTRY *entry;
+ HIST_ENTRY *entry = NULL;
int i;
rb_secure(4);
@@ -536,7 +554,9 @@ hist_set(VALUE self, VALUE index, VALUE str)
if (i < 0) {
i += history_length;
}
- entry = replace_history_entry(i, RSTRING_PTR(str), NULL);
+ if (i >= 0) {
+ entry = replace_history_entry(i, RSTRING_PTR(str), NULL);
+ }
if (entry == NULL) {
rb_raise(rb_eIndexError, "invalid index");
}
@@ -581,7 +601,7 @@ rb_remove_history(int index)
entry = remove_history(index);
if (entry) {
val = rb_tainted_str_new2(entry->line);
- free(entry->line);
+ free((void *) entry->line);
free(entry);
return val;
}
@@ -624,7 +644,7 @@ hist_each(VALUE self)
rb_secure(4);
for (i = 0; i < history_length; i++) {
- entry = history_get(history_base + i);
+ entry = history_get(history_get_offset_func(i));
if (entry == NULL)
break;
rb_yield(rb_tainted_str_new2(entry->line));
@@ -662,6 +682,19 @@ hist_delete_at(VALUE self, VALUE index)
}
static VALUE
+hist_clear(VALUE self)
+{
+#ifdef HAVE_CLEAR_HISTORY
+ rb_secure(4);
+ clear_history();
+ return self;
+#else
+ rb_notimplement();
+ return Qnil; /* not reached */
+#endif
+}
+
+static VALUE
filename_completion_proc_call(VALUE self, VALUE str)
{
VALUE result;
@@ -782,6 +815,7 @@ Init_readline()
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_singleton_method(history,"clear", hist_clear, 0);
rb_define_const(mReadline, "HISTORY", history);
fcomp = rb_obj_alloc(rb_cObject);
@@ -793,8 +827,19 @@ Init_readline()
rb_define_singleton_method(ucomp, "call",
username_completion_proc_call, 1);
rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp);
+ history_get_offset_func = history_get_offset_history_base;
#if defined HAVE_RL_LIBRARY_VERSION
rb_define_const(mReadline, "VERSION", rb_str_new2(rl_library_version));
+#if defined HAVE_CLEAR_HISTORY
+ if (strncmp(rl_library_version, EDIT_LINE_LIBRARY_VERSION,
+ strlen(EDIT_LINE_LIBRARY_VERSION)) == 0) {
+ add_history("1");
+ if (history_get(history_get_offset_func(0)) == NULL) {
+ history_get_offset_func = history_get_offset_0;
+ }
+ clear_history();
+ }
+#endif
#else
rb_define_const(mReadline, "VERSION", rb_str_new2("2.0 or prior version"));
#endif