Skip to content

Commit

Permalink
Merge pull request #322 from aycabta/enable-autocompletion-in-emacs-mode
Browse files Browse the repository at this point in the history
Enable autocompletion in emacs mode
  • Loading branch information
aycabta authored Aug 29, 2021
2 parents 2668715 + 13d9b93 commit a51b354
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def completion_proc=(p)
@completion_proc = p
end

def autocompletion
@config.autocompletion
end

def autocompletion=(val)
@config.autocompletion = val
end

def output_modifier_proc=(p)
raise ArgumentError unless p.respond_to?(:call) or p.nil?
@output_modifier_proc = p
Expand Down Expand Up @@ -180,6 +188,7 @@ def get_screen_size

Reline::DEFAULT_DIALOG_PROC_AUTOCOMPLETE = ->() {
# autocomplete
return nil unless config.autocompletion
if just_cursor_moving and completion_journey_data.nil?
# Auto complete starts only when edited
return nil
Expand Down
1 change: 1 addition & 0 deletions lib/reline/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def self.set_default_key_bindings(config)
# default bindings
[27, 32] => :em_set_mark, # M-<space>
[24, 24] => :em_exchange_mark, # C-x C-x
[27, 91, 90] => :completion_journey_up, # S-Tab
}.each_pair do |key, func|
config.add_default_key_binding_by_keymap(:emacs, key, func)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/reline/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def initialize
@history_size = -1 # unlimited
@keyseq_timeout = 500
@test_mode = false
@autocompletion = false
end

def reset
Expand All @@ -89,6 +90,14 @@ def editing_mode_is?(*val)
(val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label)
end

def autocompletion=(val)
@autocompletion = val
end

def autocompletion
@autocompletion
end

def keymap
@key_actors[@keymap_label]
end
Expand Down
24 changes: 21 additions & 3 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,9 @@ def rerender
end

class DialogProcScope
def initialize(line_editor, proc_to_exec, context)
def initialize(line_editor, config, proc_to_exec, context)
@line_editor = line_editor
@config = config
@proc_to_exec = proc_to_exec
@context = context
@cursor_pos = Reline::CursorPos.new
Expand Down Expand Up @@ -519,6 +520,10 @@ def completion_journey_data
@line_editor.instance_variable_get(:@completion_journey_data)
end

def config
@config
end

def call
instance_exec(&@proc_to_exec)
end
Expand All @@ -544,7 +549,7 @@ def call

def add_dialog_proc(name, p, context = nil)
return if @dialogs.any? { |d| d.name == name }
@dialogs << Dialog.new(name, DialogProcScope.new(self, p, context))
@dialogs << Dialog.new(name, DialogProcScope.new(self, @config, p, context))
end

DIALOG_HEIGHT = 20
Expand Down Expand Up @@ -1426,7 +1431,20 @@ def input_key(key)
if result.is_a?(Array)
completion_occurs = true
process_insert
complete(result)
if @config.autocompletion
move_completed_list(result, :down)
else
complete(result)
end
end
end
elsif @config.editing_mode_is?(:emacs, :vi_insert) and key.char == :completion_journey_up
if not @config.disable_completion and @config.autocompletion
result = call_completion_proc
if result.is_a?(Array)
completion_occurs = true
process_insert
move_completed_list(result, :up)
end
end
elsif not @config.disable_completion and @config.editing_mode_is?(:vi_insert) and ["\C-p".ord, "\C-n".ord].include?(key.char)
Expand Down
12 changes: 12 additions & 0 deletions lib/reline/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def self.set_default_key_bindings(config)
}.each_pair do |key, func|
config.add_default_key_binding_by_keymap(:emacs, key, func)
end

# Emulate ANSI key sequence.
{
[27, 91, 90] => :completion_journey_up, # S-Tab
}.each_pair do |key, func|
config.add_default_key_binding_by_keymap(:emacs, key, func)
config.add_default_key_binding_by_keymap(:vi_insert, key, func)
end
end

if defined? JRUBY_VERSION
Expand Down Expand Up @@ -106,6 +114,7 @@ def call(*args)
SCROLLLOCK_ON = 0x0040
SHIFT_PRESSED = 0x0010

VK_TAB = 0x09
VK_END = 0x23
VK_HOME = 0x24
VK_LEFT = 0x25
Expand Down Expand Up @@ -199,6 +208,9 @@ def self.msys_tty?(io=@@hConsoleInputHandle)
[ { control_keys: [], virtual_key_code: VK_DELETE }, [0, 83] ],
[ { control_keys: [], virtual_key_code: VK_HOME }, [0, 71] ],
[ { control_keys: [], virtual_key_code: VK_END }, [0, 79] ],

# Emulate ANSI key sequence.
[ { control_keys: :SHIFT, virtual_key_code: VK_TAB }, [27, 91, 90] ],
]

def self.process_key_event(repeat_count, virtual_key_code, virtual_scan_code, char_code, control_key_state)
Expand Down
1 change: 1 addition & 0 deletions test/reline/test_key_actor_emacs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def setup
Reline.send(:test_mode)
@prompt = '> '
@config = Reline::Config.new # Emacs mode is default
@config.autocompletion = false
Reline::HISTORY.instance_variable_set(:@config, @config)
Reline::HISTORY.clear
@encoding = Reline::IOGate.encoding
Expand Down

0 comments on commit a51b354

Please sign in to comment.