Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-421
[bpt/emacs.git] / lisp / whitespace.el
index e330feb..a119793 100644 (file)
@@ -1,6 +1,6 @@
 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
 
-;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
+;; Copyright (C) 1999, 2000, 2005 Free Software Foundation, Inc.
 
 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
 ;; Keywords: convenience
@@ -86,7 +86,7 @@
 
 ;;; Code:
 
-(defvar whitespace-version "3.4" "Version of the whitespace library.")
+(defvar whitespace-version "3.5" "Version of the whitespace library.")
 
 (defvar whitespace-all-buffer-files nil
   "An associated list of buffers and files checked for whitespace cleanliness.
@@ -307,8 +307,8 @@ To disable timer scans, set this to zero."
   :group 'whitespace)
 
 (defcustom whitespace-display-spaces-in-color t
-  "Display the bogus whitespaces by coloring them with
-`whitespace-highlight-face'."
+  "Display the bogus whitespaces by coloring them with the face
+`whitespace-highlight'."
   :type 'boolean
   :group 'whitespace)
 
@@ -318,18 +318,20 @@ To disable timer scans, set this to zero."
   :group 'whitespace
   :group 'faces)
 
-(defface whitespace-highlight-face '((((class color) (background light))
-                                     (:background "green"))
-                                    (((class color) (background dark))
-                                     (:background "sea green"))
-                                    (((class grayscale mono)
-                                      (background light))
-                                     (:background "black"))
-                                    (((class grayscale mono)
-                                      (background dark))
-                                     (:background "white")))
+(defface whitespace-highlight '((((class color) (background light))
+                                (:background "green1"))
+                               (((class color) (background dark))
+                                (:background "sea green"))
+                               (((class grayscale mono)
+                                 (background light))
+                                (:background "black"))
+                               (((class grayscale mono)
+                                 (background dark))
+                                (:background "white")))
   "Face used for highlighting the bogus whitespaces that exist in the buffer."
   :group 'whitespace-faces)
+;; backward-compatibility alias
+(put 'whitespace-highlight-face 'face-alias 'whitespace-highlight)
 
 (if (not (assoc 'whitespace-mode minor-mode-alist))
     (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
@@ -489,16 +491,14 @@ and:
                                  (if whitespace-spacetab "s")
                                  (if whitespace-trailing "t")))))
              (whitespace-update-modeline whitespace-this-modeline)
-             (save-excursion
-               (get-buffer-create whitespace-errbuf)
-               (kill-buffer whitespace-errbuf)
-               (get-buffer-create whitespace-errbuf)
-               (set-buffer whitespace-errbuf)
+             (if (get-buffer whitespace-errbuf)
+                 (kill-buffer whitespace-errbuf))
+             (with-current-buffer (get-buffer-create whitespace-errbuf)
                (if whitespace-errmsg
                    (progn
                      (insert whitespace-errmsg)
                      (if (not (or quiet whitespace-silent))
-                         (display-buffer whitespace-errbuf t))
+                         (display-buffer (current-buffer) t))
                      (if (not quiet)
                          (message "Whitespaces: [%s%s] in %s"
                                   whitespace-this-modeline
@@ -511,9 +511,7 @@ and:
                  (if (and (not quiet) (not (equal whitespace-clean-msg "")))
                      (message "%s %s" whitespace-filename
                               whitespace-clean-msg))))))))
-    (if whitespace-error
-       t
-      nil)))
+    whitespace-error))
 
 ;;;###autoload
 (defun whitespace-region (s e)
@@ -603,7 +601,7 @@ whitespace problems."
       (setq pmax (point))
       (if (equal pmin pmax)
          (progn
-           (whitespace-highlight-the-space pmin pmax)
+           (whitespace-highlight-the-space pmin (1+ pmax))
            t)
        nil))))
 
@@ -641,7 +639,7 @@ whitespace problems."
            (setq pmax (point))
            (if (equal pmin pmax)
                (progn
-                 (whitespace-highlight-the-space pmin pmax)
+                 (whitespace-highlight-the-space (- pmin 1) pmax)
                  t)
              nil))
        nil))))
@@ -736,23 +734,16 @@ Also with whitespaces whose testing has been turned off."
 (defun whitespace-highlight-the-space (b e)
   "Highlight the current line, unhighlighting a previously jumped to line."
   (if whitespace-display-spaces-in-color
-      (progn
-       (whitespace-unhighlight-the-space)
-       (add-to-list 'whitespace-highlighted-space
-                    (whitespace-make-overlay b e))
-       (whitespace-overlay-put (whitespace-make-overlay b e) 'face
-                               'whitespace-highlight-face))))
+      (let ((ol (whitespace-make-overlay b e)))
+       (push ol whitespace-highlighted-space)
+       (whitespace-overlay-put ol 'face 'whitespace-highlight))))
 ;;  (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space))
 
-(defun whitespace-unhighlight-the-space ()
+(defun whitespace-unhighlight-the-space()
   "Unhighlight the currently highlight line."
   (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
-      (let ((whitespace-this-space nil))
-       (while whitespace-highlighted-space
-         (setq whitespace-this-space (car whitespace-highlighted-space))
-         (setq whitespace-highlighted-space
-               (cdr whitespace-highlighted-space))
-         (whitespace-delete-overlay whitespace-this-space))
+      (progn
+       (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
        (setq whitespace-highlighted-space nil))
     (remove-hook 'pre-command-hook 'whitespace-unhighlight-the-space)))
 
@@ -819,6 +810,9 @@ If timer is not set, then set it to scan the files in
       (disable-timeout whitespace-rescan-timer)
       (setq whitespace-rescan-timer nil))))
 
+;;;###autoload
+(defalias 'global-whitespace-mode 'whitespace-global-mode)
+
 ;;;###autoload
 (define-minor-mode whitespace-global-mode
   "Toggle using Whitespace mode in new buffers.
@@ -856,7 +850,9 @@ This is meant to be added buffer-locally to `write-file-functions'."
   (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
   (remove-hook 'kill-buffer-hook 'whitespace-buffer))
 
+(add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
+
 (provide 'whitespace)
 
-;;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
+;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
 ;;; whitespace.el ends here