(thumbs-call-convert): Use call-process directly
[emacs.git] / lisp / whitespace.el
blobe36a0f848180de96e03dfed486e4ad591d1ea1bf
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 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)
14 ;; any later version.
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.
26 ;;; Commentary:
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
52 ;; !<y>.
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
61 ;; even print it.
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
70 ;; to set smarttab.)
72 ;; All the above have caused (and will cause) unwanted codeline integration and
73 ;; merge problems.
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
84 ;; problems.
85 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
86 ;; and mark in the current buffer.
88 ;;; Code:
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)
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)
118 (defvar whitespace-check-buffer-indent nil
119 "Test indentation whitespace for file in current buffer if t.")
120 (make-variable-buffer-local 'whitespace-check-buffer-indent)
122 (defvar whitespace-check-buffer-spacetab nil
123 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
124 (make-variable-buffer-local 'whitespace-check-buffer-spacetab)
126 (defvar whitespace-check-buffer-ateol nil
127 "Test end-of-line whitespace for file in current buffer if t.")
128 (make-variable-buffer-local 'whitespace-check-buffer-ateol)
130 (defvar whitespace-highlighted-space nil
131 "The variable to store the extent to highlight.")
132 (make-variable-buffer-local 'whitespace-highlighted-space)
134 (defalias 'whitespace-make-overlay
135 (if (featurep 'xemacs) 'make-extent 'make-overlay))
136 (defalias 'whitespace-overlay-put
137 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
138 (defalias 'whitespace-delete-overlay
139 (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
140 (defalias 'whitespace-overlay-start
141 (if (featurep 'xemacs) 'extent-start 'overlay-start))
142 (defalias 'whitespace-overlay-end
143 (if (featurep 'xemacs) 'extent-end 'overlay-end))
144 (defalias 'whitespace-mode-line-update
145 (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
147 (defgroup whitespace nil
148 "Check for and fix five different types of whitespaces in source code."
149 :version "21.1"
150 :link '(emacs-commentary-link "whitespace.el")
151 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
152 ;; which is 'editing?
153 :group (if (featurep 'xemacs) 'editing 'convenience))
155 (defcustom whitespace-check-leading-whitespace t
156 "Flag to check leading whitespace. This is the global for the system.
157 It can be overriden by setting a buffer local variable
158 `whitespace-check-buffer-leading'."
159 :type 'boolean
160 :group 'whitespace)
162 (defcustom whitespace-check-trailing-whitespace t
163 "Flag to check trailing whitespace. This is the global for the system.
164 It can be overriden by setting a buffer local variable
165 `whitespace-check-buffer-trailing'."
166 :type 'boolean
167 :group 'whitespace)
169 (defcustom whitespace-check-spacetab-whitespace t
170 "Flag to check space followed by a TAB. This is the global for the system.
171 It can be overriden by setting a buffer local variable
172 `whitespace-check-buffer-spacetab'."
173 :type 'boolean
174 :group 'whitespace)
176 (defcustom whitespace-spacetab-regexp "[ ]+\t"
177 "Regexp to match one or more spaces followed by a TAB."
178 :type 'regexp
179 :group 'whitespace)
181 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
182 "Flag to check indentation whitespace. This is the global for the system.
183 It can be overriden by setting a buffer local variable
184 `whitespace-check-buffer-indent'."
185 :type 'boolean
186 :group 'whitespace)
188 (defcustom whitespace-indent-regexp "^\t*\\( \\)+"
189 "Regexp to match multiples of eight spaces near line beginnings.
190 The default value ignores leading TABs."
191 :type 'regexp
192 :group 'whitespace)
194 (defcustom whitespace-check-ateol-whitespace t
195 "Flag to check end-of-line whitespace. This is the global for the system.
196 It can be overriden by setting a buffer local variable
197 `whitespace-check-buffer-ateol'."
198 :type 'boolean
199 :group 'whitespace)
201 (defcustom whitespace-ateol-regexp "[ \t]+$"
202 "Regexp to match one or more TABs or spaces at line ends."
203 :type 'regexp
204 :group 'whitespace)
206 (defcustom whitespace-errbuf "*Whitespace Errors*"
207 "The name of the buffer where whitespace related messages will be logged."
208 :type 'string
209 :group 'whitespace)
211 (defcustom whitespace-clean-msg "clean."
212 "If non-nil, this message will be displayed after a whitespace check
213 determines a file to be clean."
214 :type 'string
215 :group 'whitespace)
217 (defcustom whitespace-abort-on-error nil
218 "While writing a file, abort if the file is unclean.
219 If `whitespace-auto-cleanup' is set, that takes precedence over
220 this variable."
221 :type 'boolean
222 :group 'whitespace)
224 (defcustom whitespace-auto-cleanup nil
225 "Cleanup a buffer automatically on finding it whitespace unclean."
226 :type 'boolean
227 :group 'whitespace)
229 (defcustom whitespace-silent nil
230 "All whitespace errors will be shown only in the modeline when t.
232 Note that setting this may cause all whitespaces introduced in a file to go
233 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
234 Errors*' buffer before opening (or closing) another file."
235 :type 'boolean
236 :group 'whitespace)
238 (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
239 c-mode c++-mode cc-mode
240 change-log-mode cperl-mode
241 electric-nroff-mode emacs-lisp-mode
242 f90-mode fortran-mode html-mode
243 html3-mode java-mode jde-mode
244 ksh-mode latex-mode LaTeX-mode
245 lisp-mode m4-mode makefile-mode
246 modula-2-mode nroff-mode objc-mode
247 pascal-mode perl-mode prolog-mode
248 python-mode scheme-mode sgml-mode
249 sh-mode shell-script-mode simula-mode
250 tcl-mode tex-mode texinfo-mode
251 vrml-mode xml-mode)
253 "Major modes in which we turn on whitespace checking.
255 These are mostly programming and documentation modes. But you may add other
256 modes that you want whitespaces checked in by adding something like the
257 following to your `.emacs':
259 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
260 whitespace-modes))\)
262 Or, alternately, you can use the Emacs `customize' command to set this."
263 :type '(repeat symbol)
264 :group 'whitespace)
266 (defcustom whitespace-rescan-timer-time 600
267 "Period in seconds to rescan modified buffers for whitespace creep.
269 This is the period after which the timer will fire causing
270 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
271 modified buffers.
273 To disable timer scans, set this to zero."
274 :type 'integer
275 :group 'whitespace)
277 (defcustom whitespace-display-in-modeline t
278 "Display whitespace errors on the modeline."
279 :type 'boolean
280 :group 'whitespace)
282 (defcustom whitespace-display-spaces-in-color t
283 "Display the bogus whitespaces by coloring them with the face
284 `whitespace-highlight'."
285 :type 'boolean
286 :group 'whitespace)
288 (defgroup whitespace-faces nil
289 "Faces used in whitespace."
290 :prefix "whitespace-"
291 :group 'whitespace
292 :group 'faces)
294 (defface whitespace-highlight '((((class color) (background light))
295 (:background "green1"))
296 (((class color) (background dark))
297 (:background "sea green"))
298 (((class grayscale mono)
299 (background light))
300 (:background "black"))
301 (((class grayscale mono)
302 (background dark))
303 (:background "white")))
304 "Face used for highlighting the bogus whitespaces that exist in the buffer."
305 :group 'whitespace-faces)
306 ;; backward-compatibility alias
307 (put 'whitespace-highlight-face 'face-alias 'whitespace-highlight)
309 (if (not (assoc 'whitespace-mode minor-mode-alist))
310 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
311 minor-mode-alist)))
313 (set-default 'whitespace-check-buffer-leading
314 whitespace-check-leading-whitespace)
315 (set-default 'whitespace-check-buffer-trailing
316 whitespace-check-trailing-whitespace)
317 (set-default 'whitespace-check-buffer-indent
318 whitespace-check-indent-whitespace)
319 (set-default 'whitespace-check-buffer-spacetab
320 whitespace-check-spacetab-whitespace)
321 (set-default 'whitespace-check-buffer-ateol
322 whitespace-check-ateol-whitespace)
324 (defun whitespace-check-whitespace-mode (&optional arg)
325 "Test and set the whitespace-mode in qualifying buffers."
326 (if (null whitespace-mode)
327 (setq whitespace-mode
328 (if (or arg (member major-mode whitespace-modes))
330 nil))))
332 ;;;###autoload
333 (defun whitespace-toggle-leading-check ()
334 "Toggle the check for leading space in the local buffer."
335 (interactive)
336 (let ((current-val whitespace-check-buffer-leading))
337 (setq whitespace-check-buffer-leading (not current-val))
338 (message "Will%s check for leading space in buffer."
339 (if whitespace-check-buffer-leading "" " not"))
340 (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
342 ;;;###autoload
343 (defun whitespace-toggle-trailing-check ()
344 "Toggle the check for trailing space in the local buffer."
345 (interactive)
346 (let ((current-val whitespace-check-buffer-trailing))
347 (setq whitespace-check-buffer-trailing (not current-val))
348 (message "Will%s check for trailing space in buffer."
349 (if whitespace-check-buffer-trailing "" " not"))
350 (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
352 ;;;###autoload
353 (defun whitespace-toggle-indent-check ()
354 "Toggle the check for indentation space in the local buffer."
355 (interactive)
356 (let ((current-val whitespace-check-buffer-indent))
357 (setq whitespace-check-buffer-indent (not current-val))
358 (message "Will%s check for indentation space in buffer."
359 (if whitespace-check-buffer-indent "" " not"))
360 (if whitespace-check-buffer-indent
361 (whitespace-buffer-search whitespace-indent-regexp))))
363 ;;;###autoload
364 (defun whitespace-toggle-spacetab-check ()
365 "Toggle the check for space-followed-by-TABs in the local buffer."
366 (interactive)
367 (let ((current-val whitespace-check-buffer-spacetab))
368 (setq whitespace-check-buffer-spacetab (not current-val))
369 (message "Will%s check for space-followed-by-TABs in buffer."
370 (if whitespace-check-buffer-spacetab "" " not"))
371 (if whitespace-check-buffer-spacetab
372 (whitespace-buffer-search whitespace-spacetab-regexp))))
375 ;;;###autoload
376 (defun whitespace-toggle-ateol-check ()
377 "Toggle the check for end-of-line space in the local buffer."
378 (interactive)
379 (let ((current-val whitespace-check-buffer-ateol))
380 (setq whitespace-check-buffer-ateol (not current-val))
381 (message "Will%s check for end-of-line space in buffer."
382 (if whitespace-check-buffer-ateol "" " not"))
383 (if whitespace-check-buffer-ateol
384 (whitespace-buffer-search whitespace-ateol-regexp))))
387 ;;;###autoload
388 (defun whitespace-buffer (&optional quiet)
389 "Find five different types of white spaces in buffer.
390 These are:
391 1. Leading space \(empty lines at the top of a file\).
392 2. Trailing space \(empty lines at the end of a file\).
393 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
394 4. Spaces followed by a TAB. \(Almost always, we never want that\).
395 5. Spaces or TABS at the end of a line.
397 Check for whitespace only if this buffer really contains a non-empty file
398 and:
399 1. the major mode is one of the whitespace-modes, or
400 2. `whitespace-buffer' was explicitly called with a prefix argument."
401 (interactive)
402 (let ((whitespace-error nil))
403 (whitespace-check-whitespace-mode current-prefix-arg)
404 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
405 (progn
406 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
407 (whitespace-tickle-timer)
408 (overlay-recenter (point-max))
409 (remove-overlays nil nil 'face 'whitespace-highlight)
410 (if whitespace-auto-cleanup
411 (if buffer-read-only
412 (if (not quiet)
413 (message "Can't cleanup: %s is read-only" (buffer-name)))
414 (whitespace-cleanup-internal))
415 (let ((whitespace-leading (if whitespace-check-buffer-leading
416 (whitespace-buffer-leading)
417 nil))
418 (whitespace-trailing (if whitespace-check-buffer-trailing
419 (whitespace-buffer-trailing)
420 nil))
421 (whitespace-indent (if whitespace-check-buffer-indent
422 (whitespace-buffer-search
423 whitespace-indent-regexp)
424 nil))
425 (whitespace-spacetab (if whitespace-check-buffer-spacetab
426 (whitespace-buffer-search
427 whitespace-spacetab-regexp)
428 nil))
429 (whitespace-ateol (if whitespace-check-buffer-ateol
430 (whitespace-buffer-search
431 whitespace-ateol-regexp)
432 nil))
433 (whitespace-errmsg nil)
434 (whitespace-filename buffer-file-name)
435 (whitespace-this-modeline ""))
437 ;; Now let's complain if we found any of the above.
438 (setq whitespace-error (or whitespace-leading whitespace-indent
439 whitespace-spacetab whitespace-ateol
440 whitespace-trailing))
442 (if whitespace-error
443 (progn
444 (setq whitespace-errmsg
445 (concat whitespace-filename " contains:\n"
446 (if whitespace-leading
447 "Leading whitespace\n")
448 (if whitespace-indent
449 (concat "Indentation whitespace"
450 whitespace-indent "\n"))
451 (if whitespace-spacetab
452 (concat "Space followed by Tab"
453 whitespace-spacetab "\n"))
454 (if whitespace-ateol
455 (concat "End-of-line whitespace"
456 whitespace-ateol "\n"))
457 (if whitespace-trailing
458 "Trailing whitespace\n")
459 "\ntype `M-x whitespace-cleanup' to "
460 "cleanup the file."))
461 (setq whitespace-this-modeline
462 (concat (if whitespace-ateol "e")
463 (if whitespace-indent "i")
464 (if whitespace-leading "l")
465 (if whitespace-spacetab "s")
466 (if whitespace-trailing "t")))))
467 (whitespace-update-modeline whitespace-this-modeline)
468 (if (get-buffer whitespace-errbuf)
469 (kill-buffer whitespace-errbuf))
470 (with-current-buffer (get-buffer-create whitespace-errbuf)
471 (if whitespace-errmsg
472 (progn
473 (insert whitespace-errmsg)
474 (if (not (or quiet whitespace-silent))
475 (display-buffer (current-buffer) t))
476 (if (not quiet)
477 (message "Whitespaces: [%s%s] in %s"
478 whitespace-this-modeline
479 (let ((whitespace-unchecked
480 (whitespace-unchecked-whitespaces)))
481 (if whitespace-unchecked
482 (concat "!" whitespace-unchecked)
483 ""))
484 whitespace-filename)))
485 (if (and (not quiet) (not (equal whitespace-clean-msg "")))
486 (message "%s %s" whitespace-filename
487 whitespace-clean-msg))))))))
488 whitespace-error))
490 ;;;###autoload
491 (defun whitespace-region (s e)
492 "Check the region for whitespace errors."
493 (interactive "r")
494 (save-excursion
495 (save-restriction
496 (narrow-to-region s e)
497 (whitespace-buffer))))
499 ;;;###autoload
500 (defun whitespace-cleanup ()
501 "Cleanup the five different kinds of whitespace problems.
502 It normally applies to the whole buffer, but in Transient Mark mode
503 when the mark is active it applies to the region.
504 See `whitespace-buffer' docstring for a summary of the problems."
505 (interactive)
506 (if (and transient-mark-mode mark-active)
507 (whitespace-cleanup-region (region-beginning) (region-end))
508 (whitespace-cleanup-internal)))
510 (defun whitespace-cleanup-internal (&optional region-only)
511 ;; If this buffer really contains a file, then run, else quit.
512 (whitespace-check-whitespace-mode current-prefix-arg)
513 (if (and buffer-file-name whitespace-mode)
514 (let ((whitespace-any nil)
515 (whitespace-tabwith 8)
516 (whitespace-tabwith-saved tab-width))
518 ;; since all printable TABS should be 8, irrespective of how
519 ;; they are displayed.
520 (setq tab-width whitespace-tabwith)
522 (if (and whitespace-check-buffer-leading
523 (whitespace-buffer-leading))
524 (progn
525 (whitespace-buffer-leading-cleanup)
526 (setq whitespace-any t)))
528 (if (and whitespace-check-buffer-trailing
529 (whitespace-buffer-trailing))
530 (progn
531 (whitespace-buffer-trailing-cleanup)
532 (setq whitespace-any t)))
534 (if (and whitespace-check-buffer-indent
535 (whitespace-buffer-search whitespace-indent-regexp))
536 (progn
537 (whitespace-indent-cleanup)
538 (setq whitespace-any t)))
540 (if (and whitespace-check-buffer-spacetab
541 (whitespace-buffer-search whitespace-spacetab-regexp))
542 (progn
543 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
544 (setq whitespace-any t)))
546 (if (and whitespace-check-buffer-ateol
547 (whitespace-buffer-search whitespace-ateol-regexp))
548 (progn
549 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
550 (setq whitespace-any t)))
552 ;; Call this recursively till everything is taken care of
553 (if whitespace-any
554 (whitespace-cleanup-internal region-only)
555 ;; if we are done, talk to the user
556 (progn
557 (unless whitespace-silent
558 (if region-only
559 (message "The region is now clean")
560 (message "%s is now clean" buffer-file-name)))
561 (whitespace-update-modeline)))
562 (setq tab-width whitespace-tabwith-saved))))
564 ;;;###autoload
565 (defun whitespace-cleanup-region (s e)
566 "Whitespace cleanup on the region."
567 (interactive "r")
568 (save-excursion
569 (save-restriction
570 (narrow-to-region s e)
571 (whitespace-cleanup-internal t))
572 (whitespace-buffer t)))
574 (defun whitespace-buffer-leading ()
575 "Return t if the current buffer has leading newline characters.
576 If highlighting is enabled, highlight these characters."
577 (save-excursion
578 (goto-char (point-min))
579 (skip-chars-forward "\n")
580 (unless (bobp)
581 (whitespace-highlight-the-space (point-min) (point))
582 t)))
584 (defun whitespace-buffer-leading-cleanup ()
585 "Remove any leading newline characters from current buffer."
586 (save-excursion
587 (goto-char (point-min))
588 (skip-chars-forward "\n")
589 (delete-region (point-min) (point))))
591 (defun whitespace-buffer-trailing ()
592 "Return t if the current buffer has extra trailing newline characters.
593 If highlighting is enabled, highlight these characters."
594 (save-excursion
595 (goto-char (point-max))
596 (skip-chars-backward "\n")
597 (forward-line)
598 (unless (eobp)
599 (whitespace-highlight-the-space (point) (point-max))
600 t)))
602 (defun whitespace-buffer-trailing-cleanup ()
603 "Remove extra trailing newline characters from current buffer."
604 (save-excursion
605 (goto-char (point-max))
606 (skip-chars-backward "\n")
607 (unless (eobp)
608 (forward-line)
609 (delete-region (point) (point-max)))))
611 (defun whitespace-buffer-search (regexp)
612 "Search for any given whitespace REGEXP."
613 (with-local-quit
614 (let (whitespace-retval)
615 (save-excursion
616 (goto-char (point-min))
617 (while (re-search-forward regexp nil t)
618 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
619 (push (match-beginning 0) whitespace-retval)))
620 (when whitespace-retval
621 (format " %s" (nreverse whitespace-retval))))))
623 (defun whitespace-buffer-cleanup (regexp newregexp)
624 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
625 (save-excursion
626 (goto-char (point-min))
627 (while (re-search-forward regexp nil t)
628 (replace-match newregexp))))
630 (defun whitespace-indent-cleanup ()
631 "Search for 8/more spaces at the start of a line and replace it with tabs."
632 (save-excursion
633 (goto-char (point-min))
634 (while (re-search-forward whitespace-indent-regexp nil t)
635 (let ((column (current-column))
636 (indent-tabs-mode t))
637 (delete-region (match-beginning 0) (point))
638 (indent-to column)))))
640 (defun whitespace-unchecked-whitespaces ()
641 "Return the list of whitespaces whose testing has been suppressed."
642 (let ((unchecked-spaces
643 (concat (if (not whitespace-check-buffer-ateol) "e")
644 (if (not whitespace-check-buffer-indent) "i")
645 (if (not whitespace-check-buffer-leading) "l")
646 (if (not whitespace-check-buffer-spacetab) "s")
647 (if (not whitespace-check-buffer-trailing) "t"))))
648 (if (not (equal unchecked-spaces ""))
649 unchecked-spaces
650 nil)))
652 (defun whitespace-update-modeline (&optional whitespace-err)
653 "Update modeline with whitespace errors.
654 Also with whitespaces whose testing has been turned off."
655 (if whitespace-display-in-modeline
656 (progn
657 (setq whitespace-mode-line nil)
658 ;; Whitespace errors
659 (if (and whitespace-err (not (equal whitespace-err "")))
660 (setq whitespace-mode-line whitespace-err))
661 ;; Whitespace suppressed errors
662 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
663 (if whitespace-unchecked
664 (setq whitespace-mode-line
665 (concat whitespace-mode-line "!" whitespace-unchecked))))
666 ;; Add the whitespace modeline prefix
667 (setq whitespace-mode-line (if whitespace-mode-line
668 (concat " W:" whitespace-mode-line)
669 nil))
670 (whitespace-mode-line-update))))
672 (defun whitespace-highlight-the-space (b e)
673 "Highlight the current line, unhighlighting a previously jumped to line."
674 (if whitespace-display-spaces-in-color
675 (let ((ol (whitespace-make-overlay b e)))
676 (whitespace-overlay-put ol 'face 'whitespace-highlight))))
678 (defun whitespace-unhighlight-the-space()
679 "Unhighlight the currently highlight line."
680 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
681 (progn
682 (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
683 (setq whitespace-highlighted-space nil))))
685 (defun whitespace-check-buffer-list (buf-name buf-file)
686 "Add a buffer and its file to the whitespace monitor list.
688 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
689 periodically for whitespace."
690 (if (and whitespace-mode (not (member (list buf-file buf-name)
691 whitespace-all-buffer-files)))
692 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
694 (defun whitespace-tickle-timer ()
695 "Tickle timer to periodically to scan qualifying files for whitespace creep.
697 If timer is not set, then set it to scan the files in
698 `whitespace-all-buffer-files' periodically (defined by
699 `whitespace-rescan-timer-time') for whitespace creep."
700 (if (and whitespace-rescan-timer-time
701 (/= whitespace-rescan-timer-time 0)
702 (not whitespace-rescan-timer))
703 (setq whitespace-rescan-timer
704 (add-timeout whitespace-rescan-timer-time
705 'whitespace-rescan-files-in-buffers nil
706 whitespace-rescan-timer-time))))
708 (defun whitespace-rescan-files-in-buffers (&optional arg)
709 "Check monitored files for whitespace creep since last scan."
710 (let ((whitespace-all-my-files whitespace-all-buffer-files)
711 buffile bufname thiselt buf)
712 (if (not whitespace-all-my-files)
713 (progn
714 (disable-timeout whitespace-rescan-timer)
715 (setq whitespace-rescan-timer nil))
716 (while whitespace-all-my-files
717 (setq thiselt (car whitespace-all-my-files))
718 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
719 (setq buffile (car thiselt))
720 (setq bufname (cadr thiselt))
721 (setq buf (get-buffer bufname))
722 (if (buffer-live-p buf)
723 (save-excursion
724 ;;(message "buffer %s live" bufname)
725 (set-buffer bufname)
726 (if whitespace-mode
727 (progn
728 ;;(message "checking for whitespace in %s" bufname)
729 (if whitespace-auto-cleanup
730 (progn
731 ;;(message "cleaning up whitespace in %s" bufname)
732 (whitespace-cleanup-internal))
733 (progn
734 ;;(message "whitespace-buffer %s." (buffer-name))
735 (whitespace-buffer t))))
736 ;;(message "Removing %s from refresh list" bufname)
737 (whitespace-refresh-rescan-list buffile bufname)))
738 ;;(message "Removing %s from refresh list" bufname)
739 (whitespace-refresh-rescan-list buffile bufname))))))
741 (defun whitespace-refresh-rescan-list (buffile bufname)
742 "Refresh the list of files to be rescanned for whitespace creep."
743 (if whitespace-all-buffer-files
744 (setq whitespace-all-buffer-files
745 (delete (list buffile bufname) whitespace-all-buffer-files))
746 (when whitespace-rescan-timer
747 (disable-timeout whitespace-rescan-timer)
748 (setq whitespace-rescan-timer nil))))
750 ;;;###autoload
751 (defalias 'global-whitespace-mode 'whitespace-global-mode)
753 ;;;###autoload
754 (define-minor-mode whitespace-global-mode
755 "Toggle using Whitespace mode in new buffers.
756 With ARG, turn the mode on if ARG is positive, otherwise turn it off.
758 When this mode is active, `whitespace-buffer' is added to
759 `find-file-hook' and `kill-buffer-hook'."
760 :global t
761 :group 'whitespace
762 (if whitespace-global-mode
763 (progn
764 (add-hook 'find-file-hook 'whitespace-buffer)
765 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
766 (add-hook 'kill-buffer-hook 'whitespace-buffer))
767 (remove-hook 'find-file-hook 'whitespace-buffer)
768 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
769 (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
771 ;;;###autoload
772 (defun whitespace-write-file-hook ()
773 "Hook function to be called on the buffer when whitespace check is enabled.
774 This is meant to be added buffer-locally to `write-file-functions'."
775 (let ((werr nil))
776 (if whitespace-auto-cleanup
777 (whitespace-cleanup-internal)
778 (setq werr (whitespace-buffer)))
779 (if (and whitespace-abort-on-error werr)
780 (error "Abort write due to whitespaces in %s"
781 buffer-file-name)))
782 nil)
784 (defun whitespace-unload-function ()
785 "Unload the whitespace library."
786 (if (unintern "whitespace-unload-hook")
787 ;; if whitespace-unload-hook is defined, let's get rid of it
788 ;; and recursively call `unload-feature'
789 (progn (unload-feature 'whitespace) t)
790 ;; this only happens in the recursive call
791 (whitespace-global-mode -1)
792 (save-current-buffer
793 (dolist (buf (buffer-list))
794 (set-buffer buf)
795 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)))
796 ;; continue standard unloading
797 nil))
799 (defun whitespace-unload-hook ()
800 (remove-hook 'find-file-hook 'whitespace-buffer)
801 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
802 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
804 (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
806 (provide 'whitespace)
808 ;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
809 ;;; whitespace.el ends here