Module: Textbringer::Utils

Included in:
Commands
Defined in:
lib/textbringer/utils.rb

Constant Summary collapse

Y_OR_N_MAP =
Keymap.new
HOOKS =
Hash.new { |h, k| h[k] = [] }

Instance Method Summary collapse

Instance Method Details

#add_hook(name, func) ⇒ Object



238
239
240
# File 'lib/textbringer/utils.rb', line 238

def add_hook(name, func)
  HOOKS[name].unshift(func)
end

#complete(s, candidates) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/textbringer/utils.rb', line 151

def complete(s, candidates)
  xs = candidates.select { |i| i.start_with?(s) }
  if xs.size > 0
    y, *ys = xs
    y.size.downto(1).lazy.map { |i|
      y[0, i]
    }.find { |i|
      ys.all? { |j| j.start_with?(i) }
    }
  else
    nil
  end
end

#handle_exception(e) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/textbringer/utils.rb', line 55

def handle_exception(e)
  if e.is_a?(SystemExit)
    raise
  end
  if Buffer.current.name != "*Backtrace*"
    buffer = Buffer.find_or_new("*Backtrace*", undo_limit: 0)
    if !buffer.mode.is_a?(BacktraceMode)
      buffer.apply_mode(BacktraceMode)
    end
    buffer.read_only = false
    begin
      buffer.delete_region(buffer.point_min, buffer.point_max)
      buffer.insert("#{e.class}: #{e}\n")
      e.backtrace.each do |line|
        buffer.insert(line + "\n")
      end
      buffer.beginning_of_buffer
    ensure
      buffer.read_only = true
    end
  end
  message(e.to_s.chomp)
  Window.beep
end

#message(msg, log: true, sit_for: nil, sleep_for: nil) ⇒ Object



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
# File 'lib/textbringer/utils.rb', line 5

def message(msg, log: true, sit_for: nil, sleep_for: nil)
  if log && Buffer.current.name != "*Messages*"
    buffer = Buffer["*Messages*"] ||
      Buffer.new_buffer("*Messages*", undo_limit: 0).tap { |b|
        b[:top_of_window] = b.new_mark
    }
    buffer.read_only = false
    begin
      buffer.end_of_buffer
      buffer.insert(msg + "\n")
      if buffer.current_line > 1000
        buffer.beginning_of_buffer
        10.times do
          buffer.next_line
        end
        buffer.delete_region(buffer.point_min, buffer.point)
        buffer.end_of_buffer
      end
    ensure
      buffer.read_only = true
    end
  end
  Window.echo_area.show(msg)
  if sit_for
    sit_for(sit_for)
    Window.echo_area.clear_message
  end
  if sleep_for
    sleep_for(sleep_for)
    Window.echo_area.clear_message
  end
end

#read_buffer(prompt, default: (Buffer.last || Buffer.current)&.name) ⇒ Object



165
166
167
168
# File 'lib/textbringer/utils.rb', line 165

def read_buffer(prompt, default: (Buffer.last || Buffer.current)&.name)
  f = ->(s) { complete(s, Buffer.names) }
  read_from_minibuffer(prompt, completion_proc: f, default: default)
end

#read_charObject



47
48
49
# File 'lib/textbringer/utils.rb', line 47

def read_char
  Controller.current.read_char
end

#read_command_name(prompt) ⇒ Object



170
171
172
173
174
175
# File 'lib/textbringer/utils.rb', line 170

def read_command_name(prompt)
  f = ->(s) {
    complete(s.tr("-", "_"), Commands.list.map(&:to_s))
  }
  read_from_minibuffer(prompt, completion_proc: f)
end

#read_file_name(prompt, default: nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/textbringer/utils.rb', line 126

def read_file_name(prompt, default: nil)
  f = ->(s) {
    s = File.expand_path(s) if s.start_with?("~")
    files = Dir.glob(s + "*")
    if files.size > 0
      x, *xs = files
      file = x.size.downto(1).lazy.map { |i|
        x[0, i]
      }.find { |i|
        xs.all? { |j| j.start_with?(i) }
      }
      if file && files.size == 1 &&
         File.directory?(file) && !file.end_with?(?/)
        file + "/"
      else
        file
      end
    else
      nil
    end
  }
  file = read_from_minibuffer(prompt, completion_proc: f, default: default)
  File.expand_path(file)
end

#read_from_minibuffer(prompt, completion_proc: nil, default: nil, keymap: MINIBUFFER_LOCAL_MAP) ⇒ Object



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
# File 'lib/textbringer/utils.rb', line 80

def read_from_minibuffer(prompt, completion_proc: nil, default: nil,
                         keymap: MINIBUFFER_LOCAL_MAP)
  if Window.echo_area.active?
    raise EditorError,
      "Command attempted to use minibuffer while in minibuffer"
  end
  old_buffer = Buffer.current
  old_window = Window.current
  old_completion_proc = Buffer.minibuffer[:completion_proc]
  old_current_prefix_arg = Controller.current.current_prefix_arg
  old_minibuffer_map = Buffer.minibuffer.keymap
  Buffer.minibuffer.keymap = keymap
  Buffer.minibuffer[:completion_proc] = completion_proc
  Window.echo_area.active = true
  begin
    Buffer.minibuffer.delete_region(Buffer.minibuffer.point_min,
                                    Buffer.minibuffer.point_max)
    Window.current = Window.echo_area
    if default
      prompt = prompt.sub(/:/, " (default #{default}):")
    end
    Window.echo_area.prompt = prompt
    Window.echo_area.redisplay
    Window.update
    recursive_edit
    s = Buffer.minibuffer.to_s.chomp
    if default && s.empty?
      default
    else
      s
    end
  ensure
    Window.echo_area.clear
    Window.echo_area.redisplay
    Window.update
    Window.echo_area.active = false
    Window.current = old_window
    # Just in case old_window has been deleted by resize,
    # in which case Window.current is set to the first window.
    Window.current.buffer = Buffer.current = old_buffer
    Buffer.minibuffer[:completion_proc] = old_completion_proc
    Buffer.minibuffer.keymap = old_minibuffer_map
    Controller.current.current_prefix_arg = old_current_prefix_arg
  end
end

#read_single_char(prompt, chars) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/textbringer/utils.rb', line 223

def read_single_char(prompt, chars)
  map = Keymap.new
  chars.each do |c|
    map.define_key(c, :self_insert_and_exit_minibuffer)
  end
  map.define_key(?\C-g, :abort_recursive_edit)
  char_options = chars.join(?/)
  map.handle_undefined_key do |key|
    -> { message("Invalid key.  Type C-g to quit.", sit_for: 2) }
  end
  read_from_minibuffer(prompt + " (#{char_options}) ", keymap: map)
end

#received_keyboard_quit?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/textbringer/utils.rb', line 51

def received_keyboard_quit?
  Controller.current.received_keyboard_quit?
end

#remove_hook(name, func) ⇒ Object



242
243
244
# File 'lib/textbringer/utils.rb', line 242

def remove_hook(name, func)
  HOOKS[name].delete(func)
end

#run_hooks(name, remove_on_error: false) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/textbringer/utils.rb', line 246

def run_hooks(name, remove_on_error: false)
  HOOKS[name].delete_if do |func|
    begin
      case func
      when Symbol
        send(func)
      else
        func.call
      end
      false
    rescue Exception => e
      raise if e.is_a?(SystemExit)
      if remove_on_error
        true
      else
        raise
      end
    end
  end
end

#self_insert_and_exit_minibufferObject



199
200
201
202
# File 'lib/textbringer/utils.rb', line 199

def self_insert_and_exit_minibuffer
  self_insert
  exit_recursive_edit
end

#set_transient_map(map) ⇒ Object



267
268
269
270
271
272
273
274
275
# File 'lib/textbringer/utils.rb', line 267

def set_transient_map(map)
  old_overriding_map = Controller.current.overriding_map
  hook = -> {
    Controller.current.overriding_map = old_overriding_map
    remove_hook(:pre_command_hook, hook)
  }
  add_hook(:pre_command_hook, hook)
  Controller.current.overriding_map = map
end

#sit_for(secs, no_redisplay = false) ⇒ Object



38
39
40
41
# File 'lib/textbringer/utils.rb', line 38

def sit_for(secs, no_redisplay = false)
  Window.redisplay unless no_redisplay
  Controller.current.wait_input((secs * 1000).to_i)
end

#sleep_for(secs) ⇒ Object



43
44
45
# File 'lib/textbringer/utils.rb', line 43

def sleep_for(secs)
  sleep(secs)
end

#y_or_n?(prompt) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/textbringer/utils.rb', line 204

def y_or_n?(prompt)
  new_prompt = prompt + " (y or n) "
  prompt_modified = false
  loop do
    s = read_from_minibuffer(new_prompt, keymap: Y_OR_N_MAP)
    case s
    when ?y
      break true
    when ?n
      break false
    else
      unless prompt_modified
        new_prompt.prepend("Answer y or n. ")
        prompt_modified = true
      end
    end
  end
end

#yes_or_no?(prompt) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/textbringer/utils.rb', line 177

def yes_or_no?(prompt)
  loop {
    s = read_from_minibuffer(prompt + " (yes or no) ")
    case s
    when "yes"
      return true
    when "no"
      return false
    else
      message("Please answer yes or no.", sit_for: 2)
    end
  }
end