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