0

Is there a way to check whether cursor is in the prompt of read-multiple-choice, waiting for user's response?

I have tried (minibuffer-p) and (input-pending-p), but got no luck.

Code for testing (copy the code below to a new scratch-buffer and M-x eval-buffer RET):

(defun test-prompt ()
  ;; check if running `read-multiple-choice' exists
  (unless (or (minibufferp) (input-pending-p))
    (error "Test failed. \n\
You are supposed to be in the middle of `read-multiple-choice'.")))

(run-with-idle-timer 1 nil #'test-prompt)

(read-multiple-choice
 "Prompt: Which prebuilt binary to download? "
 '((?1 "amd64-GNU/Linux"
       "GNU/Linux on Intel/AMD x86_64 CPU")
   (?2 "amd64-macOS"
       "macOS on Intel/AMD x86_64 CPU")
   (?q "quit")))

If test-prompt were able to detect a running read-multiple-choice, it shouldn't have errored out.

1

1 Answer 1

0

If you report (current-buffer) you'll confirm that (minibuffer-p) should indeed be nil.

It does look like it's using the minibuffer, but if you check the code you'll see that read-multiple-choice--short-answers calls:

(let ((cursor-in-echo-area t))
  (read-event))

Which explains that misleading appearance.

Note also that the question can be asked/answered via GUI dialogue box (if the command was triggered by a mouse event); so the minibuffer wouldn't necessarily be relevant (in general) even if it was being used in the keyboard-based scenario.

(input-pending-p) isn't helpful to you at all. That's non-nil if the user has provided an input which hasn't yet been processed.

Nothing is provided for your need, but you could always write advice to bind a dynamic variable around read-multiple-choice:

(defvar my-read-multiple-choice-p nil
  "Non-nil when calling `read-multiple-choice'.")

(define-advice read-multiple-choice (:around (orig-fun &rest args) my-predicate)
  "Set `my-read-multiple-choice-p'."
  (let ((my-read-multiple-choice-p t))
    (apply orig-fun args)))
1
  • Ahh thanks! And I just realize that cursor-in-echo-area could be useful for check whether the cursor is around any prompts. Also, binding a dynamic variable helps to restrict the scope to specific event like read-multiple-choice, completing-read, etc.
    – LawxenceX
    Commented Dec 10, 2023 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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