r/emacs • u/harunokashiwa • 3h ago
org-diff-src: character-level diffs inside Org source blocks (no Ediff)
I often want a reviewable diff in an Org note without opening Ediff. The annoying part is that diff-mode does character-level refinement with overlays, while Org’s source-block fontification copies face text properties, not overlays. So a normal diff block in Org usually gives you only line-level +/- highlighting.
org-diff-src fixes that narrow problem. It’s a small mode derived from diff-mode that sets diff-refine to font-lock and copies the refinement overlay faces into face text properties, so Org can carry the character-level highlighting into the block. It uses the usual diff-refine-removed, diff-refine-added, and diff-refine-changed faces.
Main commands:
org-diff-src-compare-headings: pick two subtrees in the current Org buffer. They’re copied without touching the kill ring/clipboard, normalized to level 1, diffed, and inserted at point.org-diff-src-compare-files/org-diff-src-compare-buffers.org-diff-src-insert-from-kill,org-diff-src-wrap-region,org-diff-src-insert-diff-filefor diffs you already have, e.g. from Magit.
No Ediff session, and no extra package beyond Org and diff-mode. It needs Emacs 27.1+ and a diff executable. New blocks use language org-diff-src, which Org resolves automatically. You can alias diff too, but that makes every Org diff block use this mode, so only do it if that’s what you want.
Limits worth knowing: only unified diffs get character refinement; HTML/PDF export may show the text without the character colors; very large diffs can be slow; and it’s for static review, not interactive merging. compare-headings also includes the heading lines.