Skip to content

Commit

Permalink
Handle mode condition in inputrc
Browse files Browse the repository at this point in the history
  • Loading branch information
ima1zumi committed Apr 24, 2024
1 parent e9d5236 commit 3096fcd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/reline/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def initialize
@oneshot_key_bindings = {}
@skip_section = nil
@if_stack = nil
@editing_mode_section = nil
@editing_mode_label = :emacs
@keymap_label = :emacs
@keymap_prefix = []
Expand Down Expand Up @@ -214,9 +215,14 @@ 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
if @editing_mode_section == 'vi'
@additional_key_bindings[:vi_insert][@keymap_prefix + keystroke] = func
else
@additional_key_bindings[:emacs][@keymap_prefix + keystroke] = func
end
end
end
unless @if_stack.empty?
Expand All @@ -232,7 +238,9 @@ def handle_directive(directive, file, no)
when 'if'
condition = false
case args
when 'mode'
when /^mode=(vi|emacs)$/i
condition = true
@editing_mode_section = $1.downcase == 'emacs' ? 'emacs' : 'vi'
when 'term'
when 'version'
else # application name
Expand All @@ -250,6 +258,7 @@ def handle_directive(directive, file, no)
if @if_stack.empty?
raise InvalidInputrc, "#{file}:#{no}: unmatched endif"
end
@editing_mode_section = nil
@skip_section = @if_stack.pop
when 'include'
read(File.expand_path(args))
Expand Down
40 changes: 40 additions & 0 deletions test/reline/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,46 @@ 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=vi
"\C-e": history-search-backward
$else
"\C-f": history-search-forward
$endif
LINES

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])
assert_equal({}, @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
$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)
Expand Down

0 comments on commit 3096fcd

Please sign in to comment.