1 ;;; whitespace.el --- Warn about and clean bogus whitespaces in the file.
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;; Author: Rajesh Vaidheeswarran <rv@dsmit.com>
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 ;; add a hook to find-file-hooks and kill-buffer-hook
42 (add-hook 'find-file-hooks
'whitespace-buffer
)
43 (add-hook 'kill-buffer-hook
'whitespace-buffer
)
45 (defvar whitespace-version
"2.8" "Version of the whitespace library.")
46 ;; Find out what type of Emacs we are running in.
47 (defvar whitespace-running-emacs
(if (string-match "XEmacs\\|Lucid"
49 "If this is Emacs, not XEmacs, this is t.")
51 (if whitespace-running-emacs
(require 'timer
))
53 (defvar whitespace-all-buffer-files nil
54 "An associated list of buffers and files checked for whitespace cleanliness.
56 This is to enable periodic checking of whitespace cleanliness in the files
57 visited by the buffers.")
59 (defvar whitespace-rescan-timer nil
60 "Timer object used to rescan the files in buffers that have been modified.")
62 ;; Tell Emacs about this new kind of minor mode
63 (defvar whitespace-mode nil
64 "Non-nil when Whitespace mode (a minor mode) is enabled.")
65 (make-variable-buffer-local 'whitespace-mode
)
66 (put 'whitespace-mode
'permanent-local nil
)
68 (defvar whitespace-mode-line nil
69 "String to display in the mode line for Whitespace mode.")
70 (make-variable-buffer-local 'whitespace-mode-line
)
71 (put 'whitespace-mode-line
'permanent-local nil
)
73 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
75 (if (not (fboundp 'defgroup
))
76 (defmacro defgroup
(sym memb doc
&rest args
)
77 "Null macro for defgroup in all versions of Emacs that don't define
80 (if (not (fboundp 'defcustom
))
81 (defmacro defcustom
(sym val doc
&rest args
)
82 "Macro to alias defcustom to defvar in all versions of Emacs that
83 don't define defcustom"
84 `(defvar ,sym
,val
,doc
))))
86 (defgroup whitespace nil
87 "Check for and fix five different types of whitespaces in source code."
88 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
90 :group
(if whitespace-running-emacs
'convenience
'editing
))
92 (defcustom whitespace-check-leading-whitespace t
93 "Flag to check leading whitespace."
97 (defcustom whitespace-check-trailing-whitespace t
98 "Flag to check trailing whitespace."
102 (defcustom whitespace-check-spacetab-whitespace t
103 "Flag to check space followed by a TAB."
107 (defcustom whitespace-spacetab-regexp
" \t"
108 "Regexp to match a space followed by a TAB."
112 (defcustom whitespace-check-indent-whitespace t
113 "Flag to check indentation whitespace."
117 (defcustom whitespace-indent-regexp
(concat "^\\(\t*\\) " " ")
118 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
122 (defcustom whitespace-check-ateol-whitespace t
123 "Flag to check end-of-line whitespace."
127 (defcustom whitespace-ateol-regexp
"[ \t]$"
128 "Regexp to match a TAB or a space at the EOL."
132 (defcustom whitespace-errbuf
"*Whitespace Errors*"
133 "The buffer where whitespace related messages will be logged."
137 (defcustom whitespace-auto-cleanup nil
138 "Cleanup a buffer automatically on finding it whitespace unclean."
142 (defcustom whitespace-silent nil
143 "All whitespace errors will be shown only in the modeline when t.
145 Note that setting this may cause all whitespaces introduced in a file to go
146 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
147 Errors*' buffer before opening (or closing) another file."
151 (defcustom whitespace-modes
'(ada-mode asm-mode autoconf-mode awk-mode
152 c-mode c
++-mode cc-mode
153 change-log-mode cperl-mode
154 electric-nroff-mode emacs-lisp-mode
155 f90-mode fortran-mode html-mode
156 html3-mode java-mode jde-mode
157 ksh-mode latex-mode LaTeX-mode
158 lisp-mode m4-mode makefile-mode
159 modula-2-mode nroff-mode objc-mode
160 pascal-mode perl-mode prolog-mode
161 python-mode scheme-mode sgml-mode
162 sh-mode shell-script-mode simula-mode
163 tcl-mode tex-mode texinfo-mode
166 "Major Modes in which we turn on whitespace checking.
168 These are mostly programming and documentation modes. But you may add other
169 modes that you want whitespaces checked in by adding something like the
170 following to your `.emacs':
172 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
175 Or, alternately, you can use the Emacs `customize' command to set this."
178 (defcustom whitespace-rescan-timer-time
60
179 "Period in seconds to rescan modified buffers for whitespace creep.
181 This is the period after which the timer will fire causing
182 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
185 To disable timer scans, set this to zero."
189 (defcustom whitespace-display-in-modeline t
190 "Display whitespace errors on the modeline."
194 (if (not (assoc 'whitespace-mode minor-mode-alist
))
195 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
198 (defun whitespace-check-whitespace-mode (&optional arg
)
199 "Test and set the whitespace-mode in qualifying buffers."
200 (if (null whitespace-mode
)
201 (setq whitespace-mode
202 (if (or arg
(member major-mode whitespace-modes
))
207 (defun whitespace-buffer (&optional quiet
)
208 "Find five different types of white spaces in buffer:
210 1. Leading space \(empty lines at the top of a file\).
211 2. Trailing space \(empty lines at the end of a file\).
212 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
213 4. Spaces followed by a TAB. \(Almost always, we never want that\).
214 5. Spaces or TABS at the end of a line.
216 Check for whitespace only if this buffer really contains a non-empty file
218 1. the major mode is one of the whitespace-modes, or
219 2. `whitespace-buffer' was explicitly called with a prefix argument."
221 (let ((whitespace-error nil
))
222 (whitespace-check-whitespace-mode current-prefix-arg
)
223 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
225 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
226 (whitespace-tickle-timer)
227 (if whitespace-auto-cleanup
230 (message "Can't cleanup: %s is read-only" (buffer-name)))
231 (whitespace-cleanup))
232 (let ((whitespace-leading (if whitespace-check-leading-whitespace
233 (whitespace-buffer-leading)
235 (whitespace-trailing (if whitespace-check-trailing-whitespace
236 (whitespace-buffer-trailing)
238 (whitespace-indent (if whitespace-check-indent-whitespace
239 (whitespace-buffer-search
240 whitespace-indent-regexp
)
242 (whitespace-spacetab (if whitespace-check-spacetab-whitespace
243 (whitespace-buffer-search
244 whitespace-spacetab-regexp
)
246 (whitespace-ateol (if whitespace-check-ateol-whitespace
247 (whitespace-buffer-search
248 whitespace-ateol-regexp
)
250 (whitespace-errmsg nil
)
251 (whitespace-filename buffer-file-name
)
252 (whitespace-this-modeline ""))
254 ;; Now let's complain if we found any of the above.
255 (setq whitespace-error
(or whitespace-leading whitespace-indent
256 whitespace-spacetab whitespace-ateol
257 whitespace-trailing
))
261 (setq whitespace-errmsg
262 (concat whitespace-filename
" contains:\n"
263 (if whitespace-leading
264 "Leading whitespace\n")
265 (if whitespace-indent
266 (concat "Indentation whitespace"
267 whitespace-indent
"\n"))
268 (if whitespace-spacetab
269 (concat "Space followed by Tab"
270 whitespace-spacetab
"\n"))
272 (concat "End-of-line whitespace"
273 whitespace-ateol
"\n"))
274 (if whitespace-trailing
275 "Trailing whitespace\n")
276 "\ntype `M-x whitespace-cleanup' to "
277 "cleanup the file."))
278 (setq whitespace-this-modeline
279 (concat (if whitespace-ateol
"e")
280 (if whitespace-indent
"i")
281 (if whitespace-leading
"l")
282 (if whitespace-spacetab
"s")
283 (if whitespace-trailing
"t")))))
284 (whitespace-update-modeline whitespace-this-modeline
)
286 (get-buffer-create whitespace-errbuf
)
287 (kill-buffer whitespace-errbuf
)
288 (get-buffer-create whitespace-errbuf
)
289 (set-buffer whitespace-errbuf
)
290 (if whitespace-errmsg
292 (insert whitespace-errmsg
)
293 (if (not (or quiet whitespace-silent
))
294 (display-buffer whitespace-errbuf t
))
296 (message "Whitespaces: [%s%s] in %s"
297 whitespace-this-modeline
298 (let ((whitespace-unchecked
299 (whitespace-unchecked-whitespaces)))
300 (if whitespace-unchecked
301 (concat "!" whitespace-unchecked
)
303 whitespace-filename
)))
305 (message "%s clean" whitespace-filename
))))))))
311 (defun whitespace-region (s e
)
312 "Check a region specified by point and mark for whitespace errors."
316 (narrow-to-region s e
)
317 (whitespace-buffer))))
320 (defun whitespace-cleanup ()
321 "Cleanup the five different kinds of whitespace problems.
323 Use \\[describe-function] whitespace-describe to read a summary of the
324 whitespace problems."
326 ;; If this buffer really contains a file, then run, else quit.
327 (whitespace-check-whitespace-mode current-prefix-arg
)
328 (if (and buffer-file-name whitespace-mode
)
329 (let ((whitespace-any nil
)
330 (whitespace-tabwith 8)
331 (whitespace-tabwith-saved tab-width
))
333 ;; since all printable TABS should be 8, irrespective of how
334 ;; they are displayed.
335 (setq tab-width whitespace-tabwith
)
337 (if (and whitespace-check-leading-whitespace
338 (whitespace-buffer-leading))
340 (whitespace-buffer-leading-cleanup)
341 (setq whitespace-any t
)))
343 (if (and whitespace-check-trailing-whitespace
344 (whitespace-buffer-trailing))
346 (whitespace-buffer-trailing-cleanup)
347 (setq whitespace-any t
)))
349 (if (and whitespace-check-indent-whitespace
350 (whitespace-buffer-search whitespace-indent-regexp
))
352 (whitespace-indent-cleanup)
353 (setq whitespace-any t
)))
355 (if (and whitespace-check-spacetab-whitespace
356 (whitespace-buffer-search whitespace-spacetab-regexp
))
358 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
359 (setq whitespace-any t
)))
361 (if (and whitespace-check-ateol-whitespace
362 (whitespace-buffer-search whitespace-ateol-regexp
))
364 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
365 (setq whitespace-any t
)))
367 ;; Call this recursively till everything is taken care of
371 (message "%s clean" buffer-file-name
)
372 (whitespace-update-modeline)))
373 (setq tab-width whitespace-tabwith-saved
))))
376 (defun whitespace-cleanup-region (s e
)
377 "Whitespace cleanup on a region specified by point and mark."
381 (narrow-to-region s e
)
382 (whitespace-cleanup))
383 (whitespace-buffer t
)))
385 (defun whitespace-buffer-leading ()
386 "Check to see if there are any empty lines at the top of the file."
390 (goto-char (point-min))
395 (if (equal pmin pmax
)
399 (defun whitespace-buffer-leading-cleanup ()
400 "Remove any empty lines at the top of the file."
404 (goto-char (point-min))
409 (if (equal pmin pmax
)
412 (whitespace-buffer-leading-cleanup))))))
414 (defun whitespace-buffer-trailing ()
415 "Check to see if are is more than one empty line at the bottom."
419 (goto-char (point-max))
424 (if (equal pmin pmax
)
426 (goto-char (- (point) 1))
431 (if (equal pmin pmax
)
436 (defun whitespace-buffer-trailing-cleanup ()
437 "Delete all the empty lines at the bottom."
441 (goto-char (point-max))
446 (if (equal pmin pmax
)
448 (goto-char (1- pmin
))
453 (if (equal pmin pmax
)
455 (goto-char (1- (point-max)))
458 (whitespace-buffer-trailing-cleanup))))))))
460 (defun whitespace-buffer-search (regexp)
461 "Search for any given whitespace REGEXP."
462 (let ((whitespace-retval ""))
464 (goto-char (point-min))
465 (while (re-search-forward regexp nil t
)
466 (setq whitespace-retval
(format "%s %s " whitespace-retval
467 (match-beginning 0))))
468 (if (equal "" whitespace-retval
)
470 whitespace-retval
))))
472 (defun whitespace-buffer-cleanup (regexp newregexp
)
473 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
475 (goto-char (point-min))
476 (while (re-search-forward regexp nil t
)
477 (replace-match newregexp
))))
479 (defun whitespace-indent-cleanup ()
480 "Search for 8/more spaces at the start of a line and replace it with tabs."
482 (goto-char (point-min))
483 (while (re-search-forward whitespace-indent-regexp nil t
)
484 (let ((column (current-column))
485 (indent-tabs-mode t
))
486 (delete-region (match-beginning 0) (point))
487 (indent-to column
)))))
489 (defun whitespace-unchecked-whitespaces ()
490 "Return the list of whitespaces whose testing has been suppressed."
491 (let ((whitespace-this-modeline
492 (concat (if (not whitespace-check-ateol-whitespace
) "e")
493 (if (not whitespace-check-indent-whitespace
) "i")
494 (if (not whitespace-check-leading-whitespace
) "l")
495 (if (not whitespace-check-spacetab-whitespace
) "s")
496 (if (not whitespace-check-trailing-whitespace
) "t"))))
497 (if (not (equal whitespace-this-modeline
""))
498 whitespace-this-modeline
501 (defun whitespace-update-modeline (&optional whitespace-err
)
502 "Update modeline with whitespace errors and whitespaces whose testing has
504 (if whitespace-display-in-modeline
506 (setq whitespace-mode-line nil
)
508 (if (and whitespace-err
(not (equal whitespace-err
"")))
509 (setq whitespace-mode-line whitespace-err
))
510 ;; Whitespace suppressed errors
511 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
512 (if whitespace-unchecked
513 (setq whitespace-mode-line
514 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
515 ;; Add the whitespace modeline prefix
516 (setq whitespace-mode-line
(if whitespace-mode-line
517 (concat " W:" whitespace-mode-line
)
519 (whitespace-force-mode-line-update))))
521 ;; Force mode line updation for different Emacs versions
522 (defun whitespace-force-mode-line-update ()
523 "Force the mode line update for different flavors of Emacs."
524 (if whitespace-running-emacs
525 (force-mode-line-update) ; Emacs
526 (redraw-modeline))) ; XEmacs
528 (defun whitespace-check-buffer-list (buf-name buf-file
)
529 "Add a buffer and its file to the whitespace monitor list.
531 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
532 periodically for whitespace."
533 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
534 whitespace-all-buffer-files
)))
535 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
537 (defun whitespace-tickle-timer ()
538 "Tickle timer to periodically to scan qualifying files for whitespace creep.
540 If timer is not set, then set it to scan the files in
541 `whitespace-all-buffer-files' periodically (defined by
542 `whitespace-rescan-timer-time') for whitespace creep."
543 (if (and whitespace-rescan-timer-time
(not whitespace-rescan-timer
))
544 (setq whitespace-rescan-timer
545 (if whitespace-running-emacs
546 (run-at-time nil whitespace-rescan-timer-time
547 'whitespace-rescan-files-in-buffers
)
548 (add-timeout whitespace-rescan-timer-time
549 'whitespace-rescan-files-in-buffers nil
550 whitespace-rescan-timer-time
)))))
552 (defun whitespace-rescan-files-in-buffers (&optional arg
)
553 "Check monitored files for whitespace creep since last scan."
554 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
555 buffile bufname thiselt buf
)
556 (if (not whitespace-all-my-files
)
558 (if whitespace-running-emacs
559 (cancel-timer whitespace-rescan-timer
)
560 (disable-timeout whitespace-rescan-timer
))
561 (setq whitespace-rescan-timer nil
))
562 (while whitespace-all-my-files
563 (setq thiselt
(car whitespace-all-my-files
))
564 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
565 (setq buffile
(car thiselt
))
566 (setq bufname
(cadr thiselt
))
567 (setq buf
(get-buffer bufname
))
568 (if (buffer-live-p buf
)
570 ;;(message "buffer %s live" bufname)
574 ;;(message "checking for whitespace in %s" bufname)
575 (if whitespace-auto-cleanup
577 ;;(message "cleaning up whitespace in %s" bufname)
578 (whitespace-cleanup))
580 ;;(message "whitespace-buffer %s." (buffer-name))
581 (whitespace-buffer t
))))
582 ;;(message "Removing %s from refresh list" bufname)
583 (whitespace-refresh-rescan-list buffile bufname
)))
584 ;;(message "Removing %s from refresh list" bufname)
585 (whitespace-refresh-rescan-list buffile bufname
))))))
587 (defun whitespace-refresh-rescan-list (buffile bufname
)
588 "Refresh the list of files to be rescaned for whitespace creep."
589 (if whitespace-all-buffer-files
591 (setq whitespace-all-buffer-files
592 (delete (list buffile bufname
) whitespace-all-buffer-files
)))
594 (if (and whitespace-running-emacs
(timerp whitespace-rescan-timer
))
595 (cancel-timer whitespace-rescan-timer
))
596 (if (and (not whitespace-running-emacs
) whitespace-rescan-timer
)
597 (disable-timeout whitespace-rescan-timer
))
598 (if whitespace-rescan-timer
599 (setq whitespace-rescan-timer nil
)))))
602 (defun whitespace-describe ()
603 "A summary of whitespaces and what this library can do about them.
605 The whitespace library is intended to find and help fix five different types
606 of whitespace problems that commonly exist in source code.
608 1. Leading space (empty lines at the top of a file).
609 2. Trailing space (empty lines at the end of a file).
610 3. Indentation space (8 or more spaces at beginning of line, that should be
612 4. Spaces followed by a TAB. (Almost always, we never want that).
613 5. Spaces or TABS at the end of a line.
615 Whitespace errors are reported in a buffer, and on the modeline.
617 Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
618 where `x' and `y' can be one (or more) of:
620 e - End-of-Line whitespace.
621 i - Indentation whitespace.
622 l - Leading whitespace.
623 s - Space followed by Tab.
624 t - Trailing whitespace.
626 If any of the whitespace checks is turned off, the modeline will display a
629 (since (3) is the most controversial one, here is the rationale: Most
630 terminal drivers and printer drivers have TAB configured or even
631 hardcoded to be 8 spaces. (Some of them allow configuration, but almost
632 always they default to 8.)
634 Changing tab-width to other than 8 and editing will cause your code to
635 look different from within Emacs, and say, if you cat it or more it, or
638 Almost all the popular programming modes let you define an offset (like
639 c-basic-offset or perl-indent-level) to configure the offset, so you
640 should never have to set your tab-width to be other than 8 in all these
641 modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs
642 to replace your 8 spaces with one \t (try it). If vi users in your
643 office complain, tell them to use vim, which distinguishes between
644 tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
647 All the above have caused (and will cause) unwanted codeline integration and
650 whitespace.el will complain if it detects whitespaces on opening a file, and
651 warn you on closing a file also. (if in case you had inserted any
652 whitespaces during the process of your editing.)"
654 (message "Use C-h f whitespace-describe to read about whitespace.el v%s."
657 (provide 'whitespace
)
659 ;;; whitespace.el ends here