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