about summary refs log tree commit diff stats
path: root/contrib
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
parent2ffea3b5158349e05fde36eef67179fa8da29808 (diff)
downloadrf-b2f8846c34af5271941d7bddd65fd45fcba6b5a9.tar.gz
Add contrib/ with Vim integration example
Signed-off-by: Rory Bradford <roryrjb@gmail.com>
Diffstat (limited to 'contrib')
-rw-r--r--contrib/vim/Makefile5
-rw-r--r--contrib/vim/README45
-rw-r--r--contrib/vim/plugin/rf.vim80
3 files changed, 130 insertions, 0 deletions
diff --git a/contrib/vim/Makefile b/contrib/vim/Makefile
new file mode 100644
index 0000000..84493bc
--- /dev/null
+++ b/contrib/vim/Makefile
@@ -0,0 +1,5 @@
+DEST = ~/.vim/after/plugin
+
+install:
+	mkdir -p $(DEST)
+	install -m644 ./plugin/rf.vim $(DEST)
diff --git a/contrib/vim/README b/contrib/vim/README
new file mode 100644
index 0000000..5752ed0
--- /dev/null
+++ b/contrib/vim/README
@@ -0,0 +1,45 @@
+This directory provides basic rf integration in Vim.
+
+It can be installed from this directory using make:
+
+    make install
+
+The plugin provides two functions:
+
+*rf*
+
+    :RF
+
+This function runs a getchar() loop, allowing you to search from the
+root of the current working directory, using only substring patterns,
+the idea being that it is very quick and simple to use, rather than
+exposing more of the functionality that rf provides.
+
+I usually bind the function like so:
+
+    map <c-p> :RF <CR>
+
+The function is very simple and will call rf with your pattern
+*at every key stroke*. Usually this is performant enough, but
+with extremely large directory trees, you may have to wait several
+seconds for results, which isn't great. Aside from any improvements
+to the implementation, just make sure that any .rfignore files,
+whether local or global are ignoring everything that can be ignored.
+
+*rf grep*
+
+    :RFGrep search-pattern filename-pattern
+
+This function is a wrapper around `grep -r` and rf. It allows you
+to grep recursively over a smaller subset of files. Again the
+implementation is very simple but with very large directory trees
+or with filename patterns that match a lot of files you may
+hit the argument limit for grep.
+
+*Issues*
+
+Aside from the above mentioned limitations and issues, if you
+are using anything other than a "Bourne" shell (bash, ksh, zsh, sh)
+the functions may not work, in that case you may want to explictly:
+
+    set shell=sh
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>)