1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
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 <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.
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-indent-tabs-mode indent-tabs-mode
1138 "Used to save locally `indent-tabs-mode' value.")
1140 (defvar whitespace-tab-width tab-width
1141 "Used to save locally `tab-width' value.")
1143 (defvar whitespace-point
(point)
1144 "Used to save locally current point value.
1145 Used by function `whitespace-trailing-regexp' (which see).")
1146 (defvar-local whitespace-point--used nil
1147 "Region whose highlighting depends on `whitespace-point'.")
1149 (defvar whitespace-font-lock-refontify nil
1150 "Used to save locally the font-lock refontify state.
1151 Used by function `whitespace-post-command-hook' (which see).")
1153 (defvar whitespace-bob-marker nil
1154 "Used to save locally the bob marker value.
1155 Used by function `whitespace-post-command-hook' (which see).")
1157 (defvar whitespace-eob-marker nil
1158 "Used to save locally the eob marker value.
1159 Used by function `whitespace-post-command-hook' (which see).")
1161 (defvar whitespace-buffer-changed nil
1162 "Used to indicate locally if buffer changed.
1163 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1164 functions (which see).")
1168 (defun whitespace-toggle-options (arg)
1169 "Toggle local `whitespace-mode' options.
1171 If local whitespace-mode is off, toggle the option given by ARG
1172 and turn on local whitespace-mode.
1174 If local whitespace-mode is on, toggle the option given by ARG
1175 and restart local whitespace-mode.
1177 Interactively, it reads one of the following chars:
1181 f toggle face visualization
1182 t toggle TAB visualization
1183 s toggle SPACE and HARD SPACE visualization
1184 r toggle trailing blanks visualization
1185 l toggle \"long lines\" visualization
1186 L toggle \"long lines\" tail visualization
1187 n toggle NEWLINE visualization
1188 e toggle empty line at bob and/or eob visualization
1189 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1190 I toggle indentation SPACEs visualization
1191 i toggle indentation TABs visualization
1192 C-t toggle big indentation visualization
1193 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1194 A toggle SPACEs after TAB: SPACEs visualization
1195 a toggle SPACEs after TAB: TABs visualization
1196 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1197 B toggle SPACEs before TAB: SPACEs visualization
1198 b toggle SPACEs before TAB: TABs visualization
1201 T toggle TAB visualization
1202 S toggle SPACEs before TAB visualization
1203 N toggle NEWLINE visualization
1205 x restore `whitespace-style' value
1206 ? display brief help
1208 Non-interactively, ARG should be a symbol or a list of symbols.
1209 The valid symbols are:
1211 face toggle face visualization
1212 tabs toggle TAB visualization
1213 spaces toggle SPACE and HARD SPACE visualization
1214 trailing toggle trailing blanks visualization
1215 lines toggle \"long lines\" visualization
1216 lines-tail toggle \"long lines\" tail visualization
1217 newline toggle NEWLINE visualization
1218 empty toggle empty line at bob and/or eob visualization
1219 indentation toggle indentation SPACEs visualization
1220 indentation::tab toggle indentation SPACEs visualization
1221 indentation::space toggle indentation TABs visualization
1222 big-indent toggle big indentation visualization
1223 space-after-tab toggle SPACEs after TAB visualization
1224 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1225 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1226 space-before-tab toggle SPACEs before TAB visualization
1227 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1228 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1230 tab-mark toggle TAB visualization
1231 space-mark toggle SPACEs before TAB visualization
1232 newline-mark toggle NEWLINE visualization
1234 whitespace-style restore `whitespace-style' value
1236 See `whitespace-style' and `indent-tabs-mode' for documentation."
1237 (interactive (whitespace-interactive-char t
))
1238 (let ((whitespace-style
1239 (whitespace-toggle-list t arg whitespace-active-style
)))
1241 (whitespace-mode 1)))
1244 (defvar whitespace-toggle-style nil
1245 "Used to toggle the global `whitespace-style' value.")
1249 (defun global-whitespace-toggle-options (arg)
1250 "Toggle global `whitespace-mode' options.
1252 If global whitespace-mode is off, toggle the option given by ARG
1253 and turn on global whitespace-mode.
1255 If global whitespace-mode is on, toggle the option given by ARG
1256 and restart global whitespace-mode.
1258 Interactively, it accepts one of the following chars:
1262 f toggle face visualization
1263 t toggle TAB visualization
1264 s toggle SPACE and HARD SPACE visualization
1265 r toggle trailing blanks visualization
1266 l toggle \"long lines\" visualization
1267 L toggle \"long lines\" tail visualization
1268 n toggle NEWLINE visualization
1269 e toggle empty line at bob and/or eob visualization
1270 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1271 I toggle indentation SPACEs visualization
1272 i toggle indentation TABs visualization
1273 C-t toggle big indentation visualization
1274 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1275 A toggle SPACEs after TAB: SPACEs visualization
1276 a toggle SPACEs after TAB: TABs visualization
1277 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1278 B toggle SPACEs before TAB: SPACEs visualization
1279 b toggle SPACEs before TAB: TABs visualization
1282 T toggle TAB visualization
1283 S toggle SPACEs before TAB visualization
1284 N toggle NEWLINE visualization
1286 x restore `whitespace-style' value
1287 ? display brief help
1289 Non-interactively, ARG should be a symbol or a list of symbols.
1290 The valid symbols are:
1292 face toggle face visualization
1293 tabs toggle TAB visualization
1294 spaces toggle SPACE and HARD SPACE visualization
1295 trailing toggle trailing blanks visualization
1296 lines toggle \"long lines\" visualization
1297 lines-tail toggle \"long lines\" tail visualization
1298 newline toggle NEWLINE visualization
1299 empty toggle empty line at bob and/or eob visualization
1300 indentation toggle indentation SPACEs visualization
1301 indentation::tab toggle indentation SPACEs visualization
1302 indentation::space toggle indentation TABs visualization
1303 big-indent toggle big indentation visualization
1304 space-after-tab toggle SPACEs after TAB visualization
1305 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1306 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1307 space-before-tab toggle SPACEs before TAB visualization
1308 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1309 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1311 tab-mark toggle TAB visualization
1312 space-mark toggle SPACEs before TAB visualization
1313 newline-mark toggle NEWLINE visualization
1315 whitespace-style restore `whitespace-style' value
1317 See `whitespace-style' and `indent-tabs-mode' for documentation."
1318 (interactive (whitespace-interactive-char nil
))
1319 (let ((whitespace-style
1320 (whitespace-toggle-list nil arg whitespace-toggle-style
)))
1321 (setq whitespace-toggle-style whitespace-style
)
1322 (global-whitespace-mode 0)
1323 (global-whitespace-mode 1)))
1326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1327 ;;;; User commands - Cleanup
1331 (defun whitespace-cleanup ()
1332 "Cleanup some blank problems in all buffer or at region.
1334 It usually applies to the whole buffer, but in transient mark
1335 mode when the mark is active, it applies to the region. It also
1336 applies to the region when it is not in transient mark mode, the
1337 mark is active and \\[universal-argument] was pressed just before
1338 calling `whitespace-cleanup' interactively.
1340 See also `whitespace-cleanup-region'.
1342 The problems cleaned up are:
1344 1. empty lines at beginning of buffer.
1345 2. empty lines at end of buffer.
1346 If `whitespace-style' includes the value `empty', remove all
1347 empty lines at beginning and/or end of buffer.
1349 3. `tab-width' or more SPACEs at beginning of line.
1350 If `whitespace-style' includes the value `indentation':
1351 replace `tab-width' or more SPACEs at beginning of line by
1352 TABs, if `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1354 If `whitespace-style' includes the value `indentation::tab',
1355 replace `tab-width' or more SPACEs at beginning of line by TABs.
1356 If `whitespace-style' includes the value `indentation::space',
1357 replace TABs by SPACEs.
1359 4. SPACEs before TAB.
1360 If `whitespace-style' includes the value `space-before-tab':
1361 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1362 otherwise, replace TABs by SPACEs.
1363 If `whitespace-style' includes the value
1364 `space-before-tab::tab', replace SPACEs by TABs.
1365 If `whitespace-style' includes the value
1366 `space-before-tab::space', replace TABs by SPACEs.
1368 5. SPACEs or TABs at end of line.
1369 If `whitespace-style' includes the value `trailing', remove
1370 all SPACEs or TABs at end of line.
1372 6. `tab-width' or more SPACEs after TAB.
1373 If `whitespace-style' includes the value `space-after-tab':
1374 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1375 otherwise, replace TABs by SPACEs.
1376 If `whitespace-style' includes the value
1377 `space-after-tab::tab', replace SPACEs by TABs.
1378 If `whitespace-style' includes the value
1379 `space-after-tab::space', replace TABs by SPACEs.
1381 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1387 (whitespace-warn-read-only "cleanup"))
1389 ((and (or transient-mark-mode
1392 ;; PROBLEMs 1 and 2 are not handled in region
1393 ;; PROBLEM 3: `tab-width' or more SPACEs at bol
1394 ;; PROBLEM 4: SPACEs before TAB
1395 ;; PROBLEM 5: SPACEs or TABs at eol
1396 ;; PROBLEM 6: `tab-width' or more SPACEs after TAB
1397 (whitespace-cleanup-region (region-beginning) (region-end)))
1401 ;; PROBLEM 1: empty lines at bob
1402 ;; PROBLEM 2: empty lines at eob
1403 ;; ACTION: remove all empty lines at bob and/or eob
1404 (when (memq 'empty whitespace-style
)
1405 (let (overwrite-mode) ; enforce no overwrite
1406 (goto-char (point-min))
1407 (when (looking-at whitespace-empty-at-bob-regexp
)
1408 (delete-region (match-beginning 1) (match-end 1)))
1409 (when (re-search-forward
1410 whitespace-empty-at-eob-regexp nil t
)
1411 (delete-region (match-beginning 1) (match-end 1))))))
1412 ;; PROBLEM 3: `tab-width' or more SPACEs at bol
1413 ;; PROBLEM 4: SPACEs before TAB
1414 ;; PROBLEM 5: SPACEs or TABs at eol
1415 ;; PROBLEM 6: `tab-width' or more SPACEs after TAB
1416 (whitespace-cleanup-region (point-min) (point-max)))))
1418 (defun whitespace-ensure-local-variables ()
1419 "Set `whitespace-indent-tabs-mode' and `whitespace-tab-width' locally."
1420 (set (make-local-variable 'whitespace-indent-tabs-mode
)
1422 (set (make-local-variable 'whitespace-tab-width
)
1426 (defun whitespace-cleanup-region (start end
)
1427 "Cleanup some blank problems at region.
1429 The problems cleaned up are:
1431 1. `tab-width' or more SPACEs at beginning of line.
1432 If `whitespace-style' includes the value `indentation':
1433 replace `tab-width' or more SPACEs at beginning of line by TABs,
1434 if `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1436 If `whitespace-style' includes the value `indentation::tab',
1437 replace `tab-width' or more SPACEs at beginning of line by TABs.
1438 If `whitespace-style' includes the value `indentation::space',
1439 replace TABs by SPACEs.
1441 2. SPACEs before TAB.
1442 If `whitespace-style' includes the value `space-before-tab':
1443 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1444 otherwise, replace TABs by SPACEs.
1445 If `whitespace-style' includes the value
1446 `space-before-tab::tab', replace SPACEs by TABs.
1447 If `whitespace-style' includes the value
1448 `space-before-tab::space', replace TABs by SPACEs.
1450 3. SPACEs or TABs at end of line.
1451 If `whitespace-style' includes the value `trailing', remove
1452 all SPACEs or TABs at end of line.
1454 4. `tab-width' or more SPACEs after TAB.
1455 If `whitespace-style' includes the value `space-after-tab':
1456 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1457 otherwise, replace TABs by SPACEs.
1458 If `whitespace-style' includes the value
1459 `space-after-tab::tab', replace SPACEs by TABs.
1460 If `whitespace-style' includes the value
1461 `space-after-tab::space', replace TABs by SPACEs.
1463 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1466 (if buffer-read-only
1468 (whitespace-warn-read-only "cleanup region")
1469 ;; non-read-only buffer
1470 (whitespace-ensure-local-variables)
1471 (let ((rstart (min start end
))
1472 (rend (copy-marker (max start end
)))
1473 (indent-tabs-mode whitespace-indent-tabs-mode
)
1474 (tab-width whitespace-tab-width
)
1475 overwrite-mode
; enforce no overwrite
1478 ;; PROBLEM 1: `tab-width' or more SPACEs at bol
1480 ;; ACTION: replace `tab-width' or more SPACEs at bol by TABs, if
1481 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1483 ((memq 'indentation whitespace-style
)
1484 (let ((regexp (whitespace-indentation-regexp)))
1486 (while (re-search-forward regexp rend t
)
1487 (setq tmp
(current-indentation))
1488 (goto-char (match-beginning 0))
1489 (delete-horizontal-space)
1492 ;; ACTION: replace `tab-width' or more SPACEs at bol by TABs.
1493 ((memq 'indentation
::tab whitespace-style
)
1494 (whitespace-replace-action
1496 (whitespace-indentation-regexp 'tab
) 0))
1497 ;; ACTION: replace TABs by SPACEs.
1498 ((memq 'indentation
::space whitespace-style
)
1499 (whitespace-replace-action
1500 'untabify rstart rend
1501 (whitespace-indentation-regexp 'space
) 0)))
1502 ;; PROBLEM 3: SPACEs or TABs at eol
1503 ;; ACTION: remove all SPACEs or TABs at eol
1504 (when (memq 'trailing whitespace-style
)
1505 (whitespace-replace-action
1506 'delete-region rstart rend
1507 whitespace-trailing-regexp
1))
1508 ;; PROBLEM 4: `tab-width' or more SPACEs after TAB
1510 ;; ACTION: replace `tab-width' or more SPACEs by TABs, if
1511 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1513 ((memq 'space-after-tab whitespace-style
)
1514 (whitespace-replace-action
1515 (if whitespace-indent-tabs-mode
'tabify
'untabify
)
1516 rstart rend
(whitespace-space-after-tab-regexp) 1))
1517 ;; ACTION: replace `tab-width' or more SPACEs by TABs.
1518 ((memq 'space-after-tab
::tab whitespace-style
)
1519 (whitespace-replace-action
1521 (whitespace-space-after-tab-regexp 'tab
) 1))
1522 ;; ACTION: replace TABs by SPACEs.
1523 ((memq 'space-after-tab
::space whitespace-style
)
1524 (whitespace-replace-action
1525 'untabify rstart rend
1526 (whitespace-space-after-tab-regexp 'space
) 1)))
1527 ;; PROBLEM 2: SPACEs before TAB
1529 ;; ACTION: replace SPACEs before TAB by TABs, if
1530 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1532 ((memq 'space-before-tab whitespace-style
)
1533 (whitespace-replace-action
1534 (if whitespace-indent-tabs-mode
'tabify
'untabify
)
1535 rstart rend whitespace-space-before-tab-regexp
1536 (if whitespace-indent-tabs-mode
0 2)))
1537 ;; ACTION: replace SPACEs before TAB by TABs.
1538 ((memq 'space-before-tab
::tab whitespace-style
)
1539 (whitespace-replace-action
1541 whitespace-space-before-tab-regexp
0))
1542 ;; ACTION: replace TABs by SPACEs.
1543 ((memq 'space-before-tab
::space whitespace-style
)
1544 (whitespace-replace-action
1545 'untabify rstart rend
1546 whitespace-space-before-tab-regexp
2))))
1547 (set-marker rend nil
)))) ; point marker to nowhere
1550 (defun whitespace-replace-action (action rstart rend regexp index
)
1551 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1553 INDEX is the level group matched by REGEXP and used by ACTION.
1555 See also `tab-width'."
1557 (while (re-search-forward regexp rend t
)
1558 (goto-char (match-end index
))
1559 (funcall action
(match-beginning index
) (match-end index
))))
1562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1563 ;;;; User command - report
1566 (defun whitespace-regexp (regexp &optional kind
)
1567 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1571 whitespace-indent-tabs-mode
)
1573 ((or (eq kind
'space
)
1574 (not whitespace-indent-tabs-mode
))
1576 whitespace-tab-width
))
1579 (defun whitespace-indentation-regexp (&optional kind
)
1580 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1581 (whitespace-regexp whitespace-indentation-regexp kind
))
1584 (defun whitespace-space-after-tab-regexp (&optional kind
)
1585 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1586 (whitespace-regexp whitespace-space-after-tab-regexp kind
))
1589 (defconst whitespace-report-list
1591 (cons 'empty whitespace-empty-at-bob-regexp
)
1592 (cons 'empty whitespace-empty-at-eob-regexp
)
1593 (cons 'trailing whitespace-trailing-regexp
)
1594 (cons 'indentation nil
)
1595 (cons 'indentation
::tab nil
)
1596 (cons 'indentation
::space nil
)
1597 (cons 'space-before-tab whitespace-space-before-tab-regexp
)
1598 (cons 'space-before-tab
::tab whitespace-space-before-tab-regexp
)
1599 (cons 'space-before-tab
::space whitespace-space-before-tab-regexp
)
1600 (cons 'space-after-tab nil
)
1601 (cons 'space-after-tab
::tab nil
)
1602 (cons 'space-after-tab
::space nil
)
1604 "List of whitespace bogus symbol and corresponding regexp.")
1607 (defconst whitespace-report-text
1608 '( ;; `indent-tabs-mode' has non-nil value
1612 Current Setting Whitespace Problem
1614 empty [] [] empty lines at beginning of buffer
1615 empty [] [] empty lines at end of buffer
1616 trailing [] [] SPACEs or TABs at end of line
1617 indentation [] [] >= `tab-width' SPACEs at beginning of line
1618 indentation::tab [] [] >= `tab-width' SPACEs at beginning of line
1619 indentation::space [] [] TABs at beginning of line
1620 space-before-tab [] [] SPACEs before TAB
1621 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1622 space-before-tab::space [] [] SPACEs before TAB: TABs
1623 space-after-tab [] [] >= `tab-width' SPACEs after TAB
1624 space-after-tab::tab [] [] >= `tab-width' SPACEs after TAB: SPACEs
1625 space-after-tab::space [] [] >= `tab-width' SPACEs after TAB: TABs
1629 .
;; `indent-tabs-mode' has nil value
1633 Current Setting Whitespace Problem
1635 empty [] [] empty lines at beginning of buffer
1636 empty [] [] empty lines at end of buffer
1637 trailing [] [] SPACEs or TABs at end of line
1638 indentation [] [] TABs at beginning of line
1639 indentation::tab [] [] >= `tab-width' SPACEs at beginning of line
1640 indentation::space [] [] TABs at beginning of line
1641 space-before-tab [] [] SPACEs before TAB
1642 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1643 space-before-tab::space [] [] SPACEs before TAB: TABs
1644 space-after-tab [] [] >= `tab-width' SPACEs after TAB
1645 space-after-tab::tab [] [] >= `tab-width' SPACEs after TAB: SPACEs
1646 space-after-tab::space [] [] >= `tab-width' SPACEs after TAB: TABs
1650 "Text for whitespace bogus report.
1652 It is a cons of strings, where the car part is used when
1653 `indent-tabs-mode' is non-nil, and the cdr part is used when
1654 `indent-tabs-mode' is nil.")
1657 (defconst whitespace-report-buffer-name
"*Whitespace Report*"
1658 "The buffer name for whitespace bogus report.")
1662 (defun whitespace-report (&optional force report-if-bogus
)
1663 "Report some whitespace problems in buffer.
1665 Perform `whitespace-report-region' on the current buffer."
1666 (interactive (list current-prefix-arg
))
1667 (whitespace-report-region (point-min) (point-max)
1668 force report-if-bogus
))
1672 (defun whitespace-report-region (start end
&optional force report-if-bogus
)
1673 "Report some whitespace problems in a region.
1675 Return nil if there is no whitespace problem; otherwise, return
1678 If FORCE is non-nil or \\[universal-argument] was pressed just
1679 before calling `whitespace-report-region' interactively, it
1680 forces all classes of whitespace problem to be considered
1683 If REPORT-IF-BOGUS is t, it reports only when there are any
1684 whitespace problems in buffer; if it is `never', it does not
1687 Report if some of the following whitespace problems exist:
1689 * If `indent-tabs-mode' is non-nil:
1690 empty 1. empty lines at beginning of buffer.
1691 empty 2. empty lines at end of buffer.
1692 trailing 3. SPACEs or TABs at end of line.
1693 indentation 4. line starts with `tab-width' or more SPACEs.
1694 space-before-tab 5. SPACEs before TAB.
1695 space-after-tab 6. `tab-width' or more SPACEs after TAB.
1697 * If `indent-tabs-mode' is nil:
1698 empty 1. empty lines at beginning of buffer.
1699 empty 2. empty lines at end of buffer.
1700 trailing 3. SPACEs or TABs at end of line.
1701 indentation 4. TABS at beginning of line.
1702 space-before-tab 5. SPACEs before TAB.
1703 space-after-tab 6. `tab-width' or more SPACEs after TAB.
1705 See `whitespace-style' for documentation.
1706 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1707 cleaning up these problems."
1709 (setq force
(or current-prefix-arg force
))
1711 (let* ((has-bogus nil
)
1712 (rstart (min start end
))
1713 (rend (max start end
))
1714 ;; Fall back to whitespace-style so we can run before
1715 ;; before the mode is active.
1716 (style (copy-sequence
1717 (or whitespace-active-style whitespace-style
)))
1722 (add-to-list 'style
(car option
)))
1726 ((eq (car option
) 'indentation
)
1727 (whitespace-indentation-regexp))
1728 ((eq (car option
) 'indentation
::tab
)
1729 (whitespace-indentation-regexp 'tab
))
1730 ((eq (car option
) 'indentation
::space
)
1731 (whitespace-indentation-regexp 'space
))
1732 ((eq (car option
) 'space-after-tab
)
1733 (whitespace-space-after-tab-regexp))
1734 ((eq (car option
) 'space-after-tab
::tab
)
1735 (whitespace-space-after-tab-regexp 'tab
))
1736 ((eq (car option
) 'space-after-tab
::space
)
1737 (whitespace-space-after-tab-regexp 'space
))
1740 (when (re-search-forward regexp rend t
)
1742 (setq has-bogus
(memq (car option
) style
)))
1744 whitespace-report-list
)))
1745 (when (pcase report-if-bogus
(`nil t
) (`never nil
) (_ has-bogus
))
1746 (whitespace-kill-buffer whitespace-report-buffer-name
)
1747 ;; `whitespace-indent-tabs-mode' is local to current buffer
1748 ;; `whitespace-tab-width' is local to current buffer
1749 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode
)
1750 (ws-tab-width whitespace-tab-width
))
1751 (with-current-buffer (get-buffer-create
1752 whitespace-report-buffer-name
)
1754 (insert (if ws-indent-tabs-mode
1755 (car whitespace-report-text
)
1756 (cdr whitespace-report-text
)))
1757 (goto-char (point-min))
1759 (dolist (option whitespace-report-list
)
1762 27 (memq (car option
) style
))
1763 (whitespace-mark-x 7 (car bogus-list
))
1764 (setq bogus-list
(cdr bogus-list
)))
1766 (whitespace-insert-value ws-indent-tabs-mode
)
1767 (whitespace-insert-value ws-tab-width
)
1769 (goto-char (point-max))
1770 (insert (substitute-command-keys
1771 " Type `\\[whitespace-cleanup]'")
1772 " to cleanup the buffer.\n\n"
1773 (substitute-command-keys
1774 " Type `\\[whitespace-cleanup-region]'")
1775 " to cleanup a region.\n\n"))
1776 (whitespace-display-window (current-buffer)))))
1780 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1781 ;;;; Internal functions
1784 (defvar whitespace-font-lock-keywords nil
1785 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1788 (defconst whitespace-help-text
1790 Whitespace Toggle Options | scroll up : SPC or > |
1791 | scroll down: M-SPC or < |
1792 FACES \\__________________________/
1793 [] f - toggle face visualization
1794 [] t - toggle TAB visualization
1795 [] s - toggle SPACE and HARD SPACE visualization
1796 [] r - toggle trailing blanks visualization
1797 [] l - toggle \"long lines\" visualization
1798 [] L - toggle \"long lines\" tail visualization
1799 [] n - toggle NEWLINE visualization
1800 [] e - toggle empty line at bob and/or eob visualization
1801 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1802 [] I - toggle indentation SPACEs visualization
1803 [] i - toggle indentation TABs visualization
1804 [] C-t - toggle big indentation visualization
1805 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1806 [] A - toggle SPACEs after TAB: SPACEs visualization
1807 [] a - toggle SPACEs after TAB: TABs visualization
1808 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1809 [] B - toggle SPACEs before TAB: SPACEs visualization
1810 [] b - toggle SPACEs before TAB: TABs visualization
1813 [] T - toggle TAB visualization
1814 [] S - toggle SPACE and HARD SPACE visualization
1815 [] N - toggle NEWLINE visualization
1817 x - restore `whitespace-style' value
1819 ? - display this text\n\n"
1820 "Text for whitespace toggle options.")
1823 (defconst whitespace-help-buffer-name
"*Whitespace Toggle Options*"
1824 "The buffer name for whitespace toggle options.")
1827 (defun whitespace-insert-value (value)
1828 "Insert VALUE at column 20 of next line."
1830 (move-to-column 20 t
)
1831 (insert (format "%s" value
)))
1834 (defun whitespace-mark-x (nchars condition
)
1835 "Insert the mark (`X' or ` ') after NCHARS depending on CONDITION."
1836 (forward-char nchars
)
1837 (insert (if condition
"X" " ")))
1840 (defun whitespace-insert-option-mark (the-list the-value
)
1841 "Insert the option mark (`X' or ` ') in toggle options buffer."
1842 (goto-char (point-min))
1844 (dolist (sym the-list
)
1845 (if (eq sym
'help-newline
)
1848 (whitespace-mark-x 2 (memq sym the-value
)))))
1851 (defun whitespace-help-on (style)
1852 "Display the whitespace toggle options."
1853 (unless (get-buffer whitespace-help-buffer-name
)
1854 (delete-other-windows)
1855 (let ((buffer (get-buffer-create whitespace-help-buffer-name
)))
1856 (with-current-buffer buffer
1858 (insert whitespace-help-text
)
1859 (whitespace-insert-option-mark
1860 whitespace-style-value-list style
)
1861 (whitespace-display-window buffer
)))))
1864 (defun whitespace-display-window (buffer)
1865 "Display BUFFER in a new window."
1866 (goto-char (point-min))
1867 (set-buffer-modified-p nil
)
1868 (when (< (window-height) (* 2 window-min-height
))
1869 (kill-buffer buffer
)
1870 (error "Window height is too small; \
1871 can't split window to display whitespace toggle options"))
1872 (let ((win (split-window)))
1873 (set-window-buffer win buffer
)
1874 (shrink-window-if-larger-than-buffer win
)))
1877 (defun whitespace-kill-buffer (buffer-name)
1878 "Kill buffer BUFFER-NAME and windows related with it."
1879 (let ((buffer (get-buffer buffer-name
)))
1881 (delete-windows-on buffer
)
1882 (kill-buffer buffer
))))
1885 (defun whitespace-help-off ()
1886 "Remove the buffer and window of the whitespace toggle options."
1887 (whitespace-kill-buffer whitespace-help-buffer-name
))
1890 (defun whitespace-help-scroll (&optional up
)
1891 "Scroll help window, if it exists.
1893 If UP is non-nil, scroll up; otherwise, scroll down."
1895 (let ((buffer (get-buffer whitespace-help-buffer-name
)))
1897 (with-selected-window (get-buffer-window buffer
)
1904 ;; just ignore error
1908 (defun whitespace-interactive-char (local-p)
1909 "Interactive function to read a char and return a symbol.
1911 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1912 uses a global context.
1914 It accepts one of the following chars:
1918 f toggle face visualization
1919 t toggle TAB visualization
1920 s toggle SPACE and HARD SPACE visualization
1921 r toggle trailing blanks visualization
1922 l toggle \"long lines\" visualization
1923 L toggle \"long lines\" tail visualization
1924 n toggle NEWLINE visualization
1925 e toggle empty line at bob and/or eob visualization
1926 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1927 I toggle indentation SPACEs visualization
1928 i toggle indentation TABs visualization
1929 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1930 A toggle SPACEs after TAB: SPACEs visualization
1931 a toggle SPACEs after TAB: TABs visualization
1932 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1933 B toggle SPACEs before TAB: SPACEs visualization
1934 b toggle SPACEs before TAB: TABs visualization
1937 T toggle TAB visualization
1938 S toggle SPACE and HARD SPACE visualization
1939 N toggle NEWLINE visualization
1941 x restore `whitespace-style' value
1942 ? display brief help
1944 See also `whitespace-toggle-option-alist'."
1945 (let* ((is-off (not (if local-p
1947 global-whitespace-mode
)))
1948 (style (cond (is-off whitespace-style
) ; use default value
1949 (local-p whitespace-active-style
)
1950 (t whitespace-toggle-style
)))
1952 (format "Whitespace Toggle %s (type ? for further options)-"
1953 (if local-p
"Local" "Global")))
1955 ;; read a valid option and get the corresponding symbol
1956 (save-window-excursion
1957 (condition-case data
1962 (setq ch
(read-char prompt
))
1966 (assq ch whitespace-toggle-option-alist
)))))
1969 ((eq ch ?
\?) (whitespace-help-on style
))
1970 ((eq ch ?\
) (whitespace-help-scroll t
))
1971 ((eq ch ?\M-
) (whitespace-help-scroll))
1972 ((eq ch ?
>) (whitespace-help-scroll t
))
1973 ((eq ch ?
<) (whitespace-help-scroll))
1975 (whitespace-help-off)
1976 (message " ")) ; clean echo area
1979 (whitespace-help-off)
1980 (error (error-message-string data
)))))
1981 (list sym
))) ; return the appropriate symbol
1984 (defun whitespace-toggle-list (local-p arg the-list
)
1985 "Toggle options in THE-LIST based on list ARG.
1987 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1988 uses a global context.
1990 ARG is a list of options to be toggled.
1992 THE-LIST is a list of options. This list will be toggled and the
1993 resultant list will be returned."
1994 (unless (if local-p whitespace-mode global-whitespace-mode
)
1995 (setq the-list whitespace-style
))
1996 (setq the-list
(copy-sequence the-list
)) ; keep original list
1997 (dolist (sym (if (listp arg
) arg
(list arg
)))
1999 ;; ignore help value
2000 ((eq sym
'help-newline
))
2001 ;; restore default values
2002 ((eq sym
'whitespace-style
)
2003 (setq the-list whitespace-style
))
2004 ;; toggle valid values
2005 ((memq sym whitespace-style-value-list
)
2006 (setq the-list
(if (memq sym the-list
)
2008 (cons sym the-list
))))))
2012 (defvar whitespace-display-table nil
2013 "Used to save a local display table.")
2015 (defvar whitespace-display-table-was-local nil
2016 "Used to remember whether a buffer initially had a local display table.")
2018 (defun whitespace-turn-on ()
2019 "Turn on whitespace visualization."
2020 ;; prepare local hooks
2021 (add-hook 'write-file-functions
'whitespace-write-file-hook nil t
)
2022 ;; create whitespace local buffer environment
2023 (set (make-local-variable 'whitespace-font-lock-keywords
) nil
)
2024 (set (make-local-variable 'whitespace-display-table
) nil
)
2025 (set (make-local-variable 'whitespace-display-table-was-local
) nil
)
2026 (set (make-local-variable 'whitespace-active-style
)
2027 (if (listp whitespace-style
)
2029 (list whitespace-style
)))
2030 (whitespace-ensure-local-variables)
2031 ;; turn on whitespace
2032 (when whitespace-active-style
2033 (whitespace-color-on)
2034 (whitespace-display-char-on)))
2037 (defun whitespace-turn-off ()
2038 "Turn off whitespace visualization."
2039 (remove-hook 'write-file-functions
'whitespace-write-file-hook t
)
2040 (when whitespace-active-style
2041 (whitespace-color-off)
2042 (whitespace-display-char-off)))
2045 (defun whitespace-style-face-p ()
2046 "Return t if there is some visualization via face."
2047 (and (memq 'face whitespace-active-style
)
2048 (or (memq 'tabs whitespace-active-style
)
2049 (memq 'spaces whitespace-active-style
)
2050 (memq 'trailing whitespace-active-style
)
2051 (memq 'lines whitespace-active-style
)
2052 (memq 'lines-tail whitespace-active-style
)
2053 (memq 'newline whitespace-active-style
)
2054 (memq 'empty whitespace-active-style
)
2055 (memq 'indentation whitespace-active-style
)
2056 (memq 'indentation
::tab whitespace-active-style
)
2057 (memq 'indentation
::space whitespace-active-style
)
2058 (memq 'big-indent whitespace-active-style
)
2059 (memq 'space-after-tab whitespace-active-style
)
2060 (memq 'space-after-tab
::tab whitespace-active-style
)
2061 (memq 'space-after-tab
::space whitespace-active-style
)
2062 (memq 'space-before-tab whitespace-active-style
)
2063 (memq 'space-before-tab
::tab whitespace-active-style
)
2064 (memq 'space-before-tab
::space whitespace-active-style
))))
2067 (defun whitespace-color-on ()
2068 "Turn on color visualization."
2069 (when (whitespace-style-face-p)
2070 ;; save current point and refontify when necessary
2071 (set (make-local-variable 'whitespace-point
)
2073 (setq whitespace-point--used
2074 (let ((ol (make-overlay (point) (point) nil nil t
)))
2075 (delete-overlay ol
) ol
))
2076 (set (make-local-variable 'whitespace-font-lock-refontify
)
2078 (set (make-local-variable 'whitespace-bob-marker
)
2080 (set (make-local-variable 'whitespace-eob-marker
)
2082 (set (make-local-variable 'whitespace-buffer-changed
)
2084 (add-hook 'post-command-hook
#'whitespace-post-command-hook nil t
)
2085 (add-hook 'before-change-functions
#'whitespace-buffer-changed nil t
)
2086 ;; Add whitespace-mode color into font lock.
2088 whitespace-font-lock-keywords
2090 (whitespace-point--flush-used)
2091 ,@(when (memq 'spaces whitespace-active-style
)
2093 `((,whitespace-space-regexp
1 whitespace-space t
)
2094 ;; Show HARD SPACEs.
2095 (,whitespace-hspace-regexp
1 whitespace-hspace t
)))
2096 ,@(when (memq 'tabs whitespace-active-style
)
2098 `((,whitespace-tab-regexp
1 whitespace-tab t
)))
2099 ,@(when (memq 'trailing whitespace-active-style
)
2100 ;; Show trailing blanks.
2101 `((,#'whitespace-trailing-regexp
1 whitespace-trailing t
)))
2102 ,@(when (or (memq 'lines whitespace-active-style
)
2103 (memq 'lines-tail whitespace-active-style
))
2104 ;; Show "long" lines.
2105 `((,(let ((line-column (or whitespace-line-column fill-column
)))
2107 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2108 whitespace-tab-width
2109 (1- whitespace-tab-width
)
2110 (/ line-column whitespace-tab-width
)
2111 (let ((rem (% line-column whitespace-tab-width
)))
2114 (format ".\\{%d\\}" rem
)))))
2115 ,(if (memq 'lines whitespace-active-style
)
2118 whitespace-line prepend
)))
2119 ,@(when (or (memq 'space-before-tab whitespace-active-style
)
2120 (memq 'space-before-tab
::tab whitespace-active-style
)
2121 (memq 'space-before-tab
::space whitespace-active-style
))
2122 `((,whitespace-space-before-tab-regexp
2124 ((memq 'space-before-tab whitespace-active-style
)
2125 ;; Show SPACEs before TAB (indent-tabs-mode).
2126 (if whitespace-indent-tabs-mode
1 2))
2127 ((memq 'space-before-tab
::tab whitespace-active-style
)
2129 ((memq 'space-before-tab
::space whitespace-active-style
)
2131 whitespace-space-before-tab t
)))
2132 ,@(when (or (memq 'indentation whitespace-active-style
)
2133 (memq 'indentation
::tab whitespace-active-style
)
2134 (memq 'indentation
::space whitespace-active-style
))
2136 ((memq 'indentation whitespace-active-style
)
2137 ;; Show indentation SPACEs (indent-tabs-mode).
2138 (whitespace-indentation-regexp))
2139 ((memq 'indentation
::tab whitespace-active-style
)
2140 ;; Show indentation SPACEs (SPACEs).
2141 (whitespace-indentation-regexp 'tab
))
2142 ((memq 'indentation
::space whitespace-active-style
)
2143 ;; Show indentation SPACEs (TABs).
2144 (whitespace-indentation-regexp 'space
)))
2145 1 whitespace-indentation t
)))
2146 ,@(when (memq 'big-indent whitespace-active-style
)
2147 ;; Show big indentation.
2148 `((,whitespace-big-indent-regexp
1 'whitespace-big-indent t
)))
2149 ,@(when (memq 'empty whitespace-active-style
)
2150 ;; Show empty lines at beginning of buffer.
2151 `((,#'whitespace-empty-at-bob-regexp
2152 1 whitespace-empty t
)
2153 ;; Show empty lines at end of buffer.
2154 (,#'whitespace-empty-at-eob-regexp
2155 1 whitespace-empty t
)))
2156 ,@(when (or (memq 'space-after-tab whitespace-active-style
)
2157 (memq 'space-after-tab
::tab whitespace-active-style
)
2158 (memq 'space-after-tab
::space whitespace-active-style
))
2160 ((memq 'space-after-tab whitespace-active-style
)
2161 ;; Show SPACEs after TAB (indent-tabs-mode).
2162 (whitespace-space-after-tab-regexp))
2163 ((memq 'space-after-tab
::tab whitespace-active-style
)
2164 ;; Show SPACEs after TAB (SPACEs).
2165 (whitespace-space-after-tab-regexp 'tab
))
2166 ((memq 'space-after-tab
::space whitespace-active-style
)
2167 ;; Show SPACEs after TAB (TABs).
2168 (whitespace-space-after-tab-regexp 'space
)))
2169 1 whitespace-space-after-tab t
)))))
2170 (font-lock-add-keywords nil whitespace-font-lock-keywords t
)
2174 (defun whitespace-color-off ()
2175 "Turn off color visualization."
2176 ;; turn off font lock
2177 (kill-local-variable 'whitespace-point--used
)
2178 (when (whitespace-style-face-p)
2179 (remove-hook 'post-command-hook
#'whitespace-post-command-hook t
)
2180 (remove-hook 'before-change-functions
#'whitespace-buffer-changed t
)
2181 (font-lock-remove-keywords nil whitespace-font-lock-keywords
)
2184 (defun whitespace-point--used (start end
)
2185 (let ((ostart (overlay-start whitespace-point--used
)))
2187 (move-overlay whitespace-point--used
2189 (max end
(overlay-end whitespace-point--used
)))
2190 (move-overlay whitespace-point--used start end
))))
2192 (defun whitespace-point--flush-used (limit)
2193 (let ((ostart (overlay-start whitespace-point--used
)))
2194 ;; Strip parts of whitespace-point--used we're about to refresh.
2196 (let ((oend (overlay-end whitespace-point--used
)))
2197 (if (<= (point) ostart
)
2199 (delete-overlay whitespace-point--used
)
2200 (move-overlay whitespace-point--used limit oend
)))
2202 (move-overlay whitespace-point--used ostart
(point))))))
2205 (defun whitespace-trailing-regexp (limit)
2206 "Match trailing spaces which do not contain the point at end of line."
2208 (while (if (re-search-forward whitespace-trailing-regexp limit t
)
2209 (when (= whitespace-point
(match-end 1)) ; Loop if point at eol.
2210 (whitespace-point--used (match-beginning 0) (match-end 0))
2212 (setq status nil
))) ;; end of buffer
2216 (defun whitespace-empty-at-bob-regexp (limit)
2217 "Match spaces at beginning of buffer which do not contain the point at \
2218 beginning of buffer."
2224 (setq r
(and (looking-at whitespace-empty-at-bob-regexp
)
2225 (or (/= whitespace-point
1)
2226 (progn (whitespace-point--used (match-beginning 0)
2229 (set-marker whitespace-bob-marker
(if r
(match-end 1) b
)))
2230 ;; inside bob empty region
2231 ((<= limit whitespace-bob-marker
)
2232 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2234 (when (< (match-end 1) limit
)
2235 (set-marker whitespace-bob-marker
(match-end 1)))
2236 (set-marker whitespace-bob-marker b
)))
2237 ;; intersection with end of bob empty region
2238 ((<= b whitespace-bob-marker
)
2239 (setq r
(looking-at whitespace-empty-at-bob-regexp
))
2240 (set-marker whitespace-bob-marker
(if r
(match-end 1) b
)))
2241 ;; it is not inside bob empty region
2244 ;; move to end of matching
2245 (and r
(goto-char (match-end 1)))
2249 (defsubst whitespace-looking-back
(regexp limit
)
2251 (when (/= 0 (skip-chars-backward " \t\n" limit
))
2254 (looking-at regexp
))))
2257 (defun whitespace-empty-at-eob-regexp (limit)
2258 "Match spaces at end of buffer which do not contain the point at end of \
2261 (e (1+ (buffer-size)))
2267 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2268 (when (and r
(= whitespace-point e
))
2270 (whitespace-point--used (match-beginning 0) (match-end 0)))
2272 (set-marker whitespace-eob-marker
(match-beginning 1))
2273 (set-marker whitespace-eob-marker limit
)
2274 (goto-char b
))) ; return back to initial position
2275 ;; inside eob empty region
2276 ((>= b whitespace-eob-marker
)
2278 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2280 (when (> (match-beginning 1) b
)
2281 (set-marker whitespace-eob-marker
(match-beginning 1)))
2282 (set-marker whitespace-eob-marker limit
)
2283 (goto-char b
))) ; return back to initial position
2284 ;; intersection with beginning of eob empty region
2285 ((>= limit whitespace-eob-marker
)
2287 (setq r
(whitespace-looking-back whitespace-empty-at-eob-regexp b
))
2289 (set-marker whitespace-eob-marker
(match-beginning 1))
2290 (set-marker whitespace-eob-marker limit
)
2291 (goto-char b
))) ; return back to initial position
2292 ;; it is not inside eob empty region
2298 (defun whitespace-buffer-changed (_beg _end
)
2299 "Set `whitespace-buffer-changed' variable to t."
2300 (setq whitespace-buffer-changed t
))
2303 (defun whitespace-post-command-hook ()
2304 "Save current point into `whitespace-point' variable.
2305 Also refontify when necessary."
2306 (unless (and (eq whitespace-point
(point))
2307 (not whitespace-buffer-changed
))
2308 (setq whitespace-point
(point)) ; current point position
2311 ;; It is at end of buffer (eob).
2312 ((= whitespace-point
(1+ (buffer-size)))
2313 (when (whitespace-looking-back whitespace-empty-at-eob-regexp
2315 (match-beginning 0)))
2316 ;; It is at end of line ...
2318 ;; ... with trailing SPACE or TAB
2319 (or (memq (preceding-char) '(?\s ?
\t))))
2320 (line-beginning-position))
2321 ;; It is at beginning of buffer (bob).
2322 ((and (= whitespace-point
1)
2323 (looking-at whitespace-empty-at-bob-regexp
))
2325 (ostart (overlay-start whitespace-point--used
)))
2328 ;; New point does not affect highlighting: just refresh the
2329 ;; highlighting of old point, if needed.
2331 (font-lock-flush ostart
2332 (overlay-end whitespace-point--used
))
2333 (delete-overlay whitespace-point--used
)))
2335 ;; Old point did not affect highlighting, but new one does: refresh the
2336 ;; highlighting of new point.
2337 (font-lock-flush (min refontify
(point)) (max refontify
(point))))
2340 (setq ostart
(line-beginning-position))
2341 (and (<= ostart
(max refontify
(point)))
2343 (goto-char (overlay-end whitespace-point--used
))
2344 (let ((oend (line-beginning-position 2)))
2345 (<= (min refontify
(point)) oend
)))))
2346 ;; The old point highlighting and the new point highlighting
2347 ;; cover a contiguous region: do a single refresh.
2348 (font-lock-flush (min refontify
(point) ostart
)
2349 (max refontify
(point)
2350 (overlay-end whitespace-point--used
)))
2351 (delete-overlay whitespace-point--used
))
2353 (font-lock-flush (min refontify
(point))
2354 (max refontify
(point)))
2355 (font-lock-flush ostart
(overlay-end whitespace-point--used
))
2356 (delete-overlay whitespace-point--used
))))))
2359 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2360 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2363 (defun whitespace-style-mark-p ()
2364 "Return t if there is some visualization via display table."
2365 (or (memq 'tab-mark whitespace-active-style
)
2366 (memq 'space-mark whitespace-active-style
)
2367 (memq 'newline-mark whitespace-active-style
)))
2370 (defsubst whitespace-char-valid-p
(char)
2371 ;; This check should be improved!!!
2376 (defun whitespace-display-vector-p (vec)
2377 "Return true if every character in vector VEC can be displayed."
2378 (let ((i (length vec
)))
2380 (while (and (>= (setq i
(1- i
)) 0)
2381 (whitespace-char-valid-p (aref vec i
))))
2385 (defun whitespace-display-char-on ()
2386 "Turn on character display mapping."
2387 (when (and whitespace-display-mappings
2388 (whitespace-style-mark-p))
2390 ;; Remember whether a buffer has a local display table.
2391 (unless whitespace-display-table-was-local
2392 (setq whitespace-display-table-was-local t
2393 whitespace-display-table
2394 (copy-sequence buffer-display-table
))
2395 ;; Assure `buffer-display-table' is unique
2396 ;; when two or more windows are visible.
2397 (setq buffer-display-table
2398 (copy-sequence buffer-display-table
)))
2399 (unless buffer-display-table
2400 (setq buffer-display-table
(make-display-table)))
2401 (dolist (entry whitespace-display-mappings
)
2402 ;; check if it is to display this mark
2403 (when (memq (car entry
) whitespace-style
)
2404 ;; Get a displayable mapping.
2405 (setq vecs
(cddr entry
))
2407 (not (whitespace-display-vector-p (car vecs
))))
2408 (setq vecs
(cdr vecs
)))
2409 ;; Display a valid mapping.
2411 (setq vec
(copy-sequence (car vecs
)))
2413 (when (and (eq (cadr entry
) ?
\n)
2414 (memq 'newline whitespace-active-style
))
2415 ;; Only insert face bits on NEWLINE char mapping to avoid
2416 ;; obstruction of other faces like TABs and (HARD) SPACEs
2417 ;; faces, font-lock faces, etc.
2418 (dotimes (i (length vec
))
2419 (or (eq (aref vec i
) ?
\n)
2421 (make-glyph-code (aref vec i
)
2422 whitespace-newline
)))))
2424 (aset buffer-display-table
(cadr entry
) vec
)))))))
2427 (defun whitespace-display-char-off ()
2428 "Turn off character display mapping."
2429 (and whitespace-display-mappings
2430 (whitespace-style-mark-p)
2431 whitespace-display-table-was-local
2432 (setq whitespace-display-table-was-local nil
2433 buffer-display-table whitespace-display-table
)))
2436 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2440 (defun whitespace-action-when-on ()
2441 "Action to be taken always when local whitespace is turned on."
2442 (cond ((memq 'cleanup whitespace-action
)
2443 (whitespace-cleanup))
2444 ((memq 'report-on-bogus whitespace-action
)
2445 (whitespace-report nil t
))))
2448 (defun whitespace-write-file-hook ()
2449 "Action to be taken when buffer is written.
2450 It should be added buffer-locally to `write-file-functions'."
2451 (cond ((memq 'auto-cleanup whitespace-action
)
2452 (whitespace-cleanup))
2453 ((memq 'abort-on-bogus whitespace-action
)
2454 (when (whitespace-report nil t
)
2455 (error "Abort write due to whitespace problems in %s"
2457 nil
) ; continue hook processing
2460 (defun whitespace-warn-read-only (msg)
2461 "Warn if buffer is read-only."
2462 (when (memq 'warn-if-read-only whitespace-action
)
2463 (message "Can't %s: %s is read-only" msg
(buffer-name))))
2466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2469 (defun whitespace-unload-function ()
2470 "Unload the whitespace library."
2471 (global-whitespace-mode -
1)
2472 ;; be sure all local whitespace mode is turned off
2473 (save-current-buffer
2474 (dolist (buf (buffer-list))
2476 (whitespace-mode -
1)))
2477 nil
) ; continue standard unloading
2480 (provide 'whitespace
)
2483 (run-hooks 'whitespace-load-hook
)
2486 ;;; whitespace.el ends here