38436
|
1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
|
25582
|
2
|
34304
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
|
25582
|
4
|
29333
|
5 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
|
25582
|
6 ;; Keywords: convenience
|
|
7
|
38436
|
8 ;; $Id: whitespace.el,v 1.15 2001/03/22 21:31:51 rv Exp $
|
25582
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
12 ;; it under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 ;; GNU General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; Whitespace.el URL: http://www.dsmit.com/lisp/
|
|
29
|
|
30 ;; Exported functions:
|
|
31
|
|
32 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
|
|
33 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
|
25625
|
34 ;; `whitespace-region' - To check between point and mark for whitespace
|
|
35 ;; problems.
|
|
36 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
|
|
37 ;; and mark in the current buffer.
|
|
38 ;; `whitespace-describe' - A simple introduction to the library.
|
25582
|
39
|
|
40 ;;; Code:
|
|
41
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
42 (defvar whitespace-version "3.0" "Version of the whitespace library.")
|
25625
|
43
|
|
44 (defvar whitespace-all-buffer-files nil
|
|
45 "An associated list of buffers and files checked for whitespace cleanliness.
|
|
46
|
|
47 This is to enable periodic checking of whitespace cleanliness in the files
|
|
48 visited by the buffers.")
|
|
49
|
|
50 (defvar whitespace-rescan-timer nil
|
|
51 "Timer object used to rescan the files in buffers that have been modified.")
|
25582
|
52
|
26245
|
53 ;; Tell Emacs about this new kind of minor mode
|
|
54 (defvar whitespace-mode nil
|
|
55 "Non-nil when Whitespace mode (a minor mode) is enabled.")
|
|
56 (make-variable-buffer-local 'whitespace-mode)
|
|
57 (put 'whitespace-mode 'permanent-local nil)
|
|
58
|
|
59 (defvar whitespace-mode-line nil
|
|
60 "String to display in the mode line for Whitespace mode.")
|
|
61 (make-variable-buffer-local 'whitespace-mode-line)
|
|
62 (put 'whitespace-mode-line 'permanent-local nil)
|
|
63
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
64 (defvar whitespace-check-buffer-leading nil
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
65 "Test leading whitespace for file in current buffer if t")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
66 (make-variable-buffer-local 'whitespace-check-buffer-leading)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
67 (put 'whitespace-check-buffer-leading 'permanent-local nil)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
68
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
69 (defvar whitespace-check-buffer-trailing nil
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
70 "Test trailing whitespace for file in current buffer if t")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
71 (make-variable-buffer-local 'whitespace-check-buffer-trailing)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
72 (put 'whitespace-check-buffer-trailing 'permanent-local nil)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
73
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
74 (defvar whitespace-check-buffer-indent nil
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
75 "Test indentation whitespace for file in current buffer if t")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
76 (make-variable-buffer-local 'whitespace-check-buffer-indent)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
77 (put 'whitespace-check-buffer-indent 'permanent-local nil)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
78
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
79 (defvar whitespace-check-buffer-spacetab nil
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
80 "Test Space-followed-by-TABS whitespace for file in current buffer if t")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
81 (make-variable-buffer-local 'whitespace-check-buffer-spacetab)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
82 (put 'whitespace-check-buffer-spacetab 'permanent-local nil)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
83
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
84 (defvar whitespace-check-buffer-ateol nil
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
85 "Test end-of-line whitespace for file in current buffer if t")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
86 (make-variable-buffer-local 'whitespace-check-buffer-ateol)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
87 (put 'whitespace-check-buffer-ateol 'permanent-local nil)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
88
|
26253
|
89 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
|
25625
|
90 (eval-when-compile
|
26253
|
91 (if (not (fboundp 'defgroup))
|
|
92 (defmacro defgroup (sym memb doc &rest args)
|
|
93 "Null macro for defgroup in all versions of Emacs that don't define
|
|
94 defgroup"
|
|
95 t))
|
|
96 (if (not (fboundp 'defcustom))
|
|
97 (defmacro defcustom (sym val doc &rest args)
|
|
98 "Macro to alias defcustom to defvar in all versions of Emacs that
|
|
99 don't define defcustom"
|
|
100 `(defvar ,sym ,val ,doc))))
|
25582
|
101
|
32501
|
102 (if (featurep 'xemacs)
|
25582
|
103 (defgroup whitespace nil
|
25625
|
104 "Check for and fix five different types of whitespaces in source code."
|
25582
|
105 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
|
|
106 ;; which is 'editing?
|
32501
|
107 :group 'editing)
|
|
108 (defgroup whitespace nil
|
|
109 "Check for and fix five different types of whitespaces in source code."
|
|
110 :version "21.1"
|
|
111 :group 'convenience))
|
25582
|
112
|
25724
|
113 (defcustom whitespace-check-leading-whitespace t
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
114 "Flag to check leading whitespace. This is the global for the system.
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
115 It can be overriden by setting a buffer local variable
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
116 `whitespace-check-buffer-leading'"
|
25724
|
117 :type 'boolean
|
|
118 :group 'whitespace)
|
|
119
|
|
120 (defcustom whitespace-check-trailing-whitespace t
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
121 "Flag to check trailing whitespace. This is the global for the system.
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
122 It can be overriden by setting a buffer local variable
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
123 `whitespace-check-buffer-trailing'"
|
25724
|
124 :type 'boolean
|
|
125 :group 'whitespace)
|
|
126
|
|
127 (defcustom whitespace-check-spacetab-whitespace t
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
128 "Flag to check space followed by a TAB. This is the global for the system.
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
129 It can be overriden by setting a buffer local variable
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
130 `whitespace-check-buffer-spacetab'"
|
25724
|
131 :type 'boolean
|
|
132 :group 'whitespace)
|
|
133
|
25625
|
134 (defcustom whitespace-spacetab-regexp " \t"
|
25724
|
135 "Regexp to match a space followed by a TAB."
|
32501
|
136 :type 'regexp
|
25625
|
137 :group 'whitespace)
|
|
138
|
25724
|
139 (defcustom whitespace-check-indent-whitespace t
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
140 "Flag to check indentation whitespace. This is the global for the system.
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
141 It can be overriden by setting a buffer local variable
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
142 `whitespace-check-buffer-indent'"
|
25724
|
143 :type 'boolean
|
|
144 :group 'whitespace)
|
|
145
|
25625
|
146 (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ")
|
|
147 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
|
32501
|
148 :type 'regexp
|
25625
|
149 :group 'whitespace)
|
|
150
|
25724
|
151 (defcustom whitespace-check-ateol-whitespace t
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
152 "Flag to check end-of-line whitespace. This is the global for the system.
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
153 It can be overriden by setting a buffer local variable
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
154 `whitespace-check-buffer-ateol'"
|
25724
|
155 :type 'boolean
|
|
156 :group 'whitespace)
|
|
157
|
25625
|
158 (defcustom whitespace-ateol-regexp "[ \t]$"
|
|
159 "Regexp to match a TAB or a space at the EOL."
|
32501
|
160 :type 'regexp
|
25625
|
161 :group 'whitespace)
|
|
162
|
|
163 (defcustom whitespace-errbuf "*Whitespace Errors*"
|
32501
|
164 "The name of the buffer where whitespace related messages will be logged."
|
25625
|
165 :type 'string
|
|
166 :group 'whitespace)
|
|
167
|
25582
|
168 (defcustom whitespace-auto-cleanup nil
|
25625
|
169 "Cleanup a buffer automatically on finding it whitespace unclean."
|
25582
|
170 :type 'boolean
|
|
171 :group 'whitespace)
|
|
172
|
|
173 (defcustom whitespace-silent nil
|
25625
|
174 "All whitespace errors will be shown only in the modeline when t.
|
25582
|
175
|
|
176 Note that setting this may cause all whitespaces introduced in a file to go
|
|
177 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
|
25625
|
178 Errors*' buffer before opening (or closing) another file."
|
25582
|
179 :type 'boolean
|
|
180 :group 'whitespace)
|
|
181
|
|
182 (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
|
25724
|
183 c-mode c++-mode cc-mode
|
|
184 change-log-mode cperl-mode
|
25582
|
185 electric-nroff-mode emacs-lisp-mode
|
|
186 f90-mode fortran-mode html-mode
|
|
187 html3-mode java-mode jde-mode
|
|
188 ksh-mode latex-mode LaTeX-mode
|
|
189 lisp-mode m4-mode makefile-mode
|
|
190 modula-2-mode nroff-mode objc-mode
|
|
191 pascal-mode perl-mode prolog-mode
|
|
192 python-mode scheme-mode sgml-mode
|
25724
|
193 sh-mode shell-script-mode simula-mode
|
|
194 tcl-mode tex-mode texinfo-mode
|
|
195 vrml-mode xml-mode)
|
25582
|
196
|
25625
|
197 "Major Modes in which we turn on whitespace checking.
|
25582
|
198
|
32501
|
199 These are mostly programming and documentation modes. But you may add other
|
25625
|
200 modes that you want whitespaces checked in by adding something like the
|
|
201 following to your `.emacs':
|
25582
|
202
|
25625
|
203 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
|
|
204 whitespace-modes))\)
|
|
205
|
|
206 Or, alternately, you can use the Emacs `customize' command to set this."
|
32501
|
207 :type '(repeat symbol)
|
25582
|
208 :group 'whitespace)
|
|
209
|
29333
|
210 (defcustom whitespace-rescan-timer-time 600
|
25625
|
211 "Period in seconds to rescan modified buffers for whitespace creep.
|
25582
|
212
|
25625
|
213 This is the period after which the timer will fire causing
|
|
214 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
|
25724
|
215 modified buffers.
|
|
216
|
|
217 To disable timer scans, set this to zero."
|
25582
|
218 :type 'integer
|
|
219 :group 'whitespace)
|
|
220
|
26245
|
221 (defcustom whitespace-display-in-modeline t
|
|
222 "Display whitespace errors on the modeline."
|
|
223 :type 'boolean
|
|
224 :group 'whitespace)
|
25582
|
225
|
|
226 (if (not (assoc 'whitespace-mode minor-mode-alist))
|
|
227 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
|
|
228 minor-mode-alist)))
|
|
229
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
230 (set-default 'whitespace-check-buffer-leading
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
231 whitespace-check-leading-whitespace)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
232 (set-default 'whitespace-check-buffer-trailing
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
233 whitespace-check-trailing-whitespace)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
234 (set-default 'whitespace-check-buffer-indent
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
235 whitespace-check-indent-whitespace)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
236 (set-default 'whitespace-check-buffer-spacetab
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
237 whitespace-check-spacetab-whitespace)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
238 (set-default 'whitespace-check-buffer-ateol
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
239 whitespace-check-ateol-whitespace)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
240
|
25582
|
241 (defun whitespace-check-whitespace-mode (&optional arg)
|
25625
|
242 "Test and set the whitespace-mode in qualifying buffers."
|
25582
|
243 (if (null whitespace-mode)
|
|
244 (setq whitespace-mode
|
|
245 (if (or arg (member major-mode whitespace-modes))
|
|
246 t
|
|
247 nil))))
|
|
248
|
25625
|
249 ;;;###autoload
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
250 (defun whitespace-toggle-leading-check ()
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
251 "Toggle the check for leading space in the local buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
252 (interactive)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
253 (let ((current-val whitespace-check-buffer-leading))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
254 (setq whitespace-check-buffer-leading (not current-val))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
255 (message "Will%s check for leading space in buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
256 (if whitespace-check-buffer-leading "" " not"))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
257 (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
258
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
259 ;;;###autoload
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
260 (defun whitespace-toggle-trailing-check ()
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
261 "Toggle the check for trailing space in the local buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
262 (interactive)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
263 (let ((current-val whitespace-check-buffer-trailing))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
264 (setq whitespace-check-buffer-trailing (not current-val))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
265 (message "Will%s check for trailing space in buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
266 (if whitespace-check-buffer-trailing "" " not"))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
267 (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
268
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
269 ;;;###autoload
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
270 (defun whitespace-toggle-indent-check ()
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
271 "Toggle the check for indentation space in the local buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
272 (interactive)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
273 (let ((current-val whitespace-check-buffer-indent))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
274 (setq whitespace-check-buffer-indent (not current-val))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
275 (message "Will%s check for indentation space in buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
276 (if whitespace-check-buffer-indent "" " not"))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
277 (if whitespace-check-buffer-indent
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
278 (whitespace-buffer-search whitespace-indent-regexp))))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
279
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
280 ;;;###autoload
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
281 (defun whitespace-toggle-spacetab-check ()
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
282 "Toggle the check for space-followed-by-TABs in the local buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
283 (interactive)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
284 (let ((current-val whitespace-check-buffer-spacetab))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
285 (setq whitespace-check-buffer-spacetab (not current-val))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
286 (message "Will%s check for space-followed-by-TABs in buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
287 (if whitespace-check-buffer-spacetab "" " not"))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
288 (if whitespace-check-buffer-spacetab
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
289 (whitespace-buffer-search whitespace-spacetab-regexp))))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
290
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
291
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
292 ;;;###autoload
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
293 (defun whitespace-toggle-ateol-check ()
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
294 "Toggle the check for end-of-line space in the local buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
295 (interactive)
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
296 (let ((current-val whitespace-check-buffer-ateol))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
297 (setq whitespace-check-buffer-ateol (not current-val))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
298 (message "Will%s check for end-of-line space in buffer."
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
299 (if whitespace-check-buffer-ateol "" " not"))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
300 (if whitespace-check-buffer-ateol
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
301 (whitespace-buffer-search whitespace-ateol-regexp))))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
302
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
303
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
304 ;;;###autoload
|
25582
|
305 (defun whitespace-buffer (&optional quiet)
|
32501
|
306 "Find five different types of white spaces in buffer.
|
|
307 These are:
|
25582
|
308 1. Leading space \(empty lines at the top of a file\).
|
|
309 2. Trailing space \(empty lines at the end of a file\).
|
|
310 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
|
|
311 4. Spaces followed by a TAB. \(Almost always, we never want that\).
|
|
312 5. Spaces or TABS at the end of a line.
|
|
313
|
|
314 Check for whitespace only if this buffer really contains a non-empty file
|
|
315 and:
|
|
316 1. the major mode is one of the whitespace-modes, or
|
25625
|
317 2. `whitespace-buffer' was explicitly called with a prefix argument."
|
25582
|
318 (interactive)
|
26245
|
319 (let ((whitespace-error nil))
|
|
320 (whitespace-check-whitespace-mode current-prefix-arg)
|
|
321 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
|
|
322 (progn
|
|
323 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
|
|
324 (whitespace-tickle-timer)
|
|
325 (if whitespace-auto-cleanup
|
|
326 (if buffer-read-only
|
|
327 (if (not quiet)
|
|
328 (message "Can't cleanup: %s is read-only" (buffer-name)))
|
|
329 (whitespace-cleanup))
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
330 (let ((whitespace-leading (if whitespace-check-buffer-leading
|
26245
|
331 (whitespace-buffer-leading)
|
|
332 nil))
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
333 (whitespace-trailing (if whitespace-check-buffer-trailing
|
26245
|
334 (whitespace-buffer-trailing)
|
|
335 nil))
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
336 (whitespace-indent (if whitespace-check-buffer-indent
|
26245
|
337 (whitespace-buffer-search
|
|
338 whitespace-indent-regexp)
|
25724
|
339 nil))
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
340 (whitespace-spacetab (if whitespace-check-buffer-spacetab
|
26245
|
341 (whitespace-buffer-search
|
|
342 whitespace-spacetab-regexp)
|
|
343 nil))
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
344 (whitespace-ateol (if whitespace-check-buffer-ateol
|
26245
|
345 (whitespace-buffer-search
|
|
346 whitespace-ateol-regexp)
|
|
347 nil))
|
|
348 (whitespace-errmsg nil)
|
|
349 (whitespace-filename buffer-file-name)
|
|
350 (whitespace-this-modeline ""))
|
25582
|
351
|
26245
|
352 ;; Now let's complain if we found any of the above.
|
|
353 (setq whitespace-error (or whitespace-leading whitespace-indent
|
|
354 whitespace-spacetab whitespace-ateol
|
|
355 whitespace-trailing))
|
25582
|
356
|
26245
|
357 (if whitespace-error
|
25582
|
358 (progn
|
26245
|
359 (setq whitespace-errmsg
|
|
360 (concat whitespace-filename " contains:\n"
|
|
361 (if whitespace-leading
|
|
362 "Leading whitespace\n")
|
|
363 (if whitespace-indent
|
|
364 (concat "Indentation whitespace"
|
|
365 whitespace-indent "\n"))
|
|
366 (if whitespace-spacetab
|
|
367 (concat "Space followed by Tab"
|
|
368 whitespace-spacetab "\n"))
|
|
369 (if whitespace-ateol
|
|
370 (concat "End-of-line whitespace"
|
|
371 whitespace-ateol "\n"))
|
|
372 (if whitespace-trailing
|
|
373 "Trailing whitespace\n")
|
|
374 "\ntype `M-x whitespace-cleanup' to "
|
|
375 "cleanup the file."))
|
|
376 (setq whitespace-this-modeline
|
|
377 (concat (if whitespace-ateol "e")
|
|
378 (if whitespace-indent "i")
|
|
379 (if whitespace-leading "l")
|
|
380 (if whitespace-spacetab "s")
|
|
381 (if whitespace-trailing "t")))))
|
|
382 (whitespace-update-modeline whitespace-this-modeline)
|
|
383 (save-excursion
|
|
384 (get-buffer-create whitespace-errbuf)
|
|
385 (kill-buffer whitespace-errbuf)
|
|
386 (get-buffer-create whitespace-errbuf)
|
|
387 (set-buffer whitespace-errbuf)
|
|
388 (if whitespace-errmsg
|
|
389 (progn
|
|
390 (insert whitespace-errmsg)
|
|
391 (if (not (or quiet whitespace-silent))
|
|
392 (display-buffer whitespace-errbuf t))
|
|
393 (if (not quiet)
|
|
394 (message "Whitespaces: [%s%s] in %s"
|
|
395 whitespace-this-modeline
|
|
396 (let ((whitespace-unchecked
|
|
397 (whitespace-unchecked-whitespaces)))
|
|
398 (if whitespace-unchecked
|
|
399 (concat "!" whitespace-unchecked)
|
|
400 ""))
|
|
401 whitespace-filename)))
|
|
402 (if (not quiet)
|
|
403 (message "%s clean" whitespace-filename))))))))
|
|
404 (if whitespace-error
|
|
405 t
|
|
406 nil)))
|
25582
|
407
|
25625
|
408 ;;;###autoload
|
25582
|
409 (defun whitespace-region (s e)
|
32501
|
410 "Check the region for whitespace errors."
|
25582
|
411 (interactive "r")
|
|
412 (save-excursion
|
|
413 (save-restriction
|
|
414 (narrow-to-region s e)
|
|
415 (whitespace-buffer))))
|
|
416
|
25625
|
417 ;;;###autoload
|
25582
|
418 (defun whitespace-cleanup ()
|
25625
|
419 "Cleanup the five different kinds of whitespace problems.
|
|
420
|
|
421 Use \\[describe-function] whitespace-describe to read a summary of the
|
|
422 whitespace problems."
|
25582
|
423 (interactive)
|
|
424 ;; If this buffer really contains a file, then run, else quit.
|
|
425 (whitespace-check-whitespace-mode current-prefix-arg)
|
|
426 (if (and buffer-file-name whitespace-mode)
|
|
427 (let ((whitespace-any nil)
|
|
428 (whitespace-tabwith 8)
|
|
429 (whitespace-tabwith-saved tab-width))
|
|
430
|
|
431 ;; since all printable TABS should be 8, irrespective of how
|
|
432 ;; they are displayed.
|
|
433 (setq tab-width whitespace-tabwith)
|
|
434
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
435 (if (and whitespace-check-buffer-leading
|
25724
|
436 (whitespace-buffer-leading))
|
25582
|
437 (progn
|
|
438 (whitespace-buffer-leading-cleanup)
|
|
439 (setq whitespace-any t)))
|
|
440
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
441 (if (and whitespace-check-buffer-trailing
|
25724
|
442 (whitespace-buffer-trailing))
|
25582
|
443 (progn
|
|
444 (whitespace-buffer-trailing-cleanup)
|
|
445 (setq whitespace-any t)))
|
|
446
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
447 (if (and whitespace-check-buffer-indent
|
25724
|
448 (whitespace-buffer-search whitespace-indent-regexp))
|
25582
|
449 (progn
|
|
450 (whitespace-indent-cleanup)
|
|
451 (setq whitespace-any t)))
|
|
452
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
453 (if (and whitespace-check-buffer-spacetab
|
25724
|
454 (whitespace-buffer-search whitespace-spacetab-regexp))
|
25582
|
455 (progn
|
|
456 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
|
|
457 (setq whitespace-any t)))
|
|
458
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
459 (if (and whitespace-check-buffer-ateol
|
25724
|
460 (whitespace-buffer-search whitespace-ateol-regexp))
|
25582
|
461 (progn
|
|
462 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
|
|
463 (setq whitespace-any t)))
|
|
464
|
|
465 ;; Call this recursively till everything is taken care of
|
25724
|
466 (if whitespace-any
|
|
467 (whitespace-cleanup)
|
25582
|
468 (progn
|
|
469 (message "%s clean" buffer-file-name)
|
26245
|
470 (whitespace-update-modeline)))
|
25582
|
471 (setq tab-width whitespace-tabwith-saved))))
|
|
472
|
25625
|
473 ;;;###autoload
|
25582
|
474 (defun whitespace-cleanup-region (s e)
|
32501
|
475 "Whitespace cleanup on the region."
|
25582
|
476 (interactive "r")
|
|
477 (save-excursion
|
|
478 (save-restriction
|
|
479 (narrow-to-region s e)
|
|
480 (whitespace-cleanup))
|
|
481 (whitespace-buffer t)))
|
|
482
|
|
483 (defun whitespace-buffer-leading ()
|
|
484 "Check to see if there are any empty lines at the top of the file."
|
|
485 (save-excursion
|
|
486 (let ((pmin nil)
|
|
487 (pmax nil))
|
|
488 (goto-char (point-min))
|
|
489 (beginning-of-line)
|
|
490 (setq pmin (point))
|
|
491 (end-of-line)
|
|
492 (setq pmax (point))
|
|
493 (if (equal pmin pmax)
|
|
494 t
|
|
495 nil))))
|
|
496
|
|
497 (defun whitespace-buffer-leading-cleanup ()
|
25625
|
498 "Remove any empty lines at the top of the file."
|
25582
|
499 (save-excursion
|
|
500 (let ((pmin nil)
|
|
501 (pmax nil))
|
|
502 (goto-char (point-min))
|
|
503 (beginning-of-line)
|
|
504 (setq pmin (point))
|
|
505 (end-of-line)
|
|
506 (setq pmax (point))
|
|
507 (if (equal pmin pmax)
|
|
508 (progn
|
|
509 (kill-line)
|
|
510 (whitespace-buffer-leading-cleanup))))))
|
|
511
|
|
512 (defun whitespace-buffer-trailing ()
|
|
513 "Check to see if are is more than one empty line at the bottom."
|
|
514 (save-excursion
|
|
515 (let ((pmin nil)
|
|
516 (pmax nil))
|
|
517 (goto-char (point-max))
|
|
518 (beginning-of-line)
|
|
519 (setq pmin (point))
|
|
520 (end-of-line)
|
|
521 (setq pmax (point))
|
|
522 (if (equal pmin pmax)
|
|
523 (progn
|
|
524 (goto-char (- (point) 1))
|
|
525 (beginning-of-line)
|
|
526 (setq pmin (point))
|
|
527 (end-of-line)
|
|
528 (setq pmax (point))
|
|
529 (if (equal pmin pmax)
|
|
530 t
|
|
531 nil))
|
|
532 nil))))
|
|
533
|
|
534 (defun whitespace-buffer-trailing-cleanup ()
|
|
535 "Delete all the empty lines at the bottom."
|
|
536 (save-excursion
|
|
537 (let ((pmin nil)
|
|
538 (pmax nil))
|
|
539 (goto-char (point-max))
|
|
540 (beginning-of-line)
|
|
541 (setq pmin (point))
|
|
542 (end-of-line)
|
|
543 (setq pmax (point))
|
|
544 (if (equal pmin pmax)
|
|
545 (progn
|
|
546 (goto-char (1- pmin))
|
|
547 (beginning-of-line)
|
|
548 (setq pmin (point))
|
|
549 (end-of-line)
|
|
550 (setq pmax (point))
|
|
551 (if (equal pmin pmax)
|
|
552 (progn
|
|
553 (goto-char (1- (point-max)))
|
|
554 (beginning-of-line)
|
|
555 (kill-line)
|
|
556 (whitespace-buffer-trailing-cleanup))))))))
|
|
557
|
|
558 (defun whitespace-buffer-search (regexp)
|
|
559 "Search for any given whitespace REGEXP."
|
|
560 (let ((whitespace-retval ""))
|
|
561 (save-excursion
|
|
562 (goto-char (point-min))
|
|
563 (while (re-search-forward regexp nil t)
|
|
564 (setq whitespace-retval (format "%s %s " whitespace-retval
|
|
565 (match-beginning 0))))
|
|
566 (if (equal "" whitespace-retval)
|
|
567 nil
|
|
568 whitespace-retval))))
|
|
569
|
|
570 (defun whitespace-buffer-cleanup (regexp newregexp)
|
|
571 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
|
|
572 (save-excursion
|
|
573 (goto-char (point-min))
|
|
574 (while (re-search-forward regexp nil t)
|
|
575 (replace-match newregexp))))
|
|
576
|
|
577 (defun whitespace-indent-cleanup ()
|
25625
|
578 "Search for 8/more spaces at the start of a line and replace it with tabs."
|
25582
|
579 (save-excursion
|
|
580 (goto-char (point-min))
|
|
581 (while (re-search-forward whitespace-indent-regexp nil t)
|
|
582 (let ((column (current-column))
|
|
583 (indent-tabs-mode t))
|
|
584 (delete-region (match-beginning 0) (point))
|
|
585 (indent-to column)))))
|
|
586
|
26245
|
587 (defun whitespace-unchecked-whitespaces ()
|
|
588 "Return the list of whitespaces whose testing has been suppressed."
|
36944
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
589 (let ((unchecked-spaces
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
590 (concat (if (not whitespace-check-buffer-ateol) "e")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
591 (if (not whitespace-check-buffer-indent) "i")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
592 (if (not whitespace-check-buffer-leading) "l")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
593 (if (not whitespace-check-buffer-spacetab) "s")
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
594 (if (not whitespace-check-buffer-trailing) "t"))))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
595 (if (not (equal unchecked-spaces ""))
|
4d294c5bb2ac
whitespace.el version 3.0 with buffer local toggle capability.
Rajesh Vaidheeswarran <rv@gnu.org>
diff
changeset
|
596 unchecked-spaces
|
26245
|
597 nil)))
|
|
598
|
|
599 (defun whitespace-update-modeline (&optional whitespace-err)
|
32501
|
600 "Update modeline with whitespace errors.
|
|
601 Also with whitespaces whose testing has been turned off."
|
26245
|
602 (if whitespace-display-in-modeline
|
26259
|
603 (progn
|
|
604 (setq whitespace-mode-line nil)
|
|
605 ;; Whitespace errors
|
|
606 (if (and whitespace-err (not (equal whitespace-err "")))
|
|
607 (setq whitespace-mode-line whitespace-err))
|
|
608 ;; Whitespace suppressed errors
|
|
609 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
|
|
610 (if whitespace-unchecked
|
|
611 (setq whitespace-mode-line
|
|
612 (concat whitespace-mode-line "!" whitespace-unchecked))))
|
|
613 ;; Add the whitespace modeline prefix
|
|
614 (setq whitespace-mode-line (if whitespace-mode-line
|
|
615 (concat " W:" whitespace-mode-line)
|
|
616 nil))
|
|
617 (whitespace-force-mode-line-update))))
|
25724
|
618
|
25582
|
619 ;; Force mode line updation for different Emacs versions
|
|
620 (defun whitespace-force-mode-line-update ()
|
25625
|
621 "Force the mode line update for different flavors of Emacs."
|
32501
|
622 (if (fboundp 'redraw-modeline)
|
|
623 (redraw-modeline) ; XEmacs
|
|
624 (force-mode-line-update))) ; Emacs
|
25582
|
625
|
|
626 (defun whitespace-check-buffer-list (buf-name buf-file)
|
25625
|
627 "Add a buffer and its file to the whitespace monitor list.
|
|
628
|
|
629 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
|
|
630 periodically for whitespace."
|
25582
|
631 (if (and whitespace-mode (not (member (list buf-file buf-name)
|
|
632 whitespace-all-buffer-files)))
|
|
633 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
|
|
634
|
|
635 (defun whitespace-tickle-timer ()
|
25625
|
636 "Tickle timer to periodically to scan qualifying files for whitespace creep.
|
|
637
|
|
638 If timer is not set, then set it to scan the files in
|
|
639 `whitespace-all-buffer-files' periodically (defined by
|
|
640 `whitespace-rescan-timer-time') for whitespace creep."
|
25724
|
641 (if (and whitespace-rescan-timer-time (not whitespace-rescan-timer))
|
25582
|
642 (setq whitespace-rescan-timer
|
32501
|
643 (add-timeout whitespace-rescan-timer-time
|
|
644 'whitespace-rescan-files-in-buffers nil
|
|
645 whitespace-rescan-timer-time))))
|
25582
|
646
|
|
647 (defun whitespace-rescan-files-in-buffers (&optional arg)
|
25625
|
648 "Check monitored files for whitespace creep since last scan."
|
25582
|
649 (let ((whitespace-all-my-files whitespace-all-buffer-files)
|
25625
|
650 buffile bufname thiselt buf)
|
25582
|
651 (if (not whitespace-all-my-files)
|
|
652 (progn
|
32501
|
653 (disable-timeout whitespace-rescan-timer)
|
25582
|
654 (setq whitespace-rescan-timer nil))
|
|
655 (while whitespace-all-my-files
|
|
656 (setq thiselt (car whitespace-all-my-files))
|
|
657 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
|
|
658 (setq buffile (car thiselt))
|
|
659 (setq bufname (cadr thiselt))
|
|
660 (setq buf (get-buffer bufname))
|
|
661 (if (buffer-live-p buf)
|
|
662 (save-excursion
|
|
663 ;;(message "buffer %s live" bufname)
|
|
664 (set-buffer bufname)
|
|
665 (if whitespace-mode
|
|
666 (progn
|
|
667 ;;(message "checking for whitespace in %s" bufname)
|
|
668 (if whitespace-auto-cleanup
|
|
669 (progn
|
|
670 ;;(message "cleaning up whitespace in %s" bufname)
|
|
671 (whitespace-cleanup))
|
|
672 (progn
|
|
673 ;;(message "whitespace-buffer %s." (buffer-name))
|
|
674 (whitespace-buffer t))))
|
|
675 ;;(message "Removing %s from refresh list" bufname)
|
|
676 (whitespace-refresh-rescan-list buffile bufname)))
|
|
677 ;;(message "Removing %s from refresh list" bufname)
|
|
678 (whitespace-refresh-rescan-list buffile bufname))))))
|
|
679
|
|
680 (defun whitespace-refresh-rescan-list (buffile bufname)
|
25625
|
681 "Refresh the list of files to be rescaned for whitespace creep."
|
25582
|
682 (if whitespace-all-buffer-files
|
32501
|
683 (setq whitespace-all-buffer-files
|
|
684 (delete (list buffile bufname) whitespace-all-buffer-files))
|
|
685 (when whitespace-rescan-timer
|
|
686 (disable-timeout whitespace-rescan-timer)
|
|
687 (setq whitespace-rescan-timer nil))))
|
|
688
|
|
689 ;;;###autoload
|
|
690 (defcustom whitespace-global-mode nil
|
|
691 "Toggle global Whitespace mode.
|
|
692
|
|
693 Setting this variable directly does not take effect;
|
|
694 use either \\[customize] or the function `whitespace-global-mode'
|
|
695 \(which see)."
|
|
696 :set (lambda (sym val)
|
35050
|
697 (whitespace-global-mode (or val 0)))
|
32501
|
698 :initialize 'custom-initialize-default
|
|
699 :type 'boolean
|
|
700 :group 'whitespace
|
|
701 :require 'whitespace)
|
|
702
|
|
703 (defun whitespace-global-mode (&optional arg)
|
|
704 "Toggle using Whitespace mode in new buffers.
|
|
705 With ARG, turn the mode on if and only iff ARG is positive.
|
|
706
|
|
707 When this mode is active, `whitespace-buffer' is added to
|
|
708 `find-file-hooks' and `kill-buffer-hook'."
|
|
709 (interactive "P")
|
|
710 (setq arg (if arg
|
|
711 (> (prefix-numeric-value arg) 0)
|
|
712 (not whitespace-global-mode)))
|
|
713 (if arg
|
25582
|
714 (progn
|
32501
|
715 (add-hook 'find-file-hooks 'whitespace-buffer)
|
|
716 (add-hook 'kill-buffer-hook 'whitespace-buffer))
|
|
717 (remove-hook 'find-file-hooks 'whitespace-buffer)
|
|
718 (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
|
25582
|
719
|
25625
|
720 ;;;###autoload
|
|
721 (defun whitespace-describe ()
|
|
722 "A summary of whitespaces and what this library can do about them.
|
|
723
|
|
724 The whitespace library is intended to find and help fix five different types
|
|
725 of whitespace problems that commonly exist in source code.
|
|
726
|
|
727 1. Leading space (empty lines at the top of a file).
|
|
728 2. Trailing space (empty lines at the end of a file).
|
|
729 3. Indentation space (8 or more spaces at beginning of line, that should be
|
|
730 replaced with TABS).
|
32501
|
731 4. Spaces followed by a TAB. (Almost always, we never want that).
|
25625
|
732 5. Spaces or TABS at the end of a line.
|
|
733
|
|
734 Whitespace errors are reported in a buffer, and on the modeline.
|
|
735
|
26245
|
736 Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
|
|
737 where `x' and `y' can be one (or more) of:
|
25625
|
738
|
|
739 e - End-of-Line whitespace.
|
|
740 i - Indentation whitespace.
|
|
741 l - Leading whitespace.
|
|
742 s - Space followed by Tab.
|
|
743 t - Trailing whitespace.
|
|
744
|
25724
|
745 If any of the whitespace checks is turned off, the modeline will display a
|
26245
|
746 !<y>.
|
25724
|
747
|
25625
|
748 (since (3) is the most controversial one, here is the rationale: Most
|
|
749 terminal drivers and printer drivers have TAB configured or even
|
32501
|
750 hardcoded to be 8 spaces. (Some of them allow configuration, but almost
|
25625
|
751 always they default to 8.)
|
|
752
|
32501
|
753 Changing `tab-width' to other than 8 and editing will cause your code to
|
25625
|
754 look different from within Emacs, and say, if you cat it or more it, or
|
|
755 even print it.
|
|
756
|
|
757 Almost all the popular programming modes let you define an offset (like
|
|
758 c-basic-offset or perl-indent-level) to configure the offset, so you
|
32501
|
759 should never have to set your `tab-width' to be other than 8 in all these
|
|
760 modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs
|
|
761 to replace your 8 spaces with one \t (try it). If vi users in your
|
25625
|
762 office complain, tell them to use vim, which distinguishes between
|
|
763 tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
|
|
764 to set smarttab.)
|
|
765
|
|
766 All the above have caused (and will cause) unwanted codeline integration and
|
|
767 merge problems.
|
|
768
|
|
769 whitespace.el will complain if it detects whitespaces on opening a file, and
|
32501
|
770 warn you on closing a file also (in case you had inserted any
|
|
771 whitespaces during the process of your editing)."
|
25625
|
772 (interactive)
|
|
773 (message "Use C-h f whitespace-describe to read about whitespace.el v%s."
|
|
774 whitespace-version))
|
|
775
|
32501
|
776 (defun whitespace-unload-hook ()
|
|
777 (remove-hook 'find-file-hooks 'whitespace-buffer)
|
|
778 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
|
|
779
|
25582
|
780 (provide 'whitespace)
|
|
781
|
|
782 ;;; whitespace.el ends here
|