Commits

Anonymous committed 5f4f1ed

Added 'trim_length' option.

  • Participants
  • Parent commits 6361deb

Comments (0)

Files changed (1)

File plugin/fuzzyfinder.vim

 "=============================================================================
 "
 " Author:  Takeshi NISHIDA <ns9tks@DELETE-ME.gmail.com>
-" Version: 2.7.1, for Vim 7.1
+" Version: 2.8, for Vim 7.1
 " Licence: MIT Licence
 " URL:     http://www.vim.org/scripts/script.php?script_id=1984
 "
 "
 "-----------------------------------------------------------------------------
 " ChangeLog:
-"   2.7.1:
+"   2.8:
+"     - Added 'trim_length' option.
 "     - Fixed a bug that entered command did not become the newest in the
 "       history.
 "     - Fixed a bug that folds could not open with <CR> in a command-line when
 "=============================================================================
 " FUNCTION: {{{1
 "-----------------------------------------------------------------------------
-" CORE FUNCTIONS:
-
-function! s:GetAvailableModes()
-  return filter(values(g:FuzzyFinderMode), 'exists(''v:val.mode_available'') && v:val.mode_available')
-endfunction
-
-function! s:GetSidPrefix()
-  return matchstr(expand('<sfile>'), '<SNR>\d\+_')
-endfunction
-
-function! s:OnCmdCR()
-  for m in s:GetAvailableModes()
-    call m.extend_options()
-    call m.on_command_pre(getcmdtype() . getcmdline())
-  endfor
-  " lets last entry become the newest in the history
-  if getcmdtype() =~ '[:/=@]'
-    call histadd(getcmdtype(), getcmdline())
-  endif
-
-  let suffix = (getcmdtype() == '/' || getcmdtype() == '?' ? 'zv' : '' )
-  " this is not mapped again (:help recursive_mapping)
-  return "\<CR>" . suffix
-endfunction
-
-"-----------------------------------------------------------------------------
 " LIST FUNCTIONS:
 
 function! s:Unique(in)
 endfunction
 
 "-----------------------------------------------------------------------------
+" STRING FUNCTIONS:
+
+" trims a:str and add a:mark if a length of a:str is more than a:len
+function! s:TrimTail(str, len)
+  if len(a:str) <= a:len
+    return a:str
+  endif
+  return a:str[:(a:len - len(s:ABBR_TRIM_MARK) - 1)] . s:ABBR_TRIM_MARK
+endfunction
+
+" takes suffix numer. if no digits, returns -1
+function! s:SuffixNumber(str)
+  let s = matchstr(a:str, '\d\+$')
+  return (len(s) ? str2nr(s) : -1)
+endfunction
+
+function! s:ConvertWildcardToRegexp(expr)
+  let re = escape(a:expr, '\')
+  for [pat, sub] in [ [ '*', '\\.\\*' ], [ '?', '\\.' ], [ '[', '\\[' ], ]
+    let re = substitute(re, pat, sub, 'g')
+  endfor
+  return '\V' . re
+endfunction
+
+" "foo/bar/hoge" -> { head: "foo/bar/", tail: "hoge" }
+function! s:SplitPath(path)
+  let dir = matchstr(a:path, '^.*[/\\]')
+  return  {
+        \   'head' : dir,
+        \   'tail' : a:path[strlen(dir):]
+        \ }
+endfunction
+
+"-----------------------------------------------------------------------------
 " FUNCTIONS FOR COMPLETION ITEM:
 
-function! s:FormatCompletionItem(expr, number, abbr, time, base_pattern, evals_path_tail)
+function! s:FormatCompletionItem(expr, number, abbr, trim_len, time, base_pattern, evals_path_tail)
   if a:evals_path_tail
     let rate = s:EvaluateMatchingRate(s:SplitPath(matchstr(a:expr, '^.*[^/\\]')).tail,
           \                           s:SplitPath(a:base_pattern).tail)
   endif
   return  {
         \   'word'  : a:expr,
-        \   'abbr'  : (a:number >= 0 ? printf('%2d: ', a:number) : '') . a:abbr,
+        \   'abbr'  : s:TrimTail((a:number >= 0 ? printf('%2d: ', a:number) : '') . a:abbr, a:trim_len),
         \   'menu'  : printf('%s[%s]', (len(a:time) ? a:time . ' ' : ''), s:MakeRateStar(rate, 5)),
         \   'order' : [-rate, (a:number >= 0 ? a:number : a:expr)]
         \ }
 "-----------------------------------------------------------------------------
 " MISC FUNCTIONS:
 
-" takes suffix numer. if no digits, returns -1
-function! s:SuffixNumber(str)
-  let s = matchstr(a:str, '\d\+$')
-  return (len(s) ? str2nr(s) : -1)
+function! s:GetAvailableModes()
+  return filter(values(g:FuzzyFinderMode), 'exists(''v:val.mode_available'') && v:val.mode_available')
 endfunction
 
-function! s:ConvertWildcardToRegexp(expr)
-  let re = escape(a:expr, '\')
-  for [pat, sub] in [ [ '*', '\\.\\*' ], [ '?', '\\.' ], [ '[', '\\[' ], ]
-    let re = substitute(re, pat, sub, 'g')
+function! s:GetSidPrefix()
+  return matchstr(expand('<sfile>'), '<SNR>\d\+_')
+endfunction
+
+function! s:OnCmdCR()
+  for m in s:GetAvailableModes()
+    call m.extend_options()
+    call m.on_command_pre(getcmdtype() . getcmdline())
   endfor
-  return '\V' . re
+  " lets last entry become the newest in the history
+  if getcmdtype() =~ '[:/=@]'
+    call histadd(getcmdtype(), getcmdline())
+  endif
+
+  let suffix = (getcmdtype() == '/' || getcmdtype() == '?' ? 'zv' : '' )
+  " this is not mapped again (:help recursive_mapping)
+  return "\<CR>" . suffix
 endfunction
 
 function! s:ExpandAbbrevMap(base, abbrev_map)
   return entries
 endfunction
 
-" "foo/bar/hoge" -> { head: "foo/bar/", tail: "hoge" }
-function! s:SplitPath(path)
-  let dir = matchstr(a:path, '^.*[/\\]')
-  return  {
-        \   'head' : dir,
-        \   'tail' : a:path[strlen(dir):]
-        \ }
-endfunction
-
 " line of :buffer -> { index, ind, path }
 function! s:ParseBufferLine(line)
   let parsed = matchlist(a:line, '^\s*\(\d*\)\([^"]*\)"\([^"]*\)".*$')
   let patterns = self.make_pattern(a:base)
   call s:EchoOnComplete(self.to_key(), patterns.wi, self.migemo_support)
   let result = s:FilterMatching(s:GetNonCurrentBuffers(), 'path', patterns.re, s:SuffixNumber(patterns.base), 0)
-  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.ind . v:val.path, "", a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.ind . v:val.path, self.trim_length, "", a:base, 1)')
 endfunction
 
 function! g:FuzzyFinderMode.Buffer.on_open(expr, mode)
   if len(result) >= self.matching_limit
     call s:HighlightError(1)
   endif
-  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, "", a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, self.trim_length, "", a:base, 1)')
 endfunction
 
 "-----------------------------------------------------------------------------
   let patterns = map(s:SplitPath(a:base), 'self.make_pattern(v:val)')
   let result = self.glob_dir_ex(patterns.head.base, patterns.tail.re, self.excluded_path, s:SuffixNumber(patterns.tail.base), 0)
   call s:EchoOnComplete(self.to_key(), patterns.head.base . patterns.tail.wi, self.migemo_support)
-  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, "", a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, self.trim_length, "", a:base, 1)')
 endfunction
 
 function! g:FuzzyFinderMode.Dir.on_open(expr, mode)
   let patterns = self.make_pattern(a:base)
   let result = s:FilterMatching(self.cache, 'path', patterns.re, s:SuffixNumber(patterns.base), 0)
   call s:EchoOnComplete(self.to_key(), patterns.wi, self.migemo_support)
-  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, v:val.time, a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, self.trim_length, v:val.time, a:base, 1)')
 endfunction
 
 function! g:FuzzyFinderMode.MruFile.on_mode_enter()
   let patterns = self.make_pattern(a:base)
   let result = s:FilterMatching(self.cache, 'command', patterns.re, s:SuffixNumber(patterns.base), 0)
   call s:EchoOnComplete(self.to_key(), patterns.wi, self.migemo_support)
-  return map(result, 's:FormatCompletionItem(v:val.command, v:val.index, v:val.command, v:val.time, a:base, 0)')
+  return map(result, 's:FormatCompletionItem(v:val.command, v:val.index, v:val.command, self.trim_length, v:val.time, a:base, 0)')
 endfunction
 
 function! g:FuzzyFinderMode.MruCmd.on_open(expr, mode)
   let patterns = self.make_pattern(a:base)
   let result = s:FilterMatching(self.cache, 'path', patterns.re, s:SuffixNumber(patterns.base), 0)
   call s:EchoOnComplete(self.to_key(), patterns.wi, self.migemo_support)
-  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, v:val.time, a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val.path, v:val.index, v:val.path, self.trim_length, v:val.time, a:base, 1)')
 endfunction
 
 function! g:FuzzyFinderMode.FavFile.on_mode_enter()
     call s:HighlightError(1)
   endif
   call s:EchoOnComplete(self.to_key(), patterns.wi, self.migemo_support)
-  return map(result,  's:FormatCompletionItem(v:val, -1, v:val, "", a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val, -1, v:val, self.trim_length, "", a:base, 1)')
 endfunction
 
 function! g:FuzzyFinderMode.Tag.on_open(expr, mode)
     call s:HighlightError(1)
   endif
   call s:EchoOnComplete(self.to_key(), patterns.wi, self.migemo_support)
-  return map(result,  's:FormatCompletionItem(v:val, -1, v:val, "", a:base, 1)')
+  return map(result, 's:FormatCompletionItem(v:val, -1, v:val, self.trim_length, "", a:base, 1)')
 endfunction
 
 function! g:FuzzyFinderMode.TaggedFile.find_tagged_file(pattern, matching_limit)
 let g:FuzzyFinderOptions.Base.abbrev_map = {}
 " [All Mode] It ignores case in search patterns if non-zero is set.
 let g:FuzzyFinderOptions.Base.ignore_case = 1
+" [All Mode] TODO
+let g:FuzzyFinderOptions.Base.trim_length = 80
 " [All Mode] It does not remove caches of completion lists at the end of
 " explorer to reuse at the next time if non-zero was set.
 let g:FuzzyFinderOptions.Base.lasting_cache = 1
 
 let s:PATH_SEPARATOR = (has('win32') || has('win64') ? '\' : '/')
 let s:MATCHING_RATE_BASE = 10000000
+let s:ABBR_TRIM_MARK = '...'
 
 augroup FuzzyfinderGlobal
   autocmd!