1

With the following function using "completing-read", how can one add a default initial input to be Convert. What can I do?

(defun subset ()
  "Convert or extract subsets of data."
  (interactive)

  (let* ( (opts '("Convert" "Split"))
          (sel  (completing-read "Subset: " opts nil t  )) )
Dilna
  • 1,173
  • 3
  • 10
  • The initial-input can be given as the 5th argument to the `completing-read` function. You can find that using `C-h f`. And, while typing the `completing-read` form, the expected arguments are displayed in the echo area. Or do you mean something else? I am not sure what is your goal, but if you have only two options, you could also consider to use the `universal argument`. – dalanicolai Jun 13 '22 at 22:31
  • I would just enter "Convert" as the 5th argument or something else? – Dilna Jun 13 '22 at 22:39
  • Yes just enter that – dalanicolai Jun 13 '22 at 22:40
  • 1
    Thank you friend. – Dilna Jun 13 '22 at 22:44
  • I am actually encountering a slight problem. I add "Convert" and run the function. What happens is that "Convert" appears as expected. But when I go through the option, the first one that appears is also "Convert". Making it seem like the option is stuck to "Convert". – Dilna Jun 13 '22 at 22:49
  • I think that is the only option when using 'plain' completing read. If you would like to see the other options also, then you can better use some 'completion package/framework', like [Ivy](https://github.com/abo-abo/swiper), [helm](https://github.com/emacs-helm/helm), [vertico](https://github.com/minad/vertico) etc. Then the first 'candidate' could be considered the default. – dalanicolai Jun 13 '22 at 22:57
  • A limitation of `completing-read` I assume. – Dilna Jun 13 '22 at 23:05
  • Well, it seems like I did not read thoroughly enough. I assume that, what you want, is the second case in Drew's answer. So, no limitation of `completing-read`, also not a shortcoming of the documentation. Just a limitation of our patience to read the function it's documentation :) – dalanicolai Jun 14 '22 at 06:03

1 Answers1

1

If you really mean use Convert as the initial input then this is how:

(let* ((opts  '("Convert" "Split"))
       (sel   (completing-read "Subset: " opts nil t "Convert")))
  (message "SEL: %s" sel))

If you really mean use Convert as the default value then this is how:

(let* ((opts  '("Convert" "Split"))
       (sel   (completing-read "Subset: " opts nil t nil nil "Convert")))
  (message "SEL: %s" sel))

If you really mean use Convert as both the initial input and the default value then this is how:

(let* ((opts  '("Convert" "Split"))
       (sel   (completing-read "Subset: " opts nil t "Convert" nil "Convert")))
  (message "SEL: %s" sel))

In the initial-input (only) case Convert is preinserted into the minibuffer, and the default, if you remove that preinserted input, is the empty string, "".

In the default (only) case Convert is not inserted -- you use M-n to insert it. And if you just RET without inserting any text then you get Convert.

In all three cases, just hitting RET chooses Convert. Convert is an allowed completion because you decided that it belongs to opts, which you pass as the allowed completions.

If you remove Convert from opts and change the t value of arg REQUIRE-MATCH to nil, then Convert isn't a completion but it is accepted as an input. Anything at all is accepted as an input, in this case.

It all depends on what you want. If you have a more specific question, please pose that separately.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Reading the docstring of `completing-read` for `INITIAL-INPUT` - which says that the argument is deprecated - telling users to pass `nil` and supply the default value `DEF` instead. – Dilna Jun 21 '22 at 07:59
  • Your comment isn't a sentence. What about such reading? – Drew Jun 21 '22 at 14:55
  • That the suggestion on using initial-input could became a problem in future. – Dilna Jun 21 '22 at 15:18