1 ;;; whitespace.el --- Warn about and clean bogus whitespaces in the file.
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
6 ;; Keywords: convenience
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Whitespace.el URL: http://www.dsmit.com/lisp/
29 ;; Exported functions:
31 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
32 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
33 ;; `whitespace-region' - To check between point and mark for whitespace
35 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
36 ;; and mark in the current buffer.
37 ;; `whitespace-describe' - A simple introduction to the library.
41 (defvar whitespace-version
"2.8" "Version of the whitespace library.")
43 (defvar whitespace-all-buffer-files nil
44 "An associated list of buffers and files checked for whitespace cleanliness.
46 This is to enable periodic checking of whitespace cleanliness in the files
47 visited by the buffers.")
49 (defvar whitespace-rescan-timer nil
50 "Timer object used to rescan the files in buffers that have been modified.")
52 ;; Tell Emacs about this new kind of minor mode
53 (defvar whitespace-mode nil
54 "Non-nil when Whitespace mode (a minor mode) is enabled.")
55 (make-variable-buffer-local 'whitespace-mode
)
56 (put 'whitespace-mode
'permanent-local nil
)
58 (defvar whitespace-mode-line nil
59 "String to display in the mode line for Whitespace mode.")
60 (make-variable-buffer-local 'whitespace-mode-line
)
61 (put 'whitespace-mode-line
'permanent-local nil
)
63 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
65 (if (not (fboundp 'defgroup
))
66 (defmacro defgroup
(sym memb doc
&rest args
)
67 "Null macro for defgroup in all versions of Emacs that don't define
70 (if (not (fboundp 'defcustom
))
71 (defmacro defcustom
(sym val doc
&rest args
)
72 "Macro to alias defcustom to defvar in all versions of Emacs that
73 don't define defcustom"
74 `(defvar ,sym
,val
,doc
))))
76 (if (featurep 'xemacs
)
77 (defgroup whitespace nil
78 "Check for and fix five different types of whitespaces in source code."
79 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
82 (defgroup whitespace nil
83 "Check for and fix five different types of whitespaces in source code."
87 (defcustom whitespace-check-leading-whitespace t
88 "Flag to check leading whitespace."
92 (defcustom whitespace-check-trailing-whitespace t
93 "Flag to check trailing whitespace."
97 (defcustom whitespace-check-spacetab-whitespace t
98 "Flag to check space followed by a TAB."
102 (defcustom whitespace-spacetab-regexp
" \t"
103 "Regexp to match a space followed by a TAB."
107 (defcustom whitespace-check-indent-whitespace t
108 "Flag to check indentation whitespace."
112 (defcustom whitespace-indent-regexp
(concat "^\\(\t*\\) " " ")
113 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
117 (defcustom whitespace-check-ateol-whitespace t
118 "Flag to check end-of-line whitespace."
122 (defcustom whitespace-ateol-regexp
"[ \t]$"
123 "Regexp to match a TAB or a space at the EOL."
127 (defcustom whitespace-errbuf
"*Whitespace Errors*"
128 "The name of the buffer where whitespace related messages will be logged."
132 (defcustom whitespace-auto-cleanup nil
133 "Cleanup a buffer automatically on finding it whitespace unclean."
137 (defcustom whitespace-silent nil
138 "All whitespace errors will be shown only in the modeline when t.
140 Note that setting this may cause all whitespaces introduced in a file to go
141 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
142 Errors*' buffer before opening (or closing) another file."
146 (defcustom whitespace-modes
'(ada-mode asm-mode autoconf-mode awk-mode
147 c-mode c
++-mode cc-mode
148 change-log-mode cperl-mode
149 electric-nroff-mode emacs-lisp-mode
150 f90-mode fortran-mode html-mode
151 html3-mode java-mode jde-mode
152 ksh-mode latex-mode LaTeX-mode
153 lisp-mode m4-mode makefile-mode
154 modula-2-mode nroff-mode objc-mode
155 pascal-mode perl-mode prolog-mode
156 python-mode scheme-mode sgml-mode
157 sh-mode shell-script-mode simula-mode
158 tcl-mode tex-mode texinfo-mode
161 "Major Modes in which we turn on whitespace checking.
163 These are mostly programming and documentation modes. But you may add other
164 modes that you want whitespaces checked in by adding something like the
165 following to your `.emacs':
167 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
170 Or, alternately, you can use the Emacs `customize' command to set this."
171 :type
'(repeat symbol
)
174 (defcustom whitespace-rescan-timer-time
600
175 "Period in seconds to rescan modified buffers for whitespace creep.
177 This is the period after which the timer will fire causing
178 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
181 To disable timer scans, set this to zero."
185 (defcustom whitespace-display-in-modeline t
186 "Display whitespace errors on the modeline."
190 (if (not (assoc 'whitespace-mode minor-mode-alist
))
191 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
194 (defun whitespace-check-whitespace-mode (&optional arg
)
195 "Test and set the whitespace-mode in qualifying buffers."
196 (if (null whitespace-mode
)
197 (setq whitespace-mode
198 (if (or arg
(member major-mode whitespace-modes
))
203 (defun whitespace-buffer (&optional quiet
)
204 "Find five different types of white spaces in buffer.
206 1. Leading space \(empty lines at the top of a file\).
207 2. Trailing space \(empty lines at the end of a file\).
208 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
209 4. Spaces followed by a TAB. \(Almost always, we never want that\).
210 5. Spaces or TABS at the end of a line.
212 Check for whitespace only if this buffer really contains a non-empty file
214 1. the major mode is one of the whitespace-modes, or
215 2. `whitespace-buffer' was explicitly called with a prefix argument."
217 (let ((whitespace-error nil
))
218 (whitespace-check-whitespace-mode current-prefix-arg
)
219 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
221 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
222 (whitespace-tickle-timer)
223 (if whitespace-auto-cleanup
226 (message "Can't cleanup: %s is read-only" (buffer-name)))
227 (whitespace-cleanup))
228 (let ((whitespace-leading (if whitespace-check-leading-whitespace
229 (whitespace-buffer-leading)
231 (whitespace-trailing (if whitespace-check-trailing-whitespace
232 (whitespace-buffer-trailing)
234 (whitespace-indent (if whitespace-check-indent-whitespace
235 (whitespace-buffer-search
236 whitespace-indent-regexp
)
238 (whitespace-spacetab (if whitespace-check-spacetab-whitespace
239 (whitespace-buffer-search
240 whitespace-spacetab-regexp
)
242 (whitespace-ateol (if whitespace-check-ateol-whitespace
243 (whitespace-buffer-search
244 whitespace-ateol-regexp
)
246 (whitespace-errmsg nil
)
247 (whitespace-filename buffer-file-name
)
248 (whitespace-this-modeline ""))
250 ;; Now let's complain if we found any of the above.
251 (setq whitespace-error
(or whitespace-leading whitespace-indent
252 whitespace-spacetab whitespace-ateol
253 whitespace-trailing
))
257 (setq whitespace-errmsg
258 (concat whitespace-filename
" contains:\n"
259 (if whitespace-leading
260 "Leading whitespace\n")
261 (if whitespace-indent
262 (concat "Indentation whitespace"
263 whitespace-indent
"\n"))
264 (if whitespace-spacetab
265 (concat "Space followed by Tab"
266 whitespace-spacetab
"\n"))
268 (concat "End-of-line whitespace"
269 whitespace-ateol
"\n"))
270 (if whitespace-trailing
271 "Trailing whitespace\n")
272 "\ntype `M-x whitespace-cleanup' to "
273 "cleanup the file."))
274 (setq whitespace-this-modeline
275 (concat (if whitespace-ateol
"e")
276 (if whitespace-indent
"i")
277 (if whitespace-leading
"l")
278 (if whitespace-spacetab
"s")
279 (if whitespace-trailing
"t")))))
280 (whitespace-update-modeline whitespace-this-modeline
)
282 (get-buffer-create whitespace-errbuf
)
283 (kill-buffer whitespace-errbuf
)
284 (get-buffer-create whitespace-errbuf
)
285 (set-buffer whitespace-errbuf
)
286 (if whitespace-errmsg
288 (insert whitespace-errmsg
)
289 (if (not (or quiet whitespace-silent
))
290 (display-buffer whitespace-errbuf t
))
292 (message "Whitespaces: [%s%s] in %s"
293 whitespace-this-modeline
294 (let ((whitespace-unchecked
295 (whitespace-unchecked-whitespaces)))
296 (if whitespace-unchecked
297 (concat "!" whitespace-unchecked
)
299 whitespace-filename
)))
301 (message "%s clean" whitespace-filename
))))))))
307 (defun whitespace-region (s e
)
308 "Check the region for whitespace errors."
312 (narrow-to-region s e
)
313 (whitespace-buffer))))
316 (defun whitespace-cleanup ()
317 "Cleanup the five different kinds of whitespace problems.
319 Use \\[describe-function] whitespace-describe to read a summary of the
320 whitespace problems."
322 ;; If this buffer really contains a file, then run, else quit.
323 (whitespace-check-whitespace-mode current-prefix-arg
)
324 (if (and buffer-file-name whitespace-mode
)
325 (let ((whitespace-any nil
)
326 (whitespace-tabwith 8)
327 (whitespace-tabwith-saved tab-width
))
329 ;; since all printable TABS should be 8, irrespective of how
330 ;; they are displayed.
331 (setq tab-width whitespace-tabwith
)
333 (if (and whitespace-check-leading-whitespace
334 (whitespace-buffer-leading))
336 (whitespace-buffer-leading-cleanup)
337 (setq whitespace-any t
)))
339 (if (and whitespace-check-trailing-whitespace
340 (whitespace-buffer-trailing))
342 (whitespace-buffer-trailing-cleanup)
343 (setq whitespace-any t
)))
345 (if (and whitespace-check-indent-whitespace
346 (whitespace-buffer-search whitespace-indent-regexp
))
348 (whitespace-indent-cleanup)
349 (setq whitespace-any t
)))
351 (if (and whitespace-check-spacetab-whitespace
352 (whitespace-buffer-search whitespace-spacetab-regexp
))
354 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
355 (setq whitespace-any t
)))
357 (if (and whitespace-check-ateol-whitespace
358 (whitespace-buffer-search whitespace-ateol-regexp
))
360 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
361 (setq whitespace-any t
)))
363 ;; Call this recursively till everything is taken care of
367 (message "%s clean" buffer-file-name
)
368 (whitespace-update-modeline)))
369 (setq tab-width whitespace-tabwith-saved
))))
372 (defun whitespace-cleanup-region (s e
)
373 "Whitespace cleanup on the region."
377 (narrow-to-region s e
)
378 (whitespace-cleanup))
379 (whitespace-buffer t
)))
381 (defun whitespace-buffer-leading ()
382 "Check to see if there are any empty lines at the top of the file."
386 (goto-char (point-min))
391 (if (equal pmin pmax
)
395 (defun whitespace-buffer-leading-cleanup ()
396 "Remove any empty lines at the top of the file."
400 (goto-char (point-min))
405 (if (equal pmin pmax
)
408 (whitespace-buffer-leading-cleanup))))))
410 (defun whitespace-buffer-trailing ()
411 "Check to see if are is more than one empty line at the bottom."
415 (goto-char (point-max))
420 (if (equal pmin pmax
)
422 (goto-char (- (point) 1))
427 (if (equal pmin pmax
)
432 (defun whitespace-buffer-trailing-cleanup ()
433 "Delete all the empty lines at the bottom."
437 (goto-char (point-max))
442 (if (equal pmin pmax
)
444 (goto-char (1- pmin
))
449 (if (equal pmin pmax
)
451 (goto-char (1- (point-max)))
454 (whitespace-buffer-trailing-cleanup))))))))
456 (defun whitespace-buffer-search (regexp)
457 "Search for any given whitespace REGEXP."
458 (let ((whitespace-retval ""))
460 (goto-char (point-min))
461 (while (re-search-forward regexp nil t
)
462 (setq whitespace-retval
(format "%s %s " whitespace-retval
463 (match-beginning 0))))
464 (if (equal "" whitespace-retval
)
466 whitespace-retval
))))
468 (defun whitespace-buffer-cleanup (regexp newregexp
)
469 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
471 (goto-char (point-min))
472 (while (re-search-forward regexp nil t
)
473 (replace-match newregexp
))))
475 (defun whitespace-indent-cleanup ()
476 "Search for 8/more spaces at the start of a line and replace it with tabs."
478 (goto-char (point-min))
479 (while (re-search-forward whitespace-indent-regexp nil t
)
480 (let ((column (current-column))
481 (indent-tabs-mode t
))
482 (delete-region (match-beginning 0) (point))
483 (indent-to column
)))))
485 (defun whitespace-unchecked-whitespaces ()
486 "Return the list of whitespaces whose testing has been suppressed."
487 (let ((whitespace-this-modeline
488 (concat (if (not whitespace-check-ateol-whitespace
) "e")
489 (if (not whitespace-check-indent-whitespace
) "i")
490 (if (not whitespace-check-leading-whitespace
) "l")
491 (if (not whitespace-check-spacetab-whitespace
) "s")
492 (if (not whitespace-check-trailing-whitespace
) "t"))))
493 (if (not (equal whitespace-this-modeline
""))
494 whitespace-this-modeline
497 (defun whitespace-update-modeline (&optional whitespace-err
)
498 "Update modeline with whitespace errors.
499 Also with whitespaces whose testing has been turned off."
500 (if whitespace-display-in-modeline
502 (setq whitespace-mode-line nil
)
504 (if (and whitespace-err
(not (equal whitespace-err
"")))
505 (setq whitespace-mode-line whitespace-err
))
506 ;; Whitespace suppressed errors
507 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
508 (if whitespace-unchecked
509 (setq whitespace-mode-line
510 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
511 ;; Add the whitespace modeline prefix
512 (setq whitespace-mode-line
(if whitespace-mode-line
513 (concat " W:" whitespace-mode-line
)
515 (whitespace-force-mode-line-update))))
517 ;; Force mode line updation for different Emacs versions
518 (defun whitespace-force-mode-line-update ()
519 "Force the mode line update for different flavors of Emacs."
520 (if (fboundp 'redraw-modeline
)
521 (redraw-modeline) ; XEmacs
522 (force-mode-line-update))) ; Emacs
524 (defun whitespace-check-buffer-list (buf-name buf-file
)
525 "Add a buffer and its file to the whitespace monitor list.
527 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
528 periodically for whitespace."
529 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
530 whitespace-all-buffer-files
)))
531 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
533 (defun whitespace-tickle-timer ()
534 "Tickle timer to periodically to scan qualifying files for whitespace creep.
536 If timer is not set, then set it to scan the files in
537 `whitespace-all-buffer-files' periodically (defined by
538 `whitespace-rescan-timer-time') for whitespace creep."
539 (if (and whitespace-rescan-timer-time
(not whitespace-rescan-timer
))
540 (setq whitespace-rescan-timer
541 (add-timeout whitespace-rescan-timer-time
542 'whitespace-rescan-files-in-buffers nil
543 whitespace-rescan-timer-time
))))
545 (defun whitespace-rescan-files-in-buffers (&optional arg
)
546 "Check monitored files for whitespace creep since last scan."
547 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
548 buffile bufname thiselt buf
)
549 (if (not whitespace-all-my-files
)
551 (disable-timeout whitespace-rescan-timer
)
552 (setq whitespace-rescan-timer nil
))
553 (while whitespace-all-my-files
554 (setq thiselt
(car whitespace-all-my-files
))
555 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
556 (setq buffile
(car thiselt
))
557 (setq bufname
(cadr thiselt
))
558 (setq buf
(get-buffer bufname
))
559 (if (buffer-live-p buf
)
561 ;;(message "buffer %s live" bufname)
565 ;;(message "checking for whitespace in %s" bufname)
566 (if whitespace-auto-cleanup
568 ;;(message "cleaning up whitespace in %s" bufname)
569 (whitespace-cleanup))
571 ;;(message "whitespace-buffer %s." (buffer-name))
572 (whitespace-buffer t
))))
573 ;;(message "Removing %s from refresh list" bufname)
574 (whitespace-refresh-rescan-list buffile bufname
)))
575 ;;(message "Removing %s from refresh list" bufname)
576 (whitespace-refresh-rescan-list buffile bufname
))))))
578 (defun whitespace-refresh-rescan-list (buffile bufname
)
579 "Refresh the list of files to be rescaned for whitespace creep."
580 (if whitespace-all-buffer-files
581 (setq whitespace-all-buffer-files
582 (delete (list buffile bufname
) whitespace-all-buffer-files
))
583 (when whitespace-rescan-timer
584 (disable-timeout whitespace-rescan-timer
)
585 (setq whitespace-rescan-timer nil
))))
588 (defcustom whitespace-global-mode nil
589 "Toggle global Whitespace mode.
591 Setting this variable directly does not take effect;
592 use either \\[customize] or the function `whitespace-global-mode'
594 :set
(lambda (sym val
)
595 (whitespace-global-mode (or val
0)))
596 :initialize
'custom-initialize-default
599 :require
'whitespace
)
601 (defun whitespace-global-mode (&optional arg
)
602 "Toggle using Whitespace mode in new buffers.
603 With ARG, turn the mode on if and only iff ARG is positive.
605 When this mode is active, `whitespace-buffer' is added to
606 `find-file-hooks' and `kill-buffer-hook'."
609 (> (prefix-numeric-value arg
) 0)
610 (not whitespace-global-mode
)))
613 (add-hook 'find-file-hooks
'whitespace-buffer
)
614 (add-hook 'kill-buffer-hook
'whitespace-buffer
))
615 (remove-hook 'find-file-hooks
'whitespace-buffer
)
616 (remove-hook 'kill-buffer-hook
'whitespace-buffer
)))
619 (defun whitespace-describe ()
620 "A summary of whitespaces and what this library can do about them.
622 The whitespace library is intended to find and help fix five different types
623 of whitespace problems that commonly exist in source code.
625 1. Leading space (empty lines at the top of a file).
626 2. Trailing space (empty lines at the end of a file).
627 3. Indentation space (8 or more spaces at beginning of line, that should be
629 4. Spaces followed by a TAB. (Almost always, we never want that).
630 5. Spaces or TABS at the end of a line.
632 Whitespace errors are reported in a buffer, and on the modeline.
634 Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
635 where `x' and `y' can be one (or more) of:
637 e - End-of-Line whitespace.
638 i - Indentation whitespace.
639 l - Leading whitespace.
640 s - Space followed by Tab.
641 t - Trailing whitespace.
643 If any of the whitespace checks is turned off, the modeline will display a
646 (since (3) is the most controversial one, here is the rationale: Most
647 terminal drivers and printer drivers have TAB configured or even
648 hardcoded to be 8 spaces. (Some of them allow configuration, but almost
649 always they default to 8.)
651 Changing `tab-width' to other than 8 and editing will cause your code to
652 look different from within Emacs, and say, if you cat it or more it, or
655 Almost all the popular programming modes let you define an offset (like
656 c-basic-offset or perl-indent-level) to configure the offset, so you
657 should never have to set your `tab-width' to be other than 8 in all these
658 modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs
659 to replace your 8 spaces with one \t (try it). If vi users in your
660 office complain, tell them to use vim, which distinguishes between
661 tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
664 All the above have caused (and will cause) unwanted codeline integration and
667 whitespace.el will complain if it detects whitespaces on opening a file, and
668 warn you on closing a file also (in case you had inserted any
669 whitespaces during the process of your editing)."
671 (message "Use C-h f whitespace-describe to read about whitespace.el v%s."
674 (defun whitespace-unload-hook ()
675 (remove-hook 'find-file-hooks
'whitespace-buffer
)
676 (remove-hook 'kill-buffer-hook
'whitespace-buffer
))
678 (provide 'whitespace
)
680 ;;; whitespace.el ends here