[ruby/reline] Handle mode condition in inputrc

(https://github.com/ruby/reline/pull/687)

https://github.com/ruby/reline/commit/bed5fb3d77
This commit is contained in:
Mari Imaizumi 2024-04-29 21:05:31 +09:00 committed by git
parent 435f449b4e
commit 018c5717e5
2 changed files with 80 additions and 1 deletions

View File

@ -210,6 +210,7 @@ class Reline::Config
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,13 @@ class Reline::Config
when 'if'
condition = false
case args
when 'mode'
when /^mode=(vi|emacs)$/i
mode = $1.downcase
# NOTE: mode=vi means vi-insert mode
mode = 'vi_insert' if mode == 'vi'
if @editing_mode_label == mode.to_sym
condition = true
end
when 'term'
when 'version'
else # application name

View File

@ -275,6 +275,78 @@ class Reline::Config::Test < Reline::TestCase
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({[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_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({[6] => :history_search_forward}, @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_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({[6] => :history_search_forward}, @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_mode_label_differs_from_keymap_label
@config.read_lines(<<~LINES.lines)
# Sets mode_label and keymap_label to vi
set editing-mode vi
# Change keymap_label to emacs. mode_label is still vi.
set keymap emacs
# condition=true because current mode_label is vi
$if mode=vi
# sets keybinding to current keymap_label=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_if_without_else_condition
@config.read_lines(<<~LINES.lines)
set editing-mode vi
$if mode=vi
"\C-e": history-search-backward
$endif
LINES
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
assert_equal({[5] => :history_search_backward}, @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)