Project

General

Profile

Feature #6626 ยป 0002-ext-readline-readline.c-Readline.delete_text.patch

nobu (Nobuyoshi Nakada), 06/22/2012 03:20 PM

View differences:

ext/readline/extconf.rb
97 97
readline.have_func("clear_history")
98 98
readline.have_func("rl_redisplay")
99 99
readline.have_func("rl_insert_text")
100
readline.have_func("rl_delete_text")
100 101
readline.have_macro("RL_PROMPT_START_IGNORE")
101 102
readline.have_macro("RL_PROMPT_END_IGNORE")
102 103
create_makefile("readline")
ext/readline/readline.c
564 564
#define readline_s_insert_text rb_f_notimplement
565 565
#endif
566 566

  
567
#if defined(HAVE_RL_DELETE_TEXT)
568
/*
569
 * call-seq:
570
 *   Readline.delete_text([start[, length]]) -> self
571
 *   Readline.delete_text(start..end)        -> self
572
 *   Readline.delete_text()                  -> self
573
 *
574
 * Delete text between start and end in the current line.
575
 *
576
 * See GNU Readline's rl_delete_text function.
577
 *
578
 * Raises SecurityError if $SAFE is 4.
579
 */
580
static VALUE
581
readline_s_delete_text(int argc, VALUE *argv, VALUE self)
582
{
583
    rb_secure(4);
584
    rb_check_arity(argc, 0, 2);
585
    if (rl_line_buffer) {
586
	char *p, *ptr = rl_line_buffer;
587
	long beg = 0, len = strlen(rl_line_buffer);
588
	struct RString fakestr;
589
	VALUE str = (VALUE)&fakestr;
590

  
591
	fakestr.basic.flags = T_STRING | RSTRING_NOEMBED;
592
	fakestr.as.heap.ptr = ptr;
593
	fakestr.as.heap.len = len;
594
	rb_enc_associate(str, rb_locale_encoding());
595
	OBJ_FREEZE(str);
596
	if (argc == 2) {
597
	    beg = NUM2LONG(argv[0]);
598
	    len = NUM2LONG(argv[1]);
599
	  num_pos:
600
	    p = rb_str_subpos(str, beg, &len);
601
	    if (!p) rb_raise(rb_eArgError, "invalid index");
602
	    beg = p - ptr;
603
	}
604
	else if (argc == 1) {
605
	    len = rb_str_strlen(str);
606
	    if (!rb_range_beg_len(argv[0], &beg, &len, len, 1)) {
607
		beg = NUM2LONG(argv[0]);
608
		goto num_pos;
609
	    }
610
	}
611
	rl_delete_text(rb_long2int(beg), rb_long2int(beg + len));
612
    }
613
    return self;
614
}
615
#else
616
#define readline_s_delete_text rb_f_notimplement
617
#endif
618

  
567 619
#if defined(HAVE_RL_REDISPLAY)
568 620
/*
569 621
 * call-seq:
......
1793 1845
			       readline_s_get_pre_input_hook, 0);
1794 1846
    rb_define_singleton_method(mReadline, "insert_text",
1795 1847
			       readline_s_insert_text, 1);
1848
    rb_define_singleton_method(mReadline, "delete_text",
1849
			       readline_s_delete_text, -1);
1796 1850
    rb_define_singleton_method(mReadline, "redisplay",
1797 1851
			       readline_s_redisplay, 0);
1798 1852
    rb_define_singleton_method(mReadline, "special_prefixes=",
test/readline/test_readline.rb
369 369
      assert_equal(str, Readline.line_buffer)
370 370
      assert_equal(get_default_internal_encoding,
371 371
                   Readline.line_buffer.encoding)
372

  
373
      Readline.delete_text(1, 3)
374
      assert_equal("t_insert_text", Readline.line_buffer)
375
      Readline.delete_text(11)
376
      assert_equal("t_insert_te", Readline.line_buffer)
377
      Readline.delete_text(-3...-1)
378
      assert_equal("t_inserte", Readline.line_buffer)
379
      Readline.delete_text(-3..-1)
380
      assert_equal("t_inse", Readline.line_buffer)
381
      Readline.delete_text(3..-3)
382
      assert_equal("t_ise", Readline.line_buffer)
383
      Readline.delete_text(3, 1)
384
      assert_equal("t_ie", Readline.line_buffer)
385
      Readline.delete_text(1..1)
386
      assert_equal("tie", Readline.line_buffer)
387
      Readline.delete_text(1...2)
388
      assert_equal("te", Readline.line_buffer)
389
      Readline.delete_text
390
      assert_equal("", Readline.line_buffer)
372 391
    rescue NotImplementedError
373 392
    end
374 393
  end if !/EditLine/n.match(Readline::VERSION)