1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
6 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
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/>.
28 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
36 ;; whitespace uses two ways to visualize blanks: faces and display
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
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
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
129 ;; There is no problem if you mix local and global minor mode usage.
131 ;; * LOCAL whitespace:
132 ;; + To toggle whitespace options locally, type:
134 ;; M-x whitespace-toggle-options RET
136 ;; + To activate whitespace locally, type:
138 ;; C-u 1 M-x whitespace-mode RET
140 ;; + To deactivate whitespace locally, type:
142 ;; C-u 0 M-x whitespace-mode RET
144 ;; + To toggle whitespace locally, type:
146 ;; M-x whitespace-mode RET
148 ;; * GLOBAL whitespace:
149 ;; + To toggle whitespace options globally, type:
151 ;; M-x global-whitespace-toggle-options RET
153 ;; + To activate whitespace globally, type:
155 ;; C-u 1 M-x global-whitespace-mode RET
157 ;; + To deactivate whitespace globally, type:
159 ;; C-u 0 M-x global-whitespace-mode RET
161 ;; + To toggle whitespace globally, type:
163 ;; M-x global-whitespace-mode RET
165 ;; There are also the following useful commands:
167 ;; `whitespace-newline-mode'
168 ;; Toggle NEWLINE minor mode visualization ("nl" on modeline).
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on modeline).
173 ;; `whitespace-report'
174 ;; Report some blank problems in buffer.
176 ;; `whitespace-report-region'
177 ;; Report some blank problems in a region.
179 ;; `whitespace-cleanup'
180 ;; Cleanup some blank problems in all buffer or at region.
182 ;; `whitespace-cleanup-region'
183 ;; Cleanup some blank problems at region.
185 ;; The problems, which are cleaned up, are:
187 ;; 1. empty lines at beginning of buffer.
188 ;; 2. empty lines at end of buffer.
189 ;; If `whitespace-style' includes the value `empty', remove all
190 ;; empty lines at beginning and/or end of buffer.
192 ;; 3. 8 or more SPACEs at beginning of line.
193 ;; If `whitespace-style' includes the value `indentation':
194 ;; replace 8 or more SPACEs at beginning of line by TABs, if
195 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
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.
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.
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
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
258 ;; `whitespace-trailing' Face used to visualize trailing
261 ;; `whitespace-line' Face used to visualize "long" lines.
263 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
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
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
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
295 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
298 ;; `whitespace-line-column' Specify column beyond which the line
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
308 ;; `whitespace-action' Specify which action is taken when a
309 ;; buffer is visited or written.
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
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
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).
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 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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")
386 (defcustom whitespace-style
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
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
413 Whole line is highlighted.
414 It has precedence over `lines-tail' (see
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
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
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
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
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
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
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
510 3. indentation::space
512 * For SPACEs after TABs:
514 2. space-after-tab::tab
515 3. space-after-tab::space
517 * For SPACEs before TABs:
519 2. space-before-tab::tab
520 3. space-before-tab::space
522 So, for example, if indentation and indentation::space are
523 included in `whitespace-style' list, the indentation value is
524 evaluated instead of indentation::space value.
526 One reason for not visualize spaces via faces (if `face' is not
527 included in `whitespace-style') is to use exclusively for
528 cleanning up a buffer. See `whitespace-cleanup' and
529 `whitespace-cleanup-region' for documentation.
531 See also `whitespace-display-mappings' for documentation."
532 :type
'(repeat :tag
"Kind of Blank"
533 (choice :tag
"Kind of Blank Face"
534 (const :tag
"(Face) Face visualization"
536 (const :tag
"(Face) Trailing TABs, SPACEs and HARD SPACEs"
538 (const :tag
"(Face) SPACEs and HARD SPACEs"
540 (const :tag
"(Face) TABs" tabs
)
541 (const :tag
"(Face) Lines" lines
)
542 (const :tag
"(Face) SPACEs before TAB"
544 (const :tag
"(Face) NEWLINEs" newline
)
545 (const :tag
"(Face) Indentation SPACEs"
547 (const :tag
"(Face) Empty Lines At BOB And/Or EOB"
549 (const :tag
"(Face) SPACEs after TAB"
551 (const :tag
"(Mark) SPACEs and HARD SPACEs"
553 (const :tag
"(Mark) TABs" tab-mark
)
554 (const :tag
"(Mark) NEWLINEs" newline-mark
)))
558 (defcustom whitespace-space
'whitespace-space
559 "Symbol face used to visualize SPACE.
561 Used when `whitespace-style' includes the value `spaces'."
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."
576 (defcustom whitespace-hspace
'whitespace-hspace
577 "Symbol face used to visualize HARD SPACE.
579 Used when `whitespace-style' includes the value `spaces'."
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."
594 (defcustom whitespace-tab
'whitespace-tab
595 "Symbol face used to visualize TAB.
597 Used when `whitespace-style' includes the value `tabs'."
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."
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'
623 (defface whitespace-newline
624 '((((class color
) (background dark
))
625 (:foreground
"darkgray" :bold nil
))
626 (((class color
) (background light
))
627 (:foreground
"lightgray" :bold nil
))
628 (t (:underline t
:bold nil
)))
629 "Face used to visualize NEWLINE char mapping.
631 See `whitespace-display-mappings'."
635 (defcustom whitespace-trailing
'whitespace-trailing
636 "Symbol face used to visualize trailing blanks.
638 Used when `whitespace-style' includes the value `trailing'."
643 (defface whitespace-trailing
; 'trailing-whitespace
644 '((((class mono
)) (:inverse-video t
:bold t
:underline t
))
645 (t (:background
"red1" :foreground
"yellow" :bold t
)))
646 "Face used to visualize trailing blanks."
650 (defcustom whitespace-line
'whitespace-line
651 "Symbol face used to visualize \"long\" lines.
653 See `whitespace-line-column'.
655 Used when `whitespace-style' includes the value `line'."
660 (defface whitespace-line
661 '((((class mono
)) (:inverse-video t
:bold t
:underline t
))
662 (t (:background
"gray20" :foreground
"violet")))
663 "Face used to visualize \"long\" lines.
665 See `whitespace-line-column'."
669 (defcustom whitespace-space-before-tab
'whitespace-space-before-tab
670 "Symbol face used to visualize SPACEs before TAB.
672 Used when `whitespace-style' includes the value `space-before-tab'."
677 (defface whitespace-space-before-tab
678 '((((class mono
)) (:inverse-video t
:bold t
:underline t
))
679 (t (:background
"DarkOrange" :foreground
"firebrick")))
680 "Face used to visualize SPACEs before TAB."
684 (defcustom whitespace-indentation
'whitespace-indentation
685 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
687 Used when `whitespace-style' includes the value `indentation'."
692 (defface whitespace-indentation
693 '((((class mono
)) (:inverse-video t
:bold t
:underline t
))
694 (t (:background
"yellow" :foreground
"firebrick")))
695 "Face used to visualize 8 or more SPACEs at beginning of line."
699 (defcustom whitespace-empty
'whitespace-empty
700 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
702 Used when `whitespace-style' includes the value `empty'."
707 (defface whitespace-empty
708 '((((class mono
)) (:inverse-video t
:bold t
:underline t
))
709 (t (:background
"yellow" :foreground
"firebrick")))
710 "Face used to visualize empty lines at beginning and/or end of buffer."
714 (defcustom whitespace-space-after-tab
'whitespace-space-after-tab
715 "Symbol face used to visualize 8 or more SPACEs after TAB.
717 Used when `whitespace-style' includes the value `space-after-tab'."
722 (defface whitespace-space-after-tab
723 '((((class mono
)) (:inverse-video t
:bold t
:underline t
))
724 (t (:background
"yellow" :foreground
"firebrick")))
725 "Face used to visualize 8 or more SPACEs after TAB."
729 (defcustom whitespace-hspace-regexp
730 "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
731 "Specify HARD SPACE characters regexp.
733 If you're using `mule' package, there may be other characters besides:
735 \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
737 that should be considered HARD SPACE.
739 Here are some examples:
741 \"\\\\(^\\xA0+\\\\)\" \
742 visualize only leading HARD SPACEs.
743 \"\\\\(\\xA0+$\\\\)\" \
744 visualize only trailing HARD SPACEs.
745 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
746 visualize leading and/or trailing HARD SPACEs.
747 \"\\t\\\\(\\xA0+\\\\)\\t\" \
748 visualize only HARD SPACEs between TABs.
750 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
751 Use exactly one pair of enclosing \\\\( and \\\\).
753 Used when `whitespace-style' includes `spaces'."
754 :type
'(regexp :tag
"HARD SPACE Chars")
758 (defcustom whitespace-space-regexp
"\\( +\\)"
759 "Specify SPACE characters regexp.
761 If you're using `mule' package, there may be other characters
762 besides \" \" that should be considered SPACE.
764 Here are some examples:
766 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
767 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
768 \"\\\\(^ +\\\\| +$\\\\)\" \
769 visualize leading and/or trailing SPACEs.
770 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
772 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
773 Use exactly one pair of enclosing \\\\( and \\\\).
775 Used when `whitespace-style' includes `spaces'."
776 :type
'(regexp :tag
"SPACE Chars")
780 (defcustom whitespace-tab-regexp
"\\(\t+\\)"
781 "Specify TAB characters regexp.
783 If you're using `mule' package, there may be other characters
784 besides \"\\t\" that should be considered TAB.
786 Here are some examples:
788 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
789 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
790 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
791 visualize leading and/or trailing TABs.
792 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
794 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
795 Use exactly one pair of enclosing \\\\( and \\\\).
797 Used when `whitespace-style' includes `tabs'."
798 :type
'(regexp :tag
"TAB Chars")
802 (defcustom whitespace-trailing-regexp
803 "\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$"
804 "Specify trailing characters regexp.
806 If you're using `mule' package, there may be other characters besides:
808 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
811 that should be considered blank.
813 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
814 Use exactly one pair of enclosing elements above.
816 Used when `whitespace-style' includes `trailing'."
817 :type
'(regexp :tag
"Trailing Chars")
821 (defcustom whitespace-space-before-tab-regexp
"\\( +\\)\\(\t+\\)"
822 "Specify SPACEs before TAB regexp.
824 If you're using `mule' package, there may be other characters besides:
826 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
829 that should be considered blank.
831 Used when `whitespace-style' includes `space-before-tab',
832 `space-before-tab::tab' or `space-before-tab::space'."
833 :type
'(regexp :tag
"SPACEs Before TAB")
837 (defcustom whitespace-indentation-regexp
838 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
839 .
"^ *\\(\t+\\)[^\n]")
840 "Specify regexp for 8 or more SPACEs at beginning of line.
842 It is a cons where the cons car is used for SPACEs visualization
843 and the cons cdr is used for TABs visualization.
845 If you're using `mule' package, there may be other characters besides:
847 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
850 that should be considered blank.
852 Used when `whitespace-style' includes `indentation',
853 `indentation::tab' or `indentation::space'."
854 :type
'(cons (regexp :tag
"Indentation SPACEs")
855 (regexp :tag
"Indentation TABs"))
859 (defcustom whitespace-empty-at-bob-regexp
"^\\(\\([ \t]*\n\\)+\\)"
860 "Specify regexp for empty lines at beginning of buffer.
862 If you're using `mule' package, there may be other characters besides:
864 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
867 that should be considered blank.
869 Used when `whitespace-style' includes `empty'."
870 :type
'(regexp :tag
"Empty Lines At Beginning Of Buffer")
874 (defcustom whitespace-empty-at-eob-regexp
"^\\([ \t\n]+\\)"
875 "Specify regexp for empty lines at end of buffer.
877 If you're using `mule' package, there may be other characters besides:
879 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
882 that should be considered blank.
884 Used when `whitespace-style' includes `empty'."
885 :type
'(regexp :tag
"Empty Lines At End Of Buffer")
889 (defcustom whitespace-space-after-tab-regexp
890 '("\t+\\(\\( \\{%d\\}\\)+\\)"
892 "Specify regexp for 8 or more SPACEs after TAB.
894 It is a cons where the cons car is used for SPACEs visualization
895 and the cons cdr is used for TABs visualization.
897 If you're using `mule' package, there may be other characters besides:
899 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
902 that should be considered blank.
904 Used when `whitespace-style' includes `space-after-tab',
905 `space-after-tab::tab' or `space-after-tab::space'."
906 :type
'(regexp :tag
"SPACEs After TAB")
910 (defcustom whitespace-line-column
80
911 "Specify column beyond which the line is highlighted.
913 It must be an integer or nil. If nil, the `fill-column' variable value is
916 Used when `whitespace-style' includes `lines' or `lines-tail'."
917 :type
'(choice :tag
"Line Length Limit"
918 (integer :tag
"Line Length")
919 (const :tag
"Use fill-column" nil
))
923 ;; Hacked from `visible-whitespace-mappings' in visws.el
924 (defcustom whitespace-display-mappings
926 (space-mark ?\
[?
\u00B7] [?.
]) ; space - centered dot
927 (space-mark ?
\xA0 [?
\u00A4] [?_
]) ; hard space - currency
928 (space-mark ?
\x8A0 [?
\x8A4] [?_
]) ; hard space - currency
929 (space-mark ?
\x920
[?
\x924
] [?_
]) ; hard space - currency
930 (space-mark ?
\xE20
[?
\xE24
] [?_
]) ; hard space - currency
931 (space-mark ?
\xF20
[?
\xF24
] [?_
]) ; hard space - currency
932 ;; NEWLINE is displayed using the face `whitespace-newline'
933 (newline-mark ?
\n [?$ ?
\n]) ; eol - dollar sign
934 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
935 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
936 ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
937 ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
938 ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
940 ;; WARNING: the mapping below has a problem.
941 ;; When a TAB occupies exactly one column, it will display the
942 ;; character ?\xBB at that column followed by a TAB which goes to
943 ;; the next TAB column.
944 ;; If this is a problem for you, please, comment the line below.
945 (tab-mark ?
\t [?
\u00BB ?
\t] [?
\\ ?
\t]) ; tab - left quote mark
947 "Specify an alist of mappings for displaying characters.
949 Each element has the following form:
951 (KIND CHAR VECTOR...)
955 KIND is the kind of character.
956 It can be one of the following symbols:
958 tab-mark for TAB character
960 space-mark for SPACE or HARD SPACE character
962 newline-mark for NEWLINE character
964 CHAR is the character to be mapped.
966 VECTOR is a vector of characters to be displayed in place of CHAR.
967 The first display vector that can be displayed is used;
968 if no display vector for a mapping can be displayed, then
969 that character is displayed unmodified.
971 The NEWLINE character is displayed using the face given by
972 `whitespace-newline' variable.
974 Used when `whitespace-style' includes `tab-mark', `space-mark' or
977 (list :tag
"Character Mapping"
978 (choice :tag
"Char Kind"
979 (const :tag
"Tab" tab-mark
)
980 (const :tag
"Space" space-mark
)
981 (const :tag
"Newline" newline-mark
))
982 (character :tag
"Char")
983 (repeat :inline t
:tag
"Vector List"
986 :tag
"Vector Characters"
987 (character :tag
"Char"))))))
991 (defcustom whitespace-global-modes t
992 "Modes for which global `whitespace-mode' is automagically turned on.
994 Global `whitespace-mode' is controlled by the command
995 `global-whitespace-mode'.
997 If nil, means no modes have `whitespace-mode' automatically
1000 If t, all modes that support `whitespace-mode' have it
1001 automatically turned on.
1003 Else it should be a list of `major-mode' symbol names for which
1004 `whitespace-mode' should be automatically turned on. The sense
1005 of the list is negated if it begins with `not'. For example:
1009 means that `whitespace-mode' is turned on for buffers in C and
1011 :type
'(choice :tag
"Global Modes"
1012 (const :tag
"None" nil
)
1013 (const :tag
"All" t
)
1014 (set :menu-tag
"Mode Specific" :tag
"Modes"
1016 (const :tag
"Except" not
)
1018 (symbol :tag
"Mode"))))
1022 (defcustom whitespace-action nil
1023 "Specify which action is taken when a buffer is visited or written.
1025 It's a list containing some or all of the following values:
1027 nil no action is taken.
1029 cleanup cleanup any bogus whitespace always when local
1030 whitespace is turned on.
1031 See `whitespace-cleanup' and
1032 `whitespace-cleanup-region'.
1034 report-on-bogus report if there is any bogus whitespace always
1035 when local whitespace is turned on.
1037 auto-cleanup cleanup any bogus whitespace when buffer is
1039 See `whitespace-cleanup' and
1040 `whitespace-cleanup-region'.
1042 abort-on-bogus abort if there is any bogus whitespace and the
1045 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
1046 is included in `whitespace-action' and the
1047 buffer is read-only.
1049 Any other value is treated as nil."
1050 :type
'(choice :tag
"Actions"
1051 (const :tag
"None" nil
)
1052 (repeat :tag
"Action List"
1053 (choice :tag
"Action"
1054 (const :tag
"Cleanup When On" cleanup
)
1055 (const :tag
"Report On Bogus" report-on-bogus
)
1056 (const :tag
"Auto Cleanup" auto-cleanup
)
1057 (const :tag
"Abort On Bogus" abort-on-bogus
)
1058 (const :tag
"Warn If Read-Only" warn-if-read-only
))))
1062 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1063 ;;;; User commands - Local mode
1067 (define-minor-mode whitespace-mode
1068 "Toggle whitespace minor mode visualization (\"ws\" on modeline).
1070 If ARG is null, toggle whitespace visualization.
1071 If ARG is a number greater than zero, turn on visualization;
1072 otherwise, turn off visualization.
1074 See also `whitespace-style', `whitespace-newline' and
1075 `whitespace-display-mappings'."
1081 (noninteractive ; running a batch job
1082 (setq whitespace-mode nil
))
1083 (whitespace-mode ; whitespace-mode on
1084 (whitespace-turn-on)
1085 (whitespace-action-when-on))
1086 (t ; whitespace-mode off
1087 (whitespace-turn-off))))
1091 (define-minor-mode whitespace-newline-mode
1092 "Toggle NEWLINE minor mode visualization (\"nl\" on modeline).
1094 If ARG is null, toggle NEWLINE visualization.
1095 If ARG is a number greater than zero, turn on visualization;
1096 otherwise, turn off visualization.
1098 Use `whitespace-newline-mode' only for NEWLINE visualization
1099 exclusively. For other visualizations, including NEWLINE
1100 visualization together with (HARD) SPACEs and/or TABs, please,
1101 use `whitespace-mode'.
1103 See also `whitespace-newline' and `whitespace-display-mappings'."
1108 (let ((whitespace-style '(face newline-mark newline
)))
1109 (whitespace-mode whitespace-newline-mode
)
1110 ;; sync states (running a batch job)
1111 (setq whitespace-newline-mode whitespace-mode
)))
1114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1115 ;;;; User commands - Global mode
1119 (define-minor-mode global-whitespace-mode
1120 "Toggle whitespace global minor mode visualization (\"WS\" on modeline).
1122 If ARG is null, toggle whitespace visualization.
1123 If ARG is a number greater than zero, turn on visualization;
1124 otherwise, turn off visualization.
1126 See also `whitespace-style', `whitespace-newline' and
1127 `whitespace-display-mappings'."
1133 (noninteractive ; running a batch job
1134 (setq global-whitespace-mode nil
))
1135 (global-whitespace-mode ; global-whitespace-mode on
1137 (add-hook 'find-file-hook
'whitespace-turn-on-if-enabled
)
1138 (add-hook 'after-change-major-mode-hook
'whitespace-turn-on-if-enabled
)
1139 (dolist (buffer (buffer-list)) ; adjust all local mode
1141 (unless whitespace-mode
1142 (whitespace-turn-on-if-enabled)))))
1143 (t ; global-whitespace-mode off
1145 (remove-hook 'find-file-hook
'whitespace-turn-on-if-enabled
)
1146 (remove-hook 'after-change-major-mode-hook
'whitespace-turn-on-if-enabled
)
1147 (dolist (buffer (buffer-list)) ; adjust all local mode
1149 (unless whitespace-mode
1150 (whitespace-turn-off)))))))
1153 (defun whitespace-turn-on-if-enabled ()
1155 ((eq whitespace-global-modes t
))
1156 ((listp whitespace-global-modes
)
1157 (if (eq (car-safe whitespace-global-modes
) 'not
)
1158 (not (memq major-mode
(cdr whitespace-global-modes
)))
1159 (memq major-mode whitespace-global-modes
)))
1162 ;; Don't turn on whitespace mode if...
1164 ;; ...we don't have a display (we're running a batch job)
1166 ;; ...or if the buffer is invisible (name starts with a space)
1167 (eq (aref (buffer-name) 0) ?\
)
1168 ;; ...or if the buffer is temporary (name starts with *)
1169 (and (eq (aref (buffer-name) 0) ?
*)
1170 ;; except the scratch buffer.
1171 (not (string= (buffer-name) "*scratch*")))
1172 ;; Otherwise, turn on whitespace mode.
1173 (whitespace-turn-on)))))
1177 (define-minor-mode global-whitespace-newline-mode
1178 "Toggle NEWLINE global minor mode visualization (\"NL\" on modeline).
1180 If ARG is null, toggle NEWLINE visualization.
1181 If ARG is a number greater than zero, turn on visualization;
1182 otherwise, turn off visualization.
1184 Use `global-whitespace-newline-mode' only for NEWLINE
1185 visualization exclusively. For other visualizations, including
1186 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1187 please use `global-whitespace-mode'.
1189 See also `whitespace-newline' and `whitespace-display-mappings'."
1194 (let ((whitespace-style '(newline-mark newline
)))
1195 (global-whitespace-mode (if global-whitespace-newline-mode
1197 ;; sync states (running a batch job)
1198 (setq global-whitespace-newline-mode global-whitespace-mode
)))
1201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1202 ;;;; User commands - Toggle
1205 (defconst whitespace-style-value-list
1218 space-after-tab
::tab
1219 space-after-tab
::space
1221 space-before-tab
::tab
1222 space-before-tab
::space
1223 help-newline
; value used by `whitespace-insert-option-mark'
1228 "List of valid `whitespace-style' values.")
1231 (defconst whitespace-toggle-option-alist
1240 (?\C-i . indentation
)
1241 (?I . indentation
::tab
)
1242 (?i . indentation
::space
)
1243 (?\C-a . space-after-tab
)
1244 (?A . space-after-tab
::tab
)
1245 (?a . space-after-tab
::space
)
1246 (?\C-b . space-before-tab
)
1247 (?B . space-before-tab
::tab
)
1248 (?b . space-before-tab
::space
)
1252 (?x . whitespace-style
)
1254 "Alist of toggle options.
1256 Each element has the form:
1262 CHAR is a char which the user will have to type.
1264 SYMBOL is a valid symbol associated with CHAR.
1265 See `whitespace-style-value-list'.")
1268 (defvar whitespace-active-style nil
1269 "Used to save locally `whitespace-style' value.")
1271 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1272 "Used to save locally `indent-tabs-mode' value.")
1274 (defvar whitespace-tab-width tab-width
1275 "Used to save locally `tab-width' value.")
1277 (defvar whitespace-point
(point)
1278 "Used to save locally current point value.
1279 Used by `whitespace-trailing-regexp' function (which see).")
1281 (defvar whitespace-font-lock-refontify nil
1282 "Used to save locally the font-lock refontify state.
1283 Used by `whitespace-post-command-hook' function (which see).")
1285 (defvar whitespace-bob-marker nil
1286 "Used to save locally the bob marker value.
1287 Used by `whitespace-post-command-hook' function (which see).")
1289 (defvar whitespace-eob-marker nil
1290 "Used to save locally the eob marker value.
1291 Used by `whitespace-post-command-hook' function (which see).")
1293 (defvar whitespace-buffer-changed nil
1294 "Used to indicate locally if buffer changed.
1295 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1296 functions (which see).")
1300 (defun whitespace-toggle-options (arg)
1301 "Toggle local `whitespace-mode' options.
1303 If local whitespace-mode is off, toggle the option given by ARG
1304 and turn on local whitespace-mode.
1306 If local whitespace-mode is on, toggle the option given by ARG
1307 and restart local whitespace-mode.
1309 Interactively, it reads one of the following chars:
1313 f toggle face visualization
1314 t toggle TAB visualization
1315 s toggle SPACE and HARD SPACE visualization
1316 r toggle trailing blanks visualization
1317 l toggle \"long lines\" visualization
1318 L toggle \"long lines\" tail visualization
1319 n toggle NEWLINE visualization
1320 e toggle empty line at bob and/or eob visualization
1321 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1322 I toggle indentation SPACEs visualization
1323 i toggle indentation TABs visualization
1324 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1325 A toggle SPACEs after TAB: SPACEs visualization
1326 a toggle SPACEs after TAB: TABs visualization
1327 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1328 B toggle SPACEs before TAB: SPACEs visualization
1329 b toggle SPACEs before TAB: TABs visualization
1332 T toggle TAB visualization
1333 S toggle SPACEs before TAB visualization
1334 N toggle NEWLINE visualization
1336 x restore `whitespace-style' value
1337 ? display brief help
1339 Non-interactively, ARG should be a symbol or a list of symbols.
1340 The valid symbols are:
1342 face toggle face visualization
1343 tabs toggle TAB visualization
1344 spaces toggle SPACE and HARD SPACE visualization
1345 trailing toggle trailing blanks visualization
1346 lines toggle \"long lines\" visualization
1347 lines-tail toggle \"long lines\" tail visualization
1348 newline toggle NEWLINE visualization
1349 empty toggle empty line at bob and/or eob visualization
1350 indentation toggle indentation SPACEs visualization
1351 indentation::tab toggle indentation SPACEs visualization
1352 indentation::space toggle indentation TABs visualization
1353 space-after-tab toggle SPACEs after TAB visualization
1354 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1355 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1356 space-before-tab toggle SPACEs before TAB visualization
1357 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1358 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1360 tab-mark toggle TAB visualization
1361 space-mark toggle SPACEs before TAB visualization
1362 newline-mark toggle NEWLINE visualization
1364 whitespace-style restore `whitespace-style' value
1366 See `whitespace-style' and `indent-tabs-mode' for documentation."
1367 (interactive (whitespace-interactive-char t
))
1368 (let ((whitespace-style
1369 (whitespace-toggle-list t arg whitespace-active-style
)))
1371 (whitespace-mode 1)))
1374 (defvar whitespace-toggle-style nil
1375 "Used to toggle the global `whitespace-style' value.")
1379 (defun global-whitespace-toggle-options (arg)
1380 "Toggle global `whitespace-mode' options.
1382 If global whitespace-mode is off, toggle the option given by ARG
1383 and turn on global whitespace-mode.
1385 If global whitespace-mode is on, toggle the option given by ARG
1386 and restart global whitespace-mode.
1388 Interactively, it accepts one of the following chars:
1392 f toggle face visualization
1393 t toggle TAB visualization
1394 s toggle SPACE and HARD SPACE visualization
1395 r toggle trailing blanks visualization
1396 l toggle \"long lines\" visualization
1397 L toggle \"long lines\" tail visualization
1398 n toggle NEWLINE visualization
1399 e toggle empty line at bob and/or eob visualization
1400 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1401 I toggle indentation SPACEs visualization
1402 i toggle indentation TABs visualization
1403 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1404 A toggle SPACEs after TAB: SPACEs visualization
1405 a toggle SPACEs after TAB: TABs visualization
1406 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1407 B toggle SPACEs before TAB: SPACEs visualization
1408 b toggle SPACEs before TAB: TABs visualization
1411 T toggle TAB visualization
1412 S toggle SPACEs before TAB visualization
1413 N toggle NEWLINE visualization
1415 x restore `whitespace-style' value
1416 ? display brief help
1418 Non-interactively, ARG should be a symbol or a list of symbols.
1419 The valid symbols are:
1421 face toggle face visualization
1422 tabs toggle TAB visualization
1423 spaces toggle SPACE and HARD SPACE visualization
1424 trailing toggle trailing blanks visualization
1425 lines toggle \"long lines\" visualization
1426 lines-tail toggle \"long lines\" tail visualization
1427 newline toggle NEWLINE visualization
1428 empty toggle empty line at bob and/or eob visualization
1429 indentation toggle indentation SPACEs visualization
1430 indentation::tab toggle indentation SPACEs visualization
1431 indentation::space toggle indentation TABs visualization
1432 space-after-tab toggle SPACEs after TAB visualization
1433 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1434 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1435 space-before-tab toggle SPACEs before TAB visualization
1436 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1437 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1439 tab-mark toggle TAB visualization
1440 space-mark toggle SPACEs before TAB visualization
1441 newline-mark toggle NEWLINE visualization
1443 whitespace-style restore `whitespace-style' value
1445 See `whitespace-style' and `indent-tabs-mode' for documentation."
1446 (interactive (whitespace-interactive-char nil
))
1447 (let ((whitespace-style
1448 (whitespace-toggle-list nil arg whitespace-toggle-style
)))
1449 (setq whitespace-toggle-style whitespace-style
)
1450 (global-whitespace-mode 0)
1451 (global-whitespace-mode 1)))
1454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1455 ;;;; User commands - Cleanup
1459 (defun whitespace-cleanup ()
1460 "Cleanup some blank problems in all buffer or at region.
1462 It usually applies to the whole buffer, but in transient mark
1463 mode when the mark is active, it applies to the region. It also
1464 applies to the region when it is not in transient mark mode, the
1465 mark is active and \\[universal-argument] was pressed just before
1466 calling `whitespace-cleanup' interactively.
1468 See also `whitespace-cleanup-region'.
1470 The problems cleaned up are:
1472 1. empty lines at beginning of buffer.
1473 2. empty lines at end of buffer.
1474 If `whitespace-style' includes the value `empty', remove all
1475 empty lines at beginning and/or end of buffer.
1477 3. 8 or more SPACEs at beginning of line.
1478 If `whitespace-style' includes the value `indentation':
1479 replace 8 or more SPACEs at beginning of line by TABs, if
1480 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1482 If `whitespace-style' includes the value `indentation::tab',
1483 replace 8 or more SPACEs at beginning of line by TABs.
1484 If `whitespace-style' includes the value `indentation::space',
1485 replace TABs by SPACEs.
1487 4. SPACEs before TAB.
1488 If `whitespace-style' includes the value `space-before-tab':
1489 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1490 otherwise, replace TABs by SPACEs.
1491 If `whitespace-style' includes the value
1492 `space-before-tab::tab', replace SPACEs by TABs.
1493 If `whitespace-style' includes the value
1494 `space-before-tab::space', replace TABs by SPACEs.
1496 5. SPACEs or TABs at end of line.
1497 If `whitespace-style' includes the value `trailing', remove
1498 all SPACEs or TABs at end of line.
1500 6. 8 or more SPACEs after TAB.
1501 If `whitespace-style' includes the value `space-after-tab':
1502 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1503 otherwise, replace TABs by SPACEs.
1504 If `whitespace-style' includes the value
1505 `space-after-tab::tab', replace SPACEs by TABs.
1506 If `whitespace-style' includes the value
1507 `space-after-tab::space', replace TABs by SPACEs.
1509 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1515 (whitespace-warn-read-only "cleanup"))
1517 ((and (or transient-mark-mode
1520 ;; PROBLEMs 1 and 2 are not handled in region
1521 ;; PROBLEM 3: 8 or more SPACEs at bol
1522 ;; PROBLEM 4: SPACEs before TAB
1523 ;; PROBLEM 5: SPACEs or TABs at eol
1524 ;; PROBLEM 6: 8 or more SPACEs after TAB
1525 (whitespace-cleanup-region (region-beginning) (region-end)))
1530 ;; PROBLEM 1: empty lines at bob
1531 ;; PROBLEM 2: empty lines at eob
1532 ;; ACTION: remove all empty lines at bob and/or eob
1533 (when (memq 'empty whitespace-style
)
1534 (let (overwrite-mode) ; enforce no overwrite
1535 (goto-char (point-min))
1536 (when (re-search-forward
1537 (concat "\\`" whitespace-empty-at-bob-regexp
) nil t
)
1538 (delete-region (match-beginning 1) (match-end 1)))
1539 (when (re-search-forward
1540 (concat whitespace-empty-at-eob-regexp
"\\'") nil t
)
1541 (delete-region (match-beginning 1) (match-end 1)))))))
1542 ;; PROBLEM 3: 8 or more SPACEs at bol
1543 ;; PROBLEM 4: SPACEs before TAB
1544 ;; PROBLEM 5: SPACEs or TABs at eol
1545 ;; PROBLEM 6: 8 or more SPACEs after TAB
1546 (whitespace-cleanup-region (point-min) (point-max)))))
1550 (defun whitespace-cleanup-region (start end
)
1551 "Cleanup some blank problems at region.
1553 The problems cleaned up are:
1555 1. 8 or more SPACEs at beginning of line.
1556 If `whitespace-style' includes the value `indentation':
1557 replace 8 or more SPACEs at beginning of line by TABs, if
1558 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1560 If `whitespace-style' includes the value `indentation::tab',
1561 replace 8 or more SPACEs at beginning of line by TABs.
1562 If `whitespace-style' includes the value `indentation::space',
1563 replace TABs by SPACEs.
1565 2. SPACEs before TAB.
1566 If `whitespace-style' includes the value `space-before-tab':
1567 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1568 otherwise, replace TABs by SPACEs.
1569 If `whitespace-style' includes the value
1570 `space-before-tab::tab', replace SPACEs by TABs.
1571 If `whitespace-style' includes the value
1572 `space-before-tab::space', replace TABs by SPACEs.
1574 3. SPACEs or TABs at end of line.
1575 If `whitespace-style' includes the value `trailing', remove
1576 all SPACEs or TABs at end of line.
1578 4. 8 or more SPACEs after TAB.
1579 If `whitespace-style' includes the value `space-after-tab':
1580 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1581 otherwise, replace TABs by SPACEs.
1582 If `whitespace-style' includes the value
1583 `space-after-tab::tab', replace SPACEs by TABs.
1584 If `whitespace-style' includes the value
1585 `space-after-tab::space', replace TABs by SPACEs.
1587 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1590 (if buffer-read-only
1592 (whitespace-warn-read-only "cleanup region")
1593 ;; non-read-only buffer
1594 (let ((rstart (min start end
))
1595 (rend (copy-marker (max start end
)))
1596 (indent-tabs-mode whitespace-indent-tabs-mode
)
1597 (tab-width whitespace-tab-width
)
1598 overwrite-mode
; enforce no overwrite
1602 ;; PROBLEM 1: 8 or more SPACEs at bol
1604 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1605 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1607 ((memq 'indentation whitespace-style
)
1608 (let ((regexp (whitespace-indentation-regexp)))
1610 (while (re-search-forward regexp rend t
)
1611 (setq tmp
(current-indentation))
1612 (goto-char (match-beginning 0))
1613 (delete-horizontal-space)
1616 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1617 ((memq 'indentation
::tab whitespace-style
)
1618 (whitespace-replace-action
1620 (whitespace-indentation-regexp 'tab
) 0))
1621 ;; ACTION: replace TABs by SPACEs.
1622 ((memq 'indentation
::space whitespace-style
)
1623 (whitespace-replace-action
1624 'untabify rstart rend
1625 (whitespace-indentation-regexp 'space
) 0)))
1626 ;; PROBLEM 3: SPACEs or TABs at eol
1627 ;; ACTION: remove all SPACEs or TABs at eol
1628 (when (memq 'trailing whitespace-style
)
1629 (whitespace-replace-action
1630 'delete-region rstart rend
1631 whitespace-trailing-regexp
1))
1632 ;; PROBLEM 4: 8 or more SPACEs after TAB
1634 ;; ACTION: replace 8 or more SPACEs by TABs, if
1635 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1637 ((memq 'space-after-tab whitespace-style
)
1638 (whitespace-replace-action
1639 (if whitespace-indent-tabs-mode
'tabify
'untabify
)
1640 rstart rend
(whitespace-space-after-tab-regexp) 1))
1641 ;; ACTION: replace 8 or more SPACEs by TABs.
1642 ((memq 'space-after-tab
::tab whitespace-style
)
1643 (whitespace-replace-action
1645 (whitespace-space-after-tab-regexp 'tab
) 1))
1646 ;; ACTION: replace TABs by SPACEs.
1647 ((memq 'space-after-tab
::space whitespace-style
)
1648 (whitespace-replace-action
1649 'untabify rstart rend
1650 (whitespace-space-after-tab-regexp 'space
) 1)))
1651 ;; PROBLEM 2: SPACEs before TAB
1653 ;; ACTION: replace SPACEs before TAB by TABs, if
1654 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1656 ((memq 'space-before-tab whitespace-style
)
1657 (whitespace-replace-action
1658 (if whitespace-indent-tabs-mode
'tabify
'untabify
)
1659 rstart rend whitespace-space-before-tab-regexp
1660 (if whitespace-indent-tabs-mode
0 2)))
1661 ;; ACTION: replace SPACEs before TAB by TABs.
1662 ((memq 'space-before-tab
::tab whitespace-style
)
1663 (whitespace-replace-action
1665 whitespace-space-before-tab-regexp
0))
1666 ;; ACTION: replace TABs by SPACEs.
1667 ((memq 'space-before-tab
::space whitespace-style
)
1668 (whitespace-replace-action
1669 'untabify rstart rend
1670 whitespace-space-before-tab-regexp
2)))))
1671 (set-marker rend nil
)))) ; point marker to nowhere
1674 (defun whitespace-replace-action (action rstart rend regexp index
)
1675 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1677 INDEX is the level group matched by REGEXP and used by ACTION.
1679 See also `tab-width'."
1681 (while (re-search-forward regexp rend t
)
1682 (goto-char (match-end index
))
1683 (funcall action
(match-beginning index
) (match-end index
))))
1686 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1687 ;;;; User command - report
1690 (defun whitespace-regexp (regexp &optional kind
)
1691 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1694 whitespace-indent-tabs-mode
)
1695 (format (car regexp
) whitespace-tab-width
))
1696 ((or (eq kind
'space
)
1697 (not whitespace-indent-tabs-mode
))
1701 (defun whitespace-indentation-regexp (&optional kind
)
1702 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1703 (whitespace-regexp whitespace-indentation-regexp kind
))
1706 (defun whitespace-space-after-tab-regexp (&optional kind
)
1707 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1708 (whitespace-regexp whitespace-space-after-tab-regexp kind
))
1711 (defconst whitespace-report-list
1713 (cons 'empty whitespace-empty-at-bob-regexp
)
1714 (cons 'empty whitespace-empty-at-eob-regexp
)
1715 (cons 'trailing whitespace-trailing-regexp
)
1716 (cons 'indentation nil
)
1717 (cons 'indentation
::tab nil
)
1718 (cons 'indentation
::space nil
)
1719 (cons 'space-before-tab whitespace-space-before-tab-regexp
)
1720 (cons 'space-before-tab
::tab whitespace-space-before-tab-regexp
)
1721 (cons 'space-before-tab
::space whitespace-space-before-tab-regexp
)
1722 (cons 'space-after-tab nil
)
1723 (cons 'space-after-tab
::tab nil
)
1724 (cons 'space-after-tab
::space nil
)
1726 "List of whitespace bogus symbol and corresponding regexp.")
1729 (defconst whitespace-report-text
1730 '( ;; `indent-tabs-mode' has non-nil value
1734 Current Setting Whitespace Problem
1736 empty [] [] empty lines at beginning of buffer
1737 empty [] [] empty lines at end of buffer
1738 trailing [] [] SPACEs or TABs at end of line
1739 indentation [] [] 8 or more SPACEs at beginning of line
1740 indentation::tab [] [] 8 or more SPACEs at beginning of line
1741 indentation::space [] [] TABs at beginning of line
1742 space-before-tab [] [] SPACEs before TAB
1743 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1744 space-before-tab::space [] [] SPACEs before TAB: TABs
1745 space-after-tab [] [] 8 or more SPACEs after TAB
1746 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1747 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1751 .
;; `indent-tabs-mode' has nil value
1755 Current Setting Whitespace Problem
1757 empty [] [] empty lines at beginning of buffer
1758 empty [] [] empty lines at end of buffer
1759 trailing [] [] SPACEs or TABs at end of line
1760 indentation [] [] TABs at beginning of line
1761 indentation::tab [] [] 8 or more SPACEs at beginning of line
1762 indentation::space [] [] TABs at beginning of line
1763 space-before-tab [] [] SPACEs before TAB
1764 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1765 space-before-tab::space [] [] SPACEs before TAB: TABs
1766 space-after-tab [] [] 8 or more SPACEs after TAB
1767 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1768 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1772 "Text for whitespace bogus report.
1774 It is a cons of strings, where the car part is used when
1775 `indent-tabs-mode' is non-nil, and the cdr part is used when
1776 `indent-tabs-mode' is nil.")
1779 (defconst whitespace-report-buffer-name
"*Whitespace Report*"
1780 "The buffer name for whitespace bogus report.")
1784 (defun whitespace-report (&optional force report-if-bogus
)
1785 "Report some whitespace problems in buffer.
1787 Return nil if there is no whitespace problem; otherwise, return
1790 If FORCE is non-nil or \\[universal-argument] was pressed just
1791 before calling `whitespace-report' interactively, it forces
1792 `whitespace-style' to have:
1800 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1801 whitespace problems in buffer.
1803 Report if some of the following whitespace problems exist:
1805 * If `indent-tabs-mode' is non-nil:
1806 empty 1. empty lines at beginning of buffer.
1807 empty 2. empty lines at end of buffer.
1808 trailing 3. SPACEs or TABs at end of line.
1809 indentation 4. 8 or more SPACEs at beginning of line.
1810 space-before-tab 5. SPACEs before TAB.
1811 space-after-tab 6. 8 or more SPACEs after TAB.
1813 * If `indent-tabs-mode' is nil:
1814 empty 1. empty lines at beginning of buffer.
1815 empty 2. empty lines at end of buffer.
1816 trailing 3. SPACEs or TABs at end of line.
1817 indentation 4. TABS at beginning of line.
1818 space-before-tab 5. SPACEs before TAB.
1819 space-after-tab 6. 8 or more SPACEs after TAB.
1821 See `whitespace-style' for documentation.
1822 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1823 cleaning up these problems."
1824 (interactive (list current-prefix-arg
))
1825 (whitespace-report-region (point-min) (point-max)
1826 force report-if-bogus
))
1830 (defun whitespace-report-region (start end
&optional force report-if-bogus
)
1831 "Report some whitespace problems in a region.
1833 Return nil if there is no whitespace problem; otherwise, return
1836 If FORCE is non-nil or \\[universal-argument] was pressed just
1837 before calling `whitespace-report-region' interactively, it
1838 forces `whitespace-style' to have:
1846 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1847 whitespace problems in buffer.
1849 Report if some of the following whitespace problems exist:
1851 * If `indent-tabs-mode' is non-nil:
1852 empty 1. empty lines at beginning of buffer.
1853 empty 2. empty lines at end of buffer.
1854 trailing 3. SPACEs or TABs at end of line.
1855 indentation 4. 8 or more SPACEs at beginning of line.
1856 space-before-tab 5. SPACEs before TAB.
1857 space-after-tab 6. 8 or more SPACEs after TAB.
1859 * If `indent-tabs-mode' is nil:
1860 empty 1. empty lines at beginning of buffer.
1861 empty 2. empty lines at end of buffer.
1862 trailing 3. SPACEs or TABs at end of line.
1863 indentation 4. TABS at beginning of line.
1864 space-before-tab 5. SPACEs before TAB.
1865 space-after-tab 6. 8 or more SPACEs after TAB.
1867 See `whitespace-style' for documentation.
1868 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1869 cleaning up these problems."
1871 (setq force
(or current-prefix-arg force
))
1874 (let* ((has-bogus nil
)
1875 (rstart (min start end
))
1876 (rend (max start end
))
1881 (add-to-list 'whitespace-style
(car option
)))
1885 ((eq (car option
) 'indentation
)
1886 (whitespace-indentation-regexp))
1887 ((eq (car option
) 'indentation
::tab
)
1888 (whitespace-indentation-regexp 'tab
))
1889 ((eq (car option
) 'indentation
::space
)
1890 (whitespace-indentation-regexp 'space
))
1891 ((eq (car option
) 'space-after-tab
)
1892 (whitespace-space-after-tab-regexp))
1893 ((eq (car option
) 'space-after-tab
::tab
)
1894 (whitespace-space-after-tab-regexp 'tab
))
1895 ((eq (car option
) 'space-after-tab
::space
)
1896 (whitespace-space-after-tab-regexp 'space
))
1899 (and (re-search-forward regexp rend t
)
1900 (setq has-bogus t
))))
1901 whitespace-report-list
)))
1902 (when (if report-if-bogus has-bogus t
)
1903 (whitespace-kill-buffer whitespace-report-buffer-name
)
1904 ;; `whitespace-indent-tabs-mode' is local to current buffer
1905 ;; `whitespace-tab-width' is local to current buffer
1906 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode
)
1907 (ws-tab-width whitespace-tab-width
))
1908 (with-current-buffer (get-buffer-create
1909 whitespace-report-buffer-name
)
1911 (insert (if ws-indent-tabs-mode
1912 (car whitespace-report-text
)
1913 (cdr whitespace-report-text
)))
1914 (goto-char (point-min))
1916 (dolist (option whitespace-report-list
)
1919 27 (memq (car option
) whitespace-style
))
1920 (whitespace-mark-x 7 (car bogus-list
))
1921 (setq bogus-list
(cdr bogus-list
)))
1923 (whitespace-insert-value ws-indent-tabs-mode
)
1924 (whitespace-insert-value ws-tab-width
)
1926 (goto-char (point-max))
1927 (insert " Type `M-x whitespace-cleanup'"
1928 " to cleanup the buffer.\n\n"
1929 " Type `M-x whitespace-cleanup-region'"
1930 " to cleanup a region.\n\n"))
1931 (whitespace-display-window (current-buffer)))))
1935 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1936 ;;;; Internal functions
1939 (defvar whitespace-font-lock-mode nil
1940 "Used to remember whether a buffer had font lock mode on or not.")
1942 (defvar whitespace-font-lock nil
1943 "Used to remember whether a buffer initially had font lock on or not.")
1945 (defvar whitespace-font-lock-keywords nil
1946 "Used to save locally `font-lock-keywords' value.")
1949 (defconst whitespace-help-text
1951 Whitespace Toggle Options | scroll up : SPC or > |
1952 | scroll down: M-SPC or < |
1953 FACES \\__________________________/
1954 [] f - toggle face visualization
1955 [] t - toggle TAB visualization
1956 [] s - toggle SPACE and HARD SPACE visualization
1957 [] r - toggle trailing blanks visualization
1958 [] l - toggle \"long lines\" visualization
1959 [] L - toggle \"long lines\" tail visualization
1960 [] n - toggle NEWLINE visualization
1961 [] e - toggle empty line at bob and/or eob visualization
1962 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1963 [] I - toggle indentation SPACEs visualization
1964 [] i - toggle indentation TABs visualization
1965 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1966 [] A - toggle SPACEs after TAB: SPACEs visualization
1967 [] a - toggle SPACEs after TAB: TABs visualization
1968 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1969 [] B - toggle SPACEs before TAB: SPACEs visualization
1970 [] b - toggle SPACEs before TAB: TABs visualization
1973 [] T - toggle TAB visualization
1974 [] S - toggle SPACE and HARD SPACE visualization
1975 [] N - toggle NEWLINE visualization
1977 x - restore `whitespace-style' value
1979 ? - display this text\n\n"
1980 "Text for whitespace toggle options.")
1983 (defconst whitespace-help-buffer-name
"*Whitespace Toggle Options*"
1984 "The buffer name for whitespace toggle options.")
1987 (defun whitespace-insert-value (value)
1988 "Insert VALUE at column 20 of next line."
1990 (move-to-column 20 t
)
1991 (insert (format "%s" value
)))
1994 (defun whitespace-mark-x (nchars condition
)
1995 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1996 (forward-char nchars
)
1997 (insert (if condition
"X" " ")))
2000 (defun whitespace-insert-option-mark (the-list the-value
)
2001 "Insert the option mark ('X' or ' ') in toggle options buffer."
2002 (goto-char (point-min))
2004 (dolist (sym the-list
)
2005 (if (eq sym
'help-newline
)
2008 (whitespace-mark-x 2 (memq sym the-value
)))))
2011 (defun whitespace-help-on (style)
2012 "Display the whitespace toggle options."
2013 (unless (get-buffer whitespace-help-buffer-name
)
2014 (delete-other-windows)
2015 (let ((buffer (get-buffer-create whitespace-help-buffer-name
)))
2016 (with-current-buffer buffer
2018 (insert whitespace-help-text
)
2019 (whitespace-insert-option-mark
2020 whitespace-style-value-list style
)
2021 (whitespace-display-window buffer
)))))
2024 (defun whitespace-display-window (buffer)
2025 "Display BUFFER in a new window."
2026 (goto-char (point-min))
2027 (set-buffer-modified-p nil
)
2028 (when (< (window-height) (* 2 window-min-height
))
2029 (kill-buffer buffer
)
2030 (error "Window height is too small; \
2031 can't split window to display whitespace toggle options"))
2032 (let ((win (split-window)))
2033 (set-window-buffer win buffer
)
2034 (shrink-window-if-larger-than-buffer win
)))
2037 (defun whitespace-kill-buffer (buffer-name)
2038 "Kill buffer BUFFER-NAME and windows related with it."
2039 (let ((buffer (get-buffer buffer-name
)))
2041 (delete-windows-on buffer
)
2042 (kill-buffer buffer
))))
2045 (defun whitespace-help-off ()
2046 "Remove the buffer and window of the whitespace toggle options."
2047 (whitespace-kill-buffer whitespace-help-buffer-name
))
2050 (defun whitespace-help-scroll (&optional up
)
2051 "Scroll help window, if it exists.
2053 If UP is non-nil, scroll up; otherwise, scroll down."
2055 (let ((buffer (get-buffer whitespace-help-buffer-name
)))
2057 (with-selected-window (get-buffer-window buffer
)
2064 ;; just ignore error
2068 (defun whitespace-interactive-char (local-p)
2069 "Interactive function to read a char and return a symbol.
2071 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2072 uses a global context.
2074 It accepts one of the following chars:
2078 f toggle face visualization
2079 t toggle TAB visualization
2080 s toggle SPACE and HARD SPACE visualization
2081 r toggle trailing blanks visualization
2082 l toggle \"long lines\" visualization
2083 L toggle \"long lines\" tail visualization
2084 n toggle NEWLINE visualization
2085 e toggle empty line at bob and/or eob visualization
2086 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2087 I toggle indentation SPACEs visualization
2088 i toggle indentation TABs visualization
2089 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2090 A toggle SPACEs after TAB: SPACEs visualization
2091 a toggle SPACEs after TAB: TABs visualization
2092 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2093 B toggle SPACEs before TAB: SPACEs visualization
2094 b toggle SPACEs before TAB: TABs visualization
2097 T toggle TAB visualization
2098 S toggle SPACE and HARD SPACE visualization
2099 N toggle NEWLINE visualization
2101 x restore `whitespace-style' value
2102 ? display brief help
2104 See also `whitespace-toggle-option-alist'."
2105 (let* ((is-off (not (if local-p
2107 global-whitespace-mode
)))
2108 (style (cond (is-off whitespace-style
) ; use default value
2109 (local-p whitespace-active-style
)
2110 (t whitespace-toggle-style
)))
2112 (format "Whitespace Toggle %s (type ? for further options)-"
2113 (if local-p
"Local" "Global")))
2115 ;; read a valid option and get the corresponding symbol
2116 (save-window-excursion
2117 (condition-case data
2122 (setq ch
(read-char prompt
))
2126 (assq ch whitespace-toggle-option-alist
)))))
2129 ((eq ch ?
\?) (whitespace-help-on style
))
2130 ((eq ch ?\
) (whitespace-help-scroll t
))
2131 ((eq ch ?\M-
) (whitespace-help-scroll))
2132 ((eq ch ?
>) (whitespace-help-scroll t
))
2133 ((eq ch ?
<) (whitespace-help-scroll))
2135 (whitespace-help-off)
2136 (message " ")) ; clean echo area
2139 (whitespace-help-off)
2140 (error (error-message-string data
)))))
2141 (list sym
))) ; return the appropriate symbol
2144 (defun whitespace-toggle-list (local-p arg the-list
)
2145 "Toggle options in THE-LIST based on list ARG.
2147 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2148 uses a global context.
2150 ARG is a list of options to be toggled.
2152 THE-LIST is a list of options. This list will be toggled and the
2153 resultant list will be returned."
2154 (unless (if local-p whitespace-mode global-whitespace-mode
)
2155 (setq the-list whitespace-style
))
2156 (setq the-list
(copy-sequence the-list
)) ; keep original list
2157 (dolist (sym (if (listp arg
) arg
(list arg
)))
2159 ;; ignore help value
2160 ((eq sym
'help-newline
))
2161 ;; restore default values
2162 ((eq sym
'whitespace-style
)
2163 (setq the-list whitespace-style
))
2164 ;; toggle valid values
2165 ((memq sym whitespace-style-value-list
)
2166 (setq the-list
(if (memq sym the-list
)
2168 (cons sym the-list
))))))
2172 (defvar whitespace-display-table nil
2173 "Used to save a local display table.")
2175 (defvar whitespace-display-table-was-local nil
2176 "Used to remember whether a buffer initially had a local display table.")
2179 (defun whitespace-turn-on ()
2180 "Turn on whitespace visualization."
2181 ;; prepare local hooks
2182 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
2183 ;; create whitespace local buffer environment
2184 (set (make-local-variable 'whitespace-font-lock-mode
) nil
)
2185 (set (make-local-variable 'whitespace-font-lock
) nil
)
2186 (set (make-local-variable 'whitespace-font-lock-keywords
) nil
)
2187 (set (make-local-variable 'whitespace-display-table
) nil
)
2188 (set (make-local-variable 'whitespace-display-table-was-local
) nil
)
2189 (set (make-local-variable 'whitespace-active-style
)
2190 (if (listp whitespace-style
)
2192 (list whitespace-style
)))
2193 (set (make-local-variable 'whitespace-indent-tabs-mode
)
2195 (set (make-local-variable 'whitespace-tab-width
)
2197 ;; turn on whitespace
2198 (when whitespace-active-style
2199 (whitespace-color-on)
2200 (whitespace-display-char-on)))
2203 (defun whitespace-turn-off ()
2204 "Turn off whitespace visualization."
2205 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
2206 (when whitespace-active-style
2207 (whitespace-color-off)
2208 (whitespace-display-char-off)))
2211 (defun whitespace-style-face-p ()
2212 "Return t if there is some visualization via face."
2213 (and (memq 'face whitespace-active-style
)
2214 (or (memq 'tabs whitespace-active-style
)
2215 (memq 'spaces whitespace-active-style
)
2216 (memq 'trailing whitespace-active-style
)
2217 (memq 'lines whitespace-active-style
)
2218 (memq 'lines-tail whitespace-active-style
)
2219 (memq 'newline whitespace-active-style
)
2220 (memq 'empty whitespace-active-style
)
2221 (memq 'indentation whitespace-active-style
)
2222 (memq 'indentation
::tab whitespace-active-style
)
2223 (memq 'indentation
::space whitespace-active-style
)
2224 (memq 'space-after-tab whitespace-active-style
)
2225 (memq 'space-after-tab
::tab whitespace-active-style
)
2226 (memq 'space-after-tab
::space whitespace-active-style
)
2227 (memq 'space-before-tab whitespace-active-style
)
2228 (memq 'space-before-tab
::tab whitespace-active-style
)
2229 (memq 'space-before-tab
::space whitespace-active-style
))))
2232 (defun whitespace-color-on ()
2233 "Turn on color visualization."
2234 (when (whitespace-style-face-p)
2235 (unless whitespace-font-lock
2236 (setq whitespace-font-lock t
2237 whitespace-font-lock-keywords
2238 (copy-sequence font-lock-keywords
)))
2239 ;; save current point and refontify when necessary
2240 (set (make-local-variable 'whitespace-point
)
2242 (set (make-local-variable 'whitespace-font-lock-refontify
)
2244 (set (make-local-variable 'whitespace-bob-marker
)
2246 (set (make-local-variable 'whitespace-eob-marker
)
2248 (set (make-local-variable 'whitespace-buffer-changed
)
2250 (add-hook 'post-command-hook
#'whitespace-post-command-hook nil t
)
2251 (add-hook 'before-change-functions
#'whitespace-buffer-changed nil t
)
2252 ;; turn off font lock
2253 (set (make-local-variable 'whitespace-font-lock-mode
)
2256 ;; add whitespace-mode color into font lock
2257 (when (memq 'spaces whitespace-active-style
)
2258 (font-lock-add-keywords
2262 (list whitespace-space-regexp
1 whitespace-space t
)
2264 (list whitespace-hspace-regexp
1 whitespace-hspace t
))
2266 (when (memq 'tabs whitespace-active-style
)
2267 (font-lock-add-keywords
2271 (list whitespace-tab-regexp
1 whitespace-tab t
))
2273 (when (memq 'trailing whitespace-active-style
)
2274 (font-lock-add-keywords
2277 ;; Show trailing blanks
2278 (list #'whitespace-trailing-regexp
1 whitespace-trailing t
))
2280 (when (or (memq 'lines whitespace-active-style
)
2281 (memq 'lines-tail whitespace-active-style
))
2282 (font-lock-add-keywords
2285 ;; Show "long" lines
2287 (let ((line-column (or whitespace-line-column fill-column
)))
2289 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2290 whitespace-tab-width
2291 (1- whitespace-tab-width
)
2292 (/ line-column whitespace-tab-width
)
2293 (let ((rem (% line-column whitespace-tab-width
)))
2296 (format ".\\{%d\\}" rem
)))))
2297 (if (memq 'lines whitespace-active-style
)
2303 ((memq 'space-before-tab whitespace-active-style
)
2304 (font-lock-add-keywords
2307 ;; Show SPACEs before TAB (indent-tabs-mode)
2308 (list whitespace-space-before-tab-regexp
2309 (if whitespace-indent-tabs-mode
1 2)
2310 whitespace-space-before-tab t
))
2312 ((memq 'space-before-tab
::tab whitespace-active-style
)
2313 (font-lock-add-keywords
2316 ;; Show SPACEs before TAB (SPACEs)
2317 (list whitespace-space-before-tab-regexp
2318 1 whitespace-space-before-tab t
))
2320 ((memq 'space-before-tab
::space whitespace-active-style
)
2321 (font-lock-add-keywords
2324 ;; Show SPACEs before TAB (TABs)
2325 (list whitespace-space-before-tab-regexp
2326 2 whitespace-space-before-tab t
))
2329 ((memq 'indentation whitespace-active-style
)
2330 (font-lock-add-keywords
2333 ;; Show indentation SPACEs (indent-tabs-mode)
2334 (list (whitespace-indentation-regexp)
2335 1 whitespace-indentation t
))
2337 ((memq 'indentation
::tab whitespace-active-style
)
2338 (font-lock-add-keywords
2341 ;; Show indentation SPACEs (SPACEs)
2342 (list (whitespace-indentation-regexp 'tab
)
2343 1 whitespace-indentation t
))
2345 ((memq 'indentation
::space whitespace-active-style
)
2346 (font-lock-add-keywords
2349 ;; Show indentation SPACEs (TABs)
2350 (list (whitespace-indentation-regexp 'space
)
2351 1 whitespace-indentation t
))
2353 (when (memq 'empty whitespace-active-style
)
2354 (font-lock-add-keywords
2357 ;; Show empty lines at beginning of buffer
2358 (list #'whitespace-empty-at-bob-regexp
2359 1 whitespace-empty t
))
2361 (font-lock-add-keywords
2364 ;; Show empty lines at end of buffer
2365 (list #'whitespace-empty-at-eob-regexp
2366 1 whitespace-empty t
))
2369 ((memq 'space-after-tab whitespace-active-style
)
2370 (font-lock-add-keywords
2373 ;; Show SPACEs after TAB (indent-tabs-mode)
2374 (list (whitespace-space-after-tab-regexp)
2375 1 whitespace-space-after-tab t
))
2377 ((memq 'space-after-tab
::tab whitespace-active-style
)
2378 (font-lock-add-keywords
2381 ;; Show SPACEs after TAB (SPACEs)
2382 (list (whitespace-space-after-tab-regexp 'tab
)
2383 1 whitespace-space-after-tab t
))
2385 ((memq 'space-after-tab
::space whitespace-active-style
)
2386 (font-lock-add-keywords
2389 ;; Show SPACEs after TAB (TABs)
2390 (list (whitespace-space-after-tab-regexp 'space
)
2391 1 whitespace-space-after-tab t
))
2393 ;; now turn on font lock and highlight blanks
2394 (font-lock-mode 1)))
2397 (defun whitespace-color-off ()
2398 "Turn off color visualization."
2399 ;; turn off font lock
2400 (when (whitespace-style-face-p)
2402 (remove-hook 'post-command-hook
#'whitespace-post-command-hook t
)
2403 (remove-hook 'before-change-functions
#'whitespace-buffer-changed t
)
2404 (when whitespace-font-lock
2405 (setq whitespace-font-lock nil
2406 font-lock-keywords whitespace-font-lock-keywords
))
2407 ;; restore original font lock state
2408 (font-lock-mode whitespace-font-lock-mode
)))
2411 (defun whitespace-trailing-regexp (limit)
2412 "Match trailing spaces which do not contain the point at end of line."
2414 (while (if (re-search-forward whitespace-trailing-regexp limit t
)
2416 (= whitespace-point
(match-end 1))) ;; loop if point at eol
2417 (setq status nil
))) ;; end of buffer
2421 (defun whitespace-empty-at-bob-regexp (limit)
2422 "Match spaces at beginning of buffer which do not contain the point at \
2423 beginning of buffer."
2429 (setq r
(and (/= whitespace-point
1)
2430 (looking-at whitespace-empty-at-bob-regexp
)))
2432 (set-marker whitespace-bob-marker
(match-end 1))
2433 (set-marker whitespace-bob-marker b
)))
2434 ;; inside bob empty region
2435 ((<= limit whitespace-bob-marker
)
2436 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2438 (when (< (match-end 1) limit
)
2439 (set-marker whitespace-bob-marker
(match-end 1)))
2440 (set-marker whitespace-bob-marker b
)))
2441 ;; intersection with end of bob empty region
2442 ((<= b whitespace-bob-marker
)
2443 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2445 (set-marker whitespace-bob-marker
(match-end 1))
2446 (set-marker whitespace-bob-marker b
)))
2447 ;; it is not inside bob empty region
2450 ;; move to end of matching
2451 (and r
(goto-char (match-end 1)))
2455 (defsubst whitespace-looking-back
(regexp limit
)
2457 (when (/= 0 (skip-chars-backward " \t\n" limit
))
2460 (looking-at regexp
))))
2463 (defun whitespace-empty-at-eob-regexp (limit)
2464 "Match spaces at end of buffer which do not contain the point at end of \
2467 (e (1+ (buffer-size)))
2472 (when (/= whitespace-point e
)
2474 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp 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 ;; inside eob empty region
2480 ((>= b whitespace-eob-marker
)
2482 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2484 (when (> (match-beginning 1) b
)
2485 (set-marker whitespace-eob-marker
(match-beginning 1)))
2486 (set-marker whitespace-eob-marker limit
)
2487 (goto-char b
))) ; return back to initial position
2488 ;; intersection with beginning of eob empty region
2489 ((>= limit whitespace-eob-marker
)
2491 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2493 (set-marker whitespace-eob-marker
(match-beginning 1))
2494 (set-marker whitespace-eob-marker limit
)
2495 (goto-char b
))) ; return back to initial position
2496 ;; it is not inside eob empty region
2502 (defun whitespace-buffer-changed (_beg _end
)
2503 "Set `whitespace-buffer-changed' variable to t."
2504 (setq whitespace-buffer-changed t
))
2507 (defun whitespace-post-command-hook ()
2508 "Save current point into `whitespace-point' variable.
2509 Also refontify when necessary."
2510 (setq whitespace-point
(point)) ; current point position
2513 ;; it is at end of line ...
2515 ;; ... with trailing SPACE or TAB
2516 (or (= (preceding-char) ?\
)
2517 (= (preceding-char) ?
\t)))
2518 ;; it is at beginning of buffer (bob)
2519 (= whitespace-point
1)
2520 ;; the buffer was modified and ...
2521 (and whitespace-buffer-changed
2523 ;; ... or inside bob whitespace region
2524 (<= whitespace-point whitespace-bob-marker
)
2525 ;; ... or at bob whitespace region border
2526 (and (= whitespace-point
(1+ whitespace-bob-marker
))
2527 (= (preceding-char) ?
\n))))
2528 ;; it is at end of buffer (eob)
2529 (= whitespace-point
(1+ (buffer-size)))
2530 ;; the buffer was modified and ...
2531 (and whitespace-buffer-changed
2533 ;; ... or inside eob whitespace region
2534 (>= whitespace-point whitespace-eob-marker
)
2535 ;; ... or at eob whitespace region border
2536 (and (= whitespace-point
(1- whitespace-eob-marker
))
2537 (= (following-char) ?
\n)))))))
2538 (when (or refontify
(> whitespace-font-lock-refontify
0))
2539 (setq whitespace-buffer-changed nil
)
2540 ;; adjust refontify counter
2541 (setq whitespace-font-lock-refontify
2544 (1- whitespace-font-lock-refontify
)))
2546 (jit-lock-refontify))))
2549 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2550 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2553 (defun whitespace-style-mark-p ()
2554 "Return t if there is some visualization via display table."
2555 (or (memq 'tab-mark whitespace-active-style
)
2556 (memq 'space-mark whitespace-active-style
)
2557 (memq 'newline-mark whitespace-active-style
)))
2560 (defsubst whitespace-char-valid-p
(char)
2561 ;; This check should be improved!!!
2566 (defun whitespace-display-vector-p (vec)
2567 "Return true if every character in vector VEC can be displayed."
2568 (let ((i (length vec
)))
2570 (while (and (>= (setq i
(1- i
)) 0)
2571 (whitespace-char-valid-p (aref vec i
))))
2575 (defun whitespace-display-char-on ()
2576 "Turn on character display mapping."
2577 (when (and whitespace-display-mappings
2578 (whitespace-style-mark-p))
2580 ;; Remember whether a buffer has a local display table.
2581 (unless whitespace-display-table-was-local
2582 (setq whitespace-display-table-was-local t
2583 whitespace-display-table
2584 (copy-sequence buffer-display-table
))
2585 ;; asure `buffer-display-table' is unique
2586 ;; when two or more windows are visible.
2587 (setq buffer-display-table
2588 (copy-sequence buffer-display-table
)))
2589 (unless buffer-display-table
2590 (setq buffer-display-table
(make-display-table)))
2591 (dolist (entry whitespace-display-mappings
)
2592 ;; check if it is to display this mark
2593 (when (memq (car entry
) whitespace-style
)
2594 ;; Get a displayable mapping.
2595 (setq vecs
(cddr entry
))
2597 (not (whitespace-display-vector-p (car vecs
))))
2598 (setq vecs
(cdr vecs
)))
2599 ;; Display a valid mapping.
2601 (setq vec
(copy-sequence (car vecs
)))
2603 (when (and (eq (cadr entry
) ?
\n)
2604 (memq 'newline whitespace-active-style
))
2605 ;; Only insert face bits on NEWLINE char mapping to avoid
2606 ;; obstruction of other faces like TABs and (HARD) SPACEs
2607 ;; faces, font-lock faces, etc.
2608 (dotimes (i (length vec
))
2609 (or (eq (aref vec i
) ?
\n)
2611 (make-glyph-code (aref vec i
)
2612 whitespace-newline
)))))
2614 (aset buffer-display-table
(cadr entry
) vec
)))))))
2617 (defun whitespace-display-char-off ()
2618 "Turn off character display mapping."
2619 (and whitespace-display-mappings
2620 (whitespace-style-mark-p)
2621 whitespace-display-table-was-local
2622 (setq whitespace-display-table-was-local nil
2623 buffer-display-table whitespace-display-table
)))
2626 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2630 (defun whitespace-action-when-on ()
2631 "Action to be taken always when local whitespace is turned on."
2632 (cond ((memq 'cleanup whitespace-action
)
2633 (whitespace-cleanup))
2634 ((memq 'report-on-bogus whitespace-action
)
2635 (whitespace-report nil t
))))
2638 (defun whitespace-write-file-hook ()
2639 "Action to be taken when buffer is written.
2640 It should be added buffer-locally to `write-file-functions'."
2641 (cond ((memq 'auto-cleanup whitespace-action
)
2642 (whitespace-cleanup))
2643 ((memq 'abort-on-bogus whitespace-action
)
2644 (when (whitespace-report nil t
)
2645 (error "Abort write due to whitespace problems in %s"
2647 nil
) ; continue hook processing
2650 (defun whitespace-warn-read-only (msg)
2651 "Warn if buffer is read-only."
2652 (when (memq 'warn-if-read-only whitespace-action
)
2653 (message "Can't %s: %s is read-only" msg
(buffer-name))))
2656 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2659 (defun whitespace-unload-function ()
2660 "Unload the whitespace library."
2661 (global-whitespace-mode -
1)
2662 ;; be sure all local whitespace mode is turned off
2663 (save-current-buffer
2664 (dolist (buf (buffer-list))
2666 (whitespace-mode -
1)))
2667 nil
) ; continue standard unloading
2670 (provide 'whitespace
)
2673 (run-hooks 'whitespace-load-hook
)
2676 ;;; whitespace.el ends here