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:
parent
5d8b373b05
commit
89fd521319
5 changed files with 161 additions and 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>
|
Fri Jul 10 16:30:03 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* array.c (recursive_join): use obj to tell if recursion occurs.
|
* array.c (recursive_join): use obj to tell if recursion occurs.
|
||||||
|
|
|
@ -167,16 +167,31 @@ Readline.completion_case_fold -> bool
|
||||||
ユーザの入力を補完する際、大文字と小文字を区別する/しないを取得します。
|
ユーザの入力を補完する際、大文字と小文字を区別する/しないを取得します。
|
||||||
bool が真ならば区別しません。bool が偽ならば区別します。
|
bool が真ならば区別しません。bool が偽ならば区別します。
|
||||||
|
|
||||||
なお、Readline.completion_case_fold= メソッドで指定したオブジェクトを
|
なお、Readline.completion_case_fold= メソッドで指定したオブジェクトを
|
||||||
そのまま取得するので、次のような動作をします。
|
そのまま取得するので、次のような動作をします。
|
||||||
|
|
||||||
require "readline"
|
require "readline"
|
||||||
|
|
||||||
Readline.completion_case_fold = "This is a String."
|
Readline.completion_case_fold = "This is a String."
|
||||||
p Readline.completion_case_fold # => "This is a String."
|
p Readline.completion_case_fold # => "This is a String."
|
||||||
|
|
||||||
$SAFE が 4 の場合、例外 SecurityError が発生します。
|
$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
|
Readline.vi_editing_mode -> nil
|
||||||
|
|
||||||
編集モードを vi モードにします。
|
編集モードを vi モードにします。
|
||||||
|
|
|
@ -60,6 +60,8 @@ have_readline_var("rl_filename_quote_characters")
|
||||||
have_readline_var("rl_attempted_completion_over")
|
have_readline_var("rl_attempted_completion_over")
|
||||||
have_readline_var("rl_library_version")
|
have_readline_var("rl_library_version")
|
||||||
have_readline_var("rl_editing_mode")
|
have_readline_var("rl_editing_mode")
|
||||||
|
have_readline_var("rl_line_buffer")
|
||||||
|
have_readline_var("rl_point")
|
||||||
# workaround for native windows.
|
# workaround for native windows.
|
||||||
/mswin|bccwin|mingw/ !~ RUBY_PLATFORM && have_readline_var("rl_event_hook")
|
/mswin|bccwin|mingw/ !~ RUBY_PLATFORM && have_readline_var("rl_event_hook")
|
||||||
have_readline_func("rl_cleanup_after_signal")
|
have_readline_func("rl_cleanup_after_signal")
|
||||||
|
|
|
@ -398,6 +398,54 @@ readline_s_get_completion_case_fold(VALUE self)
|
||||||
return rb_attr_get(mReadline, completion_case_fold);
|
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 **
|
static char **
|
||||||
readline_attempted_completion_function(const char *text, int start, int end)
|
readline_attempted_completion_function(const char *text, int start, int end)
|
||||||
{
|
{
|
||||||
|
@ -1262,6 +1310,10 @@ Init_readline()
|
||||||
readline_s_set_completion_case_fold, 1);
|
readline_s_set_completion_case_fold, 1);
|
||||||
rb_define_singleton_method(mReadline, "completion_case_fold",
|
rb_define_singleton_method(mReadline, "completion_case_fold",
|
||||||
readline_s_get_completion_case_fold, 0);
|
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",
|
rb_define_singleton_method(mReadline, "set_screen_size",
|
||||||
readline_s_set_screen_size, 2);
|
readline_s_set_screen_size, 2);
|
||||||
rb_define_singleton_method(mReadline, "get_screen_size",
|
rb_define_singleton_method(mReadline, "get_screen_size",
|
||||||
|
|
|
@ -3,6 +3,8 @@ begin
|
||||||
=begin
|
=begin
|
||||||
class << Readline
|
class << Readline
|
||||||
[
|
[
|
||||||
|
"line_buffer",
|
||||||
|
"point",
|
||||||
"set_screen_size",
|
"set_screen_size",
|
||||||
"get_screen_size",
|
"get_screen_size",
|
||||||
"vi_editing_mode",
|
"vi_editing_mode",
|
||||||
|
@ -63,6 +65,8 @@ class TestReadline < Test::Unit::TestCase
|
||||||
["completer_quote_characters"],
|
["completer_quote_characters"],
|
||||||
["filename_quote_characters=", "\\"],
|
["filename_quote_characters=", "\\"],
|
||||||
["filename_quote_characters"],
|
["filename_quote_characters"],
|
||||||
|
["line_buffer"],
|
||||||
|
["point"],
|
||||||
["set_screen_size", 1, 1],
|
["set_screen_size", 1, 1],
|
||||||
["get_screen_size"],
|
["get_screen_size"],
|
||||||
]
|
]
|
||||||
|
@ -78,41 +82,83 @@ class TestReadline < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_readline
|
if !/EditLine/n.match(Readline::VERSION)
|
||||||
stdin = Tempfile.new("test_readline_stdin")
|
def test_readline
|
||||||
stdout = Tempfile.new("test_readline_stdout")
|
stdin = Tempfile.new("test_readline_stdin")
|
||||||
begin
|
stdout = Tempfile.new("test_readline_stdout")
|
||||||
stdin.write("hello\n")
|
begin
|
||||||
stdin.close
|
stdin.write("hello\n")
|
||||||
stdout.close
|
stdin.close
|
||||||
line = replace_stdio(stdin.path, stdout.path) {
|
stdout.close
|
||||||
Readline.readline("> ", true)
|
line = replace_stdio(stdin.path, stdout.path) {
|
||||||
}
|
Readline.readline("> ", true)
|
||||||
assert_equal("hello", line)
|
}
|
||||||
assert_equal(true, line.tainted?)
|
assert_equal("hello", line)
|
||||||
stdout.open
|
assert_equal(true, line.tainted?)
|
||||||
assert_equal("> ", stdout.read(2))
|
stdout.open
|
||||||
assert_equal(1, Readline::HISTORY.length)
|
assert_equal("> ", stdout.read(2))
|
||||||
assert_equal("hello", Readline::HISTORY[0])
|
assert_equal(1, Readline::HISTORY.length)
|
||||||
assert_raise(SecurityError) do
|
assert_equal("hello", Readline::HISTORY[0])
|
||||||
Thread.start {
|
assert_raise(SecurityError) do
|
||||||
$SAFE = 1
|
Thread.start {
|
||||||
replace_stdio(stdin.path, stdout.path) do
|
$SAFE = 1
|
||||||
Readline.readline("> ".taint)
|
replace_stdio(stdin.path, stdout.path) do
|
||||||
end
|
Readline.readline("> ".taint)
|
||||||
}.join
|
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
|
end
|
||||||
assert_raise(SecurityError) do
|
end
|
||||||
Thread.start {
|
|
||||||
$SAFE = 4
|
# line_buffer
|
||||||
replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") }
|
# point
|
||||||
}.join
|
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
|
end
|
||||||
ensure
|
|
||||||
stdin.close(true)
|
|
||||||
stdout.close(true)
|
|
||||||
end
|
end
|
||||||
end if !/EditLine/n.match(Readline::VERSION)
|
end
|
||||||
|
|
||||||
def test_input=
|
def test_input=
|
||||||
assert_raise(TypeError) do
|
assert_raise(TypeError) do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue