Saturday, July 24, 2010

improving on the standard :grep behavior...

I'm trying to tune the behavior of :grep.

The things I'm trying to fix, and the solutions I have, if any:

1. Don't like the big list vim spits up (solution: use silent command)

2. Don't want to auto-jump to first match (no solution yet)

3. Want context (--context option to grep) (solution: add --context
(to 'grepprg' and %f-%l- to grepformat so :copen buffer lines are
justified consistently)

4. Unfortunately, doing 3 solution causes context lines to be considered
matches by vim (no easy solution)

5. Want :copen to happen automatically after grep (solution: use custom
command and function).

6. Want :match called automatically in Quickfix buffer for searched pattern
(solution: use custom command and function).

7. Actually, solution to 6 isn't quite what I want, I'd like it to
behave as if I
typed '/<pat>' in quickfix so n, N, etc. work (no solution yet).

8. From command line, I often refilter grep output (e.g.
grep --with-filename foo | grep --invert-match 'foo\.autogenerated')
I would like to be able to do this from within vim. For this
the secondary
filter is usually simple so I don't care if its vim regex
rather than POSIX/perl
I'm used to that external grep provides). (no solution yet).

The code that implements the above partial solutions is shown below.

It looks like some of the remaining problems 3/4 and 8 could be addressed
using filter (!) or just rewriting the buffer, but I guess changing the Quickfix
buffer contents might break other things?

Any ideas greatly appreciated.

And the code:

set grepprg+=\ --exclude=tags\ --binary-files=without-match\ --extended-regexp
\\ --context=1\ "
set grepformat+=,%f-%l-%m,%f-%l-

nnoremap ;g :MyGrep
command! -nargs=+ -complete=tag_listfiles MyGrep :call MyGrepFunction(<q-args>)
function! MyGrepFunction (grepArgs)
if has('perl')
perl MyGrepFunction_perl
if exists('retVal')
throw "unexpected return value"
return retVal
endif
else
echo("Sorry, perl support is not compiled into this copy of vim.")
endif
endfunction
if has('perl')
perl <<EOF
use strict;
use warnings;
use IO::Handle;
# Takes: a string consisting of a grep pattern and file(s) arguments, and/or -r
# Affects: splits open a new grep results buffer, possible display error message
# Returns: nothing
sub MyGrepFunction_perl
{
my $ga = VIM::Eval("a:grepArgs"); # Grep args.

# For debugging.
open(MY_GREP_FUNCTION_LOG, ">/tmp/my_grep_function_log") or die;
MY_GREP_FUNCTION_LOG->autoflush(1);
print MY_GREP_FUNCTION_LOG "log opened\n";
print MY_GREP_FUNCTION_LOG "argument string: '".$ga."'\n";

my @sa = split(' ', $ga); # Split arguments

my $pat = shift(@sa); # Pattern
my $oa = join(' ', @sa); # Other arguments

VIM::DoCommand("execute 'silent grep \"$pat\" $oa' | copen 33");
VIM::DoCommand("match Search /$pat/");
}
EOF
endif

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment