Skip to content

Commit bed5fb3

Browse files
authored
Handle mode condition in inputrc (#687)
1 parent 0d8aea2 commit bed5fb3

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lib/reline/config.rb

Lines changed: 8 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,13 @@ 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+
# NOTE: mode=vi means vi-insert mode
233+
mode = 'vi_insert' if mode == 'vi'
234+
if @editing_mode_label == mode.to_sym
235+
condition = true
236+
end
230237
when 'term'
231238
when 'version'
232239
else # application name

test/reline/test_config.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,78 @@ 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({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
288+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
289+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
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({[6] => :history_search_forward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
302+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
303+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
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({[6] => :history_search_forward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
316+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
317+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
318+
end
319+
320+
def test_mode_label_differs_from_keymap_label
321+
@config.read_lines(<<~LINES.lines)
322+
# Sets mode_label and keymap_label to vi
323+
set editing-mode vi
324+
# Change keymap_label to emacs. mode_label is still vi.
325+
set keymap emacs
326+
# condition=true because current mode_label is vi
327+
$if mode=vi
328+
# sets keybinding to current keymap_label=emacs
329+
"\C-e": history-search-backward
330+
$endif
331+
LINES
332+
assert_equal({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
333+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
334+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
335+
end
336+
337+
def test_if_without_else_condition
338+
@config.read_lines(<<~LINES.lines)
339+
set editing-mode vi
340+
$if mode=vi
341+
"\C-e": history-search-backward
342+
$endif
343+
LINES
344+
345+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:emacs])
346+
assert_equal({[5] => :history_search_backward}, @config.instance_variable_get(:@additional_key_bindings)[:vi_insert])
347+
assert_equal({}, @config.instance_variable_get(:@additional_key_bindings)[:vi_command])
348+
end
349+
278350
def test_default_key_bindings
279351
@config.add_default_key_binding('abcd'.bytes, 'EFGH'.bytes)
280352
@config.read_lines(<<~'LINES'.lines)

0 commit comments

Comments
 (0)