| 1 | " **grep.vim** |
| 2 | " |
| 3 | " Find the occurences of a string "myfunction" in all files of |
| 4 | " the current directory, and put the filenames, linenumbers and |
| 5 | " lines in a file _grep_myfunction_ in the &backupdir directory: |
| 6 | " |
| 7 | " call Grep("myfunction") |
| 8 | " |
| 9 | " In the _grep file use |
| 10 | " |
| 11 | " call GrepOpen() |
| 12 | " |
| 13 | " to edit the file on the current line, at the position where |
| 14 | " "myfunction" occurs. |
| 15 | " |
| 16 | " If you use filebrws.vim, the files in the _grep file are colored, |
| 17 | " the same way as in the filebrowser. |
| 18 | " |
| 19 | " MAPS: |
| 20 | " |
| 21 | " ,gr Find occurences of the word under the cursor, if no word |
| 22 | " under the cursor, prompt for a search word. |
| 23 | " Hit gr in visual mode to search for the visually selected |
| 24 | " word |
| 25 | " |
| 26 | " e In a _grep file: edit the file on the current line, at the |
| 27 | " position where the search string occurs. |
| 28 | " |
| 29 | " ,gs Split the window, and display the hits there. |
| 30 | " |
| 31 | " w In the grep-window: open the file on the current line in the |
| 32 | " other widow, keep displaying grep window |
| 33 | " |
| 34 | " ,gl Split-open the last grep-window. |
| 35 | " |
| 36 | " <esc> Quit the grep window. |
| 37 | " |
| 38 | " EXAMPLE: |
| 39 | " |
| 40 | " Hit gr with the cursor on bufenter and a file _grep_bufenter is |
| 41 | " opened containing lines like: |
| 42 | " |
| 43 | " bookmark.vim:14:au bufenter bookmarks nmap <cr> __loadfile- |
| 44 | " bookmark.vim:16:au bufenter bookmarks nmap <esc> :bd<cr>:b <c-r>e<cr> |
| 45 | " buflist.vim:16: au bufenter _buflist nmap <cr> :call BuflistEdit()<cr> |
| 46 | " buflist.vim:17: au bufenter _buflist nmap <esc> :call BuflistClose()<cr> |
| 47 | " buflist.vim:20: au bufenter _buflist nmap x :call BuflistDelete()<cr> |
| 48 | " comment.vim:164:au bufenter * let commentlinestyle="-" |
| 49 | " comment.vim:165:au bufenter *.html let commentlinestyle="=" |
| 50 | " |
| 51 | " Now position the cursor on the second line and hit e, to open |
| 52 | " the file bookmark.vim at line 16. |
| 53 | " |
| 54 | " NOTE: |
| 55 | " |
| 56 | " The functions need the grep programme (or grep.exe for windows 95). |
| 57 | " Gnu grep for windows 95 is available from |
| 58 | " |
| 59 | " http://www.coffeecomputing.com/free/index.html |
| 60 | " |
| 61 | " and other locations. |
| 62 | |
| 63 | " ----------------------------------------------------------------------------- |
| 64 | if !exists("_grep_vim_sourced") |
| 65 | let _grep_vim_sourced=1 |
| 66 | " ----------------------------------------------------------------------------- |
| 67 | so array.vim |
| 68 | |
| 69 | nm ,gr :cal Grep(expand("<cword>"))<cr> |
| 70 | vm ,gr "zy:cal Grep(@z)<cr> |
| 71 | nm ,gs :let b=bufnr("%")<cr>gr:exe b."sbuffer"\|res17<cr><c-w>w |
| 72 | vm ,gs "zy:let b=bufnr("%")\|cal Grep(@z)\|exe b."sbuffer"\|res17<cr><c-w>w |
| 73 | nm ,gl :exe "sp ".grep_fn<cr>:res 7<cr> |
| 74 | |
| 75 | aug grep |
| 76 | au! |
| 77 | au bufenter _grep_* nm e :exe "e!".GrepFile()."\|".GrepLine()<cr> |
| 78 | au bufleave _grep_* nun e |
| 79 | au bufenter _grep_* nn w :let gf=GrepFile()\|let gl=GrepLine()<cr><c-w>w:exe "e!".gf."\|".gl<cr>zz<c-w>w |
| 80 | au bufleave _grep_* nun w |
| 81 | au bufenter _grep_* nm <esc> :bd!<cr> |
| 82 | au bufleave _grep_* nun <esc> |
| 83 | if exists("g:_filebrws_vim_sourced") |
| 84 | au bufenter _grep_* cal FilebrowserSyn("") |
| 85 | endif |
| 86 | au bufenter _grep_* normal M |
| 87 | aug END |
| 88 | |
| 89 | let grep_dir=ArrGet(ArrGetItems(&backupdir,","),0) |
| 90 | |
| 91 | fu! Grep(str) |
| 92 | set ch=10 |
| 93 | if a:str=="" |
| 94 | let str=input("Search: ") |
| 95 | else |
| 96 | let str=a:str |
| 97 | endif |
| 98 | let fn=g:grep_dir."\\_grep_".str |
| 99 | let g:grep_fn=fn |
| 100 | cal delete(fn) |
| 101 | exe "e! ".fn |
| 102 | exe "r! grep -n ".str." *.*" |
| 103 | if exists("g:_filebrws_vim_sourced") |
| 104 | exe "%s/^/ " |
| 105 | endif |
| 106 | w! |
| 107 | set ch=1 |
| 108 | norm gg |
| 109 | endf |
| 110 | |
| 111 | fu! GrepLine() |
| 112 | return ArrGet(ArrGetItems(getline("."),":"),1) |
| 113 | endf |
| 114 | |
| 115 | fu! GrepFile() |
| 116 | return ArrGet(ArrGetItems(getline("."),":"),0) |
| 117 | endf |
| 118 | " ----------------------------------------------------------------------------- |
| 119 | endif |
| 120 | " ----------------------------------------------------------------------------- |
| 121 | |