1

I have recently started using multiple frames regularly. I keep accidentally hitting C-x 5 0 instead of C-x 5 o. I do want to still be able to close frames, but I want an "are you sure? (y/n)" or similar prompt first. Is there anything built-in that can simply wrap a function in to prompt, or am I left inventing my own lisp to do so? I am hoping for something generic that I can easily reuse for other functions/hotkeys. Google gives me numerous but unrelated results.

memtha
  • 137
  • 6

1 Answers1

1

A popular y/n built-in function is y-or-n-p. To write up this answer, I first typed C-h k C-x 5 0 and saw that it was bound to delete-frame with two optional arguments being FRAME and FORCE; i.e., the *Help* buffer displayed (in part): (delete-frame &optional FRAME FORCE). From there, I wrote up a simple function and rebound C-x 5 0 to the new function. Stringing together functions and rebinding keyboard shortcuts to suit the needs of a particular user is, in my opinion, the Emacs way of doing things. The variable ctl-x-5-map is defined in subr.el.

(defun my-delete-frame (&optional frame force)
"My doc-string."
  (interactive)
  (if (y-or-n-p "Shall we execute `delete-frame'?:  ")
    (delete-frame frame force)
    (message "my-delete-frame:  You have chosen _not_ to execute `delete-frame'!")))

(define-key ctl-x-5-map "0" 'my-delete-frame)
lawlist
  • 18,826
  • 5
  • 37
  • 118
  • Nice. That's short enough that I'm not even going to bother trying to make a generic version that takes the function as a callback. – memtha Jul 24 '21 at 22:43