2019-04-27 01:53:09 -04:00
|
|
|
class Reline::Config
|
2019-06-01 18:39:12 -04:00
|
|
|
attr_reader :test_mode
|
|
|
|
|
2019-07-04 07:58:11 -04:00
|
|
|
KEYSEQ_PATTERN = /\\(?:C|Control)-[A-Za-z_]|\\(?:M|Meta)-[0-9A-Za-z_]|\\(?:C|Control)-(?:M|Meta)-[A-Za-z_]|\\(?:M|Meta)-(?:C|Control)-[A-Za-z_]|\\e|\\[\\\"\'abdfnrtv]|\\\d{1,3}|\\x\h{1,2}|./
|
2019-07-04 05:54:50 -04:00
|
|
|
|
2019-06-01 23:14:29 -04:00
|
|
|
class InvalidInputrc < RuntimeError
|
|
|
|
attr_accessor :file, :lineno
|
|
|
|
end
|
|
|
|
|
2019-05-12 14:24:15 -04:00
|
|
|
VARIABLE_NAMES = %w{
|
|
|
|
bind-tty-special-chars
|
|
|
|
blink-matching-paren
|
|
|
|
byte-oriented
|
|
|
|
completion-ignore-case
|
|
|
|
convert-meta
|
|
|
|
disable-completion
|
|
|
|
enable-keypad
|
|
|
|
expand-tilde
|
|
|
|
history-preserve-point
|
2019-05-12 14:26:10 -04:00
|
|
|
history-size
|
2019-05-12 14:24:15 -04:00
|
|
|
horizontal-scroll-mode
|
|
|
|
input-meta
|
2019-05-24 10:38:40 -04:00
|
|
|
keyseq-timeout
|
2019-05-12 14:24:15 -04:00
|
|
|
mark-directories
|
|
|
|
mark-modified-lines
|
|
|
|
mark-symlinked-directories
|
|
|
|
match-hidden-files
|
|
|
|
meta-flag
|
|
|
|
output-meta
|
|
|
|
page-completions
|
|
|
|
prefer-visible-bell
|
|
|
|
print-completions-horizontally
|
|
|
|
show-all-if-ambiguous
|
|
|
|
show-all-if-unmodified
|
|
|
|
visible-stats
|
2020-07-12 17:47:43 -04:00
|
|
|
show-mode-in-prompt
|
2020-12-28 23:28:14 -05:00
|
|
|
vi-cmd-mode-string
|
|
|
|
vi-ins-mode-string
|
2020-07-12 17:47:43 -04:00
|
|
|
emacs-mode-string
|
2020-11-26 05:13:34 -05:00
|
|
|
enable-bracketed-paste
|
2020-12-21 14:38:01 -05:00
|
|
|
isearch-terminators
|
2019-05-12 14:24:15 -04:00
|
|
|
}
|
|
|
|
VARIABLE_NAME_SYMBOLS = VARIABLE_NAMES.map { |v| :"#{v.tr(?-, ?_)}" }
|
|
|
|
VARIABLE_NAME_SYMBOLS.each do |v|
|
|
|
|
attr_accessor v
|
|
|
|
end
|
|
|
|
|
2019-04-27 01:53:09 -04:00
|
|
|
def initialize
|
2019-06-01 18:28:26 -04:00
|
|
|
@additional_key_bindings = {} # from inputrc
|
2021-04-05 03:03:53 -04:00
|
|
|
@additional_key_bindings[:emacs] = {}
|
|
|
|
@additional_key_bindings[:vi_insert] = {}
|
|
|
|
@additional_key_bindings[:vi_command] = {}
|
2019-04-27 01:53:09 -04:00
|
|
|
@skip_section = nil
|
2019-06-01 23:14:29 -04:00
|
|
|
@if_stack = nil
|
2019-04-27 01:53:09 -04:00
|
|
|
@editing_mode_label = :emacs
|
|
|
|
@keymap_label = :emacs
|
|
|
|
@key_actors = {}
|
|
|
|
@key_actors[:emacs] = Reline::KeyActor::Emacs.new
|
|
|
|
@key_actors[:vi_insert] = Reline::KeyActor::ViInsert.new
|
|
|
|
@key_actors[:vi_command] = Reline::KeyActor::ViCommand.new
|
2020-12-28 23:28:14 -05:00
|
|
|
@vi_cmd_mode_string = '(cmd)'
|
|
|
|
@vi_ins_mode_string = '(ins)'
|
2020-07-12 17:47:43 -04:00
|
|
|
@emacs_mode_string = '@'
|
2020-04-25 14:18:38 -04:00
|
|
|
# https://tiswww.case.edu/php/chet/readline/readline.html#IDX25
|
2020-04-24 13:20:52 -04:00
|
|
|
@history_size = -1 # unlimited
|
2019-05-24 10:38:40 -04:00
|
|
|
@keyseq_timeout = 500
|
2019-06-01 18:39:12 -04:00
|
|
|
@test_mode = false
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
if editing_mode_is?(:vi_command)
|
|
|
|
@editing_mode_label = :vi_insert
|
|
|
|
end
|
2021-04-05 03:03:53 -04:00
|
|
|
@additional_key_bindings.keys.each do |key|
|
|
|
|
@additional_key_bindings[key].clear
|
|
|
|
end
|
2021-04-08 22:57:23 -04:00
|
|
|
reset_default_key_bindings
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def editing_mode
|
|
|
|
@key_actors[@editing_mode_label]
|
|
|
|
end
|
|
|
|
|
|
|
|
def editing_mode=(val)
|
|
|
|
@editing_mode_label = val
|
|
|
|
end
|
|
|
|
|
|
|
|
def editing_mode_is?(*val)
|
|
|
|
(val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label)
|
|
|
|
end
|
|
|
|
|
|
|
|
def keymap
|
|
|
|
@key_actors[@keymap_label]
|
|
|
|
end
|
|
|
|
|
2020-03-21 05:13:50 -04:00
|
|
|
def inputrc_path
|
|
|
|
case ENV['INPUTRC']
|
|
|
|
when nil, ''
|
|
|
|
else
|
2020-04-21 05:17:27 -04:00
|
|
|
return File.expand_path(ENV['INPUTRC'])
|
|
|
|
end
|
|
|
|
|
|
|
|
# In the XDG Specification, if ~/.config/readline/inputrc exists, then
|
|
|
|
# ~/.inputrc should not be read, but for compatibility with GNU Readline,
|
|
|
|
# if ~/.inputrc exists, then it is given priority.
|
2020-04-23 14:10:41 -04:00
|
|
|
home_rc_path = File.expand_path('~/.inputrc')
|
|
|
|
return home_rc_path if File.exist?(home_rc_path)
|
2020-04-21 05:17:27 -04:00
|
|
|
|
2020-04-24 04:54:54 -04:00
|
|
|
case path = ENV['XDG_CONFIG_HOME']
|
2020-04-21 05:17:27 -04:00
|
|
|
when nil, ''
|
|
|
|
else
|
2020-04-24 04:54:54 -04:00
|
|
|
path = File.join(path, 'readline/inputrc')
|
|
|
|
return path if File.exist?(path) and path == File.expand_path(path)
|
2020-03-21 05:13:50 -04:00
|
|
|
end
|
2020-04-23 14:10:41 -04:00
|
|
|
|
2020-04-24 04:54:54 -04:00
|
|
|
path = File.expand_path('~/.config/readline/inputrc')
|
|
|
|
return path if File.exist?(path)
|
|
|
|
|
2020-04-23 14:10:41 -04:00
|
|
|
return home_rc_path
|
2020-03-21 05:13:50 -04:00
|
|
|
end
|
|
|
|
|
2019-06-01 23:06:49 -04:00
|
|
|
def read(file = nil)
|
2020-04-21 05:17:27 -04:00
|
|
|
file ||= inputrc_path
|
2019-04-27 01:53:09 -04:00
|
|
|
begin
|
|
|
|
if file.respond_to?(:readlines)
|
|
|
|
lines = file.readlines
|
|
|
|
else
|
2019-06-01 01:57:36 -04:00
|
|
|
lines = File.readlines(file)
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2019-06-01 23:14:29 -04:00
|
|
|
read_lines(lines, file)
|
2019-04-27 01:53:09 -04:00
|
|
|
self
|
2019-06-01 23:14:29 -04:00
|
|
|
rescue InvalidInputrc => e
|
|
|
|
warn e.message
|
|
|
|
nil
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
|
2019-05-31 20:05:58 -04:00
|
|
|
def key_bindings
|
2021-04-05 03:03:53 -04:00
|
|
|
# override @key_actors[@editing_mode_label].default_key_bindings with @additional_key_bindings[@editing_mode_label]
|
|
|
|
@key_actors[@editing_mode_label].default_key_bindings.merge(@additional_key_bindings[@editing_mode_label])
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_default_key_binding_by_keymap(keymap, keystroke, target)
|
|
|
|
@key_actors[keymap].default_key_bindings[keystroke] = target
|
2019-05-31 20:05:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_default_key_binding(keystroke, target)
|
2021-04-05 03:03:53 -04:00
|
|
|
@key_actors[@keymap_label].default_key_bindings[keystroke] = target
|
2019-05-31 20:05:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_default_key_bindings
|
2021-04-05 03:03:53 -04:00
|
|
|
@key_actors.values.each do |ka|
|
|
|
|
ka.reset_default_key_bindings
|
|
|
|
end
|
2019-05-31 20:05:58 -04:00
|
|
|
end
|
|
|
|
|
2019-06-01 23:14:29 -04:00
|
|
|
def read_lines(lines, file = nil)
|
|
|
|
conditions = [@skip_section, @if_stack]
|
|
|
|
@skip_section = nil
|
|
|
|
@if_stack = []
|
|
|
|
|
|
|
|
lines.each_with_index do |line, no|
|
2019-07-04 05:46:34 -04:00
|
|
|
next if line.match(/\A\s*#/)
|
2019-05-31 20:05:58 -04:00
|
|
|
|
2019-06-01 23:14:29 -04:00
|
|
|
no += 1
|
|
|
|
|
2019-07-04 05:49:42 -04:00
|
|
|
line = line.chomp.lstrip
|
2019-07-27 20:52:07 -04:00
|
|
|
if line.start_with?('$')
|
2019-06-01 23:14:29 -04:00
|
|
|
handle_directive(line[1..-1], file, no)
|
2019-04-27 01:53:09 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
next if @skip_section
|
|
|
|
|
2019-07-27 20:52:07 -04:00
|
|
|
case line
|
|
|
|
when /^set +([^ ]+) +([^ ]+)/i
|
2020-07-20 07:52:07 -04:00
|
|
|
var, value = $1.downcase, $2
|
2019-04-27 01:53:09 -04:00
|
|
|
bind_variable(var, value)
|
|
|
|
next
|
2019-07-27 20:52:07 -04:00
|
|
|
when /\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/o
|
2019-04-27 01:53:09 -04:00
|
|
|
key, func_name = $1, $2
|
2019-05-31 20:05:58 -04:00
|
|
|
keystroke, func = bind_key(key, func_name)
|
2019-07-04 05:54:50 -04:00
|
|
|
next unless keystroke
|
2021-04-05 03:03:53 -04:00
|
|
|
@additional_key_bindings[@keymap_label][keystroke] = func
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-06-01 23:14:29 -04:00
|
|
|
unless @if_stack.empty?
|
|
|
|
raise InvalidInputrc, "#{file}:#{@if_stack.last[1]}: unclosed if"
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@skip_section, @if_stack = conditions
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
|
2019-06-01 23:14:29 -04:00
|
|
|
def handle_directive(directive, file, no)
|
2019-04-27 01:53:09 -04:00
|
|
|
directive, args = directive.split(' ')
|
|
|
|
case directive
|
|
|
|
when 'if'
|
|
|
|
condition = false
|
2019-12-18 08:57:25 -05:00
|
|
|
case args
|
2019-04-27 01:53:09 -04:00
|
|
|
when 'mode'
|
|
|
|
when 'term'
|
|
|
|
when 'version'
|
|
|
|
else # application name
|
|
|
|
condition = true if args == 'Ruby'
|
2019-06-01 23:21:09 -04:00
|
|
|
condition = true if args == 'Reline'
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
2019-06-01 23:14:29 -04:00
|
|
|
@if_stack << [file, no, @skip_section]
|
2019-04-27 01:53:09 -04:00
|
|
|
@skip_section = !condition
|
|
|
|
when 'else'
|
2019-06-01 23:14:29 -04:00
|
|
|
if @if_stack.empty?
|
|
|
|
raise InvalidInputrc, "#{file}:#{no}: unmatched else"
|
|
|
|
end
|
2019-04-27 01:53:09 -04:00
|
|
|
@skip_section = !@skip_section
|
|
|
|
when 'endif'
|
2019-06-01 23:14:29 -04:00
|
|
|
if @if_stack.empty?
|
|
|
|
raise InvalidInputrc, "#{file}:#{no}: unmatched endif"
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
2019-06-01 23:14:29 -04:00
|
|
|
@skip_section = @if_stack.pop
|
2019-04-27 01:53:09 -04:00
|
|
|
when 'include'
|
|
|
|
read(args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bind_variable(name, value)
|
|
|
|
case name
|
2020-01-06 07:50:48 -05:00
|
|
|
when 'history-size'
|
2020-05-03 01:36:33 -04:00
|
|
|
begin
|
|
|
|
@history_size = Integer(value)
|
|
|
|
rescue ArgumentError
|
|
|
|
@history_size = 500
|
|
|
|
end
|
2019-04-27 01:53:09 -04:00
|
|
|
when 'bell-style'
|
|
|
|
@bell_style =
|
|
|
|
case value
|
|
|
|
when 'none', 'off'
|
|
|
|
:none
|
|
|
|
when 'audible', 'on'
|
|
|
|
:audible
|
|
|
|
when 'visible'
|
|
|
|
:visible
|
|
|
|
else
|
|
|
|
:audible
|
|
|
|
end
|
|
|
|
when 'comment-begin'
|
|
|
|
@comment_begin = value.dup
|
|
|
|
when 'completion-query-items'
|
|
|
|
@completion_query_items = value.to_i
|
|
|
|
when 'isearch-terminators'
|
2020-12-21 14:38:01 -05:00
|
|
|
@isearch_terminators = retrieve_string(value)
|
2019-04-27 01:53:09 -04:00
|
|
|
when 'editing-mode'
|
|
|
|
case value
|
|
|
|
when 'emacs'
|
|
|
|
@editing_mode_label = :emacs
|
|
|
|
@keymap_label = :emacs
|
|
|
|
when 'vi'
|
|
|
|
@editing_mode_label = :vi_insert
|
|
|
|
@keymap_label = :vi_insert
|
|
|
|
end
|
|
|
|
when 'keymap'
|
|
|
|
case value
|
|
|
|
when 'emacs', 'emacs-standard', 'emacs-meta', 'emacs-ctlx'
|
|
|
|
@keymap_label = :emacs
|
|
|
|
when 'vi', 'vi-move', 'vi-command'
|
|
|
|
@keymap_label = :vi_command
|
|
|
|
when 'vi-insert'
|
|
|
|
@keymap_label = :vi_insert
|
|
|
|
end
|
2019-05-24 10:38:40 -04:00
|
|
|
when 'keyseq-timeout'
|
|
|
|
@keyseq_timeout = value.to_i
|
2020-07-12 17:47:43 -04:00
|
|
|
when 'show-mode-in-prompt'
|
|
|
|
case value
|
|
|
|
when 'off'
|
|
|
|
@show_mode_in_prompt = false
|
|
|
|
when 'on'
|
|
|
|
@show_mode_in_prompt = true
|
|
|
|
else
|
|
|
|
@show_mode_in_prompt = false
|
|
|
|
end
|
|
|
|
when 'vi-cmd-mode-string'
|
2020-12-28 23:28:14 -05:00
|
|
|
@vi_cmd_mode_string = retrieve_string(value)
|
2020-07-12 17:47:43 -04:00
|
|
|
when 'vi-ins-mode-string'
|
2020-12-28 23:28:14 -05:00
|
|
|
@vi_ins_mode_string = retrieve_string(value)
|
2020-07-12 17:47:43 -04:00
|
|
|
when 'emacs-mode-string'
|
2020-07-20 07:52:07 -04:00
|
|
|
@emacs_mode_string = retrieve_string(value)
|
2020-01-06 07:50:48 -05:00
|
|
|
when *VARIABLE_NAMES then
|
|
|
|
variable_name = :"@#{name.tr(?-, ?_)}"
|
|
|
|
instance_variable_set(variable_name, value.nil? || value == '1' || value == 'on')
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-20 07:52:07 -04:00
|
|
|
def retrieve_string(str)
|
|
|
|
if str =~ /\A"(.*)"\z/
|
|
|
|
parse_keyseq($1).map(&:chr).join
|
|
|
|
else
|
|
|
|
parse_keyseq(str).map(&:chr).join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-27 01:53:09 -04:00
|
|
|
def bind_key(key, func_name)
|
2019-07-04 05:54:50 -04:00
|
|
|
if key =~ /\A"(.*)"\z/
|
2019-05-31 20:05:58 -04:00
|
|
|
keyseq = parse_keyseq($1)
|
2019-04-27 01:53:09 -04:00
|
|
|
else
|
|
|
|
keyseq = nil
|
|
|
|
end
|
|
|
|
if func_name =~ /"(.*)"/
|
2019-05-31 20:05:58 -04:00
|
|
|
func = parse_keyseq($1)
|
2019-04-27 01:53:09 -04:00
|
|
|
else
|
2019-05-31 20:05:58 -04:00
|
|
|
func = func_name.tr(?-, ?_).to_sym # It must be macro.
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
[keyseq, func]
|
|
|
|
end
|
|
|
|
|
2019-05-31 20:05:58 -04:00
|
|
|
def key_notation_to_code(notation)
|
2019-04-27 01:53:09 -04:00
|
|
|
case notation
|
2019-07-04 07:58:11 -04:00
|
|
|
when /\\(?:C|Control)-([A-Za-z_])/
|
2019-05-31 20:05:58 -04:00
|
|
|
(1 + $1.downcase.ord - ?a.ord)
|
2019-07-04 07:58:11 -04:00
|
|
|
when /\\(?:M|Meta)-([0-9A-Za-z_])/
|
2019-04-27 01:53:09 -04:00
|
|
|
modified_key = $1
|
2019-05-31 20:05:58 -04:00
|
|
|
case $1
|
|
|
|
when /[0-9]/
|
|
|
|
?\M-0.bytes.first + (modified_key.ord - ?0.ord)
|
|
|
|
when /[A-Z]/
|
|
|
|
?\M-A.bytes.first + (modified_key.ord - ?A.ord)
|
|
|
|
when /[a-z]/
|
|
|
|
?\M-a.bytes.first + (modified_key.ord - ?a.ord)
|
|
|
|
end
|
2019-07-04 07:58:11 -04:00
|
|
|
when /\\(?:C|Control)-(?:M|Meta)-[A-Za-z_]/, /\\(?:M|Meta)-(?:C|Control)-[A-Za-z_]/
|
2019-04-27 01:53:09 -04:00
|
|
|
# 129 M-^A
|
2019-05-31 20:05:58 -04:00
|
|
|
when /\\(\d{1,3})/ then $1.to_i(8) # octal
|
|
|
|
when /\\x(\h{1,2})/ then $1.to_i(16) # hexadecimal
|
|
|
|
when "\\e" then ?\e.ord
|
|
|
|
when "\\\\" then ?\\.ord
|
|
|
|
when "\\\"" then ?".ord
|
|
|
|
when "\\'" then ?'.ord
|
|
|
|
when "\\a" then ?\a.ord
|
|
|
|
when "\\b" then ?\b.ord
|
|
|
|
when "\\d" then ?\d.ord
|
|
|
|
when "\\f" then ?\f.ord
|
|
|
|
when "\\n" then ?\n.ord
|
|
|
|
when "\\r" then ?\r.ord
|
|
|
|
when "\\t" then ?\t.ord
|
|
|
|
when "\\v" then ?\v.ord
|
|
|
|
else notation.ord
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_keyseq(str)
|
2019-05-31 20:05:58 -04:00
|
|
|
ret = []
|
2019-07-04 05:54:50 -04:00
|
|
|
str.scan(KEYSEQ_PATTERN) do
|
2019-05-31 20:05:58 -04:00
|
|
|
ret << key_notation_to_code($&)
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
end
|