casouri / vundo

Visualize the undo tree.
413 stars 20 forks source link

Simple diff functionality #56

Closed jdtsmith closed 6 months ago

jdtsmith commented 1 year ago

I missed the "diff" function of undo-tree. You don't always need it, but for complex changes, it can be very helpful to figure out which branch you want. I cobbled together something that works for me — an "on demand" diff on key "d". I didn't study vundo's source closely enough to do this more intelligently, but it would be lovely if something like this made it into vundo.

Thanks for this simple, robust, and great tool.

(defun my/vundo-diff ()
    (interactive)
    (let* ((orig vundo--orig-buffer)
           (source (vundo--current-node vundo--prev-mod-list))
           (dest (vundo-m-parent source)))
      (if (or (not dest) (eq source dest))
          (message "vundo diff not available.")
    (let ((buf (make-temp-name (concat (buffer-name orig) "-vundo-diff"))))
          (vundo--move-to-node source dest orig vundo--prev-mod-list)
          (with-current-buffer (get-buffer-create buf)
        (insert-buffer orig))
          (vundo--refresh-buffer orig (current-buffer) 'incremental)
          (vundo--move-to-node dest source orig vundo--prev-mod-list)
          (vundo--refresh-buffer orig (current-buffer) 'incremental)
          (diff-buffers buf orig)
          (kill-buffer buf)))))
(define-key vundo-mode-map "d" #'my/vundo-diff)
casouri commented 1 year ago

Oh Cool! I guess it can be useful when you couldn't tell what was changed in an undo. You can use undo-hl but this is another way. I'll see what I can come up with when I find som time. Thanks!

jdtsmith commented 1 year ago

Yeah especially for "big" diffs like copying in a bunch of changes from another version, or a complex find/replace, it really helps. I probably use it 1 in 20 times I invoke vundo. Compared to undo-tree, I actually find I prefer the diff not to change as I move around nodes, i.e. only when invoked. That said, it would be very nice to highlight the node from which diff was invoked in some way. I.e. add another overlay that does vundo--highlight-node but in diff-header face color.

One thing I didn't quite figure out was what (vundo--refresh-buffer orig (current-buffer) 'incremental) really does, so I sprinkled them in until it worked ;). Probably this could be streamlined.

jdtsmith commented 7 months ago

Picking this back up. It would be nice to (m)ark a node (or (u)nmark it), navigate around using the normal vundo nav functions, then (d)iff between the marked and current node (by default, the prior node, as above). I can take a look.

gitrj95 commented 7 months ago

This would be great for my workflow. Thanks.

jdtsmith commented 6 months ago

This is implemented in #78.