[前][次][番号順一覧][スレッド一覧]

ruby-changes:52409

From: nobu <ko1@a...>
Date: Mon, 3 Sep 2018 08:27:14 +0900 (JST)
Subject: [ruby-changes:52409] nobu:r64618 (trunk): Readline: expose rl_completion_quote_character variable

nobu	2018-09-03 08:27:08 +0900 (Mon, 03 Sep 2018)

  New Revision: 64618

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64618

  Log:
    Readline: expose rl_completion_quote_character variable
    
    [Feature #13050]
    
    From: georgebrock (George Brocklehurst) <ruby@g...>

  Modified files:
    trunk/ext/readline/extconf.rb
    trunk/ext/readline/readline.c
    trunk/test/readline/test_readline.rb
Index: ext/readline/extconf.rb
===================================================================
--- ext/readline/extconf.rb	(revision 64617)
+++ ext/readline/extconf.rb	(revision 64618)
@@ -71,6 +71,7 @@ readline.have_func("rl_completion_matche https://github.com/ruby/ruby/blob/trunk/ext/readline/extconf.rb#L71
 readline.have_func("rl_refresh_line")
 readline.have_var("rl_deprep_term_function")
 readline.have_var("rl_completion_append_character")
+readline.have_var("rl_completion_quote_character")
 readline.have_var("rl_basic_word_break_characters")
 readline.have_var("rl_completer_word_break_characters")
 readline.have_var("rl_basic_quote_characters")
Index: ext/readline/readline.c
===================================================================
--- ext/readline/readline.c	(revision 64617)
+++ ext/readline/readline.c	(revision 64618)
@@ -1303,6 +1303,35 @@ readline_s_get_completion_append_charact https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L1303
 #define readline_s_get_completion_append_character rb_f_notimplement
 #endif
 
+#ifdef HAVE_RL_COMPLETION_QUOTE_CHARACTER
+/*
+ * call-seq:
+ *   Readline.completion_quote_character -> char
+ *
+ * When called during a completion (e.g. from within your completion_proc),
+ * it will return a string containing the chracter used to quote the
+ * argument being completed, or nil if the argument is unquoted.
+ *
+ * When called at other times, it will always return nil.
+ *
+ * Note that ``Readline.completer_quote_characters`` must be set,
+ * or this method will always return nil.
+ */
+static VALUE
+readline_s_get_completion_quote_character(VALUE self)
+{
+    char buf[1];
+
+    if (rl_completion_quote_character == '\0')
+        return Qnil;
+
+    buf[0] = (char) rl_completion_quote_character;
+    return rb_locale_str_new(buf, 1);
+}
+#else
+#define readline_s_get_completion_quote_character rb_f_notimplement
+#endif
+
 #ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
 /*
  * call-seq:
@@ -1958,6 +1987,8 @@ Init_readline(void) https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L1987
                                readline_s_set_completion_append_character, 1);
     rb_define_singleton_method(mReadline, "completion_append_character",
                                readline_s_get_completion_append_character, 0);
+    rb_define_singleton_method(mReadline, "completion_quote_character",
+                               readline_s_get_completion_quote_character, 0);
     rb_define_singleton_method(mReadline, "basic_word_break_characters=",
                                readline_s_set_basic_word_break_characters, 1);
     rb_define_singleton_method(mReadline, "basic_word_break_characters",
Index: test/readline/test_readline.rb
===================================================================
--- test/readline/test_readline.rb	(revision 64617)
+++ test/readline/test_readline.rb	(revision 64618)
@@ -553,6 +553,65 @@ class TestReadline < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/readline/test_readline.rb#L553
     Readline.completer_word_break_characters = saved_completer_word_break_characters
   end
 
+  def test_completion_quote_character_completing_unquoted_argument
+    return unless Readline.respond_to?(:completion_quote_character)
+
+    quote_character = "original value"
+    Readline.completion_proc = -> (_) do
+      quote_character = Readline.completion_quote_character
+      []
+    end
+    Readline.completer_quote_characters = "'\""
+
+    with_temp_stdio do |stdin, stdout|
+      replace_stdio(stdin.path, stdout.path) do
+        stdin.write("input\t")
+        stdin.flush
+        Readline.readline("> ", false)
+      end
+    end
+
+    assert_nil(quote_character)
+  end
+
+  def test_completion_quote_character_completing_quoted_argument
+    return unless Readline.respond_to?(:completion_quote_character)
+
+    quote_character = "original value"
+    Readline.completion_proc = -> (_) do
+      quote_character = Readline.completion_quote_character
+      []
+    end
+    Readline.completer_quote_characters = "'\""
+
+    with_temp_stdio do |stdin, stdout|
+      replace_stdio(stdin.path, stdout.path) do
+        stdin.write("'input\t")
+        stdin.flush
+        Readline.readline("> ", false)
+      end
+    end
+
+    assert_equal("'", quote_character)
+  end
+
+  def test_completion_quote_character_after_completion
+    return unless Readline.respond_to?(:completion_quote_character)
+
+    Readline.completion_proc = -> (_) { [] }
+    Readline.completer_quote_characters = "'\""
+
+    with_temp_stdio do |stdin, stdout|
+      replace_stdio(stdin.path, stdout.path) do
+        stdin.write("'input\t")
+        stdin.flush
+        Readline.readline("> ", false)
+      end
+    end
+
+    assert_nil(Readline.completion_quote_character)
+  end
+
   private
 
   def replace_stdio(stdin_path, stdout_path)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]