* Irritation in C-u M-x grep, caused by overprotectiveness
@ 2007-07-29 11:09 Alan Mackenzie
2007-07-29 13:49 ` Stefan Monnier
[not found] ` <E1IFYM6-000453-N8@fencepost.gnu.org>
0 siblings, 2 replies; 7+ messages in thread
From: Alan Mackenzie @ 2007-07-29 11:09 UTC (permalink / raw)
To: emacs-devel
Hi, Emacs!
On the command line, do this:
$ CC_FILESEL=cc-{align,awk,bytecomp,cmds,compat,defs,engine,fonts,langs,menus,mode,styles,subword,vars}.el
$ export CC_FILESEL
, and start (or return) to an emacs session. Load a file from
.../lisp/progmodes, for example:
C-x C-f ...../lisp/progmodes/cc-mode.el
. This establishes the current directory. With this as the current
buffer, do:
M-x grep
, which prompts with "Run grep (like this): grep -nH -e " in the
mini-buffer. Fill in the minibuffer like this:
Run grep (like this): grep -nH -e kwd-clause-end $CC_FILESEL
, and start the grep with <CR>. This will bring up, inter alia,
cc-engine.el. Put point on a symbol in this file and do this:
C-u M-x grep <CR>
. Emacs 21 returns this prompt in the minibuffer:
grep -n c-keyword-member $CC_FILESEL
^^^^^^^^^^^
, which is better than what Emacs 22 currently returns:
grep -nH c-keyword-member *.el
^^^^
If I give an environment variable as "filename", the chances are I'll
want to use it in subsequent greps. I think the current behaviour is a
bug, a regression, albeit a small one. However, it's been irritating
me, water torture style (drip, drip, drip, drip, drip, ......), ever
since the release of Emacs 22 to the point where I just can't take it
any more.
#########################################################################
This manipulation is done in `grep-default-command' in
.../progmodes/grep.el. In the following patch, I simply add a check for
the first character of "the filename" being ?$. This doesn't seem very
satisfactory because it might not be portable enough. There doesn't
seem to be a function `env-variable-name-p' which would be useful here.
I've also answered RMS's RFC from V1.65 from a year and 9 hours ago. In
the process I've renamed `sh-arg-re' to `pattern-re' to make the code
more understandable.
2007-07-29 Alan Mackenzie <acm@muc.de>
* progmodes/grep.el (grep-default-command): Add comments. Rename
`sh-arg-re' to `pattern-re'. Don't splat the filename from the
history if it is an environment variable.
Index: grep.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/progmodes/grep.el,v
retrieving revision 1.76
diff -c -r1.76 grep.el
*** grep.el 26 Jul 2007 05:27:27 -0000 1.76
--- grep.el 29 Jul 2007 10:44:14 -0000
***************
*** 537,550 ****
(defun grep-default-command ()
"Compute the default grep command for C-u M-x grep to offer."
(let ((tag-default (shell-quote-argument (grep-tag-default)))
! ;; This a regexp to match single shell arguments.
! ;; Could someone please add comments explaining it?
! (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)")
(grep-default (or (car grep-history) grep-command)))
;; In the default command, find the arg that specifies the pattern.
(when (or (string-match
! (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*"
! sh-arg-re "\\(\\s +\\(\\S +\\)\\)?")
grep-default)
;; If the string is not yet complete.
(string-match "\\(\\)\\'" grep-default))
--- 537,551 ----
(defun grep-default-command ()
"Compute the default grep command for C-u M-x grep to offer."
(let ((tag-default (shell-quote-argument (grep-tag-default)))
! ;; pattern-re is a regexp to match a grep pattern; that's a sequence
! ;; of single or double quoted strings and non-WS characters. It
! ;; doesn't try to handle backslashes properly; maybe it should.
! (pattern-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)")
(grep-default (or (car grep-history) grep-command)))
;; In the default command, find the arg that specifies the pattern.
(when (or (string-match
! (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*" ; (POSIX) command with options
! pattern-re "\\(\\s +\\(\\S +\\)\\)?") ; pattern + optional filename
grep-default)
;; If the string is not yet complete.
(string-match "\\(\\)\\'" grep-default))
***************
*** 552,564 ****
;; But first, maybe replace the file name pattern.
(condition-case nil
(unless (or (not (stringp buffer-file-name))
! (when (match-beginning 2)
! (save-match-data
! (string-match
! (wildcard-to-regexp
! (file-name-nondirectory
! (match-string 3 grep-default)))
! (file-name-nondirectory buffer-file-name)))))
(setq grep-default (concat (substring grep-default
0 (match-beginning 2))
" *."
--- 553,566 ----
;; But first, maybe replace the file name pattern.
(condition-case nil
(unless (or (not (stringp buffer-file-name))
! (when (match-beginning 3) ; "filename"(s) in grep-default?
! (or (save-match-data
! (string-match
! (wildcard-to-regexp
! (file-name-nondirectory
! (match-string 3 grep-default)))
! (file-name-nondirectory buffer-file-name)))
! (eq (aref grep-default (match-beginning 3)) ?$))))
(setq grep-default (concat (substring grep-default
0 (match-beginning 2))
" *."
--
Alan Mackenzie (Ittersbach, Germany).
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Irritation in C-u M-x grep, caused by overprotectiveness
2007-07-29 11:09 Irritation in C-u M-x grep, caused by overprotectiveness Alan Mackenzie
@ 2007-07-29 13:49 ` Stefan Monnier
2007-07-29 15:30 ` Alan Mackenzie
[not found] ` <E1IFYM6-000453-N8@fencepost.gnu.org>
1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2007-07-29 13:49 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: emacs-devel
> If I give an environment variable as "filename", the chances are I'll
> want to use it in subsequent greps.
I don't see why that'd generally be the case, but I guess it's just as good
a default as the current one.
Maybe a better and more general approach to the problem goes as follows:
Currently the code checks whether the previous "list of files" (typically
a global pattern) matches the current buffer's file name. If it does then
the previous list of files is reused, otherwise the previous list of files
is ignored and replaced by a new glob pattern.
Now in your case, the list of files which *you* wrote did not "match" the
current buffer's file name, so clearly, the above heuristic shouldn't be
applied anyway. I.e. we should only check "does it match the current file?"
if it did match the current file in its previous use.
Stefan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Irritation in C-u M-x grep, caused by overprotectiveness
2007-07-29 13:49 ` Stefan Monnier
@ 2007-07-29 15:30 ` Alan Mackenzie
2007-07-29 17:33 ` Stefan Monnier
0 siblings, 1 reply; 7+ messages in thread
From: Alan Mackenzie @ 2007-07-29 15:30 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
Hi, Stefan!
On Sun, Jul 29, 2007 at 09:49:33AM -0400, Stefan Monnier wrote:
> > If I give an environment variable as "filename", the chances are I'll
> > want to use it in subsequent greps.
> I don't see why that'd generally be the case, but I guess it's just as
> good a default as the current one.
Well, there's nothing certain in Emacs users other than taxes and death.
I think it's a reasonable assumption, because setting up such a variable
is a lot of work; much more than just typing a list of filenames into the
minibuffer - anybody who does this will be expecting to use the variable
more than once.
> Maybe a better and more general approach to the problem goes as follows:
> Currently the code checks whether the previous "list of files"
> (typically a global pattern) matches the current buffer's file name.
No, it doesn't quite do this. If several "filenames" have been given on
the top command line of grep's history, it only uses the first one,
(match-string 3 grep-default). Maybe it should be using (substring
grep-default (match-begin 3)) instead. But this is a separate issue.
> If it does then the previous list of files is reused, otherwise the
> previous list of files is ignored and replaced by a new glob pattern.
> Now in your case, the list of files which *you* wrote did not "match"
> the current buffer's file name, so clearly, the above heuristic
> shouldn't be applied anyway. I.e. we should only check "does it match
> the current file?" if it did match the current file in its previous
> use.
Maybe $VARIABLEs should be evaluated first. What do you think?
Something like (getenv (substring grep-default (1+ (match-begin 3)))) fed
into `regexp-opt', each element having been through `wildcard-to-regexp'.
That might be heavy overkill, though.
> Stefan
--
Alan.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Irritation in C-u M-x grep, caused by overprotectiveness
2007-07-29 15:30 ` Alan Mackenzie
@ 2007-07-29 17:33 ` Stefan Monnier
0 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2007-07-29 17:33 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: emacs-devel
>> Currently the code checks whether the previous "list of files"
>> (typically a global pattern) matches the current buffer's file name.
> No, it doesn't quite do this. If several "filenames" have been given on
> the top command line of grep's history, it only uses the first one,
> (match-string 3 grep-default). Maybe it should be using (substring
> grep-default (match-begin 3)) instead. But this is a separate issue.
Yes, the code could be improved.
>> If it does then the previous list of files is reused, otherwise the
>> previous list of files is ignored and replaced by a new glob pattern.
>> Now in your case, the list of files which *you* wrote did not "match"
>> the current buffer's file name, so clearly, the above heuristic
>> shouldn't be applied anyway. I.e. we should only check "does it match
>> the current file?" if it did match the current file in its previous
>> use.
> Maybe $VARIABLEs should be evaluated first. What do you think?
> Something like (getenv (substring grep-default (1+ (match-begin 3)))) fed
> into `regexp-opt', each element having been through `wildcard-to-regexp'.
> That might be heavy overkill, though.
Again, I agree that the implementation of the heuristic can be improved, but
in any case the implementation won't be perfect and the heuristic isn't
always correct, so it seems that if the heuristic doesn't apply to the
original case (either because of imperfect implementation or for some other
reason), then there's no reason to think it'll apply to the next case.
Stefan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Irritation in C-u M-x grep, caused by overprotectiveness
[not found] ` <E1IFYM6-000453-N8@fencepost.gnu.org>
@ 2007-07-31 21:07 ` Alan Mackenzie
2007-08-01 14:07 ` Stefan Monnier
2007-08-01 14:30 ` Richard Stallman
0 siblings, 2 replies; 7+ messages in thread
From: Alan Mackenzie @ 2007-07-31 21:07 UTC (permalink / raw)
To: Richard Stallman; +Cc: emacs-devel
Hi, Richard!
On Mon, Jul 30, 2007 at 12:44:26PM -0400, Richard Stallman wrote:
> I've also answered RMS's RFC from V1.65 from a year and 9 hours ago. In
> the process I've renamed `sh-arg-re' to `pattern-re' to make the code
> more understandable.
>
> Thank you.
Well, thanks! Should I install the patch or not, and if so to just the
trunk or Emacs 22 as well?
I think I should definitely install the comment enhancements. But what
about the prime change, i.e. causing grep to remember a filename
argument like "$CC_FILESEL" when started with C-u M-x grep? So far,
only Stefan has commented - he didn't seem to think the change was good
or bad.
--
Alan.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Irritation in C-u M-x grep, caused by overprotectiveness
2007-07-31 21:07 ` Alan Mackenzie
@ 2007-08-01 14:07 ` Stefan Monnier
2007-08-01 14:30 ` Richard Stallman
1 sibling, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2007-08-01 14:07 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: Richard Stallman, emacs-devel
>> I've also answered RMS's RFC from V1.65 from a year and 9 hours ago. In
>> the process I've renamed `sh-arg-re' to `pattern-re' to make the code
>> more understandable.
>>
>> Thank you.
> Well, thanks! Should I install the patch or not, and if so to just the
> trunk or Emacs 22 as well?
> I think I should definitely install the comment enhancements. But what
> about the prime change, i.e. causing grep to remember a filename
> argument like "$CC_FILESEL" when started with C-u M-x grep? So far,
> only Stefan has commented - he didn't seem to think the change was good
> or bad.
Yes, I think the behavior implemented by your patch is correct. I also
think it would be even better to implement the behavior I suggested, which
would cover your case as well as several others.
Stefan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Irritation in C-u M-x grep, caused by overprotectiveness
2007-07-31 21:07 ` Alan Mackenzie
2007-08-01 14:07 ` Stefan Monnier
@ 2007-08-01 14:30 ` Richard Stallman
1 sibling, 0 replies; 7+ messages in thread
From: Richard Stallman @ 2007-08-01 14:30 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: emacs-devel
I think I should definitely install the comment enhancements. But what
about the prime change, i.e. causing grep to remember a filename
argument like "$CC_FILESEL" when started with C-u M-x grep? So far,
only Stefan has commented - he didn't seem to think the change was good
or bad.
That change should not go in Emacs 22. Please see if others respond
positively before installing it in the trunk.
Please do install the comment enhancements in the trunk.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2007-08-01 14:30 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-29 11:09 Irritation in C-u M-x grep, caused by overprotectiveness Alan Mackenzie
2007-07-29 13:49 ` Stefan Monnier
2007-07-29 15:30 ` Alan Mackenzie
2007-07-29 17:33 ` Stefan Monnier
[not found] ` <E1IFYM6-000453-N8@fencepost.gnu.org>
2007-07-31 21:07 ` Alan Mackenzie
2007-08-01 14:07 ` Stefan Monnier
2007-08-01 14:30 ` Richard Stallman
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).