Fix the MSDOS build due to recent changes.
[emacs.git] / lisp / obsolete / old-whitespace.el
blobc33794f668d0c803897573e998d6a49808b3fbf1
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
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 modeline.
40 ;; Modeline 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 modeline 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 modeline 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 (defgroup whitespace-faces nil
292 "Faces used in whitespace."
293 :prefix "whitespace-"
294 :group 'whitespace
295 :group 'faces)
297 (defface whitespace-highlight '((((class color) (background light))
298 (:background "green1"))
299 (((class color) (background dark))
300 (:background "sea green"))
301 (((class grayscale mono)
302 (background light))
303 (:background "black"))
304 (((class grayscale mono)
305 (background dark))
306 (:background "white")))
307 "Face used for highlighting the bogus whitespaces that exist in the buffer."
308 :group 'whitespace-faces)
309 (define-obsolete-face-alias 'whitespace-highlight-face
310 'whitespace-highlight "22.1")
312 (if (not (assoc 'whitespace-mode minor-mode-alist))
313 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
314 minor-mode-alist)))
316 (set-default 'whitespace-check-buffer-leading
317 whitespace-check-leading-whitespace)
318 (set-default 'whitespace-check-buffer-trailing
319 whitespace-check-trailing-whitespace)
320 (set-default 'whitespace-check-buffer-indent
321 whitespace-check-indent-whitespace)
322 (set-default 'whitespace-check-buffer-spacetab
323 whitespace-check-spacetab-whitespace)
324 (set-default 'whitespace-check-buffer-ateol
325 whitespace-check-ateol-whitespace)
327 (defun whitespace-check-whitespace-mode (&optional arg)
328 "Test and set the whitespace-mode in qualifying buffers."
329 (if (null whitespace-mode)
330 (setq whitespace-mode
331 (if (or arg (member major-mode whitespace-modes))
333 nil))))
335 ;;;###autoload
336 (defun whitespace-toggle-leading-check ()
337 "Toggle the check for leading space in the local buffer."
338 (interactive)
339 (let ((current-val whitespace-check-buffer-leading))
340 (setq whitespace-check-buffer-leading (not current-val))
341 (message "Will%s check for leading space in buffer."
342 (if whitespace-check-buffer-leading "" " not"))
343 (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
345 ;;;###autoload
346 (defun whitespace-toggle-trailing-check ()
347 "Toggle the check for trailing space in the local buffer."
348 (interactive)
349 (let ((current-val whitespace-check-buffer-trailing))
350 (setq whitespace-check-buffer-trailing (not current-val))
351 (message "Will%s check for trailing space in buffer."
352 (if whitespace-check-buffer-trailing "" " not"))
353 (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
355 ;;;###autoload
356 (defun whitespace-toggle-indent-check ()
357 "Toggle the check for indentation space in the local buffer."
358 (interactive)
359 (let ((current-val whitespace-check-buffer-indent))
360 (setq whitespace-check-buffer-indent (not current-val))
361 (message "Will%s check for indentation space in buffer."
362 (if whitespace-check-buffer-indent "" " not"))
363 (if whitespace-check-buffer-indent
364 (whitespace-buffer-search whitespace-indent-regexp))))
366 ;;;###autoload
367 (defun whitespace-toggle-spacetab-check ()
368 "Toggle the check for space-followed-by-TABs in the local buffer."
369 (interactive)
370 (let ((current-val whitespace-check-buffer-spacetab))
371 (setq whitespace-check-buffer-spacetab (not current-val))
372 (message "Will%s check for space-followed-by-TABs in buffer."
373 (if whitespace-check-buffer-spacetab "" " not"))
374 (if whitespace-check-buffer-spacetab
375 (whitespace-buffer-search whitespace-spacetab-regexp))))
378 ;;;###autoload
379 (defun whitespace-toggle-ateol-check ()
380 "Toggle the check for end-of-line space in the local buffer."
381 (interactive)
382 (let ((current-val whitespace-check-buffer-ateol))
383 (setq whitespace-check-buffer-ateol (not current-val))
384 (message "Will%s check for end-of-line space in buffer."
385 (if whitespace-check-buffer-ateol "" " not"))
386 (if whitespace-check-buffer-ateol
387 (whitespace-buffer-search whitespace-ateol-regexp))))
390 ;;;###autoload
391 (defun whitespace-buffer (&optional quiet)
392 "Find five different types of white spaces in buffer.
393 These are:
394 1. Leading space \(empty lines at the top of a file\).
395 2. Trailing space \(empty lines at the end of a file\).
396 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
397 4. Spaces followed by a TAB. \(Almost always, we never want that\).
398 5. Spaces or TABS at the end of a line.
400 Check for whitespace only if this buffer really contains a non-empty file
401 and:
402 1. the major mode is one of the whitespace-modes, or
403 2. `whitespace-buffer' was explicitly called with a prefix argument."
404 (interactive)
405 (let ((whitespace-error nil))
406 (whitespace-check-whitespace-mode current-prefix-arg)
407 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
408 (progn
409 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
410 (whitespace-tickle-timer)
411 (overlay-recenter (point-max))
412 (remove-overlays nil nil 'face 'whitespace-highlight)
413 (if whitespace-auto-cleanup
414 (if buffer-read-only
415 (if (not quiet)
416 (message "Can't cleanup: %s is read-only" (buffer-name)))
417 (whitespace-cleanup-internal))
418 (let ((whitespace-leading (if whitespace-check-buffer-leading
419 (whitespace-buffer-leading)
420 nil))
421 (whitespace-trailing (if whitespace-check-buffer-trailing
422 (whitespace-buffer-trailing)
423 nil))
424 (whitespace-indent (if whitespace-check-buffer-indent
425 (whitespace-buffer-search
426 whitespace-indent-regexp)
427 nil))
428 (whitespace-spacetab (if whitespace-check-buffer-spacetab
429 (whitespace-buffer-search
430 whitespace-spacetab-regexp)
431 nil))
432 (whitespace-ateol (if whitespace-check-buffer-ateol
433 (whitespace-buffer-search
434 whitespace-ateol-regexp)
435 nil))
436 (whitespace-errmsg nil)
437 (whitespace-filename buffer-file-name)
438 (whitespace-this-modeline ""))
440 ;; Now let's complain if we found any of the above.
441 (setq whitespace-error (or whitespace-leading whitespace-indent
442 whitespace-spacetab whitespace-ateol
443 whitespace-trailing))
445 (if whitespace-error
446 (progn
447 (setq whitespace-errmsg
448 (concat whitespace-filename " contains:\n"
449 (if whitespace-leading
450 "Leading whitespace\n")
451 (if whitespace-indent
452 (concat "Indentation whitespace"
453 whitespace-indent "\n"))
454 (if whitespace-spacetab
455 (concat "Space followed by Tab"
456 whitespace-spacetab "\n"))
457 (if whitespace-ateol
458 (concat "End-of-line whitespace"
459 whitespace-ateol "\n"))
460 (if whitespace-trailing
461 "Trailing whitespace\n")
462 "\ntype `M-x whitespace-cleanup' to "
463 "cleanup the file."))
464 (setq whitespace-this-modeline
465 (concat (if whitespace-ateol "e")
466 (if whitespace-indent "i")
467 (if whitespace-leading "l")
468 (if whitespace-spacetab "s")
469 (if whitespace-trailing "t")))))
470 (whitespace-update-modeline whitespace-this-modeline)
471 (if (get-buffer whitespace-errbuf)
472 (kill-buffer whitespace-errbuf))
473 (with-current-buffer (get-buffer-create whitespace-errbuf)
474 (if whitespace-errmsg
475 (progn
476 (insert whitespace-errmsg)
477 (if (not (or quiet whitespace-silent))
478 (display-buffer (current-buffer) t))
479 (if (not quiet)
480 (message "Whitespaces: [%s%s] in %s"
481 whitespace-this-modeline
482 (let ((whitespace-unchecked
483 (whitespace-unchecked-whitespaces)))
484 (if whitespace-unchecked
485 (concat "!" whitespace-unchecked)
486 ""))
487 whitespace-filename)))
488 (if (and (not quiet) (not (equal whitespace-clean-msg "")))
489 (message "%s %s" whitespace-filename
490 whitespace-clean-msg))))))))
491 whitespace-error))
493 ;;;###autoload
494 (defun whitespace-region (s e)
495 "Check the region for whitespace errors."
496 (interactive "r")
497 (save-excursion
498 (save-restriction
499 (narrow-to-region s e)
500 (whitespace-buffer))))
502 ;;;###autoload
503 (defun whitespace-cleanup ()
504 "Cleanup the five different kinds of whitespace problems.
505 It normally applies to the whole buffer, but in Transient Mark mode
506 when the mark is active it applies to the region.
507 See `whitespace-buffer' docstring for a summary of the problems."
508 (interactive)
509 (if (and transient-mark-mode mark-active)
510 (whitespace-cleanup-region (region-beginning) (region-end))
511 (whitespace-cleanup-internal)))
513 (defun whitespace-cleanup-internal (&optional region-only)
514 ;; If this buffer really contains a file, then run, else quit.
515 (whitespace-check-whitespace-mode current-prefix-arg)
516 (if (and buffer-file-name whitespace-mode)
517 (let ((whitespace-any nil)
518 (whitespace-tabwith 8)
519 (whitespace-tabwith-saved tab-width))
521 ;; since all printable TABS should be 8, irrespective of how
522 ;; they are displayed.
523 (setq tab-width whitespace-tabwith)
525 (if (and whitespace-check-buffer-leading
526 (whitespace-buffer-leading))
527 (progn
528 (whitespace-buffer-leading-cleanup)
529 (setq whitespace-any t)))
531 (if (and whitespace-check-buffer-trailing
532 (whitespace-buffer-trailing))
533 (progn
534 (whitespace-buffer-trailing-cleanup)
535 (setq whitespace-any t)))
537 (if (and whitespace-check-buffer-indent
538 (whitespace-buffer-search whitespace-indent-regexp))
539 (progn
540 (whitespace-indent-cleanup)
541 (setq whitespace-any t)))
543 (if (and whitespace-check-buffer-spacetab
544 (whitespace-buffer-search whitespace-spacetab-regexp))
545 (progn
546 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
547 (setq whitespace-any t)))
549 (if (and whitespace-check-buffer-ateol
550 (whitespace-buffer-search whitespace-ateol-regexp))
551 (progn
552 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
553 (setq whitespace-any t)))
555 ;; Call this recursively till everything is taken care of
556 (if whitespace-any
557 (whitespace-cleanup-internal region-only)
558 ;; if we are done, talk to the user
559 (progn
560 (unless whitespace-silent
561 (if region-only
562 (message "The region is now clean")
563 (message "%s is now clean" buffer-file-name)))
564 (whitespace-update-modeline)))
565 (setq tab-width whitespace-tabwith-saved))))
567 ;;;###autoload
568 (defun whitespace-cleanup-region (s e)
569 "Whitespace cleanup on the region."
570 (interactive "r")
571 (save-excursion
572 (save-restriction
573 (narrow-to-region s e)
574 (whitespace-cleanup-internal t))
575 (whitespace-buffer t)))
577 (defun whitespace-buffer-leading ()
578 "Return t if the current buffer has leading newline characters.
579 If highlighting is enabled, highlight these characters."
580 (save-excursion
581 (goto-char (point-min))
582 (skip-chars-forward "\n")
583 (unless (bobp)
584 (whitespace-highlight-the-space (point-min) (point))
585 t)))
587 (defun whitespace-buffer-leading-cleanup ()
588 "Remove any leading newline characters from current buffer."
589 (save-excursion
590 (goto-char (point-min))
591 (skip-chars-forward "\n")
592 (delete-region (point-min) (point))))
594 (defun whitespace-buffer-trailing ()
595 "Return t if the current buffer has extra trailing newline characters.
596 If highlighting is enabled, highlight these characters."
597 (save-excursion
598 (goto-char (point-max))
599 (skip-chars-backward "\n")
600 (forward-line)
601 (unless (eobp)
602 (whitespace-highlight-the-space (point) (point-max))
603 t)))
605 (defun whitespace-buffer-trailing-cleanup ()
606 "Remove extra trailing newline characters from current buffer."
607 (save-excursion
608 (goto-char (point-max))
609 (skip-chars-backward "\n")
610 (unless (eobp)
611 (forward-line)
612 (delete-region (point) (point-max)))))
614 (defun whitespace-buffer-search (regexp)
615 "Search for any given whitespace REGEXP."
616 (with-local-quit
617 (let (whitespace-retval)
618 (save-excursion
619 (goto-char (point-min))
620 (while (re-search-forward regexp nil t)
621 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
622 (push (match-beginning 0) whitespace-retval)))
623 (when whitespace-retval
624 (format " %s" (nreverse whitespace-retval))))))
626 (defun whitespace-buffer-cleanup (regexp newregexp)
627 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
628 (save-excursion
629 (goto-char (point-min))
630 (while (re-search-forward regexp nil t)
631 (replace-match newregexp))))
633 (defun whitespace-indent-cleanup ()
634 "Search for 8/more spaces at the start of a line and replace it with tabs."
635 (save-excursion
636 (goto-char (point-min))
637 (while (re-search-forward whitespace-indent-regexp nil t)
638 (let ((column (current-column))
639 (indent-tabs-mode t))
640 (delete-region (match-beginning 0) (point))
641 (indent-to column)))))
643 (defun whitespace-unchecked-whitespaces ()
644 "Return the list of whitespaces whose testing has been suppressed."
645 (let ((unchecked-spaces
646 (concat (if (not whitespace-check-buffer-ateol) "e")
647 (if (not whitespace-check-buffer-indent) "i")
648 (if (not whitespace-check-buffer-leading) "l")
649 (if (not whitespace-check-buffer-spacetab) "s")
650 (if (not whitespace-check-buffer-trailing) "t"))))
651 (if (not (equal unchecked-spaces ""))
652 unchecked-spaces
653 nil)))
655 (defun whitespace-update-modeline (&optional whitespace-err)
656 "Update modeline with whitespace errors.
657 Also with whitespaces whose testing has been turned off."
658 (if whitespace-display-in-modeline
659 (progn
660 (setq whitespace-mode-line nil)
661 ;; Whitespace errors
662 (if (and whitespace-err (not (equal whitespace-err "")))
663 (setq whitespace-mode-line whitespace-err))
664 ;; Whitespace suppressed errors
665 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
666 (if whitespace-unchecked
667 (setq whitespace-mode-line
668 (concat whitespace-mode-line "!" whitespace-unchecked))))
669 ;; Add the whitespace modeline prefix
670 (setq whitespace-mode-line (if whitespace-mode-line
671 (concat " W:" whitespace-mode-line)
672 nil))
673 (whitespace-mode-line-update))))
675 (defun whitespace-highlight-the-space (b e)
676 "Highlight the current line, unhighlighting a previously jumped to line."
677 (if whitespace-display-spaces-in-color
678 (let ((ol (whitespace-make-overlay b e)))
679 (whitespace-overlay-put ol 'face 'whitespace-highlight))))
681 (defun whitespace-unhighlight-the-space()
682 "Unhighlight the currently highlight line."
683 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
684 (progn
685 (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
686 (setq whitespace-highlighted-space nil))))
688 (defun whitespace-check-buffer-list (buf-name buf-file)
689 "Add a buffer and its file to the whitespace monitor list.
691 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
692 periodically for whitespace."
693 (if (and whitespace-mode (not (member (list buf-file buf-name)
694 whitespace-all-buffer-files)))
695 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
697 (defun whitespace-tickle-timer ()
698 "Tickle timer to periodically to scan qualifying files for whitespace creep.
700 If timer is not set, then set it to scan the files in
701 `whitespace-all-buffer-files' periodically (defined by
702 `whitespace-rescan-timer-time') for whitespace creep."
703 (if (and whitespace-rescan-timer-time
704 (/= whitespace-rescan-timer-time 0)
705 (not whitespace-rescan-timer))
706 (setq whitespace-rescan-timer
707 (add-timeout whitespace-rescan-timer-time
708 'whitespace-rescan-files-in-buffers nil
709 whitespace-rescan-timer-time))))
711 (defun whitespace-rescan-files-in-buffers (&optional arg)
712 "Check monitored files for whitespace creep since last scan."
713 (let ((whitespace-all-my-files whitespace-all-buffer-files)
714 buffile bufname thiselt buf)
715 (if (not whitespace-all-my-files)
716 (progn
717 (disable-timeout whitespace-rescan-timer)
718 (setq whitespace-rescan-timer nil))
719 (while whitespace-all-my-files
720 (setq thiselt (car whitespace-all-my-files))
721 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
722 (setq buffile (car thiselt))
723 (setq bufname (cadr thiselt))
724 (setq buf (get-buffer bufname))
725 (if (buffer-live-p buf)
726 (with-current-buffer bufname
727 ;;(message "buffer %s live" bufname)
728 (if whitespace-mode
729 (progn
730 ;;(message "checking for whitespace in %s" bufname)
731 (if whitespace-auto-cleanup
732 (progn
733 ;;(message "cleaning up whitespace in %s" bufname)
734 (whitespace-cleanup-internal))
735 (progn
736 ;;(message "whitespace-buffer %s." (buffer-name))
737 (whitespace-buffer t))))
738 ;;(message "Removing %s from refresh list" bufname)
739 (whitespace-refresh-rescan-list buffile bufname)))
740 ;;(message "Removing %s from refresh list" bufname)
741 (whitespace-refresh-rescan-list buffile bufname))))))
743 (defun whitespace-refresh-rescan-list (buffile bufname)
744 "Refresh the list of files to be rescanned for whitespace creep."
745 (if whitespace-all-buffer-files
746 (setq whitespace-all-buffer-files
747 (delete (list buffile bufname) whitespace-all-buffer-files))
748 (when whitespace-rescan-timer
749 (disable-timeout whitespace-rescan-timer)
750 (setq whitespace-rescan-timer nil))))
752 ;;;###autoload
753 (defalias 'global-whitespace-mode 'whitespace-global-mode)
755 ;;;###autoload
756 (define-minor-mode whitespace-global-mode
757 "Toggle using Whitespace mode in new buffers.
758 With ARG, turn the mode on if ARG is positive, otherwise turn it off.
760 When this mode is active, `whitespace-buffer' is added to
761 `find-file-hook' and `kill-buffer-hook'."
762 :global t
763 :group 'whitespace
764 (if whitespace-global-mode
765 (progn
766 (add-hook 'find-file-hook 'whitespace-buffer)
767 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
768 (add-hook 'kill-buffer-hook 'whitespace-buffer))
769 (remove-hook 'find-file-hook 'whitespace-buffer)
770 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
771 (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
773 ;;;###autoload
774 (defun whitespace-write-file-hook ()
775 "Hook function to be called on the buffer when whitespace check is enabled.
776 This is meant to be added buffer-locally to `write-file-functions'."
777 (let ((werr nil))
778 (if whitespace-auto-cleanup
779 (whitespace-cleanup-internal)
780 (setq werr (whitespace-buffer)))
781 (if (and whitespace-abort-on-error werr)
782 (error "Abort write due to whitespaces in %s"
783 buffer-file-name)))
784 nil)
786 (defun whitespace-unload-function ()
787 "Unload the whitespace library."
788 (if (unintern "whitespace-unload-hook" obarray)
789 ;; if whitespace-unload-hook is defined, let's get rid of it
790 ;; and recursively call `unload-feature'
791 (progn (unload-feature 'whitespace) t)
792 ;; this only happens in the recursive call
793 (whitespace-global-mode -1)
794 (save-current-buffer
795 (dolist (buf (buffer-list))
796 (set-buffer buf)
797 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)))
798 ;; continue standard unloading
799 nil))
801 (defun whitespace-unload-hook ()
802 (remove-hook 'find-file-hook 'whitespace-buffer)
803 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
804 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
806 (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
808 (provide 'whitespace)
810 ;;; whitespace.el ends here