/
file.vim
156 lines (139 loc) · 4.1 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
" Utilities for file copy/move/mkdir/etc.
let s:save_cpo = &cpo
set cpo&vim
let s:is_windows = has('win16') || has('win32') || has('win64')
let s:is_cygwin = has('win32unix')
let s:is_mac = !s:is_windows && !s:is_cygwin
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
\ (!executable('xdg-open') && system('uname') =~? '^darwin'))
" Open a file.
function! s:open(filename) "{{{
let filename = fnamemodify(a:filename, ':p')
" Detect desktop environment.
if s:is_windows
" For URI only.
let filename = iconv(filename, &encoding, 'char')
silent execute '!start rundll32 url.dll,FileProtocolHandler' filename
elseif s:is_cygwin
" Cygwin.
call system(printf('%s %s', 'cygstart',
\ shellescape(filename)))
elseif executable('xdg-open')
" Linux.
call system(printf('%s %s &', 'xdg-open',
\ shellescape(filename)))
elseif exists('$KDE_FULL_SESSION') && $KDE_FULL_SESSION ==# 'true'
" KDE.
call system(printf('%s %s &', 'kioclien exec',
\ shellescape(filename)))
elseif exists('$GNOME_DESKTOP_SESSION_ID')
" GNOME.
call system(printf('%s %s &', 'gnome-open',
\ shellescape(filename)))
elseif executable('exo-open')
" Xfce.
call system(printf('%s %s &', 'exo-open',
\ shellescape(filename)))
elseif s:is_mac && executable('open')
" Mac OS.
call system(printf('%s %s &', 'open',
\ shellescape(filename)))
else
" Give up.
throw 'Not supported.'
endif
endfunction "}}}
" Move a file.
" Dispatch s:move_file_exe() or s:move_file_pure().
function! s:move_file(src, dest) "{{{
if executable('mv')
return s:move_file_exe(a:src, a:dest)
else
return s:move_file_pure(a:src, a:dest)
endif
endfunction "}}}
" Move a file.
" Implemented by 'mv' executable.
" TODO: Support non-*nix like system.
function! s:move_file_exe(src, dest)
if !executable('mv') | return 0 | endif
silent execute '!mv' shellescape(a:src) shellescape(a:dest)
if v:shell_error
return 0
endif
return 1
endfunction
" Move a file.
" Implemented by pure vimscript.
function! s:move_file_pure(src, dest) "{{{
return !rename(a:src, a:dest)
endfunction "}}}
" Copy a file.
" Dispatch s:copy_file_exe() or s:copy_file_pure().
function! s:copy_file(src, dest) "{{{
if executable('cp')
return s:copy_file_exe(a:src, a:dest)
else
return s:copy_file_pure(a:src, a:dest)
endif
endfunction "}}}
" Copy a file.
" Implemented by 'cp' executable.
" TODO: Support non-*nix like system.
function! s:copy_file_exe(src, dest)
if !executable('cp') | return 0 | endif
silent execute '!cp' shellescape(a:src) shellescape(a:dest)
if v:shell_error
return 0
endif
return 1
endfunction
" Copy a file.
" Implemented by pure vimscript.
function! s:copy_file_pure(src, dest) "{{{
let ret = writefile(readfile(a:src, "b"), a:dest, "b")
if ret == -1
return 0
endif
return 1
endfunction "}}}
" mkdir() but does not throw an exception.
" Returns true if success.
" Returns false if failure.
function! s:mkdir_nothrow(...) "{{{
try
call call('mkdir', a:000)
return 1
catch
return 0
endtry
endfunction "}}}
" rmdir recursively.
function! s:rmdir(path, ...)
let flags = a:0 ? a:1 : ''
if exists("*rmdir")
return call('rmdir', [a:path] + a:000)
elseif has("unix")
let option = ''
let option .= flags =~# 'f' ? ' -f' : ''
let option .= flags =~# 'r' ? ' -r' : ''
let ret = system("/bin/rm" . option . ' ' . shellescape(a:path) . ' 2>&1')
elseif has("win32") || has("win95") || has("win64") || has("win16")
let option = ''
if &shell =~? "sh$"
let option .= flags =~# 'f' ? ' -f' : ''
let option .= flags =~# 'r' ? ' -r' : ''
let ret = system("/bin/rm" . option . ' ' . shellescape(a:path))
else
let option .= flags =~# 'f' ? ' /Q' : ''
let option .= flags =~# 'r' ? ' /S' : ''
let ret = system("rmdir" . option . ' "' . a:path . '"')
endif
endif
if v:shell_error
throw substitute(iconv(ret, 'char', &encoding), '\n', '', 'g')
endif
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set et ts=2 sts=2 sw=2 tw=0: