Mon, 06 Mar 2023 23:43:39 -0500
use current format function
1022 | 1 | func! vimrc#CommandAlias(abbrev, expand) abort |
2 | execute printf('cnoreabbrev <expr> %s (getcmdtype()==":" && getcmdline()=="%s") ? "%s" : "%s"', a:abbrev, a:abbrev, a:expand, a:abbrev) | |
3 | endfunc | |
4 | ||
5 | func! vimrc#AutoFmtToggle() abort | |
6 | if &formatoptions =~# 'a' | |
7 | setl formatoptions-=a | echo '-a' | |
8 | else | |
9 | setl formatoptions+=a | echo '+a' | |
10 | endif | |
11 | endfunc | |
12 | ||
13 | " Make paths when writing, as necessary | |
14 | func! vimrc#MkNonExDir(file, buf) abort | |
15 | if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/' | |
16 | let dir=fnamemodify(a:file, ':h') | |
17 | if !isdirectory(dir) | |
18 | call mkdir(dir, 'p') | |
19 | endif | |
20 | endif | |
21 | endfunc | |
22 | ||
23 | func! vimrc#AutoSessionCheck() abort | |
24 | if strlen(v:servername) > 0 && match(v:servername, 'VIM') == -1 | |
25 | let sessionfile = g:vimcache . "/session/" . tolower(v:servername) . ".vim" | |
26 | ||
27 | if filereadable(sessionfile) | |
28 | execute "source " . sessionfile | |
29 | endif | |
30 | endif | |
31 | endfunc | |
32 | ||
33 | func! vimrc#Grep(...) abort | |
34 | let pattern = get(a:000, 0, expand('<cword>')) | |
35 | let cmd = join([&grepprg, shellescape(pattern)] + a:000[1:], ' ') | |
36 | ||
37 | echo cmd | |
38 | cgetexpr system(cmd) | |
39 | call setqflist([], 'a', {"title": cmd}) | |
40 | let @/ = '\v' . pattern | |
41 | copen | |
42 | cfirst | |
43 | endfunc | |
44 | ||
45 | func! vimrc#Gcd() abort | |
46 | let root = system('git rev-parse --show-toplevel 2>/dev/null')[:-2] | |
47 | if ! v:shell_error | |
48 | exec 'cd ' . root | |
49 | endif | |
50 | pwd | |
51 | endfunc | |
52 | ||
53 | func! vimrc#Hgcd() abort | |
54 | let root = system('hg root 2>/dev/null')[:-2] | |
55 | if ! v:shell_error | |
56 | exec 'cd ' . root | |
57 | endif | |
58 | pwd | |
59 | endfunc | |
60 | ||
61 | func! vimrc#SafeFilterFile(cmd) | |
62 | let errors = tempname() | |
63 | try | |
64 | exec 'silent %!' . a:cmd . ' 2>' . shellescape(errors) | |
65 | if v:shell_error | |
66 | for line in readfile(errors) | |
67 | echomsg line | |
68 | endfor | |
69 | endif | |
70 | finally | |
71 | call delete(errors) | |
72 | endtry | |
73 | endfunc | |
74 | ||
75 | if has('perl') | |
76 | func! vimrc#PruneSession() abort | |
77 | perl <<END_PERL | |
78 | my @bufs = | |
79 | grep { !-e $_->Name || -d _ || (-M _ >= 30) } | |
80 | grep { $_->Name } VIM::Buffers(); | |
81 | ||
82 | while (my $b = shift @bufs) { | |
83 | VIM::Msg 'pruned: ' . $b->Name, 'Comment'; | |
84 | VIM::DoCommand 'bwipeout ' . $b->Number; | |
85 | } | |
86 | VIM::DoCommand 'bprev' | |
87 | unless $curbuf->Name; | |
88 | END_PERL | |
89 | endfunc | |
90 | endif | |
91 | ||
92 | func! vimrc#PrepDir(path) abort | |
93 | if !filewritable(a:path) | |
94 | call mkdir(a:path, 'p', 0700) | |
95 | endif | |
96 | endfunc | |
97 | ||
98 | if has('ruby') | |
99 | func! s:PruneFiles(path, days) abort | |
100 | ruby <<END_RUBY | |
101 | require 'pathname' | |
102 | ||
103 | (path, days) = VIM.evaluate('[a:path, a:days]') | |
104 | sunset = Time.now - (days * 86400) | |
105 | ||
106 | Pathname(path).realpath.each_child do |file| | |
107 | file.delete if file.mtime < sunset | |
108 | end | |
109 | END_RUBY | |
110 | endfunc | |
111 | else | |
112 | func! s:PruneFiles(path, days) abort | |
113 | endfunc | |
114 | endif | |
115 | ||
116 | func! vimrc#PruneFiles(path, days) abort | |
117 | call s:PruneFiles(a:path, a:days) | |
118 | endfunc | |
119 |