* lisp/whitespace.el (whitespace-enable-predicate): New variable.
[emacs.git] / lisp / whitespace.el
blob975b89f2fc236eb7ea0677dfacfde895681f65d5
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000-2013 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.2
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 mode line).
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on mode line).
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 ;; Acknowledgments
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 cleaning 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 '((default :weight normal)
625 (((class color) (background dark)) :foreground "darkgray")
626 (((class color) (min-colors 88) (background light)) :foreground "lightgray")
627 ;; Displays with 16 colors use lightgray as background, so using a
628 ;; lightgray foreground makes the newline mark invisible.
629 (((class color) (background light)) :foreground "brown")
630 (t :underline t))
631 "Face used to visualize NEWLINE char mapping.
633 See `whitespace-display-mappings'."
634 :group 'whitespace)
637 (defcustom whitespace-trailing 'whitespace-trailing
638 "Symbol face used to visualize trailing blanks.
640 Used when `whitespace-style' includes the value `trailing'."
641 :type 'face
642 :group 'whitespace)
645 (defface whitespace-trailing ; 'trailing-whitespace
646 '((default :weight bold)
647 (((class mono)) :inverse-video t :underline t)
648 (t :background "red1" :foreground "yellow"))
649 "Face used to visualize trailing blanks."
650 :group 'whitespace)
653 (defcustom whitespace-line 'whitespace-line
654 "Symbol face used to visualize \"long\" lines.
656 See `whitespace-line-column'.
658 Used when `whitespace-style' includes the value `line'."
659 :type 'face
660 :group 'whitespace)
663 (defface whitespace-line
664 '((((class mono)) :inverse-video t :weight bold :underline t)
665 (t :background "gray20" :foreground "violet"))
666 "Face used to visualize \"long\" lines.
668 See `whitespace-line-column'."
669 :group 'whitespace)
672 (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
673 "Symbol face used to visualize SPACEs before TAB.
675 Used when `whitespace-style' includes the value `space-before-tab'."
676 :type 'face
677 :group 'whitespace)
680 (defface whitespace-space-before-tab
681 '((((class mono)) :inverse-video t :weight bold :underline t)
682 (t :background "DarkOrange" :foreground "firebrick"))
683 "Face used to visualize SPACEs before TAB."
684 :group 'whitespace)
687 (defcustom whitespace-indentation 'whitespace-indentation
688 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
690 Used when `whitespace-style' includes the value `indentation'."
691 :type 'face
692 :group 'whitespace)
695 (defface whitespace-indentation
696 '((((class mono)) :inverse-video t :weight bold :underline t)
697 (t :background "yellow" :foreground "firebrick"))
698 "Face used to visualize 8 or more SPACEs at beginning of line."
699 :group 'whitespace)
702 (defcustom whitespace-empty 'whitespace-empty
703 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
705 Used when `whitespace-style' includes the value `empty'."
706 :type 'face
707 :group 'whitespace)
710 (defface whitespace-empty
711 '((((class mono)) :inverse-video t :weight bold :underline t)
712 (t :background "yellow" :foreground "firebrick"))
713 "Face used to visualize empty lines at beginning and/or end of buffer."
714 :group 'whitespace)
717 (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
718 "Symbol face used to visualize 8 or more SPACEs after TAB.
720 Used when `whitespace-style' includes the value `space-after-tab'."
721 :type 'face
722 :group 'whitespace)
725 (defface whitespace-space-after-tab
726 '((((class mono)) :inverse-video t :weight bold :underline t)
727 (t :background "yellow" :foreground "firebrick"))
728 "Face used to visualize 8 or more SPACEs after TAB."
729 :group 'whitespace)
732 (defcustom whitespace-hspace-regexp
733 "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
734 "Specify HARD SPACE characters regexp.
736 If you're using `mule' package, there may be other characters besides:
738 \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
740 that should be considered HARD SPACE.
742 Here are some examples:
744 \"\\\\(^\\xA0+\\\\)\" \
745 visualize only leading HARD SPACEs.
746 \"\\\\(\\xA0+$\\\\)\" \
747 visualize only trailing HARD SPACEs.
748 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
749 visualize leading and/or trailing HARD SPACEs.
750 \"\\t\\\\(\\xA0+\\\\)\\t\" \
751 visualize only HARD SPACEs between TABs.
753 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
754 Use exactly one pair of enclosing \\\\( and \\\\).
756 Used when `whitespace-style' includes `spaces'."
757 :type '(regexp :tag "HARD SPACE Chars")
758 :group 'whitespace)
761 (defcustom whitespace-space-regexp "\\( +\\)"
762 "Specify SPACE characters regexp.
764 If you're using `mule' package, there may be other characters
765 besides \" \" that should be considered SPACE.
767 Here are some examples:
769 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
770 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
771 \"\\\\(^ +\\\\| +$\\\\)\" \
772 visualize leading and/or trailing SPACEs.
773 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
775 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
776 Use exactly one pair of enclosing \\\\( and \\\\).
778 Used when `whitespace-style' includes `spaces'."
779 :type '(regexp :tag "SPACE Chars")
780 :group 'whitespace)
783 (defcustom whitespace-tab-regexp "\\(\t+\\)"
784 "Specify TAB characters regexp.
786 If you're using `mule' package, there may be other characters
787 besides \"\\t\" that should be considered TAB.
789 Here are some examples:
791 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
792 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
793 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
794 visualize leading and/or trailing TABs.
795 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
797 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
798 Use exactly one pair of enclosing \\\\( and \\\\).
800 Used when `whitespace-style' includes `tabs'."
801 :type '(regexp :tag "TAB Chars")
802 :group 'whitespace)
805 (defcustom whitespace-trailing-regexp
806 "\\([\t \u00A0]+\\)$"
807 "Specify trailing characters regexp.
809 If you're using `mule' package, there may be other characters besides:
811 \" \" \"\\t\" \"\\u00A0\"
813 that should be considered blank.
815 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
816 Use exactly one pair of enclosing elements above.
818 Used when `whitespace-style' includes `trailing'."
819 :type '(regexp :tag "Trailing Chars")
820 :group 'whitespace)
823 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
824 "Specify SPACEs before TAB regexp.
826 If you're using `mule' package, there may be other characters besides:
828 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
829 \"\\xF20\"
831 that should be considered blank.
833 Used when `whitespace-style' includes `space-before-tab',
834 `space-before-tab::tab' or `space-before-tab::space'."
835 :type '(regexp :tag "SPACEs Before TAB")
836 :group 'whitespace)
839 (defcustom whitespace-indentation-regexp
840 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
841 . "^ *\\(\t+\\)[^\n]")
842 "Specify regexp for 8 or more SPACEs at beginning of line.
844 It is a cons where the cons car is used for SPACEs visualization
845 and the cons cdr is used for TABs visualization.
847 If you're using `mule' package, there may be other characters besides:
849 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
850 \"\\xF20\"
852 that should be considered blank.
854 Used when `whitespace-style' includes `indentation',
855 `indentation::tab' or `indentation::space'."
856 :type '(cons (regexp :tag "Indentation SPACEs")
857 (regexp :tag "Indentation TABs"))
858 :group 'whitespace)
861 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
862 "Specify regexp for empty lines at beginning of buffer.
864 If you're using `mule' package, there may be other characters besides:
866 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
867 \"\\xF20\"
869 that should be considered blank.
871 Used when `whitespace-style' includes `empty'."
872 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
873 :group 'whitespace)
876 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
877 "Specify regexp for empty lines at end of buffer.
879 If you're using `mule' package, there may be other characters besides:
881 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
882 \"\\xF20\"
884 that should be considered blank.
886 Used when `whitespace-style' includes `empty'."
887 :type '(regexp :tag "Empty Lines At End Of Buffer")
888 :group 'whitespace)
891 (defcustom whitespace-space-after-tab-regexp
892 '("\t+\\(\\( \\{%d\\}\\)+\\)"
893 . "\\(\t+\\) +")
894 "Specify regexp for 8 or more SPACEs after TAB.
896 It is a cons where the cons car is used for SPACEs visualization
897 and the cons cdr is used for TABs visualization.
899 If you're using `mule' package, there may be other characters besides:
901 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
902 \"\\xF20\"
904 that should be considered blank.
906 Used when `whitespace-style' includes `space-after-tab',
907 `space-after-tab::tab' or `space-after-tab::space'."
908 :type '(regexp :tag "SPACEs After TAB")
909 :group 'whitespace)
912 (defcustom whitespace-line-column 80
913 "Specify column beyond which the line is highlighted.
915 It must be an integer or nil. If nil, the `fill-column' variable value is
916 used.
918 Used when `whitespace-style' includes `lines' or `lines-tail'."
919 :type '(choice :tag "Line Length Limit"
920 (integer :tag "Line Length")
921 (const :tag "Use fill-column" nil))
922 :group 'whitespace)
925 ;; Hacked from `visible-whitespace-mappings' in visws.el
926 (defcustom whitespace-display-mappings
928 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
929 (space-mark ?\xA0 [?\u00A4] [?_]) ; 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 [?\u00AF ?\n] [?$ ?\n]) ; eol - overscore
935 ;; (newline-mark ?\n [?\u00AC ?\n] [?$ ?\n]) ; eol - negation
936 ;; (newline-mark ?\n [?\u00B0 ?\n] [?$ ?\n]) ; eol - degrees
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 visualization (Whitespace mode).
1067 With a prefix argument ARG, enable Whitespace mode if ARG is
1068 positive, and disable it otherwise. If called from Lisp, enable
1069 the mode if ARG is omitted or nil.
1071 See also `whitespace-style', `whitespace-newline' and
1072 `whitespace-display-mappings'."
1073 :lighter " ws"
1074 :init-value nil
1075 :global nil
1076 :group 'whitespace
1077 (cond
1078 (noninteractive ; running a batch job
1079 (setq whitespace-mode nil))
1080 (whitespace-mode ; whitespace-mode on
1081 (whitespace-turn-on)
1082 (whitespace-action-when-on))
1083 (t ; whitespace-mode off
1084 (whitespace-turn-off))))
1087 ;;;###autoload
1088 (define-minor-mode whitespace-newline-mode
1089 "Toggle newline visualization (Whitespace Newline mode).
1090 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1091 is positive, and disable it otherwise. If called from Lisp,
1092 enable the mode if ARG is omitted or nil.
1094 Use `whitespace-newline-mode' only for NEWLINE visualization
1095 exclusively. For other visualizations, including NEWLINE
1096 visualization together with (HARD) SPACEs and/or TABs, please,
1097 use `whitespace-mode'.
1099 See also `whitespace-newline' and `whitespace-display-mappings'."
1100 :lighter " nl"
1101 :init-value nil
1102 :global nil
1103 :group 'whitespace
1104 (let ((whitespace-style '(face newline-mark newline)))
1105 (whitespace-mode (if whitespace-newline-mode
1106 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)))))))
1148 (defvar whitespace-enable-predicate
1149 (lambda ()
1150 (and (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 ;; ...we have a display (we're running a batch job)
1158 (not noninteractive)
1159 ;; ...the buffer is not internal (name starts with a space)
1160 (not (eq (aref (buffer-name) 0) ?\ ))
1161 ;; ...the buffer is not special (name starts with *)
1162 (or (not (eq (aref (buffer-name) 0) ?*))
1163 ;; except the scratch buffer.
1164 (string= (buffer-name) "*scratch*"))))
1165 "Predicate to decide which buffers obey `global-whitespace-mode'.
1166 This function is called with no argument and should return non-nil
1167 if the current buffer should obey `global-whitespace-mode'.
1168 This variable is normally modified via `add-function'.")
1170 (defun whitespace-turn-on-if-enabled ()
1171 (when (funcall whitespace-enable-predicate)
1172 (whitespace-turn-on)))
1174 ;;;###autoload
1175 (define-minor-mode global-whitespace-newline-mode
1176 "Toggle global newline visualization (Global Whitespace Newline mode).
1177 With a prefix argument ARG, enable Global Whitespace Newline mode
1178 if ARG is positive, and disable it otherwise. If called from
1179 Lisp, enable it if ARG is omitted or nil.
1181 Use `global-whitespace-newline-mode' only for NEWLINE
1182 visualization exclusively. For other visualizations, including
1183 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1184 please use `global-whitespace-mode'.
1186 See also `whitespace-newline' and `whitespace-display-mappings'."
1187 :lighter " NL"
1188 :init-value nil
1189 :global t
1190 :group 'whitespace
1191 (let ((whitespace-style '(newline-mark newline)))
1192 (global-whitespace-mode (if global-whitespace-newline-mode
1193 1 -1))
1194 ;; sync states (running a batch job)
1195 (setq global-whitespace-newline-mode global-whitespace-mode)))
1198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1199 ;;;; User commands - Toggle
1202 (defconst whitespace-style-value-list
1203 '(face
1204 tabs
1205 spaces
1206 trailing
1207 lines
1208 lines-tail
1209 newline
1210 empty
1211 indentation
1212 indentation::tab
1213 indentation::space
1214 space-after-tab
1215 space-after-tab::tab
1216 space-after-tab::space
1217 space-before-tab
1218 space-before-tab::tab
1219 space-before-tab::space
1220 help-newline ; value used by `whitespace-insert-option-mark'
1221 tab-mark
1222 space-mark
1223 newline-mark
1225 "List of valid `whitespace-style' values.")
1228 (defconst whitespace-toggle-option-alist
1229 '((?f . face)
1230 (?t . tabs)
1231 (?s . spaces)
1232 (?r . trailing)
1233 (?l . lines)
1234 (?L . lines-tail)
1235 (?n . newline)
1236 (?e . empty)
1237 (?\C-i . indentation)
1238 (?I . indentation::tab)
1239 (?i . indentation::space)
1240 (?\C-a . space-after-tab)
1241 (?A . space-after-tab::tab)
1242 (?a . space-after-tab::space)
1243 (?\C-b . space-before-tab)
1244 (?B . space-before-tab::tab)
1245 (?b . space-before-tab::space)
1246 (?T . tab-mark)
1247 (?S . space-mark)
1248 (?N . newline-mark)
1249 (?x . whitespace-style)
1251 "Alist of toggle options.
1253 Each element has the form:
1255 (CHAR . SYMBOL)
1257 Where:
1259 CHAR is a char which the user will have to type.
1261 SYMBOL is a valid symbol associated with CHAR.
1262 See `whitespace-style-value-list'.")
1265 (defvar whitespace-active-style nil
1266 "Used to save locally `whitespace-style' value.")
1268 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1269 "Used to save locally `indent-tabs-mode' value.")
1271 (defvar whitespace-tab-width tab-width
1272 "Used to save locally `tab-width' value.")
1274 (defvar whitespace-point (point)
1275 "Used to save locally current point value.
1276 Used by function `whitespace-trailing-regexp' (which see).")
1278 (defvar whitespace-font-lock-refontify nil
1279 "Used to save locally the font-lock refontify state.
1280 Used by function `whitespace-post-command-hook' (which see).")
1282 (defvar whitespace-bob-marker nil
1283 "Used to save locally the bob marker value.
1284 Used by function `whitespace-post-command-hook' (which see).")
1286 (defvar whitespace-eob-marker nil
1287 "Used to save locally the eob marker value.
1288 Used by function `whitespace-post-command-hook' (which see).")
1290 (defvar whitespace-buffer-changed nil
1291 "Used to indicate locally if buffer changed.
1292 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1293 functions (which see).")
1296 ;;;###autoload
1297 (defun whitespace-toggle-options (arg)
1298 "Toggle local `whitespace-mode' options.
1300 If local whitespace-mode is off, toggle the option given by ARG
1301 and turn on local whitespace-mode.
1303 If local whitespace-mode is on, toggle the option given by ARG
1304 and restart local whitespace-mode.
1306 Interactively, it reads one of the following chars:
1308 CHAR MEANING
1309 (VIA FACES)
1310 f toggle face visualization
1311 t toggle TAB visualization
1312 s toggle SPACE and HARD SPACE visualization
1313 r toggle trailing blanks visualization
1314 l toggle \"long lines\" visualization
1315 L toggle \"long lines\" tail visualization
1316 n toggle NEWLINE visualization
1317 e toggle empty line at bob and/or eob visualization
1318 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1319 I toggle indentation SPACEs visualization
1320 i toggle indentation TABs visualization
1321 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1322 A toggle SPACEs after TAB: SPACEs visualization
1323 a toggle SPACEs after TAB: TABs visualization
1324 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1325 B toggle SPACEs before TAB: SPACEs visualization
1326 b toggle SPACEs before TAB: TABs visualization
1328 (VIA DISPLAY TABLE)
1329 T toggle TAB visualization
1330 S toggle SPACEs before TAB visualization
1331 N toggle NEWLINE visualization
1333 x restore `whitespace-style' value
1334 ? display brief help
1336 Non-interactively, ARG should be a symbol or a list of symbols.
1337 The valid symbols are:
1339 face toggle face visualization
1340 tabs toggle TAB visualization
1341 spaces toggle SPACE and HARD SPACE visualization
1342 trailing toggle trailing blanks visualization
1343 lines toggle \"long lines\" visualization
1344 lines-tail toggle \"long lines\" tail visualization
1345 newline toggle NEWLINE visualization
1346 empty toggle empty line at bob and/or eob visualization
1347 indentation toggle indentation SPACEs visualization
1348 indentation::tab toggle indentation SPACEs visualization
1349 indentation::space toggle indentation TABs visualization
1350 space-after-tab toggle SPACEs after TAB visualization
1351 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1352 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1353 space-before-tab toggle SPACEs before TAB visualization
1354 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1355 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1357 tab-mark toggle TAB visualization
1358 space-mark toggle SPACEs before TAB visualization
1359 newline-mark toggle NEWLINE visualization
1361 whitespace-style restore `whitespace-style' value
1363 See `whitespace-style' and `indent-tabs-mode' for documentation."
1364 (interactive (whitespace-interactive-char t))
1365 (let ((whitespace-style
1366 (whitespace-toggle-list t arg whitespace-active-style)))
1367 (whitespace-mode 0)
1368 (whitespace-mode 1)))
1371 (defvar whitespace-toggle-style nil
1372 "Used to toggle the global `whitespace-style' value.")
1375 ;;;###autoload
1376 (defun global-whitespace-toggle-options (arg)
1377 "Toggle global `whitespace-mode' options.
1379 If global whitespace-mode is off, toggle the option given by ARG
1380 and turn on global whitespace-mode.
1382 If global whitespace-mode is on, toggle the option given by ARG
1383 and restart global whitespace-mode.
1385 Interactively, it accepts one of the following chars:
1387 CHAR MEANING
1388 (VIA FACES)
1389 f toggle face visualization
1390 t toggle TAB visualization
1391 s toggle SPACE and HARD SPACE visualization
1392 r toggle trailing blanks visualization
1393 l toggle \"long lines\" visualization
1394 L toggle \"long lines\" tail visualization
1395 n toggle NEWLINE visualization
1396 e toggle empty line at bob and/or eob visualization
1397 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1398 I toggle indentation SPACEs visualization
1399 i toggle indentation TABs visualization
1400 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1401 A toggle SPACEs after TAB: SPACEs visualization
1402 a toggle SPACEs after TAB: TABs visualization
1403 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1404 B toggle SPACEs before TAB: SPACEs visualization
1405 b toggle SPACEs before TAB: TABs visualization
1407 (VIA DISPLAY TABLE)
1408 T toggle TAB visualization
1409 S toggle SPACEs before TAB visualization
1410 N toggle NEWLINE visualization
1412 x restore `whitespace-style' value
1413 ? display brief help
1415 Non-interactively, ARG should be a symbol or a list of symbols.
1416 The valid symbols are:
1418 face toggle face visualization
1419 tabs toggle TAB visualization
1420 spaces toggle SPACE and HARD SPACE visualization
1421 trailing toggle trailing blanks visualization
1422 lines toggle \"long lines\" visualization
1423 lines-tail toggle \"long lines\" tail visualization
1424 newline toggle NEWLINE visualization
1425 empty toggle empty line at bob and/or eob visualization
1426 indentation toggle indentation SPACEs visualization
1427 indentation::tab toggle indentation SPACEs visualization
1428 indentation::space toggle indentation TABs visualization
1429 space-after-tab toggle SPACEs after TAB visualization
1430 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1431 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1432 space-before-tab toggle SPACEs before TAB visualization
1433 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1434 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1436 tab-mark toggle TAB visualization
1437 space-mark toggle SPACEs before TAB visualization
1438 newline-mark toggle NEWLINE visualization
1440 whitespace-style restore `whitespace-style' value
1442 See `whitespace-style' and `indent-tabs-mode' for documentation."
1443 (interactive (whitespace-interactive-char nil))
1444 (let ((whitespace-style
1445 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1446 (setq whitespace-toggle-style whitespace-style)
1447 (global-whitespace-mode 0)
1448 (global-whitespace-mode 1)))
1451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1452 ;;;; User commands - Cleanup
1455 ;;;###autoload
1456 (defun whitespace-cleanup ()
1457 "Cleanup some blank problems in all buffer or at region.
1459 It usually applies to the whole buffer, but in transient mark
1460 mode when the mark is active, it applies to the region. It also
1461 applies to the region when it is not in transient mark mode, the
1462 mark is active and \\[universal-argument] was pressed just before
1463 calling `whitespace-cleanup' interactively.
1465 See also `whitespace-cleanup-region'.
1467 The problems cleaned up are:
1469 1. empty lines at beginning of buffer.
1470 2. empty lines at end of buffer.
1471 If `whitespace-style' includes the value `empty', remove all
1472 empty lines at beginning and/or end of buffer.
1474 3. 8 or more SPACEs at beginning of line.
1475 If `whitespace-style' includes the value `indentation':
1476 replace 8 or more SPACEs at beginning of line by TABs, if
1477 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1478 SPACEs.
1479 If `whitespace-style' includes the value `indentation::tab',
1480 replace 8 or more SPACEs at beginning of line by TABs.
1481 If `whitespace-style' includes the value `indentation::space',
1482 replace TABs by SPACEs.
1484 4. SPACEs before TAB.
1485 If `whitespace-style' includes the value `space-before-tab':
1486 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1487 otherwise, replace TABs by SPACEs.
1488 If `whitespace-style' includes the value
1489 `space-before-tab::tab', replace SPACEs by TABs.
1490 If `whitespace-style' includes the value
1491 `space-before-tab::space', replace TABs by SPACEs.
1493 5. SPACEs or TABs at end of line.
1494 If `whitespace-style' includes the value `trailing', remove
1495 all SPACEs or TABs at end of line.
1497 6. 8 or more SPACEs after TAB.
1498 If `whitespace-style' includes the value `space-after-tab':
1499 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1500 otherwise, replace TABs by SPACEs.
1501 If `whitespace-style' includes the value
1502 `space-after-tab::tab', replace SPACEs by TABs.
1503 If `whitespace-style' includes the value
1504 `space-after-tab::space', replace TABs by SPACEs.
1506 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1507 documentation."
1508 (interactive "@")
1509 (cond
1510 ;; read-only buffer
1511 (buffer-read-only
1512 (whitespace-warn-read-only "cleanup"))
1513 ;; region active
1514 ((and (or transient-mark-mode
1515 current-prefix-arg)
1516 mark-active)
1517 ;; PROBLEMs 1 and 2 are not handled in region
1518 ;; PROBLEM 3: 8 or more SPACEs at bol
1519 ;; PROBLEM 4: SPACEs before TAB
1520 ;; PROBLEM 5: SPACEs or TABs at eol
1521 ;; PROBLEM 6: 8 or more SPACEs after TAB
1522 (whitespace-cleanup-region (region-beginning) (region-end)))
1523 ;; whole buffer
1525 (save-excursion
1526 (save-match-data ;FIXME: Why?
1527 ;; PROBLEM 1: empty lines at bob
1528 ;; PROBLEM 2: empty lines at eob
1529 ;; ACTION: remove all empty lines at bob and/or eob
1530 (when (memq 'empty whitespace-style)
1531 (let (overwrite-mode) ; enforce no overwrite
1532 (goto-char (point-min))
1533 (when (looking-at whitespace-empty-at-bob-regexp)
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 ;FIXME: Why?
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 0 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 0))
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 ;FIXME: Why?
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 nil
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 (= whitespace-point (match-end 1)) ;; loop if point at eol
2412 (setq status nil))) ;; end of buffer
2413 status))
2416 (defun whitespace-empty-at-bob-regexp (limit)
2417 "Match spaces at beginning of buffer which do not contain the point at \
2418 beginning of buffer."
2419 (let ((b (point))
2421 (cond
2422 ;; at bob
2423 ((= b 1)
2424 (setq r (and (/= whitespace-point 1)
2425 (looking-at whitespace-empty-at-bob-regexp)))
2426 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2427 ;; inside bob empty region
2428 ((<= limit whitespace-bob-marker)
2429 (setq r (looking-at whitespace-empty-at-bob-regexp))
2430 (if r
2431 (when (< (match-end 1) limit)
2432 (set-marker whitespace-bob-marker (match-end 1)))
2433 (set-marker whitespace-bob-marker b)))
2434 ;; intersection with end of bob empty region
2435 ((<= b whitespace-bob-marker)
2436 (setq r (looking-at whitespace-empty-at-bob-regexp))
2437 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2438 ;; it is not inside bob empty region
2440 (setq r nil)))
2441 ;; move to end of matching
2442 (and r (goto-char (match-end 1)))
2446 (defsubst whitespace-looking-back (regexp limit)
2447 (save-excursion
2448 (when (/= 0 (skip-chars-backward " \t\n" limit))
2449 (unless (bolp)
2450 (forward-line 1))
2451 (looking-at regexp))))
2454 (defun whitespace-empty-at-eob-regexp (limit)
2455 "Match spaces at end of buffer which do not contain the point at end of \
2456 buffer."
2457 (let ((b (point))
2458 (e (1+ (buffer-size)))
2460 (cond
2461 ;; at eob
2462 ((= limit e)
2463 (when (/= whitespace-point e)
2464 (goto-char limit)
2465 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
2466 (if r
2467 (set-marker whitespace-eob-marker (match-beginning 1))
2468 (set-marker whitespace-eob-marker limit)
2469 (goto-char b))) ; return back to initial position
2470 ;; inside eob empty region
2471 ((>= b whitespace-eob-marker)
2472 (goto-char limit)
2473 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2474 (if r
2475 (when (> (match-beginning 1) b)
2476 (set-marker whitespace-eob-marker (match-beginning 1)))
2477 (set-marker whitespace-eob-marker limit)
2478 (goto-char b))) ; return back to initial position
2479 ;; intersection with beginning of eob empty region
2480 ((>= limit whitespace-eob-marker)
2481 (goto-char limit)
2482 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2483 (if r
2484 (set-marker whitespace-eob-marker (match-beginning 1))
2485 (set-marker whitespace-eob-marker limit)
2486 (goto-char b))) ; return back to initial position
2487 ;; it is not inside eob empty region
2489 (setq r nil)))
2493 (defun whitespace-buffer-changed (_beg _end)
2494 "Set `whitespace-buffer-changed' variable to t."
2495 (setq whitespace-buffer-changed t))
2498 (defun whitespace-post-command-hook ()
2499 "Save current point into `whitespace-point' variable.
2500 Also refontify when necessary."
2501 (setq whitespace-point (point)) ; current point position
2502 (let ((refontify
2504 ;; it is at end of line ...
2505 (and (eolp)
2506 ;; ... with trailing SPACE or TAB
2507 (or (= (preceding-char) ?\ )
2508 (= (preceding-char) ?\t)))
2509 ;; it is at beginning of buffer (bob)
2510 (= whitespace-point 1)
2511 ;; the buffer was modified and ...
2512 (and whitespace-buffer-changed
2514 ;; ... or inside bob whitespace region
2515 (<= whitespace-point whitespace-bob-marker)
2516 ;; ... or at bob whitespace region border
2517 (and (= whitespace-point (1+ whitespace-bob-marker))
2518 (= (preceding-char) ?\n))))
2519 ;; it is at end of buffer (eob)
2520 (= whitespace-point (1+ (buffer-size)))
2521 ;; the buffer was modified and ...
2522 (and whitespace-buffer-changed
2524 ;; ... or inside eob whitespace region
2525 (>= whitespace-point whitespace-eob-marker)
2526 ;; ... or at eob whitespace region border
2527 (and (= whitespace-point (1- whitespace-eob-marker))
2528 (= (following-char) ?\n)))))))
2529 (when (or refontify (> whitespace-font-lock-refontify 0))
2530 (setq whitespace-buffer-changed nil)
2531 ;; adjust refontify counter
2532 (setq whitespace-font-lock-refontify
2533 (if refontify
2535 (1- whitespace-font-lock-refontify)))
2536 ;; refontify
2537 (jit-lock-refontify))))
2540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2541 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2544 (defun whitespace-style-mark-p ()
2545 "Return t if there is some visualization via display table."
2546 (or (memq 'tab-mark whitespace-active-style)
2547 (memq 'space-mark whitespace-active-style)
2548 (memq 'newline-mark whitespace-active-style)))
2551 (defsubst whitespace-char-valid-p (char)
2552 ;; This check should be improved!!!
2553 (or (< char 256)
2554 (characterp char)))
2557 (defun whitespace-display-vector-p (vec)
2558 "Return true if every character in vector VEC can be displayed."
2559 (let ((i (length vec)))
2560 (when (> i 0)
2561 (while (and (>= (setq i (1- i)) 0)
2562 (whitespace-char-valid-p (aref vec i))))
2563 (< i 0))))
2566 (defun whitespace-display-char-on ()
2567 "Turn on character display mapping."
2568 (when (and whitespace-display-mappings
2569 (whitespace-style-mark-p))
2570 (let (vecs vec)
2571 ;; Remember whether a buffer has a local display table.
2572 (unless whitespace-display-table-was-local
2573 (setq whitespace-display-table-was-local t
2574 whitespace-display-table
2575 (copy-sequence buffer-display-table))
2576 ;; Assure `buffer-display-table' is unique
2577 ;; when two or more windows are visible.
2578 (setq buffer-display-table
2579 (copy-sequence buffer-display-table)))
2580 (unless buffer-display-table
2581 (setq buffer-display-table (make-display-table)))
2582 (dolist (entry whitespace-display-mappings)
2583 ;; check if it is to display this mark
2584 (when (memq (car entry) whitespace-style)
2585 ;; Get a displayable mapping.
2586 (setq vecs (cddr entry))
2587 (while (and vecs
2588 (not (whitespace-display-vector-p (car vecs))))
2589 (setq vecs (cdr vecs)))
2590 ;; Display a valid mapping.
2591 (when vecs
2592 (setq vec (copy-sequence (car vecs)))
2593 ;; NEWLINE char
2594 (when (and (eq (cadr entry) ?\n)
2595 (memq 'newline whitespace-active-style))
2596 ;; Only insert face bits on NEWLINE char mapping to avoid
2597 ;; obstruction of other faces like TABs and (HARD) SPACEs
2598 ;; faces, font-lock faces, etc.
2599 (dotimes (i (length vec))
2600 (or (eq (aref vec i) ?\n)
2601 (aset vec i
2602 (make-glyph-code (aref vec i)
2603 whitespace-newline)))))
2604 ;; Display mapping
2605 (aset buffer-display-table (cadr entry) vec)))))))
2608 (defun whitespace-display-char-off ()
2609 "Turn off character display mapping."
2610 (and whitespace-display-mappings
2611 (whitespace-style-mark-p)
2612 whitespace-display-table-was-local
2613 (setq whitespace-display-table-was-local nil
2614 buffer-display-table whitespace-display-table)))
2617 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2618 ;;;; Hook
2621 (defun whitespace-action-when-on ()
2622 "Action to be taken always when local whitespace is turned on."
2623 (cond ((memq 'cleanup whitespace-action)
2624 (whitespace-cleanup))
2625 ((memq 'report-on-bogus whitespace-action)
2626 (whitespace-report nil t))))
2629 (defun whitespace-write-file-hook ()
2630 "Action to be taken when buffer is written.
2631 It should be added buffer-locally to `write-file-functions'."
2632 (cond ((memq 'auto-cleanup whitespace-action)
2633 (whitespace-cleanup))
2634 ((memq 'abort-on-bogus whitespace-action)
2635 (when (whitespace-report nil t)
2636 (error "Abort write due to whitespace problems in %s"
2637 (buffer-name)))))
2638 nil) ; continue hook processing
2641 (defun whitespace-warn-read-only (msg)
2642 "Warn if buffer is read-only."
2643 (when (memq 'warn-if-read-only whitespace-action)
2644 (message "Can't %s: %s is read-only" msg (buffer-name))))
2647 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2650 (defun whitespace-unload-function ()
2651 "Unload the whitespace library."
2652 (global-whitespace-mode -1)
2653 ;; be sure all local whitespace mode is turned off
2654 (save-current-buffer
2655 (dolist (buf (buffer-list))
2656 (set-buffer buf)
2657 (whitespace-mode -1)))
2658 nil) ; continue standard unloading
2661 (provide 'whitespace)
2664 (run-hooks 'whitespace-load-hook)
2667 ;;; whitespace.el ends here