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