1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
7 ;; Keywords: convenience
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)
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; URL: http://www.dsmit.com/lisp/
30 ;; The whitespace library is intended to find and help fix five different types
31 ;; of whitespace problems that commonly exist in source code.
33 ;; 1. Leading space (empty lines at the top of a file).
34 ;; 2. Trailing space (empty lines at the end of a file).
35 ;; 3. Indentation space (8 or more spaces at beginning of line, that should be
36 ;; replaced with TABS).
37 ;; 4. Spaces followed by a TAB. (Almost always, we never want that).
38 ;; 5. Spaces or TABS at the end of a line.
40 ;; Whitespace errors are reported in a buffer, and on the modeline.
42 ;; Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
43 ;; where `x' and `y' can be one (or more) of:
45 ;; e - End-of-Line whitespace.
46 ;; i - Indentation whitespace.
47 ;; l - Leading whitespace.
48 ;; s - Space followed by Tab.
49 ;; t - Trailing whitespace.
51 ;; If any of the whitespace checks is turned off, the modeline will display a
54 ;; (since (3) is the most controversial one, here is the rationale: Most
55 ;; terminal drivers and printer drivers have TAB configured or even
56 ;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost
57 ;; always they default to 8.)
59 ;; Changing `tab-width' to other than 8 and editing will cause your code to
60 ;; look different from within Emacs, and say, if you cat it or more it, or
63 ;; Almost all the popular programming modes let you define an offset (like
64 ;; c-basic-offset or perl-indent-level) to configure the offset, so you
65 ;; should never have to set your `tab-width' to be other than 8 in all
66 ;; these modes. In fact, with an indent level of say, 4, 2 TABS will cause
67 ;; Emacs to replace your 8 spaces with one \t (try it). If vi users in
68 ;; your office complain, tell them to use vim, which distinguishes between
69 ;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
72 ;; All the above have caused (and will cause) unwanted codeline integration and
75 ;; whitespace.el will complain if it detects whitespaces on opening a file, and
76 ;; warn you on closing a file also (in case you had inserted any
77 ;; whitespaces during the process of your editing).
79 ;; Exported functions:
81 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
82 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
83 ;; `whitespace-region' - To check between point and mark for whitespace
85 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
86 ;; and mark in the current buffer.
90 (defvar whitespace-version
"3.5" "Version of the whitespace library.")
92 (defvar whitespace-all-buffer-files nil
93 "An associated list of buffers and files checked for whitespace cleanliness.
95 This is to enable periodic checking of whitespace cleanliness in the files
96 visited by the buffers.")
98 (defvar whitespace-rescan-timer nil
99 "Timer object used to rescan the files in buffers that have been modified.")
101 ;; Tell Emacs about this new kind of minor mode
102 (defvar whitespace-mode nil
103 "Non-nil when Whitespace mode (a minor mode) is enabled.")
104 (make-variable-buffer-local 'whitespace-mode
)
105 (put 'whitespace-mode
'permanent-local nil
)
107 (defvar whitespace-mode-line nil
108 "String to display in the mode line for Whitespace mode.")
109 (make-variable-buffer-local 'whitespace-mode-line
)
110 (put 'whitespace-mode-line
'permanent-local nil
)
112 (defvar whitespace-check-buffer-leading nil
113 "Test leading whitespace for file in current buffer if t.")
114 (make-variable-buffer-local 'whitespace-check-buffer-leading
)
115 (put 'whitespace-check-buffer-leading
'permanent-local nil
)
117 (defvar whitespace-check-buffer-trailing nil
118 "Test trailing whitespace for file in current buffer if t.")
119 (make-variable-buffer-local 'whitespace-check-buffer-trailing
)
120 (put 'whitespace-check-buffer-trailing
'permanent-local nil
)
122 (defvar whitespace-check-buffer-indent nil
123 "Test indentation whitespace for file in current buffer if t.")
124 (make-variable-buffer-local 'whitespace-check-buffer-indent
)
125 (put 'whitespace-check-buffer-indent
'permanent-local nil
)
127 (defvar whitespace-check-buffer-spacetab nil
128 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
129 (make-variable-buffer-local 'whitespace-check-buffer-spacetab
)
130 (put 'whitespace-check-buffer-spacetab
'permanent-local nil
)
132 (defvar whitespace-check-buffer-ateol nil
133 "Test end-of-line whitespace for file in current buffer if t.")
134 (make-variable-buffer-local 'whitespace-check-buffer-ateol
)
135 (put 'whitespace-check-buffer-ateol
'permanent-local nil
)
137 (defvar whitespace-highlighted-space nil
138 "The variable to store the extent to highlight.")
139 (make-variable-buffer-local 'whitespace-highlighted-space
)
140 (put 'whitespace-highlighted-space
'permanent-local nil
)
142 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
144 (if (not (fboundp 'defgroup
))
145 (defmacro defgroup
(sym memb doc
&rest args
)
146 "Null macro for `defgroup' in all versions of Emacs that don't define it."
148 (if (not (fboundp 'defcustom
))
149 (defmacro defcustom
(sym val doc
&rest args
)
150 "Macro to alias `defcustom' to `defvar' in all versions of Emacs that
152 `(defvar ,sym
,val
,doc
))))
154 (defalias 'whitespace-make-overlay
155 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
156 (defalias 'whitespace-overlay-put
157 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
158 (defalias 'whitespace-delete-overlay
159 (if (featurep 'xemacs
) 'delete-extent
'delete-overlay
))
160 (defalias 'whitespace-overlay-start
161 (if (featurep 'xemacs
) 'extent-start
'overlay-start
))
162 (defalias 'whitespace-overlay-end
163 (if (featurep 'xemacs
) 'extent-end
'overlay-end
))
164 (defalias 'whitespace-mode-line-update
165 (if (featurep 'xemacs
) 'redraw-modeline
'force-mode-line-update
))
167 (defgroup whitespace nil
168 "Check for and fix five different types of whitespaces in source code."
170 :link
'(emacs-commentary-link "whitespace.el")
171 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
172 ;; which is 'editing?
173 :group
(if (featurep 'xemacs
) 'editing
'convenience
))
175 (defcustom whitespace-check-leading-whitespace t
176 "Flag to check leading whitespace. This is the global for the system.
177 It can be overriden by setting a buffer local variable
178 `whitespace-check-buffer-leading'."
182 (defcustom whitespace-check-trailing-whitespace t
183 "Flag to check trailing whitespace. This is the global for the system.
184 It can be overriden by setting a buffer local variable
185 `whitespace-check-buffer-trailing'."
189 (defcustom whitespace-check-spacetab-whitespace t
190 "Flag to check space followed by a TAB. This is the global for the system.
191 It can be overriden by setting a buffer local variable
192 `whitespace-check-buffer-spacetab'."
196 (defcustom whitespace-spacetab-regexp
"[ ]+\t"
197 "Regexp to match one or more spaces followed by a TAB."
201 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
202 "Flag to check indentation whitespace. This is the global for the system.
203 It can be overriden by setting a buffer local variable
204 `whitespace-check-buffer-indent'."
208 (defcustom whitespace-indent-regexp
"^\t*\\( \\)+"
209 "Regexp to match multiples of eight spaces near line beginnings.
210 The default value ignores leading TABs."
214 (defcustom whitespace-check-ateol-whitespace t
215 "Flag to check end-of-line whitespace. This is the global for the system.
216 It can be overriden by setting a buffer local variable
217 `whitespace-check-buffer-ateol'."
221 (defcustom whitespace-ateol-regexp
"[ \t]+$"
222 "Regexp to match one or more TABs or spaces at line ends."
226 (defcustom whitespace-errbuf
"*Whitespace Errors*"
227 "The name of the buffer where whitespace related messages will be logged."
231 (defcustom whitespace-clean-msg
"clean."
232 "If non-nil, this message will be displayed after a whitespace check
233 determines a file to be clean."
237 (defcustom whitespace-abort-on-error nil
238 "While writing a file, abort if the file is unclean.
239 If `whitespace-auto-cleanup' is set, that takes precedence over
244 (defcustom whitespace-auto-cleanup nil
245 "Cleanup a buffer automatically on finding it whitespace unclean."
249 (defcustom whitespace-silent nil
250 "All whitespace errors will be shown only in the modeline when t.
252 Note that setting this may cause all whitespaces introduced in a file to go
253 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
254 Errors*' buffer before opening (or closing) another file."
258 (defcustom whitespace-modes
'(ada-mode asm-mode autoconf-mode awk-mode
259 c-mode c
++-mode cc-mode
260 change-log-mode cperl-mode
261 electric-nroff-mode emacs-lisp-mode
262 f90-mode fortran-mode html-mode
263 html3-mode java-mode jde-mode
264 ksh-mode latex-mode LaTeX-mode
265 lisp-mode m4-mode makefile-mode
266 modula-2-mode nroff-mode objc-mode
267 pascal-mode perl-mode prolog-mode
268 python-mode scheme-mode sgml-mode
269 sh-mode shell-script-mode simula-mode
270 tcl-mode tex-mode texinfo-mode
273 "Major modes in which we turn on whitespace checking.
275 These are mostly programming and documentation modes. But you may add other
276 modes that you want whitespaces checked in by adding something like the
277 following to your `.emacs':
279 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
282 Or, alternately, you can use the Emacs `customize' command to set this."
283 :type
'(repeat symbol
)
286 (defcustom whitespace-rescan-timer-time
600
287 "Period in seconds to rescan modified buffers for whitespace creep.
289 This is the period after which the timer will fire causing
290 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
293 To disable timer scans, set this to zero."
297 (defcustom whitespace-display-in-modeline t
298 "Display whitespace errors on the modeline."
302 (defcustom whitespace-display-spaces-in-color t
303 "Display the bogus whitespaces by coloring them with the face
304 `whitespace-highlight'."
308 (defgroup whitespace-faces nil
309 "Faces used in whitespace."
310 :prefix
"whitespace-"
314 (defface whitespace-highlight
'((((class color
) (background light
))
315 (:background
"green1"))
316 (((class color
) (background dark
))
317 (:background
"sea green"))
318 (((class grayscale mono
)
320 (:background
"black"))
321 (((class grayscale mono
)
323 (:background
"white")))
324 "Face used for highlighting the bogus whitespaces that exist in the buffer."
325 :group
'whitespace-faces
)
326 ;; backward-compatibility alias
327 (put 'whitespace-highlight-face
'face-alias
'whitespace-highlight
)
329 (if (not (assoc 'whitespace-mode minor-mode-alist
))
330 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
333 (set-default 'whitespace-check-buffer-leading
334 whitespace-check-leading-whitespace
)
335 (set-default 'whitespace-check-buffer-trailing
336 whitespace-check-trailing-whitespace
)
337 (set-default 'whitespace-check-buffer-indent
338 whitespace-check-indent-whitespace
)
339 (set-default 'whitespace-check-buffer-spacetab
340 whitespace-check-spacetab-whitespace
)
341 (set-default 'whitespace-check-buffer-ateol
342 whitespace-check-ateol-whitespace
)
344 (defun whitespace-check-whitespace-mode (&optional arg
)
345 "Test and set the whitespace-mode in qualifying buffers."
346 (if (null whitespace-mode
)
347 (setq whitespace-mode
348 (if (or arg
(member major-mode whitespace-modes
))
353 (defun whitespace-toggle-leading-check ()
354 "Toggle the check for leading space in the local buffer."
356 (let ((current-val whitespace-check-buffer-leading
))
357 (setq whitespace-check-buffer-leading
(not current-val
))
358 (message "Will%s check for leading space in buffer."
359 (if whitespace-check-buffer-leading
"" " not"))
360 (if whitespace-check-buffer-leading
(whitespace-buffer-leading))))
363 (defun whitespace-toggle-trailing-check ()
364 "Toggle the check for trailing space in the local buffer."
366 (let ((current-val whitespace-check-buffer-trailing
))
367 (setq whitespace-check-buffer-trailing
(not current-val
))
368 (message "Will%s check for trailing space in buffer."
369 (if whitespace-check-buffer-trailing
"" " not"))
370 (if whitespace-check-buffer-trailing
(whitespace-buffer-trailing))))
373 (defun whitespace-toggle-indent-check ()
374 "Toggle the check for indentation space in the local buffer."
376 (let ((current-val whitespace-check-buffer-indent
))
377 (setq whitespace-check-buffer-indent
(not current-val
))
378 (message "Will%s check for indentation space in buffer."
379 (if whitespace-check-buffer-indent
"" " not"))
380 (if whitespace-check-buffer-indent
381 (whitespace-buffer-search whitespace-indent-regexp
))))
384 (defun whitespace-toggle-spacetab-check ()
385 "Toggle the check for space-followed-by-TABs in the local buffer."
387 (let ((current-val whitespace-check-buffer-spacetab
))
388 (setq whitespace-check-buffer-spacetab
(not current-val
))
389 (message "Will%s check for space-followed-by-TABs in buffer."
390 (if whitespace-check-buffer-spacetab
"" " not"))
391 (if whitespace-check-buffer-spacetab
392 (whitespace-buffer-search whitespace-spacetab-regexp
))))
396 (defun whitespace-toggle-ateol-check ()
397 "Toggle the check for end-of-line space in the local buffer."
399 (let ((current-val whitespace-check-buffer-ateol
))
400 (setq whitespace-check-buffer-ateol
(not current-val
))
401 (message "Will%s check for end-of-line space in buffer."
402 (if whitespace-check-buffer-ateol
"" " not"))
403 (if whitespace-check-buffer-ateol
404 (whitespace-buffer-search whitespace-ateol-regexp
))))
408 (defun whitespace-buffer (&optional quiet
)
409 "Find five different types of white spaces in buffer.
411 1. Leading space \(empty lines at the top of a file\).
412 2. Trailing space \(empty lines at the end of a file\).
413 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
414 4. Spaces followed by a TAB. \(Almost always, we never want that\).
415 5. Spaces or TABS at the end of a line.
417 Check for whitespace only if this buffer really contains a non-empty file
419 1. the major mode is one of the whitespace-modes, or
420 2. `whitespace-buffer' was explicitly called with a prefix argument."
422 (let ((whitespace-error nil
))
423 (whitespace-check-whitespace-mode current-prefix-arg
)
424 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
426 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
427 (whitespace-tickle-timer)
428 (overlay-recenter (point-max))
429 (remove-overlays nil nil
'face
'whitespace-highlight
)
430 (if whitespace-auto-cleanup
433 (message "Can't cleanup: %s is read-only" (buffer-name)))
434 (whitespace-cleanup-internal))
435 (let ((whitespace-leading (if whitespace-check-buffer-leading
436 (whitespace-buffer-leading)
438 (whitespace-trailing (if whitespace-check-buffer-trailing
439 (whitespace-buffer-trailing)
441 (whitespace-indent (if whitespace-check-buffer-indent
442 (whitespace-buffer-search
443 whitespace-indent-regexp
)
445 (whitespace-spacetab (if whitespace-check-buffer-spacetab
446 (whitespace-buffer-search
447 whitespace-spacetab-regexp
)
449 (whitespace-ateol (if whitespace-check-buffer-ateol
450 (whitespace-buffer-search
451 whitespace-ateol-regexp
)
453 (whitespace-errmsg nil
)
454 (whitespace-filename buffer-file-name
)
455 (whitespace-this-modeline ""))
457 ;; Now let's complain if we found any of the above.
458 (setq whitespace-error
(or whitespace-leading whitespace-indent
459 whitespace-spacetab whitespace-ateol
460 whitespace-trailing
))
464 (setq whitespace-errmsg
465 (concat whitespace-filename
" contains:\n"
466 (if whitespace-leading
467 "Leading whitespace\n")
468 (if whitespace-indent
469 (concat "Indentation whitespace"
470 whitespace-indent
"\n"))
471 (if whitespace-spacetab
472 (concat "Space followed by Tab"
473 whitespace-spacetab
"\n"))
475 (concat "End-of-line whitespace"
476 whitespace-ateol
"\n"))
477 (if whitespace-trailing
478 "Trailing whitespace\n")
479 "\ntype `M-x whitespace-cleanup' to "
480 "cleanup the file."))
481 (setq whitespace-this-modeline
482 (concat (if whitespace-ateol
"e")
483 (if whitespace-indent
"i")
484 (if whitespace-leading
"l")
485 (if whitespace-spacetab
"s")
486 (if whitespace-trailing
"t")))))
487 (whitespace-update-modeline whitespace-this-modeline
)
488 (if (get-buffer whitespace-errbuf
)
489 (kill-buffer whitespace-errbuf
))
490 (with-current-buffer (get-buffer-create whitespace-errbuf
)
491 (if whitespace-errmsg
493 (insert whitespace-errmsg
)
494 (if (not (or quiet whitespace-silent
))
495 (display-buffer (current-buffer) t
))
497 (message "Whitespaces: [%s%s] in %s"
498 whitespace-this-modeline
499 (let ((whitespace-unchecked
500 (whitespace-unchecked-whitespaces)))
501 (if whitespace-unchecked
502 (concat "!" whitespace-unchecked
)
504 whitespace-filename
)))
505 (if (and (not quiet
) (not (equal whitespace-clean-msg
"")))
506 (message "%s %s" whitespace-filename
507 whitespace-clean-msg
))))))))
511 (defun whitespace-region (s e
)
512 "Check the region for whitespace errors."
516 (narrow-to-region s e
)
517 (whitespace-buffer))))
520 (defun whitespace-cleanup ()
521 "Cleanup the five different kinds of whitespace problems.
522 It normally applies to the whole buffer, but in Transient Mark mode
523 when the mark is active it applies to the region.
524 See `whitespace-buffer' docstring for a summary of the problems."
526 (if (and transient-mark-mode mark-active
)
527 (whitespace-cleanup-region (region-beginning) (region-end))
528 (whitespace-cleanup-internal)))
530 (defun whitespace-cleanup-internal (&optional region-only
)
531 ;; If this buffer really contains a file, then run, else quit.
532 (whitespace-check-whitespace-mode current-prefix-arg
)
533 (if (and buffer-file-name whitespace-mode
)
534 (let ((whitespace-any nil
)
535 (whitespace-tabwith 8)
536 (whitespace-tabwith-saved tab-width
))
538 ;; since all printable TABS should be 8, irrespective of how
539 ;; they are displayed.
540 (setq tab-width whitespace-tabwith
)
542 (if (and whitespace-check-buffer-leading
543 (whitespace-buffer-leading))
545 (whitespace-buffer-leading-cleanup)
546 (setq whitespace-any t
)))
548 (if (and whitespace-check-buffer-trailing
549 (whitespace-buffer-trailing))
551 (whitespace-buffer-trailing-cleanup)
552 (setq whitespace-any t
)))
554 (if (and whitespace-check-buffer-indent
555 (whitespace-buffer-search whitespace-indent-regexp
))
557 (whitespace-indent-cleanup)
558 (setq whitespace-any t
)))
560 (if (and whitespace-check-buffer-spacetab
561 (whitespace-buffer-search whitespace-spacetab-regexp
))
563 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
564 (setq whitespace-any t
)))
566 (if (and whitespace-check-buffer-ateol
567 (whitespace-buffer-search whitespace-ateol-regexp
))
569 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
570 (setq whitespace-any t
)))
572 ;; Call this recursively till everything is taken care of
574 (whitespace-cleanup-internal)
575 ;; if we are done, talk to the user
577 (unless whitespace-silent
579 (message "The region is now clean")
580 (message "%s is now clean" buffer-file-name
)))
581 (whitespace-update-modeline)))
582 (setq tab-width whitespace-tabwith-saved
))))
585 (defun whitespace-cleanup-region (s e
)
586 "Whitespace cleanup on the region."
590 (narrow-to-region s e
)
591 (whitespace-cleanup-internal t
))
592 (whitespace-buffer t
)))
594 (defun whitespace-buffer-leading ()
595 "Return t if the current buffer has leading newline characters.
596 If highlighting is enabled, highlight these characters."
598 (goto-char (point-min))
599 (skip-chars-forward "\n")
601 (whitespace-highlight-the-space (point-min) (point))
604 (defun whitespace-buffer-leading-cleanup ()
605 "Remove any leading newline characters from current buffer."
607 (goto-char (point-min))
608 (skip-chars-forward "\n")
609 (delete-region (point-min) (point))))
611 (defun whitespace-buffer-trailing ()
612 "Return t if the current buffer has extra trailing newline characters.
613 If highlighting is enabled, highlight these characters."
615 (goto-char (point-max))
616 (skip-chars-backward "\n")
619 (whitespace-highlight-the-space (point) (point-max))
622 (defun whitespace-buffer-trailing-cleanup ()
623 "Remove extra trailing newline characters from current buffer."
625 (goto-char (point-max))
626 (skip-chars-backward "\n")
629 (delete-region (point) (point-max)))))
631 (defun whitespace-buffer-search (regexp)
632 "Search for any given whitespace REGEXP."
634 (let (whitespace-retval)
636 (goto-char (point-min))
637 (while (re-search-forward regexp nil t
)
638 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
639 (push (match-beginning 0) whitespace-retval
)))
640 (when whitespace-retval
641 (format " %s" (nreverse whitespace-retval
))))))
643 (defun whitespace-buffer-cleanup (regexp newregexp
)
644 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
646 (goto-char (point-min))
647 (while (re-search-forward regexp nil t
)
648 (replace-match newregexp
))))
650 (defun whitespace-indent-cleanup ()
651 "Search for 8/more spaces at the start of a line and replace it with tabs."
653 (goto-char (point-min))
654 (while (re-search-forward whitespace-indent-regexp nil t
)
655 (let ((column (current-column))
656 (indent-tabs-mode t
))
657 (delete-region (match-beginning 0) (point))
658 (indent-to column
)))))
660 (defun whitespace-unchecked-whitespaces ()
661 "Return the list of whitespaces whose testing has been suppressed."
662 (let ((unchecked-spaces
663 (concat (if (not whitespace-check-buffer-ateol
) "e")
664 (if (not whitespace-check-buffer-indent
) "i")
665 (if (not whitespace-check-buffer-leading
) "l")
666 (if (not whitespace-check-buffer-spacetab
) "s")
667 (if (not whitespace-check-buffer-trailing
) "t"))))
668 (if (not (equal unchecked-spaces
""))
672 (defun whitespace-update-modeline (&optional whitespace-err
)
673 "Update modeline with whitespace errors.
674 Also with whitespaces whose testing has been turned off."
675 (if whitespace-display-in-modeline
677 (setq whitespace-mode-line nil
)
679 (if (and whitespace-err
(not (equal whitespace-err
"")))
680 (setq whitespace-mode-line whitespace-err
))
681 ;; Whitespace suppressed errors
682 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
683 (if whitespace-unchecked
684 (setq whitespace-mode-line
685 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
686 ;; Add the whitespace modeline prefix
687 (setq whitespace-mode-line
(if whitespace-mode-line
688 (concat " W:" whitespace-mode-line
)
690 (whitespace-mode-line-update))))
692 (defun whitespace-highlight-the-space (b e
)
693 "Highlight the current line, unhighlighting a previously jumped to line."
694 (if whitespace-display-spaces-in-color
695 (let ((ol (whitespace-make-overlay b e
)))
696 (whitespace-overlay-put ol
'face
'whitespace-highlight
))))
698 (defun whitespace-unhighlight-the-space()
699 "Unhighlight the currently highlight line."
700 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space
)
702 (mapc 'whitespace-delete-overlay whitespace-highlighted-space
)
703 (setq whitespace-highlighted-space nil
))))
705 (defun whitespace-check-buffer-list (buf-name buf-file
)
706 "Add a buffer and its file to the whitespace monitor list.
708 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
709 periodically for whitespace."
710 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
711 whitespace-all-buffer-files
)))
712 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
714 (defun whitespace-tickle-timer ()
715 "Tickle timer to periodically to scan qualifying files for whitespace creep.
717 If timer is not set, then set it to scan the files in
718 `whitespace-all-buffer-files' periodically (defined by
719 `whitespace-rescan-timer-time') for whitespace creep."
720 (if (and whitespace-rescan-timer-time
(not whitespace-rescan-timer
))
721 (setq whitespace-rescan-timer
722 (add-timeout whitespace-rescan-timer-time
723 'whitespace-rescan-files-in-buffers nil
724 whitespace-rescan-timer-time
))))
726 (defun whitespace-rescan-files-in-buffers (&optional arg
)
727 "Check monitored files for whitespace creep since last scan."
728 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
729 buffile bufname thiselt buf
)
730 (if (not whitespace-all-my-files
)
732 (disable-timeout whitespace-rescan-timer
)
733 (setq whitespace-rescan-timer nil
))
734 (while whitespace-all-my-files
735 (setq thiselt
(car whitespace-all-my-files
))
736 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
737 (setq buffile
(car thiselt
))
738 (setq bufname
(cadr thiselt
))
739 (setq buf
(get-buffer bufname
))
740 (if (buffer-live-p buf
)
742 ;;(message "buffer %s live" bufname)
746 ;;(message "checking for whitespace in %s" bufname)
747 (if whitespace-auto-cleanup
749 ;;(message "cleaning up whitespace in %s" bufname)
750 (whitespace-cleanup-internal))
752 ;;(message "whitespace-buffer %s." (buffer-name))
753 (whitespace-buffer t
))))
754 ;;(message "Removing %s from refresh list" bufname)
755 (whitespace-refresh-rescan-list buffile bufname
)))
756 ;;(message "Removing %s from refresh list" bufname)
757 (whitespace-refresh-rescan-list buffile bufname
))))))
759 (defun whitespace-refresh-rescan-list (buffile bufname
)
760 "Refresh the list of files to be rescanned for whitespace creep."
761 (if whitespace-all-buffer-files
762 (setq whitespace-all-buffer-files
763 (delete (list buffile bufname
) whitespace-all-buffer-files
))
764 (when whitespace-rescan-timer
765 (disable-timeout whitespace-rescan-timer
)
766 (setq whitespace-rescan-timer nil
))))
769 (defalias 'global-whitespace-mode
'whitespace-global-mode
)
772 (define-minor-mode whitespace-global-mode
773 "Toggle using Whitespace mode in new buffers.
774 With ARG, turn the mode on iff ARG is positive.
776 When this mode is active, `whitespace-buffer' is added to
777 `find-file-hook' and `kill-buffer-hook'."
780 (if whitespace-global-mode
782 (add-hook 'find-file-hook
'whitespace-buffer
)
783 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
784 (add-hook 'kill-buffer-hook
'whitespace-buffer
))
785 (remove-hook 'find-file-hook
'whitespace-buffer
)
786 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
787 (remove-hook 'kill-buffer-hook
'whitespace-buffer
)))
790 (defun whitespace-write-file-hook ()
791 "Hook function to be called on the buffer when whitespace check is enabled.
792 This is meant to be added buffer-locally to `write-file-functions'."
795 (if whitespace-auto-cleanup
796 (whitespace-cleanup-internal)
797 (setq werr
(whitespace-buffer)))
798 (if (and whitespace-abort-on-error werr
)
799 (error (concat "Abort write due to whitespaces in "
803 (defun whitespace-unload-hook ()
804 (remove-hook 'find-file-hook
'whitespace-buffer
)
805 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
806 (remove-hook 'kill-buffer-hook
'whitespace-buffer
))
808 (add-hook 'whitespace-unload-hook
'whitespace-unload-hook
)
810 (provide 'whitespace
)
812 ;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
813 ;;; whitespace.el ends here