1

I need to use completion prompt, but also capture the fact when the user hits <escape> or C-g. Here's an example:

(let ((proj (completing-read "choose project:" projectile-known-projects)))
    (unless proj (print "never gets here")))
    

It pops up the dialog, but if you press Esc, or C-g - it just terminates the execution and never get to print anything.

I need to capture the fact of "no choice has been made" (user hits Escape or C-g). But pressing those keys while completion dialog is active would abort everything and the execution will not proceed any further.

7
  • I don't know the way, but does it help to specify the fourth REQUIRE-MATCH to t?
    – whatacold
    Dec 20, 2021 at 23:46
  • @whatacold no, it doesn't. Even if I put (edebug) as the first thing inside the block (after the let-bindings), that part still remains unreachable if Esc gets hit
    – iLemming
    Dec 20, 2021 at 23:53
  • The default commands for C-g and ESC ESC ESC are keyboard-quit and keyboard-escape-quit, try to bind other ad-hoc commands for them and do your stuffs there?
    – whatacold
    Dec 21, 2021 at 0:02
  • BTW, why do you need to capture these events?
    – whatacold
    Dec 21, 2021 at 0:03
  • keyboard-quit signals a quit (read about signals in the Emacs Lisp manual: do C-h i g(elisp)Signals). See the function condition-case (do C-h f condition-case for its doc string) for how to handle signals.
    – NickD
    Dec 21, 2021 at 0:18

1 Answer 1

1

@NickD's comment above lead me to this snippet:

(let ((selected (condition-case err
                    (completing-read "choose project:" projectile-known-projects)
                  (quit "nothing"))))
  (message "selection: %s " selected))

And that's exactly what I needed. Pressing Escape or C-g sets the value to "nothing"

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.