1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
3 ;; Copyright (C) 1999-2011 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; This file has been obsolete since Emacs 23.1.
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
)
105 (defvar whitespace-mode-line nil
106 "String to display in the mode line for Whitespace mode.")
107 (make-variable-buffer-local 'whitespace-mode-line
)
109 (defvar whitespace-check-buffer-leading nil
110 "Test leading whitespace for file in current buffer if t.")
111 (make-variable-buffer-local 'whitespace-check-buffer-leading
)
112 ;;;###autoload(put 'whitespace-check-buffer-leading 'safe-local-variable 'booleanp)
114 (defvar whitespace-check-buffer-trailing nil
115 "Test trailing whitespace for file in current buffer if t.")
116 (make-variable-buffer-local 'whitespace-check-buffer-trailing
)
117 ;;;###autoload(put 'whitespace-check-buffer-trailing 'safe-local-variable 'booleanp)
119 (defvar whitespace-check-buffer-indent nil
120 "Test indentation whitespace for file in current buffer if t.")
121 (make-variable-buffer-local 'whitespace-check-buffer-indent
)
122 ;;;###autoload(put 'whitespace-check-buffer-indent 'safe-local-variable 'booleanp)
124 (defvar whitespace-check-buffer-spacetab nil
125 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
126 (make-variable-buffer-local 'whitespace-check-buffer-spacetab
)
127 ;;;###autoload(put 'whitespace-check-buffer-spacetab 'safe-local-variable 'booleanp)
129 (defvar whitespace-check-buffer-ateol nil
130 "Test end-of-line whitespace for file in current buffer if t.")
131 (make-variable-buffer-local 'whitespace-check-buffer-ateol
)
132 ;;;###autoload(put 'whitespace-check-buffer-ateol 'safe-local-variable 'booleanp)
134 (defvar whitespace-highlighted-space nil
135 "The variable to store the extent to highlight.")
136 (make-variable-buffer-local 'whitespace-highlighted-space
)
138 (defalias 'whitespace-make-overlay
139 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
140 (defalias 'whitespace-overlay-put
141 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
142 (defalias 'whitespace-delete-overlay
143 (if (featurep 'xemacs
) 'delete-extent
'delete-overlay
))
144 (defalias 'whitespace-overlay-start
145 (if (featurep 'xemacs
) 'extent-start
'overlay-start
))
146 (defalias 'whitespace-overlay-end
147 (if (featurep 'xemacs
) 'extent-end
'overlay-end
))
148 (defalias 'whitespace-mode-line-update
149 (if (featurep 'xemacs
) 'redraw-modeline
'force-mode-line-update
))
151 (defgroup whitespace nil
152 "Check for and fix five different types of whitespaces in source code."
154 :link
'(emacs-commentary-link "whitespace.el")
155 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
156 ;; which is 'editing?
157 :group
(if (featurep 'xemacs
) 'editing
'convenience
))
159 (defcustom whitespace-check-leading-whitespace t
160 "Flag to check leading whitespace. This is the global for the system.
161 It can be overridden by setting a buffer local variable
162 `whitespace-check-buffer-leading'."
166 (defcustom whitespace-check-trailing-whitespace t
167 "Flag to check trailing whitespace. This is the global for the system.
168 It can be overridden by setting a buffer local variable
169 `whitespace-check-buffer-trailing'."
173 (defcustom whitespace-check-spacetab-whitespace t
174 "Flag to check space followed by a TAB. This is the global for the system.
175 It can be overridden by setting a buffer local variable
176 `whitespace-check-buffer-spacetab'."
180 (defcustom whitespace-spacetab-regexp
"[ ]+\t"
181 "Regexp to match one or more spaces followed by a TAB."
185 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
186 "Flag to check indentation whitespace. This is the global for the system.
187 It can be overridden by setting a buffer local variable
188 `whitespace-check-buffer-indent'."
192 (defcustom whitespace-indent-regexp
"^\t*\\( \\)+"
193 "Regexp to match multiples of eight spaces near line beginnings.
194 The default value ignores leading TABs."
198 (defcustom whitespace-check-ateol-whitespace t
199 "Flag to check end-of-line whitespace. This is the global for the system.
200 It can be overridden by setting a buffer local variable
201 `whitespace-check-buffer-ateol'."
205 (defcustom whitespace-ateol-regexp
"[ \t]+$"
206 "Regexp to match one or more TABs or spaces at line ends."
210 (defcustom whitespace-errbuf
"*Whitespace Errors*"
211 "The name of the buffer where whitespace related messages will be logged."
215 (defcustom whitespace-clean-msg
"clean."
216 "If non-nil, this message will be displayed after a whitespace check
217 determines a file to be clean."
221 (defcustom whitespace-abort-on-error nil
222 "While writing a file, abort if the file is unclean.
223 If `whitespace-auto-cleanup' is set, that takes precedence over
228 (defcustom whitespace-auto-cleanup nil
229 "Cleanup a buffer automatically on finding it whitespace unclean."
233 (defcustom whitespace-silent nil
234 "All whitespace errors will be shown only in the modeline when t.
236 Note that setting this may cause all whitespaces introduced in a file to go
237 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
238 Errors*' buffer before opening (or closing) another file."
242 (defcustom whitespace-modes
'(ada-mode asm-mode autoconf-mode awk-mode
243 c-mode c
++-mode cc-mode
244 change-log-mode cperl-mode
245 electric-nroff-mode emacs-lisp-mode
246 f90-mode fortran-mode html-mode
247 html3-mode java-mode jde-mode
248 ksh-mode latex-mode LaTeX-mode
249 lisp-mode m4-mode makefile-mode
250 modula-2-mode nroff-mode objc-mode
251 pascal-mode perl-mode prolog-mode
252 python-mode scheme-mode sgml-mode
253 sh-mode shell-script-mode simula-mode
254 tcl-mode tex-mode texinfo-mode
257 "Major modes in which we turn on whitespace checking.
259 These are mostly programming and documentation modes. But you may add other
260 modes that you want whitespaces checked in by adding something like the
261 following to your `.emacs':
263 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
266 Or, alternately, you can use the Emacs `customize' command to set this."
267 :type
'(repeat symbol
)
270 (defcustom whitespace-rescan-timer-time
600
271 "Period in seconds to rescan modified buffers for whitespace creep.
273 This is the period after which the timer will fire causing
274 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
277 To disable timer scans, set this to zero."
281 (defcustom whitespace-display-in-modeline t
282 "Display whitespace errors on the modeline."
286 (defcustom whitespace-display-spaces-in-color t
287 "Display the bogus whitespaces by coloring them with the face
288 `whitespace-highlight'."
292 (defgroup whitespace-faces nil
293 "Faces used in whitespace."
294 :prefix
"whitespace-"
298 (defface whitespace-highlight
'((((class color
) (background light
))
299 (:background
"green1"))
300 (((class color
) (background dark
))
301 (:background
"sea green"))
302 (((class grayscale mono
)
304 (:background
"black"))
305 (((class grayscale mono
)
307 (:background
"white")))
308 "Face used for highlighting the bogus whitespaces that exist in the buffer."
309 :group
'whitespace-faces
)
310 (define-obsolete-face-alias 'whitespace-highlight-face
311 'whitespace-highlight
"22.1")
313 (if (not (assoc 'whitespace-mode minor-mode-alist
))
314 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
317 (set-default 'whitespace-check-buffer-leading
318 whitespace-check-leading-whitespace
)
319 (set-default 'whitespace-check-buffer-trailing
320 whitespace-check-trailing-whitespace
)
321 (set-default 'whitespace-check-buffer-indent
322 whitespace-check-indent-whitespace
)
323 (set-default 'whitespace-check-buffer-spacetab
324 whitespace-check-spacetab-whitespace
)
325 (set-default 'whitespace-check-buffer-ateol
326 whitespace-check-ateol-whitespace
)
328 (defun whitespace-check-whitespace-mode (&optional arg
)
329 "Test and set the whitespace-mode in qualifying buffers."
330 (if (null whitespace-mode
)
331 (setq whitespace-mode
332 (if (or arg
(member major-mode whitespace-modes
))
337 (defun whitespace-toggle-leading-check ()
338 "Toggle the check for leading space in the local buffer."
340 (let ((current-val whitespace-check-buffer-leading
))
341 (setq whitespace-check-buffer-leading
(not current-val
))
342 (message "Will%s check for leading space in buffer."
343 (if whitespace-check-buffer-leading
"" " not"))
344 (if whitespace-check-buffer-leading
(whitespace-buffer-leading))))
347 (defun whitespace-toggle-trailing-check ()
348 "Toggle the check for trailing space in the local buffer."
350 (let ((current-val whitespace-check-buffer-trailing
))
351 (setq whitespace-check-buffer-trailing
(not current-val
))
352 (message "Will%s check for trailing space in buffer."
353 (if whitespace-check-buffer-trailing
"" " not"))
354 (if whitespace-check-buffer-trailing
(whitespace-buffer-trailing))))
357 (defun whitespace-toggle-indent-check ()
358 "Toggle the check for indentation space in the local buffer."
360 (let ((current-val whitespace-check-buffer-indent
))
361 (setq whitespace-check-buffer-indent
(not current-val
))
362 (message "Will%s check for indentation space in buffer."
363 (if whitespace-check-buffer-indent
"" " not"))
364 (if whitespace-check-buffer-indent
365 (whitespace-buffer-search whitespace-indent-regexp
))))
368 (defun whitespace-toggle-spacetab-check ()
369 "Toggle the check for space-followed-by-TABs in the local buffer."
371 (let ((current-val whitespace-check-buffer-spacetab
))
372 (setq whitespace-check-buffer-spacetab
(not current-val
))
373 (message "Will%s check for space-followed-by-TABs in buffer."
374 (if whitespace-check-buffer-spacetab
"" " not"))
375 (if whitespace-check-buffer-spacetab
376 (whitespace-buffer-search whitespace-spacetab-regexp
))))
380 (defun whitespace-toggle-ateol-check ()
381 "Toggle the check for end-of-line space in the local buffer."
383 (let ((current-val whitespace-check-buffer-ateol
))
384 (setq whitespace-check-buffer-ateol
(not current-val
))
385 (message "Will%s check for end-of-line space in buffer."
386 (if whitespace-check-buffer-ateol
"" " not"))
387 (if whitespace-check-buffer-ateol
388 (whitespace-buffer-search whitespace-ateol-regexp
))))
392 (defun whitespace-buffer (&optional quiet
)
393 "Find five different types of white spaces in buffer.
395 1. Leading space \(empty lines at the top of a file\).
396 2. Trailing space \(empty lines at the end of a file\).
397 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
398 4. Spaces followed by a TAB. \(Almost always, we never want that\).
399 5. Spaces or TABS at the end of a line.
401 Check for whitespace only if this buffer really contains a non-empty file
403 1. the major mode is one of the whitespace-modes, or
404 2. `whitespace-buffer' was explicitly called with a prefix argument."
406 (let ((whitespace-error nil
))
407 (whitespace-check-whitespace-mode current-prefix-arg
)
408 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
410 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
411 (whitespace-tickle-timer)
412 (overlay-recenter (point-max))
413 (remove-overlays nil nil
'face
'whitespace-highlight
)
414 (if whitespace-auto-cleanup
417 (message "Can't cleanup: %s is read-only" (buffer-name)))
418 (whitespace-cleanup-internal))
419 (let ((whitespace-leading (if whitespace-check-buffer-leading
420 (whitespace-buffer-leading)
422 (whitespace-trailing (if whitespace-check-buffer-trailing
423 (whitespace-buffer-trailing)
425 (whitespace-indent (if whitespace-check-buffer-indent
426 (whitespace-buffer-search
427 whitespace-indent-regexp
)
429 (whitespace-spacetab (if whitespace-check-buffer-spacetab
430 (whitespace-buffer-search
431 whitespace-spacetab-regexp
)
433 (whitespace-ateol (if whitespace-check-buffer-ateol
434 (whitespace-buffer-search
435 whitespace-ateol-regexp
)
437 (whitespace-errmsg nil
)
438 (whitespace-filename buffer-file-name
)
439 (whitespace-this-modeline ""))
441 ;; Now let's complain if we found any of the above.
442 (setq whitespace-error
(or whitespace-leading whitespace-indent
443 whitespace-spacetab whitespace-ateol
444 whitespace-trailing
))
448 (setq whitespace-errmsg
449 (concat whitespace-filename
" contains:\n"
450 (if whitespace-leading
451 "Leading whitespace\n")
452 (if whitespace-indent
453 (concat "Indentation whitespace"
454 whitespace-indent
"\n"))
455 (if whitespace-spacetab
456 (concat "Space followed by Tab"
457 whitespace-spacetab
"\n"))
459 (concat "End-of-line whitespace"
460 whitespace-ateol
"\n"))
461 (if whitespace-trailing
462 "Trailing whitespace\n")
463 "\ntype `M-x whitespace-cleanup' to "
464 "cleanup the file."))
465 (setq whitespace-this-modeline
466 (concat (if whitespace-ateol
"e")
467 (if whitespace-indent
"i")
468 (if whitespace-leading
"l")
469 (if whitespace-spacetab
"s")
470 (if whitespace-trailing
"t")))))
471 (whitespace-update-modeline whitespace-this-modeline
)
472 (if (get-buffer whitespace-errbuf
)
473 (kill-buffer whitespace-errbuf
))
474 (with-current-buffer (get-buffer-create whitespace-errbuf
)
475 (if whitespace-errmsg
477 (insert whitespace-errmsg
)
478 (if (not (or quiet whitespace-silent
))
479 (display-buffer (current-buffer) t
))
481 (message "Whitespaces: [%s%s] in %s"
482 whitespace-this-modeline
483 (let ((whitespace-unchecked
484 (whitespace-unchecked-whitespaces)))
485 (if whitespace-unchecked
486 (concat "!" whitespace-unchecked
)
488 whitespace-filename
)))
489 (if (and (not quiet
) (not (equal whitespace-clean-msg
"")))
490 (message "%s %s" whitespace-filename
491 whitespace-clean-msg
))))))))
495 (defun whitespace-region (s e
)
496 "Check the region for whitespace errors."
500 (narrow-to-region s e
)
501 (whitespace-buffer))))
504 (defun whitespace-cleanup ()
505 "Cleanup the five different kinds of whitespace problems.
506 It normally applies to the whole buffer, but in Transient Mark mode
507 when the mark is active it applies to the region.
508 See `whitespace-buffer' docstring for a summary of the problems."
510 (if (and transient-mark-mode mark-active
)
511 (whitespace-cleanup-region (region-beginning) (region-end))
512 (whitespace-cleanup-internal)))
514 (defun whitespace-cleanup-internal (&optional region-only
)
515 ;; If this buffer really contains a file, then run, else quit.
516 (whitespace-check-whitespace-mode current-prefix-arg
)
517 (if (and buffer-file-name whitespace-mode
)
518 (let ((whitespace-any nil
)
519 (whitespace-tabwith 8)
520 (whitespace-tabwith-saved tab-width
))
522 ;; since all printable TABS should be 8, irrespective of how
523 ;; they are displayed.
524 (setq tab-width whitespace-tabwith
)
526 (if (and whitespace-check-buffer-leading
527 (whitespace-buffer-leading))
529 (whitespace-buffer-leading-cleanup)
530 (setq whitespace-any t
)))
532 (if (and whitespace-check-buffer-trailing
533 (whitespace-buffer-trailing))
535 (whitespace-buffer-trailing-cleanup)
536 (setq whitespace-any t
)))
538 (if (and whitespace-check-buffer-indent
539 (whitespace-buffer-search whitespace-indent-regexp
))
541 (whitespace-indent-cleanup)
542 (setq whitespace-any t
)))
544 (if (and whitespace-check-buffer-spacetab
545 (whitespace-buffer-search whitespace-spacetab-regexp
))
547 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
548 (setq whitespace-any t
)))
550 (if (and whitespace-check-buffer-ateol
551 (whitespace-buffer-search whitespace-ateol-regexp
))
553 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
554 (setq whitespace-any t
)))
556 ;; Call this recursively till everything is taken care of
558 (whitespace-cleanup-internal region-only
)
559 ;; if we are done, talk to the user
561 (unless whitespace-silent
563 (message "The region is now clean")
564 (message "%s is now clean" buffer-file-name
)))
565 (whitespace-update-modeline)))
566 (setq tab-width whitespace-tabwith-saved
))))
569 (defun whitespace-cleanup-region (s e
)
570 "Whitespace cleanup on the region."
574 (narrow-to-region s e
)
575 (whitespace-cleanup-internal t
))
576 (whitespace-buffer t
)))
578 (defun whitespace-buffer-leading ()
579 "Return t if the current buffer has leading newline characters.
580 If highlighting is enabled, highlight these characters."
582 (goto-char (point-min))
583 (skip-chars-forward "\n")
585 (whitespace-highlight-the-space (point-min) (point))
588 (defun whitespace-buffer-leading-cleanup ()
589 "Remove any leading newline characters from current buffer."
591 (goto-char (point-min))
592 (skip-chars-forward "\n")
593 (delete-region (point-min) (point))))
595 (defun whitespace-buffer-trailing ()
596 "Return t if the current buffer has extra trailing newline characters.
597 If highlighting is enabled, highlight these characters."
599 (goto-char (point-max))
600 (skip-chars-backward "\n")
603 (whitespace-highlight-the-space (point) (point-max))
606 (defun whitespace-buffer-trailing-cleanup ()
607 "Remove extra trailing newline characters from current buffer."
609 (goto-char (point-max))
610 (skip-chars-backward "\n")
613 (delete-region (point) (point-max)))))
615 (defun whitespace-buffer-search (regexp)
616 "Search for any given whitespace REGEXP."
618 (let (whitespace-retval)
620 (goto-char (point-min))
621 (while (re-search-forward regexp nil t
)
622 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
623 (push (match-beginning 0) whitespace-retval
)))
624 (when whitespace-retval
625 (format " %s" (nreverse whitespace-retval
))))))
627 (defun whitespace-buffer-cleanup (regexp newregexp
)
628 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
630 (goto-char (point-min))
631 (while (re-search-forward regexp nil t
)
632 (replace-match newregexp
))))
634 (defun whitespace-indent-cleanup ()
635 "Search for 8/more spaces at the start of a line and replace it with tabs."
637 (goto-char (point-min))
638 (while (re-search-forward whitespace-indent-regexp nil t
)
639 (let ((column (current-column))
640 (indent-tabs-mode t
))
641 (delete-region (match-beginning 0) (point))
642 (indent-to column
)))))
644 (defun whitespace-unchecked-whitespaces ()
645 "Return the list of whitespaces whose testing has been suppressed."
646 (let ((unchecked-spaces
647 (concat (if (not whitespace-check-buffer-ateol
) "e")
648 (if (not whitespace-check-buffer-indent
) "i")
649 (if (not whitespace-check-buffer-leading
) "l")
650 (if (not whitespace-check-buffer-spacetab
) "s")
651 (if (not whitespace-check-buffer-trailing
) "t"))))
652 (if (not (equal unchecked-spaces
""))
656 (defun whitespace-update-modeline (&optional whitespace-err
)
657 "Update modeline with whitespace errors.
658 Also with whitespaces whose testing has been turned off."
659 (if whitespace-display-in-modeline
661 (setq whitespace-mode-line nil
)
663 (if (and whitespace-err
(not (equal whitespace-err
"")))
664 (setq whitespace-mode-line whitespace-err
))
665 ;; Whitespace suppressed errors
666 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
667 (if whitespace-unchecked
668 (setq whitespace-mode-line
669 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
670 ;; Add the whitespace modeline prefix
671 (setq whitespace-mode-line
(if whitespace-mode-line
672 (concat " W:" whitespace-mode-line
)
674 (whitespace-mode-line-update))))
676 (defun whitespace-highlight-the-space (b e
)
677 "Highlight the current line, unhighlighting a previously jumped to line."
678 (if whitespace-display-spaces-in-color
679 (let ((ol (whitespace-make-overlay b e
)))
680 (whitespace-overlay-put ol
'face
'whitespace-highlight
))))
682 (defun whitespace-unhighlight-the-space()
683 "Unhighlight the currently highlight line."
684 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space
)
686 (mapc 'whitespace-delete-overlay whitespace-highlighted-space
)
687 (setq whitespace-highlighted-space nil
))))
689 (defun whitespace-check-buffer-list (buf-name buf-file
)
690 "Add a buffer and its file to the whitespace monitor list.
692 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
693 periodically for whitespace."
694 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
695 whitespace-all-buffer-files
)))
696 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
698 (defun whitespace-tickle-timer ()
699 "Tickle timer to periodically to scan qualifying files for whitespace creep.
701 If timer is not set, then set it to scan the files in
702 `whitespace-all-buffer-files' periodically (defined by
703 `whitespace-rescan-timer-time') for whitespace creep."
704 (if (and whitespace-rescan-timer-time
705 (/= whitespace-rescan-timer-time
0)
706 (not whitespace-rescan-timer
))
707 (setq whitespace-rescan-timer
708 (add-timeout whitespace-rescan-timer-time
709 'whitespace-rescan-files-in-buffers nil
710 whitespace-rescan-timer-time
))))
712 (defun whitespace-rescan-files-in-buffers (&optional arg
)
713 "Check monitored files for whitespace creep since last scan."
714 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
715 buffile bufname thiselt buf
)
716 (if (not whitespace-all-my-files
)
718 (disable-timeout whitespace-rescan-timer
)
719 (setq whitespace-rescan-timer nil
))
720 (while whitespace-all-my-files
721 (setq thiselt
(car whitespace-all-my-files
))
722 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
723 (setq buffile
(car thiselt
))
724 (setq bufname
(cadr thiselt
))
725 (setq buf
(get-buffer bufname
))
726 (if (buffer-live-p buf
)
727 (with-current-buffer bufname
728 ;;(message "buffer %s live" bufname)
731 ;;(message "checking for whitespace in %s" bufname)
732 (if whitespace-auto-cleanup
734 ;;(message "cleaning up whitespace in %s" bufname)
735 (whitespace-cleanup-internal))
737 ;;(message "whitespace-buffer %s." (buffer-name))
738 (whitespace-buffer t
))))
739 ;;(message "Removing %s from refresh list" bufname)
740 (whitespace-refresh-rescan-list buffile bufname
)))
741 ;;(message "Removing %s from refresh list" bufname)
742 (whitespace-refresh-rescan-list buffile bufname
))))))
744 (defun whitespace-refresh-rescan-list (buffile bufname
)
745 "Refresh the list of files to be rescanned for whitespace creep."
746 (if whitespace-all-buffer-files
747 (setq whitespace-all-buffer-files
748 (delete (list buffile bufname
) whitespace-all-buffer-files
))
749 (when whitespace-rescan-timer
750 (disable-timeout whitespace-rescan-timer
)
751 (setq whitespace-rescan-timer nil
))))
754 (defalias 'global-whitespace-mode
'whitespace-global-mode
)
757 (define-minor-mode whitespace-global-mode
758 "Toggle using Whitespace mode in new buffers.
759 With ARG, turn the mode on if ARG is positive, otherwise turn it off.
761 When this mode is active, `whitespace-buffer' is added to
762 `find-file-hook' and `kill-buffer-hook'."
765 (if whitespace-global-mode
767 (add-hook 'find-file-hook
'whitespace-buffer
)
768 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
769 (add-hook 'kill-buffer-hook
'whitespace-buffer
))
770 (remove-hook 'find-file-hook
'whitespace-buffer
)
771 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
772 (remove-hook 'kill-buffer-hook
'whitespace-buffer
)))
775 (defun whitespace-write-file-hook ()
776 "Hook function to be called on the buffer when whitespace check is enabled.
777 This is meant to be added buffer-locally to `write-file-functions'."
779 (if whitespace-auto-cleanup
780 (whitespace-cleanup-internal)
781 (setq werr
(whitespace-buffer)))
782 (if (and whitespace-abort-on-error werr
)
783 (error "Abort write due to whitespaces in %s"
787 (defun whitespace-unload-function ()
788 "Unload the whitespace library."
789 (if (unintern "whitespace-unload-hook" obarray
)
790 ;; if whitespace-unload-hook is defined, let's get rid of it
791 ;; and recursively call `unload-feature'
792 (progn (unload-feature 'whitespace
) t
)
793 ;; this only happens in the recursive call
794 (whitespace-global-mode -
1)
796 (dolist (buf (buffer-list))
798 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)))
799 ;; continue standard unloading
802 (defun whitespace-unload-hook ()
803 (remove-hook 'find-file-hook
'whitespace-buffer
)
804 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
805 (remove-hook 'kill-buffer-hook
'whitespace-buffer
))
807 (add-hook 'whitespace-unload-hook
'whitespace-unload-hook
)
809 (provide 'whitespace
)
811 ;;; whitespace.el ends here