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