casouri / vundo

Visualize the undo tree.
413 stars 20 forks source link

Add "vundo-diff-setup-hook" for manipulating the diff for display #87

Closed ideasman42 closed 6 months ago

ideasman42 commented 6 months ago

This PR adds a hook that runs after the vundo-diff buffer has been setup (created or re-initialized with new contents).

This can be useful for manipulating the diff buffer, for example, I'm currently using two hooks:

  ;; Remove noisy "Diff finished" message.
  (add-hook
   'vundo-diff-setup-hook
   (lambda ()
     (save-excursion
       (goto-char (point-max))
       (forward-line -2)
       (when (looking-at-p "\nDiff finished\\.  ")
         (delete-region (point) (point-max))))))

This hook uses the diff-ansi package for easier to read diffs:

  (add-hook
   'vundo-diff-setup-hook
   (lambda ()
     (font-lock-mode -1)
     (setq-local diff-ansi-method 'immediate)
     (save-excursion
       (goto-char (point-min))
       (forward-line 1)
       (diff-ansi-region (point) (point-max)))))

This allows delta to be used for diff display, which supports side-by-side view, syntax-highlighting and character level difference display which I find easier to parse:

screenshot.webm


Note that I had to adjust the filename formatting for delta to syntax highlight properly, perhaps the diff filename formatting could be configurable eventually, that's outside the scope of this PR though.

--- a/vundo-diff.el
+++ b/vundo-diff.el
@@ -58,7 +58,7 @@ CURRENT node."
                                     (if vundo-diff--marked-node "Marked" "Parent"))
                        collect
                        (list (format "[%d]" idx)
-                             (format "<%s> [mod %d] (%s)" orig-name idx stat)
+                             (format "[mod %d] (%s) %s"  idx stat orig-name)
                              (when (consp ts) (format-time-string "%F %r" ts)))))
         lim)
     (with-current-buffer buf
ideasman42 commented 6 months ago

This may be of interest to @jdtsmith.

jdtsmith commented 6 months ago

I'm not familiar with ansi-diff. Is the full diff header information preserved by it? If so, would it not make more sense for the hook to run prior to cleanup? Then you presumably wouldn't need to hack the --- +++ line display.

ideasman42 commented 6 months ago

I'm not familiar with ansi-diff. Is the full diff header information preserved by it? If so, would it not make more sense for the hook to run prior to cleanup? Then you presumably wouldn't need to hack the --- +++ line display.

The issue would then be that vundo-diff might attempt to cleanup content that was in a format that it didn't expect, although, in the case of diff-ansi it would mean some parts of vundo-diff could be skipped (anything that sets highlighting for e.g.).

jdtsmith commented 6 months ago

Right, the cleanup only "does what it can". Do the ---/+++ lines stay intact?

ideasman42 commented 6 months ago

The edits to --- / +++ are left in, it seems diff-ansi, which I have configured to use delta - works with the modifications.

Any overlays or text properties are lost though.

casouri commented 6 months ago

@ideasman42 if you can squash and rewrite the commit message, I can then merge it.

ideasman42 commented 6 months ago

@ideasman42 if you can squash and rewrite the commit message, I can then merge it.

Done (some reviewers prefer to see the individual commits, then squish themselves).

casouri commented 6 months ago

Cool, thanks!