1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 3, 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
)
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
)
110 (defvar whitespace-check-buffer-leading nil
111 "Test leading whitespace for file in current buffer if t.")
112 (make-variable-buffer-local 'whitespace-check-buffer-leading
)
113 ;;;###autoload(put 'whitespace-check-buffer-leading 'safe-local-variable 'booleanp)
115 (defvar whitespace-check-buffer-trailing nil
116 "Test trailing whitespace for file in current buffer if t.")
117 (make-variable-buffer-local 'whitespace-check-buffer-trailing
)
118 ;;;###autoload(put 'whitespace-check-buffer-trailing 'safe-local-variable 'booleanp)
120 (defvar whitespace-check-buffer-indent nil
121 "Test indentation whitespace for file in current buffer if t.")
122 (make-variable-buffer-local 'whitespace-check-buffer-indent
)
123 ;;;###autoload(put 'whitespace-check-buffer-indent 'safe-local-variable 'booleanp)
125 (defvar whitespace-check-buffer-spacetab nil
126 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
127 (make-variable-buffer-local 'whitespace-check-buffer-spacetab
)
128 ;;;###autoload(put 'whitespace-check-buffer-spacetab 'safe-local-variable 'booleanp)
130 (defvar whitespace-check-buffer-ateol nil
131 "Test end-of-line whitespace for file in current buffer if t.")
132 (make-variable-buffer-local 'whitespace-check-buffer-ateol
)
133 ;;;###autoload(put 'whitespace-check-buffer-ateol 'safe-local-variable 'booleanp)
135 (defvar whitespace-highlighted-space nil
136 "The variable to store the extent to highlight.")
137 (make-variable-buffer-local 'whitespace-highlighted-space
)
139 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
141 (if (not (fboundp 'defgroup
))
142 (defmacro defgroup
(sym memb doc
&rest args
)
143 "Null macro for `defgroup' in all versions of Emacs that don't define it."
145 (if (not (fboundp 'defcustom
))
146 (defmacro defcustom
(sym val doc
&rest args
)
147 "Macro to alias `defcustom' to `defvar' in all versions of Emacs that
149 `(defvar ,sym
,val
,doc
))))
151 (defalias 'whitespace-make-overlay
152 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
153 (defalias 'whitespace-overlay-put
154 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
155 (defalias 'whitespace-delete-overlay
156 (if (featurep 'xemacs
) 'delete-extent
'delete-overlay
))
157 (defalias 'whitespace-overlay-start
158 (if (featurep 'xemacs
) 'extent-start
'overlay-start
))
159 (defalias 'whitespace-overlay-end
160 (if (featurep 'xemacs
) 'extent-end
'overlay-end
))
161 (defalias 'whitespace-mode-line-update
162 (if (featurep 'xemacs
) 'redraw-modeline
'force-mode-line-update
))
164 (defgroup whitespace nil
165 "Check for and fix five different types of whitespaces in source code."
167 :link
'(emacs-commentary-link "whitespace.el")
168 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
169 ;; which is 'editing?
170 :group
(if (featurep 'xemacs
) 'editing
'convenience
))
172 (defcustom whitespace-check-leading-whitespace t
173 "Flag to check leading whitespace. This is the global for the system.
174 It can be overridden by setting a buffer local variable
175 `whitespace-check-buffer-leading'."
179 (defcustom whitespace-check-trailing-whitespace t
180 "Flag to check trailing whitespace. This is the global for the system.
181 It can be overridden by setting a buffer local variable
182 `whitespace-check-buffer-trailing'."
186 (defcustom whitespace-check-spacetab-whitespace t
187 "Flag to check space followed by a TAB. This is the global for the system.
188 It can be overridden by setting a buffer local variable
189 `whitespace-check-buffer-spacetab'."
193 (defcustom whitespace-spacetab-regexp
"[ ]+\t"
194 "Regexp to match one or more spaces followed by a TAB."
198 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
199 "Flag to check indentation whitespace. This is the global for the system.
200 It can be overridden by setting a buffer local variable
201 `whitespace-check-buffer-indent'."
205 (defcustom whitespace-indent-regexp
"^\t*\\( \\)+"
206 "Regexp to match multiples of eight spaces near line beginnings.
207 The default value ignores leading TABs."
211 (defcustom whitespace-check-ateol-whitespace t
212 "Flag to check end-of-line whitespace. This is the global for the system.
213 It can be overridden by setting a buffer local variable
214 `whitespace-check-buffer-ateol'."
218 (defcustom whitespace-ateol-regexp
"[ \t]+$"
219 "Regexp to match one or more TABs or spaces at line ends."
223 (defcustom whitespace-errbuf
"*Whitespace Errors*"
224 "The name of the buffer where whitespace related messages will be logged."
228 (defcustom whitespace-clean-msg
"clean."
229 "If non-nil, this message will be displayed after a whitespace check
230 determines a file to be clean."
234 (defcustom whitespace-abort-on-error nil
235 "While writing a file, abort if the file is unclean.
236 If `whitespace-auto-cleanup' is set, that takes precedence over
241 (defcustom whitespace-auto-cleanup nil
242 "Cleanup a buffer automatically on finding it whitespace unclean."
246 (defcustom whitespace-silent nil
247 "All whitespace errors will be shown only in the modeline when t.
249 Note that setting this may cause all whitespaces introduced in a file to go
250 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
251 Errors*' buffer before opening (or closing) another file."
255 (defcustom whitespace-modes
'(ada-mode asm-mode autoconf-mode awk-mode
256 c-mode c
++-mode cc-mode
257 change-log-mode cperl-mode
258 electric-nroff-mode emacs-lisp-mode
259 f90-mode fortran-mode html-mode
260 html3-mode java-mode jde-mode
261 ksh-mode latex-mode LaTeX-mode
262 lisp-mode m4-mode makefile-mode
263 modula-2-mode nroff-mode objc-mode
264 pascal-mode perl-mode prolog-mode
265 python-mode scheme-mode sgml-mode
266 sh-mode shell-script-mode simula-mode
267 tcl-mode tex-mode texinfo-mode
270 "Major modes in which we turn on whitespace checking.
272 These are mostly programming and documentation modes. But you may add other
273 modes that you want whitespaces checked in by adding something like the
274 following to your `.emacs':
276 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
279 Or, alternately, you can use the Emacs `customize' command to set this."
280 :type
'(repeat symbol
)
283 (defcustom whitespace-rescan-timer-time
600
284 "Period in seconds to rescan modified buffers for whitespace creep.
286 This is the period after which the timer will fire causing
287 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
290 To disable timer scans, set this to zero."
294 (defcustom whitespace-display-in-modeline t
295 "Display whitespace errors on the modeline."
299 (defcustom whitespace-display-spaces-in-color t
300 "Display the bogus whitespaces by coloring them with the face
301 `whitespace-highlight'."
305 (defgroup whitespace-faces nil
306 "Faces used in whitespace."
307 :prefix
"whitespace-"
311 (defface whitespace-highlight
'((((class color
) (background light
))
312 (:background
"green1"))
313 (((class color
) (background dark
))
314 (:background
"sea green"))
315 (((class grayscale mono
)
317 (:background
"black"))
318 (((class grayscale mono
)
320 (:background
"white")))
321 "Face used for highlighting the bogus whitespaces that exist in the buffer."
322 :group
'whitespace-faces
)
323 ;; backward-compatibility alias
324 (put 'whitespace-highlight-face
'face-alias
'whitespace-highlight
)
326 (if (not (assoc 'whitespace-mode minor-mode-alist
))
327 (setq minor-mode-alist
(cons '(whitespace-mode whitespace-mode-line
)
330 (set-default 'whitespace-check-buffer-leading
331 whitespace-check-leading-whitespace
)
332 (set-default 'whitespace-check-buffer-trailing
333 whitespace-check-trailing-whitespace
)
334 (set-default 'whitespace-check-buffer-indent
335 whitespace-check-indent-whitespace
)
336 (set-default 'whitespace-check-buffer-spacetab
337 whitespace-check-spacetab-whitespace
)
338 (set-default 'whitespace-check-buffer-ateol
339 whitespace-check-ateol-whitespace
)
341 (defun whitespace-check-whitespace-mode (&optional arg
)
342 "Test and set the whitespace-mode in qualifying buffers."
343 (if (null whitespace-mode
)
344 (setq whitespace-mode
345 (if (or arg
(member major-mode whitespace-modes
))
350 (defun whitespace-toggle-leading-check ()
351 "Toggle the check for leading space in the local buffer."
353 (let ((current-val whitespace-check-buffer-leading
))
354 (setq whitespace-check-buffer-leading
(not current-val
))
355 (message "Will%s check for leading space in buffer."
356 (if whitespace-check-buffer-leading
"" " not"))
357 (if whitespace-check-buffer-leading
(whitespace-buffer-leading))))
360 (defun whitespace-toggle-trailing-check ()
361 "Toggle the check for trailing space in the local buffer."
363 (let ((current-val whitespace-check-buffer-trailing
))
364 (setq whitespace-check-buffer-trailing
(not current-val
))
365 (message "Will%s check for trailing space in buffer."
366 (if whitespace-check-buffer-trailing
"" " not"))
367 (if whitespace-check-buffer-trailing
(whitespace-buffer-trailing))))
370 (defun whitespace-toggle-indent-check ()
371 "Toggle the check for indentation space in the local buffer."
373 (let ((current-val whitespace-check-buffer-indent
))
374 (setq whitespace-check-buffer-indent
(not current-val
))
375 (message "Will%s check for indentation space in buffer."
376 (if whitespace-check-buffer-indent
"" " not"))
377 (if whitespace-check-buffer-indent
378 (whitespace-buffer-search whitespace-indent-regexp
))))
381 (defun whitespace-toggle-spacetab-check ()
382 "Toggle the check for space-followed-by-TABs in the local buffer."
384 (let ((current-val whitespace-check-buffer-spacetab
))
385 (setq whitespace-check-buffer-spacetab
(not current-val
))
386 (message "Will%s check for space-followed-by-TABs in buffer."
387 (if whitespace-check-buffer-spacetab
"" " not"))
388 (if whitespace-check-buffer-spacetab
389 (whitespace-buffer-search whitespace-spacetab-regexp
))))
393 (defun whitespace-toggle-ateol-check ()
394 "Toggle the check for end-of-line space in the local buffer."
396 (let ((current-val whitespace-check-buffer-ateol
))
397 (setq whitespace-check-buffer-ateol
(not current-val
))
398 (message "Will%s check for end-of-line space in buffer."
399 (if whitespace-check-buffer-ateol
"" " not"))
400 (if whitespace-check-buffer-ateol
401 (whitespace-buffer-search whitespace-ateol-regexp
))))
405 (defun whitespace-buffer (&optional quiet
)
406 "Find five different types of white spaces in buffer.
408 1. Leading space \(empty lines at the top of a file\).
409 2. Trailing space \(empty lines at the end of a file\).
410 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
411 4. Spaces followed by a TAB. \(Almost always, we never want that\).
412 5. Spaces or TABS at the end of a line.
414 Check for whitespace only if this buffer really contains a non-empty file
416 1. the major mode is one of the whitespace-modes, or
417 2. `whitespace-buffer' was explicitly called with a prefix argument."
419 (let ((whitespace-error nil
))
420 (whitespace-check-whitespace-mode current-prefix-arg
)
421 (if (and buffer-file-name
(> (buffer-size) 0) whitespace-mode
)
423 (whitespace-check-buffer-list (buffer-name) buffer-file-name
)
424 (whitespace-tickle-timer)
425 (overlay-recenter (point-max))
426 (remove-overlays nil nil
'face
'whitespace-highlight
)
427 (if whitespace-auto-cleanup
430 (message "Can't cleanup: %s is read-only" (buffer-name)))
431 (whitespace-cleanup-internal))
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
)
485 (if (get-buffer whitespace-errbuf
)
486 (kill-buffer whitespace-errbuf
))
487 (with-current-buffer (get-buffer-create whitespace-errbuf
)
488 (if whitespace-errmsg
490 (insert whitespace-errmsg
)
491 (if (not (or quiet whitespace-silent
))
492 (display-buffer (current-buffer) t
))
494 (message "Whitespaces: [%s%s] in %s"
495 whitespace-this-modeline
496 (let ((whitespace-unchecked
497 (whitespace-unchecked-whitespaces)))
498 (if whitespace-unchecked
499 (concat "!" whitespace-unchecked
)
501 whitespace-filename
)))
502 (if (and (not quiet
) (not (equal whitespace-clean-msg
"")))
503 (message "%s %s" whitespace-filename
504 whitespace-clean-msg
))))))))
508 (defun whitespace-region (s e
)
509 "Check the region for whitespace errors."
513 (narrow-to-region s e
)
514 (whitespace-buffer))))
517 (defun whitespace-cleanup ()
518 "Cleanup the five different kinds of whitespace problems.
519 It normally applies to the whole buffer, but in Transient Mark mode
520 when the mark is active it applies to the region.
521 See `whitespace-buffer' docstring for a summary of the problems."
523 (if (and transient-mark-mode mark-active
)
524 (whitespace-cleanup-region (region-beginning) (region-end))
525 (whitespace-cleanup-internal)))
527 (defun whitespace-cleanup-internal (&optional region-only
)
528 ;; If this buffer really contains a file, then run, else quit.
529 (whitespace-check-whitespace-mode current-prefix-arg
)
530 (if (and buffer-file-name whitespace-mode
)
531 (let ((whitespace-any nil
)
532 (whitespace-tabwith 8)
533 (whitespace-tabwith-saved tab-width
))
535 ;; since all printable TABS should be 8, irrespective of how
536 ;; they are displayed.
537 (setq tab-width whitespace-tabwith
)
539 (if (and whitespace-check-buffer-leading
540 (whitespace-buffer-leading))
542 (whitespace-buffer-leading-cleanup)
543 (setq whitespace-any t
)))
545 (if (and whitespace-check-buffer-trailing
546 (whitespace-buffer-trailing))
548 (whitespace-buffer-trailing-cleanup)
549 (setq whitespace-any t
)))
551 (if (and whitespace-check-buffer-indent
552 (whitespace-buffer-search whitespace-indent-regexp
))
554 (whitespace-indent-cleanup)
555 (setq whitespace-any t
)))
557 (if (and whitespace-check-buffer-spacetab
558 (whitespace-buffer-search whitespace-spacetab-regexp
))
560 (whitespace-buffer-cleanup whitespace-spacetab-regexp
"\t")
561 (setq whitespace-any t
)))
563 (if (and whitespace-check-buffer-ateol
564 (whitespace-buffer-search whitespace-ateol-regexp
))
566 (whitespace-buffer-cleanup whitespace-ateol-regexp
"")
567 (setq whitespace-any t
)))
569 ;; Call this recursively till everything is taken care of
571 (whitespace-cleanup-internal region-only
)
572 ;; if we are done, talk to the user
574 (unless whitespace-silent
576 (message "The region is now clean")
577 (message "%s is now clean" buffer-file-name
)))
578 (whitespace-update-modeline)))
579 (setq tab-width whitespace-tabwith-saved
))))
582 (defun whitespace-cleanup-region (s e
)
583 "Whitespace cleanup on the region."
587 (narrow-to-region s e
)
588 (whitespace-cleanup-internal t
))
589 (whitespace-buffer t
)))
591 (defun whitespace-buffer-leading ()
592 "Return t if the current buffer has leading newline characters.
593 If highlighting is enabled, highlight these characters."
595 (goto-char (point-min))
596 (skip-chars-forward "\n")
598 (whitespace-highlight-the-space (point-min) (point))
601 (defun whitespace-buffer-leading-cleanup ()
602 "Remove any leading newline characters from current buffer."
604 (goto-char (point-min))
605 (skip-chars-forward "\n")
606 (delete-region (point-min) (point))))
608 (defun whitespace-buffer-trailing ()
609 "Return t if the current buffer has extra trailing newline characters.
610 If highlighting is enabled, highlight these characters."
612 (goto-char (point-max))
613 (skip-chars-backward "\n")
616 (whitespace-highlight-the-space (point) (point-max))
619 (defun whitespace-buffer-trailing-cleanup ()
620 "Remove extra trailing newline characters from current buffer."
622 (goto-char (point-max))
623 (skip-chars-backward "\n")
626 (delete-region (point) (point-max)))))
628 (defun whitespace-buffer-search (regexp)
629 "Search for any given whitespace REGEXP."
631 (let (whitespace-retval)
633 (goto-char (point-min))
634 (while (re-search-forward regexp nil t
)
635 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
636 (push (match-beginning 0) whitespace-retval
)))
637 (when whitespace-retval
638 (format " %s" (nreverse whitespace-retval
))))))
640 (defun whitespace-buffer-cleanup (regexp newregexp
)
641 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
643 (goto-char (point-min))
644 (while (re-search-forward regexp nil t
)
645 (replace-match newregexp
))))
647 (defun whitespace-indent-cleanup ()
648 "Search for 8/more spaces at the start of a line and replace it with tabs."
650 (goto-char (point-min))
651 (while (re-search-forward whitespace-indent-regexp nil t
)
652 (let ((column (current-column))
653 (indent-tabs-mode t
))
654 (delete-region (match-beginning 0) (point))
655 (indent-to column
)))))
657 (defun whitespace-unchecked-whitespaces ()
658 "Return the list of whitespaces whose testing has been suppressed."
659 (let ((unchecked-spaces
660 (concat (if (not whitespace-check-buffer-ateol
) "e")
661 (if (not whitespace-check-buffer-indent
) "i")
662 (if (not whitespace-check-buffer-leading
) "l")
663 (if (not whitespace-check-buffer-spacetab
) "s")
664 (if (not whitespace-check-buffer-trailing
) "t"))))
665 (if (not (equal unchecked-spaces
""))
669 (defun whitespace-update-modeline (&optional whitespace-err
)
670 "Update modeline with whitespace errors.
671 Also with whitespaces whose testing has been turned off."
672 (if whitespace-display-in-modeline
674 (setq whitespace-mode-line nil
)
676 (if (and whitespace-err
(not (equal whitespace-err
"")))
677 (setq whitespace-mode-line whitespace-err
))
678 ;; Whitespace suppressed errors
679 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
680 (if whitespace-unchecked
681 (setq whitespace-mode-line
682 (concat whitespace-mode-line
"!" whitespace-unchecked
))))
683 ;; Add the whitespace modeline prefix
684 (setq whitespace-mode-line
(if whitespace-mode-line
685 (concat " W:" whitespace-mode-line
)
687 (whitespace-mode-line-update))))
689 (defun whitespace-highlight-the-space (b e
)
690 "Highlight the current line, unhighlighting a previously jumped to line."
691 (if whitespace-display-spaces-in-color
692 (let ((ol (whitespace-make-overlay b e
)))
693 (whitespace-overlay-put ol
'face
'whitespace-highlight
))))
695 (defun whitespace-unhighlight-the-space()
696 "Unhighlight the currently highlight line."
697 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space
)
699 (mapc 'whitespace-delete-overlay whitespace-highlighted-space
)
700 (setq whitespace-highlighted-space nil
))))
702 (defun whitespace-check-buffer-list (buf-name buf-file
)
703 "Add a buffer and its file to the whitespace monitor list.
705 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
706 periodically for whitespace."
707 (if (and whitespace-mode
(not (member (list buf-file buf-name
)
708 whitespace-all-buffer-files
)))
709 (add-to-list 'whitespace-all-buffer-files
(list buf-file buf-name
))))
711 (defun whitespace-tickle-timer ()
712 "Tickle timer to periodically to scan qualifying files for whitespace creep.
714 If timer is not set, then set it to scan the files in
715 `whitespace-all-buffer-files' periodically (defined by
716 `whitespace-rescan-timer-time') for whitespace creep."
717 (if (and whitespace-rescan-timer-time
(not whitespace-rescan-timer
))
718 (setq whitespace-rescan-timer
719 (add-timeout whitespace-rescan-timer-time
720 'whitespace-rescan-files-in-buffers nil
721 whitespace-rescan-timer-time
))))
723 (defun whitespace-rescan-files-in-buffers (&optional arg
)
724 "Check monitored files for whitespace creep since last scan."
725 (let ((whitespace-all-my-files whitespace-all-buffer-files
)
726 buffile bufname thiselt buf
)
727 (if (not whitespace-all-my-files
)
729 (disable-timeout whitespace-rescan-timer
)
730 (setq whitespace-rescan-timer nil
))
731 (while whitespace-all-my-files
732 (setq thiselt
(car whitespace-all-my-files
))
733 (setq whitespace-all-my-files
(cdr whitespace-all-my-files
))
734 (setq buffile
(car thiselt
))
735 (setq bufname
(cadr thiselt
))
736 (setq buf
(get-buffer bufname
))
737 (if (buffer-live-p buf
)
739 ;;(message "buffer %s live" bufname)
743 ;;(message "checking for whitespace in %s" bufname)
744 (if whitespace-auto-cleanup
746 ;;(message "cleaning up whitespace in %s" bufname)
747 (whitespace-cleanup-internal))
749 ;;(message "whitespace-buffer %s." (buffer-name))
750 (whitespace-buffer t
))))
751 ;;(message "Removing %s from refresh list" bufname)
752 (whitespace-refresh-rescan-list buffile bufname
)))
753 ;;(message "Removing %s from refresh list" bufname)
754 (whitespace-refresh-rescan-list buffile bufname
))))))
756 (defun whitespace-refresh-rescan-list (buffile bufname
)
757 "Refresh the list of files to be rescanned for whitespace creep."
758 (if whitespace-all-buffer-files
759 (setq whitespace-all-buffer-files
760 (delete (list buffile bufname
) whitespace-all-buffer-files
))
761 (when whitespace-rescan-timer
762 (disable-timeout whitespace-rescan-timer
)
763 (setq whitespace-rescan-timer nil
))))
766 (defalias 'global-whitespace-mode
'whitespace-global-mode
)
769 (define-minor-mode whitespace-global-mode
770 "Toggle using Whitespace mode in new buffers.
771 With ARG, turn the mode on if ARG is positive, otherwise turn it off.
773 When this mode is active, `whitespace-buffer' is added to
774 `find-file-hook' and `kill-buffer-hook'."
777 (if whitespace-global-mode
779 (add-hook 'find-file-hook
'whitespace-buffer
)
780 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
781 (add-hook 'kill-buffer-hook
'whitespace-buffer
))
782 (remove-hook 'find-file-hook
'whitespace-buffer
)
783 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
784 (remove-hook 'kill-buffer-hook
'whitespace-buffer
)))
787 (defun whitespace-write-file-hook ()
788 "Hook function to be called on the buffer when whitespace check is enabled.
789 This is meant to be added buffer-locally to `write-file-functions'."
791 (if whitespace-auto-cleanup
792 (whitespace-cleanup-internal)
793 (setq werr
(whitespace-buffer)))
794 (if (and whitespace-abort-on-error werr
)
795 (error (concat "Abort write due to whitespaces in "
799 (defun whitespace-unload-function ()
800 "Unload the whitespace library."
801 (if (unintern "whitespace-unload-hook")
802 ;; if whitespace-unload-hook is defined, let's get rid of it
803 ;; and recursively call `unload-feature'
804 (progn (unload-feature 'whitespace
) t
)
805 ;; this only happens in the recursive call
806 (whitespace-global-mode -
1)
808 (dolist (buf (buffer-list))
810 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)))
811 ;; continue standard unloading
814 (defun whitespace-unload-hook ()
815 (remove-hook 'find-file-hook
'whitespace-buffer
)
816 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
817 (remove-hook 'kill-buffer-hook
'whitespace-buffer
))
819 (add-hook 'whitespace-unload-hook
'whitespace-unload-hook
)
821 (provide 'whitespace
)
823 ;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
824 ;;; whitespace.el ends here