0

I have an interactive function taking 2 arguments. I can read them with

(defun example-fct (arg1 arg2)
  "This function take two arguments"
  (interactive
   (let ((arg1 (read-string "Arg1: "))
         (arg2 (read-string "Arg2: ")))
     (list arg1 arg2)))
  (message (format "input arg1=%s arg2=%s" arg1 arg2)))

Most of the time arg2 does not need to be entered. The default value is appropriate, and the second read is boring.

So I would prefer ask for arg1 and arg2 at the same time, something like a form. TAB switch between fields, ENTER (or other key) close the form and run the function.

-UUU:----F1  *scratch*      All (4,0)      (Lisp Interaction Fly AC WK ElDo
Arg1:                                  Arg2:

or

-UUU:----F1  *scratch*      All (4,0)      (Lisp Interaction Fly AC WK ElDo
Arg1:
Arg2:

How can I do this ?

I have look at magit transient, cus-edit.el and wid-edit.el (Are there any libraries for input fields in Emacs?) but I don't find.

Edit:

When I run isearch-fordward in the completions buffer during a find-file command, I have a second field in minibuffer. Does any one know how to do this ? enter image description here

Balaïtous
  • 173
  • 7
  • 1. `completing-read-multiple` may get you part-way there. 2. [Icicles](https://www.emacswiki.org/emacs/Icicles) [multi-completions](https://www.emacswiki.org/emacs/Icicles_-_Multi-Completions) provide what you request, but there are no doubt simple answers/workarounds also. – Drew Jun 19 '21 at 13:45
  • Thanks. I have tried `(completing-read-multiple "Arg1, Arg2: " nil)`. But it is more confusing, and I don't have same autocompletion for `arg1` and `arg2`. – Balaïtous Jun 19 '21 at 15:54
  • For multiple input fields, transient is the only option I am aware of. My brother has a already very useful [stalled/unfinished tutorial on using transients](https://flonic.gitlab.io/org-blog/blog/emacs-transient-tutorial/index.html), but using transient gets tricky for default values. – dalanicolai Jun 19 '21 at 16:07
  • 1
    However, I do not know why you want it, but is it really so bad to read the colors sequentially? You could optionally only prompt for a second color when the command is prefixed with a universal argument. Also, I am not sure what colors you want to provide as options, but you might like to use the `read-color` function (and possibly modify it to make it accept default values). – dalanicolai Jun 19 '21 at 16:15
  • Related: For [Elgrep](https://github.com/TobiasZawada/elgrep) I had a similar problem at a larger scale. Elgrep has many options. To use all those options with sane default settings I provided a menu-driven input `elgrep-menu` with the help of the [Widgets library](https://www.gnu.org/software/emacs/manual/html_mono/widget.html). This is probably only relevant if your command gets more options with sensible default values. – Tobias Jun 20 '21 at 05:49
  • My use case seems similar. I am try to implement a [git-grep interface](https://github.com/adelplanque/git-grep). `arg1` is expression to look for and `arg2` is the revision. Most of the time I want to search in worktree, and wan't be promted for revision, but some time I need to specify another revision. Same improvement would be fine in [ack-and-a-half](https://github.com/adelplanque/ack-and-a-half). Most of the time the project folder is the good directory to look up. But sometime it is useful to change it. For this I have 2 functions ... – Balaïtous Jun 20 '21 at 06:37
  • @dalanicolai Thanks for the tutorial. transient is a great library, but hard to understand and very poorly documented. I choose to use it, and this is my [first result](https://github.com/adelplanque/git-grep) – Balaïtous Jun 27 '21 at 18:26
  • @Balaïtous Thanks for the link. I am not familiar with git grep, but maybe, now, I will be soon. Transient indeed is a really great library, I think the 'technical' documentation is quite good already, but the 'practical' documentation is poor indeed. Especially practical examples are welcome. So currently we must resort to the Magit source code to find practical examples. – dalanicolai Jun 27 '21 at 22:22

0 Answers0