Sat, 11 Mar 2023 20:58:42 -0500
Highlight the matched part in :ltag
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 | func! vimrc#Grep(...) abort | |
14 | let pattern = get(a:000, 0, expand('<cword>')) | |
15 | let cmd = join([&grepprg, shellescape(pattern)] + a:000[1:], ' ') | |
16 | ||
17 | echo cmd | |
18 | cgetexpr system(cmd) | |
19 | call setqflist([], 'a', {"title": cmd}) | |
20 | let @/ = '\v' . pattern | |
21 | copen | |
22 | cfirst | |
23 | endfunc | |
24 | ||
25 | func! vimrc#Gcd() abort | |
26 | let root = system('git rev-parse --show-toplevel 2>/dev/null')[:-2] | |
27 | if ! v:shell_error | |
28 | exec 'cd ' . root | |
29 | endif | |
30 | pwd | |
31 | endfunc | |
32 | ||
33 | func! vimrc#Hgcd() abort | |
34 | let root = system('hg root 2>/dev/null')[:-2] | |
35 | if ! v:shell_error | |
36 | exec 'cd ' . root | |
37 | endif | |
38 | pwd | |
39 | endfunc | |
40 | ||
41 | func! vimrc#SafeFilterFile(cmd) | |
42 | let errors = tempname() | |
43 | try | |
44 | exec 'silent %!' . a:cmd . ' 2>' . shellescape(errors) | |
45 | if v:shell_error | |
46 | for line in readfile(errors) | |
47 | echomsg line | |
48 | endfor | |
49 | endif | |
50 | finally | |
51 | call delete(errors) | |
52 | endtry | |
53 | endfunc | |
54 | ||
55 | if has('perl') | |
56 | func! vimrc#PruneSession() abort | |
57 | perl <<END_PERL | |
58 | my @bufs = | |
59 | grep { !-e $_->Name || -d _ || (-M _ >= 30) } | |
60 | grep { $_->Name } VIM::Buffers(); | |
61 | ||
62 | while (my $b = shift @bufs) { | |
63 | VIM::Msg 'pruned: ' . $b->Name, 'Comment'; | |
64 | VIM::DoCommand 'bwipeout ' . $b->Number; | |
65 | } | |
66 | VIM::DoCommand 'bprev' | |
67 | unless $curbuf->Name; | |
68 | END_PERL | |
69 | endfunc | |
70 | endif | |
71 | ||
72 | if has('ruby') | |
73 | func! s:PruneFiles(path, days) abort | |
74 | ruby <<END_RUBY | |
75 | require 'pathname' | |
76 | ||
77 | (path, days) = VIM.evaluate('[a:path, a:days]') | |
78 | sunset = Time.now - (days * 86400) | |
79 | ||
80 | Pathname(path).realpath.each_child do |file| | |
81 | file.delete if file.mtime < sunset | |
82 | end | |
83 | END_RUBY | |
84 | endfunc | |
85 | else | |
86 | func! s:PruneFiles(path, days) abort | |
87 | endfunc | |
88 | endif | |
89 | ||
90 | func! vimrc#PruneFiles(path, days) abort | |
91 | call s:PruneFiles(a:path, a:days) | |
92 | endfunc | |
93 |