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