1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE -*- lexical-binding: t -*-
3 ;; Copyright (C) 2000-2017 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 <https://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.
181 ;; See the function's docstring for more information.
183 ;; `whitespace-cleanup-region'
184 ;; Cleanup some blank problems at region.
190 ;; Whitespace's behavior can be changed with `M-x customize-group
191 ;; whitespace', which see for the full list of options.
197 ;; whitespace has the following hook variables:
199 ;; `whitespace-mode-hook'
200 ;; It is evaluated always when whitespace is turned on locally.
202 ;; `global-whitespace-mode-hook'
203 ;; It is evaluated always when whitespace is turned on globally.
205 ;; `whitespace-load-hook'
206 ;; It is evaluated after whitespace package is loaded.
212 ;; Thanks to felix (EmacsWiki) for keeping highlight when switching between
213 ;; major modes on a file.
215 ;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a
216 ;; `whitespace-newline' initialization with low contrast relative to
217 ;; the background color.
219 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
220 ;; `indent-tabs-mode' usage suggestion.
222 ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook
223 ;; actions when buffer is written as the original whitespace package
226 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
227 ;; lines tail. See EightyColumnRule (EmacsWiki).
229 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
230 ;; * `define-minor-mode'.
231 ;; * `global-whitespace-*' name for global commands.
233 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
235 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
238 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
239 ;; helping to fix `find-file-hooks' reference.
241 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
242 ;; indicating defface byte-compilation warnings.
244 ;; Thanks to Tim O'Callaghan (EmacsWiki) for the idea about highlight
245 ;; "long" lines. See EightyColumnRule (EmacsWiki).
247 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
248 ;; NEWLINE character mapping.
250 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
251 ;; whitespace-mode.el on XEmacs.
253 ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via
254 ;; visws.el (his code was modified, but the main idea was kept).
257 ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el
258 ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el
259 ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el
260 ;; Miles Bader <miles@gnu.org> visws.el
261 ;; And to all people who contributed with them.
264 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273 ;;; Interface to the command system
276 (defgroup whitespace nil
277 "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
278 :link
'(emacs-library-link :tag
"Source Lisp File" "whitespace.el")
283 (defcustom whitespace-style
285 tabs spaces trailing lines space-before-tab newline
286 indentation empty space-after-tab
287 space-mark tab-mark newline-mark
)
288 "Specify which kind of blank is visualized.
290 It's a list containing some or all of the following values:
292 face enable all visualization via faces (see below).
294 trailing trailing blanks are visualized via faces.
295 It has effect only if `face' (see above)
296 is present in `whitespace-style'.
298 tabs TABs are visualized via faces.
299 It has effect only if `face' (see above)
300 is present in `whitespace-style'.
302 spaces SPACEs and HARD SPACEs are visualized via
304 It has effect only if `face' (see above)
305 is present in `whitespace-style'.
307 lines lines which have columns beyond
308 `whitespace-line-column' are highlighted via
310 Whole line is highlighted.
311 It has precedence over `lines-tail' (see
313 It has effect only if `face' (see above)
314 is present in `whitespace-style'.
316 lines-tail lines which have columns beyond
317 `whitespace-line-column' are highlighted via
319 But only the part of line which goes
320 beyond `whitespace-line-column' column.
321 It has effect only if `lines' (see above)
322 is not present in `whitespace-style'
323 and if `face' (see above) is present in
326 newline NEWLINEs are visualized via faces.
327 It has effect only if `face' (see above)
328 is present in `whitespace-style'.
330 empty empty lines at beginning and/or end of buffer
331 are visualized via faces.
332 It has effect only if `face' (see above)
333 is present in `whitespace-style'.
335 indentation::tab `tab-width' or more SPACEs at beginning of line
336 are visualized via faces.
337 It has effect only if `face' (see above)
338 is present in `whitespace-style'.
340 indentation::space TABs at beginning of line are visualized via
342 It has effect only if `face' (see above)
343 is present in `whitespace-style'.
345 indentation `tab-width' or more SPACEs at beginning of line
346 are visualized, if `indent-tabs-mode' (which
347 see) is non-nil; otherwise, TABs at beginning
348 of line are visualized via faces.
349 It has effect only if `face' (see above)
350 is present in `whitespace-style'.
352 big-indent Big indentations are visualized via faces.
353 It has effect only if `face' (see above)
354 is present in `whitespace-style'.
356 space-after-tab::tab `tab-width' or more SPACEs after a TAB
357 are visualized via faces.
358 It has effect only if `face' (see above)
359 is present in `whitespace-style'.
361 space-after-tab::space TABs are visualized when `tab-width' or
362 more SPACEs occur after a TAB, via
364 It has effect only if `face' (see above)
365 is present in `whitespace-style'.
367 space-after-tab `tab-width' or more SPACEs after a TAB
368 are visualized, if `indent-tabs-mode'
369 (which see) is non-nil; otherwise,
370 the TABs are visualized via faces.
371 It has effect only if `face' (see above)
372 is present in `whitespace-style'.
374 space-before-tab::tab SPACEs before TAB are visualized via
376 It has effect only if `face' (see above)
377 is present in `whitespace-style'.
379 space-before-tab::space TABs are visualized when SPACEs occur
380 before TAB, via faces.
381 It has effect only if `face' (see above)
382 is present in `whitespace-style'.
384 space-before-tab SPACEs before TAB are visualized, if
385 `indent-tabs-mode' (which see) is
386 non-nil; otherwise, the TABs are
387 visualized via faces.
388 It has effect only if `face' (see above)
389 is present in `whitespace-style'.
391 space-mark SPACEs and HARD SPACEs are visualized via
394 tab-mark TABs are visualized via display table.
396 newline-mark NEWLINEs are visualized via display table.
398 Any other value is ignored.
400 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
403 There is an evaluation order for some values, if they are
404 included in `whitespace-style' list. For example, if
405 indentation, indentation::tab and/or indentation::space are
406 included in `whitespace-style' list. The evaluation order for
412 3. indentation::space
414 * For SPACEs after TABs:
416 2. space-after-tab::tab
417 3. space-after-tab::space
419 * For SPACEs before TABs:
421 2. space-before-tab::tab
422 3. space-before-tab::space
424 For example, if `indentation' and `indentation::space' are
425 included in `whitespace-style', the `indentation' value is used
426 instead of the `indentation::space' value.
428 One reason to not use faces to visualize spaces (i.e., not
429 include `face' in `whitespace-style') is to use `whitespace-mode'
430 only for cleaning up a buffer. See `whitespace-cleanup' and
431 `whitespace-cleanup-region'.
433 See also `whitespace-display-mappings' for documentation."
434 :type
'(set :tag
"Kind of Blank"
435 (const :tag
"(Face) Face visualization" face
)
436 (const :tag
"(Face) Trailing TABs, SPACEs and HARD SPACEs"
438 (const :tag
"(Face) TABs" tabs
)
439 (const :tag
"(Face) SPACEs and HARD SPACEs" spaces
)
440 (const :tag
"(Face) Lines" lines
)
441 (const :tag
"(Face) Lines, only overlong part" lines-tail
)
442 (const :tag
"(Face) NEWLINEs" newline
)
443 (const :tag
"(Face) Empty Lines At BOB And/Or EOB" empty
)
444 (const :tag
"(Face) Indentation SPACEs" indentation
::tab
)
445 (const :tag
"(Face) Indentation TABs"
447 (const :tag
"(Face) Indentation TABs or SPACEs" indentation
)
448 (const :tag
"(Face) Too much line indentation" big-indent
)
449 (const :tag
"(Face) SPACEs after TAB: SPACEs"
450 space-after-tab
::tab
)
451 (const :tag
"(Face) SPACEs after TAB: TABs"
452 space-after-tab
::space
)
453 (const :tag
"(Face) SPACEs after TAB" space-after-tab
)
454 (const :tag
"(Face) SPACEs before TAB: SPACEs"
455 space-before-tab
::tab
)
456 (const :tag
"(Face) SPACEs before TAB: TABs"
457 space-before-tab
::space
)
458 (const :tag
"(Face) SPACEs before TAB" space-before-tab
)
459 (const :tag
"(Mark) SPACEs and HARD SPACEs" space-mark
)
460 (const :tag
"(Mark) TABs" tab-mark
)
461 (const :tag
"(Mark) NEWLINEs" newline-mark
))
464 (defvar whitespace-space
'whitespace-space
465 "Symbol face used to visualize SPACE.
466 Used when `whitespace-style' includes the value `spaces'.")
467 (make-obsolete-variable 'whitespace-space
"use the face instead." "24.4")
470 (defface whitespace-space
471 '((((class color
) (background dark
))
472 :background
"grey20" :foreground
"darkgray")
473 (((class color
) (background light
))
474 :background
"LightYellow" :foreground
"lightgray")
475 (t :inverse-video t
))
476 "Face used to visualize SPACE."
480 (defvar whitespace-hspace
'whitespace-hspace
481 "Symbol face used to visualize HARD SPACE.
482 Used when `whitespace-style' includes the value `spaces'.")
483 (make-obsolete-variable 'whitespace-hspace
"use the face instead." "24.4")
485 (defface whitespace-hspace
; 'nobreak-space
486 '((((class color
) (background dark
))
487 :background
"grey24" :foreground
"darkgray")
488 (((class color
) (background light
))
489 :background
"LemonChiffon3" :foreground
"lightgray")
490 (t :inverse-video t
))
491 "Face used to visualize HARD SPACE."
495 (defvar whitespace-tab
'whitespace-tab
496 "Symbol face used to visualize TAB.
497 Used when `whitespace-style' includes the value `tabs'.")
498 (make-obsolete-variable 'whitespace-tab
"use the face instead." "24.4")
500 (defface whitespace-tab
501 '((((class color
) (background dark
))
502 :background
"grey22" :foreground
"darkgray")
503 (((class color
) (background light
))
504 :background
"beige" :foreground
"lightgray")
505 (t :inverse-video t
))
506 "Face used to visualize TAB."
510 (defvar whitespace-newline
'whitespace-newline
511 "Symbol face used to visualize NEWLINE char mapping.
512 See `whitespace-display-mappings'.
513 Used when `whitespace-style' includes the values `newline-mark'
515 (make-obsolete-variable 'whitespace-newline
"use the face instead." "24.4")
517 (defface whitespace-newline
518 '((default :weight normal
)
519 (((class color
) (background dark
)) :foreground
"darkgray")
520 (((class color
) (min-colors 88) (background light
)) :foreground
"lightgray")
521 ;; Displays with 16 colors use lightgray as background, so using a
522 ;; lightgray foreground makes the newline mark invisible.
523 (((class color
) (background light
)) :foreground
"brown")
525 "Face used to visualize NEWLINE char mapping.
527 See `whitespace-display-mappings'."
531 (defvar whitespace-trailing
'whitespace-trailing
532 "Symbol face used to visualize trailing blanks.
533 Used when `whitespace-style' includes the value `trailing'.")
534 (make-obsolete-variable 'whitespace-trailing
"use the face instead." "24.4")
536 (defface whitespace-trailing
; 'trailing-whitespace
537 '((default :weight bold
)
538 (((class mono
)) :inverse-video t
:underline t
)
539 (t :background
"red1" :foreground
"yellow"))
540 "Face used to visualize trailing blanks."
544 (defvar whitespace-line
'whitespace-line
545 "Symbol face used to visualize \"long\" lines.
546 See `whitespace-line-column'.
547 Used when `whitespace-style' includes the value `line'.")
548 (make-obsolete-variable 'whitespace-line
"use the face instead." "24.4")
550 (defface whitespace-line
551 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
552 (t :background
"gray20" :foreground
"violet"))
553 "Face used to visualize \"long\" lines.
555 See `whitespace-line-column'."
559 (defvar whitespace-space-before-tab
'whitespace-space-before-tab
560 "Symbol face used to visualize SPACEs before TAB.
561 Used when `whitespace-style' includes the value `space-before-tab'.")
562 (make-obsolete-variable 'whitespace-space-before-tab
563 "use the face instead." "24.4")
565 (defface whitespace-space-before-tab
566 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
567 (t :background
"DarkOrange" :foreground
"firebrick"))
568 "Face used to visualize SPACEs before TAB."
572 (defvar whitespace-indentation
'whitespace-indentation
573 "Symbol face used to visualize `tab-width' or more SPACEs at beginning of
574 line. Used when `whitespace-style' includes the value `indentation'.")
575 (make-obsolete-variable 'whitespace-indentation
"use the face instead." "24.4")
577 (defface whitespace-indentation
578 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
579 (t :background
"yellow" :foreground
"firebrick"))
580 "Face used to visualize `tab-width' or more SPACEs at beginning of line."
583 (defface whitespace-big-indent
584 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
585 (t :background
"red" :foreground
"firebrick"))
586 "Face used to visualize big indentation."
590 (defvar whitespace-empty
'whitespace-empty
591 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
592 Used when `whitespace-style' includes the value `empty'.")
593 (make-obsolete-variable 'whitespace-empty
"use the face instead." "24.4")
595 (defface whitespace-empty
596 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
597 (t :background
"yellow" :foreground
"firebrick"))
598 "Face used to visualize empty lines at beginning and/or end of buffer."
602 (defvar whitespace-space-after-tab
'whitespace-space-after-tab
603 "Symbol face used to visualize `tab-width' or more SPACEs after TAB.
604 Used when `whitespace-style' includes the value `space-after-tab'.")
605 (make-obsolete-variable 'whitespace-space-after-tab
606 "use the face instead." "24.4")
608 (defface whitespace-space-after-tab
609 '((((class mono
)) :inverse-video t
:weight bold
:underline t
)
610 (t :background
"yellow" :foreground
"firebrick"))
611 "Face used to visualize `tab-width' or more SPACEs after TAB."
615 (defcustom whitespace-hspace-regexp
617 "Specify HARD SPACE characters regexp.
619 Here are some examples:
621 \"\\\\(^\\xA0+\\\\)\" \
622 visualize only leading HARD SPACEs.
623 \"\\\\(\\xA0+$\\\\)\" \
624 visualize only trailing HARD SPACEs.
625 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
626 visualize leading and/or trailing HARD SPACEs.
627 \"\\t\\\\(\\xA0+\\\\)\\t\" \
628 visualize only HARD SPACEs between TABs.
630 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
631 Use exactly one pair of enclosing \\\\( and \\\\).
633 Used when `whitespace-style' includes `spaces'."
634 :type
'(regexp :tag
"HARD SPACE Chars")
638 (defcustom whitespace-space-regexp
"\\( +\\)"
639 "Specify SPACE characters regexp.
641 If you're using `mule' package, there may be other characters
642 besides \" \" that should be considered SPACE.
644 Here are some examples:
646 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
647 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
648 \"\\\\(^ +\\\\| +$\\\\)\" \
649 visualize leading and/or trailing SPACEs.
650 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
652 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
653 Use exactly one pair of enclosing \\\\( and \\\\).
655 Used when `whitespace-style' includes `spaces'."
656 :type
'(regexp :tag
"SPACE Chars")
660 (defcustom whitespace-tab-regexp
"\\(\t+\\)"
661 "Specify TAB characters regexp.
663 If you're using `mule' package, there may be other characters
664 besides \"\\t\" that should be considered TAB.
666 Here are some examples:
668 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
669 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
670 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
671 visualize leading and/or trailing TABs.
672 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
674 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
675 Use exactly one pair of enclosing \\\\( and \\\\).
677 Used when `whitespace-style' includes `tabs'."
678 :type
'(regexp :tag
"TAB Chars")
682 (defcustom whitespace-trailing-regexp
683 "\\([\t \u00A0]+\\)$"
684 "Specify trailing characters regexp.
686 There may be other characters besides:
688 \" \" \"\\t\" \"\\u00A0\"
690 that should be considered blank.
692 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
693 Use exactly one pair of enclosing elements above.
695 Used when `whitespace-style' includes `trailing'."
696 :type
'(regexp :tag
"Trailing Chars")
700 (defcustom whitespace-space-before-tab-regexp
"\\( +\\)\\(\t+\\)"
701 "Specify SPACEs before TAB regexp.
703 Used when `whitespace-style' includes `space-before-tab',
704 `space-before-tab::tab' or `space-before-tab::space'."
705 :type
'(regexp :tag
"SPACEs Before TAB")
709 (defcustom whitespace-indentation-regexp
710 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
711 .
"^ *\\(\t+\\)[^\n]")
712 "Specify regexp for `tab-width' or more SPACEs at beginning of line.
714 It is a cons where the cons car is used for SPACEs visualization
715 and the cons cdr is used for TABs visualization.
717 Used when `whitespace-style' includes `indentation',
718 `indentation::tab' or `indentation::space'."
719 :type
'(cons (string :tag
"Indentation SPACEs")
720 (string :tag
"Indentation TABs"))
724 (defcustom whitespace-empty-at-bob-regexp
"\\`\\(\\([ \t]*\n\\)+\\)"
725 "Specify regexp for empty lines at beginning of buffer.
727 Used when `whitespace-style' includes `empty'."
728 :type
'(regexp :tag
"Empty Lines At Beginning Of Buffer")
732 (defcustom whitespace-empty-at-eob-regexp
"^\\([ \t\n]+\\)\\'"
733 "Specify regexp for empty lines at end of buffer.
735 Used when `whitespace-style' includes `empty'."
736 :type
'(regexp :tag
"Empty Lines At End Of Buffer")
740 (defcustom whitespace-space-after-tab-regexp
741 '("\t+\\(\\( \\{%d,\\}\\)+\\)"
742 .
"\\(\t+\\) \\{%d,\\}")
743 "Specify regexp for `tab-width' or more SPACEs after TAB.
745 It is a cons where the cons car is used for SPACEs visualization
746 and the cons cdr is used for TABs visualization.
748 Used when `whitespace-style' includes `space-after-tab',
749 `space-after-tab::tab' or `space-after-tab::space'."
750 :type
'(cons (string :tag
"SPACEs After TAB")
754 (defcustom whitespace-big-indent-regexp
755 "^\\(\\(?:\t\\{4,\\}\\| \\{32,\\}\\)[\t ]*\\)"
756 "Specify big indentation regexp.
758 If you're using `mule' package, there may be other characters
759 besides \"\\t\" that should be considered TAB.
761 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
762 Use exactly one pair of enclosing \\\\( and \\\\).
764 Used when `whitespace-style' includes `big-indent'."
766 :type
'(regexp :tag
"Detect too much indentation at the beginning of a line")
770 (defcustom whitespace-line-column
80
771 "Specify column beyond which the line is highlighted.
773 It must be an integer or nil. If nil, the `fill-column' variable value is
776 Used when `whitespace-style' includes `lines' or `lines-tail'."
777 :type
'(choice :tag
"Line Length Limit"
778 (integer :tag
"Line Length")
779 (const :tag
"Use fill-column" nil
))
784 ;; Hacked from `visible-whitespace-mappings' in visws.el
785 (defcustom whitespace-display-mappings
787 (space-mark ?\
[?·
] [?.
]) ; space - middle dot
788 (space-mark ?
\xA0 [?¤
] [?_
]) ; hard space - currency sign
789 ;; NEWLINE is displayed using the face `whitespace-newline'
790 (newline-mark ?
\n [?$ ?
\n]) ; eol - dollar sign
791 ;; (newline-mark ?\n [?↵ ?\n] [?$ ?\n]) ; eol - downwards arrow
792 ;; (newline-mark ?\n [?¶ ?\n] [?$ ?\n]) ; eol - pilcrow
793 ;; (newline-mark ?\n [?¯ ?\n] [?$ ?\n]) ; eol - overscore
794 ;; (newline-mark ?\n [?¬ ?\n] [?$ ?\n]) ; eol - negation
795 ;; (newline-mark ?\n [?° ?\n] [?$ ?\n]) ; eol - degrees
797 ;; WARNING: the mapping below has a problem.
798 ;; When a TAB occupies exactly one column, it will display the
799 ;; character ?\xBB at that column followed by a TAB which goes to
800 ;; the next TAB column.
801 ;; If this is a problem for you, please, comment the line below.
802 (tab-mark ?
\t [?» ?
\t] [?
\\ ?
\t]) ; tab - right guillemet
804 "Specify an alist of mappings for displaying characters.
806 Each element has the following form:
808 (KIND CHAR VECTOR...)
812 KIND is the kind of character.
813 It can be one of the following symbols:
815 tab-mark for TAB character
817 space-mark for SPACE or HARD SPACE character
819 newline-mark for NEWLINE character
821 CHAR is the character to be mapped.
823 VECTOR is a vector of characters to be displayed in place of CHAR.
824 The first display vector that can be displayed is used;
825 if no display vector for a mapping can be displayed, then
826 that character is displayed unmodified.
828 The NEWLINE character is displayed using the face given by
829 `whitespace-newline' variable.
831 Used when `whitespace-style' includes `tab-mark', `space-mark' or
834 (list :tag
"Character Mapping"
835 (choice :tag
"Char Kind"
836 (const :tag
"Tab" tab-mark
)
837 (const :tag
"Space" space-mark
)
838 (const :tag
"Newline" newline-mark
))
839 (character :tag
"Char")
840 (repeat :inline t
:tag
"Vector List"
843 :tag
"Vector Characters"
844 (character :tag
"Char"))))))
848 (defcustom whitespace-global-modes t
849 "Modes for which global `whitespace-mode' is automagically turned on.
851 Global `whitespace-mode' is controlled by the command
852 `global-whitespace-mode'.
854 If nil, means no modes have `whitespace-mode' automatically
857 If t, all modes that support `whitespace-mode' have it
858 automatically turned on.
860 Else it should be a list of `major-mode' symbol names for which
861 `whitespace-mode' should be automatically turned on. The sense
862 of the list is negated if it begins with `not'. For example:
866 means that `whitespace-mode' is turned on for buffers in C and
868 :type
'(choice :tag
"Global Modes"
869 (const :tag
"None" nil
)
871 (set :menu-tag
"Mode Specific" :tag
"Modes"
873 (const :tag
"Except" not
)
875 (symbol :tag
"Mode"))))
879 (defcustom whitespace-action nil
880 "Specify which action is taken when a buffer is visited or written.
882 It's a list containing some or all of the following values:
884 nil no action is taken.
886 cleanup cleanup any bogus whitespace always when local
887 whitespace is turned on.
888 See `whitespace-cleanup' and
889 `whitespace-cleanup-region'.
891 report-on-bogus report if there is any bogus whitespace always
892 when local whitespace is turned on.
894 auto-cleanup cleanup any bogus whitespace when buffer is
896 See `whitespace-cleanup' and
897 `whitespace-cleanup-region'.
899 abort-on-bogus abort if there is any bogus whitespace and the
902 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
903 is included in `whitespace-action' and the
906 Any other value is treated as nil."
907 :type
'(choice :tag
"Actions"
908 (const :tag
"None" nil
)
909 (repeat :tag
"Action List"
910 (choice :tag
"Action"
911 (const :tag
"Cleanup When On" cleanup
)
912 (const :tag
"Report On Bogus" report-on-bogus
)
913 (const :tag
"Auto Cleanup" auto-cleanup
)
914 (const :tag
"Abort On Bogus" abort-on-bogus
)
915 (const :tag
"Warn If Read-Only" warn-if-read-only
))))
919 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
920 ;;;; User commands - Local mode
924 (define-minor-mode whitespace-mode
925 "Toggle whitespace visualization (Whitespace mode).
926 With a prefix argument ARG, enable Whitespace mode if ARG is
927 positive, and disable it otherwise.
929 If called from Lisp, also enables the mode if ARG is omitted or nil,
930 and toggles it if ARG is `toggle'.
932 See also `whitespace-style', `whitespace-newline' and
933 `whitespace-display-mappings'."
939 (noninteractive ; running a batch job
940 (setq whitespace-mode nil
))
941 (whitespace-mode ; whitespace-mode on
943 (whitespace-action-when-on))
944 (t ; whitespace-mode off
945 (whitespace-turn-off))))
949 (define-minor-mode whitespace-newline-mode
950 "Toggle newline visualization (Whitespace Newline mode).
951 With a prefix argument ARG, enable Whitespace Newline mode if ARG
952 is positive, and disable it otherwise.
954 If called from Lisp, also enables the mode if ARG is omitted or nil,
955 and toggles it if ARG is `toggle'.
957 Use `whitespace-newline-mode' only for NEWLINE visualization
958 exclusively. For other visualizations, including NEWLINE
959 visualization together with (HARD) SPACEs and/or TABs, please,
960 use `whitespace-mode'.
962 See also `whitespace-newline' and `whitespace-display-mappings'."
967 (let ((whitespace-style '(face newline-mark newline
)))
968 (whitespace-mode (if whitespace-newline-mode
970 ;; sync states (running a batch job)
971 (setq whitespace-newline-mode whitespace-mode
))
974 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
975 ;;;; User commands - Global mode
979 (define-minor-mode global-whitespace-mode
980 "Toggle whitespace visualization globally (Global Whitespace mode).
981 With a prefix argument ARG, enable Global Whitespace mode if ARG
982 is positive, and disable it otherwise.
984 If called from Lisp, also enables the mode if ARG is omitted or nil,
985 and toggles it if ARG is `toggle'.
987 See also `whitespace-style', `whitespace-newline' and
988 `whitespace-display-mappings'."
994 (noninteractive ; running a batch job
995 (setq global-whitespace-mode nil
))
996 (global-whitespace-mode ; global-whitespace-mode on
998 (add-hook 'find-file-hook
'whitespace-turn-on-if-enabled
)
999 (add-hook 'after-change-major-mode-hook
'whitespace-turn-on-if-enabled
)
1000 (dolist (buffer (buffer-list)) ; adjust all local mode
1002 (unless whitespace-mode
1003 (whitespace-turn-on-if-enabled)))))
1004 (t ; global-whitespace-mode off
1005 (save-current-buffer
1006 (remove-hook 'find-file-hook
'whitespace-turn-on-if-enabled
)
1007 (remove-hook 'after-change-major-mode-hook
'whitespace-turn-on-if-enabled
)
1008 (dolist (buffer (buffer-list)) ; adjust all local mode
1010 (unless whitespace-mode
1011 (whitespace-turn-off)))))))
1013 (defvar whitespace-enable-predicate
1016 ((eq whitespace-global-modes t
))
1017 ((listp whitespace-global-modes
)
1018 (if (eq (car-safe whitespace-global-modes
) 'not
)
1019 (not (memq major-mode
(cdr whitespace-global-modes
)))
1020 (memq major-mode whitespace-global-modes
)))
1022 ;; ...we have a display (not running a batch job)
1023 (not noninteractive
)
1024 ;; ...the buffer is not internal (name starts with a space)
1025 (not (eq (aref (buffer-name) 0) ?\
))
1026 ;; ...the buffer is not special (name starts with *)
1027 (or (not (eq (aref (buffer-name) 0) ?
*))
1028 ;; except the scratch buffer.
1029 (string= (buffer-name) "*scratch*"))))
1030 "Predicate to decide which buffers obey `global-whitespace-mode'.
1031 This function is called with no argument and should return non-nil
1032 if the current buffer should obey `global-whitespace-mode'.
1033 This variable is normally modified via `add-function'.")
1035 (defun whitespace-turn-on-if-enabled ()
1036 (when (funcall whitespace-enable-predicate
)
1037 (whitespace-turn-on)))
1040 (define-minor-mode global-whitespace-newline-mode
1041 "Toggle global newline visualization (Global Whitespace Newline mode).
1042 With a prefix argument ARG, enable Global Whitespace Newline mode
1043 if ARG is positive, and disable it otherwise.
1045 If called from Lisp, also enables the mode if ARG is omitted or nil,
1046 and toggles it if ARG is `toggle'.
1048 Use `global-whitespace-newline-mode' only for NEWLINE
1049 visualization exclusively. For other visualizations, including
1050 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1051 please use `global-whitespace-mode'.
1053 See also `whitespace-newline' and `whitespace-display-mappings'."
1058 (let ((whitespace-style '(newline-mark newline
)))
1059 (global-whitespace-mode (if global-whitespace-newline-mode
1061 ;; sync states (running a batch job)
1062 (setq global-whitespace-newline-mode global-whitespace-mode
)))
1065 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1066 ;;;; User commands - Toggle
1069 (defconst whitespace-style-value-list
1083 space-after-tab
::tab
1084 space-after-tab
::space
1086 space-before-tab
::tab
1087 space-before-tab
::space
1088 help-newline
; value used by `whitespace-insert-option-mark'
1093 "List of valid `whitespace-style' values.")
1096 (defconst whitespace-toggle-option-alist
1105 (?\C-i . indentation
)
1106 (?I . indentation
::tab
)
1107 (?i . indentation
::space
)
1108 (?\C-t . big-indent
)
1109 (?\C-a . space-after-tab
)
1110 (?A . space-after-tab
::tab
)
1111 (?a . space-after-tab
::space
)
1112 (?\C-b . space-before-tab
)
1113 (?B . space-before-tab
::tab
)
1114 (?b . space-before-tab
::space
)
1118 (?x . whitespace-style
)
1120 "Alist of toggle options.
1122 Each element has the form:
1128 CHAR is a char which the user will have to type.
1130 SYMBOL is a valid symbol associated with CHAR.
1131 See `whitespace-style-value-list'.")
1134 (defvar whitespace-active-style nil
1135 "Used to save locally `whitespace-style' value.")
1137 (defvar whitespace-point
(point)
1138 "Used to save locally current point value.
1139 Used by function `whitespace-trailing-regexp' (which see).")
1140 (defvar-local whitespace-point--used nil
1141 "Region whose highlighting depends on `whitespace-point'.")
1143 (defvar whitespace-font-lock-refontify nil
1144 "Used to save locally the font-lock refontify state.
1145 Used by function `whitespace-post-command-hook' (which see).")
1147 (defvar whitespace-bob-marker nil
1148 "Used to save locally the bob marker value.
1149 Used by function `whitespace-post-command-hook' (which see).")
1151 (defvar whitespace-eob-marker nil
1152 "Used to save locally the eob marker value.
1153 Used by function `whitespace-post-command-hook' (which see).")
1155 (defvar whitespace-buffer-changed nil
1156 "Used to indicate locally if buffer changed.
1157 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1158 functions (which see).")
1162 (defun whitespace-toggle-options (arg)
1163 "Toggle local `whitespace-mode' options.
1165 If local whitespace-mode is off, toggle the option given by ARG
1166 and turn on local whitespace-mode.
1168 If local whitespace-mode is on, toggle the option given by ARG
1169 and restart local whitespace-mode.
1171 Interactively, it reads one of the following chars:
1175 f toggle face visualization
1176 t toggle TAB visualization
1177 s toggle SPACE and HARD SPACE visualization
1178 r toggle trailing blanks visualization
1179 l toggle \"long lines\" visualization
1180 L toggle \"long lines\" tail visualization
1181 n toggle NEWLINE visualization
1182 e toggle empty line at bob and/or eob visualization
1183 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1184 I toggle indentation SPACEs visualization
1185 i toggle indentation TABs visualization
1186 C-t toggle big indentation visualization
1187 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1188 A toggle SPACEs after TAB: SPACEs visualization
1189 a toggle SPACEs after TAB: TABs visualization
1190 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1191 B toggle SPACEs before TAB: SPACEs visualization
1192 b toggle SPACEs before TAB: TABs visualization
1195 T toggle TAB visualization
1196 S toggle SPACEs before TAB visualization
1197 N toggle NEWLINE visualization
1199 x restore `whitespace-style' value
1200 ? display brief help
1202 Non-interactively, ARG should be a symbol or a list of symbols.
1203 The valid symbols are:
1205 face toggle face visualization
1206 tabs toggle TAB visualization
1207 spaces toggle SPACE and HARD SPACE visualization
1208 trailing toggle trailing blanks visualization
1209 lines toggle \"long lines\" visualization
1210 lines-tail toggle \"long lines\" tail visualization
1211 newline toggle NEWLINE visualization
1212 empty toggle empty line at bob and/or eob visualization
1213 indentation toggle indentation SPACEs visualization
1214 indentation::tab toggle indentation SPACEs visualization
1215 indentation::space toggle indentation TABs visualization
1216 big-indent toggle big indentation visualization
1217 space-after-tab toggle SPACEs after TAB visualization
1218 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1219 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1220 space-before-tab toggle SPACEs before TAB visualization
1221 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1222 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1224 tab-mark toggle TAB visualization
1225 space-mark toggle SPACEs before TAB visualization
1226 newline-mark toggle NEWLINE visualization
1228 whitespace-style restore `whitespace-style' value
1230 See `whitespace-style' and `indent-tabs-mode' for documentation."
1231 (interactive (whitespace-interactive-char t
))
1232 (let ((whitespace-style
1233 (whitespace-toggle-list t arg whitespace-active-style
)))
1235 (whitespace-mode 1)))
1238 (defvar whitespace-toggle-style nil
1239 "Used to toggle the global `whitespace-style' value.")
1243 (defun global-whitespace-toggle-options (arg)
1244 "Toggle global `whitespace-mode' options.
1246 If global whitespace-mode is off, toggle the option given by ARG
1247 and turn on global whitespace-mode.
1249 If global whitespace-mode is on, toggle the option given by ARG
1250 and restart global whitespace-mode.
1252 Interactively, it accepts one of the following chars:
1256 f toggle face visualization
1257 t toggle TAB visualization
1258 s toggle SPACE and HARD SPACE visualization
1259 r toggle trailing blanks visualization
1260 l toggle \"long lines\" visualization
1261 L toggle \"long lines\" tail visualization
1262 n toggle NEWLINE visualization
1263 e toggle empty line at bob and/or eob visualization
1264 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1265 I toggle indentation SPACEs visualization
1266 i toggle indentation TABs visualization
1267 C-t toggle big indentation visualization
1268 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1269 A toggle SPACEs after TAB: SPACEs visualization
1270 a toggle SPACEs after TAB: TABs visualization
1271 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1272 B toggle SPACEs before TAB: SPACEs visualization
1273 b toggle SPACEs before TAB: TABs visualization
1276 T toggle TAB visualization
1277 S toggle SPACEs before TAB visualization
1278 N toggle NEWLINE visualization
1280 x restore `whitespace-style' value
1281 ? display brief help
1283 Non-interactively, ARG should be a symbol or a list of symbols.
1284 The valid symbols are:
1286 face toggle face visualization
1287 tabs toggle TAB visualization
1288 spaces toggle SPACE and HARD SPACE visualization
1289 trailing toggle trailing blanks visualization
1290 lines toggle \"long lines\" visualization
1291 lines-tail toggle \"long lines\" tail visualization
1292 newline toggle NEWLINE visualization
1293 empty toggle empty line at bob and/or eob visualization
1294 indentation toggle indentation SPACEs visualization
1295 indentation::tab toggle indentation SPACEs visualization
1296 indentation::space toggle indentation TABs visualization
1297 big-indent toggle big indentation visualization
1298 space-after-tab toggle SPACEs after TAB visualization
1299 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1300 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1301 space-before-tab toggle SPACEs before TAB visualization
1302 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1303 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1305 tab-mark toggle TAB visualization
1306 space-mark toggle SPACEs before TAB visualization
1307 newline-mark toggle NEWLINE visualization
1309 whitespace-style restore `whitespace-style' value
1311 See `whitespace-style' and `indent-tabs-mode' for documentation."
1312 (interactive (whitespace-interactive-char nil
))
1313 (let ((whitespace-style
1314 (whitespace-toggle-list nil arg whitespace-toggle-style
)))
1315 (setq whitespace-toggle-style whitespace-style
)
1316 (global-whitespace-mode 0)
1317 (global-whitespace-mode 1)))
1320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1321 ;;;; User commands - Cleanup
1325 (defun whitespace-cleanup ()
1326 "Cleanup some blank problems in all buffer or at region.
1328 It usually applies to the whole buffer, but in transient mark
1329 mode when the mark is active, it applies to the region. It also
1330 applies to the region when it is not in transient mark mode, the
1331 mark is active and \\[universal-argument] was pressed just before
1332 calling `whitespace-cleanup' interactively.
1334 See also `whitespace-cleanup-region'.
1336 The problems cleaned up are:
1338 1. empty lines at beginning of buffer.
1339 2. empty lines at end of buffer.
1340 If `whitespace-style' includes the value `empty', remove all
1341 empty lines at beginning and/or end of buffer.
1343 3. `tab-width' or more SPACEs at beginning of line.
1344 If `whitespace-style' includes the value `indentation':
1345 replace `tab-width' or more SPACEs at beginning of line by
1346 TABs, if `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1348 If `whitespace-style' includes the value `indentation::tab',
1349 replace `tab-width' or more SPACEs at beginning of line by TABs.
1350 If `whitespace-style' includes the value `indentation::space',
1351 replace TABs by SPACEs.
1353 4. SPACEs before TAB.
1354 If `whitespace-style' includes the value `space-before-tab':
1355 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1356 otherwise, replace TABs by SPACEs.
1357 If `whitespace-style' includes the value
1358 `space-before-tab::tab', replace SPACEs by TABs.
1359 If `whitespace-style' includes the value
1360 `space-before-tab::space', replace TABs by SPACEs.
1362 5. SPACEs or TABs at end of line.
1363 If `whitespace-style' includes the value `trailing', remove
1364 all SPACEs or TABs at end of line.
1366 6. `tab-width' or more SPACEs after TAB.
1367 If `whitespace-style' includes the value `space-after-tab':
1368 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1369 otherwise, replace TABs by SPACEs.
1370 If `whitespace-style' includes the value
1371 `space-after-tab::tab', replace SPACEs by TABs.
1372 If `whitespace-style' includes the value
1373 `space-after-tab::space', replace TABs by SPACEs.
1375 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1381 (whitespace-warn-read-only "cleanup"))
1383 ((and (or transient-mark-mode
1386 ;; PROBLEMs 1 and 2 are not handled in region
1387 ;; PROBLEM 3: `tab-width' or more SPACEs at bol
1388 ;; PROBLEM 4: SPACEs before TAB
1389 ;; PROBLEM 5: SPACEs or TABs at eol
1390 ;; PROBLEM 6: `tab-width' or more SPACEs after TAB
1391 (whitespace-cleanup-region (region-beginning) (region-end)))
1395 ;; PROBLEM 1: empty lines at bob
1396 ;; PROBLEM 2: empty lines at eob
1397 ;; ACTION: remove all empty lines at bob and/or eob
1398 (when (memq 'empty whitespace-style
)
1399 (let (overwrite-mode) ; enforce no overwrite
1400 (goto-char (point-min))
1401 (when (looking-at whitespace-empty-at-bob-regexp
)
1402 (delete-region (match-beginning 1) (match-end 1)))
1403 (when (re-search-forward
1404 whitespace-empty-at-eob-regexp nil t
)
1405 (delete-region (match-beginning 1) (match-end 1))))))
1406 ;; PROBLEM 3: `tab-width' or more SPACEs at bol
1407 ;; PROBLEM 4: SPACEs before TAB
1408 ;; PROBLEM 5: SPACEs or TABs at eol
1409 ;; PROBLEM 6: `tab-width' or more SPACEs after TAB
1410 (whitespace-cleanup-region (point-min) (point-max)))))
1414 (defun whitespace-cleanup-region (start end
)
1415 "Cleanup some blank problems at region.
1417 The problems cleaned up are:
1419 1. `tab-width' or more SPACEs at beginning of line.
1420 If `whitespace-style' includes the value `indentation':
1421 replace `tab-width' or more SPACEs at beginning of line by TABs,
1422 if `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1424 If `whitespace-style' includes the value `indentation::tab',
1425 replace `tab-width' or more SPACEs at beginning of line by TABs.
1426 If `whitespace-style' includes the value `indentation::space',
1427 replace TABs by SPACEs.
1429 2. SPACEs before TAB.
1430 If `whitespace-style' includes the value `space-before-tab':
1431 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1432 otherwise, replace TABs by SPACEs.
1433 If `whitespace-style' includes the value
1434 `space-before-tab::tab', replace SPACEs by TABs.
1435 If `whitespace-style' includes the value
1436 `space-before-tab::space', replace TABs by SPACEs.
1438 3. SPACEs or TABs at end of line.
1439 If `whitespace-style' includes the value `trailing', remove
1440 all SPACEs or TABs at end of line.
1442 4. `tab-width' or more SPACEs after TAB.
1443 If `whitespace-style' includes the value `space-after-tab':
1444 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1445 otherwise, replace TABs by SPACEs.
1446 If `whitespace-style' includes the value
1447 `space-after-tab::tab', replace SPACEs by TABs.
1448 If `whitespace-style' includes the value
1449 `space-after-tab::space', replace TABs by SPACEs.
1451 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1454 (if buffer-read-only
1456 (whitespace-warn-read-only "cleanup region")
1457 ;; non-read-only buffer
1458 (let ((rstart (min start end
))
1459 (rend (copy-marker (max start end
)))
1460 overwrite-mode
; enforce no overwrite
1463 ;; PROBLEM 1: `tab-width' or more SPACEs at bol
1465 ;; ACTION: replace `tab-width' or more SPACEs at bol by TABs, if
1466 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1468 ((memq 'indentation whitespace-style
)
1469 (let ((regexp (whitespace-indentation-regexp)))
1471 (while (re-search-forward regexp rend t
)
1472 (setq tmp
(current-indentation))
1473 (goto-char (match-beginning 0))
1474 (delete-horizontal-space)
1477 ;; ACTION: replace `tab-width' or more SPACEs at bol by TABs.
1478 ((memq 'indentation
::tab whitespace-style
)
1479 (whitespace-replace-action
1481 (whitespace-indentation-regexp 'tab
) 0))
1482 ;; ACTION: replace TABs by SPACEs.
1483 ((memq 'indentation
::space whitespace-style
)
1484 (whitespace-replace-action
1485 'untabify rstart rend
1486 (whitespace-indentation-regexp 'space
) 0)))
1487 ;; PROBLEM 3: SPACEs or TABs at eol
1488 ;; ACTION: remove all SPACEs or TABs at eol
1489 (when (memq 'trailing whitespace-style
)
1490 (whitespace-replace-action
1491 'delete-region rstart rend
1492 whitespace-trailing-regexp
1))
1493 ;; PROBLEM 4: `tab-width' or more SPACEs after TAB
1495 ;; ACTION: replace `tab-width' or more SPACEs by TABs, if
1496 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1498 ((memq 'space-after-tab whitespace-style
)
1499 (whitespace-replace-action
1500 (if indent-tabs-mode
'tabify
'untabify
)
1501 rstart rend
(whitespace-space-after-tab-regexp) 1))
1502 ;; ACTION: replace `tab-width' or more SPACEs by TABs.
1503 ((memq 'space-after-tab
::tab whitespace-style
)
1504 (whitespace-replace-action
1506 (whitespace-space-after-tab-regexp 'tab
) 1))
1507 ;; ACTION: replace TABs by SPACEs.
1508 ((memq 'space-after-tab
::space whitespace-style
)
1509 (whitespace-replace-action
1510 'untabify rstart rend
1511 (whitespace-space-after-tab-regexp 'space
) 1)))
1512 ;; PROBLEM 2: SPACEs before TAB
1514 ;; ACTION: replace SPACEs before TAB by TABs, if
1515 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1517 ((memq 'space-before-tab whitespace-style
)
1518 (whitespace-replace-action
1519 (if indent-tabs-mode
'tabify
'untabify
)
1520 rstart rend whitespace-space-before-tab-regexp
1521 (if indent-tabs-mode
0 2)))
1522 ;; ACTION: replace SPACEs before TAB by TABs.
1523 ((memq 'space-before-tab
::tab whitespace-style
)
1524 (whitespace-replace-action
1526 whitespace-space-before-tab-regexp
0))
1527 ;; ACTION: replace TABs by SPACEs.
1528 ((memq 'space-before-tab
::space whitespace-style
)
1529 (whitespace-replace-action
1530 'untabify rstart rend
1531 whitespace-space-before-tab-regexp
2))))
1532 (set-marker rend nil
)))) ; point marker to nowhere
1535 (defun whitespace-replace-action (action rstart rend regexp index
)
1536 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1538 INDEX is the level group matched by REGEXP and used by ACTION.
1540 See also `tab-width'."
1542 (while (re-search-forward regexp rend t
)
1543 (goto-char (match-end index
))
1544 (funcall action
(match-beginning index
) (match-end index
))))
1547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1548 ;;;; User command - report
1551 (defun whitespace-regexp (regexp &optional kind
)
1552 "Return REGEXP depending on `indent-tabs-mode'."
1558 ((or (eq kind
'space
)
1559 (not indent-tabs-mode
))
1564 (defun whitespace-indentation-regexp (&optional kind
)
1565 "Return the indentation regexp depending on `indent-tabs-mode'."
1566 (whitespace-regexp whitespace-indentation-regexp kind
))
1569 (defun whitespace-space-after-tab-regexp (&optional kind
)
1570 "Return the space-after-tab regexp depending on `indent-tabs-mode'."
1571 (whitespace-regexp whitespace-space-after-tab-regexp kind
))
1574 (defconst whitespace-report-list
1576 (cons 'empty whitespace-empty-at-bob-regexp
)
1577 (cons 'empty whitespace-empty-at-eob-regexp
)
1578 (cons 'trailing whitespace-trailing-regexp
)
1579 (cons 'indentation nil
)
1580 (cons 'indentation
::tab nil
)
1581 (cons 'indentation
::space nil
)
1582 (cons 'space-before-tab whitespace-space-before-tab-regexp
)
1583 (cons 'space-before-tab
::tab whitespace-space-before-tab-regexp
)
1584 (cons 'space-before-tab
::space whitespace-space-before-tab-regexp
)
1585 (cons 'space-after-tab nil
)
1586 (cons 'space-after-tab
::tab nil
)
1587 (cons 'space-after-tab
::space nil
)
1589 "List of whitespace bogus symbol and corresponding regexp.")
1592 (defconst whitespace-report-text
1593 '( ;; `indent-tabs-mode' has non-nil value
1597 Current Setting Whitespace Problem
1599 empty [] [] empty lines at beginning of buffer
1600 empty [] [] empty lines at end of buffer
1601 trailing [] [] SPACEs or TABs at end of line
1602 indentation [] [] >= `tab-width' SPACEs at beginning of line
1603 indentation::tab [] [] >= `tab-width' SPACEs at beginning of line
1604 indentation::space [] [] TABs at beginning of line
1605 space-before-tab [] [] SPACEs before TAB
1606 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1607 space-before-tab::space [] [] SPACEs before TAB: TABs
1608 space-after-tab [] [] >= `tab-width' SPACEs after TAB
1609 space-after-tab::tab [] [] >= `tab-width' SPACEs after TAB: SPACEs
1610 space-after-tab::space [] [] >= `tab-width' SPACEs after TAB: TABs
1614 .
;; `indent-tabs-mode' has nil value
1618 Current Setting Whitespace Problem
1620 empty [] [] empty lines at beginning of buffer
1621 empty [] [] empty lines at end of buffer
1622 trailing [] [] SPACEs or TABs at end of line
1623 indentation [] [] TABs at beginning of line
1624 indentation::tab [] [] >= `tab-width' SPACEs at beginning of line
1625 indentation::space [] [] TABs at beginning of line
1626 space-before-tab [] [] SPACEs before TAB
1627 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1628 space-before-tab::space [] [] SPACEs before TAB: TABs
1629 space-after-tab [] [] >= `tab-width' SPACEs after TAB
1630 space-after-tab::tab [] [] >= `tab-width' SPACEs after TAB: SPACEs
1631 space-after-tab::space [] [] >= `tab-width' SPACEs after TAB: TABs
1635 "Text for whitespace bogus report.
1637 It is a cons of strings, where the car part is used when
1638 `indent-tabs-mode' is non-nil, and the cdr part is used when
1639 `indent-tabs-mode' is nil.")
1642 (defconst whitespace-report-buffer-name
"*Whitespace Report*"
1643 "The buffer name for whitespace bogus report.")
1647 (defun whitespace-report (&optional force report-if-bogus
)
1648 "Report some whitespace problems in buffer.
1650 Perform `whitespace-report-region' on the current buffer."
1651 (interactive (list current-prefix-arg
))
1652 (whitespace-report-region (point-min) (point-max)
1653 force report-if-bogus
))
1657 (defun whitespace-report-region (start end
&optional force report-if-bogus
)
1658 "Report some whitespace problems in a region.
1660 Return nil if there is no whitespace problem; otherwise, return
1663 If FORCE is non-nil or \\[universal-argument] was pressed just
1664 before calling `whitespace-report-region' interactively, it
1665 forces all classes of whitespace problem to be considered
1668 If REPORT-IF-BOGUS is t, it reports only when there are any
1669 whitespace problems in buffer; if it is `never', it does not
1672 Report if some of the following whitespace problems exist:
1674 * If `indent-tabs-mode' is non-nil:
1675 empty 1. empty lines at beginning of buffer.
1676 empty 2. empty lines at end of buffer.
1677 trailing 3. SPACEs or TABs at end of line.
1678 indentation 4. line starts with `tab-width' or more SPACEs.
1679 space-before-tab 5. SPACEs before TAB.
1680 space-after-tab 6. `tab-width' or more SPACEs after TAB.
1682 * If `indent-tabs-mode' is nil:
1683 empty 1. empty lines at beginning of buffer.
1684 empty 2. empty lines at end of buffer.
1685 trailing 3. SPACEs or TABs at end of line.
1686 indentation 4. TABS at beginning of line.
1687 space-before-tab 5. SPACEs before TAB.
1688 space-after-tab 6. `tab-width' or more SPACEs after TAB.
1690 See `whitespace-style' for documentation.
1691 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1692 cleaning up these problems."
1694 (setq force
(or current-prefix-arg force
))
1696 (let* ((has-bogus nil
)
1697 (rstart (min start end
))
1698 (rend (max start end
))
1699 ;; Fall back to whitespace-style so we can run before
1700 ;; before the mode is active.
1701 (style (copy-sequence
1702 (or whitespace-active-style whitespace-style
)))
1707 (add-to-list 'style
(car option
)))
1711 ((eq (car option
) 'indentation
)
1712 (whitespace-indentation-regexp))
1713 ((eq (car option
) 'indentation
::tab
)
1714 (whitespace-indentation-regexp 'tab
))
1715 ((eq (car option
) 'indentation
::space
)
1716 (whitespace-indentation-regexp 'space
))
1717 ((eq (car option
) 'space-after-tab
)
1718 (whitespace-space-after-tab-regexp))
1719 ((eq (car option
) 'space-after-tab
::tab
)
1720 (whitespace-space-after-tab-regexp 'tab
))
1721 ((eq (car option
) 'space-after-tab
::space
)
1722 (whitespace-space-after-tab-regexp 'space
))
1725 (when (re-search-forward regexp rend t
)
1727 (setq has-bogus
(memq (car option
) style
)))
1729 whitespace-report-list
)))
1730 (when (pcase report-if-bogus
(`nil t
) (`never nil
) (_ has-bogus
))
1731 (whitespace-kill-buffer whitespace-report-buffer-name
)
1732 ;; `indent-tabs-mode' may be local to current buffer
1733 ;; `tab-width' may be local to current buffer
1734 (let ((ws-indent-tabs-mode indent-tabs-mode
)
1735 (ws-tab-width tab-width
))
1736 (with-current-buffer (get-buffer-create
1737 whitespace-report-buffer-name
)
1739 (insert (if ws-indent-tabs-mode
1740 (car whitespace-report-text
)
1741 (cdr whitespace-report-text
)))
1742 (goto-char (point-min))
1744 (dolist (option whitespace-report-list
)
1747 27 (memq (car option
) style
))
1748 (whitespace-mark-x 7 (car bogus-list
))
1749 (setq bogus-list
(cdr bogus-list
)))
1751 (whitespace-insert-value ws-indent-tabs-mode
)
1752 (whitespace-insert-value ws-tab-width
)
1754 (goto-char (point-max))
1755 (insert (substitute-command-keys
1756 " Type `\\[whitespace-cleanup]'")
1757 " to cleanup the buffer.\n\n"
1758 (substitute-command-keys
1759 " Type `\\[whitespace-cleanup-region]'")
1760 " to cleanup a region.\n\n"))
1761 (whitespace-display-window (current-buffer)))))
1765 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1766 ;;;; Internal functions
1769 (defvar whitespace-font-lock-keywords nil
1770 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1773 (defconst whitespace-help-text
1775 Whitespace Toggle Options | scroll up : SPC or > |
1776 | scroll down: M-SPC or < |
1777 FACES \\__________________________/
1778 [] f - toggle face visualization
1779 [] t - toggle TAB visualization
1780 [] s - toggle SPACE and HARD SPACE visualization
1781 [] r - toggle trailing blanks visualization
1782 [] l - toggle \"long lines\" visualization
1783 [] L - toggle \"long lines\" tail visualization
1784 [] n - toggle NEWLINE visualization
1785 [] e - toggle empty line at bob and/or eob visualization
1786 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1787 [] I - toggle indentation SPACEs visualization
1788 [] i - toggle indentation TABs visualization
1789 [] C-t - toggle big indentation visualization
1790 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1791 [] A - toggle SPACEs after TAB: SPACEs visualization
1792 [] a - toggle SPACEs after TAB: TABs visualization
1793 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1794 [] B - toggle SPACEs before TAB: SPACEs visualization
1795 [] b - toggle SPACEs before TAB: TABs visualization
1798 [] T - toggle TAB visualization
1799 [] S - toggle SPACE and HARD SPACE visualization
1800 [] N - toggle NEWLINE visualization
1802 x - restore `whitespace-style' value
1804 ? - display this text\n\n"
1805 "Text for whitespace toggle options.")
1808 (defconst whitespace-help-buffer-name
"*Whitespace Toggle Options*"
1809 "The buffer name for whitespace toggle options.")
1812 (defun whitespace-insert-value (value)
1813 "Insert VALUE at column 20 of next line."
1815 (move-to-column 20 t
)
1816 (insert (format "%s" value
)))
1819 (defun whitespace-mark-x (nchars condition
)
1820 "Insert the mark (`X' or ` ') after NCHARS depending on CONDITION."
1821 (forward-char nchars
)
1822 (insert (if condition
"X" " ")))
1825 (defun whitespace-insert-option-mark (the-list the-value
)
1826 "Insert the option mark (`X' or ` ') in toggle options buffer."
1827 (goto-char (point-min))
1829 (dolist (sym the-list
)
1830 (if (eq sym
'help-newline
)
1833 (whitespace-mark-x 2 (memq sym the-value
)))))
1836 (defun whitespace-help-on (style)
1837 "Display the whitespace toggle options."
1838 (unless (get-buffer whitespace-help-buffer-name
)
1839 (delete-other-windows)
1840 (let ((buffer (get-buffer-create whitespace-help-buffer-name
)))
1841 (with-current-buffer buffer
1843 (insert whitespace-help-text
)
1844 (whitespace-insert-option-mark
1845 whitespace-style-value-list style
)
1846 (whitespace-display-window buffer
)))))
1849 (defun whitespace-display-window (buffer)
1850 "Display BUFFER in a new window."
1851 (goto-char (point-min))
1852 (set-buffer-modified-p nil
)
1853 (when (< (window-height) (* 2 window-min-height
))
1854 (kill-buffer buffer
)
1855 (error "Window height is too small; \
1856 can't split window to display whitespace toggle options"))
1857 (let ((win (split-window)))
1858 (set-window-buffer win buffer
)
1859 (shrink-window-if-larger-than-buffer win
)))
1862 (defun whitespace-kill-buffer (buffer-name)
1863 "Kill buffer BUFFER-NAME and windows related with it."
1864 (let ((buffer (get-buffer buffer-name
)))
1866 (delete-windows-on buffer
)
1867 (kill-buffer buffer
))))
1870 (defun whitespace-help-off ()
1871 "Remove the buffer and window of the whitespace toggle options."
1872 (whitespace-kill-buffer whitespace-help-buffer-name
))
1875 (defun whitespace-help-scroll (&optional up
)
1876 "Scroll help window, if it exists.
1878 If UP is non-nil, scroll up; otherwise, scroll down."
1880 (let ((buffer (get-buffer whitespace-help-buffer-name
)))
1882 (with-selected-window (get-buffer-window buffer
)
1889 ;; just ignore error
1893 (defun whitespace-interactive-char (local-p)
1894 "Interactive function to read a char and return a symbol.
1896 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1897 uses a global context.
1899 It accepts one of the following chars:
1903 f toggle face visualization
1904 t toggle TAB visualization
1905 s toggle SPACE and HARD SPACE visualization
1906 r toggle trailing blanks visualization
1907 l toggle \"long lines\" visualization
1908 L toggle \"long lines\" tail visualization
1909 n toggle NEWLINE visualization
1910 e toggle empty line at bob and/or eob visualization
1911 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1912 I toggle indentation SPACEs visualization
1913 i toggle indentation TABs visualization
1914 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1915 A toggle SPACEs after TAB: SPACEs visualization
1916 a toggle SPACEs after TAB: TABs visualization
1917 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1918 B toggle SPACEs before TAB: SPACEs visualization
1919 b toggle SPACEs before TAB: TABs visualization
1922 T toggle TAB visualization
1923 S toggle SPACE and HARD SPACE visualization
1924 N toggle NEWLINE visualization
1926 x restore `whitespace-style' value
1927 ? display brief help
1929 See also `whitespace-toggle-option-alist'."
1930 (let* ((is-off (not (if local-p
1932 global-whitespace-mode
)))
1933 (style (cond (is-off whitespace-style
) ; use default value
1934 (local-p whitespace-active-style
)
1935 (t whitespace-toggle-style
)))
1937 (format "Whitespace Toggle %s (type ? for further options)-"
1938 (if local-p
"Local" "Global")))
1940 ;; read a valid option and get the corresponding symbol
1941 (save-window-excursion
1942 (condition-case data
1947 (setq ch
(read-char prompt
))
1951 (assq ch whitespace-toggle-option-alist
)))))
1954 ((eq ch ?
\?) (whitespace-help-on style
))
1955 ((eq ch ?\
) (whitespace-help-scroll t
))
1956 ((eq ch ?\M-
) (whitespace-help-scroll))
1957 ((eq ch ?
>) (whitespace-help-scroll t
))
1958 ((eq ch ?
<) (whitespace-help-scroll))
1960 (whitespace-help-off)
1961 (message " ")) ; clean echo area
1964 (whitespace-help-off)
1965 (error (error-message-string data
)))))
1966 (list sym
))) ; return the appropriate symbol
1969 (defun whitespace-toggle-list (local-p arg the-list
)
1970 "Toggle options in THE-LIST based on list ARG.
1972 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1973 uses a global context.
1975 ARG is a list of options to be toggled.
1977 THE-LIST is a list of options. This list will be toggled and the
1978 resultant list will be returned."
1979 (unless (if local-p whitespace-mode global-whitespace-mode
)
1980 (setq the-list whitespace-style
))
1981 (setq the-list
(copy-sequence the-list
)) ; keep original list
1982 (dolist (sym (if (listp arg
) arg
(list arg
)))
1984 ;; ignore help value
1985 ((eq sym
'help-newline
))
1986 ;; restore default values
1987 ((eq sym
'whitespace-style
)
1988 (setq the-list whitespace-style
))
1989 ;; toggle valid values
1990 ((memq sym whitespace-style-value-list
)
1991 (setq the-list
(if (memq sym the-list
)
1993 (cons sym the-list
))))))
1997 (defvar whitespace-display-table nil
1998 "Used to save a local display table.")
2000 (defvar whitespace-display-table-was-local nil
2001 "Used to remember whether a buffer initially had a local display table.")
2003 (defun whitespace-turn-on ()
2004 "Turn on whitespace visualization."
2005 ;; prepare local hooks
2006 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
2007 ;; create whitespace local buffer environment
2008 (set (make-local-variable 'whitespace-font-lock-keywords
) nil
)
2009 (set (make-local-variable 'whitespace-display-table
) nil
)
2010 (set (make-local-variable 'whitespace-display-table-was-local
) nil
)
2011 (set (make-local-variable 'whitespace-active-style
)
2012 (if (listp whitespace-style
)
2014 (list whitespace-style
)))
2015 ;; turn on whitespace
2016 (when whitespace-active-style
2017 (whitespace-color-on)
2018 (whitespace-display-char-on)))
2021 (defun whitespace-turn-off ()
2022 "Turn off whitespace visualization."
2023 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
2024 (when whitespace-active-style
2025 (whitespace-color-off)
2026 (whitespace-display-char-off)))
2029 (defun whitespace-style-face-p ()
2030 "Return t if there is some visualization via face."
2031 (and (memq 'face whitespace-active-style
)
2032 (or (memq 'tabs whitespace-active-style
)
2033 (memq 'spaces whitespace-active-style
)
2034 (memq 'trailing whitespace-active-style
)
2035 (memq 'lines whitespace-active-style
)
2036 (memq 'lines-tail whitespace-active-style
)
2037 (memq 'newline whitespace-active-style
)
2038 (memq 'empty whitespace-active-style
)
2039 (memq 'indentation whitespace-active-style
)
2040 (memq 'indentation
::tab whitespace-active-style
)
2041 (memq 'indentation
::space whitespace-active-style
)
2042 (memq 'big-indent whitespace-active-style
)
2043 (memq 'space-after-tab whitespace-active-style
)
2044 (memq 'space-after-tab
::tab whitespace-active-style
)
2045 (memq 'space-after-tab
::space whitespace-active-style
)
2046 (memq 'space-before-tab whitespace-active-style
)
2047 (memq 'space-before-tab
::tab whitespace-active-style
)
2048 (memq 'space-before-tab
::space whitespace-active-style
))))
2051 (defun whitespace-color-on ()
2052 "Turn on color visualization."
2053 (when (whitespace-style-face-p)
2054 ;; save current point and refontify when necessary
2055 (set (make-local-variable 'whitespace-point
)
2057 (setq whitespace-point--used
2058 (let ((ol (make-overlay (point) (point) nil nil t
)))
2059 (delete-overlay ol
) ol
))
2060 (set (make-local-variable 'whitespace-font-lock-refontify
)
2062 (set (make-local-variable 'whitespace-bob-marker
)
2064 (set (make-local-variable 'whitespace-eob-marker
)
2066 (set (make-local-variable 'whitespace-buffer-changed
)
2068 (add-hook 'post-command-hook
#'whitespace-post-command-hook nil t
)
2069 (add-hook 'before-change-functions
#'whitespace-buffer-changed nil t
)
2070 ;; Add whitespace-mode color into font lock.
2072 whitespace-font-lock-keywords
2074 (whitespace-point--flush-used)
2075 ,@(when (memq 'spaces whitespace-active-style
)
2077 `((,whitespace-space-regexp
1 whitespace-space t
)
2078 ;; Show HARD SPACEs.
2079 (,whitespace-hspace-regexp
1 whitespace-hspace t
)))
2080 ,@(when (memq 'tabs whitespace-active-style
)
2082 `((,whitespace-tab-regexp
1 whitespace-tab t
)))
2083 ,@(when (memq 'trailing whitespace-active-style
)
2084 ;; Show trailing blanks.
2085 `((,#'whitespace-trailing-regexp
1 whitespace-trailing t
)))
2086 ,@(when (or (memq 'lines whitespace-active-style
)
2087 (memq 'lines-tail whitespace-active-style
))
2088 ;; Show "long" lines.
2089 `((,(let ((line-column (or whitespace-line-column fill-column
)))
2091 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2094 (/ line-column tab-width
)
2095 (let ((rem (% line-column tab-width
)))
2098 (format ".\\{%d\\}" rem
)))))
2099 ,(if (memq 'lines whitespace-active-style
)
2102 whitespace-line prepend
)))
2103 ,@(when (or (memq 'space-before-tab whitespace-active-style
)
2104 (memq 'space-before-tab
::tab whitespace-active-style
)
2105 (memq 'space-before-tab
::space whitespace-active-style
))
2106 `((,whitespace-space-before-tab-regexp
2108 ((memq 'space-before-tab whitespace-active-style
)
2109 ;; Show SPACEs before TAB (indent-tabs-mode).
2110 (if indent-tabs-mode
1 2))
2111 ((memq 'space-before-tab
::tab whitespace-active-style
)
2113 ((memq 'space-before-tab
::space whitespace-active-style
)
2115 whitespace-space-before-tab t
)))
2116 ,@(when (or (memq 'indentation whitespace-active-style
)
2117 (memq 'indentation
::tab whitespace-active-style
)
2118 (memq 'indentation
::space whitespace-active-style
))
2120 ((memq 'indentation whitespace-active-style
)
2121 ;; Show indentation SPACEs (indent-tabs-mode).
2122 (whitespace-indentation-regexp))
2123 ((memq 'indentation
::tab whitespace-active-style
)
2124 ;; Show indentation SPACEs (SPACEs).
2125 (whitespace-indentation-regexp 'tab
))
2126 ((memq 'indentation
::space whitespace-active-style
)
2127 ;; Show indentation SPACEs (TABs).
2128 (whitespace-indentation-regexp 'space
)))
2129 1 whitespace-indentation t
)))
2130 ,@(when (memq 'big-indent whitespace-active-style
)
2131 ;; Show big indentation.
2132 `((,whitespace-big-indent-regexp
1 'whitespace-big-indent t
)))
2133 ,@(when (memq 'empty whitespace-active-style
)
2134 ;; Show empty lines at beginning of buffer.
2135 `((,#'whitespace-empty-at-bob-regexp
2136 1 whitespace-empty t
)
2137 ;; Show empty lines at end of buffer.
2138 (,#'whitespace-empty-at-eob-regexp
2139 1 whitespace-empty t
)))
2140 ,@(when (or (memq 'space-after-tab whitespace-active-style
)
2141 (memq 'space-after-tab
::tab whitespace-active-style
)
2142 (memq 'space-after-tab
::space whitespace-active-style
))
2144 ((memq 'space-after-tab whitespace-active-style
)
2145 ;; Show SPACEs after TAB (indent-tabs-mode).
2146 (whitespace-space-after-tab-regexp))
2147 ((memq 'space-after-tab
::tab whitespace-active-style
)
2148 ;; Show SPACEs after TAB (SPACEs).
2149 (whitespace-space-after-tab-regexp 'tab
))
2150 ((memq 'space-after-tab
::space whitespace-active-style
)
2151 ;; Show SPACEs after TAB (TABs).
2152 (whitespace-space-after-tab-regexp 'space
)))
2153 1 whitespace-space-after-tab t
)))))
2154 (font-lock-add-keywords nil whitespace-font-lock-keywords t
)
2158 (defun whitespace-color-off ()
2159 "Turn off color visualization."
2160 ;; turn off font lock
2161 (kill-local-variable 'whitespace-point--used
)
2162 (when (whitespace-style-face-p)
2163 (remove-hook 'post-command-hook
#'whitespace-post-command-hook t
)
2164 (remove-hook 'before-change-functions
#'whitespace-buffer-changed t
)
2165 (font-lock-remove-keywords nil whitespace-font-lock-keywords
)
2168 (defun whitespace-point--used (start end
)
2169 (let ((ostart (overlay-start whitespace-point--used
)))
2171 (move-overlay whitespace-point--used
2173 (max end
(overlay-end whitespace-point--used
)))
2174 (move-overlay whitespace-point--used start end
))))
2176 (defun whitespace-point--flush-used (limit)
2177 (let ((ostart (overlay-start whitespace-point--used
)))
2178 ;; Strip parts of whitespace-point--used we're about to refresh.
2180 (let ((oend (overlay-end whitespace-point--used
)))
2181 (if (<= (point) ostart
)
2183 (delete-overlay whitespace-point--used
)
2184 (move-overlay whitespace-point--used limit oend
)))
2186 (move-overlay whitespace-point--used ostart
(point))))))
2189 (defun whitespace-trailing-regexp (limit)
2190 "Match trailing spaces which do not contain the point at end of line."
2192 (while (if (re-search-forward whitespace-trailing-regexp limit t
)
2193 (when (= whitespace-point
(match-end 1)) ; Loop if point at eol.
2194 (whitespace-point--used (match-beginning 0) (match-end 0))
2196 (setq status nil
))) ;; end of buffer
2200 (defun whitespace-empty-at-bob-regexp (limit)
2201 "Match spaces at beginning of buffer which do not contain the point at \
2202 beginning of buffer."
2208 (setq r
(and (looking-at whitespace-empty-at-bob-regexp
)
2209 (or (/= whitespace-point
1)
2210 (progn (whitespace-point--used (match-beginning 0)
2213 (set-marker whitespace-bob-marker
(if r
(match-end 1) b
)))
2214 ;; inside bob empty region
2215 ((<= limit whitespace-bob-marker
)
2216 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2218 (when (< (match-end 1) limit
)
2219 (set-marker whitespace-bob-marker
(match-end 1)))
2220 (set-marker whitespace-bob-marker b
)))
2221 ;; intersection with end of bob empty region
2222 ((<= b whitespace-bob-marker
)
2223 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2224 (set-marker whitespace-bob-marker
(if r
(match-end 1) b
)))
2225 ;; it is not inside bob empty region
2228 ;; move to end of matching
2229 (and r
(goto-char (match-end 1)))
2233 (defsubst whitespace-looking-back
(regexp limit
)
2235 (when (/= 0 (skip-chars-backward " \t\n" limit
))
2238 (looking-at regexp
))))
2241 (defun whitespace-empty-at-eob-regexp (limit)
2242 "Match spaces at end of buffer which do not contain the point at end of \
2245 (e (1+ (buffer-size)))
2251 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2252 (when (and r
(= whitespace-point e
))
2254 (whitespace-point--used (match-beginning 0) (match-end 0)))
2256 (set-marker whitespace-eob-marker
(match-beginning 1))
2257 (set-marker whitespace-eob-marker limit
)
2258 (goto-char b
))) ; return back to initial position
2259 ;; inside eob empty region
2260 ((>= b whitespace-eob-marker
)
2262 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2264 (when (> (match-beginning 1) b
)
2265 (set-marker whitespace-eob-marker
(match-beginning 1)))
2266 (set-marker whitespace-eob-marker limit
)
2267 (goto-char b
))) ; return back to initial position
2268 ;; intersection with beginning of eob empty region
2269 ((>= limit whitespace-eob-marker
)
2271 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2273 (set-marker whitespace-eob-marker
(match-beginning 1))
2274 (set-marker whitespace-eob-marker limit
)
2275 (goto-char b
))) ; return back to initial position
2276 ;; it is not inside eob empty region
2282 (defun whitespace-buffer-changed (_beg _end
)
2283 "Set `whitespace-buffer-changed' variable to t."
2284 (setq whitespace-buffer-changed t
))
2287 (defun whitespace-post-command-hook ()
2288 "Save current point into `whitespace-point' variable.
2289 Also refontify when necessary."
2290 (unless (and (eq whitespace-point
(point))
2291 (not whitespace-buffer-changed
))
2292 (setq whitespace-point
(point)) ; current point position
2295 ;; It is at end of buffer (eob).
2296 ((= whitespace-point
(1+ (buffer-size)))
2297 (when (whitespace-looking-back whitespace-empty-at-eob-regexp
2299 (match-beginning 0)))
2300 ;; It is at end of line ...
2302 ;; ... with trailing SPACE or TAB
2303 (or (memq (preceding-char) '(?\s ?
\t))))
2304 (line-beginning-position))
2305 ;; It is at beginning of buffer (bob).
2306 ((and (= whitespace-point
1)
2307 (looking-at whitespace-empty-at-bob-regexp
))
2309 (ostart (overlay-start whitespace-point--used
)))
2312 ;; New point does not affect highlighting: just refresh the
2313 ;; highlighting of old point, if needed.
2315 (font-lock-flush ostart
2316 (overlay-end whitespace-point--used
))
2317 (delete-overlay whitespace-point--used
)))
2319 ;; Old point did not affect highlighting, but new one does: refresh the
2320 ;; highlighting of new point.
2321 (font-lock-flush (min refontify
(point)) (max refontify
(point))))
2324 (setq ostart
(line-beginning-position))
2325 (and (<= ostart
(max refontify
(point)))
2327 (goto-char (overlay-end whitespace-point--used
))
2328 (let ((oend (line-beginning-position 2)))
2329 (<= (min refontify
(point)) oend
)))))
2330 ;; The old point highlighting and the new point highlighting
2331 ;; cover a contiguous region: do a single refresh.
2332 (font-lock-flush (min refontify
(point) ostart
)
2333 (max refontify
(point)
2334 (overlay-end whitespace-point--used
)))
2335 (delete-overlay whitespace-point--used
))
2337 (font-lock-flush (min refontify
(point))
2338 (max refontify
(point)))
2339 (font-lock-flush ostart
(overlay-end whitespace-point--used
))
2340 (delete-overlay whitespace-point--used
))))))
2343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2344 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2347 (defun whitespace-style-mark-p ()
2348 "Return t if there is some visualization via display table."
2349 (or (memq 'tab-mark whitespace-active-style
)
2350 (memq 'space-mark whitespace-active-style
)
2351 (memq 'newline-mark whitespace-active-style
)))
2354 (defsubst whitespace-char-valid-p
(char)
2355 ;; This check should be improved!!!
2360 (defun whitespace-display-vector-p (vec)
2361 "Return true if every character in vector VEC can be displayed."
2362 (let ((i (length vec
)))
2364 (while (and (>= (setq i
(1- i
)) 0)
2365 (whitespace-char-valid-p (aref vec i
))))
2369 (defun whitespace-display-char-on ()
2370 "Turn on character display mapping."
2371 (when (and whitespace-display-mappings
2372 (whitespace-style-mark-p))
2374 ;; Remember whether a buffer has a local display table.
2375 (unless whitespace-display-table-was-local
2376 (setq whitespace-display-table-was-local t
)
2377 (unless (or whitespace-mode global-whitespace-mode
)
2378 (setq whitespace-display-table
2379 (copy-sequence buffer-display-table
)))
2380 ;; Assure `buffer-display-table' is unique
2381 ;; when two or more windows are visible.
2382 (setq buffer-display-table
2383 (copy-sequence buffer-display-table
)))
2384 (unless buffer-display-table
2385 (setq buffer-display-table
(make-display-table)))
2386 (dolist (entry whitespace-display-mappings
)
2387 ;; check if it is to display this mark
2388 (when (memq (car entry
) whitespace-style
)
2389 ;; Get a displayable mapping.
2390 (setq vecs
(cddr entry
))
2392 (not (whitespace-display-vector-p (car vecs
))))
2393 (setq vecs
(cdr vecs
)))
2394 ;; Display a valid mapping.
2396 (setq vec
(copy-sequence (car vecs
)))
2398 (when (and (eq (cadr entry
) ?
\n)
2399 (memq 'newline whitespace-active-style
))
2400 ;; Only insert face bits on NEWLINE char mapping to avoid
2401 ;; obstruction of other faces like TABs and (HARD) SPACEs
2402 ;; faces, font-lock faces, etc.
2403 (dotimes (i (length vec
))
2404 (or (eq (aref vec i
) ?
\n)
2406 (make-glyph-code (aref vec i
)
2407 whitespace-newline
)))))
2409 (aset buffer-display-table
(cadr entry
) vec
)))))))
2412 (defun whitespace-display-char-off ()
2413 "Turn off character display mapping."
2414 (and whitespace-display-mappings
2415 (whitespace-style-mark-p)
2416 whitespace-display-table-was-local
2417 (setq whitespace-display-table-was-local nil
2418 buffer-display-table whitespace-display-table
)))
2421 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2425 (defun whitespace-action-when-on ()
2426 "Action to be taken always when local whitespace is turned on."
2427 (cond ((memq 'cleanup whitespace-action
)
2428 (whitespace-cleanup))
2429 ((memq 'report-on-bogus whitespace-action
)
2430 (whitespace-report nil t
))))
2433 (defun whitespace-write-file-hook ()
2434 "Action to be taken when buffer is written.
2435 It should be added buffer-locally to `write-file-functions'."
2436 (cond ((memq 'auto-cleanup whitespace-action
)
2437 (whitespace-cleanup))
2438 ((memq 'abort-on-bogus whitespace-action
)
2439 (when (whitespace-report nil t
)
2440 (error "Abort write due to whitespace problems in %s"
2442 nil
) ; continue hook processing
2445 (defun whitespace-warn-read-only (msg)
2446 "Warn if buffer is read-only."
2447 (when (memq 'warn-if-read-only whitespace-action
)
2448 (message "Can't %s: %s is read-only" msg
(buffer-name))))
2451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2454 (defun whitespace-unload-function ()
2455 "Unload the whitespace library."
2456 (global-whitespace-mode -
1)
2457 ;; be sure all local whitespace mode is turned off
2458 (save-current-buffer
2459 (dolist (buf (buffer-list))
2461 (whitespace-mode -
1)))
2462 nil
) ; continue standard unloading
2465 (provide 'whitespace
)
2468 (run-hooks 'whitespace-load-hook
)
2471 ;;; whitespace.el ends here