dmenu.vim

dmenu functions for vim
git clone git://edryd.org/dmenu.vim
Log | Files | Refs

dmenu.vim (2186B)


      1 " dmenu.vim - dmenu functions for vim
      2 " Author:  Ed van Bruggen
      3 " Version: 1.0
      4 " License: Vim License. See :help license
      5 
      6 if exists('g:loaded_dmenu')
      7   finish
      8 endif
      9 let g:loaded_dmenu = 1
     10 
     11 let s:save_cpo = &cpo
     12 set cpo&vim
     13 
     14 " global varibles {{{1
     15 if !exists('g:dmenu_cmd')
     16   let g:dmenu_cmd = 'dmenu -i -l 20'
     17 endif
     18 
     19 if !exists('g:dmenu_ls')
     20   let g:dmenu_ls = 'find -type f -printf "%P\n" | tail -n +2'
     21 endif
     22 
     23 if !exists('g:dmenu_git_ls')
     24   let g:dmenu_git_ls= 'git ls-files'
     25 endif
     26 
     27 if !exists('g:dmenu_ctags')
     28   let g:dmenu_ctags = 'ctags -f - --sort=no -R . | cut -f 1'
     29 endif
     30 
     31 " functions {{{1
     32 " return true if vim is in a git repository
     33 function! s:is_git_repo()
     34   call system('git rev-parse')
     35   return v:shell_error ? 0 : 1
     36 endfunction
     37 
     38 " launches dmenu with input and opens selection with given vim cmd
     39 function! s:dmenu_open(input, cmd)
     40   let file = system('echo -ne "' . a:input . '" | ' . g:dmenu_cmd . ' -p :' . a:cmd)
     41   if !empty(file)
     42     execute a:cmd . ' ' . file
     43   endif
     44 endfunction
     45 
     46 " open file in cwd with cmd
     47 function! s:dmenu_file(cmd)
     48   let files = s:is_git_repo() ? g:dmenu_git_ls : g:dmenu_ls
     49   call s:dmenu_open(system(files), a:cmd)
     50 endfunction
     51 
     52 " switch to already loaded buffer with cmd
     53 function! s:dmenu_buf(cmd)
     54   let bufs = range(1, bufnr('$'))       " get open buffers
     55   call filter(bufs, 'buflisted(v:val)') " remove unlisted buffers
     56   if bufname('#') != bufname('%') " if there is a previous buffer, move it to top
     57     let bufs = ['#'] + bufs
     58   endif
     59   let files = map(copy(bufs), 'bufname(v:val)') " convert numbers to names
     60   let files = filter(copy(files), 'v:val !~ bufname("#") || v:key == 0') " only leave previous buffer at top
     61   call s:dmenu_open(join(files, '\n'), a:cmd) " convert files list into string for dmenu
     62 endfunction
     63 
     64 function! s:dmenu_tag()
     65   call system('ctags -R .') " TODO dont create tags files
     66   let file = system(g:dmenu_ctags . ' | ' . g:dmenu_cmd . ' -p :tag')
     67   if !empty(file)
     68     execute 'tag ' . file
     69   endif
     70 endfunction
     71 
     72 " commands {{{1
     73 command! -nargs=1 DmenuFile call s:dmenu_file(<args>)
     74 command! -nargs=1 DmenuBuf  call s:dmenu_buf(<args>)
     75 command!          DmenuTag  call s:dmenu_tag()
     76 
     77 " }}}
     78 
     79 let &cpo = s:save_cpo