Remove time-stamp annoyance.
[emacs.git] / lisp / whitespace.el
blob5972c1f1751ba533f8ab2060d1c97b24991844f1
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
8 ;; Keywords: data, wp
9 ;; Version: 11.0
10 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published
16 ;; by the Free Software Foundation; either version 3, or (at your
17 ;; option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; Introduction
34 ;; ------------
36 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
37 ;; and NEWLINE).
39 ;; whitespace uses two ways to visualize blanks: faces and display
40 ;; table.
42 ;; * Faces are used to highlight the background with a color.
43 ;; whitespace uses font-lock to highlight blank characters.
45 ;; * Display table changes the way a character is displayed, that is,
46 ;; it provides a visual mark for characters, for example, at the end
47 ;; of line (?\xB6), at SPACEs (?\xB7) and at TABs (?\xBB).
49 ;; The `whitespace-style' variable selects which way blanks are
50 ;; visualized.
52 ;; Note that when whitespace is turned on, whitespace saves the
53 ;; font-lock state, that is, if font-lock is on or off. And
54 ;; whitespace restores the font-lock state when it is turned off. So,
55 ;; if whitespace is turned on and font-lock is off, whitespace also
56 ;; turns on the font-lock to highlight blanks, but the font-lock will
57 ;; be turned off when whitespace is turned off. Thus, turn on
58 ;; font-lock before whitespace is on, if you want that font-lock
59 ;; continues on after whitespace is turned off.
61 ;; When whitespace is on, it takes care of highlighting some special
62 ;; characters over the default mechanism of `nobreak-char-display'
63 ;; (which see) and `show-trailing-whitespace' (which see).
65 ;; There are two ways of using whitespace: local and global.
67 ;; * Local whitespace affects only the current buffer.
69 ;; * Global whitespace affects all current and future buffers. That
70 ;; is, if you turn on global whitespace and then create a new
71 ;; buffer, the new buffer will also have whitespace on. The
72 ;; `whitespace-global-modes' variable controls which major-mode will
73 ;; be automagically turned on.
75 ;; You can mix the local and global usage without any conflict. But
76 ;; local whitespace has priority over global whitespace. Whitespace
77 ;; mode is active in a buffer if you have enabled it in that buffer or
78 ;; if you have enabled it globally.
80 ;; When global and local whitespace are on:
82 ;; * if local whitespace is turned off, whitespace is turned off for
83 ;; the current buffer only.
85 ;; * if global whitespace is turned off, whitespace continues on only
86 ;; in the buffers in which local whitespace is on.
88 ;; To use whitespace, insert in your ~/.emacs:
90 ;; (require 'whitespace-mode)
92 ;; Or autoload at least one of the commands`whitespace-mode',
93 ;; `whitespace-toggle-options', `global-whitespace-mode' or
94 ;; `global-whitespace-toggle-options'. For example:
96 ;; (autoload 'whitespace-mode "whitespace"
97 ;; "Toggle whitespace visualization." t)
98 ;; (autoload 'whitespace-toggle-options "whitespace"
99 ;; "Toggle local `whitespace-mode' options." t)
101 ;; whitespace was inspired by:
103 ;; whitespace.el Rajesh Vaidheeswarran <rv@gnu.org>
104 ;; Warn about and clean bogus whitespaces in the file
105 ;; (inspired the idea to warn and clean some blanks)
106 ;; This was the original `whitespace.el' which was replaced by
107 ;; `blank-mode.el'. And later `blank-mode.el' was renamed to
108 ;; `whitespace.el'.
110 ;; show-whitespace-mode.el Aurelien Tisne <aurelien.tisne@free.fr>
111 ;; Simple mode to highlight whitespaces
112 ;; (inspired the idea to use font-lock)
114 ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li>
115 ;; Major mode for editing Whitespace
116 ;; (inspired the idea to use display table)
118 ;; visws.el Miles Bader <miles@gnu.org>
119 ;; Make whitespace visible
120 ;; (handle display table, his code was modified, but the main
121 ;; idea was kept)
124 ;; Using whitespace
125 ;; ----------------
127 ;; There is no problem if you mix local and global minor mode usage.
129 ;; * LOCAL whitespace:
130 ;; + To toggle whitespace options locally, type:
132 ;; M-x whitespace-toggle-options RET
134 ;; + To activate whitespace locally, type:
136 ;; C-u 1 M-x whitespace-mode RET
138 ;; + To deactivate whitespace locally, type:
140 ;; C-u 0 M-x whitespace-mode RET
142 ;; + To toggle whitespace locally, type:
144 ;; M-x whitespace-mode RET
146 ;; * GLOBAL whitespace:
147 ;; + To toggle whitespace options globally, type:
149 ;; M-x global-whitespace-toggle-options RET
151 ;; + To activate whitespace globally, type:
153 ;; C-u 1 M-x global-whitespace-mode RET
155 ;; + To deactivate whitespace globally, type:
157 ;; C-u 0 M-x global-whitespace-mode RET
159 ;; + To toggle whitespace globally, type:
161 ;; M-x global-whitespace-mode RET
163 ;; There are also the following useful commands:
165 ;; `whitespace-report'
166 ;; Report some blank problems in buffer.
168 ;; `whitespace-report-region'
169 ;; Report some blank problems in a region.
171 ;; `whitespace-cleanup'
172 ;; Cleanup some blank problems in all buffer or at region.
174 ;; `whitespace-cleanup-region'
175 ;; Cleanup some blank problems at region.
177 ;; The problems, which are cleaned up, are:
179 ;; 1. empty lines at beginning of buffer.
180 ;; 2. empty lines at end of buffer.
181 ;; If `whitespace-style' includes the value `empty', remove all
182 ;; empty lines at beginning and/or end of buffer.
184 ;; 3. 8 or more SPACEs at beginning of line.
185 ;; If `whitespace-style' includes the value `indentation':
186 ;; replace 8 or more SPACEs at beginning of line by TABs, if
187 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
188 ;; SPACEs.
189 ;; If `whitespace-style' includes the value `indentation::tab',
190 ;; replace 8 or more SPACEs at beginning of line by TABs.
191 ;; If `whitespace-style' includes the value `indentation::space',
192 ;; replace TABs by SPACEs.
194 ;; 4. SPACEs before TAB.
195 ;; If `whitespace-style' includes the value `space-before-tab':
196 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
197 ;; otherwise, replace TABs by SPACEs.
198 ;; If `whitespace-style' includes the value
199 ;; `space-before-tab::tab', replace SPACEs by TABs.
200 ;; If `whitespace-style' includes the value
201 ;; `space-before-tab::space', replace TABs by SPACEs.
203 ;; 5. SPACEs or TABs at end of line.
204 ;; If `whitespace-style' includes the value `trailing', remove all
205 ;; SPACEs or TABs at end of line.
207 ;; 6. 8 or more SPACEs after TAB.
208 ;; If `whitespace-style' includes the value `space-after-tab':
209 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
210 ;; otherwise, replace TABs by SPACEs.
211 ;; If `whitespace-style' includes the value `space-after-tab::tab',
212 ;; replace SPACEs by TABs.
213 ;; If `whitespace-style' includes the value
214 ;; `space-after-tab::space', replace TABs by SPACEs.
217 ;; Hooks
218 ;; -----
220 ;; whitespace has the following hook variables:
222 ;; `whitespace-mode-hook'
223 ;; It is evaluated always when whitespace is turned on locally.
225 ;; `global-whitespace-mode-hook'
226 ;; It is evaluated always when whitespace is turned on globally.
228 ;; `whitespace-load-hook'
229 ;; It is evaluated after whitespace package is loaded.
232 ;; Options
233 ;; -------
235 ;; Below it's shown a brief description of whitespace options, please,
236 ;; see the options declaration in the code for a long documentation.
238 ;; `whitespace-style' Specify which kind of blank is
239 ;; visualized.
241 ;; `whitespace-space' Face used to visualize SPACE.
243 ;; `whitespace-hspace' Face used to visualize HARD SPACE.
245 ;; `whitespace-tab' Face used to visualize TAB.
247 ;; `whitespace-newline' Face used to visualize NEWLINE char
248 ;; mapping.
250 ;; `whitespace-trailing' Face used to visualize trailing
251 ;; blanks.
253 ;; `whitespace-line' Face used to visualize "long" lines.
255 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
256 ;; before TAB.
258 ;; `whitespace-indentation' Face used to visualize 8 or more
259 ;; SPACEs at beginning of line.
261 ;; `whitespace-empty' Face used to visualize empty lines at
262 ;; beginning and/or end of buffer.
264 ;; `whitespace-space-after-tab' Face used to visualize 8 or more
265 ;; SPACEs after TAB.
267 ;; `whitespace-space-regexp' Specify SPACE characters regexp.
269 ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
271 ;; `whitespace-tab-regexp' Specify TAB characters regexp.
273 ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
275 ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
276 ;; regexp.
278 ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
279 ;; SPACEs at beginning of line.
281 ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
282 ;; at beginning of buffer.
284 ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
285 ;; at end of buffer.
287 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
288 ;; SPACEs after TAB.
290 ;; `whitespace-line-column' Specify column beyond which the line
291 ;; is highlighted.
293 ;; `whitespace-display-mappings' Specify an alist of mappings
294 ;; for displaying characters.
296 ;; `whitespace-global-modes' Modes for which global
297 ;; `whitespace-mode' is automagically
298 ;; turned on.
300 ;; `whitespace-action' Specify which action is taken when a
301 ;; buffer is visited, killed or written.
304 ;; Acknowledgements
305 ;; ----------------
307 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
308 ;; `indent-tabs-mode' usage suggestion.
310 ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook
311 ;; actions when buffer is written or killed as the original whitespace
312 ;; package had.
314 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
315 ;; lines tail. See EightyColumnRule (EmacsWiki).
317 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
318 ;; * `define-minor-mode'.
319 ;; * `global-whitespace-*' name for global commands.
321 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
323 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
324 ;; suggestion.
326 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
327 ;; helping to fix `find-file-hooks' reference.
329 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
330 ;; indicating defface byte-compilation warnings.
332 ;; Thanks to TimOCallaghan (EmacsWiki) for the idea about highlight
333 ;; "long" lines. See EightyColumnRule (EmacsWiki).
335 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
336 ;; newline character mapping.
338 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
339 ;; whitespace-mode.el on XEmacs.
341 ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via
342 ;; visws.el (his code was modified, but the main idea was kept).
344 ;; Thanks to:
345 ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el
346 ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el
347 ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el
348 ;; Miles Bader <miles@gnu.org> visws.el
349 ;; And to all people who contributed with them.
352 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
354 ;;; code:
357 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
358 ;;;; User Variables:
361 ;;; Interface to the command system
364 (defgroup whitespace nil
365 "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
366 :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
367 :version "23.1"
368 :group 'wp
369 :group 'data)
372 (defcustom whitespace-style
373 '(tabs spaces trailing lines space-before-tab newline
374 indentation empty space-after-tab
375 space-mark tab-mark newline-mark)
376 "*Specify which kind of blank is visualized.
378 It's a list containing some or all of the following values:
380 trailing trailing blanks are visualized via faces.
382 tabs TABs are visualized via faces.
384 spaces SPACEs and HARD SPACEs are visualized via
385 faces.
387 lines lines whose have columns beyond
388 `whitespace-line-column' are highlighted via
389 faces .
390 Whole line is highlighted.
391 It has precedence over `lines-tail' (see
392 below).
394 lines-tail lines whose have columns beyond
395 `whitespace-line-column' are highlighted via
396 faces.
397 But only the part of line which goes
398 beyond `whitespace-line-column' column.
399 It has effect only if `lines' (see above)
400 is not present in `whitespace-style'.
402 newline NEWLINEs are visualized via faces.
404 empty empty lines at beginning and/or end of buffer
405 are visualized via faces.
407 indentation::tab 8 or more SPACEs at beginning of line are
408 visualized via faces.
410 indentation::space TABs at beginning of line are visualized via
411 faces.
413 indentation 8 or more SPACEs at beginning of line are
414 visualized, if `indent-tabs-mode' (which see)
415 is non-nil; otherwise, TABs at beginning of
416 line are visualized via faces.
418 space-after-tab::tab 8 or more SPACEs after a TAB are
419 visualized via faces.
421 space-after-tab::space TABs are visualized when occurs 8 or
422 more SPACEs after a TAB via faces.
424 space-after-tab 8 or more SPACEs after a TAB are
425 visualized, if `indent-tabs-mode'
426 (which see) is non-nil; otherwise,
427 the TABs are visualized via faces.
429 space-before-tab::tab SPACEs before TAB are visualized via
430 faces.
432 space-before-tab::space TABs are visualized when occurs SPACEs
433 before TAB via faces.
435 space-before-tab SPACEs before TAB are visualized, if
436 `indent-tabs-mode' (which see) is
437 non-nil; otherwise, the TABs are
438 visualized via faces.
440 space-mark SPACEs and HARD SPACEs are visualized via
441 display table.
443 tab-mark TABs are visualized via display table.
445 newline-mark NEWLINEs are visualized via display table.
447 Any other value is ignored.
449 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
450 via display table.
452 There is an evaluation order for some values, if some values are
453 included in `whitespace-style' list. For example, if
454 indentation, indentation::tab and/or indentation::space are
455 included in `whitespace-style' list. The evaluation order for
456 these values is:
458 * For indentation:
459 1. indentation
460 2. indentation::tab
461 3. indentation::space
463 * For SPACEs after TABs:
464 1. space-after-tab
465 2. space-after-tab::tab
466 3. space-after-tab::space
468 * For SPACEs before TABs:
469 1. space-before-tab
470 2. space-before-tab::tab
471 3. space-before-tab::space
473 So, for example, if indentation and indentation::space are
474 included in `whitespace-style' list, the indentation value is
475 evaluated instead of indentation::space value.
477 See also `whitespace-display-mappings' for documentation."
478 :type '(repeat :tag "Kind of Blank"
479 (choice :tag "Kind of Blank Face"
480 (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
481 trailing)
482 (const :tag "(Face) SPACEs and HARD SPACEs"
483 spaces)
484 (const :tag "(Face) TABs" tabs)
485 (const :tag "(Face) Lines" lines)
486 (const :tag "(Face) SPACEs before TAB"
487 space-before-tab)
488 (const :tag "(Face) NEWLINEs" newline)
489 (const :tag "(Face) Indentation SPACEs"
490 indentation)
491 (const :tag "(Face) Empty Lines At BOB And/Or EOB"
492 empty)
493 (const :tag "(Face) SPACEs after TAB"
494 space-after-tab)
495 (const :tag "(Mark) SPACEs and HARD SPACEs"
496 space-mark)
497 (const :tag "(Mark) TABs" tab-mark)
498 (const :tag "(Mark) NEWLINEs" newline-mark)))
499 :group 'whitespace)
502 (defcustom whitespace-space 'whitespace-space
503 "*Symbol face used to visualize SPACE.
505 Used when `whitespace-style' includes the value `spaces'."
506 :type 'face
507 :group 'whitespace)
510 (defface whitespace-space
511 '((((class color) (background dark))
512 (:background "grey20" :foreground "aquamarine3"))
513 (((class color) (background light))
514 (:background "LightYellow" :foreground "aquamarine3"))
515 (t (:inverse-video t)))
516 "Face used to visualize SPACE."
517 :group 'whitespace)
520 (defcustom whitespace-hspace 'whitespace-hspace
521 "*Symbol face used to visualize HARD SPACE.
523 Used when `whitespace-style' includes the value `spaces'."
524 :type 'face
525 :group 'whitespace)
528 (defface whitespace-hspace ; 'nobreak-space
529 '((((class color) (background dark))
530 (:background "grey24" :foreground "aquamarine3"))
531 (((class color) (background light))
532 (:background "LemonChiffon3" :foreground "aquamarine3"))
533 (t (:inverse-video t)))
534 "Face used to visualize HARD SPACE."
535 :group 'whitespace)
538 (defcustom whitespace-tab 'whitespace-tab
539 "*Symbol face used to visualize TAB.
541 Used when `whitespace-style' includes the value `tabs'."
542 :type 'face
543 :group 'whitespace)
546 (defface whitespace-tab
547 '((((class color) (background dark))
548 (:background "grey22" :foreground "aquamarine3"))
549 (((class color) (background light))
550 (:background "beige" :foreground "aquamarine3"))
551 (t (:inverse-video t)))
552 "Face used to visualize TAB."
553 :group 'whitespace)
556 (defcustom whitespace-newline 'whitespace-newline
557 "*Symbol face used to visualize NEWLINE char mapping.
559 See `whitespace-display-mappings'.
561 Used when `whitespace-style' includes the values `newline-mark'
562 and `newline'."
563 :type 'face
564 :group 'whitespace)
567 (defface whitespace-newline
568 '((((class color) (background dark))
569 (:background "grey26" :foreground "aquamarine3" :bold t))
570 (((class color) (background light))
571 (:background "linen" :foreground "aquamarine3" :bold t))
572 (t (:bold t :underline t)))
573 "Face used to visualize NEWLINE char mapping.
575 See `whitespace-display-mappings'."
576 :group 'whitespace)
579 (defcustom whitespace-trailing 'whitespace-trailing
580 "*Symbol face used to visualize traling blanks.
582 Used when `whitespace-style' includes the value `trailing'."
583 :type 'face
584 :group 'whitespace)
587 (defface whitespace-trailing ; 'trailing-whitespace
588 '((((class mono)) (:inverse-video t :bold t :underline t))
589 (t (:background "red1" :foreground "yellow" :bold t)))
590 "Face used to visualize trailing blanks."
591 :group 'whitespace)
594 (defcustom whitespace-line 'whitespace-line
595 "*Symbol face used to visualize \"long\" lines.
597 See `whitespace-line-column'.
599 Used when `whitespace-style' includes the value `line'."
600 :type 'face
601 :group 'whitespace)
604 (defface whitespace-line
605 '((((class mono)) (:inverse-video t :bold t :underline t))
606 (t (:background "gray20" :foreground "violet")))
607 "Face used to visualize \"long\" lines.
609 See `whitespace-line-column'."
610 :group 'whitespace)
613 (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
614 "*Symbol face used to visualize SPACEs before TAB.
616 Used when `whitespace-style' includes the value `space-before-tab'."
617 :type 'face
618 :group 'whitespace)
621 (defface whitespace-space-before-tab
622 '((((class mono)) (:inverse-video t :bold t :underline t))
623 (t (:background "DarkOrange" :foreground "firebrick")))
624 "Face used to visualize SPACEs before TAB."
625 :group 'whitespace)
628 (defcustom whitespace-indentation 'whitespace-indentation
629 "*Symbol face used to visualize 8 or more SPACEs at beginning of line.
631 Used when `whitespace-style' includes the value `indentation'."
632 :type 'face
633 :group 'whitespace)
636 (defface whitespace-indentation
637 '((((class mono)) (:inverse-video t :bold t :underline t))
638 (t (:background "yellow" :foreground "firebrick")))
639 "Face used to visualize 8 or more SPACEs at beginning of line."
640 :group 'whitespace)
643 (defcustom whitespace-empty 'whitespace-empty
644 "*Symbol face used to visualize empty lines at beginning and/or end of buffer.
646 Used when `whitespace-style' includes the value `empty'."
647 :type 'face
648 :group 'whitespace)
651 (defface whitespace-empty
652 '((((class mono)) (:inverse-video t :bold t :underline t))
653 (t (:background "yellow" :foreground "firebrick")))
654 "Face used to visualize empty lines at beginning and/or end of buffer."
655 :group 'whitespace)
658 (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
659 "*Symbol face used to visualize 8 or more SPACEs after TAB.
661 Used when `whitespace-style' includes the value `space-after-tab'."
662 :type 'face
663 :group 'whitespace)
666 (defface whitespace-space-after-tab
667 '((((class mono)) (:inverse-video t :bold t :underline t))
668 (t (:background "yellow" :foreground "firebrick")))
669 "Face used to visualize 8 or more SPACEs after TAB."
670 :group 'whitespace)
673 (defcustom whitespace-hspace-regexp
674 "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
675 "*Specify HARD SPACE characters regexp.
677 If you're using `mule' package, there may be other characters besides:
679 \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
681 that should be considered HARD SPACE.
683 Here are some examples:
685 \"\\\\(^\\xA0+\\\\)\" \
686 visualize only leading HARD SPACEs.
687 \"\\\\(\\xA0+$\\\\)\" \
688 visualize only trailing HARD SPACEs.
689 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
690 visualize leading and/or trailing HARD SPACEs.
691 \"\\t\\\\(\\xA0+\\\\)\\t\" \
692 visualize only HARD SPACEs between TABs.
694 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
695 Use exactly one pair of enclosing \\\\( and \\\\).
697 Used when `whitespace-style' includes `spaces'."
698 :type '(regexp :tag "HARD SPACE Chars")
699 :group 'whitespace)
702 (defcustom whitespace-space-regexp "\\( +\\)"
703 "*Specify SPACE characters regexp.
705 If you're using `mule' package, there may be other characters
706 besides \" \" that should be considered SPACE.
708 Here are some examples:
710 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
711 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
712 \"\\\\(^ +\\\\| +$\\\\)\" \
713 visualize leading and/or trailing SPACEs.
714 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
716 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
717 Use exactly one pair of enclosing \\\\( and \\\\).
719 Used when `whitespace-style' includes `spaces'."
720 :type '(regexp :tag "SPACE Chars")
721 :group 'whitespace)
724 (defcustom whitespace-tab-regexp "\\(\t+\\)"
725 "*Specify TAB characters regexp.
727 If you're using `mule' package, there may be other characters
728 besides \"\\t\" that should be considered TAB.
730 Here are some examples:
732 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
733 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
734 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
735 visualize leading and/or trailing TABs.
736 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
738 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
739 Use exactly one pair of enclosing \\\\( and \\\\).
741 Used when `whitespace-style' includes `tabs'."
742 :type '(regexp :tag "TAB Chars")
743 :group 'whitespace)
746 (defcustom whitespace-trailing-regexp
747 "\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20"
748 "*Specify trailing characters regexp.
750 If you're using `mule' package, there may be other characters besides:
752 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
753 \"\\xF20\"
755 that should be considered blank.
757 NOTE: DO NOT enclose by \\\\( and \\\\) the elements to highlight.
758 `whitespace-mode' surrounds this regexp by \"\\\\(\\\\(\" and
759 \"\\\\)+\\\\)$\".
761 Used when `whitespace-style' includes `trailing'."
762 :type '(regexp :tag "Trailing Chars")
763 :group 'whitespace)
766 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
767 "*Specify SPACEs before TAB regexp.
769 If you're using `mule' package, there may be other characters besides:
771 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
772 \"\\xF20\"
774 that should be considered blank.
776 Used when `whitespace-style' includes `space-before-tab',
777 `space-before-tab::tab' or `space-before-tab::space'."
778 :type '(regexp :tag "SPACEs Before TAB")
779 :group 'whitespace)
782 (defcustom whitespace-indentation-regexp
783 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
784 . "^ *\\(\t+\\)[^\n]")
785 "*Specify regexp for 8 or more SPACEs at beginning of line.
787 It is a cons where the cons car is used for SPACEs visualization
788 and the cons cdr is used for TABs visualization.
790 If you're using `mule' package, there may be other characters besides:
792 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
793 \"\\xF20\"
795 that should be considered blank.
797 Used when `whitespace-style' includes `indentation',
798 `indentation::tab' or `indentation::space'."
799 :type '(cons (regexp :tag "Indentation SPACEs")
800 (regexp :tag "Indentation TABs"))
801 :group 'whitespace)
804 (defcustom whitespace-empty-at-bob-regexp "\\`\\(\\([ \t]*\n\\)+\\)"
805 "*Specify regexp for empty lines at beginning of buffer.
807 If you're using `mule' package, there may be other characters besides:
809 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
810 \"\\xF20\"
812 that should be considered blank.
814 Used when `whitespace-style' includes `empty'."
815 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
816 :group 'whitespace)
819 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)\\'"
820 "*Specify regexp for empty lines at end of buffer.
822 If you're using `mule' package, there may be other characters besides:
824 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
825 \"\\xF20\"
827 that should be considered blank.
829 Used when `whitespace-style' includes `empty'."
830 :type '(regexp :tag "Empty Lines At End Of Buffer")
831 :group 'whitespace)
834 (defcustom whitespace-space-after-tab-regexp
835 '("\t+\\(\\( \\{%d\\}\\)+\\)"
836 . "\\(\t+\\) +")
837 "*Specify regexp for 8 or more SPACEs after TAB.
839 It is a cons where the cons car is used for SPACEs visualization
840 and the cons cdr is used for TABs visualization.
842 If you're using `mule' package, there may be other characters besides:
844 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
845 \"\\xF20\"
847 that should be considered blank.
849 Used when `whitespace-style' includes `space-after-tab',
850 `space-after-tab::tab' or `space-after-tab::space'."
851 :type '(regexp :tag "SPACEs After TAB")
852 :group 'whitespace)
855 (defcustom whitespace-line-column 80
856 "*Specify column beyond which the line is highlighted.
858 Used when `whitespace-style' includes `lines' or `lines-tail'."
859 :type '(integer :tag "Line Length")
860 :group 'whitespace)
863 ;; Hacked from `visible-whitespace-mappings' in visws.el
864 (defcustom whitespace-display-mappings
866 (space-mark ?\ [?\xB7] [?.]) ; space - centered dot
867 (space-mark ?\xA0 [?\xA4] [?_]) ; hard space - currency
868 (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency
869 (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency
870 (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency
871 (space-mark ?\xF20 [?\xF24] [?_]) ; hard space - currency
872 ;; NEWLINE is displayed using the face `whitespace-newline'
873 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
874 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
875 ;; (newline-mark ?\n [?\xB6 ?\n] [?$ ?\n]) ; eol - pilcrow
876 ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
877 ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
878 ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
880 ;; WARNING: the mapping below has a problem.
881 ;; When a TAB occupies exactly one column, it will display the
882 ;; character ?\xBB at that column followed by a TAB which goes to
883 ;; the next TAB column.
884 ;; If this is a problem for you, please, comment the line below.
885 (tab-mark ?\t [?\xBB ?\t] [?\\ ?\t]) ; tab - left quote mark
887 "*Specify an alist of mappings for displaying characters.
889 Each element has the following form:
891 (KIND CHAR VECTOR...)
893 Where:
895 KIND is the kind of character.
896 It can be one of the following symbols:
898 tab-mark for TAB character
900 space-mark for SPACE or HARD SPACE character
902 newline-mark for NEWLINE character
904 CHAR is the character to be mapped.
906 VECTOR is a vector of characters to be displayed in place of CHAR.
907 The first display vector that can be displayed is used;
908 if no display vector for a mapping can be displayed, then
909 that character is displayed unmodified.
911 The NEWLINE character is displayed using the face given by
912 `whitespace-newline' variable.
914 Used when `whitespace-style' includes `tab-mark', `space-mark' or
915 `newline-mark'."
916 :type '(repeat
917 (list :tag "Character Mapping"
918 (choice :tag "Char Kind"
919 (const :tag "Tab" tab-mark)
920 (const :tag "Space" space-mark)
921 (const :tag "Newline" newline-mark))
922 (character :tag "Char")
923 (repeat :inline t :tag "Vector List"
924 (vector :tag ""
925 (repeat :inline t
926 :tag "Vector Characters"
927 (character :tag "Char"))))))
928 :group 'whitespace)
931 (defcustom whitespace-global-modes t
932 "*Modes for which global `whitespace-mode' is automagically turned on.
934 Global `whitespace-mode' is controlled by the command
935 `global-whitespace-mode'.
937 If nil, means no modes have `whitespace-mode' automatically
938 turned on.
940 If t, all modes that support `whitespace-mode' have it
941 automatically turned on.
943 Else it should be a list of `major-mode' symbol names for which
944 `whitespace-mode' should be automatically turned on. The sense
945 of the list is negated if it begins with `not'. For example:
947 (c-mode c++-mode)
949 means that `whitespace-mode' is turned on for buffers in C and
950 C++ modes only."
951 :type '(choice :tag "Global Modes"
952 (const :tag "None" nil)
953 (const :tag "All" t)
954 (set :menu-tag "Mode Specific" :tag "Modes"
955 :value (not)
956 (const :tag "Except" not)
957 (repeat :inline t
958 (symbol :tag "Mode"))))
959 :group 'whitespace)
962 (defcustom whitespace-action nil
963 "*Specify which action is taken when a buffer is visited, killed or written.
965 It's a list containing some or all of the following values:
967 nil no action is taken.
969 cleanup cleanup any bogus whitespace always when local
970 whitespace is turned on.
971 See `whitespace-cleanup' and
972 `whitespace-cleanup-region'.
974 report-on-bogus report if there is any bogus whitespace always
975 when local whitespace is turned on.
977 auto-cleanup cleanup any bogus whitespace when buffer is
978 written or killed.
979 See `whitespace-cleanup' and
980 `whitespace-cleanup-region'.
982 abort-on-bogus abort if there is any bogus whitespace and the
983 buffer is written or killed.
985 Any other value is treated as nil."
986 :type '(choice :tag "Actions"
987 (const :tag "None" nil)
988 (repeat :tag "Action List"
989 (choice :tag "Action"
990 (const :tag "Cleanup When On" cleanup)
991 (const :tag "Report On Bogus" report-on-bogus)
992 (const :tag "Auto Cleanup" auto-cleanup)
993 (const :tag "Abort On Bogus" abort-on-bogus))))
994 :group 'whitespace)
997 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
998 ;;;; User commands - Local mode
1001 ;;;###autoload
1002 (define-minor-mode whitespace-mode
1003 "Toggle whitespace minor mode visualization (\"ws\" on modeline).
1005 If ARG is null, toggle whitespace visualization.
1006 If ARG is a number greater than zero, turn on visualization;
1007 otherwise, turn off visualization.
1008 Only useful with a windowing system."
1009 :lighter " ws"
1010 :init-value nil
1011 :global nil
1012 :group 'whitespace
1013 (cond
1014 (noninteractive ; running a batch job
1015 (setq whitespace-mode nil))
1016 (whitespace-mode ; whitespace-mode on
1017 (whitespace-turn-on)
1018 (whitespace-action-when-on))
1019 (t ; whitespace-mode off
1020 (whitespace-turn-off))))
1023 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1024 ;;;; User commands - Global mode
1027 ;;;###autoload
1028 (define-minor-mode global-whitespace-mode
1029 "Toggle whitespace global minor mode visualization (\"WS\" on modeline).
1031 If ARG is null, toggle whitespace visualization.
1032 If ARG is a number greater than zero, turn on visualization;
1033 otherwise, turn off visualization.
1034 Only useful with a windowing system."
1035 :lighter " WS"
1036 :init-value nil
1037 :global t
1038 :group 'whitespace
1039 (cond
1040 (noninteractive ; running a batch job
1041 (setq global-whitespace-mode nil))
1042 (global-whitespace-mode ; global-whitespace-mode on
1043 (save-excursion
1044 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1045 (dolist (buffer (buffer-list)) ; adjust all local mode
1046 (set-buffer buffer)
1047 (unless whitespace-mode
1048 (whitespace-turn-on-if-enabled)))))
1049 (t ; global-whitespace-mode off
1050 (save-excursion
1051 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1052 (dolist (buffer (buffer-list)) ; adjust all local mode
1053 (set-buffer buffer)
1054 (unless whitespace-mode
1055 (whitespace-turn-off)))))))
1058 (defun whitespace-turn-on-if-enabled ()
1059 (when (cond
1060 ((eq whitespace-global-modes t))
1061 ((listp whitespace-global-modes)
1062 (if (eq (car-safe whitespace-global-modes) 'not)
1063 (not (memq major-mode (cdr whitespace-global-modes)))
1064 (memq major-mode whitespace-global-modes)))
1065 (t nil))
1066 (let (inhibit-quit)
1067 ;; Don't turn on whitespace mode if...
1069 ;; ...we don't have a display (we're running a batch job)
1070 noninteractive
1071 ;; ...or if the buffer is invisible (name starts with a space)
1072 (eq (aref (buffer-name) 0) ?\ )
1073 ;; ...or if the buffer is temporary (name starts with *)
1074 (and (eq (aref (buffer-name) 0) ?*)
1075 ;; except the scratch buffer.
1076 (not (string= (buffer-name) "*scratch*")))
1077 ;; Otherwise, turn on whitespace mode.
1078 (whitespace-turn-on)))))
1081 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1082 ;;;; User commands - Toggle
1085 (defconst whitespace-style-value-list
1086 '(tabs
1087 spaces
1088 trailing
1089 lines
1090 lines-tail
1091 newline
1092 empty
1093 indentation
1094 indentation::tab
1095 indentation::space
1096 space-after-tab
1097 space-after-tab::tab
1098 space-after-tab::space
1099 space-before-tab
1100 space-before-tab::tab
1101 space-before-tab::space
1102 help-newline ; value used by `whitespace-insert-option-mark'
1103 tab-mark
1104 space-mark
1105 newline-mark
1107 "List of valid `whitespace-style' values.")
1110 (defconst whitespace-toggle-option-alist
1111 '((?t . tabs)
1112 (?s . spaces)
1113 (?r . trailing)
1114 (?l . lines)
1115 (?L . lines-tail)
1116 (?n . newline)
1117 (?e . empty)
1118 (?\C-i . indentation)
1119 (?I . indentation::tab)
1120 (?i . indentation::space)
1121 (?\C-a . space-after-tab)
1122 (?A . space-after-tab::tab)
1123 (?a . space-after-tab::space)
1124 (?\C-b . space-before-tab)
1125 (?B . space-before-tab::tab)
1126 (?b . space-before-tab::space)
1127 (?T . tab-mark)
1128 (?S . space-mark)
1129 (?N . newline-mark)
1130 (?x . whitespace-style)
1132 "Alist of toggle options.
1134 Each element has the form:
1136 (CHAR . SYMBOL)
1138 Where:
1140 CHAR is a char which the user will have to type.
1142 SYMBOL is a valid symbol associated with CHAR.
1143 See `whitespace-style-value-list'.")
1146 (defvar whitespace-active-style nil
1147 "Used to save locally `whitespace-style' value.")
1149 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1150 "Used to save locally `indent-tabs-mode' value.")
1152 (defvar whitespace-tab-width tab-width
1153 "Used to save locally `tab-width' value.")
1156 ;;;###autoload
1157 (defun whitespace-toggle-options (arg)
1158 "Toggle local `whitespace-mode' options.
1160 If local whitespace-mode is off, toggle the option given by ARG
1161 and turn on local whitespace-mode.
1163 If local whitespace-mode is on, toggle the option given by ARG
1164 and restart local whitespace-mode.
1166 Interactively, it reads one of the following chars:
1168 CHAR MEANING
1169 (VIA FACES)
1170 t toggle TAB visualization
1171 s toggle SPACE and HARD SPACE visualization
1172 r toggle trailing blanks visualization
1173 l toggle \"long lines\" visualization
1174 L toggle \"long lines\" tail visualization
1175 n toggle NEWLINE visualization
1176 e toggle empty line at bob and/or eob visualization
1177 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1178 I toggle indentation SPACEs visualization
1179 i toggle indentation TABs visualization
1180 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1181 A toggle SPACEs after TAB: SPACEs visualization
1182 a toggle SPACEs after TAB: TABs visualization
1183 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1184 B toggle SPACEs before TAB: SPACEs visualization
1185 b toggle SPACEs before TAB: TABs visualization
1187 (VIA DISPLAY TABLE)
1188 T toggle TAB visualization
1189 S toggle SPACEs before TAB visualization
1190 N toggle NEWLINE visualization
1192 x restore `whitespace-style' value
1193 ? display brief help
1195 Non-interactively, ARG should be a symbol or a list of symbols.
1196 The valid symbols are:
1198 tabs toggle TAB visualization
1199 spaces toggle SPACE and HARD SPACE visualization
1200 trailing toggle trailing blanks visualization
1201 lines toggle \"long lines\" visualization
1202 lines-tail toggle \"long lines\" tail visualization
1203 newline toggle NEWLINE visualization
1204 empty toggle empty line at bob and/or eob visualization
1205 indentation toggle indentation SPACEs visualization
1206 indentation::tab toggle indentation SPACEs visualization
1207 indentation::space toggle indentation TABs visualization
1208 space-after-tab toggle SPACEs after TAB visualization
1209 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1210 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1211 space-before-tab toggle SPACEs before TAB visualization
1212 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1213 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1215 tab-mark toggle TAB visualization
1216 space-mark toggle SPACEs before TAB visualization
1217 newline-mark toggle NEWLINE visualization
1219 whitespace-style restore `whitespace-style' value
1221 Only useful with a windowing system.
1223 See `whitespace-style' and `indent-tabs-mode' for documentation."
1224 (interactive (whitespace-interactive-char t))
1225 (let ((whitespace-style
1226 (whitespace-toggle-list t arg whitespace-active-style)))
1227 (whitespace-mode 0)
1228 (whitespace-mode 1)))
1231 (defvar whitespace-toggle-style nil
1232 "Used to toggle the global `whitespace-style' value.")
1235 ;;;###autoload
1236 (defun global-whitespace-toggle-options (arg)
1237 "Toggle global `whitespace-mode' options.
1239 If global whitespace-mode is off, toggle the option given by ARG
1240 and turn on global whitespace-mode.
1242 If global whitespace-mode is on, toggle the option given by ARG
1243 and restart global whitespace-mode.
1245 Interactively, it accepts one of the following chars:
1247 CHAR MEANING
1248 (VIA FACES)
1249 t toggle TAB visualization
1250 s toggle SPACE and HARD SPACE visualization
1251 r toggle trailing blanks visualization
1252 l toggle \"long lines\" visualization
1253 L toggle \"long lines\" tail visualization
1254 n toggle NEWLINE visualization
1255 e toggle empty line at bob and/or eob visualization
1256 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1257 I toggle indentation SPACEs visualization
1258 i toggle indentation TABs visualization
1259 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1260 A toggle SPACEs after TAB: SPACEs visualization
1261 a toggle SPACEs after TAB: TABs visualization
1262 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1263 B toggle SPACEs before TAB: SPACEs visualization
1264 b toggle SPACEs before TAB: TABs visualization
1266 (VIA DISPLAY TABLE)
1267 T toggle TAB visualization
1268 S toggle SPACEs before TAB visualization
1269 N toggle NEWLINE visualization
1271 x restore `whitespace-style' value
1272 ? display brief help
1274 Non-interactively, ARG should be a symbol or a list of symbols.
1275 The valid symbols are:
1277 tabs toggle TAB visualization
1278 spaces toggle SPACE and HARD SPACE visualization
1279 trailing toggle trailing blanks visualization
1280 lines toggle \"long lines\" visualization
1281 lines-tail toggle \"long lines\" tail visualization
1282 newline toggle NEWLINE visualization
1283 empty toggle empty line at bob and/or eob visualization
1284 indentation toggle indentation SPACEs visualization
1285 indentation::tab toggle indentation SPACEs visualization
1286 indentation::space toggle indentation TABs visualization
1287 space-after-tab toggle SPACEs after TAB visualization
1288 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1289 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1290 space-before-tab toggle SPACEs before TAB visualization
1291 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1292 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1294 tab-mark toggle TAB visualization
1295 space-mark toggle SPACEs before TAB visualization
1296 newline-mark toggle NEWLINE visualization
1298 whitespace-style restore `whitespace-style' value
1300 Only useful with a windowing system.
1302 See `whitespace-style' and `indent-tabs-mode' for documentation."
1303 (interactive (whitespace-interactive-char nil))
1304 (let ((whitespace-style
1305 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1306 (setq whitespace-toggle-style whitespace-style)
1307 (global-whitespace-mode 0)
1308 (global-whitespace-mode 1)))
1311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1312 ;;;; User commands - Cleanup
1315 ;;;###autoload
1316 (defun whitespace-cleanup ()
1317 "Cleanup some blank problems in all buffer or at region.
1319 It usually applies to the whole buffer, but in transient mark
1320 mode when the mark is active, it applies to the region. It also
1321 applies to the region when it is not in transiente mark mode, the
1322 mark is active and \\[universal-argument] was pressed just before
1323 calling `whitespace-cleanup' interactively.
1325 See also `whitespace-cleanup-region'.
1327 The problems cleaned up are:
1329 1. empty lines at beginning of buffer.
1330 2. empty lines at end of buffer.
1331 If `whitespace-style' includes the value `empty', remove all
1332 empty lines at beginning and/or end of buffer.
1334 3. 8 or more SPACEs at beginning of line.
1335 If `whitespace-style' includes the value `indentation':
1336 replace 8 or more SPACEs at beginning of line by TABs, if
1337 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1338 SPACEs.
1339 If `whitespace-style' includes the value `indentation::tab',
1340 replace 8 or more SPACEs at beginning of line by TABs.
1341 If `whitespace-style' includes the value `indentation::space',
1342 replace TABs by SPACEs.
1344 4. SPACEs before TAB.
1345 If `whitespace-style' includes the value `space-before-tab':
1346 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1347 otherwise, replace TABs by SPACEs.
1348 If `whitespace-style' includes the value
1349 `space-before-tab::tab', replace SPACEs by TABs.
1350 If `whitespace-style' includes the value
1351 `space-before-tab::space', replace TABs by SPACEs.
1353 5. SPACEs or TABs at end of line.
1354 If `whitespace-style' includes the value `trailing', remove
1355 all SPACEs or TABs at end of line.
1357 6. 8 or more SPACEs after TAB.
1358 If `whitespace-style' includes the value `space-after-tab':
1359 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1360 otherwise, replace TABs by SPACEs.
1361 If `whitespace-style' includes the value
1362 `space-after-tab::tab', replace SPACEs by TABs.
1363 If `whitespace-style' includes the value
1364 `space-after-tab::space', replace TABs by SPACEs.
1366 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1367 documentation."
1368 (interactive "@*")
1369 (if (and (or transient-mark-mode
1370 current-prefix-arg)
1371 mark-active)
1372 ;; region active
1373 ;; PROBLEMs 1 and 2 are not handled in region
1374 ;; PROBLEM 3: 8 or more SPACEs at bol
1375 ;; PROBLEM 4: SPACEs before TAB
1376 ;; PROBLEM 5: SPACEs or TABs at eol
1377 ;; PROBLEM 6: 8 or more SPACEs after TAB
1378 (whitespace-cleanup-region (region-beginning) (region-end))
1379 ;; whole buffer
1380 (save-excursion
1381 (save-match-data
1382 ;; PROBLEM 1: empty lines at bob
1383 ;; PROBLEM 2: empty lines at eob
1384 ;; ACTION: remove all empty lines at bob and/or eob
1385 (when (memq 'empty whitespace-style)
1386 (let (overwrite-mode) ; enforce no overwrite
1387 (goto-char (point-min))
1388 (when (re-search-forward
1389 whitespace-empty-at-bob-regexp nil t)
1390 (delete-region (match-beginning 1) (match-end 1)))
1391 (when (re-search-forward
1392 whitespace-empty-at-eob-regexp nil t)
1393 (delete-region (match-beginning 1) (match-end 1)))))))
1394 ;; PROBLEM 3: 8 or more SPACEs at bol
1395 ;; PROBLEM 4: SPACEs before TAB
1396 ;; PROBLEM 5: SPACEs or TABs at eol
1397 ;; PROBLEM 6: 8 or more SPACEs after TAB
1398 (whitespace-cleanup-region (point-min) (point-max))))
1401 ;;;###autoload
1402 (defun whitespace-cleanup-region (start end)
1403 "Cleanup some blank problems at region.
1405 The problems cleaned up are:
1407 1. 8 or more SPACEs at beginning of line.
1408 If `whitespace-style' includes the value `indentation':
1409 replace 8 or more SPACEs at beginning of line by TABs, if
1410 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1411 SPACEs.
1412 If `whitespace-style' includes the value `indentation::tab',
1413 replace 8 or more SPACEs at beginning of line by TABs.
1414 If `whitespace-style' includes the value `indentation::space',
1415 replace TABs by SPACEs.
1417 2. SPACEs before TAB.
1418 If `whitespace-style' includes the value `space-before-tab':
1419 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1420 otherwise, replace TABs by SPACEs.
1421 If `whitespace-style' includes the value
1422 `space-before-tab::tab', replace SPACEs by TABs.
1423 If `whitespace-style' includes the value
1424 `space-before-tab::space', replace TABs by SPACEs.
1426 3. SPACEs or TABs at end of line.
1427 If `whitespace-style' includes the value `trailing', remove
1428 all SPACEs or TABs at end of line.
1430 4. 8 or more SPACEs after TAB.
1431 If `whitespace-style' includes the value `space-after-tab':
1432 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1433 otherwise, replace TABs by SPACEs.
1434 If `whitespace-style' includes the value
1435 `space-after-tab::tab', replace SPACEs by TABs.
1436 If `whitespace-style' includes the value
1437 `space-after-tab::space', replace TABs by SPACEs.
1439 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1440 documentation."
1441 (interactive "@*r")
1442 (let ((rstart (min start end))
1443 (rend (copy-marker (max start end)))
1444 (indent-tabs-mode whitespace-indent-tabs-mode)
1445 (tab-width whitespace-tab-width)
1446 overwrite-mode ; enforce no overwrite
1447 tmp)
1448 (save-excursion
1449 (save-match-data
1450 ;; PROBLEM 1: 8 or more SPACEs at bol
1451 (cond
1452 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1453 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1454 ;; SPACEs.
1455 ((memq 'indentation whitespace-style)
1456 (let ((regexp (whitespace-indentation-regexp)))
1457 (goto-char rstart)
1458 (while (re-search-forward regexp rend t)
1459 (setq tmp (current-indentation))
1460 (goto-char (match-beginning 0))
1461 (delete-horizontal-space)
1462 (unless (eolp)
1463 (indent-to tmp)))))
1464 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1465 ((memq 'indentation::tab whitespace-style)
1466 (whitespace-replace-action
1467 'tabify rstart rend
1468 (whitespace-indentation-regexp 'tab) 0))
1469 ;; ACTION: replace TABs by SPACEs.
1470 ((memq 'indentation::space whitespace-style)
1471 (whitespace-replace-action
1472 'untabify rstart rend
1473 (whitespace-indentation-regexp 'space) 0)))
1474 ;; PROBLEM 3: SPACEs or TABs at eol
1475 ;; ACTION: remove all SPACEs or TABs at eol
1476 (when (memq 'trailing whitespace-style)
1477 (whitespace-replace-action
1478 'delete-region rstart rend
1479 (whitespace-trailing-regexp) 1))
1480 ;; PROBLEM 4: 8 or more SPACEs after TAB
1481 (cond
1482 ;; ACTION: replace 8 or more SPACEs by TABs, if
1483 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1484 ;; SPACEs.
1485 ((memq 'space-after-tab whitespace-style)
1486 (whitespace-replace-action
1487 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1488 rstart rend (whitespace-space-after-tab-regexp) 1))
1489 ;; ACTION: replace 8 or more SPACEs by TABs.
1490 ((memq 'space-after-tab::tab whitespace-style)
1491 (whitespace-replace-action
1492 'tabify rstart rend
1493 (whitespace-space-after-tab-regexp 'tab) 1))
1494 ;; ACTION: replace TABs by SPACEs.
1495 ((memq 'space-after-tab::space whitespace-style)
1496 (whitespace-replace-action
1497 'untabify rstart rend
1498 (whitespace-space-after-tab-regexp 'space) 1)))
1499 ;; PROBLEM 2: SPACEs before TAB
1500 (cond
1501 ;; ACTION: replace SPACEs before TAB by TABs, if
1502 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1503 ;; SPACEs.
1504 ((memq 'space-before-tab whitespace-style)
1505 (whitespace-replace-action
1506 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1507 rstart rend whitespace-space-before-tab-regexp
1508 (if whitespace-indent-tabs-mode 1 2)))
1509 ;; ACTION: replace SPACEs before TAB by TABs.
1510 ((memq 'space-before-tab::tab whitespace-style)
1511 (whitespace-replace-action
1512 'tabify rstart rend
1513 whitespace-space-before-tab-regexp 1))
1514 ;; ACTION: replace TABs by SPACEs.
1515 ((memq 'space-before-tab::space whitespace-style)
1516 (whitespace-replace-action
1517 'untabify rstart rend
1518 whitespace-space-before-tab-regexp 2)))))
1519 (set-marker rend nil))) ; point marker to nowhere
1522 (defun whitespace-replace-action (action rstart rend regexp index)
1523 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1525 INDEX is the level group matched by REGEXP and used by ACTION.
1527 See also `tab-width'."
1528 (goto-char rstart)
1529 (while (re-search-forward regexp rend t)
1530 (goto-char (match-end index))
1531 (funcall action (match-beginning index) (match-end index))))
1534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1535 ;;;; User command - report
1538 (defun whitespace-trailing-regexp ()
1539 "Make the `whitespace-trailing-regexp' regexp."
1540 (concat "\\(\\(" whitespace-trailing-regexp "\\)+\\)$"))
1543 (defun whitespace-regexp (regexp &optional kind)
1544 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1545 (cond
1546 ((or (eq kind 'tab)
1547 whitespace-indent-tabs-mode)
1548 (format (car regexp) whitespace-tab-width))
1549 ((or (eq kind 'space)
1550 (not whitespace-indent-tabs-mode))
1551 (cdr regexp))))
1554 (defun whitespace-indentation-regexp (&optional kind)
1555 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1556 (whitespace-regexp whitespace-indentation-regexp kind))
1559 (defun whitespace-space-after-tab-regexp (&optional kind)
1560 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1561 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1564 (defconst whitespace-report-list
1565 (list
1566 (cons 'empty whitespace-empty-at-bob-regexp)
1567 (cons 'empty whitespace-empty-at-eob-regexp)
1568 (cons 'trailing (whitespace-trailing-regexp))
1569 (cons 'indentation nil)
1570 (cons 'indentation::tab nil)
1571 (cons 'indentation::space nil)
1572 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1573 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1574 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1575 (cons 'space-after-tab nil)
1576 (cons 'space-after-tab::tab nil)
1577 (cons 'space-after-tab::space nil)
1579 "List of whitespace bogus symbol and corresponding regexp.")
1582 (defconst whitespace-report-text
1583 '( ;; `indent-tabs-mode' has non-nil value
1585 Whitespace Report
1587 Current Setting Whitespace Problem
1589 empty [] [] empty lines at beginning of buffer
1590 empty [] [] empty lines at end of buffer
1591 trailing [] [] SPACEs or TABs at end of line
1592 indentation [] [] 8 or more SPACEs at beginning of line
1593 indentation::tab [] [] 8 or more SPACEs at beginning of line
1594 indentation::space [] [] TABs at beginning of line
1595 space-before-tab [] [] SPACEs before TAB
1596 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1597 space-before-tab::space [] [] SPACEs before TAB: TABs
1598 space-after-tab [] [] 8 or more SPACEs after TAB
1599 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1600 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1602 indent-tabs-mode =
1603 tab-width = \n\n"
1604 . ;; `indent-tabs-mode' has nil value
1606 Whitespace Report
1608 Current Setting Whitespace Problem
1610 empty [] [] empty lines at beginning of buffer
1611 empty [] [] empty lines at end of buffer
1612 trailing [] [] SPACEs or TABs at end of line
1613 indentation [] [] TABs at beginning of line
1614 indentation::tab [] [] 8 or more SPACEs at beginning of line
1615 indentation::space [] [] TABs at beginning of line
1616 space-before-tab [] [] SPACEs before TAB
1617 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1618 space-before-tab::space [] [] SPACEs before TAB: TABs
1619 space-after-tab [] [] 8 or more SPACEs after TAB
1620 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1621 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1623 indent-tabs-mode =
1624 tab-width = \n\n")
1625 "Text for whitespace bogus report.
1627 It is a cons of strings, where the car part is used when
1628 `indent-tabs-mode' is non-nil, and the cdr part is used when
1629 `indent-tabs-mode' is nil.")
1632 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1633 "The buffer name for whitespace bogus report.")
1636 ;;;###autoload
1637 (defun whitespace-report (&optional force report-if-bogus)
1638 "Report some whitespace problems in buffer.
1640 Return nil if there is no whitespace problem; otherwise, return
1641 non-nil.
1643 If FORCE is non-nil or \\[universal-argument] was pressed just
1644 before calling `whitespace-report' interactively, it forces
1645 `whitespace-style' to have:
1647 empty
1648 trailing
1649 indentation
1650 space-before-tab
1651 space-after-tab
1653 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1654 whitespace problems in buffer.
1656 Report if some of the following whitespace problems exist:
1658 * If `indent-tabs-mode' is non-nil:
1659 empty 1. empty lines at beginning of buffer.
1660 empty 2. empty lines at end of buffer.
1661 trailing 3. SPACEs or TABs at end of line.
1662 indentation 4. 8 or more SPACEs at beginning of line.
1663 space-before-tab 5. SPACEs before TAB.
1664 space-after-tab 6. 8 or more SPACEs after TAB.
1666 * If `indent-tabs-mode' is nil:
1667 empty 1. empty lines at beginning of buffer.
1668 empty 2. empty lines at end of buffer.
1669 trailing 3. SPACEs or TABs at end of line.
1670 indentation 4. TABS at beginning of line.
1671 space-before-tab 5. SPACEs before TAB.
1672 space-after-tab 6. 8 or more SPACEs after TAB.
1674 See `whitespace-style' for documentation.
1675 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1676 cleaning up these problems."
1677 (interactive (list current-prefix-arg))
1678 (whitespace-report-region (point-min) (point-max)
1679 force report-if-bogus))
1682 ;;;###autoload
1683 (defun whitespace-report-region (start end &optional force report-if-bogus)
1684 "Report some whitespace problems in a region.
1686 Return nil if there is no whitespace problem; otherwise, return
1687 non-nil.
1689 If FORCE is non-nil or \\[universal-argument] was pressed just
1690 before calling `whitespace-report-region' interactively, it
1691 forces `whitespace-style' to have:
1693 empty
1694 indentation
1695 space-before-tab
1696 trailing
1697 space-after-tab
1699 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1700 whitespace problems in buffer.
1702 Report if some of the following whitespace problems exist:
1704 * If `indent-tabs-mode' is non-nil:
1705 empty 1. empty lines at beginning of buffer.
1706 empty 2. empty lines at end of buffer.
1707 trailing 3. SPACEs or TABs at end of line.
1708 indentation 4. 8 or more SPACEs at beginning of line.
1709 space-before-tab 5. SPACEs before TAB.
1710 space-after-tab 6. 8 or more SPACEs after TAB.
1712 * If `indent-tabs-mode' is nil:
1713 empty 1. empty lines at beginning of buffer.
1714 empty 2. empty lines at end of buffer.
1715 trailing 3. SPACEs or TABs at end of line.
1716 indentation 4. TABS at beginning of line.
1717 space-before-tab 5. SPACEs before TAB.
1718 space-after-tab 6. 8 or more SPACEs after TAB.
1720 See `whitespace-style' for documentation.
1721 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1722 cleaning up these problems."
1723 (interactive "r")
1724 (setq force (or current-prefix-arg force))
1725 (save-excursion
1726 (save-match-data
1727 (let* ((has-bogus nil)
1728 (rstart (min start end))
1729 (rend (max start end))
1730 (bogus-list
1731 (mapcar
1732 #'(lambda (option)
1733 (when force
1734 (add-to-list 'whitespace-style (car option)))
1735 (goto-char rstart)
1736 (let ((regexp
1737 (cond
1738 ((eq (car option) 'indentation)
1739 (whitespace-indentation-regexp))
1740 ((eq (car option) 'indentation::tab)
1741 (whitespace-indentation-regexp 'tab))
1742 ((eq (car option) 'indentation::space)
1743 (whitespace-indentation-regexp 'space))
1744 ((eq (car option) 'space-after-tab)
1745 (whitespace-space-after-tab-regexp))
1746 ((eq (car option) 'space-after-tab::tab)
1747 (whitespace-space-after-tab-regexp 'tab))
1748 ((eq (car option) 'space-after-tab::space)
1749 (whitespace-space-after-tab-regexp 'space))
1751 (cdr option)))))
1752 (and (re-search-forward regexp rend t)
1753 (setq has-bogus t))))
1754 whitespace-report-list)))
1755 (when (if report-if-bogus has-bogus t)
1756 (whitespace-kill-buffer whitespace-report-buffer-name)
1757 ;; `whitespace-indent-tabs-mode' is local to current buffer
1758 ;; `whitespace-tab-width' is local to current buffer
1759 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1760 (ws-tab-width whitespace-tab-width))
1761 (with-current-buffer (get-buffer-create
1762 whitespace-report-buffer-name)
1763 (erase-buffer)
1764 (insert (if ws-indent-tabs-mode
1765 (car whitespace-report-text)
1766 (cdr whitespace-report-text)))
1767 (goto-char (point-min))
1768 (forward-line 3)
1769 (dolist (option whitespace-report-list)
1770 (forward-line 1)
1771 (whitespace-mark-x
1772 27 (memq (car option) whitespace-style))
1773 (whitespace-mark-x 7 (car bogus-list))
1774 (setq bogus-list (cdr bogus-list)))
1775 (forward-line 1)
1776 (whitespace-insert-value ws-indent-tabs-mode)
1777 (whitespace-insert-value ws-tab-width)
1778 (when has-bogus
1779 (goto-char (point-max))
1780 (insert " Type `M-x whitespace-cleanup'"
1781 " to cleanup the buffer.\n\n"
1782 " Type `M-x whitespace-cleanup-region'"
1783 " to cleanup a region.\n\n"))
1784 (whitespace-display-window (current-buffer)))))
1785 has-bogus))))
1788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1789 ;;;; Internal functions
1792 (defvar whitespace-font-lock-mode nil
1793 "Used to remember whether a buffer had font lock mode on or not.")
1795 (defvar whitespace-font-lock nil
1796 "Used to remember whether a buffer initially had font lock on or not.")
1798 (defvar whitespace-font-lock-keywords nil
1799 "Used to save locally `font-lock-keywords' value.")
1802 (defconst whitespace-help-text
1804 Whitespace Toggle Options
1806 FACES
1807 [] t - toggle TAB visualization
1808 [] s - toggle SPACE and HARD SPACE visualization
1809 [] r - toggle trailing blanks visualization
1810 [] l - toggle \"long lines\" visualization
1811 [] L - toggle \"long lines\" tail visualization
1812 [] n - toggle NEWLINE visualization
1813 [] e - toggle empty line at bob and/or eob visualization
1814 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1815 [] I - toggle indentation SPACEs visualization
1816 [] i - toggle indentation TABs visualization
1817 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1818 [] A - toggle SPACEs after TAB: SPACEs visualization
1819 [] a - toggle SPACEs after TAB: TABs visualization
1820 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1821 [] B - toggle SPACEs before TAB: SPACEs visualization
1822 [] b - toggle SPACEs before TAB: TABs visualization
1824 DISPLAY TABLE
1825 [] T - toggle TAB visualization
1826 [] S - toggle SPACE and HARD SPACE visualization
1827 [] N - toggle NEWLINE visualization
1829 x - restore `whitespace-style' value
1831 ? - display this text\n\n"
1832 "Text for whitespace toggle options.")
1835 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1836 "The buffer name for whitespace toggle options.")
1839 (defun whitespace-insert-value (value)
1840 "Insert VALUE at column 20 of next line."
1841 (forward-line 1)
1842 (move-to-column 20 t)
1843 (insert (format "%s" value)))
1846 (defun whitespace-mark-x (nchars condition)
1847 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1848 (forward-char nchars)
1849 (insert (if condition "X" " ")))
1852 (defun whitespace-insert-option-mark (the-list the-value)
1853 "Insert the option mark ('X' or ' ') in toggle options buffer."
1854 (goto-char (point-min))
1855 (forward-line 2)
1856 (dolist (sym the-list)
1857 (if (eq sym 'help-newline)
1858 (forward-line 2)
1859 (forward-line 1)
1860 (whitespace-mark-x 2 (memq sym the-value)))))
1863 (defun whitespace-help-on (style)
1864 "Display the whitespace toggle options."
1865 (unless (get-buffer whitespace-help-buffer-name)
1866 (delete-other-windows)
1867 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
1868 (save-excursion
1869 (set-buffer buffer)
1870 (erase-buffer)
1871 (insert whitespace-help-text)
1872 (whitespace-insert-option-mark
1873 whitespace-style-value-list style)
1874 (whitespace-display-window buffer)))))
1877 (defun whitespace-display-window (buffer)
1878 "Display BUFFER in a new window."
1879 (goto-char (point-min))
1880 (set-buffer-modified-p nil)
1881 (let ((size (- (window-height)
1882 (max window-min-height
1883 (1+ (count-lines (point-min)
1884 (point-max)))))))
1885 (when (<= size 0)
1886 (kill-buffer buffer)
1887 (error "Frame height is too small; \
1888 can't split window to display whitespace toggle options"))
1889 (set-window-buffer (split-window nil size) buffer)))
1892 (defun whitespace-kill-buffer (buffer-name)
1893 "Kill buffer BUFFER-NAME and windows related with it."
1894 (let ((buffer (get-buffer buffer-name)))
1895 (when buffer
1896 (delete-windows-on buffer)
1897 (kill-buffer buffer))))
1900 (defun whitespace-help-off ()
1901 "Remove the buffer and window of the whitespace toggle options."
1902 (whitespace-kill-buffer whitespace-help-buffer-name))
1905 (defun whitespace-interactive-char (local-p)
1906 "Interactive function to read a char and return a symbol.
1908 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1909 uses a global context.
1911 It accepts one of the following chars:
1913 CHAR MEANING
1914 (VIA FACES)
1915 t toggle TAB visualization
1916 s toggle SPACE and HARD SPACE visualization
1917 r toggle trailing blanks visualization
1918 l toggle \"long lines\" visualization
1919 L toggle \"long lines\" tail visualization
1920 n toggle NEWLINE visualization
1921 e toggle empty line at bob and/or eob visualization
1922 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1923 I toggle indentation SPACEs visualization
1924 i toggle indentation TABs visualization
1925 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1926 A toggle SPACEs after TAB: SPACEs visualization
1927 a toggle SPACEs after TAB: TABs visualization
1928 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1929 B toggle SPACEs before TAB: SPACEs visualization
1930 b toggle SPACEs before TAB: TABs visualization
1932 (VIA DISPLAY TABLE)
1933 T toggle TAB visualization
1934 S toggle SPACE and HARD SPACE visualization
1935 N toggle NEWLINE visualization
1937 x restore `whitespace-style' value
1938 ? display brief help
1940 See also `whitespace-toggle-option-alist'."
1941 (let* ((is-off (not (if local-p
1942 whitespace-mode
1943 global-whitespace-mode)))
1944 (style (cond (is-off whitespace-style) ; use default value
1945 (local-p whitespace-active-style)
1946 (t whitespace-toggle-style)))
1947 (prompt
1948 (format "Whitespace Toggle %s (type ? for further options)-"
1949 (if local-p "Local" "Global")))
1950 ch sym)
1951 ;; read a valid option and get the corresponding symbol
1952 (save-window-excursion
1953 (condition-case data
1954 (progn
1955 (while
1956 ;; while condition
1957 (progn
1958 (setq ch (read-char prompt))
1959 (not
1960 (setq sym
1961 (cdr
1962 (assq ch whitespace-toggle-option-alist)))))
1963 ;; while body
1964 (if (eq ch ?\?)
1965 (whitespace-help-on style)
1966 (ding)))
1967 (whitespace-help-off)
1968 (message " ")) ; clean echo area
1969 ;; handler
1970 ((quit error)
1971 (whitespace-help-off)
1972 (error (error-message-string data)))))
1973 (list sym))) ; return the apropriate symbol
1976 (defun whitespace-toggle-list (local-p arg the-list)
1977 "Toggle options in THE-LIST based on list ARG.
1979 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1980 uses a global context.
1982 ARG is a list of options to be toggled.
1984 THE-LIST is a list of options. This list will be toggled and the
1985 resultant list will be returned."
1986 (unless (if local-p whitespace-mode global-whitespace-mode)
1987 (setq the-list whitespace-style))
1988 (setq the-list (copy-sequence the-list)) ; keep original list
1989 (dolist (sym (if (listp arg) arg (list arg)))
1990 (cond
1991 ;; ignore help value
1992 ((eq sym 'help-newline))
1993 ;; restore default values
1994 ((eq sym 'whitespace-style)
1995 (setq the-list whitespace-style))
1996 ;; toggle valid values
1997 ((memq sym whitespace-style-value-list)
1998 (setq the-list (if (memq sym the-list)
1999 (delq sym the-list)
2000 (cons sym the-list))))))
2001 the-list)
2004 (defvar whitespace-display-table nil
2005 "Used to save a local display table.")
2007 (defvar whitespace-display-table-was-local nil
2008 "Used to remember whether a buffer initially had a local display table.")
2011 (defun whitespace-turn-on ()
2012 "Turn on whitespace visualization."
2013 ;; prepare local hooks
2014 (whitespace-add-local-hook)
2015 ;; create whitespace local buffer environment
2016 (set (make-local-variable 'whitespace-font-lock-mode) nil)
2017 (set (make-local-variable 'whitespace-font-lock) nil)
2018 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2019 (set (make-local-variable 'whitespace-display-table) nil)
2020 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2021 (set (make-local-variable 'whitespace-active-style)
2022 (if (listp whitespace-style)
2023 whitespace-style
2024 (list whitespace-style)))
2025 (set (make-local-variable 'whitespace-indent-tabs-mode)
2026 indent-tabs-mode)
2027 (set (make-local-variable 'whitespace-tab-width)
2028 tab-width)
2029 ;; turn on whitespace
2030 (when whitespace-active-style
2031 (whitespace-color-on)
2032 (whitespace-display-char-on)))
2035 (defun whitespace-turn-off ()
2036 "Turn off whitespace visualization."
2037 (whitespace-remove-local-hook)
2038 (when whitespace-active-style
2039 (whitespace-color-off)
2040 (whitespace-display-char-off)))
2043 (defun whitespace-style-face-p ()
2044 "Return t if there is some visualization via face."
2045 (or (memq 'tabs whitespace-active-style)
2046 (memq 'spaces whitespace-active-style)
2047 (memq 'trailing whitespace-active-style)
2048 (memq 'lines whitespace-active-style)
2049 (memq 'lines-tail whitespace-active-style)
2050 (memq 'newline whitespace-active-style)
2051 (memq 'empty whitespace-active-style)
2052 (memq 'indentation whitespace-active-style)
2053 (memq 'indentation::tab whitespace-active-style)
2054 (memq 'indentation::space whitespace-active-style)
2055 (memq 'space-after-tab whitespace-active-style)
2056 (memq 'space-after-tab::tab whitespace-active-style)
2057 (memq 'space-after-tab::space whitespace-active-style)
2058 (memq 'space-before-tab whitespace-active-style)
2059 (memq 'space-before-tab::tab whitespace-active-style)
2060 (memq 'space-before-tab::space whitespace-active-style)))
2063 (defun whitespace-color-on ()
2064 "Turn on color visualization."
2065 (when (whitespace-style-face-p)
2066 (unless whitespace-font-lock
2067 (setq whitespace-font-lock t
2068 whitespace-font-lock-keywords
2069 (copy-sequence font-lock-keywords)))
2070 ;; turn off font lock
2071 (set (make-local-variable 'whitespace-font-lock-mode)
2072 font-lock-mode)
2073 (font-lock-mode 0)
2074 ;; add whitespace-mode color into font lock
2075 (when (memq 'spaces whitespace-active-style)
2076 (font-lock-add-keywords
2078 (list
2079 ;; Show SPACEs
2080 (list whitespace-space-regexp 1 whitespace-space t)
2081 ;; Show HARD SPACEs
2082 (list whitespace-hspace-regexp 1 whitespace-hspace t))
2084 (when (memq 'tabs whitespace-active-style)
2085 (font-lock-add-keywords
2087 (list
2088 ;; Show TABs
2089 (list whitespace-tab-regexp 1 whitespace-tab t))
2091 (when (memq 'trailing whitespace-active-style)
2092 (font-lock-add-keywords
2094 (list
2095 ;; Show trailing blanks
2096 (list (whitespace-trailing-regexp) 1 whitespace-trailing t))
2098 (when (or (memq 'lines whitespace-active-style)
2099 (memq 'lines-tail whitespace-active-style))
2100 (font-lock-add-keywords
2102 (list
2103 ;; Show "long" lines
2104 (list
2105 (format
2106 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2107 whitespace-tab-width (1- whitespace-tab-width)
2108 (/ whitespace-line-column tab-width)
2109 (let ((rem (% whitespace-line-column whitespace-tab-width)))
2110 (if (zerop rem)
2112 (format ".\\{%d\\}" rem))))
2113 (if (memq 'lines whitespace-active-style)
2114 0 ; whole line
2115 2) ; line tail
2116 whitespace-line t))
2118 (cond
2119 ((memq 'space-before-tab whitespace-active-style)
2120 (font-lock-add-keywords
2122 (list
2123 ;; Show SPACEs before TAB (indent-tabs-mode)
2124 (list whitespace-space-before-tab-regexp
2125 (if whitespace-indent-tabs-mode 1 2)
2126 whitespace-space-before-tab t))
2128 ((memq 'space-before-tab::tab whitespace-active-style)
2129 (font-lock-add-keywords
2131 (list
2132 ;; Show SPACEs before TAB (SPACEs)
2133 (list whitespace-space-before-tab-regexp
2134 1 whitespace-space-before-tab t))
2136 ((memq 'space-before-tab::space whitespace-active-style)
2137 (font-lock-add-keywords
2139 (list
2140 ;; Show SPACEs before TAB (TABs)
2141 (list whitespace-space-before-tab-regexp
2142 2 whitespace-space-before-tab t))
2143 t)))
2144 (cond
2145 ((memq 'indentation whitespace-active-style)
2146 (font-lock-add-keywords
2148 (list
2149 ;; Show indentation SPACEs (indent-tabs-mode)
2150 (list (whitespace-indentation-regexp)
2151 1 whitespace-indentation t))
2153 ((memq 'indentation::tab whitespace-active-style)
2154 (font-lock-add-keywords
2156 (list
2157 ;; Show indentation SPACEs (SPACEs)
2158 (list (whitespace-indentation-regexp 'tab)
2159 1 whitespace-indentation t))
2161 ((memq 'indentation::space whitespace-active-style)
2162 (font-lock-add-keywords
2164 (list
2165 ;; Show indentation SPACEs (TABs)
2166 (list (whitespace-indentation-regexp 'space)
2167 1 whitespace-indentation t))
2168 t)))
2169 (when (memq 'empty whitespace-active-style)
2170 (font-lock-add-keywords
2172 (list
2173 ;; Show empty lines at beginning of buffer
2174 (list whitespace-empty-at-bob-regexp
2175 1 whitespace-empty t))
2177 (font-lock-add-keywords
2179 (list
2180 ;; Show empty lines at end of buffer
2181 (list whitespace-empty-at-eob-regexp
2182 1 whitespace-empty t))
2184 (cond
2185 ((memq 'space-after-tab whitespace-active-style)
2186 (font-lock-add-keywords
2188 (list
2189 ;; Show SPACEs after TAB (indent-tabs-mode)
2190 (list (whitespace-space-after-tab-regexp)
2191 1 whitespace-space-after-tab t))
2193 ((memq 'space-after-tab::tab whitespace-active-style)
2194 (font-lock-add-keywords
2196 (list
2197 ;; Show SPACEs after TAB (SPACEs)
2198 (list (whitespace-space-after-tab-regexp 'tab)
2199 1 whitespace-space-after-tab t))
2201 ((memq 'space-after-tab::space whitespace-active-style)
2202 (font-lock-add-keywords
2204 (list
2205 ;; Show SPACEs after TAB (TABs)
2206 (list (whitespace-space-after-tab-regexp 'space)
2207 1 whitespace-space-after-tab t))
2208 t)))
2209 ;; now turn on font lock and highlight blanks
2210 (font-lock-mode 1)))
2213 (defun whitespace-color-off ()
2214 "Turn off color visualization."
2215 ;; turn off font lock
2216 (when (whitespace-style-face-p)
2217 (font-lock-mode 0)
2218 (when whitespace-font-lock
2219 (setq whitespace-font-lock nil
2220 font-lock-keywords whitespace-font-lock-keywords))
2221 ;; restore original font lock state
2222 (font-lock-mode whitespace-font-lock-mode)))
2225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2226 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2229 (defun whitespace-style-mark-p ()
2230 "Return t if there is some visualization via display table."
2231 (or (memq 'tab-mark whitespace-active-style)
2232 (memq 'space-mark whitespace-active-style)
2233 (memq 'newline-mark whitespace-active-style)))
2236 (defsubst whitespace-char-valid-p (char)
2237 ;; This check should be improved!!!
2238 (or (< char 256)
2239 (characterp char)))
2242 (defun whitespace-display-vector-p (vec)
2243 "Return true if every character in vector VEC can be displayed."
2244 (let ((i (length vec)))
2245 (when (> i 0)
2246 (while (and (>= (setq i (1- i)) 0)
2247 (whitespace-char-valid-p (aref vec i))))
2248 (< i 0))))
2251 (defun whitespace-display-char-on ()
2252 "Turn on character display mapping."
2253 (when (and whitespace-display-mappings
2254 (whitespace-style-mark-p))
2255 (let (vecs vec)
2256 ;; Remember whether a buffer has a local display table.
2257 (unless whitespace-display-table-was-local
2258 (setq whitespace-display-table-was-local t
2259 whitespace-display-table
2260 (copy-sequence buffer-display-table)))
2261 (unless buffer-display-table
2262 (setq buffer-display-table (make-display-table)))
2263 (dolist (entry whitespace-display-mappings)
2264 ;; check if it is to display this mark
2265 (when (memq (car entry) whitespace-style)
2266 ;; Get a displayable mapping.
2267 (setq vecs (cddr entry))
2268 (while (and vecs
2269 (not (whitespace-display-vector-p (car vecs))))
2270 (setq vecs (cdr vecs)))
2271 ;; Display a valid mapping.
2272 (when vecs
2273 (setq vec (copy-sequence (car vecs)))
2274 ;; NEWLINE char
2275 (when (and (eq (cadr entry) ?\n)
2276 (memq 'newline whitespace-active-style))
2277 ;; Only insert face bits on NEWLINE char mapping to avoid
2278 ;; obstruction of other faces like TABs and (HARD) SPACEs
2279 ;; faces, font-lock faces, etc.
2280 (dotimes (i (length vec))
2281 (or (eq (aref vec i) ?\n)
2282 (aset vec i
2283 (make-glyph-code (aref vec i)
2284 whitespace-newline)))))
2285 ;; Display mapping
2286 (aset buffer-display-table (cadr entry) vec)))))))
2289 (defun whitespace-display-char-off ()
2290 "Turn off character display mapping."
2291 (and whitespace-display-mappings
2292 (whitespace-style-mark-p)
2293 whitespace-display-table-was-local
2294 (setq whitespace-display-table-was-local nil
2295 buffer-display-table whitespace-display-table)))
2298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2299 ;;;; Hook
2302 (defun whitespace-action-when-on ()
2303 "Action to be taken always when local whitespace is turned on."
2304 (cond ((memq 'cleanup whitespace-action)
2305 (whitespace-cleanup))
2306 ((memq 'report-on-bogus whitespace-action)
2307 (whitespace-report nil t))))
2310 (defun whitespace-add-local-hook ()
2311 "Add some whitespace hooks locally."
2312 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2313 (add-hook 'kill-buffer-hook 'whitespace-kill-buffer-hook nil t))
2316 (defun whitespace-remove-local-hook ()
2317 "Remove some whitespace hooks locally."
2318 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2319 (remove-hook 'kill-buffer-hook 'whitespace-kill-buffer-hook t))
2322 (defun whitespace-write-file-hook ()
2323 "Action to be taken when buffer is written.
2324 It should be added buffer-locally to `write-file-functions'."
2325 (when (whitespace-action)
2326 (error "Abort write due to whitespace problems in %s"
2327 (buffer-name)))
2328 nil) ; continue hook processing
2331 (defun whitespace-kill-buffer-hook ()
2332 "Action to be taken when buffer is killed.
2333 It should be added buffer-locally to `kill-buffer-hook'."
2334 (whitespace-action)
2335 nil) ; continue hook processing
2338 (defun whitespace-action ()
2339 "Action to be taken when buffer is killed or written.
2340 Return t when the action should be aborted."
2341 (cond ((memq 'auto-cleanup whitespace-action)
2342 (whitespace-cleanup)
2343 nil)
2344 ((memq 'abort-on-bogus whitespace-action)
2345 (whitespace-report nil t))
2347 nil)))
2350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2353 (defun whitespace-unload-function ()
2354 "Unload the whitespace library."
2355 (global-whitespace-mode -1)
2356 ;; be sure all local whitespace mode is turned off
2357 (save-current-buffer
2358 (dolist (buf (buffer-list))
2359 (set-buffer buf)
2360 (whitespace-mode -1)))
2361 nil) ; continue standard unloading
2364 (provide 'whitespace)
2367 (run-hooks 'whitespace-load-hook)
2370 ;; arch-tag: 1b1e2500-dbd4-4a26-8f7a-5a5edfd3c97e
2371 ;;; whitespace.el ends here