From 67537d50f04568bcbc8f3b59a0a6d108bf89d423 Mon Sep 17 00:00:00 2001 From: netoctone Date: Fri, 4 May 2018 12:00:53 +0100 Subject: [PATCH] Add fzf-complete-path-relative --- README.md | 25 +++++++++++++------------ autoload/fzf/vim/complete.vim | 27 +++++++++++++++++++++++++++ plugin/fzf.vim | 18 ++++++++++-------- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 315b14df3..2819c48fc 100644 --- a/README.md +++ b/README.md @@ -216,18 +216,19 @@ command! -bang -nargs=? -complete=dir Files Mappings -------- -| Mapping | Description | -| --- | --- | -| `(fzf-maps-n)` | Normal mode mappings | -| `(fzf-maps-i)` | Insert mode mappings | -| `(fzf-maps-x)` | Visual mode mappings | -| `(fzf-maps-o)` | Operator-pending mappings | -| `(fzf-complete-word)` | `cat /usr/share/dict/words` | -| `(fzf-complete-path)` | Path completion using `find` (file + dir) | -| `(fzf-complete-file)` | File completion using `find` | -| `(fzf-complete-file-ag)` | File completion using `ag` | -| `(fzf-complete-line)` | Line completion (all open buffers) | -| `(fzf-complete-buffer-line)` | Line completion (current buffer only) | +| Mapping | Description | +| --- | --- | +| `(fzf-maps-n)` | Normal mode mappings | +| `(fzf-maps-i)` | Insert mode mappings | +| `(fzf-maps-x)` | Visual mode mappings | +| `(fzf-maps-o)` | Operator-pending mappings | +| `(fzf-complete-word)` | `cat /usr/share/dict/words` | +| `(fzf-complete-path)` | Path completion using `find` (file + dir) | +| `(fzf-complete-path-relative)` | Generate relative path (starting ./ or ../) | +| `(fzf-complete-file)` | File completion using `find` | +| `(fzf-complete-file-ag)` | File completion using `ag` | +| `(fzf-complete-line)` | Line completion (all open buffers) | +| `(fzf-complete-buffer-line)` | Line completion (current buffer only) | ### Usage diff --git a/autoload/fzf/vim/complete.vim b/autoload/fzf/vim/complete.vim index 03c9b3b90..d96f461b2 100644 --- a/autoload/fzf/vim/complete.vim +++ b/autoload/fzf/vim/complete.vim @@ -54,6 +54,7 @@ endfunction " ---------------------------------------------------------------------------- " (fzf-complete-path) +" (fzf-complete-path-relative) " (fzf-complete-file) " (fzf-complete-file-ag) " ---------------------------------------------------------------------------- @@ -125,6 +126,32 @@ function! s:fname_prefix(str) return prefix endfunction +function! s:generate_relative(path) + let target = getcwd() . '/' . (join(a:path)) + let base = expand('%:p:h') + + let prefix = "" + while stridx(target, base) != 0 + let base = substitute(system('dirname ' . base), '\n\+$', '', '') + let prefix = '../' . prefix + endwhile + + if prefix == '' + let prefix = './' + endif + + return prefix . substitute(target, base . '/', '', '') +endfunction + +function! fzf#vim#complete#path_relative(command, ...) + let s:file_cmd = a:command + return fzf#vim#complete(s:extend({ + \ 'prefix': s:function('s:fname_prefix'), + \ 'source': s:function('s:file_source'), + \ 'options': s:function('s:file_options'), + \ 'reducer': s:function('s:generate_relative')}, get(a:000, 0, fzf#wrap()))) +endfunction + function! fzf#vim#complete#path(command, ...) let s:file_cmd = a:command return fzf#vim#complete(s:extend({ diff --git a/plugin/fzf.vim b/plugin/fzf.vim index bd6c83602..d2fbfb44a 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -123,17 +123,19 @@ augroup fzf_buffers autocmd BufDelete * silent! call remove(g:fzf#vim#buffers, expand('')) augroup END -inoremap (fzf-complete-word) fzf#vim#complete#word() +inoremap (fzf-complete-word) fzf#vim#complete#word() if s:is_win - inoremap (fzf-complete-path) fzf#vim#complete#path('dir /s/b') - inoremap (fzf-complete-file) fzf#vim#complete#path('dir /s/b/a:-d') + inoremap (fzf-complete-path) fzf#vim#complete#path('dir /s/b') + inoremap (fzf-complete-path-relative) fzf#vim#complete#path_relative('dir /s/b/') + inoremap (fzf-complete-file) fzf#vim#complete#path('dir /s/b/a:-d') else - inoremap (fzf-complete-path) fzf#vim#complete#path("find . -path '*/\.*' -prune -o -print \| sed '1d;s:^..::'") - inoremap (fzf-complete-file) fzf#vim#complete#path("find . -path '*/\.*' -prune -o -type f -print -o -type l -print \| sed 's:^..::'") + inoremap (fzf-complete-path) fzf#vim#complete#path("find . -path '*/\.*' -prune -o -print \| sed '1d;s:^..::'") + inoremap (fzf-complete-path-relative) fzf#vim#complete#path_relative("find . -path '*/\.*' -prune -o -print \| sed '1d;s:^..::'") + inoremap (fzf-complete-file) fzf#vim#complete#path("find . -path '*/\.*' -prune -o -type f -print -o -type l -print \| sed 's:^..::'") endif -inoremap (fzf-complete-file-ag) fzf#vim#complete#path('ag -l -g ""') -inoremap (fzf-complete-line) fzf#vim#complete#line() -inoremap (fzf-complete-buffer-line) fzf#vim#complete#buffer_line() +inoremap (fzf-complete-file-ag) fzf#vim#complete#path('ag -l -g ""') +inoremap (fzf-complete-line) fzf#vim#complete#line() +inoremap (fzf-complete-buffer-line) fzf#vim#complete#buffer_line() nnoremap (fzf-maps-n) :call fzf#vim#maps('n', 0) inoremap (fzf-maps-i) :call fzf#vim#maps('i', 0)