Skip to content

Commit

Permalink
Bump eunuch.vim
Browse files Browse the repository at this point in the history
  • Loading branch information
tpope committed Apr 30, 2014
1 parent e264250 commit f884ce7
Showing 1 changed file with 105 additions and 17 deletions.
122 changes: 105 additions & 17 deletions .vim/plugin/eunuch.vim
@@ -1,17 +1,30 @@
" eunuch.vim - Helpers for UNIX
" Maintainer: Tim Pope <http://tpo.pe/>
" Version: 1.0
" Version: 1.1

if exists('g:loaded_eunuch') || &cp || v:version < 700
finish
endif
let g:loaded_eunuch = 1

function! s:fnameescape(string) abort
if exists('*fnameescape')
return fnameescape(a:string)
elseif a:string ==# '-'
return '\-'
else
return substitute(escape(a:string," \t\n*?[{`$\\%#'\"|!<"),'^[+>]','\\&','')
endif
endfunction

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

command! -bar -bang Unlink :
\ let v:errmsg = '' |
\ let s:file = fnamemodify(bufname(<q-args>),':p') |
\ execute 'bdelete<bang>' |
\ if v:errmsg ==# '' && delete(s:file) |
\ if !bufloaded(s:file) && delete(s:file) |
\ echoerr 'Failed to delete "'.s:file.'"' |
\ endif |
\ unlet s:file
Expand All @@ -21,27 +34,48 @@ command! -bar -bang Remove :Unlink<bang>
command! -bar -nargs=1 -bang -complete=file Move :
\ let s:src = expand('%:p') |
\ let s:dst = expand(<q-args>) |
\ if isdirectory(s:dst) |
\ let s:dst .= '/' . fnamemodify(s:src, ':t') |
\ if isdirectory(s:dst) || s:dst[-1:-1] =~# '[\\/]' |
\ let s:dst .= (s:dst[-1:-1] =~# '[\\/]' ? '' : s:separator()) .
\ fnamemodify(s:src, ':t') |
\ endif |
\ if !isdirectory(fnamemodify(s:dst, ':h')) |
\ call mkdir(fnamemodify(s:dst, ':h'), 'p') |
\ endif |
\ let s:dst = substitute(simplify(s:dst), '^\.\'.s:separator(), '', '') |
\ if <bang>1 && filereadable(s:dst) |
\ exe 'keepalt saveas '.fnameescape(s:dst) |
\ exe 'keepalt saveas '.s:fnameescape(s:dst) |
\ elseif rename(s:src, s:dst) |
\ echoerr 'Failed to rename "'.s:src.'" to "'.s:dst.'"' |
\ else |
\ setlocal modified |
\ exe 'keepalt saveas! '.fnameescape(s:dst) |
\ exe 'keepalt saveas! '.s:fnameescape(s:dst) |
\ if s:src !=# expand('%:p') |
\ execute 'bwipe '.fnameescape(s:src) |
\ execute 'bwipe '.s:fnameescape(s:src) |
\ endif |
\ endif |
\ unlet s:src |
\ 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 :
\ echoerr split(system('chmod '.<q-args>.' -- '.shellescape(expand('%'))), "\n")[0] |
\ echoerr get(split(system('chmod '.<q-args>.' '.shellescape(expand('%'))), "\n"), 0, '') |

command! -bar -bang -nargs=? -complete=dir Mkdir
\ call mkdir(empty(<q-args>) ? expand('%:h') : <q-args>, <bang>0 ? 'p' : '') |
\ if empty(<q-args>) |
\ silent keepalt execute 'file' s:fnameescape(expand('%')) |
\ endif

command! -bar -bang -complete=file -nargs=+ Find :call s:Grep(<q-bang>, <q-args>, 'find')
command! -bar -bang -complete=file -nargs=+ Locate :call s:Grep(<q-bang>, <q-args>, 'locate')
Expand All @@ -63,21 +97,74 @@ function! s:Grep(bang,args,prg) abort
endtry
endfunction

command! -bar SudoWrite :
\ setlocal nomodified |
\ silent exe 'write !sudo tee % >/dev/null' |
\ let &modified = v:shell_error
function! s:SudoSetup(file) abort
if !filereadable(a:file) && !exists('#BufReadCmd#'.s:fnameescape(a:file))
execute 'autocmd BufReadCmd ' s:fnameescape(a:file) 'call s:SudoReadCmd()'
endif
if !filewritable(a:file) && !exists('#BufWriteCmd#'.s:fnameescape(a:file))
execute 'autocmd BufReadPost ' s:fnameescape(a:file) 'set noreadonly'
execute 'autocmd BufWriteCmd ' s:fnameescape(a:file) 'call s:SudoWriteCmd()'
endif
endfunction

command! -bar W :call s:W()
function! s:W() abort
function! s:SudoReadCmd() abort
silent %delete_
execute (has('gui_running') ? '' : 'silent') 'read !env SUDO_EDITOR=cat sudo -e %'
silent 1delete_
set nomodified
endfunction

function! s:SudoWriteCmd() abort
execute (has('gui_running') ? '' : 'silent') 'write !env SUDO_EDITOR=tee sudo -e % >/dev/null'
let &modified = v:shell_error
endfunction

command! -bar -bang -complete=file -nargs=? SudoEdit
\ call s:SudoSetup(fnamemodify(empty(<q-args>) ? expand('%') : <q-args>, ':p')) |
\ if !&modified || !empty(<q-args>) |
\ edit<bang> <args> |
\ endif |
\ if empty(<q-args>) || expand('%:p') ==# fnamemodify(<q-args>, ':p') |
\ set noreadonly |
\ endif

command! -bar SudoWrite
\ call s:SudoSetup(expand('%:p')) |
\ write!

function! s:SudoEditInit() abort
let files = split($SUDO_COMMAND, ' ')[1:-1]
if len(files) ==# argc()
for i in range(argc())
execute 'autocmd BufEnter' s:fnameescape(argv(i))
\ 'if empty(&filetype) || &filetype ==# "conf"'
\ '|doautocmd filetypedetect BufReadPost' s:fnameescape(files[i])
\ '|endif'
endfor
endif
endfunction
if $SUDO_COMMAND =~# '^sudoedit '
call s:SudoEditInit()
endif

command! -bar -nargs=? Wall
\ if empty(<q-args>) |
\ call s:Wall() |
\ else |
\ call system('wall', <q-args>) |
\ endif
if !exists(':W') !=# 2
command! -bar W Wall
endif
function! s:Wall() abort
let tab = tabpagenr()
let win = winnr()
let seen = {}
if !&readonly && expand('%') !=# ''
let seen[bufnr('')] = 1
write
endif
tabdo windo if !&readonly && expand('%') !=# '' && !has_key(seen, bufnr('')) | silent write | let seen[bufnr('')] = 1 | endif
tabdo windo if !&readonly && &buftype =~# '^\%(acwrite\)\=$' && expand('%') !=# '' && !has_key(seen, bufnr('')) | silent write | let seen[bufnr('')] = 1 | endif
execute 'tabnext '.tab
execute win.'wincmd w'
endfunction
Expand All @@ -95,6 +182,7 @@ augroup shebang_chmod
autocmd BufWritePost,FileWritePost *
\ if exists('b:chmod_post') && executable('chmod') |
\ silent! execute '!chmod '.b:chmod_post.' "<afile>"' |
\ edit |
\ unlet b:chmod_post |
\ endif
augroup END
Expand Down

0 comments on commit f884ce7

Please sign in to comment.