1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
3 ;; Copyright (C) 1999, 2000, 2005 Free Software Foundation, Inc.
5 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
6 ;; Keywords: convenience
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; URL: http://www.dsmit.com/lisp/
29 ;; The whitespace library is intended to find and help fix five different types
30 ;; of whitespace problems that commonly exist in source code.
32 ;; 1. Leading space (empty lines at the top of a file).
33 ;; 2. Trailing space (empty lines at the end of a file).
34 ;; 3. Indentation space (8 or more spaces at beginning of line, that should be
35 ;; replaced with TABS).
36 ;; 4. Spaces followed by a TAB. (Almost always, we never want that).
37 ;; 5. Spaces or TABS at the end of a line.
39 ;; Whitespace errors are reported in a buffer, and on the modeline.
41 ;; Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
42 ;; where `x' and `y' can be one (or more) of:
44 ;; e - End-of-Line whitespace.
45 ;; i - Indentation whitespace.
46 ;; l - Leading whitespace.
47 ;; s - Space followed by Tab.
48 ;; t - Trailing whitespace.
50 ;; If any of the whitespace checks is turned off, the modeline will display a
53 ;; (since (3) is the most controversial one, here is the rationale: Most
54 ;; terminal drivers and printer drivers have TAB configured or even
55 ;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost
56 ;; always they default to 8.)
58 ;; Changing `tab-width' to other than 8 and editing will cause your code to
59 ;; look different from within Emacs, and say, if you cat it or more it, or
62 ;; Almost all the popular programming modes let you define an offset (like
63 ;; c-basic-offset or perl-indent-level) to configure the offset, so you
64 ;; should never have to set your `tab-width' to be other than 8 in all
65 ;; these modes. In fact, with an indent level of say, 4, 2 TABS will cause
66 ;; Emacs to replace your 8 spaces with one \t (try it). If vi users in
67 ;; your office complain, tell them to use vim, which distinguishes between
68 ;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
71 ;; All the above have caused (and will cause) unwanted codeline integration and
74 ;; whitespace.el will complain if it detects whitespaces on opening a file, and
75 ;; warn you on closing a file also (in case you had inserted any
76 ;; whitespaces during the process of your editing).
78 ;; Exported functions:
80 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
81 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
82 ;; `whitespace-region' - To check between point and mark for whitespace
84 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
85 ;; and mark in the current buffer.
89 (defvar whitespace-version
"3.5" "Version of the whitespace library.")
91 (defvar whitespace-all-buffer-files nil
92 "An associated list of buffers and files checked for whitespace cleanliness.
94 This is to enable periodic checking of whitespace cleanliness in the files
95 visited by the buffers.")
97 (defvar whitespace-rescan-timer nil
98 "Timer object used to rescan the files in buffers that have been modified.")
100 ;; Tell Emacs about this new kind of minor mode
101 (defvar whitespace-mode nil
102 "Non-nil when Whitespace mode (a minor mode) is enabled.")
103 (make-variable-buffer-local 'whitespace-mode
)
104 (put 'whitespace-mode
'permanent-local nil
)
106 (defvar whitespace-mode-line nil
107 "String to display in the mode line for Whitespace mode.")
108 (make-variable-buffer-local 'whitespace-mode-line
)
109 (put 'whitespace-mode-line
'permanent-local nil
)
111 (defvar whitespace-check-buffer-leading nil
112 "Test leading whitespace for file in current buffer if t.")
113 (make-variable-buffer-local 'whitespace-check-buffer-leading
)
114 (put 'whitespace-check-buffer-leading
'permanent-local nil
)
116 (defvar whitespace-check-buffer-trailing nil
117 "Test trailing whitespace for file in current buffer if t.")
118 (make-variable-buffer-local 'whitespace-check-buffer-trailing
)
119 (put 'whitespace-check-buffer-trailing
'permanent-local nil
)
121 (defvar whitespace-check-buffer-indent nil
122 "Test indentation whitespace for file in current buffer if t.")
123 (make-variable-buffer-local 'whitespace-check-buffer-indent
)
124 (put 'whitespace-check-buffer-indent
'permanent-local nil
)
126 (defvar whitespace-check-buffer-spacetab nil
127 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
128 (make-variable-buffer-local 'whitespace-check-buffer-spacetab
)
129 (put 'whitespace-check-buffer-spacetab
'permanent-local nil
)
131 (defvar whitespace-check-buffer-ateol nil
132 "Test end-of-line whitespace for file in current buffer if t.")
133 (make-variable-buffer-local 'whitespace-check-buffer-ateol
)
134 (put 'whitespace-check-buffer-ateol
'permanent-local nil
)
136 (defvar whitespace-highlighted-space nil
137 "The variable to store the extent to highlight.")
138 (make-variable-buffer-local 'whitespace-highlighted-space
)
139 (put 'whitespace-highlighted-space
'permanent-local nil
)
141 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
143 (if (not (fboundp 'defgroup
))
144 (defmacro defgroup
(sym memb doc
&rest args
)
145 "Null macro for `defgroup' in all versions of Emacs that don't define it."
147 (if (not (fboundp 'defcustom
))
148 (defmacro defcustom
(sym val doc
&rest args
)
149 "Macro to alias `defcustom' to `defvar' in all versions of Emacs that
151 `(defvar ,sym
,val
,doc
))))
153 (defalias 'whitespace-make-overlay
154 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
155 (defalias 'whitespace-overlay-put
156 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
157 (defalias 'whitespace-delete-overlay
158 (if (featurep 'xemacs
) 'delete-extent
'delete-overlay
))
159 (defalias 'whitespace-overlay-start
160 (if (featurep 'xemacs
) 'extent-start
'overlay-start
))
161 (defalias 'whitespace-overlay-end
162 (if (featurep 'xemacs
) 'extent-end
'overlay-end
))
163 (defalias 'whitespace-mode-line-update
164 (if (featurep 'xemacs
) 'redraw-modeline
'force-mode-line-update
))
166 (defgroup whitespace nil
167 "Check for and fix five different types of whitespaces in source code."
169 :link
'(emacs-commentary-link "whitespace.el")
170 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
171 ;; which is 'editing?
172 :group
(if (featurep 'xemacs
) 'editing
'convenience
))
174 (defcustom whitespace-check-leading-whitespace t
175 "Flag to check leading whitespace. This is the global for the system.
176 It can be overriden by setting a buffer local variable
177 `whitespace-check-buffer-leading'."
181 (defcustom whitespace-check-trailing-whitespace t
182 "Flag to check trailing whitespace. This is the global for the system.
183 It can be overriden by setting a buffer local variable
184 `whitespace-check-buffer-trailing'."
188 (defcustom whitespace-check-spacetab-whitespace t
189 "Flag to check space followed by a TAB. This is the global for the system.
190 It can be overriden by setting a buffer local variable
191 `whitespace-check-buffer-spacetab'."
195 (defcustom whitespace-spacetab-regexp
"[ ]+\t"
196 "Regexp to match a space followed by a TAB."
200 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
201 "Flag to check indentation whitespace. This is the global for the system.
202 It can be overriden by setting a buffer local variable
203 `whitespace-check-buffer-indent'."
207 (defcustom whitespace-indent-regexp
(concat "^\\(\t*\\) " " ")
208 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
212 (defcustom whitespace-check-ateol-whitespace t
213 "Flag to check end-of-line whitespace. This is the global for the system.
214 It can be overriden by setting a buffer local variable
215 `whitespace-check-buffer-ateol'."
219 ;; (defcustom whitespace-ateol-regexp "[ \t]$"
220 (defcustom whitespace-ateol-regexp
"[ \t]+$"
221 "Regexp to match a TAB or a space at the EOL."
225 (defcustom whitespace-errbuf
"*Whitespace Errors*"
226 "The name of the buffer where whitespace related messages will be logged."
230 (defcustom whitespace-clean-msg
"clean."
231 "If non-nil, this message will be displayed after a whitespace check
232 determines a file to be clean."
236 (defcustom whitespace-abort-on-error nil
237 "While writing a file, abort if the file is unclean.
238 If `whitespace-auto-cleanup' is set, that takes precedence over
243 (defcustom whitespace-auto-cleanup nil
244 "Cleanup a buffer automatically on finding it whitespace unclean."
248 (defcustom whitespace-silent nil
249 "All whitespace errors will be shown only in the modeline when t.
251 Note that setting this may cause all whitespaces introduced in a file to go
252 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
253 Errors*' buffer before opening (or closing) another file."
257 (defcustom whitespace-modes
'(ada-mode asm-mode autoconf-mode awk-mode
258 c-mode c
++-mode cc-mode
259 change-log-mode cperl-mode
260 electric-nroff-mode emacs-lisp-mode
261 f90-mode fortran-mode html-mode
262 html3-mode java-mode jde-mode
263 ksh-mode latex-mode LaTeX-mode
264 lisp-mode m4-mode makefile-mode
265 modula-2-mode nroff-mode objc-mode
266 pascal-mode perl-mode prolog-mode
267 python-mode scheme-mode sgml-mode
268 sh-mode shell-script-mode simula-mode
269 tcl-mode tex-mode texinfo-mode
272 "Major modes in which we turn on whitespace checking.
274 These are mostly programming and documentation modes. But you may add other
275 modes that you want whitespaces checked in by adding something like the
276 following to your `.emacs':
278 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
281 Or, alternately, you can use the Emacs `customize' command to set this."
282 :type
'(repeat symbol
)
285 (defcustom whitespace-rescan-timer-time
600
286 "Period in seconds to rescan modified buffers for whitespace creep.
288 This is the period after which the timer will fire causing
289 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
292 To disable timer scans, set this to zero."
296 (defcustom whitespace-display-in-modeline t
297 "Display whitespace errors on the modeline."
301 (defcustom whitespace-display-spaces-in-color t
302 "Display the bogus whitespaces by coloring them with the face
303 `whitespace-highlight'."
307 (defgroup whitespace-faces nil
308 "Faces used in whitespace."
309 :prefix
"whitespace-"
313 (defface whitespace-highlight
'((((class color
) (background light
))
314 (:background
"green1"))
315 (((class color
) (background dark
))
316 (:background
"sea green"))
317 (((class grayscale mono
)
319 (:background
"black"))
320 (((class grayscale mono
)
322 (:background
"white")))
323 "Face used for highlighting the bogus whitespaces that exist in the buffer."
324 :group
'whitespace-faces
)
325 ;; backward-compatibility alias
326 (put 'whitespace-highlight-face
'face-alias
'whitespace-highlight
)
328 (if (not (assoc 'whitespace-mode minor-mode-alist
))
329 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
332 (set-default 'whitespace-check-buffer-leading
333 whitespace-check-leading-whitespace
)
334 (set-default 'whitespace-check-buffer-trailing
335 whitespace-check-trailing-whitespace
)
336 (set-default 'whitespace-check-buffer-indent
337 whitespace-check-indent-whitespace
)
338 (set-default 'whitespace-check-buffer-spacetab
339 whitespace-check-spacetab-whitespace
)
340 (set-default 'whitespace-check-buffer-ateol
341 whitespace-check-ateol-whitespace
)
343 (defun whitespace-check-whitespace-mode (&optional arg
)
344 "Test and set the whitespace-mode in qualifying buffers."
345 (if (null whitespace-mode
)
346 (setq whitespace-mode
347 (if (or arg
(member major-mode whitespace-modes
))
352 (defun whitespace-toggle-leading-check ()
353 "Toggle the check for leading space in the local buffer."
355 (let ((current-val whitespace-check-buffer-leading
))
356 (setq whitespace-check-buffer-leading
(not current-val
))
357 (message "Will%s check for leading space in buffer."
358 (if whitespace-check-buffer-leading
"" " not"))
359 (if whitespace-check-buffer-leading
(whitespace-buffer-leading))))
362 (defun whitespace-toggle-trailing-check ()
363 "Toggle the check for trailing space in the local buffer."
365 (let ((current-val whitespace-check-buffer-trailing
))
366 (setq whitespace-check-buffer-trailing
(not current-val
))
367 (message "Will%s check for trailing space in buffer."
368 (if whitespace-check-buffer-trailing
"" " not"))
369 (if whitespace-check-buffer-trailing
(whitespace-buffer-trailing))))
372 (defun whitespace-toggle-indent-check ()
373 "Toggle the check for indentation space in the local buffer."
375 (let ((current-val whitespace-check-buffer-indent
))
376 (setq whitespace-check-buffer-indent
(not current-val
))
377 (message "Will%s check for indentation space in buffer."
378 (if whitespace-check-buffer-indent
"" " not"))
379 (if whitespace-check-buffer-indent
380 (whitespace-buffer-search whitespace-indent-regexp
))))
383 (defun whitespace-toggle-spacetab-check ()
384 "Toggle the check for space-followed-by-TABs in the local buffer."
386 (let ((current-val whitespace-check-buffer-spacetab
))
387 (setq whitespace-check-buffer-spacetab
(not current-val
))
388 (message "Will%s check for space-followed-by-TABs in buffer."
389 (if whitespace-check-buffer-spacetab
"" " not"))
390 (if whitespace-check-buffer-spacetab
391 (whitespace-buffer-search whitespace-spacetab-regexp
))))
395 (defun whitespace-toggle-ateol-check ()
396 "Toggle the check for end-of-line space in the local buffer."
398 (let ((current-val whitespace-check-buffer-ateol
))
399 (setq whitespace-check-buffer-ateol
(not current-val
))
400 (message "Will%s check for end-of-line space in buffer."
401 (if whitespace-check-buffer-ateol
"" " not"))
402 (if whitespace-check-buffer-ateol
403 (whitespace-buffer-search whitespace-ateol-regexp
))))
407 (defun whitespace-buffer (&optional quiet
)
408 "Find five different types of white spaces in buffer.
410 1. Leading space \(empty lines at the top of a file\).
411 2. Trailing space \(empty lines at the end of a file\).
412 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
413 4. Spaces followed by a TAB. \(Almost always, we never want that\).
414 5. Spaces or TABS at the end of a line.
416 Check for whitespace only if this buffer really contains a non-empty file
418 1. the major mode is one of the whitespace-modes, or
419 2. `whitespace-buffer' was explicitly called with a prefix argument."
421 (let ((whitespace-error nil
))
422 (whitespace-check-whitespace-mode current-prefix-arg
)
423 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
425 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
426 (whitespace-tickle-timer)
427 (whitespace-unhighlight-the-space)
428 (if whitespace-auto-cleanup
431 (message "Can't cleanup: %s is read-only" (buffer-name)))
432 (whitespace-cleanup))
433 (let ((whitespace-leading (if whitespace-check-buffer-leading
434 (whitespace-buffer-leading)
436 (whitespace-trailing (if whitespace-check-buffer-trailing
437 (whitespace-buffer-trailing)
439 (whitespace-indent (if whitespace-check-buffer-indent
440 (whitespace-buffer-search
441 whitespace-indent-regexp
)
443 (whitespace-spacetab (if whitespace-check-buffer-spacetab
444 (whitespace-buffer-search
445 whitespace-spacetab-regexp
)
447 (whitespace-ateol (if whitespace-check-buffer-ateol
448 (whitespace-buffer-search
449 whitespace-ateol-regexp
)
451 (whitespace-errmsg nil
)
452 (whitespace-filename buffer-file-name
)
453 (whitespace-this-modeline ""))
455 ;; Now let's complain if we found any of the above.
456 (setq whitespace-error
(or whitespace-leading whitespace-indent
457 whitespace-spacetab whitespace-ateol
458 whitespace-trailing
))
462 (setq whitespace-errmsg
463 (concat whitespace-filename
" contains:\n"
464 (if whitespace-leading
465 "Leading whitespace\n")
466 (if whitespace-indent
467 (concat "Indentation whitespace"
468 whitespace-indent
"\n"))
469 (if whitespace-spacetab
470 (concat "Space followed by Tab"
471 whitespace-spacetab
"\n"))
473 (concat "End-of-line whitespace"
474 whitespace-ateol
"\n"))
475 (if whitespace-trailing
476 "Trailing whitespace\n")
477 "\ntype `M-x whitespace-cleanup' to "
478 "cleanup the file."))
479 (setq whitespace-this-modeline
480 (concat (if whitespace-ateol
"e")
481 (if whitespace-indent
"i")
482 (if whitespace-leading
"l")
483 (if whitespace-spacetab
"s")
484 (if whitespace-trailing
"t")))))
485 (whitespace-update-modeline whitespace-this-modeline
)
486 (if (get-buffer whitespace-errbuf
)
487 (kill-buffer whitespace-errbuf
))
488 (with-current-buffer (get-buffer-create whitespace-errbuf
)
489 (if whitespace-errmsg
491 (insert whitespace-errmsg
)
492 (if (not (or quiet whitespace-silent
))
493 (display-buffer (current-buffer) t
))
495 (message "Whitespaces: [%s%s] in %s"
496 whitespace-this-modeline
497 (let ((whitespace-unchecked
498 (whitespace-unchecked-whitespaces)))
499 (if whitespace-unchecked
500 (concat "!" whitespace-unchecked
)
502 whitespace-filename
)))
503 (if (and (not quiet
) (not (equal whitespace-clean-msg
"")))
504 (message "%s %s" whitespace-filename
505 whitespace-clean-msg
))))))))
509 (defun whitespace-region (s e
)
510 "Check the region for whitespace errors."
514 (narrow-to-region s e
)
515 (whitespace-buffer))))
518 (defun whitespace-cleanup ()
519 "Cleanup the five different kinds of whitespace problems.
521 Use \\[describe-function] whitespace-describe to read a summary of the
522 whitespace problems."
524 ;; If this buffer really contains a file, then run, else quit.
525 (whitespace-check-whitespace-mode current-prefix-arg
)
526 (if (and buffer-file-name whitespace-mode
)
527 (let ((whitespace-any nil
)
528 (whitespace-tabwith 8)
529 (whitespace-tabwith-saved tab-width
))
531 ;; since all printable TABS should be 8, irrespective of how
532 ;; they are displayed.
533 (setq tab-width whitespace-tabwith
)
535 (if (and whitespace-check-buffer-leading
536 (whitespace-buffer-leading))
538 (whitespace-buffer-leading-cleanup)
539 (setq whitespace-any t
)))
541 (if (and whitespace-check-buffer-trailing
542 (whitespace-buffer-trailing))
544 (whitespace-buffer-trailing-cleanup)
545 (setq whitespace-any t
)))
547 (if (and whitespace-check-buffer-indent
548 (whitespace-buffer-search whitespace-indent-regexp
))
550 (whitespace-indent-cleanup)
551 (setq whitespace-any t
)))
553 (if (and whitespace-check-buffer-spacetab
554 (whitespace-buffer-search whitespace-spacetab-regexp
))
556 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
557 (setq whitespace-any t
)))
559 (if (and whitespace-check-buffer-ateol
560 (whitespace-buffer-search whitespace-ateol-regexp
))
562 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
563 (setq whitespace-any t
)))
565 ;; Call this recursively till everything is taken care of
569 (if (not whitespace-silent
)
570 (message "%s clean" buffer-file-name
))
571 (whitespace-update-modeline)))
572 (setq tab-width whitespace-tabwith-saved
))))
575 (defun whitespace-cleanup-region (s e
)
576 "Whitespace cleanup on the region."
580 (narrow-to-region s e
)
581 (whitespace-cleanup))
582 (whitespace-buffer t
)))
584 (defun whitespace-buffer-leading ()
585 "Check to see if there are any empty lines at the top of the file."
589 (goto-char (point-min))
594 (if (equal pmin pmax
)
596 (whitespace-highlight-the-space pmin
(1+ pmax
))
600 (defun whitespace-buffer-leading-cleanup ()
601 "Remove any empty lines at the top of the file."
603 (goto-char (point-min))
604 (skip-chars-forward "\n")
605 (delete-region (point-min) (point))))
607 (defun whitespace-buffer-trailing ()
608 "Check to see if are is more than one empty line at the bottom."
612 (goto-char (point-max))
617 (if (equal pmin pmax
)
619 (goto-char (- (point) 1))
624 (if (equal pmin pmax
)
626 (whitespace-highlight-the-space (- pmin
1) pmax
)
631 (defun whitespace-buffer-trailing-cleanup ()
632 "Delete all the empty lines at the bottom."
634 (goto-char (point-max))
635 (skip-chars-backward "\n")
638 (delete-region (point) (point-max))))
640 (defun whitespace-buffer-search (regexp)
641 "Search for any given whitespace REGEXP."
642 (let ((whitespace-retval ""))
644 (goto-char (point-min))
645 (while (re-search-forward regexp nil t
)
647 (setq whitespace-retval
(format "%s %s" whitespace-retval
648 (match-beginning 0)))
649 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))))
650 (if (equal "" whitespace-retval
)
652 whitespace-retval
))))
654 (defun whitespace-buffer-cleanup (regexp newregexp
)
655 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
657 (goto-char (point-min))
658 (while (re-search-forward regexp nil t
)
659 (replace-match newregexp
))))
661 (defun whitespace-indent-cleanup ()
662 "Search for 8/more spaces at the start of a line and replace it with tabs."
664 (goto-char (point-min))
665 (while (re-search-forward whitespace-indent-regexp nil t
)
666 (let ((column (current-column))
667 (indent-tabs-mode t
))
668 (delete-region (match-beginning 0) (point))
669 (indent-to column
)))))
671 (defun whitespace-unchecked-whitespaces ()
672 "Return the list of whitespaces whose testing has been suppressed."
673 (let ((unchecked-spaces
674 (concat (if (not whitespace-check-buffer-ateol
) "e")
675 (if (not whitespace-check-buffer-indent
) "i")
676 (if (not whitespace-check-buffer-leading
) "l")
677 (if (not whitespace-check-buffer-spacetab
) "s")
678 (if (not whitespace-check-buffer-trailing
) "t"))))
679 (if (not (equal unchecked-spaces
""))
683 (defun whitespace-update-modeline (&optional whitespace-err
)
684 "Update modeline with whitespace errors.
685 Also with whitespaces whose testing has been turned off."
686 (if whitespace-display-in-modeline
688 (setq whitespace-mode-line nil
)
690 (if (and whitespace-err
(not (equal whitespace-err
"")))
691 (setq whitespace-mode-line whitespace-err
))
692 ;; Whitespace suppressed errors
693 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
694 (if whitespace-unchecked
695 (setq whitespace-mode-line
696 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
697 ;; Add the whitespace modeline prefix
698 (setq whitespace-mode-line
(if whitespace-mode-line
699 (concat " W:" whitespace-mode-line
)
701 (whitespace-mode-line-update))))
703 (defun whitespace-highlight-the-space (b e
)
704 "Highlight the current line, unhighlighting a previously jumped to line."
705 (if whitespace-display-spaces-in-color
706 (let ((ol (whitespace-make-overlay b e
)))
707 (push ol whitespace-highlighted-space
)
708 (whitespace-overlay-put ol
'face
'whitespace-highlight
))))
709 ;; (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space))
711 (defun whitespace-unhighlight-the-space()
712 "Unhighlight the currently highlight line."
713 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space
)
715 (mapc 'whitespace-delete-overlay whitespace-highlighted-space
)
716 (setq whitespace-highlighted-space nil
))
717 (remove-hook 'pre-command-hook
'whitespace-unhighlight-the-space
)))
719 (defun whitespace-check-buffer-list (buf-name buf-file
)
720 "Add a buffer and its file to the whitespace monitor list.
722 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
723 periodically for whitespace."
724 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
725 whitespace-all-buffer-files
)))
726 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
728 (defun whitespace-tickle-timer ()
729 "Tickle timer to periodically to scan qualifying files for whitespace creep.
731 If timer is not set, then set it to scan the files in
732 `whitespace-all-buffer-files' periodically (defined by
733 `whitespace-rescan-timer-time') for whitespace creep."
734 (if (and whitespace-rescan-timer-time
(not whitespace-rescan-timer
))
735 (setq whitespace-rescan-timer
736 (add-timeout whitespace-rescan-timer-time
737 'whitespace-rescan-files-in-buffers nil
738 whitespace-rescan-timer-time
))))
740 (defun whitespace-rescan-files-in-buffers (&optional arg
)
741 "Check monitored files for whitespace creep since last scan."
742 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
743 buffile bufname thiselt buf
)
744 (if (not whitespace-all-my-files
)
746 (disable-timeout whitespace-rescan-timer
)
747 (setq whitespace-rescan-timer nil
))
748 (while whitespace-all-my-files
749 (setq thiselt
(car whitespace-all-my-files
))
750 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
751 (setq buffile
(car thiselt
))
752 (setq bufname
(cadr thiselt
))
753 (setq buf
(get-buffer bufname
))
754 (if (buffer-live-p buf
)
756 ;;(message "buffer %s live" bufname)
760 ;;(message "checking for whitespace in %s" bufname)
761 (if whitespace-auto-cleanup
763 ;;(message "cleaning up whitespace in %s" bufname)
764 (whitespace-cleanup))
766 ;;(message "whitespace-buffer %s." (buffer-name))
767 (whitespace-buffer t
))))
768 ;;(message "Removing %s from refresh list" bufname)
769 (whitespace-refresh-rescan-list buffile bufname
)))
770 ;;(message "Removing %s from refresh list" bufname)
771 (whitespace-refresh-rescan-list buffile bufname
))))))
773 (defun whitespace-refresh-rescan-list (buffile bufname
)
774 "Refresh the list of files to be rescaned for whitespace creep."
775 (if whitespace-all-buffer-files
776 (setq whitespace-all-buffer-files
777 (delete (list buffile bufname
) whitespace-all-buffer-files
))
778 (when whitespace-rescan-timer
779 (disable-timeout whitespace-rescan-timer
)
780 (setq whitespace-rescan-timer nil
))))
783 (defalias 'global-whitespace-mode
'whitespace-global-mode
)
786 (define-minor-mode whitespace-global-mode
787 "Toggle using Whitespace mode in new buffers.
788 With ARG, turn the mode on iff ARG is positive.
790 When this mode is active, `whitespace-buffer' is added to
791 `find-file-hook' and `kill-buffer-hook'."
794 (if whitespace-global-mode
796 (add-hook 'find-file-hook
'whitespace-buffer
)
797 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
798 (add-hook 'kill-buffer-hook
'whitespace-buffer
))
799 (remove-hook 'find-file-hook
'whitespace-buffer
)
800 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
801 (remove-hook 'kill-buffer-hook
'whitespace-buffer
)))
804 (defun whitespace-write-file-hook ()
805 "Hook function to be called on the buffer when whitespace check is enabled.
806 This is meant to be added buffer-locally to `write-file-functions'."
809 (if whitespace-auto-cleanup
811 (setq werr
(whitespace-buffer)))
812 (if (and whitespace-abort-on-error werr
)
813 (error (concat "Abort write due to whitespaces in "
817 (defun whitespace-unload-hook ()
818 (remove-hook 'find-file-hook
'whitespace-buffer
)
819 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
820 (remove-hook 'kill-buffer-hook
'whitespace-buffer
))
822 (add-hook 'whitespace-unload-hook
'whitespace-unload-hook
)
824 (provide 'whitespace
)
826 ;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
827 ;;; whitespace.el ends here