Class: VER::MiniBuffer

Inherits:
Text
  • Object
show all
Includes:
Keymapped
Defined in:
lib/ver/minibuffer.rb

Direct Known Subclasses

Peer

Defined Under Namespace

Classes: Peer

Constant Summary collapse

HISTORY =
Hash.new{|h,k| h[k] = SizedArray.new(50) }

Instance Attribute Summary collapse

Attributes included from Keymapped

#major_mode

Attributes inherited from Text

#lock

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Keymapped

#minor_mode, #minor_mode?

Methods inherited from Text

#execute, #execute_only, #focus, #index, #insert=, #inspect, #mark, #mark_next, #mark_previous, #marks, #place_forget, #range, #tag, #tag_ranges, #tags

Constructor Details

#initialize(*args) ⇒ MiniBuffer

Returns a new instance of MiniBuffer.



34
35
36
37
# File 'lib/ver/minibuffer.rb', line 34

def initialize(*args)
  super
  setup
end

Instance Attribute Details

#ask_stackObject

Returns the value of attribute ask_stack.



30
31
32
# File 'lib/ver/minibuffer.rb', line 30

def ask_stack
  @ask_stack
end

#askingObject (readonly)

Returns the value of attribute asking.



32
33
34
# File 'lib/ver/minibuffer.rb', line 32

def asking
  @asking
end

#at_insertObject

Returns the value of attribute at_insert.



30
31
32
# File 'lib/ver/minibuffer.rb', line 30

def at_insert
  @at_insert
end

#bufferObject (readonly)

Returns the value of attribute buffer.



32
33
34
# File 'lib/ver/minibuffer.rb', line 32

def buffer
  @buffer
end

#char_widthObject

Returns the value of attribute char_width.



30
31
32
# File 'lib/ver/minibuffer.rb', line 30

def char_width
  @char_width
end

#completion_bufferObject

Returns the value of attribute completion_buffer.



30
31
32
# File 'lib/ver/minibuffer.rb', line 30

def completion_buffer
  @completion_buffer
end

#messagesObject (readonly)

Returns the value of attribute messages.



32
33
34
# File 'lib/ver/minibuffer.rb', line 32

def messages
  @messages
end

#messages_expireObject

Returns the value of attribute messages_expire.



30
31
32
# File 'lib/ver/minibuffer.rb', line 30

def messages_expire
  @messages_expire
end

#messages_pendingObject (readonly)

Returns the value of attribute messages_pending.



32
33
34
# File 'lib/ver/minibuffer.rb', line 32

def messages_pending
  @messages_pending
end

Class Method Details

.answer_from(text) ⇒ Object



21
22
23
24
25
26
# File 'lib/ver/minibuffer.rb', line 21

def self.answer_from(text)
  line = text.get('current linestart', 'current lineend')
  minibuf = VER.minibuf
  minibuf.answer = line
  minibuf.complete_small
end

Instance Method Details

#abort(event = nil) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ver/minibuffer.rb', line 257

def abort(event = nil)
  self.prompt = ''
  self.answer = ''
  completion_buffer.place_forget rescue nil
  @asking = false

  if ask_stack.empty?
    self.messages_expire = false
    @caller.focus
  else
    ask(*ask_stack.shift)
  end
end

#adjust_size(event = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ver/minibuffer.rb', line 130

def adjust_size(event = nil)
  if get_displaychars('1.0', 'end').strip.empty?
    if (info = grid_info) && !info.empty?
      @old_grid_info = info
      grid_forget
    end
  else
    height = count('1.0', 'end', 'displaylines') || 1
    grid_configure(@old_grid_info) if @old_grid_info
    configure(height: height)
  end

  # a generous 25ms to make sure the display is updated.
  # Might need longer, lemme know if that's the case.
  Tk::After.ms(25){ buffer.see(:insert) } if buffer
rescue => ex
  VER.error(ex)
end

#answerObject



161
162
163
# File 'lib/ver/minibuffer.rb', line 161

def answer
  get_displaychars('answer', 'answer.last')
end

#answer=(value) ⇒ Object



157
158
159
# File 'lib/ver/minibuffer.rb', line 157

def answer=(value)
  replace('answer', 'answer.last', value, 'answer')
end

#ask(prompt, options = {}, action = nil, &block) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ver/minibuffer.rb', line 233

def ask(prompt, options = {}, action = nil, &block)
  action ||= block

  if @asking
    ask_stack << [prompt, options, action]
  else
    @asking = true
    @action = action || options[:action]
    @caller = options.fetch(:caller)

    self.prompt = prompt
    self.answer = options[:value]
    self.insert = 'answer.last'

    message ''
    warn ''
    self.messages_expire = true

    # Make sure display is ready.
    Tk.update
    focus
  end
end

#attempt(event = nil) ⇒ Object



271
272
273
274
# File 'lib/ver/minibuffer.rb', line 271

def attempt(event = nil)
  HISTORY[@prompt.get] << answer
  invoke(:attempt)
end

#claim_focusObject



224
225
226
227
# File 'lib/ver/minibuffer.rb', line 224

def claim_focus
  bind('<FocusOut>'){ focus; Tk.callback_break }
  focus
end

#common_subchoice(choices) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/ver/minibuffer.rb', line 311

def common_subchoice(choices)
  longest = choices.max_by{|choice| choice.size }
  common = ''

  longest.each_char do |char|
    current = common + char

    if choices.all?{|choice| choice.start_with?(current) }
      common << char
    else
      break
    end
  end

  common
end

#complete_large(event = nil) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/ver/minibuffer.rb', line 298

def complete_large(event = nil)
  if choices = invoke(:complete)
    case choices.size
    when 0
    when 1
      self.answer = choices.first
    else
      self.answer = common_subchoice(choices)
      show_completions(choices)
    end
  end
end

#complete_small(event = nil) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ver/minibuffer.rb', line 286

def complete_small(event = nil)
  if choices = invoke(:complete)
    case choices.size
    when 0
    when 1
      self.answer = choices.first
    else
      self.answer = common_subchoice(choices)
    end
  end
end

#copy(string) ⇒ Object



422
423
424
# File 'lib/ver/minibuffer.rb', line 422

def copy(string)
  Clipboard.dwim = string
end

#delete_next_char(event = nil) ⇒ Object



370
371
372
373
374
# File 'lib/ver/minibuffer.rb', line 370

def delete_next_char(event = nil)
  return unless line = get_displaychars('insert', 'answer.last')
  delete('insert') unless line == ''
  invoke(:modified)
end

#delete_next_word(event = nil) ⇒ Object



376
377
378
379
380
381
# File 'lib/ver/minibuffer.rb', line 376

def delete_next_word(event = nil)
  line = get_displaychars('insert', 'answer.last')
  return unless word = line[/^\s*(?:\w+|[^\w\s]+)\s*/]
  delete('insert', "insert + #{word.size} chars")
  invoke(:modified)
end

#delete_prev_char(event = nil) ⇒ Object



383
384
385
386
387
# File 'lib/ver/minibuffer.rb', line 383

def delete_prev_char(event = nil)
  return unless compare('insert', '>', 'answer')
  delete('insert - 1 display chars')
  invoke(:modified)
end

#delete_prev_word(event = nil) ⇒ Object



389
390
391
392
393
394
# File 'lib/ver/minibuffer.rb', line 389

def delete_prev_word(event = nil)
  line = get_displaychars('answer', 'insert').reverse
  return unless word = line[/^\s*(?:\w+|[^\w\s]+)\s*/]
  delete("insert - #{word.size} display chars", 'insert')
  invoke(:modified)
end

#end_of_line(event = nil) ⇒ Object



358
359
360
361
# File 'lib/ver/minibuffer.rb', line 358

def end_of_line(event = nil)
  mark_set('insert', 'answer.last')
  invoke(:movement)
end

#eventsObject



334
335
336
# File 'lib/ver/minibuffer.rb', line 334

def events
  major_mode.event_history
end

#historyObject



126
127
128
# File 'lib/ver/minibuffer.rb', line 126

def history
  HISTORY[@prompt.get]
end

#insert_selection(event = nil) ⇒ Object



348
349
350
351
# File 'lib/ver/minibuffer.rb', line 348

def insert_selection(event = nil)
  insert(:insert, Tk::Selection.get(type: 'UTF8_STRING'), 'answer')
  invoke(:modified)
end

#insert_stringObject



338
339
340
341
342
343
344
345
346
# File 'lib/ver/minibuffer.rb', line 338

def insert_string
  case string = events.last.unicode
  when /^[^\n\r[:cntrl:]]+$/
    insert(:insert, string, 'answer')
    invoke(:modified)
  else
    l insert_string: string
  end
end

#insert_tab(event = nil) ⇒ Object



353
354
355
356
# File 'lib/ver/minibuffer.rb', line 353

def insert_tab(event = nil)
  insert(:insert, "\t", 'answer')
  invoke(:modified)
end

#invoke(action, *args) ⇒ Object



328
329
330
331
332
# File 'lib/ver/minibuffer.rb', line 328

def invoke(action, *args)
  Tk::Event.generate(self, "<<#{action.capitalize}>>")
  result = @action.call(answer, action, *args)
  result == :abort ? abort : result
end

#kill_end_of_line(event = nil) ⇒ Object



363
364
365
366
367
368
# File 'lib/ver/minibuffer.rb', line 363

def kill_end_of_line(event = nil)
  content = get_displaychars('insert', 'answer.last')
  copy(content) if content =~ /\S/
  delete('insert', 'answer.last')
  invoke(:modified)
end

#message(string, tag = 'info') ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ver/minibuffer.rb', line 176

def message(string, tag = 'info')
  insert = at_insert.index

  begin
    replace("#{tag}.first", "#{tag}.last", " #{string}", tag)
    # mark(tag, "#{tag}.first", :left)
  rescue
    begin
      insert(tag, " #{string}", tag)
    rescue
      insert('1.0 lineend', " #{string}", tag)
    end
  end

  if string.to_s.strip != ''
    messages << string
    message_expire(tag) #  if messages_expire
    # message_notify(tag)
  end
  adjust_size

  self.insert = insert
  see "#{tag}.first"
end

#message_expire(tag, timeout = 5000) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ver/minibuffer.rb', line 212

def message_expire(tag, timeout = 5000)
  self.messages_pending[tag] += 1

  Tk::After.ms timeout.to_int do
    self.messages_pending[tag] -= 1
    if messages_pending[tag] == 0
      delete("#{tag}.first", "#{tag}.last") rescue nil
      adjust_size
    end
  end
end

#message_notify(tag, timeout = 500) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/ver/minibuffer.rb', line 201

def message_notify(tag, timeout = 500)
  tag_add('highlight', "#{tag}.first", "#{tag}.last")

  Tk::After.ms timeout.to_int do
    begin
      tag_remove('highlight', "#{tag}.first", "#{tag}.last")
    rescue
    end
  end
end

#next_char(event = nil) ⇒ Object



396
397
398
399
400
# File 'lib/ver/minibuffer.rb', line 396

def next_char(event = nil)
  return if compare('insert', '>=', 'answer.last')
  mark_set('insert', 'insert + 1 chars')
  invoke(:movement)
end

#next_historyObject



437
438
439
440
# File 'lib/ver/minibuffer.rb', line 437

def next_history
  history = HISTORY[@prompt.get]
  l next_history: history
end

#on_destroy(event) ⇒ Object



118
119
120
# File 'lib/ver/minibuffer.rb', line 118

def on_destroy(event)
  @lock = true
end

#paste(event = nil) ⇒ Object



426
427
428
429
430
# File 'lib/ver/minibuffer.rb', line 426

def paste(event = nil)
  return unless content = Clipboard.dwim
  insert(:insert, content, 'answer')
  invoke(:modified)
end

#peer_create(parent, buffer, *args) ⇒ Object



39
40
41
# File 'lib/ver/minibuffer.rb', line 39

def peer_create(parent, buffer, *args)
  Peer.new(self, parent, buffer, *args)
end

#prev_char(event = nil) ⇒ Object



402
403
404
405
406
# File 'lib/ver/minibuffer.rb', line 402

def prev_char(event = nil)
  return if compare('insert', '<=', 'answer')
  mark_set('insert', 'insert - 1 chars')
  invoke(:movement)
end

#prev_historyObject



432
433
434
435
# File 'lib/ver/minibuffer.rb', line 432

def prev_history
  history = HISTORY[@prompt.get]
  l prev_history: history
end

#promptObject



153
154
155
# File 'lib/ver/minibuffer.rb', line 153

def prompt
  get_displaychars('prompt', 'prompt.last')
end

#prompt=(value) ⇒ Object



149
150
151
# File 'lib/ver/minibuffer.rb', line 149

def prompt=(value)
  replace('prompt', 'prompt.last', value, 'prompt')
end

#release_focusObject



229
230
231
# File 'lib/ver/minibuffer.rb', line 229

def release_focus
  bind('<FocusOut>'){ }
end

#setupObject



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
# File 'lib/ver/minibuffer.rb', line 43

def setup
  setup_common

  @info.configure(foreground: '#fff')
  @warn.configure(foreground: '#f00')
  @highlight.configure(background: '#330')

  # Now to the difficult part.
  # When i talk about "tag", i mean the prompt and answer tags.
  #
  # If the "tag" contents are deleted, we will get errors in all places
  # where it's used.
  # To avoid that, we add extra tags called "tag"_keeper.
  # These are elided and contain only one character, their priority is
  # higher than "tag" to sure they never show.
  # The character within is also tagged with "tag".
  # When doing modifications, we just have to make sure that the
  # "tag"_keeper tag stays around.
  # To simplify referencing, we add a mark called "tag" with left gravity.
  # When getting contents of "tag", get only non-elided part.

  @prompt_keeper = tag(:prompt_keeper)
  insert('1.0', 'p', @prompt_keeper)
  @prompt_mark = mark(:prompt, '1.1', :left)
  @prompt_tag  = tag(:prompt, '1.0', '1.1')
  @prompt_keeper.configure(elide: true)
  tag_lower(@prompt_tag, @prompt_keeper)

  @answer_keeper = tag(:answer_keeper)
  insert('1.1', 'a', :answer_keeper)
  @answer_mark = mark(:answer, '1.2', :left)
  @answer_tag  = tag(:answer, '1.1', '1.2')
  @answer_keeper.configure(elide: true)
  tag_lower(@answer_tag, @answer_keeper)

  bind('<Configure>'){ pack_forget }
end

#setup_commonObject



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
# File 'lib/ver/minibuffer.rb', line 81

def setup_common
  configure(
    autoseparators: true,
    background: '#000',
    blockcursor: false,
    borderwidth: 0,
    exportselection: true,
    font: VER.options.font,
    foreground: '#fff',
    highlightbackground: '#000', # this specifies the colors
    highlightcolor: '#fff',      # that are used when the widget
    highlightthickness: 1,       # has input focus.
    insertbackground: '#fff',
    insertofftime: 0,
    setgrid: false, # wtf?
    undo: true,
    wrap: :word
  )

  self.major_mode = :MiniBuffer
  self.messages_expire = false
  @messages_pending = Hash.new(0)
  @messages = SizedArray.new(10)
  self.char_width = VER.options.font.measure('0')
  self.ask_stack = []
  self.at_insert = mark(:insert)
  self.completion_buffer = Tk::Text.new(tk_parent)
  @old_grid_info = nil

  [ :info, :warn, :highlight, :prompt, :answer ].each do |name|
    instance_variable_set("@#{name}", tag(name))
  end

  bind('<Configure>', &method(:adjust_size))
  bind('<Destroy>', &method(:on_destroy))
end

#show_completions(completions) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/ver/minibuffer.rb', line 276

def show_completions(completions)
  buffer = completion_buffer
  buffer.delete('1.0', 'end')
  completions.each do |completion|
    buffer.insert('end', completion, 'ver.minibuf.completion')
    buffer.insert('end', "\n")
  end
  buffer.place relx: 0.0, rely: 0.9, anchor: :sw, relheight: 0.9
end

#start_of_line(event = nil) ⇒ Object



408
409
410
411
# File 'lib/ver/minibuffer.rb', line 408

def start_of_line(event = nil)
  mark_set('insert', 'answer')
  invoke(:movement)
end

#transpose_chars(event = nil) ⇒ Object



413
414
415
416
417
418
419
420
# File 'lib/ver/minibuffer.rb', line 413

def transpose_chars(event = nil)
  insert = index('insert')
  line = get_displaychars('insert', 'answer.last')
  line[0, 2] = line[0, 2].reverse
  replace('insert', 'answer.last', line, 'answer')
  mark_set('insert', insert)
  invoke(:modified)
end

#type(string) ⇒ Object



122
123
124
# File 'lib/ver/minibuffer.rb', line 122

def type(string)
  major_mode.fake(string)
end

#warn(object) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/ver/minibuffer.rb', line 165

def warn(object)
  case object
  when Exception
    message("#{object.class}: #{object}", 'warn')
  when Symbol, String
    message(object.to_s, 'warn')
  else
    message(object.to_str, 'warn')
  end
end