lib/textbringer/utils.rb in textbringer-0.1.6 vs lib/textbringer/utils.rb in textbringer-0.1.7

- old
+ new

@@ -5,19 +5,20 @@ module Textbringer module Utils module_function def message(msg, log: true, sit_for: nil, sleep_for: nil) + str = msg.to_s 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") + buffer.insert(str + "\n") if buffer.current_line > 1000 buffer.beginning_of_buffer 10.times do buffer.forward_line end @@ -26,11 +27,11 @@ end ensure buffer.read_only = true end end - Window.echo_area.show(msg) + Window.echo_area.show(str) if sit_for sit_for(sit_for) Window.echo_area.clear_message end if sleep_for @@ -81,10 +82,15 @@ end message(e.to_s.chomp) Window.beep end + COMPLETION = { + original_buffer: nil, + completions_window: nil + } + 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" @@ -124,49 +130,34 @@ # 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 + if COMPLETION[:original_buffer] + COMPLETION[:completions_window].buffer = COMPLETION[:original_buffer] + COMPLETION[:completions_window] = nil + COMPLETION[:original_buffer] = nil + end end end 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?(?/) + Dir.glob(s + "*").map { |file| + if 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 def complete_for_minibuffer(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 + candidates.select { |i| i.start_with?(s) } end def read_buffer(prompt, default: (Buffer.last || Buffer.current)&.name) f = ->(s) { complete_for_minibuffer(s, Buffer.names) } read_from_minibuffer(prompt, completion_proc: f, default: default)