Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0f5c8cbf3fa91e4e8001805ac05009b9b50bb3c2 @jamis committed Oct 9, 2008
Showing with 143 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +41 −0 README
  3. +101 −0 fuzzyfinder_textmate.vim
1 .gitignore
@@ -0,0 +1 @@
+*.swp
41 README
@@ -0,0 +1,41 @@
+FuzzyFinder: TextMate
+--------------------------
+This is an extension to Takeshi NISHIDA's excellent Fuzzyfinder VIM script[1]. It adds a TextMate mode, for completing file names in a similar fashion to how TextMate[2] does file completion in its cmd-T window.
+
+This extension is partially written in Ruby[3], and it depends on a separate Ruby module (fuzzy_file_finder[4]), so you'll need Vim to be compiled with Ruby support.
+
+
+Usage
+------------------------
+Usage is almost identical to the Fuzzyfinder script, so you should check out the documentation[1] for that (which is very good). The only difference is that to start TextMate mode, you just invoke the :FuzzyFinderTextMate command. It is recommended to map that command to something more agile, e.g.:
+
+ map <leader>t :FuzzyFinderTextMate<CR>
+
+Once in TextMate mode, completion is done against the entire directory tree at once, using partial matches. Results are sorted by "score", which is a measure of how closely the file matches the text you entered.
+
+
+Installation
+-----------------------
+First, make sure you have the Fuzzyfinder script[1]. Install that as per the instructions for that script.
+
+Then, you'll need to install the fuzzy_file_finder[4] Ruby module. The simplest way to do this is via Rubygems:
+
+ gem install --source=http://gems.github.com jamis-fuzzy_file_finder
+
+Alternatively, you can take the lib/fuzzy_file_finder.rb file from that project and put it in ~/.vim/ruby.
+
+Lastly, you'll need to install this extension script. Take the fuzzyfinder_textmate.vim script and put it under ~/.vim/plugin.
+
+
+License
+-----------------------
+Most of this script and its supporting documentation is made available in the PUBLIC DOMAIN. It may be used, modified, and redistributed, almost without exception. The only exceptions are those parts of the code that were copied from NISHIDA's original script; those parts are distributed under the MIT license, as indicated by NISHIDA's code. The parts that were copied are indicated thus by comments.
+
+
+References
+-----------------------
+[1] http://www.vim.org/scripts/script.php?script_id=1984
+[2] http://www.macromates.com
+[3] http://www.ruby-lang.org
+[4] http://github.com/jamis/fuzzy_file_finder/tree/master
+
101 fuzzyfinder_textmate.vim
@@ -0,0 +1,101 @@
+if has("ruby")
+
+" ====================================================================================
+" COPIED FROM FUZZYFINDER.VIM {{{
+" since they can't be called from outside fuzzyfinder.vim
+" ====================================================================================
+function! s:GetCurrentTagFiles()
+ return sort(filter(map(tagfiles(), 'fnamemodify(v:val, '':p'')'), 'filereadable(v:val)'))
+endfunction
+
+function! s:HighlightPrompt(prompt, highlight)
+ syntax clear
+ execute printf('syntax match %s /^\V%s/', a:highlight, escape(a:prompt, '\'))
+endfunction
+
+function! s:HighlightError()
+ syntax clear
+ syntax match Error /^.*$/
+endfunction
+" ------------------------------------------------------------------------------------
+" }}}
+" ====================================================================================
+
+command! -bang -narg=? -complete=file FuzzyFinderTextMate call FuzzyFinderTextMateLauncher(<q-args>, len(<q-bang>), bufnr('%'), s:GetCurrentTagFiles())
+
+function! InstantiateTextMateMode() "{{{
+ruby << RUBY
+ $LOAD_PATH << "#{ENV['HOME']}/.vim/ruby"
+
+ begin
+ require 'rubygems'
+ gem 'fuzzy_file_finder'
+ rescue LoadError
+ end
+
+ require 'fuzzy_file_finder'
+RUBY
+
+ ruby def finder; @finder ||= FuzzyFileFinder.new; end
+
+ let g:FuzzyFinderMode.TextMate = copy(g:FuzzyFinderMode.Base)
+
+ " ================================================================================
+ " This function is copied almost whole-sale from fuzzyfinder.vim. Ideally, I could
+ " used the on_complete callback to more cleanly add the new behavior, but the
+ " TextMate-style completion broke a few of fuzzyfinder.vim's assumptions, and the
+ " only way to patch that up was to override Base.complete...which required me to
+ " copy-and-paste much of the original implementation.
+ "
+ " Ugly. But effective.
+ " ================================================================================
+ function! g:FuzzyFinderMode.TextMate.complete(findstart, base)
+ if a:findstart
+ return 0
+ elseif !self.exists_prompt(a:base) || len(self.remove_prompt(a:base)) < self.min_length
+ return []
+ endif
+ call s:HighlightPrompt(self.prompt, self.prompt_highlight)
+
+ let result = []
+ ruby << RUBY
+ matches = finder.find(VIM.evaluate('self.remove_prompt(a:base)'), VIM.evaluate('self.matching_limit').to_i + 1)
+ matches.sort_by { |a| [-a[:score], a[:path]] }.each_with_index do |match, index|
+ word = match[:path]
+ abbr = "%2d: %s" % [index+1, match[:abbr]]
+ menu = "[%5d]" % [match[:score] * 10000]
+ VIM.evaluate("add(result, { 'word' : #{word.inspect}, 'abbr' : #{abbr.inspect}, 'menu' : #{menu.inspect} })")
+ end
+RUBY
+ if empty(result) || len(result) >= self.matching_limit
+ call s:HighlightError()
+ endif
+
+ if !empty(result)
+ call feedkeys("\<C-p>\<Down>", 'n')
+ endif
+
+ return result
+ endfunction
+
+ function! FuzzyFinderTextMateLauncher(initial_text, partial_matching, prev_bufnr, tag_files)
+ call g:FuzzyFinderMode.TextMate.launch(a:initial_text, a:partial_matching, a:prev_bufnr, a:tag_files)
+ endfunction
+
+ let g:FuzzyFinderOptions.TextMate = copy(g:FuzzyFinderOptions.File)
+endfunction "}}}
+
+if !exists('loaded_fuzzyfinder') "{{{
+ function! FuzzyFinderTextMateLauncher(initial_text, partial_matching, prev_bufnr, tag_files)
+ call InstantiateTextMateMode()
+ function! FuzzyFinderTextMateLauncher(initial_text, partial_matching, prev_bufnr, tag_files)
+ call g:FuzzyFinderMode.TextMate.launch(a:initial_text, a:partial_matching, a:prev_bufnr, a:tag_files)
+ endfunction
+ call g:FuzzyFinderMode.TextMate.launch(a:initial_text, a:partial_matching, a:prev_bufnr, a:tag_files)
+ endfunction
+ finish
+end "}}}
+
+call InstantiateTextMateMode()
+
+endif

4 comments on commit 0f5c8cb

@jinzhu

How to install it?
I have drop fuzzyfinder.vim and fuzzyfinder_textmate.vim into ~/.vim/plugin directory
and install fuzzy_file_finder
@@@sudo gem install jamis-fuzzy_file_finder@@@
But It’s doesn’t work…… :(

@dpetersen

Works here. Is your version of Vim compiled with Ruby support? I think you can check that by doing this in normal mode:

:ruby puts “ruby supported”

Note that it has a colon in the front.

@dpetersen

Wow that failed horribly. Glad I went to the Textile reference to try and figure that one out. One more time, with feeling… Do this:

:ruby puts "ruby supported"
@jinzhu

Oh, Thank you.
It’s workable :)

Please sign in to comment.