1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24019 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kouji 2009-07-10 12:13:18 +00:00
parent 5d8b373b05
commit 89fd521319
5 changed files with 161 additions and 38 deletions

View file

@ -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.

View file

@ -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 モードにします。

View file

@ -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")

View file

@ -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",

View file

@ -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