* lisp/emacs-lisp/package.el (package--with-work-buffer-async):
[emacs.git] / lisp / whitespace.el
bloba1a6c3ce04abdfc68f915acfe1ea303d41ae780a
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
6 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Keywords: data, wp
8 ;; Version: 13.2.2
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/>.
26 ;;; Commentary:
28 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Introduction
31 ;; ------------
33 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
34 ;; and NEWLINE).
36 ;; whitespace uses two ways to visualize blanks: faces and display
37 ;; table.
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
47 ;; visualized.
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
110 ;; `whitespace.el'.
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
123 ;; idea was kept)
126 ;; Using whitespace
127 ;; ----------------
129 ;; There is no problem if you mix local and global minor mode usage.
131 ;; * LOCAL whitespace:
132 ;; + To toggle whitespace options locally, type:
134 ;; M-x whitespace-toggle-options RET
136 ;; + To activate whitespace locally, type:
138 ;; C-u 1 M-x whitespace-mode RET
140 ;; + To deactivate whitespace locally, type:
142 ;; C-u 0 M-x whitespace-mode RET
144 ;; + To toggle whitespace locally, type:
146 ;; M-x whitespace-mode RET
148 ;; * GLOBAL whitespace:
149 ;; + To toggle whitespace options globally, type:
151 ;; M-x global-whitespace-toggle-options RET
153 ;; + To activate whitespace globally, type:
155 ;; C-u 1 M-x global-whitespace-mode RET
157 ;; + To deactivate whitespace globally, type:
159 ;; C-u 0 M-x global-whitespace-mode RET
161 ;; + To toggle whitespace globally, type:
163 ;; M-x global-whitespace-mode RET
165 ;; There are also the following useful commands:
167 ;; `whitespace-newline-mode'
168 ;; Toggle NEWLINE minor mode visualization ("nl" on mode line).
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on mode line).
173 ;; `whitespace-report'
174 ;; Report some blank problems in buffer.
176 ;; `whitespace-report-region'
177 ;; Report some blank problems in a region.
179 ;; `whitespace-cleanup'
180 ;; Cleanup some blank problems in all buffer or at region.
182 ;; `whitespace-cleanup-region'
183 ;; Cleanup some blank problems at region.
185 ;; The problems, which are cleaned up, are:
187 ;; 1. empty lines at beginning of buffer.
188 ;; 2. empty lines at end of buffer.
189 ;; If `whitespace-style' includes the value `empty', remove all
190 ;; empty lines at beginning and/or end of buffer.
192 ;; 3. 8 or more SPACEs at beginning of line.
193 ;; If `whitespace-style' includes the value `indentation':
194 ;; replace 8 or more SPACEs at beginning of line by TABs, if
195 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
196 ;; SPACEs.
197 ;; If `whitespace-style' includes the value `indentation::tab',
198 ;; replace 8 or more SPACEs at beginning of line by TABs.
199 ;; If `whitespace-style' includes the value `indentation::space',
200 ;; replace TABs by SPACEs.
202 ;; 4. SPACEs before TAB.
203 ;; If `whitespace-style' includes the value `space-before-tab':
204 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
205 ;; otherwise, replace TABs by SPACEs.
206 ;; If `whitespace-style' includes the value
207 ;; `space-before-tab::tab', replace SPACEs by TABs.
208 ;; If `whitespace-style' includes the value
209 ;; `space-before-tab::space', replace TABs by SPACEs.
211 ;; 5. SPACEs or TABs at end of line.
212 ;; If `whitespace-style' includes the value `trailing', remove all
213 ;; SPACEs or TABs at end of line.
215 ;; 6. 8 or more SPACEs after TAB.
216 ;; If `whitespace-style' includes the value `space-after-tab':
217 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
218 ;; otherwise, replace TABs by SPACEs.
219 ;; If `whitespace-style' includes the value `space-after-tab::tab',
220 ;; replace SPACEs by TABs.
221 ;; If `whitespace-style' includes the value
222 ;; `space-after-tab::space', replace TABs by SPACEs.
225 ;; Hooks
226 ;; -----
228 ;; whitespace has the following hook variables:
230 ;; `whitespace-mode-hook'
231 ;; It is evaluated always when whitespace is turned on locally.
233 ;; `global-whitespace-mode-hook'
234 ;; It is evaluated always when whitespace is turned on globally.
236 ;; `whitespace-load-hook'
237 ;; It is evaluated after whitespace package is loaded.
240 ;; Options
241 ;; -------
243 ;; Below it's shown a brief description of whitespace options, please,
244 ;; see the options declaration in the code for a long documentation.
246 ;; `whitespace-style' Specify which kind of blank is
247 ;; visualized.
249 ;; `whitespace-space' Face used to visualize SPACE.
251 ;; `whitespace-hspace' Face used to visualize HARD SPACE.
253 ;; `whitespace-tab' Face used to visualize TAB.
255 ;; `whitespace-newline' Face used to visualize NEWLINE char
256 ;; mapping.
258 ;; `whitespace-trailing' Face used to visualize trailing
259 ;; blanks.
261 ;; `whitespace-line' Face used to visualize "long" lines.
263 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
264 ;; before TAB.
266 ;; `whitespace-indentation' Face used to visualize 8 or more
267 ;; SPACEs at beginning of line.
269 ;; `whitespace-big-indent' Face used to visualize big indentation.
271 ;; `whitespace-empty' Face used to visualize empty lines at
272 ;; beginning and/or end of buffer.
274 ;; `whitespace-space-after-tab' Face used to visualize 8 or more
275 ;; SPACEs after TAB.
277 ;; `whitespace-space-regexp' Specify SPACE characters regexp.
279 ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
281 ;; `whitespace-tab-regexp' Specify TAB characters regexp.
283 ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
285 ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
286 ;; regexp.
288 ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
289 ;; SPACEs at beginning of line.
291 ;; `whitespace-big-indent-regexp' Specify big indentation at beginning of line
292 ;; regexp.
294 ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
295 ;; at beginning of buffer.
297 ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
298 ;; at end of buffer.
300 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
301 ;; SPACEs after TAB.
303 ;; `whitespace-line-column' Specify column beyond which the line
304 ;; is highlighted.
306 ;; `whitespace-display-mappings' Specify an alist of mappings
307 ;; for displaying characters.
309 ;; `whitespace-global-modes' Modes for which global
310 ;; `whitespace-mode' is automagically
311 ;; turned on.
313 ;; `whitespace-action' Specify which action is taken when a
314 ;; buffer is visited or written.
317 ;; Acknowledgments
318 ;; ---------------
320 ;; Thanks to felix (EmacsWiki) for keeping highlight when switching between
321 ;; major modes on a file.
323 ;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a
324 ;; `whitespace-newline' initialization with low contrast relative to
325 ;; the background color.
327 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
328 ;; `indent-tabs-mode' usage suggestion.
330 ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook
331 ;; actions when buffer is written as the original whitespace package
332 ;; had.
334 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
335 ;; lines tail. See EightyColumnRule (EmacsWiki).
337 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
338 ;; * `define-minor-mode'.
339 ;; * `global-whitespace-*' name for global commands.
341 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
343 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
344 ;; suggestion.
346 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
347 ;; helping to fix `find-file-hooks' reference.
349 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
350 ;; indicating defface byte-compilation warnings.
352 ;; Thanks to Tim O'Callaghan (EmacsWiki) for the idea about highlight
353 ;; "long" lines. See EightyColumnRule (EmacsWiki).
355 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
356 ;; NEWLINE character mapping.
358 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
359 ;; whitespace-mode.el on XEmacs.
361 ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via
362 ;; visws.el (his code was modified, but the main idea was kept).
364 ;; Thanks to:
365 ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el
366 ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el
367 ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el
368 ;; Miles Bader <miles@gnu.org> visws.el
369 ;; And to all people who contributed with them.
372 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 ;;; code:
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;;;; User Variables:
381 ;;; Interface to the command system
384 (defgroup whitespace nil
385 "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
386 :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
387 :version "23.1"
388 :group 'convenience)
391 (defcustom whitespace-style
392 '(face
393 tabs spaces trailing lines space-before-tab newline
394 indentation empty space-after-tab
395 space-mark tab-mark newline-mark)
396 "Specify which kind of blank is visualized.
398 It's a list containing some or all of the following values:
400 face enable all visualization via faces (see below).
402 trailing trailing blanks are visualized via faces.
403 It has effect only if `face' (see above)
404 is present in `whitespace-style'.
406 tabs TABs are visualized via faces.
407 It has effect only if `face' (see above)
408 is present in `whitespace-style'.
410 spaces SPACEs and HARD SPACEs are visualized via
411 faces.
412 It has effect only if `face' (see above)
413 is present in `whitespace-style'.
415 lines lines which have columns beyond
416 `whitespace-line-column' are highlighted via
417 faces.
418 Whole line is highlighted.
419 It has precedence over `lines-tail' (see
420 below).
421 It has effect only if `face' (see above)
422 is present in `whitespace-style'.
424 lines-tail lines which have columns beyond
425 `whitespace-line-column' are highlighted via
426 faces.
427 But only the part of line which goes
428 beyond `whitespace-line-column' column.
429 It has effect only if `lines' (see above)
430 is not present in `whitespace-style'
431 and if `face' (see above) is present in
432 `whitespace-style'.
434 newline NEWLINEs are visualized via faces.
435 It has effect only if `face' (see above)
436 is present in `whitespace-style'.
438 empty empty lines at beginning and/or end of buffer
439 are visualized via faces.
440 It has effect only if `face' (see above)
441 is present in `whitespace-style'.
443 indentation::tab 8 or more SPACEs at beginning of line are
444 visualized via faces.
445 It has effect only if `face' (see above)
446 is present in `whitespace-style'.
448 indentation::space TABs at beginning of line are visualized via
449 faces.
450 It has effect only if `face' (see above)
451 is present in `whitespace-style'.
453 indentation 8 or more SPACEs at beginning of line are
454 visualized, if `indent-tabs-mode' (which see)
455 is non-nil; otherwise, TABs at beginning of
456 line are visualized via faces.
457 It has effect only if `face' (see above)
458 is present in `whitespace-style'.
460 big-indent Big indentations are visualized via faces.
461 It has effect only if `face' (see above)
462 is present in `whitespace-style'.
464 space-after-tab::tab 8 or more SPACEs after a TAB are
465 visualized via faces.
466 It has effect only if `face' (see above)
467 is present in `whitespace-style'.
469 space-after-tab::space TABs are visualized when 8 or more
470 SPACEs occur after a TAB, via faces.
471 It has effect only if `face' (see above)
472 is present in `whitespace-style'.
474 space-after-tab 8 or more SPACEs after a TAB are
475 visualized, if `indent-tabs-mode'
476 (which see) is non-nil; otherwise,
477 the TABs are visualized via faces.
478 It has effect only if `face' (see above)
479 is present in `whitespace-style'.
481 space-before-tab::tab SPACEs before TAB are visualized via
482 faces.
483 It has effect only if `face' (see above)
484 is present in `whitespace-style'.
486 space-before-tab::space TABs are visualized when SPACEs occur
487 before TAB, via faces.
488 It has effect only if `face' (see above)
489 is present in `whitespace-style'.
491 space-before-tab SPACEs before TAB are visualized, if
492 `indent-tabs-mode' (which see) is
493 non-nil; otherwise, the TABs are
494 visualized via faces.
495 It has effect only if `face' (see above)
496 is present in `whitespace-style'.
498 space-mark SPACEs and HARD SPACEs are visualized via
499 display table.
501 tab-mark TABs are visualized via display table.
503 newline-mark NEWLINEs are visualized via display table.
505 Any other value is ignored.
507 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
508 via display table.
510 There is an evaluation order for some values, if they are
511 included in `whitespace-style' list. For example, if
512 indentation, indentation::tab and/or indentation::space are
513 included in `whitespace-style' list. The evaluation order for
514 these values is:
516 * For indentation:
517 1. indentation
518 2. indentation::tab
519 3. indentation::space
521 * For SPACEs after TABs:
522 1. space-after-tab
523 2. space-after-tab::tab
524 3. space-after-tab::space
526 * For SPACEs before TABs:
527 1. space-before-tab
528 2. space-before-tab::tab
529 3. space-before-tab::space
531 So, for example, if indentation and indentation::space are
532 included in `whitespace-style' list, the indentation value is
533 evaluated instead of indentation::space value.
535 One reason for not visualize spaces via faces (if `face' is not
536 included in `whitespace-style') is to use exclusively for
537 cleaning up a buffer. See `whitespace-cleanup' and
538 `whitespace-cleanup-region' for documentation.
540 See also `whitespace-display-mappings' for documentation."
541 :type '(set :tag "Kind of Blank"
542 (const :tag "(Face) Face visualization" face)
543 (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
544 trailing)
545 (const :tag "(Face) TABs" tabs)
546 (const :tag "(Face) SPACEs and HARD SPACEs" spaces)
547 (const :tag "(Face) Lines" lines)
548 (const :tag "(Face) Lines, only overlong part" lines-tail)
549 (const :tag "(Face) NEWLINEs" newline)
550 (const :tag "(Face) Empty Lines At BOB And/Or EOB" empty)
551 (const :tag "(Face) Indentation SPACEs" indentation::tab)
552 (const :tag "(Face) Indentation TABs"
553 indentation::space)
554 (const :tag "(Face) Indentation TABs or SPACEs" indentation)
555 (const :tag "(Face) Too much line indentation" big-indent)
556 (const :tag "(Face) SPACEs after TAB: SPACEs"
557 space-after-tab::tab)
558 (const :tag "(Face) SPACEs after TAB: TABs"
559 space-after-tab::space)
560 (const :tag "(Face) SPACEs after TAB" space-after-tab)
561 (const :tag "(Face) SPACEs before TAB: SPACEs"
562 space-before-tab::tab)
563 (const :tag "(Face) SPACEs before TAB: TABs"
564 space-before-tab::space)
565 (const :tag "(Face) SPACEs before TAB" space-before-tab)
566 (const :tag "(Mark) SPACEs and HARD SPACEs" space-mark)
567 (const :tag "(Mark) TABs" tab-mark)
568 (const :tag "(Mark) NEWLINEs" newline-mark))
569 :group 'whitespace)
571 (defvar whitespace-space 'whitespace-space
572 "Symbol face used to visualize SPACE.
573 Used when `whitespace-style' includes the value `spaces'.")
574 (make-obsolete-variable 'whitespace-space "use the face instead." "24.4")
577 (defface whitespace-space
578 '((((class color) (background dark))
579 :background "grey20" :foreground "darkgray")
580 (((class color) (background light))
581 :background "LightYellow" :foreground "lightgray")
582 (t :inverse-video t))
583 "Face used to visualize SPACE."
584 :group 'whitespace)
587 (defvar whitespace-hspace 'whitespace-hspace
588 "Symbol face used to visualize HARD SPACE.
589 Used when `whitespace-style' includes the value `spaces'.")
590 (make-obsolete-variable 'whitespace-hspace "use the face instead." "24.4")
592 (defface whitespace-hspace ; 'nobreak-space
593 '((((class color) (background dark))
594 :background "grey24" :foreground "darkgray")
595 (((class color) (background light))
596 :background "LemonChiffon3" :foreground "lightgray")
597 (t :inverse-video t))
598 "Face used to visualize HARD SPACE."
599 :group 'whitespace)
602 (defvar whitespace-tab 'whitespace-tab
603 "Symbol face used to visualize TAB.
604 Used when `whitespace-style' includes the value `tabs'.")
605 (make-obsolete-variable 'whitespace-tab "use the face instead." "24.4")
607 (defface whitespace-tab
608 '((((class color) (background dark))
609 :background "grey22" :foreground "darkgray")
610 (((class color) (background light))
611 :background "beige" :foreground "lightgray")
612 (t :inverse-video t))
613 "Face used to visualize TAB."
614 :group 'whitespace)
617 (defvar whitespace-newline 'whitespace-newline
618 "Symbol face used to visualize NEWLINE char mapping.
619 See `whitespace-display-mappings'.
620 Used when `whitespace-style' includes the values `newline-mark'
621 and `newline'.")
622 (make-obsolete-variable 'whitespace-newline "use the face instead." "24.4")
624 (defface whitespace-newline
625 '((default :weight normal)
626 (((class color) (background dark)) :foreground "darkgray")
627 (((class color) (min-colors 88) (background light)) :foreground "lightgray")
628 ;; Displays with 16 colors use lightgray as background, so using a
629 ;; lightgray foreground makes the newline mark invisible.
630 (((class color) (background light)) :foreground "brown")
631 (t :underline t))
632 "Face used to visualize NEWLINE char mapping.
634 See `whitespace-display-mappings'."
635 :group 'whitespace)
638 (defvar whitespace-trailing 'whitespace-trailing
639 "Symbol face used to visualize trailing blanks.
640 Used when `whitespace-style' includes the value `trailing'.")
641 (make-obsolete-variable 'whitespace-trailing "use the face instead." "24.4")
643 (defface whitespace-trailing ; 'trailing-whitespace
644 '((default :weight bold)
645 (((class mono)) :inverse-video t :underline t)
646 (t :background "red1" :foreground "yellow"))
647 "Face used to visualize trailing blanks."
648 :group 'whitespace)
651 (defvar whitespace-line 'whitespace-line
652 "Symbol face used to visualize \"long\" lines.
653 See `whitespace-line-column'.
654 Used when `whitespace-style' includes the value `line'.")
655 (make-obsolete-variable 'whitespace-line "use the face instead." "24.4")
657 (defface whitespace-line
658 '((((class mono)) :inverse-video t :weight bold :underline t)
659 (t :background "gray20" :foreground "violet"))
660 "Face used to visualize \"long\" lines.
662 See `whitespace-line-column'."
663 :group 'whitespace)
666 (defvar whitespace-space-before-tab 'whitespace-space-before-tab
667 "Symbol face used to visualize SPACEs before TAB.
668 Used when `whitespace-style' includes the value `space-before-tab'.")
669 (make-obsolete-variable 'whitespace-space-before-tab
670 "use the face instead." "24.4")
672 (defface whitespace-space-before-tab
673 '((((class mono)) :inverse-video t :weight bold :underline t)
674 (t :background "DarkOrange" :foreground "firebrick"))
675 "Face used to visualize SPACEs before TAB."
676 :group 'whitespace)
679 (defvar whitespace-indentation 'whitespace-indentation
680 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
681 Used when `whitespace-style' includes the value `indentation'.")
682 (make-obsolete-variable 'whitespace-indentation "use the face instead." "24.4")
684 (defface whitespace-indentation
685 '((((class mono)) :inverse-video t :weight bold :underline t)
686 (t :background "yellow" :foreground "firebrick"))
687 "Face used to visualize 8 or more SPACEs at beginning of line."
688 :group 'whitespace)
690 (defface whitespace-big-indent
691 '((((class mono)) :inverse-video t :weight bold :underline t)
692 (t :background "red" :foreground "firebrick"))
693 "Face used to visualize big indentation."
694 :group 'whitespace)
697 (defvar whitespace-empty 'whitespace-empty
698 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
699 Used when `whitespace-style' includes the value `empty'.")
700 (make-obsolete-variable 'whitespace-empty "use the face instead." "24.4")
702 (defface whitespace-empty
703 '((((class mono)) :inverse-video t :weight bold :underline t)
704 (t :background "yellow" :foreground "firebrick"))
705 "Face used to visualize empty lines at beginning and/or end of buffer."
706 :group 'whitespace)
709 (defvar whitespace-space-after-tab 'whitespace-space-after-tab
710 "Symbol face used to visualize 8 or more SPACEs after TAB.
711 Used when `whitespace-style' includes the value `space-after-tab'.")
712 (make-obsolete-variable 'whitespace-space-after-tab
713 "use the face instead." "24.4")
715 (defface whitespace-space-after-tab
716 '((((class mono)) :inverse-video t :weight bold :underline t)
717 (t :background "yellow" :foreground "firebrick"))
718 "Face used to visualize 8 or more SPACEs after TAB."
719 :group 'whitespace)
722 (defcustom whitespace-hspace-regexp
723 "\\(\u00A0+\\)"
724 "Specify HARD SPACE characters regexp.
726 Here are some examples:
728 \"\\\\(^\\xA0+\\\\)\" \
729 visualize only leading HARD SPACEs.
730 \"\\\\(\\xA0+$\\\\)\" \
731 visualize only trailing HARD SPACEs.
732 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
733 visualize leading and/or trailing HARD SPACEs.
734 \"\\t\\\\(\\xA0+\\\\)\\t\" \
735 visualize only HARD SPACEs between TABs.
737 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
738 Use exactly one pair of enclosing \\\\( and \\\\).
740 Used when `whitespace-style' includes `spaces'."
741 :type '(regexp :tag "HARD SPACE Chars")
742 :group 'whitespace)
745 (defcustom whitespace-space-regexp "\\( +\\)"
746 "Specify SPACE characters regexp.
748 If you're using `mule' package, there may be other characters
749 besides \" \" that should be considered SPACE.
751 Here are some examples:
753 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
754 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
755 \"\\\\(^ +\\\\| +$\\\\)\" \
756 visualize leading and/or trailing SPACEs.
757 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
759 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
760 Use exactly one pair of enclosing \\\\( and \\\\).
762 Used when `whitespace-style' includes `spaces'."
763 :type '(regexp :tag "SPACE Chars")
764 :group 'whitespace)
767 (defcustom whitespace-tab-regexp "\\(\t+\\)"
768 "Specify TAB characters regexp.
770 If you're using `mule' package, there may be other characters
771 besides \"\\t\" that should be considered TAB.
773 Here are some examples:
775 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
776 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
777 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
778 visualize leading and/or trailing TABs.
779 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
781 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
782 Use exactly one pair of enclosing \\\\( and \\\\).
784 Used when `whitespace-style' includes `tabs'."
785 :type '(regexp :tag "TAB Chars")
786 :group 'whitespace)
789 (defcustom whitespace-trailing-regexp
790 "\\([\t \u00A0]+\\)$"
791 "Specify trailing characters regexp.
793 There may be other characters besides:
795 \" \" \"\\t\" \"\\u00A0\"
797 that should be considered blank.
799 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
800 Use exactly one pair of enclosing elements above.
802 Used when `whitespace-style' includes `trailing'."
803 :type '(regexp :tag "Trailing Chars")
804 :group 'whitespace)
807 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
808 "Specify SPACEs before TAB regexp.
810 Used when `whitespace-style' includes `space-before-tab',
811 `space-before-tab::tab' or `space-before-tab::space'."
812 :type '(regexp :tag "SPACEs Before TAB")
813 :group 'whitespace)
816 (defcustom whitespace-indentation-regexp
817 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
818 . "^ *\\(\t+\\)[^\n]")
819 "Specify regexp for 8 or more SPACEs at beginning of line.
821 It is a cons where the cons car is used for SPACEs visualization
822 and the cons cdr is used for TABs visualization.
824 Used when `whitespace-style' includes `indentation',
825 `indentation::tab' or `indentation::space'."
826 :type '(cons (string :tag "Indentation SPACEs")
827 (string :tag "Indentation TABs"))
828 :group 'whitespace)
831 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
832 "Specify regexp for empty lines at beginning of buffer.
834 Used when `whitespace-style' includes `empty'."
835 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
836 :group 'whitespace)
839 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
840 "Specify regexp for empty lines at end of buffer.
842 Used when `whitespace-style' includes `empty'."
843 :type '(regexp :tag "Empty Lines At End Of Buffer")
844 :group 'whitespace)
847 (defcustom whitespace-space-after-tab-regexp
848 '("\t+\\(\\( \\{%d\\}\\)+\\)"
849 . "\\(\t+\\) +")
850 "Specify regexp for 8 or more SPACEs after TAB.
852 It is a cons where the cons car is used for SPACEs visualization
853 and the cons cdr is used for TABs visualization.
855 Used when `whitespace-style' includes `space-after-tab',
856 `space-after-tab::tab' or `space-after-tab::space'."
857 :type '(cons (string :tag "SPACEs After TAB")
858 string)
859 :group 'whitespace)
861 (defcustom whitespace-big-indent-regexp
862 "^\\(\\(?:\t\\{4,\\}\\| \\{32,\\}\\)[\t ]*\\)"
863 "Specify big indentation regexp.
865 If you're using `mule' package, there may be other characters
866 besides \"\\t\" that should be considered TAB.
868 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
869 Use exactly one pair of enclosing \\\\( and \\\\).
871 Used when `whitespace-style' includes `big-indent'."
872 :version "25.1"
873 :type '(regexp :tag "Detect too much indentation at the beginning of a line")
874 :group 'whitespace)
877 (defcustom whitespace-line-column 80
878 "Specify column beyond which the line is highlighted.
880 It must be an integer or nil. If nil, the `fill-column' variable value is
881 used.
883 Used when `whitespace-style' includes `lines' or `lines-tail'."
884 :type '(choice :tag "Line Length Limit"
885 (integer :tag "Line Length")
886 (const :tag "Use fill-column" nil))
887 :safe 'integerp
888 :group 'whitespace)
891 ;; Hacked from `visible-whitespace-mappings' in visws.el
892 (defcustom whitespace-display-mappings
894 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
895 (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
896 ;; NEWLINE is displayed using the face `whitespace-newline'
897 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
898 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
899 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
900 ;; (newline-mark ?\n [?\u00AF ?\n] [?$ ?\n]) ; eol - overscore
901 ;; (newline-mark ?\n [?\u00AC ?\n] [?$ ?\n]) ; eol - negation
902 ;; (newline-mark ?\n [?\u00B0 ?\n] [?$ ?\n]) ; eol - degrees
904 ;; WARNING: the mapping below has a problem.
905 ;; When a TAB occupies exactly one column, it will display the
906 ;; character ?\xBB at that column followed by a TAB which goes to
907 ;; the next TAB column.
908 ;; If this is a problem for you, please, comment the line below.
909 (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
911 "Specify an alist of mappings for displaying characters.
913 Each element has the following form:
915 (KIND CHAR VECTOR...)
917 Where:
919 KIND is the kind of character.
920 It can be one of the following symbols:
922 tab-mark for TAB character
924 space-mark for SPACE or HARD SPACE character
926 newline-mark for NEWLINE character
928 CHAR is the character to be mapped.
930 VECTOR is a vector of characters to be displayed in place of CHAR.
931 The first display vector that can be displayed is used;
932 if no display vector for a mapping can be displayed, then
933 that character is displayed unmodified.
935 The NEWLINE character is displayed using the face given by
936 `whitespace-newline' variable.
938 Used when `whitespace-style' includes `tab-mark', `space-mark' or
939 `newline-mark'."
940 :type '(repeat
941 (list :tag "Character Mapping"
942 (choice :tag "Char Kind"
943 (const :tag "Tab" tab-mark)
944 (const :tag "Space" space-mark)
945 (const :tag "Newline" newline-mark))
946 (character :tag "Char")
947 (repeat :inline t :tag "Vector List"
948 (vector :tag ""
949 (repeat :inline t
950 :tag "Vector Characters"
951 (character :tag "Char"))))))
952 :group 'whitespace)
955 (defcustom whitespace-global-modes t
956 "Modes for which global `whitespace-mode' is automagically turned on.
958 Global `whitespace-mode' is controlled by the command
959 `global-whitespace-mode'.
961 If nil, means no modes have `whitespace-mode' automatically
962 turned on.
964 If t, all modes that support `whitespace-mode' have it
965 automatically turned on.
967 Else it should be a list of `major-mode' symbol names for which
968 `whitespace-mode' should be automatically turned on. The sense
969 of the list is negated if it begins with `not'. For example:
971 (c-mode c++-mode)
973 means that `whitespace-mode' is turned on for buffers in C and
974 C++ modes only."
975 :type '(choice :tag "Global Modes"
976 (const :tag "None" nil)
977 (const :tag "All" t)
978 (set :menu-tag "Mode Specific" :tag "Modes"
979 :value (not)
980 (const :tag "Except" not)
981 (repeat :inline t
982 (symbol :tag "Mode"))))
983 :group 'whitespace)
986 (defcustom whitespace-action nil
987 "Specify which action is taken when a buffer is visited or written.
989 It's a list containing some or all of the following values:
991 nil no action is taken.
993 cleanup cleanup any bogus whitespace always when local
994 whitespace is turned on.
995 See `whitespace-cleanup' and
996 `whitespace-cleanup-region'.
998 report-on-bogus report if there is any bogus whitespace always
999 when local whitespace is turned on.
1001 auto-cleanup cleanup any bogus whitespace when buffer is
1002 written.
1003 See `whitespace-cleanup' and
1004 `whitespace-cleanup-region'.
1006 abort-on-bogus abort if there is any bogus whitespace and the
1007 buffer is written.
1009 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
1010 is included in `whitespace-action' and the
1011 buffer is read-only.
1013 Any other value is treated as nil."
1014 :type '(choice :tag "Actions"
1015 (const :tag "None" nil)
1016 (repeat :tag "Action List"
1017 (choice :tag "Action"
1018 (const :tag "Cleanup When On" cleanup)
1019 (const :tag "Report On Bogus" report-on-bogus)
1020 (const :tag "Auto Cleanup" auto-cleanup)
1021 (const :tag "Abort On Bogus" abort-on-bogus)
1022 (const :tag "Warn If Read-Only" warn-if-read-only))))
1023 :group 'whitespace)
1026 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1027 ;;;; User commands - Local mode
1030 ;;;###autoload
1031 (define-minor-mode whitespace-mode
1032 "Toggle whitespace visualization (Whitespace mode).
1033 With a prefix argument ARG, enable Whitespace mode if ARG is
1034 positive, and disable it otherwise. If called from Lisp, enable
1035 the mode if ARG is omitted or nil.
1037 See also `whitespace-style', `whitespace-newline' and
1038 `whitespace-display-mappings'."
1039 :lighter " ws"
1040 :init-value nil
1041 :global nil
1042 :group 'whitespace
1043 (cond
1044 (noninteractive ; running a batch job
1045 (setq whitespace-mode nil))
1046 (whitespace-mode ; whitespace-mode on
1047 (whitespace-turn-on)
1048 (whitespace-action-when-on))
1049 (t ; whitespace-mode off
1050 (whitespace-turn-off))))
1053 ;;;###autoload
1054 (define-minor-mode whitespace-newline-mode
1055 "Toggle newline visualization (Whitespace Newline mode).
1056 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1057 is positive, and disable it otherwise. If called from Lisp,
1058 enable the mode if ARG is omitted or nil.
1060 Use `whitespace-newline-mode' only for NEWLINE visualization
1061 exclusively. For other visualizations, including NEWLINE
1062 visualization together with (HARD) SPACEs and/or TABs, please,
1063 use `whitespace-mode'.
1065 See also `whitespace-newline' and `whitespace-display-mappings'."
1066 :lighter " nl"
1067 :init-value nil
1068 :global nil
1069 :group 'whitespace
1070 (let ((whitespace-style '(face newline-mark newline)))
1071 (whitespace-mode (if whitespace-newline-mode
1072 1 -1)))
1073 ;; sync states (running a batch job)
1074 (setq whitespace-newline-mode whitespace-mode))
1077 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1078 ;;;; User commands - Global mode
1081 ;;;###autoload
1082 (define-minor-mode global-whitespace-mode
1083 "Toggle whitespace visualization globally (Global Whitespace mode).
1084 With a prefix argument ARG, enable Global Whitespace mode if ARG
1085 is positive, and disable it otherwise. If called from Lisp,
1086 enable it if ARG is omitted or nil.
1088 See also `whitespace-style', `whitespace-newline' and
1089 `whitespace-display-mappings'."
1090 :lighter " WS"
1091 :init-value nil
1092 :global t
1093 :group 'whitespace
1094 (cond
1095 (noninteractive ; running a batch job
1096 (setq global-whitespace-mode nil))
1097 (global-whitespace-mode ; global-whitespace-mode on
1098 (save-current-buffer
1099 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1100 (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1101 (dolist (buffer (buffer-list)) ; adjust all local mode
1102 (set-buffer buffer)
1103 (unless whitespace-mode
1104 (whitespace-turn-on-if-enabled)))))
1105 (t ; global-whitespace-mode off
1106 (save-current-buffer
1107 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1108 (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1109 (dolist (buffer (buffer-list)) ; adjust all local mode
1110 (set-buffer buffer)
1111 (unless whitespace-mode
1112 (whitespace-turn-off)))))))
1114 (defvar whitespace-enable-predicate
1115 (lambda ()
1116 (and (cond
1117 ((eq whitespace-global-modes t))
1118 ((listp whitespace-global-modes)
1119 (if (eq (car-safe whitespace-global-modes) 'not)
1120 (not (memq major-mode (cdr whitespace-global-modes)))
1121 (memq major-mode whitespace-global-modes)))
1122 (t nil))
1123 ;; ...we have a display (not running a batch job)
1124 (not noninteractive)
1125 ;; ...the buffer is not internal (name starts with a space)
1126 (not (eq (aref (buffer-name) 0) ?\ ))
1127 ;; ...the buffer is not special (name starts with *)
1128 (or (not (eq (aref (buffer-name) 0) ?*))
1129 ;; except the scratch buffer.
1130 (string= (buffer-name) "*scratch*"))))
1131 "Predicate to decide which buffers obey `global-whitespace-mode'.
1132 This function is called with no argument and should return non-nil
1133 if the current buffer should obey `global-whitespace-mode'.
1134 This variable is normally modified via `add-function'.")
1136 (defun whitespace-turn-on-if-enabled ()
1137 (when (funcall whitespace-enable-predicate)
1138 (whitespace-turn-on)))
1140 ;;;###autoload
1141 (define-minor-mode global-whitespace-newline-mode
1142 "Toggle global newline visualization (Global Whitespace Newline mode).
1143 With a prefix argument ARG, enable Global Whitespace Newline mode
1144 if ARG is positive, and disable it otherwise. If called from
1145 Lisp, enable it if ARG is omitted or nil.
1147 Use `global-whitespace-newline-mode' only for NEWLINE
1148 visualization exclusively. For other visualizations, including
1149 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1150 please use `global-whitespace-mode'.
1152 See also `whitespace-newline' and `whitespace-display-mappings'."
1153 :lighter " NL"
1154 :init-value nil
1155 :global t
1156 :group 'whitespace
1157 (let ((whitespace-style '(newline-mark newline)))
1158 (global-whitespace-mode (if global-whitespace-newline-mode
1159 1 -1))
1160 ;; sync states (running a batch job)
1161 (setq global-whitespace-newline-mode global-whitespace-mode)))
1164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1165 ;;;; User commands - Toggle
1168 (defconst whitespace-style-value-list
1169 '(face
1170 tabs
1171 spaces
1172 trailing
1173 lines
1174 lines-tail
1175 newline
1176 empty
1177 indentation
1178 indentation::tab
1179 indentation::space
1180 big-indent
1181 space-after-tab
1182 space-after-tab::tab
1183 space-after-tab::space
1184 space-before-tab
1185 space-before-tab::tab
1186 space-before-tab::space
1187 help-newline ; value used by `whitespace-insert-option-mark'
1188 tab-mark
1189 space-mark
1190 newline-mark
1192 "List of valid `whitespace-style' values.")
1195 (defconst whitespace-toggle-option-alist
1196 '((?f . face)
1197 (?t . tabs)
1198 (?s . spaces)
1199 (?r . trailing)
1200 (?l . lines)
1201 (?L . lines-tail)
1202 (?n . newline)
1203 (?e . empty)
1204 (?\C-i . indentation)
1205 (?I . indentation::tab)
1206 (?i . indentation::space)
1207 (?\C-t . big-indent)
1208 (?\C-a . space-after-tab)
1209 (?A . space-after-tab::tab)
1210 (?a . space-after-tab::space)
1211 (?\C-b . space-before-tab)
1212 (?B . space-before-tab::tab)
1213 (?b . space-before-tab::space)
1214 (?T . tab-mark)
1215 (?S . space-mark)
1216 (?N . newline-mark)
1217 (?x . whitespace-style)
1219 "Alist of toggle options.
1221 Each element has the form:
1223 (CHAR . SYMBOL)
1225 Where:
1227 CHAR is a char which the user will have to type.
1229 SYMBOL is a valid symbol associated with CHAR.
1230 See `whitespace-style-value-list'.")
1233 (defvar whitespace-active-style nil
1234 "Used to save locally `whitespace-style' value.")
1236 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1237 "Used to save locally `indent-tabs-mode' value.")
1239 (defvar whitespace-tab-width tab-width
1240 "Used to save locally `tab-width' value.")
1242 (defvar whitespace-point (point)
1243 "Used to save locally current point value.
1244 Used by function `whitespace-trailing-regexp' (which see).")
1245 (defvar-local whitespace-point--used nil
1246 "Region whose highlighting depends on `whitespace-point'.")
1248 (defvar whitespace-font-lock-refontify nil
1249 "Used to save locally the font-lock refontify state.
1250 Used by function `whitespace-post-command-hook' (which see).")
1252 (defvar whitespace-bob-marker nil
1253 "Used to save locally the bob marker value.
1254 Used by function `whitespace-post-command-hook' (which see).")
1256 (defvar whitespace-eob-marker nil
1257 "Used to save locally the eob marker value.
1258 Used by function `whitespace-post-command-hook' (which see).")
1260 (defvar whitespace-buffer-changed nil
1261 "Used to indicate locally if buffer changed.
1262 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1263 functions (which see).")
1266 ;;;###autoload
1267 (defun whitespace-toggle-options (arg)
1268 "Toggle local `whitespace-mode' options.
1270 If local whitespace-mode is off, toggle the option given by ARG
1271 and turn on local whitespace-mode.
1273 If local whitespace-mode is on, toggle the option given by ARG
1274 and restart local whitespace-mode.
1276 Interactively, it reads one of the following chars:
1278 CHAR MEANING
1279 (VIA FACES)
1280 f toggle face visualization
1281 t toggle TAB visualization
1282 s toggle SPACE and HARD SPACE visualization
1283 r toggle trailing blanks visualization
1284 l toggle \"long lines\" visualization
1285 L toggle \"long lines\" tail visualization
1286 n toggle NEWLINE visualization
1287 e toggle empty line at bob and/or eob visualization
1288 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1289 I toggle indentation SPACEs visualization
1290 i toggle indentation TABs visualization
1291 C-t toggle big indentation visualization
1292 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1293 A toggle SPACEs after TAB: SPACEs visualization
1294 a toggle SPACEs after TAB: TABs visualization
1295 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1296 B toggle SPACEs before TAB: SPACEs visualization
1297 b toggle SPACEs before TAB: TABs visualization
1299 (VIA DISPLAY TABLE)
1300 T toggle TAB visualization
1301 S toggle SPACEs before TAB visualization
1302 N toggle NEWLINE visualization
1304 x restore `whitespace-style' value
1305 ? display brief help
1307 Non-interactively, ARG should be a symbol or a list of symbols.
1308 The valid symbols are:
1310 face toggle face visualization
1311 tabs toggle TAB visualization
1312 spaces toggle SPACE and HARD SPACE visualization
1313 trailing toggle trailing blanks visualization
1314 lines toggle \"long lines\" visualization
1315 lines-tail toggle \"long lines\" tail visualization
1316 newline toggle NEWLINE visualization
1317 empty toggle empty line at bob and/or eob visualization
1318 indentation toggle indentation SPACEs visualization
1319 indentation::tab toggle indentation SPACEs visualization
1320 indentation::space toggle indentation TABs visualization
1321 big-indent toggle big indentation visualization
1322 space-after-tab toggle SPACEs after TAB visualization
1323 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1324 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1325 space-before-tab toggle SPACEs before TAB visualization
1326 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1327 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1329 tab-mark toggle TAB visualization
1330 space-mark toggle SPACEs before TAB visualization
1331 newline-mark toggle NEWLINE visualization
1333 whitespace-style restore `whitespace-style' value
1335 See `whitespace-style' and `indent-tabs-mode' for documentation."
1336 (interactive (whitespace-interactive-char t))
1337 (let ((whitespace-style
1338 (whitespace-toggle-list t arg whitespace-active-style)))
1339 (whitespace-mode 0)
1340 (whitespace-mode 1)))
1343 (defvar whitespace-toggle-style nil
1344 "Used to toggle the global `whitespace-style' value.")
1347 ;;;###autoload
1348 (defun global-whitespace-toggle-options (arg)
1349 "Toggle global `whitespace-mode' options.
1351 If global whitespace-mode is off, toggle the option given by ARG
1352 and turn on global whitespace-mode.
1354 If global whitespace-mode is on, toggle the option given by ARG
1355 and restart global whitespace-mode.
1357 Interactively, it accepts one of the following chars:
1359 CHAR MEANING
1360 (VIA FACES)
1361 f toggle face visualization
1362 t toggle TAB visualization
1363 s toggle SPACE and HARD SPACE visualization
1364 r toggle trailing blanks visualization
1365 l toggle \"long lines\" visualization
1366 L toggle \"long lines\" tail visualization
1367 n toggle NEWLINE visualization
1368 e toggle empty line at bob and/or eob visualization
1369 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1370 I toggle indentation SPACEs visualization
1371 i toggle indentation TABs visualization
1372 C-t toggle big indentation visualization
1373 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1374 A toggle SPACEs after TAB: SPACEs visualization
1375 a toggle SPACEs after TAB: TABs visualization
1376 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1377 B toggle SPACEs before TAB: SPACEs visualization
1378 b toggle SPACEs before TAB: TABs visualization
1380 (VIA DISPLAY TABLE)
1381 T toggle TAB visualization
1382 S toggle SPACEs before TAB visualization
1383 N toggle NEWLINE visualization
1385 x restore `whitespace-style' value
1386 ? display brief help
1388 Non-interactively, ARG should be a symbol or a list of symbols.
1389 The valid symbols are:
1391 face toggle face visualization
1392 tabs toggle TAB visualization
1393 spaces toggle SPACE and HARD SPACE visualization
1394 trailing toggle trailing blanks visualization
1395 lines toggle \"long lines\" visualization
1396 lines-tail toggle \"long lines\" tail visualization
1397 newline toggle NEWLINE visualization
1398 empty toggle empty line at bob and/or eob visualization
1399 indentation toggle indentation SPACEs visualization
1400 indentation::tab toggle indentation SPACEs visualization
1401 indentation::space toggle indentation TABs visualization
1402 big-indent toggle big indentation visualization
1403 space-after-tab toggle SPACEs after TAB visualization
1404 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1405 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1406 space-before-tab toggle SPACEs before TAB visualization
1407 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1408 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1410 tab-mark toggle TAB visualization
1411 space-mark toggle SPACEs before TAB visualization
1412 newline-mark toggle NEWLINE visualization
1414 whitespace-style restore `whitespace-style' value
1416 See `whitespace-style' and `indent-tabs-mode' for documentation."
1417 (interactive (whitespace-interactive-char nil))
1418 (let ((whitespace-style
1419 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1420 (setq whitespace-toggle-style whitespace-style)
1421 (global-whitespace-mode 0)
1422 (global-whitespace-mode 1)))
1425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1426 ;;;; User commands - Cleanup
1429 ;;;###autoload
1430 (defun whitespace-cleanup ()
1431 "Cleanup some blank problems in all buffer or at region.
1433 It usually applies to the whole buffer, but in transient mark
1434 mode when the mark is active, it applies to the region. It also
1435 applies to the region when it is not in transient mark mode, the
1436 mark is active and \\[universal-argument] was pressed just before
1437 calling `whitespace-cleanup' interactively.
1439 See also `whitespace-cleanup-region'.
1441 The problems cleaned up are:
1443 1. empty lines at beginning of buffer.
1444 2. empty lines at end of buffer.
1445 If `whitespace-style' includes the value `empty', remove all
1446 empty lines at beginning and/or end of buffer.
1448 3. 8 or more SPACEs at beginning of line.
1449 If `whitespace-style' includes the value `indentation':
1450 replace 8 or more SPACEs at beginning of line by TABs, if
1451 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1452 SPACEs.
1453 If `whitespace-style' includes the value `indentation::tab',
1454 replace 8 or more SPACEs at beginning of line by TABs.
1455 If `whitespace-style' includes the value `indentation::space',
1456 replace TABs by SPACEs.
1458 4. SPACEs before TAB.
1459 If `whitespace-style' includes the value `space-before-tab':
1460 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1461 otherwise, replace TABs by SPACEs.
1462 If `whitespace-style' includes the value
1463 `space-before-tab::tab', replace SPACEs by TABs.
1464 If `whitespace-style' includes the value
1465 `space-before-tab::space', replace TABs by SPACEs.
1467 5. SPACEs or TABs at end of line.
1468 If `whitespace-style' includes the value `trailing', remove
1469 all SPACEs or TABs at end of line.
1471 6. 8 or more SPACEs after TAB.
1472 If `whitespace-style' includes the value `space-after-tab':
1473 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1474 otherwise, replace TABs by SPACEs.
1475 If `whitespace-style' includes the value
1476 `space-after-tab::tab', replace SPACEs by TABs.
1477 If `whitespace-style' includes the value
1478 `space-after-tab::space', replace TABs by SPACEs.
1480 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1481 documentation."
1482 (interactive "@")
1483 (cond
1484 ;; read-only buffer
1485 (buffer-read-only
1486 (whitespace-warn-read-only "cleanup"))
1487 ;; region active
1488 ((and (or transient-mark-mode
1489 current-prefix-arg)
1490 mark-active)
1491 ;; PROBLEMs 1 and 2 are not handled in region
1492 ;; PROBLEM 3: 8 or more SPACEs at bol
1493 ;; PROBLEM 4: SPACEs before TAB
1494 ;; PROBLEM 5: SPACEs or TABs at eol
1495 ;; PROBLEM 6: 8 or more SPACEs after TAB
1496 (whitespace-cleanup-region (region-beginning) (region-end)))
1497 ;; whole buffer
1499 (save-excursion
1500 (save-match-data ;FIXME: Why?
1501 ;; PROBLEM 1: empty lines at bob
1502 ;; PROBLEM 2: empty lines at eob
1503 ;; ACTION: remove all empty lines at bob and/or eob
1504 (when (memq 'empty whitespace-style)
1505 (let (overwrite-mode) ; enforce no overwrite
1506 (goto-char (point-min))
1507 (when (looking-at whitespace-empty-at-bob-regexp)
1508 (delete-region (match-beginning 1) (match-end 1)))
1509 (when (re-search-forward
1510 (concat whitespace-empty-at-eob-regexp "\\'") nil t)
1511 (delete-region (match-beginning 1) (match-end 1)))))))
1512 ;; PROBLEM 3: 8 or more SPACEs at bol
1513 ;; PROBLEM 4: SPACEs before TAB
1514 ;; PROBLEM 5: SPACEs or TABs at eol
1515 ;; PROBLEM 6: 8 or more SPACEs after TAB
1516 (whitespace-cleanup-region (point-min) (point-max)))))
1518 (defun whitespace-ensure-local-variables ()
1519 "Set `whitespace-indent-tabs-mode' and `whitespace-tab-width' locally."
1520 (set (make-local-variable 'whitespace-indent-tabs-mode)
1521 indent-tabs-mode)
1522 (set (make-local-variable 'whitespace-tab-width)
1523 tab-width))
1525 ;;;###autoload
1526 (defun whitespace-cleanup-region (start end)
1527 "Cleanup some blank problems at region.
1529 The problems cleaned up are:
1531 1. 8 or more SPACEs at beginning of line.
1532 If `whitespace-style' includes the value `indentation':
1533 replace 8 or more SPACEs at beginning of line by TABs, if
1534 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1535 SPACEs.
1536 If `whitespace-style' includes the value `indentation::tab',
1537 replace 8 or more SPACEs at beginning of line by TABs.
1538 If `whitespace-style' includes the value `indentation::space',
1539 replace TABs by SPACEs.
1541 2. SPACEs before TAB.
1542 If `whitespace-style' includes the value `space-before-tab':
1543 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1544 otherwise, replace TABs by SPACEs.
1545 If `whitespace-style' includes the value
1546 `space-before-tab::tab', replace SPACEs by TABs.
1547 If `whitespace-style' includes the value
1548 `space-before-tab::space', replace TABs by SPACEs.
1550 3. SPACEs or TABs at end of line.
1551 If `whitespace-style' includes the value `trailing', remove
1552 all SPACEs or TABs at end of line.
1554 4. 8 or more SPACEs after TAB.
1555 If `whitespace-style' includes the value `space-after-tab':
1556 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1557 otherwise, replace TABs by SPACEs.
1558 If `whitespace-style' includes the value
1559 `space-after-tab::tab', replace SPACEs by TABs.
1560 If `whitespace-style' includes the value
1561 `space-after-tab::space', replace TABs by SPACEs.
1563 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1564 documentation."
1565 (interactive "@r")
1566 (if buffer-read-only
1567 ;; read-only buffer
1568 (whitespace-warn-read-only "cleanup region")
1569 ;; non-read-only buffer
1570 (whitespace-ensure-local-variables)
1571 (let ((rstart (min start end))
1572 (rend (copy-marker (max start end)))
1573 (indent-tabs-mode whitespace-indent-tabs-mode)
1574 (tab-width whitespace-tab-width)
1575 overwrite-mode ; enforce no overwrite
1576 tmp)
1577 (save-excursion
1578 (save-match-data ;FIXME: Why?
1579 ;; PROBLEM 1: 8 or more SPACEs at bol
1580 (cond
1581 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1582 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1583 ;; by SPACEs.
1584 ((memq 'indentation whitespace-style)
1585 (let ((regexp (whitespace-indentation-regexp)))
1586 (goto-char rstart)
1587 (while (re-search-forward regexp rend t)
1588 (setq tmp (current-indentation))
1589 (goto-char (match-beginning 0))
1590 (delete-horizontal-space)
1591 (unless (eolp)
1592 (indent-to tmp)))))
1593 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1594 ((memq 'indentation::tab whitespace-style)
1595 (whitespace-replace-action
1596 'tabify rstart rend
1597 (whitespace-indentation-regexp 'tab) 0))
1598 ;; ACTION: replace TABs by SPACEs.
1599 ((memq 'indentation::space whitespace-style)
1600 (whitespace-replace-action
1601 'untabify rstart rend
1602 (whitespace-indentation-regexp 'space) 0)))
1603 ;; PROBLEM 3: SPACEs or TABs at eol
1604 ;; ACTION: remove all SPACEs or TABs at eol
1605 (when (memq 'trailing whitespace-style)
1606 (whitespace-replace-action
1607 'delete-region rstart rend
1608 whitespace-trailing-regexp 1))
1609 ;; PROBLEM 4: 8 or more SPACEs after TAB
1610 (cond
1611 ;; ACTION: replace 8 or more SPACEs by TABs, if
1612 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1613 ;; by SPACEs.
1614 ((memq 'space-after-tab whitespace-style)
1615 (whitespace-replace-action
1616 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1617 rstart rend (whitespace-space-after-tab-regexp) 1))
1618 ;; ACTION: replace 8 or more SPACEs by TABs.
1619 ((memq 'space-after-tab::tab whitespace-style)
1620 (whitespace-replace-action
1621 'tabify rstart rend
1622 (whitespace-space-after-tab-regexp 'tab) 1))
1623 ;; ACTION: replace TABs by SPACEs.
1624 ((memq 'space-after-tab::space whitespace-style)
1625 (whitespace-replace-action
1626 'untabify rstart rend
1627 (whitespace-space-after-tab-regexp 'space) 1)))
1628 ;; PROBLEM 2: SPACEs before TAB
1629 (cond
1630 ;; ACTION: replace SPACEs before TAB by TABs, if
1631 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1632 ;; by SPACEs.
1633 ((memq 'space-before-tab whitespace-style)
1634 (whitespace-replace-action
1635 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1636 rstart rend whitespace-space-before-tab-regexp
1637 (if whitespace-indent-tabs-mode 0 2)))
1638 ;; ACTION: replace SPACEs before TAB by TABs.
1639 ((memq 'space-before-tab::tab whitespace-style)
1640 (whitespace-replace-action
1641 'tabify rstart rend
1642 whitespace-space-before-tab-regexp 0))
1643 ;; ACTION: replace TABs by SPACEs.
1644 ((memq 'space-before-tab::space whitespace-style)
1645 (whitespace-replace-action
1646 'untabify rstart rend
1647 whitespace-space-before-tab-regexp 2)))))
1648 (set-marker rend nil)))) ; point marker to nowhere
1651 (defun whitespace-replace-action (action rstart rend regexp index)
1652 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1654 INDEX is the level group matched by REGEXP and used by ACTION.
1656 See also `tab-width'."
1657 (goto-char rstart)
1658 (while (re-search-forward regexp rend t)
1659 (goto-char (match-end index))
1660 (funcall action (match-beginning index) (match-end index))))
1663 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1664 ;;;; User command - report
1667 (defun whitespace-regexp (regexp &optional kind)
1668 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1669 (cond
1670 ((or (eq kind 'tab)
1671 whitespace-indent-tabs-mode)
1672 (format (car regexp) whitespace-tab-width))
1673 ((or (eq kind 'space)
1674 (not whitespace-indent-tabs-mode))
1675 (cdr regexp))))
1678 (defun whitespace-indentation-regexp (&optional kind)
1679 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1680 (whitespace-regexp whitespace-indentation-regexp kind))
1683 (defun whitespace-space-after-tab-regexp (&optional kind)
1684 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1685 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1688 (defconst whitespace-report-list
1689 (list
1690 (cons 'empty whitespace-empty-at-bob-regexp)
1691 (cons 'empty whitespace-empty-at-eob-regexp)
1692 (cons 'trailing whitespace-trailing-regexp)
1693 (cons 'indentation nil)
1694 (cons 'indentation::tab nil)
1695 (cons 'indentation::space nil)
1696 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1697 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1698 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1699 (cons 'space-after-tab nil)
1700 (cons 'space-after-tab::tab nil)
1701 (cons 'space-after-tab::space nil)
1703 "List of whitespace bogus symbol and corresponding regexp.")
1706 (defconst whitespace-report-text
1707 '( ;; `indent-tabs-mode' has non-nil value
1709 Whitespace Report
1711 Current Setting Whitespace Problem
1713 empty [] [] empty lines at beginning of buffer
1714 empty [] [] empty lines at end of buffer
1715 trailing [] [] SPACEs or TABs at end of line
1716 indentation [] [] 8 or more SPACEs at beginning of line
1717 indentation::tab [] [] 8 or more SPACEs at beginning of line
1718 indentation::space [] [] TABs at beginning of line
1719 space-before-tab [] [] SPACEs before TAB
1720 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1721 space-before-tab::space [] [] SPACEs before TAB: TABs
1722 space-after-tab [] [] 8 or more SPACEs after TAB
1723 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1724 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1726 indent-tabs-mode =
1727 tab-width = \n\n"
1728 . ;; `indent-tabs-mode' has nil value
1730 Whitespace Report
1732 Current Setting Whitespace Problem
1734 empty [] [] empty lines at beginning of buffer
1735 empty [] [] empty lines at end of buffer
1736 trailing [] [] SPACEs or TABs at end of line
1737 indentation [] [] TABs at beginning of line
1738 indentation::tab [] [] 8 or more SPACEs at beginning of line
1739 indentation::space [] [] TABs at beginning of line
1740 space-before-tab [] [] SPACEs before TAB
1741 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1742 space-before-tab::space [] [] SPACEs before TAB: TABs
1743 space-after-tab [] [] 8 or more SPACEs after TAB
1744 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1745 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1747 indent-tabs-mode =
1748 tab-width = \n\n")
1749 "Text for whitespace bogus report.
1751 It is a cons of strings, where the car part is used when
1752 `indent-tabs-mode' is non-nil, and the cdr part is used when
1753 `indent-tabs-mode' is nil.")
1756 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1757 "The buffer name for whitespace bogus report.")
1760 ;;;###autoload
1761 (defun whitespace-report (&optional force report-if-bogus)
1762 "Report some whitespace problems in buffer.
1764 Perform `whitespace-report-region' on the current buffer."
1765 (interactive (list current-prefix-arg))
1766 (whitespace-report-region (point-min) (point-max)
1767 force report-if-bogus))
1770 ;;;###autoload
1771 (defun whitespace-report-region (start end &optional force report-if-bogus)
1772 "Report some whitespace problems in a region.
1774 Return nil if there is no whitespace problem; otherwise, return
1775 non-nil.
1777 If FORCE is non-nil or \\[universal-argument] was pressed just
1778 before calling `whitespace-report-region' interactively, it
1779 forces `whitespace-style' to have:
1781 empty
1782 trailing
1783 indentation
1784 space-before-tab
1785 space-after-tab
1787 If REPORT-IF-BOGUS is t, it reports only when there are any
1788 whitespace problems in buffer; if it is `never', it does not
1789 report problems.
1791 Report if some of the following whitespace problems exist:
1793 * If `indent-tabs-mode' is non-nil:
1794 empty 1. empty lines at beginning of buffer.
1795 empty 2. empty lines at end of buffer.
1796 trailing 3. SPACEs or TABs at end of line.
1797 indentation 4. 8 or more SPACEs at beginning of line.
1798 space-before-tab 5. SPACEs before TAB.
1799 space-after-tab 6. 8 or more SPACEs after TAB.
1801 * If `indent-tabs-mode' is nil:
1802 empty 1. empty lines at beginning of buffer.
1803 empty 2. empty lines at end of buffer.
1804 trailing 3. SPACEs or TABs at end of line.
1805 indentation 4. TABS at beginning of line.
1806 space-before-tab 5. SPACEs before TAB.
1807 space-after-tab 6. 8 or more SPACEs after TAB.
1809 See `whitespace-style' for documentation.
1810 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1811 cleaning up these problems."
1812 (interactive "r")
1813 (setq force (or current-prefix-arg force))
1814 (save-excursion
1815 (save-match-data ;FIXME: Why?
1816 (let* ((has-bogus nil)
1817 (rstart (min start end))
1818 (rend (max start end))
1819 (bogus-list
1820 (mapcar
1821 #'(lambda (option)
1822 (when force
1823 (add-to-list 'whitespace-style (car option)))
1824 (goto-char rstart)
1825 (let ((regexp
1826 (cond
1827 ((eq (car option) 'indentation)
1828 (whitespace-indentation-regexp))
1829 ((eq (car option) 'indentation::tab)
1830 (whitespace-indentation-regexp 'tab))
1831 ((eq (car option) 'indentation::space)
1832 (whitespace-indentation-regexp 'space))
1833 ((eq (car option) 'space-after-tab)
1834 (whitespace-space-after-tab-regexp))
1835 ((eq (car option) 'space-after-tab::tab)
1836 (whitespace-space-after-tab-regexp 'tab))
1837 ((eq (car option) 'space-after-tab::space)
1838 (whitespace-space-after-tab-regexp 'space))
1840 (cdr option)))))
1841 (and (re-search-forward regexp rend t)
1842 (setq has-bogus t))))
1843 whitespace-report-list)))
1844 (when (pcase report-if-bogus (`nil t) (`never nil) (_ has-bogus))
1845 (whitespace-kill-buffer whitespace-report-buffer-name)
1846 ;; `whitespace-indent-tabs-mode' is local to current buffer
1847 ;; `whitespace-tab-width' is local to current buffer
1848 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1849 (ws-tab-width whitespace-tab-width))
1850 (with-current-buffer (get-buffer-create
1851 whitespace-report-buffer-name)
1852 (erase-buffer)
1853 (insert (if ws-indent-tabs-mode
1854 (car whitespace-report-text)
1855 (cdr whitespace-report-text)))
1856 (goto-char (point-min))
1857 (forward-line 3)
1858 (dolist (option whitespace-report-list)
1859 (forward-line 1)
1860 (whitespace-mark-x
1861 27 (memq (car option) whitespace-style))
1862 (whitespace-mark-x 7 (car bogus-list))
1863 (setq bogus-list (cdr bogus-list)))
1864 (forward-line 1)
1865 (whitespace-insert-value ws-indent-tabs-mode)
1866 (whitespace-insert-value ws-tab-width)
1867 (when has-bogus
1868 (goto-char (point-max))
1869 (insert " Type `M-x whitespace-cleanup'"
1870 " to cleanup the buffer.\n\n"
1871 " Type `M-x whitespace-cleanup-region'"
1872 " to cleanup a region.\n\n"))
1873 (whitespace-display-window (current-buffer)))))
1874 has-bogus))))
1877 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1878 ;;;; Internal functions
1881 (defvar whitespace-font-lock-keywords nil
1882 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1885 (defconst whitespace-help-text
1887 Whitespace Toggle Options | scroll up : SPC or > |
1888 | scroll down: M-SPC or < |
1889 FACES \\__________________________/
1890 [] f - toggle face visualization
1891 [] t - toggle TAB visualization
1892 [] s - toggle SPACE and HARD SPACE visualization
1893 [] r - toggle trailing blanks visualization
1894 [] l - toggle \"long lines\" visualization
1895 [] L - toggle \"long lines\" tail visualization
1896 [] n - toggle NEWLINE visualization
1897 [] e - toggle empty line at bob and/or eob visualization
1898 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1899 [] I - toggle indentation SPACEs visualization
1900 [] i - toggle indentation TABs visualization
1901 [] C-t - toggle big indentation visualization
1902 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1903 [] A - toggle SPACEs after TAB: SPACEs visualization
1904 [] a - toggle SPACEs after TAB: TABs visualization
1905 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1906 [] B - toggle SPACEs before TAB: SPACEs visualization
1907 [] b - toggle SPACEs before TAB: TABs visualization
1909 DISPLAY TABLE
1910 [] T - toggle TAB visualization
1911 [] S - toggle SPACE and HARD SPACE visualization
1912 [] N - toggle NEWLINE visualization
1914 x - restore `whitespace-style' value
1916 ? - display this text\n\n"
1917 "Text for whitespace toggle options.")
1920 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1921 "The buffer name for whitespace toggle options.")
1924 (defun whitespace-insert-value (value)
1925 "Insert VALUE at column 20 of next line."
1926 (forward-line 1)
1927 (move-to-column 20 t)
1928 (insert (format "%s" value)))
1931 (defun whitespace-mark-x (nchars condition)
1932 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1933 (forward-char nchars)
1934 (insert (if condition "X" " ")))
1937 (defun whitespace-insert-option-mark (the-list the-value)
1938 "Insert the option mark ('X' or ' ') in toggle options buffer."
1939 (goto-char (point-min))
1940 (forward-line 2)
1941 (dolist (sym the-list)
1942 (if (eq sym 'help-newline)
1943 (forward-line 2)
1944 (forward-line 1)
1945 (whitespace-mark-x 2 (memq sym the-value)))))
1948 (defun whitespace-help-on (style)
1949 "Display the whitespace toggle options."
1950 (unless (get-buffer whitespace-help-buffer-name)
1951 (delete-other-windows)
1952 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
1953 (with-current-buffer buffer
1954 (erase-buffer)
1955 (insert whitespace-help-text)
1956 (whitespace-insert-option-mark
1957 whitespace-style-value-list style)
1958 (whitespace-display-window buffer)))))
1961 (defun whitespace-display-window (buffer)
1962 "Display BUFFER in a new window."
1963 (goto-char (point-min))
1964 (set-buffer-modified-p nil)
1965 (when (< (window-height) (* 2 window-min-height))
1966 (kill-buffer buffer)
1967 (error "Window height is too small; \
1968 can't split window to display whitespace toggle options"))
1969 (let ((win (split-window)))
1970 (set-window-buffer win buffer)
1971 (shrink-window-if-larger-than-buffer win)))
1974 (defun whitespace-kill-buffer (buffer-name)
1975 "Kill buffer BUFFER-NAME and windows related with it."
1976 (let ((buffer (get-buffer buffer-name)))
1977 (when buffer
1978 (delete-windows-on buffer)
1979 (kill-buffer buffer))))
1982 (defun whitespace-help-off ()
1983 "Remove the buffer and window of the whitespace toggle options."
1984 (whitespace-kill-buffer whitespace-help-buffer-name))
1987 (defun whitespace-help-scroll (&optional up)
1988 "Scroll help window, if it exists.
1990 If UP is non-nil, scroll up; otherwise, scroll down."
1991 (condition-case nil
1992 (let ((buffer (get-buffer whitespace-help-buffer-name)))
1993 (if buffer
1994 (with-selected-window (get-buffer-window buffer)
1995 (if up
1996 (scroll-up 3)
1997 (scroll-down 3)))
1998 (ding)))
1999 ;; handler
2000 ((error)
2001 ;; just ignore error
2005 (defun whitespace-interactive-char (local-p)
2006 "Interactive function to read a char and return a symbol.
2008 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2009 uses a global context.
2011 It accepts one of the following chars:
2013 CHAR MEANING
2014 (VIA FACES)
2015 f toggle face visualization
2016 t toggle TAB visualization
2017 s toggle SPACE and HARD SPACE visualization
2018 r toggle trailing blanks visualization
2019 l toggle \"long lines\" visualization
2020 L toggle \"long lines\" tail visualization
2021 n toggle NEWLINE visualization
2022 e toggle empty line at bob and/or eob visualization
2023 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2024 I toggle indentation SPACEs visualization
2025 i toggle indentation TABs visualization
2026 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2027 A toggle SPACEs after TAB: SPACEs visualization
2028 a toggle SPACEs after TAB: TABs visualization
2029 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2030 B toggle SPACEs before TAB: SPACEs visualization
2031 b toggle SPACEs before TAB: TABs visualization
2033 (VIA DISPLAY TABLE)
2034 T toggle TAB visualization
2035 S toggle SPACE and HARD SPACE visualization
2036 N toggle NEWLINE visualization
2038 x restore `whitespace-style' value
2039 ? display brief help
2041 See also `whitespace-toggle-option-alist'."
2042 (let* ((is-off (not (if local-p
2043 whitespace-mode
2044 global-whitespace-mode)))
2045 (style (cond (is-off whitespace-style) ; use default value
2046 (local-p whitespace-active-style)
2047 (t whitespace-toggle-style)))
2048 (prompt
2049 (format "Whitespace Toggle %s (type ? for further options)-"
2050 (if local-p "Local" "Global")))
2051 ch sym)
2052 ;; read a valid option and get the corresponding symbol
2053 (save-window-excursion
2054 (condition-case data
2055 (progn
2056 (while
2057 ;; while condition
2058 (progn
2059 (setq ch (read-char prompt))
2060 (not
2061 (setq sym
2062 (cdr
2063 (assq ch whitespace-toggle-option-alist)))))
2064 ;; while body
2065 (cond
2066 ((eq ch ?\?) (whitespace-help-on style))
2067 ((eq ch ?\ ) (whitespace-help-scroll t))
2068 ((eq ch ?\M- ) (whitespace-help-scroll))
2069 ((eq ch ?>) (whitespace-help-scroll t))
2070 ((eq ch ?<) (whitespace-help-scroll))
2071 (t (ding))))
2072 (whitespace-help-off)
2073 (message " ")) ; clean echo area
2074 ;; handler
2075 ((quit error)
2076 (whitespace-help-off)
2077 (error (error-message-string data)))))
2078 (list sym))) ; return the appropriate symbol
2081 (defun whitespace-toggle-list (local-p arg the-list)
2082 "Toggle options in THE-LIST based on list ARG.
2084 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2085 uses a global context.
2087 ARG is a list of options to be toggled.
2089 THE-LIST is a list of options. This list will be toggled and the
2090 resultant list will be returned."
2091 (unless (if local-p whitespace-mode global-whitespace-mode)
2092 (setq the-list whitespace-style))
2093 (setq the-list (copy-sequence the-list)) ; keep original list
2094 (dolist (sym (if (listp arg) arg (list arg)))
2095 (cond
2096 ;; ignore help value
2097 ((eq sym 'help-newline))
2098 ;; restore default values
2099 ((eq sym 'whitespace-style)
2100 (setq the-list whitespace-style))
2101 ;; toggle valid values
2102 ((memq sym whitespace-style-value-list)
2103 (setq the-list (if (memq sym the-list)
2104 (delq sym the-list)
2105 (cons sym the-list))))))
2106 the-list)
2109 (defvar whitespace-display-table nil
2110 "Used to save a local display table.")
2112 (defvar whitespace-display-table-was-local nil
2113 "Used to remember whether a buffer initially had a local display table.")
2115 (defun whitespace-turn-on ()
2116 "Turn on whitespace visualization."
2117 ;; prepare local hooks
2118 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2119 ;; create whitespace local buffer environment
2120 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2121 (set (make-local-variable 'whitespace-display-table) nil)
2122 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2123 (set (make-local-variable 'whitespace-active-style)
2124 (if (listp whitespace-style)
2125 whitespace-style
2126 (list whitespace-style)))
2127 (whitespace-ensure-local-variables)
2128 ;; turn on whitespace
2129 (when whitespace-active-style
2130 (whitespace-color-on)
2131 (whitespace-display-char-on)))
2134 (defun whitespace-turn-off ()
2135 "Turn off whitespace visualization."
2136 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2137 (when whitespace-active-style
2138 (whitespace-color-off)
2139 (whitespace-display-char-off)))
2142 (defun whitespace-style-face-p ()
2143 "Return t if there is some visualization via face."
2144 (and (memq 'face whitespace-active-style)
2145 (or (memq 'tabs whitespace-active-style)
2146 (memq 'spaces whitespace-active-style)
2147 (memq 'trailing whitespace-active-style)
2148 (memq 'lines whitespace-active-style)
2149 (memq 'lines-tail whitespace-active-style)
2150 (memq 'newline whitespace-active-style)
2151 (memq 'empty whitespace-active-style)
2152 (memq 'indentation whitespace-active-style)
2153 (memq 'indentation::tab whitespace-active-style)
2154 (memq 'indentation::space whitespace-active-style)
2155 (memq 'big-indent whitespace-active-style)
2156 (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)
2159 (memq 'space-before-tab whitespace-active-style)
2160 (memq 'space-before-tab::tab whitespace-active-style)
2161 (memq 'space-before-tab::space whitespace-active-style))))
2164 (defun whitespace-color-on ()
2165 "Turn on color visualization."
2166 (when (whitespace-style-face-p)
2167 ;; save current point and refontify when necessary
2168 (set (make-local-variable 'whitespace-point)
2169 (point))
2170 (setq whitespace-point--used
2171 (let ((ol (make-overlay (point) (point) nil nil t)))
2172 (delete-overlay ol) ol))
2173 (set (make-local-variable 'whitespace-font-lock-refontify)
2175 (set (make-local-variable 'whitespace-bob-marker)
2176 (point-min-marker))
2177 (set (make-local-variable 'whitespace-eob-marker)
2178 (point-max-marker))
2179 (set (make-local-variable 'whitespace-buffer-changed)
2180 nil)
2181 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
2182 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
2183 ;; Add whitespace-mode color into font lock.
2184 (setq
2185 whitespace-font-lock-keywords
2187 (whitespace-point--flush-used)
2188 ,@(when (memq 'spaces whitespace-active-style)
2189 ;; Show SPACEs.
2190 `((,whitespace-space-regexp 1 whitespace-space t)
2191 ;; Show HARD SPACEs.
2192 (,whitespace-hspace-regexp 1 whitespace-hspace t)))
2193 ,@(when (memq 'tabs whitespace-active-style)
2194 ;; Show TABs.
2195 `((,whitespace-tab-regexp 1 whitespace-tab t)))
2196 ,@(when (memq 'trailing whitespace-active-style)
2197 ;; Show trailing blanks.
2198 `((,#'whitespace-trailing-regexp 1 whitespace-trailing t)))
2199 ,@(when (or (memq 'lines whitespace-active-style)
2200 (memq 'lines-tail whitespace-active-style))
2201 ;; Show "long" lines.
2202 `((,(let ((line-column (or whitespace-line-column fill-column)))
2203 (format
2204 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2205 whitespace-tab-width
2206 (1- whitespace-tab-width)
2207 (/ line-column whitespace-tab-width)
2208 (let ((rem (% line-column whitespace-tab-width)))
2209 (if (zerop rem)
2211 (format ".\\{%d\\}" rem)))))
2212 ,(if (memq 'lines whitespace-active-style)
2213 0 ; whole line
2214 2) ; line tail
2215 whitespace-line prepend)))
2216 ,@(when (or (memq 'space-before-tab whitespace-active-style)
2217 (memq 'space-before-tab::tab whitespace-active-style)
2218 (memq 'space-before-tab::space whitespace-active-style))
2219 `((,whitespace-space-before-tab-regexp
2220 ,(cond
2221 ((memq 'space-before-tab whitespace-active-style)
2222 ;; Show SPACEs before TAB (indent-tabs-mode).
2223 (if whitespace-indent-tabs-mode 1 2))
2224 ((memq 'space-before-tab::tab whitespace-active-style)
2226 ((memq 'space-before-tab::space whitespace-active-style)
2228 whitespace-space-before-tab t)))
2229 ,@(when (or (memq 'indentation whitespace-active-style)
2230 (memq 'indentation::tab whitespace-active-style)
2231 (memq 'indentation::space whitespace-active-style))
2232 `((,(cond
2233 ((memq 'indentation whitespace-active-style)
2234 ;; Show indentation SPACEs (indent-tabs-mode).
2235 (whitespace-indentation-regexp))
2236 ((memq 'indentation::tab whitespace-active-style)
2237 ;; Show indentation SPACEs (SPACEs).
2238 (whitespace-indentation-regexp 'tab))
2239 ((memq 'indentation::space whitespace-active-style)
2240 ;; Show indentation SPACEs (TABs).
2241 (whitespace-indentation-regexp 'space)))
2242 1 whitespace-indentation t)))
2243 ,@(when (memq 'big-indent whitespace-active-style)
2244 ;; Show big indentation.
2245 `((,whitespace-big-indent-regexp 1 'whitespace-big-indent t)))
2246 ,@(when (memq 'empty whitespace-active-style)
2247 ;; Show empty lines at beginning of buffer.
2248 `((,#'whitespace-empty-at-bob-regexp
2249 1 whitespace-empty t)
2250 ;; Show empty lines at end of buffer.
2251 (,#'whitespace-empty-at-eob-regexp
2252 1 whitespace-empty t)))
2253 ,@(when (or (memq 'space-after-tab whitespace-active-style)
2254 (memq 'space-after-tab::tab whitespace-active-style)
2255 (memq 'space-after-tab::space whitespace-active-style))
2256 `((,(cond
2257 ((memq 'space-after-tab whitespace-active-style)
2258 ;; Show SPACEs after TAB (indent-tabs-mode).
2259 (whitespace-space-after-tab-regexp))
2260 ((memq 'space-after-tab::tab whitespace-active-style)
2261 ;; Show SPACEs after TAB (SPACEs).
2262 (whitespace-space-after-tab-regexp 'tab))
2263 ((memq 'space-after-tab::space whitespace-active-style)
2264 ;; Show SPACEs after TAB (TABs).
2265 (whitespace-space-after-tab-regexp 'space)))
2266 1 whitespace-space-after-tab t)))))
2267 (font-lock-add-keywords nil whitespace-font-lock-keywords t)
2268 (font-lock-flush)))
2271 (defun whitespace-color-off ()
2272 "Turn off color visualization."
2273 ;; turn off font lock
2274 (kill-local-variable 'whitespace-point--used)
2275 (when (whitespace-style-face-p)
2276 (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
2277 (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
2278 (font-lock-remove-keywords nil whitespace-font-lock-keywords)
2279 (font-lock-flush)))
2281 (defun whitespace-point--used (start end)
2282 (let ((ostart (overlay-start whitespace-point--used)))
2283 (if ostart
2284 (move-overlay whitespace-point--used
2285 (min start ostart)
2286 (max end (overlay-end whitespace-point--used)))
2287 (move-overlay whitespace-point--used start end))))
2289 (defun whitespace-point--flush-used (limit)
2290 (let ((ostart (overlay-start whitespace-point--used)))
2291 ;; Strip parts of whitespace-point--used we're about to refresh.
2292 (when ostart
2293 (let ((oend (overlay-end whitespace-point--used)))
2294 (if (<= (point) ostart)
2295 (if (<= oend limit)
2296 (delete-overlay whitespace-point--used)
2297 (move-overlay whitespace-point--used limit oend)))
2298 (if (<= oend limit)
2299 (move-overlay whitespace-point--used ostart (point))))))
2300 nil)
2302 (defun whitespace-trailing-regexp (limit)
2303 "Match trailing spaces which do not contain the point at end of line."
2304 (let ((status t))
2305 (while (if (re-search-forward whitespace-trailing-regexp limit t)
2306 (when (= whitespace-point (match-end 1)) ; Loop if point at eol.
2307 (whitespace-point--used (match-beginning 0) (match-end 0))
2309 (setq status nil))) ;; end of buffer
2310 status))
2313 (defun whitespace-empty-at-bob-regexp (limit)
2314 "Match spaces at beginning of buffer which do not contain the point at \
2315 beginning of buffer."
2316 (let ((b (point))
2318 (cond
2319 ;; at bob
2320 ((= b 1)
2321 (setq r (and (looking-at whitespace-empty-at-bob-regexp)
2322 (or (/= whitespace-point 1)
2323 (progn (whitespace-point--used (match-beginning 0)
2324 (match-end 0))
2325 nil))))
2326 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2327 ;; inside bob empty region
2328 ((<= limit whitespace-bob-marker)
2329 (setq r (looking-at whitespace-empty-at-bob-regexp))
2330 (if r
2331 (when (< (match-end 1) limit)
2332 (set-marker whitespace-bob-marker (match-end 1)))
2333 (set-marker whitespace-bob-marker b)))
2334 ;; intersection with end of bob empty region
2335 ((<= b whitespace-bob-marker)
2336 (setq r (looking-at whitespace-empty-at-bob-regexp))
2337 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2338 ;; it is not inside bob empty region
2340 (setq r nil)))
2341 ;; move to end of matching
2342 (and r (goto-char (match-end 1)))
2346 (defsubst whitespace-looking-back (regexp limit)
2347 (save-excursion
2348 (when (/= 0 (skip-chars-backward " \t\n" limit))
2349 (unless (bolp)
2350 (forward-line 1))
2351 (looking-at regexp))))
2354 (defun whitespace-empty-at-eob-regexp (limit)
2355 "Match spaces at end of buffer which do not contain the point at end of \
2356 buffer."
2357 (let ((b (point))
2358 (e (1+ (buffer-size)))
2360 (cond
2361 ;; at eob
2362 ((= limit e)
2363 (goto-char limit)
2364 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2365 (when (and r (= whitespace-point e))
2366 (setq r nil)
2367 (whitespace-point--used (match-beginning 0) (match-end 0)))
2368 (if r
2369 (set-marker whitespace-eob-marker (match-beginning 1))
2370 (set-marker whitespace-eob-marker limit)
2371 (goto-char b))) ; return back to initial position
2372 ;; inside eob empty region
2373 ((>= b whitespace-eob-marker)
2374 (goto-char limit)
2375 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2376 (if r
2377 (when (> (match-beginning 1) b)
2378 (set-marker whitespace-eob-marker (match-beginning 1)))
2379 (set-marker whitespace-eob-marker limit)
2380 (goto-char b))) ; return back to initial position
2381 ;; intersection with beginning of eob empty region
2382 ((>= limit whitespace-eob-marker)
2383 (goto-char limit)
2384 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2385 (if r
2386 (set-marker whitespace-eob-marker (match-beginning 1))
2387 (set-marker whitespace-eob-marker limit)
2388 (goto-char b))) ; return back to initial position
2389 ;; it is not inside eob empty region
2391 (setq r nil)))
2395 (defun whitespace-buffer-changed (_beg _end)
2396 "Set `whitespace-buffer-changed' variable to t."
2397 (setq whitespace-buffer-changed t))
2400 (defun whitespace-post-command-hook ()
2401 "Save current point into `whitespace-point' variable.
2402 Also refontify when necessary."
2403 (unless (and (eq whitespace-point (point))
2404 (not whitespace-buffer-changed))
2405 (setq whitespace-point (point)) ; current point position
2406 (let ((refontify
2407 (cond
2408 ;; It is at end of buffer (eob).
2409 ((= whitespace-point (1+ (buffer-size)))
2410 (when (whitespace-looking-back whitespace-empty-at-eob-regexp
2411 nil)
2412 (match-beginning 0)))
2413 ;; It is at end of line ...
2414 ((and (eolp)
2415 ;; ... with trailing SPACE or TAB
2416 (or (memq (preceding-char) '(?\s ?\t))))
2417 (line-beginning-position))
2418 ;; It is at beginning of buffer (bob).
2419 ((and (= whitespace-point 1)
2420 (looking-at whitespace-empty-at-bob-regexp))
2421 (match-end 0))))
2422 (ostart (overlay-start whitespace-point--used)))
2423 (cond
2424 ((not refontify)
2425 ;; New point does not affect highlighting: just refresh the
2426 ;; highlighting of old point, if needed.
2427 (when ostart
2428 (font-lock-flush ostart
2429 (overlay-end whitespace-point--used))
2430 (delete-overlay whitespace-point--used)))
2431 ((not ostart)
2432 ;; Old point did not affect highlighting, but new one does: refresh the
2433 ;; highlighting of new point.
2434 (font-lock-flush (min refontify (point)) (max refontify (point))))
2435 ((save-excursion
2436 (goto-char ostart)
2437 (setq ostart (line-beginning-position))
2438 (and (<= ostart (max refontify (point)))
2439 (progn
2440 (goto-char (overlay-end whitespace-point--used))
2441 (let ((oend (line-beginning-position 2)))
2442 (<= (min refontify (point)) oend)))))
2443 ;; The old point highlighting and the new point highlighting
2444 ;; cover a contiguous region: do a single refresh.
2445 (font-lock-flush (min refontify (point) ostart)
2446 (max refontify (point)
2447 (overlay-end whitespace-point--used)))
2448 (delete-overlay whitespace-point--used))
2450 (font-lock-flush (min refontify (point))
2451 (max refontify (point)))
2452 (font-lock-flush ostart (overlay-end whitespace-point--used))
2453 (delete-overlay whitespace-point--used))))))
2456 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2457 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2460 (defun whitespace-style-mark-p ()
2461 "Return t if there is some visualization via display table."
2462 (or (memq 'tab-mark whitespace-active-style)
2463 (memq 'space-mark whitespace-active-style)
2464 (memq 'newline-mark whitespace-active-style)))
2467 (defsubst whitespace-char-valid-p (char)
2468 ;; This check should be improved!!!
2469 (or (< char 256)
2470 (characterp char)))
2473 (defun whitespace-display-vector-p (vec)
2474 "Return true if every character in vector VEC can be displayed."
2475 (let ((i (length vec)))
2476 (when (> i 0)
2477 (while (and (>= (setq i (1- i)) 0)
2478 (whitespace-char-valid-p (aref vec i))))
2479 (< i 0))))
2482 (defun whitespace-display-char-on ()
2483 "Turn on character display mapping."
2484 (when (and whitespace-display-mappings
2485 (whitespace-style-mark-p))
2486 (let (vecs vec)
2487 ;; Remember whether a buffer has a local display table.
2488 (unless whitespace-display-table-was-local
2489 (setq whitespace-display-table-was-local t
2490 whitespace-display-table
2491 (copy-sequence buffer-display-table))
2492 ;; Assure `buffer-display-table' is unique
2493 ;; when two or more windows are visible.
2494 (setq buffer-display-table
2495 (copy-sequence buffer-display-table)))
2496 (unless buffer-display-table
2497 (setq buffer-display-table (make-display-table)))
2498 (dolist (entry whitespace-display-mappings)
2499 ;; check if it is to display this mark
2500 (when (memq (car entry) whitespace-style)
2501 ;; Get a displayable mapping.
2502 (setq vecs (cddr entry))
2503 (while (and vecs
2504 (not (whitespace-display-vector-p (car vecs))))
2505 (setq vecs (cdr vecs)))
2506 ;; Display a valid mapping.
2507 (when vecs
2508 (setq vec (copy-sequence (car vecs)))
2509 ;; NEWLINE char
2510 (when (and (eq (cadr entry) ?\n)
2511 (memq 'newline whitespace-active-style))
2512 ;; Only insert face bits on NEWLINE char mapping to avoid
2513 ;; obstruction of other faces like TABs and (HARD) SPACEs
2514 ;; faces, font-lock faces, etc.
2515 (dotimes (i (length vec))
2516 (or (eq (aref vec i) ?\n)
2517 (aset vec i
2518 (make-glyph-code (aref vec i)
2519 whitespace-newline)))))
2520 ;; Display mapping
2521 (aset buffer-display-table (cadr entry) vec)))))))
2524 (defun whitespace-display-char-off ()
2525 "Turn off character display mapping."
2526 (and whitespace-display-mappings
2527 (whitespace-style-mark-p)
2528 whitespace-display-table-was-local
2529 (setq whitespace-display-table-was-local nil
2530 buffer-display-table whitespace-display-table)))
2533 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2534 ;;;; Hook
2537 (defun whitespace-action-when-on ()
2538 "Action to be taken always when local whitespace is turned on."
2539 (cond ((memq 'cleanup whitespace-action)
2540 (whitespace-cleanup))
2541 ((memq 'report-on-bogus whitespace-action)
2542 (whitespace-report nil t))))
2545 (defun whitespace-write-file-hook ()
2546 "Action to be taken when buffer is written.
2547 It should be added buffer-locally to `write-file-functions'."
2548 (cond ((memq 'auto-cleanup whitespace-action)
2549 (whitespace-cleanup))
2550 ((memq 'abort-on-bogus whitespace-action)
2551 (when (whitespace-report nil t)
2552 (error "Abort write due to whitespace problems in %s"
2553 (buffer-name)))))
2554 nil) ; continue hook processing
2557 (defun whitespace-warn-read-only (msg)
2558 "Warn if buffer is read-only."
2559 (when (memq 'warn-if-read-only whitespace-action)
2560 (message "Can't %s: %s is read-only" msg (buffer-name))))
2563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2566 (defun whitespace-unload-function ()
2567 "Unload the whitespace library."
2568 (global-whitespace-mode -1)
2569 ;; be sure all local whitespace mode is turned off
2570 (save-current-buffer
2571 (dolist (buf (buffer-list))
2572 (set-buffer buf)
2573 (whitespace-mode -1)))
2574 nil) ; continue standard unloading
2577 (provide 'whitespace)
2580 (run-hooks 'whitespace-load-hook)
2583 ;;; whitespace.el ends here