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