lib/textbringer/commands/misc.rb in textbringer-0.1.6 vs lib/textbringer/commands/misc.rb in textbringer-0.1.7

- old
+ new

@@ -75,19 +75,70 @@ define_command(:keyboard_quit) do raise Quit end + def update_completions(xs) + if xs.size > 1 + if COMPLETION[:original_buffer].nil? + COMPLETION[:completions_window] = Window.windows[-2] + COMPLETION[:original_buffer] = + COMPLETION[:completions_window].buffer + end + completions = Buffer.find_or_new("*Completions*", undo_limit: 0) + if !completions.mode.is_a?(CompletionListMode) + completions.apply_mode(CompletionListMode) + end + completions.read_only = false + begin + completions.clear + xs.each do |x| + completions.insert(x + "\n") + end + COMPLETION[:completions_window].buffer = completions + ensure + completions.read_only = true + end + else + if COMPLETION[:original_buffer] + COMPLETION[:completions_window].buffer = + COMPLETION[:original_buffer] + end + end + end + private :update_completions + + def complete_minibuffer_with_string(s) + minibuffer = Buffer.minibuffer + if s.start_with?(minibuffer.to_s) + minibuffer.insert(s[minibuffer.to_s.size..-1]) + else + minibuffer.delete_region(minibuffer.point_min, + minibuffer.point_max) + minibuffer.insert(s) + end + end + private :complete_minibuffer_with_string + define_command(:complete_minibuffer) do minibuffer = Buffer.minibuffer completion_proc = minibuffer[:completion_proc] if completion_proc - s = completion_proc.call(minibuffer.to_s) + xs = completion_proc.call(minibuffer.to_s) + update_completions(xs) + if xs.empty? + message("No match", sit_for: 1) + return + end + y, *ys = xs + s = y.size.downto(1).lazy.map { |i| + y[0, i] + }.find { |i| + ys.all? { |j| j.start_with?(i) } + } if s - minibuffer.delete_region(minibuffer.point_min, - minibuffer.point_max) - minibuffer.insert(s) + complete_minibuffer_with_string(s) end end end UNIVERSAL_ARGUMENT_MAP = Keymap.new @@ -182,22 +233,15 @@ buffer.read_only = false buffer.clear Window.redisplay signals = [:INT, :TERM, :KILL] begin - if /mswin32|mingw32/ =~ RUBY_PLATFORM - opts = {} - else - opts = {pgroup: true} - end + opts = /mswin32|mingw32/ =~ RUBY_PLATFORM ? {} : {pgroup: true} Open3.popen2e(cmd, opts) do |input, output, wait_thread| input.close loop do status = output.wait_readable(0.5) - if status == false - break # EOF - end if status begin s = output.read_nonblock(1024).force_encoding("utf-8"). scrub("\u{3013}").gsub(/\r\n/, "\n") buffer.insert(s) @@ -226,11 +270,9 @@ code = status.exitstatus message("Process #{pid} exited with status code #{code}") elsif status.signaled? signame = Signal.signame(status.termsig) message("Process #{pid} was killed by #{signame}") - else - message("Process #{pid} exited") end end ensure buffer.read_only = true end