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: Thorsten Jolitz
Subject: Re: Suppress user-prompting when calling commands in programs
Date: Sat, 14 Jun 2014 10:22:36 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Thorsten Jolitz <address@hidden> writes:

> Stefan Monnier <address@hidden> writes:
>
>>> I tried convincing upstream before and never made it! And I actually
>>> understand the authors of code like that and even copied that technique
>>> sometimes, because it might be harder to write the interactive spec for
>>> both interactive and programmatical use than to write the function
>>> itself, and then there is no need for a wrapper command or interactive
>>> (lambda ...) expression when it comes to define a key for that command.
>>
>> I don't know what complications you're referring to.
>>
>> In 99% of the cases you can just turn
>>
>>     (defun foo (&optional arg)
>>       (interactive "P")
>>       (let ((bar (org-icompleting-read ...)))))
>>
>> into
>>
>>     (defun foo (bar &optional arg)
>>       (interactive
>>        (list (org-icompleting-read ...) current-prefix-arg))
>>
>> I'm not saying it's always perfect, but there's clearly some difference
>> between my experience and yours, so if you give some examples, it would help.

[Ups, that one was send to early accidentally ...]

Maybe I just find the Emacs Lisp Manual too dense and lacking examples
and templates for using lists in the interactive spec?

This is more or less or there is:

 ,-------------------------------------------------------------------
 | * It may be a Lisp expression that is not a string; then it should
 |   be a form that is evaluated to get a list of arguments to pass
 |   to the command. Usually this form will call various functions to
 |   read input from the user, most often through the minibuffer (see
 |   Minibuffers) or directly from the keyboard (see Reading Input).
 |  
 |   Providing point or the mark as an argument value is also common,
 |   but if you do this and read input (whether using the minibuffer
 |   or not), be sure to get the integer values of point or the mark
 |   after reading. The current buffer may be receiving subprocess
 |   output; if subprocess output arrives while the command is
 |   waiting for input, it could relocate point and the mark.
 |  
 |   Here's an example of what not to do:
 |  
 |             (interactive
 |              (list (region-beginning) (region-end)
 |                    (read-string "Foo: " nil 'my-history)))
 |   
 |   Here's how to avoid the problem, by examining point and the mark
 |   after reading the keyboard input:
 |  
 |             (interactive
 |              (let ((string (read-string "Foo: " nil 'my-history)))
 |                (list (region-beginning) (region-end) string)))
 `-------------------------------------------------------------------

But what about a case like this, a command with several optional args,
that are not always necessary. How to give the user the option to decide
for which args he wants to give input (and use the defaults for the
others)? Where to put the (prefix) 'arg argument in this case? How does
Emacs know which of the optional arguments should be the
prefix-argument? Do I need a "P" or "p" then somewhere in the spec when
I add 'arg' to the arguments list? 

I came up with something like this, but most likely inspired by other
elisp libraries, not by the manual. Is it a good solution? Hard to tell,
since there are no templates or best practice examples given for more
complex interactive specs in the manual. 

 #+begin_src emacs-lisp 
 ;; Start a REPL
 (defun iorg-scrape-repl (&optional port host how local)
   "Run inferior Picolisp and setup process for GUI-scripting."
   (interactive
    (cond
     ((equal current-prefix-arg nil) nil)
     ((equal current-prefix-arg '(4))
      (list
       (read-number "Port: ")))
     ((equal current-prefix-arg '(16))
      (list
       (read-number "Port: ")
       (read-string "Host: ")))
     ((equal current-prefix-arg '(32))
      (list
       (read-number "Port: ")
       (read-string "Host: ")
       (read-string "How: ")))
     (t
      (list
       (read-number "Port: ")
       (read-string "Host: ")
       (read-string "How: ")
       (read-string "Local: ")))))
   (let* ((hst (or host "localhost"))
          (prt (or port 5000)) [...]
 #+end_src

-- 
cheers,
Thorsten




reply via email to

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