]> code.delx.au - gnu-emacs/blobdiff - lisp/whitespace.el
(Abbrevs): A @node line without explicit Prev, Next, and Up links.
[gnu-emacs] / lisp / whitespace.el
index 4406a0615ee9ff4ea13cd1cf0f655c7931827844..7f3cbd913ca39c2144fa62186d35683182a383d8 100644 (file)
@@ -1,6 +1,7 @@
 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
 
-;; Copyright (C) 1999, 2000, 2005 Free Software Foundation, Inc.
+;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
+;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
 ;; Keywords: convenience
@@ -19,8 +20,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 ;;
@@ -86,7 +87,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.
@@ -109,32 +110,32 @@ visited by the buffers.")
 (put 'whitespace-mode-line 'permanent-local nil)
 
 (defvar whitespace-check-buffer-leading nil
-  "Test leading whitespace for file in current buffer if t")
+  "Test leading whitespace for file in current buffer if t.")
 (make-variable-buffer-local 'whitespace-check-buffer-leading)
 (put 'whitespace-check-buffer-leading 'permanent-local nil)
 
 (defvar whitespace-check-buffer-trailing nil
-  "Test trailing whitespace for file in current buffer if t")
+  "Test trailing whitespace for file in current buffer if t.")
 (make-variable-buffer-local 'whitespace-check-buffer-trailing)
 (put 'whitespace-check-buffer-trailing 'permanent-local nil)
 
 (defvar whitespace-check-buffer-indent nil
-  "Test indentation whitespace for file in current buffer if t")
+  "Test indentation whitespace for file in current buffer if t.")
 (make-variable-buffer-local 'whitespace-check-buffer-indent)
 (put 'whitespace-check-buffer-indent 'permanent-local nil)
 
 (defvar whitespace-check-buffer-spacetab nil
-  "Test Space-followed-by-TABS whitespace for file in current buffer if t")
+  "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
 (make-variable-buffer-local 'whitespace-check-buffer-spacetab)
 (put 'whitespace-check-buffer-spacetab 'permanent-local nil)
 
 (defvar whitespace-check-buffer-ateol nil
-  "Test end-of-line whitespace for file in current buffer if t")
+  "Test end-of-line whitespace for file in current buffer if t.")
 (make-variable-buffer-local 'whitespace-check-buffer-ateol)
 (put 'whitespace-check-buffer-ateol 'permanent-local nil)
 
 (defvar whitespace-highlighted-space nil
-  "The variable to store the extent to highlight")
+  "The variable to store the extent to highlight.")
 (make-variable-buffer-local 'whitespace-highlighted-space)
 (put 'whitespace-highlighted-space 'permanent-local nil)
 
@@ -142,61 +143,53 @@ visited by the buffers.")
 (eval-when-compile
   (if (not (fboundp 'defgroup))
       (defmacro defgroup (sym memb doc &rest args)
-       "Null macro for defgroup in all versions of Emacs that don't define
-defgroup"
+       "Null macro for `defgroup' in all versions of Emacs that don't define it."
        t))
   (if (not (fboundp 'defcustom))
       (defmacro defcustom (sym val doc &rest args)
-       "Macro to alias defcustom to defvar in all versions of Emacs that
-don't define defcustom"
+       "Macro to alias `defcustom' to `defvar' in all versions of Emacs that
+don't define it."
        `(defvar ,sym ,val ,doc))))
 
-(if (fboundp 'make-overlay)
-    (progn
-      (defalias 'whitespace-make-overlay 'make-overlay)
-      (defalias 'whitespace-overlay-put 'overlay-put)
-      (defalias 'whitespace-delete-overlay 'delete-overlay)
-      (defalias 'whitespace-overlay-start 'overlay-start)
-      (defalias 'whitespace-overlay-end 'overlay-end)
-      (defalias 'whitespace-mode-line-update 'force-mode-line-update))
-  (defalias 'whitespace-make-overlay 'make-extent)
-  (defalias 'whitespace-overlay-put 'set-extent-property)
-  (defalias 'whitespace-delete-overlay 'delete-extent)
-  (defalias 'whitespace-overlay-start 'extent-start)
-  (defalias 'whitespace-overlay-end 'extent-end)
-  (defalias 'whitespace-mode-line-update 'redraw-modeline))
-
-(if (featurep 'xemacs)
-(defgroup whitespace nil
-  "Check for and fix five different types of whitespaces in source code."
-  ;; Since XEmacs doesn't have a 'convenience group, use the next best group
-  ;; which is 'editing?
-  :link '(emacs-commentary-link "whitespace.el")
-  :group 'editing)
+(defalias 'whitespace-make-overlay
+  (if (featurep 'xemacs) 'make-extent 'make-overlay))
+(defalias 'whitespace-overlay-put
+  (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
+(defalias 'whitespace-delete-overlay
+  (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
+(defalias 'whitespace-overlay-start
+  (if (featurep 'xemacs) 'extent-start 'overlay-start))
+(defalias 'whitespace-overlay-end
+  (if (featurep 'xemacs) 'extent-end 'overlay-end))
+(defalias 'whitespace-mode-line-update
+  (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
+
 (defgroup whitespace nil
   "Check for and fix five different types of whitespaces in source code."
   :version "21.1"
   :link '(emacs-commentary-link "whitespace.el")
-  :group 'convenience))
+  ;; Since XEmacs doesn't have a 'convenience group, use the next best group
+  ;; which is 'editing?
+  :group (if (featurep 'xemacs) 'editing 'convenience))
 
 (defcustom whitespace-check-leading-whitespace t
-  "Flag to check leading whitespace. This is the global for the system.
+  "Flag to check leading whitespace.  This is the global for the system.
 It can be overriden by setting a buffer local variable
-`whitespace-check-buffer-leading'"
+`whitespace-check-buffer-leading'."
   :type 'boolean
   :group 'whitespace)
 
 (defcustom whitespace-check-trailing-whitespace t
-  "Flag to check trailing whitespace. This is the global for the system.
+  "Flag to check trailing whitespace.  This is the global for the system.
 It can be overriden by setting a buffer local variable
-`whitespace-check-buffer-trailing'"
+`whitespace-check-buffer-trailing'."
   :type 'boolean
   :group 'whitespace)
 
 (defcustom whitespace-check-spacetab-whitespace t
-  "Flag to check space followed by a TAB. This is the global for the system.
+  "Flag to check space followed by a TAB.  This is the global for the system.
 It can be overriden by setting a buffer local variable
-`whitespace-check-buffer-spacetab'"
+`whitespace-check-buffer-spacetab'."
   :type 'boolean
   :group 'whitespace)
 
@@ -206,9 +199,9 @@ It can be overriden by setting a buffer local variable
   :group 'whitespace)
 
 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
-  "Flag to check indentation whitespace. This is the global for the system.
+  "Flag to check indentation whitespace.  This is the global for the system.
 It can be overriden by setting a buffer local variable
-`whitespace-check-buffer-indent'"
+`whitespace-check-buffer-indent'."
   :type 'boolean
   :group 'whitespace)
 
@@ -218,9 +211,9 @@ It can be overriden by setting a buffer local variable
   :group 'whitespace)
 
 (defcustom whitespace-check-ateol-whitespace t
-  "Flag to check end-of-line whitespace. This is the global for the system.
+  "Flag to check end-of-line whitespace.  This is the global for the system.
 It can be overriden by setting a buffer local variable
-`whitespace-check-buffer-ateol'"
+`whitespace-check-buffer-ateol'."
   :type 'boolean
   :group 'whitespace)
 
@@ -242,9 +235,9 @@ determines a file to be clean."
   :group 'whitespace)
 
 (defcustom whitespace-abort-on-error nil
-  "While writing a file, abort if the file is unclean. If
-`whitespace-auto-cleanup' is set, that takes precedence over this
-variable."
+  "While writing a file, abort if the file is unclean.
+If `whitespace-auto-cleanup' is set, that takes precedence over
+this variable."
   :type  'boolean
   :group 'whitespace)
 
@@ -277,7 +270,7 @@ Errors*' buffer before opening (or closing) another file."
                                       tcl-mode tex-mode texinfo-mode
                                       vrml-mode xml-mode)
 
-  "Major Modes in which we turn on whitespace checking.
+  "Major modes in which we turn on whitespace checking.
 
 These are mostly programming and documentation modes.  But you may add other
 modes that you want whitespaces checked in by adding something like the
@@ -307,8 +300,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 +311,20 @@ To disable timer scans, set this to zero."
   :group 'whitespace
   :group 'faces)
 
-(defface whitespace-highlight-face '((((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")))
+(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)
@@ -523,9 +518,7 @@ and:
 ;;;###autoload
 (defun whitespace-cleanup ()
   "Cleanup the five different kinds of whitespace problems.
-
-Use \\[describe-function] whitespace-describe to read a summary of the
-whitespace problems."
+See `whitespace-buffer' docstring for a summary of the problems."
   (interactive)
   ;; If this buffer really contains a file, then run, else quit.
   (whitespace-check-whitespace-mode current-prefix-arg)
@@ -599,24 +592,16 @@ 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))))
 
 (defun whitespace-buffer-leading-cleanup ()
   "Remove any empty lines at the top of the file."
   (save-excursion
-    (let ((pmin nil)
-         (pmax nil))
-      (goto-char (point-min))
-      (beginning-of-line)
-      (setq pmin (point))
-      (end-of-line)
-      (setq pmax (point))
-      (if (equal pmin pmax)
-         (progn
-           (kill-line)
-           (whitespace-buffer-leading-cleanup))))))
+    (goto-char (point-min))
+    (skip-chars-forward "\n")
+    (delete-region (point-min) (point))))
 
 (defun whitespace-buffer-trailing ()
   "Check to see if are is more than one empty line at the bottom."
@@ -637,7 +622,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))))
@@ -645,26 +630,11 @@ whitespace problems."
 (defun whitespace-buffer-trailing-cleanup ()
   "Delete all the empty lines at the bottom."
   (save-excursion
-    (let ((pmin nil)
-         (pmax nil))
-      (goto-char (point-max))
-      (beginning-of-line)
-      (setq pmin (point))
-      (end-of-line)
-      (setq pmax (point))
-      (if (equal pmin pmax)
-         (progn
-           (goto-char (1- pmin))
-           (beginning-of-line)
-           (setq pmin (point))
-           (end-of-line)
-           (setq pmax (point))
-           (if (equal pmin pmax)
-               (progn
-                 (goto-char (1- (point-max)))
-                 (beginning-of-line)
-                 (kill-line)
-                 (whitespace-buffer-trailing-cleanup))))))))
+    (goto-char (point-max))
+    (skip-chars-backward "\n")
+    (if (not (bolp))
+       (forward-char 1))
+    (delete-region (point) (point-max))))
 
 (defun whitespace-buffer-search (regexp)
   "Search for any given whitespace REGEXP."
@@ -733,12 +703,11 @@ Also with whitespaces whose testing has been turned off."
   "Highlight the current line, unhighlighting a previously jumped to line."
   (if whitespace-display-spaces-in-color
       (let ((ol (whitespace-make-overlay b e)))
-       (whitespace-unhighlight-the-space)
        (push ol whitespace-highlighted-space)
-       (whitespace-overlay-put ol 'face 'whitespace-highlight-face))))
+       (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)
       (progn