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