Skip to content

Commit 6feae66

Browse files
committed
Initial commit.
1 parent 8e22725 commit 6feae66

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Diff for: plugin/.mygrep.vim.swp

12 KB
Binary file not shown.

Diff for: plugin/mygrep.vim

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
" TODO: Move these mappings elsewhere.
2+
nnoremap <silent> <Leader>gg :set operatorfunc=MyGrepOperator<CR>g@
3+
vnoremap <silent> <Leader>gg :<C-u>call MyGrepOperator(visualmode())<CR>
4+
5+
6+
nnoremap <silent> ]g :call MyGrepReplaceNext()<CR>
7+
nnoremap <silent> [g :call MyGrepReplacePrevious()<CR>
8+
nnoremap <silent> <Leader>gr :call SetNewGrepString()<CR>
9+
nnoremap <silent> <Leader>ga :call MyGrepReplaceAll()<CR>
10+
nnoremap <silent> <Leader>r :call MyGrepReplace()<CR>
11+
12+
function! MyGrepReplaceAll()
13+
if !GrepHasRun()
14+
return
15+
endif
16+
17+
silent! cc 1
18+
for item in g:grepped_items_list
19+
call MyGrepReplace()
20+
silent! cnext
21+
endfor
22+
endfunction!
23+
24+
function! MyGrepReplaceNext()
25+
if !GrepHasRun()
26+
return
27+
endif
28+
29+
silent! cnext
30+
call MyGrepReplace()
31+
endfunction
32+
33+
function! MyGrepReplacePrevious()
34+
if !GrepHasRun()
35+
return
36+
endif
37+
38+
silent! cprev
39+
call MyGrepReplace()
40+
endfunction
41+
42+
function! MyGrepOperator(type)
43+
let l:current_buffer = winnr()
44+
if a:type ==# 'char'
45+
silent execute "normal! `[v`]y"
46+
elseif a:type ==# 'v'
47+
silent execute "normal! `<v`>y"
48+
endif
49+
let g:current_grep_str = @@
50+
51+
let l:exclude_flags = " . --exclude-dir=blaze-\\* --exclude=\\*.swp"
52+
let l:cmd="grep! -R " . shellescape(g:current_grep_str) . l:exclude_flags
53+
let g:grepped_items_list = getqflist()
54+
let g:grepped_items_current_index = 0
55+
silent execute l:cmd
56+
redraw!
57+
botright copen
58+
silent execute l:current_buffer . "wincmd w"
59+
60+
61+
let l:filenames = {}
62+
for item in g:grepped_items_list
63+
let l:filenames[bufname(item['bufnr'])] = 1
64+
endfor
65+
66+
let l:filenames_list = keys(l:filenames)
67+
68+
echohl ModeMsg
69+
echom "Grepped: '" . g:current_grep_str . "' (" .
70+
\ len(g:grepped_items_list) . "X, " .
71+
\ len(l:filenames_list) . " file(s))"
72+
echohl None
73+
endfunction
74+
75+
function! GrepHasRun()
76+
if !exists("g:current_grep_str")
77+
return 0
78+
elseif !exists("g:grepped_items_list")
79+
return 0
80+
elseif len(g:grepped_items_list) == 0
81+
return 0
82+
endif
83+
return 1
84+
endfunction
85+
86+
function! MyGrepReplace()
87+
if !GrepHasRun()
88+
return
89+
endif
90+
91+
if !exists("g:new_grep_string")
92+
call SetNewGrepString()
93+
endif
94+
let l:cmd= "s/" . g:current_grep_str. "/" . g:new_grep_string . "/gce"
95+
execute l:cmd
96+
endfunction
97+
98+
function! SetNewGrepString()
99+
echohl ModeMsg | echom "Replace " . g:current_grep_str . " with ?"
100+
" TODO: Custom completion?
101+
echohl Search
102+
let g:new_grep_string = input(">>")
103+
echohl None
104+
endfunction
105+

0 commit comments

Comments
 (0)