compare-w.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. ;;; compare-w.el --- compare text between windows for Emacs
  2. ;; Copyright (C) 1986, 1989, 1993, 1997, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  5. ;; Keywords: convenience files vc
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package provides one entry point, compare-windows. It compares
  19. ;; text starting from point in two adjacent windows, advancing point
  20. ;; until it finds a difference. Option variables permit you to ignore
  21. ;; whitespace differences, or case differences, or both.
  22. ;;; Code:
  23. (defgroup compare-windows nil
  24. "Compare text between windows."
  25. :prefix "compare-"
  26. :group 'tools)
  27. (defcustom compare-windows-whitespace "\\(\\s-\\|\n\\)+"
  28. "Regexp or function that defines whitespace sequences for `compare-windows'.
  29. That command optionally ignores changes in whitespace.
  30. The value of `compare-windows-whitespace' is normally a regexp, but it
  31. can also be a function. The function's job is to categorize any
  32. whitespace around (including before) point; it should also advance
  33. past any whitespace. The function is called in each window, with
  34. point at the current scanning point. It gets one argument, the point
  35. where \\[compare-windows] was originally called; it should not look at
  36. any text before that point.
  37. If the function returns the same value for both windows, then the
  38. whitespace is considered to match, and is skipped."
  39. :type '(choice regexp function)
  40. :group 'compare-windows)
  41. (defcustom compare-ignore-whitespace nil
  42. "Non-nil means `compare-windows' ignores whitespace."
  43. :type 'boolean
  44. :group 'compare-windows
  45. :version "22.1")
  46. (defcustom compare-ignore-case nil
  47. "Non-nil means `compare-windows' ignores case differences."
  48. :type 'boolean
  49. :group 'compare-windows)
  50. (defcustom compare-windows-sync 'compare-windows-sync-default-function
  51. "Function or regexp that is used to synchronize points in two
  52. windows if before calling `compare-windows' points are located
  53. on mismatched positions.
  54. The value of `compare-windows-sync' can be a function. The
  55. function's job is to advance points in both windows to the next
  56. matching text. If the value of `compare-windows-sync' is a
  57. regexp, then points in both windows are advanced to the next
  58. occurrence of this regexp.
  59. The current default value is the general function
  60. `compare-windows-sync-default-function' that is able to
  61. synchronize points by using quadratic algorithm to find the first
  62. matching 32-character string in two windows.
  63. The other useful values of this variable could be such functions
  64. as `forward-word', `forward-sentence', `forward-paragraph', or a
  65. regexp containing some field separator or a newline, depending on
  66. the nature of the difference units separator. The variable can
  67. be made buffer-local.
  68. If the value of this variable is `nil' (option \"No sync\"), then
  69. no synchronization is performed, and the function `ding' is called
  70. to beep or flash the screen when points are mismatched."
  71. :type '(choice function regexp (const :tag "No sync" nil))
  72. :group 'compare-windows
  73. :version "22.1")
  74. (defcustom compare-windows-sync-string-size 32
  75. "Size of string from one window that is searched in second window.
  76. Small number makes difference regions more fine-grained, but it
  77. may fail by finding the wrong match. The bigger number makes
  78. difference regions more coarse-grained.
  79. The default value 32 is good for the most cases."
  80. :type 'integer
  81. :group 'compare-windows
  82. :version "22.1")
  83. (defcustom compare-windows-recenter nil
  84. "List of two values, each of which is used as argument of
  85. function `recenter' called in each of two windows to place
  86. matching points side-by-side.
  87. The value `(-1 0)' is useful if windows are split vertically,
  88. and the value `((4) (4))' for horizontally split windows."
  89. :type '(list sexp sexp)
  90. :group 'compare-windows
  91. :version "22.1")
  92. (defcustom compare-windows-highlight t
  93. "Non-nil means compare-windows highlights the differences.
  94. The value t removes highlighting immediately after invoking a command
  95. other than `compare-windows'.
  96. The value `persistent' leaves all highlighted differences. You can clear
  97. out all highlighting later with the command `compare-windows-dehighlight'."
  98. :type '(choice (const :tag "No highlighting" nil)
  99. (const :tag "Persistent highlighting" persistent)
  100. (other :tag "Highlight until next command" t))
  101. :group 'compare-windows
  102. :version "22.1")
  103. (defface compare-windows
  104. '((t :inherit lazy-highlight))
  105. "Face for highlighting of compare-windows difference regions."
  106. :group 'compare-windows
  107. :version "22.1")
  108. (defvar compare-windows-overlay1 nil)
  109. (defvar compare-windows-overlay2 nil)
  110. (defvar compare-windows-overlays1 nil)
  111. (defvar compare-windows-overlays2 nil)
  112. (defvar compare-windows-sync-point nil)
  113. ;;;###autoload
  114. (defun compare-windows (ignore-whitespace)
  115. "Compare text in current window with text in next window.
  116. Compares the text starting at point in each window,
  117. moving over text in each one as far as they match.
  118. This command pushes the mark in each window
  119. at the prior location of point in that window.
  120. If both windows display the same buffer,
  121. the mark is pushed twice in that buffer:
  122. first in the other window, then in the selected window.
  123. A prefix arg means reverse the value of variable
  124. `compare-ignore-whitespace'. If `compare-ignore-whitespace' is
  125. nil, then a prefix arg means ignore changes in whitespace. If
  126. `compare-ignore-whitespace' is non-nil, then a prefix arg means
  127. don't ignore changes in whitespace. The variable
  128. `compare-windows-whitespace' controls how whitespace is skipped.
  129. If `compare-ignore-case' is non-nil, changes in case are also
  130. ignored.
  131. If `compare-windows-sync' is non-nil, then successive calls of
  132. this command work in interlaced mode:
  133. on first call it advances points to the next difference,
  134. on second call it synchronizes points by skipping the difference,
  135. on third call it again advances points to the next difference and so on."
  136. (interactive "P")
  137. (if compare-ignore-whitespace
  138. (setq ignore-whitespace (not ignore-whitespace)))
  139. (let* (p1 p2 maxp1 maxp2 b1 b2 w2
  140. (progress 1)
  141. (opoint1 (point))
  142. opoint2
  143. skip-func-1
  144. skip-func-2
  145. (sync-func (if (stringp compare-windows-sync)
  146. 'compare-windows-sync-regexp
  147. compare-windows-sync)))
  148. (setq p1 (point) b1 (current-buffer))
  149. (setq w2 (next-window (selected-window)))
  150. (if (eq w2 (selected-window))
  151. (setq w2 (next-window (selected-window) nil 'visible)))
  152. (if (eq w2 (selected-window))
  153. (error "No other window"))
  154. (setq p2 (window-point w2)
  155. b2 (window-buffer w2))
  156. (setq opoint2 p2)
  157. (setq maxp1 (point-max))
  158. (setq skip-func-1 (if ignore-whitespace
  159. (if (stringp compare-windows-whitespace)
  160. (lambda (pos)
  161. (compare-windows-skip-whitespace pos)
  162. t)
  163. compare-windows-whitespace)))
  164. (with-current-buffer b2
  165. (setq skip-func-2 (if ignore-whitespace
  166. (if (stringp compare-windows-whitespace)
  167. (lambda (pos)
  168. (compare-windows-skip-whitespace pos)
  169. t)
  170. compare-windows-whitespace)))
  171. (push-mark p2 t)
  172. (setq maxp2 (point-max)))
  173. (push-mark)
  174. (while (> progress 0)
  175. ;; If both windows have whitespace next to point,
  176. ;; optionally skip over it.
  177. (and skip-func-1
  178. (save-excursion
  179. (let (p1a p2a w1 w2 result1 result2)
  180. (setq result1 (funcall skip-func-1 opoint1))
  181. (setq p1a (point))
  182. (set-buffer b2)
  183. (goto-char p2)
  184. (setq result2 (funcall skip-func-2 opoint2))
  185. (setq p2a (point))
  186. (if (and result1 result2 (eq result1 result2))
  187. (setq p1 p1a
  188. p2 p2a)))))
  189. (let ((size (min (- maxp1 p1) (- maxp2 p2)))
  190. (case-fold-search compare-ignore-case))
  191. (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
  192. b1 p1 (+ size p1)))
  193. (setq progress (if (zerop progress) size (1- (abs progress))))
  194. (setq p1 (+ p1 progress) p2 (+ p2 progress)))
  195. ;; Advance point now rather than later, in case we're interrupted.
  196. (goto-char p1)
  197. (set-window-point w2 p2)
  198. (when compare-windows-recenter
  199. (recenter (car compare-windows-recenter))
  200. (with-selected-window w2 (recenter (cadr compare-windows-recenter)))))
  201. (if (= (point) opoint1)
  202. (if (not sync-func)
  203. (ding)
  204. ;; If points are not advanced (i.e. already on mismatch position),
  205. ;; then synchronize points between both windows
  206. (save-excursion
  207. (setq compare-windows-sync-point nil)
  208. (funcall sync-func)
  209. (setq p1 (point))
  210. (set-buffer b2)
  211. (goto-char p2)
  212. (funcall sync-func)
  213. (setq p2 (point)))
  214. (goto-char p1)
  215. (set-window-point w2 p2)
  216. (when compare-windows-recenter
  217. (recenter (car compare-windows-recenter))
  218. (with-selected-window w2 (recenter (cadr compare-windows-recenter))))
  219. ;; If points are still not synchronized, then ding
  220. (when (and (= p1 opoint1) (= p2 opoint2))
  221. ;; Display error message when current points in two windows
  222. ;; are unmatched and next matching points can't be found.
  223. (compare-windows-dehighlight)
  224. (ding)
  225. (message "No more matching points"))))))
  226. ;; Move forward over whatever might be called whitespace.
  227. ;; compare-windows-whitespace is a regexp that matches whitespace.
  228. ;; Match it at various starting points before the original point
  229. ;; and find the latest point at which a match ends.
  230. ;; Don't try starting points before START, though.
  231. ;; Value is non-nil if whitespace is found.
  232. ;; If there is whitespace before point, but none after,
  233. ;; then return t, but don't advance point.
  234. (defun compare-windows-skip-whitespace (start)
  235. (let ((end (point))
  236. (beg (point))
  237. (opoint (point)))
  238. (while (or (and (looking-at compare-windows-whitespace)
  239. (<= end (match-end 0))
  240. ;; This match goes past END, so advance END.
  241. (progn (setq end (match-end 0))
  242. (> (point) start)))
  243. (and (/= (point) start)
  244. ;; Consider at least the char before point,
  245. ;; unless it is also before START.
  246. (= (point) opoint)))
  247. ;; keep going back until whitespace
  248. ;; doesn't extend to or past end
  249. (forward-char -1))
  250. (setq beg (point))
  251. (goto-char end)
  252. (or (/= beg opoint)
  253. (/= end opoint))))
  254. ;; Move forward to the next synchronization regexp.
  255. (defun compare-windows-sync-regexp ()
  256. (if (stringp compare-windows-sync)
  257. (re-search-forward compare-windows-sync nil t)))
  258. ;; Function works in two passes: one call on each window.
  259. ;; On the first call both matching points are computed,
  260. ;; and one of them is stored in compare-windows-sync-point
  261. ;; to be used when this function is called on second window.
  262. (defun compare-windows-sync-default-function ()
  263. (if (not compare-windows-sync-point)
  264. (let* ((w1 (selected-window))
  265. (w2 (next-window w1))
  266. (b2 (window-buffer w2))
  267. (point-max2 (with-current-buffer b2 (point-max)))
  268. (op2 (window-point w2))
  269. (op1 (point))
  270. (region-size compare-windows-sync-string-size)
  271. (string-size compare-windows-sync-string-size)
  272. in-bounds-p s1 p2 p12s p12)
  273. (while (and
  274. ;; until matching points are found
  275. (not p12s)
  276. ;; until size exceeds the maximum points of both buffers
  277. ;; (bounds below take care to not overdo in each of them)
  278. (or (setq in-bounds-p (< region-size (max (- (point-max) op1)
  279. (- point-max2 op2))))
  280. ;; until string size becomes smaller than 4
  281. (> string-size 4)))
  282. (if in-bounds-p
  283. ;; make the next search in the double-sized region;
  284. ;; on first iteration it is 2*compare-windows-sync-string-size,
  285. ;; on last iterations it exceeds both buffers maximum points
  286. (setq region-size (* region-size 2))
  287. ;; if region size exceeds the maximum points of both buffers,
  288. ;; then start to halve the string size until 4;
  289. ;; this helps to find differences near the end of buffers
  290. (setq string-size (/ string-size 2)))
  291. (let ((p1 op1)
  292. (bound1 (- (min (+ op1 region-size) (point-max)) string-size))
  293. (bound2 (min (+ op2 region-size) point-max2)))
  294. (while (< p1 bound1)
  295. (setq s1 (buffer-substring-no-properties p1 (+ p1 string-size)))
  296. (setq p2 (with-current-buffer b2
  297. (goto-char op2)
  298. (let ((case-fold-search compare-ignore-case))
  299. (search-forward s1 bound2 t))))
  300. (when p2
  301. (setq p2 (- p2 string-size))
  302. (setq p12s (cons (list (+ p1 p2) p1 p2) p12s)))
  303. (setq p1 (1+ p1)))))
  304. (when p12s
  305. ;; use closest matching points (i.e. points with minimal sum)
  306. (setq p12 (cdr (assq (apply 'min (mapcar 'car p12s)) p12s)))
  307. (goto-char (car p12))
  308. (compare-windows-highlight op1 (car p12) (current-buffer) w1
  309. op2 (cadr p12) b2 w2))
  310. (setq compare-windows-sync-point (or (cadr p12) t)))
  311. ;; else set point in the second window to the pre-calculated value
  312. (if (numberp compare-windows-sync-point)
  313. (goto-char compare-windows-sync-point))
  314. (setq compare-windows-sync-point nil)))
  315. ;; Highlight differences
  316. (defun compare-windows-highlight (beg1 end1 b1 w1 beg2 end2 b2 w2)
  317. (when compare-windows-highlight
  318. (if compare-windows-overlay1
  319. (move-overlay compare-windows-overlay1 beg1 end1 b1)
  320. (setq compare-windows-overlay1 (make-overlay beg1 end1 b1))
  321. (overlay-put compare-windows-overlay1 'face 'compare-windows)
  322. (overlay-put compare-windows-overlay1 'priority 1000))
  323. (overlay-put compare-windows-overlay1 'window w1)
  324. (if compare-windows-overlay2
  325. (move-overlay compare-windows-overlay2 beg2 end2 b2)
  326. (setq compare-windows-overlay2 (make-overlay beg2 end2 b2))
  327. (overlay-put compare-windows-overlay2 'face 'compare-windows)
  328. (overlay-put compare-windows-overlay2 'priority 1000))
  329. (overlay-put compare-windows-overlay2 'window w2)
  330. (if (not (eq compare-windows-highlight 'persistent))
  331. ;; Remove highlighting before next command is executed
  332. (add-hook 'pre-command-hook 'compare-windows-dehighlight)
  333. (when compare-windows-overlay1
  334. (push (copy-overlay compare-windows-overlay1) compare-windows-overlays1)
  335. (delete-overlay compare-windows-overlay1))
  336. (when compare-windows-overlay2
  337. (push (copy-overlay compare-windows-overlay2) compare-windows-overlays2)
  338. (delete-overlay compare-windows-overlay2)))))
  339. (defun compare-windows-dehighlight ()
  340. "Remove highlighting created by `compare-windows-highlight'."
  341. (interactive)
  342. (remove-hook 'pre-command-hook 'compare-windows-dehighlight)
  343. (mapc 'delete-overlay compare-windows-overlays1)
  344. (mapc 'delete-overlay compare-windows-overlays2)
  345. (and compare-windows-overlay1 (delete-overlay compare-windows-overlay1))
  346. (and compare-windows-overlay2 (delete-overlay compare-windows-overlay2)))
  347. (provide 'compare-w)
  348. ;;; compare-w.el ends here