Version 1.7

Added filename completion support for grep commands. In GUI Vim, added Tools->Search menu item for using the Grep commands.
This commit is contained in:
Yegappan Lakshmanan
2006-06-16 00:00:00 +00:00
committed by Able Scraper
parent af64febf42
commit 666c1a77ca

View File

@@ -1,19 +1,37 @@
" File: grep.vim " File: grep.vim
" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
" Version: 1.6 " Version: 1.7
" Last Modified: August 10, 2005 " Last Modified: June 16, 2006
" "
" Overview " Overview
" -------- " --------
" The grep.vim plugin script integrates the grep/fgrep/egrep/agrep tools with " The grep.vim plugin script integrates the grep, fgrep, egrep, and agrep
" Vim. To use this script, you need the grep, fgrep, egrep, agrep, find and " tools with Vim. To use this script, you need the grep, fgrep, egrep, agrep,
" xargs utilities. These tools come with most of the Unix installations. For " find and xargs utilities. These tools are present in most of the Unix
" MS-Windows systems, you can download these tools from the " installations. For MS-Windows systems, you can download these tools from the
" http://unxutils.sourceforge.net or http://gnuwin32.sourceforge.net site. " following sites:
"
" http://gnuwin32.sourceforge.net
" http://unxutils.sourceforge.net
"
" Installation
" ------------
" 1. Copy the grep.vim file to the $HOME/.vim/plugin or $HOME/vimfiles/plugin
" directory.
" Refer to the following Vim help topics for more information about Vim
" plugins:
" :help add-plugin
" :help add-global-plugin
" :help runtimepath
" 2. If the grep utility is not already in PATH, then set the Grep_Path
" and other _Path variables to point to the location of the grep utilites.
" 3. Restart Vim.
" 4. You can now use the ":Grep" and other commands to search for patterns in
" files.
" "
" Usage " Usage
" ----- " -----
" The grep.vim plugin script introduces the following commands: " The grep.vim plugin introduces the following Vim commands:
" "
" :Grep - Search for the specified pattern in the specified files " :Grep - Search for the specified pattern in the specified files
" :Rgrep - Run recursive grep " :Rgrep - Run recursive grep
@@ -46,41 +64,40 @@
" "
" You can specify grep options like -i (ignore case) or -w (search for a word) " You can specify grep options like -i (ignore case) or -w (search for a word)
" to the above commands. If the <grep_options> are not specified, then the " to the above commands. If the <grep_options> are not specified, then the
" default grep options specified by the variable Grep_Default_Options will be " default grep options specified by the variable Grep_Default_Options is
" used. " used.
" "
" You can specify the grep pattern to search as argument to the above " You can specify the grep pattern to search as an argument to the above
" commands. If the <search_pattern> is not specified, then you will be " commands. If the <search_pattern> is not specified, then you will be
" prompted to enter a search pattern. By default, the keyword under the cursor " prompted to enter a search pattern. By default, the keyword under the cursor
" will be displayed for the search pattern prompt. You can accept the default " is displayed for the search pattern prompt. You can accept the default or
" or modify it. " modify it.
" "
" The search pattern is automatically enclosed by the character specified in " The search pattern is automatically enclosed by the character specified in
" the Grep_Shell_Quote_Char variable. You should not enclose the search " the Grep_Shell_Quote_Char variable. You should not enclose the search
" pattern with a shell escape character. " pattern with a shell escape character.
" "
" If you want to specify a search pattern with space characters or a " If you want to specify a search pattern with space characters or a
" multi-word pattern, then you should use the Grep command input prompt to " multi-word pattern, then you should use the Grep command pattern input
" supply the pattern. " prompt to supply the pattern.
" "
" You can specify one or more file names (or file patterns) to the above " You can specify one or more file names (or file patterns) to the above
" commands. If the <file_names> are not specified, then you will be prompted " commands. If the <file_names> are not specified, then you will be prompted
" to enter file names. By default, the pattern specified by the " to enter file names. By default, the pattern specified by the
" Grep_Default_Filelist variable will be used. To specify the file name(s) as " Grep_Default_Filelist variable is used. To specify the file name(s) as an
" an argument to the above commands, you should specify the search pattern " argument to the above commands, you have to specify the search pattern also.
" also.
" "
" When you enter only the command name, you will be prompted to enter " When you enter only the command name, you will be prompted to enter the
" the search pattern and the files in which to search for the pattern. By " search pattern and the files in which to search for the pattern. By default,
" default, the keyword under the cursor will be displayed for the search " the keyword under the cursor is displayed for the search pattern prompt.
" pattern prompt. Depending on the command, you may prompted for additional " Depending on the command, you may prompted for additional parameters like
" parameters like the directories to search for the pattern. " the directories to search for the pattern.
" "
" You can retrieve previously entered values for the Grep prompts using the up " You can retrieve previously entered values for the Grep prompts using the up
" and down arrow keys. You can cancel the command by pressing the escape key. " and down arrow keys. You can cancel the command by pressing the escape key.
" You can use CTRL-U to erase the default shown for the prompt and CTRL-W to " You can use CTRL-U to erase the default shown for the prompt and CTRL-W to
" erase the previous word in the prompt. For more information about editing " erase the previous word in the prompt. For more information about editing
" the prompt, read :help cmdline-editing " the prompt, read ':help cmdline-editing' Vim help topic.
" "
" After invoking any of the grep commands, you can cancel the command, when " After invoking any of the grep commands, you can cancel the command, when
" you are prompted for a search pattern or file names or a directory by " you are prompted for a search pattern or file names or a directory by
@@ -88,8 +105,8 @@
" grep/fgrep/egrep/agrep commands after the external command is invoked. " grep/fgrep/egrep/agrep commands after the external command is invoked.
" "
" You can map a key to invoke any of the above commands. For example, the " You can map a key to invoke any of the above commands. For example, the
" following map will invoke the :Grep command to search for the keyword under " following map invokes the :Grep command to search for the keyword under the
" the cursor: " cursor:
" "
" nnoremap <silent> <F3> :Grep<CR> " nnoremap <silent> <F3> :Grep<CR>
" "
@@ -107,6 +124,9 @@
" "
" For more information about other quickfix commands read ":help quickfix" " For more information about other quickfix commands read ":help quickfix"
" "
" When using GUI Vim, the Tools->Search menu item with a few sub-menu items is
" created for few variations of the search command.
"
" Configuration " Configuration
" ------------- " -------------
" By changing the following variables you can configure the behavior of this " By changing the following variables you can configure the behavior of this
@@ -180,10 +200,11 @@
" "
" You can manually open the quickfix window using the :cwindow command. " You can manually open the quickfix window using the :cwindow command.
" "
" By default, for recursive searches, the find with the xargs utility is used. " By default, for recursive searches, the 'find' and 'xargs' utilities are
" If you don't have the xargs utility or don't want to use the xargs utility, " used. If you don't have the 'xargs' utility or don't want to use the
" then you can set the 'Grep_Find_Use_Xargs' variable to zero. If this is set " 'xargs' utility, " then you can set the 'Grep_Find_Use_Xargs' variable to
" to zero, then only the find utility will be used to do recursive searches: " zero. If this is set to zero, then only the 'find' utility is used for
" recursive searches:
" "
" :let Grep_Find_Use_Xargs = 0 " :let Grep_Find_Use_Xargs = 0
" "
@@ -372,7 +393,7 @@ endfunction
" Run specified grep command recursively " Run specified grep command recursively
function! s:RunGrepRecursive(cmd_name, grep_cmd, ...) function! s:RunGrepRecursive(cmd_name, grep_cmd, ...)
if a:0 > 0 && (a:1 == "-?" || a:1 == "-h") if a:0 > 0 && (a:1 == "-?" || a:1 == "-h")
echo a:cmd_name . " [<grep_options>] [<search_pattern> " . echo 'Usage: ' . a:cmd_name . " [<grep_options>] [<search_pattern> " .
\ "[<file_name(s)>]]" \ "[<file_name(s)>]]"
return return
endif endif
@@ -381,7 +402,7 @@ function! s:RunGrepRecursive(cmd_name, grep_cmd, ...)
let pattern = "" let pattern = ""
let filepattern = "" let filepattern = ""
let argcnt= 1 let argcnt = 1
while argcnt <= a:0 while argcnt <= a:0
if a:{argcnt} =~ '^-' if a:{argcnt} =~ '^-'
let grep_opt = grep_opt . " " . a:{argcnt} let grep_opt = grep_opt . " " . a:{argcnt}
@@ -397,6 +418,9 @@ function! s:RunGrepRecursive(cmd_name, grep_cmd, ...)
let grep_opt = g:Grep_Default_Options let grep_opt = g:Grep_Default_Options
endif endif
" Don't display messages about non-existent files
let grep_opt = grep_opt . " -s"
if a:grep_cmd == 'grep' if a:grep_cmd == 'grep'
let grep_path = g:Grep_Path let grep_path = g:Grep_Path
let grep_expr_option = '--' let grep_expr_option = '--'
@@ -513,7 +537,7 @@ endfunction
" argument list " argument list
function! s:RunGrepSpecial(cmd_name, which, ...) function! s:RunGrepSpecial(cmd_name, which, ...)
if a:0 > 0 && (a:1 == "-?" || a:1 == "-h") if a:0 > 0 && (a:1 == "-?" || a:1 == "-h")
echo a:cmd_name . " [<grep_options>] [<search_pattern>]" echo 'Usage: ' . a:cmd_name . " [<grep_options>] [<search_pattern>]"
return return
endif endif
@@ -542,7 +566,7 @@ function! s:RunGrepSpecial(cmd_name, which, ...)
if arg_cnt == 0 if arg_cnt == 0
echohl WarningMsg echohl WarningMsg
echomsg "Error: No filenames specified in the argument list " echomsg "Error: Argument list is empty"
echohl None echohl None
return return
endif endif
@@ -558,7 +582,7 @@ function! s:RunGrepSpecial(cmd_name, which, ...)
" No arguments " No arguments
if filenames == "" if filenames == ""
echohl WarningMsg echohl WarningMsg
echomsg "Error: No filenames specified in the argument list " echomsg "Error: Argument list is empty"
echohl None echohl None
return return
endif endif
@@ -581,6 +605,9 @@ function! s:RunGrepSpecial(cmd_name, which, ...)
let grep_opt = g:Grep_Default_Options let grep_opt = g:Grep_Default_Options
endif endif
" Don't display messages about non-existent files
let grep_opt = grep_opt . " -s"
" The last argument specified by the user is the pattern " The last argument specified by the user is the pattern
if argcnt == a:0 if argcnt == a:0
let pattern = a:{argcnt} let pattern = a:{argcnt}
@@ -609,7 +636,7 @@ endfunction
" Run the specified grep command " Run the specified grep command
function! s:RunGrep(cmd_name, grep_cmd, ...) function! s:RunGrep(cmd_name, grep_cmd, ...)
if a:0 > 0 && (a:1 == "-?" || a:1 == "-h") if a:0 > 0 && (a:1 == "-?" || a:1 == "-h")
echo a:cmd_name . " [<grep_options>] [<search_pattern> " . echo 'Usage: ' . a:cmd_name . " [<grep_options>] [<search_pattern> " .
\ "[<file_name(s)>]]" \ "[<file_name(s)>]]"
return return
endif endif
@@ -639,6 +666,9 @@ function! s:RunGrep(cmd_name, grep_cmd, ...)
let grep_opt = g:Grep_Default_Options let grep_opt = g:Grep_Default_Options
endif endif
" Don't display messages about non-existent files
let grep_opt = grep_opt . " -s"
if a:grep_cmd == 'grep' if a:grep_cmd == 'grep'
let grep_path = g:Grep_Path let grep_path = g:Grep_Path
let grep_expr_option = '--' let grep_expr_option = '--'
@@ -685,23 +715,41 @@ function! s:RunGrep(cmd_name, grep_cmd, ...)
endfunction endfunction
" Define the set of grep commands " Define the set of grep commands
command! -nargs=* Grep call s:RunGrep('Grep', 'grep', <f-args>) command! -nargs=* -complete=file Grep
command! -nargs=* Rgrep call s:RunGrepRecursive('Rgrep', 'grep', <f-args>) \ call s:RunGrep('Grep', 'grep', <f-args>)
command! -nargs=* GrepBuffer call s:RunGrepSpecial('GrepBuffer', 'buffer', command! -nargs=* -complete=file Rgrep
\ <f-args>) \ call s:RunGrepRecursive('Rgrep', 'grep', <f-args>)
command! -nargs=* Bgrep call s:RunGrepSpecial('Bgrep', 'buffer', <f-args>) command! -nargs=* GrepBuffer
command! -nargs=* GrepArgs call s:RunGrepSpecial('GrepArgs', 'args', \ call s:RunGrepSpecial('GrepBuffer', 'buffer', <f-args>)
\ <f-args>) command! -nargs=* Bgrep
\ call s:RunGrepSpecial('Bgrep', 'buffer', <f-args>)
command! -nargs=* GrepArgs
\ call s:RunGrepSpecial('GrepArgs', 'args', <f-args>)
command! -nargs=* Fgrep call s:RunGrep('Fgrep', 'fgrep', <f-args>) command! -nargs=* -complete=file Fgrep
command! -nargs=* Rfgrep call s:RunGrepRecursive('Rfgrep', 'fgrep', \ call s:RunGrep('Fgrep', 'fgrep', <f-args>)
\ <f-args>) command! -nargs=* -complete=file Rfgrep
command! -nargs=* Egrep call s:RunGrep('Egrep', 'egrep', <f-args>) \ call s:RunGrepRecursive('Rfgrep', 'fgrep', <f-args>)
command! -nargs=* Regrep call s:RunGrepRecursive('Regrep', 'egrep', command! -nargs=* -complete=file Egrep
\ <f-args>) \ call s:RunGrep('Egrep', 'egrep', <f-args>)
command! -nargs=* Agrep call s:RunGrep('Agrep', 'agrep', <f-args>) command! -nargs=* -complete=file Regrep
command! -nargs=* Ragrep call s:RunGrepRecursive('Ragrep', 'agrep', \ call s:RunGrepRecursive('Regrep', 'egrep', <f-args>)
\ <f-args>) command! -nargs=* -complete=file Agrep
\ call s:RunGrep('Agrep', 'agrep', <f-args>)
command! -nargs=* -complete=file Ragrep
\ call s:RunGrepRecursive('Ragrep', 'agrep', <f-args>)
" Add the Tools->Search Files menu
if has('gui_running')
anoremenu <silent> Tools.Search.Current\ Directory<Tab>:Grep
\ :Grep<CR>
anoremenu <silent> Tools.Search.Recursively<Tab>:Rgrep
\ :Rgrep<CR>
anoremenu <silent> Tools.Search.Buffer\ List<Tab>:Bgrep
\ :Bgrep<CR>
anoremenu <silent> Tools.Search.Argument\ List<Tab>:GrepArgs
\ :GrepArgs<CR>
endif
" restore 'cpo' " restore 'cpo'
let &cpo = s:cpo_save let &cpo = s:cpo_save