+;; Comparing buffers/files
+;; These use ediff to find the differences.
+
+(defun highlight-markup-buffers
+ (buf-a file-a buf-b file-b &optional markup-a-only)
+ "Get differences between two buffers and set highlight changes.
+Both buffers are done unless optional parameter MARKUP-A-ONLY
+is non-nil."
+ (save-window-excursion
+ (let* (change-info
+ change-a change-b
+ a-start a-end len-a
+ b-start b-end len-b
+ (bufa-modified (buffer-modified-p buf-a))
+ (bufb-modified (buffer-modified-p buf-b))
+ (buf-a-read-only (with-current-buffer buf-a buffer-read-only))
+ (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
+ temp-a temp-b)
+ (if (and file-a bufa-modified)
+ (if (y-or-n-p (format "Save buffer %s? " buf-a))
+ (with-current-buffer buf-a
+ (save-buffer)
+ (setq bufa-modified (buffer-modified-p buf-a)))
+ (setq file-a nil)))
+ (or file-a
+ (setq temp-a (setq file-a (ediff-make-temp-file buf-a nil))))
+
+ (if (and file-b bufb-modified)
+ (if (y-or-n-p (format "Save buffer %s? " buf-b))
+ (with-current-buffer buf-b
+ (save-buffer)
+ (setq bufb-modified (buffer-modified-p buf-b)))
+ (setq file-b nil)))
+ (or file-b
+ (setq temp-b (setq file-b (ediff-make-temp-file buf-b nil))))
+ (set-buffer buf-a)
+ (highlight-changes-mode 'active)
+ (or markup-a-only (with-current-buffer buf-b
+ (highlight-changes-mode 'active)))
+ (setq change-info (hilit-chg-get-diff-info buf-a file-a buf-b file-b))
+
+
+ (setq change-a (car change-info))
+ (setq change-b (car (cdr change-info)))
+
+ (hilit-chg-make-list)
+ (while change-a
+ (setq a-start (nth 0 (car change-a)))
+ (setq a-end (nth 1 (car change-a)))
+ (setq b-start (nth 0 (car change-b)))
+ (setq b-end (nth 1 (car change-b)))
+ (setq len-a (- a-end a-start))
+ (setq len-b (- b-end b-start))
+ (set-buffer buf-a)
+ (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
+ (or markup-a-only
+ (with-current-buffer buf-b
+ (hilit-chg-set-face-on-change b-start b-end len-a
+ buf-b-read-only)
+ ))
+ (setq change-a (cdr change-a))
+ (setq change-b (cdr change-b)))
+ (or bufa-modified
+ (with-current-buffer buf-a (set-buffer-modified-p nil)))
+ (or bufb-modified
+ (with-current-buffer buf-b (set-buffer-modified-p nil)))
+ (if temp-a
+ (delete-file temp-a))
+ (if temp-b
+ (delete-file temp-b)))
+ ))
+
+;;;###autoload
+(defun highlight-compare-buffers (buf-a buf-b)
+"Compare two buffers and highlight the differences.
+
+The default is the current buffer and the one in the next window.
+
+If either buffer is modified and is visiting a file, you are prompted
+to save the file.
+
+Unless the buffer is unmodified and visiting a file, the buffer is
+written to a temporary file for comparison.
+
+If a buffer is read-only, differences will be highlighted but no property
+changes are made, so \\[highlight-changes-next-change] and
+\\[highlight-changes-previous-change] will not work."
+ (interactive
+ (list
+ (get-buffer (read-buffer "buffer-a " (current-buffer) t))
+ (get-buffer
+ (read-buffer "buffer-b "
+ (window-buffer (next-window (selected-window))) t))))
+ (let ((file-a (buffer-file-name buf-a))
+ (file-b (buffer-file-name buf-b)))
+ (highlight-markup-buffers buf-a file-a buf-b file-b)
+ ))