Skip to content

Instantly share code, notes, and snippets.

@agzam
Last active July 27, 2020 15:46
Show Gist options
  • Save agzam/792ccef7eb57e9bcb27253be1bea16f3 to your computer and use it in GitHub Desktop.
Save agzam/792ccef7eb57e9bcb27253be1bea16f3 to your computer and use it in GitHub Desktop.
Diff last couple of things in the kill ring
(defun diff-last-two-kills (&optional ediff?)
"Diff last couple of things in the kill-ring. With prefix open ediff."
(interactive "P")
(let* ((old "/tmp/old-kill")
(new "/tmp/new-kill")
(prev-ediff-quit-hook ediff-quit-hook))
(cl-flet ((kill-temps
()
(dolist (f (list old new))
(kill-buffer (find-buffer-visiting f)))
(setq ediff-quit-hook prev-ediff-quit-hook)))
(with-temp-file new
(insert (current-kill 0 t)))
(with-temp-file old
(insert (current-kill 1 t)))
(if ediff?
(progn
(add-hook 'ediff-quit-hook #'kill-temps)
(ediff old new))
(diff old new "-u" t)))))
@piranha
Copy link

piranha commented Jul 1, 2020

That's a bit better regarding Emacs' complains about unknown variables (if ediff was not run yet):

(defun diff-last-two-kills (&optional ediff?)
  "Diff last couple of things in the kill-ring. With prefix open ediff."
  (interactive "P")
  (let* ((old "/tmp/old-kill")
         (new "/tmp/new-kill")
         (prev-ediff-quit-hook (if (fboundp 'ediff-quit-hook)
                                   (symbol-value 'ediff-quit-hook))))
    (cl-flet ((kill-temps ()
                          (dolist (f (list old new))
                            (kill-buffer (find-buffer-visiting f)))
                          (if prev-ediff-quit-hook
                              (setq ediff-quit-hook prev-ediff-quit-hook))))
      (with-temp-file new
        (insert (current-kill 0 t)))
      (with-temp-file old
        (insert (current-kill 1 t)))
      (if ediff?
          (progn
            (add-hook 'ediff-quit-hook #'kill-temps)
            (ediff old new))
        (diff old new "-u" t)))))

It still complains about ediff-quit-hook though. :-)

@agzam
Copy link
Author

agzam commented Jul 1, 2020

Hmm... I haven't encountered that. Maybe it just needs (require 'ediff)?

@piranha
Copy link

piranha commented Jul 1, 2020

Yeah, require would help, I just don't like requiring too much on startup. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment