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