[gnu.emacs.bug] compare-w.el ignoring whitespace and case

hallvard@IFI.UIO.NO (Hallvard B Furuseth) (01/19/90)

I have modified compare-w.el to obey case-fold-search and skip some
whitespace if compare-windows-exactly is nil.  You may specify a
regexp matching one whitespace character, and a function that descides
whether whitespace may be skipped in the other buffer.  It will not
skip all legal whitespace, but that is better than skipping too much.

I am sure the default permit-whitespace-function could be improved,
but I didn't care.  Maybe someone else will do it.

Hallvard Furuseth
hallvard@ifi.uio.no
------------------------------------------------------------------------

;; Compare text between windows for Emacs.
;; Copyright (C) 1986 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.

;; BUGS: compare-windows-exactly should be ignored inside quotes.


(defvar compare-windows-exactly t
  "nil makes M-X compare-windows obey case-fold-search,
and skip spurious whitespace (see variable permitted-whitespace).")

(defvar permitted-whitespace '("[ \t\n]" . permit-whitespace-function)
  "CAR is regexp for whitespace characters recognized by permit-whitespace-p.
CDR is the function that permit-whitespace-p calls if there is no unquoted
whitespace (or beg/end of buffer) around point.")


(defun permit-whitespace-p ()
  "non-nil if I am sure whitespace may safely be inserted at point, without
changing the semantics of the buffer.  See variable permitted-whitespace."
  (or (bobp)
      (eobp)
      (save-excursion
	(forward-char 1)
	(if (re-search-backward (car permitted-whitespace) (- (point) 2) t)
	    (not (eq (preceding-char) ?\\))
	  (forward-char -1)
	  (funcall (cdr permitted-whitespace))))))

(defun permit-whitespace-function ()
  "non-nil if point is next to a bracket which is not escaped, or
at the beginning/end of something which may be both a word and a symbol."
  (if (zerop
       ;; Avoid multichar comments.
       ;; If someone wants to write a better test, please do!
       (lsh (logior (aref (syntax-table) (following-char))
		    (aref (syntax-table) (preceding-char))) -16))
      (let ((next-syntax (char-syntax (following-char)))
	    (prev-syntax (char-syntax (preceding-char)))
	    (pprev-syntax (char-syntax (char-after (- (point) 2)))))
	(cond ((eq prev-syntax ?\\)  nil)
	      ((memq next-syntax '(?\( ?\))))
	      ((eq pprev-syntax ?\\)  nil)
	      ((memq prev-syntax '(?\( ?\))))
	      ;; At words, I can't know whether user thinks in words or symbols
	      ((looking-at "\b")  (not (or (eq prev-syntax ?_)
					   (memq next-syntax '(?_ ?\\)))))))))


(defun compare-windows ()
  "Compare text in current window with text in next window.
Compares the text starting at point in each window,
moving over text in each one as far as they match.
See variables compare-windows-exactly and permitted-whitespace."
  (interactive)
  (let (p1 p2 maxp1 maxp2 b1 b2 s1 s2 w2
	   success size
	   (opoint (point)) opoint2
	   (moved t)
	   (temp-wh (concat "\\(" (car permitted-whitespace) "\\)+")))
    (setq p1 (point)
	  b1 (current-buffer))
    (setq w2 (next-window (selected-window)))
    (if (eq w2 (selected-window))
	(error "No other window."))
    (setq p2 (window-point w2)
	  b2 (window-buffer w2)
	  opoint2 p2)
    (setq maxp1 (point-max))
    (save-excursion
      (set-buffer b2)
      (setq maxp2 (point-max)))

    ;; Try advancing comparing 1000 chars at a time.
    ;; When that fails, go 500 chars at a time, and so on.
    (setq size 1000)
    (save-excursion
      (while moved
	(setq moved nil)
	(and (not compare-windows-exactly)
	     (permit-whitespace-p)
	     (save-excursion
	       (set-buffer b2)
	       (goto-char p2)
	       (if (permit-whitespace-p)
		   (progn (if (looking-at temp-wh)
			      (setq p2 (match-end 0)))
			  t)))
	     (if (looking-at temp-wh)
		 (goto-char (setq p1 (match-end 0)))))
	(while (> size 0)
	  (setq success t)
	  (while success
	    (setq size (min size (- maxp1 p1) (- maxp2 p2)))
	    (save-excursion
	      (set-buffer b2)
	      (setq s2 (buffer-substring p2 (+ size p2))))
	    (if compare-windows-exactly
		(setq s1 (buffer-substring p1 (+ size p1))
		      success (and (> size 0) (equal s1 s2)))
	      (setq success (search-forward s2 (+ (point) (length s2)) t)))
	    (if success
		(setq p1 (+ p1 size)
		      p2 (+ p2 size)
		      moved t)))
	  (setq size (/ size 2)))
	;; Try comparing 50 chars at a time if retrying after whitespace.
	(setq size 50)))

    (goto-char p1)
    (set-window-point w2 p2)
    (and (= (point) opoint)
	 (= p2 opoint2)
	 (ding))))