1

The function completing-read permits the user to simply press the enter key without selecting a match against the list of valid choices:

(let* ((completion-ignore-case t)
       (client (completing-read "CLIENT:  " '("a" "b" "c" "d") nil t)))
  client)

Q: Is there a similar completion function that will prevent the user from exiting by simply pressing the enter key without making a valid choice?

1

I'd do this (similar to @lawlist's answer):

(let ((completion-ignore-case t)
      (client ""))
  (while (equal client "")
    (setq client  (completing-read "CLIENT:  " '("a" "b" "c" "d") nil t)))
  client)
0

See the answer by Stefan in a similar thread on stackoverflow.com [ https://stackoverflow.com/a/12824748/2112489 ] that states in relevant part:

My guess is that you want to prevent the user from hitting RET with an
empty answer.  Indeed `completing-read' does not prevent that, even
with `require-match' set.  The way this is usually handled is by using
a non-nil value for the `default' argument, in which case this value
is returned when the user just hits RET.

If that's not good enough, then you're probably going to have to use
`minibiffer-with-setup-hook' and in the hook, setup a special keymap
you've created for this purpose where RET is bound to a new function
that signals an error if the minibuffer is empty and calls
`minibuffer-complete-and-exit' otherwise.

Another option is to use a while loop:

(let ((client (catch 'break
                (while t
                  (let* ((completion-ignore-case t)
                         (choice (completing-read "CLIENT:  " '("a" "b" "c" "d") nil t)))
                    (if (equal choice "")
                      (message "Please select a valid choice!")
                      (throw 'break choice)))))))
  client)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.