(gdb-frame-address, gdb-var-changed, gdb-output-sink, gdba,
[emacs.git] / lisp / whitespace.el
blobac31a4d820b7f4520feb24aaa9e1efb54543e995
1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
3 ;; Copyright (C) 1999, 2000, 2005 Free Software Foundation, Inc.
5 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
6 ;; Keywords: convenience
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; URL: http://www.dsmit.com/lisp/
29 ;; The whitespace library is intended to find and help fix five different types
30 ;; of whitespace problems that commonly exist in source code.
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.
39 ;; Whitespace errors are reported in a buffer, and on the modeline.
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:
44 ;; e - End-of-Line whitespace.
45 ;; i - Indentation whitespace.
46 ;; l - Leading whitespace.
47 ;; s - Space followed by Tab.
48 ;; t - Trailing whitespace.
50 ;; If any of the whitespace checks is turned off, the modeline will display a
51 ;; !<y>.
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.)
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.
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.)
71 ;; All the above have caused (and will cause) unwanted codeline integration and
72 ;; merge problems.
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).
78 ;; Exported functions:
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.
87 ;;; Code:
89 (defvar whitespace-version "3.5" "Version of the whitespace library.")
91 (defvar whitespace-all-buffer-files nil
92 "An associated list of buffers and files checked for whitespace cleanliness.
94 This is to enable periodic checking of whitespace cleanliness in the files
95 visited by the buffers.")
97 (defvar whitespace-rescan-timer nil
98 "Timer object used to rescan the files in buffers that have been modified.")
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)
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)
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)
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)
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)
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)
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)
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)
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."
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))))
153 (defalias 'whitespace-make-overlay
154 (if (featurep 'xemacs) 'make-extent 'make-overlay))
155 (defalias 'whitespace-overlay-put
156 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
157 (defalias 'whitespace-delete-overlay
158 (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
159 (defalias 'whitespace-overlay-start
160 (if (featurep 'xemacs) 'extent-start 'overlay-start))
161 (defalias 'whitespace-overlay-end
162 (if (featurep 'xemacs) 'extent-end 'overlay-end))
163 (defalias 'whitespace-mode-line-update
164 (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
166 (defgroup whitespace nil
167 "Check for and fix five different types of whitespaces in source code."
168 :version "21.1"
169 :link '(emacs-commentary-link "whitespace.el")
170 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
171 ;; which is 'editing?
172 :group (if (featurep 'xemacs) 'editing 'convenience))
174 (defcustom whitespace-check-leading-whitespace t
175 "Flag to check leading whitespace. This is the global for the system.
176 It can be overriden by setting a buffer local variable
177 `whitespace-check-buffer-leading'."
178 :type 'boolean
179 :group 'whitespace)
181 (defcustom whitespace-check-trailing-whitespace t
182 "Flag to check trailing whitespace. This is the global for the system.
183 It can be overriden by setting a buffer local variable
184 `whitespace-check-buffer-trailing'."
185 :type 'boolean
186 :group 'whitespace)
188 (defcustom whitespace-check-spacetab-whitespace t
189 "Flag to check space followed by a TAB. This is the global for the system.
190 It can be overriden by setting a buffer local variable
191 `whitespace-check-buffer-spacetab'."
192 :type 'boolean
193 :group 'whitespace)
195 (defcustom whitespace-spacetab-regexp "[ ]+\t"
196 "Regexp to match a space followed by a TAB."
197 :type 'regexp
198 :group 'whitespace)
200 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
201 "Flag to check indentation whitespace. This is the global for the system.
202 It can be overriden by setting a buffer local variable
203 `whitespace-check-buffer-indent'."
204 :type 'boolean
205 :group 'whitespace)
207 (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ")
208 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
209 :type 'regexp
210 :group 'whitespace)
212 (defcustom whitespace-check-ateol-whitespace t
213 "Flag to check end-of-line whitespace. This is the global for the system.
214 It can be overriden by setting a buffer local variable
215 `whitespace-check-buffer-ateol'."
216 :type 'boolean
217 :group 'whitespace)
219 ;; (defcustom whitespace-ateol-regexp "[ \t]$"
220 (defcustom whitespace-ateol-regexp "[ \t]+$"
221 "Regexp to match a TAB or a space at the EOL."
222 :type 'regexp
223 :group 'whitespace)
225 (defcustom whitespace-errbuf "*Whitespace Errors*"
226 "The name of the buffer where whitespace related messages will be logged."
227 :type 'string
228 :group 'whitespace)
230 (defcustom whitespace-clean-msg "clean."
231 "If non-nil, this message will be displayed after a whitespace check
232 determines a file to be clean."
233 :type 'string
234 :group 'whitespace)
236 (defcustom whitespace-abort-on-error nil
237 "While writing a file, abort if the file is unclean.
238 If `whitespace-auto-cleanup' is set, that takes precedence over
239 this variable."
240 :type 'boolean
241 :group 'whitespace)
243 (defcustom whitespace-auto-cleanup nil
244 "Cleanup a buffer automatically on finding it whitespace unclean."
245 :type 'boolean
246 :group 'whitespace)
248 (defcustom whitespace-silent nil
249 "All whitespace errors will be shown only in the modeline when t.
251 Note that setting this may cause all whitespaces introduced in a file to go
252 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
253 Errors*' buffer before opening (or closing) another file."
254 :type 'boolean
255 :group 'whitespace)
257 (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
258 c-mode c++-mode cc-mode
259 change-log-mode cperl-mode
260 electric-nroff-mode emacs-lisp-mode
261 f90-mode fortran-mode html-mode
262 html3-mode java-mode jde-mode
263 ksh-mode latex-mode LaTeX-mode
264 lisp-mode m4-mode makefile-mode
265 modula-2-mode nroff-mode objc-mode
266 pascal-mode perl-mode prolog-mode
267 python-mode scheme-mode sgml-mode
268 sh-mode shell-script-mode simula-mode
269 tcl-mode tex-mode texinfo-mode
270 vrml-mode xml-mode)
272 "Major modes in which we turn on whitespace checking.
274 These are mostly programming and documentation modes. But you may add other
275 modes that you want whitespaces checked in by adding something like the
276 following to your `.emacs':
278 \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
279 whitespace-modes))\)
281 Or, alternately, you can use the Emacs `customize' command to set this."
282 :type '(repeat symbol)
283 :group 'whitespace)
285 (defcustom whitespace-rescan-timer-time 600
286 "Period in seconds to rescan modified buffers for whitespace creep.
288 This is the period after which the timer will fire causing
289 `whitespace-rescan-files-in-buffers' to check for whitespace creep in
290 modified buffers.
292 To disable timer scans, set this to zero."
293 :type 'integer
294 :group 'whitespace)
296 (defcustom whitespace-display-in-modeline t
297 "Display whitespace errors on the modeline."
298 :type 'boolean
299 :group 'whitespace)
301 (defcustom whitespace-display-spaces-in-color t
302 "Display the bogus whitespaces by coloring them with the face
303 `whitespace-highlight'."
304 :type 'boolean
305 :group 'whitespace)
307 (defgroup whitespace-faces nil
308 "Faces used in whitespace."
309 :prefix "whitespace-"
310 :group 'whitespace
311 :group 'faces)
313 (defface whitespace-highlight '((((class color) (background light))
314 (:background "green1"))
315 (((class color) (background dark))
316 (:background "sea green"))
317 (((class grayscale mono)
318 (background light))
319 (:background "black"))
320 (((class grayscale mono)
321 (background dark))
322 (:background "white")))
323 "Face used for highlighting the bogus whitespaces that exist in the buffer."
324 :group 'whitespace-faces)
325 ;; backward-compatibility alias
326 (put 'whitespace-highlight-face 'face-alias 'whitespace-highlight)
328 (if (not (assoc 'whitespace-mode minor-mode-alist))
329 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
330 minor-mode-alist)))
332 (set-default 'whitespace-check-buffer-leading
333 whitespace-check-leading-whitespace)
334 (set-default 'whitespace-check-buffer-trailing
335 whitespace-check-trailing-whitespace)
336 (set-default 'whitespace-check-buffer-indent
337 whitespace-check-indent-whitespace)
338 (set-default 'whitespace-check-buffer-spacetab
339 whitespace-check-spacetab-whitespace)
340 (set-default 'whitespace-check-buffer-ateol
341 whitespace-check-ateol-whitespace)
343 (defun whitespace-check-whitespace-mode (&optional arg)
344 "Test and set the whitespace-mode in qualifying buffers."
345 (if (null whitespace-mode)
346 (setq whitespace-mode
347 (if (or arg (member major-mode whitespace-modes))
349 nil))))
351 ;;;###autoload
352 (defun whitespace-toggle-leading-check ()
353 "Toggle the check for leading space in the local buffer."
354 (interactive)
355 (let ((current-val whitespace-check-buffer-leading))
356 (setq whitespace-check-buffer-leading (not current-val))
357 (message "Will%s check for leading space in buffer."
358 (if whitespace-check-buffer-leading "" " not"))
359 (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
361 ;;;###autoload
362 (defun whitespace-toggle-trailing-check ()
363 "Toggle the check for trailing space in the local buffer."
364 (interactive)
365 (let ((current-val whitespace-check-buffer-trailing))
366 (setq whitespace-check-buffer-trailing (not current-val))
367 (message "Will%s check for trailing space in buffer."
368 (if whitespace-check-buffer-trailing "" " not"))
369 (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
371 ;;;###autoload
372 (defun whitespace-toggle-indent-check ()
373 "Toggle the check for indentation space in the local buffer."
374 (interactive)
375 (let ((current-val whitespace-check-buffer-indent))
376 (setq whitespace-check-buffer-indent (not current-val))
377 (message "Will%s check for indentation space in buffer."
378 (if whitespace-check-buffer-indent "" " not"))
379 (if whitespace-check-buffer-indent
380 (whitespace-buffer-search whitespace-indent-regexp))))
382 ;;;###autoload
383 (defun whitespace-toggle-spacetab-check ()
384 "Toggle the check for space-followed-by-TABs in the local buffer."
385 (interactive)
386 (let ((current-val whitespace-check-buffer-spacetab))
387 (setq whitespace-check-buffer-spacetab (not current-val))
388 (message "Will%s check for space-followed-by-TABs in buffer."
389 (if whitespace-check-buffer-spacetab "" " not"))
390 (if whitespace-check-buffer-spacetab
391 (whitespace-buffer-search whitespace-spacetab-regexp))))
394 ;;;###autoload
395 (defun whitespace-toggle-ateol-check ()
396 "Toggle the check for end-of-line space in the local buffer."
397 (interactive)
398 (let ((current-val whitespace-check-buffer-ateol))
399 (setq whitespace-check-buffer-ateol (not current-val))
400 (message "Will%s check for end-of-line space in buffer."
401 (if whitespace-check-buffer-ateol "" " not"))
402 (if whitespace-check-buffer-ateol
403 (whitespace-buffer-search whitespace-ateol-regexp))))
406 ;;;###autoload
407 (defun whitespace-buffer (&optional quiet)
408 "Find five different types of white spaces in buffer.
409 These are:
410 1. Leading space \(empty lines at the top of a file\).
411 2. Trailing space \(empty lines at the end of a file\).
412 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
413 4. Spaces followed by a TAB. \(Almost always, we never want that\).
414 5. Spaces or TABS at the end of a line.
416 Check for whitespace only if this buffer really contains a non-empty file
417 and:
418 1. the major mode is one of the whitespace-modes, or
419 2. `whitespace-buffer' was explicitly called with a prefix argument."
420 (interactive)
421 (let ((whitespace-error nil))
422 (whitespace-check-whitespace-mode current-prefix-arg)
423 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
424 (progn
425 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
426 (whitespace-tickle-timer)
427 (whitespace-unhighlight-the-space)
428 (if whitespace-auto-cleanup
429 (if buffer-read-only
430 (if (not quiet)
431 (message "Can't cleanup: %s is read-only" (buffer-name)))
432 (whitespace-cleanup))
433 (let ((whitespace-leading (if whitespace-check-buffer-leading
434 (whitespace-buffer-leading)
435 nil))
436 (whitespace-trailing (if whitespace-check-buffer-trailing
437 (whitespace-buffer-trailing)
438 nil))
439 (whitespace-indent (if whitespace-check-buffer-indent
440 (whitespace-buffer-search
441 whitespace-indent-regexp)
442 nil))
443 (whitespace-spacetab (if whitespace-check-buffer-spacetab
444 (whitespace-buffer-search
445 whitespace-spacetab-regexp)
446 nil))
447 (whitespace-ateol (if whitespace-check-buffer-ateol
448 (whitespace-buffer-search
449 whitespace-ateol-regexp)
450 nil))
451 (whitespace-errmsg nil)
452 (whitespace-filename buffer-file-name)
453 (whitespace-this-modeline ""))
455 ;; Now let's complain if we found any of the above.
456 (setq whitespace-error (or whitespace-leading whitespace-indent
457 whitespace-spacetab whitespace-ateol
458 whitespace-trailing))
460 (if whitespace-error
461 (progn
462 (setq whitespace-errmsg
463 (concat whitespace-filename " contains:\n"
464 (if whitespace-leading
465 "Leading whitespace\n")
466 (if whitespace-indent
467 (concat "Indentation whitespace"
468 whitespace-indent "\n"))
469 (if whitespace-spacetab
470 (concat "Space followed by Tab"
471 whitespace-spacetab "\n"))
472 (if whitespace-ateol
473 (concat "End-of-line whitespace"
474 whitespace-ateol "\n"))
475 (if whitespace-trailing
476 "Trailing whitespace\n")
477 "\ntype `M-x whitespace-cleanup' to "
478 "cleanup the file."))
479 (setq whitespace-this-modeline
480 (concat (if whitespace-ateol "e")
481 (if whitespace-indent "i")
482 (if whitespace-leading "l")
483 (if whitespace-spacetab "s")
484 (if whitespace-trailing "t")))))
485 (whitespace-update-modeline whitespace-this-modeline)
486 (if (get-buffer whitespace-errbuf)
487 (kill-buffer whitespace-errbuf))
488 (with-current-buffer (get-buffer-create whitespace-errbuf)
489 (if whitespace-errmsg
490 (progn
491 (insert whitespace-errmsg)
492 (if (not (or quiet whitespace-silent))
493 (display-buffer (current-buffer) t))
494 (if (not quiet)
495 (message "Whitespaces: [%s%s] in %s"
496 whitespace-this-modeline
497 (let ((whitespace-unchecked
498 (whitespace-unchecked-whitespaces)))
499 (if whitespace-unchecked
500 (concat "!" whitespace-unchecked)
501 ""))
502 whitespace-filename)))
503 (if (and (not quiet) (not (equal whitespace-clean-msg "")))
504 (message "%s %s" whitespace-filename
505 whitespace-clean-msg))))))))
506 whitespace-error))
508 ;;;###autoload
509 (defun whitespace-region (s e)
510 "Check the region for whitespace errors."
511 (interactive "r")
512 (save-excursion
513 (save-restriction
514 (narrow-to-region s e)
515 (whitespace-buffer))))
517 ;;;###autoload
518 (defun whitespace-cleanup ()
519 "Cleanup the five different kinds of whitespace problems.
521 Use \\[describe-function] whitespace-describe to read a summary of the
522 whitespace problems."
523 (interactive)
524 ;; If this buffer really contains a file, then run, else quit.
525 (whitespace-check-whitespace-mode current-prefix-arg)
526 (if (and buffer-file-name whitespace-mode)
527 (let ((whitespace-any nil)
528 (whitespace-tabwith 8)
529 (whitespace-tabwith-saved tab-width))
531 ;; since all printable TABS should be 8, irrespective of how
532 ;; they are displayed.
533 (setq tab-width whitespace-tabwith)
535 (if (and whitespace-check-buffer-leading
536 (whitespace-buffer-leading))
537 (progn
538 (whitespace-buffer-leading-cleanup)
539 (setq whitespace-any t)))
541 (if (and whitespace-check-buffer-trailing
542 (whitespace-buffer-trailing))
543 (progn
544 (whitespace-buffer-trailing-cleanup)
545 (setq whitespace-any t)))
547 (if (and whitespace-check-buffer-indent
548 (whitespace-buffer-search whitespace-indent-regexp))
549 (progn
550 (whitespace-indent-cleanup)
551 (setq whitespace-any t)))
553 (if (and whitespace-check-buffer-spacetab
554 (whitespace-buffer-search whitespace-spacetab-regexp))
555 (progn
556 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
557 (setq whitespace-any t)))
559 (if (and whitespace-check-buffer-ateol
560 (whitespace-buffer-search whitespace-ateol-regexp))
561 (progn
562 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
563 (setq whitespace-any t)))
565 ;; Call this recursively till everything is taken care of
566 (if whitespace-any
567 (whitespace-cleanup)
568 (progn
569 (if (not whitespace-silent)
570 (message "%s clean" buffer-file-name))
571 (whitespace-update-modeline)))
572 (setq tab-width whitespace-tabwith-saved))))
574 ;;;###autoload
575 (defun whitespace-cleanup-region (s e)
576 "Whitespace cleanup on the region."
577 (interactive "r")
578 (save-excursion
579 (save-restriction
580 (narrow-to-region s e)
581 (whitespace-cleanup))
582 (whitespace-buffer t)))
584 (defun whitespace-buffer-leading ()
585 "Check to see if there are any empty lines at the top of the file."
586 (save-excursion
587 (let ((pmin nil)
588 (pmax nil))
589 (goto-char (point-min))
590 (beginning-of-line)
591 (setq pmin (point))
592 (end-of-line)
593 (setq pmax (point))
594 (if (equal pmin pmax)
595 (progn
596 (whitespace-highlight-the-space pmin (1+ pmax))
598 nil))))
600 (defun whitespace-buffer-leading-cleanup ()
601 "Remove any empty lines at the top of the file."
602 (save-excursion
603 (goto-char (point-min))
604 (skip-chars-forward "\n")
605 (delete-region (point-min) (point))))
607 (defun whitespace-buffer-trailing ()
608 "Check to see if are is more than one empty line at the bottom."
609 (save-excursion
610 (let ((pmin nil)
611 (pmax nil))
612 (goto-char (point-max))
613 (beginning-of-line)
614 (setq pmin (point))
615 (end-of-line)
616 (setq pmax (point))
617 (if (equal pmin pmax)
618 (progn
619 (goto-char (- (point) 1))
620 (beginning-of-line)
621 (setq pmin (point))
622 (end-of-line)
623 (setq pmax (point))
624 (if (equal pmin pmax)
625 (progn
626 (whitespace-highlight-the-space (- pmin 1) pmax)
628 nil))
629 nil))))
631 (defun whitespace-buffer-trailing-cleanup ()
632 "Delete all the empty lines at the bottom."
633 (save-excursion
634 (goto-char (point-max))
635 (skip-chars-backward "\n")
636 (if (not (bolp))
637 (forward-char 1))
638 (delete-region (point) (point-max))))
640 (defun whitespace-buffer-search (regexp)
641 "Search for any given whitespace REGEXP."
642 (let ((whitespace-retval ""))
643 (save-excursion
644 (goto-char (point-min))
645 (while (re-search-forward regexp nil t)
646 (progn
647 (setq whitespace-retval (format "%s %s" whitespace-retval
648 (match-beginning 0)))
649 (whitespace-highlight-the-space (match-beginning 0) (match-end 0))))
650 (if (equal "" whitespace-retval)
652 whitespace-retval))))
654 (defun whitespace-buffer-cleanup (regexp newregexp)
655 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
656 (save-excursion
657 (goto-char (point-min))
658 (while (re-search-forward regexp nil t)
659 (replace-match newregexp))))
661 (defun whitespace-indent-cleanup ()
662 "Search for 8/more spaces at the start of a line and replace it with tabs."
663 (save-excursion
664 (goto-char (point-min))
665 (while (re-search-forward whitespace-indent-regexp nil t)
666 (let ((column (current-column))
667 (indent-tabs-mode t))
668 (delete-region (match-beginning 0) (point))
669 (indent-to column)))))
671 (defun whitespace-unchecked-whitespaces ()
672 "Return the list of whitespaces whose testing has been suppressed."
673 (let ((unchecked-spaces
674 (concat (if (not whitespace-check-buffer-ateol) "e")
675 (if (not whitespace-check-buffer-indent) "i")
676 (if (not whitespace-check-buffer-leading) "l")
677 (if (not whitespace-check-buffer-spacetab) "s")
678 (if (not whitespace-check-buffer-trailing) "t"))))
679 (if (not (equal unchecked-spaces ""))
680 unchecked-spaces
681 nil)))
683 (defun whitespace-update-modeline (&optional whitespace-err)
684 "Update modeline with whitespace errors.
685 Also with whitespaces whose testing has been turned off."
686 (if whitespace-display-in-modeline
687 (progn
688 (setq whitespace-mode-line nil)
689 ;; Whitespace errors
690 (if (and whitespace-err (not (equal whitespace-err "")))
691 (setq whitespace-mode-line whitespace-err))
692 ;; Whitespace suppressed errors
693 (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
694 (if whitespace-unchecked
695 (setq whitespace-mode-line
696 (concat whitespace-mode-line "!" whitespace-unchecked))))
697 ;; Add the whitespace modeline prefix
698 (setq whitespace-mode-line (if whitespace-mode-line
699 (concat " W:" whitespace-mode-line)
700 nil))
701 (whitespace-mode-line-update))))
703 (defun whitespace-highlight-the-space (b e)
704 "Highlight the current line, unhighlighting a previously jumped to line."
705 (if whitespace-display-spaces-in-color
706 (let ((ol (whitespace-make-overlay b e)))
707 (push ol whitespace-highlighted-space)
708 (whitespace-overlay-put ol 'face 'whitespace-highlight))))
709 ;; (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space))
711 (defun whitespace-unhighlight-the-space()
712 "Unhighlight the currently highlight line."
713 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
714 (progn
715 (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
716 (setq whitespace-highlighted-space nil))
717 (remove-hook 'pre-command-hook 'whitespace-unhighlight-the-space)))
719 (defun whitespace-check-buffer-list (buf-name buf-file)
720 "Add a buffer and its file to the whitespace monitor list.
722 The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
723 periodically for whitespace."
724 (if (and whitespace-mode (not (member (list buf-file buf-name)
725 whitespace-all-buffer-files)))
726 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
728 (defun whitespace-tickle-timer ()
729 "Tickle timer to periodically to scan qualifying files for whitespace creep.
731 If timer is not set, then set it to scan the files in
732 `whitespace-all-buffer-files' periodically (defined by
733 `whitespace-rescan-timer-time') for whitespace creep."
734 (if (and whitespace-rescan-timer-time (not whitespace-rescan-timer))
735 (setq whitespace-rescan-timer
736 (add-timeout whitespace-rescan-timer-time
737 'whitespace-rescan-files-in-buffers nil
738 whitespace-rescan-timer-time))))
740 (defun whitespace-rescan-files-in-buffers (&optional arg)
741 "Check monitored files for whitespace creep since last scan."
742 (let ((whitespace-all-my-files whitespace-all-buffer-files)
743 buffile bufname thiselt buf)
744 (if (not whitespace-all-my-files)
745 (progn
746 (disable-timeout whitespace-rescan-timer)
747 (setq whitespace-rescan-timer nil))
748 (while whitespace-all-my-files
749 (setq thiselt (car whitespace-all-my-files))
750 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
751 (setq buffile (car thiselt))
752 (setq bufname (cadr thiselt))
753 (setq buf (get-buffer bufname))
754 (if (buffer-live-p buf)
755 (save-excursion
756 ;;(message "buffer %s live" bufname)
757 (set-buffer bufname)
758 (if whitespace-mode
759 (progn
760 ;;(message "checking for whitespace in %s" bufname)
761 (if whitespace-auto-cleanup
762 (progn
763 ;;(message "cleaning up whitespace in %s" bufname)
764 (whitespace-cleanup))
765 (progn
766 ;;(message "whitespace-buffer %s." (buffer-name))
767 (whitespace-buffer t))))
768 ;;(message "Removing %s from refresh list" bufname)
769 (whitespace-refresh-rescan-list buffile bufname)))
770 ;;(message "Removing %s from refresh list" bufname)
771 (whitespace-refresh-rescan-list buffile bufname))))))
773 (defun whitespace-refresh-rescan-list (buffile bufname)
774 "Refresh the list of files to be rescaned for whitespace creep."
775 (if whitespace-all-buffer-files
776 (setq whitespace-all-buffer-files
777 (delete (list buffile bufname) whitespace-all-buffer-files))
778 (when whitespace-rescan-timer
779 (disable-timeout whitespace-rescan-timer)
780 (setq whitespace-rescan-timer nil))))
782 ;;;###autoload
783 (defalias 'global-whitespace-mode 'whitespace-global-mode)
785 ;;;###autoload
786 (define-minor-mode whitespace-global-mode
787 "Toggle using Whitespace mode in new buffers.
788 With ARG, turn the mode on iff ARG is positive.
790 When this mode is active, `whitespace-buffer' is added to
791 `find-file-hook' and `kill-buffer-hook'."
792 :global t
793 :group 'whitespace
794 (if whitespace-global-mode
795 (progn
796 (add-hook 'find-file-hook 'whitespace-buffer)
797 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
798 (add-hook 'kill-buffer-hook 'whitespace-buffer))
799 (remove-hook 'find-file-hook 'whitespace-buffer)
800 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
801 (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
803 ;;;###autoload
804 (defun whitespace-write-file-hook ()
805 "Hook function to be called on the buffer when whitespace check is enabled.
806 This is meant to be added buffer-locally to `write-file-functions'."
807 (interactive)
808 (let ((werr nil))
809 (if whitespace-auto-cleanup
810 (whitespace-cleanup)
811 (setq werr (whitespace-buffer)))
812 (if (and whitespace-abort-on-error werr)
813 (error (concat "Abort write due to whitespaces in "
814 buffer-file-name))))
815 nil)
817 (defun whitespace-unload-hook ()
818 (remove-hook 'find-file-hook 'whitespace-buffer)
819 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
820 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
822 (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
824 (provide 'whitespace)
826 ;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
827 ;;; whitespace.el ends here