(ediff-copy-list): Use `defalias' instead of
[emacs.git] / lisp / whitespace.el
blobe8c4dd4b9dd180a83e465fde3c862b55bc5b93d6
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 ;; $Id: whitespace.el,v 1.18 2001/08/20 20:56:08 rv Exp $
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; Whitespace.el URL: http://www.dsmit.com/lisp/
30 ;; Exported functions:
32 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
33 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
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.
40 ;;; Code:
42 (defvar whitespace-version "3.2" "Version of the whitespace library.")
44 (defvar whitespace-all-buffer-files nil
45 "An associated list of buffers and files checked for whitespace cleanliness.
47 This is to enable periodic checking of whitespace cleanliness in the files
48 visited by the buffers.")
50 (defvar whitespace-rescan-timer nil
51 "Timer object used to rescan the files in buffers that have been modified.")
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)
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)
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)
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)
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)
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)
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)
89 (defvar whitespace-highlighted-space nil
90 "The variable to store the extent to highlight")
91 (make-variable-buffer-local 'whitespace-highlighted-space)
92 (put 'whitespace-highlighted-space 'permanent-local nil)
94 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
95 (eval-when-compile
96 (if (not (fboundp 'defgroup))
97 (defmacro defgroup (sym memb doc &rest args)
98 "Null macro for defgroup in all versions of Emacs that don't define
99 defgroup"
101 (if (not (fboundp 'defcustom))
102 (defmacro defcustom (sym val doc &rest args)
103 "Macro to alias defcustom to defvar in all versions of Emacs that
104 don't define defcustom"
105 `(defvar ,sym ,val ,doc))))
107 (if (fboundp 'make-overlay)
108 (progn
109 (defalias 'whitespace-make-overlay 'make-overlay)
110 (defalias 'whitespace-overlay-put 'overlay-put)
111 (defalias 'whitespace-delete-overlay 'delete-overlay)
112 (defalias 'whitespace-overlay-start 'overlay-start)
113 (defalias 'whitespace-overlay-end 'overlay-end)
114 (defalias 'whitespace-mode-line-update 'force-mode-line-update))
115 (defalias 'whitespace-make-overlay 'make-extent)
116 (defalias 'whitespace-overlay-put 'set-extent-property)
117 (defalias 'whitespace-delete-overlay 'delete-extent)
118 (defalias 'whitespace-overlay-start 'extent-start)
119 (defalias 'whitespace-overlay-end 'extent-end)
120 (defalias 'whitespace-mode-line-update 'redraw-modeline))
122 (if (featurep 'xemacs)
123 (defgroup whitespace nil
124 "Check for and fix five different types of whitespaces in source code."
125 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
126 ;; which is 'editing?
127 :group 'editing)
128 (defgroup whitespace nil
129 "Check for and fix five different types of whitespaces in source code."
130 :version "21.1"
131 :group 'convenience))
133 (defcustom whitespace-check-leading-whitespace t
134 "Flag to check leading whitespace. This is the global for the system.
135 It can be overriden by setting a buffer local variable
136 `whitespace-check-buffer-leading'"
137 :type 'boolean
138 :group 'whitespace)
140 (defcustom whitespace-check-trailing-whitespace t
141 "Flag to check trailing whitespace. This is the global for the system.
142 It can be overriden by setting a buffer local variable
143 `whitespace-check-buffer-trailing'"
144 :type 'boolean
145 :group 'whitespace)
147 (defcustom whitespace-check-spacetab-whitespace t
148 "Flag to check space followed by a TAB. This is the global for the system.
149 It can be overriden by setting a buffer local variable
150 `whitespace-check-buffer-spacetab'"
151 :type 'boolean
152 :group 'whitespace)
154 (defcustom whitespace-spacetab-regexp "[ ]+\t"
155 "Regexp to match a space followed by a TAB."
156 :type 'regexp
157 :group 'whitespace)
159 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
160 "Flag to check indentation whitespace. This is the global for the system.
161 It can be overriden by setting a buffer local variable
162 `whitespace-check-buffer-indent'"
163 :type 'boolean
164 :group 'whitespace)
166 (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ")
167 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
168 :type 'regexp
169 :group 'whitespace)
171 (defcustom whitespace-check-ateol-whitespace t
172 "Flag to check end-of-line whitespace. This is the global for the system.
173 It can be overriden by setting a buffer local variable
174 `whitespace-check-buffer-ateol'"
175 :type 'boolean
176 :group 'whitespace)
178 ;; (defcustom whitespace-ateol-regexp "[ \t]$"
179 (defcustom whitespace-ateol-regexp "[ \t]+$"
180 "Regexp to match a TAB or a space at the EOL."
181 :type 'regexp
182 :group 'whitespace)
184 (defcustom whitespace-errbuf "*Whitespace Errors*"
185 "The name of the buffer where whitespace related messages will be logged."
186 :type 'string
187 :group 'whitespace)
189 (defcustom whitespace-abort-on-error nil
190 "While writing a file, abort if the file is unclean. If
191 `whitespace-auto-cleanup' is set, that takes precedence over this
192 variable."
193 :type 'boolean
194 :group 'whitespace)
196 (defcustom whitespace-auto-cleanup nil
197 "Cleanup a buffer automatically on finding it whitespace unclean."
198 :type 'boolean
199 :group 'whitespace)
201 (defcustom whitespace-silent nil
202 "All whitespace errors will be shown only in the modeline when t.
204 Note that setting this may cause all whitespaces introduced in a file to go
205 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
206 Errors*' buffer before opening (or closing) another file."
207 :type 'boolean
208 :group 'whitespace)
210 (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
211 c-mode c++-mode cc-mode
212 change-log-mode cperl-mode
213 electric-nroff-mode emacs-lisp-mode
214 f90-mode fortran-mode html-mode
215 html3-mode java-mode jde-mode
216 ksh-mode latex-mode LaTeX-mode
217 lisp-mode m4-mode makefile-mode
218 modula-2-mode nroff-mode objc-mode
219 pascal-mode perl-mode prolog-mode
220 python-mode scheme-mode sgml-mode
221 sh-mode shell-script-mode simula-mode
222 tcl-mode tex-mode texinfo-mode
223 vrml-mode xml-mode)
225 "Major Modes in which we turn on whitespace checking.
227 These are mostly programming and documentation modes. But you may add other
228 modes that you want whitespaces checked in by adding something like the
229 following to your `.emacs':
231 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
232 whitespace-modes))\)
234 Or, alternately, you can use the Emacs `customize' command to set this."
235 :type '(repeat symbol)
236 :group 'whitespace)
238 (defcustom whitespace-rescan-timer-time 600
239 "Period in seconds to rescan modified buffers for whitespace creep.
241 This is the period after which the timer will fire causing
242 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
243 modified buffers.
245 To disable timer scans, set this to zero."
246 :type 'integer
247 :group 'whitespace)
249 (defcustom whitespace-display-in-modeline t
250 "Display whitespace errors on the modeline."
251 :type 'boolean
252 :group 'whitespace)
254 (defcustom whitespace-display-spaces-in-color t
255 "Display the bogus whitespaces by coloring them with
256 `whitespace-highlight-face'."
257 :type 'boolean
258 :group 'whitespace)
260 (defgroup whitespace-faces nil
261 "Faces used in whitespace."
262 :prefix "whitespace-"
263 :group 'whitespace
264 :group 'faces)
266 (defface whitespace-highlight-face '((((class color) (background light))
267 (:background "green"))
268 (((class color) (background dark))
269 (:background "sea green"))
270 (((class grayscale monochrome)
271 (background light))
272 (:background "black"))
273 (((class grayscale monochrome)
274 (background dark))
275 (:background "white")))
276 "Face used for highlighting the bogus whitespaces that exist in the buffer."
277 :group 'whitespace-faces)
279 (if (not (assoc 'whitespace-mode minor-mode-alist))
280 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
281 minor-mode-alist)))
283 (set-default 'whitespace-check-buffer-leading
284 whitespace-check-leading-whitespace)
285 (set-default 'whitespace-check-buffer-trailing
286 whitespace-check-trailing-whitespace)
287 (set-default 'whitespace-check-buffer-indent
288 whitespace-check-indent-whitespace)
289 (set-default 'whitespace-check-buffer-spacetab
290 whitespace-check-spacetab-whitespace)
291 (set-default 'whitespace-check-buffer-ateol
292 whitespace-check-ateol-whitespace)
294 (defun whitespace-check-whitespace-mode (&optional arg)
295 "Test and set the whitespace-mode in qualifying buffers."
296 (if (null whitespace-mode)
297 (setq whitespace-mode
298 (if (or arg (member major-mode whitespace-modes))
300 nil))))
302 ;;;###autoload
303 (defun whitespace-toggle-leading-check ()
304 "Toggle the check for leading space in the local buffer."
305 (interactive)
306 (let ((current-val whitespace-check-buffer-leading))
307 (setq whitespace-check-buffer-leading (not current-val))
308 (message "Will%s check for leading space in buffer."
309 (if whitespace-check-buffer-leading "" " not"))
310 (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
312 ;;;###autoload
313 (defun whitespace-toggle-trailing-check ()
314 "Toggle the check for trailing space in the local buffer."
315 (interactive)
316 (let ((current-val whitespace-check-buffer-trailing))
317 (setq whitespace-check-buffer-trailing (not current-val))
318 (message "Will%s check for trailing space in buffer."
319 (if whitespace-check-buffer-trailing "" " not"))
320 (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
322 ;;;###autoload
323 (defun whitespace-toggle-indent-check ()
324 "Toggle the check for indentation space in the local buffer."
325 (interactive)
326 (let ((current-val whitespace-check-buffer-indent))
327 (setq whitespace-check-buffer-indent (not current-val))
328 (message "Will%s check for indentation space in buffer."
329 (if whitespace-check-buffer-indent "" " not"))
330 (if whitespace-check-buffer-indent
331 (whitespace-buffer-search whitespace-indent-regexp))))
333 ;;;###autoload
334 (defun whitespace-toggle-spacetab-check ()
335 "Toggle the check for space-followed-by-TABs in the local buffer."
336 (interactive)
337 (let ((current-val whitespace-check-buffer-spacetab))
338 (setq whitespace-check-buffer-spacetab (not current-val))
339 (message "Will%s check for space-followed-by-TABs in buffer."
340 (if whitespace-check-buffer-spacetab "" " not"))
341 (if whitespace-check-buffer-spacetab
342 (whitespace-buffer-search whitespace-spacetab-regexp))))
345 ;;;###autoload
346 (defun whitespace-toggle-ateol-check ()
347 "Toggle the check for end-of-line space in the local buffer."
348 (interactive)
349 (let ((current-val whitespace-check-buffer-ateol))
350 (setq whitespace-check-buffer-ateol (not current-val))
351 (message "Will%s check for end-of-line space in buffer."
352 (if whitespace-check-buffer-ateol "" " not"))
353 (if whitespace-check-buffer-ateol
354 (whitespace-buffer-search whitespace-ateol-regexp))))
357 ;;;###autoload
358 (defun whitespace-buffer (&optional quiet)
359 "Find five different types of white spaces in buffer.
360 These are:
361 1. Leading space \(empty lines at the top of a file\).
362 2. Trailing space \(empty lines at the end of a file\).
363 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
364 4. Spaces followed by a TAB. \(Almost always, we never want that\).
365 5. Spaces or TABS at the end of a line.
367 Check for whitespace only if this buffer really contains a non-empty file
368 and:
369 1. the major mode is one of the whitespace-modes, or
370 2. `whitespace-buffer' was explicitly called with a prefix argument."
371 (interactive)
372 (let ((whitespace-error nil))
373 (whitespace-check-whitespace-mode current-prefix-arg)
374 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
375 (progn
376 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
377 (whitespace-tickle-timer)
378 (whitespace-unhighlight-the-space)
379 (if whitespace-auto-cleanup
380 (if buffer-read-only
381 (if (not quiet)
382 (message "Can't cleanup: %s is read-only" (buffer-name)))
383 (whitespace-cleanup))
384 (let ((whitespace-leading (if whitespace-check-buffer-leading
385 (whitespace-buffer-leading)
386 nil))
387 (whitespace-trailing (if whitespace-check-buffer-trailing
388 (whitespace-buffer-trailing)
389 nil))
390 (whitespace-indent (if whitespace-check-buffer-indent
391 (whitespace-buffer-search
392 whitespace-indent-regexp)
393 nil))
394 (whitespace-spacetab (if whitespace-check-buffer-spacetab
395 (whitespace-buffer-search
396 whitespace-spacetab-regexp)
397 nil))
398 (whitespace-ateol (if whitespace-check-buffer-ateol
399 (whitespace-buffer-search
400 whitespace-ateol-regexp)
401 nil))
402 (whitespace-errmsg nil)
403 (whitespace-filename buffer-file-name)
404 (whitespace-this-modeline ""))
406 ;; Now let's complain if we found any of the above.
407 (setq whitespace-error (or whitespace-leading whitespace-indent
408 whitespace-spacetab whitespace-ateol
409 whitespace-trailing))
411 (if whitespace-error
412 (progn
413 (setq whitespace-errmsg
414 (concat whitespace-filename " contains:\n"
415 (if whitespace-leading
416 "Leading whitespace\n")
417 (if whitespace-indent
418 (concat "Indentation whitespace"
419 whitespace-indent "\n"))
420 (if whitespace-spacetab
421 (concat "Space followed by Tab"
422 whitespace-spacetab "\n"))
423 (if whitespace-ateol
424 (concat "End-of-line whitespace"
425 whitespace-ateol "\n"))
426 (if whitespace-trailing
427 "Trailing whitespace\n")
428 "\ntype `M-x whitespace-cleanup' to "
429 "cleanup the file."))
430 (setq whitespace-this-modeline
431 (concat (if whitespace-ateol "e")
432 (if whitespace-indent "i")
433 (if whitespace-leading "l")
434 (if whitespace-spacetab "s")
435 (if whitespace-trailing "t")))))
436 (whitespace-update-modeline whitespace-this-modeline)
437 (save-excursion
438 (get-buffer-create whitespace-errbuf)
439 (kill-buffer whitespace-errbuf)
440 (get-buffer-create whitespace-errbuf)
441 (set-buffer whitespace-errbuf)
442 (if whitespace-errmsg
443 (progn
444 (insert whitespace-errmsg)
445 (if (not (or quiet whitespace-silent))
446 (display-buffer whitespace-errbuf t))
447 (if (not quiet)
448 (message "Whitespaces: [%s%s] in %s"
449 whitespace-this-modeline
450 (let ((whitespace-unchecked
451 (whitespace-unchecked-whitespaces)))
452 (if whitespace-unchecked
453 (concat "!" whitespace-unchecked)
454 ""))
455 whitespace-filename)))
456 (if (not quiet)
457 (message "%s clean" whitespace-filename))))))))
458 (if whitespace-error
460 nil)))
462 ;;;###autoload
463 (defun whitespace-region (s e)
464 "Check the region for whitespace errors."
465 (interactive "r")
466 (save-excursion
467 (save-restriction
468 (narrow-to-region s e)
469 (whitespace-buffer))))
471 ;;;###autoload
472 (defun whitespace-cleanup ()
473 "Cleanup the five different kinds of whitespace problems.
475 Use \\[describe-function] whitespace-describe to read a summary of the
476 whitespace problems."
477 (interactive)
478 ;; If this buffer really contains a file, then run, else quit.
479 (whitespace-check-whitespace-mode current-prefix-arg)
480 (if (and buffer-file-name whitespace-mode)
481 (let ((whitespace-any nil)
482 (whitespace-tabwith 8)
483 (whitespace-tabwith-saved tab-width))
485 ;; since all printable TABS should be 8, irrespective of how
486 ;; they are displayed.
487 (setq tab-width whitespace-tabwith)
489 (if (and whitespace-check-buffer-leading
490 (whitespace-buffer-leading))
491 (progn
492 (whitespace-buffer-leading-cleanup)
493 (setq whitespace-any t)))
495 (if (and whitespace-check-buffer-trailing
496 (whitespace-buffer-trailing))
497 (progn
498 (whitespace-buffer-trailing-cleanup)
499 (setq whitespace-any t)))
501 (if (and whitespace-check-buffer-indent
502 (whitespace-buffer-search whitespace-indent-regexp))
503 (progn
504 (whitespace-indent-cleanup)
505 (setq whitespace-any t)))
507 (if (and whitespace-check-buffer-spacetab
508 (whitespace-buffer-search whitespace-spacetab-regexp))
509 (progn
510 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
511 (setq whitespace-any t)))
513 (if (and whitespace-check-buffer-ateol
514 (whitespace-buffer-search whitespace-ateol-regexp))
515 (progn
516 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
517 (setq whitespace-any t)))
519 ;; Call this recursively till everything is taken care of
520 (if whitespace-any
521 (whitespace-cleanup)
522 (progn
523 (message "%s clean" buffer-file-name)
524 (whitespace-update-modeline)))
525 (setq tab-width whitespace-tabwith-saved))))
527 ;;;###autoload
528 (defun whitespace-cleanup-region (s e)
529 "Whitespace cleanup on the region."
530 (interactive "r")
531 (save-excursion
532 (save-restriction
533 (narrow-to-region s e)
534 (whitespace-cleanup))
535 (whitespace-buffer t)))
537 (defun whitespace-buffer-leading ()
538 "Check to see if there are any empty lines at the top of the file."
539 (save-excursion
540 (let ((pmin nil)
541 (pmax nil))
542 (goto-char (point-min))
543 (beginning-of-line)
544 (setq pmin (point))
545 (end-of-line)
546 (setq pmax (point))
547 (if (equal pmin pmax)
548 (progn
549 (whitespace-highlight-the-space pmin pmax)
551 nil))))
553 (defun whitespace-buffer-leading-cleanup ()
554 "Remove any empty lines at the top of the file."
555 (save-excursion
556 (let ((pmin nil)
557 (pmax nil))
558 (goto-char (point-min))
559 (beginning-of-line)
560 (setq pmin (point))
561 (end-of-line)
562 (setq pmax (point))
563 (if (equal pmin pmax)
564 (progn
565 (kill-line)
566 (whitespace-buffer-leading-cleanup))))))
568 (defun whitespace-buffer-trailing ()
569 "Check to see if are is more than one empty line at the bottom."
570 (save-excursion
571 (let ((pmin nil)
572 (pmax nil))
573 (goto-char (point-max))
574 (beginning-of-line)
575 (setq pmin (point))
576 (end-of-line)
577 (setq pmax (point))
578 (if (equal pmin pmax)
579 (progn
580 (goto-char (- (point) 1))
581 (beginning-of-line)
582 (setq pmin (point))
583 (end-of-line)
584 (setq pmax (point))
585 (if (equal pmin pmax)
586 (progn
587 (whitespace-highlight-the-space pmin pmax)
589 nil))
590 nil))))
592 (defun whitespace-buffer-trailing-cleanup ()
593 "Delete all the empty lines at the bottom."
594 (save-excursion
595 (let ((pmin nil)
596 (pmax nil))
597 (goto-char (point-max))
598 (beginning-of-line)
599 (setq pmin (point))
600 (end-of-line)
601 (setq pmax (point))
602 (if (equal pmin pmax)
603 (progn
604 (goto-char (1- pmin))
605 (beginning-of-line)
606 (setq pmin (point))
607 (end-of-line)
608 (setq pmax (point))
609 (if (equal pmin pmax)
610 (progn
611 (goto-char (1- (point-max)))
612 (beginning-of-line)
613 (kill-line)
614 (whitespace-buffer-trailing-cleanup))))))))
616 (defun whitespace-buffer-search (regexp)
617 "Search for any given whitespace REGEXP."
618 (let ((whitespace-retval ""))
619 (save-excursion
620 (goto-char (point-min))
621 (while (re-search-forward regexp nil t)
622 (progn
623 (setq whitespace-retval (format "%s %s" whitespace-retval
624 (match-beginning 0)))
625 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))))
626 (if (equal "" whitespace-retval)
628 whitespace-retval))))
630 (defun whitespace-buffer-cleanup (regexp newregexp)
631 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
632 (save-excursion
633 (goto-char (point-min))
634 (while (re-search-forward regexp nil t)
635 (replace-match newregexp))))
637 (defun whitespace-indent-cleanup ()
638 "Search for 8/more spaces at the start of a line and replace it with tabs."
639 (save-excursion
640 (goto-char (point-min))
641 (while (re-search-forward whitespace-indent-regexp nil t)
642 (let ((column (current-column))
643 (indent-tabs-mode t))
644 (delete-region (match-beginning 0) (point))
645 (indent-to column)))))
647 (defun whitespace-unchecked-whitespaces ()
648 "Return the list of whitespaces whose testing has been suppressed."
649 (let ((unchecked-spaces
650 (concat (if (not whitespace-check-buffer-ateol) "e")
651 (if (not whitespace-check-buffer-indent) "i")
652 (if (not whitespace-check-buffer-leading) "l")
653 (if (not whitespace-check-buffer-spacetab) "s")
654 (if (not whitespace-check-buffer-trailing) "t"))))
655 (if (not (equal unchecked-spaces ""))
656 unchecked-spaces
657 nil)))
659 (defun whitespace-update-modeline (&optional whitespace-err)
660 "Update modeline with whitespace errors.
661 Also with whitespaces whose testing has been turned off."
662 (if whitespace-display-in-modeline
663 (progn
664 (setq whitespace-mode-line nil)
665 ;; Whitespace errors
666 (if (and whitespace-err (not (equal whitespace-err "")))
667 (setq whitespace-mode-line whitespace-err))
668 ;; Whitespace suppressed errors
669 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
670 (if whitespace-unchecked
671 (setq whitespace-mode-line
672 (concat whitespace-mode-line "!" whitespace-unchecked))))
673 ;; Add the whitespace modeline prefix
674 (setq whitespace-mode-line (if whitespace-mode-line
675 (concat " W:" whitespace-mode-line)
676 nil))
677 (whitespace-mode-line-update))))
679 (defun whitespace-highlight-the-space (b e)
680 "Highlight the current line, unhighlighting a previously jumped to line."
681 (if whitespace-display-spaces-in-color
682 (progn
683 (whitespace-unhighlight-the-space)
684 (add-to-list 'whitespace-highlighted-space
685 (whitespace-make-overlay b e))
686 (whitespace-overlay-put (whitespace-make-overlay b e) 'face
687 'whitespace-highlight-face))))
688 ;; (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space))
690 (defun whitespace-unhighlight-the-space ()
691 "Unhighlight the currently highlight line."
692 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
693 (let ((whitespace-this-space nil))
694 (while whitespace-highlighted-space
695 (setq whitespace-this-space (car whitespace-highlighted-space))
696 (setq whitespace-highlighted-space
697 (cdr whitespace-highlighted-space))
698 (whitespace-delete-overlay whitespace-this-space))
699 (setq whitespace-highlighted-space nil))
700 (remove-hook 'pre-command-hook 'whitespace-unhighlight-the-space)))
702 (defun whitespace-check-buffer-list (buf-name buf-file)
703 "Add a buffer and its file to the whitespace monitor list.
705 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
706 periodically for whitespace."
707 (if (and whitespace-mode (not (member (list buf-file buf-name)
708 whitespace-all-buffer-files)))
709 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
711 (defun whitespace-tickle-timer ()
712 "Tickle timer to periodically to scan qualifying files for whitespace creep.
714 If timer is not set, then set it to scan the files in
715 `whitespace-all-buffer-files' periodically (defined by
716 `whitespace-rescan-timer-time') for whitespace creep."
717 (if (and whitespace-rescan-timer-time (not whitespace-rescan-timer))
718 (setq whitespace-rescan-timer
719 (add-timeout whitespace-rescan-timer-time
720 'whitespace-rescan-files-in-buffers nil
721 whitespace-rescan-timer-time))))
723 (defun whitespace-rescan-files-in-buffers (&optional arg)
724 "Check monitored files for whitespace creep since last scan."
725 (let ((whitespace-all-my-files whitespace-all-buffer-files)
726 buffile bufname thiselt buf)
727 (if (not whitespace-all-my-files)
728 (progn
729 (disable-timeout whitespace-rescan-timer)
730 (setq whitespace-rescan-timer nil))
731 (while whitespace-all-my-files
732 (setq thiselt (car whitespace-all-my-files))
733 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
734 (setq buffile (car thiselt))
735 (setq bufname (cadr thiselt))
736 (setq buf (get-buffer bufname))
737 (if (buffer-live-p buf)
738 (save-excursion
739 ;;(message "buffer %s live" bufname)
740 (set-buffer bufname)
741 (if whitespace-mode
742 (progn
743 ;;(message "checking for whitespace in %s" bufname)
744 (if whitespace-auto-cleanup
745 (progn
746 ;;(message "cleaning up whitespace in %s" bufname)
747 (whitespace-cleanup))
748 (progn
749 ;;(message "whitespace-buffer %s." (buffer-name))
750 (whitespace-buffer t))))
751 ;;(message "Removing %s from refresh list" bufname)
752 (whitespace-refresh-rescan-list buffile bufname)))
753 ;;(message "Removing %s from refresh list" bufname)
754 (whitespace-refresh-rescan-list buffile bufname))))))
756 (defun whitespace-refresh-rescan-list (buffile bufname)
757 "Refresh the list of files to be rescaned for whitespace creep."
758 (if whitespace-all-buffer-files
759 (setq whitespace-all-buffer-files
760 (delete (list buffile bufname) whitespace-all-buffer-files))
761 (when whitespace-rescan-timer
762 (disable-timeout whitespace-rescan-timer)
763 (setq whitespace-rescan-timer nil))))
765 ;;;###autoload
766 (defcustom whitespace-global-mode nil
767 "Toggle global Whitespace mode.
769 Setting this variable directly does not take effect;
770 use either \\[customize] or the function `whitespace-global-mode'
771 \(which see)."
772 :set (lambda (sym val)
773 (whitespace-global-mode (or val 0)))
774 :initialize 'custom-initialize-default
775 :type 'boolean
776 :group 'whitespace
777 :require 'whitespace)
779 ;;;###autoload
780 (defun whitespace-global-mode (&optional arg)
781 "Toggle using Whitespace mode in new buffers.
782 With ARG, turn the mode on if and only iff ARG is positive.
784 When this mode is active, `whitespace-buffer' is added to
785 `find-file-hooks' and `kill-buffer-hook'."
786 (interactive "P")
787 (setq arg (if arg
788 (> (prefix-numeric-value arg) 0)
789 (not whitespace-global-mode)))
790 (if arg
791 (progn
792 (add-hook 'find-file-hooks 'whitespace-buffer)
793 (add-hook 'local-write-file-hooks 'whitespace-write-file-hook)
794 (add-hook 'kill-buffer-hook 'whitespace-buffer))
795 (remove-hook 'find-file-hooks 'whitespace-buffer)
796 (remove-hook 'local-write-file-hooks 'whitespace-write-file-hook)
797 (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
799 ;;;###autoload
800 (defun whitespace-write-file-hook ()
801 "The local-write-file-hook to be called on the buffer when
802 whitespace check is enabled."
803 (interactive)
804 (let ((werr nil))
805 (if whitespace-auto-cleanup
806 (whitespace-cleanup)
807 (setq werr (whitespace-buffer)))
808 (if (and whitespace-abort-on-error werr)
809 (error (concat "Abort write due to whitespaces in "
810 buffer-file-name))))
811 nil)
813 ;;;###autoload
814 (defun whitespace-describe ()
815 "A summary of whitespaces and what this library can do about them.
817 The whitespace library is intended to find and help fix five different types
818 of whitespace problems that commonly exist in source code.
820 1. Leading space (empty lines at the top of a file).
821 2. Trailing space (empty lines at the end of a file).
822 3. Indentation space (8 or more spaces at beginning of line, that should be
823 replaced with TABS).
824 4. Spaces followed by a TAB. (Almost always, we never want that).
825 5. Spaces or TABS at the end of a line.
827 Whitespace errors are reported in a buffer, and on the modeline.
829 Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
830 where `x' and `y' can be one (or more) of:
832 e - End-of-Line whitespace.
833 i - Indentation whitespace.
834 l - Leading whitespace.
835 s - Space followed by Tab.
836 t - Trailing whitespace.
838 If any of the whitespace checks is turned off, the modeline will display a
839 !<y>.
841 (since (3) is the most controversial one, here is the rationale: Most
842 terminal drivers and printer drivers have TAB configured or even
843 hardcoded to be 8 spaces. (Some of them allow configuration, but almost
844 always they default to 8.)
846 Changing `tab-width' to other than 8 and editing will cause your code to
847 look different from within Emacs, and say, if you cat it or more it, or
848 even print it.
850 Almost all the popular programming modes let you define an offset (like
851 c-basic-offset or perl-indent-level) to configure the offset, so you
852 should never have to set your `tab-width' to be other than 8 in all these
853 modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs
854 to replace your 8 spaces with one \t (try it). If vi users in your
855 office complain, tell them to use vim, which distinguishes between
856 tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
857 to set smarttab.)
859 All the above have caused (and will cause) unwanted codeline integration and
860 merge problems.
862 whitespace.el will complain if it detects whitespaces on opening a file, and
863 warn you on closing a file also (in case you had inserted any
864 whitespaces during the process of your editing)."
865 (interactive)
866 (message "Use C-h f whitespace-describe to read about whitespace.el v%s."
867 whitespace-version))
869 (defun whitespace-unload-hook ()
870 (remove-hook 'find-file-hooks 'whitespace-buffer)
871 (remove-hook 'local-write-file-hooks 'whitespace-write-file-hook)
872 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
874 (provide 'whitespace)
875 ;;; whitespace.el ends here