Class: Reline::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/reline/config.rb

Defined Under Namespace

Classes: InvalidInputrc

Constant Summary collapse

DEFAULT_PATH =
'~/.inputrc'
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
}
VARIABLE_NAME_SYMBOLS =
VARIABLE_NAMES.map { |v| :"#{v.tr(?-, ?_)}" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reline/config.rb', line 46

def initialize
  @additional_key_bindings = {} # from inputrc
  @default_key_bindings = {} # environment-dependent
  @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
  @history_size = 500
  @keyseq_timeout = 500
  @test_mode = false
end

Instance Attribute Details

#test_modeObject (readonly)

Returns the value of attribute test_mode.



4
5
6
# File 'lib/reline/config.rb', line 4

def test_mode
  @test_mode
end

Instance Method Details

#add_default_key_binding(keystroke, target) ⇒ Object



110
111
112
# File 'lib/reline/config.rb', line 110

def add_default_key_binding(keystroke, target)
  @default_key_bindings[keystroke] = target
end

#bind_key(key, func_name) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/reline/config.rb', line 233

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



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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/reline/config.rb', line 185

def bind_variable(name, value)
  case name
  when 'history-size'
    @history_size = value.to_i
  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 = instance_eval(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 *VARIABLE_NAMES then
    variable_name = :"@#{name.tr(?-, ?_)}"
    instance_variable_set(variable_name, value.nil? || value == '1' || value == 'on')
  end
end

#editing_modeObject



70
71
72
# File 'lib/reline/config.rb', line 70

def editing_mode
  @key_actors[@editing_mode_label]
end

#editing_mode=(val) ⇒ Object



74
75
76
# File 'lib/reline/config.rb', line 74

def editing_mode=(val)
  @editing_mode_label = val
end

#editing_mode_is?(*val) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/reline/config.rb', line 78

def editing_mode_is?(*val)
  (val.respond_to?(:any?) ? val : [val]).any?(@editing_mode_label)
end

#handle_directive(directive, file, no) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/reline/config.rb', line 155

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

#key_bindingsObject



105
106
107
108
# File 'lib/reline/config.rb', line 105

def key_bindings
  # override @default_key_bindings with @additional_key_bindings
  @default_key_bindings.merge(@additional_key_bindings)
end

#key_notation_to_code(notation) ⇒ 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
# File 'lib/reline/config.rb', line 247

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

#keymapObject



82
83
84
# File 'lib/reline/config.rb', line 82

def keymap
  @key_actors[@keymap_label]
end

#parse_keyseq(str) ⇒ Object



281
282
283
284
285
286
287
# File 'lib/reline/config.rb', line 281

def parse_keyseq(str)
  ret = []
  str.scan(KEYSEQ_PATTERN) do
    ret << key_notation_to_code($&)
  end
  ret
end

#read(file = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/reline/config.rb', line 86

def read(file = nil)
  file ||= File.expand_path(ENV['INPUTRC'] || DEFAULT_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.message
  nil
end

#read_lines(lines, file = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/reline/config.rb', line 118

def read_lines(lines, file = nil)
  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.downcase
      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[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

#resetObject



62
63
64
65
66
67
68
# File 'lib/reline/config.rb', line 62

def reset
  if editing_mode_is?(:vi_command)
    @editing_mode_label = :vi_insert
  end
  @additional_key_bindings = {}
  @default_key_bindings = {}
end

#reset_default_key_bindingsObject



114
115
116
# File 'lib/reline/config.rb', line 114

def reset_default_key_bindings
  @default_key_bindings = {}
end