Class: Reline::Config
- Inherits:
-
Object
- Object
- Reline::Config
- Defined in:
- lib/reline/config.rb
Defined Under Namespace
Classes: InvalidInputrc
Constant Summary collapse
- 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}|./
- VARIABLE_NAMES =
%w{ completion-ignore-case convert-meta disable-completion history-size keyseq-timeout show-all-if-ambiguous show-mode-in-prompt vi-cmd-mode-string vi-ins-mode-string emacs-mode-string enable-bracketed-paste isearch-terminators }
- VARIABLE_NAME_SYMBOLS =
VARIABLE_NAMES.map { |v| :"#{v.tr(?-, ?_)}" }
Instance Attribute Summary collapse
-
#autocompletion ⇒ Object
Returns the value of attribute autocompletion.
-
#test_mode ⇒ Object
readonly
Returns the value of attribute test_mode.
Instance Method Summary collapse
- #add_default_key_binding(keystroke, target) ⇒ Object
- #add_default_key_binding_by_keymap(keymap, keystroke, target) ⇒ Object
- #add_oneshot_key_binding(keystroke, target) ⇒ Object
- #bind_key(key, value) ⇒ Object
- #bind_variable(name, value, raw_value) ⇒ Object
- #editing_mode ⇒ Object
- #editing_mode=(val) ⇒ Object
- #editing_mode_is?(*val) ⇒ Boolean
- #handle_directive(directive, file, no, if_stack) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #inputrc_path ⇒ Object
- #key_bindings ⇒ Object
- #key_notation_to_code(notation) ⇒ Object
- #keymap ⇒ Object
- #loaded? ⇒ Boolean
- #parse_key_binding(key, func_name) ⇒ Object
- #parse_keyseq(str) ⇒ Object
- #read(file = nil) ⇒ Object
- #read_lines(lines, file = nil) ⇒ Object
- #reload ⇒ Object
- #reset ⇒ Object
- #reset_oneshot_key_bindings ⇒ Object
- #reset_variables ⇒ Object
- #retrieve_string(str) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
31 32 33 |
# File 'lib/reline/config.rb', line 31 def initialize reset_variables end |
Instance Attribute Details
#autocompletion ⇒ Object
Returns the value of attribute autocompletion.
29 30 31 |
# File 'lib/reline/config.rb', line 29 def autocompletion @autocompletion end |
#test_mode ⇒ Object (readonly)
Returns the value of attribute test_mode.
2 3 4 |
# File 'lib/reline/config.rb', line 2 def test_mode @test_mode end |
Instance Method Details
#add_default_key_binding(keystroke, target) ⇒ Object
162 163 164 |
# File 'lib/reline/config.rb', line 162 def add_default_key_binding(keystroke, target) add_default_key_binding_by_keymap(@keymap_label, keystroke, target) end |
#add_default_key_binding_by_keymap(keymap, keystroke, target) ⇒ Object
158 159 160 |
# File 'lib/reline/config.rb', line 158 def add_default_key_binding_by_keymap(keymap, keystroke, target) @default_key_bindings[keymap].add(keystroke, target) end |
#add_oneshot_key_binding(keystroke, target) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/reline/config.rb', line 147 def add_oneshot_key_binding(keystroke, target) # IRB sets invalid keystroke [Reline::Key]. We should ignore it. return unless keystroke.all? { |c| c.is_a?(Integer) } @oneshot_key_bindings.add(keystroke, target) end |
#bind_key(key, value) ⇒ Object
314 315 316 317 |
# File 'lib/reline/config.rb', line 314 def bind_key(key, value) keystroke, func = parse_key_binding(key, value) @additional_key_bindings[@keymap_label].add(@keymap_prefix + keystroke, func) if keystroke end |
#bind_variable(name, value, raw_value) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/reline/config.rb', line 247 def bind_variable(name, value, raw_value) case name when 'history-size' begin @history_size = Integer(value) rescue ArgumentError @history_size = 500 end when 'isearch-terminators' @isearch_terminators = retrieve_string(raw_value) when 'editing-mode' case value when 'emacs' @editing_mode_label = :emacs @keymap_label = :emacs @keymap_prefix = [] when 'vi' @editing_mode_label = :vi_insert @keymap_label = :vi_insert @keymap_prefix = [] end when 'keymap' case value when 'emacs', 'emacs-standard' @keymap_label = :emacs @keymap_prefix = [] when 'emacs-ctlx' @keymap_label = :emacs @keymap_prefix = [?\C-x.ord] when 'emacs-meta' @keymap_label = :emacs @keymap_prefix = [?\e.ord] when 'vi', 'vi-move', 'vi-command' @keymap_label = :vi_command @keymap_prefix = [] when 'vi-insert' @keymap_label = :vi_insert @keymap_prefix = [] end when 'keyseq-timeout' @keyseq_timeout = value.to_i 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' @vi_cmd_mode_string = retrieve_string(raw_value) when 'vi-ins-mode-string' @vi_ins_mode_string = retrieve_string(raw_value) when 'emacs-mode-string' @emacs_mode_string = retrieve_string(raw_value) when *VARIABLE_NAMES then variable_name = :"@#{name.tr(?-, ?_)}" instance_variable_set(variable_name, value.nil? || value == '1' || value == 'on') end end |
#editing_mode ⇒ Object
72 73 74 |
# File 'lib/reline/config.rb', line 72 def editing_mode @default_key_bindings[@editing_mode_label] end |
#editing_mode=(val) ⇒ Object
76 77 78 |
# File 'lib/reline/config.rb', line 76 def editing_mode=(val) @editing_mode_label = val end |
#editing_mode_is?(*val) ⇒ Boolean
80 81 82 |
# File 'lib/reline/config.rb', line 80 def editing_mode_is?(*val) val.any?(@editing_mode_label) end |
#handle_directive(directive, file, no, if_stack) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/reline/config.rb', line 212 def handle_directive(directive, file, no, if_stack) directive, args = directive.split(' ') case directive when 'if' condition = false case args when /^mode=(vi|emacs)$/i mode = $1.downcase # NOTE: mode=vi means vi-insert mode mode = 'vi_insert' if mode == 'vi' if @editing_mode_label == mode.to_sym condition = true end when 'term' when 'version' else # application name condition = true if args == 'Ruby' condition = true if args == 'Reline' end if_stack << [no, !condition] when 'else' if if_stack.empty? raise InvalidInputrc, "#{file}:#{no}: unmatched else" end if_stack.last[1] = !if_stack.last[1] when 'endif' if if_stack.empty? raise InvalidInputrc, "#{file}:#{no}: unmatched endif" end if_stack.pop when 'include' read(File.(args)) end end |
#inputrc_path ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/reline/config.rb', line 92 def inputrc_path case ENV['INPUTRC'] when nil, '' else return File.(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. home_rc_path = File.('~/.inputrc') return home_rc_path if File.exist?(home_rc_path) case path = ENV['XDG_CONFIG_HOME'] when nil, '' else path = File.join(path, 'readline/inputrc') return path if File.exist?(path) and path == File.(path) end path = File.('~/.config/readline/inputrc') return path if File.exist?(path) return home_rc_path end |
#key_bindings ⇒ Object
142 143 144 145 |
# File 'lib/reline/config.rb', line 142 def key_bindings # The key bindings for each editing mode will be overwritten by the user-defined ones. Reline::KeyActor::Composite.new([@oneshot_key_bindings, @additional_key_bindings[@editing_mode_label], @default_key_bindings[@editing_mode_label]]) end |
#key_notation_to_code(notation) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/reline/config.rb', line 333 def key_notation_to_code(notation) case notation when /(?:\\(?:C|Control)-\\(?:M|Meta)|\\(?:M|Meta)-\\(?:C|Control))-([A-Za-z_])/ [?\e.ord, $1.ord % 32] when /\\(?:C|Control)-([A-Za-z_])/ ($1.upcase.ord % 32) when /\\(?:M|Meta)-([0-9A-Za-z_])/ [?\e.ord, $1.ord] 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 end end |
#keymap ⇒ Object
84 85 86 |
# File 'lib/reline/config.rb', line 84 def keymap @default_key_bindings[@keymap_label] end |
#loaded? ⇒ Boolean
88 89 90 |
# File 'lib/reline/config.rb', line 88 def loaded? @loaded end |
#parse_key_binding(key, func_name) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/reline/config.rb', line 319 def parse_key_binding(key, func_name) if key =~ /\A"(.*)"\z/ keyseq = parse_keyseq($1) else keyseq = nil end if func_name =~ /"(.*)"/ func = parse_keyseq($1) else func = func_name.split.first.tr(?-, ?_).to_sym # It must be macro. end [keyseq, func] end |
#parse_keyseq(str) ⇒ Object
359 360 361 362 363 |
# File 'lib/reline/config.rb', line 359 def parse_keyseq(str) str.scan(KEYSEQ_PATTERN).flat_map do |notation| key_notation_to_code(notation) end end |
#read(file = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/reline/config.rb', line 122 def read(file = nil) @loaded = true file ||= default_inputrc_path begin if file.respond_to?(:readlines) lines = file.readlines else lines = File.readlines(file) end rescue Errno::ENOENT return nil end read_lines(lines, file) self rescue InvalidInputrc => e warn e. nil end |
#read_lines(lines, file = nil) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/reline/config.rb', line 166 def read_lines(lines, file = nil) if not lines.empty? and lines.first.encoding != Reline.encoding_system_needs begin lines = lines.map do |l| l.encode(Reline.encoding_system_needs) rescue Encoding::UndefinedConversionError mes = "The inputrc encoded in #{lines.first.encoding.name} can't be converted to the locale #{Reline.encoding_system_needs.name}." raise Reline::ConfigEncodingConversionError.new(mes) end end end if_stack = [] lines.each_with_index do |line, no| next if line.match(/\A\s*#/) no += 1 line = line.chomp.lstrip if line.start_with?('$') handle_directive(line[1..-1], file, no, if_stack) next end next if if_stack.any? { |_no, skip| skip } case line when /^set +([^ ]+) +(.+)/i # value ignores everything after a space, raw_value does not. var, value, raw_value = $1.downcase, $2.partition(' ').first, $2 bind_variable(var, value, raw_value) when /^\s*(?:M|Meta)-([a-zA-Z_])\s*:\s*(.*)\s*$/o bind_key("\"\\M-#$1\"", $2) when /^\s*(?:C|Control)-([a-zA-Z_])\s*:\s*(.*)\s*$/o bind_key("\"\\C-#$1\"", $2) when /^\s*(?:(?:C|Control)-(?:M|Meta)|(?:M|Meta)-(?:C|Control))-([a-zA-Z_])\s*:\s*(.*)\s*$/o bind_key("\"\\M-\\C-#$1\"", $2) when /^\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/o bind_key($1, $2) end end unless if_stack.empty? raise InvalidInputrc, "#{file}:#{if_stack.last[0]}: unclosed if" end end |
#reload ⇒ Object
365 366 367 368 |
# File 'lib/reline/config.rb', line 365 def reload reset_variables read end |
#reset ⇒ Object
35 36 37 38 39 40 |
# File 'lib/reline/config.rb', line 35 def reset if editing_mode_is?(:vi_command) @editing_mode_label = :vi_insert end @oneshot_key_bindings.clear end |
#reset_oneshot_key_bindings ⇒ Object
154 155 156 |
# File 'lib/reline/config.rb', line 154 def reset_oneshot_key_bindings @oneshot_key_bindings.clear end |
#reset_variables ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/reline/config.rb', line 42 def reset_variables @additional_key_bindings = { # from inputrc emacs: Reline::KeyActor::Base.new, vi_insert: Reline::KeyActor::Base.new, vi_command: Reline::KeyActor::Base.new } @oneshot_key_bindings = Reline::KeyActor::Base.new @editing_mode_label = :emacs @keymap_label = :emacs @keymap_prefix = [] @default_key_bindings = { emacs: Reline::KeyActor::Base.new(Reline::KeyActor::EMACS_MAPPING), vi_insert: Reline::KeyActor::Base.new(Reline::KeyActor::VI_INSERT_MAPPING), vi_command: Reline::KeyActor::Base.new(Reline::KeyActor::VI_COMMAND_MAPPING) } @vi_cmd_mode_string = '(cmd)' @vi_ins_mode_string = '(ins)' @emacs_mode_string = '@' # https://tiswww.case.edu/php/chet/readline/readline.html#IDX25 @history_size = -1 # unlimited @keyseq_timeout = 500 @test_mode = false @autocompletion = false @convert_meta = seven_bit_encoding?(Reline::IOGate.encoding) @loaded = false @enable_bracketed_paste = true @show_mode_in_prompt = false @default_inputrc_path = nil end |
#retrieve_string(str) ⇒ Object
309 310 311 312 |
# File 'lib/reline/config.rb', line 309 def retrieve_string(str) str = $1 if str =~ /\A"(.*)"\z/ parse_keyseq(str).map { |c| c.chr(Reline.encoding_system_needs) }.join end |