diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | ext/readline/README.ja | 27 | ||||
-rw-r--r-- | ext/readline/extconf.rb | 2 | ||||
-rw-r--r-- | ext/readline/readline.c | 52 | ||||
-rw-r--r-- | test/readline/test_readline.rb | 110 |
5 files changed, 161 insertions, 38 deletions
@@ -1,3 +1,11 @@ +Fri Jul 10 21:00:05 2009 TAKAO Kouji <kouji@takao7.net> + + * ext/readline/extconf.rb: checked rl_line_buffer and rl_point in + readline. + + * ext/readline/readline.c (readline_s_get_line_buffer): new method. + (readline_s_get_point): new method. + Fri Jul 10 16:30:03 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> * array.c (recursive_join): use obj to tell if recursion occurs. diff --git a/ext/readline/README.ja b/ext/readline/README.ja index 3c6c6f3d33..5c8e0da508 100644 --- a/ext/readline/README.ja +++ b/ext/readline/README.ja @@ -167,16 +167,31 @@ Readline.completion_case_fold -> bool ユーザの入力を補完する際、大文字と小文字を区別する/しないを取得します。 bool が真ならば区別しません。bool が偽ならば区別します。 - なお、Readline.completion_case_fold= メソッドで指定したオブジェクトを - そのまま取得するので、次のような動作をします。 + なお、Readline.completion_case_fold= メソッドで指定したオブジェクトを + そのまま取得するので、次のような動作をします。 - require "readline" - - Readline.completion_case_fold = "This is a String." - p Readline.completion_case_fold # => "This is a String." + require "readline" + + Readline.completion_case_fold = "This is a String." + p Readline.completion_case_fold # => "This is a String." $SAFE が 4 の場合、例外 SecurityError が発生します。 +Readline.line_buffer -> string + + 入力中の行全体を返します。complete_proc の中で使用することを想定し + ています。Readline.line_buffer の長さは GNU Readline の rl_end 変数の + 値と一致します。 + +Readline.point -> int + + 現在のカーソルの位置を返します。 + Readline モジュールは補完対象の単語の開始位置の情報を提供していません。 + しかしながら、 completion_proc の中で入力した単語 text と + Readline.point を使用することで開始位置を導くことができます。 + + 開始位置 = 入力した単語の長さ - Readline.point + Readline.vi_editing_mode -> nil 編集モードを vi モードにします。 diff --git a/ext/readline/extconf.rb b/ext/readline/extconf.rb index faf539fffc..d3ccca697a 100644 --- a/ext/readline/extconf.rb +++ b/ext/readline/extconf.rb @@ -60,6 +60,8 @@ have_readline_var("rl_filename_quote_characters") have_readline_var("rl_attempted_completion_over") have_readline_var("rl_library_version") have_readline_var("rl_editing_mode") +have_readline_var("rl_line_buffer") +have_readline_var("rl_point") # workaround for native windows. /mswin|bccwin|mingw/ !~ RUBY_PLATFORM && have_readline_var("rl_event_hook") have_readline_func("rl_cleanup_after_signal") diff --git a/ext/readline/readline.c b/ext/readline/readline.c index d86a714155..d54a280fcd 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -398,6 +398,54 @@ readline_s_get_completion_case_fold(VALUE self) return rb_attr_get(mReadline, completion_case_fold); } +#ifdef HAVE_RL_LINE_BUFFER +/* + * call-seq: + * Readline.line_buffer -> string + * + * Returns the full line that is being edited. This is useful from + * within the complete_proc for determining the context of the + * completion request. + * + * The length of +Readline.line_buffer+ and GNU Readline's rl_end are + * same. + */ +static VALUE +readline_s_get_line_buffer(VALUE self) +{ + rb_secure(4); + if (rl_line_buffer == NULL) + return Qnil; + return rb_tainted_str_new2(rl_line_buffer); +} +#else +#define readline_s_get_line_buffer rb_f_notimplement +#endif + +#ifdef HAVE_RL_POINT +/* + * call-seq: + * Readline.point -> int + * + * Returns the index of the current cursor position in + * +Readline.line_buffer+. + * + * The index in +Readline.line_buffer+ which matches the start of + * input-string passed to completion_proc is computed by subtracting + * the length of input-string from +Readline.point+. + * + * start = (the length of input-string) - Readline.point + */ +static VALUE +readline_s_get_point(VALUE self) +{ + rb_secure(4); + return INT2NUM(rl_point); +} +#else +#define readline_s_get_point rb_f_notimplement +#endif + static char ** readline_attempted_completion_function(const char *text, int start, int end) { @@ -1262,6 +1310,10 @@ Init_readline() readline_s_set_completion_case_fold, 1); rb_define_singleton_method(mReadline, "completion_case_fold", readline_s_get_completion_case_fold, 0); + rb_define_singleton_method(mReadline, "line_buffer", + readline_s_get_line_buffer, 0); + rb_define_singleton_method(mReadline, "point", + readline_s_get_point, 0); rb_define_singleton_method(mReadline, "set_screen_size", readline_s_set_screen_size, 2); rb_define_singleton_method(mReadline, "get_screen_size", diff --git a/test/readline/test_readline.rb b/test/readline/test_readline.rb index c9a88c2f68..259f7ddd6d 100644 --- a/test/readline/test_readline.rb +++ b/test/readline/test_readline.rb @@ -3,6 +3,8 @@ begin =begin class << Readline [ + "line_buffer", + "point", "set_screen_size", "get_screen_size", "vi_editing_mode", @@ -63,6 +65,8 @@ class TestReadline < Test::Unit::TestCase ["completer_quote_characters"], ["filename_quote_characters=", "\\"], ["filename_quote_characters"], + ["line_buffer"], + ["point"], ["set_screen_size", 1, 1], ["get_screen_size"], ] @@ -78,41 +82,83 @@ class TestReadline < Test::Unit::TestCase end end - def test_readline - stdin = Tempfile.new("test_readline_stdin") - stdout = Tempfile.new("test_readline_stdout") - begin - stdin.write("hello\n") - stdin.close - stdout.close - line = replace_stdio(stdin.path, stdout.path) { - Readline.readline("> ", true) - } - assert_equal("hello", line) - assert_equal(true, line.tainted?) - stdout.open - assert_equal("> ", stdout.read(2)) - assert_equal(1, Readline::HISTORY.length) - assert_equal("hello", Readline::HISTORY[0]) - assert_raise(SecurityError) do - Thread.start { - $SAFE = 1 - replace_stdio(stdin.path, stdout.path) do - Readline.readline("> ".taint) - end - }.join + if !/EditLine/n.match(Readline::VERSION) + def test_readline + stdin = Tempfile.new("test_readline_stdin") + stdout = Tempfile.new("test_readline_stdout") + begin + stdin.write("hello\n") + stdin.close + stdout.close + line = replace_stdio(stdin.path, stdout.path) { + Readline.readline("> ", true) + } + assert_equal("hello", line) + assert_equal(true, line.tainted?) + stdout.open + assert_equal("> ", stdout.read(2)) + assert_equal(1, Readline::HISTORY.length) + assert_equal("hello", Readline::HISTORY[0]) + assert_raise(SecurityError) do + Thread.start { + $SAFE = 1 + replace_stdio(stdin.path, stdout.path) do + Readline.readline("> ".taint) + end + }.join + end + assert_raise(SecurityError) do + Thread.start { + $SAFE = 4 + replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") } + }.join + end + ensure + stdin.close(true) + stdout.close(true) end - assert_raise(SecurityError) do - Thread.start { - $SAFE = 4 - replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") } - }.join + end + + # line_buffer + # point + def test_line_buffer__point + begin + Readline.line_buffer + Readline.point + rescue NotImplementedError + return + end + + stdin = Tempfile.new("test_readline_stdin") + stdout = Tempfile.new("test_readline_stdout") + begin + actual_text = nil + actual_line_buffer = nil + actual_point = nil + Readline.completion_proc = proc { |text| + actual_text = text + actual_point = Readline.point + actual_buffer_line = Readline.line_buffer + stdin.write(" finish\n") + stdin.close + stdout.close + return ["complete"] + } + stdin.write("first second\t") + stdin.flush + line = replace_stdio(stdin.path, stdout.path) { + Readline.readline("> ", false) + } + assert_equal("first second", actual_line_buffer) + assert_equal(12, actual_point) + assert_equal("first complete finish", Readline.line_buffer) + assert_equal(21, Readline.point) + ensure + stdin.close(true) + stdout.close(true) end - ensure - stdin.close(true) - stdout.close(true) end - end if !/EditLine/n.match(Readline::VERSION) + end def test_input= assert_raise(TypeError) do |