Skip to content

Commit 59e22dd

Browse files
committed
Handle mode condition in inputrc
1 parent 0d8aea2 commit 59e22dd

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/reline/config.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def read_lines(lines, file = nil)
210210
next
211211
when /\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/o
212212
key, func_name = $1, $2
213+
func_name = func_name.split.first
213214
keystroke, func = bind_key(key, func_name)
214215
next unless keystroke
215216
@additional_key_bindings[@keymap_label][@keymap_prefix + keystroke] = func
@@ -226,7 +227,14 @@ def handle_directive(directive, file, no, if_stack)
226227
when 'if'
227228
condition = false
228229
case args
229-
when 'mode'
230+
when /^mode=(vi|emacs)$/i
231+
mode = $1.downcase
232+
if @editing_mode_label == mode.to_sym
233+
condition = true
234+
@keymap_label = mode == 'emacs' ? :emacs : :vi_insert
235+
else
236+
condition = false
237+
end
230238
when 'term'
231239
when 'version'
232240
else # application name

test/reline/test_config.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,60 @@ def test_unmatched_endif
275275
assert_equal "INPUTRC:1: unmatched endif", e.message
276276
end
277277

278+
def test_if_with_mode
279+
@config.read_lines(<<~LINES.lines)
280+
$if mode=emacs
281+
"\C-e": history-search-backward # comment
282+
$else
283+
"\C-f": history-search-forward
284+
$endif
285+
LINES
286+
287+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
288+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
289+
assert_equal({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
290+
end
291+
292+
def test_else
293+
@config.read_lines(<<~LINES.lines)
294+
$if mode=vi
295+
"\C-e": history-search-backward # comment
296+
$else
297+
"\C-f": history-search-forward
298+
$endif
299+
LINES
300+
301+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
302+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
303+
assert_equal({[6] => :history_search_forward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
304+
end
305+
306+
def test_if_with_invalid_mode
307+
@config.read_lines(<<~LINES.lines)
308+
$if mode=vim
309+
"\C-e": history-search-backward
310+
$else
311+
"\C-f": history-search-forward # comment
312+
$endif
313+
LINES
314+
315+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
316+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
317+
assert_equal({[6] => :history_search_forward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
318+
end
319+
320+
def test_if_without_else_condition
321+
@config.read_lines(<<~LINES.lines)
322+
$if mode=emacs
323+
"\C-e": history-search-backward
324+
$endif
325+
LINES
326+
327+
assert_equal({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
328+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
329+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
330+
end
331+
278332
def test_default_key_bindings
279333
@config.add_default_key_binding('abcd'.bytes, 'EFGH'.bytes)
280334
@config.read_lines(<<~'LINES'.lines)

0 commit comments

Comments
 (0)