Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; whitespace.el --- warn about and clean bogus whitespaces in the file |
acc975b1 | 2 | |
e7cff550 | 3 | ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
acc975b1 | 4 | |
7f565d87 | 5 | ;; Author: Rajesh Vaidheeswarran <rv@gnu.org> |
acc975b1 DL |
6 | ;; Keywords: convenience |
7 | ||
e8af40ee | 8 | ;; $Id: whitespace.el,v 1.15 2001/03/22 21:31:51 rv Exp $ |
acc975b1 DL |
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. | |
24b72a45 RS |
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. | |
acc975b1 DL |
39 | |
40 | ;;; Code: | |
41 | ||
a3db02ad | 42 | (defvar whitespace-version "3.0" "Version of the whitespace library.") |
acc975b1 | 43 | |
24b72a45 RS |
44 | (defvar whitespace-all-buffer-files nil |
45 | "An associated list of buffers and files checked for whitespace cleanliness. | |
acc975b1 | 46 | |
24b72a45 RS |
47 | This is to enable periodic checking of whitespace cleanliness in the files |
48 | visited by the buffers.") | |
acc975b1 | 49 | |
24b72a45 RS |
50 | (defvar whitespace-rescan-timer nil |
51 | "Timer object used to rescan the files in buffers that have been modified.") | |
acc975b1 | 52 | |
dd24f431 GM |
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 | ||
a3db02ad RV |
64 | (defvar whitespace-check-buffer-leading nil |
65 | "Test leading whitespace for file in current buffer if t") | |
66 | (make-variable-buffer-local 'whitespace-check-buffer-leading) | |
67 | (put 'whitespace-check-buffer-leading 'permanent-local nil) | |
68 | ||
69 | (defvar whitespace-check-buffer-trailing nil | |
70 | "Test trailing whitespace for file in current buffer if t") | |
71 | (make-variable-buffer-local 'whitespace-check-buffer-trailing) | |
72 | (put 'whitespace-check-buffer-trailing 'permanent-local nil) | |
73 | ||
74 | (defvar whitespace-check-buffer-indent nil | |
75 | "Test indentation whitespace for file in current buffer if t") | |
76 | (make-variable-buffer-local 'whitespace-check-buffer-indent) | |
77 | (put 'whitespace-check-buffer-indent 'permanent-local nil) | |
78 | ||
79 | (defvar whitespace-check-buffer-spacetab nil | |
80 | "Test Space-followed-by-TABS whitespace for file in current buffer if t") | |
81 | (make-variable-buffer-local 'whitespace-check-buffer-spacetab) | |
82 | (put 'whitespace-check-buffer-spacetab 'permanent-local nil) | |
83 | ||
84 | (defvar whitespace-check-buffer-ateol nil | |
85 | "Test end-of-line whitespace for file in current buffer if t") | |
86 | (make-variable-buffer-local 'whitespace-check-buffer-ateol) | |
87 | (put 'whitespace-check-buffer-ateol 'permanent-local nil) | |
88 | ||
07dc4175 | 89 | ;; For flavors of Emacs which don't define `defgroup' and `defcustom'. |
24b72a45 | 90 | (eval-when-compile |
07dc4175 GM |
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)))) | |
acc975b1 | 101 | |
fc333790 | 102 | (if (featurep 'xemacs) |
24b72a45 RS |
103 | (defgroup whitespace nil |
104 | "Check for and fix five different types of whitespaces in source code." | |
acc975b1 DL |
105 | ;; Since XEmacs doesn't have a 'convenience group, use the next best group |
106 | ;; which is 'editing? | |
fc333790 DL |
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)) | |
acc975b1 | 112 | |
18d51459 | 113 | (defcustom whitespace-check-leading-whitespace t |
a3db02ad RV |
114 | "Flag to check leading whitespace. This is the global for the system. |
115 | It can be overriden by setting a buffer local variable | |
116 | `whitespace-check-buffer-leading'" | |
18d51459 RS |
117 | :type 'boolean |
118 | :group 'whitespace) | |
119 | ||
120 | (defcustom whitespace-check-trailing-whitespace t | |
a3db02ad RV |
121 | "Flag to check trailing whitespace. This is the global for the system. |
122 | It can be overriden by setting a buffer local variable | |
123 | `whitespace-check-buffer-trailing'" | |
18d51459 RS |
124 | :type 'boolean |
125 | :group 'whitespace) | |
126 | ||
127 | (defcustom whitespace-check-spacetab-whitespace t | |
a3db02ad RV |
128 | "Flag to check space followed by a TAB. This is the global for the system. |
129 | It can be overriden by setting a buffer local variable | |
130 | `whitespace-check-buffer-spacetab'" | |
18d51459 RS |
131 | :type 'boolean |
132 | :group 'whitespace) | |
133 | ||
24b72a45 | 134 | (defcustom whitespace-spacetab-regexp " \t" |
18d51459 | 135 | "Regexp to match a space followed by a TAB." |
fc333790 | 136 | :type 'regexp |
24b72a45 | 137 | :group 'whitespace) |
acc975b1 | 138 | |
18d51459 | 139 | (defcustom whitespace-check-indent-whitespace t |
a3db02ad RV |
140 | "Flag to check indentation whitespace. This is the global for the system. |
141 | It can be overriden by setting a buffer local variable | |
142 | `whitespace-check-buffer-indent'" | |
18d51459 RS |
143 | :type 'boolean |
144 | :group 'whitespace) | |
145 | ||
24b72a45 RS |
146 | (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ") |
147 | "Regexp to match (any TABS followed by) 8/more whitespaces at start of line." | |
fc333790 | 148 | :type 'regexp |
24b72a45 RS |
149 | :group 'whitespace) |
150 | ||
18d51459 | 151 | (defcustom whitespace-check-ateol-whitespace t |
a3db02ad RV |
152 | "Flag to check end-of-line whitespace. This is the global for the system. |
153 | It can be overriden by setting a buffer local variable | |
154 | `whitespace-check-buffer-ateol'" | |
18d51459 RS |
155 | :type 'boolean |
156 | :group 'whitespace) | |
157 | ||
24b72a45 RS |
158 | (defcustom whitespace-ateol-regexp "[ \t]$" |
159 | "Regexp to match a TAB or a space at the EOL." | |
fc333790 | 160 | :type 'regexp |
24b72a45 RS |
161 | :group 'whitespace) |
162 | ||
163 | (defcustom whitespace-errbuf "*Whitespace Errors*" | |
fc333790 | 164 | "The name of the buffer where whitespace related messages will be logged." |
24b72a45 RS |
165 | :type 'string |
166 | :group 'whitespace) | |
167 | ||
168 | (defcustom whitespace-auto-cleanup nil | |
169 | "Cleanup a buffer automatically on finding it whitespace unclean." | |
acc975b1 DL |
170 | :type 'boolean |
171 | :group 'whitespace) | |
172 | ||
173 | (defcustom whitespace-silent nil | |
24b72a45 | 174 | "All whitespace errors will be shown only in the modeline when t. |
acc975b1 DL |
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 | |
24b72a45 | 178 | Errors*' buffer before opening (or closing) another file." |
acc975b1 DL |
179 | :type 'boolean |
180 | :group 'whitespace) | |
181 | ||
182 | (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode | |
18d51459 RS |
183 | c-mode c++-mode cc-mode |
184 | change-log-mode cperl-mode | |
acc975b1 DL |
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 | |
18d51459 RS |
193 | sh-mode shell-script-mode simula-mode |
194 | tcl-mode tex-mode texinfo-mode | |
195 | vrml-mode xml-mode) | |
acc975b1 | 196 | |
24b72a45 | 197 | "Major Modes in which we turn on whitespace checking. |
acc975b1 | 198 | |
fc333790 | 199 | These are mostly programming and documentation modes. But you may add other |
24b72a45 RS |
200 | modes that you want whitespaces checked in by adding something like the |
201 | following to your `.emacs': | |
acc975b1 | 202 | |
24b72a45 RS |
203 | \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode |
204 | whitespace-modes))\) | |
acc975b1 | 205 | |
24b72a45 | 206 | Or, alternately, you can use the Emacs `customize' command to set this." |
fc333790 | 207 | :type '(repeat symbol) |
24b72a45 | 208 | :group 'whitespace) |
acc975b1 | 209 | |
7f565d87 | 210 | (defcustom whitespace-rescan-timer-time 600 |
24b72a45 RS |
211 | "Period in seconds to rescan modified buffers for whitespace creep. |
212 | ||
213 | This is the period after which the timer will fire causing | |
214 | `whitespace-rescan-files-in-buffers' to check for whitespace creep in | |
18d51459 RS |
215 | modified buffers. |
216 | ||
217 | To disable timer scans, set this to zero." | |
acc975b1 DL |
218 | :type 'integer |
219 | :group 'whitespace) | |
220 | ||
dd24f431 GM |
221 | (defcustom whitespace-display-in-modeline t |
222 | "Display whitespace errors on the modeline." | |
223 | :type 'boolean | |
224 | :group 'whitespace) | |
acc975b1 DL |
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 | ||
a3db02ad RV |
230 | (set-default 'whitespace-check-buffer-leading |
231 | whitespace-check-leading-whitespace) | |
232 | (set-default 'whitespace-check-buffer-trailing | |
233 | whitespace-check-trailing-whitespace) | |
234 | (set-default 'whitespace-check-buffer-indent | |
235 | whitespace-check-indent-whitespace) | |
236 | (set-default 'whitespace-check-buffer-spacetab | |
237 | whitespace-check-spacetab-whitespace) | |
238 | (set-default 'whitespace-check-buffer-ateol | |
239 | whitespace-check-ateol-whitespace) | |
240 | ||
acc975b1 | 241 | (defun whitespace-check-whitespace-mode (&optional arg) |
24b72a45 | 242 | "Test and set the whitespace-mode in qualifying buffers." |
acc975b1 DL |
243 | (if (null whitespace-mode) |
244 | (setq whitespace-mode | |
245 | (if (or arg (member major-mode whitespace-modes)) | |
246 | t | |
247 | nil)))) | |
248 | ||
a3db02ad RV |
249 | ;;;###autoload |
250 | (defun whitespace-toggle-leading-check () | |
251 | "Toggle the check for leading space in the local buffer." | |
252 | (interactive) | |
253 | (let ((current-val whitespace-check-buffer-leading)) | |
254 | (setq whitespace-check-buffer-leading (not current-val)) | |
255 | (message "Will%s check for leading space in buffer." | |
256 | (if whitespace-check-buffer-leading "" " not")) | |
257 | (if whitespace-check-buffer-leading (whitespace-buffer-leading)))) | |
258 | ||
259 | ;;;###autoload | |
260 | (defun whitespace-toggle-trailing-check () | |
261 | "Toggle the check for trailing space in the local buffer." | |
262 | (interactive) | |
263 | (let ((current-val whitespace-check-buffer-trailing)) | |
264 | (setq whitespace-check-buffer-trailing (not current-val)) | |
265 | (message "Will%s check for trailing space in buffer." | |
266 | (if whitespace-check-buffer-trailing "" " not")) | |
267 | (if whitespace-check-buffer-trailing (whitespace-buffer-trailing)))) | |
268 | ||
269 | ;;;###autoload | |
270 | (defun whitespace-toggle-indent-check () | |
271 | "Toggle the check for indentation space in the local buffer." | |
272 | (interactive) | |
273 | (let ((current-val whitespace-check-buffer-indent)) | |
274 | (setq whitespace-check-buffer-indent (not current-val)) | |
275 | (message "Will%s check for indentation space in buffer." | |
276 | (if whitespace-check-buffer-indent "" " not")) | |
277 | (if whitespace-check-buffer-indent | |
278 | (whitespace-buffer-search whitespace-indent-regexp)))) | |
279 | ||
280 | ;;;###autoload | |
281 | (defun whitespace-toggle-spacetab-check () | |
282 | "Toggle the check for space-followed-by-TABs in the local buffer." | |
283 | (interactive) | |
284 | (let ((current-val whitespace-check-buffer-spacetab)) | |
285 | (setq whitespace-check-buffer-spacetab (not current-val)) | |
286 | (message "Will%s check for space-followed-by-TABs in buffer." | |
287 | (if whitespace-check-buffer-spacetab "" " not")) | |
288 | (if whitespace-check-buffer-spacetab | |
289 | (whitespace-buffer-search whitespace-spacetab-regexp)))) | |
290 | ||
291 | ||
292 | ;;;###autoload | |
293 | (defun whitespace-toggle-ateol-check () | |
294 | "Toggle the check for end-of-line space in the local buffer." | |
295 | (interactive) | |
296 | (let ((current-val whitespace-check-buffer-ateol)) | |
297 | (setq whitespace-check-buffer-ateol (not current-val)) | |
298 | (message "Will%s check for end-of-line space in buffer." | |
299 | (if whitespace-check-buffer-ateol "" " not")) | |
300 | (if whitespace-check-buffer-ateol | |
301 | (whitespace-buffer-search whitespace-ateol-regexp)))) | |
302 | ||
303 | ||
24b72a45 | 304 | ;;;###autoload |
acc975b1 | 305 | (defun whitespace-buffer (&optional quiet) |
fc333790 DL |
306 | "Find five different types of white spaces in buffer. |
307 | These are: | |
acc975b1 DL |
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 | |
24b72a45 | 317 | 2. `whitespace-buffer' was explicitly called with a prefix argument." |
acc975b1 | 318 | (interactive) |
dd24f431 GM |
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)) | |
a3db02ad | 330 | (let ((whitespace-leading (if whitespace-check-buffer-leading |
dd24f431 GM |
331 | (whitespace-buffer-leading) |
332 | nil)) | |
a3db02ad | 333 | (whitespace-trailing (if whitespace-check-buffer-trailing |
dd24f431 GM |
334 | (whitespace-buffer-trailing) |
335 | nil)) | |
a3db02ad | 336 | (whitespace-indent (if whitespace-check-buffer-indent |
18d51459 | 337 | (whitespace-buffer-search |
dd24f431 | 338 | whitespace-indent-regexp) |
18d51459 | 339 | nil)) |
a3db02ad | 340 | (whitespace-spacetab (if whitespace-check-buffer-spacetab |
dd24f431 GM |
341 | (whitespace-buffer-search |
342 | whitespace-spacetab-regexp) | |
343 | nil)) | |
a3db02ad | 344 | (whitespace-ateol (if whitespace-check-buffer-ateol |
dd24f431 GM |
345 | (whitespace-buffer-search |
346 | whitespace-ateol-regexp) | |
347 | nil)) | |
348 | (whitespace-errmsg nil) | |
349 | (whitespace-filename buffer-file-name) | |
350 | (whitespace-this-modeline "")) | |
351 | ||
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)) | |
356 | ||
357 | (if whitespace-error | |
acc975b1 | 358 | (progn |
dd24f431 GM |
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))) | |
acc975b1 | 407 | |
24b72a45 | 408 | ;;;###autoload |
acc975b1 | 409 | (defun whitespace-region (s e) |
fc333790 | 410 | "Check the region for whitespace errors." |
acc975b1 DL |
411 | (interactive "r") |
412 | (save-excursion | |
413 | (save-restriction | |
414 | (narrow-to-region s e) | |
415 | (whitespace-buffer)))) | |
416 | ||
24b72a45 | 417 | ;;;###autoload |
acc975b1 | 418 | (defun whitespace-cleanup () |
24b72a45 RS |
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." | |
acc975b1 DL |
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 | ||
a3db02ad | 435 | (if (and whitespace-check-buffer-leading |
18d51459 | 436 | (whitespace-buffer-leading)) |
acc975b1 DL |
437 | (progn |
438 | (whitespace-buffer-leading-cleanup) | |
439 | (setq whitespace-any t))) | |
440 | ||
a3db02ad | 441 | (if (and whitespace-check-buffer-trailing |
18d51459 | 442 | (whitespace-buffer-trailing)) |
acc975b1 DL |
443 | (progn |
444 | (whitespace-buffer-trailing-cleanup) | |
445 | (setq whitespace-any t))) | |
446 | ||
a3db02ad | 447 | (if (and whitespace-check-buffer-indent |
18d51459 | 448 | (whitespace-buffer-search whitespace-indent-regexp)) |
acc975b1 DL |
449 | (progn |
450 | (whitespace-indent-cleanup) | |
451 | (setq whitespace-any t))) | |
452 | ||
a3db02ad | 453 | (if (and whitespace-check-buffer-spacetab |
18d51459 | 454 | (whitespace-buffer-search whitespace-spacetab-regexp)) |
acc975b1 DL |
455 | (progn |
456 | (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t") | |
457 | (setq whitespace-any t))) | |
458 | ||
a3db02ad | 459 | (if (and whitespace-check-buffer-ateol |
18d51459 | 460 | (whitespace-buffer-search whitespace-ateol-regexp)) |
acc975b1 DL |
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 | |
18d51459 RS |
466 | (if whitespace-any |
467 | (whitespace-cleanup) | |
acc975b1 DL |
468 | (progn |
469 | (message "%s clean" buffer-file-name) | |
dd24f431 | 470 | (whitespace-update-modeline))) |
acc975b1 DL |
471 | (setq tab-width whitespace-tabwith-saved)))) |
472 | ||
24b72a45 | 473 | ;;;###autoload |
acc975b1 | 474 | (defun whitespace-cleanup-region (s e) |
fc333790 | 475 | "Whitespace cleanup on the region." |
acc975b1 DL |
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 () | |
24b72a45 | 498 | "Remove any empty lines at the top of the file." |
acc975b1 DL |
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 () | |
24b72a45 | 578 | "Search for 8/more spaces at the start of a line and replace it with tabs." |
acc975b1 DL |
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 | ||
dd24f431 GM |
587 | (defun whitespace-unchecked-whitespaces () |
588 | "Return the list of whitespaces whose testing has been suppressed." | |
a3db02ad RV |
589 | (let ((unchecked-spaces |
590 | (concat (if (not whitespace-check-buffer-ateol) "e") | |
591 | (if (not whitespace-check-buffer-indent) "i") | |
592 | (if (not whitespace-check-buffer-leading) "l") | |
593 | (if (not whitespace-check-buffer-spacetab) "s") | |
594 | (if (not whitespace-check-buffer-trailing) "t")))) | |
595 | (if (not (equal unchecked-spaces "")) | |
596 | unchecked-spaces | |
dd24f431 GM |
597 | nil))) |
598 | ||
599 | (defun whitespace-update-modeline (&optional whitespace-err) | |
fc333790 DL |
600 | "Update modeline with whitespace errors. |
601 | Also with whitespaces whose testing has been turned off." | |
dd24f431 | 602 | (if whitespace-display-in-modeline |
0e38b2a2 GM |
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)))) | |
18d51459 | 618 | |
acc975b1 DL |
619 | ;; Force mode line updation for different Emacs versions |
620 | (defun whitespace-force-mode-line-update () | |
24b72a45 | 621 | "Force the mode line update for different flavors of Emacs." |
fc333790 DL |
622 | (if (fboundp 'redraw-modeline) |
623 | (redraw-modeline) ; XEmacs | |
624 | (force-mode-line-update))) ; Emacs | |
acc975b1 DL |
625 | |
626 | (defun whitespace-check-buffer-list (buf-name buf-file) | |
24b72a45 RS |
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." | |
acc975b1 DL |
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 () | |
24b72a45 RS |
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." | |
18d51459 | 641 | (if (and whitespace-rescan-timer-time (not whitespace-rescan-timer)) |
acc975b1 | 642 | (setq whitespace-rescan-timer |
fc333790 DL |
643 | (add-timeout whitespace-rescan-timer-time |
644 | 'whitespace-rescan-files-in-buffers nil | |
645 | whitespace-rescan-timer-time)))) | |
acc975b1 DL |
646 | |
647 | (defun whitespace-rescan-files-in-buffers (&optional arg) | |
24b72a45 | 648 | "Check monitored files for whitespace creep since last scan." |
acc975b1 | 649 | (let ((whitespace-all-my-files whitespace-all-buffer-files) |
24b72a45 | 650 | buffile bufname thiselt buf) |
acc975b1 DL |
651 | (if (not whitespace-all-my-files) |
652 | (progn | |
fc333790 | 653 | (disable-timeout whitespace-rescan-timer) |
acc975b1 DL |
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) | |
24b72a45 | 681 | "Refresh the list of files to be rescaned for whitespace creep." |
acc975b1 | 682 | (if whitespace-all-buffer-files |
fc333790 DL |
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) | |
75d2d89f | 697 | (whitespace-global-mode (or val 0))) |
fc333790 DL |
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 | |
acc975b1 | 714 | (progn |
fc333790 DL |
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))) | |
acc975b1 | 719 | |
24b72a45 RS |
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). | |
fc333790 | 731 | 4. Spaces followed by a TAB. (Almost always, we never want that). |
24b72a45 RS |
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 | ||
dd24f431 GM |
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: | |
24b72a45 RS |
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 | ||
18d51459 | 745 | If any of the whitespace checks is turned off, the modeline will display a |
dd24f431 | 746 | !<y>. |
18d51459 | 747 | |
24b72a45 RS |
748 | (since (3) is the most controversial one, here is the rationale: Most |
749 | terminal drivers and printer drivers have TAB configured or even | |
fc333790 | 750 | hardcoded to be 8 spaces. (Some of them allow configuration, but almost |
24b72a45 RS |
751 | always they default to 8.) |
752 | ||
fc333790 | 753 | Changing `tab-width' to other than 8 and editing will cause your code to |
24b72a45 RS |
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 | |
fc333790 DL |
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 | |
24b72a45 RS |
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 | |
fc333790 DL |
770 | warn you on closing a file also (in case you had inserted any |
771 | whitespaces during the process of your editing)." | |
24b72a45 RS |
772 | (interactive) |
773 | (message "Use C-h f whitespace-describe to read about whitespace.el v%s." | |
774 | whitespace-version)) | |
775 | ||
fc333790 DL |
776 | (defun whitespace-unload-hook () |
777 | (remove-hook 'find-file-hooks 'whitespace-buffer) | |
778 | (remove-hook 'kill-buffer-hook 'whitespace-buffer)) | |
779 | ||
acc975b1 DL |
780 | (provide 'whitespace) |
781 | ||
782 | ;;; whitespace.el ends here |