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