From 4abf1f58614fc6b4c20bb247db59469a9a52ff9b Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Mon, 31 Dec 2012 15:56:37 -0500 Subject: [PATCH] Make :Rename relative to containing directory Closes #5. --- doc/eunuch.txt | 3 ++- plugin/eunuch.vim | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/eunuch.txt b/doc/eunuch.txt index 2574a38..d8bf24c 100644 --- a/doc/eunuch.txt +++ b/doc/eunuch.txt @@ -31,7 +31,8 @@ COMMANDS *eunuch-commands* :Move[!] {file} Like |:saveas|, but delete the old file afterwards. *eunuch-:Rename* -:Rename[!] {file} Alias for |:Move|. +:Rename[!] {file} Like |:Move|, but relative to the current file's + containing directory. *eunuch-:Chmod* :Chmod {mode} Change the permissions of the current file. diff --git a/plugin/eunuch.vim b/plugin/eunuch.vim index 50db1b7..0c2cd82 100644 --- a/plugin/eunuch.vim +++ b/plugin/eunuch.vim @@ -7,6 +7,10 @@ if exists('g:loaded_eunuch') || &cp || v:version < 700 endif let g:loaded_eunuch = 1 +function! s:separator() + return !exists('+shellslash') || &shellslash ? '/' : '\\' +endfunction + command! -bar -bang Unlink : \ let v:errmsg = '' | \ let s:file = fnamemodify(bufname(),':p') | @@ -22,8 +26,10 @@ command! -bar -nargs=1 -bang -complete=file Move : \ let s:src = expand('%:p') | \ let s:dst = expand() | \ if isdirectory(s:dst) | - \ let s:dst .= '/' . fnamemodify(s:src, ':t') | + \ let s:dst .= (s:dst[-1:-1] =~# '[\\/]' ? '' : s:separator()) . + \ fnamemodify(s:src, ':t') | \ endif | + \ let s:dst = substitute(simplify(s:dst), '^\.\'.s:separator(), '', '') | \ if 1 && filereadable(s:dst) | \ exe 'keepalt saveas '.fnameescape(s:dst) | \ elseif rename(s:src, s:dst) | @@ -38,7 +44,17 @@ command! -bar -nargs=1 -bang -complete=file Move : \ unlet s:src | \ unlet s:dst -command! -bar -nargs=1 -bang -complete=file Rename :Move +function! s:Rename_complete(A, L, P) abort + let sep = s:separator() + let prefix = expand('%:p:h').sep + let files = split(glob(prefix.a:A.'*'), "\n") + call filter(files, 'simplify(v:val) !=# simplify(expand("%:p"))') + call map(files, 'v:val[strlen(prefix) : -1] . (isdirectory(v:val) ? sep : "")') + return join(files + ['..'.s:separator()], "\n") +endfunction + +command! -bar -nargs=1 -bang -complete=custom,s:Rename_complete Rename + \ Move %:h/ command! -bar -nargs=1 Chmod : \ echoerr split(system('chmod '..' -- '.shellescape(expand('%'))), "\n")[0] |