Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / obsolete / old-whitespace.el
blob1b7ca5be7719bd84a288e88763735fd648c40e56
1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
6 ;; Keywords: convenience
7 ;; Obsolete-since: 23.1
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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; URL: http://www.dsmit.com/lisp/
28 ;; The whitespace library is intended to find and help fix five different types
29 ;; of whitespace problems that commonly exist in source code.
31 ;; 1. Leading space (empty lines at the top of a file).
32 ;; 2. Trailing space (empty lines at the end of a file).
33 ;; 3. Indentation space (8 or more spaces at beginning of line, that should be
34 ;; replaced with TABS).
35 ;; 4. Spaces followed by a TAB. (Almost always, we never want that).
36 ;; 5. Spaces or TABS at the end of a line.
38 ;; Whitespace errors are reported in a buffer, and on the mode line.
40 ;; Mode line will show a W:<x>!<y> to denote a particular type of whitespace,
41 ;; where `x' and `y' can be one (or more) of:
43 ;; e - End-of-Line whitespace.
44 ;; i - Indentation whitespace.
45 ;; l - Leading whitespace.
46 ;; s - Space followed by Tab.
47 ;; t - Trailing whitespace.
49 ;; If any of the whitespace checks is turned off, the mode line will display a
50 ;; !<y>.
52 ;; (since (3) is the most controversial one, here is the rationale: Most
53 ;; terminal drivers and printer drivers have TAB configured or even
54 ;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost
55 ;; always they default to 8.)
57 ;; Changing `tab-width' to other than 8 and editing will cause your code to
58 ;; look different from within Emacs, and say, if you cat it or more it, or
59 ;; even print it.
61 ;; Almost all the popular programming modes let you define an offset (like
62 ;; c-basic-offset or perl-indent-level) to configure the offset, so you
63 ;; should never have to set your `tab-width' to be other than 8 in all
64 ;; these modes. In fact, with an indent level of say, 4, 2 TABS will cause
65 ;; Emacs to replace your 8 spaces with one \t (try it). If vi users in
66 ;; your office complain, tell them to use vim, which distinguishes between
67 ;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
68 ;; to set smarttab.)
70 ;; All the above have caused (and will cause) unwanted codeline integration and
71 ;; merge problems.
73 ;; whitespace.el will complain if it detects whitespaces on opening a file, and
74 ;; warn you on closing a file also (in case you had inserted any
75 ;; whitespaces during the process of your editing).
77 ;; Exported functions:
79 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
80 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
81 ;; `whitespace-region' - To check between point and mark for whitespace
82 ;; problems.
83 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
84 ;; and mark in the current buffer.
86 ;;; Code:
88 (defvar whitespace-version "3.5" "Version of the whitespace library.")
90 (defvar whitespace-all-buffer-files nil
91 "An associated list of buffers and files checked for whitespace cleanliness.
93 This is to enable periodic checking of whitespace cleanliness in the files
94 visited by the buffers.")
96 (defvar whitespace-rescan-timer nil
97 "Timer object used to rescan the files in buffers that have been modified.")
99 ;; Tell Emacs about this new kind of minor mode
100 (defvar whitespace-mode nil
101 "Non-nil when Whitespace mode (a minor mode) is enabled.")
102 (make-variable-buffer-local 'whitespace-mode)
104 (defvar whitespace-mode-line nil
105 "String to display in the mode line for Whitespace mode.")
106 (make-variable-buffer-local 'whitespace-mode-line)
108 (defvar whitespace-check-buffer-leading nil
109 "Test leading whitespace for file in current buffer if t.")
110 (make-variable-buffer-local 'whitespace-check-buffer-leading)
111 ;;;###autoload(put 'whitespace-check-buffer-leading 'safe-local-variable 'booleanp)
113 (defvar whitespace-check-buffer-trailing nil
114 "Test trailing whitespace for file in current buffer if t.")
115 (make-variable-buffer-local 'whitespace-check-buffer-trailing)
116 ;;;###autoload(put 'whitespace-check-buffer-trailing 'safe-local-variable 'booleanp)
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)
121 ;;;###autoload(put 'whitespace-check-buffer-indent 'safe-local-variable 'booleanp)
123 (defvar whitespace-check-buffer-spacetab nil
124 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
125 (make-variable-buffer-local 'whitespace-check-buffer-spacetab)
126 ;;;###autoload(put 'whitespace-check-buffer-spacetab 'safe-local-variable 'booleanp)
128 (defvar whitespace-check-buffer-ateol nil
129 "Test end-of-line whitespace for file in current buffer if t.")
130 (make-variable-buffer-local 'whitespace-check-buffer-ateol)
131 ;;;###autoload(put 'whitespace-check-buffer-ateol 'safe-local-variable 'booleanp)
133 (defvar whitespace-highlighted-space nil
134 "The variable to store the extent to highlight.")
135 (make-variable-buffer-local 'whitespace-highlighted-space)
137 (defalias 'whitespace-make-overlay
138 (if (featurep 'xemacs) 'make-extent 'make-overlay))
139 (defalias 'whitespace-overlay-put
140 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
141 (defalias 'whitespace-delete-overlay
142 (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
143 (defalias 'whitespace-overlay-start
144 (if (featurep 'xemacs) 'extent-start 'overlay-start))
145 (defalias 'whitespace-overlay-end
146 (if (featurep 'xemacs) 'extent-end 'overlay-end))
147 (defalias 'whitespace-mode-line-update
148 (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
150 (defgroup whitespace nil
151 "Check for and fix five different types of whitespaces in source code."
152 :version "21.1"
153 :link '(emacs-commentary-link "whitespace.el")
154 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
155 ;; which is 'editing?
156 :group (if (featurep 'xemacs) 'editing 'convenience))
158 (defcustom whitespace-check-leading-whitespace t
159 "Flag to check leading whitespace. This is the global for the system.
160 It can be overridden by setting a buffer local variable
161 `whitespace-check-buffer-leading'."
162 :type 'boolean
163 :group 'whitespace)
165 (defcustom whitespace-check-trailing-whitespace t
166 "Flag to check trailing whitespace. This is the global for the system.
167 It can be overridden by setting a buffer local variable
168 `whitespace-check-buffer-trailing'."
169 :type 'boolean
170 :group 'whitespace)
172 (defcustom whitespace-check-spacetab-whitespace t
173 "Flag to check space followed by a TAB. This is the global for the system.
174 It can be overridden by setting a buffer local variable
175 `whitespace-check-buffer-spacetab'."
176 :type 'boolean
177 :group 'whitespace)
179 (defcustom whitespace-spacetab-regexp "[ ]+\t"
180 "Regexp to match one or more spaces followed by a TAB."
181 :type 'regexp
182 :group 'whitespace)
184 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
185 "Flag to check indentation whitespace. This is the global for the system.
186 It can be overridden by setting a buffer local variable
187 `whitespace-check-buffer-indent'."
188 :type 'boolean
189 :group 'whitespace)
191 (defcustom whitespace-indent-regexp "^\t*\\( \\)+"
192 "Regexp to match multiples of eight spaces near line beginnings.
193 The default value ignores leading TABs."
194 :type 'regexp
195 :group 'whitespace)
197 (defcustom whitespace-check-ateol-whitespace t
198 "Flag to check end-of-line whitespace. This is the global for the system.
199 It can be overridden by setting a buffer local variable
200 `whitespace-check-buffer-ateol'."
201 :type 'boolean
202 :group 'whitespace)
204 (defcustom whitespace-ateol-regexp "[ \t]+$"
205 "Regexp to match one or more TABs or spaces at line ends."
206 :type 'regexp
207 :group 'whitespace)
209 (defcustom whitespace-errbuf "*Whitespace Errors*"
210 "The name of the buffer where whitespace related messages will be logged."
211 :type 'string
212 :group 'whitespace)
214 (defcustom whitespace-clean-msg "clean."
215 "If non-nil, this message will be displayed after a whitespace check
216 determines a file to be clean."
217 :type 'string
218 :group 'whitespace)
220 (defcustom whitespace-abort-on-error nil
221 "While writing a file, abort if the file is unclean.
222 If `whitespace-auto-cleanup' is set, that takes precedence over
223 this variable."
224 :type 'boolean
225 :group 'whitespace)
227 (defcustom whitespace-auto-cleanup nil
228 "Cleanup a buffer automatically on finding it whitespace unclean."
229 :type 'boolean
230 :group 'whitespace)
232 (defcustom whitespace-silent nil
233 "All whitespace errors will be shown only in the mode line when t.
235 Note that setting this may cause all whitespaces introduced in a file to go
236 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
237 Errors*' buffer before opening (or closing) another file."
238 :type 'boolean
239 :group 'whitespace)
241 (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
242 c-mode c++-mode cc-mode
243 change-log-mode cperl-mode
244 electric-nroff-mode emacs-lisp-mode
245 f90-mode fortran-mode html-mode
246 html3-mode java-mode jde-mode
247 ksh-mode latex-mode LaTeX-mode
248 lisp-mode m4-mode makefile-mode
249 modula-2-mode nroff-mode objc-mode
250 pascal-mode perl-mode prolog-mode
251 python-mode scheme-mode sgml-mode
252 sh-mode shell-script-mode simula-mode
253 tcl-mode tex-mode texinfo-mode
254 vrml-mode xml-mode)
256 "Major modes in which we turn on whitespace checking.
258 These are mostly programming and documentation modes. But you may add other
259 modes that you want whitespaces checked in by adding something like the
260 following to your `.emacs':
262 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
263 whitespace-modes))\)
265 Or, alternately, you can use the Emacs `customize' command to set this."
266 :type '(repeat symbol)
267 :group 'whitespace)
269 (defcustom whitespace-rescan-timer-time 600
270 "Period in seconds to rescan modified buffers for whitespace creep.
272 This is the period after which the timer will fire causing
273 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
274 modified buffers.
276 To disable timer scans, set this to zero."
277 :type 'integer
278 :group 'whitespace)
280 (defcustom whitespace-display-in-modeline t
281 "Display whitespace errors on the modeline."
282 :type 'boolean
283 :group 'whitespace)
285 (defcustom whitespace-display-spaces-in-color t
286 "Display the bogus whitespaces by coloring them with the face
287 `whitespace-highlight'."
288 :type 'boolean
289 :group 'whitespace)
291 (defface whitespace-highlight '((((class color) (background light))
292 (:background "green1"))
293 (((class color) (background dark))
294 (:background "sea green"))
295 (((class grayscale mono)
296 (background light))
297 (:background "black"))
298 (((class grayscale mono)
299 (background dark))
300 (:background "white")))
301 "Face used for highlighting the bogus whitespaces that exist in the buffer."
302 :group 'whitespace)
303 (define-obsolete-face-alias 'whitespace-highlight-face
304 'whitespace-highlight "22.1")
306 (if (not (assoc 'whitespace-mode minor-mode-alist))
307 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
308 minor-mode-alist)))
310 (set-default 'whitespace-check-buffer-leading
311 whitespace-check-leading-whitespace)
312 (set-default 'whitespace-check-buffer-trailing
313 whitespace-check-trailing-whitespace)
314 (set-default 'whitespace-check-buffer-indent
315 whitespace-check-indent-whitespace)
316 (set-default 'whitespace-check-buffer-spacetab
317 whitespace-check-spacetab-whitespace)
318 (set-default 'whitespace-check-buffer-ateol
319 whitespace-check-ateol-whitespace)
321 (defun whitespace-check-whitespace-mode (&optional arg)
322 "Test and set the whitespace-mode in qualifying buffers."
323 (if (null whitespace-mode)
324 (setq whitespace-mode
325 (if (or arg (member major-mode whitespace-modes))
327 nil))))
329 ;;;###autoload
330 (defun whitespace-toggle-leading-check ()
331 "Toggle the check for leading space in the local buffer."
332 (interactive)
333 (let ((current-val whitespace-check-buffer-leading))
334 (setq whitespace-check-buffer-leading (not current-val))
335 (message "Will%s check for leading space in buffer."
336 (if whitespace-check-buffer-leading "" " not"))
337 (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
339 ;;;###autoload
340 (defun whitespace-toggle-trailing-check ()
341 "Toggle the check for trailing space in the local buffer."
342 (interactive)
343 (let ((current-val whitespace-check-buffer-trailing))
344 (setq whitespace-check-buffer-trailing (not current-val))
345 (message "Will%s check for trailing space in buffer."
346 (if whitespace-check-buffer-trailing "" " not"))
347 (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
349 ;;;###autoload
350 (defun whitespace-toggle-indent-check ()
351 "Toggle the check for indentation space in the local buffer."
352 (interactive)
353 (let ((current-val whitespace-check-buffer-indent))
354 (setq whitespace-check-buffer-indent (not current-val))
355 (message "Will%s check for indentation space in buffer."
356 (if whitespace-check-buffer-indent "" " not"))
357 (if whitespace-check-buffer-indent
358 (whitespace-buffer-search whitespace-indent-regexp))))
360 ;;;###autoload
361 (defun whitespace-toggle-spacetab-check ()
362 "Toggle the check for space-followed-by-TABs in the local buffer."
363 (interactive)
364 (let ((current-val whitespace-check-buffer-spacetab))
365 (setq whitespace-check-buffer-spacetab (not current-val))
366 (message "Will%s check for space-followed-by-TABs in buffer."
367 (if whitespace-check-buffer-spacetab "" " not"))
368 (if whitespace-check-buffer-spacetab
369 (whitespace-buffer-search whitespace-spacetab-regexp))))
372 ;;;###autoload
373 (defun whitespace-toggle-ateol-check ()
374 "Toggle the check for end-of-line space in the local buffer."
375 (interactive)
376 (let ((current-val whitespace-check-buffer-ateol))
377 (setq whitespace-check-buffer-ateol (not current-val))
378 (message "Will%s check for end-of-line space in buffer."
379 (if whitespace-check-buffer-ateol "" " not"))
380 (if whitespace-check-buffer-ateol
381 (whitespace-buffer-search whitespace-ateol-regexp))))
384 ;;;###autoload
385 (defun whitespace-buffer (&optional quiet)
386 "Find five different types of white spaces in buffer.
387 These are:
388 1. Leading space \(empty lines at the top of a file\).
389 2. Trailing space \(empty lines at the end of a file\).
390 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
391 4. Spaces followed by a TAB. \(Almost always, we never want that\).
392 5. Spaces or TABS at the end of a line.
394 Check for whitespace only if this buffer really contains a non-empty file
395 and:
396 1. the major mode is one of the whitespace-modes, or
397 2. `whitespace-buffer' was explicitly called with a prefix argument."
398 (interactive)
399 (let ((whitespace-error nil))
400 (whitespace-check-whitespace-mode current-prefix-arg)
401 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
402 (progn
403 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
404 (whitespace-tickle-timer)
405 (overlay-recenter (point-max))
406 (remove-overlays nil nil 'face 'whitespace-highlight)
407 (if whitespace-auto-cleanup
408 (if buffer-read-only
409 (if (not quiet)
410 (message "Can't cleanup: %s is read-only" (buffer-name)))
411 (whitespace-cleanup-internal))
412 (let ((whitespace-leading (if whitespace-check-buffer-leading
413 (whitespace-buffer-leading)
414 nil))
415 (whitespace-trailing (if whitespace-check-buffer-trailing
416 (whitespace-buffer-trailing)
417 nil))
418 (whitespace-indent (if whitespace-check-buffer-indent
419 (whitespace-buffer-search
420 whitespace-indent-regexp)
421 nil))
422 (whitespace-spacetab (if whitespace-check-buffer-spacetab
423 (whitespace-buffer-search
424 whitespace-spacetab-regexp)
425 nil))
426 (whitespace-ateol (if whitespace-check-buffer-ateol
427 (whitespace-buffer-search
428 whitespace-ateol-regexp)
429 nil))
430 (whitespace-errmsg nil)
431 (whitespace-filename buffer-file-name)
432 (whitespace-this-modeline ""))
434 ;; Now let's complain if we found any of the above.
435 (setq whitespace-error (or whitespace-leading whitespace-indent
436 whitespace-spacetab whitespace-ateol
437 whitespace-trailing))
439 (if whitespace-error
440 (progn
441 (setq whitespace-errmsg
442 (concat whitespace-filename " contains:\n"
443 (if whitespace-leading
444 "Leading whitespace\n")
445 (if whitespace-indent
446 (concat "Indentation whitespace"
447 whitespace-indent "\n"))
448 (if whitespace-spacetab
449 (concat "Space followed by Tab"
450 whitespace-spacetab "\n"))
451 (if whitespace-ateol
452 (concat "End-of-line whitespace"
453 whitespace-ateol "\n"))
454 (if whitespace-trailing
455 "Trailing whitespace\n")
456 "\ntype `M-x whitespace-cleanup' to "
457 "cleanup the file."))
458 (setq whitespace-this-modeline
459 (concat (if whitespace-ateol "e")
460 (if whitespace-indent "i")
461 (if whitespace-leading "l")
462 (if whitespace-spacetab "s")
463 (if whitespace-trailing "t")))))
464 (whitespace-update-modeline whitespace-this-modeline)
465 (if (get-buffer whitespace-errbuf)
466 (kill-buffer whitespace-errbuf))
467 (with-current-buffer (get-buffer-create whitespace-errbuf)
468 (if whitespace-errmsg
469 (progn
470 (insert whitespace-errmsg)
471 (if (not (or quiet whitespace-silent))
472 (display-buffer (current-buffer) t))
473 (if (not quiet)
474 (message "Whitespaces: [%s%s] in %s"
475 whitespace-this-modeline
476 (let ((whitespace-unchecked
477 (whitespace-unchecked-whitespaces)))
478 (if whitespace-unchecked
479 (concat "!" whitespace-unchecked)
480 ""))
481 whitespace-filename)))
482 (if (and (not quiet) (not (equal whitespace-clean-msg "")))
483 (message "%s %s" whitespace-filename
484 whitespace-clean-msg))))))))
485 whitespace-error))
487 ;;;###autoload
488 (defun whitespace-region (s e)
489 "Check the region for whitespace errors."
490 (interactive "r")
491 (save-excursion
492 (save-restriction
493 (narrow-to-region s e)
494 (whitespace-buffer))))
496 ;;;###autoload
497 (defun whitespace-cleanup ()
498 "Cleanup the five different kinds of whitespace problems.
499 It normally applies to the whole buffer, but in Transient Mark mode
500 when the mark is active it applies to the region.
501 See `whitespace-buffer' docstring for a summary of the problems."
502 (interactive)
503 (if (and transient-mark-mode mark-active)
504 (whitespace-cleanup-region (region-beginning) (region-end))
505 (whitespace-cleanup-internal)))
507 (defun whitespace-cleanup-internal (&optional region-only)
508 ;; If this buffer really contains a file, then run, else quit.
509 (whitespace-check-whitespace-mode current-prefix-arg)
510 (if (and buffer-file-name whitespace-mode)
511 (let ((whitespace-any nil)
512 (whitespace-tabwidth 8)
513 (whitespace-tabwidth-saved tab-width))
515 ;; since all printable TABS should be 8, irrespective of how
516 ;; they are displayed.
517 (setq tab-width whitespace-tabwidth)
519 (if (and whitespace-check-buffer-leading
520 (whitespace-buffer-leading))
521 (progn
522 (whitespace-buffer-leading-cleanup)
523 (setq whitespace-any t)))
525 (if (and whitespace-check-buffer-trailing
526 (whitespace-buffer-trailing))
527 (progn
528 (whitespace-buffer-trailing-cleanup)
529 (setq whitespace-any t)))
531 (if (and whitespace-check-buffer-indent
532 (whitespace-buffer-search whitespace-indent-regexp))
533 (progn
534 (whitespace-indent-cleanup)
535 (setq whitespace-any t)))
537 (if (and whitespace-check-buffer-spacetab
538 (whitespace-buffer-search whitespace-spacetab-regexp))
539 (progn
540 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
541 (setq whitespace-any t)))
543 (if (and whitespace-check-buffer-ateol
544 (whitespace-buffer-search whitespace-ateol-regexp))
545 (progn
546 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
547 (setq whitespace-any t)))
549 ;; Call this recursively till everything is taken care of
550 (if whitespace-any
551 (whitespace-cleanup-internal region-only)
552 ;; if we are done, talk to the user
553 (progn
554 (unless whitespace-silent
555 (if region-only
556 (message "The region is now clean")
557 (message "%s is now clean" buffer-file-name)))
558 (whitespace-update-modeline)))
559 (setq tab-width whitespace-tabwidth-saved))))
561 ;;;###autoload
562 (defun whitespace-cleanup-region (s e)
563 "Whitespace cleanup on the region."
564 (interactive "r")
565 (save-excursion
566 (save-restriction
567 (narrow-to-region s e)
568 (whitespace-cleanup-internal t))
569 (whitespace-buffer t)))
571 (defun whitespace-buffer-leading ()
572 "Return t if the current buffer has leading newline characters.
573 If highlighting is enabled, highlight these characters."
574 (save-excursion
575 (goto-char (point-min))
576 (skip-chars-forward "\n")
577 (unless (bobp)
578 (whitespace-highlight-the-space (point-min) (point))
579 t)))
581 (defun whitespace-buffer-leading-cleanup ()
582 "Remove any leading newline characters from current buffer."
583 (save-excursion
584 (goto-char (point-min))
585 (skip-chars-forward "\n")
586 (delete-region (point-min) (point))))
588 (defun whitespace-buffer-trailing ()
589 "Return t if the current buffer has extra trailing newline characters.
590 If highlighting is enabled, highlight these characters."
591 (save-excursion
592 (goto-char (point-max))
593 (skip-chars-backward "\n")
594 (forward-line)
595 (unless (eobp)
596 (whitespace-highlight-the-space (point) (point-max))
597 t)))
599 (defun whitespace-buffer-trailing-cleanup ()
600 "Remove extra trailing newline characters from current buffer."
601 (save-excursion
602 (goto-char (point-max))
603 (skip-chars-backward "\n")
604 (unless (eobp)
605 (forward-line)
606 (delete-region (point) (point-max)))))
608 (defun whitespace-buffer-search (regexp)
609 "Search for any given whitespace REGEXP."
610 (with-local-quit
611 (let (whitespace-retval)
612 (save-excursion
613 (goto-char (point-min))
614 (while (re-search-forward regexp nil t)
615 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
616 (push (match-beginning 0) whitespace-retval)))
617 (when whitespace-retval
618 (format " %s" (nreverse whitespace-retval))))))
620 (defun whitespace-buffer-cleanup (regexp newregexp)
621 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
622 (save-excursion
623 (goto-char (point-min))
624 (while (re-search-forward regexp nil t)
625 (replace-match newregexp))))
627 (defun whitespace-indent-cleanup ()
628 "Search for 8/more spaces at the start of a line and replace it with tabs."
629 (save-excursion
630 (goto-char (point-min))
631 (while (re-search-forward whitespace-indent-regexp nil t)
632 (let ((column (current-column))
633 (indent-tabs-mode t))
634 (delete-region (match-beginning 0) (point))
635 (indent-to column)))))
637 (defun whitespace-unchecked-whitespaces ()
638 "Return the list of whitespaces whose testing has been suppressed."
639 (let ((unchecked-spaces
640 (concat (if (not whitespace-check-buffer-ateol) "e")
641 (if (not whitespace-check-buffer-indent) "i")
642 (if (not whitespace-check-buffer-leading) "l")
643 (if (not whitespace-check-buffer-spacetab) "s")
644 (if (not whitespace-check-buffer-trailing) "t"))))
645 (if (not (equal unchecked-spaces ""))
646 unchecked-spaces
647 nil)))
649 (defun whitespace-update-modeline (&optional whitespace-err)
650 "Update mode line with whitespace errors.
651 Also with whitespaces whose testing has been turned off."
652 (if whitespace-display-in-modeline
653 (progn
654 (setq whitespace-mode-line nil)
655 ;; Whitespace errors
656 (if (and whitespace-err (not (equal whitespace-err "")))
657 (setq whitespace-mode-line whitespace-err))
658 ;; Whitespace suppressed errors
659 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
660 (if whitespace-unchecked
661 (setq whitespace-mode-line
662 (concat whitespace-mode-line "!" whitespace-unchecked))))
663 ;; Add the whitespace modeline prefix
664 (setq whitespace-mode-line (if whitespace-mode-line
665 (concat " W:" whitespace-mode-line)
666 nil))
667 (whitespace-mode-line-update))))
669 (defun whitespace-highlight-the-space (b e)
670 "Highlight the current line, unhighlighting a previously jumped to line."
671 (if whitespace-display-spaces-in-color
672 (let ((ol (whitespace-make-overlay b e)))
673 (whitespace-overlay-put ol 'face 'whitespace-highlight))))
675 (defun whitespace-unhighlight-the-space()
676 "Unhighlight the currently highlight line."
677 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
678 (progn
679 (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
680 (setq whitespace-highlighted-space nil))))
682 (defun whitespace-check-buffer-list (buf-name buf-file)
683 "Add a buffer and its file to the whitespace monitor list.
685 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
686 periodically for whitespace."
687 (if (and whitespace-mode (not (member (list buf-file buf-name)
688 whitespace-all-buffer-files)))
689 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
691 (defun whitespace-tickle-timer ()
692 "Tickle timer to periodically to scan qualifying files for whitespace creep.
694 If timer is not set, then set it to scan the files in
695 `whitespace-all-buffer-files' periodically (defined by
696 `whitespace-rescan-timer-time') for whitespace creep."
697 (if (and whitespace-rescan-timer-time
698 (/= whitespace-rescan-timer-time 0)
699 (not whitespace-rescan-timer))
700 (setq whitespace-rescan-timer
701 (add-timeout whitespace-rescan-timer-time
702 'whitespace-rescan-files-in-buffers nil
703 whitespace-rescan-timer-time))))
705 (defun whitespace-rescan-files-in-buffers (&optional arg)
706 "Check monitored files for whitespace creep since last scan."
707 (let ((whitespace-all-my-files whitespace-all-buffer-files)
708 buffile bufname thiselt buf)
709 (if (not whitespace-all-my-files)
710 (progn
711 (disable-timeout whitespace-rescan-timer)
712 (setq whitespace-rescan-timer nil))
713 (while whitespace-all-my-files
714 (setq thiselt (car whitespace-all-my-files))
715 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
716 (setq buffile (car thiselt))
717 (setq bufname (cadr thiselt))
718 (setq buf (get-buffer bufname))
719 (if (buffer-live-p buf)
720 (with-current-buffer bufname
721 ;;(message "buffer %s live" bufname)
722 (if whitespace-mode
723 (progn
724 ;;(message "checking for whitespace in %s" bufname)
725 (if whitespace-auto-cleanup
726 (progn
727 ;;(message "cleaning up whitespace in %s" bufname)
728 (whitespace-cleanup-internal))
729 (progn
730 ;;(message "whitespace-buffer %s." (buffer-name))
731 (whitespace-buffer t))))
732 ;;(message "Removing %s from refresh list" bufname)
733 (whitespace-refresh-rescan-list buffile bufname)))
734 ;;(message "Removing %s from refresh list" bufname)
735 (whitespace-refresh-rescan-list buffile bufname))))))
737 (defun whitespace-refresh-rescan-list (buffile bufname)
738 "Refresh the list of files to be rescanned for whitespace creep."
739 (if whitespace-all-buffer-files
740 (setq whitespace-all-buffer-files
741 (delete (list buffile bufname) whitespace-all-buffer-files))
742 (when whitespace-rescan-timer
743 (disable-timeout whitespace-rescan-timer)
744 (setq whitespace-rescan-timer nil))))
746 ;;;###autoload
747 (defalias 'global-whitespace-mode 'whitespace-global-mode)
749 ;;;###autoload
750 (define-minor-mode whitespace-global-mode
751 "Toggle using Whitespace mode in new buffers.
752 With ARG, turn the mode on if ARG is positive, otherwise turn it off.
754 When this mode is active, `whitespace-buffer' is added to
755 `find-file-hook' and `kill-buffer-hook'."
756 :global t
757 :group 'whitespace
758 (if whitespace-global-mode
759 (progn
760 (add-hook 'find-file-hook 'whitespace-buffer)
761 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
762 (add-hook 'kill-buffer-hook 'whitespace-buffer))
763 (remove-hook 'find-file-hook 'whitespace-buffer)
764 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
765 (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
767 ;;;###autoload
768 (defun whitespace-write-file-hook ()
769 "Hook function to be called on the buffer when whitespace check is enabled.
770 This is meant to be added buffer-locally to `write-file-functions'."
771 (let ((werr nil))
772 (if whitespace-auto-cleanup
773 (whitespace-cleanup-internal)
774 (setq werr (whitespace-buffer)))
775 (if (and whitespace-abort-on-error werr)
776 (error "Abort write due to whitespaces in %s"
777 buffer-file-name)))
778 nil)
780 (defun whitespace-unload-function ()
781 "Unload the whitespace library."
782 (if (unintern "whitespace-unload-hook" obarray)
783 ;; if whitespace-unload-hook is defined, let's get rid of it
784 ;; and recursively call `unload-feature'
785 (progn (unload-feature 'whitespace) t)
786 ;; this only happens in the recursive call
787 (whitespace-global-mode -1)
788 (save-current-buffer
789 (dolist (buf (buffer-list))
790 (set-buffer buf)
791 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)))
792 ;; continue standard unloading
793 nil))
795 (defun whitespace-unload-hook ()
796 (remove-hook 'find-file-hook 'whitespace-buffer)
797 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
798 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
800 (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
802 (provide 'whitespace)
804 ;;; whitespace.el ends here