0

Context:

I have been using an interesting software called Emacs Anywhere. It helps me bring emacs keybindings for stuff like this (editing text for a question on Stack Exchange). Overall, the software has been working well!

This is an illustration of the expected behavior:

  1. I put my cursor on a text box from Stack Overflow to write a question.
  2. Then, I invoke Emacs Anywhere with a hotkey
  3. Cool, I am editing text now with Emacs
  4. After finishing what I want to ask on SO, I press C-x 5 0, the frame is deleted, and the text written on the Emacs frame is inserted on the text box my cursor was pointing to on the browser. Cool! I believe in magic :)

The main problem I have is when I change the cursor on the browser before the text is inserted. For instance:

  1. I put my cursor on a text box from Stack Overflow to write a question.
  2. Then, I invoke Emacs Anywhere with a hotkey
  3. Cool, I am editing text now with Emacs
  4. As I am writing text, I decide to check Wikipedia and official documentation to base my answer, I left my pointer in a selected text from Wikipedia
  5. Now, I am back to the Emacs frame editing my question
  6. After finishing what I want to ask on SO, I press C-x 5 0 and the frame is deleted. Unfortunately, the test I wrote is not introduced to text box of SO. Why? Because the cursor on the browser is not on the same page and point of the browser it was when I started the process!

Oh, boy. After written a big question, I wished I had saved it. I am sure yank-pop could save me!

Hence, I have the idea: maybe I should write something on my config file to add the content of a frame to the clipboard ring (or kill ring) before a frame is deleted!

Question

So, I would like to define a hook so that every time I delete an Emacs frame (default keybinding C-x 5 0), the content happens to be added to the kill ring (clipboard ring) before the real deletion happens.

How would you do it?

Pedro Delfino
  • 1,369
  • 3
  • 13

2 Answers2

1

Sounds like you're looking for this hook: delete-frame-functions

delete-frame-functions is a variable defined in C source code.

Its value is nil

This variable can be risky when used as a file-local variable.

Documentation:

Functions run before deleting a frame.

The functions are run with one arg, the frame to be deleted. See delete-frame.

Note that functions in this list may be called just before the frame is actually deleted, or some time later (or even both when an earlier function in delete-frame-functions (indirectly) calls `delete-frame' recursively).

BTW, you can find this hook also if you look for variables whose names end in hook, as delete-frame-hook is an alias for delete-frame-functions.

See the Elisp manual, node Hooks, for information about "abnormal hooks" such as this one -- they're the hooks whose names end in -functions instead of -hook.

Drew
  • 75,699
  • 9
  • 109
  • 225
0

After @Drew's and @Gilles help on another question, I managed to build the code below and add it to my config file:

(defun pmd/select-and-kill-frame-before-it-is-deleted ()
  "Add content of frame to the kill ring before frame is deleted.
This helps with a problem that I faced while using the software
called Emacs Anywhere."
  (kill-new (filter-buffer-substring beg end)))

(add-hook 'delete-frame-functions 'pmd/select-and-kill-frame-before-it-is-deleted)
Pedro Delfino
  • 1,369
  • 3
  • 13