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

ruby-changes:30049

From: kouji <ko1@a...>
Date: Mon, 22 Jul 2013 09:37:21 +0900 (JST)
Subject: [ruby-changes:30049] kouji:r42101 (trunk): * ext/readline/readline.c (Init_readline): added

kouji	2013-07-22 09:37:10 +0900 (Mon, 22 Jul 2013)

  New Revision: 42101

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42101

  Log:
    * ext/readline/readline.c (Init_readline): added
      Readline.delete_text. [ruby-dev:45789] [Feature #6626]
    * ext/readline/extconf.rb: check for rl_delete_text() in Readline library.
    
      Thanks, Nobuyoshi Nakada, for the patch.

  Modified files:
    trunk/ChangeLog
    trunk/ext/readline/extconf.rb
    trunk/ext/readline/readline.c
    trunk/test/readline/test_readline.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42100)
+++ ChangeLog	(revision 42101)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jul 22 09:24:19 2013  Kouji Takao  <kouji@t...>
+
+	* ext/readline/readline.c (Init_readline): added
+	  Readline.delete_text. [ruby-dev:45789] [Feature #6626]
+	* ext/readline/extconf.rb: check for rl_delete_text() in Readline library.
+
+	  Thanks, Nobuyoshi Nakada, for the patch.
+
 Mon Jul 22 03:15:54 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/date/date_parse.c (rfc2822_cb): check if wday is given, since it
Index: ext/readline/readline.c
===================================================================
--- ext/readline/readline.c	(revision 42100)
+++ ext/readline/readline.c	(revision 42101)
@@ -558,6 +558,58 @@ readline_s_insert_text(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L558
 #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:
@@ -1747,6 +1799,8 @@ Init_readline() https://github.com/ruby/ruby/blob/trunk/ext/readline/readline.c#L1799
 			       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=",
Index: ext/readline/extconf.rb
===================================================================
--- ext/readline/extconf.rb	(revision 42100)
+++ ext/readline/extconf.rb	(revision 42101)
@@ -93,4 +93,5 @@ readline.have_func("remove_history") https://github.com/ruby/ruby/blob/trunk/ext/readline/extconf.rb#L93
 readline.have_func("clear_history")
 readline.have_func("rl_redisplay")
 readline.have_func("rl_insert_text")
+readline.have_func("rl_delete_text")
 create_makefile("readline")
Index: test/readline/test_readline.rb
===================================================================
--- test/readline/test_readline.rb	(revision 42100)
+++ test/readline/test_readline.rb	(revision 42101)
@@ -317,6 +317,25 @@ class TestReadline < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/readline/test_readline.rb#L317
       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)
@@ -358,7 +377,7 @@ class TestReadline < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/readline/test_readline.rb#L377
     assert_equal("hello", line, bug6601)
   ensure
     wo.close
-    with_pipe {|r, w| w.write("\C-a\C-k\n")} # clear line_buffer
+    Readline.delete_text
     Readline::HISTORY.clear
   end if !/EditLine/n.match(Readline::VERSION)
 
@@ -384,7 +403,7 @@ class TestReadline < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/readline/test_readline.rb#L403
       end
     end
   ensure
-    with_pipe {|r, w| w.write("\C-a\C-k\n")} # clear line_buffer
+    Readline.delete_text
     Readline::HISTORY.clear
   end if !/EditLine/n.match(Readline::VERSION)
 

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

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