unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: help-gnu-emacs@gnu.org
Subject: RE: Suppress user-prompting when calling commands in programs
Date: Fri, 13 Jun 2014 08:29:58 -0700 (PDT)	[thread overview]
Message-ID: <7d140fb3-6b45-4e37-8109-0f794d907b09@default> (raw)
In-Reply-To: <jwvvbs46gaz.fsf-monnier+gmane.emacs.help@gnu.org>

> >> (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).



  parent reply	other threads:[~2014-06-13 15:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-13 13:51 Suppress user-prompting when calling commands in programs Thorsten Jolitz
2014-06-13 14:03 ` Eli Zaretskii
2014-06-13 14:25   ` Stefan Monnier
2014-06-13 15:09     ` Thorsten Jolitz
2014-06-13 15:29     ` Drew Adams [this message]
2014-06-13 15:49       ` Thorsten Jolitz
2014-06-13 14:20 ` Nicolas Richard
2014-06-13 15:18   ` Thorsten Jolitz
2014-06-13 16:04     ` Drew Adams
2014-06-13 18:14       ` Thorsten Jolitz
2014-06-14  3:33       ` Eric Abrahamsen
2014-06-14  3:36         ` Drew Adams
2014-06-14  3:47           ` Eric Abrahamsen
     [not found]   ` <mailman.3579.1402672740.1147.help-gnu-emacs@gnu.org>
2014-06-13 18:55     ` Stefan Monnier
2014-06-14  8:07       ` Thorsten Jolitz
2014-06-14  8:22         ` Thorsten Jolitz
2014-06-15  1:58           ` Stefan Monnier
2014-06-14 15:52         ` Drew Adams
2014-06-14 16:27           ` Thorsten Jolitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7d140fb3-6b45-4e37-8109-0f794d907b09@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).