From 031807fcaf03c2fe0aa793a9f865b9f859ef3cd6 Mon Sep 17 00:00:00 2001 From: Wincent Colaiuta Date: Fri, 5 Nov 2010 00:00:00 +0000 Subject: [PATCH] Version 1.0b - work around platform-specific Vim 7.3 bug seen by some users (wherein Vim always falsely reports to Ruby that the buffer numbers is 0) - re-use the buffer that is used to show the match listing, rather than throwing it away and recreating it each time Command-T is shown; this stops the buffer numbers from creeping up needlessly --- doc/command-t.txt | 15 +++++++ ruby/command-t/controller.rb | 23 ++++++---- ruby/command-t/match_window.rb | 77 +++++++++++++++++++++------------- 3 files changed, 80 insertions(+), 35 deletions(-) diff --git a/doc/command-t.txt b/doc/command-t.txt index bb0b8ef..4f38318 100644 --- a/doc/command-t.txt +++ b/doc/command-t.txt @@ -540,6 +540,7 @@ Other contributors that have submitted patches include (in alphabetical order): Lucas de Vries + Matthew Todd Mike Lundy Scott Bronson Sung Pae @@ -620,6 +621,20 @@ POSSIBILITY OF SUCH DAMAGE. HISTORY *command-t-history* +1.0b (5 November 2010) + +- work around platform-specific Vim 7.3 bug seen by some users (wherein + Vim always falsely reports to Ruby that the buffer numbers is 0) +- re-use the buffer that is used to show the match listing, rather than + throwing it away and recreating it each time Command-T is shown; this + stops the buffer numbers from creeping up needlessly + +0.9 (8 October 2010) + +- use relative paths when opening files inside the current working directory + in order to keep buffer listings as brief as possible (patch from Matthew + Todd) + 0.8.1 (14 September 2010) - fix mapping issues for users who have set |'notimeout'| (patch from Sung diff --git a/ruby/command-t/controller.rb b/ruby/command-t/controller.rb index 2f8f59a..1e89d23 100644 --- a/ruby/command-t/controller.rb +++ b/ruby/command-t/controller.rb @@ -45,7 +45,7 @@ def show @focus = @prompt @prompt.focus register_for_key_presses - clear # clears prompt and list matches + clear # clears prompt and lists matches rescue Errno::ENOENT # probably a problem with the optional parameter @match_window.print_no_such_file_or_directory @@ -54,7 +54,13 @@ def show def hide @match_window.close if VIM::Window.select @initial_window - ::VIM::command "silent b #{@initial_buffer.number}" + if @initial_buffer.number == 0 + # upstream bug: buffer number misreported as 0 + # see: https://wincent.com/issues/1617 + ::VIM::command "silent b #{@initial_buffer.name}" + else + ::VIM::command "silent b #{@initial_buffer.number}" + end end end @@ -95,11 +101,7 @@ def accept_selection options = {} def toggle_focus @focus.unfocus # old focus - if @focus == @prompt - @focus = @match_window - else - @focus = @prompt - end + @focus = @focus == @prompt ? @match_window : @prompt @focus.focus # new focus end @@ -178,6 +180,12 @@ def get_list_or_string name end end + def relative_path_under_working_directory path + # any path under the working directory will be specified as a relative + # path to improve the readability of the buffer list etc + path.index(pwd = "#{VIM::pwd}/") == 0 ? path[pwd.length..-1] : path + end + # Backslash-escape space, \, |, %, #, " def sanitize_path_string str # for details on escaping command-line mode arguments see: :h : @@ -216,6 +224,7 @@ def ensure_appropriate_window_selection def open_selection selection, options = {} command = options[:command] || default_open_command selection = File.expand_path selection, @path + selection = relative_path_under_working_directory selection selection = sanitize_path_string selection ensure_appropriate_window_selection ::VIM::command "silent #{command} #{selection}" diff --git a/ruby/command-t/match_window.rb b/ruby/command-t/match_window.rb index eae0f87..7311d89 100644 --- a/ruby/command-t/match_window.rb +++ b/ruby/command-t/match_window.rb @@ -29,6 +29,7 @@ class MatchWindow @@selection_marker = '> ' @@marker_length = @@selection_marker.length @@unselected_marker = ' ' * @@marker_length + @@buffer = nil def initialize options = {} @prompt = options[:prompt] @@ -52,28 +53,35 @@ def initialize options = {} ::VIM::set_option 'sidescrolloff=0' # don't sidescroll automatically ::VIM::set_option 'noequalalways' # don't auto-balance window sizes - # create match window and set it up + # show match window split_location = options[:match_window_at_top] ? 'topleft' : 'botright' - split_command = "silent! #{split_location} 1split GoToFile" - [ - split_command, - 'setlocal bufhidden=delete', # delete buf when no longer displayed - 'setlocal buftype=nofile', # buffer is not related to any file - 'setlocal nomodifiable', # prevent manual edits - 'setlocal noswapfile', # don't create a swapfile - 'setlocal nowrap', # don't soft-wrap - 'setlocal nonumber', # don't show line numbers - 'setlocal nolist', # don't use List mode (visible tabs etc) - 'setlocal foldcolumn=0', # don't show a fold column at side - 'setlocal foldlevel=99', # don't fold anything - 'setlocal nocursorline', # don't highlight line cursor is on - 'setlocal nospell', # spell-checking off - 'setlocal nobuflisted', # don't show up in the buffer list - 'setlocal textwidth=0' # don't hard-wrap (break long lines) - ].each { |command| ::VIM::command command } - - # sanity check: make sure the buffer really was created - raise "Can't find buffer" unless $curbuf.name.match /GoToFile/ + if @@buffer # still have buffer from last time + ::VIM::command "silent! #{split_location} #{@@buffer.number}sbuffer" + raise "Can't re-open GoToFile buffer" unless $curbuf.number == @@buffer.number + $curwin.height = 1 + else # creating match window for first time and set it up + split_command = "silent! #{split_location} 1split GoToFile" + [ + split_command, + 'setlocal bufhidden=unload', # unload buf when no longer displayed + 'setlocal buftype=nofile', # buffer is not related to any file + 'setlocal nomodifiable', # prevent manual edits + 'setlocal noswapfile', # don't create a swapfile + 'setlocal nowrap', # don't soft-wrap + 'setlocal nonumber', # don't show line numbers + 'setlocal nolist', # don't use List mode (visible tabs etc) + 'setlocal foldcolumn=0', # don't show a fold column at side + 'setlocal foldlevel=99', # don't fold anything + 'setlocal nocursorline', # don't highlight line cursor is on + 'setlocal nospell', # spell-checking off + 'setlocal nobuflisted', # don't show up in the buffer list + 'setlocal textwidth=0' # don't hard-wrap (break long lines) + ].each { |command| ::VIM::command command } + + # sanity check: make sure the buffer really was created + raise "Can't find GoToFile buffer" unless $curbuf.name.match /GoToFile/ + @@buffer = $curbuf + end # syntax coloring if VIM::has_syntax? @@ -94,11 +102,24 @@ def initialize options = {} @selection = nil @abbrev = '' @window = $curwin - @buffer = $curbuf end def close - ::VIM::command "bwipeout! #{@buffer.number}" + # Workaround for upstream bug in Vim 7.3 on some platforms + # + # On some platforms, $curbuf.number always returns 0. One workaround is + # to build Vim with --disable-largefile, but as this is producing lots of + # support requests, implement the following fallback to the buffer name + # instead, at least until upstream gets fixed. + # + # For more details, see: https://wincent.com/issues/1617 + if $curbuf.number == 0 + # use bwipeout as bunload fails if passed the name of a hidden buffer + ::VIM::command "bwipeout! GoToFile" + @@buffer = nil + else + ::VIM::command "bunload! #{@@buffer.number}" + end restore_window_dimensions @settings.restore @prompt.dispose @@ -197,7 +218,7 @@ def print_error msg unlock clear @window.height = 1 - @buffer[1] = "-- #{msg} --" + @@buffer[1] = "-- #{msg} --" lock end @@ -231,7 +252,7 @@ def match_text_for_idx idx def print_match idx return unless VIM::Window.select(@window) unlock - @buffer[idx + 1] = match_text_for_idx idx + @@buffer[idx + 1] = match_text_for_idx idx lock end @@ -252,10 +273,10 @@ def print_matches @window.height = actual_lines (1..actual_lines).each do |line| idx = line - 1 - if @buffer.count >= line - @buffer[line] = match_text_for_idx idx + if @@buffer.count >= line + @@buffer[line] = match_text_for_idx idx else - @buffer.append line - 1, match_text_for_idx(idx) + @@buffer.append line - 1, match_text_for_idx(idx) end end lock