From f9a8a6b84e274ea739db47a009d4c6a11bfc9efd Mon Sep 17 00:00:00 2001 From: Mari Imaizumi Date: Fri, 26 Apr 2024 01:17:10 +0900 Subject: [PATCH] Handle mode condition in inputrc --- lib/reline/config.rb | 7 ++++- test/reline/test_config.rb | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/reline/config.rb b/lib/reline/config.rb index d7564ba4b7..0a3a49f0ff 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -210,6 +210,7 @@ def read_lines(lines, file = nil) next when /\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/o key, func_name = $1, $2 + func_name = func_name.split.first keystroke, func = bind_key(key, func_name) next unless keystroke @additional_key_bindings[@keymap_label][@keymap_prefix + keystroke] = func @@ -226,7 +227,11 @@ def handle_directive(directive, file, no, if_stack) when 'if' condition = false case args - when 'mode' + when /^mode=(vi|emacs)$/i + mode = $1.downcase + if @editing_mode_label == mode.to_sym + condition = true + end when 'term' when 'version' else # application name diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index e9f0a9449d..5023b556c9 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -275,6 +275,60 @@ def test_unmatched_endif assert_equal "INPUTRC:1: unmatched endif", e.message end + def test_if_with_mode + @config.read_lines(<<~LINES.lines) + $if mode=emacs + "\C-e": history-search-backward # comment + $else + "\C-f": history-search-forward + $endif + LINES + + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert]) + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command]) + assert_equal({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs]) + end + + def test_else + @config.read_lines(<<~LINES.lines) + $if mode=vi + "\C-e": history-search-backward # comment + $else + "\C-f": history-search-forward + $endif + LINES + + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert]) + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command]) + assert_equal({[6] => :history_search_forward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs]) + end + + def test_if_with_invalid_mode + @config.read_lines(<<~LINES.lines) + $if mode=vim + "\C-e": history-search-backward + $else + "\C-f": history-search-forward # comment + $endif + LINES + + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert]) + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command]) + assert_equal({[6] => :history_search_forward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs]) + end + + def test_if_without_else_condition + @config.read_lines(<<~LINES.lines) + $if mode=emacs + "\C-e": history-search-backward + $endif + LINES + + assert_equal({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs]) + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert]) + assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command]) + end + def test_default_key_bindings @config.add_default_key_binding('abcd'.bytes, 'EFGH'.bytes) @config.read_lines(<<~'LINES'.lines)