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{ bind-tty-special-chars blink-matching-paren byte-oriented completion-ignore-case convert-meta disable-completion enable-keypad expand-tilde history-preserve-point history-size horizontal-scroll-mode input-meta keyseq-timeout 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 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
-
#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
- #autocompletion ⇒ Object
- #autocompletion=(val) ⇒ Object
- #bind_key(key, func_name) ⇒ Object
- #bind_variable(name, value) ⇒ Object
- #editing_mode ⇒ Object
- #editing_mode=(val) ⇒ Object
- #editing_mode_is?(*val) ⇒ Boolean
- #handle_directive(directive, file, no) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #inputrc_path ⇒ Object
- #key_bindings ⇒ Object
- #key_notation_to_code(notation) ⇒ Object
- #keymap ⇒ Object
- #parse_keyseq(str) ⇒ Object
- #read(file = nil) ⇒ Object
- #read_lines(lines, file = nil) ⇒ Object
- #reset ⇒ Object
- #reset_default_key_bindings ⇒ Object
- #reset_oneshot_key_bindings ⇒ Object
- #retrieve_string(str) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/reline/config.rb', line 48 def initialize @additional_key_bindings = {} # from inputrc @additional_key_bindings[:emacs] = {} @additional_key_bindings[:vi_insert] = {} @additional_key_bindings[:vi_command] = {} @oneshot_key_bindings = {} @skip_section = nil @if_stack = nil @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 @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 = true if seven_bit_encoding?(Reline::IOGate.encoding) end |
Instance Attribute Details
#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
177 178 179 |
# File 'lib/reline/config.rb', line 177 def add_default_key_binding(keystroke, target) @key_actors[@keymap_label].default_key_bindings[keystroke] = target end |
#add_default_key_binding_by_keymap(keymap, keystroke, target) ⇒ Object
173 174 175 |
# File 'lib/reline/config.rb', line 173 def add_default_key_binding_by_keymap(keymap, keystroke, target) @key_actors[keymap].default_key_bindings[keystroke] = target end |
#add_oneshot_key_binding(keystroke, target) ⇒ Object
165 166 167 |
# File 'lib/reline/config.rb', line 165 def add_oneshot_key_binding(keystroke, target) @oneshot_key_bindings[keystroke] = target end |
#autocompletion ⇒ Object
100 101 102 |
# File 'lib/reline/config.rb', line 100 def autocompletion @autocompletion end |
#autocompletion=(val) ⇒ Object
96 97 98 |
# File 'lib/reline/config.rb', line 96 def autocompletion=(val) @autocompletion = val end |
#bind_key(key, func_name) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/reline/config.rb', line 336 def bind_key(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.tr(?-, ?_).to_sym # It must be macro. end [keyseq, func] end |
#bind_variable(name, value) ⇒ Object
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/reline/config.rb', line 264 def bind_variable(name, value) case name when 'history-size' begin @history_size = Integer(value) rescue ArgumentError @history_size = 500 end 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' @isearch_terminators = retrieve_string(value) 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 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(value) when 'vi-ins-mode-string' @vi_ins_mode_string = retrieve_string(value) when 'emacs-mode-string' @emacs_mode_string = retrieve_string(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
84 85 86 |
# File 'lib/reline/config.rb', line 84 def editing_mode @key_actors[@editing_mode_label] end |
#editing_mode=(val) ⇒ Object
88 89 90 |
# File 'lib/reline/config.rb', line 88 def editing_mode=(val) @editing_mode_label = val end |
#editing_mode_is?(*val) ⇒ Boolean
92 93 94 |
# File 'lib/reline/config.rb', line 92 def editing_mode_is?(*val) (val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label) end |
#handle_directive(directive, file, no) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/reline/config.rb', line 234 def handle_directive(directive, file, no) directive, args = directive.split(' ') case directive when 'if' condition = false case args when 'mode' when 'term' when 'version' else # application name condition = true if args == 'Ruby' condition = true if args == 'Reline' end @if_stack << [file, no, @skip_section] @skip_section = !condition when 'else' if @if_stack.empty? raise InvalidInputrc, "#{file}:#{no}: unmatched else" end @skip_section = !@skip_section when 'endif' if @if_stack.empty? raise InvalidInputrc, "#{file}:#{no}: unmatched endif" end @skip_section = @if_stack.pop when 'include' read(args) end end |
#inputrc_path ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/reline/config.rb', line 108 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
157 158 159 160 161 162 163 |
# File 'lib/reline/config.rb', line 157 def key_bindings # The key bindings for each editing mode will be overwritten by the user-defined ones. kb = @key_actors[@editing_mode_label].default_key_bindings.dup kb.merge!(@additional_key_bindings[@editing_mode_label]) kb.merge!(@oneshot_key_bindings) kb end |
#key_notation_to_code(notation) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/reline/config.rb', line 350 def key_notation_to_code(notation) case notation when /\\(?:C|Control)-([A-Za-z_])/ (1 + $1.downcase.ord - ?a.ord) when /\\(?:M|Meta)-([0-9A-Za-z_])/ modified_key = $1 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 when /\\(?:C|Control)-(?:M|Meta)-[A-Za-z_]/, /\\(?:M|Meta)-(?:C|Control)-[A-Za-z_]/ # 129 M-^A 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
104 105 106 |
# File 'lib/reline/config.rb', line 104 def keymap @key_actors[@keymap_label] end |
#parse_keyseq(str) ⇒ Object
384 385 386 387 388 389 390 |
# File 'lib/reline/config.rb', line 384 def parse_keyseq(str) ret = [] str.scan(KEYSEQ_PATTERN) do ret << key_notation_to_code($&) end ret end |
#read(file = nil) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/reline/config.rb', line 138 def read(file = nil) 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
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/reline/config.rb', line 187 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 conditions = [@skip_section, @if_stack] @skip_section = nil @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) next end next if @skip_section case line when /^set +([^ ]+) +([^ ]+)/i var, value = $1.downcase, $2 bind_variable(var, value) next when /\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/o key, func_name = $1, $2 keystroke, func = bind_key(key, func_name) next unless keystroke @additional_key_bindings[@keymap_label][keystroke] = func end end unless @if_stack.empty? raise InvalidInputrc, "#{file}:#{@if_stack.last[1]}: unclosed if" end ensure @skip_section, @if_stack = conditions end |
#reset ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/reline/config.rb', line 73 def reset if editing_mode_is?(:vi_command) @editing_mode_label = :vi_insert end @additional_key_bindings.keys.each do |key| @additional_key_bindings[key].clear end @oneshot_key_bindings.clear reset_default_key_bindings end |
#reset_default_key_bindings ⇒ Object
181 182 183 184 185 |
# File 'lib/reline/config.rb', line 181 def reset_default_key_bindings @key_actors.values.each do |ka| ka.reset_default_key_bindings end end |
#reset_oneshot_key_bindings ⇒ Object
169 170 171 |
# File 'lib/reline/config.rb', line 169 def reset_oneshot_key_bindings @oneshot_key_bindings.clear end |
#retrieve_string(str) ⇒ Object
331 332 333 334 |
# File 'lib/reline/config.rb', line 331 def retrieve_string(str) str = $1 if str =~ /\A"(.*)"\z/ parse_keyseq(str).map { |c| c.chr(Reline.encoding_system_needs) }.join end |