Project

General

Profile

Feature #6626 » 0002-ext-readline-readline.c-Readline.delete_text.patch

nobu (Nobuyoshi Nakada), 06/22/2012 03:20 PM

View differences:

ext/readline/extconf.rb
readline.have_func("clear_history")
readline.have_func("rl_redisplay")
readline.have_func("rl_insert_text")
readline.have_func("rl_delete_text")
readline.have_macro("RL_PROMPT_START_IGNORE")
readline.have_macro("RL_PROMPT_END_IGNORE")
create_makefile("readline")
ext/readline/readline.c
#define readline_s_insert_text rb_f_notimplement
#endif
#if defined(HAVE_RL_DELETE_TEXT)
/*
* call-seq:
* Readline.delete_text([start[, length]]) -> self
* Readline.delete_text(start..end) -> self
* Readline.delete_text() -> self
*
* Delete text between start and end in the current line.
*
* See GNU Readline's rl_delete_text function.
*
* Raises SecurityError if $SAFE is 4.
*/
static VALUE
readline_s_delete_text(int argc, VALUE *argv, VALUE self)
{
rb_secure(4);
rb_check_arity(argc, 0, 2);
if (rl_line_buffer) {
char *p, *ptr = rl_line_buffer;
long beg = 0, len = strlen(rl_line_buffer);
struct RString fakestr;
VALUE str = (VALUE)&fakestr;
fakestr.basic.flags = T_STRING | RSTRING_NOEMBED;
fakestr.as.heap.ptr = ptr;
fakestr.as.heap.len = len;
rb_enc_associate(str, rb_locale_encoding());
OBJ_FREEZE(str);
if (argc == 2) {
beg = NUM2LONG(argv[0]);
len = NUM2LONG(argv[1]);
num_pos:
p = rb_str_subpos(str, beg, &len);
if (!p) rb_raise(rb_eArgError, "invalid index");
beg = p - ptr;
}
else if (argc == 1) {
len = rb_str_strlen(str);
if (!rb_range_beg_len(argv[0], &beg, &len, len, 1)) {
beg = NUM2LONG(argv[0]);
goto num_pos;
}
}
rl_delete_text(rb_long2int(beg), rb_long2int(beg + len));
}
return self;
}
#else
#define readline_s_delete_text rb_f_notimplement
#endif
#if defined(HAVE_RL_REDISPLAY)
/*
* call-seq:
......
readline_s_get_pre_input_hook, 0);
rb_define_singleton_method(mReadline, "insert_text",
readline_s_insert_text, 1);
rb_define_singleton_method(mReadline, "delete_text",
readline_s_delete_text, -1);
rb_define_singleton_method(mReadline, "redisplay",
readline_s_redisplay, 0);
rb_define_singleton_method(mReadline, "special_prefixes=",
test/readline/test_readline.rb
assert_equal(str, Readline.line_buffer)
assert_equal(get_default_internal_encoding,
Readline.line_buffer.encoding)
Readline.delete_text(1, 3)
assert_equal("t_insert_text", Readline.line_buffer)
Readline.delete_text(11)
assert_equal("t_insert_te", Readline.line_buffer)
Readline.delete_text(-3...-1)
assert_equal("t_inserte", Readline.line_buffer)
Readline.delete_text(-3..-1)
assert_equal("t_inse", Readline.line_buffer)
Readline.delete_text(3..-3)
assert_equal("t_ise", Readline.line_buffer)
Readline.delete_text(3, 1)
assert_equal("t_ie", Readline.line_buffer)
Readline.delete_text(1..1)
assert_equal("tie", Readline.line_buffer)
Readline.delete_text(1...2)
assert_equal("te", Readline.line_buffer)
Readline.delete_text
assert_equal("", Readline.line_buffer)
rescue NotImplementedError
end
end if !/EditLine/n.match(Readline::VERSION)
(2-2/3)