Project

General

Profile

Feature #13050 ยป readline_completion_quote_character.patch

georgebrock (George Brocklehurst), 12/18/2016 10:48 PM

View differences:

ext/readline/extconf.rb
71 71
readline.have_func("rl_refresh_line")
72 72
readline.have_var("rl_deprep_term_function")
73 73
readline.have_var("rl_completion_append_character")
74
readline.have_var("rl_completion_quote_character")
74 75
readline.have_var("rl_basic_word_break_characters")
75 76
readline.have_var("rl_completer_word_break_characters")
76 77
readline.have_var("rl_basic_quote_characters")
ext/readline/readline.c
1301 1301
#define readline_s_get_completion_append_character rb_f_notimplement
1302 1302
#endif
1303 1303

  
1304
#ifdef HAVE_RL_COMPLETION_QUOTE_CHARACTER
1305
/*
1306
 * call-seq:
1307
 *   Readline.completion_quote_character -> char
1308
 *
1309
 * When called during a completion (e.g. from within your completion_proc),
1310
 * it will return a string containing the chracter used to quote the
1311
 * argument being completed, or nil if the argument is unquoted.
1312
 *
1313
 * When called at other times, it will always return nil.
1314
 *
1315
 * Note that ``Readline.completer_quote_characters`` must be set,
1316
 * or this method will always return nil.
1317
 */
1318
static VALUE
1319
readline_s_get_completion_quote_character(VALUE self)
1320
{
1321
    char buf[1];
1322

  
1323
    if (rl_completion_quote_character == '\0')
1324
        return Qnil;
1325

  
1326
    buf[0] = (char) rl_completion_quote_character;
1327
    return rb_locale_str_new(buf, 1);
1328
}
1329
#else
1330
#define readline_s_get_completion_quote_character rb_f_notimplement
1331
#endif
1332

  
1304 1333
#ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
1305 1334
/*
1306 1335
 * call-seq:
......
1956 1985
                               readline_s_set_completion_append_character, 1);
1957 1986
    rb_define_singleton_method(mReadline, "completion_append_character",
1958 1987
                               readline_s_get_completion_append_character, 0);
1988
    rb_define_singleton_method(mReadline, "completion_quote_character",
1989
                               readline_s_get_completion_quote_character, 0);
1959 1990
    rb_define_singleton_method(mReadline, "basic_word_break_characters=",
1960 1991
                               readline_s_set_basic_word_break_characters, 1);
1961 1992
    rb_define_singleton_method(mReadline, "basic_word_break_characters",
test/readline/test_readline.rb
550 550
    Readline.completer_word_break_characters = saved_completer_word_break_characters
551 551
  end
552 552

  
553
  def test_completion_quote_character_completing_unquoted_argument
554
    return unless Readline.respond_to?(:completion_quote_character)
555

  
556
    quote_character = "original value"
557
    Readline.completion_proc = -> (_) do
558
      quote_character = Readline.completion_quote_character
559
      []
560
    end
561
    Readline.completer_quote_characters = "'\""
562

  
563
    with_temp_stdio do |stdin, stdout|
564
      replace_stdio(stdin.path, stdout.path) do
565
        stdin.write("input\t")
566
        stdin.flush
567
        Readline.readline("> ", false)
568
      end
569
    end
570

  
571
    assert_nil(quote_character)
572
  end
573

  
574
  def test_completion_quote_character_completing_quoted_argument
575
    return unless Readline.respond_to?(:completion_quote_character)
576

  
577
    quote_character = "original value"
578
    Readline.completion_proc = -> (_) do
579
      quote_character = Readline.completion_quote_character
580
      []
581
    end
582
    Readline.completer_quote_characters = "'\""
583

  
584
    with_temp_stdio do |stdin, stdout|
585
      replace_stdio(stdin.path, stdout.path) do
586
        stdin.write("'input\t")
587
        stdin.flush
588
        Readline.readline("> ", false)
589
      end
590
    end
591

  
592
    assert_equal("'", quote_character)
593
  end
594

  
595
  def test_completion_quote_character_after_completion
596
    return unless Readline.respond_to?(:completion_quote_character)
597

  
598
    Readline.completion_proc = -> (_) { [] }
599
    Readline.completer_quote_characters = "'\""
600

  
601
    with_temp_stdio do |stdin, stdout|
602
      replace_stdio(stdin.path, stdout.path) do
603
        stdin.write("'input\t")
604
        stdin.flush
605
        Readline.readline("> ", false)
606
      end
607
    end
608

  
609
    assert_nil(Readline.completion_quote_character)
610
  end
611

  
553 612
  private
554 613

  
555 614
  def replace_stdio(stdin_path, stdout_path)