]> code.delx.au - gnu-emacs/blobdiff - lisp/whitespace.el
(Info-goto-index): One register one step in the history.
[gnu-emacs] / lisp / whitespace.el
index ca2813985e183b2e4abcc9d8337f4b915153a6da..6f99f07692f9b752857b66d0a4dd52ad3e9d030f 100644 (file)
@@ -1,10 +1,11 @@
-;;; whitespace.el --- Warn about and clean bogus whitespaces in the file.
+;;; whitespace.el --- warn about and clean bogus whitespaces in the file
 
-;; Copyright (C) 1999-2000 Free Software Foundation, Inc.
+;; Copyright (C) 19992000 Free Software Foundation, Inc.
 
 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
 ;; Keywords: convenience
 
+;; $Id: whitespace.el,v 1.17 2001/08/20 10:05:03 gerd Exp $
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
@@ -38,7 +39,7 @@
 
 ;;; Code:
 
-(defvar whitespace-version "2.8" "Version of the whitespace library.")
+(defvar whitespace-version "3.1" "Version of the whitespace library.")
 
 (defvar whitespace-all-buffer-files nil
   "An associated list of buffers and files checked for whitespace cleanliness.
@@ -60,6 +61,31 @@ visited by the buffers.")
 (make-variable-buffer-local 'whitespace-mode-line)
 (put 'whitespace-mode-line 'permanent-local nil)
 
+(defvar whitespace-check-buffer-leading nil
+  "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")
+(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")
+(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")
+(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")
+(make-variable-buffer-local 'whitespace-check-buffer-ateol)
+(put 'whitespace-check-buffer-ateol 'permanent-local nil)
+
 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
 (eval-when-compile
   (if (not (fboundp 'defgroup))
@@ -85,17 +111,23 @@ don't define defcustom"
   :group 'convenience))
 
 (defcustom whitespace-check-leading-whitespace t
-  "Flag to check leading whitespace."
+  "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'"
   :type 'boolean
   :group 'whitespace)
 
 (defcustom whitespace-check-trailing-whitespace t
-  "Flag to check trailing whitespace."
+  "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'"
   :type 'boolean
   :group 'whitespace)
 
 (defcustom whitespace-check-spacetab-whitespace t
-  "Flag to check space followed by a TAB."
+  "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'"
   :type 'boolean
   :group 'whitespace)
 
@@ -104,8 +136,10 @@ don't define defcustom"
   :type 'regexp
   :group 'whitespace)
 
-(defcustom whitespace-check-indent-whitespace t
-  "Flag to check indentation whitespace."
+(defcustom whitespace-check-indent-whitespace indent-tabs-mode
+  "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'"
   :type 'boolean
   :group 'whitespace)
 
@@ -115,7 +149,9 @@ don't define defcustom"
   :group 'whitespace)
 
 (defcustom whitespace-check-ateol-whitespace t
-  "Flag to check end-of-line whitespace."
+  "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'"
   :type 'boolean
   :group 'whitespace)
 
@@ -129,6 +165,13 @@ don't define defcustom"
   :type 'string
   :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."
+  :type  'boolean
+  :group 'whitespace)
+
 (defcustom whitespace-auto-cleanup nil
   "Cleanup a buffer automatically on finding it whitespace unclean."
   :type  'boolean
@@ -191,6 +234,17 @@ To disable timer scans, set this to zero."
     (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
                                 minor-mode-alist)))
 
+(set-default 'whitespace-check-buffer-leading
+            whitespace-check-leading-whitespace)
+(set-default 'whitespace-check-buffer-trailing
+            whitespace-check-trailing-whitespace)
+(set-default 'whitespace-check-buffer-indent
+            whitespace-check-indent-whitespace)
+(set-default 'whitespace-check-buffer-spacetab
+            whitespace-check-spacetab-whitespace)
+(set-default 'whitespace-check-buffer-ateol
+            whitespace-check-ateol-whitespace)
+
 (defun whitespace-check-whitespace-mode (&optional arg)
   "Test and set the whitespace-mode in qualifying buffers."
   (if (null whitespace-mode)
@@ -199,6 +253,61 @@ To disable timer scans, set this to zero."
                t
              nil))))
 
+;;;###autoload
+(defun whitespace-toggle-leading-check ()
+  "Toggle the check for leading space in the local buffer."
+  (interactive)
+  (let ((current-val whitespace-check-buffer-leading))
+    (setq whitespace-check-buffer-leading (not current-val))
+    (message "Will%s check for leading space in buffer."
+            (if whitespace-check-buffer-leading "" " not"))
+    (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
+
+;;;###autoload
+(defun whitespace-toggle-trailing-check ()
+  "Toggle the check for trailing space in the local buffer."
+  (interactive)
+  (let ((current-val whitespace-check-buffer-trailing))
+    (setq whitespace-check-buffer-trailing (not current-val))
+    (message "Will%s check for trailing space in buffer."
+            (if whitespace-check-buffer-trailing "" " not"))
+    (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
+
+;;;###autoload
+(defun whitespace-toggle-indent-check ()
+  "Toggle the check for indentation space in the local buffer."
+  (interactive)
+  (let ((current-val whitespace-check-buffer-indent))
+    (setq whitespace-check-buffer-indent (not current-val))
+    (message "Will%s check for indentation space in buffer."
+            (if whitespace-check-buffer-indent "" " not"))
+    (if whitespace-check-buffer-indent
+       (whitespace-buffer-search whitespace-indent-regexp))))
+
+;;;###autoload
+(defun whitespace-toggle-spacetab-check ()
+  "Toggle the check for space-followed-by-TABs in the local buffer."
+  (interactive)
+  (let ((current-val whitespace-check-buffer-spacetab))
+    (setq whitespace-check-buffer-spacetab (not current-val))
+    (message "Will%s check for space-followed-by-TABs in buffer."
+            (if whitespace-check-buffer-spacetab "" " not"))
+    (if whitespace-check-buffer-spacetab
+       (whitespace-buffer-search whitespace-spacetab-regexp))))
+
+
+;;;###autoload
+(defun whitespace-toggle-ateol-check ()
+  "Toggle the check for end-of-line space in the local buffer."
+  (interactive)
+  (let ((current-val whitespace-check-buffer-ateol))
+    (setq whitespace-check-buffer-ateol (not current-val))
+    (message "Will%s check for end-of-line space in buffer."
+            (if whitespace-check-buffer-ateol "" " not"))
+    (if whitespace-check-buffer-ateol
+       (whitespace-buffer-search whitespace-ateol-regexp))))
+
+
 ;;;###autoload
 (defun whitespace-buffer (&optional quiet)
   "Find five different types of white spaces in buffer.
@@ -225,21 +334,21 @@ and:
                  (if (not quiet)
                      (message "Can't cleanup: %s is read-only" (buffer-name)))
                (whitespace-cleanup))
-           (let ((whitespace-leading (if whitespace-check-leading-whitespace
+           (let ((whitespace-leading (if whitespace-check-buffer-leading
                                          (whitespace-buffer-leading)
                                        nil))
-                 (whitespace-trailing (if whitespace-check-trailing-whitespace
+                 (whitespace-trailing (if whitespace-check-buffer-trailing
                                           (whitespace-buffer-trailing)
                                         nil))
-                 (whitespace-indent (if whitespace-check-indent-whitespace
+                 (whitespace-indent (if whitespace-check-buffer-indent
                                         (whitespace-buffer-search
                                          whitespace-indent-regexp)
                                       nil))
-                 (whitespace-spacetab (if whitespace-check-spacetab-whitespace
+                 (whitespace-spacetab (if whitespace-check-buffer-spacetab
                                           (whitespace-buffer-search
                                            whitespace-spacetab-regexp)
                                         nil))
-                 (whitespace-ateol (if whitespace-check-ateol-whitespace
+                 (whitespace-ateol (if whitespace-check-buffer-ateol
                                        (whitespace-buffer-search
                                         whitespace-ateol-regexp)
                                      nil))
@@ -330,31 +439,31 @@ whitespace problems."
        ;; they are displayed.
        (setq tab-width whitespace-tabwith)
 
-       (if (and whitespace-check-leading-whitespace
+       (if (and whitespace-check-buffer-leading
                 (whitespace-buffer-leading))
            (progn
              (whitespace-buffer-leading-cleanup)
              (setq whitespace-any t)))
 
-       (if (and whitespace-check-trailing-whitespace
+       (if (and whitespace-check-buffer-trailing
                 (whitespace-buffer-trailing))
            (progn
              (whitespace-buffer-trailing-cleanup)
              (setq whitespace-any t)))
 
-       (if (and whitespace-check-indent-whitespace
+       (if (and whitespace-check-buffer-indent
                 (whitespace-buffer-search whitespace-indent-regexp))
            (progn
              (whitespace-indent-cleanup)
              (setq whitespace-any t)))
 
-       (if (and whitespace-check-spacetab-whitespace
+       (if (and whitespace-check-buffer-spacetab
                 (whitespace-buffer-search whitespace-spacetab-regexp))
            (progn
              (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
              (setq whitespace-any t)))
 
-       (if (and whitespace-check-ateol-whitespace
+       (if (and whitespace-check-buffer-ateol
                 (whitespace-buffer-search whitespace-ateol-regexp))
            (progn
              (whitespace-buffer-cleanup whitespace-ateol-regexp "")
@@ -459,7 +568,7 @@ whitespace problems."
     (save-excursion
       (goto-char (point-min))
       (while (re-search-forward regexp nil t)
-       (setq whitespace-retval (format "%s %s " whitespace-retval
+       (setq whitespace-retval (format "%s %s" whitespace-retval
                                        (match-beginning 0))))
       (if (equal "" whitespace-retval)
          nil
@@ -484,14 +593,14 @@ whitespace problems."
 
 (defun whitespace-unchecked-whitespaces ()
   "Return the list of whitespaces whose testing has been suppressed."
-  (let ((whitespace-this-modeline
-        (concat (if (not whitespace-check-ateol-whitespace) "e")
-                (if (not whitespace-check-indent-whitespace) "i")
-                (if (not whitespace-check-leading-whitespace) "l")
-                (if (not whitespace-check-spacetab-whitespace) "s")
-                (if (not whitespace-check-trailing-whitespace) "t"))))
-    (if (not (equal whitespace-this-modeline ""))
-       whitespace-this-modeline
+  (let ((unchecked-spaces
+        (concat (if (not whitespace-check-buffer-ateol) "e")
+                (if (not whitespace-check-buffer-indent) "i")
+                (if (not whitespace-check-buffer-leading) "l")
+                (if (not whitespace-check-buffer-spacetab) "s")
+                (if (not whitespace-check-buffer-trailing) "t"))))
+    (if (not (equal unchecked-spaces ""))
+       unchecked-spaces
       nil)))
 
 (defun whitespace-update-modeline (&optional whitespace-err)
@@ -592,12 +701,13 @@ Setting this variable directly does not take effect;
 use either \\[customize] or the function `whitespace-global-mode'
 \(which see)."
   :set (lambda (sym val)
-        (whitespace-global-mode (or value 0)))
+        (whitespace-global-mode (or val 0)))
   :initialize 'custom-initialize-default
   :type 'boolean
   :group 'whitespace
   :require 'whitespace)
 
+;;;###autoload
 (defun whitespace-global-mode (&optional arg)
   "Toggle using Whitespace mode in new buffers.
 With ARG, turn the mode on if and only iff ARG is positive.
@@ -611,10 +721,26 @@ When this mode is active, `whitespace-buffer' is added to
   (if arg
       (progn
        (add-hook 'find-file-hooks 'whitespace-buffer)
+       (add-hook 'local-write-file-hooks 'whitespace-write-file-hook)
        (add-hook 'kill-buffer-hook 'whitespace-buffer))
     (remove-hook 'find-file-hooks 'whitespace-buffer)
+    (remove-hook 'local-write-file-hooks 'whitespace-write-file-hook)
     (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
 
+;;;###autoload
+(defun whitespace-write-file-hook ()
+  "The local-write-file-hook to be called on the buffer when
+whitespace check is enabled."
+  (interactive)
+  (let ((werr nil))
+    (if whitespace-auto-cleanup
+       (whitespace-cleanup)
+      (setq werr (whitespace-buffer)))
+    (if (and whitespace-abort-on-error werr)
+       (error (concat "Abort write due to whitespaces in "
+                      buffer-file-name))))
+  nil)
+
 ;;;###autoload
 (defun whitespace-describe ()
   "A summary of whitespaces and what this library can do about them.
@@ -673,6 +799,7 @@ whitespaces during the process of your editing)."
 
 (defun whitespace-unload-hook ()
   (remove-hook 'find-file-hooks 'whitespace-buffer)
+  (remove-hook 'local-write-file-hooks 'whitespace-write-file-hook)
   (remove-hook 'kill-buffer-hook 'whitespace-buffer))
 
 (provide 'whitespace)