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

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

Re: strangeness with let


From: jpkotta
Subject: Re: strangeness with let
Date: Wed, 28 Mar 2012 19:20:18 -0000
User-agent: G2/1.0

On Jul 26, 5:13 pm, "Pascal J. Bourguignon" <p...@informatimago.com>
wrote:
> jpkotta <jpko...@gmail.com> writes:
> > I want to have an easy way to do rgrep with context lines.  C-u M-x
> > rgrep to manually edit the command doesn't count as easy.  So I tried
> > this:
>
> > (defun rgrep-context (arg)
> >   (interactive "p")
> >   (setq arg (or arg grep-context-lines))
> >   (let ((c (number-to-string arg))
> >         grep-find-template
> >         current-prefix-arg)
> >     (grep-apply-setting 'grep-find-template
> >                         (concat "find . <X> -type f <F> -print0 |
> > xargs -0 -e grep <C> -nH -C " c " -e <R>"))
> >     (call-interactively 'rgrep)))
>
> > It works, but then grep-find-template still has the "-C <number>" in
> > it and I get context lines when I run plain rgrep.
>
> grep-apply-setting calls (set-default symbol value), this doesn't set
> the value of the symbol, but the default value.   Notably, when the
> symbol is a buffer local variable, it has no effect on it.
>
> >  Then I try this in
> > a scratch buffer:
>
> > (progn
> >   (let (grep-find-template)
> >     (grep-apply-setting 'grep-find-template "true")
> >     (call-interactively 'rgrep))
> >   (sleep-for 1)
> >   (message "grep-find-template: '%s'" grep-find-template))
>
> > and it doesn't print "true", IOW the let is doing what it should here,
> > but not in rgrep-context.  What am I doing wrong?  Is there a simpler
> > way to achieve my ultimate goal?
>
> Yes.  Just bind the variable with let.
>
> (let ((grep-find-template "true"))
>   (call-interactively 'rgrep)
>   (sleep-for 1)
>   (message "grep-find-template: '%s'" grep-find-template))
>
> -*- mode: grep; default-directory: "/tmp/emacs1000/" -*-
> Grep started at Wed Jul 27 00:12:52
>
> true
>
> Grep finished (matches found) at Wed Jul 27 00:12:52
>
> --> "grep-find-template: 'true'"
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
> A bad day in () is better than a good day in {}.

I think the problem was that rgrep runs grep-compute-defaults, which
updates grep-host-defaults-alist, which is in turn used to set things
like grep-find-template.  Seems overly complicated to me.  Anyway,
this seems to do what I want:

(defvar grep-context-lines 2
  "Default number of context lines (non-matching lines before and
  after the matching line) for `rgrep-context'.")

(defun rgrep-context (arg)
  (interactive "p")
  (setq arg (or arg grep-context-lines))
  (let ((grep-find-template
         (concat "find . <X> -type f <F> -print0 | xargs -0 -e grep
<C> -nH -C "
                 (number-to-string arg) " -e <R>"))
        grep-host-defaults-alist
        current-prefix-arg)
    (call-interactively 'rgrep)))


reply via email to

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