hilit-chg.el 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. ;;; hilit-chg.el --- minor mode displaying buffer changes with special face
  2. ;; Copyright (C) 1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Richard Sharman <rsharman@pobox.com>
  4. ;; Keywords: faces
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; A minor mode: "Highlight Changes mode".
  18. ;; When Highlight Changes mode is enabled changes to the buffer are
  19. ;; recorded with a text property. Normally these ranges of text are
  20. ;; displayed in a distinctive face. However, sometimes it is
  21. ;; desirable to temporarily not see these changes. Instead of
  22. ;; disabling Highlight Changes mode (which would remove the text property)
  23. ;; use the command highlight-changes-visible-mode.
  24. ;; Two faces are supported: one for changed or inserted text and
  25. ;; another for the first character after text has been deleted.
  26. ;; When Highlight Changes mode is on (even if changes are not visible)
  27. ;; you can go to the next or previous change with
  28. ;; `highlight-changes-next-change' or `highlight-changes-previous-change'.
  29. ;; Command highlight-compare-with-file shows changes in this file
  30. ;; compared with another file (by default the previous version of the
  31. ;; file).
  32. ;;
  33. ;; The command highlight-compare-buffers compares two buffers by
  34. ;; highlighting their differences.
  35. ;; You can "age" different sets of changes by using
  36. ;; `highlight-changes-rotate-faces'. This rotates through a series
  37. ;; of different faces, so you can distinguish "new" changes from "older"
  38. ;; changes. You can customize these "rotated" faces in two ways. You can
  39. ;; either explicitly define each face by customizing
  40. ;; `highlight-changes-face-list'. If, however, the faces differ from
  41. ;; `highlight-changes-face' only in the foreground color, you can simply set
  42. ;; `highlight-changes-colors'. If `highlight-changes-face-list' is nil when
  43. ;; the faces are required they will be constructed from
  44. ;; `highlight-changes-colors'.
  45. ;; You can automatically rotate faces when the buffer is saved;
  46. ;; see function `highlight-changes-rotate-faces' for how to do this.
  47. ;; The hook `highlight-changes-mode-hook' is called when
  48. ;; Highlight Changes mode is turned on or off.
  49. ;; When it called, variable `highlight-changes-mode' has been updated
  50. ;; to the new value.
  51. ;;
  52. ;; Example usage:
  53. ;; (defun my-highlight-changes-mode-hook ()
  54. ;; (if highlight-changes-mode
  55. ;; (add-hook 'write-file-functions 'highlight-changes-rotate-faces nil t)
  56. ;; (remove-hook 'write-file-functions 'highlight-changes-rotate-faces t)
  57. ;; ))
  58. ;; Automatically enabling Highlight Changes mode
  59. ;;
  60. ;; Normally, Highlight Changes mode is turned on explicitly in a buffer.
  61. ;;
  62. ;; If you prefer to have it automatically invoked you can do it as
  63. ;; follows.
  64. ;; 1. Most modes have a major-hook, typically called MODE-hook. You
  65. ;; can use `add-hook' to call `highlight-changes-mode'.
  66. ;; Example:
  67. ;; (add-hook 'c-mode-hook 'highlight-changes-mode)
  68. ;; However, this cannot be done for Fundamental mode for there is no
  69. ;; such hook.
  70. ;; 2. You can use the function `global-highlight-changes-mode'
  71. ;;
  72. ;; This function, which is fashioned after the way `global-font-lock' works,
  73. ;; toggles on or off global Highlight Changes mode. When activated, it turns
  74. ;; on Highlight Changes mode in all "suitable" existing buffers and will turn
  75. ;; it on in new "suitable" buffers to be created.
  76. ;; A buffer's "suitability" is determined by variable
  77. ;; `highlight-changes-global-modes', as follows. If it is
  78. ;; * nil -- then no buffers are suitable;
  79. ;; * a function -- this function is called and the result is used. As
  80. ;; an example, if the value is `buffer-file-name' then all buffers
  81. ;; who are visiting files are suitable, but others (like dired
  82. ;; buffers) are not;
  83. ;; * a list -- then the buffer is suitable if and only if its mode is in the
  84. ;; list, except if the first element is `not', in which case the test
  85. ;; is reversed (i.e. it is a list of unsuitable modes).
  86. ;; * Otherwise, the buffer is suitable if its name does not begin with
  87. ;; ` ' or `*' and if `buffer-file-name' returns true.
  88. ;; To enable it for future sessions put this in your ~/.emacs file:
  89. ;; (global-highlight-changes-mode t)
  90. ;; Possible bindings:
  91. ;; (global-set-key '[C-right] 'highlight-changes-next-change)
  92. ;; (global-set-key '[C-left] 'highlight-changes-previous-change)
  93. ;;
  94. ;; Other interactive functions (that could be bound if desired):
  95. ;; highlight-changes-mode
  96. ;; highlight-changes-toggle-visibility
  97. ;; highlight-changes-remove-highlight
  98. ;; highlight-compare-with-file
  99. ;; highlight-compare-buffers
  100. ;; highlight-changes-rotate-faces
  101. ;;; Bugs:
  102. ;; - the next-change and previous-change functions are too literal;
  103. ;; they should find the next "real" change, in other words treat
  104. ;; consecutive changes as one.
  105. ;;; To do (maybe), notes, ...
  106. ;; - having different faces for deletion and non-deletion: is it
  107. ;; really worth the hassle?
  108. ;; - highlight-compare-with-file should allow RCS files - e.g. nice to be
  109. ;; able to say show changes compared with version 2.1.
  110. ;;; History:
  111. ;; R Sharman (rsharman@pobox.com) Feb 1998:
  112. ;; - initial release as change-mode.
  113. ;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998
  114. ;; - fixes for byte compile errors
  115. ;; - use eval-and-compile for autoload
  116. ;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98
  117. ;; - suggested turning it on by default
  118. ;; Eric Ludlam <zappo@gnu.org> Suggested using overlays.
  119. ;; July 98
  120. ;; - global mode and various stuff added
  121. ;; - Changed to use overlays
  122. ;; August 98
  123. ;; - renamed to Highlight Changes mode.
  124. ;; Dec 2003
  125. ;; - Use require for ediff stuff
  126. ;; - Added highlight-compare-buffers
  127. ;; Mar 2008
  128. ;; - Made highlight-changes-mode like other modes (toggle on/off)
  129. ;; - Added new command highlight-changes-visible-mode to replace the
  130. ;; previous active/passive aspect of highlight-changes-mode.
  131. ;; - Removed highlight-changes-toggle-hook
  132. ;; - Put back eval-and-compile inadvertently dropped
  133. ;; May 2008
  134. ;; - Removed highlight-changes-disable-hook and highlight-changes-enable-hook
  135. ;; because highlight-changes-mode-hook can do both.
  136. ;;; Code:
  137. (require 'wid-edit)
  138. ;; ====================== Customization =======================
  139. (defgroup highlight-changes nil
  140. "Highlight Changes mode."
  141. :version "20.4"
  142. :group 'faces)
  143. ;; Face information: How the changes appear.
  144. ;; Defaults for face: red foreground, no change to background,
  145. ;; and underlined if a change is because of a deletion.
  146. ;; Note: underlining is helpful in that it shows up changes in white space.
  147. ;; However, having it set for non-delete changes can be annoying because all
  148. ;; indentation on inserts gets underlined (which can look pretty ugly!).
  149. (defface highlight-changes
  150. '((((min-colors 88) (class color)) (:foreground "red1"))
  151. (((class color)) (:foreground "red" ))
  152. (t (:inverse-video t)))
  153. "Face used for highlighting changes."
  154. :group 'highlight-changes)
  155. (define-obsolete-face-alias 'highlight-changes-face
  156. 'highlight-changes "22.1")
  157. ;; This looks pretty ugly, actually. Maybe the underline should be removed.
  158. (defface highlight-changes-delete
  159. '((((min-colors 88) (class color)) (:foreground "red1" :underline t))
  160. (((class color)) (:foreground "red" :underline t))
  161. (t (:inverse-video t)))
  162. "Face used for highlighting deletions."
  163. :group 'highlight-changes)
  164. (define-obsolete-face-alias 'highlight-changes-delete-face
  165. 'highlight-changes-delete "22.1")
  166. ;; A (not very good) default list of colors to rotate through.
  167. (define-obsolete-variable-alias 'highlight-changes-colours
  168. 'highlight-changes-colors "22.1")
  169. (defcustom highlight-changes-colors
  170. (if (eq (frame-parameter nil 'background-mode) 'light)
  171. ;; defaults for light background:
  172. '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue")
  173. ;; defaults for dark background:
  174. '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid"))
  175. "Colors used by `highlight-changes-rotate-faces'.
  176. The newest rotated change will be displayed in the first element of this list,
  177. the next older will be in the second element etc.
  178. This list is used if `highlight-changes-face-list' is nil, otherwise that
  179. variable overrides this list. If you only care about foreground
  180. colors then use this, if you want fancier faces then set
  181. `highlight-changes-face-list'."
  182. :type '(repeat color)
  183. :group 'highlight-changes)
  184. ;; When you invoke highlight-changes-mode, should highlight-changes-visible-mode
  185. ;; be on or off?
  186. (define-obsolete-variable-alias 'highlight-changes-initial-state
  187. 'highlight-changes-visibility-initial-state "23.1")
  188. (defcustom highlight-changes-visibility-initial-state t
  189. "Controls whether changes are initially visible in Highlight Changes mode.
  190. This controls the initial value of `highlight-changes-visible-mode'.
  191. When a buffer is in Highlight Changes mode the function
  192. `highlight-changes-visible-mode' is used to toggle the mode on or off."
  193. :type 'boolean
  194. :group 'highlight-changes)
  195. ;; highlight-changes-global-initial-state has been removed
  196. ;; These are the strings displayed in the mode-line for the minor mode:
  197. (define-obsolete-variable-alias 'highlight-changes-active-string
  198. 'highlight-changes-visible-string "23.1")
  199. (defcustom highlight-changes-visible-string " +Chg"
  200. "The string used when in Highlight Changes mode and changes are visible.
  201. This should be set to nil if no indication is desired, or to
  202. a string with a leading space."
  203. :type '(choice string
  204. (const :tag "None" nil))
  205. :group 'highlight-changes)
  206. (define-obsolete-variable-alias 'highlight-changes-passive-string
  207. 'highlight-changes-invisible-string "23.1")
  208. (defcustom highlight-changes-invisible-string " -Chg"
  209. "The string used when in Highlight Changes mode and changes are hidden.
  210. This should be set to nil if no indication is desired, or to
  211. a string with a leading space."
  212. :type '(choice string
  213. (const :tag "None" nil))
  214. :group 'highlight-changes)
  215. (defcustom highlight-changes-global-modes t
  216. "Determine whether a buffer is suitable for global Highlight Changes mode.
  217. A function means call that function to decide: if it returns non-nil,
  218. the buffer is suitable.
  219. A list means the elements are major modes suitable for Highlight
  220. Changes mode, or a list whose first element is `not' followed by major
  221. modes which are not suitable.
  222. A value of t means the buffer is suitable if it is visiting a file and
  223. its name does not begin with ` ' or `*'.
  224. A value of nil means no buffers are suitable for `global-highlight-changes-mode'
  225. \(effectively disabling the mode).
  226. Example:
  227. (c-mode c++-mode)
  228. means that Highlight Changes mode is turned on for buffers in C and C++
  229. modes only."
  230. :type '(choice
  231. (const :tag "all non-special buffers visiting files" t)
  232. (set :menu-tag "specific modes" :tag "modes"
  233. :value (not)
  234. (const :tag "All except these" not)
  235. (repeat :tag "Modes" :inline t (symbol :tag "mode")))
  236. (function :menu-tag "determined by function"
  237. :value buffer-file-name)
  238. (const :tag "none" nil)
  239. )
  240. :group 'highlight-changes)
  241. (defcustom highlight-changes-global-changes-existing-buffers nil
  242. "If non-nil, toggling global Highlight Changes mode affects existing buffers.
  243. Normally, `global-highlight-changes' affects only new buffers (to be
  244. created). However, if `highlight-changes-global-changes-existing-buffers'
  245. is non-nil, then turning on `global-highlight-changes' will turn on
  246. Highlight Changes mode in suitable buffers, and turning the mode off will
  247. remove it from existing buffers."
  248. :type 'boolean
  249. :group 'highlight-changes)
  250. ;; These are for internal use.
  251. (defvar hilit-chg-list nil)
  252. (defvar hilit-chg-string " ??")
  253. (make-variable-buffer-local 'hilit-chg-string)
  254. ;;; Functions...
  255. ;;;###autoload
  256. (define-minor-mode highlight-changes-mode
  257. "Toggle highlighting changes in this buffer (Highlight Changes mode).
  258. With a prefix argument ARG, enable Highlight Changes mode if ARG
  259. is positive, and disable it otherwise. If called from Lisp,
  260. enable the mode if ARG is omitted or nil.
  261. When Highlight Changes is enabled, changes are marked with a text
  262. property. Normally they are displayed in a distinctive face, but
  263. command \\[highlight-changes-visible-mode] can be used to toggles
  264. this on and off.
  265. Other functions for buffers in this mode include:
  266. \\[highlight-changes-next-change] - move point to beginning of next change
  267. \\[highlight-changes-previous-change] - move to beginning of previous change
  268. \\[highlight-changes-remove-highlight] - remove the change face from the region
  269. \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes
  270. through various faces.
  271. \\[highlight-compare-with-file] - mark text as changed by comparing this
  272. buffer with the contents of a file
  273. \\[highlight-compare-buffers] highlights differences between two buffers."
  274. nil ;; init-value
  275. hilit-chg-string ;; lighter
  276. nil ;; keymap
  277. (if (or (display-color-p)
  278. (and (fboundp 'x-display-grayscale-p) (x-display-grayscale-p)))
  279. (progn
  280. (if (and (eq this-command 'global-highlight-changes-mode)
  281. (not highlight-changes-global-changes-existing-buffers))
  282. ;; The global mode has toggled the value of the mode variable,
  283. ;; but not other changes have been mode, so we are safe
  284. ;; to retoggle it.
  285. (setq highlight-changes-mode (not highlight-changes-mode)))
  286. (if highlight-changes-mode
  287. ;; it is being turned on
  288. (hilit-chg-set)
  289. ;; mode is turned off
  290. (hilit-chg-clear)))
  291. (message "Highlight Changes mode requires color or grayscale display")))
  292. ;;;###autoload
  293. (define-minor-mode highlight-changes-visible-mode
  294. "Toggle visibility of highlighting due to Highlight Changes mode.
  295. With a prefix argument ARG, enable Highlight Changes Visible mode
  296. if ARG is positive, and disable it otherwise. If called from
  297. Lisp, enable the mode if ARG is omitted or nil.
  298. Highlight Changes Visible mode only has an effect when Highlight
  299. Changes mode is on. When enabled, the changed text is displayed
  300. in a distinctive face.
  301. The default value can be customized with variable
  302. `highlight-changes-visibility-initial-state'.
  303. This command does not itself set highlight-changes mode."
  304. t ;; init-value
  305. nil ;; lighter
  306. nil ;; keymap
  307. (hilit-chg-update)
  308. )
  309. (defun hilit-chg-cust-fix-changes-face-list (w _wc &optional event)
  310. ;; When customization function `highlight-changes-face-list' inserts a new
  311. ;; face it uses the default face. We don't want the user to modify this
  312. ;; face, so we rename the faces in the list on an insert. The rename is
  313. ;; actually done by copying the faces so user-defined faces still remain
  314. ;; in the same order.
  315. ;; The notifying the parent is needed because without it changes to the
  316. ;; faces are saved but not to the actual list itself.
  317. (let ((old-list (widget-value w)))
  318. (if (member 'default old-list)
  319. (let
  320. ((p (reverse old-list))
  321. (n (length old-list))
  322. new-name old-name
  323. (new-list nil)
  324. )
  325. (while p
  326. (setq old-name (car p))
  327. (setq new-name (intern (format "highlight-changes-%d" n)))
  328. (if (eq old-name new-name)
  329. nil
  330. ;; A new face has been inserted: we don't want to modify the
  331. ;; default face so copy it. Better, though, (I think) is to
  332. ;; make a new face have the same attributes as
  333. ;; the `highlight-changes' face.
  334. (if (eq old-name 'default)
  335. (copy-face 'highlight-changes new-name)
  336. (copy-face old-name new-name)
  337. ))
  338. (setq new-list (append (list new-name) new-list))
  339. (setq n (1- n))
  340. (setq p (cdr p)))
  341. (if (equal new-list (widget-value w))
  342. nil ;; (message "notify: no change!")
  343. (widget-value-set w new-list)
  344. (widget-setup)
  345. )
  346. )
  347. ;; (message "notify: no default here!")
  348. ))
  349. (let ((parent (widget-get w :parent)))
  350. (when parent
  351. (widget-apply parent :notify w event))))
  352. (defcustom highlight-changes-face-list nil
  353. "A list of faces used when rotating changes.
  354. Normally the variable is initialized to nil and the list is created from
  355. `highlight-changes-colors' when needed. However, you can set this variable
  356. to any list of faces. You will have to do this if you want faces which
  357. don't just differ from the `highlight-changes' face by the foreground color.
  358. Otherwise, this list will be constructed when needed from
  359. `highlight-changes-colors'."
  360. :type '(choice
  361. (repeat
  362. :notify hilit-chg-cust-fix-changes-face-list
  363. face )
  364. (const :tag "Derive from highlight-changes-colors" nil)
  365. )
  366. :group 'highlight-changes)
  367. (defun hilit-chg-map-changes (func &optional start-position end-position)
  368. "Call function FUNC for each region used by Highlight Changes mode.
  369. If START-POSITION is nil, (point-min) is used.
  370. If END-POSITION is nil, (point-max) is used.
  371. FUNC is called with 3 params: PROPERTY START STOP."
  372. (let ((start (or start-position (point-min)))
  373. (limit (or end-position (point-max)))
  374. prop end)
  375. (while (and start (< start limit))
  376. (setq prop (get-text-property start 'hilit-chg))
  377. (setq end (text-property-not-all start limit 'hilit-chg prop))
  378. (if prop
  379. (funcall func prop start (or end limit)))
  380. (setq start end))))
  381. (defun hilit-chg-display-changes (&optional beg end)
  382. "Display face information for Highlight Changes mode.
  383. An overlay from BEG to END containing a change face is added from the
  384. information in the text property of type `hilit-chg'.
  385. This is the opposite of `hilit-chg-hide-changes'."
  386. (hilit-chg-map-changes 'hilit-chg-make-ov beg end))
  387. (defun hilit-chg-make-ov (prop start end)
  388. (or prop
  389. (error "hilit-chg-make-ov: prop is nil"))
  390. ;; For the region create overlays with a distinctive face
  391. ;; and the text property 'hilit-chg.
  392. (let ((ov (make-overlay start end))
  393. (face (if (eq prop 'hilit-chg-delete)
  394. 'highlight-changes-delete
  395. (nth 1 (member prop hilit-chg-list)))))
  396. (if face
  397. (progn
  398. ;; We must mark the face, that is the purpose of the overlay.
  399. (overlay-put ov 'face face)
  400. ;; I don't think we need to set evaporate since we should
  401. ;; be controlling them!
  402. (overlay-put ov 'evaporate t)
  403. ;; We set the change property so we can tell this is one
  404. ;; of our overlays (so we don't delete someone else's).
  405. (overlay-put ov 'hilit-chg t)
  406. )
  407. (error "hilit-chg-make-ov: no face for prop: %s" prop))))
  408. (defun hilit-chg-hide-changes (&optional beg end)
  409. "Remove face information for Highlight Changes mode.
  410. The overlay containing the face is removed, but the text property
  411. containing the change information is retained.
  412. This is the opposite of `hilit-chg-display-changes'."
  413. (let ((start (or beg (point-min)))
  414. (limit (or end (point-max))))
  415. (dolist (p (overlays-in start limit))
  416. ;; don't delete the overlay if it isn't ours!
  417. (if (overlay-get p 'hilit-chg)
  418. (delete-overlay p)))))
  419. (defun hilit-chg-fixup (beg end)
  420. "Fix change overlays in region between BEG and END.
  421. Ensure the overlays agree with the changes as determined from
  422. the text properties of type `hilit-chg'."
  423. ;; Remove or alter overlays in region beg..end
  424. (remove-overlays beg end 'hilit-chg t)
  425. (hilit-chg-display-changes beg end))
  426. ;; Inspired by font-lock. Something like this should be moved to subr.el.
  427. (defmacro highlight-save-buffer-state (&rest body)
  428. "Bind variables according to VARLIST and eval BODY restoring buffer state."
  429. (declare (indent 0) (debug t))
  430. (let ((modified (make-symbol "modified")))
  431. `(let* ((,modified (buffer-modified-p))
  432. (inhibit-modification-hooks t)
  433. deactivate-mark
  434. ;; So we don't check the file's mtime.
  435. buffer-file-name
  436. buffer-file-truename)
  437. (progn
  438. ,@body)
  439. (unless ,modified
  440. (restore-buffer-modified-p nil)))))
  441. ;;;###autoload
  442. (defun highlight-changes-remove-highlight (beg end)
  443. "Remove the change face from the region between BEG and END.
  444. This allows you to manually remove highlighting from uninteresting changes."
  445. (interactive "r")
  446. (highlight-save-buffer-state
  447. (remove-text-properties beg end '(hilit-chg nil))
  448. (hilit-chg-fixup beg end)))
  449. (defun hilit-chg-set-face-on-change (beg end leng-before
  450. &optional no-property-change)
  451. "Record changes and optionally display them in a distinctive face.
  452. `hilit-chg-set' adds this function to the `after-change-functions' hook."
  453. ;;
  454. ;; This function is called by the `after-change-functions' hook, which
  455. ;; is how we are notified when text is changed.
  456. ;; It is also called from `highlight-compare-with-file'.
  457. ;;
  458. ;; We do NOT want to simply do this if this is an undo command, because
  459. ;; otherwise an undone change shows up as changed. While the properties
  460. ;; are automatically restored by undo, we must fix up the overlay.
  461. (save-match-data
  462. (let (;;(beg-decr 1)
  463. (end-incr 1)
  464. (type 'hilit-chg))
  465. (if undo-in-progress
  466. (if (and highlight-changes-mode
  467. highlight-changes-visible-mode)
  468. (hilit-chg-fixup beg end))
  469. (highlight-save-buffer-state
  470. (if (and (= beg end) (> leng-before 0))
  471. ;; deletion
  472. (progn
  473. ;; The eolp and bolp tests are a kludge! But they prevent
  474. ;; rather nasty looking displays when deleting text at the end
  475. ;; of line, such as normal corrections as one is typing and
  476. ;; immediately makes a correction, and when deleting first
  477. ;; character of a line.
  478. ;; (if (= leng-before 1)
  479. ;; (if (eolp)
  480. ;; (setq beg-decr 0 end-incr 0)
  481. ;; (if (bolp)
  482. ;; (setq beg-decr 0))))
  483. ;; (setq beg (max (- beg beg-decr) (point-min)))
  484. (setq end (min (+ end end-incr) (point-max)))
  485. (setq type 'hilit-chg-delete))
  486. ;; Not a deletion.
  487. ;; Most of the time the following is not necessary, but
  488. ;; if the current text was marked as a deletion then
  489. ;; the old overlay is still in effect, so if we add some
  490. ;; text then remove the deletion marking, but set it to
  491. ;; changed otherwise its highlighting disappears.
  492. (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete)
  493. (progn
  494. (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg)
  495. (if highlight-changes-visible-mode
  496. (hilit-chg-fixup beg (+ end 1))))))
  497. (unless no-property-change
  498. (put-text-property beg end 'hilit-chg type))
  499. (if (or highlight-changes-visible-mode no-property-change)
  500. (hilit-chg-make-ov type beg end)))))))
  501. (defun hilit-chg-update ()
  502. "Update a buffer's highlight changes when visibility changed."
  503. (if highlight-changes-visible-mode
  504. ;; changes are visible
  505. (progn
  506. (setq hilit-chg-string highlight-changes-visible-string)
  507. (or buffer-read-only
  508. (hilit-chg-display-changes)))
  509. ;; changes are invisible
  510. (setq hilit-chg-string highlight-changes-invisible-string)
  511. (or buffer-read-only
  512. (hilit-chg-hide-changes))))
  513. (defun hilit-chg-set ()
  514. "Turn on Highlight Changes mode for this buffer."
  515. (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
  516. (hilit-chg-make-list)
  517. (setq highlight-changes-mode t)
  518. (setq highlight-changes-visible-mode highlight-changes-visibility-initial-state)
  519. (hilit-chg-update)
  520. (force-mode-line-update)
  521. (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t))
  522. (defun hilit-chg-clear ()
  523. "Remove Highlight Changes mode for this buffer.
  524. This removes all saved change information."
  525. (if buffer-read-only
  526. ;; We print the buffer name because this function could be called
  527. ;; on many buffers from `global-highlight-changes-mode'.
  528. (message "Cannot remove highlighting from read-only mode buffer %s"
  529. (buffer-name))
  530. (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t)
  531. (highlight-save-buffer-state
  532. (hilit-chg-hide-changes)
  533. (hilit-chg-map-changes
  534. (lambda (_prop start stop)
  535. (remove-text-properties start stop '(hilit-chg nil)))))
  536. (setq highlight-changes-mode nil)
  537. (force-mode-line-update)))
  538. ;;;###autoload
  539. (defun highlight-changes-next-change ()
  540. "Move to the beginning of the next change, if in Highlight Changes mode."
  541. (interactive)
  542. (if highlight-changes-mode
  543. (let ((start (point))
  544. prop)
  545. (setq prop (get-text-property (point) 'hilit-chg))
  546. (if prop
  547. ;; we are in a change
  548. (setq start (next-single-property-change (point) 'hilit-chg)))
  549. (if start
  550. (setq start (next-single-property-change start 'hilit-chg)))
  551. (if start
  552. (goto-char start)
  553. (message "no next change")))
  554. (message "This buffer is not in Highlight Changes mode.")))
  555. ;;;###autoload
  556. (defun highlight-changes-previous-change ()
  557. "Move to the beginning of the previous change, if in Highlight Changes mode."
  558. (interactive)
  559. (if highlight-changes-mode
  560. (let ( (start (point)) (prop nil) )
  561. (or (bobp)
  562. (setq prop (get-text-property (1- (point)) 'hilit-chg)))
  563. (if prop
  564. ;; we are in a change
  565. (setq start (previous-single-property-change (point) 'hilit-chg)))
  566. (if start
  567. (setq start (previous-single-property-change start 'hilit-chg)))
  568. ;; special handling for the case where (point-min) is a change
  569. (if start
  570. (setq start (or (previous-single-property-change start 'hilit-chg)
  571. (if (get-text-property (point-min) 'hilit-chg)
  572. (point-min)))))
  573. (if start
  574. (goto-char start)
  575. (message "no previous change")))
  576. (message "This buffer is not in Highlight Changes mode.")))
  577. ;; ========================================================================
  578. (defun hilit-chg-make-list (&optional force)
  579. "Construct `hilit-chg-list' and `highlight-changes-face-list'."
  580. ;; Constructs highlight-changes-face-list if necessary,
  581. ;; and hilit-chg-list always:
  582. ;; Maybe this should always be called when rotating a face
  583. ;; so we pick up any changes?
  584. (if (or (null highlight-changes-face-list) ; Don't do it if it
  585. force) ; already exists unless FORCE non-nil.
  586. (let ((p highlight-changes-colors)
  587. (n 1) name)
  588. (setq highlight-changes-face-list nil)
  589. (while p
  590. (setq name (intern (format "highlight-changes-%d" n)))
  591. (copy-face 'highlight-changes name)
  592. (set-face-foreground name (car p))
  593. (setq highlight-changes-face-list
  594. (append highlight-changes-face-list (list name)))
  595. (setq p (cdr p))
  596. (setq n (1+ n)))))
  597. (setq hilit-chg-list (list 'hilit-chg 'highlight-changes))
  598. (let ((p highlight-changes-face-list)
  599. (n 1)
  600. last-category last-face)
  601. (while p
  602. (setq last-category (intern (format "change-%d" n)))
  603. ;; (setq last-face (intern (format "highlight-changes-%d" n)))
  604. (setq last-face (car p))
  605. (setq hilit-chg-list
  606. (append hilit-chg-list
  607. (list last-category last-face)))
  608. (setq p (cdr p))
  609. (setq n (1+ n)))
  610. (setq hilit-chg-list
  611. (append hilit-chg-list
  612. (list last-category last-face)))))
  613. (defun hilit-chg-bump-change (prop start end)
  614. "Increment (age) the Highlight Changes mode text property."
  615. (let ( new-prop )
  616. (if (eq prop 'hilit-chg-delete)
  617. (setq new-prop (nth 2 hilit-chg-list))
  618. (setq new-prop (nth 2 (member prop hilit-chg-list))))
  619. (if prop
  620. (put-text-property start end 'hilit-chg new-prop)
  621. (message "%d-%d unknown property %s not changed" start end prop))))
  622. ;;;###autoload
  623. (defun highlight-changes-rotate-faces ()
  624. "Rotate the faces if in Highlight Changes mode and the changes are visible.
  625. Current changes are displayed in the face described by the first element
  626. of `highlight-changes-face-list', one level older changes are shown in
  627. face described by the second element, and so on. Very old changes remain
  628. shown in the last face in the list.
  629. You can automatically rotate colors when the buffer is saved by adding
  630. this function to `write-file-functions' as a buffer-local value. To do
  631. this, eval the following in the buffer to be saved:
  632. \(add-hook 'write-file-functions 'highlight-changes-rotate-faces nil t)"
  633. (interactive)
  634. (when (and highlight-changes-mode highlight-changes-visible-mode)
  635. (let ((modified (buffer-modified-p))
  636. (inhibit-modification-hooks t))
  637. ;; The `modified' related code tries to combine two goals: (1) Record the
  638. ;; rotation in `buffer-undo-list' and (2) avoid setting the modified flag
  639. ;; of the current buffer due to the rotation. We do this by inserting (in
  640. ;; `buffer-undo-list') entries restoring buffer-modified-p to nil before
  641. ;; and after the entry for the rotation.
  642. ;; FIXME: this is no good: we need to test the `modified' state at the
  643. ;; time of the undo, not at the time of the "do", otherwise the undo
  644. ;; may erroneously clear the modified flag. --Stef
  645. ;; (unless modified
  646. ;; ;; Install the "before" entry.
  647. ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list))
  648. (unwind-protect
  649. (progn
  650. ;; ensure hilit-chg-list is made and up to date
  651. (hilit-chg-make-list)
  652. ;; remove our existing overlays
  653. (hilit-chg-hide-changes)
  654. ;; for each change text property, increment it
  655. (hilit-chg-map-changes 'hilit-chg-bump-change)
  656. ;; and display them
  657. (hilit-chg-display-changes))
  658. (unless modified
  659. ;; Install the "after" entry. FIXME: See above.
  660. ;; (push '(apply restore-buffer-modified-p nil) buffer-undo-list)
  661. (restore-buffer-modified-p nil)))))
  662. ;; This always returns nil so it is safe to use in write-file-functions
  663. nil)
  664. ;; ========================================================================
  665. ;; Comparing buffers/files
  666. ;; These use ediff to find the differences.
  667. (defun highlight-markup-buffers
  668. (buf-a file-a buf-b file-b &optional markup-a-only)
  669. "Get differences between two buffers and set highlight changes.
  670. Both buffers are done unless optional parameter MARKUP-A-ONLY
  671. is non-nil."
  672. (eval-and-compile
  673. (require 'ediff-util))
  674. (save-window-excursion
  675. (let* (change-info
  676. change-a change-b
  677. a-start a-end len-a
  678. b-start b-end len-b
  679. (bufa-modified (buffer-modified-p buf-a))
  680. (bufb-modified (buffer-modified-p buf-b))
  681. (buf-a-read-only (with-current-buffer buf-a buffer-read-only))
  682. (buf-b-read-only (with-current-buffer buf-b buffer-read-only))
  683. temp-a temp-b)
  684. (if (and file-a bufa-modified)
  685. (if (y-or-n-p (format "Save buffer %s? " buf-a))
  686. (with-current-buffer buf-a
  687. (save-buffer)
  688. (setq bufa-modified (buffer-modified-p buf-a)))
  689. (setq file-a nil)))
  690. (or file-a
  691. (setq temp-a (setq file-a (ediff-make-temp-file buf-a nil))))
  692. (if (and file-b bufb-modified)
  693. (if (y-or-n-p (format "Save buffer %s? " buf-b))
  694. (with-current-buffer buf-b
  695. (save-buffer)
  696. (setq bufb-modified (buffer-modified-p buf-b)))
  697. (setq file-b nil)))
  698. (or file-b
  699. (setq temp-b (setq file-b (ediff-make-temp-file buf-b nil))))
  700. (set-buffer buf-a)
  701. (highlight-changes-mode 1)
  702. (or markup-a-only (with-current-buffer buf-b
  703. (highlight-changes-mode 1)))
  704. (setq change-info (hilit-chg-get-diff-info buf-a file-a buf-b file-b))
  705. (setq change-a (car change-info))
  706. (setq change-b (car (cdr change-info)))
  707. (hilit-chg-make-list)
  708. (while change-a
  709. (setq a-start (nth 0 (car change-a)))
  710. (setq a-end (nth 1 (car change-a)))
  711. (setq b-start (nth 0 (car change-b)))
  712. (setq b-end (nth 1 (car change-b)))
  713. (setq len-a (- a-end a-start))
  714. (setq len-b (- b-end b-start))
  715. (set-buffer buf-a)
  716. (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only)
  717. (or markup-a-only
  718. (with-current-buffer buf-b
  719. (hilit-chg-set-face-on-change b-start b-end len-a
  720. buf-b-read-only)
  721. ))
  722. (setq change-a (cdr change-a))
  723. (setq change-b (cdr change-b)))
  724. (or bufa-modified
  725. (with-current-buffer buf-a (set-buffer-modified-p nil)))
  726. (or bufb-modified
  727. (with-current-buffer buf-b (set-buffer-modified-p nil)))
  728. (if temp-a
  729. (delete-file temp-a))
  730. (if temp-b
  731. (delete-file temp-b)))
  732. ))
  733. ;;;###autoload
  734. (defun highlight-compare-buffers (buf-a buf-b)
  735. "Compare two buffers and highlight the differences.
  736. The default is the current buffer and the one in the next window.
  737. If either buffer is modified and is visiting a file, you are prompted
  738. to save the file.
  739. Unless the buffer is unmodified and visiting a file, the buffer is
  740. written to a temporary file for comparison.
  741. If a buffer is read-only, differences will be highlighted but no property
  742. changes are made, so \\[highlight-changes-next-change] and
  743. \\[highlight-changes-previous-change] will not work."
  744. (interactive
  745. (list
  746. (get-buffer (read-buffer "buffer-a " (current-buffer) t))
  747. (get-buffer
  748. (read-buffer "buffer-b "
  749. (window-buffer (next-window (selected-window))) t))))
  750. (let ((file-a (buffer-file-name buf-a))
  751. (file-b (buffer-file-name buf-b)))
  752. (highlight-markup-buffers buf-a file-a buf-b file-b)
  753. ))
  754. ;;;###autoload
  755. (defun highlight-compare-with-file (file-b)
  756. "Compare this buffer with a file, and highlight differences.
  757. If the buffer has a backup filename, it is used as the default when
  758. this function is called interactively.
  759. If the current buffer is visiting the file being compared against, it
  760. also will have its differences highlighted. Otherwise, the file is
  761. read in temporarily but the buffer is deleted.
  762. If the buffer is read-only, differences will be highlighted but no property
  763. changes are made, so \\[highlight-changes-next-change] and
  764. \\[highlight-changes-previous-change] will not work."
  765. (interactive
  766. (let ((file buffer-file-name)
  767. (file-name nil)
  768. (file-dir nil))
  769. (and file
  770. (setq file-name (file-name-nondirectory file)
  771. file-dir (file-name-directory file)))
  772. (setq file-name (make-backup-file-name file-name))
  773. (unless (file-exists-p file-name)
  774. (setq file-name nil))
  775. (list (read-file-name
  776. "Find to compare with: " ;; prompt
  777. file-dir ;; directory
  778. nil ;; default
  779. nil ;; existing
  780. file-name) ;; initial
  781. )))
  782. (let* ((buf-a (current-buffer))
  783. (file-a (buffer-file-name))
  784. (existing-buf (get-file-buffer file-b))
  785. (buf-b (or existing-buf
  786. (find-file-noselect file-b))))
  787. (highlight-markup-buffers buf-a file-a buf-b file-b (not existing-buf))
  788. (unless existing-buf
  789. (kill-buffer buf-b))
  790. ))
  791. (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b)
  792. ;; hilit-e,x,y are set by function hilit-chg-get-diff-list-hk.
  793. (let (hilit-e hilit-x hilit-y)
  794. (ediff-setup buf-a file-a buf-b file-b
  795. nil nil ; buf-c file-C
  796. 'hilit-chg-get-diff-list-hk
  797. (list (cons 'ediff-job-name 'something))
  798. )
  799. (ediff-with-current-buffer hilit-e (ediff-really-quit nil))
  800. (list hilit-x hilit-y)))
  801. (defun hilit-chg-get-diff-list-hk ()
  802. ;; hilit-e/x/y are dynamically bound by hilit-chg-get-diff-info
  803. ;; which calls this function as a hook.
  804. (defvar hilit-x) ; placate the byte-compiler
  805. (defvar hilit-y)
  806. (defvar hilit-e)
  807. (setq hilit-e (current-buffer))
  808. (let ((n 0) extent p va vb a b)
  809. (setq hilit-x nil hilit-y nil)
  810. (while (< n ediff-number-of-differences)
  811. (ediff-make-fine-diffs n)
  812. (setq va (ediff-get-fine-diff-vector n 'A))
  813. ;; va is a vector if there are fine differences
  814. (if va
  815. (setq a (append va nil))
  816. ;; if not, get the unrefined difference
  817. (setq va (ediff-get-difference n 'A))
  818. (setq a (list (elt va 0))))
  819. ;; a list a list
  820. (setq p a)
  821. (while p
  822. (setq extent (list (overlay-start (car p))
  823. (overlay-end (car p))))
  824. (setq p (cdr p))
  825. (setq hilit-x (append hilit-x (list extent) )));; while p
  826. ;;
  827. (setq vb (ediff-get-fine-diff-vector n 'B))
  828. ;; vb is a vector
  829. (if vb
  830. (setq b (append vb nil))
  831. ;; if not, get the unrefined difference
  832. (setq vb (ediff-get-difference n 'B))
  833. (setq b (list (elt vb 0))))
  834. ;; b list a list
  835. (setq p b)
  836. (while p
  837. (setq extent (list (overlay-start (car p))
  838. (overlay-end (car p))))
  839. (setq p (cdr p))
  840. (setq hilit-y (append hilit-y (list extent) )))
  841. (setq n (1+ n)));; while
  842. ;; ediff-quit doesn't work here.
  843. ;; No point in returning a value, since this is a hook function.
  844. ))
  845. ;; ======================= global-highlight-changes-mode ==============
  846. ;;;###autoload
  847. (define-globalized-minor-mode global-highlight-changes-mode
  848. highlight-changes-mode highlight-changes-mode-turn-on)
  849. (define-obsolete-function-alias
  850. 'global-highlight-changes
  851. 'global-highlight-changes-mode "23.1")
  852. (defun highlight-changes-mode-turn-on ()
  853. "See if Highlight Changes mode should be turned on for this buffer.
  854. This is called when `global-highlight-changes-mode' is turned on."
  855. (or highlight-changes-mode ; do nothing if already on
  856. (if
  857. (cond
  858. ((null highlight-changes-global-modes)
  859. nil)
  860. ((functionp highlight-changes-global-modes)
  861. (funcall highlight-changes-global-modes))
  862. ((listp highlight-changes-global-modes)
  863. (if (eq (car-safe highlight-changes-global-modes) 'not)
  864. (not (memq major-mode (cdr highlight-changes-global-modes)))
  865. (memq major-mode highlight-changes-global-modes)))
  866. (t
  867. (and
  868. (not (string-match "^[ *]" (buffer-name)))
  869. (buffer-file-name))))
  870. (highlight-changes-mode 1))
  871. ))
  872. ;;;; Desktop support.
  873. ;; Called by `desktop-create-buffer' to restore `highlight-changes-mode'.
  874. (defun hilit-chg-desktop-restore (desktop-buffer-locals)
  875. (highlight-changes-mode
  876. (or (cdr (assq 'highlight-changes-mode desktop-buffer-locals)) 1)))
  877. (add-to-list 'desktop-minor-mode-handlers
  878. '(highlight-changes-mode . hilit-chg-desktop-restore))
  879. (add-to-list 'desktop-locals-to-save 'highlight-changes-mode)
  880. ;; ===================== debug ==================
  881. ;; For debug & test use:
  882. ;;
  883. ;; (defun hilit-chg-debug-show (&optional beg end)
  884. ;; (interactive)
  885. ;; (message "--- hilit-chg-debug-show ---")
  886. ;; (hilit-chg-map-changes (lambda (prop start end)
  887. ;; (message "%d-%d: %s" start end prop))
  888. ;; beg end
  889. ;; ))
  890. ;;
  891. ;; ================== end of debug ===============
  892. (provide 'hilit-chg)
  893. ;;; hilit-chg.el ends here