EIEIO&cl-generic: Add obsolescence warnings and fix corner case
[emacs.git] / lisp / whitespace.el
blobb4cd67ff6b91c2abed1b0d01330b551a08e0f535
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 '(repeat :tag "Kind of Blank"
542 (choice :tag "Kind of Blank Face"
543 (const :tag "(Face) Face visualization"
544 face)
545 (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
546 trailing)
547 (const :tag "(Face) SPACEs and HARD SPACEs"
548 spaces)
549 (const :tag "(Face) TABs" tabs)
550 (const :tag "(Face) Lines" lines)
551 (const :tag "(Face) SPACEs before TAB"
552 space-before-tab)
553 (const :tag "(Face) NEWLINEs" newline)
554 (const :tag "(Face) Indentation SPACEs"
555 indentation)
556 (const :tag "(Face) Too much line indentation"
557 big-indent)
558 (const :tag "(Face) Empty Lines At BOB And/Or EOB"
559 empty)
560 (const :tag "(Face) SPACEs after TAB"
561 space-after-tab)
562 (const :tag "(Mark) SPACEs and HARD SPACEs"
563 space-mark)
564 (const :tag "(Mark) TABs" tab-mark)
565 (const :tag "(Mark) NEWLINEs" newline-mark)))
566 :group 'whitespace)
568 (defvar whitespace-space 'whitespace-space
569 "Symbol face used to visualize SPACE.
570 Used when `whitespace-style' includes the value `spaces'.")
571 (make-obsolete-variable 'whitespace-space "use the face instead." "24.4")
574 (defface whitespace-space
575 '((((class color) (background dark))
576 :background "grey20" :foreground "darkgray")
577 (((class color) (background light))
578 :background "LightYellow" :foreground "lightgray")
579 (t :inverse-video t))
580 "Face used to visualize SPACE."
581 :group 'whitespace)
584 (defvar whitespace-hspace 'whitespace-hspace
585 "Symbol face used to visualize HARD SPACE.
586 Used when `whitespace-style' includes the value `spaces'.")
587 (make-obsolete-variable 'whitespace-hspace "use the face instead." "24.4")
589 (defface whitespace-hspace ; 'nobreak-space
590 '((((class color) (background dark))
591 :background "grey24" :foreground "darkgray")
592 (((class color) (background light))
593 :background "LemonChiffon3" :foreground "lightgray")
594 (t :inverse-video t))
595 "Face used to visualize HARD SPACE."
596 :group 'whitespace)
599 (defvar whitespace-tab 'whitespace-tab
600 "Symbol face used to visualize TAB.
601 Used when `whitespace-style' includes the value `tabs'.")
602 (make-obsolete-variable 'whitespace-tab "use the face instead." "24.4")
604 (defface whitespace-tab
605 '((((class color) (background dark))
606 :background "grey22" :foreground "darkgray")
607 (((class color) (background light))
608 :background "beige" :foreground "lightgray")
609 (t :inverse-video t))
610 "Face used to visualize TAB."
611 :group 'whitespace)
614 (defvar whitespace-newline 'whitespace-newline
615 "Symbol face used to visualize NEWLINE char mapping.
616 See `whitespace-display-mappings'.
617 Used when `whitespace-style' includes the values `newline-mark'
618 and `newline'.")
619 (make-obsolete-variable 'whitespace-newline "use the face instead." "24.4")
621 (defface whitespace-newline
622 '((default :weight normal)
623 (((class color) (background dark)) :foreground "darkgray")
624 (((class color) (min-colors 88) (background light)) :foreground "lightgray")
625 ;; Displays with 16 colors use lightgray as background, so using a
626 ;; lightgray foreground makes the newline mark invisible.
627 (((class color) (background light)) :foreground "brown")
628 (t :underline t))
629 "Face used to visualize NEWLINE char mapping.
631 See `whitespace-display-mappings'."
632 :group 'whitespace)
635 (defvar whitespace-trailing 'whitespace-trailing
636 "Symbol face used to visualize trailing blanks.
637 Used when `whitespace-style' includes the value `trailing'.")
638 (make-obsolete-variable 'whitespace-trailing "use the face instead." "24.4")
640 (defface whitespace-trailing ; 'trailing-whitespace
641 '((default :weight bold)
642 (((class mono)) :inverse-video t :underline t)
643 (t :background "red1" :foreground "yellow"))
644 "Face used to visualize trailing blanks."
645 :group 'whitespace)
648 (defvar whitespace-line 'whitespace-line
649 "Symbol face used to visualize \"long\" lines.
650 See `whitespace-line-column'.
651 Used when `whitespace-style' includes the value `line'.")
652 (make-obsolete-variable 'whitespace-line "use the face instead." "24.4")
654 (defface whitespace-line
655 '((((class mono)) :inverse-video t :weight bold :underline t)
656 (t :background "gray20" :foreground "violet"))
657 "Face used to visualize \"long\" lines.
659 See `whitespace-line-column'."
660 :group 'whitespace)
663 (defvar whitespace-space-before-tab 'whitespace-space-before-tab
664 "Symbol face used to visualize SPACEs before TAB.
665 Used when `whitespace-style' includes the value `space-before-tab'.")
666 (make-obsolete-variable 'whitespace-space-before-tab
667 "use the face instead." "24.4")
669 (defface whitespace-space-before-tab
670 '((((class mono)) :inverse-video t :weight bold :underline t)
671 (t :background "DarkOrange" :foreground "firebrick"))
672 "Face used to visualize SPACEs before TAB."
673 :group 'whitespace)
676 (defvar whitespace-indentation 'whitespace-indentation
677 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
678 Used when `whitespace-style' includes the value `indentation'.")
679 (make-obsolete-variable 'whitespace-indentation "use the face instead." "24.4")
681 (defface whitespace-indentation
682 '((((class mono)) :inverse-video t :weight bold :underline t)
683 (t :background "yellow" :foreground "firebrick"))
684 "Face used to visualize 8 or more SPACEs at beginning of line."
685 :group 'whitespace)
687 (defface whitespace-big-indent
688 '((((class mono)) :inverse-video t :weight bold :underline t)
689 (t :background "red" :foreground "firebrick"))
690 "Face used to visualize big indentation."
691 :group 'whitespace)
694 (defvar whitespace-empty 'whitespace-empty
695 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
696 Used when `whitespace-style' includes the value `empty'.")
697 (make-obsolete-variable 'whitespace-empty "use the face instead." "24.4")
699 (defface whitespace-empty
700 '((((class mono)) :inverse-video t :weight bold :underline t)
701 (t :background "yellow" :foreground "firebrick"))
702 "Face used to visualize empty lines at beginning and/or end of buffer."
703 :group 'whitespace)
706 (defvar whitespace-space-after-tab 'whitespace-space-after-tab
707 "Symbol face used to visualize 8 or more SPACEs after TAB.
708 Used when `whitespace-style' includes the value `space-after-tab'.")
709 (make-obsolete-variable 'whitespace-space-after-tab
710 "use the face instead." "24.4")
712 (defface whitespace-space-after-tab
713 '((((class mono)) :inverse-video t :weight bold :underline t)
714 (t :background "yellow" :foreground "firebrick"))
715 "Face used to visualize 8 or more SPACEs after TAB."
716 :group 'whitespace)
719 (defcustom whitespace-hspace-regexp
720 "\\(\u00A0+\\)"
721 "Specify HARD SPACE characters regexp.
723 Here are some examples:
725 \"\\\\(^\\xA0+\\\\)\" \
726 visualize only leading HARD SPACEs.
727 \"\\\\(\\xA0+$\\\\)\" \
728 visualize only trailing HARD SPACEs.
729 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
730 visualize leading and/or trailing HARD SPACEs.
731 \"\\t\\\\(\\xA0+\\\\)\\t\" \
732 visualize only HARD SPACEs between TABs.
734 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
735 Use exactly one pair of enclosing \\\\( and \\\\).
737 Used when `whitespace-style' includes `spaces'."
738 :type '(regexp :tag "HARD SPACE Chars")
739 :group 'whitespace)
742 (defcustom whitespace-space-regexp "\\( +\\)"
743 "Specify SPACE characters regexp.
745 If you're using `mule' package, there may be other characters
746 besides \" \" that should be considered SPACE.
748 Here are some examples:
750 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
751 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
752 \"\\\\(^ +\\\\| +$\\\\)\" \
753 visualize leading and/or trailing SPACEs.
754 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
756 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
757 Use exactly one pair of enclosing \\\\( and \\\\).
759 Used when `whitespace-style' includes `spaces'."
760 :type '(regexp :tag "SPACE Chars")
761 :group 'whitespace)
764 (defcustom whitespace-tab-regexp "\\(\t+\\)"
765 "Specify TAB characters regexp.
767 If you're using `mule' package, there may be other characters
768 besides \"\\t\" that should be considered TAB.
770 Here are some examples:
772 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
773 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
774 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
775 visualize leading and/or trailing TABs.
776 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
778 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
779 Use exactly one pair of enclosing \\\\( and \\\\).
781 Used when `whitespace-style' includes `tabs'."
782 :type '(regexp :tag "TAB Chars")
783 :group 'whitespace)
786 (defcustom whitespace-trailing-regexp
787 "\\([\t \u00A0]+\\)$"
788 "Specify trailing characters regexp.
790 There may be other characters besides:
792 \" \" \"\\t\" \"\\u00A0\"
794 that should be considered blank.
796 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
797 Use exactly one pair of enclosing elements above.
799 Used when `whitespace-style' includes `trailing'."
800 :type '(regexp :tag "Trailing Chars")
801 :group 'whitespace)
804 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
805 "Specify SPACEs before TAB regexp.
807 Used when `whitespace-style' includes `space-before-tab',
808 `space-before-tab::tab' or `space-before-tab::space'."
809 :type '(regexp :tag "SPACEs Before TAB")
810 :group 'whitespace)
813 (defcustom whitespace-indentation-regexp
814 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
815 . "^ *\\(\t+\\)[^\n]")
816 "Specify regexp for 8 or more SPACEs at beginning of line.
818 It is a cons where the cons car is used for SPACEs visualization
819 and the cons cdr is used for TABs visualization.
821 Used when `whitespace-style' includes `indentation',
822 `indentation::tab' or `indentation::space'."
823 :type '(cons (string :tag "Indentation SPACEs")
824 (string :tag "Indentation TABs"))
825 :group 'whitespace)
828 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
829 "Specify regexp for empty lines at beginning of buffer.
831 Used when `whitespace-style' includes `empty'."
832 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
833 :group 'whitespace)
836 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
837 "Specify regexp for empty lines at end of buffer.
839 Used when `whitespace-style' includes `empty'."
840 :type '(regexp :tag "Empty Lines At End Of Buffer")
841 :group 'whitespace)
844 (defcustom whitespace-space-after-tab-regexp
845 '("\t+\\(\\( \\{%d\\}\\)+\\)"
846 . "\\(\t+\\) +")
847 "Specify regexp for 8 or more SPACEs after TAB.
849 It is a cons where the cons car is used for SPACEs visualization
850 and the cons cdr is used for TABs visualization.
852 Used when `whitespace-style' includes `space-after-tab',
853 `space-after-tab::tab' or `space-after-tab::space'."
854 :type '(cons (string :tag "SPACEs After TAB")
855 string)
856 :group 'whitespace)
858 (defcustom whitespace-big-indent-regexp
859 "^\\(\\(?:\t\\{4,\\}\\| \\{32,\\}\\)[\t ]*\\)"
860 "Specify big indentation regexp.
862 If you're using `mule' package, there may be other characters
863 besides \"\\t\" that should be considered TAB.
865 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
866 Use exactly one pair of enclosing \\\\( and \\\\).
868 Used when `whitespace-style' includes `big-indent'."
869 :version "25.1"
870 :type '(regexp :tag "Detect too much indentation at the beginning of a line")
871 :group 'whitespace)
874 (defcustom whitespace-line-column 80
875 "Specify column beyond which the line is highlighted.
877 It must be an integer or nil. If nil, the `fill-column' variable value is
878 used.
880 Used when `whitespace-style' includes `lines' or `lines-tail'."
881 :type '(choice :tag "Line Length Limit"
882 (integer :tag "Line Length")
883 (const :tag "Use fill-column" nil))
884 :group 'whitespace)
887 ;; Hacked from `visible-whitespace-mappings' in visws.el
888 (defcustom whitespace-display-mappings
890 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
891 (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
892 ;; NEWLINE is displayed using the face `whitespace-newline'
893 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
894 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
895 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
896 ;; (newline-mark ?\n [?\u00AF ?\n] [?$ ?\n]) ; eol - overscore
897 ;; (newline-mark ?\n [?\u00AC ?\n] [?$ ?\n]) ; eol - negation
898 ;; (newline-mark ?\n [?\u00B0 ?\n] [?$ ?\n]) ; eol - degrees
900 ;; WARNING: the mapping below has a problem.
901 ;; When a TAB occupies exactly one column, it will display the
902 ;; character ?\xBB at that column followed by a TAB which goes to
903 ;; the next TAB column.
904 ;; If this is a problem for you, please, comment the line below.
905 (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
907 "Specify an alist of mappings for displaying characters.
909 Each element has the following form:
911 (KIND CHAR VECTOR...)
913 Where:
915 KIND is the kind of character.
916 It can be one of the following symbols:
918 tab-mark for TAB character
920 space-mark for SPACE or HARD SPACE character
922 newline-mark for NEWLINE character
924 CHAR is the character to be mapped.
926 VECTOR is a vector of characters to be displayed in place of CHAR.
927 The first display vector that can be displayed is used;
928 if no display vector for a mapping can be displayed, then
929 that character is displayed unmodified.
931 The NEWLINE character is displayed using the face given by
932 `whitespace-newline' variable.
934 Used when `whitespace-style' includes `tab-mark', `space-mark' or
935 `newline-mark'."
936 :type '(repeat
937 (list :tag "Character Mapping"
938 (choice :tag "Char Kind"
939 (const :tag "Tab" tab-mark)
940 (const :tag "Space" space-mark)
941 (const :tag "Newline" newline-mark))
942 (character :tag "Char")
943 (repeat :inline t :tag "Vector List"
944 (vector :tag ""
945 (repeat :inline t
946 :tag "Vector Characters"
947 (character :tag "Char"))))))
948 :group 'whitespace)
951 (defcustom whitespace-global-modes t
952 "Modes for which global `whitespace-mode' is automagically turned on.
954 Global `whitespace-mode' is controlled by the command
955 `global-whitespace-mode'.
957 If nil, means no modes have `whitespace-mode' automatically
958 turned on.
960 If t, all modes that support `whitespace-mode' have it
961 automatically turned on.
963 Else it should be a list of `major-mode' symbol names for which
964 `whitespace-mode' should be automatically turned on. The sense
965 of the list is negated if it begins with `not'. For example:
967 (c-mode c++-mode)
969 means that `whitespace-mode' is turned on for buffers in C and
970 C++ modes only."
971 :type '(choice :tag "Global Modes"
972 (const :tag "None" nil)
973 (const :tag "All" t)
974 (set :menu-tag "Mode Specific" :tag "Modes"
975 :value (not)
976 (const :tag "Except" not)
977 (repeat :inline t
978 (symbol :tag "Mode"))))
979 :group 'whitespace)
982 (defcustom whitespace-action nil
983 "Specify which action is taken when a buffer is visited or written.
985 It's a list containing some or all of the following values:
987 nil no action is taken.
989 cleanup cleanup any bogus whitespace always when local
990 whitespace is turned on.
991 See `whitespace-cleanup' and
992 `whitespace-cleanup-region'.
994 report-on-bogus report if there is any bogus whitespace always
995 when local whitespace is turned on.
997 auto-cleanup cleanup any bogus whitespace when buffer is
998 written.
999 See `whitespace-cleanup' and
1000 `whitespace-cleanup-region'.
1002 abort-on-bogus abort if there is any bogus whitespace and the
1003 buffer is written.
1005 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
1006 is included in `whitespace-action' and the
1007 buffer is read-only.
1009 Any other value is treated as nil."
1010 :type '(choice :tag "Actions"
1011 (const :tag "None" nil)
1012 (repeat :tag "Action List"
1013 (choice :tag "Action"
1014 (const :tag "Cleanup When On" cleanup)
1015 (const :tag "Report On Bogus" report-on-bogus)
1016 (const :tag "Auto Cleanup" auto-cleanup)
1017 (const :tag "Abort On Bogus" abort-on-bogus)
1018 (const :tag "Warn If Read-Only" warn-if-read-only))))
1019 :group 'whitespace)
1022 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1023 ;;;; User commands - Local mode
1026 ;;;###autoload
1027 (define-minor-mode whitespace-mode
1028 "Toggle whitespace visualization (Whitespace mode).
1029 With a prefix argument ARG, enable Whitespace mode if ARG is
1030 positive, and disable it otherwise. If called from Lisp, enable
1031 the mode if ARG is omitted or nil.
1033 See also `whitespace-style', `whitespace-newline' and
1034 `whitespace-display-mappings'."
1035 :lighter " ws"
1036 :init-value nil
1037 :global nil
1038 :group 'whitespace
1039 (cond
1040 (noninteractive ; running a batch job
1041 (setq whitespace-mode nil))
1042 (whitespace-mode ; whitespace-mode on
1043 (whitespace-turn-on)
1044 (whitespace-action-when-on))
1045 (t ; whitespace-mode off
1046 (whitespace-turn-off))))
1049 ;;;###autoload
1050 (define-minor-mode whitespace-newline-mode
1051 "Toggle newline visualization (Whitespace Newline mode).
1052 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1053 is positive, and disable it otherwise. If called from Lisp,
1054 enable the mode if ARG is omitted or nil.
1056 Use `whitespace-newline-mode' only for NEWLINE visualization
1057 exclusively. For other visualizations, including NEWLINE
1058 visualization together with (HARD) SPACEs and/or TABs, please,
1059 use `whitespace-mode'.
1061 See also `whitespace-newline' and `whitespace-display-mappings'."
1062 :lighter " nl"
1063 :init-value nil
1064 :global nil
1065 :group 'whitespace
1066 (let ((whitespace-style '(face newline-mark newline)))
1067 (whitespace-mode (if whitespace-newline-mode
1068 1 -1)))
1069 ;; sync states (running a batch job)
1070 (setq whitespace-newline-mode whitespace-mode))
1073 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1074 ;;;; User commands - Global mode
1077 ;;;###autoload
1078 (define-minor-mode global-whitespace-mode
1079 "Toggle whitespace visualization globally (Global Whitespace mode).
1080 With a prefix argument ARG, enable Global Whitespace mode if ARG
1081 is positive, and disable it otherwise. If called from Lisp,
1082 enable it if ARG is omitted or nil.
1084 See also `whitespace-style', `whitespace-newline' and
1085 `whitespace-display-mappings'."
1086 :lighter " WS"
1087 :init-value nil
1088 :global t
1089 :group 'whitespace
1090 (cond
1091 (noninteractive ; running a batch job
1092 (setq global-whitespace-mode nil))
1093 (global-whitespace-mode ; global-whitespace-mode on
1094 (save-current-buffer
1095 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1096 (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1097 (dolist (buffer (buffer-list)) ; adjust all local mode
1098 (set-buffer buffer)
1099 (unless whitespace-mode
1100 (whitespace-turn-on-if-enabled)))))
1101 (t ; global-whitespace-mode off
1102 (save-current-buffer
1103 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1104 (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1105 (dolist (buffer (buffer-list)) ; adjust all local mode
1106 (set-buffer buffer)
1107 (unless whitespace-mode
1108 (whitespace-turn-off)))))))
1110 (defvar whitespace-enable-predicate
1111 (lambda ()
1112 (and (cond
1113 ((eq whitespace-global-modes t))
1114 ((listp whitespace-global-modes)
1115 (if (eq (car-safe whitespace-global-modes) 'not)
1116 (not (memq major-mode (cdr whitespace-global-modes)))
1117 (memq major-mode whitespace-global-modes)))
1118 (t nil))
1119 ;; ...we have a display (not running a batch job)
1120 (not noninteractive)
1121 ;; ...the buffer is not internal (name starts with a space)
1122 (not (eq (aref (buffer-name) 0) ?\ ))
1123 ;; ...the buffer is not special (name starts with *)
1124 (or (not (eq (aref (buffer-name) 0) ?*))
1125 ;; except the scratch buffer.
1126 (string= (buffer-name) "*scratch*"))))
1127 "Predicate to decide which buffers obey `global-whitespace-mode'.
1128 This function is called with no argument and should return non-nil
1129 if the current buffer should obey `global-whitespace-mode'.
1130 This variable is normally modified via `add-function'.")
1132 (defun whitespace-turn-on-if-enabled ()
1133 (when (funcall whitespace-enable-predicate)
1134 (whitespace-turn-on)))
1136 ;;;###autoload
1137 (define-minor-mode global-whitespace-newline-mode
1138 "Toggle global newline visualization (Global Whitespace Newline mode).
1139 With a prefix argument ARG, enable Global Whitespace Newline mode
1140 if ARG is positive, and disable it otherwise. If called from
1141 Lisp, enable it if ARG is omitted or nil.
1143 Use `global-whitespace-newline-mode' only for NEWLINE
1144 visualization exclusively. For other visualizations, including
1145 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1146 please use `global-whitespace-mode'.
1148 See also `whitespace-newline' and `whitespace-display-mappings'."
1149 :lighter " NL"
1150 :init-value nil
1151 :global t
1152 :group 'whitespace
1153 (let ((whitespace-style '(newline-mark newline)))
1154 (global-whitespace-mode (if global-whitespace-newline-mode
1155 1 -1))
1156 ;; sync states (running a batch job)
1157 (setq global-whitespace-newline-mode global-whitespace-mode)))
1160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1161 ;;;; User commands - Toggle
1164 (defconst whitespace-style-value-list
1165 '(face
1166 tabs
1167 spaces
1168 trailing
1169 lines
1170 lines-tail
1171 newline
1172 empty
1173 indentation
1174 indentation::tab
1175 indentation::space
1176 big-indent
1177 space-after-tab
1178 space-after-tab::tab
1179 space-after-tab::space
1180 space-before-tab
1181 space-before-tab::tab
1182 space-before-tab::space
1183 help-newline ; value used by `whitespace-insert-option-mark'
1184 tab-mark
1185 space-mark
1186 newline-mark
1188 "List of valid `whitespace-style' values.")
1191 (defconst whitespace-toggle-option-alist
1192 '((?f . face)
1193 (?t . tabs)
1194 (?s . spaces)
1195 (?r . trailing)
1196 (?l . lines)
1197 (?L . lines-tail)
1198 (?n . newline)
1199 (?e . empty)
1200 (?\C-i . indentation)
1201 (?I . indentation::tab)
1202 (?i . indentation::space)
1203 (?\C-t . big-indent)
1204 (?\C-a . space-after-tab)
1205 (?A . space-after-tab::tab)
1206 (?a . space-after-tab::space)
1207 (?\C-b . space-before-tab)
1208 (?B . space-before-tab::tab)
1209 (?b . space-before-tab::space)
1210 (?T . tab-mark)
1211 (?S . space-mark)
1212 (?N . newline-mark)
1213 (?x . whitespace-style)
1215 "Alist of toggle options.
1217 Each element has the form:
1219 (CHAR . SYMBOL)
1221 Where:
1223 CHAR is a char which the user will have to type.
1225 SYMBOL is a valid symbol associated with CHAR.
1226 See `whitespace-style-value-list'.")
1229 (defvar whitespace-active-style nil
1230 "Used to save locally `whitespace-style' value.")
1232 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1233 "Used to save locally `indent-tabs-mode' value.")
1235 (defvar whitespace-tab-width tab-width
1236 "Used to save locally `tab-width' value.")
1238 (defvar whitespace-point (point)
1239 "Used to save locally current point value.
1240 Used by function `whitespace-trailing-regexp' (which see).")
1241 (defvar-local whitespace-point--used nil
1242 "Region whose highlighting depends on `whitespace-point'.")
1244 (defvar whitespace-font-lock-refontify nil
1245 "Used to save locally the font-lock refontify state.
1246 Used by function `whitespace-post-command-hook' (which see).")
1248 (defvar whitespace-bob-marker nil
1249 "Used to save locally the bob marker value.
1250 Used by function `whitespace-post-command-hook' (which see).")
1252 (defvar whitespace-eob-marker nil
1253 "Used to save locally the eob marker value.
1254 Used by function `whitespace-post-command-hook' (which see).")
1256 (defvar whitespace-buffer-changed nil
1257 "Used to indicate locally if buffer changed.
1258 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1259 functions (which see).")
1262 ;;;###autoload
1263 (defun whitespace-toggle-options (arg)
1264 "Toggle local `whitespace-mode' options.
1266 If local whitespace-mode is off, toggle the option given by ARG
1267 and turn on local whitespace-mode.
1269 If local whitespace-mode is on, toggle the option given by ARG
1270 and restart local whitespace-mode.
1272 Interactively, it reads one of the following chars:
1274 CHAR MEANING
1275 (VIA FACES)
1276 f toggle face visualization
1277 t toggle TAB visualization
1278 s toggle SPACE and HARD SPACE visualization
1279 r toggle trailing blanks visualization
1280 l toggle \"long lines\" visualization
1281 L toggle \"long lines\" tail visualization
1282 n toggle NEWLINE visualization
1283 e toggle empty line at bob and/or eob visualization
1284 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1285 I toggle indentation SPACEs visualization
1286 i toggle indentation TABs visualization
1287 C-t toggle big indentation visualization
1288 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1289 A toggle SPACEs after TAB: SPACEs visualization
1290 a toggle SPACEs after TAB: TABs visualization
1291 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1292 B toggle SPACEs before TAB: SPACEs visualization
1293 b toggle SPACEs before TAB: TABs visualization
1295 (VIA DISPLAY TABLE)
1296 T toggle TAB visualization
1297 S toggle SPACEs before TAB visualization
1298 N toggle NEWLINE visualization
1300 x restore `whitespace-style' value
1301 ? display brief help
1303 Non-interactively, ARG should be a symbol or a list of symbols.
1304 The valid symbols are:
1306 face toggle face visualization
1307 tabs toggle TAB visualization
1308 spaces toggle SPACE and HARD SPACE visualization
1309 trailing toggle trailing blanks visualization
1310 lines toggle \"long lines\" visualization
1311 lines-tail toggle \"long lines\" tail visualization
1312 newline toggle NEWLINE visualization
1313 empty toggle empty line at bob and/or eob visualization
1314 indentation toggle indentation SPACEs visualization
1315 indentation::tab toggle indentation SPACEs visualization
1316 indentation::space toggle indentation TABs visualization
1317 big-indent toggle big indentation visualization
1318 space-after-tab toggle SPACEs after TAB visualization
1319 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1320 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1321 space-before-tab toggle SPACEs before TAB visualization
1322 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1323 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1325 tab-mark toggle TAB visualization
1326 space-mark toggle SPACEs before TAB visualization
1327 newline-mark toggle NEWLINE visualization
1329 whitespace-style restore `whitespace-style' value
1331 See `whitespace-style' and `indent-tabs-mode' for documentation."
1332 (interactive (whitespace-interactive-char t))
1333 (let ((whitespace-style
1334 (whitespace-toggle-list t arg whitespace-active-style)))
1335 (whitespace-mode 0)
1336 (whitespace-mode 1)))
1339 (defvar whitespace-toggle-style nil
1340 "Used to toggle the global `whitespace-style' value.")
1343 ;;;###autoload
1344 (defun global-whitespace-toggle-options (arg)
1345 "Toggle global `whitespace-mode' options.
1347 If global whitespace-mode is off, toggle the option given by ARG
1348 and turn on global whitespace-mode.
1350 If global whitespace-mode is on, toggle the option given by ARG
1351 and restart global whitespace-mode.
1353 Interactively, it accepts one of the following chars:
1355 CHAR MEANING
1356 (VIA FACES)
1357 f toggle face visualization
1358 t toggle TAB visualization
1359 s toggle SPACE and HARD SPACE visualization
1360 r toggle trailing blanks visualization
1361 l toggle \"long lines\" visualization
1362 L toggle \"long lines\" tail visualization
1363 n toggle NEWLINE visualization
1364 e toggle empty line at bob and/or eob visualization
1365 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1366 I toggle indentation SPACEs visualization
1367 i toggle indentation TABs visualization
1368 C-t toggle big indentation visualization
1369 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1370 A toggle SPACEs after TAB: SPACEs visualization
1371 a toggle SPACEs after TAB: TABs visualization
1372 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1373 B toggle SPACEs before TAB: SPACEs visualization
1374 b toggle SPACEs before TAB: TABs visualization
1376 (VIA DISPLAY TABLE)
1377 T toggle TAB visualization
1378 S toggle SPACEs before TAB visualization
1379 N toggle NEWLINE visualization
1381 x restore `whitespace-style' value
1382 ? display brief help
1384 Non-interactively, ARG should be a symbol or a list of symbols.
1385 The valid symbols are:
1387 face toggle face visualization
1388 tabs toggle TAB visualization
1389 spaces toggle SPACE and HARD SPACE visualization
1390 trailing toggle trailing blanks visualization
1391 lines toggle \"long lines\" visualization
1392 lines-tail toggle \"long lines\" tail visualization
1393 newline toggle NEWLINE visualization
1394 empty toggle empty line at bob and/or eob visualization
1395 indentation toggle indentation SPACEs visualization
1396 indentation::tab toggle indentation SPACEs visualization
1397 indentation::space toggle indentation TABs visualization
1398 big-indent toggle big indentation visualization
1399 space-after-tab toggle SPACEs after TAB visualization
1400 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1401 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1402 space-before-tab toggle SPACEs before TAB visualization
1403 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1404 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1406 tab-mark toggle TAB visualization
1407 space-mark toggle SPACEs before TAB visualization
1408 newline-mark toggle NEWLINE visualization
1410 whitespace-style restore `whitespace-style' value
1412 See `whitespace-style' and `indent-tabs-mode' for documentation."
1413 (interactive (whitespace-interactive-char nil))
1414 (let ((whitespace-style
1415 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1416 (setq whitespace-toggle-style whitespace-style)
1417 (global-whitespace-mode 0)
1418 (global-whitespace-mode 1)))
1421 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1422 ;;;; User commands - Cleanup
1425 ;;;###autoload
1426 (defun whitespace-cleanup ()
1427 "Cleanup some blank problems in all buffer or at region.
1429 It usually applies to the whole buffer, but in transient mark
1430 mode when the mark is active, it applies to the region. It also
1431 applies to the region when it is not in transient mark mode, the
1432 mark is active and \\[universal-argument] was pressed just before
1433 calling `whitespace-cleanup' interactively.
1435 See also `whitespace-cleanup-region'.
1437 The problems cleaned up are:
1439 1. empty lines at beginning of buffer.
1440 2. empty lines at end of buffer.
1441 If `whitespace-style' includes the value `empty', remove all
1442 empty lines at beginning and/or end of buffer.
1444 3. 8 or more SPACEs at beginning of line.
1445 If `whitespace-style' includes the value `indentation':
1446 replace 8 or more SPACEs at beginning of line by TABs, if
1447 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1448 SPACEs.
1449 If `whitespace-style' includes the value `indentation::tab',
1450 replace 8 or more SPACEs at beginning of line by TABs.
1451 If `whitespace-style' includes the value `indentation::space',
1452 replace TABs by SPACEs.
1454 4. SPACEs before TAB.
1455 If `whitespace-style' includes the value `space-before-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-before-tab::tab', replace SPACEs by TABs.
1460 If `whitespace-style' includes the value
1461 `space-before-tab::space', replace TABs by SPACEs.
1463 5. SPACEs or TABs at end of line.
1464 If `whitespace-style' includes the value `trailing', remove
1465 all SPACEs or TABs at end of line.
1467 6. 8 or more SPACEs after TAB.
1468 If `whitespace-style' includes the value `space-after-tab':
1469 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1470 otherwise, replace TABs by SPACEs.
1471 If `whitespace-style' includes the value
1472 `space-after-tab::tab', replace SPACEs by TABs.
1473 If `whitespace-style' includes the value
1474 `space-after-tab::space', replace TABs by SPACEs.
1476 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1477 documentation."
1478 (interactive "@")
1479 (cond
1480 ;; read-only buffer
1481 (buffer-read-only
1482 (whitespace-warn-read-only "cleanup"))
1483 ;; region active
1484 ((and (or transient-mark-mode
1485 current-prefix-arg)
1486 mark-active)
1487 ;; PROBLEMs 1 and 2 are not handled in region
1488 ;; PROBLEM 3: 8 or more SPACEs at bol
1489 ;; PROBLEM 4: SPACEs before TAB
1490 ;; PROBLEM 5: SPACEs or TABs at eol
1491 ;; PROBLEM 6: 8 or more SPACEs after TAB
1492 (whitespace-cleanup-region (region-beginning) (region-end)))
1493 ;; whole buffer
1495 (save-excursion
1496 (save-match-data ;FIXME: Why?
1497 ;; PROBLEM 1: empty lines at bob
1498 ;; PROBLEM 2: empty lines at eob
1499 ;; ACTION: remove all empty lines at bob and/or eob
1500 (when (memq 'empty whitespace-style)
1501 (let (overwrite-mode) ; enforce no overwrite
1502 (goto-char (point-min))
1503 (when (looking-at whitespace-empty-at-bob-regexp)
1504 (delete-region (match-beginning 1) (match-end 1)))
1505 (when (re-search-forward
1506 (concat whitespace-empty-at-eob-regexp "\\'") nil t)
1507 (delete-region (match-beginning 1) (match-end 1)))))))
1508 ;; PROBLEM 3: 8 or more SPACEs at bol
1509 ;; PROBLEM 4: SPACEs before TAB
1510 ;; PROBLEM 5: SPACEs or TABs at eol
1511 ;; PROBLEM 6: 8 or more SPACEs after TAB
1512 (whitespace-cleanup-region (point-min) (point-max)))))
1514 (defun whitespace-ensure-local-variables ()
1515 "Set `whitespace-indent-tabs-mode' and `whitespace-tab-width' locally."
1516 (set (make-local-variable 'whitespace-indent-tabs-mode)
1517 indent-tabs-mode)
1518 (set (make-local-variable 'whitespace-tab-width)
1519 tab-width))
1521 ;;;###autoload
1522 (defun whitespace-cleanup-region (start end)
1523 "Cleanup some blank problems at region.
1525 The problems cleaned up are:
1527 1. 8 or more SPACEs at beginning of line.
1528 If `whitespace-style' includes the value `indentation':
1529 replace 8 or more SPACEs at beginning of line by TABs, if
1530 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1531 SPACEs.
1532 If `whitespace-style' includes the value `indentation::tab',
1533 replace 8 or more SPACEs at beginning of line by TABs.
1534 If `whitespace-style' includes the value `indentation::space',
1535 replace TABs by SPACEs.
1537 2. SPACEs before TAB.
1538 If `whitespace-style' includes the value `space-before-tab':
1539 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1540 otherwise, replace TABs by SPACEs.
1541 If `whitespace-style' includes the value
1542 `space-before-tab::tab', replace SPACEs by TABs.
1543 If `whitespace-style' includes the value
1544 `space-before-tab::space', replace TABs by SPACEs.
1546 3. SPACEs or TABs at end of line.
1547 If `whitespace-style' includes the value `trailing', remove
1548 all SPACEs or TABs at end of line.
1550 4. 8 or more SPACEs after TAB.
1551 If `whitespace-style' includes the value `space-after-tab':
1552 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1553 otherwise, replace TABs by SPACEs.
1554 If `whitespace-style' includes the value
1555 `space-after-tab::tab', replace SPACEs by TABs.
1556 If `whitespace-style' includes the value
1557 `space-after-tab::space', replace TABs by SPACEs.
1559 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1560 documentation."
1561 (interactive "@r")
1562 (if buffer-read-only
1563 ;; read-only buffer
1564 (whitespace-warn-read-only "cleanup region")
1565 ;; non-read-only buffer
1566 (whitespace-ensure-local-variables)
1567 (let ((rstart (min start end))
1568 (rend (copy-marker (max start end)))
1569 (indent-tabs-mode whitespace-indent-tabs-mode)
1570 (tab-width whitespace-tab-width)
1571 overwrite-mode ; enforce no overwrite
1572 tmp)
1573 (save-excursion
1574 (save-match-data ;FIXME: Why?
1575 ;; PROBLEM 1: 8 or more SPACEs at bol
1576 (cond
1577 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1578 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1579 ;; by SPACEs.
1580 ((memq 'indentation whitespace-style)
1581 (let ((regexp (whitespace-indentation-regexp)))
1582 (goto-char rstart)
1583 (while (re-search-forward regexp rend t)
1584 (setq tmp (current-indentation))
1585 (goto-char (match-beginning 0))
1586 (delete-horizontal-space)
1587 (unless (eolp)
1588 (indent-to tmp)))))
1589 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1590 ((memq 'indentation::tab whitespace-style)
1591 (whitespace-replace-action
1592 'tabify rstart rend
1593 (whitespace-indentation-regexp 'tab) 0))
1594 ;; ACTION: replace TABs by SPACEs.
1595 ((memq 'indentation::space whitespace-style)
1596 (whitespace-replace-action
1597 'untabify rstart rend
1598 (whitespace-indentation-regexp 'space) 0)))
1599 ;; PROBLEM 3: SPACEs or TABs at eol
1600 ;; ACTION: remove all SPACEs or TABs at eol
1601 (when (memq 'trailing whitespace-style)
1602 (whitespace-replace-action
1603 'delete-region rstart rend
1604 whitespace-trailing-regexp 1))
1605 ;; PROBLEM 4: 8 or more SPACEs after TAB
1606 (cond
1607 ;; ACTION: replace 8 or more SPACEs by TABs, if
1608 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1609 ;; by SPACEs.
1610 ((memq 'space-after-tab whitespace-style)
1611 (whitespace-replace-action
1612 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1613 rstart rend (whitespace-space-after-tab-regexp) 1))
1614 ;; ACTION: replace 8 or more SPACEs by TABs.
1615 ((memq 'space-after-tab::tab whitespace-style)
1616 (whitespace-replace-action
1617 'tabify rstart rend
1618 (whitespace-space-after-tab-regexp 'tab) 1))
1619 ;; ACTION: replace TABs by SPACEs.
1620 ((memq 'space-after-tab::space whitespace-style)
1621 (whitespace-replace-action
1622 'untabify rstart rend
1623 (whitespace-space-after-tab-regexp 'space) 1)))
1624 ;; PROBLEM 2: SPACEs before TAB
1625 (cond
1626 ;; ACTION: replace SPACEs before TAB by TABs, if
1627 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1628 ;; by SPACEs.
1629 ((memq 'space-before-tab whitespace-style)
1630 (whitespace-replace-action
1631 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1632 rstart rend whitespace-space-before-tab-regexp
1633 (if whitespace-indent-tabs-mode 0 2)))
1634 ;; ACTION: replace SPACEs before TAB by TABs.
1635 ((memq 'space-before-tab::tab whitespace-style)
1636 (whitespace-replace-action
1637 'tabify rstart rend
1638 whitespace-space-before-tab-regexp 0))
1639 ;; ACTION: replace TABs by SPACEs.
1640 ((memq 'space-before-tab::space whitespace-style)
1641 (whitespace-replace-action
1642 'untabify rstart rend
1643 whitespace-space-before-tab-regexp 2)))))
1644 (set-marker rend nil)))) ; point marker to nowhere
1647 (defun whitespace-replace-action (action rstart rend regexp index)
1648 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1650 INDEX is the level group matched by REGEXP and used by ACTION.
1652 See also `tab-width'."
1653 (goto-char rstart)
1654 (while (re-search-forward regexp rend t)
1655 (goto-char (match-end index))
1656 (funcall action (match-beginning index) (match-end index))))
1659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1660 ;;;; User command - report
1663 (defun whitespace-regexp (regexp &optional kind)
1664 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1665 (cond
1666 ((or (eq kind 'tab)
1667 whitespace-indent-tabs-mode)
1668 (format (car regexp) whitespace-tab-width))
1669 ((or (eq kind 'space)
1670 (not whitespace-indent-tabs-mode))
1671 (cdr regexp))))
1674 (defun whitespace-indentation-regexp (&optional kind)
1675 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1676 (whitespace-regexp whitespace-indentation-regexp kind))
1679 (defun whitespace-space-after-tab-regexp (&optional kind)
1680 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1681 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1684 (defconst whitespace-report-list
1685 (list
1686 (cons 'empty whitespace-empty-at-bob-regexp)
1687 (cons 'empty whitespace-empty-at-eob-regexp)
1688 (cons 'trailing whitespace-trailing-regexp)
1689 (cons 'indentation nil)
1690 (cons 'indentation::tab nil)
1691 (cons 'indentation::space nil)
1692 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1693 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1694 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1695 (cons 'space-after-tab nil)
1696 (cons 'space-after-tab::tab nil)
1697 (cons 'space-after-tab::space nil)
1699 "List of whitespace bogus symbol and corresponding regexp.")
1702 (defconst whitespace-report-text
1703 '( ;; `indent-tabs-mode' has non-nil value
1705 Whitespace Report
1707 Current Setting Whitespace Problem
1709 empty [] [] empty lines at beginning of buffer
1710 empty [] [] empty lines at end of buffer
1711 trailing [] [] SPACEs or TABs at end of line
1712 indentation [] [] 8 or more SPACEs at beginning of line
1713 indentation::tab [] [] 8 or more SPACEs at beginning of line
1714 indentation::space [] [] TABs at beginning of line
1715 space-before-tab [] [] SPACEs before TAB
1716 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1717 space-before-tab::space [] [] SPACEs before TAB: TABs
1718 space-after-tab [] [] 8 or more SPACEs after TAB
1719 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1720 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1722 indent-tabs-mode =
1723 tab-width = \n\n"
1724 . ;; `indent-tabs-mode' has nil value
1726 Whitespace Report
1728 Current Setting Whitespace Problem
1730 empty [] [] empty lines at beginning of buffer
1731 empty [] [] empty lines at end of buffer
1732 trailing [] [] SPACEs or TABs at end of line
1733 indentation [] [] TABs at beginning of line
1734 indentation::tab [] [] 8 or more SPACEs at beginning of line
1735 indentation::space [] [] TABs at beginning of line
1736 space-before-tab [] [] SPACEs before TAB
1737 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1738 space-before-tab::space [] [] SPACEs before TAB: TABs
1739 space-after-tab [] [] 8 or more SPACEs after TAB
1740 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1741 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1743 indent-tabs-mode =
1744 tab-width = \n\n")
1745 "Text for whitespace bogus report.
1747 It is a cons of strings, where the car part is used when
1748 `indent-tabs-mode' is non-nil, and the cdr part is used when
1749 `indent-tabs-mode' is nil.")
1752 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1753 "The buffer name for whitespace bogus report.")
1756 ;;;###autoload
1757 (defun whitespace-report (&optional force report-if-bogus)
1758 "Report some whitespace problems in buffer.
1760 Perform `whitespace-report-region' on the current buffer."
1761 (interactive (list current-prefix-arg))
1762 (whitespace-report-region (point-min) (point-max)
1763 force report-if-bogus))
1766 ;;;###autoload
1767 (defun whitespace-report-region (start end &optional force report-if-bogus)
1768 "Report some whitespace problems in a region.
1770 Return nil if there is no whitespace problem; otherwise, return
1771 non-nil.
1773 If FORCE is non-nil or \\[universal-argument] was pressed just
1774 before calling `whitespace-report-region' interactively, it
1775 forces `whitespace-style' to have:
1777 empty
1778 trailing
1779 indentation
1780 space-before-tab
1781 space-after-tab
1783 If REPORT-IF-BOGUS is t, it reports only when there are any
1784 whitespace problems in buffer; if it is `never', it does not
1785 report problems.
1787 Report if some of the following whitespace problems exist:
1789 * If `indent-tabs-mode' is non-nil:
1790 empty 1. empty lines at beginning of buffer.
1791 empty 2. empty lines at end of buffer.
1792 trailing 3. SPACEs or TABs at end of line.
1793 indentation 4. 8 or more SPACEs at beginning of line.
1794 space-before-tab 5. SPACEs before TAB.
1795 space-after-tab 6. 8 or more SPACEs after TAB.
1797 * If `indent-tabs-mode' is nil:
1798 empty 1. empty lines at beginning of buffer.
1799 empty 2. empty lines at end of buffer.
1800 trailing 3. SPACEs or TABs at end of line.
1801 indentation 4. TABS at beginning of line.
1802 space-before-tab 5. SPACEs before TAB.
1803 space-after-tab 6. 8 or more SPACEs after TAB.
1805 See `whitespace-style' for documentation.
1806 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1807 cleaning up these problems."
1808 (interactive "r")
1809 (setq force (or current-prefix-arg force))
1810 (save-excursion
1811 (save-match-data ;FIXME: Why?
1812 (let* ((has-bogus nil)
1813 (rstart (min start end))
1814 (rend (max start end))
1815 (bogus-list
1816 (mapcar
1817 #'(lambda (option)
1818 (when force
1819 (add-to-list 'whitespace-style (car option)))
1820 (goto-char rstart)
1821 (let ((regexp
1822 (cond
1823 ((eq (car option) 'indentation)
1824 (whitespace-indentation-regexp))
1825 ((eq (car option) 'indentation::tab)
1826 (whitespace-indentation-regexp 'tab))
1827 ((eq (car option) 'indentation::space)
1828 (whitespace-indentation-regexp 'space))
1829 ((eq (car option) 'space-after-tab)
1830 (whitespace-space-after-tab-regexp))
1831 ((eq (car option) 'space-after-tab::tab)
1832 (whitespace-space-after-tab-regexp 'tab))
1833 ((eq (car option) 'space-after-tab::space)
1834 (whitespace-space-after-tab-regexp 'space))
1836 (cdr option)))))
1837 (and (re-search-forward regexp rend t)
1838 (setq has-bogus t))))
1839 whitespace-report-list)))
1840 (when (pcase report-if-bogus (`nil t) (`never nil) (_ has-bogus))
1841 (whitespace-kill-buffer whitespace-report-buffer-name)
1842 ;; `whitespace-indent-tabs-mode' is local to current buffer
1843 ;; `whitespace-tab-width' is local to current buffer
1844 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1845 (ws-tab-width whitespace-tab-width))
1846 (with-current-buffer (get-buffer-create
1847 whitespace-report-buffer-name)
1848 (erase-buffer)
1849 (insert (if ws-indent-tabs-mode
1850 (car whitespace-report-text)
1851 (cdr whitespace-report-text)))
1852 (goto-char (point-min))
1853 (forward-line 3)
1854 (dolist (option whitespace-report-list)
1855 (forward-line 1)
1856 (whitespace-mark-x
1857 27 (memq (car option) whitespace-style))
1858 (whitespace-mark-x 7 (car bogus-list))
1859 (setq bogus-list (cdr bogus-list)))
1860 (forward-line 1)
1861 (whitespace-insert-value ws-indent-tabs-mode)
1862 (whitespace-insert-value ws-tab-width)
1863 (when has-bogus
1864 (goto-char (point-max))
1865 (insert " Type `M-x whitespace-cleanup'"
1866 " to cleanup the buffer.\n\n"
1867 " Type `M-x whitespace-cleanup-region'"
1868 " to cleanup a region.\n\n"))
1869 (whitespace-display-window (current-buffer)))))
1870 has-bogus))))
1873 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1874 ;;;; Internal functions
1877 (defvar whitespace-font-lock-keywords nil
1878 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1881 (defconst whitespace-help-text
1883 Whitespace Toggle Options | scroll up : SPC or > |
1884 | scroll down: M-SPC or < |
1885 FACES \\__________________________/
1886 [] f - toggle face visualization
1887 [] t - toggle TAB visualization
1888 [] s - toggle SPACE and HARD SPACE visualization
1889 [] r - toggle trailing blanks visualization
1890 [] l - toggle \"long lines\" visualization
1891 [] L - toggle \"long lines\" tail visualization
1892 [] n - toggle NEWLINE visualization
1893 [] e - toggle empty line at bob and/or eob visualization
1894 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1895 [] I - toggle indentation SPACEs visualization
1896 [] i - toggle indentation TABs visualization
1897 [] C-t - toggle big indentation visualization
1898 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1899 [] A - toggle SPACEs after TAB: SPACEs visualization
1900 [] a - toggle SPACEs after TAB: TABs visualization
1901 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1902 [] B - toggle SPACEs before TAB: SPACEs visualization
1903 [] b - toggle SPACEs before TAB: TABs visualization
1905 DISPLAY TABLE
1906 [] T - toggle TAB visualization
1907 [] S - toggle SPACE and HARD SPACE visualization
1908 [] N - toggle NEWLINE visualization
1910 x - restore `whitespace-style' value
1912 ? - display this text\n\n"
1913 "Text for whitespace toggle options.")
1916 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1917 "The buffer name for whitespace toggle options.")
1920 (defun whitespace-insert-value (value)
1921 "Insert VALUE at column 20 of next line."
1922 (forward-line 1)
1923 (move-to-column 20 t)
1924 (insert (format "%s" value)))
1927 (defun whitespace-mark-x (nchars condition)
1928 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1929 (forward-char nchars)
1930 (insert (if condition "X" " ")))
1933 (defun whitespace-insert-option-mark (the-list the-value)
1934 "Insert the option mark ('X' or ' ') in toggle options buffer."
1935 (goto-char (point-min))
1936 (forward-line 2)
1937 (dolist (sym the-list)
1938 (if (eq sym 'help-newline)
1939 (forward-line 2)
1940 (forward-line 1)
1941 (whitespace-mark-x 2 (memq sym the-value)))))
1944 (defun whitespace-help-on (style)
1945 "Display the whitespace toggle options."
1946 (unless (get-buffer whitespace-help-buffer-name)
1947 (delete-other-windows)
1948 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
1949 (with-current-buffer buffer
1950 (erase-buffer)
1951 (insert whitespace-help-text)
1952 (whitespace-insert-option-mark
1953 whitespace-style-value-list style)
1954 (whitespace-display-window buffer)))))
1957 (defun whitespace-display-window (buffer)
1958 "Display BUFFER in a new window."
1959 (goto-char (point-min))
1960 (set-buffer-modified-p nil)
1961 (when (< (window-height) (* 2 window-min-height))
1962 (kill-buffer buffer)
1963 (error "Window height is too small; \
1964 can't split window to display whitespace toggle options"))
1965 (let ((win (split-window)))
1966 (set-window-buffer win buffer)
1967 (shrink-window-if-larger-than-buffer win)))
1970 (defun whitespace-kill-buffer (buffer-name)
1971 "Kill buffer BUFFER-NAME and windows related with it."
1972 (let ((buffer (get-buffer buffer-name)))
1973 (when buffer
1974 (delete-windows-on buffer)
1975 (kill-buffer buffer))))
1978 (defun whitespace-help-off ()
1979 "Remove the buffer and window of the whitespace toggle options."
1980 (whitespace-kill-buffer whitespace-help-buffer-name))
1983 (defun whitespace-help-scroll (&optional up)
1984 "Scroll help window, if it exists.
1986 If UP is non-nil, scroll up; otherwise, scroll down."
1987 (condition-case nil
1988 (let ((buffer (get-buffer whitespace-help-buffer-name)))
1989 (if buffer
1990 (with-selected-window (get-buffer-window buffer)
1991 (if up
1992 (scroll-up 3)
1993 (scroll-down 3)))
1994 (ding)))
1995 ;; handler
1996 ((error)
1997 ;; just ignore error
2001 (defun whitespace-interactive-char (local-p)
2002 "Interactive function to read a char and return a symbol.
2004 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2005 uses a global context.
2007 It accepts one of the following chars:
2009 CHAR MEANING
2010 (VIA FACES)
2011 f toggle face visualization
2012 t toggle TAB visualization
2013 s toggle SPACE and HARD SPACE visualization
2014 r toggle trailing blanks visualization
2015 l toggle \"long lines\" visualization
2016 L toggle \"long lines\" tail visualization
2017 n toggle NEWLINE visualization
2018 e toggle empty line at bob and/or eob visualization
2019 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2020 I toggle indentation SPACEs visualization
2021 i toggle indentation TABs visualization
2022 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2023 A toggle SPACEs after TAB: SPACEs visualization
2024 a toggle SPACEs after TAB: TABs visualization
2025 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2026 B toggle SPACEs before TAB: SPACEs visualization
2027 b toggle SPACEs before TAB: TABs visualization
2029 (VIA DISPLAY TABLE)
2030 T toggle TAB visualization
2031 S toggle SPACE and HARD SPACE visualization
2032 N toggle NEWLINE visualization
2034 x restore `whitespace-style' value
2035 ? display brief help
2037 See also `whitespace-toggle-option-alist'."
2038 (let* ((is-off (not (if local-p
2039 whitespace-mode
2040 global-whitespace-mode)))
2041 (style (cond (is-off whitespace-style) ; use default value
2042 (local-p whitespace-active-style)
2043 (t whitespace-toggle-style)))
2044 (prompt
2045 (format "Whitespace Toggle %s (type ? for further options)-"
2046 (if local-p "Local" "Global")))
2047 ch sym)
2048 ;; read a valid option and get the corresponding symbol
2049 (save-window-excursion
2050 (condition-case data
2051 (progn
2052 (while
2053 ;; while condition
2054 (progn
2055 (setq ch (read-char prompt))
2056 (not
2057 (setq sym
2058 (cdr
2059 (assq ch whitespace-toggle-option-alist)))))
2060 ;; while body
2061 (cond
2062 ((eq ch ?\?) (whitespace-help-on style))
2063 ((eq ch ?\ ) (whitespace-help-scroll t))
2064 ((eq ch ?\M- ) (whitespace-help-scroll))
2065 ((eq ch ?>) (whitespace-help-scroll t))
2066 ((eq ch ?<) (whitespace-help-scroll))
2067 (t (ding))))
2068 (whitespace-help-off)
2069 (message " ")) ; clean echo area
2070 ;; handler
2071 ((quit error)
2072 (whitespace-help-off)
2073 (error (error-message-string data)))))
2074 (list sym))) ; return the appropriate symbol
2077 (defun whitespace-toggle-list (local-p arg the-list)
2078 "Toggle options in THE-LIST based on list ARG.
2080 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2081 uses a global context.
2083 ARG is a list of options to be toggled.
2085 THE-LIST is a list of options. This list will be toggled and the
2086 resultant list will be returned."
2087 (unless (if local-p whitespace-mode global-whitespace-mode)
2088 (setq the-list whitespace-style))
2089 (setq the-list (copy-sequence the-list)) ; keep original list
2090 (dolist (sym (if (listp arg) arg (list arg)))
2091 (cond
2092 ;; ignore help value
2093 ((eq sym 'help-newline))
2094 ;; restore default values
2095 ((eq sym 'whitespace-style)
2096 (setq the-list whitespace-style))
2097 ;; toggle valid values
2098 ((memq sym whitespace-style-value-list)
2099 (setq the-list (if (memq sym the-list)
2100 (delq sym the-list)
2101 (cons sym the-list))))))
2102 the-list)
2105 (defvar whitespace-display-table nil
2106 "Used to save a local display table.")
2108 (defvar whitespace-display-table-was-local nil
2109 "Used to remember whether a buffer initially had a local display table.")
2111 (defun whitespace-turn-on ()
2112 "Turn on whitespace visualization."
2113 ;; prepare local hooks
2114 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2115 ;; create whitespace local buffer environment
2116 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2117 (set (make-local-variable 'whitespace-display-table) nil)
2118 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2119 (set (make-local-variable 'whitespace-active-style)
2120 (if (listp whitespace-style)
2121 whitespace-style
2122 (list whitespace-style)))
2123 (whitespace-ensure-local-variables)
2124 ;; turn on whitespace
2125 (when whitespace-active-style
2126 (whitespace-color-on)
2127 (whitespace-display-char-on)))
2130 (defun whitespace-turn-off ()
2131 "Turn off whitespace visualization."
2132 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2133 (when whitespace-active-style
2134 (whitespace-color-off)
2135 (whitespace-display-char-off)))
2138 (defun whitespace-style-face-p ()
2139 "Return t if there is some visualization via face."
2140 (and (memq 'face whitespace-active-style)
2141 (or (memq 'tabs whitespace-active-style)
2142 (memq 'spaces whitespace-active-style)
2143 (memq 'trailing whitespace-active-style)
2144 (memq 'lines whitespace-active-style)
2145 (memq 'lines-tail whitespace-active-style)
2146 (memq 'newline whitespace-active-style)
2147 (memq 'empty whitespace-active-style)
2148 (memq 'indentation whitespace-active-style)
2149 (memq 'indentation::tab whitespace-active-style)
2150 (memq 'indentation::space whitespace-active-style)
2151 (memq 'big-indent whitespace-active-style)
2152 (memq 'space-after-tab whitespace-active-style)
2153 (memq 'space-after-tab::tab whitespace-active-style)
2154 (memq 'space-after-tab::space whitespace-active-style)
2155 (memq 'space-before-tab whitespace-active-style)
2156 (memq 'space-before-tab::tab whitespace-active-style)
2157 (memq 'space-before-tab::space whitespace-active-style))))
2160 (defun whitespace-color-on ()
2161 "Turn on color visualization."
2162 (when (whitespace-style-face-p)
2163 ;; save current point and refontify when necessary
2164 (set (make-local-variable 'whitespace-point)
2165 (point))
2166 (setq whitespace-point--used
2167 (let ((ol (make-overlay (point) (point) nil nil t)))
2168 (delete-overlay ol) ol))
2169 (set (make-local-variable 'whitespace-font-lock-refontify)
2171 (set (make-local-variable 'whitespace-bob-marker)
2172 (point-min-marker))
2173 (set (make-local-variable 'whitespace-eob-marker)
2174 (point-max-marker))
2175 (set (make-local-variable 'whitespace-buffer-changed)
2176 nil)
2177 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
2178 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
2179 ;; Add whitespace-mode color into font lock.
2180 (setq
2181 whitespace-font-lock-keywords
2183 (whitespace-point--flush-used)
2184 ,@(when (memq 'spaces whitespace-active-style)
2185 ;; Show SPACEs.
2186 `((,whitespace-space-regexp 1 whitespace-space t)
2187 ;; Show HARD SPACEs.
2188 (,whitespace-hspace-regexp 1 whitespace-hspace t)))
2189 ,@(when (memq 'tabs whitespace-active-style)
2190 ;; Show TABs.
2191 `((,whitespace-tab-regexp 1 whitespace-tab t)))
2192 ,@(when (memq 'trailing whitespace-active-style)
2193 ;; Show trailing blanks.
2194 `((,#'whitespace-trailing-regexp 1 whitespace-trailing t)))
2195 ,@(when (or (memq 'lines whitespace-active-style)
2196 (memq 'lines-tail whitespace-active-style))
2197 ;; Show "long" lines.
2198 `((,(let ((line-column (or whitespace-line-column fill-column)))
2199 (format
2200 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2201 whitespace-tab-width
2202 (1- whitespace-tab-width)
2203 (/ line-column whitespace-tab-width)
2204 (let ((rem (% line-column whitespace-tab-width)))
2205 (if (zerop rem)
2207 (format ".\\{%d\\}" rem)))))
2208 ,(if (memq 'lines whitespace-active-style)
2209 0 ; whole line
2210 2) ; line tail
2211 whitespace-line prepend)))
2212 ,@(when (or (memq 'space-before-tab whitespace-active-style)
2213 (memq 'space-before-tab::tab whitespace-active-style)
2214 (memq 'space-before-tab::space whitespace-active-style))
2215 `((,whitespace-space-before-tab-regexp
2216 ,(cond
2217 ((memq 'space-before-tab whitespace-active-style)
2218 ;; Show SPACEs before TAB (indent-tabs-mode).
2219 (if whitespace-indent-tabs-mode 1 2))
2220 ((memq 'space-before-tab::tab whitespace-active-style)
2222 ((memq 'space-before-tab::space whitespace-active-style)
2224 whitespace-space-before-tab t)))
2225 ,@(when (or (memq 'indentation whitespace-active-style)
2226 (memq 'indentation::tab whitespace-active-style)
2227 (memq 'indentation::space whitespace-active-style))
2228 `((,(cond
2229 ((memq 'indentation whitespace-active-style)
2230 ;; Show indentation SPACEs (indent-tabs-mode).
2231 (whitespace-indentation-regexp))
2232 ((memq 'indentation::tab whitespace-active-style)
2233 ;; Show indentation SPACEs (SPACEs).
2234 (whitespace-indentation-regexp 'tab))
2235 ((memq 'indentation::space whitespace-active-style)
2236 ;; Show indentation SPACEs (TABs).
2237 (whitespace-indentation-regexp 'space)))
2238 1 whitespace-indentation t)))
2239 ,@(when (memq 'big-indent whitespace-active-style)
2240 ;; Show big indentation.
2241 `((,whitespace-big-indent-regexp 1 'whitespace-big-indent t)))
2242 ,@(when (memq 'empty whitespace-active-style)
2243 ;; Show empty lines at beginning of buffer.
2244 `((,#'whitespace-empty-at-bob-regexp
2245 1 whitespace-empty t)
2246 ;; Show empty lines at end of buffer.
2247 (,#'whitespace-empty-at-eob-regexp
2248 1 whitespace-empty t)))
2249 ,@(when (or (memq 'space-after-tab whitespace-active-style)
2250 (memq 'space-after-tab::tab whitespace-active-style)
2251 (memq 'space-after-tab::space whitespace-active-style))
2252 `((,(cond
2253 ((memq 'space-after-tab whitespace-active-style)
2254 ;; Show SPACEs after TAB (indent-tabs-mode).
2255 (whitespace-space-after-tab-regexp))
2256 ((memq 'space-after-tab::tab whitespace-active-style)
2257 ;; Show SPACEs after TAB (SPACEs).
2258 (whitespace-space-after-tab-regexp 'tab))
2259 ((memq 'space-after-tab::space whitespace-active-style)
2260 ;; Show SPACEs after TAB (TABs).
2261 (whitespace-space-after-tab-regexp 'space)))
2262 1 whitespace-space-after-tab t)))))
2263 (font-lock-add-keywords nil whitespace-font-lock-keywords t)
2264 (font-lock-flush)))
2267 (defun whitespace-color-off ()
2268 "Turn off color visualization."
2269 ;; turn off font lock
2270 (kill-local-variable 'whitespace-point--used)
2271 (when (whitespace-style-face-p)
2272 (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
2273 (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
2274 (font-lock-remove-keywords nil whitespace-font-lock-keywords)
2275 (font-lock-flush)))
2277 (defun whitespace-point--used (start end)
2278 (let ((ostart (overlay-start whitespace-point--used)))
2279 (if ostart
2280 (move-overlay whitespace-point--used
2281 (min start ostart)
2282 (max end (overlay-end whitespace-point--used)))
2283 (move-overlay whitespace-point--used start end))))
2285 (defun whitespace-point--flush-used (limit)
2286 (let ((ostart (overlay-start whitespace-point--used)))
2287 ;; Strip parts of whitespace-point--used we're about to refresh.
2288 (when ostart
2289 (let ((oend (overlay-end whitespace-point--used)))
2290 (if (<= (point) ostart)
2291 (if (<= oend limit)
2292 (delete-overlay whitespace-point--used)
2293 (move-overlay whitespace-point--used limit oend)))
2294 (if (<= oend limit)
2295 (move-overlay whitespace-point--used ostart (point))))))
2296 nil)
2298 (defun whitespace-trailing-regexp (limit)
2299 "Match trailing spaces which do not contain the point at end of line."
2300 (let ((status t))
2301 (while (if (re-search-forward whitespace-trailing-regexp limit t)
2302 (when (= whitespace-point (match-end 1)) ; Loop if point at eol.
2303 (whitespace-point--used (match-beginning 0) (match-end 0))
2305 (setq status nil))) ;; end of buffer
2306 status))
2309 (defun whitespace-empty-at-bob-regexp (limit)
2310 "Match spaces at beginning of buffer which do not contain the point at \
2311 beginning of buffer."
2312 (let ((b (point))
2314 (cond
2315 ;; at bob
2316 ((= b 1)
2317 (setq r (and (looking-at whitespace-empty-at-bob-regexp)
2318 (or (/= whitespace-point 1)
2319 (progn (whitespace-point--used (match-beginning 0)
2320 (match-end 0))
2321 nil))))
2322 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2323 ;; inside bob empty region
2324 ((<= limit whitespace-bob-marker)
2325 (setq r (looking-at whitespace-empty-at-bob-regexp))
2326 (if r
2327 (when (< (match-end 1) limit)
2328 (set-marker whitespace-bob-marker (match-end 1)))
2329 (set-marker whitespace-bob-marker b)))
2330 ;; intersection with end of bob empty region
2331 ((<= b whitespace-bob-marker)
2332 (setq r (looking-at whitespace-empty-at-bob-regexp))
2333 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2334 ;; it is not inside bob empty region
2336 (setq r nil)))
2337 ;; move to end of matching
2338 (and r (goto-char (match-end 1)))
2342 (defsubst whitespace-looking-back (regexp limit)
2343 (save-excursion
2344 (when (/= 0 (skip-chars-backward " \t\n" limit))
2345 (unless (bolp)
2346 (forward-line 1))
2347 (looking-at regexp))))
2350 (defun whitespace-empty-at-eob-regexp (limit)
2351 "Match spaces at end of buffer which do not contain the point at end of \
2352 buffer."
2353 (let ((b (point))
2354 (e (1+ (buffer-size)))
2356 (cond
2357 ;; at eob
2358 ((= limit e)
2359 (goto-char limit)
2360 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2361 (when (and r (= whitespace-point e))
2362 (setq r nil)
2363 (whitespace-point--used (match-beginning 0) (match-end 0)))
2364 (if r
2365 (set-marker whitespace-eob-marker (match-beginning 1))
2366 (set-marker whitespace-eob-marker limit)
2367 (goto-char b))) ; return back to initial position
2368 ;; inside eob empty region
2369 ((>= b whitespace-eob-marker)
2370 (goto-char limit)
2371 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2372 (if r
2373 (when (> (match-beginning 1) b)
2374 (set-marker whitespace-eob-marker (match-beginning 1)))
2375 (set-marker whitespace-eob-marker limit)
2376 (goto-char b))) ; return back to initial position
2377 ;; intersection with beginning of eob empty region
2378 ((>= limit whitespace-eob-marker)
2379 (goto-char limit)
2380 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2381 (if r
2382 (set-marker whitespace-eob-marker (match-beginning 1))
2383 (set-marker whitespace-eob-marker limit)
2384 (goto-char b))) ; return back to initial position
2385 ;; it is not inside eob empty region
2387 (setq r nil)))
2391 (defun whitespace-buffer-changed (_beg _end)
2392 "Set `whitespace-buffer-changed' variable to t."
2393 (setq whitespace-buffer-changed t))
2396 (defun whitespace-post-command-hook ()
2397 "Save current point into `whitespace-point' variable.
2398 Also refontify when necessary."
2399 (unless (and (eq whitespace-point (point))
2400 (not whitespace-buffer-changed))
2401 (setq whitespace-point (point)) ; current point position
2402 (let ((refontify
2403 (cond
2404 ;; It is at end of buffer (eob).
2405 ((= whitespace-point (1+ (buffer-size)))
2406 (when (whitespace-looking-back whitespace-empty-at-eob-regexp
2407 nil)
2408 (match-beginning 0)))
2409 ;; It is at end of line ...
2410 ((and (eolp)
2411 ;; ... with trailing SPACE or TAB
2412 (or (memq (preceding-char) '(?\s ?\t))))
2413 (line-beginning-position))
2414 ;; It is at beginning of buffer (bob).
2415 ((and (= whitespace-point 1)
2416 (looking-at whitespace-empty-at-bob-regexp))
2417 (match-end 0))))
2418 (ostart (overlay-start whitespace-point--used)))
2419 (cond
2420 ((not refontify)
2421 ;; New point does not affect highlighting: just refresh the
2422 ;; highlighting of old point, if needed.
2423 (when ostart
2424 (font-lock-flush ostart
2425 (overlay-end whitespace-point--used))
2426 (delete-overlay whitespace-point--used)))
2427 ((not ostart)
2428 ;; Old point did not affect highlighting, but new one does: refresh the
2429 ;; highlighting of new point.
2430 (font-lock-flush (min refontify (point)) (max refontify (point))))
2431 ((save-excursion
2432 (goto-char ostart)
2433 (setq ostart (line-beginning-position))
2434 (and (<= ostart (max refontify (point)))
2435 (progn
2436 (goto-char (overlay-end whitespace-point--used))
2437 (let ((oend (line-beginning-position 2)))
2438 (<= (min refontify (point)) oend)))))
2439 ;; The old point highlighting and the new point highlighting
2440 ;; cover a contiguous region: do a single refresh.
2441 (font-lock-flush (min refontify (point) ostart)
2442 (max refontify (point)
2443 (overlay-end whitespace-point--used)))
2444 (delete-overlay whitespace-point--used))
2446 (font-lock-flush (min refontify (point))
2447 (max refontify (point)))
2448 (font-lock-flush ostart (overlay-end whitespace-point--used))
2449 (delete-overlay whitespace-point--used))))))
2452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2453 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2456 (defun whitespace-style-mark-p ()
2457 "Return t if there is some visualization via display table."
2458 (or (memq 'tab-mark whitespace-active-style)
2459 (memq 'space-mark whitespace-active-style)
2460 (memq 'newline-mark whitespace-active-style)))
2463 (defsubst whitespace-char-valid-p (char)
2464 ;; This check should be improved!!!
2465 (or (< char 256)
2466 (characterp char)))
2469 (defun whitespace-display-vector-p (vec)
2470 "Return true if every character in vector VEC can be displayed."
2471 (let ((i (length vec)))
2472 (when (> i 0)
2473 (while (and (>= (setq i (1- i)) 0)
2474 (whitespace-char-valid-p (aref vec i))))
2475 (< i 0))))
2478 (defun whitespace-display-char-on ()
2479 "Turn on character display mapping."
2480 (when (and whitespace-display-mappings
2481 (whitespace-style-mark-p))
2482 (let (vecs vec)
2483 ;; Remember whether a buffer has a local display table.
2484 (unless whitespace-display-table-was-local
2485 (setq whitespace-display-table-was-local t
2486 whitespace-display-table
2487 (copy-sequence buffer-display-table))
2488 ;; Assure `buffer-display-table' is unique
2489 ;; when two or more windows are visible.
2490 (setq buffer-display-table
2491 (copy-sequence buffer-display-table)))
2492 (unless buffer-display-table
2493 (setq buffer-display-table (make-display-table)))
2494 (dolist (entry whitespace-display-mappings)
2495 ;; check if it is to display this mark
2496 (when (memq (car entry) whitespace-style)
2497 ;; Get a displayable mapping.
2498 (setq vecs (cddr entry))
2499 (while (and vecs
2500 (not (whitespace-display-vector-p (car vecs))))
2501 (setq vecs (cdr vecs)))
2502 ;; Display a valid mapping.
2503 (when vecs
2504 (setq vec (copy-sequence (car vecs)))
2505 ;; NEWLINE char
2506 (when (and (eq (cadr entry) ?\n)
2507 (memq 'newline whitespace-active-style))
2508 ;; Only insert face bits on NEWLINE char mapping to avoid
2509 ;; obstruction of other faces like TABs and (HARD) SPACEs
2510 ;; faces, font-lock faces, etc.
2511 (dotimes (i (length vec))
2512 (or (eq (aref vec i) ?\n)
2513 (aset vec i
2514 (make-glyph-code (aref vec i)
2515 whitespace-newline)))))
2516 ;; Display mapping
2517 (aset buffer-display-table (cadr entry) vec)))))))
2520 (defun whitespace-display-char-off ()
2521 "Turn off character display mapping."
2522 (and whitespace-display-mappings
2523 (whitespace-style-mark-p)
2524 whitespace-display-table-was-local
2525 (setq whitespace-display-table-was-local nil
2526 buffer-display-table whitespace-display-table)))
2529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2530 ;;;; Hook
2533 (defun whitespace-action-when-on ()
2534 "Action to be taken always when local whitespace is turned on."
2535 (cond ((memq 'cleanup whitespace-action)
2536 (whitespace-cleanup))
2537 ((memq 'report-on-bogus whitespace-action)
2538 (whitespace-report nil t))))
2541 (defun whitespace-write-file-hook ()
2542 "Action to be taken when buffer is written.
2543 It should be added buffer-locally to `write-file-functions'."
2544 (cond ((memq 'auto-cleanup whitespace-action)
2545 (whitespace-cleanup))
2546 ((memq 'abort-on-bogus whitespace-action)
2547 (when (whitespace-report nil t)
2548 (error "Abort write due to whitespace problems in %s"
2549 (buffer-name)))))
2550 nil) ; continue hook processing
2553 (defun whitespace-warn-read-only (msg)
2554 "Warn if buffer is read-only."
2555 (when (memq 'warn-if-read-only whitespace-action)
2556 (message "Can't %s: %s is read-only" msg (buffer-name))))
2559 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2562 (defun whitespace-unload-function ()
2563 "Unload the whitespace library."
2564 (global-whitespace-mode -1)
2565 ;; be sure all local whitespace mode is turned off
2566 (save-current-buffer
2567 (dolist (buf (buffer-list))
2568 (set-buffer buf)
2569 (whitespace-mode -1)))
2570 nil) ; continue standard unloading
2573 (provide 'whitespace)
2576 (run-hooks 'whitespace-load-hook)
2579 ;;; whitespace.el ends here