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.21 2002/09/13 06:21:32 lektu 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)
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.
28 ;; Whitespace.el 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.2" "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
149 (if (not (fboundp 'defcustom
))
150 (defmacro defcustom
(sym val doc
&rest args
)
151 "Macro to alias defcustom to defvar in all versions of Emacs that
152 don't define defcustom"
153 `(defvar ,sym
,val
,doc
))))
155 (if (fboundp 'make-overlay
)
157 (defalias 'whitespace-make-overlay
'make-overlay
)
158 (defalias 'whitespace-overlay-put
'overlay-put
)
159 (defalias 'whitespace-delete-overlay
'delete-overlay
)
160 (defalias 'whitespace-overlay-start
'overlay-start
)
161 (defalias 'whitespace-overlay-end
'overlay-end
)
162 (defalias 'whitespace-mode-line-update
'force-mode-line-update
))
163 (defalias 'whitespace-make-overlay
'make-extent
)
164 (defalias 'whitespace-overlay-put
'set-extent-property
)
165 (defalias 'whitespace-delete-overlay
'delete-extent
)
166 (defalias 'whitespace-overlay-start
'extent-start
)
167 (defalias 'whitespace-overlay-end
'extent-end
)
168 (defalias 'whitespace-mode-line-update
'redraw-modeline
))
170 (if (featurep 'xemacs
)
171 (defgroup whitespace nil
172 "Check for and fix five different types of whitespaces in source code."
173 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
174 ;; which is 'editing?
176 (defgroup whitespace nil
177 "Check for and fix five different types of whitespaces in source code."
179 :group
'convenience
))
181 (defcustom whitespace-check-leading-whitespace t
182 "Flag to check leading whitespace. This is the global for the system.
183 It can be overriden by setting a buffer local variable
184 `whitespace-check-buffer-leading'"
188 (defcustom whitespace-check-trailing-whitespace t
189 "Flag to check trailing whitespace. This is the global for the system.
190 It can be overriden by setting a buffer local variable
191 `whitespace-check-buffer-trailing'"
195 (defcustom whitespace-check-spacetab-whitespace t
196 "Flag to check space followed by a TAB. This is the global for the system.
197 It can be overriden by setting a buffer local variable
198 `whitespace-check-buffer-spacetab'"
202 (defcustom whitespace-spacetab-regexp
"[ ]+\t"
203 "Regexp to match a space followed by a TAB."
207 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
208 "Flag to check indentation whitespace. This is the global for the system.
209 It can be overriden by setting a buffer local variable
210 `whitespace-check-buffer-indent'"
214 (defcustom whitespace-indent-regexp
(concat "^\\(\t*\\) " " ")
215 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
219 (defcustom whitespace-check-ateol-whitespace t
220 "Flag to check end-of-line whitespace. This is the global for the system.
221 It can be overriden by setting a buffer local variable
222 `whitespace-check-buffer-ateol'"
226 ;; (defcustom whitespace-ateol-regexp "[ \t]$"
227 (defcustom whitespace-ateol-regexp
"[ \t]+$"
228 "Regexp to match a TAB or a space at the EOL."
232 (defcustom whitespace-errbuf
"*Whitespace Errors*"
233 "The name of the buffer where whitespace related messages will be logged."
237 (defcustom whitespace-abort-on-error nil
238 "While writing a file, abort if the file is unclean. If
239 `whitespace-auto-cleanup' is set, that takes precedence over this
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
304 `whitespace-highlight-face'."
308 (defgroup whitespace-faces nil
309 "Faces used in whitespace."
310 :prefix
"whitespace-"
314 (defface whitespace-highlight-face
'((((class color
) (background light
))
315 (:background
"green"))
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
)
327 (if (not (assoc 'whitespace-mode minor-mode-alist
))
328 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
331 (set-default 'whitespace-check-buffer-leading
332 whitespace-check-leading-whitespace
)
333 (set-default 'whitespace-check-buffer-trailing
334 whitespace-check-trailing-whitespace
)
335 (set-default 'whitespace-check-buffer-indent
336 whitespace-check-indent-whitespace
)
337 (set-default 'whitespace-check-buffer-spacetab
338 whitespace-check-spacetab-whitespace
)
339 (set-default 'whitespace-check-buffer-ateol
340 whitespace-check-ateol-whitespace
)
342 (defun whitespace-check-whitespace-mode (&optional arg
)
343 "Test and set the whitespace-mode in qualifying buffers."
344 (if (null whitespace-mode
)
345 (setq whitespace-mode
346 (if (or arg
(member major-mode whitespace-modes
))
351 (defun whitespace-toggle-leading-check ()
352 "Toggle the check for leading space in the local buffer."
354 (let ((current-val whitespace-check-buffer-leading
))
355 (setq whitespace-check-buffer-leading
(not current-val
))
356 (message "Will%s check for leading space in buffer."
357 (if whitespace-check-buffer-leading
"" " not"))
358 (if whitespace-check-buffer-leading
(whitespace-buffer-leading))))
361 (defun whitespace-toggle-trailing-check ()
362 "Toggle the check for trailing space in the local buffer."
364 (let ((current-val whitespace-check-buffer-trailing
))
365 (setq whitespace-check-buffer-trailing
(not current-val
))
366 (message "Will%s check for trailing space in buffer."
367 (if whitespace-check-buffer-trailing
"" " not"))
368 (if whitespace-check-buffer-trailing
(whitespace-buffer-trailing))))
371 (defun whitespace-toggle-indent-check ()
372 "Toggle the check for indentation space in the local buffer."
374 (let ((current-val whitespace-check-buffer-indent
))
375 (setq whitespace-check-buffer-indent
(not current-val
))
376 (message "Will%s check for indentation space in buffer."
377 (if whitespace-check-buffer-indent
"" " not"))
378 (if whitespace-check-buffer-indent
379 (whitespace-buffer-search whitespace-indent-regexp
))))
382 (defun whitespace-toggle-spacetab-check ()
383 "Toggle the check for space-followed-by-TABs in the local buffer."
385 (let ((current-val whitespace-check-buffer-spacetab
))
386 (setq whitespace-check-buffer-spacetab
(not current-val
))
387 (message "Will%s check for space-followed-by-TABs in buffer."
388 (if whitespace-check-buffer-spacetab
"" " not"))
389 (if whitespace-check-buffer-spacetab
390 (whitespace-buffer-search whitespace-spacetab-regexp
))))
394 (defun whitespace-toggle-ateol-check ()
395 "Toggle the check for end-of-line space in the local buffer."
397 (let ((current-val whitespace-check-buffer-ateol
))
398 (setq whitespace-check-buffer-ateol
(not current-val
))
399 (message "Will%s check for end-of-line space in buffer."
400 (if whitespace-check-buffer-ateol
"" " not"))
401 (if whitespace-check-buffer-ateol
402 (whitespace-buffer-search whitespace-ateol-regexp
))))
406 (defun whitespace-buffer (&optional quiet
)
407 "Find five different types of white spaces in buffer.
409 1. Leading space \(empty lines at the top of a file\).
410 2. Trailing space \(empty lines at the end of a file\).
411 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
412 4. Spaces followed by a TAB. \(Almost always, we never want that\).
413 5. Spaces or TABS at the end of a line.
415 Check for whitespace only if this buffer really contains a non-empty file
417 1. the major mode is one of the whitespace-modes, or
418 2. `whitespace-buffer' was explicitly called with a prefix argument."
420 (let ((whitespace-error nil
))
421 (whitespace-check-whitespace-mode current-prefix-arg
)
422 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
424 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
425 (whitespace-tickle-timer)
426 (whitespace-unhighlight-the-space)
427 (if whitespace-auto-cleanup
430 (message "Can't cleanup: %s is read-only" (buffer-name)))
431 (whitespace-cleanup))
432 (let ((whitespace-leading (if whitespace-check-buffer-leading
433 (whitespace-buffer-leading)
435 (whitespace-trailing (if whitespace-check-buffer-trailing
436 (whitespace-buffer-trailing)
438 (whitespace-indent (if whitespace-check-buffer-indent
439 (whitespace-buffer-search
440 whitespace-indent-regexp
)
442 (whitespace-spacetab (if whitespace-check-buffer-spacetab
443 (whitespace-buffer-search
444 whitespace-spacetab-regexp
)
446 (whitespace-ateol (if whitespace-check-buffer-ateol
447 (whitespace-buffer-search
448 whitespace-ateol-regexp
)
450 (whitespace-errmsg nil
)
451 (whitespace-filename buffer-file-name
)
452 (whitespace-this-modeline ""))
454 ;; Now let's complain if we found any of the above.
455 (setq whitespace-error
(or whitespace-leading whitespace-indent
456 whitespace-spacetab whitespace-ateol
457 whitespace-trailing
))
461 (setq whitespace-errmsg
462 (concat whitespace-filename
" contains:\n"
463 (if whitespace-leading
464 "Leading whitespace\n")
465 (if whitespace-indent
466 (concat "Indentation whitespace"
467 whitespace-indent
"\n"))
468 (if whitespace-spacetab
469 (concat "Space followed by Tab"
470 whitespace-spacetab
"\n"))
472 (concat "End-of-line whitespace"
473 whitespace-ateol
"\n"))
474 (if whitespace-trailing
475 "Trailing whitespace\n")
476 "\ntype `M-x whitespace-cleanup' to "
477 "cleanup the file."))
478 (setq whitespace-this-modeline
479 (concat (if whitespace-ateol
"e")
480 (if whitespace-indent
"i")
481 (if whitespace-leading
"l")
482 (if whitespace-spacetab
"s")
483 (if whitespace-trailing
"t")))))
484 (whitespace-update-modeline whitespace-this-modeline
)
486 (get-buffer-create whitespace-errbuf
)
487 (kill-buffer whitespace-errbuf
)
488 (get-buffer-create whitespace-errbuf
)
489 (set-buffer whitespace-errbuf
)
490 (if whitespace-errmsg
492 (insert whitespace-errmsg
)
493 (if (not (or quiet whitespace-silent
))
494 (display-buffer whitespace-errbuf t
))
496 (message "Whitespaces: [%s%s] in %s"
497 whitespace-this-modeline
498 (let ((whitespace-unchecked
499 (whitespace-unchecked-whitespaces)))
500 (if whitespace-unchecked
501 (concat "!" whitespace-unchecked
)
503 whitespace-filename
)))
505 (message "%s clean" whitespace-filename
))))))))
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.
523 Use \\[describe-function] whitespace-describe to read a summary of the
524 whitespace problems."
526 ;; If this buffer really contains a file, then run, else quit.
527 (whitespace-check-whitespace-mode current-prefix-arg
)
528 (if (and buffer-file-name whitespace-mode
)
529 (let ((whitespace-any nil
)
530 (whitespace-tabwith 8)
531 (whitespace-tabwith-saved tab-width
))
533 ;; since all printable TABS should be 8, irrespective of how
534 ;; they are displayed.
535 (setq tab-width whitespace-tabwith
)
537 (if (and whitespace-check-buffer-leading
538 (whitespace-buffer-leading))
540 (whitespace-buffer-leading-cleanup)
541 (setq whitespace-any t
)))
543 (if (and whitespace-check-buffer-trailing
544 (whitespace-buffer-trailing))
546 (whitespace-buffer-trailing-cleanup)
547 (setq whitespace-any t
)))
549 (if (and whitespace-check-buffer-indent
550 (whitespace-buffer-search whitespace-indent-regexp
))
552 (whitespace-indent-cleanup)
553 (setq whitespace-any t
)))
555 (if (and whitespace-check-buffer-spacetab
556 (whitespace-buffer-search whitespace-spacetab-regexp
))
558 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
559 (setq whitespace-any t
)))
561 (if (and whitespace-check-buffer-ateol
562 (whitespace-buffer-search whitespace-ateol-regexp
))
564 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
565 (setq whitespace-any t
)))
567 ;; Call this recursively till everything is taken care of
571 (message "%s clean" buffer-file-name
)
572 (whitespace-update-modeline)))
573 (setq tab-width whitespace-tabwith-saved
))))
576 (defun whitespace-cleanup-region (s e
)
577 "Whitespace cleanup on the region."
581 (narrow-to-region s e
)
582 (whitespace-cleanup))
583 (whitespace-buffer t
)))
585 (defun whitespace-buffer-leading ()
586 "Check to see if there are any empty lines at the top of the file."
590 (goto-char (point-min))
595 (if (equal pmin pmax
)
597 (whitespace-highlight-the-space pmin pmax
)
601 (defun whitespace-buffer-leading-cleanup ()
602 "Remove any empty lines at the top of the file."
606 (goto-char (point-min))
611 (if (equal pmin pmax
)
614 (whitespace-buffer-leading-cleanup))))))
616 (defun whitespace-buffer-trailing ()
617 "Check to see if are is more than one empty line at the bottom."
621 (goto-char (point-max))
626 (if (equal pmin pmax
)
628 (goto-char (- (point) 1))
633 (if (equal pmin pmax
)
635 (whitespace-highlight-the-space pmin pmax
)
640 (defun whitespace-buffer-trailing-cleanup ()
641 "Delete all the empty lines at the bottom."
645 (goto-char (point-max))
650 (if (equal pmin pmax
)
652 (goto-char (1- pmin
))
657 (if (equal pmin pmax
)
659 (goto-char (1- (point-max)))
662 (whitespace-buffer-trailing-cleanup))))))))
664 (defun whitespace-buffer-search (regexp)
665 "Search for any given whitespace REGEXP."
666 (let ((whitespace-retval ""))
668 (goto-char (point-min))
669 (while (re-search-forward regexp nil t
)
671 (setq whitespace-retval
(format "%s %s" whitespace-retval
672 (match-beginning 0)))
673 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))))
674 (if (equal "" whitespace-retval
)
676 whitespace-retval
))))
678 (defun whitespace-buffer-cleanup (regexp newregexp
)
679 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
681 (goto-char (point-min))
682 (while (re-search-forward regexp nil t
)
683 (replace-match newregexp
))))
685 (defun whitespace-indent-cleanup ()
686 "Search for 8/more spaces at the start of a line and replace it with tabs."
688 (goto-char (point-min))
689 (while (re-search-forward whitespace-indent-regexp nil t
)
690 (let ((column (current-column))
691 (indent-tabs-mode t
))
692 (delete-region (match-beginning 0) (point))
693 (indent-to column
)))))
695 (defun whitespace-unchecked-whitespaces ()
696 "Return the list of whitespaces whose testing has been suppressed."
697 (let ((unchecked-spaces
698 (concat (if (not whitespace-check-buffer-ateol
) "e")
699 (if (not whitespace-check-buffer-indent
) "i")
700 (if (not whitespace-check-buffer-leading
) "l")
701 (if (not whitespace-check-buffer-spacetab
) "s")
702 (if (not whitespace-check-buffer-trailing
) "t"))))
703 (if (not (equal unchecked-spaces
""))
707 (defun whitespace-update-modeline (&optional whitespace-err
)
708 "Update modeline with whitespace errors.
709 Also with whitespaces whose testing has been turned off."
710 (if whitespace-display-in-modeline
712 (setq whitespace-mode-line nil
)
714 (if (and whitespace-err
(not (equal whitespace-err
"")))
715 (setq whitespace-mode-line whitespace-err
))
716 ;; Whitespace suppressed errors
717 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
718 (if whitespace-unchecked
719 (setq whitespace-mode-line
720 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
721 ;; Add the whitespace modeline prefix
722 (setq whitespace-mode-line
(if whitespace-mode-line
723 (concat " W:" whitespace-mode-line
)
725 (whitespace-mode-line-update))))
727 (defun whitespace-highlight-the-space (b e
)
728 "Highlight the current line, unhighlighting a previously jumped to line."
729 (if whitespace-display-spaces-in-color
731 (whitespace-unhighlight-the-space)
732 (add-to-list 'whitespace-highlighted-space
733 (whitespace-make-overlay b e
))
734 (whitespace-overlay-put (whitespace-make-overlay b e
) 'face
735 'whitespace-highlight-face
))))
736 ;; (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space))
738 (defun whitespace-unhighlight-the-space ()
739 "Unhighlight the currently highlight line."
740 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space
)
741 (let ((whitespace-this-space nil
))
742 (while whitespace-highlighted-space
743 (setq whitespace-this-space
(car whitespace-highlighted-space
))
744 (setq whitespace-highlighted-space
745 (cdr whitespace-highlighted-space
))
746 (whitespace-delete-overlay whitespace-this-space
))
747 (setq whitespace-highlighted-space nil
))
748 (remove-hook 'pre-command-hook
'whitespace-unhighlight-the-space
)))
750 (defun whitespace-check-buffer-list (buf-name buf-file
)
751 "Add a buffer and its file to the whitespace monitor list.
753 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
754 periodically for whitespace."
755 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
756 whitespace-all-buffer-files
)))
757 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
759 (defun whitespace-tickle-timer ()
760 "Tickle timer to periodically to scan qualifying files for whitespace creep.
762 If timer is not set, then set it to scan the files in
763 `whitespace-all-buffer-files' periodically (defined by
764 `whitespace-rescan-timer-time') for whitespace creep."
765 (if (and whitespace-rescan-timer-time
(not whitespace-rescan-timer
))
766 (setq whitespace-rescan-timer
767 (add-timeout whitespace-rescan-timer-time
768 'whitespace-rescan-files-in-buffers nil
769 whitespace-rescan-timer-time
))))
771 (defun whitespace-rescan-files-in-buffers (&optional arg
)
772 "Check monitored files for whitespace creep since last scan."
773 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
774 buffile bufname thiselt buf
)
775 (if (not whitespace-all-my-files
)
777 (disable-timeout whitespace-rescan-timer
)
778 (setq whitespace-rescan-timer nil
))
779 (while whitespace-all-my-files
780 (setq thiselt
(car whitespace-all-my-files
))
781 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
782 (setq buffile
(car thiselt
))
783 (setq bufname
(cadr thiselt
))
784 (setq buf
(get-buffer bufname
))
785 (if (buffer-live-p buf
)
787 ;;(message "buffer %s live" bufname)
791 ;;(message "checking for whitespace in %s" bufname)
792 (if whitespace-auto-cleanup
794 ;;(message "cleaning up whitespace in %s" bufname)
795 (whitespace-cleanup))
797 ;;(message "whitespace-buffer %s." (buffer-name))
798 (whitespace-buffer t
))))
799 ;;(message "Removing %s from refresh list" bufname)
800 (whitespace-refresh-rescan-list buffile bufname
)))
801 ;;(message "Removing %s from refresh list" bufname)
802 (whitespace-refresh-rescan-list buffile bufname
))))))
804 (defun whitespace-refresh-rescan-list (buffile bufname
)
805 "Refresh the list of files to be rescaned for whitespace creep."
806 (if whitespace-all-buffer-files
807 (setq whitespace-all-buffer-files
808 (delete (list buffile bufname
) whitespace-all-buffer-files
))
809 (when whitespace-rescan-timer
810 (disable-timeout whitespace-rescan-timer
)
811 (setq whitespace-rescan-timer nil
))))
814 (define-minor-mode whitespace-global-mode
815 "Toggle using Whitespace mode in new buffers.
816 With ARG, turn the mode on if and only iff ARG is positive.
818 When this mode is active, `whitespace-buffer' is added to
819 `find-file-hook' and `kill-buffer-hook'."
820 :global t
:group
'whitespace
821 (if whitespace-global-mode
823 (add-hook 'find-file-hook
'whitespace-buffer
)
824 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
825 (add-hook 'kill-buffer-hook
'whitespace-buffer
))
826 (remove-hook 'find-file-hook
'whitespace-buffer
)
827 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
828 (remove-hook 'kill-buffer-hook
'whitespace-buffer
)))
831 (defun whitespace-write-file-hook ()
832 "Hook function to be called on the buffer when whitespace check is enabled.
833 This is meant to be added buffer-locally to `write-file-functions'."
836 (if whitespace-auto-cleanup
838 (setq werr
(whitespace-buffer)))
839 (if (and whitespace-abort-on-error werr
)
840 (error (concat "Abort write due to whitespaces in "
844 (defun whitespace-unload-hook ()
845 (remove-hook 'find-file-hook
'whitespace-buffer
)
846 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
847 (remove-hook 'kill-buffer-hook
'whitespace-buffer
))
849 (provide 'whitespace
)
850 ;;; whitespace.el ends here