Skip to content

Instantly share code, notes, and snippets.

@cpoile
Forked from zdavkeos/diff_region.el
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpoile/9981798 to your computer and use it in GitHub Desktop.
Save cpoile/9981798 to your computer and use it in GitHub Desktop.
;; built on: https://gist.github.com/zdavkeos/1279865
;; and ediff-util (GNU Emacs)
;;
;; diff-region-with-killring* - Diff the current region with the
;; region that was most recently killed (cut or copied). The
;; highlighted region is called Region.B during the ediff session.
;; Changes made to the highlighted region (Region.B) will be saved
;; when quitting the ediff session. This is designed to be similar to
;; the 'compare with clipboard' function in IntelliJ IDEA.
;;
(defun diff-region-with-killring ()
"Select a region to compare with the kill-ring's most recent region."
(interactive)
(when (use-region-p) ; there is a region
(let ((buffer-A (get-buffer-create "-killring-Region.A-"))
(buffer-B (ediff-make-cloned-buffer (current-buffer) "-Region.B-"))
reg-A-beg reg-A-end reg-B-beg reg-B-end)
;; make sure the temp and cloned buffers are removed after ediff-session
(ediff-with-current-buffer buffer-A
(setq ediff-temp-indirect-buffer t))
(ediff-with-current-buffer buffer-B
(setq ediff-temp-indirect-buffer t))
(with-current-buffer buffer-A
(erase-buffer)
(yank)
(setq reg-A-beg (point-min))
(setq reg-A-end (point-max))
(set-buffer buffer-B)
(setq reg-B-beg (region-beginning))
(setq reg-B-end (region-end)))
(ediff-regions-internal
(get-buffer buffer-A) reg-A-beg reg-A-end
(get-buffer buffer-B) reg-B-beg reg-B-end
nil 'ediff-regions-wordwise 'word-mode nil))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment