Sat, 11 Mar 2023 21:38:57 -0600
Start porting custom commands to lua
1022 | 1 | func! vimrc#AutoFmtToggle() abort |
2 | if &formatoptions =~# 'a' | |
3 | setl formatoptions-=a | echo '-a' | |
4 | else | |
5 | setl formatoptions+=a | echo '+a' | |
6 | endif | |
7 | endfunc | |
8 | ||
9 | func! vimrc#Grep(...) abort | |
10 | let pattern = get(a:000, 0, expand('<cword>')) | |
11 | let cmd = join([&grepprg, shellescape(pattern)] + a:000[1:], ' ') | |
12 | ||
13 | echo cmd | |
14 | cgetexpr system(cmd) | |
15 | call setqflist([], 'a', {"title": cmd}) | |
16 | let @/ = '\v' . pattern | |
17 | copen | |
18 | cfirst | |
19 | endfunc | |
20 | ||
21 | func! vimrc#SafeFilterFile(cmd) | |
22 | let errors = tempname() | |
23 | try | |
24 | exec 'silent %!' . a:cmd . ' 2>' . shellescape(errors) | |
25 | if v:shell_error | |
26 | for line in readfile(errors) | |
27 | echomsg line | |
28 | endfor | |
29 | endif | |
30 | finally | |
31 | call delete(errors) | |
32 | endtry | |
33 | endfunc | |
34 | ||
35 | if has('perl') | |
36 | func! vimrc#PruneSession() abort | |
37 | perl <<END_PERL | |
38 | my @bufs = | |
39 | grep { !-e $_->Name || -d _ || (-M _ >= 30) } | |
40 | grep { $_->Name } VIM::Buffers(); | |
41 | ||
42 | while (my $b = shift @bufs) { | |
43 | VIM::Msg 'pruned: ' . $b->Name, 'Comment'; | |
44 | VIM::DoCommand 'bwipeout ' . $b->Number; | |
45 | } | |
46 | VIM::DoCommand 'bprev' | |
47 | unless $curbuf->Name; | |
48 | END_PERL | |
49 | endfunc | |
50 | endif | |
51 | ||
52 | if has('ruby') | |
53 | func! s:PruneFiles(path, days) abort | |
54 | ruby <<END_RUBY | |
55 | require 'pathname' | |
56 | ||
57 | (path, days) = VIM.evaluate('[a:path, a:days]') | |
58 | sunset = Time.now - (days * 86400) | |
59 | ||
60 | Pathname(path).realpath.each_child do |file| | |
61 | file.delete if file.mtime < sunset | |
62 | end | |
63 | END_RUBY | |
64 | endfunc | |
65 | else | |
66 | func! s:PruneFiles(path, days) abort | |
67 | endfunc | |
68 | endif | |
69 | ||
70 | func! vimrc#PruneFiles(path, days) abort | |
71 | call s:PruneFiles(a:path, a:days) | |
72 | endfunc | |
73 |