-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.vim
69 lines (54 loc) · 1.48 KB
/
file.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function! s:rename_current_file(new_path) abort
let l:bufnr = bufnr('%')
let l:old_path = expand('%:p')
let l:new_path = fnamemodify(a:new_path, ':p')
if l:new_path ==# l:old_path || l:new_path ==# ''
return
endif
if !filereadable(l:old_path)
exec 'write' fnameescape(l:new_path)
return
endif
if &modified
noautocmd w
endif
if filereadable(l:new_path)
let l:answer = input(l:new_path . ' already exists, overwrite? (y/n)')
call inputrestore()
if l:answer !~# '^[yY]$'
return
endif
call s:delete(l:new_path)
endif
keepalt enew
exec l:bufnr . 'bwipeout!'
" ensure directory
call mkdir(fnamemodify(l:new_path, ':h'), 'p')
" rename(2) is required by https://github.com/facebook/watchman
" @see https://man7.org/linux/man-pages/man2/rename.2.html
call v:lua.os.rename(l:old_path, l:new_path)
exec 'keepalt' 'edit' fnameescape(l:new_path)
endfunction
function! s:delete(file) abort
" @see https://github.com/ali-rantakari/trash
if executable('trash')
call jobstart(['trash', a:file])
else
call delete(a:file)
endif
endfunction
function! s:delete_current_file() abort
let l:bufnr = bufnr('%')
let l:file = expand('%:p')
if empty(l:file)
return
endif
if &modified
noautocmd w
endif
keepalt enew
exec l:bufnr . 'bwipeout!'
call s:delete(l:file)
endfunction
command! -nargs=1 -complete=file Rename call <SID>rename_current_file(<q-args>)
command! -nargs=0 Delete call s:delete_current_file()