Function: compare-windows

compare-windows is an autoloaded, interactive and byte-compiled function defined in compare-w.el.gz.

Signature

(compare-windows IGNORE-WHITESPACE)

Documentation

Compare text in current window with text in another window.

The option compare-windows-get-window-function defines how to get another window.

Compares the text starting at point in each window, moving over text in each one as far as they match.

This command pushes the mark in each window at the prior location of point in that window. If both windows display the same buffer, the mark is pushed twice in that buffer: first in the other window, then in the selected window.

A prefix arg IGNORE-WHITESPACE, means reverse the value of variable compare-ignore-whitespace. If compare-ignore-whitespace is nil, then a prefix arg means ignore changes in whitespace. If compare-ignore-whitespace is non-nil, then a prefix arg means don't ignore changes in whitespace. The variable compare-windows-whitespace controls how whitespace is skipped. If compare-ignore-case is non-nil, changes in case are also ignored.

If compare-windows-sync is non-nil, then successive calls of this command work in interlaced mode: on first call it advances points to the next difference, on second call it synchronizes points by skipping the difference, on third call it again advances points to the next difference and so on.

View in manual

Probably introduced at or before Emacs version 19.20.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/compare-w.el.gz
;;;###autoload
(defun compare-windows (ignore-whitespace)
  "Compare text in current window with text in another window.
The option `compare-windows-get-window-function' defines how
to get another window.

Compares the text starting at point in each window,
moving over text in each one as far as they match.

This command pushes the mark in each window
at the prior location of point in that window.
If both windows display the same buffer,
the mark is pushed twice in that buffer:
first in the other window, then in the selected window.

A prefix arg IGNORE-WHITESPACE, means reverse the value of variable
`compare-ignore-whitespace'.  If `compare-ignore-whitespace' is
nil, then a prefix arg means ignore changes in whitespace.  If
`compare-ignore-whitespace' is non-nil, then a prefix arg means
don't ignore changes in whitespace.  The variable
`compare-windows-whitespace' controls how whitespace is skipped.
If `compare-ignore-case' is non-nil, changes in case are also
ignored.

If `compare-windows-sync' is non-nil, then successive calls of
this command work in interlaced mode:
on first call it advances points to the next difference,
on second call it synchronizes points by skipping the difference,
on third call it again advances points to the next difference and so on."
  (interactive "P")
  (if compare-ignore-whitespace
      (setq ignore-whitespace (not ignore-whitespace)))
  (let* (p1 p2 maxp1 maxp2 b1 b2 w2
	    (progress 1)
	    (opoint1 (point))
	    opoint2
	    skip-func-1
	    skip-func-2
	    (sync-func (if (stringp compare-windows-sync)
                           'compare-windows-sync-regexp
                         compare-windows-sync)))
    (setq p1 (point) b1 (current-buffer))
    (setq w2 (funcall compare-windows-get-window-function))
    (setq p2 (window-point w2)
	  b2 (window-buffer w2))
    (setq opoint2 p2)
    (setq maxp1 (point-max))

    (setq skip-func-1 (if ignore-whitespace
			  (if (stringp compare-windows-whitespace)
			      (lambda (pos)
				(compare-windows-skip-whitespace pos)
				t)
			    compare-windows-whitespace)))

    (with-current-buffer b2
      (setq skip-func-2 (if ignore-whitespace
			    (if (stringp compare-windows-whitespace)
			      (lambda (pos)
				(compare-windows-skip-whitespace pos)
				t)
			      compare-windows-whitespace)))
      (push-mark p2 t)
      (setq maxp2 (point-max)))
    (push-mark)

    (while (> progress 0)
      ;; If both windows have whitespace next to point,
      ;; optionally skip over it.
      (and skip-func-1
	   (save-excursion
	     (let (p1a p2a result1 result2)
	       (setq result1 (funcall skip-func-1 opoint1))
	       (setq p1a (point))
	       (set-buffer b2)
	       (goto-char p2)
	       (setq result2 (funcall skip-func-2 opoint2))
	       (setq p2a (point))
	       (if (and result1 result2 (eq result1 result2))
		   (setq p1 p1a
			 p2 p2a)))))

      (let ((size (min (- maxp1 p1) (- maxp2 p2)))
	    (case-fold-search compare-ignore-case))
	(setq progress (compare-buffer-substrings b2 p2 (+ size p2)
						  b1 p1 (+ size p1)))
	(setq progress (if (zerop progress) size (1- (abs progress))))
	(setq p1 (+ p1 progress) p2 (+ p2 progress)))
      ;; Advance point now rather than later, in case we're interrupted.
      (goto-char p1)
      (set-window-point w2 p2)
      (when compare-windows-recenter
        (recenter (car compare-windows-recenter))
        (with-selected-window w2 (recenter (cadr compare-windows-recenter)))))

    (if (= (point) opoint1)
	(if (not sync-func)
            (ding)
          ;; If points are not advanced (i.e. already on mismatch position),
          ;; then synchronize points between both windows
          (save-excursion
            (setq compare-windows-sync-point nil)
            (funcall sync-func)
            (setq p1 (point))
            (set-buffer b2)
            (goto-char p2)
            (funcall sync-func)
            (setq p2 (point)))
          (goto-char p1)
          (set-window-point w2 p2)
          (when compare-windows-recenter
            (recenter (car compare-windows-recenter))
            (with-selected-window w2 (recenter (cadr compare-windows-recenter))))
          ;; If points are still not synchronized, then ding
          (if (and (= p1 opoint1) (= p2 opoint2))
	      (progn
		;; Display error message when current points in two windows
		;; are unmatched and next matching points can't be found.
		(compare-windows-dehighlight)
		(ding)
		(message "No more matches with %s" b2))
	    (message "Diff -%s,%s +%s,%s with %s" opoint2 p2 opoint1 p1 b2)))
      (message "Match -%s,%s +%s,%s with %s" opoint2 p2 opoint1 p1 b2))))