Boring merge from savannah.
[emacs.git] / lisp / whitespace.el
blob8dc1996734dd5204e007556e22ae60744f1ef159
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000-2011 Free Software Foundation, Inc.
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.1
9 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;;; Commentary:
28 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Introduction
31 ;; ------------
33 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
34 ;; and NEWLINE).
36 ;; whitespace uses two ways to visualize blanks: faces and display
37 ;; table.
39 ;; * Faces are used to highlight the background with a color.
40 ;; whitespace uses font-lock to highlight blank characters.
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).
46 ;; The `whitespace-style' variable selects which way blanks are
47 ;; visualized.
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.
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).
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.
67 ;; There are two ways of using whitespace: local and global.
69 ;; * Local whitespace affects only the current buffer.
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.
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.
82 ;; When global and local whitespace are on:
84 ;; * if local whitespace is turned off, whitespace is turned off for
85 ;; the current buffer only.
87 ;; * if global whitespace is turned off, whitespace continues on only
88 ;; in the buffers in which local whitespace is on.
90 ;; To use whitespace, insert in your ~/.emacs:
92 ;; (require 'whitespace)
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:
98 ;; (autoload 'whitespace-mode "whitespace"
99 ;; "Toggle whitespace visualization." t)
100 ;; (autoload 'whitespace-toggle-options "whitespace"
101 ;; "Toggle local `whitespace-mode' options." t)
103 ;; whitespace was inspired by:
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'.
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)
116 ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li>
117 ;; Major mode for editing Whitespace
118 ;; (inspired the idea to use display table)
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)
126 ;; Using whitespace
127 ;; ----------------
129 ;; There is no problem if you mix local and global minor mode usage.
131 ;; * LOCAL whitespace:
132 ;; + To toggle whitespace options locally, type:
134 ;; M-x whitespace-toggle-options RET
136 ;; + To activate whitespace locally, type:
138 ;; C-u 1 M-x whitespace-mode RET
140 ;; + To deactivate whitespace locally, type:
142 ;; C-u 0 M-x whitespace-mode RET
144 ;; + To toggle whitespace locally, type:
146 ;; M-x whitespace-mode RET
148 ;; * GLOBAL whitespace:
149 ;; + To toggle whitespace options globally, type:
151 ;; M-x global-whitespace-toggle-options RET
153 ;; + To activate whitespace globally, type:
155 ;; C-u 1 M-x global-whitespace-mode RET
157 ;; + To deactivate whitespace globally, type:
159 ;; C-u 0 M-x global-whitespace-mode RET
161 ;; + To toggle whitespace globally, type:
163 ;; M-x global-whitespace-mode RET
165 ;; There are also the following useful commands:
167 ;; `whitespace-newline-mode'
168 ;; Toggle NEWLINE minor mode visualization ("nl" on modeline).
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on modeline).
173 ;; `whitespace-report'
174 ;; Report some blank problems in buffer.
176 ;; `whitespace-report-region'
177 ;; Report some blank problems in a region.
179 ;; `whitespace-cleanup'
180 ;; Cleanup some blank problems in all buffer or at region.
182 ;; `whitespace-cleanup-region'
183 ;; Cleanup some blank problems at region.
185 ;; The problems, which are cleaned up, are:
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.
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.
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.
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.
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.
225 ;; Hooks
226 ;; -----
228 ;; whitespace has the following hook variables:
230 ;; `whitespace-mode-hook'
231 ;; It is evaluated always when whitespace is turned on locally.
233 ;; `global-whitespace-mode-hook'
234 ;; It is evaluated always when whitespace is turned on globally.
236 ;; `whitespace-load-hook'
237 ;; It is evaluated after whitespace package is loaded.
240 ;; Options
241 ;; -------
243 ;; Below it's shown a brief description of whitespace options, please,
244 ;; see the options declaration in the code for a long documentation.
246 ;; `whitespace-style' Specify which kind of blank is
247 ;; visualized.
249 ;; `whitespace-space' Face used to visualize SPACE.
251 ;; `whitespace-hspace' Face used to visualize HARD SPACE.
253 ;; `whitespace-tab' Face used to visualize TAB.
255 ;; `whitespace-newline' Face used to visualize NEWLINE char
256 ;; mapping.
258 ;; `whitespace-trailing' Face used to visualize trailing
259 ;; blanks.
261 ;; `whitespace-line' Face used to visualize "long" lines.
263 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
264 ;; before TAB.
266 ;; `whitespace-indentation' Face used to visualize 8 or more
267 ;; SPACEs at beginning of line.
269 ;; `whitespace-empty' Face used to visualize empty lines at
270 ;; beginning and/or end of buffer.
272 ;; `whitespace-space-after-tab' Face used to visualize 8 or more
273 ;; SPACEs after TAB.
275 ;; `whitespace-space-regexp' Specify SPACE characters regexp.
277 ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
279 ;; `whitespace-tab-regexp' Specify TAB characters regexp.
281 ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
283 ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
284 ;; regexp.
286 ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
287 ;; SPACEs at beginning of line.
289 ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
290 ;; at beginning of buffer.
292 ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
293 ;; at end of buffer.
295 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
296 ;; SPACEs after TAB.
298 ;; `whitespace-line-column' Specify column beyond which the line
299 ;; is highlighted.
301 ;; `whitespace-display-mappings' Specify an alist of mappings
302 ;; for displaying characters.
304 ;; `whitespace-global-modes' Modes for which global
305 ;; `whitespace-mode' is automagically
306 ;; turned on.
308 ;; `whitespace-action' Specify which action is taken when a
309 ;; buffer is visited or written.
312 ;; Acknowledgements
313 ;; ----------------
315 ;; Thanks to felix (EmacsWiki) for keeping highlight when switching between
316 ;; major modes on a file.
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.
322 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
323 ;; `indent-tabs-mode' usage suggestion.
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.
329 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
330 ;; lines tail. See EightyColumnRule (EmacsWiki).
332 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
333 ;; * `define-minor-mode'.
334 ;; * `global-whitespace-*' name for global commands.
336 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
338 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
339 ;; suggestion.
341 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
342 ;; helping to fix `find-file-hooks' reference.
344 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
345 ;; indicating defface byte-compilation warnings.
347 ;; Thanks to TimOCallaghan (EmacsWiki) for the idea about highlight
348 ;; "long" lines. See EightyColumnRule (EmacsWiki).
350 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
351 ;; NEWLINE character mapping.
353 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
354 ;; whitespace-mode.el on XEmacs.
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).
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.
367 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;;; code:
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;;;; User Variables:
376 ;;; Interface to the command system
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)
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.
393 It's a list containing some or all of the following values:
395 face enable all visualization via faces (see below).
397 trailing trailing blanks are visualized via faces.
398 It has effect only if `face' (see above)
399 is present in `whitespace-style'.
401 tabs TABs are visualized via faces.
402 It has effect only if `face' (see above)
403 is present in `whitespace-style'.
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'.
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'.
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'.
429 newline NEWLINEs are visualized via faces.
430 It has effect only if `face' (see above)
431 is present in `whitespace-style'.
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'.
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'.
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'.
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'.
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'.
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'.
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'.
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'.
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'.
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'.
489 space-mark SPACEs and HARD SPACEs are visualized via
490 display table.
492 tab-mark TABs are visualized via display table.
494 newline-mark NEWLINEs are visualized via display table.
496 Any other value is ignored.
498 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
499 via display table.
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:
507 * For indentation:
508 1. indentation
509 2. indentation::tab
510 3. indentation::space
512 * For SPACEs after TABs:
513 1. space-after-tab
514 2. space-after-tab::tab
515 3. space-after-tab::space
517 * For SPACEs before TABs:
518 1. space-before-tab
519 2. space-before-tab::tab
520 3. space-before-tab::space
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.
526 One reason for not visualize spaces via faces (if `face' is not
527 included in `whitespace-style') is to use exclusively for
528 cleanning up a buffer. See `whitespace-cleanup' and
529 `whitespace-cleanup-region' for documentation.
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)
558 (defcustom whitespace-space 'whitespace-space
559 "Symbol face used to visualize SPACE.
561 Used when `whitespace-style' includes the value `spaces'."
562 :type 'face
563 :group 'whitespace)
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)
576 (defcustom whitespace-hspace 'whitespace-hspace
577 "Symbol face used to visualize HARD SPACE.
579 Used when `whitespace-style' includes the value `spaces'."
580 :type 'face
581 :group 'whitespace)
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)
594 (defcustom whitespace-tab 'whitespace-tab
595 "Symbol face used to visualize TAB.
597 Used when `whitespace-style' includes the value `tabs'."
598 :type 'face
599 :group 'whitespace)
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)
612 (defcustom whitespace-newline 'whitespace-newline
613 "Symbol face used to visualize NEWLINE char mapping.
615 See `whitespace-display-mappings'.
617 Used when `whitespace-style' includes the values `newline-mark'
618 and `newline'."
619 :type 'face
620 :group 'whitespace)
623 (defface whitespace-newline
624 '((((class color) (background dark))
625 (:foreground "darkgray" :bold nil))
626 (((class color) (background light))
627 (:foreground "lightgray" :bold nil))
628 (t (:underline t :bold nil)))
629 "Face used to visualize NEWLINE char mapping.
631 See `whitespace-display-mappings'."
632 :group 'whitespace)
635 (defcustom whitespace-trailing 'whitespace-trailing
636 "Symbol face used to visualize trailing blanks.
638 Used when `whitespace-style' includes the value `trailing'."
639 :type 'face
640 :group 'whitespace)
643 (defface whitespace-trailing ; 'trailing-whitespace
644 '((((class mono)) (:inverse-video t :bold t :underline t))
645 (t (:background "red1" :foreground "yellow" :bold t)))
646 "Face used to visualize trailing blanks."
647 :group 'whitespace)
650 (defcustom whitespace-line 'whitespace-line
651 "Symbol face used to visualize \"long\" lines.
653 See `whitespace-line-column'.
655 Used when `whitespace-style' includes the value `line'."
656 :type 'face
657 :group 'whitespace)
660 (defface whitespace-line
661 '((((class mono)) (:inverse-video t :bold t :underline t))
662 (t (:background "gray20" :foreground "violet")))
663 "Face used to visualize \"long\" lines.
665 See `whitespace-line-column'."
666 :group 'whitespace)
669 (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
670 "Symbol face used to visualize SPACEs before TAB.
672 Used when `whitespace-style' includes the value `space-before-tab'."
673 :type 'face
674 :group 'whitespace)
677 (defface whitespace-space-before-tab
678 '((((class mono)) (:inverse-video t :bold t :underline t))
679 (t (:background "DarkOrange" :foreground "firebrick")))
680 "Face used to visualize SPACEs before TAB."
681 :group 'whitespace)
684 (defcustom whitespace-indentation 'whitespace-indentation
685 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
687 Used when `whitespace-style' includes the value `indentation'."
688 :type 'face
689 :group 'whitespace)
692 (defface whitespace-indentation
693 '((((class mono)) (:inverse-video t :bold t :underline t))
694 (t (:background "yellow" :foreground "firebrick")))
695 "Face used to visualize 8 or more SPACEs at beginning of line."
696 :group 'whitespace)
699 (defcustom whitespace-empty 'whitespace-empty
700 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
702 Used when `whitespace-style' includes the value `empty'."
703 :type 'face
704 :group 'whitespace)
707 (defface whitespace-empty
708 '((((class mono)) (:inverse-video t :bold t :underline t))
709 (t (:background "yellow" :foreground "firebrick")))
710 "Face used to visualize empty lines at beginning and/or end of buffer."
711 :group 'whitespace)
714 (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
715 "Symbol face used to visualize 8 or more SPACEs after TAB.
717 Used when `whitespace-style' includes the value `space-after-tab'."
718 :type 'face
719 :group 'whitespace)
722 (defface whitespace-space-after-tab
723 '((((class mono)) (:inverse-video t :bold t :underline t))
724 (t (:background "yellow" :foreground "firebrick")))
725 "Face used to visualize 8 or more SPACEs after TAB."
726 :group 'whitespace)
729 (defcustom whitespace-hspace-regexp
730 "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
731 "Specify HARD SPACE characters regexp.
733 If you're using `mule' package, there may be other characters besides:
735 \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
737 that should be considered HARD SPACE.
739 Here are some examples:
741 \"\\\\(^\\xA0+\\\\)\" \
742 visualize only leading HARD SPACEs.
743 \"\\\\(\\xA0+$\\\\)\" \
744 visualize only trailing HARD SPACEs.
745 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
746 visualize leading and/or trailing HARD SPACEs.
747 \"\\t\\\\(\\xA0+\\\\)\\t\" \
748 visualize only HARD SPACEs between TABs.
750 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
751 Use exactly one pair of enclosing \\\\( and \\\\).
753 Used when `whitespace-style' includes `spaces'."
754 :type '(regexp :tag "HARD SPACE Chars")
755 :group 'whitespace)
758 (defcustom whitespace-space-regexp "\\( +\\)"
759 "Specify SPACE characters regexp.
761 If you're using `mule' package, there may be other characters
762 besides \" \" that should be considered SPACE.
764 Here are some examples:
766 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
767 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
768 \"\\\\(^ +\\\\| +$\\\\)\" \
769 visualize leading and/or trailing SPACEs.
770 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
772 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
773 Use exactly one pair of enclosing \\\\( and \\\\).
775 Used when `whitespace-style' includes `spaces'."
776 :type '(regexp :tag "SPACE Chars")
777 :group 'whitespace)
780 (defcustom whitespace-tab-regexp "\\(\t+\\)"
781 "Specify TAB characters regexp.
783 If you're using `mule' package, there may be other characters
784 besides \"\\t\" that should be considered TAB.
786 Here are some examples:
788 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
789 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
790 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
791 visualize leading and/or trailing TABs.
792 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
794 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
795 Use exactly one pair of enclosing \\\\( and \\\\).
797 Used when `whitespace-style' includes `tabs'."
798 :type '(regexp :tag "TAB Chars")
799 :group 'whitespace)
802 (defcustom whitespace-trailing-regexp
803 "\\([\t \u00A0]+\\)$"
804 "Specify trailing characters regexp.
806 If you're using `mule' package, there may be other characters besides:
808 \" \" \"\\t\" \"\\u00A0\"
810 that should be considered blank.
812 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
813 Use exactly one pair of enclosing elements above.
815 Used when `whitespace-style' includes `trailing'."
816 :type '(regexp :tag "Trailing Chars")
817 :group 'whitespace)
820 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
821 "Specify SPACEs before TAB regexp.
823 If you're using `mule' package, there may be other characters besides:
825 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
826 \"\\xF20\"
828 that should be considered blank.
830 Used when `whitespace-style' includes `space-before-tab',
831 `space-before-tab::tab' or `space-before-tab::space'."
832 :type '(regexp :tag "SPACEs Before TAB")
833 :group 'whitespace)
836 (defcustom whitespace-indentation-regexp
837 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
838 . "^ *\\(\t+\\)[^\n]")
839 "Specify regexp for 8 or more SPACEs at beginning of line.
841 It is a cons where the cons car is used for SPACEs visualization
842 and the cons cdr is used for TABs visualization.
844 If you're using `mule' package, there may be other characters besides:
846 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
847 \"\\xF20\"
849 that should be considered blank.
851 Used when `whitespace-style' includes `indentation',
852 `indentation::tab' or `indentation::space'."
853 :type '(cons (regexp :tag "Indentation SPACEs")
854 (regexp :tag "Indentation TABs"))
855 :group 'whitespace)
858 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
859 "Specify regexp for empty lines at beginning of buffer.
861 If you're using `mule' package, there may be other characters besides:
863 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
864 \"\\xF20\"
866 that should be considered blank.
868 Used when `whitespace-style' includes `empty'."
869 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
870 :group 'whitespace)
873 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
874 "Specify regexp for empty lines at end of buffer.
876 If you're using `mule' package, there may be other characters besides:
878 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
879 \"\\xF20\"
881 that should be considered blank.
883 Used when `whitespace-style' includes `empty'."
884 :type '(regexp :tag "Empty Lines At End Of Buffer")
885 :group 'whitespace)
888 (defcustom whitespace-space-after-tab-regexp
889 '("\t+\\(\\( \\{%d\\}\\)+\\)"
890 . "\\(\t+\\) +")
891 "Specify regexp for 8 or more SPACEs after TAB.
893 It is a cons where the cons car is used for SPACEs visualization
894 and the cons cdr is used for TABs visualization.
896 If you're using `mule' package, there may be other characters besides:
898 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
899 \"\\xF20\"
901 that should be considered blank.
903 Used when `whitespace-style' includes `space-after-tab',
904 `space-after-tab::tab' or `space-after-tab::space'."
905 :type '(regexp :tag "SPACEs After TAB")
906 :group 'whitespace)
909 (defcustom whitespace-line-column 80
910 "Specify column beyond which the line is highlighted.
912 It must be an integer or nil. If nil, the `fill-column' variable value is
913 used.
915 Used when `whitespace-style' includes `lines' or `lines-tail'."
916 :type '(choice :tag "Line Length Limit"
917 (integer :tag "Line Length")
918 (const :tag "Use fill-column" nil))
919 :group 'whitespace)
922 ;; Hacked from `visible-whitespace-mappings' in visws.el
923 (defcustom whitespace-display-mappings
925 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
926 (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
927 (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency
928 (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency
929 (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency
930 (space-mark ?\xF20 [?\xF24] [?_]) ; hard space - currency
931 ;; NEWLINE is displayed using the face `whitespace-newline'
932 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
933 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
934 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
935 ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
936 ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
937 ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
939 ;; WARNING: the mapping below has a problem.
940 ;; When a TAB occupies exactly one column, it will display the
941 ;; character ?\xBB at that column followed by a TAB which goes to
942 ;; the next TAB column.
943 ;; If this is a problem for you, please, comment the line below.
944 (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
946 "Specify an alist of mappings for displaying characters.
948 Each element has the following form:
950 (KIND CHAR VECTOR...)
952 Where:
954 KIND is the kind of character.
955 It can be one of the following symbols:
957 tab-mark for TAB character
959 space-mark for SPACE or HARD SPACE character
961 newline-mark for NEWLINE character
963 CHAR is the character to be mapped.
965 VECTOR is a vector of characters to be displayed in place of CHAR.
966 The first display vector that can be displayed is used;
967 if no display vector for a mapping can be displayed, then
968 that character is displayed unmodified.
970 The NEWLINE character is displayed using the face given by
971 `whitespace-newline' variable.
973 Used when `whitespace-style' includes `tab-mark', `space-mark' or
974 `newline-mark'."
975 :type '(repeat
976 (list :tag "Character Mapping"
977 (choice :tag "Char Kind"
978 (const :tag "Tab" tab-mark)
979 (const :tag "Space" space-mark)
980 (const :tag "Newline" newline-mark))
981 (character :tag "Char")
982 (repeat :inline t :tag "Vector List"
983 (vector :tag ""
984 (repeat :inline t
985 :tag "Vector Characters"
986 (character :tag "Char"))))))
987 :group 'whitespace)
990 (defcustom whitespace-global-modes t
991 "Modes for which global `whitespace-mode' is automagically turned on.
993 Global `whitespace-mode' is controlled by the command
994 `global-whitespace-mode'.
996 If nil, means no modes have `whitespace-mode' automatically
997 turned on.
999 If t, all modes that support `whitespace-mode' have it
1000 automatically turned on.
1002 Else it should be a list of `major-mode' symbol names for which
1003 `whitespace-mode' should be automatically turned on. The sense
1004 of the list is negated if it begins with `not'. For example:
1006 (c-mode c++-mode)
1008 means that `whitespace-mode' is turned on for buffers in C and
1009 C++ modes only."
1010 :type '(choice :tag "Global Modes"
1011 (const :tag "None" nil)
1012 (const :tag "All" t)
1013 (set :menu-tag "Mode Specific" :tag "Modes"
1014 :value (not)
1015 (const :tag "Except" not)
1016 (repeat :inline t
1017 (symbol :tag "Mode"))))
1018 :group 'whitespace)
1021 (defcustom whitespace-action nil
1022 "Specify which action is taken when a buffer is visited or written.
1024 It's a list containing some or all of the following values:
1026 nil no action is taken.
1028 cleanup cleanup any bogus whitespace always when local
1029 whitespace is turned on.
1030 See `whitespace-cleanup' and
1031 `whitespace-cleanup-region'.
1033 report-on-bogus report if there is any bogus whitespace always
1034 when local whitespace is turned on.
1036 auto-cleanup cleanup any bogus whitespace when buffer is
1037 written.
1038 See `whitespace-cleanup' and
1039 `whitespace-cleanup-region'.
1041 abort-on-bogus abort if there is any bogus whitespace and the
1042 buffer is written.
1044 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
1045 is included in `whitespace-action' and the
1046 buffer is read-only.
1048 Any other value is treated as nil."
1049 :type '(choice :tag "Actions"
1050 (const :tag "None" nil)
1051 (repeat :tag "Action List"
1052 (choice :tag "Action"
1053 (const :tag "Cleanup When On" cleanup)
1054 (const :tag "Report On Bogus" report-on-bogus)
1055 (const :tag "Auto Cleanup" auto-cleanup)
1056 (const :tag "Abort On Bogus" abort-on-bogus)
1057 (const :tag "Warn If Read-Only" warn-if-read-only))))
1058 :group 'whitespace)
1061 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1062 ;;;; User commands - Local mode
1065 ;;;###autoload
1066 (define-minor-mode whitespace-mode
1067 "Toggle whitespace visualization (Whitespace mode).
1068 With a prefix argument ARG, enable Whitespace mode if ARG is
1069 positive, and disable it otherwise. If called from Lisp, enable
1070 the mode if ARG is omitted or nil.
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 visualization (Whitespace Newline mode).
1091 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1092 is positive, and disable it otherwise. If called from Lisp,
1093 enable the mode if ARG is omitted or nil.
1095 Use `whitespace-newline-mode' only for NEWLINE visualization
1096 exclusively. For other visualizations, including NEWLINE
1097 visualization together with (HARD) SPACEs and/or TABs, please,
1098 use `whitespace-mode'.
1100 See also `whitespace-newline' and `whitespace-display-mappings'."
1101 :lighter " nl"
1102 :init-value nil
1103 :global nil
1104 :group 'whitespace
1105 (let ((whitespace-style '(face newline-mark newline)))
1106 (whitespace-mode (if whitespace-newline-mode 1 -1))
1107 ;; Sync states (running a batch job).
1108 (setq whitespace-newline-mode whitespace-mode)))
1111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1112 ;;;; User commands - Global mode
1115 ;;;###autoload
1116 (define-minor-mode global-whitespace-mode
1117 "Toggle whitespace visualization globally (Global Whitespace mode).
1118 With a prefix argument ARG, enable Global Whitespace mode if ARG
1119 is positive, and disable it otherwise. If called from Lisp,
1120 enable it if ARG is omitted or nil.
1122 See also `whitespace-style', `whitespace-newline' and
1123 `whitespace-display-mappings'."
1124 :lighter " WS"
1125 :init-value nil
1126 :global t
1127 :group 'whitespace
1128 (cond
1129 (noninteractive ; running a batch job
1130 (setq global-whitespace-mode nil))
1131 (global-whitespace-mode ; global-whitespace-mode on
1132 (save-current-buffer
1133 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1134 (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1135 (dolist (buffer (buffer-list)) ; adjust all local mode
1136 (set-buffer buffer)
1137 (unless whitespace-mode
1138 (whitespace-turn-on-if-enabled)))))
1139 (t ; global-whitespace-mode off
1140 (save-current-buffer
1141 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1142 (remove-hook 'after-change-major-mode-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 global newline visualization (Global Whitespace Newline mode).
1175 With a prefix argument ARG, enable Global Whitespace Newline mode
1176 if ARG is positive, and disable it otherwise. If called from
1177 Lisp, enable it if ARG is omitted or nil.
1179 Use `global-whitespace-newline-mode' only for NEWLINE
1180 visualization exclusively. For other visualizations, including
1181 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1182 please use `global-whitespace-mode'.
1184 See also `whitespace-newline' and `whitespace-display-mappings'."
1185 :lighter " NL"
1186 :init-value nil
1187 :global t
1188 :group 'whitespace
1189 (let ((whitespace-style '(newline-mark newline)))
1190 (global-whitespace-mode (if global-whitespace-newline-mode
1191 1 -1))
1192 ;; sync states (running a batch job)
1193 (setq global-whitespace-newline-mode global-whitespace-mode)))
1196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1197 ;;;; User commands - Toggle
1200 (defconst whitespace-style-value-list
1201 '(face
1202 tabs
1203 spaces
1204 trailing
1205 lines
1206 lines-tail
1207 newline
1208 empty
1209 indentation
1210 indentation::tab
1211 indentation::space
1212 space-after-tab
1213 space-after-tab::tab
1214 space-after-tab::space
1215 space-before-tab
1216 space-before-tab::tab
1217 space-before-tab::space
1218 help-newline ; value used by `whitespace-insert-option-mark'
1219 tab-mark
1220 space-mark
1221 newline-mark
1223 "List of valid `whitespace-style' values.")
1226 (defconst whitespace-toggle-option-alist
1227 '((?f . face)
1228 (?t . tabs)
1229 (?s . spaces)
1230 (?r . trailing)
1231 (?l . lines)
1232 (?L . lines-tail)
1233 (?n . newline)
1234 (?e . empty)
1235 (?\C-i . indentation)
1236 (?I . indentation::tab)
1237 (?i . indentation::space)
1238 (?\C-a . space-after-tab)
1239 (?A . space-after-tab::tab)
1240 (?a . space-after-tab::space)
1241 (?\C-b . space-before-tab)
1242 (?B . space-before-tab::tab)
1243 (?b . space-before-tab::space)
1244 (?T . tab-mark)
1245 (?S . space-mark)
1246 (?N . newline-mark)
1247 (?x . whitespace-style)
1249 "Alist of toggle options.
1251 Each element has the form:
1253 (CHAR . SYMBOL)
1255 Where:
1257 CHAR is a char which the user will have to type.
1259 SYMBOL is a valid symbol associated with CHAR.
1260 See `whitespace-style-value-list'.")
1263 (defvar whitespace-active-style nil
1264 "Used to save locally `whitespace-style' value.")
1266 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1267 "Used to save locally `indent-tabs-mode' value.")
1269 (defvar whitespace-tab-width tab-width
1270 "Used to save locally `tab-width' value.")
1272 (defvar whitespace-point (point)
1273 "Used to save locally current point value.
1274 Used by `whitespace-trailing-regexp' function (which see).")
1276 (defvar whitespace-font-lock-refontify nil
1277 "Used to save locally the font-lock refontify state.
1278 Used by `whitespace-post-command-hook' function (which see).")
1280 (defvar whitespace-bob-marker nil
1281 "Used to save locally the bob marker value.
1282 Used by `whitespace-post-command-hook' function (which see).")
1284 (defvar whitespace-eob-marker nil
1285 "Used to save locally the eob marker value.
1286 Used by `whitespace-post-command-hook' function (which see).")
1288 (defvar whitespace-buffer-changed nil
1289 "Used to indicate locally if buffer changed.
1290 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1291 functions (which see).")
1294 ;;;###autoload
1295 (defun whitespace-toggle-options (arg)
1296 "Toggle local `whitespace-mode' options.
1298 If local whitespace-mode is off, toggle the option given by ARG
1299 and turn on local whitespace-mode.
1301 If local whitespace-mode is on, toggle the option given by ARG
1302 and restart local whitespace-mode.
1304 Interactively, it reads one of the following chars:
1306 CHAR MEANING
1307 (VIA FACES)
1308 f toggle face visualization
1309 t toggle TAB visualization
1310 s toggle SPACE and HARD SPACE visualization
1311 r toggle trailing blanks visualization
1312 l toggle \"long lines\" visualization
1313 L toggle \"long lines\" tail visualization
1314 n toggle NEWLINE visualization
1315 e toggle empty line at bob and/or eob visualization
1316 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1317 I toggle indentation SPACEs visualization
1318 i toggle indentation TABs visualization
1319 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1320 A toggle SPACEs after TAB: SPACEs visualization
1321 a toggle SPACEs after TAB: TABs visualization
1322 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1323 B toggle SPACEs before TAB: SPACEs visualization
1324 b toggle SPACEs before TAB: TABs visualization
1326 (VIA DISPLAY TABLE)
1327 T toggle TAB visualization
1328 S toggle SPACEs before TAB visualization
1329 N toggle NEWLINE visualization
1331 x restore `whitespace-style' value
1332 ? display brief help
1334 Non-interactively, ARG should be a symbol or a list of symbols.
1335 The valid symbols are:
1337 face toggle face visualization
1338 tabs toggle TAB visualization
1339 spaces toggle SPACE and HARD SPACE visualization
1340 trailing toggle trailing blanks visualization
1341 lines toggle \"long lines\" visualization
1342 lines-tail toggle \"long lines\" tail visualization
1343 newline toggle NEWLINE visualization
1344 empty toggle empty line at bob and/or eob visualization
1345 indentation toggle indentation SPACEs visualization
1346 indentation::tab toggle indentation SPACEs visualization
1347 indentation::space toggle indentation TABs visualization
1348 space-after-tab toggle SPACEs after TAB visualization
1349 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1350 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1351 space-before-tab toggle SPACEs before TAB visualization
1352 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1353 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1355 tab-mark toggle TAB visualization
1356 space-mark toggle SPACEs before TAB visualization
1357 newline-mark toggle NEWLINE visualization
1359 whitespace-style restore `whitespace-style' value
1361 See `whitespace-style' and `indent-tabs-mode' for documentation."
1362 (interactive (whitespace-interactive-char t))
1363 (let ((whitespace-style
1364 (whitespace-toggle-list t arg whitespace-active-style)))
1365 (whitespace-mode 0)
1366 (whitespace-mode 1)))
1369 (defvar whitespace-toggle-style nil
1370 "Used to toggle the global `whitespace-style' value.")
1373 ;;;###autoload
1374 (defun global-whitespace-toggle-options (arg)
1375 "Toggle global `whitespace-mode' options.
1377 If global whitespace-mode is off, toggle the option given by ARG
1378 and turn on global whitespace-mode.
1380 If global whitespace-mode is on, toggle the option given by ARG
1381 and restart global whitespace-mode.
1383 Interactively, it accepts one of the following chars:
1385 CHAR MEANING
1386 (VIA FACES)
1387 f toggle face visualization
1388 t toggle TAB visualization
1389 s toggle SPACE and HARD SPACE visualization
1390 r toggle trailing blanks visualization
1391 l toggle \"long lines\" visualization
1392 L toggle \"long lines\" tail visualization
1393 n toggle NEWLINE visualization
1394 e toggle empty line at bob and/or eob visualization
1395 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1396 I toggle indentation SPACEs visualization
1397 i toggle indentation TABs visualization
1398 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1399 A toggle SPACEs after TAB: SPACEs visualization
1400 a toggle SPACEs after TAB: TABs visualization
1401 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1402 B toggle SPACEs before TAB: SPACEs visualization
1403 b toggle SPACEs before TAB: TABs visualization
1405 (VIA DISPLAY TABLE)
1406 T toggle TAB visualization
1407 S toggle SPACEs before TAB visualization
1408 N toggle NEWLINE visualization
1410 x restore `whitespace-style' value
1411 ? display brief help
1413 Non-interactively, ARG should be a symbol or a list of symbols.
1414 The valid symbols are:
1416 face toggle face visualization
1417 tabs toggle TAB visualization
1418 spaces toggle SPACE and HARD SPACE visualization
1419 trailing toggle trailing blanks visualization
1420 lines toggle \"long lines\" visualization
1421 lines-tail toggle \"long lines\" tail visualization
1422 newline toggle NEWLINE visualization
1423 empty toggle empty line at bob and/or eob visualization
1424 indentation toggle indentation SPACEs visualization
1425 indentation::tab toggle indentation SPACEs visualization
1426 indentation::space toggle indentation TABs visualization
1427 space-after-tab toggle SPACEs after TAB visualization
1428 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1429 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1430 space-before-tab toggle SPACEs before TAB visualization
1431 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1432 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1434 tab-mark toggle TAB visualization
1435 space-mark toggle SPACEs before TAB visualization
1436 newline-mark toggle NEWLINE visualization
1438 whitespace-style restore `whitespace-style' value
1440 See `whitespace-style' and `indent-tabs-mode' for documentation."
1441 (interactive (whitespace-interactive-char nil))
1442 (let ((whitespace-style
1443 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1444 (setq whitespace-toggle-style whitespace-style)
1445 (global-whitespace-mode 0)
1446 (global-whitespace-mode 1)))
1449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1450 ;;;; User commands - Cleanup
1453 ;;;###autoload
1454 (defun whitespace-cleanup ()
1455 "Cleanup some blank problems in all buffer or at region.
1457 It usually applies to the whole buffer, but in transient mark
1458 mode when the mark is active, it applies to the region. It also
1459 applies to the region when it is not in transient mark mode, the
1460 mark is active and \\[universal-argument] was pressed just before
1461 calling `whitespace-cleanup' interactively.
1463 See also `whitespace-cleanup-region'.
1465 The problems cleaned up are:
1467 1. empty lines at beginning of buffer.
1468 2. empty lines at end of buffer.
1469 If `whitespace-style' includes the value `empty', remove all
1470 empty lines at beginning and/or end of buffer.
1472 3. 8 or more SPACEs at beginning of line.
1473 If `whitespace-style' includes the value `indentation':
1474 replace 8 or more SPACEs at beginning of line by TABs, if
1475 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1476 SPACEs.
1477 If `whitespace-style' includes the value `indentation::tab',
1478 replace 8 or more SPACEs at beginning of line by TABs.
1479 If `whitespace-style' includes the value `indentation::space',
1480 replace TABs by SPACEs.
1482 4. SPACEs before TAB.
1483 If `whitespace-style' includes the value `space-before-tab':
1484 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1485 otherwise, replace TABs by SPACEs.
1486 If `whitespace-style' includes the value
1487 `space-before-tab::tab', replace SPACEs by TABs.
1488 If `whitespace-style' includes the value
1489 `space-before-tab::space', replace TABs by SPACEs.
1491 5. SPACEs or TABs at end of line.
1492 If `whitespace-style' includes the value `trailing', remove
1493 all SPACEs or TABs at end of line.
1495 6. 8 or more SPACEs after TAB.
1496 If `whitespace-style' includes the value `space-after-tab':
1497 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1498 otherwise, replace TABs by SPACEs.
1499 If `whitespace-style' includes the value
1500 `space-after-tab::tab', replace SPACEs by TABs.
1501 If `whitespace-style' includes the value
1502 `space-after-tab::space', replace TABs by SPACEs.
1504 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1505 documentation."
1506 (interactive "@")
1507 (cond
1508 ;; read-only buffer
1509 (buffer-read-only
1510 (whitespace-warn-read-only "cleanup"))
1511 ;; region active
1512 ((and (or transient-mark-mode
1513 current-prefix-arg)
1514 mark-active)
1515 ;; PROBLEMs 1 and 2 are not handled in region
1516 ;; PROBLEM 3: 8 or more SPACEs at bol
1517 ;; PROBLEM 4: SPACEs before TAB
1518 ;; PROBLEM 5: SPACEs or TABs at eol
1519 ;; PROBLEM 6: 8 or more SPACEs after TAB
1520 (whitespace-cleanup-region (region-beginning) (region-end)))
1521 ;; whole buffer
1523 (save-excursion
1524 (save-match-data ;FIXME: Why?
1525 ;; PROBLEM 1: empty lines at bob
1526 ;; PROBLEM 2: empty lines at eob
1527 ;; ACTION: remove all empty lines at bob and/or eob
1528 (when (memq 'empty whitespace-style)
1529 (let (overwrite-mode) ; enforce no overwrite
1530 (goto-char (point-min))
1531 (when (re-search-forward
1532 (concat "\\`" whitespace-empty-at-bob-regexp) nil t)
1533 (delete-region (match-beginning 1) (match-end 1)))
1534 (when (re-search-forward
1535 (concat whitespace-empty-at-eob-regexp "\\'") nil t)
1536 (delete-region (match-beginning 1) (match-end 1)))))))
1537 ;; PROBLEM 3: 8 or more SPACEs at bol
1538 ;; PROBLEM 4: SPACEs before TAB
1539 ;; PROBLEM 5: SPACEs or TABs at eol
1540 ;; PROBLEM 6: 8 or more SPACEs after TAB
1541 (whitespace-cleanup-region (point-min) (point-max)))))
1544 ;;;###autoload
1545 (defun whitespace-cleanup-region (start end)
1546 "Cleanup some blank problems at region.
1548 The problems cleaned up are:
1550 1. 8 or more SPACEs at beginning of line.
1551 If `whitespace-style' includes the value `indentation':
1552 replace 8 or more SPACEs at beginning of line by TABs, if
1553 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1554 SPACEs.
1555 If `whitespace-style' includes the value `indentation::tab',
1556 replace 8 or more SPACEs at beginning of line by TABs.
1557 If `whitespace-style' includes the value `indentation::space',
1558 replace TABs by SPACEs.
1560 2. SPACEs before TAB.
1561 If `whitespace-style' includes the value `space-before-tab':
1562 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1563 otherwise, replace TABs by SPACEs.
1564 If `whitespace-style' includes the value
1565 `space-before-tab::tab', replace SPACEs by TABs.
1566 If `whitespace-style' includes the value
1567 `space-before-tab::space', replace TABs by SPACEs.
1569 3. SPACEs or TABs at end of line.
1570 If `whitespace-style' includes the value `trailing', remove
1571 all SPACEs or TABs at end of line.
1573 4. 8 or more SPACEs after TAB.
1574 If `whitespace-style' includes the value `space-after-tab':
1575 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1576 otherwise, replace TABs by SPACEs.
1577 If `whitespace-style' includes the value
1578 `space-after-tab::tab', replace SPACEs by TABs.
1579 If `whitespace-style' includes the value
1580 `space-after-tab::space', replace TABs by SPACEs.
1582 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1583 documentation."
1584 (interactive "@r")
1585 (if buffer-read-only
1586 ;; read-only buffer
1587 (whitespace-warn-read-only "cleanup region")
1588 ;; non-read-only buffer
1589 (let ((rstart (min start end))
1590 (rend (copy-marker (max start end)))
1591 (indent-tabs-mode whitespace-indent-tabs-mode)
1592 (tab-width whitespace-tab-width)
1593 overwrite-mode ; enforce no overwrite
1594 tmp)
1595 (save-excursion
1596 (save-match-data ;FIXME: Why?
1597 ;; PROBLEM 1: 8 or more SPACEs at bol
1598 (cond
1599 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1600 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1601 ;; by SPACEs.
1602 ((memq 'indentation whitespace-style)
1603 (let ((regexp (whitespace-indentation-regexp)))
1604 (goto-char rstart)
1605 (while (re-search-forward regexp rend t)
1606 (setq tmp (current-indentation))
1607 (goto-char (match-beginning 0))
1608 (delete-horizontal-space)
1609 (unless (eolp)
1610 (indent-to tmp)))))
1611 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1612 ((memq 'indentation::tab whitespace-style)
1613 (whitespace-replace-action
1614 'tabify rstart rend
1615 (whitespace-indentation-regexp 'tab) 0))
1616 ;; ACTION: replace TABs by SPACEs.
1617 ((memq 'indentation::space whitespace-style)
1618 (whitespace-replace-action
1619 'untabify rstart rend
1620 (whitespace-indentation-regexp 'space) 0)))
1621 ;; PROBLEM 3: SPACEs or TABs at eol
1622 ;; ACTION: remove all SPACEs or TABs at eol
1623 (when (memq 'trailing whitespace-style)
1624 (whitespace-replace-action
1625 'delete-region rstart rend
1626 whitespace-trailing-regexp 1))
1627 ;; PROBLEM 4: 8 or more SPACEs after TAB
1628 (cond
1629 ;; ACTION: replace 8 or more SPACEs by TABs, if
1630 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1631 ;; by SPACEs.
1632 ((memq 'space-after-tab whitespace-style)
1633 (whitespace-replace-action
1634 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1635 rstart rend (whitespace-space-after-tab-regexp) 1))
1636 ;; ACTION: replace 8 or more SPACEs by TABs.
1637 ((memq 'space-after-tab::tab whitespace-style)
1638 (whitespace-replace-action
1639 'tabify rstart rend
1640 (whitespace-space-after-tab-regexp 'tab) 1))
1641 ;; ACTION: replace TABs by SPACEs.
1642 ((memq 'space-after-tab::space whitespace-style)
1643 (whitespace-replace-action
1644 'untabify rstart rend
1645 (whitespace-space-after-tab-regexp 'space) 1)))
1646 ;; PROBLEM 2: SPACEs before TAB
1647 (cond
1648 ;; ACTION: replace SPACEs before TAB by TABs, if
1649 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1650 ;; by SPACEs.
1651 ((memq 'space-before-tab whitespace-style)
1652 (whitespace-replace-action
1653 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1654 rstart rend whitespace-space-before-tab-regexp
1655 (if whitespace-indent-tabs-mode 0 2)))
1656 ;; ACTION: replace SPACEs before TAB by TABs.
1657 ((memq 'space-before-tab::tab whitespace-style)
1658 (whitespace-replace-action
1659 'tabify rstart rend
1660 whitespace-space-before-tab-regexp 0))
1661 ;; ACTION: replace TABs by SPACEs.
1662 ((memq 'space-before-tab::space whitespace-style)
1663 (whitespace-replace-action
1664 'untabify rstart rend
1665 whitespace-space-before-tab-regexp 2)))))
1666 (set-marker rend nil)))) ; point marker to nowhere
1669 (defun whitespace-replace-action (action rstart rend regexp index)
1670 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1672 INDEX is the level group matched by REGEXP and used by ACTION.
1674 See also `tab-width'."
1675 (goto-char rstart)
1676 (while (re-search-forward regexp rend t)
1677 (goto-char (match-end index))
1678 (funcall action (match-beginning index) (match-end index))))
1681 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1682 ;;;; User command - report
1685 (defun whitespace-regexp (regexp &optional kind)
1686 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1687 (cond
1688 ((or (eq kind 'tab)
1689 whitespace-indent-tabs-mode)
1690 (format (car regexp) whitespace-tab-width))
1691 ((or (eq kind 'space)
1692 (not whitespace-indent-tabs-mode))
1693 (cdr regexp))))
1696 (defun whitespace-indentation-regexp (&optional kind)
1697 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1698 (whitespace-regexp whitespace-indentation-regexp kind))
1701 (defun whitespace-space-after-tab-regexp (&optional kind)
1702 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1703 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1706 (defconst whitespace-report-list
1707 (list
1708 (cons 'empty whitespace-empty-at-bob-regexp)
1709 (cons 'empty whitespace-empty-at-eob-regexp)
1710 (cons 'trailing whitespace-trailing-regexp)
1711 (cons 'indentation nil)
1712 (cons 'indentation::tab nil)
1713 (cons 'indentation::space nil)
1714 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1715 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1716 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1717 (cons 'space-after-tab nil)
1718 (cons 'space-after-tab::tab nil)
1719 (cons 'space-after-tab::space nil)
1721 "List of whitespace bogus symbol and corresponding regexp.")
1724 (defconst whitespace-report-text
1725 '( ;; `indent-tabs-mode' has non-nil value
1727 Whitespace Report
1729 Current Setting Whitespace Problem
1731 empty [] [] empty lines at beginning of buffer
1732 empty [] [] empty lines at end of buffer
1733 trailing [] [] SPACEs or TABs at end of line
1734 indentation [] [] 8 or more SPACEs at beginning of line
1735 indentation::tab [] [] 8 or more SPACEs at beginning of line
1736 indentation::space [] [] TABs at beginning of line
1737 space-before-tab [] [] SPACEs before TAB
1738 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1739 space-before-tab::space [] [] SPACEs before TAB: TABs
1740 space-after-tab [] [] 8 or more SPACEs after TAB
1741 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1742 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1744 indent-tabs-mode =
1745 tab-width = \n\n"
1746 . ;; `indent-tabs-mode' has nil value
1748 Whitespace Report
1750 Current Setting Whitespace Problem
1752 empty [] [] empty lines at beginning of buffer
1753 empty [] [] empty lines at end of buffer
1754 trailing [] [] SPACEs or TABs at end of line
1755 indentation [] [] TABs at beginning of line
1756 indentation::tab [] [] 8 or more SPACEs at beginning of line
1757 indentation::space [] [] TABs at beginning of line
1758 space-before-tab [] [] SPACEs before TAB
1759 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1760 space-before-tab::space [] [] SPACEs before TAB: TABs
1761 space-after-tab [] [] 8 or more SPACEs after TAB
1762 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1763 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1765 indent-tabs-mode =
1766 tab-width = \n\n")
1767 "Text for whitespace bogus report.
1769 It is a cons of strings, where the car part is used when
1770 `indent-tabs-mode' is non-nil, and the cdr part is used when
1771 `indent-tabs-mode' is nil.")
1774 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1775 "The buffer name for whitespace bogus report.")
1778 ;;;###autoload
1779 (defun whitespace-report (&optional force report-if-bogus)
1780 "Report some whitespace problems in buffer.
1782 Return nil if there is no whitespace problem; otherwise, return
1783 non-nil.
1785 If FORCE is non-nil or \\[universal-argument] was pressed just
1786 before calling `whitespace-report' interactively, it forces
1787 `whitespace-style' to have:
1789 empty
1790 trailing
1791 indentation
1792 space-before-tab
1793 space-after-tab
1795 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1796 whitespace problems in buffer.
1798 Report if some of the following whitespace problems exist:
1800 * If `indent-tabs-mode' is non-nil:
1801 empty 1. empty lines at beginning of buffer.
1802 empty 2. empty lines at end of buffer.
1803 trailing 3. SPACEs or TABs at end of line.
1804 indentation 4. 8 or more SPACEs at beginning of line.
1805 space-before-tab 5. SPACEs before TAB.
1806 space-after-tab 6. 8 or more SPACEs after TAB.
1808 * If `indent-tabs-mode' is nil:
1809 empty 1. empty lines at beginning of buffer.
1810 empty 2. empty lines at end of buffer.
1811 trailing 3. SPACEs or TABs at end of line.
1812 indentation 4. TABS at beginning of line.
1813 space-before-tab 5. SPACEs before TAB.
1814 space-after-tab 6. 8 or more SPACEs after TAB.
1816 See `whitespace-style' for documentation.
1817 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1818 cleaning up these problems."
1819 (interactive (list current-prefix-arg))
1820 (whitespace-report-region (point-min) (point-max)
1821 force report-if-bogus))
1824 ;;;###autoload
1825 (defun whitespace-report-region (start end &optional force report-if-bogus)
1826 "Report some whitespace problems in a region.
1828 Return nil if there is no whitespace problem; otherwise, return
1829 non-nil.
1831 If FORCE is non-nil or \\[universal-argument] was pressed just
1832 before calling `whitespace-report-region' interactively, it
1833 forces `whitespace-style' to have:
1835 empty
1836 indentation
1837 space-before-tab
1838 trailing
1839 space-after-tab
1841 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1842 whitespace problems in buffer.
1844 Report if some of the following whitespace problems exist:
1846 * If `indent-tabs-mode' is non-nil:
1847 empty 1. empty lines at beginning of buffer.
1848 empty 2. empty lines at end of buffer.
1849 trailing 3. SPACEs or TABs at end of line.
1850 indentation 4. 8 or more SPACEs at beginning of line.
1851 space-before-tab 5. SPACEs before TAB.
1852 space-after-tab 6. 8 or more SPACEs after TAB.
1854 * If `indent-tabs-mode' is nil:
1855 empty 1. empty lines at beginning of buffer.
1856 empty 2. empty lines at end of buffer.
1857 trailing 3. SPACEs or TABs at end of line.
1858 indentation 4. TABS at beginning of line.
1859 space-before-tab 5. SPACEs before TAB.
1860 space-after-tab 6. 8 or more SPACEs after TAB.
1862 See `whitespace-style' for documentation.
1863 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1864 cleaning up these problems."
1865 (interactive "r")
1866 (setq force (or current-prefix-arg force))
1867 (save-excursion
1868 (save-match-data ;FIXME: Why?
1869 (let* ((has-bogus nil)
1870 (rstart (min start end))
1871 (rend (max start end))
1872 (bogus-list
1873 (mapcar
1874 #'(lambda (option)
1875 (when force
1876 (add-to-list 'whitespace-style (car option)))
1877 (goto-char rstart)
1878 (let ((regexp
1879 (cond
1880 ((eq (car option) 'indentation)
1881 (whitespace-indentation-regexp))
1882 ((eq (car option) 'indentation::tab)
1883 (whitespace-indentation-regexp 'tab))
1884 ((eq (car option) 'indentation::space)
1885 (whitespace-indentation-regexp 'space))
1886 ((eq (car option) 'space-after-tab)
1887 (whitespace-space-after-tab-regexp))
1888 ((eq (car option) 'space-after-tab::tab)
1889 (whitespace-space-after-tab-regexp 'tab))
1890 ((eq (car option) 'space-after-tab::space)
1891 (whitespace-space-after-tab-regexp 'space))
1893 (cdr option)))))
1894 (and (re-search-forward regexp rend t)
1895 (setq has-bogus t))))
1896 whitespace-report-list)))
1897 (when (if report-if-bogus has-bogus t)
1898 (whitespace-kill-buffer whitespace-report-buffer-name)
1899 ;; `whitespace-indent-tabs-mode' is local to current buffer
1900 ;; `whitespace-tab-width' is local to current buffer
1901 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1902 (ws-tab-width whitespace-tab-width))
1903 (with-current-buffer (get-buffer-create
1904 whitespace-report-buffer-name)
1905 (erase-buffer)
1906 (insert (if ws-indent-tabs-mode
1907 (car whitespace-report-text)
1908 (cdr whitespace-report-text)))
1909 (goto-char (point-min))
1910 (forward-line 3)
1911 (dolist (option whitespace-report-list)
1912 (forward-line 1)
1913 (whitespace-mark-x
1914 27 (memq (car option) whitespace-style))
1915 (whitespace-mark-x 7 (car bogus-list))
1916 (setq bogus-list (cdr bogus-list)))
1917 (forward-line 1)
1918 (whitespace-insert-value ws-indent-tabs-mode)
1919 (whitespace-insert-value ws-tab-width)
1920 (when has-bogus
1921 (goto-char (point-max))
1922 (insert " Type `M-x whitespace-cleanup'"
1923 " to cleanup the buffer.\n\n"
1924 " Type `M-x whitespace-cleanup-region'"
1925 " to cleanup a region.\n\n"))
1926 (whitespace-display-window (current-buffer)))))
1927 has-bogus))))
1930 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1931 ;;;; Internal functions
1934 (defvar whitespace-font-lock-mode nil
1935 "Used to remember whether a buffer had font lock mode on or not.")
1937 (defvar whitespace-font-lock nil
1938 "Used to remember whether a buffer initially had font lock on or not.")
1940 (defvar whitespace-font-lock-keywords nil
1941 "Used to save locally `font-lock-keywords' value.")
1944 (defconst whitespace-help-text
1946 Whitespace Toggle Options | scroll up : SPC or > |
1947 | scroll down: M-SPC or < |
1948 FACES \\__________________________/
1949 [] f - toggle face visualization
1950 [] t - toggle TAB visualization
1951 [] s - toggle SPACE and HARD SPACE visualization
1952 [] r - toggle trailing blanks visualization
1953 [] l - toggle \"long lines\" visualization
1954 [] L - toggle \"long lines\" tail visualization
1955 [] n - toggle NEWLINE visualization
1956 [] e - toggle empty line at bob and/or eob visualization
1957 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1958 [] I - toggle indentation SPACEs visualization
1959 [] i - toggle indentation TABs visualization
1960 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1961 [] A - toggle SPACEs after TAB: SPACEs visualization
1962 [] a - toggle SPACEs after TAB: TABs visualization
1963 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1964 [] B - toggle SPACEs before TAB: SPACEs visualization
1965 [] b - toggle SPACEs before TAB: TABs visualization
1967 DISPLAY TABLE
1968 [] T - toggle TAB visualization
1969 [] S - toggle SPACE and HARD SPACE visualization
1970 [] N - toggle NEWLINE visualization
1972 x - restore `whitespace-style' value
1974 ? - display this text\n\n"
1975 "Text for whitespace toggle options.")
1978 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1979 "The buffer name for whitespace toggle options.")
1982 (defun whitespace-insert-value (value)
1983 "Insert VALUE at column 20 of next line."
1984 (forward-line 1)
1985 (move-to-column 20 t)
1986 (insert (format "%s" value)))
1989 (defun whitespace-mark-x (nchars condition)
1990 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1991 (forward-char nchars)
1992 (insert (if condition "X" " ")))
1995 (defun whitespace-insert-option-mark (the-list the-value)
1996 "Insert the option mark ('X' or ' ') in toggle options buffer."
1997 (goto-char (point-min))
1998 (forward-line 2)
1999 (dolist (sym the-list)
2000 (if (eq sym 'help-newline)
2001 (forward-line 2)
2002 (forward-line 1)
2003 (whitespace-mark-x 2 (memq sym the-value)))))
2006 (defun whitespace-help-on (style)
2007 "Display the whitespace toggle options."
2008 (unless (get-buffer whitespace-help-buffer-name)
2009 (delete-other-windows)
2010 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
2011 (with-current-buffer buffer
2012 (erase-buffer)
2013 (insert whitespace-help-text)
2014 (whitespace-insert-option-mark
2015 whitespace-style-value-list style)
2016 (whitespace-display-window buffer)))))
2019 (defun whitespace-display-window (buffer)
2020 "Display BUFFER in a new window."
2021 (goto-char (point-min))
2022 (set-buffer-modified-p nil)
2023 (when (< (window-height) (* 2 window-min-height))
2024 (kill-buffer buffer)
2025 (error "Window height is too small; \
2026 can't split window to display whitespace toggle options"))
2027 (let ((win (split-window)))
2028 (set-window-buffer win buffer)
2029 (shrink-window-if-larger-than-buffer win)))
2032 (defun whitespace-kill-buffer (buffer-name)
2033 "Kill buffer BUFFER-NAME and windows related with it."
2034 (let ((buffer (get-buffer buffer-name)))
2035 (when buffer
2036 (delete-windows-on buffer)
2037 (kill-buffer buffer))))
2040 (defun whitespace-help-off ()
2041 "Remove the buffer and window of the whitespace toggle options."
2042 (whitespace-kill-buffer whitespace-help-buffer-name))
2045 (defun whitespace-help-scroll (&optional up)
2046 "Scroll help window, if it exists.
2048 If UP is non-nil, scroll up; otherwise, scroll down."
2049 (condition-case nil
2050 (let ((buffer (get-buffer whitespace-help-buffer-name)))
2051 (if buffer
2052 (with-selected-window (get-buffer-window buffer)
2053 (if up
2054 (scroll-up 3)
2055 (scroll-down 3)))
2056 (ding)))
2057 ;; handler
2058 ((error)
2059 ;; just ignore error
2063 (defun whitespace-interactive-char (local-p)
2064 "Interactive function to read a char and return a symbol.
2066 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2067 uses a global context.
2069 It accepts one of the following chars:
2071 CHAR MEANING
2072 (VIA FACES)
2073 f toggle face visualization
2074 t toggle TAB visualization
2075 s toggle SPACE and HARD SPACE visualization
2076 r toggle trailing blanks visualization
2077 l toggle \"long lines\" visualization
2078 L toggle \"long lines\" tail visualization
2079 n toggle NEWLINE visualization
2080 e toggle empty line at bob and/or eob visualization
2081 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2082 I toggle indentation SPACEs visualization
2083 i toggle indentation TABs visualization
2084 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2085 A toggle SPACEs after TAB: SPACEs visualization
2086 a toggle SPACEs after TAB: TABs visualization
2087 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2088 B toggle SPACEs before TAB: SPACEs visualization
2089 b toggle SPACEs before TAB: TABs visualization
2091 (VIA DISPLAY TABLE)
2092 T toggle TAB visualization
2093 S toggle SPACE and HARD SPACE visualization
2094 N toggle NEWLINE visualization
2096 x restore `whitespace-style' value
2097 ? display brief help
2099 See also `whitespace-toggle-option-alist'."
2100 (let* ((is-off (not (if local-p
2101 whitespace-mode
2102 global-whitespace-mode)))
2103 (style (cond (is-off whitespace-style) ; use default value
2104 (local-p whitespace-active-style)
2105 (t whitespace-toggle-style)))
2106 (prompt
2107 (format "Whitespace Toggle %s (type ? for further options)-"
2108 (if local-p "Local" "Global")))
2109 ch sym)
2110 ;; read a valid option and get the corresponding symbol
2111 (save-window-excursion
2112 (condition-case data
2113 (progn
2114 (while
2115 ;; while condition
2116 (progn
2117 (setq ch (read-char prompt))
2118 (not
2119 (setq sym
2120 (cdr
2121 (assq ch whitespace-toggle-option-alist)))))
2122 ;; while body
2123 (cond
2124 ((eq ch ?\?) (whitespace-help-on style))
2125 ((eq ch ?\ ) (whitespace-help-scroll t))
2126 ((eq ch ?\M- ) (whitespace-help-scroll))
2127 ((eq ch ?>) (whitespace-help-scroll t))
2128 ((eq ch ?<) (whitespace-help-scroll))
2129 (t (ding))))
2130 (whitespace-help-off)
2131 (message " ")) ; clean echo area
2132 ;; handler
2133 ((quit error)
2134 (whitespace-help-off)
2135 (error (error-message-string data)))))
2136 (list sym))) ; return the appropriate symbol
2139 (defun whitespace-toggle-list (local-p arg the-list)
2140 "Toggle options in THE-LIST based on list ARG.
2142 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2143 uses a global context.
2145 ARG is a list of options to be toggled.
2147 THE-LIST is a list of options. This list will be toggled and the
2148 resultant list will be returned."
2149 (unless (if local-p whitespace-mode global-whitespace-mode)
2150 (setq the-list whitespace-style))
2151 (setq the-list (copy-sequence the-list)) ; keep original list
2152 (dolist (sym (if (listp arg) arg (list arg)))
2153 (cond
2154 ;; ignore help value
2155 ((eq sym 'help-newline))
2156 ;; restore default values
2157 ((eq sym 'whitespace-style)
2158 (setq the-list whitespace-style))
2159 ;; toggle valid values
2160 ((memq sym whitespace-style-value-list)
2161 (setq the-list (if (memq sym the-list)
2162 (delq sym the-list)
2163 (cons sym the-list))))))
2164 the-list)
2167 (defvar whitespace-display-table nil
2168 "Used to save a local display table.")
2170 (defvar whitespace-display-table-was-local nil
2171 "Used to remember whether a buffer initially had a local display table.")
2174 (defun whitespace-turn-on ()
2175 "Turn on whitespace visualization."
2176 ;; prepare local hooks
2177 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2178 ;; create whitespace local buffer environment
2179 (set (make-local-variable 'whitespace-font-lock-mode) nil)
2180 (set (make-local-variable 'whitespace-font-lock) nil)
2181 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2182 (set (make-local-variable 'whitespace-display-table) nil)
2183 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2184 (set (make-local-variable 'whitespace-active-style)
2185 (if (listp whitespace-style)
2186 whitespace-style
2187 (list whitespace-style)))
2188 (set (make-local-variable 'whitespace-indent-tabs-mode)
2189 indent-tabs-mode)
2190 (set (make-local-variable 'whitespace-tab-width)
2191 tab-width)
2192 ;; turn on whitespace
2193 (when whitespace-active-style
2194 (whitespace-color-on)
2195 (whitespace-display-char-on)))
2198 (defun whitespace-turn-off ()
2199 "Turn off whitespace visualization."
2200 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2201 (when whitespace-active-style
2202 (whitespace-color-off)
2203 (whitespace-display-char-off)))
2206 (defun whitespace-style-face-p ()
2207 "Return t if there is some visualization via face."
2208 (and (memq 'face whitespace-active-style)
2209 (or (memq 'tabs whitespace-active-style)
2210 (memq 'spaces whitespace-active-style)
2211 (memq 'trailing whitespace-active-style)
2212 (memq 'lines whitespace-active-style)
2213 (memq 'lines-tail whitespace-active-style)
2214 (memq 'newline whitespace-active-style)
2215 (memq 'empty whitespace-active-style)
2216 (memq 'indentation whitespace-active-style)
2217 (memq 'indentation::tab whitespace-active-style)
2218 (memq 'indentation::space whitespace-active-style)
2219 (memq 'space-after-tab whitespace-active-style)
2220 (memq 'space-after-tab::tab whitespace-active-style)
2221 (memq 'space-after-tab::space whitespace-active-style)
2222 (memq 'space-before-tab whitespace-active-style)
2223 (memq 'space-before-tab::tab whitespace-active-style)
2224 (memq 'space-before-tab::space whitespace-active-style))))
2227 (defun whitespace-color-on ()
2228 "Turn on color visualization."
2229 (when (whitespace-style-face-p)
2230 (unless whitespace-font-lock
2231 (setq whitespace-font-lock t
2232 whitespace-font-lock-keywords
2233 (copy-sequence font-lock-keywords)))
2234 ;; save current point and refontify when necessary
2235 (set (make-local-variable 'whitespace-point)
2236 (point))
2237 (set (make-local-variable 'whitespace-font-lock-refontify)
2239 (set (make-local-variable 'whitespace-bob-marker)
2240 (point-min-marker))
2241 (set (make-local-variable 'whitespace-eob-marker)
2242 (point-max-marker))
2243 (set (make-local-variable 'whitespace-buffer-changed)
2244 nil)
2245 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
2246 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
2247 ;; turn off font lock
2248 (set (make-local-variable 'whitespace-font-lock-mode)
2249 font-lock-mode)
2250 (font-lock-mode 0)
2251 ;; add whitespace-mode color into font lock
2252 (when (memq 'spaces whitespace-active-style)
2253 (font-lock-add-keywords
2255 (list
2256 ;; Show SPACEs
2257 (list whitespace-space-regexp 1 whitespace-space t)
2258 ;; Show HARD SPACEs
2259 (list whitespace-hspace-regexp 1 whitespace-hspace t))
2261 (when (memq 'tabs whitespace-active-style)
2262 (font-lock-add-keywords
2264 (list
2265 ;; Show TABs
2266 (list whitespace-tab-regexp 1 whitespace-tab t))
2268 (when (memq 'trailing whitespace-active-style)
2269 (font-lock-add-keywords
2271 (list
2272 ;; Show trailing blanks
2273 (list #'whitespace-trailing-regexp 1 whitespace-trailing t))
2275 (when (or (memq 'lines whitespace-active-style)
2276 (memq 'lines-tail whitespace-active-style))
2277 (font-lock-add-keywords
2279 (list
2280 ;; Show "long" lines
2281 (list
2282 (let ((line-column (or whitespace-line-column fill-column)))
2283 (format
2284 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2285 whitespace-tab-width
2286 (1- whitespace-tab-width)
2287 (/ line-column whitespace-tab-width)
2288 (let ((rem (% line-column whitespace-tab-width)))
2289 (if (zerop rem)
2291 (format ".\\{%d\\}" rem)))))
2292 (if (memq 'lines whitespace-active-style)
2293 0 ; whole line
2294 2) ; line tail
2295 whitespace-line t))
2297 (cond
2298 ((memq 'space-before-tab whitespace-active-style)
2299 (font-lock-add-keywords
2301 (list
2302 ;; Show SPACEs before TAB (indent-tabs-mode)
2303 (list whitespace-space-before-tab-regexp
2304 (if whitespace-indent-tabs-mode 1 2)
2305 whitespace-space-before-tab t))
2307 ((memq 'space-before-tab::tab whitespace-active-style)
2308 (font-lock-add-keywords
2310 (list
2311 ;; Show SPACEs before TAB (SPACEs)
2312 (list whitespace-space-before-tab-regexp
2313 1 whitespace-space-before-tab t))
2315 ((memq 'space-before-tab::space whitespace-active-style)
2316 (font-lock-add-keywords
2318 (list
2319 ;; Show SPACEs before TAB (TABs)
2320 (list whitespace-space-before-tab-regexp
2321 2 whitespace-space-before-tab t))
2322 t)))
2323 (cond
2324 ((memq 'indentation whitespace-active-style)
2325 (font-lock-add-keywords
2327 (list
2328 ;; Show indentation SPACEs (indent-tabs-mode)
2329 (list (whitespace-indentation-regexp)
2330 1 whitespace-indentation t))
2332 ((memq 'indentation::tab whitespace-active-style)
2333 (font-lock-add-keywords
2335 (list
2336 ;; Show indentation SPACEs (SPACEs)
2337 (list (whitespace-indentation-regexp 'tab)
2338 1 whitespace-indentation t))
2340 ((memq 'indentation::space whitespace-active-style)
2341 (font-lock-add-keywords
2343 (list
2344 ;; Show indentation SPACEs (TABs)
2345 (list (whitespace-indentation-regexp 'space)
2346 1 whitespace-indentation t))
2347 t)))
2348 (when (memq 'empty whitespace-active-style)
2349 (font-lock-add-keywords
2351 (list
2352 ;; Show empty lines at beginning of buffer
2353 (list #'whitespace-empty-at-bob-regexp
2354 1 whitespace-empty t))
2356 (font-lock-add-keywords
2358 (list
2359 ;; Show empty lines at end of buffer
2360 (list #'whitespace-empty-at-eob-regexp
2361 1 whitespace-empty t))
2363 (cond
2364 ((memq 'space-after-tab whitespace-active-style)
2365 (font-lock-add-keywords
2367 (list
2368 ;; Show SPACEs after TAB (indent-tabs-mode)
2369 (list (whitespace-space-after-tab-regexp)
2370 1 whitespace-space-after-tab t))
2372 ((memq 'space-after-tab::tab whitespace-active-style)
2373 (font-lock-add-keywords
2375 (list
2376 ;; Show SPACEs after TAB (SPACEs)
2377 (list (whitespace-space-after-tab-regexp 'tab)
2378 1 whitespace-space-after-tab t))
2380 ((memq 'space-after-tab::space whitespace-active-style)
2381 (font-lock-add-keywords
2383 (list
2384 ;; Show SPACEs after TAB (TABs)
2385 (list (whitespace-space-after-tab-regexp 'space)
2386 1 whitespace-space-after-tab t))
2387 t)))
2388 ;; now turn on font lock and highlight blanks
2389 (font-lock-mode 1)))
2392 (defun whitespace-color-off ()
2393 "Turn off color visualization."
2394 ;; turn off font lock
2395 (when (whitespace-style-face-p)
2396 (font-lock-mode 0)
2397 (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
2398 (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
2399 (when whitespace-font-lock
2400 (setq whitespace-font-lock nil
2401 font-lock-keywords whitespace-font-lock-keywords))
2402 ;; restore original font lock state
2403 (font-lock-mode whitespace-font-lock-mode)))
2406 (defun whitespace-trailing-regexp (limit)
2407 "Match trailing spaces which do not contain the point at end of line."
2408 (let ((status t))
2409 (while (if (re-search-forward whitespace-trailing-regexp limit t)
2410 (= whitespace-point (match-end 1)) ;; Loop if point at eol.
2411 (setq status nil))) ;; End of buffer.
2412 status))
2415 (defun whitespace-empty-at-bob-regexp (limit)
2416 "Match spaces at beginning of buffer which do not contain the point at \
2417 beginning of buffer."
2418 (let ((b (point))
2420 (cond
2421 ;; at bob
2422 ((= b 1)
2423 (setq r (and (/= whitespace-point 1)
2424 (looking-at whitespace-empty-at-bob-regexp)))
2425 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2426 ;; inside bob empty region
2427 ((<= limit whitespace-bob-marker)
2428 (setq r (looking-at whitespace-empty-at-bob-regexp))
2429 (if r
2430 (when (< (match-end 1) limit)
2431 (set-marker whitespace-bob-marker (match-end 1)))
2432 (set-marker whitespace-bob-marker b)))
2433 ;; intersection with end of bob empty region
2434 ((<= b whitespace-bob-marker)
2435 (setq r (looking-at whitespace-empty-at-bob-regexp))
2436 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2437 ;; it is not inside bob empty region
2439 (setq r nil)))
2440 ;; move to end of matching
2441 (and r (goto-char (match-end 1)))
2445 (defsubst whitespace-looking-back (regexp limit)
2446 (save-excursion
2447 (when (/= 0 (skip-chars-backward " \t\n" limit))
2448 (unless (bolp)
2449 (forward-line 1))
2450 (looking-at regexp))))
2453 (defun whitespace-empty-at-eob-regexp (limit)
2454 "Match spaces at end of buffer which do not contain the point at end of \
2455 buffer."
2456 (let ((b (point))
2457 (e (1+ (buffer-size)))
2459 (cond
2460 ;; at eob
2461 ((= limit e)
2462 (when (/= whitespace-point e)
2463 (goto-char limit)
2464 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
2465 (if r
2466 (set-marker whitespace-eob-marker (match-beginning 1))
2467 (set-marker whitespace-eob-marker limit)
2468 (goto-char b))) ; return back to initial position
2469 ;; inside eob empty region
2470 ((>= b whitespace-eob-marker)
2471 (goto-char limit)
2472 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2473 (if r
2474 (when (> (match-beginning 1) b)
2475 (set-marker whitespace-eob-marker (match-beginning 1)))
2476 (set-marker whitespace-eob-marker limit)
2477 (goto-char b))) ; return back to initial position
2478 ;; intersection with beginning of eob empty region
2479 ((>= limit whitespace-eob-marker)
2480 (goto-char limit)
2481 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2482 (if r
2483 (set-marker whitespace-eob-marker (match-beginning 1))
2484 (set-marker whitespace-eob-marker limit)
2485 (goto-char b))) ; return back to initial position
2486 ;; it is not inside eob empty region
2488 (setq r nil)))
2492 (defun whitespace-buffer-changed (_beg _end)
2493 "Set `whitespace-buffer-changed' variable to t."
2494 (setq whitespace-buffer-changed t))
2497 (defun whitespace-post-command-hook ()
2498 "Save current point into `whitespace-point' variable.
2499 Also refontify when necessary."
2500 (setq whitespace-point (point)) ; current point position
2501 (let ((refontify
2503 ;; it is at end of line ...
2504 (and (eolp)
2505 ;; ... with trailing SPACE or TAB
2506 (or (= (preceding-char) ?\ )
2507 (= (preceding-char) ?\t)))
2508 ;; it is at beginning of buffer (bob)
2509 (= whitespace-point 1)
2510 ;; the buffer was modified and ...
2511 (and whitespace-buffer-changed
2513 ;; ... or inside bob whitespace region
2514 (<= whitespace-point whitespace-bob-marker)
2515 ;; ... or at bob whitespace region border
2516 (and (= whitespace-point (1+ whitespace-bob-marker))
2517 (= (preceding-char) ?\n))))
2518 ;; it is at end of buffer (eob)
2519 (= whitespace-point (1+ (buffer-size)))
2520 ;; the buffer was modified and ...
2521 (and whitespace-buffer-changed
2523 ;; ... or inside eob whitespace region
2524 (>= whitespace-point whitespace-eob-marker)
2525 ;; ... or at eob whitespace region border
2526 (and (= whitespace-point (1- whitespace-eob-marker))
2527 (= (following-char) ?\n)))))))
2528 (when (or refontify (> whitespace-font-lock-refontify 0))
2529 (setq whitespace-buffer-changed nil)
2530 ;; adjust refontify counter
2531 (setq whitespace-font-lock-refontify
2532 (if refontify
2534 (1- whitespace-font-lock-refontify)))
2535 ;; refontify
2536 (jit-lock-refontify))))
2539 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2540 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2543 (defun whitespace-style-mark-p ()
2544 "Return t if there is some visualization via display table."
2545 (or (memq 'tab-mark whitespace-active-style)
2546 (memq 'space-mark whitespace-active-style)
2547 (memq 'newline-mark whitespace-active-style)))
2550 (defsubst whitespace-char-valid-p (char)
2551 ;; This check should be improved!!!
2552 (or (< char 256)
2553 (characterp char)))
2556 (defun whitespace-display-vector-p (vec)
2557 "Return true if every character in vector VEC can be displayed."
2558 (let ((i (length vec)))
2559 (when (> i 0)
2560 (while (and (>= (setq i (1- i)) 0)
2561 (whitespace-char-valid-p (aref vec i))))
2562 (< i 0))))
2565 (defun whitespace-display-char-on ()
2566 "Turn on character display mapping."
2567 (when (and whitespace-display-mappings
2568 (whitespace-style-mark-p))
2569 (let (vecs vec)
2570 ;; Remember whether a buffer has a local display table.
2571 (unless whitespace-display-table-was-local
2572 (setq whitespace-display-table-was-local t
2573 whitespace-display-table
2574 (copy-sequence buffer-display-table))
2575 ;; asure `buffer-display-table' is unique
2576 ;; when two or more windows are visible.
2577 (setq buffer-display-table
2578 (copy-sequence buffer-display-table)))
2579 (unless buffer-display-table
2580 (setq buffer-display-table (make-display-table)))
2581 (dolist (entry whitespace-display-mappings)
2582 ;; check if it is to display this mark
2583 (when (memq (car entry) whitespace-style)
2584 ;; Get a displayable mapping.
2585 (setq vecs (cddr entry))
2586 (while (and vecs
2587 (not (whitespace-display-vector-p (car vecs))))
2588 (setq vecs (cdr vecs)))
2589 ;; Display a valid mapping.
2590 (when vecs
2591 (setq vec (copy-sequence (car vecs)))
2592 ;; NEWLINE char
2593 (when (and (eq (cadr entry) ?\n)
2594 (memq 'newline whitespace-active-style))
2595 ;; Only insert face bits on NEWLINE char mapping to avoid
2596 ;; obstruction of other faces like TABs and (HARD) SPACEs
2597 ;; faces, font-lock faces, etc.
2598 (dotimes (i (length vec))
2599 (or (eq (aref vec i) ?\n)
2600 (aset vec i
2601 (make-glyph-code (aref vec i)
2602 whitespace-newline)))))
2603 ;; Display mapping
2604 (aset buffer-display-table (cadr entry) vec)))))))
2607 (defun whitespace-display-char-off ()
2608 "Turn off character display mapping."
2609 (and whitespace-display-mappings
2610 (whitespace-style-mark-p)
2611 whitespace-display-table-was-local
2612 (setq whitespace-display-table-was-local nil
2613 buffer-display-table whitespace-display-table)))
2616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2617 ;;;; Hook
2620 (defun whitespace-action-when-on ()
2621 "Action to be taken always when local whitespace is turned on."
2622 (cond ((memq 'cleanup whitespace-action)
2623 (whitespace-cleanup))
2624 ((memq 'report-on-bogus whitespace-action)
2625 (whitespace-report nil t))))
2628 (defun whitespace-write-file-hook ()
2629 "Action to be taken when buffer is written.
2630 It should be added buffer-locally to `write-file-functions'."
2631 (cond ((memq 'auto-cleanup whitespace-action)
2632 (whitespace-cleanup))
2633 ((memq 'abort-on-bogus whitespace-action)
2634 (when (whitespace-report nil t)
2635 (error "Abort write due to whitespace problems in %s"
2636 (buffer-name)))))
2637 nil) ; continue hook processing
2640 (defun whitespace-warn-read-only (msg)
2641 "Warn if buffer is read-only."
2642 (when (memq 'warn-if-read-only whitespace-action)
2643 (message "Can't %s: %s is read-only" msg (buffer-name))))
2646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2649 (defun whitespace-unload-function ()
2650 "Unload the whitespace library."
2651 (global-whitespace-mode -1)
2652 ;; be sure all local whitespace mode is turned off
2653 (save-current-buffer
2654 (dolist (buf (buffer-list))
2655 (set-buffer buf)
2656 (whitespace-mode -1)))
2657 nil) ; continue standard unloading
2660 (provide 'whitespace)
2663 (run-hooks 'whitespace-load-hook)
2666 ;;; whitespace.el ends here