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

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

RE: Suppress user-prompting when calling commands in programs


From: Drew Adams
Subject: RE: Suppress user-prompting when calling commands in programs
Date: Fri, 13 Jun 2014 08:29:58 -0700 (PDT)

> >> (defun foo (&optional arg)
> >>   (interactive "P")
> >>   (let ((bar (org-icompleting-read ...)))))
> > Yuck!
> 
> Indeed, the prompting should normally take place in the `interactive'
> spec, but the above is sadly pretty common.

File a code bug / enhancement request for `foo'...

> >> Assuming `foo' can't be changed - is there another way to bind `bar'
> >> before calling `foo' in a program rather than advising `foo' (with the
> >> aim to suppress any user-prompting at all during the execution of
> >> `foo')?
> 
> Not really, no.

Sure there is.  If `bar' is a dynamic variable, at least.

> And advising `foo' only won't help: you also need to
> advise org-icompleting-read.
>
> > cl-flet org-icompleting-read to 'ignore?
> 
> Nope.  That worked with `flet', but `cl-flet' is actually providing
> Common-Lisp's `flet' which defines a lexically-scoped function.
> Better use an advice here.

It's pretty simple if `bar' is dynamic, not lexical - e.g., top-level
(defvar bar 51 "Step up to the bar")

;; Unchangeable `foo' - just instantiating your "...", for a test:
(defun foo (&optional arg)
  (interactive "P")
  (let ((bar  (org-icompleting-read "Prompt: " '("a" "b" "c"))))
    (message "`foo' arg is %S.  BAR is %S" arg bar)))

Write a function that binds `bar' to whatever you want and
provides whatever arg you want to `foo'.  Temporarily redefine
`org-icompleting-read' to just return your value of `bar'.
Then restore `org-icompleting-read'.

(defun toto (my-bar my-foo-arg)
  (let ((bar    my-bar)
        (o-i-r  (symbol-function 'org-icompleting-read)))
    (unwind-protect
         (progn (fset 'org-icompleting-read
                      (lambda (&rest _) (symbol-value 'bar)))
                (foo my-foo-arg))
      (fset 'org-icompleting-read o-i-r))))
    
(toto 42 36)
==> `foo' arg is 36.  BAR is 42

The original definition of `org-icompleting-read' is ignored (no
prompting etc.), and `foo's binding of `bar' is overruled by
`toto's binding of `bar'.

None of this is pretty.  But it's not complicated either.  And yes,
the `foo' code should be rewritten in one of the ways already
suggested (file a bug).



reply via email to

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