Commit | Line | Data |
---|---|---|
acc975b1 DL |
1 | ;;; whitespace.el --- Warn about and clean bogus whitespaces in the file. |
2 | ||
3 | ;; Copyright (C) 1999 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Rajesh Vaidheeswarran <rv@dsmit.com> | |
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: | |
26 | ||
27 | ;; Whitespace.el URL: http://www.dsmit.com/lisp/ | |
28 | ||
29 | ;; Exported functions: | |
30 | ||
31 | ;; `whitespace-buffer' - To check the current buffer for whitespace problems. | |
32 | ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer. | |
24b72a45 RS |
33 | ;; `whitespace-region' - To check between point and mark for whitespace |
34 | ;; problems. | |
35 | ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point | |
36 | ;; and mark in the current buffer. | |
37 | ;; `whitespace-describe' - A simple introduction to the library. | |
acc975b1 DL |
38 | |
39 | ;;; Code: | |
40 | ||
41 | ;; add a hook to find-file-hooks and kill-buffer-hook | |
42 | (add-hook 'find-file-hooks 'whitespace-buffer) | |
43 | (add-hook 'kill-buffer-hook 'whitespace-buffer) | |
44 | ||
07dc4175 | 45 | (defvar whitespace-version "2.8" "Version of the whitespace library.") |
24b72a45 | 46 | ;; Find out what type of Emacs we are running in. |
acc975b1 DL |
47 | (defvar whitespace-running-emacs (if (string-match "XEmacs\\|Lucid" |
48 | emacs-version) nil t) | |
24b72a45 | 49 | "If this is Emacs, not XEmacs, this is t.") |
acc975b1 | 50 | |
24b72a45 | 51 | (if whitespace-running-emacs (require 'timer)) |
acc975b1 | 52 | |
24b72a45 RS |
53 | (defvar whitespace-all-buffer-files nil |
54 | "An associated list of buffers and files checked for whitespace cleanliness. | |
acc975b1 | 55 | |
24b72a45 RS |
56 | This is to enable periodic checking of whitespace cleanliness in the files |
57 | visited by the buffers.") | |
acc975b1 | 58 | |
24b72a45 RS |
59 | (defvar whitespace-rescan-timer nil |
60 | "Timer object used to rescan the files in buffers that have been modified.") | |
acc975b1 | 61 | |
dd24f431 GM |
62 | ;; Tell Emacs about this new kind of minor mode |
63 | (defvar whitespace-mode nil | |
64 | "Non-nil when Whitespace mode (a minor mode) is enabled.") | |
65 | (make-variable-buffer-local 'whitespace-mode) | |
66 | (put 'whitespace-mode 'permanent-local nil) | |
67 | ||
68 | (defvar whitespace-mode-line nil | |
69 | "String to display in the mode line for Whitespace mode.") | |
70 | (make-variable-buffer-local 'whitespace-mode-line) | |
71 | (put 'whitespace-mode-line 'permanent-local nil) | |
72 | ||
07dc4175 | 73 | ;; For flavors of Emacs which don't define `defgroup' and `defcustom'. |
24b72a45 | 74 | (eval-when-compile |
07dc4175 GM |
75 | (if (not (fboundp 'defgroup)) |
76 | (defmacro defgroup (sym memb doc &rest args) | |
77 | "Null macro for defgroup in all versions of Emacs that don't define | |
78 | defgroup" | |
79 | t)) | |
80 | (if (not (fboundp 'defcustom)) | |
81 | (defmacro defcustom (sym val doc &rest args) | |
82 | "Macro to alias defcustom to defvar in all versions of Emacs that | |
83 | don't define defcustom" | |
84 | `(defvar ,sym ,val ,doc)))) | |
acc975b1 | 85 | |
24b72a45 RS |
86 | (defgroup whitespace nil |
87 | "Check for and fix five different types of whitespaces in source code." | |
acc975b1 DL |
88 | ;; Since XEmacs doesn't have a 'convenience group, use the next best group |
89 | ;; which is 'editing? | |
90 | :group (if whitespace-running-emacs 'convenience 'editing)) | |
91 | ||
18d51459 RS |
92 | (defcustom whitespace-check-leading-whitespace t |
93 | "Flag to check leading whitespace." | |
94 | :type 'boolean | |
95 | :group 'whitespace) | |
96 | ||
97 | (defcustom whitespace-check-trailing-whitespace t | |
98 | "Flag to check trailing whitespace." | |
99 | :type 'boolean | |
100 | :group 'whitespace) | |
101 | ||
102 | (defcustom whitespace-check-spacetab-whitespace t | |
103 | "Flag to check space followed by a TAB." | |
104 | :type 'boolean | |
105 | :group 'whitespace) | |
106 | ||
24b72a45 | 107 | (defcustom whitespace-spacetab-regexp " \t" |
18d51459 | 108 | "Regexp to match a space followed by a TAB." |
24b72a45 RS |
109 | :type 'string |
110 | :group 'whitespace) | |
acc975b1 | 111 | |
18d51459 RS |
112 | (defcustom whitespace-check-indent-whitespace t |
113 | "Flag to check indentation whitespace." | |
114 | :type 'boolean | |
115 | :group 'whitespace) | |
116 | ||
24b72a45 RS |
117 | (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ") |
118 | "Regexp to match (any TABS followed by) 8/more whitespaces at start of line." | |
119 | :type 'string | |
120 | :group 'whitespace) | |
121 | ||
18d51459 RS |
122 | (defcustom whitespace-check-ateol-whitespace t |
123 | "Flag to check end-of-line whitespace." | |
124 | :type 'boolean | |
125 | :group 'whitespace) | |
126 | ||
24b72a45 RS |
127 | (defcustom whitespace-ateol-regexp "[ \t]$" |
128 | "Regexp to match a TAB or a space at the EOL." | |
129 | :type 'string | |
130 | :group 'whitespace) | |
131 | ||
132 | (defcustom whitespace-errbuf "*Whitespace Errors*" | |
133 | "The buffer where whitespace related messages will be logged." | |
134 | :type 'string | |
135 | :group 'whitespace) | |
136 | ||
137 | (defcustom whitespace-auto-cleanup nil | |
138 | "Cleanup a buffer automatically on finding it whitespace unclean." | |
acc975b1 DL |
139 | :type 'boolean |
140 | :group 'whitespace) | |
141 | ||
142 | (defcustom whitespace-silent nil | |
24b72a45 | 143 | "All whitespace errors will be shown only in the modeline when t. |
acc975b1 DL |
144 | |
145 | Note that setting this may cause all whitespaces introduced in a file to go | |
146 | unnoticed when the buffer is killed, unless the user visits the `*Whitespace | |
24b72a45 | 147 | Errors*' buffer before opening (or closing) another file." |
acc975b1 DL |
148 | :type 'boolean |
149 | :group 'whitespace) | |
150 | ||
151 | (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode | |
18d51459 RS |
152 | c-mode c++-mode cc-mode |
153 | change-log-mode cperl-mode | |
acc975b1 DL |
154 | electric-nroff-mode emacs-lisp-mode |
155 | f90-mode fortran-mode html-mode | |
156 | html3-mode java-mode jde-mode | |
157 | ksh-mode latex-mode LaTeX-mode | |
158 | lisp-mode m4-mode makefile-mode | |
159 | modula-2-mode nroff-mode objc-mode | |
160 | pascal-mode perl-mode prolog-mode | |
161 | python-mode scheme-mode sgml-mode | |
18d51459 RS |
162 | sh-mode shell-script-mode simula-mode |
163 | tcl-mode tex-mode texinfo-mode | |
164 | vrml-mode xml-mode) | |
acc975b1 | 165 | |
24b72a45 | 166 | "Major Modes in which we turn on whitespace checking. |
acc975b1 | 167 | |
24b72a45 RS |
168 | These are mostly programming and documentation modes. But you may add other |
169 | modes that you want whitespaces checked in by adding something like the | |
170 | following to your `.emacs': | |
acc975b1 | 171 | |
24b72a45 RS |
172 | \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode |
173 | whitespace-modes))\) | |
acc975b1 | 174 | |
24b72a45 RS |
175 | Or, alternately, you can use the Emacs `customize' command to set this." |
176 | :group 'whitespace) | |
acc975b1 DL |
177 | |
178 | (defcustom whitespace-rescan-timer-time 60 | |
24b72a45 RS |
179 | "Period in seconds to rescan modified buffers for whitespace creep. |
180 | ||
181 | This is the period after which the timer will fire causing | |
182 | `whitespace-rescan-files-in-buffers' to check for whitespace creep in | |
18d51459 RS |
183 | modified buffers. |
184 | ||
185 | To disable timer scans, set this to zero." | |
acc975b1 DL |
186 | :type 'integer |
187 | :group 'whitespace) | |
188 | ||
dd24f431 GM |
189 | (defcustom whitespace-display-in-modeline t |
190 | "Display whitespace errors on the modeline." | |
191 | :type 'boolean | |
192 | :group 'whitespace) | |
acc975b1 DL |
193 | |
194 | (if (not (assoc 'whitespace-mode minor-mode-alist)) | |
195 | (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line) | |
196 | minor-mode-alist))) | |
197 | ||
198 | (defun whitespace-check-whitespace-mode (&optional arg) | |
24b72a45 | 199 | "Test and set the whitespace-mode in qualifying buffers." |
acc975b1 DL |
200 | (if (null whitespace-mode) |
201 | (setq whitespace-mode | |
202 | (if (or arg (member major-mode whitespace-modes)) | |
203 | t | |
204 | nil)))) | |
205 | ||
24b72a45 | 206 | ;;;###autoload |
acc975b1 DL |
207 | (defun whitespace-buffer (&optional quiet) |
208 | "Find five different types of white spaces in buffer: | |
209 | ||
210 | 1. Leading space \(empty lines at the top of a file\). | |
211 | 2. Trailing space \(empty lines at the end of a file\). | |
212 | 3. Indentation space \(8 or more spaces, that should be replaced with TABS\). | |
213 | 4. Spaces followed by a TAB. \(Almost always, we never want that\). | |
214 | 5. Spaces or TABS at the end of a line. | |
215 | ||
216 | Check for whitespace only if this buffer really contains a non-empty file | |
217 | and: | |
218 | 1. the major mode is one of the whitespace-modes, or | |
24b72a45 | 219 | 2. `whitespace-buffer' was explicitly called with a prefix argument." |
acc975b1 | 220 | (interactive) |
dd24f431 GM |
221 | (let ((whitespace-error nil)) |
222 | (whitespace-check-whitespace-mode current-prefix-arg) | |
223 | (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode) | |
224 | (progn | |
225 | (whitespace-check-buffer-list (buffer-name) buffer-file-name) | |
226 | (whitespace-tickle-timer) | |
227 | (if whitespace-auto-cleanup | |
228 | (if buffer-read-only | |
229 | (if (not quiet) | |
230 | (message "Can't cleanup: %s is read-only" (buffer-name))) | |
231 | (whitespace-cleanup)) | |
232 | (let ((whitespace-leading (if whitespace-check-leading-whitespace | |
233 | (whitespace-buffer-leading) | |
234 | nil)) | |
235 | (whitespace-trailing (if whitespace-check-trailing-whitespace | |
236 | (whitespace-buffer-trailing) | |
237 | nil)) | |
238 | (whitespace-indent (if whitespace-check-indent-whitespace | |
18d51459 | 239 | (whitespace-buffer-search |
dd24f431 | 240 | whitespace-indent-regexp) |
18d51459 | 241 | nil)) |
dd24f431 GM |
242 | (whitespace-spacetab (if whitespace-check-spacetab-whitespace |
243 | (whitespace-buffer-search | |
244 | whitespace-spacetab-regexp) | |
245 | nil)) | |
246 | (whitespace-ateol (if whitespace-check-ateol-whitespace | |
247 | (whitespace-buffer-search | |
248 | whitespace-ateol-regexp) | |
249 | nil)) | |
250 | (whitespace-errmsg nil) | |
251 | (whitespace-filename buffer-file-name) | |
252 | (whitespace-this-modeline "")) | |
253 | ||
254 | ;; Now let's complain if we found any of the above. | |
255 | (setq whitespace-error (or whitespace-leading whitespace-indent | |
256 | whitespace-spacetab whitespace-ateol | |
257 | whitespace-trailing)) | |
258 | ||
259 | (if whitespace-error | |
acc975b1 | 260 | (progn |
dd24f431 GM |
261 | (setq whitespace-errmsg |
262 | (concat whitespace-filename " contains:\n" | |
263 | (if whitespace-leading | |
264 | "Leading whitespace\n") | |
265 | (if whitespace-indent | |
266 | (concat "Indentation whitespace" | |
267 | whitespace-indent "\n")) | |
268 | (if whitespace-spacetab | |
269 | (concat "Space followed by Tab" | |
270 | whitespace-spacetab "\n")) | |
271 | (if whitespace-ateol | |
272 | (concat "End-of-line whitespace" | |
273 | whitespace-ateol "\n")) | |
274 | (if whitespace-trailing | |
275 | "Trailing whitespace\n") | |
276 | "\ntype `M-x whitespace-cleanup' to " | |
277 | "cleanup the file.")) | |
278 | (setq whitespace-this-modeline | |
279 | (concat (if whitespace-ateol "e") | |
280 | (if whitespace-indent "i") | |
281 | (if whitespace-leading "l") | |
282 | (if whitespace-spacetab "s") | |
283 | (if whitespace-trailing "t"))))) | |
284 | (whitespace-update-modeline whitespace-this-modeline) | |
285 | (save-excursion | |
286 | (get-buffer-create whitespace-errbuf) | |
287 | (kill-buffer whitespace-errbuf) | |
288 | (get-buffer-create whitespace-errbuf) | |
289 | (set-buffer whitespace-errbuf) | |
290 | (if whitespace-errmsg | |
291 | (progn | |
292 | (insert whitespace-errmsg) | |
293 | (if (not (or quiet whitespace-silent)) | |
294 | (display-buffer whitespace-errbuf t)) | |
295 | (if (not quiet) | |
296 | (message "Whitespaces: [%s%s] in %s" | |
297 | whitespace-this-modeline | |
298 | (let ((whitespace-unchecked | |
299 | (whitespace-unchecked-whitespaces))) | |
300 | (if whitespace-unchecked | |
301 | (concat "!" whitespace-unchecked) | |
302 | "")) | |
303 | whitespace-filename))) | |
304 | (if (not quiet) | |
305 | (message "%s clean" whitespace-filename)))))))) | |
306 | (if whitespace-error | |
307 | t | |
308 | nil))) | |
acc975b1 | 309 | |
24b72a45 | 310 | ;;;###autoload |
acc975b1 | 311 | (defun whitespace-region (s e) |
24b72a45 | 312 | "Check a region specified by point and mark for whitespace errors." |
acc975b1 DL |
313 | (interactive "r") |
314 | (save-excursion | |
315 | (save-restriction | |
316 | (narrow-to-region s e) | |
317 | (whitespace-buffer)))) | |
318 | ||
24b72a45 | 319 | ;;;###autoload |
acc975b1 | 320 | (defun whitespace-cleanup () |
24b72a45 RS |
321 | "Cleanup the five different kinds of whitespace problems. |
322 | ||
323 | Use \\[describe-function] whitespace-describe to read a summary of the | |
324 | whitespace problems." | |
acc975b1 DL |
325 | (interactive) |
326 | ;; If this buffer really contains a file, then run, else quit. | |
327 | (whitespace-check-whitespace-mode current-prefix-arg) | |
328 | (if (and buffer-file-name whitespace-mode) | |
329 | (let ((whitespace-any nil) | |
330 | (whitespace-tabwith 8) | |
331 | (whitespace-tabwith-saved tab-width)) | |
332 | ||
333 | ;; since all printable TABS should be 8, irrespective of how | |
334 | ;; they are displayed. | |
335 | (setq tab-width whitespace-tabwith) | |
336 | ||
18d51459 RS |
337 | (if (and whitespace-check-leading-whitespace |
338 | (whitespace-buffer-leading)) | |
acc975b1 DL |
339 | (progn |
340 | (whitespace-buffer-leading-cleanup) | |
341 | (setq whitespace-any t))) | |
342 | ||
18d51459 RS |
343 | (if (and whitespace-check-trailing-whitespace |
344 | (whitespace-buffer-trailing)) | |
acc975b1 DL |
345 | (progn |
346 | (whitespace-buffer-trailing-cleanup) | |
347 | (setq whitespace-any t))) | |
348 | ||
18d51459 RS |
349 | (if (and whitespace-check-indent-whitespace |
350 | (whitespace-buffer-search whitespace-indent-regexp)) | |
acc975b1 DL |
351 | (progn |
352 | (whitespace-indent-cleanup) | |
353 | (setq whitespace-any t))) | |
354 | ||
18d51459 RS |
355 | (if (and whitespace-check-spacetab-whitespace |
356 | (whitespace-buffer-search whitespace-spacetab-regexp)) | |
acc975b1 DL |
357 | (progn |
358 | (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t") | |
359 | (setq whitespace-any t))) | |
360 | ||
18d51459 RS |
361 | (if (and whitespace-check-ateol-whitespace |
362 | (whitespace-buffer-search whitespace-ateol-regexp)) | |
acc975b1 DL |
363 | (progn |
364 | (whitespace-buffer-cleanup whitespace-ateol-regexp "") | |
365 | (setq whitespace-any t))) | |
366 | ||
367 | ;; Call this recursively till everything is taken care of | |
18d51459 RS |
368 | (if whitespace-any |
369 | (whitespace-cleanup) | |
acc975b1 DL |
370 | (progn |
371 | (message "%s clean" buffer-file-name) | |
dd24f431 | 372 | (whitespace-update-modeline))) |
acc975b1 DL |
373 | (setq tab-width whitespace-tabwith-saved)))) |
374 | ||
24b72a45 | 375 | ;;;###autoload |
acc975b1 | 376 | (defun whitespace-cleanup-region (s e) |
24b72a45 | 377 | "Whitespace cleanup on a region specified by point and mark." |
acc975b1 DL |
378 | (interactive "r") |
379 | (save-excursion | |
380 | (save-restriction | |
381 | (narrow-to-region s e) | |
382 | (whitespace-cleanup)) | |
383 | (whitespace-buffer t))) | |
384 | ||
385 | (defun whitespace-buffer-leading () | |
386 | "Check to see if there are any empty lines at the top of the file." | |
387 | (save-excursion | |
388 | (let ((pmin nil) | |
389 | (pmax nil)) | |
390 | (goto-char (point-min)) | |
391 | (beginning-of-line) | |
392 | (setq pmin (point)) | |
393 | (end-of-line) | |
394 | (setq pmax (point)) | |
395 | (if (equal pmin pmax) | |
396 | t | |
397 | nil)))) | |
398 | ||
399 | (defun whitespace-buffer-leading-cleanup () | |
24b72a45 | 400 | "Remove any empty lines at the top of the file." |
acc975b1 DL |
401 | (save-excursion |
402 | (let ((pmin nil) | |
403 | (pmax nil)) | |
404 | (goto-char (point-min)) | |
405 | (beginning-of-line) | |
406 | (setq pmin (point)) | |
407 | (end-of-line) | |
408 | (setq pmax (point)) | |
409 | (if (equal pmin pmax) | |
410 | (progn | |
411 | (kill-line) | |
412 | (whitespace-buffer-leading-cleanup)))))) | |
413 | ||
414 | (defun whitespace-buffer-trailing () | |
415 | "Check to see if are is more than one empty line at the bottom." | |
416 | (save-excursion | |
417 | (let ((pmin nil) | |
418 | (pmax nil)) | |
419 | (goto-char (point-max)) | |
420 | (beginning-of-line) | |
421 | (setq pmin (point)) | |
422 | (end-of-line) | |
423 | (setq pmax (point)) | |
424 | (if (equal pmin pmax) | |
425 | (progn | |
426 | (goto-char (- (point) 1)) | |
427 | (beginning-of-line) | |
428 | (setq pmin (point)) | |
429 | (end-of-line) | |
430 | (setq pmax (point)) | |
431 | (if (equal pmin pmax) | |
432 | t | |
433 | nil)) | |
434 | nil)))) | |
435 | ||
436 | (defun whitespace-buffer-trailing-cleanup () | |
437 | "Delete all the empty lines at the bottom." | |
438 | (save-excursion | |
439 | (let ((pmin nil) | |
440 | (pmax nil)) | |
441 | (goto-char (point-max)) | |
442 | (beginning-of-line) | |
443 | (setq pmin (point)) | |
444 | (end-of-line) | |
445 | (setq pmax (point)) | |
446 | (if (equal pmin pmax) | |
447 | (progn | |
448 | (goto-char (1- pmin)) | |
449 | (beginning-of-line) | |
450 | (setq pmin (point)) | |
451 | (end-of-line) | |
452 | (setq pmax (point)) | |
453 | (if (equal pmin pmax) | |
454 | (progn | |
455 | (goto-char (1- (point-max))) | |
456 | (beginning-of-line) | |
457 | (kill-line) | |
458 | (whitespace-buffer-trailing-cleanup)))))))) | |
459 | ||
460 | (defun whitespace-buffer-search (regexp) | |
461 | "Search for any given whitespace REGEXP." | |
462 | (let ((whitespace-retval "")) | |
463 | (save-excursion | |
464 | (goto-char (point-min)) | |
465 | (while (re-search-forward regexp nil t) | |
466 | (setq whitespace-retval (format "%s %s " whitespace-retval | |
467 | (match-beginning 0)))) | |
468 | (if (equal "" whitespace-retval) | |
469 | nil | |
470 | whitespace-retval)))) | |
471 | ||
472 | (defun whitespace-buffer-cleanup (regexp newregexp) | |
473 | "Search for any given whitespace REGEXP and replace it with the NEWREGEXP." | |
474 | (save-excursion | |
475 | (goto-char (point-min)) | |
476 | (while (re-search-forward regexp nil t) | |
477 | (replace-match newregexp)))) | |
478 | ||
479 | (defun whitespace-indent-cleanup () | |
24b72a45 | 480 | "Search for 8/more spaces at the start of a line and replace it with tabs." |
acc975b1 DL |
481 | (save-excursion |
482 | (goto-char (point-min)) | |
483 | (while (re-search-forward whitespace-indent-regexp nil t) | |
484 | (let ((column (current-column)) | |
485 | (indent-tabs-mode t)) | |
486 | (delete-region (match-beginning 0) (point)) | |
487 | (indent-to column))))) | |
488 | ||
dd24f431 GM |
489 | (defun whitespace-unchecked-whitespaces () |
490 | "Return the list of whitespaces whose testing has been suppressed." | |
18d51459 RS |
491 | (let ((whitespace-this-modeline |
492 | (concat (if (not whitespace-check-ateol-whitespace) "e") | |
493 | (if (not whitespace-check-indent-whitespace) "i") | |
494 | (if (not whitespace-check-leading-whitespace) "l") | |
495 | (if (not whitespace-check-spacetab-whitespace) "s") | |
496 | (if (not whitespace-check-trailing-whitespace) "t")))) | |
18d51459 | 497 | (if (not (equal whitespace-this-modeline "")) |
dd24f431 GM |
498 | whitespace-this-modeline |
499 | nil))) | |
500 | ||
501 | (defun whitespace-update-modeline (&optional whitespace-err) | |
502 | "Update modeline with whitespace errors and whitespaces whose testing has | |
503 | been turned off." | |
504 | (if whitespace-display-in-modeline | |
0e38b2a2 GM |
505 | (progn |
506 | (setq whitespace-mode-line nil) | |
507 | ;; Whitespace errors | |
508 | (if (and whitespace-err (not (equal whitespace-err ""))) | |
509 | (setq whitespace-mode-line whitespace-err)) | |
510 | ;; Whitespace suppressed errors | |
511 | (let ((whitespace-unchecked (whitespace-unchecked-whitespaces))) | |
512 | (if whitespace-unchecked | |
513 | (setq whitespace-mode-line | |
514 | (concat whitespace-mode-line "!" whitespace-unchecked)))) | |
515 | ;; Add the whitespace modeline prefix | |
516 | (setq whitespace-mode-line (if whitespace-mode-line | |
517 | (concat " W:" whitespace-mode-line) | |
518 | nil)) | |
519 | (whitespace-force-mode-line-update)))) | |
18d51459 | 520 | |
acc975b1 DL |
521 | ;; Force mode line updation for different Emacs versions |
522 | (defun whitespace-force-mode-line-update () | |
24b72a45 | 523 | "Force the mode line update for different flavors of Emacs." |
acc975b1 | 524 | (if whitespace-running-emacs |
24b72a45 RS |
525 | (force-mode-line-update) ; Emacs |
526 | (redraw-modeline))) ; XEmacs | |
acc975b1 DL |
527 | |
528 | (defun whitespace-check-buffer-list (buf-name buf-file) | |
24b72a45 RS |
529 | "Add a buffer and its file to the whitespace monitor list. |
530 | ||
531 | The buffer named BUF-NAME and its associated file BUF-FILE are now monitored | |
532 | periodically for whitespace." | |
acc975b1 DL |
533 | (if (and whitespace-mode (not (member (list buf-file buf-name) |
534 | whitespace-all-buffer-files))) | |
535 | (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name)))) | |
536 | ||
537 | (defun whitespace-tickle-timer () | |
24b72a45 RS |
538 | "Tickle timer to periodically to scan qualifying files for whitespace creep. |
539 | ||
540 | If timer is not set, then set it to scan the files in | |
541 | `whitespace-all-buffer-files' periodically (defined by | |
542 | `whitespace-rescan-timer-time') for whitespace creep." | |
18d51459 | 543 | (if (and whitespace-rescan-timer-time (not whitespace-rescan-timer)) |
acc975b1 DL |
544 | (setq whitespace-rescan-timer |
545 | (if whitespace-running-emacs | |
546 | (run-at-time nil whitespace-rescan-timer-time | |
547 | 'whitespace-rescan-files-in-buffers) | |
548 | (add-timeout whitespace-rescan-timer-time | |
549 | 'whitespace-rescan-files-in-buffers nil | |
550 | whitespace-rescan-timer-time))))) | |
551 | ||
552 | (defun whitespace-rescan-files-in-buffers (&optional arg) | |
24b72a45 | 553 | "Check monitored files for whitespace creep since last scan." |
acc975b1 | 554 | (let ((whitespace-all-my-files whitespace-all-buffer-files) |
24b72a45 | 555 | buffile bufname thiselt buf) |
acc975b1 DL |
556 | (if (not whitespace-all-my-files) |
557 | (progn | |
558 | (if whitespace-running-emacs | |
559 | (cancel-timer whitespace-rescan-timer) | |
560 | (disable-timeout whitespace-rescan-timer)) | |
561 | (setq whitespace-rescan-timer nil)) | |
562 | (while whitespace-all-my-files | |
563 | (setq thiselt (car whitespace-all-my-files)) | |
564 | (setq whitespace-all-my-files (cdr whitespace-all-my-files)) | |
565 | (setq buffile (car thiselt)) | |
566 | (setq bufname (cadr thiselt)) | |
567 | (setq buf (get-buffer bufname)) | |
568 | (if (buffer-live-p buf) | |
569 | (save-excursion | |
570 | ;;(message "buffer %s live" bufname) | |
571 | (set-buffer bufname) | |
572 | (if whitespace-mode | |
573 | (progn | |
574 | ;;(message "checking for whitespace in %s" bufname) | |
575 | (if whitespace-auto-cleanup | |
576 | (progn | |
577 | ;;(message "cleaning up whitespace in %s" bufname) | |
578 | (whitespace-cleanup)) | |
579 | (progn | |
580 | ;;(message "whitespace-buffer %s." (buffer-name)) | |
581 | (whitespace-buffer t)))) | |
582 | ;;(message "Removing %s from refresh list" bufname) | |
583 | (whitespace-refresh-rescan-list buffile bufname))) | |
584 | ;;(message "Removing %s from refresh list" bufname) | |
585 | (whitespace-refresh-rescan-list buffile bufname)))))) | |
586 | ||
587 | (defun whitespace-refresh-rescan-list (buffile bufname) | |
24b72a45 | 588 | "Refresh the list of files to be rescaned for whitespace creep." |
acc975b1 DL |
589 | (if whitespace-all-buffer-files |
590 | (progn | |
591 | (setq whitespace-all-buffer-files | |
592 | (delete (list buffile bufname) whitespace-all-buffer-files))) | |
593 | (progn | |
594 | (if (and whitespace-running-emacs (timerp whitespace-rescan-timer)) | |
595 | (cancel-timer whitespace-rescan-timer)) | |
596 | (if (and (not whitespace-running-emacs) whitespace-rescan-timer) | |
597 | (disable-timeout whitespace-rescan-timer)) | |
598 | (if whitespace-rescan-timer | |
599 | (setq whitespace-rescan-timer nil))))) | |
600 | ||
24b72a45 RS |
601 | ;;;###autoload |
602 | (defun whitespace-describe () | |
603 | "A summary of whitespaces and what this library can do about them. | |
604 | ||
605 | The whitespace library is intended to find and help fix five different types | |
606 | of whitespace problems that commonly exist in source code. | |
607 | ||
608 | 1. Leading space (empty lines at the top of a file). | |
609 | 2. Trailing space (empty lines at the end of a file). | |
610 | 3. Indentation space (8 or more spaces at beginning of line, that should be | |
611 | replaced with TABS). | |
612 | 4. Spaces followed by a TAB. (Almost always, we never want that). | |
613 | 5. Spaces or TABS at the end of a line. | |
614 | ||
615 | Whitespace errors are reported in a buffer, and on the modeline. | |
616 | ||
dd24f431 GM |
617 | Modeline will show a W:<x>!<y> to denote a particular type of whitespace, |
618 | where `x' and `y' can be one (or more) of: | |
24b72a45 RS |
619 | |
620 | e - End-of-Line whitespace. | |
621 | i - Indentation whitespace. | |
622 | l - Leading whitespace. | |
623 | s - Space followed by Tab. | |
624 | t - Trailing whitespace. | |
625 | ||
18d51459 | 626 | If any of the whitespace checks is turned off, the modeline will display a |
dd24f431 | 627 | !<y>. |
18d51459 | 628 | |
24b72a45 RS |
629 | (since (3) is the most controversial one, here is the rationale: Most |
630 | terminal drivers and printer drivers have TAB configured or even | |
631 | hardcoded to be 8 spaces. (Some of them allow configuration, but almost | |
632 | always they default to 8.) | |
633 | ||
634 | Changing tab-width to other than 8 and editing will cause your code to | |
635 | look different from within Emacs, and say, if you cat it or more it, or | |
636 | even print it. | |
637 | ||
638 | Almost all the popular programming modes let you define an offset (like | |
639 | c-basic-offset or perl-indent-level) to configure the offset, so you | |
640 | should never have to set your tab-width to be other than 8 in all these | |
641 | modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs | |
642 | to replace your 8 spaces with one \t (try it). If vi users in your | |
643 | office complain, tell them to use vim, which distinguishes between | |
644 | tabstop and shiftwidth (vi equivalent of our offsets), and also ask them | |
645 | to set smarttab.) | |
646 | ||
647 | All the above have caused (and will cause) unwanted codeline integration and | |
648 | merge problems. | |
649 | ||
650 | whitespace.el will complain if it detects whitespaces on opening a file, and | |
651 | warn you on closing a file also. (if in case you had inserted any | |
652 | whitespaces during the process of your editing.)" | |
653 | (interactive) | |
654 | (message "Use C-h f whitespace-describe to read about whitespace.el v%s." | |
655 | whitespace-version)) | |
656 | ||
acc975b1 DL |
657 | (provide 'whitespace) |
658 | ||
659 | ;;; whitespace.el ends here |