Commits

Josh VanderLinden committed 970fb67

Cleaning up. Added some new features to the grep work I did earlier.

Comments (0)

Files changed (1)

 nnoremap <silent> N Nzz
 
 " Find in files
-nnoremap <Leader>f :vimgrep /\<<C-r><C-w>\>/g **/*
-"nnoremap <Leader>g :vimgrep /\<<C-r><C-w>\>/g **/*<CR>
+function! s:FindCurrentWord()
+    " Find the word under the cursor in all files (recursively) in the cwd
+    normal "wyiw
+    call s:FindWordInFS('\<'.@w.'\>')
+endfunction
 
-function! s:FindWordInFS()
-    normal "wyiw
+function! s:FindNeedle()
+    " Find whatever is in the search buffer in all files (recursively in the
+    " current working directory
+    call s:FindWordInFS(@/)
+endfunction
+
+function! s:FindWordInFS(word)
+    " Searches for all instances of a:word in all text files in the current
+    " working directory (recursively).  Matches are shown with some context
+    " in a new scratch buffer
 
     tabnew
     setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
     setlocal modifiable
-    execute 'silent r!grep -rInC2 "\<'.@w.'\>" *'
+    execute 'silent r!egrep -riInC2 "'.a:word.'" *'
     normal ggdd
 
     nnoremap <buffer> <Esc> <C-w>c
-    nnoremap <buffer> <Return> <C-w>gF
+    nnoremap <buffer> <Return> ^<C-w>gF
+
+    let b:qf_list = []
+    python << EOF
+import vim
+import re
+
+magic = re.compile(r'^([^:-]+):(\d+):(.*)$')
+
+for line in vim.current.buffer:
+    matches = magic.match(line)
+    if matches:
+        vim.command('let l:qf_item = {}')
+        vim.command('let l:qf_item.filename = "%s"' % matches.group(1))
+        vim.command('let l:qf_item.lnum = %s' % matches.group(2))
+        vim.command("let l:qf_item.text = '%s'" % matches.group(3).replace("'", "''"))
+        vim.command('call add(b:qf_list, l:qf_item)')
+EOF
+    call setqflist(b:qf_list, 'r')
+
     set nomodifiable
 endfunction
 
-command! FindWordInFS call s:FindWordInFS()
-nnoremap <Leader>g :FindWordInFS<CR>
-nnoremap tc :tabclose!<CR>
+command! FindCurrentWord call s:FindCurrentWord()
+command! FindNeedle call s:FindNeedle()
+nnoremap <Leader>f :FindNeedle<CR>
+nnoremap <Leader>g :FindCurrentWord<CR>
+nnoremap tc :tabclose!<CR><CR><CR>
 
 " Next/Previous matches in files
 nnoremap <silent> <F12> :cn<CR>zz