1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000-2014 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 mode line).
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on mode line).
173 ;; `whitespace-report'
174 ;; Report some blank problems in buffer.
176 ;; `whitespace-report-region'
177 ;; Report some blank problems in a region.
179 ;; `whitespace-cleanup'
180 ;; Cleanup some blank problems in all buffer or at region.
182 ;; `whitespace-cleanup-region'
183 ;; Cleanup some blank problems at region.
185 ;; The problems, which are cleaned up, are:
187 ;; 1. empty lines at beginning of buffer.
188 ;; 2. empty lines at end of buffer.
189 ;; If `whitespace-style' includes the value `empty', remove all
190 ;; empty lines at beginning and/or end of buffer.
192 ;; 3. 8 or more SPACEs at beginning of line.
193 ;; If `whitespace-style' includes the value `indentation':
194 ;; replace 8 or more SPACEs at beginning of line by TABs, if
195 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
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 Tim O'Callaghan (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 cleaning up a buffer. See `whitespace-cleanup' and
529 `whitespace-cleanup-region' for documentation.
531 See also `whitespace-display-mappings' for documentation."
532 :type
'(repeat :tag
"Kind of Blank"
533 (choice :tag
"Kind of Blank Face"
534 (const :tag
"(Face) Face visualization"
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
)))
557 (defvar whitespace-space
'whitespace-space
558 "Symbol face used to visualize SPACE.
559 Used when `whitespace-style' includes the value `spaces'.")
560 (make-obsolete-variable 'whitespace-space
"use the face instead." "24.4")
563 (defface whitespace-space
564 '((((class color
) (background dark
))
565 :background
"grey20" :foreground
"darkgray")
566 (((class color
) (background light
))
567 :background
"LightYellow" :foreground
"lightgray")
568 (t :inverse-video t
))
569 "Face used to visualize SPACE."
573 (defvar whitespace-hspace
'whitespace-hspace
574 "Symbol face used to visualize HARD SPACE.
575 Used when `whitespace-style' includes the value `spaces'.")
576 (make-obsolete-variable 'whitespace-hspace
"use the face instead." "24.4")
578 (defface whitespace-hspace
; 'nobreak-space
579 '((((class color
) (background dark
))
580 :background
"grey24" :foreground
"darkgray")
581 (((class color
) (background light
))
582 :background
"LemonChiffon3" :foreground
"lightgray")
583 (t :inverse-video t
))
584 "Face used to visualize HARD SPACE."
588 (defvar whitespace-tab
'whitespace-tab
589 "Symbol face used to visualize TAB.
590 Used when `whitespace-style' includes the value `tabs'.")
591 (make-obsolete-variable 'whitespace-tab
"use the face instead." "24.4")
593 (defface whitespace-tab
594 '((((class color
) (background dark
))
595 :background
"grey22" :foreground
"darkgray")
596 (((class color
) (background light
))
597 :background
"beige" :foreground
"lightgray")
598 (t :inverse-video t
))
599 "Face used to visualize TAB."
603 (defvar whitespace-newline
'whitespace-newline
604 "Symbol face used to visualize NEWLINE char mapping.
605 See `whitespace-display-mappings'.
606 Used when `whitespace-style' includes the values `newline-mark'
608 (make-obsolete-variable 'whitespace-newline
"use the face instead." "24.4")
610 (defface whitespace-newline
611 '((default :weight normal
)
612 (((class color
) (background dark
)) :foreground
"darkgray")
613 (((class color
) (min-colors 88) (background light
)) :foreground
"lightgray")
614 ;; Displays with 16 colors use lightgray as background, so using a
615 ;; lightgray foreground makes the newline mark invisible.
616 (((class color
) (background light
)) :foreground
"brown")
618 "Face used to visualize NEWLINE char mapping.
620 See `whitespace-display-mappings'."
624 (defvar whitespace-trailing
'whitespace-trailing
625 "Symbol face used to visualize trailing blanks.
626 Used when `whitespace-style' includes the value `trailing'.")
627 (make-obsolete-variable 'whitespace-trailing
"use the face instead." "24.4")
629 (defface whitespace-trailing
; 'trailing-whitespace
630 '((default :weight bold
)
631 (((class mono
)) :inverse-video t
:underline t
)
632 (t :background
"red1" :foreground
"yellow"))
633 "Face used to visualize trailing blanks."
637 (defvar whitespace-line
'whitespace-line
638 "Symbol face used to visualize \"long\" lines.
639 See `whitespace-line-column'.
640 Used when `whitespace-style' includes the value `line'.")
641 (make-obsolete-variable 'whitespace-line
"use the face instead." "24.4")
643 (defface whitespace-line
644 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
645 (t :background
"gray20" :foreground
"violet"))
646 "Face used to visualize \"long\" lines.
648 See `whitespace-line-column'."
652 (defvar whitespace-space-before-tab
'whitespace-space-before-tab
653 "Symbol face used to visualize SPACEs before TAB.
654 Used when `whitespace-style' includes the value `space-before-tab'.")
655 (make-obsolete-variable 'whitespace-space-before-tab
656 "use the face instead." "24.4")
658 (defface whitespace-space-before-tab
659 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
660 (t :background
"DarkOrange" :foreground
"firebrick"))
661 "Face used to visualize SPACEs before TAB."
665 (defvar whitespace-indentation
'whitespace-indentation
666 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
667 Used when `whitespace-style' includes the value `indentation'.")
668 (make-obsolete-variable 'whitespace-indentation
"use the face instead." "24.4")
670 (defface whitespace-indentation
671 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
672 (t :background
"yellow" :foreground
"firebrick"))
673 "Face used to visualize 8 or more SPACEs at beginning of line."
677 (defvar whitespace-empty
'whitespace-empty
678 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
679 Used when `whitespace-style' includes the value `empty'.")
680 (make-obsolete-variable 'whitespace-empty
"use the face instead." "24.4")
682 (defface whitespace-empty
683 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
684 (t :background
"yellow" :foreground
"firebrick"))
685 "Face used to visualize empty lines at beginning and/or end of buffer."
689 (defvar whitespace-space-after-tab
'whitespace-space-after-tab
690 "Symbol face used to visualize 8 or more SPACEs after TAB.
691 Used when `whitespace-style' includes the value `space-after-tab'.")
692 (make-obsolete-variable 'whitespace-space-after-tab
693 "use the face instead." "24.4")
695 (defface whitespace-space-after-tab
696 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
697 (t :background
"yellow" :foreground
"firebrick"))
698 "Face used to visualize 8 or more SPACEs after TAB."
702 (defcustom whitespace-hspace-regexp
704 "Specify HARD SPACE characters regexp.
706 Here are some examples:
708 \"\\\\(^\\xA0+\\\\)\" \
709 visualize only leading HARD SPACEs.
710 \"\\\\(\\xA0+$\\\\)\" \
711 visualize only trailing HARD SPACEs.
712 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
713 visualize leading and/or trailing HARD SPACEs.
714 \"\\t\\\\(\\xA0+\\\\)\\t\" \
715 visualize only HARD SPACEs between TABs.
717 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
718 Use exactly one pair of enclosing \\\\( and \\\\).
720 Used when `whitespace-style' includes `spaces'."
721 :type
'(regexp :tag
"HARD SPACE Chars")
725 (defcustom whitespace-space-regexp
"\\( +\\)"
726 "Specify SPACE characters regexp.
728 If you're using `mule' package, there may be other characters
729 besides \" \" that should be considered SPACE.
731 Here are some examples:
733 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
734 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
735 \"\\\\(^ +\\\\| +$\\\\)\" \
736 visualize leading and/or trailing SPACEs.
737 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
739 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
740 Use exactly one pair of enclosing \\\\( and \\\\).
742 Used when `whitespace-style' includes `spaces'."
743 :type
'(regexp :tag
"SPACE Chars")
747 (defcustom whitespace-tab-regexp
"\\(\t+\\)"
748 "Specify TAB characters regexp.
750 If you're using `mule' package, there may be other characters
751 besides \"\\t\" that should be considered TAB.
753 Here are some examples:
755 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
756 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
757 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
758 visualize leading and/or trailing TABs.
759 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
761 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
762 Use exactly one pair of enclosing \\\\( and \\\\).
764 Used when `whitespace-style' includes `tabs'."
765 :type
'(regexp :tag
"TAB Chars")
769 (defcustom whitespace-trailing-regexp
770 "\\([\t \u00A0]+\\)$"
771 "Specify trailing characters regexp.
773 There may be other characters besides:
775 \" \" \"\\t\" \"\\u00A0\"
777 that should be considered blank.
779 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
780 Use exactly one pair of enclosing elements above.
782 Used when `whitespace-style' includes `trailing'."
783 :type
'(regexp :tag
"Trailing Chars")
787 (defcustom whitespace-space-before-tab-regexp
"\\( +\\)\\(\t+\\)"
788 "Specify SPACEs before TAB regexp.
790 Used when `whitespace-style' includes `space-before-tab',
791 `space-before-tab::tab' or `space-before-tab::space'."
792 :type
'(regexp :tag
"SPACEs Before TAB")
796 (defcustom whitespace-indentation-regexp
797 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
798 .
"^ *\\(\t+\\)[^\n]")
799 "Specify regexp for 8 or more SPACEs at beginning of line.
801 It is a cons where the cons car is used for SPACEs visualization
802 and the cons cdr is used for TABs visualization.
804 Used when `whitespace-style' includes `indentation',
805 `indentation::tab' or `indentation::space'."
806 :type
'(cons (string :tag
"Indentation SPACEs")
807 (string :tag
"Indentation TABs"))
811 (defcustom whitespace-empty-at-bob-regexp
"^\\(\\([ \t]*\n\\)+\\)"
812 "Specify regexp for empty lines at beginning of buffer.
814 Used when `whitespace-style' includes `empty'."
815 :type
'(regexp :tag
"Empty Lines At Beginning Of Buffer")
819 (defcustom whitespace-empty-at-eob-regexp
"^\\([ \t\n]+\\)"
820 "Specify regexp for empty lines at end of buffer.
822 Used when `whitespace-style' includes `empty'."
823 :type
'(regexp :tag
"Empty Lines At End Of Buffer")
827 (defcustom whitespace-space-after-tab-regexp
828 '("\t+\\(\\( \\{%d\\}\\)+\\)"
830 "Specify regexp for 8 or more SPACEs after TAB.
832 It is a cons where the cons car is used for SPACEs visualization
833 and the cons cdr is used for TABs visualization.
835 Used when `whitespace-style' includes `space-after-tab',
836 `space-after-tab::tab' or `space-after-tab::space'."
837 :type
'(cons (string :tag
"SPACEs After TAB")
842 (defcustom whitespace-line-column
80
843 "Specify column beyond which the line is highlighted.
845 It must be an integer or nil. If nil, the `fill-column' variable value is
848 Used when `whitespace-style' includes `lines' or `lines-tail'."
849 :type
'(choice :tag
"Line Length Limit"
850 (integer :tag
"Line Length")
851 (const :tag
"Use fill-column" nil
))
855 ;; Hacked from `visible-whitespace-mappings' in visws.el
856 (defcustom whitespace-display-mappings
858 (space-mark ?\
[?
\u00B7] [?.
]) ; space - centered dot
859 (space-mark ?
\xA0 [?
\u00A4] [?_
]) ; hard space - currency
860 ;; NEWLINE is displayed using the face `whitespace-newline'
861 (newline-mark ?
\n [?$ ?
\n]) ; eol - dollar sign
862 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
863 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
864 ;; (newline-mark ?\n [?\u00AF ?\n] [?$ ?\n]) ; eol - overscore
865 ;; (newline-mark ?\n [?\u00AC ?\n] [?$ ?\n]) ; eol - negation
866 ;; (newline-mark ?\n [?\u00B0 ?\n] [?$ ?\n]) ; eol - degrees
868 ;; WARNING: the mapping below has a problem.
869 ;; When a TAB occupies exactly one column, it will display the
870 ;; character ?\xBB at that column followed by a TAB which goes to
871 ;; the next TAB column.
872 ;; If this is a problem for you, please, comment the line below.
873 (tab-mark ?
\t [?
\u00BB ?
\t] [?
\\ ?
\t]) ; tab - left quote mark
875 "Specify an alist of mappings for displaying characters.
877 Each element has the following form:
879 (KIND CHAR VECTOR...)
883 KIND is the kind of character.
884 It can be one of the following symbols:
886 tab-mark for TAB character
888 space-mark for SPACE or HARD SPACE character
890 newline-mark for NEWLINE character
892 CHAR is the character to be mapped.
894 VECTOR is a vector of characters to be displayed in place of CHAR.
895 The first display vector that can be displayed is used;
896 if no display vector for a mapping can be displayed, then
897 that character is displayed unmodified.
899 The NEWLINE character is displayed using the face given by
900 `whitespace-newline' variable.
902 Used when `whitespace-style' includes `tab-mark', `space-mark' or
905 (list :tag
"Character Mapping"
906 (choice :tag
"Char Kind"
907 (const :tag
"Tab" tab-mark
)
908 (const :tag
"Space" space-mark
)
909 (const :tag
"Newline" newline-mark
))
910 (character :tag
"Char")
911 (repeat :inline t
:tag
"Vector List"
914 :tag
"Vector Characters"
915 (character :tag
"Char"))))))
919 (defcustom whitespace-global-modes t
920 "Modes for which global `whitespace-mode' is automagically turned on.
922 Global `whitespace-mode' is controlled by the command
923 `global-whitespace-mode'.
925 If nil, means no modes have `whitespace-mode' automatically
928 If t, all modes that support `whitespace-mode' have it
929 automatically turned on.
931 Else it should be a list of `major-mode' symbol names for which
932 `whitespace-mode' should be automatically turned on. The sense
933 of the list is negated if it begins with `not'. For example:
937 means that `whitespace-mode' is turned on for buffers in C and
939 :type
'(choice :tag
"Global Modes"
940 (const :tag
"None" nil
)
942 (set :menu-tag
"Mode Specific" :tag
"Modes"
944 (const :tag
"Except" not
)
946 (symbol :tag
"Mode"))))
950 (defcustom whitespace-action nil
951 "Specify which action is taken when a buffer is visited or written.
953 It's a list containing some or all of the following values:
955 nil no action is taken.
957 cleanup cleanup any bogus whitespace always when local
958 whitespace is turned on.
959 See `whitespace-cleanup' and
960 `whitespace-cleanup-region'.
962 report-on-bogus report if there is any bogus whitespace always
963 when local whitespace is turned on.
965 auto-cleanup cleanup any bogus whitespace when buffer is
967 See `whitespace-cleanup' and
968 `whitespace-cleanup-region'.
970 abort-on-bogus abort if there is any bogus whitespace and the
973 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
974 is included in `whitespace-action' and the
977 Any other value is treated as nil."
978 :type
'(choice :tag
"Actions"
979 (const :tag
"None" nil
)
980 (repeat :tag
"Action List"
981 (choice :tag
"Action"
982 (const :tag
"Cleanup When On" cleanup
)
983 (const :tag
"Report On Bogus" report-on-bogus
)
984 (const :tag
"Auto Cleanup" auto-cleanup
)
985 (const :tag
"Abort On Bogus" abort-on-bogus
)
986 (const :tag
"Warn If Read-Only" warn-if-read-only
))))
990 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
991 ;;;; User commands - Local mode
995 (define-minor-mode whitespace-mode
996 "Toggle whitespace visualization (Whitespace mode).
997 With a prefix argument ARG, enable Whitespace mode if ARG is
998 positive, and disable it otherwise. If called from Lisp, enable
999 the mode if ARG is omitted or nil.
1001 See also `whitespace-style', `whitespace-newline' and
1002 `whitespace-display-mappings'."
1008 (noninteractive ; running a batch job
1009 (setq whitespace-mode nil
))
1010 (whitespace-mode ; whitespace-mode on
1011 (whitespace-turn-on)
1012 (whitespace-action-when-on))
1013 (t ; whitespace-mode off
1014 (whitespace-turn-off))))
1018 (define-minor-mode whitespace-newline-mode
1019 "Toggle newline visualization (Whitespace Newline mode).
1020 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1021 is positive, and disable it otherwise. If called from Lisp,
1022 enable the mode if ARG is omitted or nil.
1024 Use `whitespace-newline-mode' only for NEWLINE visualization
1025 exclusively. For other visualizations, including NEWLINE
1026 visualization together with (HARD) SPACEs and/or TABs, please,
1027 use `whitespace-mode'.
1029 See also `whitespace-newline' and `whitespace-display-mappings'."
1034 (let ((whitespace-style '(face newline-mark newline
)))
1035 (whitespace-mode (if whitespace-newline-mode
1037 ;; sync states (running a batch job)
1038 (setq whitespace-newline-mode whitespace-mode
))
1041 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1042 ;;;; User commands - Global mode
1046 (define-minor-mode global-whitespace-mode
1047 "Toggle whitespace visualization globally (Global Whitespace mode).
1048 With a prefix argument ARG, enable Global Whitespace mode if ARG
1049 is positive, and disable it otherwise. If called from Lisp,
1050 enable it if ARG is omitted or nil.
1052 See also `whitespace-style', `whitespace-newline' and
1053 `whitespace-display-mappings'."
1059 (noninteractive ; running a batch job
1060 (setq global-whitespace-mode nil
))
1061 (global-whitespace-mode ; global-whitespace-mode on
1062 (save-current-buffer
1063 (add-hook 'find-file-hook
'whitespace-turn-on-if-enabled
)
1064 (add-hook 'after-change-major-mode-hook
'whitespace-turn-on-if-enabled
)
1065 (dolist (buffer (buffer-list)) ; adjust all local mode
1067 (unless whitespace-mode
1068 (whitespace-turn-on-if-enabled)))))
1069 (t ; global-whitespace-mode off
1070 (save-current-buffer
1071 (remove-hook 'find-file-hook
'whitespace-turn-on-if-enabled
)
1072 (remove-hook 'after-change-major-mode-hook
'whitespace-turn-on-if-enabled
)
1073 (dolist (buffer (buffer-list)) ; adjust all local mode
1075 (unless whitespace-mode
1076 (whitespace-turn-off)))))))
1078 (defvar whitespace-enable-predicate
1081 ((eq whitespace-global-modes t
))
1082 ((listp whitespace-global-modes
)
1083 (if (eq (car-safe whitespace-global-modes
) 'not
)
1084 (not (memq major-mode
(cdr whitespace-global-modes
)))
1085 (memq major-mode whitespace-global-modes
)))
1087 ;; ...we have a display (not running a batch job)
1088 (not noninteractive
)
1089 ;; ...the buffer is not internal (name starts with a space)
1090 (not (eq (aref (buffer-name) 0) ?\
))
1091 ;; ...the buffer is not special (name starts with *)
1092 (or (not (eq (aref (buffer-name) 0) ?
*))
1093 ;; except the scratch buffer.
1094 (string= (buffer-name) "*scratch*"))))
1095 "Predicate to decide which buffers obey `global-whitespace-mode'.
1096 This function is called with no argument and should return non-nil
1097 if the current buffer should obey `global-whitespace-mode'.
1098 This variable is normally modified via `add-function'.")
1100 (defun whitespace-turn-on-if-enabled ()
1101 (when (funcall whitespace-enable-predicate
)
1102 (whitespace-turn-on)))
1105 (define-minor-mode global-whitespace-newline-mode
1106 "Toggle global newline visualization (Global Whitespace Newline mode).
1107 With a prefix argument ARG, enable Global Whitespace Newline mode
1108 if ARG is positive, and disable it otherwise. If called from
1109 Lisp, enable it if ARG is omitted or nil.
1111 Use `global-whitespace-newline-mode' only for NEWLINE
1112 visualization exclusively. For other visualizations, including
1113 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1114 please use `global-whitespace-mode'.
1116 See also `whitespace-newline' and `whitespace-display-mappings'."
1121 (let ((whitespace-style '(newline-mark newline
)))
1122 (global-whitespace-mode (if global-whitespace-newline-mode
1124 ;; sync states (running a batch job)
1125 (setq global-whitespace-newline-mode global-whitespace-mode
)))
1128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1129 ;;;; User commands - Toggle
1132 (defconst whitespace-style-value-list
1145 space-after-tab
::tab
1146 space-after-tab
::space
1148 space-before-tab
::tab
1149 space-before-tab
::space
1150 help-newline
; value used by `whitespace-insert-option-mark'
1155 "List of valid `whitespace-style' values.")
1158 (defconst whitespace-toggle-option-alist
1167 (?\C-i . indentation
)
1168 (?I . indentation
::tab
)
1169 (?i . indentation
::space
)
1170 (?\C-a . space-after-tab
)
1171 (?A . space-after-tab
::tab
)
1172 (?a . space-after-tab
::space
)
1173 (?\C-b . space-before-tab
)
1174 (?B . space-before-tab
::tab
)
1175 (?b . space-before-tab
::space
)
1179 (?x . whitespace-style
)
1181 "Alist of toggle options.
1183 Each element has the form:
1189 CHAR is a char which the user will have to type.
1191 SYMBOL is a valid symbol associated with CHAR.
1192 See `whitespace-style-value-list'.")
1195 (defvar whitespace-active-style nil
1196 "Used to save locally `whitespace-style' value.")
1198 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1199 "Used to save locally `indent-tabs-mode' value.")
1201 (defvar whitespace-tab-width tab-width
1202 "Used to save locally `tab-width' value.")
1204 (defvar whitespace-point
(point)
1205 "Used to save locally current point value.
1206 Used by function `whitespace-trailing-regexp' (which see).")
1208 (defvar whitespace-font-lock-refontify nil
1209 "Used to save locally the font-lock refontify state.
1210 Used by function `whitespace-post-command-hook' (which see).")
1212 (defvar whitespace-bob-marker nil
1213 "Used to save locally the bob marker value.
1214 Used by function `whitespace-post-command-hook' (which see).")
1216 (defvar whitespace-eob-marker nil
1217 "Used to save locally the eob marker value.
1218 Used by function `whitespace-post-command-hook' (which see).")
1220 (defvar whitespace-buffer-changed nil
1221 "Used to indicate locally if buffer changed.
1222 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1223 functions (which see).")
1227 (defun whitespace-toggle-options (arg)
1228 "Toggle local `whitespace-mode' options.
1230 If local whitespace-mode is off, toggle the option given by ARG
1231 and turn on local whitespace-mode.
1233 If local whitespace-mode is on, toggle the option given by ARG
1234 and restart local whitespace-mode.
1236 Interactively, it reads one of the following chars:
1240 f toggle face visualization
1241 t toggle TAB visualization
1242 s toggle SPACE and HARD SPACE visualization
1243 r toggle trailing blanks visualization
1244 l toggle \"long lines\" visualization
1245 L toggle \"long lines\" tail visualization
1246 n toggle NEWLINE visualization
1247 e toggle empty line at bob and/or eob visualization
1248 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1249 I toggle indentation SPACEs visualization
1250 i toggle indentation TABs visualization
1251 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1252 A toggle SPACEs after TAB: SPACEs visualization
1253 a toggle SPACEs after TAB: TABs visualization
1254 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1255 B toggle SPACEs before TAB: SPACEs visualization
1256 b toggle SPACEs before TAB: TABs visualization
1259 T toggle TAB visualization
1260 S toggle SPACEs before TAB visualization
1261 N toggle NEWLINE visualization
1263 x restore `whitespace-style' value
1264 ? display brief help
1266 Non-interactively, ARG should be a symbol or a list of symbols.
1267 The valid symbols are:
1269 face toggle face visualization
1270 tabs toggle TAB visualization
1271 spaces toggle SPACE and HARD SPACE visualization
1272 trailing toggle trailing blanks visualization
1273 lines toggle \"long lines\" visualization
1274 lines-tail toggle \"long lines\" tail visualization
1275 newline toggle NEWLINE visualization
1276 empty toggle empty line at bob and/or eob visualization
1277 indentation toggle indentation SPACEs visualization
1278 indentation::tab toggle indentation SPACEs visualization
1279 indentation::space toggle indentation TABs visualization
1280 space-after-tab toggle SPACEs after TAB visualization
1281 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1282 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1283 space-before-tab toggle SPACEs before TAB visualization
1284 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1285 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1287 tab-mark toggle TAB visualization
1288 space-mark toggle SPACEs before TAB visualization
1289 newline-mark toggle NEWLINE visualization
1291 whitespace-style restore `whitespace-style' value
1293 See `whitespace-style' and `indent-tabs-mode' for documentation."
1294 (interactive (whitespace-interactive-char t
))
1295 (let ((whitespace-style
1296 (whitespace-toggle-list t arg whitespace-active-style
)))
1298 (whitespace-mode 1)))
1301 (defvar whitespace-toggle-style nil
1302 "Used to toggle the global `whitespace-style' value.")
1306 (defun global-whitespace-toggle-options (arg)
1307 "Toggle global `whitespace-mode' options.
1309 If global whitespace-mode is off, toggle the option given by ARG
1310 and turn on global whitespace-mode.
1312 If global whitespace-mode is on, toggle the option given by ARG
1313 and restart global whitespace-mode.
1315 Interactively, it accepts one of the following chars:
1319 f toggle face visualization
1320 t toggle TAB visualization
1321 s toggle SPACE and HARD SPACE visualization
1322 r toggle trailing blanks visualization
1323 l toggle \"long lines\" visualization
1324 L toggle \"long lines\" tail visualization
1325 n toggle NEWLINE visualization
1326 e toggle empty line at bob and/or eob visualization
1327 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1328 I toggle indentation SPACEs visualization
1329 i toggle indentation TABs visualization
1330 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1331 A toggle SPACEs after TAB: SPACEs visualization
1332 a toggle SPACEs after TAB: TABs visualization
1333 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1334 B toggle SPACEs before TAB: SPACEs visualization
1335 b toggle SPACEs before TAB: TABs visualization
1338 T toggle TAB visualization
1339 S toggle SPACEs before TAB visualization
1340 N toggle NEWLINE visualization
1342 x restore `whitespace-style' value
1343 ? display brief help
1345 Non-interactively, ARG should be a symbol or a list of symbols.
1346 The valid symbols are:
1348 face toggle face visualization
1349 tabs toggle TAB visualization
1350 spaces toggle SPACE and HARD SPACE visualization
1351 trailing toggle trailing blanks visualization
1352 lines toggle \"long lines\" visualization
1353 lines-tail toggle \"long lines\" tail visualization
1354 newline toggle NEWLINE visualization
1355 empty toggle empty line at bob and/or eob visualization
1356 indentation toggle indentation SPACEs visualization
1357 indentation::tab toggle indentation SPACEs visualization
1358 indentation::space toggle indentation TABs visualization
1359 space-after-tab toggle SPACEs after TAB visualization
1360 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1361 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1362 space-before-tab toggle SPACEs before TAB visualization
1363 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1364 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1366 tab-mark toggle TAB visualization
1367 space-mark toggle SPACEs before TAB visualization
1368 newline-mark toggle NEWLINE visualization
1370 whitespace-style restore `whitespace-style' value
1372 See `whitespace-style' and `indent-tabs-mode' for documentation."
1373 (interactive (whitespace-interactive-char nil
))
1374 (let ((whitespace-style
1375 (whitespace-toggle-list nil arg whitespace-toggle-style
)))
1376 (setq whitespace-toggle-style whitespace-style
)
1377 (global-whitespace-mode 0)
1378 (global-whitespace-mode 1)))
1381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1382 ;;;; User commands - Cleanup
1386 (defun whitespace-cleanup ()
1387 "Cleanup some blank problems in all buffer or at region.
1389 It usually applies to the whole buffer, but in transient mark
1390 mode when the mark is active, it applies to the region. It also
1391 applies to the region when it is not in transient mark mode, the
1392 mark is active and \\[universal-argument] was pressed just before
1393 calling `whitespace-cleanup' interactively.
1395 See also `whitespace-cleanup-region'.
1397 The problems cleaned up are:
1399 1. empty lines at beginning of buffer.
1400 2. empty lines at end of buffer.
1401 If `whitespace-style' includes the value `empty', remove all
1402 empty lines at beginning and/or end of buffer.
1404 3. 8 or more SPACEs at beginning of line.
1405 If `whitespace-style' includes the value `indentation':
1406 replace 8 or more SPACEs at beginning of line by TABs, if
1407 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1409 If `whitespace-style' includes the value `indentation::tab',
1410 replace 8 or more SPACEs at beginning of line by TABs.
1411 If `whitespace-style' includes the value `indentation::space',
1412 replace TABs by SPACEs.
1414 4. SPACEs before TAB.
1415 If `whitespace-style' includes the value `space-before-tab':
1416 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1417 otherwise, replace TABs by SPACEs.
1418 If `whitespace-style' includes the value
1419 `space-before-tab::tab', replace SPACEs by TABs.
1420 If `whitespace-style' includes the value
1421 `space-before-tab::space', replace TABs by SPACEs.
1423 5. SPACEs or TABs at end of line.
1424 If `whitespace-style' includes the value `trailing', remove
1425 all SPACEs or TABs at end of line.
1427 6. 8 or more SPACEs after TAB.
1428 If `whitespace-style' includes the value `space-after-tab':
1429 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1430 otherwise, replace TABs by SPACEs.
1431 If `whitespace-style' includes the value
1432 `space-after-tab::tab', replace SPACEs by TABs.
1433 If `whitespace-style' includes the value
1434 `space-after-tab::space', replace TABs by SPACEs.
1436 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1442 (whitespace-warn-read-only "cleanup"))
1444 ((and (or transient-mark-mode
1447 ;; PROBLEMs 1 and 2 are not handled in region
1448 ;; PROBLEM 3: 8 or more SPACEs at bol
1449 ;; PROBLEM 4: SPACEs before TAB
1450 ;; PROBLEM 5: SPACEs or TABs at eol
1451 ;; PROBLEM 6: 8 or more SPACEs after TAB
1452 (whitespace-cleanup-region (region-beginning) (region-end)))
1456 (save-match-data ;FIXME: Why?
1457 ;; PROBLEM 1: empty lines at bob
1458 ;; PROBLEM 2: empty lines at eob
1459 ;; ACTION: remove all empty lines at bob and/or eob
1460 (when (memq 'empty whitespace-style
)
1461 (let (overwrite-mode) ; enforce no overwrite
1462 (goto-char (point-min))
1463 (when (looking-at whitespace-empty-at-bob-regexp
)
1464 (delete-region (match-beginning 1) (match-end 1)))
1465 (when (re-search-forward
1466 (concat whitespace-empty-at-eob-regexp
"\\'") nil t
)
1467 (delete-region (match-beginning 1) (match-end 1)))))))
1468 ;; PROBLEM 3: 8 or more SPACEs at bol
1469 ;; PROBLEM 4: SPACEs before TAB
1470 ;; PROBLEM 5: SPACEs or TABs at eol
1471 ;; PROBLEM 6: 8 or more SPACEs after TAB
1472 (whitespace-cleanup-region (point-min) (point-max)))))
1474 (defun whitespace-ensure-local-variables ()
1475 "Set `whitespace-indent-tabs-mode' and `whitespace-tab-width' locally."
1476 (set (make-local-variable 'whitespace-indent-tabs-mode
)
1478 (set (make-local-variable 'whitespace-tab-width
)
1482 (defun whitespace-cleanup-region (start end
)
1483 "Cleanup some blank problems at region.
1485 The problems cleaned up are:
1487 1. 8 or more SPACEs at beginning of line.
1488 If `whitespace-style' includes the value `indentation':
1489 replace 8 or more SPACEs at beginning of line by TABs, if
1490 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1492 If `whitespace-style' includes the value `indentation::tab',
1493 replace 8 or more SPACEs at beginning of line by TABs.
1494 If `whitespace-style' includes the value `indentation::space',
1495 replace TABs by SPACEs.
1497 2. SPACEs before TAB.
1498 If `whitespace-style' includes the value `space-before-tab':
1499 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1500 otherwise, replace TABs by SPACEs.
1501 If `whitespace-style' includes the value
1502 `space-before-tab::tab', replace SPACEs by TABs.
1503 If `whitespace-style' includes the value
1504 `space-before-tab::space', replace TABs by SPACEs.
1506 3. SPACEs or TABs at end of line.
1507 If `whitespace-style' includes the value `trailing', remove
1508 all SPACEs or TABs at end of line.
1510 4. 8 or more SPACEs after TAB.
1511 If `whitespace-style' includes the value `space-after-tab':
1512 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1513 otherwise, replace TABs by SPACEs.
1514 If `whitespace-style' includes the value
1515 `space-after-tab::tab', replace SPACEs by TABs.
1516 If `whitespace-style' includes the value
1517 `space-after-tab::space', replace TABs by SPACEs.
1519 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1522 (if buffer-read-only
1524 (whitespace-warn-read-only "cleanup region")
1525 ;; non-read-only buffer
1526 (whitespace-ensure-local-variables)
1527 (let ((rstart (min start end
))
1528 (rend (copy-marker (max start end
)))
1529 (indent-tabs-mode whitespace-indent-tabs-mode
)
1530 (tab-width whitespace-tab-width
)
1531 overwrite-mode
; enforce no overwrite
1534 (save-match-data ;FIXME: Why?
1535 ;; PROBLEM 1: 8 or more SPACEs at bol
1537 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1538 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1540 ((memq 'indentation whitespace-style
)
1541 (let ((regexp (whitespace-indentation-regexp)))
1543 (while (re-search-forward regexp rend t
)
1544 (setq tmp
(current-indentation))
1545 (goto-char (match-beginning 0))
1546 (delete-horizontal-space)
1549 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1550 ((memq 'indentation
::tab whitespace-style
)
1551 (whitespace-replace-action
1553 (whitespace-indentation-regexp 'tab
) 0))
1554 ;; ACTION: replace TABs by SPACEs.
1555 ((memq 'indentation
::space whitespace-style
)
1556 (whitespace-replace-action
1557 'untabify rstart rend
1558 (whitespace-indentation-regexp 'space
) 0)))
1559 ;; PROBLEM 3: SPACEs or TABs at eol
1560 ;; ACTION: remove all SPACEs or TABs at eol
1561 (when (memq 'trailing whitespace-style
)
1562 (whitespace-replace-action
1563 'delete-region rstart rend
1564 whitespace-trailing-regexp
1))
1565 ;; PROBLEM 4: 8 or more SPACEs after TAB
1567 ;; ACTION: replace 8 or more SPACEs by TABs, if
1568 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1570 ((memq 'space-after-tab whitespace-style
)
1571 (whitespace-replace-action
1572 (if whitespace-indent-tabs-mode
'tabify
'untabify
)
1573 rstart rend
(whitespace-space-after-tab-regexp) 1))
1574 ;; ACTION: replace 8 or more SPACEs by TABs.
1575 ((memq 'space-after-tab
::tab whitespace-style
)
1576 (whitespace-replace-action
1578 (whitespace-space-after-tab-regexp 'tab
) 1))
1579 ;; ACTION: replace TABs by SPACEs.
1580 ((memq 'space-after-tab
::space whitespace-style
)
1581 (whitespace-replace-action
1582 'untabify rstart rend
1583 (whitespace-space-after-tab-regexp 'space
) 1)))
1584 ;; PROBLEM 2: SPACEs before TAB
1586 ;; ACTION: replace SPACEs before TAB by TABs, if
1587 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1589 ((memq 'space-before-tab whitespace-style
)
1590 (whitespace-replace-action
1591 (if whitespace-indent-tabs-mode
'tabify
'untabify
)
1592 rstart rend whitespace-space-before-tab-regexp
1593 (if whitespace-indent-tabs-mode
0 2)))
1594 ;; ACTION: replace SPACEs before TAB by TABs.
1595 ((memq 'space-before-tab
::tab whitespace-style
)
1596 (whitespace-replace-action
1598 whitespace-space-before-tab-regexp
0))
1599 ;; ACTION: replace TABs by SPACEs.
1600 ((memq 'space-before-tab
::space whitespace-style
)
1601 (whitespace-replace-action
1602 'untabify rstart rend
1603 whitespace-space-before-tab-regexp
2)))))
1604 (set-marker rend nil
)))) ; point marker to nowhere
1607 (defun whitespace-replace-action (action rstart rend regexp index
)
1608 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1610 INDEX is the level group matched by REGEXP and used by ACTION.
1612 See also `tab-width'."
1614 (while (re-search-forward regexp rend t
)
1615 (goto-char (match-end index
))
1616 (funcall action
(match-beginning index
) (match-end index
))))
1619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1620 ;;;; User command - report
1623 (defun whitespace-regexp (regexp &optional kind
)
1624 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1627 whitespace-indent-tabs-mode
)
1628 (format (car regexp
) whitespace-tab-width
))
1629 ((or (eq kind
'space
)
1630 (not whitespace-indent-tabs-mode
))
1634 (defun whitespace-indentation-regexp (&optional kind
)
1635 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1636 (whitespace-regexp whitespace-indentation-regexp kind
))
1639 (defun whitespace-space-after-tab-regexp (&optional kind
)
1640 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1641 (whitespace-regexp whitespace-space-after-tab-regexp kind
))
1644 (defconst whitespace-report-list
1646 (cons 'empty whitespace-empty-at-bob-regexp
)
1647 (cons 'empty whitespace-empty-at-eob-regexp
)
1648 (cons 'trailing whitespace-trailing-regexp
)
1649 (cons 'indentation nil
)
1650 (cons 'indentation
::tab nil
)
1651 (cons 'indentation
::space nil
)
1652 (cons 'space-before-tab whitespace-space-before-tab-regexp
)
1653 (cons 'space-before-tab
::tab whitespace-space-before-tab-regexp
)
1654 (cons 'space-before-tab
::space whitespace-space-before-tab-regexp
)
1655 (cons 'space-after-tab nil
)
1656 (cons 'space-after-tab
::tab nil
)
1657 (cons 'space-after-tab
::space nil
)
1659 "List of whitespace bogus symbol and corresponding regexp.")
1662 (defconst whitespace-report-text
1663 '( ;; `indent-tabs-mode' has non-nil value
1667 Current Setting Whitespace Problem
1669 empty [] [] empty lines at beginning of buffer
1670 empty [] [] empty lines at end of buffer
1671 trailing [] [] SPACEs or TABs at end of line
1672 indentation [] [] 8 or more SPACEs at beginning of line
1673 indentation::tab [] [] 8 or more SPACEs at beginning of line
1674 indentation::space [] [] TABs at beginning of line
1675 space-before-tab [] [] SPACEs before TAB
1676 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1677 space-before-tab::space [] [] SPACEs before TAB: TABs
1678 space-after-tab [] [] 8 or more SPACEs after TAB
1679 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1680 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1684 .
;; `indent-tabs-mode' has nil value
1688 Current Setting Whitespace Problem
1690 empty [] [] empty lines at beginning of buffer
1691 empty [] [] empty lines at end of buffer
1692 trailing [] [] SPACEs or TABs at end of line
1693 indentation [] [] TABs at beginning of line
1694 indentation::tab [] [] 8 or more SPACEs at beginning of line
1695 indentation::space [] [] TABs at beginning of line
1696 space-before-tab [] [] SPACEs before TAB
1697 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1698 space-before-tab::space [] [] SPACEs before TAB: TABs
1699 space-after-tab [] [] 8 or more SPACEs after TAB
1700 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1701 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1705 "Text for whitespace bogus report.
1707 It is a cons of strings, where the car part is used when
1708 `indent-tabs-mode' is non-nil, and the cdr part is used when
1709 `indent-tabs-mode' is nil.")
1712 (defconst whitespace-report-buffer-name
"*Whitespace Report*"
1713 "The buffer name for whitespace bogus report.")
1717 (defun whitespace-report (&optional force report-if-bogus
)
1718 "Report some whitespace problems in buffer.
1720 Return nil if there is no whitespace problem; otherwise, return
1723 If FORCE is non-nil or \\[universal-argument] was pressed just
1724 before calling `whitespace-report' interactively, it forces
1725 `whitespace-style' to have:
1733 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1734 whitespace problems in buffer.
1736 Report if some of the following whitespace problems exist:
1738 * If `indent-tabs-mode' is non-nil:
1739 empty 1. empty lines at beginning of buffer.
1740 empty 2. empty lines at end of buffer.
1741 trailing 3. SPACEs or TABs at end of line.
1742 indentation 4. 8 or more SPACEs at beginning of line.
1743 space-before-tab 5. SPACEs before TAB.
1744 space-after-tab 6. 8 or more SPACEs after TAB.
1746 * If `indent-tabs-mode' is nil:
1747 empty 1. empty lines at beginning of buffer.
1748 empty 2. empty lines at end of buffer.
1749 trailing 3. SPACEs or TABs at end of line.
1750 indentation 4. TABS at beginning of line.
1751 space-before-tab 5. SPACEs before TAB.
1752 space-after-tab 6. 8 or more SPACEs after TAB.
1754 See `whitespace-style' for documentation.
1755 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1756 cleaning up these problems."
1757 (interactive (list current-prefix-arg
))
1758 (whitespace-report-region (point-min) (point-max)
1759 force report-if-bogus
))
1763 (defun whitespace-report-region (start end
&optional force report-if-bogus
)
1764 "Report some whitespace problems in a region.
1766 Return nil if there is no whitespace problem; otherwise, return
1769 If FORCE is non-nil or \\[universal-argument] was pressed just
1770 before calling `whitespace-report-region' interactively, it
1771 forces `whitespace-style' to have:
1779 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1780 whitespace problems in buffer.
1782 Report if some of the following whitespace problems exist:
1784 * If `indent-tabs-mode' is non-nil:
1785 empty 1. empty lines at beginning of buffer.
1786 empty 2. empty lines at end of buffer.
1787 trailing 3. SPACEs or TABs at end of line.
1788 indentation 4. 8 or more SPACEs at beginning of line.
1789 space-before-tab 5. SPACEs before TAB.
1790 space-after-tab 6. 8 or more SPACEs after TAB.
1792 * If `indent-tabs-mode' is nil:
1793 empty 1. empty lines at beginning of buffer.
1794 empty 2. empty lines at end of buffer.
1795 trailing 3. SPACEs or TABs at end of line.
1796 indentation 4. TABS at beginning of line.
1797 space-before-tab 5. SPACEs before TAB.
1798 space-after-tab 6. 8 or more SPACEs after TAB.
1800 See `whitespace-style' for documentation.
1801 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1802 cleaning up these problems."
1804 (setq force
(or current-prefix-arg force
))
1806 (save-match-data ;FIXME: Why?
1807 (let* ((has-bogus nil
)
1808 (rstart (min start end
))
1809 (rend (max start end
))
1814 (add-to-list 'whitespace-style
(car option
)))
1818 ((eq (car option
) 'indentation
)
1819 (whitespace-indentation-regexp))
1820 ((eq (car option
) 'indentation
::tab
)
1821 (whitespace-indentation-regexp 'tab
))
1822 ((eq (car option
) 'indentation
::space
)
1823 (whitespace-indentation-regexp 'space
))
1824 ((eq (car option
) 'space-after-tab
)
1825 (whitespace-space-after-tab-regexp))
1826 ((eq (car option
) 'space-after-tab
::tab
)
1827 (whitespace-space-after-tab-regexp 'tab
))
1828 ((eq (car option
) 'space-after-tab
::space
)
1829 (whitespace-space-after-tab-regexp 'space
))
1832 (and (re-search-forward regexp rend t
)
1833 (setq has-bogus t
))))
1834 whitespace-report-list
)))
1835 (when (if report-if-bogus has-bogus t
)
1836 (whitespace-kill-buffer whitespace-report-buffer-name
)
1837 ;; `whitespace-indent-tabs-mode' is local to current buffer
1838 ;; `whitespace-tab-width' is local to current buffer
1839 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode
)
1840 (ws-tab-width whitespace-tab-width
))
1841 (with-current-buffer (get-buffer-create
1842 whitespace-report-buffer-name
)
1844 (insert (if ws-indent-tabs-mode
1845 (car whitespace-report-text
)
1846 (cdr whitespace-report-text
)))
1847 (goto-char (point-min))
1849 (dolist (option whitespace-report-list
)
1852 27 (memq (car option
) whitespace-style
))
1853 (whitespace-mark-x 7 (car bogus-list
))
1854 (setq bogus-list
(cdr bogus-list
)))
1856 (whitespace-insert-value ws-indent-tabs-mode
)
1857 (whitespace-insert-value ws-tab-width
)
1859 (goto-char (point-max))
1860 (insert " Type `M-x whitespace-cleanup'"
1861 " to cleanup the buffer.\n\n"
1862 " Type `M-x whitespace-cleanup-region'"
1863 " to cleanup a region.\n\n"))
1864 (whitespace-display-window (current-buffer)))))
1868 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1869 ;;;; Internal functions
1872 (defvar whitespace-font-lock-keywords nil
1873 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1876 (defconst whitespace-help-text
1878 Whitespace Toggle Options | scroll up : SPC or > |
1879 | scroll down: M-SPC or < |
1880 FACES \\__________________________/
1881 [] f - toggle face visualization
1882 [] t - toggle TAB visualization
1883 [] s - toggle SPACE and HARD SPACE visualization
1884 [] r - toggle trailing blanks visualization
1885 [] l - toggle \"long lines\" visualization
1886 [] L - toggle \"long lines\" tail visualization
1887 [] n - toggle NEWLINE visualization
1888 [] e - toggle empty line at bob and/or eob visualization
1889 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1890 [] I - toggle indentation SPACEs visualization
1891 [] i - toggle indentation TABs visualization
1892 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1893 [] A - toggle SPACEs after TAB: SPACEs visualization
1894 [] a - toggle SPACEs after TAB: TABs visualization
1895 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1896 [] B - toggle SPACEs before TAB: SPACEs visualization
1897 [] b - toggle SPACEs before TAB: TABs visualization
1900 [] T - toggle TAB visualization
1901 [] S - toggle SPACE and HARD SPACE visualization
1902 [] N - toggle NEWLINE visualization
1904 x - restore `whitespace-style' value
1906 ? - display this text\n\n"
1907 "Text for whitespace toggle options.")
1910 (defconst whitespace-help-buffer-name
"*Whitespace Toggle Options*"
1911 "The buffer name for whitespace toggle options.")
1914 (defun whitespace-insert-value (value)
1915 "Insert VALUE at column 20 of next line."
1917 (move-to-column 20 t
)
1918 (insert (format "%s" value
)))
1921 (defun whitespace-mark-x (nchars condition
)
1922 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1923 (forward-char nchars
)
1924 (insert (if condition
"X" " ")))
1927 (defun whitespace-insert-option-mark (the-list the-value
)
1928 "Insert the option mark ('X' or ' ') in toggle options buffer."
1929 (goto-char (point-min))
1931 (dolist (sym the-list
)
1932 (if (eq sym
'help-newline
)
1935 (whitespace-mark-x 2 (memq sym the-value
)))))
1938 (defun whitespace-help-on (style)
1939 "Display the whitespace toggle options."
1940 (unless (get-buffer whitespace-help-buffer-name
)
1941 (delete-other-windows)
1942 (let ((buffer (get-buffer-create whitespace-help-buffer-name
)))
1943 (with-current-buffer buffer
1945 (insert whitespace-help-text
)
1946 (whitespace-insert-option-mark
1947 whitespace-style-value-list style
)
1948 (whitespace-display-window buffer
)))))
1951 (defun whitespace-display-window (buffer)
1952 "Display BUFFER in a new window."
1953 (goto-char (point-min))
1954 (set-buffer-modified-p nil
)
1955 (when (< (window-height) (* 2 window-min-height
))
1956 (kill-buffer buffer
)
1957 (error "Window height is too small; \
1958 can't split window to display whitespace toggle options"))
1959 (let ((win (split-window)))
1960 (set-window-buffer win buffer
)
1961 (shrink-window-if-larger-than-buffer win
)))
1964 (defun whitespace-kill-buffer (buffer-name)
1965 "Kill buffer BUFFER-NAME and windows related with it."
1966 (let ((buffer (get-buffer buffer-name
)))
1968 (delete-windows-on buffer
)
1969 (kill-buffer buffer
))))
1972 (defun whitespace-help-off ()
1973 "Remove the buffer and window of the whitespace toggle options."
1974 (whitespace-kill-buffer whitespace-help-buffer-name
))
1977 (defun whitespace-help-scroll (&optional up
)
1978 "Scroll help window, if it exists.
1980 If UP is non-nil, scroll up; otherwise, scroll down."
1982 (let ((buffer (get-buffer whitespace-help-buffer-name
)))
1984 (with-selected-window (get-buffer-window buffer
)
1991 ;; just ignore error
1995 (defun whitespace-interactive-char (local-p)
1996 "Interactive function to read a char and return a symbol.
1998 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1999 uses a global context.
2001 It accepts one of the following chars:
2005 f toggle face visualization
2006 t toggle TAB visualization
2007 s toggle SPACE and HARD SPACE visualization
2008 r toggle trailing blanks visualization
2009 l toggle \"long lines\" visualization
2010 L toggle \"long lines\" tail visualization
2011 n toggle NEWLINE visualization
2012 e toggle empty line at bob and/or eob visualization
2013 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2014 I toggle indentation SPACEs visualization
2015 i toggle indentation TABs visualization
2016 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2017 A toggle SPACEs after TAB: SPACEs visualization
2018 a toggle SPACEs after TAB: TABs visualization
2019 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2020 B toggle SPACEs before TAB: SPACEs visualization
2021 b toggle SPACEs before TAB: TABs visualization
2024 T toggle TAB visualization
2025 S toggle SPACE and HARD SPACE visualization
2026 N toggle NEWLINE visualization
2028 x restore `whitespace-style' value
2029 ? display brief help
2031 See also `whitespace-toggle-option-alist'."
2032 (let* ((is-off (not (if local-p
2034 global-whitespace-mode
)))
2035 (style (cond (is-off whitespace-style
) ; use default value
2036 (local-p whitespace-active-style
)
2037 (t whitespace-toggle-style
)))
2039 (format "Whitespace Toggle %s (type ? for further options)-"
2040 (if local-p
"Local" "Global")))
2042 ;; read a valid option and get the corresponding symbol
2043 (save-window-excursion
2044 (condition-case data
2049 (setq ch
(read-char prompt
))
2053 (assq ch whitespace-toggle-option-alist
)))))
2056 ((eq ch ?
\?) (whitespace-help-on style
))
2057 ((eq ch ?\
) (whitespace-help-scroll t
))
2058 ((eq ch ?\M-
) (whitespace-help-scroll))
2059 ((eq ch ?
>) (whitespace-help-scroll t
))
2060 ((eq ch ?
<) (whitespace-help-scroll))
2062 (whitespace-help-off)
2063 (message " ")) ; clean echo area
2066 (whitespace-help-off)
2067 (error (error-message-string data
)))))
2068 (list sym
))) ; return the appropriate symbol
2071 (defun whitespace-toggle-list (local-p arg the-list
)
2072 "Toggle options in THE-LIST based on list ARG.
2074 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2075 uses a global context.
2077 ARG is a list of options to be toggled.
2079 THE-LIST is a list of options. This list will be toggled and the
2080 resultant list will be returned."
2081 (unless (if local-p whitespace-mode global-whitespace-mode
)
2082 (setq the-list whitespace-style
))
2083 (setq the-list
(copy-sequence the-list
)) ; keep original list
2084 (dolist (sym (if (listp arg
) arg
(list arg
)))
2086 ;; ignore help value
2087 ((eq sym
'help-newline
))
2088 ;; restore default values
2089 ((eq sym
'whitespace-style
)
2090 (setq the-list whitespace-style
))
2091 ;; toggle valid values
2092 ((memq sym whitespace-style-value-list
)
2093 (setq the-list
(if (memq sym the-list
)
2095 (cons sym the-list
))))))
2099 (defvar whitespace-display-table nil
2100 "Used to save a local display table.")
2102 (defvar whitespace-display-table-was-local nil
2103 "Used to remember whether a buffer initially had a local display table.")
2105 (defun whitespace-turn-on ()
2106 "Turn on whitespace visualization."
2107 ;; prepare local hooks
2108 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
2109 ;; create whitespace local buffer environment
2110 (set (make-local-variable 'whitespace-font-lock-keywords
) nil
)
2111 (set (make-local-variable 'whitespace-display-table
) nil
)
2112 (set (make-local-variable 'whitespace-display-table-was-local
) nil
)
2113 (set (make-local-variable 'whitespace-active-style
)
2114 (if (listp whitespace-style
)
2116 (list whitespace-style
)))
2117 (whitespace-ensure-local-variables)
2118 ;; turn on whitespace
2119 (when whitespace-active-style
2120 (whitespace-color-on)
2121 (whitespace-display-char-on)))
2124 (defun whitespace-turn-off ()
2125 "Turn off whitespace visualization."
2126 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
2127 (when whitespace-active-style
2128 (whitespace-color-off)
2129 (whitespace-display-char-off)))
2132 (defun whitespace-style-face-p ()
2133 "Return t if there is some visualization via face."
2134 (and (memq 'face whitespace-active-style
)
2135 (or (memq 'tabs whitespace-active-style
)
2136 (memq 'spaces whitespace-active-style
)
2137 (memq 'trailing whitespace-active-style
)
2138 (memq 'lines whitespace-active-style
)
2139 (memq 'lines-tail whitespace-active-style
)
2140 (memq 'newline whitespace-active-style
)
2141 (memq 'empty whitespace-active-style
)
2142 (memq 'indentation whitespace-active-style
)
2143 (memq 'indentation
::tab whitespace-active-style
)
2144 (memq 'indentation
::space whitespace-active-style
)
2145 (memq 'space-after-tab whitespace-active-style
)
2146 (memq 'space-after-tab
::tab whitespace-active-style
)
2147 (memq 'space-after-tab
::space whitespace-active-style
)
2148 (memq 'space-before-tab whitespace-active-style
)
2149 (memq 'space-before-tab
::tab whitespace-active-style
)
2150 (memq 'space-before-tab
::space whitespace-active-style
))))
2153 (defun whitespace-color-on ()
2154 "Turn on color visualization."
2155 (when (whitespace-style-face-p)
2156 ;; save current point and refontify when necessary
2157 (set (make-local-variable 'whitespace-point
)
2159 (set (make-local-variable 'whitespace-font-lock-refontify
)
2161 (set (make-local-variable 'whitespace-bob-marker
)
2163 (set (make-local-variable 'whitespace-eob-marker
)
2165 (set (make-local-variable 'whitespace-buffer-changed
)
2167 (add-hook 'post-command-hook
#'whitespace-post-command-hook nil t
)
2168 (add-hook 'before-change-functions
#'whitespace-buffer-changed nil t
)
2169 ;; Add whitespace-mode color into font lock.
2171 whitespace-font-lock-keywords
2173 ,@(when (memq 'spaces whitespace-active-style
)
2175 `((,whitespace-space-regexp
1 whitespace-space t
)
2176 ;; Show HARD SPACEs.
2177 (,whitespace-hspace-regexp
1 whitespace-hspace t
)))
2178 ,@(when (memq 'tabs whitespace-active-style
)
2180 `((,whitespace-tab-regexp
1 whitespace-tab t
)))
2181 ,@(when (memq 'trailing whitespace-active-style
)
2182 ;; Show trailing blanks.
2183 `((,#'whitespace-trailing-regexp
1 whitespace-trailing t
)))
2184 ,@(when (or (memq 'lines whitespace-active-style
)
2185 (memq 'lines-tail whitespace-active-style
))
2186 ;; Show "long" lines.
2187 `((,(let ((line-column (or whitespace-line-column fill-column
)))
2189 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2190 whitespace-tab-width
2191 (1- whitespace-tab-width
)
2192 (/ line-column whitespace-tab-width
)
2193 (let ((rem (% line-column whitespace-tab-width
)))
2196 (format ".\\{%d\\}" rem
)))))
2197 ,(if (memq 'lines whitespace-active-style
)
2200 whitespace-line prepend
)))
2201 ,@(when (or (memq 'space-before-tab whitespace-active-style
)
2202 (memq 'space-before-tab
::tab whitespace-active-style
)
2203 (memq 'space-before-tab
::space whitespace-active-style
))
2204 `((,whitespace-space-before-tab-regexp
2206 ((memq 'space-before-tab whitespace-active-style
)
2207 ;; Show SPACEs before TAB (indent-tabs-mode).
2208 (if whitespace-indent-tabs-mode
1 2))
2209 ((memq 'space-before-tab
::tab whitespace-active-style
)
2211 ((memq 'space-before-tab
::space whitespace-active-style
)
2213 whitespace-space-before-tab t
)))
2214 ,@(when (or (memq 'indentation whitespace-active-style
)
2215 (memq 'indentation
::tab whitespace-active-style
)
2216 (memq 'indentation
::space whitespace-active-style
))
2218 ((memq 'indentation whitespace-active-style
)
2219 ;; Show indentation SPACEs (indent-tabs-mode).
2220 (whitespace-indentation-regexp))
2221 ((memq 'indentation
::tab whitespace-active-style
)
2222 ;; Show indentation SPACEs (SPACEs).
2223 (whitespace-indentation-regexp 'tab
))
2224 ((memq 'indentation
::space whitespace-active-style
)
2225 ;; Show indentation SPACEs (TABs).
2226 (whitespace-indentation-regexp 'space
)))
2227 1 whitespace-indentation t
)))
2228 ,@(when (memq 'empty whitespace-active-style
)
2229 ;; Show empty lines at beginning of buffer.
2230 `((,#'whitespace-empty-at-bob-regexp
2231 1 whitespace-empty t
)
2232 ;; Show empty lines at end of buffer.
2233 (,#'whitespace-empty-at-eob-regexp
2234 1 whitespace-empty t
)))
2235 ,@(when (or (memq 'space-after-tab whitespace-active-style
)
2236 (memq 'space-after-tab
::tab whitespace-active-style
)
2237 (memq 'space-after-tab
::space whitespace-active-style
))
2239 ((memq 'space-after-tab whitespace-active-style
)
2240 ;; Show SPACEs after TAB (indent-tabs-mode).
2241 (whitespace-space-after-tab-regexp))
2242 ((memq 'space-after-tab
::tab whitespace-active-style
)
2243 ;; Show SPACEs after TAB (SPACEs).
2244 (whitespace-space-after-tab-regexp 'tab
))
2245 ((memq 'space-after-tab
::space whitespace-active-style
)
2246 ;; Show SPACEs after TAB (TABs).
2247 (whitespace-space-after-tab-regexp 'space
)))
2248 1 whitespace-space-after-tab t
)))))
2249 (font-lock-add-keywords nil whitespace-font-lock-keywords t
)
2250 (when font-lock-mode
2251 (font-lock-fontify-buffer))))
2254 (defun whitespace-color-off ()
2255 "Turn off color visualization."
2256 ;; turn off font lock
2257 (when (whitespace-style-face-p)
2258 (remove-hook 'post-command-hook
#'whitespace-post-command-hook t
)
2259 (remove-hook 'before-change-functions
#'whitespace-buffer-changed t
)
2260 (font-lock-remove-keywords nil whitespace-font-lock-keywords
)
2261 (when font-lock-mode
2262 (font-lock-fontify-buffer))))
2265 (defun whitespace-trailing-regexp (limit)
2266 "Match trailing spaces which do not contain the point at end of line."
2268 (while (if (re-search-forward whitespace-trailing-regexp limit t
)
2269 (= whitespace-point
(match-end 1)) ;; loop if point at eol
2270 (setq status nil
))) ;; end of buffer
2274 (defun whitespace-empty-at-bob-regexp (limit)
2275 "Match spaces at beginning of buffer which do not contain the point at \
2276 beginning of buffer."
2282 (setq r
(and (/= whitespace-point
1)
2283 (looking-at whitespace-empty-at-bob-regexp
)))
2284 (set-marker whitespace-bob-marker
(if r
(match-end 1) b
)))
2285 ;; inside bob empty region
2286 ((<= limit whitespace-bob-marker
)
2287 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2289 (when (< (match-end 1) limit
)
2290 (set-marker whitespace-bob-marker
(match-end 1)))
2291 (set-marker whitespace-bob-marker b
)))
2292 ;; intersection with end of bob empty region
2293 ((<= b whitespace-bob-marker
)
2294 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2295 (set-marker whitespace-bob-marker
(if r
(match-end 1) b
)))
2296 ;; it is not inside bob empty region
2299 ;; move to end of matching
2300 (and r
(goto-char (match-end 1)))
2304 (defsubst whitespace-looking-back
(regexp limit
)
2306 (when (/= 0 (skip-chars-backward " \t\n" limit
))
2309 (looking-at regexp
))))
2312 (defun whitespace-empty-at-eob-regexp (limit)
2313 "Match spaces at end of buffer which do not contain the point at end of \
2316 (e (1+ (buffer-size)))
2321 (when (/= whitespace-point e
)
2323 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
)))
2325 (set-marker whitespace-eob-marker
(match-beginning 1))
2326 (set-marker whitespace-eob-marker limit
)
2327 (goto-char b
))) ; return back to initial position
2328 ;; inside eob empty region
2329 ((>= b whitespace-eob-marker
)
2331 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2333 (when (> (match-beginning 1) b
)
2334 (set-marker whitespace-eob-marker
(match-beginning 1)))
2335 (set-marker whitespace-eob-marker limit
)
2336 (goto-char b
))) ; return back to initial position
2337 ;; intersection with beginning of eob empty region
2338 ((>= limit whitespace-eob-marker
)
2340 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2342 (set-marker whitespace-eob-marker
(match-beginning 1))
2343 (set-marker whitespace-eob-marker limit
)
2344 (goto-char b
))) ; return back to initial position
2345 ;; it is not inside eob empty region
2351 (defun whitespace-buffer-changed (_beg _end
)
2352 "Set `whitespace-buffer-changed' variable to t."
2353 (setq whitespace-buffer-changed t
))
2356 (defun whitespace-post-command-hook ()
2357 "Save current point into `whitespace-point' variable.
2358 Also refontify when necessary."
2359 (setq whitespace-point
(point)) ; current point position
2362 ;; it is at end of line ...
2364 ;; ... with trailing SPACE or TAB
2365 (or (= (preceding-char) ?\
)
2366 (= (preceding-char) ?
\t)))
2367 ;; it is at beginning of buffer (bob)
2368 (= whitespace-point
1)
2369 ;; the buffer was modified and ...
2370 (and whitespace-buffer-changed
2372 ;; ... or inside bob whitespace region
2373 (<= whitespace-point whitespace-bob-marker
)
2374 ;; ... or at bob whitespace region border
2375 (and (= whitespace-point
(1+ whitespace-bob-marker
))
2376 (= (preceding-char) ?
\n))))
2377 ;; it is at end of buffer (eob)
2378 (= whitespace-point
(1+ (buffer-size)))
2379 ;; the buffer was modified and ...
2380 (and whitespace-buffer-changed
2382 ;; ... or inside eob whitespace region
2383 (>= whitespace-point whitespace-eob-marker
)
2384 ;; ... or at eob whitespace region border
2385 (and (= whitespace-point
(1- whitespace-eob-marker
))
2386 (= (following-char) ?
\n)))))))
2387 (when (or refontify
(> whitespace-font-lock-refontify
0))
2388 (setq whitespace-buffer-changed nil
)
2389 ;; adjust refontify counter
2390 (setq whitespace-font-lock-refontify
2393 (1- whitespace-font-lock-refontify
)))
2395 (jit-lock-refontify))))
2398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2399 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2402 (defun whitespace-style-mark-p ()
2403 "Return t if there is some visualization via display table."
2404 (or (memq 'tab-mark whitespace-active-style
)
2405 (memq 'space-mark whitespace-active-style
)
2406 (memq 'newline-mark whitespace-active-style
)))
2409 (defsubst whitespace-char-valid-p
(char)
2410 ;; This check should be improved!!!
2415 (defun whitespace-display-vector-p (vec)
2416 "Return true if every character in vector VEC can be displayed."
2417 (let ((i (length vec
)))
2419 (while (and (>= (setq i
(1- i
)) 0)
2420 (whitespace-char-valid-p (aref vec i
))))
2424 (defun whitespace-display-char-on ()
2425 "Turn on character display mapping."
2426 (when (and whitespace-display-mappings
2427 (whitespace-style-mark-p))
2429 ;; Remember whether a buffer has a local display table.
2430 (unless whitespace-display-table-was-local
2431 (setq whitespace-display-table-was-local t
2432 whitespace-display-table
2433 (copy-sequence buffer-display-table
))
2434 ;; Assure `buffer-display-table' is unique
2435 ;; when two or more windows are visible.
2436 (setq buffer-display-table
2437 (copy-sequence buffer-display-table
)))
2438 (unless buffer-display-table
2439 (setq buffer-display-table
(make-display-table)))
2440 (dolist (entry whitespace-display-mappings
)
2441 ;; check if it is to display this mark
2442 (when (memq (car entry
) whitespace-style
)
2443 ;; Get a displayable mapping.
2444 (setq vecs
(cddr entry
))
2446 (not (whitespace-display-vector-p (car vecs
))))
2447 (setq vecs
(cdr vecs
)))
2448 ;; Display a valid mapping.
2450 (setq vec
(copy-sequence (car vecs
)))
2452 (when (and (eq (cadr entry
) ?
\n)
2453 (memq 'newline whitespace-active-style
))
2454 ;; Only insert face bits on NEWLINE char mapping to avoid
2455 ;; obstruction of other faces like TABs and (HARD) SPACEs
2456 ;; faces, font-lock faces, etc.
2457 (dotimes (i (length vec
))
2458 (or (eq (aref vec i
) ?
\n)
2460 (make-glyph-code (aref vec i
)
2461 whitespace-newline
)))))
2463 (aset buffer-display-table
(cadr entry
) vec
)))))))
2466 (defun whitespace-display-char-off ()
2467 "Turn off character display mapping."
2468 (and whitespace-display-mappings
2469 (whitespace-style-mark-p)
2470 whitespace-display-table-was-local
2471 (setq whitespace-display-table-was-local nil
2472 buffer-display-table whitespace-display-table
)))
2475 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2479 (defun whitespace-action-when-on ()
2480 "Action to be taken always when local whitespace is turned on."
2481 (cond ((memq 'cleanup whitespace-action
)
2482 (whitespace-cleanup))
2483 ((memq 'report-on-bogus whitespace-action
)
2484 (whitespace-report nil t
))))
2487 (defun whitespace-write-file-hook ()
2488 "Action to be taken when buffer is written.
2489 It should be added buffer-locally to `write-file-functions'."
2490 (cond ((memq 'auto-cleanup whitespace-action
)
2491 (whitespace-cleanup))
2492 ((memq 'abort-on-bogus whitespace-action
)
2493 (when (whitespace-report nil t
)
2494 (error "Abort write due to whitespace problems in %s"
2496 nil
) ; continue hook processing
2499 (defun whitespace-warn-read-only (msg)
2500 "Warn if buffer is read-only."
2501 (when (memq 'warn-if-read-only whitespace-action
)
2502 (message "Can't %s: %s is read-only" msg
(buffer-name))))
2505 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2508 (defun whitespace-unload-function ()
2509 "Unload the whitespace library."
2510 (global-whitespace-mode -
1)
2511 ;; be sure all local whitespace mode is turned off
2512 (save-current-buffer
2513 (dolist (buf (buffer-list))
2515 (whitespace-mode -
1)))
2516 nil
) ; continue standard unloading
2519 (provide 'whitespace
)
2522 (run-hooks 'whitespace-load-hook
)
2525 ;;; whitespace.el ends here