1

What I'm trying to achieve

I'm trying to create a popup that runs an elisp funtion I've written which searches my library directory for pdfs, gets the user to choose an option (using completing read) and opens the selected pdf.

What I've done

Because I want it available as a keybinding in my desktop env, I'm running it through a wrapper using emacsclient. I'm calling:

emacsclient --eval "(mwe-pdf-lookup-and-quit)" -a ""

Which points to the function:

(defun mwe-pdf-lookup-and-quit ()
  (make-frame '((width . 80) (height . 25)
                (minibuffer . only)
                (name . "lookup")))
  (sleep-for 0.01)
  (select-frame-by-name "lookup")
  (select-frame-set-input-focus (selected-frame))
  (message (completing-read ; This is where my pdf search function goes
            "Choose: "
            '("Option 1" "Option 2")))
  (delete-frame))

The problem

Works perfectly if I choose a file. The pdf is opened and the frame is deleted. The problem is, if I decide I don't want to open a file anymore and press C-g, the frame displays "Quit emacsclient request" and hangs around. I would like to know how I can delete the minibuffer frame instead.

1

You could use unwind-protect like that:

(make-frame '((width . 80) (height . 25)
                (minibuffer . only)
                (name . "lookup")))
  (sleep-for 0.01)
  (select-frame-by-name "lookup")
  (select-frame-set-input-focus (selected-frame))
  (unwind-protect
      (message (completing-read ; This is where my pdf search function goes
        "Choose: "
        '("Option 1" "Option 2")))
    (delete-frame)))

The unwind forms are executed unconditionally, also in the case of C-g.

1
  • That works! Thank you
    – jamesmaj
    Feb 14 '21 at 20:11

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.