diff options
| author | Richard M. Stallman | 1998-08-08 22:20:17 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1998-08-08 22:20:17 +0000 |
| commit | e287d32889b3e57ba9f5f1cebf26a554212d2526 (patch) | |
| tree | 2e472a2a259df7488eccf427814abf49769d2c75 | |
| parent | 036bad88d801a36057f1d22b34833eea2293df9f (diff) | |
| download | emacs-e287d32889b3e57ba9f5f1cebf26a554212d2526.tar.gz emacs-e287d32889b3e57ba9f5f1cebf26a554212d2526.zip | |
Initial revision
| -rw-r--r-- | lisp/hilit-chg.el | 1202 |
1 files changed, 1202 insertions, 0 deletions
diff --git a/lisp/hilit-chg.el b/lisp/hilit-chg.el new file mode 100644 index 00000000000..132d8f81b10 --- /dev/null +++ b/lisp/hilit-chg.el | |||
| @@ -0,0 +1,1202 @@ | |||
| 1 | ;;; hilit-chg.el --- minor mode displaying buffer changes with special face | ||
| 2 | |||
| 3 | ;; Copyright (C) 1998 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Richard Sharman <rsharman@magma.ca> | ||
| 6 | ;; Keywords: faces | ||
| 7 | |||
| 8 | ;; This program is free software; you can redistribute it and/or modify | ||
| 9 | ;; it under the terms of the GNU General Public License as published by | ||
| 10 | ;; the Free Software Foundation; either version 2, or (at your option) | ||
| 11 | ;; any later version. | ||
| 12 | |||
| 13 | ;; This program is distributed in the hope that it will be useful, | ||
| 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | ;; GNU General Public License for more details. | ||
| 17 | |||
| 18 | ;; You should have received a copy of the GNU General Public License | ||
| 19 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | ||
| 20 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 21 | ;; Boston, MA 02111-1307, USA. | ||
| 22 | |||
| 23 | |||
| 24 | ;;; Commentary: | ||
| 25 | |||
| 26 | ;; A minor mode: "Highlight Changes mode". | ||
| 27 | ;; | ||
| 28 | |||
| 29 | ;; Highlight Changes mode has 2 submodes: active and passive. | ||
| 30 | ;; When active, changes to the buffer are displayed in a different face. | ||
| 31 | ;; When passive, any existing displayed changes are saved and new ones | ||
| 32 | ;; recorded but are not displayed differently. | ||
| 33 | ;; Why active and passive? Having the changes visible can be handy when you | ||
| 34 | ;; want the information but very distracting otherwise. So, you can keep | ||
| 35 | ;; Highlight Changes mode in passive state while you make your changes, toggle | ||
| 36 | ;; it on to active mode to see them, then toggle it back off to avoid | ||
| 37 | ;; distraction. | ||
| 38 | ;; | ||
| 39 | ;; When active, changes are displayed in `highlight-changes-face'. When | ||
| 40 | ;; text is deleted, the following character is displayed in | ||
| 41 | ;; `highlight-changes-delete-face' face. | ||
| 42 | ;; | ||
| 43 | ;; | ||
| 44 | ;; You can "age" different sets of changes by using | ||
| 45 | ;; `highlight-changes-rotate-faces'. This rotates different through a series | ||
| 46 | ;; of different faces, so you can distinguish "new" changes from "older" | ||
| 47 | ;; changes. You can customize these "rotates" faces in two ways. You can | ||
| 48 | ;; either explicitly define each face by customizing | ||
| 49 | ;; `highlight-changes-face-list'. If, however, the faces differ from | ||
| 50 | ;; `highlight-changes-face' only in the foreground colour, you can simply set | ||
| 51 | ;; `highlight-changes-colours'. If `highlight-changes-face-list' is nil when | ||
| 52 | ;; the faces are required they will be constructed from | ||
| 53 | ;; `highlight-changes-colours'. | ||
| 54 | ;; | ||
| 55 | ;; | ||
| 56 | ;; When a Highlight Changes mode is on (either active or passive) you can go | ||
| 57 | ;; to the next or previous change with `highlight-changes-next-change' and | ||
| 58 | ;; `highlight-changes-previous-change'. | ||
| 59 | ;; | ||
| 60 | ;; | ||
| 61 | ;; You can also use the command compare-with-file to show changes in this | ||
| 62 | ;; file compared with another file (typically the previous version of the | ||
| 63 | ;; file). | ||
| 64 | ;; | ||
| 65 | ;; | ||
| 66 | ;; There are currently three hooks run by `highlight-changes-mode': | ||
| 67 | ;; `highlight-changes-enable-hook' - is run when Highlight Changes mode | ||
| 68 | ;; is initially enabled for a buffer. | ||
| 69 | ;; `highlight-changes-disable-hook' - is run when Highlight Changes mode | ||
| 70 | ;; is turned off. | ||
| 71 | ;; `highlight-changes-toggle-hook' - is run each time `highlight-changes-mode' | ||
| 72 | ;; is called. Typically this is when | ||
| 73 | ;; toggling between active and passive | ||
| 74 | ;; modes. The variable | ||
| 75 | ;; `highlight-changes-mode' contains the new | ||
| 76 | ;; state (`active' or `passive'.) | ||
| 77 | ;; | ||
| 78 | ;; | ||
| 79 | ;; | ||
| 80 | ;; Example usage: | ||
| 81 | ;; (defun my-highlight-changes-enable-hook () | ||
| 82 | ;; (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces) | ||
| 83 | ;; ) | ||
| 84 | ;; | ||
| 85 | ;; (defun my-highlight-changes-disable-hook () | ||
| 86 | ;; (remove-hook 'local-write-file-hooks 'highlight-changes-rotate-faces) | ||
| 87 | ;; ) | ||
| 88 | ;; | ||
| 89 | ;; (add-hook 'highlight-changes-enable-hook 'my-highlight-changes-enable-hook) | ||
| 90 | ;; (add-hook 'highlight-changes-disable-hook | ||
| 91 | ;; 'my-highlight-changes-disable-hook) | ||
| 92 | |||
| 93 | |||
| 94 | ;; Explciit vs. Implicit | ||
| 95 | ;; | ||
| 96 | |||
| 97 | ;; Normally, Highlight Changes mode is turned on explicitly in a buffer. | ||
| 98 | ;; | ||
| 99 | ;; If you prefer to have it automatically invoked you can do it as | ||
| 100 | ;; follows. | ||
| 101 | ;; | ||
| 102 | ;; 1. Most modes have a major-hook, typically called MODE-hook. You | ||
| 103 | ;; can use `add-hook' to call `highlight-changes-mode'. | ||
| 104 | ;; | ||
| 105 | ;; Example: | ||
| 106 | ;; (add-hook 'c-mode-hook 'highlight-changes-mode) | ||
| 107 | ;; | ||
| 108 | ;; If you want to make it start up in passive mode (regardless of the | ||
| 109 | ;; setting of highlight-changes-initial-state): | ||
| 110 | ;; (add-hook 'emacs-lisp-mode-hook | ||
| 111 | ;; (lambda () | ||
| 112 | ;; (highlight-changes-mode 'passive))) | ||
| 113 | ;; | ||
| 114 | ;; However, this cannot be done for Fundamental mode for there is no | ||
| 115 | ;; such hook. | ||
| 116 | ;; | ||
| 117 | ;; 2. You can use the function `global-highlight-changes' | ||
| 118 | ;; | ||
| 119 | ;; This function, which is fashioned after the way `global-font-lock' works, | ||
| 120 | ;; toggles on or off global Highlight Changes mode. When activated, it turns | ||
| 121 | ;; on Highlight Changes mode in all "suitable" existings buffers and will turn | ||
| 122 | ;; it on in new "suitable" buffers to be created. | ||
| 123 | ;; | ||
| 124 | ;; A buffer's "suitability" is determined by variable | ||
| 125 | ;; `highlight-changes-global-modes', as follows. If the variable is | ||
| 126 | ;; * nil -- then no buffers are suitable; | ||
| 127 | ;; * a function -- this function is called and the result is used. As | ||
| 128 | ;; an example, if the value is 'buffer-file-name then all buffers | ||
| 129 | ;; who are visiting files are suitable, but others (like dired | ||
| 130 | ;; buffers) are not; | ||
| 131 | ;; * a list -- then if the buufer is suitable iff its mode is in the | ||
| 132 | ;; list, exccept if the first element is nil in which case the test | ||
| 133 | ;; is reversed (i.e. it is a list of unsuitable modes). | ||
| 134 | ;; * Otherwise, the buffer is suitable if its name does not begin with | ||
| 135 | ;; ` ' or `*' and if `buffer-file-name' returns true. | ||
| 136 | ;; | ||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | ;; Possible bindings: | ||
| 141 | ;; (global-set-key '[C-right] 'highlight-changes-next-change) | ||
| 142 | ;; (global-set-key '[C-left] 'highlight-changes-previous-change) | ||
| 143 | ;; | ||
| 144 | ;; Other interactive functions (which could be bound if desired): | ||
| 145 | ;; highlight-changes-mode | ||
| 146 | ;; highlight-changes-remove-highlight | ||
| 147 | ;; highlight-changes-rotate-faces | ||
| 148 | ;; compare-with-file | ||
| 149 | |||
| 150 | ;; | ||
| 151 | ;; You can automatically rotate faces when the buffer is saved; | ||
| 152 | ;; see function `highlight-changes-rotate-faces' for how to do this. | ||
| 153 | ;; | ||
| 154 | |||
| 155 | |||
| 156 | ;;; Bugs: | ||
| 157 | |||
| 158 | ;; - the next-change and previous-change functions are too literal; | ||
| 159 | ;; they should find the next "real" change, in other words treat | ||
| 160 | ;; consecutive changes as one. | ||
| 161 | |||
| 162 | |||
| 163 | ;;; To do (maybe), notes, ... | ||
| 164 | |||
| 165 | ;; - having different faces for deletion and non-deletion: is it | ||
| 166 | ;; really worth the hassle? | ||
| 167 | ;; - should have better hooks: when should they be run? | ||
| 168 | ;; - compare-with-file should allow RCS files - e.g. nice to be able | ||
| 169 | ;; to say show changes compared with version 2.1. | ||
| 170 | ;; - Maybe we should have compare-with-buffer as well. (When I tried | ||
| 171 | ;; a while back I ran into a problem with ediff-buffers-internal.) | ||
| 172 | |||
| 173 | |||
| 174 | ;;; History: | ||
| 175 | |||
| 176 | ;; R Sharman (rsharman@magma.ca) Feb 1998: | ||
| 177 | ;; - initial release as change-mode. | ||
| 178 | ;; Ray Nickson (nickson@mcs.vuw.ac.nz) 20 Feb 1998: | ||
| 179 | ;; - deleting text causes surrounding characters to be highlighted. | ||
| 180 | ;; - make it only active for the current buffer. | ||
| 181 | ;; Jari Aalto <jari.aalto@ntc.nokia.com> Mar 1998 | ||
| 182 | ;; - fixes for byte compile errors | ||
| 183 | ;; - use eval-and-compile for autoload | ||
| 184 | ;; Marijn Ros <J.M.Ros@fys.ruu.nl> Mar 98 | ||
| 185 | ;; - suggested turning it on by default | ||
| 186 | ;; Adrian Bridgett <adrian.bridgett@zetnet.co.uk> June 98: | ||
| 187 | ;; - make hide/unhide not affect the buffer modified status | ||
| 188 | ;; Eric Ludlam <zappo@gnu.org> Suggested using overlays. | ||
| 189 | ;; July 98 | ||
| 190 | ;; - global mode and various stuff added | ||
| 191 | ;; - Changed to use overlays | ||
| 192 | ;; August 98 | ||
| 193 | ;; - renmaed to Highlight Changes mode. | ||
| 194 | |||
| 195 | |||
| 196 | ;;; Code: | ||
| 197 | |||
| 198 | ;; ========== Remove when undo-in-progress is set by emacs ======== | ||
| 199 | (unless (boundp 'undo-in-progress) | ||
| 200 | (message "advising undo...") | ||
| 201 | (eval-when-compile (require 'advice)) | ||
| 202 | (defvar undo-in-progress nil) ;; only undo should change this! | ||
| 203 | (defadvice undo (around record-this-is-an-undo activate) | ||
| 204 | (let ((undo-in-progress t)) | ||
| 205 | ad-do-it))) | ||
| 206 | ;; ================================================================ | ||
| 207 | |||
| 208 | (require 'wid-edit) | ||
| 209 | |||
| 210 | ;; ====================== Customization ======================= | ||
| 211 | (defgroup highlight-changes nil | ||
| 212 | "Highlight Changes mode." | ||
| 213 | :group 'faces) | ||
| 214 | |||
| 215 | |||
| 216 | ;; Face information: How the changes appear. | ||
| 217 | |||
| 218 | ;; Defaults for face: red foreground, no change to background, | ||
| 219 | ;; and underlined if a change is because of a deletion. | ||
| 220 | ;; Note: underlining is helpful in that is shows up changes in white space. | ||
| 221 | ;; However, having it set for non-delete changes can be annoying because all | ||
| 222 | ;; indentation on inserts gets underlined (which can look pretty ugly!). | ||
| 223 | |||
| 224 | (defface highlight-changes-face | ||
| 225 | '((((class color)) (:foreground "red" )) | ||
| 226 | (t (:inverse-video t))) | ||
| 227 | "Face used for highlighting changes." | ||
| 228 | :group 'highlight-changes | ||
| 229 | ) | ||
| 230 | |||
| 231 | ;; This looks pretty ugly, actually. Maybe the underline should be removed. | ||
| 232 | (defface highlight-changes-delete-face | ||
| 233 | '((((class color)) (:foreground "red" :underline t)) | ||
| 234 | (t (:inverse-video t))) | ||
| 235 | "Face used for highlighting deletions." | ||
| 236 | :group 'highlight-changes | ||
| 237 | ) | ||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | ;; A (not very good) default list of colours to rotate through. | ||
| 242 | ;; | ||
| 243 | (defcustom highlight-changes-colours | ||
| 244 | (if (eq (frame-parameter nil 'background-mode) 'light) | ||
| 245 | ;; defaults for light background: | ||
| 246 | '( "magenta" "blue" "darkgreen" "chocolate" "sienna4" "NavyBlue") | ||
| 247 | ;; defaults for dark background: | ||
| 248 | '("yellow" "magenta" "blue" "maroon" "firebrick" "green4" "DarkOrchid")) | ||
| 249 | "*Colours used by `highlight-changes-rotate-faces'. | ||
| 250 | The newest rotated change will be displayed in the first element of this list, | ||
| 251 | the next older will be in the second element etc. | ||
| 252 | |||
| 253 | This list is used if `highlight-changes-face-list' is nil, otherwise that | ||
| 254 | variable overrides this list. If you only care about foreground | ||
| 255 | colours then use this, if you want fancier faces then set | ||
| 256 | `highlight-changes-face-list'." | ||
| 257 | :type '(repeat color) | ||
| 258 | :group 'highlight-changes | ||
| 259 | ) | ||
| 260 | |||
| 261 | |||
| 262 | ;; If you invoke highlight-changes-mode with no argument, should it start in | ||
| 263 | ;; active or passive mode? | ||
| 264 | ;; | ||
| 265 | (defcustom highlight-changes-initial-state 'active | ||
| 266 | "*What state (active or passive) `highlight-changes' should start in. | ||
| 267 | This is used when `highlight-changes' is called with no argument. | ||
| 268 | This variable must be set to one of the symbols `active' or `passive'." | ||
| 269 | :type '(choice (const :tag "Active" active) | ||
| 270 | (const :tag "Passive" passive)) | ||
| 271 | :group 'highlight-changes | ||
| 272 | ) | ||
| 273 | |||
| 274 | (defcustom highlight-changes-global-initial-state 'passive | ||
| 275 | "*What state `global-highlight-changes' should start in. | ||
| 276 | This is used if `global-highlight-changes' is called with no argument. | ||
| 277 | This variable must be set to either `active' or `passive'" | ||
| 278 | :type '(choice (const :tag "Active" active) | ||
| 279 | (const :tag "Passive" passive)) | ||
| 280 | :group 'highlight-changes | ||
| 281 | ) | ||
| 282 | |||
| 283 | ;; The strings displayed in the mode-line for the minor mode: | ||
| 284 | (defcustom highlight-changes-active-string nil | ||
| 285 | "*The string used when Highlight Changes mode is in the active state. | ||
| 286 | This should be set to nil if no indication is desired, or to | ||
| 287 | a string with a leading space." | ||
| 288 | :type '(choice string | ||
| 289 | (const :tag "None" nil)) | ||
| 290 | :group 'highlight-changes | ||
| 291 | ) | ||
| 292 | |||
| 293 | (defcustom highlight-changes-passive-string " Chg" | ||
| 294 | "*The string used when Highlight Changes mode is in the passive state. | ||
| 295 | This should be set to nil if no indication is desired, or to | ||
| 296 | a string with a leading space." | ||
| 297 | :type '(choice string | ||
| 298 | (const :tag "None" nil)) | ||
| 299 | :group 'highlight-changes | ||
| 300 | ) | ||
| 301 | |||
| 302 | (defcustom highlight-changes-global-modes t | ||
| 303 | "*Determine whether a buffer is suitable for global Highlight Changes mode. | ||
| 304 | |||
| 305 | A function means that function is called: if it returns non-nil the | ||
| 306 | buffer is suitable. | ||
| 307 | |||
| 308 | A list is a list of modes for which it is suitable, or a list whose | ||
| 309 | first element is 'not followed by modes which are not suitable. | ||
| 310 | |||
| 311 | t means the buffer is suitable if its name does not begin with ` ' nor | ||
| 312 | `*' and the buffer has a filename. | ||
| 313 | |||
| 314 | nil means no buffers are suitable for `global-highlight-changes' | ||
| 315 | (effectively disabling the mode). | ||
| 316 | |||
| 317 | Examples: | ||
| 318 | (c-mode c++-mode) | ||
| 319 | means that Highlight Changes mode is turned on for buffers in C and C++ | ||
| 320 | modes only." | ||
| 321 | :type '(choice | ||
| 322 | (const :tag "all non-special buffers visiting files" t) | ||
| 323 | (set :menu-tag "specific modes" :tag "modes" | ||
| 324 | :value (not) | ||
| 325 | (const :tag "All except these" not) | ||
| 326 | (repeat :tag "Modes" :inline t (symbol :tag "mode"))) | ||
| 327 | (function :menu-tag "determined by function" | ||
| 328 | :value buffer-file-name) | ||
| 329 | (const :tag "none" nil) | ||
| 330 | ) | ||
| 331 | :group 'highlight-changes | ||
| 332 | ) | ||
| 333 | |||
| 334 | |||
| 335 | (defvar global-highlight-changes nil) | ||
| 336 | |||
| 337 | (defcustom highlight-changes-global-changes-existing-buffers nil | ||
| 338 | "*If non-nil toggling global Highlight Changes mode affects existing buffers. | ||
| 339 | Normally, `global-highlight-changes' means affects only new buffers (to be | ||
| 340 | created). However, if highlight-changes-global-changes-existing-buffers | ||
| 341 | is non-nil then turning on `global-highlight-changes' will turn on | ||
| 342 | highlight-changes-mode in suitable buffers and turning the mode off will | ||
| 343 | remove it from existing buffers." | ||
| 344 | :type 'boolean | ||
| 345 | :group 'highlight-changes) | ||
| 346 | |||
| 347 | (defun hilit-chg-cust-fix-changes-face-list (w wc &optional event) | ||
| 348 | ;; When customization function `highlight-changes-face-list' inserts a new | ||
| 349 | ;; face it uses the default face. We don't want the user to modify this | ||
| 350 | ;; face, so we rename the faces in the list on an insert. The rename is | ||
| 351 | ;; actually done by copying the faces so user-defined faces still remain | ||
| 352 | ;; in the same order. | ||
| 353 | ;; The notifying the parent is needed because without it changes to the | ||
| 354 | ;; faces are saved but not to the actual list itself. | ||
| 355 | (let ((old-list (widget-value w))) | ||
| 356 | (if (member 'default old-list) | ||
| 357 | (let | ||
| 358 | ((p (reverse old-list)) | ||
| 359 | (n (length old-list)) | ||
| 360 | new-name old-name | ||
| 361 | (new-list nil) | ||
| 362 | ) | ||
| 363 | (while p | ||
| 364 | (setq old-name (car p)) | ||
| 365 | (setq new-name (intern (format "highlight-changes-face-%d" n))) | ||
| 366 | (if (eq old-name new-name) | ||
| 367 | nil | ||
| 368 | ;; A new face has been inserted: we don't want to modify the | ||
| 369 | ;; default face so copy it. Better, though, (I think) is to | ||
| 370 | ;; make a new face have the same attributes as | ||
| 371 | ;; highlight-changes-face . | ||
| 372 | (if (eq old-name 'default) | ||
| 373 | (copy-face 'highlight-changes-face new-name) | ||
| 374 | (copy-face old-name new-name) | ||
| 375 | )) | ||
| 376 | (setq new-list (append (list new-name) new-list)) | ||
| 377 | (setq n (1- n)) | ||
| 378 | (setq p (cdr p))) | ||
| 379 | (if (equal new-list (widget-value w)) | ||
| 380 | nil ;; (message "notify: no change!") | ||
| 381 | (widget-value-set w new-list) | ||
| 382 | (widget-setup) | ||
| 383 | ) | ||
| 384 | ) | ||
| 385 | ;; (message "notify: no default here!") | ||
| 386 | )) | ||
| 387 | (let ((parent (widget-get w :parent))) | ||
| 388 | (when parent | ||
| 389 | (widget-apply parent :notify w event))) | ||
| 390 | ) | ||
| 391 | |||
| 392 | |||
| 393 | (defcustom highlight-changes-face-list nil | ||
| 394 | "*A list of faces used when rotatating changes. | ||
| 395 | Normally the variable is initialized to nil and the list is created from | ||
| 396 | `highlight-changes-colours' when needed. However, you can set this variable | ||
| 397 | to any list of faces. You will have to do this if you want faces which | ||
| 398 | don't just differ from `highlight-changes-face' by the foreground colour. | ||
| 399 | Otherwise, this list will be constructed when needed from | ||
| 400 | `highlight-changes-colours'." | ||
| 401 | :type '(choice | ||
| 402 | (repeat | ||
| 403 | :notify hilit-chg-cust-fix-changes-face-list | ||
| 404 | face ) | ||
| 405 | (const :tag "Derive from highlight-changes-colours" nil) | ||
| 406 | ) | ||
| 407 | :group 'highlight-changes | ||
| 408 | ) | ||
| 409 | |||
| 410 | ;; ======================================================================== | ||
| 411 | |||
| 412 | ;; These shouldn't be changed! | ||
| 413 | |||
| 414 | (defvar highlight-changes-mode nil) | ||
| 415 | (defvar hilit-chg-list nil) | ||
| 416 | (defvar hilit-chg-string " ??") | ||
| 417 | (or (assq 'highlight-changes-mode minor-mode-alist) | ||
| 418 | (setq minor-mode-alist | ||
| 419 | (cons '(highlight-changes-mode hilit-chg-string) minor-mode-alist) | ||
| 420 | )) | ||
| 421 | (make-variable-buffer-local 'highlight-changes-mode) | ||
| 422 | (make-variable-buffer-local 'hilit-chg-string) | ||
| 423 | |||
| 424 | |||
| 425 | |||
| 426 | (eval-and-compile | ||
| 427 | ;; For compare-with-file | ||
| 428 | (defvar ediff-number-of-differences) | ||
| 429 | (autoload 'ediff-setup "ediff") | ||
| 430 | (autoload 'ediff-with-current-buffer "ediff") | ||
| 431 | (autoload 'ediff-really-quit "ediff") | ||
| 432 | (autoload 'ediff-make-fine-diffs "ediff") | ||
| 433 | (autoload 'ediff-get-fine-diff-vector "ediff") | ||
| 434 | (autoload 'ediff-get-difference "ediff") | ||
| 435 | ) | ||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | ;;; Functions... | ||
| 440 | |||
| 441 | (defun hilit-chg-map-changes (func &optional start-position end-position) | ||
| 442 | "Call function FUNC for each region used by Highlight Changes mode." | ||
| 443 | ;; if start-position is nil, (point-min) is used | ||
| 444 | ;; if end-position is nil, (point-max) is used | ||
| 445 | ;; FUNC is called with 3 params: property start stop | ||
| 446 | (let ((start (or start-position (point-min))) | ||
| 447 | (limit (or end-position (point-max))) | ||
| 448 | prop end) | ||
| 449 | (while (and start (< start limit)) | ||
| 450 | (setq prop (get-text-property start 'hilit-chg)) | ||
| 451 | (setq end (text-property-not-all start limit 'hilit-chg prop)) | ||
| 452 | (if prop | ||
| 453 | (funcall func prop start (or end limit))) | ||
| 454 | (setq start end) | ||
| 455 | ))) | ||
| 456 | |||
| 457 | |||
| 458 | (defun hilit-chg-display-changes (&optional beg end) | ||
| 459 | "Display face information for Highlight Changes mode. | ||
| 460 | |||
| 461 | An overlay containing a change face is added, from the information | ||
| 462 | in the text property of type change. | ||
| 463 | |||
| 464 | This is the opposite of hilit-chg-hide-changes." | ||
| 465 | (hilit-chg-map-changes 'hilit-chg-make-ov beg end)) | ||
| 466 | |||
| 467 | |||
| 468 | (defun hilit-chg-make-ov (prop start end) | ||
| 469 | ;; for the region make change overlays corresponding to | ||
| 470 | ;; the text property 'hilit-chg | ||
| 471 | (let ((ov (make-overlay start end)) | ||
| 472 | face) | ||
| 473 | (or prop | ||
| 474 | (error "hilit-chg-make-ov: prop is nil")) | ||
| 475 | (if (eq prop 'hilit-chg-delete) | ||
| 476 | (setq face 'highlight-changes-delete-face) | ||
| 477 | (setq face (nth 1 (member prop hilit-chg-list)))) | ||
| 478 | (if face | ||
| 479 | (progn | ||
| 480 | ;; We must mark the face, that is the purpose of the overlay | ||
| 481 | (overlay-put ov 'face face) | ||
| 482 | ;; I don't think we need to set evaporate since we should | ||
| 483 | ;; be controlling them! | ||
| 484 | (overlay-put ov 'evaporate t) | ||
| 485 | ;; We set the change property so we can tell this is one | ||
| 486 | ;; of our overlays (so we don't delete someone else's). | ||
| 487 | (overlay-put ov 'hilit-chg t) | ||
| 488 | ) | ||
| 489 | (error "hilit-chg-make-ov: no face for prop: %s" prop) | ||
| 490 | ) | ||
| 491 | )) | ||
| 492 | |||
| 493 | (defun hilit-chg-hide-changes (&optional beg end) | ||
| 494 | "Remove face information for Highlight Changes mode. | ||
| 495 | |||
| 496 | The overlay containing the face is removed, but the text property | ||
| 497 | containing the change information is retained. | ||
| 498 | |||
| 499 | This is the opposite of hilit-chg-display-changes." | ||
| 500 | (let ((start (or beg (point-min))) | ||
| 501 | (limit (or end (point-max))) | ||
| 502 | p ov) | ||
| 503 | (setq p (overlays-in start limit)) | ||
| 504 | (while p | ||
| 505 | ;; don't delete the overlay if it isn't ours! | ||
| 506 | (if (overlay-get (car p) 'hilit-chg) | ||
| 507 | (delete-overlay (car p))) | ||
| 508 | (setq p (cdr p)) | ||
| 509 | ))) | ||
| 510 | |||
| 511 | (defun hilit-chg-fixup (beg end) | ||
| 512 | "Fix change overlays in region beg .. end. | ||
| 513 | |||
| 514 | Ensure the overlays agree with the changes as determined from | ||
| 515 | the text properties of type `hilit-chg' ." | ||
| 516 | ;; Remove or alter overlays in region beg..end | ||
| 517 | (let (p ov ov-start ov-end | ||
| 518 | props q) | ||
| 519 | (setq p (overlays-in beg end)) | ||
| 520 | ;; temp for debugging: | ||
| 521 | ;; (or (eq highlight-changes-mode 'active) | ||
| 522 | ;; (error "hilit-chg-fixup called but Highlight Changes mode not active")) | ||
| 523 | (while p | ||
| 524 | (setq ov (car p)) | ||
| 525 | (setq ov-start (overlay-start ov)) | ||
| 526 | (setq ov-end (overlay-end ov)) | ||
| 527 | (if (< ov-start beg) | ||
| 528 | (progn | ||
| 529 | (move-overlay ov ov-start beg) | ||
| 530 | (if (> ov-end end) | ||
| 531 | (progn | ||
| 532 | (setq props (overlay-properties ov)) | ||
| 533 | (setq ov (make-overlay end ov-end)) | ||
| 534 | (while props | ||
| 535 | (overlay-put ov (car props)(car (cdr props))) | ||
| 536 | (setq props (cdr (cdr props)))) | ||
| 537 | ) | ||
| 538 | ) | ||
| 539 | ) | ||
| 540 | (if (> ov-end end) | ||
| 541 | (move-overlay ov end ov-end) | ||
| 542 | (delete-overlay ov) | ||
| 543 | )) | ||
| 544 | (setq p (cdr p))) | ||
| 545 | (hilit-chg-display-changes beg end) | ||
| 546 | )) | ||
| 547 | |||
| 548 | |||
| 549 | |||
| 550 | |||
| 551 | |||
| 552 | |||
| 553 | ;;;###autoload | ||
| 554 | (defun highlight-changes-remove-highlight (beg end) | ||
| 555 | "Remove the change face from the region. | ||
| 556 | This allows you to manually remove highlighting from uninteresting changes." | ||
| 557 | (interactive "r") | ||
| 558 | (let ((after-change-functions nil)) | ||
| 559 | (remove-text-properties beg end '(hilit-chg nil)) | ||
| 560 | (hilit-chg-fixup beg end))) | ||
| 561 | |||
| 562 | (defun hilit-chg-set-face-on-change (beg end leng-before | ||
| 563 | &optional no-proerty-change) | ||
| 564 | "Record changes and optionally display them in a distinctive face. | ||
| 565 | `hilit-chg-set' adds this function to the `after-change-functions' hook." | ||
| 566 | ;; | ||
| 567 | ;; This function is called by the `after-change-functions' hook, which | ||
| 568 | ;; is how we are notified when text is changed. | ||
| 569 | ;; It is also called from `compare-with-file'. | ||
| 570 | ;; | ||
| 571 | ;; We do NOT want to simply do this if this is an undo command, because | ||
| 572 | ;; otherwise an undone change shows up as changed. While the properties | ||
| 573 | ;; are automatically restored by undo, we must fixup the overlay. | ||
| 574 | (save-match-data | ||
| 575 | (let ((beg-decr 1) (end-incr 1) | ||
| 576 | (type 'hilit-chg) | ||
| 577 | old) | ||
| 578 | (if undo-in-progress | ||
| 579 | (if (eq highlight-changes-mode 'active) | ||
| 580 | (hilit-chg-fixup beg end)) | ||
| 581 | (if (and (= beg end) (> leng-before 0)) | ||
| 582 | ;; deletion | ||
| 583 | (progn | ||
| 584 | ;; The eolp and bolp tests are a kludge! But they prevent | ||
| 585 | ;; rather nasty looking displays when deleting text at the end | ||
| 586 | ;; of line, such as normal corrections as one is typing and | ||
| 587 | ;; immediately makes a corrections, and when deleting first | ||
| 588 | ;; character of a line. | ||
| 589 | ;;; (if (= leng-before 1) | ||
| 590 | ;;; (if (eolp) | ||
| 591 | ;;; (setq beg-decr 0 end-incr 0) | ||
| 592 | ;;; (if (bolp) | ||
| 593 | ;;; (setq beg-decr 0)))) | ||
| 594 | ;;; (setq beg (max (- beg beg-decr) (point-min))) | ||
| 595 | (setq end (min (+ end end-incr) (point-max))) | ||
| 596 | (setq type 'hilit-chg-delete)) | ||
| 597 | ;; Not a deletion. | ||
| 598 | ;; Most of the time the following is not necessary, but | ||
| 599 | ;; if the current text was marked as a deletion then | ||
| 600 | ;; the old overlay is still in effect, so if we add some | ||
| 601 | ;; text then remove the deletion marking, but set it to | ||
| 602 | ;; changed otherwise its highlighting disappears. | ||
| 603 | (if (eq (get-text-property end 'hilit-chg) 'hilit-chg-delete) | ||
| 604 | (progn | ||
| 605 | (remove-text-properties end (+ end 1) '(hilit-chg nil)) | ||
| 606 | (put-text-property end (+ end 1) 'hilit-chg 'hilit-chg) | ||
| 607 | (if (eq highlight-changes-mode 'active) | ||
| 608 | (hilit-chg-fixup beg (+ end 1)))))) | ||
| 609 | (unless no-proerty-change | ||
| 610 | (put-text-property beg end 'hilit-chg type)) | ||
| 611 | (if (or (eq highlight-changes-mode 'active) no-proerty-change) | ||
| 612 | (hilit-chg-make-ov type beg end)) | ||
| 613 | )))) | ||
| 614 | |||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | |||
| 619 | (defun hilit-chg-set (value) | ||
| 620 | "Turn on Highlight Changes mode for this buffer." | ||
| 621 | (setq highlight-changes-mode value) | ||
| 622 | (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | ||
| 623 | (hilit-chg-make-list) | ||
| 624 | (if (eq highlight-changes-mode 'active) | ||
| 625 | (progn | ||
| 626 | (setq hilit-chg-string highlight-changes-active-string) | ||
| 627 | (or buffer-read-only | ||
| 628 | (hilit-chg-display-changes))) | ||
| 629 | ;; mode is passive | ||
| 630 | (setq hilit-chg-string highlight-changes-passive-string) | ||
| 631 | (or buffer-read-only | ||
| 632 | (hilit-chg-hide-changes)) | ||
| 633 | ) | ||
| 634 | (force-mode-line-update) | ||
| 635 | (make-local-hook 'after-change-functions) | ||
| 636 | (add-hook 'after-change-functions 'hilit-chg-set-face-on-change nil t) | ||
| 637 | ) | ||
| 638 | |||
| 639 | (defun hilit-chg-clear () | ||
| 640 | "Remove Highlight Changes mode for this buffer. | ||
| 641 | This removes all saved change information." | ||
| 642 | (if buffer-read-only | ||
| 643 | ;; We print the buffer name because this function could be called | ||
| 644 | ;; on many buffers from `global-highlight-changes'. | ||
| 645 | (message "Cannot remove highlighting from read-only mode buffer %s" | ||
| 646 | (buffer-name)) | ||
| 647 | (remove-hook 'after-change-functions 'hilit-chg-set-face-on-change t) | ||
| 648 | (let ((after-change-functions nil)) | ||
| 649 | (hilit-chg-hide-changes) | ||
| 650 | (hilit-chg-map-changes | ||
| 651 | '(lambda (prop start stop) | ||
| 652 | (remove-text-properties start stop '(hilit-chg nil)))) | ||
| 653 | ) | ||
| 654 | (setq highlight-changes-mode nil) | ||
| 655 | (force-mode-line-update) | ||
| 656 | ;; If we type: C-u -1 M-x highlight-changes-mode | ||
| 657 | ;; we want to turn it off, but hilit-chg-post-command-hook | ||
| 658 | ;; runs and that turns it back on! | ||
| 659 | (remove-hook 'post-command-hook 'hilit-chg-post-command-hook) | ||
| 660 | )) | ||
| 661 | |||
| 662 | ;;;###autoload | ||
| 663 | (defun highlight-changes-mode (&optional arg) | ||
| 664 | "Toggle (or initially set) Highlight Changes mode. | ||
| 665 | |||
| 666 | Without an argument, | ||
| 667 | if Highlight Changes mode is not enabled, then enable it (to either active | ||
| 668 | or passive as determined by variable highlight-changes-initial-state); | ||
| 669 | otherwise, toggle between active and passive states. | ||
| 670 | |||
| 671 | With an argument, | ||
| 672 | if just C-u or a positive argument, set state to active; | ||
| 673 | with a zero argument, set state to passive; | ||
| 674 | with a negative argument, disable Highlight Changes mode completely. | ||
| 675 | |||
| 676 | Active state - means changes are shown in a distinctive face. | ||
| 677 | Passive state - means changes are kept and new ones recorded but are | ||
| 678 | not displayed in a different face. | ||
| 679 | |||
| 680 | Functions: | ||
| 681 | \\[highlight-changes-next-change] - move point to beginning of next change | ||
| 682 | \\[highlight-changes-previous-change] - move to beginning of previous change | ||
| 683 | \\[compare-with-file] - mark text as changed by comparing this buffer with | ||
| 684 | the contents of a file | ||
| 685 | \\[highlight-changes-remove-highlight] - remove the change face from the region | ||
| 686 | \\[highlight-changes-rotate-faces] - rotate different \"ages\" of changes \ | ||
| 687 | through | ||
| 688 | various faces. | ||
| 689 | |||
| 690 | |||
| 691 | Hook variables: | ||
| 692 | highlight-changes-enable-hook - when Highlight Changes mode enabled. | ||
| 693 | highlight-changes-toggle-hook - when entering active or passive state | ||
| 694 | highlight-changes-disable-hook - when turning off Highlight Changes mode. | ||
| 695 | " | ||
| 696 | (interactive "P") | ||
| 697 | (if window-system | ||
| 698 | (let ((was-on highlight-changes-mode) | ||
| 699 | (new-highlight-changes-mode | ||
| 700 | (cond | ||
| 701 | ((null arg) | ||
| 702 | ;; no arg => toggle (or set to active initially) | ||
| 703 | (if highlight-changes-mode | ||
| 704 | (if (eq highlight-changes-mode 'active) 'passive 'active) | ||
| 705 | highlight-changes-initial-state)) | ||
| 706 | ;; an argument is given | ||
| 707 | ((eq arg 'active) | ||
| 708 | 'active) | ||
| 709 | ((eq arg 'passive) | ||
| 710 | 'passive) | ||
| 711 | ((> (prefix-numeric-value arg) 0) | ||
| 712 | 'active) | ||
| 713 | ((< (prefix-numeric-value arg) 0) | ||
| 714 | nil) | ||
| 715 | (t | ||
| 716 | 'passive) | ||
| 717 | ))) | ||
| 718 | (if new-highlight-changes-mode | ||
| 719 | ;; mode is turned on -- but may be passive | ||
| 720 | (progn | ||
| 721 | (hilit-chg-set new-highlight-changes-mode) | ||
| 722 | (or was-on | ||
| 723 | ;; run highlight-changes-enable-hook once | ||
| 724 | (run-hooks 'highlight-changes-enable-hook)) | ||
| 725 | (run-hooks 'highlight-changes-toggle-hook)) | ||
| 726 | ;; mode is turned off | ||
| 727 | (run-hooks 'highlight-changes-disable-hook) | ||
| 728 | (hilit-chg-clear)) | ||
| 729 | ) | ||
| 730 | (message "Highlight Changes mode only works when using a window system")) | ||
| 731 | ) | ||
| 732 | |||
| 733 | |||
| 734 | |||
| 735 | ;;;###autoload | ||
| 736 | (defun highlight-changes-next-change () | ||
| 737 | "Move to the beginning of the next change, if in Highlight Changes mode." | ||
| 738 | (interactive) | ||
| 739 | (if highlight-changes-mode | ||
| 740 | (let ((start (point)) | ||
| 741 | prop) | ||
| 742 | (setq prop (get-text-property (point) 'hilit-chg)) | ||
| 743 | (if prop | ||
| 744 | ;; we are in a change | ||
| 745 | (setq start (next-single-property-change (point) 'hilit-chg))) | ||
| 746 | (if start | ||
| 747 | (setq start (next-single-property-change start 'hilit-chg))) | ||
| 748 | (if start | ||
| 749 | (goto-char start) | ||
| 750 | (message "no next change"))) | ||
| 751 | (message "This buffer is not in Highlight Changes mode."))) | ||
| 752 | |||
| 753 | |||
| 754 | ;;;###autoload | ||
| 755 | (defun highlight-changes-previous-change () | ||
| 756 | "Move to the beginning of the previous change, if in Highlight Changes mode." | ||
| 757 | (interactive) | ||
| 758 | (if highlight-changes-mode | ||
| 759 | (let ( (start (point)) (prop nil) ) | ||
| 760 | (or (bobp) | ||
| 761 | (setq prop (get-text-property (1- (point)) 'hilit-chg))) | ||
| 762 | (if prop | ||
| 763 | ;; we are in a change | ||
| 764 | (setq start (previous-single-property-change (point) 'hilit-chg))) | ||
| 765 | (if start | ||
| 766 | (setq start (previous-single-property-change start 'hilit-chg))) | ||
| 767 | ;; special handling for the case where (point-min) is a change | ||
| 768 | (if start | ||
| 769 | (setq start (or (previous-single-property-change start 'hilit-chg) | ||
| 770 | (if (get-text-property (point-min) 'hilit-chg) | ||
| 771 | (point-min))))) | ||
| 772 | (if start | ||
| 773 | (goto-char start) | ||
| 774 | (message "no previous change"))) | ||
| 775 | (message "This buffer is not in Highlight Changes mode."))) | ||
| 776 | |||
| 777 | |||
| 778 | ;; ======================================================================== | ||
| 779 | |||
| 780 | |||
| 781 | (defun hilit-chg-make-list (&optional force) | ||
| 782 | "Construct hilit-chg-list and highlight-changes-face-list." | ||
| 783 | ;; Constructs highlight-changes-face-list if necessary, | ||
| 784 | ;; and hilit-chg-list always: | ||
| 785 | ;; Maybe this should always be called when rotating a face | ||
| 786 | ;; so we pick up any changes? | ||
| 787 | (if (or (null highlight-changes-face-list) ; Don't do it if it | ||
| 788 | force) ; already exists unless FORCE non-nil. | ||
| 789 | (let ((p highlight-changes-colours) | ||
| 790 | (n 1) name) | ||
| 791 | (setq highlight-changes-face-list nil) | ||
| 792 | (while p | ||
| 793 | (setq name (intern (format "highlight-changes-face-%d" n))) | ||
| 794 | (copy-face 'highlight-changes-face name) | ||
| 795 | (set-face-foreground name (car p)) | ||
| 796 | (setq highlight-changes-face-list | ||
| 797 | (append highlight-changes-face-list (list name))) | ||
| 798 | (setq p (cdr p)) | ||
| 799 | (setq n (1+ n))))) | ||
| 800 | (setq hilit-chg-list (list 'hilit-chg 'highlight-changes-face)) | ||
| 801 | (let ((p highlight-changes-face-list) | ||
| 802 | (n 1) | ||
| 803 | last-category last-face) | ||
| 804 | (while p | ||
| 805 | (setq last-category (intern (format "change-%d" n))) | ||
| 806 | ;; (setq last-face (intern (format "highlight-changes-face-%d" n))) | ||
| 807 | (setq last-face (car p)) | ||
| 808 | (setq hilit-chg-list | ||
| 809 | (append hilit-chg-list | ||
| 810 | (list last-category last-face))) | ||
| 811 | (setq p (cdr p)) | ||
| 812 | (setq n (1+ n))) | ||
| 813 | (setq hilit-chg-list | ||
| 814 | (append hilit-chg-list | ||
| 815 | (list last-category last-face))) | ||
| 816 | )) | ||
| 817 | |||
| 818 | |||
| 819 | (defun hilit-chg-bump-change (prop start end) | ||
| 820 | "Increment (age) the Highlight Changes mode text property of type change." | ||
| 821 | (let ( new-prop ) | ||
| 822 | (if (eq prop 'hilit-chg-delete) | ||
| 823 | (setq new-prop (nth 2 hilit-chg-list)) | ||
| 824 | (setq new-prop (nth 2 (member prop hilit-chg-list))) | ||
| 825 | ) | ||
| 826 | (if prop | ||
| 827 | (put-text-property start end 'hilit-chg new-prop) | ||
| 828 | (message "%d-%d unknown property %s not changed" start end prop) | ||
| 829 | ) | ||
| 830 | )) | ||
| 831 | |||
| 832 | ;;;###autoload | ||
| 833 | (defun highlight-changes-rotate-faces () | ||
| 834 | "Rotate the faces used by Highlight Changes mode. | ||
| 835 | |||
| 836 | Current changes will be display in the face described by the first element | ||
| 837 | of highlight-changes-face-list, those (older) changes will be shown in the | ||
| 838 | face described by the second element, and so on. Very old changes remain | ||
| 839 | shown in the last face in the list. | ||
| 840 | |||
| 841 | You can automatically rotate colours when the buffer is saved | ||
| 842 | by adding this to local-write-file-hooks, by evaling (in the | ||
| 843 | buffer to be saved): | ||
| 844 | (add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces) | ||
| 845 | " | ||
| 846 | (interactive) | ||
| 847 | ;; If not in active mode do nothing but don't complain because this | ||
| 848 | ;; may be bound to a hook. | ||
| 849 | (if (eq highlight-changes-mode 'active) | ||
| 850 | (let ((after-change-functions nil)) | ||
| 851 | ;; ensure hilit-chg-list is made and up to date | ||
| 852 | (hilit-chg-make-list) | ||
| 853 | ;; remove our existing overlays | ||
| 854 | (hilit-chg-hide-changes) | ||
| 855 | ;; for each change text property, increment it | ||
| 856 | (hilit-chg-map-changes 'hilit-chg-bump-change) | ||
| 857 | ;; and display them all if active | ||
| 858 | (if (eq highlight-changes-mode 'active) | ||
| 859 | (hilit-chg-display-changes)) | ||
| 860 | )) | ||
| 861 | ;; This always returns nil so it is safe to use in | ||
| 862 | ;; local-write-file-hook | ||
| 863 | nil) | ||
| 864 | |||
| 865 | |||
| 866 | ;; ======================================================================== | ||
| 867 | ;; Comparing with an existing file. | ||
| 868 | ;; This uses ediff to find the differences. | ||
| 869 | |||
| 870 | ;;;###autoload | ||
| 871 | (defun compare-with-file (file-b) | ||
| 872 | "Compare this buffer with a file. | ||
| 873 | |||
| 874 | The current buffer must be an unmodified buffer visiting a file, | ||
| 875 | and not in read-only mode. | ||
| 876 | |||
| 877 | If the backup filename exists, it is used as the default | ||
| 878 | when called interactively. | ||
| 879 | |||
| 880 | If a buffer is visiting the file being compared against, it also will | ||
| 881 | have its differences highlighted. Otherwise, the file is read in | ||
| 882 | temporarily but the buffer is deleted. | ||
| 883 | |||
| 884 | If a buffer is read-only, differences will be highlighted but no property | ||
| 885 | changes made, so \\[highlight-changes-next-change] and | ||
| 886 | \\[highlight-changes-previous-change] will not work." | ||
| 887 | (interactive (list | ||
| 888 | (read-file-name | ||
| 889 | "File to compare with? " ;; prompt | ||
| 890 | "" ;; directory | ||
| 891 | nil ;; default | ||
| 892 | 'yes ;; must exist | ||
| 893 | (let ((f (make-backup-file-name | ||
| 894 | (or (buffer-file-name (current-buffer)) | ||
| 895 | (error "no file for this buffer"))))) | ||
| 896 | (if (file-exists-p f) f "")) | ||
| 897 | ))) | ||
| 898 | |||
| 899 | (let* ((buf-a (current-buffer)) | ||
| 900 | (buf-a-read-only buffer-read-only) | ||
| 901 | (orig-pos (point)) | ||
| 902 | (file-a (buffer-file-name)) | ||
| 903 | (existing-buf (get-file-buffer file-b)) | ||
| 904 | (buf-b (or existing-buf | ||
| 905 | (find-file-noselect file-b))) | ||
| 906 | (buf-b-read-only (with-current-buffer buf-b buffer-read-only)) | ||
| 907 | xy xx yy p q | ||
| 908 | a-start a-end len-a | ||
| 909 | b-start b-end len-b | ||
| 910 | ) | ||
| 911 | |||
| 912 | ;; We use the fact that the buffer is not marked modified at the | ||
| 913 | ;; end where we clear its modified status | ||
| 914 | (if (buffer-modified-p buf-a) | ||
| 915 | (if (y-or-n-p (format "OK to save %s? " file-a)) | ||
| 916 | (save-buffer buf-a) | ||
| 917 | (error "Buffer must be saved before comparing with a file."))) | ||
| 918 | (if (and existing-buf (buffer-modified-p buf-b)) | ||
| 919 | (if (y-or-n-p (format "OK to save %s? " file-b)) | ||
| 920 | (save-buffer buf-b) | ||
| 921 | (error "Cannot compare with a file in an unsaved buffer."))) | ||
| 922 | (highlight-changes-mode 'active) | ||
| 923 | (if existing-buf (with-current-buffer buf-b | ||
| 924 | (highlight-changes-mode 'active))) | ||
| 925 | (save-window-excursion | ||
| 926 | (setq xy (hilit-chg-get-diff-info buf-a file-a buf-b file-b))) | ||
| 927 | (setq xx (car xy)) | ||
| 928 | (setq p xx) | ||
| 929 | (setq yy (car (cdr xy))) | ||
| 930 | (setq q yy) | ||
| 931 | (hilit-chg-make-list) | ||
| 932 | (while p | ||
| 933 | (setq a-start (nth 0 (car p))) | ||
| 934 | (setq a-end (nth 1 (car p))) | ||
| 935 | (setq b-start (nth 0 (car q))) | ||
| 936 | (setq b-end (nth 1 (car q))) | ||
| 937 | (setq len-a (- a-end a-start)) | ||
| 938 | (setq len-b (- b-end b-start)) | ||
| 939 | (set-buffer buf-a) | ||
| 940 | (hilit-chg-set-face-on-change a-start a-end len-b buf-a-read-only) | ||
| 941 | (set-buffer-modified-p nil) | ||
| 942 | (goto-char orig-pos) | ||
| 943 | (if existing-buf | ||
| 944 | (with-current-buffer buf-b | ||
| 945 | (hilit-chg-set-face-on-change b-start b-end len-a | ||
| 946 | buf-b-read-only ) | ||
| 947 | )) | ||
| 948 | (setq p (cdr p)) | ||
| 949 | (setq q (cdr q)) | ||
| 950 | ) | ||
| 951 | (if existing-buf | ||
| 952 | (set-buffer-modified-p nil) | ||
| 953 | (kill-buffer buf-b)) | ||
| 954 | )) | ||
| 955 | |||
| 956 | |||
| 957 | (defun hilit-chg-get-diff-info (buf-a file-a buf-b file-b) | ||
| 958 | (let ((e nil) x y) ;; e is set by function hilit-chg-get-diff-list-hk | ||
| 959 | (ediff-setup buf-a file-a buf-b file-b | ||
| 960 | nil nil ; buf-c file-C | ||
| 961 | 'hilit-chg-get-diff-list-hk | ||
| 962 | (list (cons 'ediff-job-name 'something)) | ||
| 963 | ) | ||
| 964 | (ediff-with-current-buffer e (ediff-really-quit nil)) | ||
| 965 | (list x y))) | ||
| 966 | |||
| 967 | |||
| 968 | (defun hilit-chg-get-diff-list-hk () | ||
| 969 | ;; x and y are dynamically bound by hilit-chg-get-diff-info | ||
| 970 | ;; which calls this function as a hook | ||
| 971 | (defvar x) ;; placate the byte-compiler | ||
| 972 | (defvar y) | ||
| 973 | (setq e (current-buffer)) | ||
| 974 | (let ((n 0) extent p va vb a b) | ||
| 975 | (setq x nil y nil) ;; x and y are bound by hilit-chg-get-diff-info | ||
| 976 | (while (< n ediff-number-of-differences) | ||
| 977 | (ediff-make-fine-diffs n) | ||
| 978 | (setq va (ediff-get-fine-diff-vector n 'A)) | ||
| 979 | ;; va is a vector if there are fine differences | ||
| 980 | (if va | ||
| 981 | (setq a (append va nil)) | ||
| 982 | ;; if not, get the unrefined difference | ||
| 983 | (setq va (ediff-get-difference n 'A)) | ||
| 984 | (setq a (list (elt va 0))) | ||
| 985 | ) | ||
| 986 | ;; a list a list | ||
| 987 | (setq p a) | ||
| 988 | (while p | ||
| 989 | (setq extent (list (overlay-start (car p)) | ||
| 990 | (overlay-end (car p)))) | ||
| 991 | (setq p (cdr p)) | ||
| 992 | (setq x (append x (list extent) )) | ||
| 993 | );; while p | ||
| 994 | ;; | ||
| 995 | (setq vb (ediff-get-fine-diff-vector n 'B)) | ||
| 996 | ;; vb is a vector | ||
| 997 | (if vb | ||
| 998 | (setq b (append vb nil)) | ||
| 999 | ;; if not, get the unrefined difference | ||
| 1000 | (setq vb (ediff-get-difference n 'B)) | ||
| 1001 | (setq b (list (elt vb 0))) | ||
| 1002 | ) | ||
| 1003 | ;; b list a list | ||
| 1004 | (setq p b) | ||
| 1005 | (while p | ||
| 1006 | (setq extent (list (overlay-start (car p)) | ||
| 1007 | (overlay-end (car p)))) | ||
| 1008 | (setq p (cdr p)) | ||
| 1009 | (setq y (append y (list extent) )) | ||
| 1010 | );; while p | ||
| 1011 | ;; | ||
| 1012 | (setq n (1+ n)) | ||
| 1013 | );; while | ||
| 1014 | ;; ediff-quit doesn't work here. | ||
| 1015 | ;; No point in returning a value, since this is a hook function. | ||
| 1016 | )) | ||
| 1017 | |||
| 1018 | ;; ======================= automatic stuff ============== | ||
| 1019 | |||
| 1020 | ;; Global Highlight Changes mode is modelled after Global Font-lock mode. | ||
| 1021 | ;; Three hooks are used to gain control. When Global Changes Mode is | ||
| 1022 | ;; enabled, `find-file-hooks' and `change-major-mode-hook' are set. | ||
| 1023 | ;; `find-file-hooks' is called when visiting a file, the new mode is | ||
| 1024 | ;; known at this time. | ||
| 1025 | ;; `change-major-mode-hook' is called when a buffer is changing mode. | ||
| 1026 | ;; This could be because of finding a file in which case | ||
| 1027 | ;; `find-file-hooks' has already been called and has done its work. | ||
| 1028 | ;; However, it also catches the case where a new mode is being set by | ||
| 1029 | ;; the user. However, it is called from `kill-all-variables' and at | ||
| 1030 | ;; this time the mode is the old mode, which is not what we want. | ||
| 1031 | ;; So, our function temporarily sets `post-command-hook' which will | ||
| 1032 | ;; be called after the buffer has been completely set up (with the new | ||
| 1033 | ;; mode). It then removes the `post-command-hook'. | ||
| 1034 | ;; One other wrinkle - every M-x command runs the `change-major-mode-hook' | ||
| 1035 | ;; so we ignore this by examining the buffer name. | ||
| 1036 | |||
| 1037 | |||
| 1038 | (defun hilit-chg-major-mode-hook () | ||
| 1039 | (add-hook 'post-command-hook 'hilit-chg-post-command-hook) | ||
| 1040 | ) | ||
| 1041 | |||
| 1042 | (defun hilit-chg-post-command-hook () | ||
| 1043 | ;; This is called after changing a major mode, but also after each | ||
| 1044 | ;; M-x command, in which case the current buffer is a minibuffer. | ||
| 1045 | ;; In that case, do not act on it here, but don't turn it off | ||
| 1046 | ;; either, we will get called here again soon-after. | ||
| 1047 | ;; Also, don't enable it for other special buffers. | ||
| 1048 | (if (string-match "^[ *]" (buffer-name)) | ||
| 1049 | nil ;; (message "ignoring this post-command-hook") | ||
| 1050 | (remove-hook 'post-command-hook 'hilit-chg-post-command-hook) | ||
| 1051 | ;; The following check isn't necessary, since | ||
| 1052 | ;; hilit-chg-turn-on-maybe makes this check too. | ||
| 1053 | (or highlight-changes-mode ;; don't turn it on if it already is | ||
| 1054 | (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)) | ||
| 1055 | )) | ||
| 1056 | |||
| 1057 | (defun hilit-chg-check-global () | ||
| 1058 | ;; This is called from the find file hook. | ||
| 1059 | (hilit-chg-turn-on-maybe highlight-changes-global-initial-state)) | ||
| 1060 | |||
| 1061 | |||
| 1062 | |||
| 1063 | ;;;###autoload | ||
| 1064 | (defun global-highlight-changes (&optional arg) | ||
| 1065 | "Turn on or off global Highlight Changes mode. | ||
| 1066 | |||
| 1067 | When called interactively: | ||
| 1068 | - if no prefix, toggle global Highlight Changes mode on or off | ||
| 1069 | - if called with a positive prefix (or just C-u) turn it on in active mode | ||
| 1070 | - if called with a zero prefix turn it on in passive mode | ||
| 1071 | - if called with a negative prefix turn it off | ||
| 1072 | |||
| 1073 | When called from a program: | ||
| 1074 | - if ARG is nil or omitted, turn it off | ||
| 1075 | - if ARG is 'active, turn it on in active mode | ||
| 1076 | - if ARG is 'passive, turn it on in passive mode | ||
| 1077 | - otherwise just turn it on | ||
| 1078 | |||
| 1079 | When global Highlight Changes mode is enabled, Highlight Changes mode is turned | ||
| 1080 | on for future \"suitable\" buffers (and for \"suitable\" existing buffers if | ||
| 1081 | variable `highlight-changes-global-changes-existing-buffers' is non-nil). | ||
| 1082 | \"Suitablity\" is determined by variable `highlight-changes-global-modes'." | ||
| 1083 | |||
| 1084 | (interactive | ||
| 1085 | (list | ||
| 1086 | (cond | ||
| 1087 | ((null current-prefix-arg) | ||
| 1088 | ;; no arg => toggle it on/off | ||
| 1089 | (setq global-highlight-changes (not global-highlight-changes))) | ||
| 1090 | ;; positive interactive arg - turn it on as active | ||
| 1091 | ((> (prefix-numeric-value current-prefix-arg) 0) | ||
| 1092 | (setq global-highlight-changes t) | ||
| 1093 | 'active) | ||
| 1094 | ;; zero interactive arg - turn it on as passive | ||
| 1095 | ((= (prefix-numeric-value current-prefix-arg) 0) | ||
| 1096 | (setq global-highlight-changes t) | ||
| 1097 | 'passive) | ||
| 1098 | ;; negative interactive arg - turn it off | ||
| 1099 | (t | ||
| 1100 | (setq global-highlight-changes nil) | ||
| 1101 | nil)))) | ||
| 1102 | |||
| 1103 | (if arg | ||
| 1104 | (progn | ||
| 1105 | (if (eq arg 'active) | ||
| 1106 | (setq highlight-changes-global-initial-state 'active) | ||
| 1107 | (if (eq arg 'passive) | ||
| 1108 | (setq highlight-changes-global-initial-state 'passive))) | ||
| 1109 | (setq global-highlight-changes t) | ||
| 1110 | (message "turning ON Global Highlight Changes mode in %s state" | ||
| 1111 | highlight-changes-global-initial-state) | ||
| 1112 | (add-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | ||
| 1113 | (add-hook 'find-file-hooks 'hilit-chg-check-global) | ||
| 1114 | (if highlight-changes-global-changes-existing-buffers | ||
| 1115 | (hilit-chg-update-all-buffers | ||
| 1116 | highlight-changes-global-initial-state)) | ||
| 1117 | ) | ||
| 1118 | (message "turning OFF global Highlight Changes mode") | ||
| 1119 | (remove-hook 'hilit-chg-major-mode-hook 'hilit-chg-major-mode-hook) | ||
| 1120 | (remove-hook 'find-file-hooks 'hilit-chg-check-global) | ||
| 1121 | (remove-hook 'post-command-hook | ||
| 1122 | 'hilit-chg-post-command-hook) | ||
| 1123 | (remove-hook 'find-file-hooks 'hilit-chg-check-global) | ||
| 1124 | (if highlight-changes-global-changes-existing-buffers | ||
| 1125 | (hilit-chg-update-all-buffers nil)) | ||
| 1126 | ) | ||
| 1127 | ) | ||
| 1128 | |||
| 1129 | |||
| 1130 | |||
| 1131 | |||
| 1132 | |||
| 1133 | (defun hilit-chg-turn-on-maybe (value) | ||
| 1134 | "Turn on Highlight Changes mode if it is appropriate for this buffer. | ||
| 1135 | |||
| 1136 | A buffer is appropriate for Highlight Changes mode if all these are true: | ||
| 1137 | - the buffer is not a special buffer (one whose name begins with | ||
| 1138 | `*' or ` ') | ||
| 1139 | - the buffer's mode is suitable as per variable highlight-changes-global-modes | ||
| 1140 | - Highlight Changes mode is not already on for this buffer. | ||
| 1141 | |||
| 1142 | This function is called from hilit-chg-update-all-buffers | ||
| 1143 | from `global-highlight-changes' when turning on global Highlight Changes mode. | ||
| 1144 | " | ||
| 1145 | (or highlight-changes-mode ; do nothing if already on | ||
| 1146 | (if | ||
| 1147 | (cond | ||
| 1148 | ((null highlight-changes-global-modes) | ||
| 1149 | nil) | ||
| 1150 | ((functionp highlight-changes-global-modes) | ||
| 1151 | (funcall highlight-changes-global-modes)) | ||
| 1152 | ((listp highlight-changes-global-modes) | ||
| 1153 | (if (eq (car-safe highlight-changes-global-modes) 'not) | ||
| 1154 | (not (memq major-mode (cdr highlight-changes-global-modes))) | ||
| 1155 | (memq major-mode highlight-changes-global-modes))) | ||
| 1156 | (t | ||
| 1157 | (and | ||
| 1158 | (not (string-match "^[ *]" (buffer-name))) | ||
| 1159 | (buffer-file-name)) | ||
| 1160 | )) | ||
| 1161 | (progn | ||
| 1162 | (hilit-chg-set value) | ||
| 1163 | (run-hooks 'highlight-changes-enable-hook))) | ||
| 1164 | )) | ||
| 1165 | |||
| 1166 | |||
| 1167 | (defun hilit-chg-turn-off-maybe () | ||
| 1168 | (if highlight-changes-mode | ||
| 1169 | (progn | ||
| 1170 | (run-hooks 'highlight-changes-disable-hook) | ||
| 1171 | (hilit-chg-clear)))) | ||
| 1172 | |||
| 1173 | |||
| 1174 | |||
| 1175 | (defun hilit-chg-update-all-buffers (value) | ||
| 1176 | (mapcar | ||
| 1177 | (function (lambda (buffer) | ||
| 1178 | (with-current-buffer buffer | ||
| 1179 | (if value | ||
| 1180 | (hilit-chg-turn-on-maybe value) | ||
| 1181 | (hilit-chg-turn-off-maybe)) | ||
| 1182 | ))) | ||
| 1183 | (buffer-list))) | ||
| 1184 | |||
| 1185 | ;; ===================== debug ================== | ||
| 1186 | ;; For debug & test use: | ||
| 1187 | ;; | ||
| 1188 | ;; (defun hilit-chg-debug-show (&optional beg end) | ||
| 1189 | ;; (interactive) | ||
| 1190 | ;; (message "--- hilit-chg-debug-show ---") | ||
| 1191 | ;; (hilit-chg-map-changes '(lambda (prop start end) | ||
| 1192 | ;; (message "%d-%d: %s" start end prop) | ||
| 1193 | ;; ) | ||
| 1194 | ;; beg end | ||
| 1195 | ;; )) | ||
| 1196 | ;; | ||
| 1197 | ;; ================== end of debug =============== | ||
| 1198 | |||
| 1199 | |||
| 1200 | (provide 'hilit-chg) | ||
| 1201 | ;;; hilit-chg.el ends here | ||
| 1202 | |||