I'm trying to write a function to do the following tasks:
- Extract a piece of text from the current buffer (specifically, a diff)
- Switch to a temporary buffer in another window, and insert this text into that buffer.
- Set the temporary buffer's major mode (to diff-mode).
- Prompt the user to save that buffer to a file.
- Kill the buffer, and restore the window layout if necessary.
The rough skeleton of the function is as follows:
(defun extract-and-save () (interactive) (let ((text-to-save (get-text-to-save))) (with-temp-buffer? (insert text-to-save) (diff-mode) (save-buffer))))
I'm not able to get the
with-temp-buffer invocation right -- I either end up saving the buffer before it's visible and not killing it afterwards, or mess up the major mode of the previous buffer before switching.
I've tried using
with-temp-buffer-window and variants, but I don't understand them well enough/they don't work.