mirror of
https://github.com/vim-scripts/grep.vim.git
synced 2026-08-12 02:42:47 +08:00
Version 1.10
Fix the problem in running the grep command in MS-Windows because of command-line quoting issues.
This commit is contained in:
committed by
Able Scraper
parent
1e5658c6d9
commit
961dfaa5b9
+54
-12
@@ -1,7 +1,7 @@
|
|||||||
" File: grep.vim
|
" File: grep.vim
|
||||||
" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
|
" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
|
||||||
" Version: 1.9
|
" Version: 1.10
|
||||||
" Last Modified: September 10, 2007
|
" Last Modified: April 20, 2013
|
||||||
"
|
"
|
||||||
" Overview
|
" Overview
|
||||||
" --------
|
" --------
|
||||||
@@ -251,13 +251,13 @@
|
|||||||
"
|
"
|
||||||
" :let Grep_Find_Use_Xargs = 0
|
" :let Grep_Find_Use_Xargs = 0
|
||||||
"
|
"
|
||||||
" To handle file names with space characters in them, the xargs utility
|
" To handle file names with space characters in them, the xargs utility is
|
||||||
" is invoked with the '--null' argument. If the xargs utility in your system
|
" invoked with the '-0' argument. If the xargs utility in your system doesn't
|
||||||
" doesn't accept the '--null' argument, then you can change the
|
" accept the '-0' argument, then you can change the Grep_Xargs_Options
|
||||||
" Grep_Xargs_Options variable. For example, to use the '--print0' xargs
|
" variable. For example, to use the '--null' xargs argument, you can use the
|
||||||
" argument, you can use the following command:
|
" following command:
|
||||||
"
|
"
|
||||||
" :let Grep_Xargs_Options = '--print0'
|
" :let Grep_Xargs_Options = '--null'
|
||||||
"
|
"
|
||||||
" The Grep_Cygwin_Find variable should be set to 1, if you are using the find
|
" The Grep_Cygwin_Find variable should be set to 1, if you are using the find
|
||||||
" utility from the cygwin package. This setting is used to handle the
|
" utility from the cygwin package. This setting is used to handle the
|
||||||
@@ -352,7 +352,7 @@ endif
|
|||||||
|
|
||||||
" The command-line arguments to supply to the xargs utility
|
" The command-line arguments to supply to the xargs utility
|
||||||
if !exists('Grep_Xargs_Options')
|
if !exists('Grep_Xargs_Options')
|
||||||
let Grep_Xargs_Options = '--null'
|
let Grep_Xargs_Options = '-0'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" The find utility is from the cygwin package or some other find utility.
|
" The find utility is from the cygwin package or some other find utility.
|
||||||
@@ -404,7 +404,36 @@ endif
|
|||||||
" RunGrepCmd()
|
" RunGrepCmd()
|
||||||
" Run the specified grep command using the supplied pattern
|
" Run the specified grep command using the supplied pattern
|
||||||
function! s:RunGrepCmd(cmd, pattern, action)
|
function! s:RunGrepCmd(cmd, pattern, action)
|
||||||
let cmd_output = system(a:cmd)
|
if has('win32') && !has('win32unix') && !has('win95')
|
||||||
|
\ && (&shell =~ 'cmd.exe')
|
||||||
|
" Windows does not correctly deal with commands that have more than 1
|
||||||
|
" set of double quotes. It will strip them all resulting in:
|
||||||
|
" 'C:\Program' is not recognized as an internal or external command
|
||||||
|
" operable program or batch file. To work around this, place the
|
||||||
|
" command inside a batch file and call the batch file.
|
||||||
|
" Do this only on Win2K, WinXP and above.
|
||||||
|
let s:grep_tempfile = fnamemodify(tempname(), ':h') . '\mygrep.cmd'
|
||||||
|
if v:version >= 700
|
||||||
|
call writefile([a:cmd], s:grep_tempfile, "b")
|
||||||
|
else
|
||||||
|
exe 'redir! > ' . s:grep_tempfile
|
||||||
|
silent echo a:cmd
|
||||||
|
redir END
|
||||||
|
endif
|
||||||
|
|
||||||
|
let cmd_output = system('"' . s:grep_tempfile . '"')
|
||||||
|
else
|
||||||
|
let cmd_output = system(a:cmd)
|
||||||
|
endif
|
||||||
|
|
||||||
|
if exists('s:grep_tempfile')
|
||||||
|
" Delete the temporary cmd file created on MS-Windows
|
||||||
|
call delete(s:grep_tempfile)
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Do not check for the shell_error (return code from the command).
|
||||||
|
" Even if there are valid matches, grep returns error codes if there
|
||||||
|
" are problems with a few input files.
|
||||||
|
|
||||||
if cmd_output == ""
|
if cmd_output == ""
|
||||||
echohl WarningMsg |
|
echohl WarningMsg |
|
||||||
@@ -508,6 +537,7 @@ function! s:RunGrepRecursive(cmd_name, grep_cmd, action, ...)
|
|||||||
endif
|
endif
|
||||||
let pattern = g:Grep_Shell_Quote_Char . pattern .
|
let pattern = g:Grep_Shell_Quote_Char . pattern .
|
||||||
\ g:Grep_Shell_Quote_Char
|
\ g:Grep_Shell_Quote_Char
|
||||||
|
echo "\r"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let cwd = getcwd()
|
let cwd = getcwd()
|
||||||
@@ -522,6 +552,7 @@ function! s:RunGrepRecursive(cmd_name, grep_cmd, action, ...)
|
|||||||
if startdir == ""
|
if startdir == ""
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
echo "\r"
|
||||||
|
|
||||||
if filepattern == ""
|
if filepattern == ""
|
||||||
let filepattern = input("Search in files matching pattern: ",
|
let filepattern = input("Search in files matching pattern: ",
|
||||||
@@ -529,6 +560,7 @@ function! s:RunGrepRecursive(cmd_name, grep_cmd, action, ...)
|
|||||||
if filepattern == ""
|
if filepattern == ""
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
echo "\r"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let txt = filepattern . ' '
|
let txt = filepattern . ' '
|
||||||
@@ -576,7 +608,7 @@ function! s:RunGrepRecursive(cmd_name, grep_cmd, action, ...)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
if g:Grep_Find_Use_Xargs == 1
|
if g:Grep_Find_Use_Xargs == 1
|
||||||
let cmd = g:Grep_Find_Path . " " . startdir
|
let cmd = g:Grep_Find_Path . ' "' . startdir . '"'
|
||||||
let cmd = cmd . " " . find_prune . " -prune -o"
|
let cmd = cmd . " " . find_prune . " -prune -o"
|
||||||
let cmd = cmd . " " . find_skip_files
|
let cmd = cmd . " " . find_skip_files
|
||||||
let cmd = cmd . " " . find_file_pattern
|
let cmd = cmd . " " . find_file_pattern
|
||||||
@@ -618,7 +650,14 @@ function! s:RunGrepSpecial(cmd_name, which, action, ...)
|
|||||||
|
|
||||||
while i <= last_bufno
|
while i <= last_bufno
|
||||||
if bufexists(i) && buflisted(i)
|
if bufexists(i) && buflisted(i)
|
||||||
let filenames = filenames . " " . bufname(i)
|
let fullpath = fnamemodify(bufname(i), ':p')
|
||||||
|
if filereadable(fullpath)
|
||||||
|
if v:version >= 702
|
||||||
|
let filenames = filenames . " " . fnameescape(fullpath)
|
||||||
|
else
|
||||||
|
let filenames = filenames . " " . fullpath
|
||||||
|
endif
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
let i = i + 1
|
let i = i + 1
|
||||||
endwhile
|
endwhile
|
||||||
@@ -684,6 +723,7 @@ function! s:RunGrepSpecial(cmd_name, which, action, ...)
|
|||||||
if pattern == ""
|
if pattern == ""
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
echo "\r"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let pattern = g:Grep_Shell_Quote_Char . pattern . g:Grep_Shell_Quote_Char
|
let pattern = g:Grep_Shell_Quote_Char . pattern . g:Grep_Shell_Quote_Char
|
||||||
@@ -761,6 +801,7 @@ function! s:RunGrep(cmd_name, grep_cmd, action, ...)
|
|||||||
endif
|
endif
|
||||||
let pattern = g:Grep_Shell_Quote_Char . pattern .
|
let pattern = g:Grep_Shell_Quote_Char . pattern .
|
||||||
\ g:Grep_Shell_Quote_Char
|
\ g:Grep_Shell_Quote_Char
|
||||||
|
echo "\r"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if filenames == ""
|
if filenames == ""
|
||||||
@@ -773,6 +814,7 @@ function! s:RunGrep(cmd_name, grep_cmd, action, ...)
|
|||||||
if filenames == ""
|
if filenames == ""
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
echo "\r"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Add /dev/null to the list of filenames, so that grep print the
|
" Add /dev/null to the list of filenames, so that grep print the
|
||||||
|
|||||||
Reference in New Issue
Block a user