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