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