]> code.delx.au - gnu-emacs/blob - lisp/whitespace.el
Merge from emacs-24; up to 2012-04-24T21:47:24Z!michael.albinus@gmx.de
[gnu-emacs] / lisp / whitespace.el
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
2
3 ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
4
5 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
6 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Keywords: data, wp
8 ;; Version: 13.2.2
9 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;
30 ;; Introduction
31 ;; ------------
32 ;;
33 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
34 ;; and NEWLINE).
35 ;;
36 ;; whitespace uses two ways to visualize blanks: faces and display
37 ;; table.
38 ;;
39 ;; * Faces are used to highlight the background with a color.
40 ;; whitespace uses font-lock to highlight blank characters.
41 ;;
42 ;; * Display table changes the way a character is displayed, that is,
43 ;; it provides a visual mark for characters, for example, at the end
44 ;; of line (?\xB6), at SPACEs (?\xB7) and at TABs (?\xBB).
45 ;;
46 ;; The `whitespace-style' variable selects which way blanks are
47 ;; visualized.
48 ;;
49 ;; Note that when whitespace is turned on, whitespace saves the
50 ;; font-lock state, that is, if font-lock is on or off. And
51 ;; whitespace restores the font-lock state when it is turned off. So,
52 ;; if whitespace is turned on and font-lock is off, whitespace also
53 ;; turns on the font-lock to highlight blanks, but the font-lock will
54 ;; be turned off when whitespace is turned off. Thus, turn on
55 ;; font-lock before whitespace is on, if you want that font-lock
56 ;; continues on after whitespace is turned off.
57 ;;
58 ;; When whitespace is on, it takes care of highlighting some special
59 ;; characters over the default mechanism of `nobreak-char-display'
60 ;; (which see) and `show-trailing-whitespace' (which see).
61 ;;
62 ;; The trailing spaces are not highlighted while point is at end of line.
63 ;; Also the spaces at beginning of buffer are not highlighted while point is at
64 ;; beginning of buffer; and the spaces at end of buffer are not highlighted
65 ;; while point is at end of buffer.
66 ;;
67 ;; There are two ways of using whitespace: local and global.
68 ;;
69 ;; * Local whitespace affects only the current buffer.
70 ;;
71 ;; * Global whitespace affects all current and future buffers. That
72 ;; is, if you turn on global whitespace and then create a new
73 ;; buffer, the new buffer will also have whitespace on. The
74 ;; `whitespace-global-modes' variable controls which major-mode will
75 ;; be automagically turned on.
76 ;;
77 ;; You can mix the local and global usage without any conflict. But
78 ;; local whitespace has priority over global whitespace. Whitespace
79 ;; mode is active in a buffer if you have enabled it in that buffer or
80 ;; if you have enabled it globally.
81 ;;
82 ;; When global and local whitespace are on:
83 ;;
84 ;; * if local whitespace is turned off, whitespace is turned off for
85 ;; the current buffer only.
86 ;;
87 ;; * if global whitespace is turned off, whitespace continues on only
88 ;; in the buffers in which local whitespace is on.
89 ;;
90 ;; To use whitespace, insert in your ~/.emacs:
91 ;;
92 ;; (require 'whitespace)
93 ;;
94 ;; Or autoload at least one of the commands`whitespace-mode',
95 ;; `whitespace-toggle-options', `global-whitespace-mode' or
96 ;; `global-whitespace-toggle-options'. For example:
97 ;;
98 ;; (autoload 'whitespace-mode "whitespace"
99 ;; "Toggle whitespace visualization." t)
100 ;; (autoload 'whitespace-toggle-options "whitespace"
101 ;; "Toggle local `whitespace-mode' options." t)
102 ;;
103 ;; whitespace was inspired by:
104 ;;
105 ;; whitespace.el Rajesh Vaidheeswarran <rv@gnu.org>
106 ;; Warn about and clean bogus whitespaces in the file
107 ;; (inspired the idea to warn and clean some blanks)
108 ;; This was the original `whitespace.el' which was replaced by
109 ;; `blank-mode.el'. And later `blank-mode.el' was renamed to
110 ;; `whitespace.el'.
111 ;;
112 ;; show-whitespace-mode.el Aurelien Tisne <aurelien.tisne@free.fr>
113 ;; Simple mode to highlight whitespaces
114 ;; (inspired the idea to use font-lock)
115 ;;
116 ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li>
117 ;; Major mode for editing Whitespace
118 ;; (inspired the idea to use display table)
119 ;;
120 ;; visws.el Miles Bader <miles@gnu.org>
121 ;; Make whitespace visible
122 ;; (handle display table, his code was modified, but the main
123 ;; idea was kept)
124 ;;
125 ;;
126 ;; Using whitespace
127 ;; ----------------
128 ;;
129 ;; There is no problem if you mix local and global minor mode usage.
130 ;;
131 ;; * LOCAL whitespace:
132 ;; + To toggle whitespace options locally, type:
133 ;;
134 ;; M-x whitespace-toggle-options RET
135 ;;
136 ;; + To activate whitespace locally, type:
137 ;;
138 ;; C-u 1 M-x whitespace-mode RET
139 ;;
140 ;; + To deactivate whitespace locally, type:
141 ;;
142 ;; C-u 0 M-x whitespace-mode RET
143 ;;
144 ;; + To toggle whitespace locally, type:
145 ;;
146 ;; M-x whitespace-mode RET
147 ;;
148 ;; * GLOBAL whitespace:
149 ;; + To toggle whitespace options globally, type:
150 ;;
151 ;; M-x global-whitespace-toggle-options RET
152 ;;
153 ;; + To activate whitespace globally, type:
154 ;;
155 ;; C-u 1 M-x global-whitespace-mode RET
156 ;;
157 ;; + To deactivate whitespace globally, type:
158 ;;
159 ;; C-u 0 M-x global-whitespace-mode RET
160 ;;
161 ;; + To toggle whitespace globally, type:
162 ;;
163 ;; M-x global-whitespace-mode RET
164 ;;
165 ;; There are also the following useful commands:
166 ;;
167 ;; `whitespace-newline-mode'
168 ;; Toggle NEWLINE minor mode visualization ("nl" on modeline).
169 ;;
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on modeline).
172 ;;
173 ;; `whitespace-report'
174 ;; Report some blank problems in buffer.
175 ;;
176 ;; `whitespace-report-region'
177 ;; Report some blank problems in a region.
178 ;;
179 ;; `whitespace-cleanup'
180 ;; Cleanup some blank problems in all buffer or at region.
181 ;;
182 ;; `whitespace-cleanup-region'
183 ;; Cleanup some blank problems at region.
184 ;;
185 ;; The problems, which are cleaned up, are:
186 ;;
187 ;; 1. empty lines at beginning of buffer.
188 ;; 2. empty lines at end of buffer.
189 ;; If `whitespace-style' includes the value `empty', remove all
190 ;; empty lines at beginning and/or end of buffer.
191 ;;
192 ;; 3. 8 or more SPACEs at beginning of line.
193 ;; If `whitespace-style' includes the value `indentation':
194 ;; replace 8 or more SPACEs at beginning of line by TABs, if
195 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
196 ;; SPACEs.
197 ;; If `whitespace-style' includes the value `indentation::tab',
198 ;; replace 8 or more SPACEs at beginning of line by TABs.
199 ;; If `whitespace-style' includes the value `indentation::space',
200 ;; replace TABs by SPACEs.
201 ;;
202 ;; 4. SPACEs before TAB.
203 ;; If `whitespace-style' includes the value `space-before-tab':
204 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
205 ;; otherwise, replace TABs by SPACEs.
206 ;; If `whitespace-style' includes the value
207 ;; `space-before-tab::tab', replace SPACEs by TABs.
208 ;; If `whitespace-style' includes the value
209 ;; `space-before-tab::space', replace TABs by SPACEs.
210 ;;
211 ;; 5. SPACEs or TABs at end of line.
212 ;; If `whitespace-style' includes the value `trailing', remove all
213 ;; SPACEs or TABs at end of line.
214 ;;
215 ;; 6. 8 or more SPACEs after TAB.
216 ;; If `whitespace-style' includes the value `space-after-tab':
217 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
218 ;; otherwise, replace TABs by SPACEs.
219 ;; If `whitespace-style' includes the value `space-after-tab::tab',
220 ;; replace SPACEs by TABs.
221 ;; If `whitespace-style' includes the value
222 ;; `space-after-tab::space', replace TABs by SPACEs.
223 ;;
224 ;;
225 ;; Hooks
226 ;; -----
227 ;;
228 ;; whitespace has the following hook variables:
229 ;;
230 ;; `whitespace-mode-hook'
231 ;; It is evaluated always when whitespace is turned on locally.
232 ;;
233 ;; `global-whitespace-mode-hook'
234 ;; It is evaluated always when whitespace is turned on globally.
235 ;;
236 ;; `whitespace-load-hook'
237 ;; It is evaluated after whitespace package is loaded.
238 ;;
239 ;;
240 ;; Options
241 ;; -------
242 ;;
243 ;; Below it's shown a brief description of whitespace options, please,
244 ;; see the options declaration in the code for a long documentation.
245 ;;
246 ;; `whitespace-style' Specify which kind of blank is
247 ;; visualized.
248 ;;
249 ;; `whitespace-space' Face used to visualize SPACE.
250 ;;
251 ;; `whitespace-hspace' Face used to visualize HARD SPACE.
252 ;;
253 ;; `whitespace-tab' Face used to visualize TAB.
254 ;;
255 ;; `whitespace-newline' Face used to visualize NEWLINE char
256 ;; mapping.
257 ;;
258 ;; `whitespace-trailing' Face used to visualize trailing
259 ;; blanks.
260 ;;
261 ;; `whitespace-line' Face used to visualize "long" lines.
262 ;;
263 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
264 ;; before TAB.
265 ;;
266 ;; `whitespace-indentation' Face used to visualize 8 or more
267 ;; SPACEs at beginning of line.
268 ;;
269 ;; `whitespace-empty' Face used to visualize empty lines at
270 ;; beginning and/or end of buffer.
271 ;;
272 ;; `whitespace-space-after-tab' Face used to visualize 8 or more
273 ;; SPACEs after TAB.
274 ;;
275 ;; `whitespace-space-regexp' Specify SPACE characters regexp.
276 ;;
277 ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
278 ;;
279 ;; `whitespace-tab-regexp' Specify TAB characters regexp.
280 ;;
281 ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
282 ;;
283 ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
284 ;; regexp.
285 ;;
286 ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
287 ;; SPACEs at beginning of line.
288 ;;
289 ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
290 ;; at beginning of buffer.
291 ;;
292 ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
293 ;; at end of buffer.
294 ;;
295 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
296 ;; SPACEs after TAB.
297 ;;
298 ;; `whitespace-line-column' Specify column beyond which the line
299 ;; is highlighted.
300 ;;
301 ;; `whitespace-display-mappings' Specify an alist of mappings
302 ;; for displaying characters.
303 ;;
304 ;; `whitespace-global-modes' Modes for which global
305 ;; `whitespace-mode' is automagically
306 ;; turned on.
307 ;;
308 ;; `whitespace-action' Specify which action is taken when a
309 ;; buffer is visited or written.
310 ;;
311 ;;
312 ;; Acknowledgements
313 ;; ----------------
314 ;;
315 ;; Thanks to felix (EmacsWiki) for keeping highlight when switching between
316 ;; major modes on a file.
317 ;;
318 ;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a
319 ;; `whitespace-newline' initialization with low contrast relative to
320 ;; the background color.
321 ;;
322 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
323 ;; `indent-tabs-mode' usage suggestion.
324 ;;
325 ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook
326 ;; actions when buffer is written as the original whitespace package
327 ;; had.
328 ;;
329 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
330 ;; lines tail. See EightyColumnRule (EmacsWiki).
331 ;;
332 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
333 ;; * `define-minor-mode'.
334 ;; * `global-whitespace-*' name for global commands.
335 ;;
336 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
337 ;;
338 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
339 ;; suggestion.
340 ;;
341 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
342 ;; helping to fix `find-file-hooks' reference.
343 ;;
344 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
345 ;; indicating defface byte-compilation warnings.
346 ;;
347 ;; Thanks to TimOCallaghan (EmacsWiki) for the idea about highlight
348 ;; "long" lines. See EightyColumnRule (EmacsWiki).
349 ;;
350 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
351 ;; NEWLINE character mapping.
352 ;;
353 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
354 ;; whitespace-mode.el on XEmacs.
355 ;;
356 ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via
357 ;; visws.el (his code was modified, but the main idea was kept).
358 ;;
359 ;; Thanks to:
360 ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el
361 ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el
362 ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el
363 ;; Miles Bader <miles@gnu.org> visws.el
364 ;; And to all people who contributed with them.
365 ;;
366 ;;
367 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368
369 ;;; code:
370
371 \f
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;;;; User Variables:
374
375
376 ;;; Interface to the command system
377
378
379 (defgroup whitespace nil
380 "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
381 :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
382 :version "23.1"
383 :group 'convenience)
384
385
386 (defcustom whitespace-style
387 '(face
388 tabs spaces trailing lines space-before-tab newline
389 indentation empty space-after-tab
390 space-mark tab-mark newline-mark)
391 "Specify which kind of blank is visualized.
392
393 It's a list containing some or all of the following values:
394
395 face enable all visualization via faces (see below).
396
397 trailing trailing blanks are visualized via faces.
398 It has effect only if `face' (see above)
399 is present in `whitespace-style'.
400
401 tabs TABs are visualized via faces.
402 It has effect only if `face' (see above)
403 is present in `whitespace-style'.
404
405 spaces SPACEs and HARD SPACEs are visualized via
406 faces.
407 It has effect only if `face' (see above)
408 is present in `whitespace-style'.
409
410 lines lines which have columns beyond
411 `whitespace-line-column' are highlighted via
412 faces.
413 Whole line is highlighted.
414 It has precedence over `lines-tail' (see
415 below).
416 It has effect only if `face' (see above)
417 is present in `whitespace-style'.
418
419 lines-tail lines which have columns beyond
420 `whitespace-line-column' are highlighted via
421 faces.
422 But only the part of line which goes
423 beyond `whitespace-line-column' column.
424 It has effect only if `lines' (see above)
425 is not present in `whitespace-style'
426 and if `face' (see above) is present in
427 `whitespace-style'.
428
429 newline NEWLINEs are visualized via faces.
430 It has effect only if `face' (see above)
431 is present in `whitespace-style'.
432
433 empty empty lines at beginning and/or end of buffer
434 are visualized via faces.
435 It has effect only if `face' (see above)
436 is present in `whitespace-style'.
437
438 indentation::tab 8 or more SPACEs at beginning of line are
439 visualized via faces.
440 It has effect only if `face' (see above)
441 is present in `whitespace-style'.
442
443 indentation::space TABs at beginning of line are visualized via
444 faces.
445 It has effect only if `face' (see above)
446 is present in `whitespace-style'.
447
448 indentation 8 or more SPACEs at beginning of line are
449 visualized, if `indent-tabs-mode' (which see)
450 is non-nil; otherwise, TABs at beginning of
451 line are visualized via faces.
452 It has effect only if `face' (see above)
453 is present in `whitespace-style'.
454
455 space-after-tab::tab 8 or more SPACEs after a TAB are
456 visualized via faces.
457 It has effect only if `face' (see above)
458 is present in `whitespace-style'.
459
460 space-after-tab::space TABs are visualized when 8 or more
461 SPACEs occur after a TAB, via faces.
462 It has effect only if `face' (see above)
463 is present in `whitespace-style'.
464
465 space-after-tab 8 or more SPACEs after a TAB are
466 visualized, if `indent-tabs-mode'
467 (which see) is non-nil; otherwise,
468 the TABs are visualized via faces.
469 It has effect only if `face' (see above)
470 is present in `whitespace-style'.
471
472 space-before-tab::tab SPACEs before TAB are visualized via
473 faces.
474 It has effect only if `face' (see above)
475 is present in `whitespace-style'.
476
477 space-before-tab::space TABs are visualized when SPACEs occur
478 before TAB, via faces.
479 It has effect only if `face' (see above)
480 is present in `whitespace-style'.
481
482 space-before-tab SPACEs before TAB are visualized, if
483 `indent-tabs-mode' (which see) is
484 non-nil; otherwise, the TABs are
485 visualized via faces.
486 It has effect only if `face' (see above)
487 is present in `whitespace-style'.
488
489 space-mark SPACEs and HARD SPACEs are visualized via
490 display table.
491
492 tab-mark TABs are visualized via display table.
493
494 newline-mark NEWLINEs are visualized via display table.
495
496 Any other value is ignored.
497
498 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
499 via display table.
500
501 There is an evaluation order for some values, if they are
502 included in `whitespace-style' list. For example, if
503 indentation, indentation::tab and/or indentation::space are
504 included in `whitespace-style' list. The evaluation order for
505 these values is:
506
507 * For indentation:
508 1. indentation
509 2. indentation::tab
510 3. indentation::space
511
512 * For SPACEs after TABs:
513 1. space-after-tab
514 2. space-after-tab::tab
515 3. space-after-tab::space
516
517 * For SPACEs before TABs:
518 1. space-before-tab
519 2. space-before-tab::tab
520 3. space-before-tab::space
521
522 So, for example, if indentation and indentation::space are
523 included in `whitespace-style' list, the indentation value is
524 evaluated instead of indentation::space value.
525
526 One reason for not visualize spaces via faces (if `face' is not
527 included in `whitespace-style') is to use exclusively for
528 cleaning up a buffer. See `whitespace-cleanup' and
529 `whitespace-cleanup-region' for documentation.
530
531 See also `whitespace-display-mappings' for documentation."
532 :type '(repeat :tag "Kind of Blank"
533 (choice :tag "Kind of Blank Face"
534 (const :tag "(Face) Face visualization"
535 face)
536 (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
537 trailing)
538 (const :tag "(Face) SPACEs and HARD SPACEs"
539 spaces)
540 (const :tag "(Face) TABs" tabs)
541 (const :tag "(Face) Lines" lines)
542 (const :tag "(Face) SPACEs before TAB"
543 space-before-tab)
544 (const :tag "(Face) NEWLINEs" newline)
545 (const :tag "(Face) Indentation SPACEs"
546 indentation)
547 (const :tag "(Face) Empty Lines At BOB And/Or EOB"
548 empty)
549 (const :tag "(Face) SPACEs after TAB"
550 space-after-tab)
551 (const :tag "(Mark) SPACEs and HARD SPACEs"
552 space-mark)
553 (const :tag "(Mark) TABs" tab-mark)
554 (const :tag "(Mark) NEWLINEs" newline-mark)))
555 :group 'whitespace)
556
557
558 (defcustom whitespace-space 'whitespace-space
559 "Symbol face used to visualize SPACE.
560
561 Used when `whitespace-style' includes the value `spaces'."
562 :type 'face
563 :group 'whitespace)
564
565
566 (defface whitespace-space
567 '((((class color) (background dark))
568 (:background "grey20" :foreground "darkgray"))
569 (((class color) (background light))
570 (:background "LightYellow" :foreground "lightgray"))
571 (t (:inverse-video t)))
572 "Face used to visualize SPACE."
573 :group 'whitespace)
574
575
576 (defcustom whitespace-hspace 'whitespace-hspace
577 "Symbol face used to visualize HARD SPACE.
578
579 Used when `whitespace-style' includes the value `spaces'."
580 :type 'face
581 :group 'whitespace)
582
583
584 (defface whitespace-hspace ; 'nobreak-space
585 '((((class color) (background dark))
586 (:background "grey24" :foreground "darkgray"))
587 (((class color) (background light))
588 (:background "LemonChiffon3" :foreground "lightgray"))
589 (t (:inverse-video t)))
590 "Face used to visualize HARD SPACE."
591 :group 'whitespace)
592
593
594 (defcustom whitespace-tab 'whitespace-tab
595 "Symbol face used to visualize TAB.
596
597 Used when `whitespace-style' includes the value `tabs'."
598 :type 'face
599 :group 'whitespace)
600
601
602 (defface whitespace-tab
603 '((((class color) (background dark))
604 (:background "grey22" :foreground "darkgray"))
605 (((class color) (background light))
606 (:background "beige" :foreground "lightgray"))
607 (t (:inverse-video t)))
608 "Face used to visualize TAB."
609 :group 'whitespace)
610
611
612 (defcustom whitespace-newline 'whitespace-newline
613 "Symbol face used to visualize NEWLINE char mapping.
614
615 See `whitespace-display-mappings'.
616
617 Used when `whitespace-style' includes the values `newline-mark'
618 and `newline'."
619 :type 'face
620 :group 'whitespace)
621
622
623 (defface whitespace-newline
624 '((((class color) (background dark))
625 (:foreground "darkgray" :bold nil))
626 (((class color) (min-colors 88) (background light))
627 (:foreground "lightgray" :bold nil))
628 ;; Displays with 16 colors use lightgray as background, so using a
629 ;; lightgray foreground makes the newline mark invisible.
630 (((class color) (background light))
631 (:foreground "brown" :bold nil))
632 (t (:underline t :bold nil)))
633 "Face used to visualize NEWLINE char mapping.
634
635 See `whitespace-display-mappings'."
636 :group 'whitespace)
637
638
639 (defcustom whitespace-trailing 'whitespace-trailing
640 "Symbol face used to visualize trailing blanks.
641
642 Used when `whitespace-style' includes the value `trailing'."
643 :type 'face
644 :group 'whitespace)
645
646
647 (defface whitespace-trailing ; 'trailing-whitespace
648 '((((class mono)) (:inverse-video t :bold t :underline t))
649 (t (:background "red1" :foreground "yellow" :bold t)))
650 "Face used to visualize trailing blanks."
651 :group 'whitespace)
652
653
654 (defcustom whitespace-line 'whitespace-line
655 "Symbol face used to visualize \"long\" lines.
656
657 See `whitespace-line-column'.
658
659 Used when `whitespace-style' includes the value `line'."
660 :type 'face
661 :group 'whitespace)
662
663
664 (defface whitespace-line
665 '((((class mono)) (:inverse-video t :bold t :underline t))
666 (t (:background "gray20" :foreground "violet")))
667 "Face used to visualize \"long\" lines.
668
669 See `whitespace-line-column'."
670 :group 'whitespace)
671
672
673 (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
674 "Symbol face used to visualize SPACEs before TAB.
675
676 Used when `whitespace-style' includes the value `space-before-tab'."
677 :type 'face
678 :group 'whitespace)
679
680
681 (defface whitespace-space-before-tab
682 '((((class mono)) (:inverse-video t :bold t :underline t))
683 (t (:background "DarkOrange" :foreground "firebrick")))
684 "Face used to visualize SPACEs before TAB."
685 :group 'whitespace)
686
687
688 (defcustom whitespace-indentation 'whitespace-indentation
689 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
690
691 Used when `whitespace-style' includes the value `indentation'."
692 :type 'face
693 :group 'whitespace)
694
695
696 (defface whitespace-indentation
697 '((((class mono)) (:inverse-video t :bold t :underline t))
698 (t (:background "yellow" :foreground "firebrick")))
699 "Face used to visualize 8 or more SPACEs at beginning of line."
700 :group 'whitespace)
701
702
703 (defcustom whitespace-empty 'whitespace-empty
704 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
705
706 Used when `whitespace-style' includes the value `empty'."
707 :type 'face
708 :group 'whitespace)
709
710
711 (defface whitespace-empty
712 '((((class mono)) (:inverse-video t :bold t :underline t))
713 (t (:background "yellow" :foreground "firebrick")))
714 "Face used to visualize empty lines at beginning and/or end of buffer."
715 :group 'whitespace)
716
717
718 (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
719 "Symbol face used to visualize 8 or more SPACEs after TAB.
720
721 Used when `whitespace-style' includes the value `space-after-tab'."
722 :type 'face
723 :group 'whitespace)
724
725
726 (defface whitespace-space-after-tab
727 '((((class mono)) (:inverse-video t :bold t :underline t))
728 (t (:background "yellow" :foreground "firebrick")))
729 "Face used to visualize 8 or more SPACEs after TAB."
730 :group 'whitespace)
731
732
733 (defcustom whitespace-hspace-regexp
734 "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
735 "Specify HARD SPACE characters regexp.
736
737 If you're using `mule' package, there may be other characters besides:
738
739 \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
740
741 that should be considered HARD SPACE.
742
743 Here are some examples:
744
745 \"\\\\(^\\xA0+\\\\)\" \
746 visualize only leading HARD SPACEs.
747 \"\\\\(\\xA0+$\\\\)\" \
748 visualize only trailing HARD SPACEs.
749 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
750 visualize leading and/or trailing HARD SPACEs.
751 \"\\t\\\\(\\xA0+\\\\)\\t\" \
752 visualize only HARD SPACEs between TABs.
753
754 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
755 Use exactly one pair of enclosing \\\\( and \\\\).
756
757 Used when `whitespace-style' includes `spaces'."
758 :type '(regexp :tag "HARD SPACE Chars")
759 :group 'whitespace)
760
761
762 (defcustom whitespace-space-regexp "\\( +\\)"
763 "Specify SPACE characters regexp.
764
765 If you're using `mule' package, there may be other characters
766 besides \" \" that should be considered SPACE.
767
768 Here are some examples:
769
770 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
771 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
772 \"\\\\(^ +\\\\| +$\\\\)\" \
773 visualize leading and/or trailing SPACEs.
774 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
775
776 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
777 Use exactly one pair of enclosing \\\\( and \\\\).
778
779 Used when `whitespace-style' includes `spaces'."
780 :type '(regexp :tag "SPACE Chars")
781 :group 'whitespace)
782
783
784 (defcustom whitespace-tab-regexp "\\(\t+\\)"
785 "Specify TAB characters regexp.
786
787 If you're using `mule' package, there may be other characters
788 besides \"\\t\" that should be considered TAB.
789
790 Here are some examples:
791
792 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
793 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
794 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
795 visualize leading and/or trailing TABs.
796 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
797
798 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
799 Use exactly one pair of enclosing \\\\( and \\\\).
800
801 Used when `whitespace-style' includes `tabs'."
802 :type '(regexp :tag "TAB Chars")
803 :group 'whitespace)
804
805
806 (defcustom whitespace-trailing-regexp
807 "\\([\t \u00A0]+\\)$"
808 "Specify trailing characters regexp.
809
810 If you're using `mule' package, there may be other characters besides:
811
812 \" \" \"\\t\" \"\\u00A0\"
813
814 that should be considered blank.
815
816 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
817 Use exactly one pair of enclosing elements above.
818
819 Used when `whitespace-style' includes `trailing'."
820 :type '(regexp :tag "Trailing Chars")
821 :group 'whitespace)
822
823
824 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
825 "Specify SPACEs before TAB regexp.
826
827 If you're using `mule' package, there may be other characters besides:
828
829 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
830 \"\\xF20\"
831
832 that should be considered blank.
833
834 Used when `whitespace-style' includes `space-before-tab',
835 `space-before-tab::tab' or `space-before-tab::space'."
836 :type '(regexp :tag "SPACEs Before TAB")
837 :group 'whitespace)
838
839
840 (defcustom whitespace-indentation-regexp
841 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
842 . "^ *\\(\t+\\)[^\n]")
843 "Specify regexp for 8 or more SPACEs at beginning of line.
844
845 It is a cons where the cons car is used for SPACEs visualization
846 and the cons cdr is used for TABs visualization.
847
848 If you're using `mule' package, there may be other characters besides:
849
850 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
851 \"\\xF20\"
852
853 that should be considered blank.
854
855 Used when `whitespace-style' includes `indentation',
856 `indentation::tab' or `indentation::space'."
857 :type '(cons (regexp :tag "Indentation SPACEs")
858 (regexp :tag "Indentation TABs"))
859 :group 'whitespace)
860
861
862 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
863 "Specify regexp for empty lines at beginning of buffer.
864
865 If you're using `mule' package, there may be other characters besides:
866
867 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
868 \"\\xF20\"
869
870 that should be considered blank.
871
872 Used when `whitespace-style' includes `empty'."
873 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
874 :group 'whitespace)
875
876
877 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
878 "Specify regexp for empty lines at end of buffer.
879
880 If you're using `mule' package, there may be other characters besides:
881
882 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
883 \"\\xF20\"
884
885 that should be considered blank.
886
887 Used when `whitespace-style' includes `empty'."
888 :type '(regexp :tag "Empty Lines At End Of Buffer")
889 :group 'whitespace)
890
891
892 (defcustom whitespace-space-after-tab-regexp
893 '("\t+\\(\\( \\{%d\\}\\)+\\)"
894 . "\\(\t+\\) +")
895 "Specify regexp for 8 or more SPACEs after TAB.
896
897 It is a cons where the cons car is used for SPACEs visualization
898 and the cons cdr is used for TABs visualization.
899
900 If you're using `mule' package, there may be other characters besides:
901
902 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
903 \"\\xF20\"
904
905 that should be considered blank.
906
907 Used when `whitespace-style' includes `space-after-tab',
908 `space-after-tab::tab' or `space-after-tab::space'."
909 :type '(regexp :tag "SPACEs After TAB")
910 :group 'whitespace)
911
912
913 (defcustom whitespace-line-column 80
914 "Specify column beyond which the line is highlighted.
915
916 It must be an integer or nil. If nil, the `fill-column' variable value is
917 used.
918
919 Used when `whitespace-style' includes `lines' or `lines-tail'."
920 :type '(choice :tag "Line Length Limit"
921 (integer :tag "Line Length")
922 (const :tag "Use fill-column" nil))
923 :group 'whitespace)
924
925
926 ;; Hacked from `visible-whitespace-mappings' in visws.el
927 (defcustom whitespace-display-mappings
928 '(
929 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
930 (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
931 (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency
932 (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency
933 (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency
934 (space-mark ?\xF20 [?\xF24] [?_]) ; hard space - currency
935 ;; NEWLINE is displayed using the face `whitespace-newline'
936 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
937 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
938 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
939 ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
940 ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
941 ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
942 ;;
943 ;; WARNING: the mapping below has a problem.
944 ;; When a TAB occupies exactly one column, it will display the
945 ;; character ?\xBB at that column followed by a TAB which goes to
946 ;; the next TAB column.
947 ;; If this is a problem for you, please, comment the line below.
948 (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
949 )
950 "Specify an alist of mappings for displaying characters.
951
952 Each element has the following form:
953
954 (KIND CHAR VECTOR...)
955
956 Where:
957
958 KIND is the kind of character.
959 It can be one of the following symbols:
960
961 tab-mark for TAB character
962
963 space-mark for SPACE or HARD SPACE character
964
965 newline-mark for NEWLINE character
966
967 CHAR is the character to be mapped.
968
969 VECTOR is a vector of characters to be displayed in place of CHAR.
970 The first display vector that can be displayed is used;
971 if no display vector for a mapping can be displayed, then
972 that character is displayed unmodified.
973
974 The NEWLINE character is displayed using the face given by
975 `whitespace-newline' variable.
976
977 Used when `whitespace-style' includes `tab-mark', `space-mark' or
978 `newline-mark'."
979 :type '(repeat
980 (list :tag "Character Mapping"
981 (choice :tag "Char Kind"
982 (const :tag "Tab" tab-mark)
983 (const :tag "Space" space-mark)
984 (const :tag "Newline" newline-mark))
985 (character :tag "Char")
986 (repeat :inline t :tag "Vector List"
987 (vector :tag ""
988 (repeat :inline t
989 :tag "Vector Characters"
990 (character :tag "Char"))))))
991 :group 'whitespace)
992
993
994 (defcustom whitespace-global-modes t
995 "Modes for which global `whitespace-mode' is automagically turned on.
996
997 Global `whitespace-mode' is controlled by the command
998 `global-whitespace-mode'.
999
1000 If nil, means no modes have `whitespace-mode' automatically
1001 turned on.
1002
1003 If t, all modes that support `whitespace-mode' have it
1004 automatically turned on.
1005
1006 Else it should be a list of `major-mode' symbol names for which
1007 `whitespace-mode' should be automatically turned on. The sense
1008 of the list is negated if it begins with `not'. For example:
1009
1010 (c-mode c++-mode)
1011
1012 means that `whitespace-mode' is turned on for buffers in C and
1013 C++ modes only."
1014 :type '(choice :tag "Global Modes"
1015 (const :tag "None" nil)
1016 (const :tag "All" t)
1017 (set :menu-tag "Mode Specific" :tag "Modes"
1018 :value (not)
1019 (const :tag "Except" not)
1020 (repeat :inline t
1021 (symbol :tag "Mode"))))
1022 :group 'whitespace)
1023
1024
1025 (defcustom whitespace-action nil
1026 "Specify which action is taken when a buffer is visited or written.
1027
1028 It's a list containing some or all of the following values:
1029
1030 nil no action is taken.
1031
1032 cleanup cleanup any bogus whitespace always when local
1033 whitespace is turned on.
1034 See `whitespace-cleanup' and
1035 `whitespace-cleanup-region'.
1036
1037 report-on-bogus report if there is any bogus whitespace always
1038 when local whitespace is turned on.
1039
1040 auto-cleanup cleanup any bogus whitespace when buffer is
1041 written.
1042 See `whitespace-cleanup' and
1043 `whitespace-cleanup-region'.
1044
1045 abort-on-bogus abort if there is any bogus whitespace and the
1046 buffer is written.
1047
1048 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
1049 is included in `whitespace-action' and the
1050 buffer is read-only.
1051
1052 Any other value is treated as nil."
1053 :type '(choice :tag "Actions"
1054 (const :tag "None" nil)
1055 (repeat :tag "Action List"
1056 (choice :tag "Action"
1057 (const :tag "Cleanup When On" cleanup)
1058 (const :tag "Report On Bogus" report-on-bogus)
1059 (const :tag "Auto Cleanup" auto-cleanup)
1060 (const :tag "Abort On Bogus" abort-on-bogus)
1061 (const :tag "Warn If Read-Only" warn-if-read-only))))
1062 :group 'whitespace)
1063
1064 \f
1065 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1066 ;;;; User commands - Local mode
1067
1068
1069 ;;;###autoload
1070 (define-minor-mode whitespace-mode
1071 "Toggle whitespace visualization (Whitespace mode).
1072 With a prefix argument ARG, enable Whitespace mode if ARG is
1073 positive, and disable it otherwise. If called from Lisp, enable
1074 the mode if ARG is omitted or nil.
1075
1076 See also `whitespace-style', `whitespace-newline' and
1077 `whitespace-display-mappings'."
1078 :lighter " ws"
1079 :init-value nil
1080 :global nil
1081 :group 'whitespace
1082 (cond
1083 (noninteractive ; running a batch job
1084 (setq whitespace-mode nil))
1085 (whitespace-mode ; whitespace-mode on
1086 (whitespace-turn-on)
1087 (whitespace-action-when-on))
1088 (t ; whitespace-mode off
1089 (whitespace-turn-off))))
1090
1091
1092 ;;;###autoload
1093 (define-minor-mode whitespace-newline-mode
1094 "Toggle newline visualization (Whitespace Newline mode).
1095 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1096 is positive, and disable it otherwise. If called from Lisp,
1097 enable the mode if ARG is omitted or nil.
1098
1099 Use `whitespace-newline-mode' only for NEWLINE visualization
1100 exclusively. For other visualizations, including NEWLINE
1101 visualization together with (HARD) SPACEs and/or TABs, please,
1102 use `whitespace-mode'.
1103
1104 See also `whitespace-newline' and `whitespace-display-mappings'."
1105 :lighter " nl"
1106 :init-value nil
1107 :global nil
1108 :group 'whitespace
1109 (let ((whitespace-style '(face newline-mark newline)))
1110 (whitespace-mode (if whitespace-newline-mode
1111 1 -1)))
1112 ;; sync states (running a batch job)
1113 (setq whitespace-newline-mode whitespace-mode))
1114
1115 \f
1116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1117 ;;;; User commands - Global mode
1118
1119
1120 ;;;###autoload
1121 (define-minor-mode global-whitespace-mode
1122 "Toggle whitespace visualization globally (Global Whitespace mode).
1123 With a prefix argument ARG, enable Global Whitespace mode if ARG
1124 is positive, and disable it otherwise. If called from Lisp,
1125 enable it if ARG is omitted or nil.
1126
1127 See also `whitespace-style', `whitespace-newline' and
1128 `whitespace-display-mappings'."
1129 :lighter " WS"
1130 :init-value nil
1131 :global t
1132 :group 'whitespace
1133 (cond
1134 (noninteractive ; running a batch job
1135 (setq global-whitespace-mode nil))
1136 (global-whitespace-mode ; global-whitespace-mode on
1137 (save-current-buffer
1138 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1139 (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1140 (dolist (buffer (buffer-list)) ; adjust all local mode
1141 (set-buffer buffer)
1142 (unless whitespace-mode
1143 (whitespace-turn-on-if-enabled)))))
1144 (t ; global-whitespace-mode off
1145 (save-current-buffer
1146 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1147 (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1148 (dolist (buffer (buffer-list)) ; adjust all local mode
1149 (set-buffer buffer)
1150 (unless whitespace-mode
1151 (whitespace-turn-off)))))))
1152
1153
1154 (defun whitespace-turn-on-if-enabled ()
1155 (when (cond
1156 ((eq whitespace-global-modes t))
1157 ((listp whitespace-global-modes)
1158 (if (eq (car-safe whitespace-global-modes) 'not)
1159 (not (memq major-mode (cdr whitespace-global-modes)))
1160 (memq major-mode whitespace-global-modes)))
1161 (t nil))
1162 (let (inhibit-quit)
1163 ;; Don't turn on whitespace mode if...
1164 (or
1165 ;; ...we don't have a display (we're running a batch job)
1166 noninteractive
1167 ;; ...or if the buffer is invisible (name starts with a space)
1168 (eq (aref (buffer-name) 0) ?\ )
1169 ;; ...or if the buffer is temporary (name starts with *)
1170 (and (eq (aref (buffer-name) 0) ?*)
1171 ;; except the scratch buffer.
1172 (not (string= (buffer-name) "*scratch*")))
1173 ;; Otherwise, turn on whitespace mode.
1174 (whitespace-turn-on)))))
1175
1176
1177 ;;;###autoload
1178 (define-minor-mode global-whitespace-newline-mode
1179 "Toggle global newline visualization (Global Whitespace Newline mode).
1180 With a prefix argument ARG, enable Global Whitespace Newline mode
1181 if ARG is positive, and disable it otherwise. If called from
1182 Lisp, enable it if ARG is omitted or nil.
1183
1184 Use `global-whitespace-newline-mode' only for NEWLINE
1185 visualization exclusively. For other visualizations, including
1186 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1187 please use `global-whitespace-mode'.
1188
1189 See also `whitespace-newline' and `whitespace-display-mappings'."
1190 :lighter " NL"
1191 :init-value nil
1192 :global t
1193 :group 'whitespace
1194 (let ((whitespace-style '(newline-mark newline)))
1195 (global-whitespace-mode (if global-whitespace-newline-mode
1196 1 -1))
1197 ;; sync states (running a batch job)
1198 (setq global-whitespace-newline-mode global-whitespace-mode)))
1199
1200 \f
1201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1202 ;;;; User commands - Toggle
1203
1204
1205 (defconst whitespace-style-value-list
1206 '(face
1207 tabs
1208 spaces
1209 trailing
1210 lines
1211 lines-tail
1212 newline
1213 empty
1214 indentation
1215 indentation::tab
1216 indentation::space
1217 space-after-tab
1218 space-after-tab::tab
1219 space-after-tab::space
1220 space-before-tab
1221 space-before-tab::tab
1222 space-before-tab::space
1223 help-newline ; value used by `whitespace-insert-option-mark'
1224 tab-mark
1225 space-mark
1226 newline-mark
1227 )
1228 "List of valid `whitespace-style' values.")
1229
1230
1231 (defconst whitespace-toggle-option-alist
1232 '((?f . face)
1233 (?t . tabs)
1234 (?s . spaces)
1235 (?r . trailing)
1236 (?l . lines)
1237 (?L . lines-tail)
1238 (?n . newline)
1239 (?e . empty)
1240 (?\C-i . indentation)
1241 (?I . indentation::tab)
1242 (?i . indentation::space)
1243 (?\C-a . space-after-tab)
1244 (?A . space-after-tab::tab)
1245 (?a . space-after-tab::space)
1246 (?\C-b . space-before-tab)
1247 (?B . space-before-tab::tab)
1248 (?b . space-before-tab::space)
1249 (?T . tab-mark)
1250 (?S . space-mark)
1251 (?N . newline-mark)
1252 (?x . whitespace-style)
1253 )
1254 "Alist of toggle options.
1255
1256 Each element has the form:
1257
1258 (CHAR . SYMBOL)
1259
1260 Where:
1261
1262 CHAR is a char which the user will have to type.
1263
1264 SYMBOL is a valid symbol associated with CHAR.
1265 See `whitespace-style-value-list'.")
1266
1267
1268 (defvar whitespace-active-style nil
1269 "Used to save locally `whitespace-style' value.")
1270
1271 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1272 "Used to save locally `indent-tabs-mode' value.")
1273
1274 (defvar whitespace-tab-width tab-width
1275 "Used to save locally `tab-width' value.")
1276
1277 (defvar whitespace-point (point)
1278 "Used to save locally current point value.
1279 Used by `whitespace-trailing-regexp' function (which see).")
1280
1281 (defvar whitespace-font-lock-refontify nil
1282 "Used to save locally the font-lock refontify state.
1283 Used by `whitespace-post-command-hook' function (which see).")
1284
1285 (defvar whitespace-bob-marker nil
1286 "Used to save locally the bob marker value.
1287 Used by `whitespace-post-command-hook' function (which see).")
1288
1289 (defvar whitespace-eob-marker nil
1290 "Used to save locally the eob marker value.
1291 Used by `whitespace-post-command-hook' function (which see).")
1292
1293 (defvar whitespace-buffer-changed nil
1294 "Used to indicate locally if buffer changed.
1295 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1296 functions (which see).")
1297
1298
1299 ;;;###autoload
1300 (defun whitespace-toggle-options (arg)
1301 "Toggle local `whitespace-mode' options.
1302
1303 If local whitespace-mode is off, toggle the option given by ARG
1304 and turn on local whitespace-mode.
1305
1306 If local whitespace-mode is on, toggle the option given by ARG
1307 and restart local whitespace-mode.
1308
1309 Interactively, it reads one of the following chars:
1310
1311 CHAR MEANING
1312 (VIA FACES)
1313 f toggle face visualization
1314 t toggle TAB visualization
1315 s toggle SPACE and HARD SPACE visualization
1316 r toggle trailing blanks visualization
1317 l toggle \"long lines\" visualization
1318 L toggle \"long lines\" tail visualization
1319 n toggle NEWLINE visualization
1320 e toggle empty line at bob and/or eob visualization
1321 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1322 I toggle indentation SPACEs visualization
1323 i toggle indentation TABs visualization
1324 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1325 A toggle SPACEs after TAB: SPACEs visualization
1326 a toggle SPACEs after TAB: TABs visualization
1327 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1328 B toggle SPACEs before TAB: SPACEs visualization
1329 b toggle SPACEs before TAB: TABs visualization
1330
1331 (VIA DISPLAY TABLE)
1332 T toggle TAB visualization
1333 S toggle SPACEs before TAB visualization
1334 N toggle NEWLINE visualization
1335
1336 x restore `whitespace-style' value
1337 ? display brief help
1338
1339 Non-interactively, ARG should be a symbol or a list of symbols.
1340 The valid symbols are:
1341
1342 face toggle face visualization
1343 tabs toggle TAB visualization
1344 spaces toggle SPACE and HARD SPACE visualization
1345 trailing toggle trailing blanks visualization
1346 lines toggle \"long lines\" visualization
1347 lines-tail toggle \"long lines\" tail visualization
1348 newline toggle NEWLINE visualization
1349 empty toggle empty line at bob and/or eob visualization
1350 indentation toggle indentation SPACEs visualization
1351 indentation::tab toggle indentation SPACEs visualization
1352 indentation::space toggle indentation TABs visualization
1353 space-after-tab toggle SPACEs after TAB visualization
1354 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1355 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1356 space-before-tab toggle SPACEs before TAB visualization
1357 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1358 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1359
1360 tab-mark toggle TAB visualization
1361 space-mark toggle SPACEs before TAB visualization
1362 newline-mark toggle NEWLINE visualization
1363
1364 whitespace-style restore `whitespace-style' value
1365
1366 See `whitespace-style' and `indent-tabs-mode' for documentation."
1367 (interactive (whitespace-interactive-char t))
1368 (let ((whitespace-style
1369 (whitespace-toggle-list t arg whitespace-active-style)))
1370 (whitespace-mode 0)
1371 (whitespace-mode 1)))
1372
1373
1374 (defvar whitespace-toggle-style nil
1375 "Used to toggle the global `whitespace-style' value.")
1376
1377
1378 ;;;###autoload
1379 (defun global-whitespace-toggle-options (arg)
1380 "Toggle global `whitespace-mode' options.
1381
1382 If global whitespace-mode is off, toggle the option given by ARG
1383 and turn on global whitespace-mode.
1384
1385 If global whitespace-mode is on, toggle the option given by ARG
1386 and restart global whitespace-mode.
1387
1388 Interactively, it accepts one of the following chars:
1389
1390 CHAR MEANING
1391 (VIA FACES)
1392 f toggle face visualization
1393 t toggle TAB visualization
1394 s toggle SPACE and HARD SPACE visualization
1395 r toggle trailing blanks visualization
1396 l toggle \"long lines\" visualization
1397 L toggle \"long lines\" tail visualization
1398 n toggle NEWLINE visualization
1399 e toggle empty line at bob and/or eob visualization
1400 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1401 I toggle indentation SPACEs visualization
1402 i toggle indentation TABs visualization
1403 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1404 A toggle SPACEs after TAB: SPACEs visualization
1405 a toggle SPACEs after TAB: TABs visualization
1406 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1407 B toggle SPACEs before TAB: SPACEs visualization
1408 b toggle SPACEs before TAB: TABs visualization
1409
1410 (VIA DISPLAY TABLE)
1411 T toggle TAB visualization
1412 S toggle SPACEs before TAB visualization
1413 N toggle NEWLINE visualization
1414
1415 x restore `whitespace-style' value
1416 ? display brief help
1417
1418 Non-interactively, ARG should be a symbol or a list of symbols.
1419 The valid symbols are:
1420
1421 face toggle face visualization
1422 tabs toggle TAB visualization
1423 spaces toggle SPACE and HARD SPACE visualization
1424 trailing toggle trailing blanks visualization
1425 lines toggle \"long lines\" visualization
1426 lines-tail toggle \"long lines\" tail visualization
1427 newline toggle NEWLINE visualization
1428 empty toggle empty line at bob and/or eob visualization
1429 indentation toggle indentation SPACEs visualization
1430 indentation::tab toggle indentation SPACEs visualization
1431 indentation::space toggle indentation TABs visualization
1432 space-after-tab toggle SPACEs after TAB visualization
1433 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1434 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1435 space-before-tab toggle SPACEs before TAB visualization
1436 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1437 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1438
1439 tab-mark toggle TAB visualization
1440 space-mark toggle SPACEs before TAB visualization
1441 newline-mark toggle NEWLINE visualization
1442
1443 whitespace-style restore `whitespace-style' value
1444
1445 See `whitespace-style' and `indent-tabs-mode' for documentation."
1446 (interactive (whitespace-interactive-char nil))
1447 (let ((whitespace-style
1448 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1449 (setq whitespace-toggle-style whitespace-style)
1450 (global-whitespace-mode 0)
1451 (global-whitespace-mode 1)))
1452
1453 \f
1454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1455 ;;;; User commands - Cleanup
1456
1457
1458 ;;;###autoload
1459 (defun whitespace-cleanup ()
1460 "Cleanup some blank problems in all buffer or at region.
1461
1462 It usually applies to the whole buffer, but in transient mark
1463 mode when the mark is active, it applies to the region. It also
1464 applies to the region when it is not in transient mark mode, the
1465 mark is active and \\[universal-argument] was pressed just before
1466 calling `whitespace-cleanup' interactively.
1467
1468 See also `whitespace-cleanup-region'.
1469
1470 The problems cleaned up are:
1471
1472 1. empty lines at beginning of buffer.
1473 2. empty lines at end of buffer.
1474 If `whitespace-style' includes the value `empty', remove all
1475 empty lines at beginning and/or end of buffer.
1476
1477 3. 8 or more SPACEs at beginning of line.
1478 If `whitespace-style' includes the value `indentation':
1479 replace 8 or more SPACEs at beginning of line by TABs, if
1480 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1481 SPACEs.
1482 If `whitespace-style' includes the value `indentation::tab',
1483 replace 8 or more SPACEs at beginning of line by TABs.
1484 If `whitespace-style' includes the value `indentation::space',
1485 replace TABs by SPACEs.
1486
1487 4. SPACEs before TAB.
1488 If `whitespace-style' includes the value `space-before-tab':
1489 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1490 otherwise, replace TABs by SPACEs.
1491 If `whitespace-style' includes the value
1492 `space-before-tab::tab', replace SPACEs by TABs.
1493 If `whitespace-style' includes the value
1494 `space-before-tab::space', replace TABs by SPACEs.
1495
1496 5. SPACEs or TABs at end of line.
1497 If `whitespace-style' includes the value `trailing', remove
1498 all SPACEs or TABs at end of line.
1499
1500 6. 8 or more SPACEs after TAB.
1501 If `whitespace-style' includes the value `space-after-tab':
1502 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1503 otherwise, replace TABs by SPACEs.
1504 If `whitespace-style' includes the value
1505 `space-after-tab::tab', replace SPACEs by TABs.
1506 If `whitespace-style' includes the value
1507 `space-after-tab::space', replace TABs by SPACEs.
1508
1509 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1510 documentation."
1511 (interactive "@")
1512 (cond
1513 ;; read-only buffer
1514 (buffer-read-only
1515 (whitespace-warn-read-only "cleanup"))
1516 ;; region active
1517 ((and (or transient-mark-mode
1518 current-prefix-arg)
1519 mark-active)
1520 ;; PROBLEMs 1 and 2 are not handled in region
1521 ;; PROBLEM 3: 8 or more SPACEs at bol
1522 ;; PROBLEM 4: SPACEs before TAB
1523 ;; PROBLEM 5: SPACEs or TABs at eol
1524 ;; PROBLEM 6: 8 or more SPACEs after TAB
1525 (whitespace-cleanup-region (region-beginning) (region-end)))
1526 ;; whole buffer
1527 (t
1528 (save-excursion
1529 (save-match-data ;FIXME: Why?
1530 ;; PROBLEM 1: empty lines at bob
1531 ;; PROBLEM 2: empty lines at eob
1532 ;; ACTION: remove all empty lines at bob and/or eob
1533 (when (memq 'empty whitespace-style)
1534 (let (overwrite-mode) ; enforce no overwrite
1535 (goto-char (point-min))
1536 (when (looking-at whitespace-empty-at-bob-regexp)
1537 (delete-region (match-beginning 1) (match-end 1)))
1538 (when (re-search-forward
1539 (concat whitespace-empty-at-eob-regexp "\\'") nil t)
1540 (delete-region (match-beginning 1) (match-end 1)))))))
1541 ;; PROBLEM 3: 8 or more SPACEs at bol
1542 ;; PROBLEM 4: SPACEs before TAB
1543 ;; PROBLEM 5: SPACEs or TABs at eol
1544 ;; PROBLEM 6: 8 or more SPACEs after TAB
1545 (whitespace-cleanup-region (point-min) (point-max)))))
1546
1547
1548 ;;;###autoload
1549 (defun whitespace-cleanup-region (start end)
1550 "Cleanup some blank problems at region.
1551
1552 The problems cleaned up are:
1553
1554 1. 8 or more SPACEs at beginning of line.
1555 If `whitespace-style' includes the value `indentation':
1556 replace 8 or more SPACEs at beginning of line by TABs, if
1557 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1558 SPACEs.
1559 If `whitespace-style' includes the value `indentation::tab',
1560 replace 8 or more SPACEs at beginning of line by TABs.
1561 If `whitespace-style' includes the value `indentation::space',
1562 replace TABs by SPACEs.
1563
1564 2. SPACEs before TAB.
1565 If `whitespace-style' includes the value `space-before-tab':
1566 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1567 otherwise, replace TABs by SPACEs.
1568 If `whitespace-style' includes the value
1569 `space-before-tab::tab', replace SPACEs by TABs.
1570 If `whitespace-style' includes the value
1571 `space-before-tab::space', replace TABs by SPACEs.
1572
1573 3. SPACEs or TABs at end of line.
1574 If `whitespace-style' includes the value `trailing', remove
1575 all SPACEs or TABs at end of line.
1576
1577 4. 8 or more SPACEs after TAB.
1578 If `whitespace-style' includes the value `space-after-tab':
1579 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1580 otherwise, replace TABs by SPACEs.
1581 If `whitespace-style' includes the value
1582 `space-after-tab::tab', replace SPACEs by TABs.
1583 If `whitespace-style' includes the value
1584 `space-after-tab::space', replace TABs by SPACEs.
1585
1586 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1587 documentation."
1588 (interactive "@r")
1589 (if buffer-read-only
1590 ;; read-only buffer
1591 (whitespace-warn-read-only "cleanup region")
1592 ;; non-read-only buffer
1593 (let ((rstart (min start end))
1594 (rend (copy-marker (max start end)))
1595 (indent-tabs-mode whitespace-indent-tabs-mode)
1596 (tab-width whitespace-tab-width)
1597 overwrite-mode ; enforce no overwrite
1598 tmp)
1599 (save-excursion
1600 (save-match-data ;FIXME: Why?
1601 ;; PROBLEM 1: 8 or more SPACEs at bol
1602 (cond
1603 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1604 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1605 ;; by SPACEs.
1606 ((memq 'indentation whitespace-style)
1607 (let ((regexp (whitespace-indentation-regexp)))
1608 (goto-char rstart)
1609 (while (re-search-forward regexp rend t)
1610 (setq tmp (current-indentation))
1611 (goto-char (match-beginning 0))
1612 (delete-horizontal-space)
1613 (unless (eolp)
1614 (indent-to tmp)))))
1615 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1616 ((memq 'indentation::tab whitespace-style)
1617 (whitespace-replace-action
1618 'tabify rstart rend
1619 (whitespace-indentation-regexp 'tab) 0))
1620 ;; ACTION: replace TABs by SPACEs.
1621 ((memq 'indentation::space whitespace-style)
1622 (whitespace-replace-action
1623 'untabify rstart rend
1624 (whitespace-indentation-regexp 'space) 0)))
1625 ;; PROBLEM 3: SPACEs or TABs at eol
1626 ;; ACTION: remove all SPACEs or TABs at eol
1627 (when (memq 'trailing whitespace-style)
1628 (whitespace-replace-action
1629 'delete-region rstart rend
1630 whitespace-trailing-regexp 1))
1631 ;; PROBLEM 4: 8 or more SPACEs after TAB
1632 (cond
1633 ;; ACTION: replace 8 or more SPACEs by TABs, if
1634 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1635 ;; by SPACEs.
1636 ((memq 'space-after-tab whitespace-style)
1637 (whitespace-replace-action
1638 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1639 rstart rend (whitespace-space-after-tab-regexp) 1))
1640 ;; ACTION: replace 8 or more SPACEs by TABs.
1641 ((memq 'space-after-tab::tab whitespace-style)
1642 (whitespace-replace-action
1643 'tabify rstart rend
1644 (whitespace-space-after-tab-regexp 'tab) 1))
1645 ;; ACTION: replace TABs by SPACEs.
1646 ((memq 'space-after-tab::space whitespace-style)
1647 (whitespace-replace-action
1648 'untabify rstart rend
1649 (whitespace-space-after-tab-regexp 'space) 1)))
1650 ;; PROBLEM 2: SPACEs before TAB
1651 (cond
1652 ;; ACTION: replace SPACEs before TAB by TABs, if
1653 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1654 ;; by SPACEs.
1655 ((memq 'space-before-tab whitespace-style)
1656 (whitespace-replace-action
1657 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1658 rstart rend whitespace-space-before-tab-regexp
1659 (if whitespace-indent-tabs-mode 0 2)))
1660 ;; ACTION: replace SPACEs before TAB by TABs.
1661 ((memq 'space-before-tab::tab whitespace-style)
1662 (whitespace-replace-action
1663 'tabify rstart rend
1664 whitespace-space-before-tab-regexp 0))
1665 ;; ACTION: replace TABs by SPACEs.
1666 ((memq 'space-before-tab::space whitespace-style)
1667 (whitespace-replace-action
1668 'untabify rstart rend
1669 whitespace-space-before-tab-regexp 2)))))
1670 (set-marker rend nil)))) ; point marker to nowhere
1671
1672
1673 (defun whitespace-replace-action (action rstart rend regexp index)
1674 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1675
1676 INDEX is the level group matched by REGEXP and used by ACTION.
1677
1678 See also `tab-width'."
1679 (goto-char rstart)
1680 (while (re-search-forward regexp rend t)
1681 (goto-char (match-end index))
1682 (funcall action (match-beginning index) (match-end index))))
1683
1684 \f
1685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1686 ;;;; User command - report
1687
1688
1689 (defun whitespace-regexp (regexp &optional kind)
1690 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1691 (cond
1692 ((or (eq kind 'tab)
1693 whitespace-indent-tabs-mode)
1694 (format (car regexp) whitespace-tab-width))
1695 ((or (eq kind 'space)
1696 (not whitespace-indent-tabs-mode))
1697 (cdr regexp))))
1698
1699
1700 (defun whitespace-indentation-regexp (&optional kind)
1701 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1702 (whitespace-regexp whitespace-indentation-regexp kind))
1703
1704
1705 (defun whitespace-space-after-tab-regexp (&optional kind)
1706 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1707 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1708
1709
1710 (defconst whitespace-report-list
1711 (list
1712 (cons 'empty whitespace-empty-at-bob-regexp)
1713 (cons 'empty whitespace-empty-at-eob-regexp)
1714 (cons 'trailing whitespace-trailing-regexp)
1715 (cons 'indentation nil)
1716 (cons 'indentation::tab nil)
1717 (cons 'indentation::space nil)
1718 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1719 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1720 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1721 (cons 'space-after-tab nil)
1722 (cons 'space-after-tab::tab nil)
1723 (cons 'space-after-tab::space nil)
1724 )
1725 "List of whitespace bogus symbol and corresponding regexp.")
1726
1727
1728 (defconst whitespace-report-text
1729 '( ;; `indent-tabs-mode' has non-nil value
1730 "\
1731 Whitespace Report
1732
1733 Current Setting Whitespace Problem
1734
1735 empty [] [] empty lines at beginning of buffer
1736 empty [] [] empty lines at end of buffer
1737 trailing [] [] SPACEs or TABs at end of line
1738 indentation [] [] 8 or more SPACEs at beginning of line
1739 indentation::tab [] [] 8 or more SPACEs at beginning of line
1740 indentation::space [] [] TABs at beginning of line
1741 space-before-tab [] [] SPACEs before TAB
1742 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1743 space-before-tab::space [] [] SPACEs before TAB: TABs
1744 space-after-tab [] [] 8 or more SPACEs after TAB
1745 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1746 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1747
1748 indent-tabs-mode =
1749 tab-width = \n\n"
1750 . ;; `indent-tabs-mode' has nil value
1751 "\
1752 Whitespace Report
1753
1754 Current Setting Whitespace Problem
1755
1756 empty [] [] empty lines at beginning of buffer
1757 empty [] [] empty lines at end of buffer
1758 trailing [] [] SPACEs or TABs at end of line
1759 indentation [] [] TABs at beginning of line
1760 indentation::tab [] [] 8 or more SPACEs at beginning of line
1761 indentation::space [] [] TABs at beginning of line
1762 space-before-tab [] [] SPACEs before TAB
1763 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1764 space-before-tab::space [] [] SPACEs before TAB: TABs
1765 space-after-tab [] [] 8 or more SPACEs after TAB
1766 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1767 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1768
1769 indent-tabs-mode =
1770 tab-width = \n\n")
1771 "Text for whitespace bogus report.
1772
1773 It is a cons of strings, where the car part is used when
1774 `indent-tabs-mode' is non-nil, and the cdr part is used when
1775 `indent-tabs-mode' is nil.")
1776
1777
1778 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1779 "The buffer name for whitespace bogus report.")
1780
1781
1782 ;;;###autoload
1783 (defun whitespace-report (&optional force report-if-bogus)
1784 "Report some whitespace problems in buffer.
1785
1786 Return nil if there is no whitespace problem; otherwise, return
1787 non-nil.
1788
1789 If FORCE is non-nil or \\[universal-argument] was pressed just
1790 before calling `whitespace-report' interactively, it forces
1791 `whitespace-style' to have:
1792
1793 empty
1794 trailing
1795 indentation
1796 space-before-tab
1797 space-after-tab
1798
1799 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1800 whitespace problems in buffer.
1801
1802 Report if some of the following whitespace problems exist:
1803
1804 * If `indent-tabs-mode' is non-nil:
1805 empty 1. empty lines at beginning of buffer.
1806 empty 2. empty lines at end of buffer.
1807 trailing 3. SPACEs or TABs at end of line.
1808 indentation 4. 8 or more SPACEs at beginning of line.
1809 space-before-tab 5. SPACEs before TAB.
1810 space-after-tab 6. 8 or more SPACEs after TAB.
1811
1812 * If `indent-tabs-mode' is nil:
1813 empty 1. empty lines at beginning of buffer.
1814 empty 2. empty lines at end of buffer.
1815 trailing 3. SPACEs or TABs at end of line.
1816 indentation 4. TABS at beginning of line.
1817 space-before-tab 5. SPACEs before TAB.
1818 space-after-tab 6. 8 or more SPACEs after TAB.
1819
1820 See `whitespace-style' for documentation.
1821 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1822 cleaning up these problems."
1823 (interactive (list current-prefix-arg))
1824 (whitespace-report-region (point-min) (point-max)
1825 force report-if-bogus))
1826
1827
1828 ;;;###autoload
1829 (defun whitespace-report-region (start end &optional force report-if-bogus)
1830 "Report some whitespace problems in a region.
1831
1832 Return nil if there is no whitespace problem; otherwise, return
1833 non-nil.
1834
1835 If FORCE is non-nil or \\[universal-argument] was pressed just
1836 before calling `whitespace-report-region' interactively, it
1837 forces `whitespace-style' to have:
1838
1839 empty
1840 indentation
1841 space-before-tab
1842 trailing
1843 space-after-tab
1844
1845 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1846 whitespace problems in buffer.
1847
1848 Report if some of the following whitespace problems exist:
1849
1850 * If `indent-tabs-mode' is non-nil:
1851 empty 1. empty lines at beginning of buffer.
1852 empty 2. empty lines at end of buffer.
1853 trailing 3. SPACEs or TABs at end of line.
1854 indentation 4. 8 or more SPACEs at beginning of line.
1855 space-before-tab 5. SPACEs before TAB.
1856 space-after-tab 6. 8 or more SPACEs after TAB.
1857
1858 * If `indent-tabs-mode' is nil:
1859 empty 1. empty lines at beginning of buffer.
1860 empty 2. empty lines at end of buffer.
1861 trailing 3. SPACEs or TABs at end of line.
1862 indentation 4. TABS at beginning of line.
1863 space-before-tab 5. SPACEs before TAB.
1864 space-after-tab 6. 8 or more SPACEs after TAB.
1865
1866 See `whitespace-style' for documentation.
1867 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1868 cleaning up these problems."
1869 (interactive "r")
1870 (setq force (or current-prefix-arg force))
1871 (save-excursion
1872 (save-match-data ;FIXME: Why?
1873 (let* ((has-bogus nil)
1874 (rstart (min start end))
1875 (rend (max start end))
1876 (bogus-list
1877 (mapcar
1878 #'(lambda (option)
1879 (when force
1880 (add-to-list 'whitespace-style (car option)))
1881 (goto-char rstart)
1882 (let ((regexp
1883 (cond
1884 ((eq (car option) 'indentation)
1885 (whitespace-indentation-regexp))
1886 ((eq (car option) 'indentation::tab)
1887 (whitespace-indentation-regexp 'tab))
1888 ((eq (car option) 'indentation::space)
1889 (whitespace-indentation-regexp 'space))
1890 ((eq (car option) 'space-after-tab)
1891 (whitespace-space-after-tab-regexp))
1892 ((eq (car option) 'space-after-tab::tab)
1893 (whitespace-space-after-tab-regexp 'tab))
1894 ((eq (car option) 'space-after-tab::space)
1895 (whitespace-space-after-tab-regexp 'space))
1896 (t
1897 (cdr option)))))
1898 (and (re-search-forward regexp rend t)
1899 (setq has-bogus t))))
1900 whitespace-report-list)))
1901 (when (if report-if-bogus has-bogus t)
1902 (whitespace-kill-buffer whitespace-report-buffer-name)
1903 ;; `whitespace-indent-tabs-mode' is local to current buffer
1904 ;; `whitespace-tab-width' is local to current buffer
1905 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1906 (ws-tab-width whitespace-tab-width))
1907 (with-current-buffer (get-buffer-create
1908 whitespace-report-buffer-name)
1909 (erase-buffer)
1910 (insert (if ws-indent-tabs-mode
1911 (car whitespace-report-text)
1912 (cdr whitespace-report-text)))
1913 (goto-char (point-min))
1914 (forward-line 3)
1915 (dolist (option whitespace-report-list)
1916 (forward-line 1)
1917 (whitespace-mark-x
1918 27 (memq (car option) whitespace-style))
1919 (whitespace-mark-x 7 (car bogus-list))
1920 (setq bogus-list (cdr bogus-list)))
1921 (forward-line 1)
1922 (whitespace-insert-value ws-indent-tabs-mode)
1923 (whitespace-insert-value ws-tab-width)
1924 (when has-bogus
1925 (goto-char (point-max))
1926 (insert " Type `M-x whitespace-cleanup'"
1927 " to cleanup the buffer.\n\n"
1928 " Type `M-x whitespace-cleanup-region'"
1929 " to cleanup a region.\n\n"))
1930 (whitespace-display-window (current-buffer)))))
1931 has-bogus))))
1932
1933 \f
1934 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1935 ;;;; Internal functions
1936
1937
1938 (defvar whitespace-font-lock-mode nil
1939 "Used to remember whether a buffer had font lock mode on or not.")
1940
1941 (defvar whitespace-font-lock nil
1942 "Used to remember whether a buffer initially had font lock on or not.")
1943
1944 (defvar whitespace-font-lock-keywords nil
1945 "Used to save locally `font-lock-keywords' value.")
1946
1947
1948 (defconst whitespace-help-text
1949 "\
1950 Whitespace Toggle Options | scroll up : SPC or > |
1951 | scroll down: M-SPC or < |
1952 FACES \\__________________________/
1953 [] f - toggle face visualization
1954 [] t - toggle TAB visualization
1955 [] s - toggle SPACE and HARD SPACE visualization
1956 [] r - toggle trailing blanks visualization
1957 [] l - toggle \"long lines\" visualization
1958 [] L - toggle \"long lines\" tail visualization
1959 [] n - toggle NEWLINE visualization
1960 [] e - toggle empty line at bob and/or eob visualization
1961 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1962 [] I - toggle indentation SPACEs visualization
1963 [] i - toggle indentation TABs visualization
1964 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1965 [] A - toggle SPACEs after TAB: SPACEs visualization
1966 [] a - toggle SPACEs after TAB: TABs visualization
1967 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1968 [] B - toggle SPACEs before TAB: SPACEs visualization
1969 [] b - toggle SPACEs before TAB: TABs visualization
1970
1971 DISPLAY TABLE
1972 [] T - toggle TAB visualization
1973 [] S - toggle SPACE and HARD SPACE visualization
1974 [] N - toggle NEWLINE visualization
1975
1976 x - restore `whitespace-style' value
1977
1978 ? - display this text\n\n"
1979 "Text for whitespace toggle options.")
1980
1981
1982 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1983 "The buffer name for whitespace toggle options.")
1984
1985
1986 (defun whitespace-insert-value (value)
1987 "Insert VALUE at column 20 of next line."
1988 (forward-line 1)
1989 (move-to-column 20 t)
1990 (insert (format "%s" value)))
1991
1992
1993 (defun whitespace-mark-x (nchars condition)
1994 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1995 (forward-char nchars)
1996 (insert (if condition "X" " ")))
1997
1998
1999 (defun whitespace-insert-option-mark (the-list the-value)
2000 "Insert the option mark ('X' or ' ') in toggle options buffer."
2001 (goto-char (point-min))
2002 (forward-line 2)
2003 (dolist (sym the-list)
2004 (if (eq sym 'help-newline)
2005 (forward-line 2)
2006 (forward-line 1)
2007 (whitespace-mark-x 2 (memq sym the-value)))))
2008
2009
2010 (defun whitespace-help-on (style)
2011 "Display the whitespace toggle options."
2012 (unless (get-buffer whitespace-help-buffer-name)
2013 (delete-other-windows)
2014 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
2015 (with-current-buffer buffer
2016 (erase-buffer)
2017 (insert whitespace-help-text)
2018 (whitespace-insert-option-mark
2019 whitespace-style-value-list style)
2020 (whitespace-display-window buffer)))))
2021
2022
2023 (defun whitespace-display-window (buffer)
2024 "Display BUFFER in a new window."
2025 (goto-char (point-min))
2026 (set-buffer-modified-p nil)
2027 (when (< (window-height) (* 2 window-min-height))
2028 (kill-buffer buffer)
2029 (error "Window height is too small; \
2030 can't split window to display whitespace toggle options"))
2031 (let ((win (split-window)))
2032 (set-window-buffer win buffer)
2033 (shrink-window-if-larger-than-buffer win)))
2034
2035
2036 (defun whitespace-kill-buffer (buffer-name)
2037 "Kill buffer BUFFER-NAME and windows related with it."
2038 (let ((buffer (get-buffer buffer-name)))
2039 (when buffer
2040 (delete-windows-on buffer)
2041 (kill-buffer buffer))))
2042
2043
2044 (defun whitespace-help-off ()
2045 "Remove the buffer and window of the whitespace toggle options."
2046 (whitespace-kill-buffer whitespace-help-buffer-name))
2047
2048
2049 (defun whitespace-help-scroll (&optional up)
2050 "Scroll help window, if it exists.
2051
2052 If UP is non-nil, scroll up; otherwise, scroll down."
2053 (condition-case nil
2054 (let ((buffer (get-buffer whitespace-help-buffer-name)))
2055 (if buffer
2056 (with-selected-window (get-buffer-window buffer)
2057 (if up
2058 (scroll-up 3)
2059 (scroll-down 3)))
2060 (ding)))
2061 ;; handler
2062 ((error)
2063 ;; just ignore error
2064 )))
2065
2066
2067 (defun whitespace-interactive-char (local-p)
2068 "Interactive function to read a char and return a symbol.
2069
2070 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2071 uses a global context.
2072
2073 It accepts one of the following chars:
2074
2075 CHAR MEANING
2076 (VIA FACES)
2077 f toggle face visualization
2078 t toggle TAB visualization
2079 s toggle SPACE and HARD SPACE visualization
2080 r toggle trailing blanks visualization
2081 l toggle \"long lines\" visualization
2082 L toggle \"long lines\" tail visualization
2083 n toggle NEWLINE visualization
2084 e toggle empty line at bob and/or eob visualization
2085 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2086 I toggle indentation SPACEs visualization
2087 i toggle indentation TABs visualization
2088 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2089 A toggle SPACEs after TAB: SPACEs visualization
2090 a toggle SPACEs after TAB: TABs visualization
2091 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2092 B toggle SPACEs before TAB: SPACEs visualization
2093 b toggle SPACEs before TAB: TABs visualization
2094
2095 (VIA DISPLAY TABLE)
2096 T toggle TAB visualization
2097 S toggle SPACE and HARD SPACE visualization
2098 N toggle NEWLINE visualization
2099
2100 x restore `whitespace-style' value
2101 ? display brief help
2102
2103 See also `whitespace-toggle-option-alist'."
2104 (let* ((is-off (not (if local-p
2105 whitespace-mode
2106 global-whitespace-mode)))
2107 (style (cond (is-off whitespace-style) ; use default value
2108 (local-p whitespace-active-style)
2109 (t whitespace-toggle-style)))
2110 (prompt
2111 (format "Whitespace Toggle %s (type ? for further options)-"
2112 (if local-p "Local" "Global")))
2113 ch sym)
2114 ;; read a valid option and get the corresponding symbol
2115 (save-window-excursion
2116 (condition-case data
2117 (progn
2118 (while
2119 ;; while condition
2120 (progn
2121 (setq ch (read-char prompt))
2122 (not
2123 (setq sym
2124 (cdr
2125 (assq ch whitespace-toggle-option-alist)))))
2126 ;; while body
2127 (cond
2128 ((eq ch ?\?) (whitespace-help-on style))
2129 ((eq ch ?\ ) (whitespace-help-scroll t))
2130 ((eq ch ?\M- ) (whitespace-help-scroll))
2131 ((eq ch ?>) (whitespace-help-scroll t))
2132 ((eq ch ?<) (whitespace-help-scroll))
2133 (t (ding))))
2134 (whitespace-help-off)
2135 (message " ")) ; clean echo area
2136 ;; handler
2137 ((quit error)
2138 (whitespace-help-off)
2139 (error (error-message-string data)))))
2140 (list sym))) ; return the appropriate symbol
2141
2142
2143 (defun whitespace-toggle-list (local-p arg the-list)
2144 "Toggle options in THE-LIST based on list ARG.
2145
2146 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2147 uses a global context.
2148
2149 ARG is a list of options to be toggled.
2150
2151 THE-LIST is a list of options. This list will be toggled and the
2152 resultant list will be returned."
2153 (unless (if local-p whitespace-mode global-whitespace-mode)
2154 (setq the-list whitespace-style))
2155 (setq the-list (copy-sequence the-list)) ; keep original list
2156 (dolist (sym (if (listp arg) arg (list arg)))
2157 (cond
2158 ;; ignore help value
2159 ((eq sym 'help-newline))
2160 ;; restore default values
2161 ((eq sym 'whitespace-style)
2162 (setq the-list whitespace-style))
2163 ;; toggle valid values
2164 ((memq sym whitespace-style-value-list)
2165 (setq the-list (if (memq sym the-list)
2166 (delq sym the-list)
2167 (cons sym the-list))))))
2168 the-list)
2169
2170
2171 (defvar whitespace-display-table nil
2172 "Used to save a local display table.")
2173
2174 (defvar whitespace-display-table-was-local nil
2175 "Used to remember whether a buffer initially had a local display table.")
2176
2177
2178 (defun whitespace-turn-on ()
2179 "Turn on whitespace visualization."
2180 ;; prepare local hooks
2181 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2182 ;; create whitespace local buffer environment
2183 (set (make-local-variable 'whitespace-font-lock-mode) nil)
2184 (set (make-local-variable 'whitespace-font-lock) nil)
2185 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2186 (set (make-local-variable 'whitespace-display-table) nil)
2187 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2188 (set (make-local-variable 'whitespace-active-style)
2189 (if (listp whitespace-style)
2190 whitespace-style
2191 (list whitespace-style)))
2192 (set (make-local-variable 'whitespace-indent-tabs-mode)
2193 indent-tabs-mode)
2194 (set (make-local-variable 'whitespace-tab-width)
2195 tab-width)
2196 ;; turn on whitespace
2197 (when whitespace-active-style
2198 (whitespace-color-on)
2199 (whitespace-display-char-on)))
2200
2201
2202 (defun whitespace-turn-off ()
2203 "Turn off whitespace visualization."
2204 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2205 (when whitespace-active-style
2206 (whitespace-color-off)
2207 (whitespace-display-char-off)))
2208
2209
2210 (defun whitespace-style-face-p ()
2211 "Return t if there is some visualization via face."
2212 (and (memq 'face whitespace-active-style)
2213 (or (memq 'tabs whitespace-active-style)
2214 (memq 'spaces whitespace-active-style)
2215 (memq 'trailing whitespace-active-style)
2216 (memq 'lines whitespace-active-style)
2217 (memq 'lines-tail whitespace-active-style)
2218 (memq 'newline whitespace-active-style)
2219 (memq 'empty whitespace-active-style)
2220 (memq 'indentation whitespace-active-style)
2221 (memq 'indentation::tab whitespace-active-style)
2222 (memq 'indentation::space whitespace-active-style)
2223 (memq 'space-after-tab whitespace-active-style)
2224 (memq 'space-after-tab::tab whitespace-active-style)
2225 (memq 'space-after-tab::space whitespace-active-style)
2226 (memq 'space-before-tab whitespace-active-style)
2227 (memq 'space-before-tab::tab whitespace-active-style)
2228 (memq 'space-before-tab::space whitespace-active-style))))
2229
2230
2231 (defun whitespace-color-on ()
2232 "Turn on color visualization."
2233 (when (whitespace-style-face-p)
2234 (unless whitespace-font-lock
2235 (setq whitespace-font-lock t
2236 whitespace-font-lock-keywords
2237 (copy-sequence font-lock-keywords)))
2238 ;; save current point and refontify when necessary
2239 (set (make-local-variable 'whitespace-point)
2240 (point))
2241 (set (make-local-variable 'whitespace-font-lock-refontify)
2242 0)
2243 (set (make-local-variable 'whitespace-bob-marker)
2244 (point-min-marker))
2245 (set (make-local-variable 'whitespace-eob-marker)
2246 (point-max-marker))
2247 (set (make-local-variable 'whitespace-buffer-changed)
2248 nil)
2249 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
2250 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
2251 ;; turn off font lock
2252 (set (make-local-variable 'whitespace-font-lock-mode)
2253 font-lock-mode)
2254 (font-lock-mode 0)
2255 ;; add whitespace-mode color into font lock
2256 (when (memq 'spaces whitespace-active-style)
2257 (font-lock-add-keywords
2258 nil
2259 (list
2260 ;; Show SPACEs
2261 (list whitespace-space-regexp 1 whitespace-space t)
2262 ;; Show HARD SPACEs
2263 (list whitespace-hspace-regexp 1 whitespace-hspace t))
2264 t))
2265 (when (memq 'tabs whitespace-active-style)
2266 (font-lock-add-keywords
2267 nil
2268 (list
2269 ;; Show TABs
2270 (list whitespace-tab-regexp 1 whitespace-tab t))
2271 t))
2272 (when (memq 'trailing whitespace-active-style)
2273 (font-lock-add-keywords
2274 nil
2275 (list
2276 ;; Show trailing blanks
2277 (list #'whitespace-trailing-regexp 1 whitespace-trailing t))
2278 t))
2279 (when (or (memq 'lines whitespace-active-style)
2280 (memq 'lines-tail whitespace-active-style))
2281 (font-lock-add-keywords
2282 nil
2283 (list
2284 ;; Show "long" lines
2285 (list
2286 (let ((line-column (or whitespace-line-column fill-column)))
2287 (format
2288 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2289 whitespace-tab-width
2290 (1- whitespace-tab-width)
2291 (/ line-column whitespace-tab-width)
2292 (let ((rem (% line-column whitespace-tab-width)))
2293 (if (zerop rem)
2294 ""
2295 (format ".\\{%d\\}" rem)))))
2296 (if (memq 'lines whitespace-active-style)
2297 0 ; whole line
2298 2) ; line tail
2299 whitespace-line t))
2300 t))
2301 (cond
2302 ((memq 'space-before-tab whitespace-active-style)
2303 (font-lock-add-keywords
2304 nil
2305 (list
2306 ;; Show SPACEs before TAB (indent-tabs-mode)
2307 (list whitespace-space-before-tab-regexp
2308 (if whitespace-indent-tabs-mode 1 2)
2309 whitespace-space-before-tab t))
2310 t))
2311 ((memq 'space-before-tab::tab whitespace-active-style)
2312 (font-lock-add-keywords
2313 nil
2314 (list
2315 ;; Show SPACEs before TAB (SPACEs)
2316 (list whitespace-space-before-tab-regexp
2317 1 whitespace-space-before-tab t))
2318 t))
2319 ((memq 'space-before-tab::space whitespace-active-style)
2320 (font-lock-add-keywords
2321 nil
2322 (list
2323 ;; Show SPACEs before TAB (TABs)
2324 (list whitespace-space-before-tab-regexp
2325 2 whitespace-space-before-tab t))
2326 t)))
2327 (cond
2328 ((memq 'indentation whitespace-active-style)
2329 (font-lock-add-keywords
2330 nil
2331 (list
2332 ;; Show indentation SPACEs (indent-tabs-mode)
2333 (list (whitespace-indentation-regexp)
2334 1 whitespace-indentation t))
2335 t))
2336 ((memq 'indentation::tab whitespace-active-style)
2337 (font-lock-add-keywords
2338 nil
2339 (list
2340 ;; Show indentation SPACEs (SPACEs)
2341 (list (whitespace-indentation-regexp 'tab)
2342 1 whitespace-indentation t))
2343 t))
2344 ((memq 'indentation::space whitespace-active-style)
2345 (font-lock-add-keywords
2346 nil
2347 (list
2348 ;; Show indentation SPACEs (TABs)
2349 (list (whitespace-indentation-regexp 'space)
2350 1 whitespace-indentation t))
2351 t)))
2352 (when (memq 'empty whitespace-active-style)
2353 (font-lock-add-keywords
2354 nil
2355 (list
2356 ;; Show empty lines at beginning of buffer
2357 (list #'whitespace-empty-at-bob-regexp
2358 1 whitespace-empty t))
2359 t)
2360 (font-lock-add-keywords
2361 nil
2362 (list
2363 ;; Show empty lines at end of buffer
2364 (list #'whitespace-empty-at-eob-regexp
2365 1 whitespace-empty t))
2366 t))
2367 (cond
2368 ((memq 'space-after-tab whitespace-active-style)
2369 (font-lock-add-keywords
2370 nil
2371 (list
2372 ;; Show SPACEs after TAB (indent-tabs-mode)
2373 (list (whitespace-space-after-tab-regexp)
2374 1 whitespace-space-after-tab t))
2375 t))
2376 ((memq 'space-after-tab::tab whitespace-active-style)
2377 (font-lock-add-keywords
2378 nil
2379 (list
2380 ;; Show SPACEs after TAB (SPACEs)
2381 (list (whitespace-space-after-tab-regexp 'tab)
2382 1 whitespace-space-after-tab t))
2383 t))
2384 ((memq 'space-after-tab::space whitespace-active-style)
2385 (font-lock-add-keywords
2386 nil
2387 (list
2388 ;; Show SPACEs after TAB (TABs)
2389 (list (whitespace-space-after-tab-regexp 'space)
2390 1 whitespace-space-after-tab t))
2391 t)))
2392 ;; now turn on font lock and highlight blanks
2393 (font-lock-mode 1)))
2394
2395
2396 (defun whitespace-color-off ()
2397 "Turn off color visualization."
2398 ;; turn off font lock
2399 (when (whitespace-style-face-p)
2400 (font-lock-mode 0)
2401 (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
2402 (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
2403 (when whitespace-font-lock
2404 (setq whitespace-font-lock nil
2405 font-lock-keywords whitespace-font-lock-keywords))
2406 ;; restore original font lock state
2407 (font-lock-mode whitespace-font-lock-mode)))
2408
2409
2410 (defun whitespace-trailing-regexp (limit)
2411 "Match trailing spaces which do not contain the point at end of line."
2412 (let ((status t))
2413 (while (if (re-search-forward whitespace-trailing-regexp limit t)
2414 (= whitespace-point (match-end 1)) ;; loop if point at eol
2415 (setq status nil))) ;; end of buffer
2416 status))
2417
2418
2419 (defun whitespace-empty-at-bob-regexp (limit)
2420 "Match spaces at beginning of buffer which do not contain the point at \
2421 beginning of buffer."
2422 (let ((b (point))
2423 r)
2424 (cond
2425 ;; at bob
2426 ((= b 1)
2427 (setq r (and (/= whitespace-point 1)
2428 (looking-at whitespace-empty-at-bob-regexp)))
2429 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2430 ;; inside bob empty region
2431 ((<= limit whitespace-bob-marker)
2432 (setq r (looking-at whitespace-empty-at-bob-regexp))
2433 (if r
2434 (when (< (match-end 1) limit)
2435 (set-marker whitespace-bob-marker (match-end 1)))
2436 (set-marker whitespace-bob-marker b)))
2437 ;; intersection with end of bob empty region
2438 ((<= b whitespace-bob-marker)
2439 (setq r (looking-at whitespace-empty-at-bob-regexp))
2440 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2441 ;; it is not inside bob empty region
2442 (t
2443 (setq r nil)))
2444 ;; move to end of matching
2445 (and r (goto-char (match-end 1)))
2446 r))
2447
2448
2449 (defsubst whitespace-looking-back (regexp limit)
2450 (save-excursion
2451 (when (/= 0 (skip-chars-backward " \t\n" limit))
2452 (unless (bolp)
2453 (forward-line 1))
2454 (looking-at regexp))))
2455
2456
2457 (defun whitespace-empty-at-eob-regexp (limit)
2458 "Match spaces at end of buffer which do not contain the point at end of \
2459 buffer."
2460 (let ((b (point))
2461 (e (1+ (buffer-size)))
2462 r)
2463 (cond
2464 ;; at eob
2465 ((= limit e)
2466 (when (/= whitespace-point e)
2467 (goto-char limit)
2468 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
2469 (if r
2470 (set-marker whitespace-eob-marker (match-beginning 1))
2471 (set-marker whitespace-eob-marker limit)
2472 (goto-char b))) ; return back to initial position
2473 ;; inside eob empty region
2474 ((>= b whitespace-eob-marker)
2475 (goto-char limit)
2476 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2477 (if r
2478 (when (> (match-beginning 1) b)
2479 (set-marker whitespace-eob-marker (match-beginning 1)))
2480 (set-marker whitespace-eob-marker limit)
2481 (goto-char b))) ; return back to initial position
2482 ;; intersection with beginning of eob empty region
2483 ((>= limit whitespace-eob-marker)
2484 (goto-char limit)
2485 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2486 (if r
2487 (set-marker whitespace-eob-marker (match-beginning 1))
2488 (set-marker whitespace-eob-marker limit)
2489 (goto-char b))) ; return back to initial position
2490 ;; it is not inside eob empty region
2491 (t
2492 (setq r nil)))
2493 r))
2494
2495
2496 (defun whitespace-buffer-changed (_beg _end)
2497 "Set `whitespace-buffer-changed' variable to t."
2498 (setq whitespace-buffer-changed t))
2499
2500
2501 (defun whitespace-post-command-hook ()
2502 "Save current point into `whitespace-point' variable.
2503 Also refontify when necessary."
2504 (setq whitespace-point (point)) ; current point position
2505 (let ((refontify
2506 (or
2507 ;; it is at end of line ...
2508 (and (eolp)
2509 ;; ... with trailing SPACE or TAB
2510 (or (= (preceding-char) ?\ )
2511 (= (preceding-char) ?\t)))
2512 ;; it is at beginning of buffer (bob)
2513 (= whitespace-point 1)
2514 ;; the buffer was modified and ...
2515 (and whitespace-buffer-changed
2516 (or
2517 ;; ... or inside bob whitespace region
2518 (<= whitespace-point whitespace-bob-marker)
2519 ;; ... or at bob whitespace region border
2520 (and (= whitespace-point (1+ whitespace-bob-marker))
2521 (= (preceding-char) ?\n))))
2522 ;; it is at end of buffer (eob)
2523 (= whitespace-point (1+ (buffer-size)))
2524 ;; the buffer was modified and ...
2525 (and whitespace-buffer-changed
2526 (or
2527 ;; ... or inside eob whitespace region
2528 (>= whitespace-point whitespace-eob-marker)
2529 ;; ... or at eob whitespace region border
2530 (and (= whitespace-point (1- whitespace-eob-marker))
2531 (= (following-char) ?\n)))))))
2532 (when (or refontify (> whitespace-font-lock-refontify 0))
2533 (setq whitespace-buffer-changed nil)
2534 ;; adjust refontify counter
2535 (setq whitespace-font-lock-refontify
2536 (if refontify
2537 1
2538 (1- whitespace-font-lock-refontify)))
2539 ;; refontify
2540 (jit-lock-refontify))))
2541
2542 \f
2543 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2544 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2545
2546
2547 (defun whitespace-style-mark-p ()
2548 "Return t if there is some visualization via display table."
2549 (or (memq 'tab-mark whitespace-active-style)
2550 (memq 'space-mark whitespace-active-style)
2551 (memq 'newline-mark whitespace-active-style)))
2552
2553
2554 (defsubst whitespace-char-valid-p (char)
2555 ;; This check should be improved!!!
2556 (or (< char 256)
2557 (characterp char)))
2558
2559
2560 (defun whitespace-display-vector-p (vec)
2561 "Return true if every character in vector VEC can be displayed."
2562 (let ((i (length vec)))
2563 (when (> i 0)
2564 (while (and (>= (setq i (1- i)) 0)
2565 (whitespace-char-valid-p (aref vec i))))
2566 (< i 0))))
2567
2568
2569 (defun whitespace-display-char-on ()
2570 "Turn on character display mapping."
2571 (when (and whitespace-display-mappings
2572 (whitespace-style-mark-p))
2573 (let (vecs vec)
2574 ;; Remember whether a buffer has a local display table.
2575 (unless whitespace-display-table-was-local
2576 (setq whitespace-display-table-was-local t
2577 whitespace-display-table
2578 (copy-sequence buffer-display-table))
2579 ;; Assure `buffer-display-table' is unique
2580 ;; when two or more windows are visible.
2581 (setq buffer-display-table
2582 (copy-sequence buffer-display-table)))
2583 (unless buffer-display-table
2584 (setq buffer-display-table (make-display-table)))
2585 (dolist (entry whitespace-display-mappings)
2586 ;; check if it is to display this mark
2587 (when (memq (car entry) whitespace-style)
2588 ;; Get a displayable mapping.
2589 (setq vecs (cddr entry))
2590 (while (and vecs
2591 (not (whitespace-display-vector-p (car vecs))))
2592 (setq vecs (cdr vecs)))
2593 ;; Display a valid mapping.
2594 (when vecs
2595 (setq vec (copy-sequence (car vecs)))
2596 ;; NEWLINE char
2597 (when (and (eq (cadr entry) ?\n)
2598 (memq 'newline whitespace-active-style))
2599 ;; Only insert face bits on NEWLINE char mapping to avoid
2600 ;; obstruction of other faces like TABs and (HARD) SPACEs
2601 ;; faces, font-lock faces, etc.
2602 (dotimes (i (length vec))
2603 (or (eq (aref vec i) ?\n)
2604 (aset vec i
2605 (make-glyph-code (aref vec i)
2606 whitespace-newline)))))
2607 ;; Display mapping
2608 (aset buffer-display-table (cadr entry) vec)))))))
2609
2610
2611 (defun whitespace-display-char-off ()
2612 "Turn off character display mapping."
2613 (and whitespace-display-mappings
2614 (whitespace-style-mark-p)
2615 whitespace-display-table-was-local
2616 (setq whitespace-display-table-was-local nil
2617 buffer-display-table whitespace-display-table)))
2618
2619 \f
2620 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2621 ;;;; Hook
2622
2623
2624 (defun whitespace-action-when-on ()
2625 "Action to be taken always when local whitespace is turned on."
2626 (cond ((memq 'cleanup whitespace-action)
2627 (whitespace-cleanup))
2628 ((memq 'report-on-bogus whitespace-action)
2629 (whitespace-report nil t))))
2630
2631
2632 (defun whitespace-write-file-hook ()
2633 "Action to be taken when buffer is written.
2634 It should be added buffer-locally to `write-file-functions'."
2635 (cond ((memq 'auto-cleanup whitespace-action)
2636 (whitespace-cleanup))
2637 ((memq 'abort-on-bogus whitespace-action)
2638 (when (whitespace-report nil t)
2639 (error "Abort write due to whitespace problems in %s"
2640 (buffer-name)))))
2641 nil) ; continue hook processing
2642
2643
2644 (defun whitespace-warn-read-only (msg)
2645 "Warn if buffer is read-only."
2646 (when (memq 'warn-if-read-only whitespace-action)
2647 (message "Can't %s: %s is read-only" msg (buffer-name))))
2648
2649 \f
2650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2651
2652
2653 (defun whitespace-unload-function ()
2654 "Unload the whitespace library."
2655 (global-whitespace-mode -1)
2656 ;; be sure all local whitespace mode is turned off
2657 (save-current-buffer
2658 (dolist (buf (buffer-list))
2659 (set-buffer buf)
2660 (whitespace-mode -1)))
2661 nil) ; continue standard unloading
2662
2663
2664 (provide 'whitespace)
2665
2666
2667 (run-hooks 'whitespace-load-hook)
2668
2669
2670 ;;; whitespace.el ends here