Created
October 12, 2011 00:21
-
-
Save zdavkeos/1279865 to your computer and use it in GitHub Desktop.
Emacs diff-region
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; diff-region* - Diff two regions | |
;; | |
;; To compare two regions, select the first region | |
;; and run `diff-region`. The region is now copied | |
;; to a seperate diff-ing buffer. Next, navigate | |
;; to the next region in question (even in another file). | |
;; Mark the region and run `diff-region-now`, the diff | |
;; of the two regions will be displayed by ediff. | |
;; | |
;; You can re-select the first region at any time | |
;; by re-calling `diff-region`. | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(defun diff-region () | |
"Select a region to compare" | |
(interactive) | |
(when (use-region-p) ; there is a region | |
(let (buf) | |
(setq buf (get-buffer-create "*Diff-regionA*")) | |
(save-current-buffer | |
(set-buffer buf) | |
(erase-buffer)) | |
(append-to-buffer buf (region-beginning) (region-end))) | |
) | |
(message "Now select other region to compare and run `diff-region-now`") | |
) | |
(defun diff-region-now () | |
"Compare current region with region already selected by `diff-region`" | |
(interactive) | |
(when (use-region-p) | |
(let (bufa bufb) | |
(setq bufa (get-buffer-create "*Diff-regionA*")) | |
(setq bufb (get-buffer-create "*Diff-regionB*")) | |
(save-current-buffer | |
(set-buffer bufb) | |
(erase-buffer)) | |
(append-to-buffer bufb (region-beginning) (region-end)) | |
(ediff-buffers bufa bufb)) | |
) | |
) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Thanks! It works great! You should consider adding this to the Emacs wiki :)
Great idea but it's already implemented in GNU Emacs: ediff-regions-linewise and ediff-regions-wordwise. Nonetheless, I discovered this thanks to you when I misstyped "diff-region" as "ediff-region". :)
Very nice, exactly what I needed now!
@vifon I think the ediff-regions-* functions are different, you can't compare regions in the same buffer with them, can you?
@rashack: Sure you can. Just select the same buffer in both prompts.
works great! thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I always seem to find myself wanting to diff two regions in Emacs, not two buffers, not to files, not a buffer and a file, just two regions. This pair of functions is intended to mimic WinMerge's Compare/Compare To... functionality. You first select a region with
diff-region
, which gets saved off in a hidden buffer. You are then free to navigate/edit/work until you have a corresponding region to diff. Runningdiff-region-now
copies the new region to another hidden buffer, and the the diff of the two buffers is displayed byediff
.This seemed to small to wrap into a full package, but at some point it might be nice. For now, just paste the code into your
.emacs
file - upon restart/evaluation you will have access todiff-region
anddiff-region-now
.