about summary refs log tree commit diff stats
path: root/contrib/vim/plugin
diff options
context:
space:
mode:
authorRory Bradford <roryrjb@gmail.com>2020-08-08 14:53:44 +0100
committerRory Bradford <roryrjb@gmail.com>2020-08-08 14:53:44 +0100
commitb2f8846c34af5271941d7bddd65fd45fcba6b5a9 (patch)
treec0c3d2897c16c2747949198928d898d8c5f63a58 /contrib/vim/plugin
parent2ffea3b5158349e05fde36eef67179fa8da29808 (diff)
downloadrf-b2f8846c34af5271941d7bddd65fd45fcba6b5a9.tar.gz
Add contrib/ with Vim integration example
Signed-off-by: Rory Bradford <roryrjb@gmail.com>
Diffstat (limited to 'contrib/vim/plugin')
-rw-r--r--contrib/vim/plugin/rf.vim80
1 files changed, 80 insertions, 0 deletions
diff --git a/contrib/vim/plugin/rf.vim b/contrib/vim/plugin/rf.vim
new file mode 100644
index 0000000..cf4649b
--- /dev/null
+++ b/contrib/vim/plugin/rf.vim
@@ -0,0 +1,80 @@
+if exists('loaded_rf')
+    finish
+endif
+
+let loaded_rf = 1
+
+set errorformat+=%f
+
+function! g:RFGrepR(args, ft)
+    let results = system('grep -nrI ' . shellescape(a:args) . " $(rf -- " . shellescape(a:ft) . ")")
+    cgete results | copen 9
+endfunction
+
+function! g:RFPrompt()
+    echo "RF: "
+
+    let l:rf_pattern = ''
+    let l:rf_cmd = 0
+    let l:rf_results = []
+
+    while 1
+        let c = getchar()
+
+        if c == 13
+            if l:rf_pattern == ''
+                cclose
+                break
+            endif
+
+            let i = getqflist({'idx' : 0}).idx
+
+            if len(l:rf_results) > 0
+                cclose
+                execute('e ' . l:rf_results[i - 1])
+            endif
+
+            break
+        elseif c == "\<BS>"
+            let l:rf_pattern = l:rf_pattern[0:-2]
+        elseif c == "\<UP>"
+            let i = getqflist({'idx' : 0}).idx
+
+            if i > 0
+                call setqflist([], 'a', {'idx' : i - 1})
+                redraw
+            endif
+
+            let l:rf_cmd = 1
+        elseif c == "\<DOWN>"
+            let i = getqflist({'idx' : 0}).idx
+            call setqflist([], 'a', {'idx' : i + 1})
+            redraw
+            let l:rf_cmd = 1
+        else
+            let ch = nr2char(c)
+            let l:rf_pattern = l:rf_pattern . ch
+        endif
+
+        if l:rf_cmd == 0
+            echo "RF: " . l:rf_pattern
+            redraw
+
+            let results = system('rf -ws -- ' . shellescape(l:rf_pattern))
+            let results_list = split(results, '\n')
+            let l:rf_results = results_list
+
+            if len(results_list) > 0
+                cgete results | copen 9
+                redraw
+            else
+                cexpr []
+            endif
+        else
+            let l:rf_cmd = 0
+        endif
+    endwhile
+endfunction
+
+command! -nargs=* RF call RFPrompt()
+command! -nargs=* RFGrep call RFGrepR(<f-args>)