Skip to content

Commit

Permalink
vim: Soup up Diffusion and search
Browse files Browse the repository at this point in the history
- Don't waste `<LocalLeader>s` on `:CommandTSearch`; I never use it.
- Instead, assign it to new `:Search` command (which is a terrible name
  and I will change it once I've thought of something better), which
  does a corp search for the word under the cursor (can also pass in a
  phrase explicitly at the Vim command prompt).
- Add `:OpenInDiffusion`, which is a souped-up version of what I
  previously had bound to `<LocalLeader>d` (itself just a basic search);
  this version is repo-aware.
  • Loading branch information
wincent committed Dec 16, 2016
1 parent 99ed835 commit 881328e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@
/roles/dotfiles/files/.vim/.base16
/roles/dotfiles/files/.vim/after/plugin/command-t.private.vim
/roles/dotfiles/files/.vim/autoload/functions/private.vim
/roles/dotfiles/files/.vim/plugin/private.vim
/roles/dotfiles/files/.vim/spell/en.utf-8.add.spl
/roles/dotfiles/files/.vim/tmp/viminfo
/roles/dotfiles/files/.vim/ultisnips-private/markdown.snippets
Expand Down
1 change: 0 additions & 1 deletion roles/dotfiles/files/.vim/after/plugin/command-t.vim
Expand Up @@ -29,7 +29,6 @@ nmap <unique> <Leader>c <Plug>(CommandTCommand)
nmap <unique> <Leader>h <Plug>(CommandTHelp)
nmap <unique> <LocalLeader>h <Plug>(CommandTHistory)
nmap <unique> <LocalLeader>l <Plug>(CommandTLine)
nmap <unique> <LocalLeader>s <Plug>(CommandTSearch)
nmap <unique> <LocalLeader>t <Plug>(CommandTTag)
let s:path = expand('<sfile>:p:h') . '/command-t.private.vim'
Expand Down
84 changes: 82 additions & 2 deletions roles/dotfiles/files/.vim/autoload/commands.vim
@@ -1,11 +1,11 @@
function s:preview(file)
function! s:preview(file)
" TODO: remove this hack once new version of Marked 2 is out:
" http://support.markedapp.com/discussions/questions/8670
silent! execute "!xattr -d com.apple.quarantine " . shellescape(a:file)
silent execute "!open -a 'Marked 2.app' " . shellescape(a:file)
endfunction

function commands#preview(...)
function! commands#preview(...) abort
if a:0 == 0
call s:preview(expand('%'))
else
Expand All @@ -14,3 +14,83 @@ function commands#preview(...)
endfor
endif
endfunction

function! s:open_in_diffusion(file)
let l:relative_path=''
let l:repos=keys(g:diffusion_map)
for l:repo in l:repos
let l:base=g:diffusion_map[l:repo]
let l:var='$' . l:repo
if exists(l:var)
let l:directory=eval(l:var)
if stridx(a:file, l:directory) == 0
let l:relative_path=strcharpart(a:file, strchars(l:directory))
break
endif
endif
endfor

if l:relative_path == ''
call functions#echoerr('Could not find repo configuration for file ' . a:file)
return
endif

let l:url=shellescape(l:base . s:url_encode(l:relative_path))
call system('open ' . l:url)
endfunction

" Opens the specified files (or the current file if there is no explicit
" selection) in Diffusion.
"
" Depends on `g:diffusion_map` being set up in `plugin/private.vim`.
function! commands#open_in_diffusion(...) abort
if a:0 == 0
let l:files=[expand('%')]
else
let l:files=a:000
endif
let l:did_open=0
for l:file in l:files
let l:file=fnamemodify(l:file, ':p')
if l:file != ''
call s:open_in_diffusion(l:file)
let l:did_open=1
endif
endfor

if !l:did_open
call functions#echoerr('No filename')
return
endif
endfunction

" Try to open the module under the cursor in Diffusion.
"
" Depends on g:search_base being set up in `pluging/private.vim`.
function! s:search(query)
" ibgs: normal search
" ibgl: "lucky" search, if results unambiguous
let l:url=shellescape(
\ g:search_base . 'ibgl+' .
\ s:url_encode(a:query)
\ )
call system('open ' . l:url)
endfunction

" Based on method of same name in: https://github.com/tpope/vim-unimpaired
function! s:url_encode(string)
return substitute(
\ a:string,
\ '[^A-Za-z0-9_.~-]',
\ '\="%" . printf("%02X", char2nr(submatch(0)))',
\ 'g'
\ )
endfunction

function! commands#search(query) abort
if a:query == ''
call s:search(expand('<cword>'))
else
call s:search(a:query)
endif
endfunction
10 changes: 10 additions & 0 deletions roles/dotfiles/files/.vim/autoload/functions.vim
Expand Up @@ -56,3 +56,13 @@ function! functions#clearregisters() abort
call setreg(l:reg, [])
endfor
endfunction

" Like :echoerr, but without the stack trace.
function! functions#echoerr(msg) abort
try
echohl ErrorMsg
echomsg a:msg
finally
echohl None
endtry
endfunction
22 changes: 22 additions & 0 deletions roles/dotfiles/files/.vim/plugin/.private.vim.encrypted
@@ -0,0 +1,22 @@
-----BEGIN PGP MESSAGE-----
Version: GnuPG v2

hQIMA/8IuvaF3PmcARAArSdZa0OIVPGrE+s9KxoowRkwJK2XiHbcaOtn89yZqSzD
TB7jv7UlvStztHlHafG+Q7j1moINwP6ItAMW2T0HpOfnZIRMCE3NVJcX2jFxkjjK
+GCb/ky34ojVnb03vpsa+aYNRePzWwSsWI9VGCwgTOUnX/HoKlMqQ8aDbG4IdxXC
lzSYk+V+5RAfmQnRKcjO53tLUlNhEbPXGJ3iZD7If+qkbGLHgqBG9QuIAeCXdfLf
KEojW1GRABFKK/Ec6l02uC2JBhPemISqETXXkVQWfI/pow8lR0upMMnZN6oPfhop
4hrAFX4p64oyCFDNLbbytdX4QUyReT43UeWCXDVmzFQlK/oZOUFP7jCMHBBkABLS
kycC3HlD5cxe8ztcJ9QWfe8Uog7xqtTceTLdt8dT73pYnfZYnmvU4TlInLeAYv1l
xUSeIQev0qevqhwkWfWRZeaoPxyhmHHBYVKO4sYrirASV02U+zwrUiCZhbYC4a5k
qQKy6qLRGgTBSY5H+5umd3L4Fqbq+NFTf6GcbTOUmneKXg8oWZbsJ9xWo+MY3gnt
t0aazq2VKFXbyGEFSMh3VmNhvdYCAbJoX3Kl6J4JIoKj+5Dtt9f/y1Vrix23eVg0
jV1voVtCJj7lLUWvPftr7Vy6ITkFaFsP0/OdXuJVMZgRs5bopltW4oS9T6vaj6LS
wFEBUx8rjXnAf6rzTfM530T/LP3TTXofdDjkmJ6v1mRZ/Q/FpNzUCpHkoXVtAEcb
YykmkJcytBxBZb4BQSa0VuvN7zApShNXJYput+E01IYo+JDX4Y9SiPJXwBUqN/Ft
t176zi+JFOQj0tSdHt0rxEvA53D5tbsKZy041cyqRffBgTYKrTBHTbbA7tpPGR01
M8msQ2pZoKz8M8ZIu0nY36JYC4GOM4V6+9+Dmyt7joAGb7ZJaW8szr/eunAILntS
kgFU7sR1Quu1z1q/Fj8ZZSx46f7BG/VLHSm1B/rxNtF6ThrbpkcNXU5UWE5zCqn8
nNBsWqmSK8teq3oQoA+cTfycw7CGMmL50YYNqSHaoA0SJgY=
=SSF1
-----END PGP MESSAGE-----
2 changes: 2 additions & 0 deletions roles/dotfiles/files/.vim/plugin/commands.vim
@@ -1 +1,3 @@
command! -nargs=* -complete=file OpenInDiffusion call commands#open_in_diffusion(<f-args>)
command! -nargs=* -complete=file Preview call commands#preview(<f-args>)
command! -nargs=* Search call commands#search(<q-args>)
3 changes: 2 additions & 1 deletion roles/dotfiles/files/.vim/plugin/mappings/leader.vim
Expand Up @@ -32,7 +32,8 @@ nnoremap <silent> <Leader>zz :call mappings#leader#zap()<CR>
" (mnemonic: coloring).
nnoremap <silent> <LocalLeader>c :syntax sync fromstart<CR>
nnoremap <silent> <LocalLeader>d :call functions#private#diffusion()<CR>
nnoremap <silent> <LocalLeader>d :OpenInDiffusion<CR>
nnoremap <silent> <LocalLeader>s :Search<CR>
" <LocalLeader>e -- Edit file, starting in same directory as current file.
nnoremap <LocalLeader>e :edit <C-R>=expand('%:p:h') . '/'<CR>
Expand Down

0 comments on commit 881328e

Please sign in to comment.