compare-w.el 17 KB

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