bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ find-grep-dired ] Regexp input and other remarks


From: Kevin Rodgers
Subject: Re: [ find-grep-dired ] Regexp input and other remarks
Date: Tue, 05 Mar 2002 12:53:48 -0700

Fabrice Bauzac wrote:
> I used the find-grep-dired command to search for files matching a
> regular expression.  My regular expression was simple: it was the
> string `2.4.12'.  Since find-grep asked for a regexp:
> 
> Find-grep (grep regexp):
> 
> I entered:
> 
> 2\.4\.12
> 
> so that the dots aren't interpreted by grep as the all-matching
> character.  But the commandline sent to the shell seems to not quote
> special characters, because here is the result of find-grep:
> 
>   ~/linux-2.4.12/:
>   find . \( -type f -exec grep -q 2\.4\.12 {} \;  \) -exec ls -ld {} \;
>   -rw-r--r--    1 noon     noon       359166 mar  7  2001 
> drivers/net/dgrs_firmware.c
>   [...]
> 
> The files don't contain the _string_ `2.4.12' (at least, not the first
> file).  But the first file contains strings such as `254,128' which
> match the regexp /2.4.12/.
> 
> Moreover, if the user inserts an unquoted space, `grep' recieves
> several arguments, and that isn't a proper behaviour.  If the user
> inserts the string {}, or if there is a leading dash (which will be
> interpreted as the start of a switch by grep), too.  In fact there are
> quite many situations where one can't just put a normal regular
> expression at the find-grep prompt.

I think `M-x grep' has the same problem.
 
> One workaround would be to quote the regexp with single quotes '' at
> the `Find-grep (grep regexp):' prompt.
> 
> But I think it would be more user-friendly if it was GNU Emacs that quoted
> the user's string:
> 
> * Replace every _'_ with _'\''_
> 
> * Put an initial and a final quote _'_

The easiest way to fix it would be to fix that would be to use
shell-quote-argument, since the command is run via
start-process-shell-command.  I'll include a patch below...

> * Use -q -e as arguments to `grep' instead of just -q, in order to
>   prevent the "leading dash" interpretation.

`M-x grep' does that.

> But if GNU Emacs does that, there are possible problems, including:
> 
> * No compatibility with previous GNU Emacs versions (but it is
>   possible to use another name for the modified command, thus leaving
>   find-grep with its current behaviour).
> 
> * Maybe shell dependance (I don't know the UNIX shells that are not
>   Bourne: maybe the character _'_ does not have the same
>   meaning/behaviour).  Maybe Emacs could explicitely run /bin/sh.

I think csh/tcsh is the only shell with non-Bourne quoting syntax, but
shell-quote-argument should be simplistic enough to work for it/them.

> * Maybe dependance for the version of grep concerning the -e switch
>   (i.e. I don't know whether all the grep programs understand the
>   meaning of that switch).

grep-compute-defaults (in progmodes/compile.el) figures that out dynamically,
so the same technique could be used in find-dired.el.

Here's the shell-quote-argument patch:

2002-03-05  Kevin Rodgers <kevinr@ihs.com>

        * find-dired.el (find-name-dired, find-grep-dired): Quote file name
        PATTERN and grep regexp ARGS respectively with shell-quote-argument.

*** emacs-20.7/lisp/find-dired.el.orig  Sat Jun 27 15:57:12 1998
--- emacs-20.7/lisp/find-dired.el       Tue Mar  5 12:33:22 2002
***************
*** 137,143 ****
      find . -name 'PATTERN' -ls"
    (interactive
     "DFind-name (directory): \nsFind-name (filename wildcard): ")
!   (find-dired dir (concat "-name '" pattern "'")))
  
  ;; This functionality suggested by
  ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
--- 137,143 ----
      find . -name 'PATTERN' -ls"
    (interactive
     "DFind-name (directory): \nsFind-name (filename wildcard): ")
!   (find-dired dir (concat "-name " (shell-quote-argument pattern))))
  
  ;; This functionality suggested by
  ;; From: oblanc@watcgl.waterloo.edu (Olivier Blanc)
***************
*** 162,168 ****
    ;; about symlinks, so as far as I know this is not wrong.
    (find-dired dir
              (concat "-type f -exec grep " find-grep-options " "
!                     args " {} \\\; ")))
  
  (defun find-dired-filter (proc string)
    ;; Filter for \\[find-dired] processes.
--- 162,168 ----
    ;; about symlinks, so as far as I know this is not wrong.
    (find-dired dir
              (concat "-type f -exec grep " find-grep-options " "
!                     (shell-quote-argument args) " {} \\\; ")))
  
  (defun find-dired-filter (proc string)
    ;; Filter for \\[find-dired] processes.

-- 
Kevin Rodgers <kevinr@ihs.com>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]