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