casouri / vundo

Visualize the undo tree.
413 stars 20 forks source link

Add special hook vundo-after-undo-functions #74

Closed protesilaos closed 1 year ago

protesilaos commented 1 year ago

The idea is to allow users to call functions that act on the vundo--orig-buffer.

protesilaos commented 1 year ago

I am using this special hook to produce a diff of the underlying file. Here is how I am doing it right now:

(defvar prot/vundo-diff-buffer-window nil
  "Window object of `prot/vundo-diff-buffer'.")

(defun prot/vundo-quit-diff-window ()
  "Quit `prot/vundo-diff-buffer-window' if it is live.
Assign this function to the `vundo-post-exit-hook'."
  (when (and prot/vundo-diff-buffer-window
             (window-live-p prot/vundo-diff-buffer-window))
    (quit-window nil prot/vundo-diff-buffer-window)
    (setq prot/vundo-diff-buffer-window nil)))

(defun prot/vundo-diff-buffer (buffer)
  "Diff BUFFER with its underlying file, if possible.
Assign this to `vundo-after-undo-functions'.  BUFFER is provided
by that special hook."
  (when (buffer-file-name buffer)
    (with-current-buffer (window-buffer (diff-buffer-with-file buffer))
      (setq prot/vundo-diff-buffer-window (get-buffer-window)))))

(add-hook 'vundo-after-undo-functions #'prot/vundo-diff-buffer)
(add-hook 'vundo-post-exit-hook #'prot/vundo-quit-diff-window)
gitrj95 commented 1 year ago

hi guys :). wonder if we can select two points in vundo and construct a diff between them?

casouri commented 1 year ago

Cool. I think this is fine. I'd call hooks hook though. Is there any particular reason to call it functions?

protesilaos commented 1 year ago

Thank you @casouri!

Is there any particular reason to call it functions?

Quote from (info "(elisp) Hooks")

If the hook variable’s name does not end with ‘-hook’, that indicates it is probably an “abnormal hook”. These differ from normal hooks in two ways: they can be called with one or more arguments, and their return values can be used in some way. The hook’s documentation says how the functions are called and how their return values are used. Any functions added to an abnormal hook must follow the hook’s calling convention. By convention, abnormal hook names end in ‘-functions’.

casouri commented 1 year ago

Thanks, TIL. I merged the change.

protesilaos commented 1 year ago

@casouri You are welcome!

@gitrj95 Maybe that is possible. Though I have not studied the code thoroughly to know the answer.

casouri commented 1 year ago

@gitrj95 You can move between nodes on the tree with vundo--move-to-node, grab the buffer (vundo--orig-buffer), and diff the two buffer. I would search for vundo--move-to-node in the source to see how it's used--I can't tell you from the top of my head, because it's been a while since I wrote it ;-)

To be extra save you can clone the original buffer first.

jdtsmith commented 7 months ago

@gitrj95 It's very possible to a "mark and diff", and we should definitely add this option. Can reuse some of the marking functionality from the saved buffer state improvements. In fact, I use a d="diff to the last node" capability all the time with vundo; see #56. We can continue the conversation there.