summaryrefslogtreecommitdiff
path: root/lib/reline/config.rb
blob: a140959ca9c8304d0665ba004bc3e8c8bbb4a181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
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
154
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
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
211
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
require 'pathname'

class Reline::Config
  DEFAULT_PATH = Pathname.new(Dir.home).join('.inputrc')

  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
    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(?-, ?_)}" }
  VARIABLE_NAME_SYMBOLS.each do |v|
    attr_accessor v
  end

  def initialize
    @skip_section = nil
    @if_stack = []
    @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
  end

  def reset
    if editing_mode_is?(:vi_command)
      @editing_mode_label = :vi_insert
    end
  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

  def read(file = DEFAULT_PATH)
    file = ENV['INPUTRC'] if ENV['INPUTRC']
    begin
      if file.respond_to?(:readlines)
        lines = file.readlines
      else
          File.open(file, 'rt') do |f|
            lines = f.readlines
          end
      end
    rescue Errno::ENOENT
      $stderr.puts "no such file #{file}"
      return nil
    end

    read_lines(lines)
    self
  end

  def read_lines(lines)
    lines.each do |line|
      line = line.chomp.gsub(/^\s*/, '')
      if line[0, 1] == '$'
        handle_directive(line[1..-1])
        next
      end

      next if @skip_section

      if line.match(/^set +([^ ]+) +([^ ]+)/i)
        var, value = $1.downcase, $2.downcase
        bind_variable(var, value)
        next
      end

      if line =~ /\s*(.*)\s*:\s*(.*)\s*$/
        key, func_name = $1, $2
        bind_key(key, func_name)
      end
    end
  end

  def handle_directive(directive)
    directive, args = directive.split(' ')
    case directive
    when 'if'
      condition = false
      case args # TODO: variables
      when 'mode'
      when 'term'
      when 'version'
      else # application name
        condition = true if args == 'Ruby'
      end
      unless @skip_section.nil?
        @if_stack << @skip_section
      end
      @skip_section = !condition
    when 'else'
      @skip_section = !@skip_section
    when 'endif'
      @skip_section = nil
      unless @if_stack.empty?
        @skip_section = @if_stack.pop
      end
    when 'include'
      read(args)
    end
  end

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

  def bind_key(key, func_name)
    if key =~ /"(.*)"/
      keyseq = parse_keyseq($1).force_encoding('ASCII-8BIT')
    else
      keyseq = nil
    end
    if func_name =~ /"(.*)"/
      func = parse_keyseq($1).force_encoding('ASCII-8BIT')
    else
      func = func_name.to_sym # It must be macro.
    end
    [keyseq, func]
  end

  def key_notation_to_char(notation)
    case notation
    when /\\C-([A-Za-z_])/
      (1 + $1.downcase.ord - ?a.ord).chr('ASCII-8BIT')
    when /\\M-([0-9A-Za-z_])/
      modified_key = $1
      code =
        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
      code.chr('ASCII-8BIT')
    when /\\C-M-[A-Za-z_]/, /\\M-C-[A-Za-z_]/
    # 129 M-^A
    when /\\(\d{1,3})/ then $1.to_i(8).chr # octal
    when /\\x(\h{1,2})/ then $1.to_i(16).chr # hexadecimal
    when "\\e" then ?\e
    when "\\\\" then ?\\
    when "\\\"" then ?"
    when "\\'" then ?'
    when "\\a" then ?\a
    when "\\b" then ?\b
    when "\\d" then ?\d
    when "\\f" then ?\f
    when "\\n" then ?\n
    when "\\r" then ?\r
    when "\\t" then ?\t
    when "\\v" then ?\v
    else notation
    end
  end

  def parse_keyseq(str)
    # TODO: Control- and Meta-
    ret = String.new(encoding: 'ASCII-8BIT')
    while str =~ /(\\C-[A-Za-z_]|\\M-[0-9A-Za-z_]|\\C-M-[A-Za-z_]|\\M-C-[A-Za-z_]|\\e|\\\\|\\"|\\'|\\a|\\b|\\d|\\f|\\n|\\r|\\t|\\v|\\\d{1,3}|\\x\h{1,2}|.)/
      ret << key_notation_to_char($&)
      str = $'
    end
    ret
  end
end