Skip to content

Commit

Permalink
Make :Rename relative to containing directory
Browse files Browse the repository at this point in the history
Closes #5.
  • Loading branch information
tpope committed Dec 31, 2012
1 parent c4e8d83 commit 4abf1f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion doc/eunuch.txt
Expand Up @@ -31,7 +31,8 @@ COMMANDS *eunuch-commands*
:Move[!] {file} Like |:saveas|, but delete the old file afterwards. :Move[!] {file} Like |:saveas|, but delete the old file afterwards.


*eunuch-:Rename* *eunuch-:Rename*
:Rename[!] {file} Alias for |:Move|. :Rename[!] {file} Like |:Move|, but relative to the current file's
containing directory.


*eunuch-:Chmod* *eunuch-:Chmod*
:Chmod {mode} Change the permissions of the current file. :Chmod {mode} Change the permissions of the current file.
Expand Down
20 changes: 18 additions & 2 deletions plugin/eunuch.vim
Expand Up @@ -7,6 +7,10 @@ if exists('g:loaded_eunuch') || &cp || v:version < 700
endif endif
let g:loaded_eunuch = 1 let g:loaded_eunuch = 1


function! s:separator()
return !exists('+shellslash') || &shellslash ? '/' : '\\'
endfunction

command! -bar -bang Unlink : command! -bar -bang Unlink :
\ let v:errmsg = '' | \ let v:errmsg = '' |
\ let s:file = fnamemodify(bufname(<q-args>),':p') | \ let s:file = fnamemodify(bufname(<q-args>),':p') |
Expand All @@ -22,8 +26,10 @@ command! -bar -nargs=1 -bang -complete=file Move :
\ let s:src = expand('%:p') | \ let s:src = expand('%:p') |
\ let s:dst = expand(<q-args>) | \ let s:dst = expand(<q-args>) |
\ if isdirectory(s:dst) | \ 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 | \ endif |
\ let s:dst = substitute(simplify(s:dst), '^\.\'.s:separator(), '', '') |
\ if <bang>1 && filereadable(s:dst) | \ if <bang>1 && filereadable(s:dst) |
\ exe 'keepalt saveas '.fnameescape(s:dst) | \ exe 'keepalt saveas '.fnameescape(s:dst) |
\ elseif rename(s:src, s:dst) | \ elseif rename(s:src, s:dst) |
Expand All @@ -38,7 +44,17 @@ command! -bar -nargs=1 -bang -complete=file Move :
\ unlet s:src | \ unlet s:src |
\ unlet s:dst \ unlet s:dst


command! -bar -nargs=1 -bang -complete=file Rename :Move<bang> <args> 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<bang> %:h/<args>


command! -bar -nargs=1 Chmod : command! -bar -nargs=1 Chmod :
\ echoerr split(system('chmod '.<q-args>.' -- '.shellescape(expand('%'))), "\n")[0] | \ echoerr split(system('chmod '.<q-args>.' -- '.shellescape(expand('%'))), "\n")[0] |
Expand Down

0 comments on commit 4abf1f5

Please sign in to comment.