Fix EOL mishap.
[emacs.git] / lisp / whitespace.el
blob41f0af600dd14903da39512756d62d1199d65aea
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
8 ;; Keywords: data, wp
9 ;; Version: 11.2
10 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; Introduction
32 ;; ------------
34 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
35 ;; and NEWLINE).
37 ;; whitespace uses two ways to visualize blanks: faces and display
38 ;; table.
40 ;; * Faces are used to highlight the background with a color.
41 ;; whitespace uses font-lock to highlight blank characters.
43 ;; * Display table changes the way a character is displayed, that is,
44 ;; it provides a visual mark for characters, for example, at the end
45 ;; of line (?\xB6), at SPACEs (?\xB7) and at TABs (?\xBB).
47 ;; The `whitespace-style' variable selects which way blanks are
48 ;; visualized.
50 ;; Note that when whitespace is turned on, whitespace saves the
51 ;; font-lock state, that is, if font-lock is on or off. And
52 ;; whitespace restores the font-lock state when it is turned off. So,
53 ;; if whitespace is turned on and font-lock is off, whitespace also
54 ;; turns on the font-lock to highlight blanks, but the font-lock will
55 ;; be turned off when whitespace is turned off. Thus, turn on
56 ;; font-lock before whitespace is on, if you want that font-lock
57 ;; continues on after whitespace is turned off.
59 ;; When whitespace is on, it takes care of highlighting some special
60 ;; characters over the default mechanism of `nobreak-char-display'
61 ;; (which see) and `show-trailing-whitespace' (which see).
63 ;; There are two ways of using whitespace: local and global.
65 ;; * Local whitespace affects only the current buffer.
67 ;; * Global whitespace affects all current and future buffers. That
68 ;; is, if you turn on global whitespace and then create a new
69 ;; buffer, the new buffer will also have whitespace on. The
70 ;; `whitespace-global-modes' variable controls which major-mode will
71 ;; be automagically turned on.
73 ;; You can mix the local and global usage without any conflict. But
74 ;; local whitespace has priority over global whitespace. Whitespace
75 ;; mode is active in a buffer if you have enabled it in that buffer or
76 ;; if you have enabled it globally.
78 ;; When global and local whitespace are on:
80 ;; * if local whitespace is turned off, whitespace is turned off for
81 ;; the current buffer only.
83 ;; * if global whitespace is turned off, whitespace continues on only
84 ;; in the buffers in which local whitespace is on.
86 ;; To use whitespace, insert in your ~/.emacs:
88 ;; (require 'whitespace-mode)
90 ;; Or autoload at least one of the commands`whitespace-mode',
91 ;; `whitespace-toggle-options', `global-whitespace-mode' or
92 ;; `global-whitespace-toggle-options'. For example:
94 ;; (autoload 'whitespace-mode "whitespace"
95 ;; "Toggle whitespace visualization." t)
96 ;; (autoload 'whitespace-toggle-options "whitespace"
97 ;; "Toggle local `whitespace-mode' options." t)
99 ;; whitespace was inspired by:
101 ;; whitespace.el Rajesh Vaidheeswarran <rv@gnu.org>
102 ;; Warn about and clean bogus whitespaces in the file
103 ;; (inspired the idea to warn and clean some blanks)
104 ;; This was the original `whitespace.el' which was replaced by
105 ;; `blank-mode.el'. And later `blank-mode.el' was renamed to
106 ;; `whitespace.el'.
108 ;; show-whitespace-mode.el Aurelien Tisne <aurelien.tisne@free.fr>
109 ;; Simple mode to highlight whitespaces
110 ;; (inspired the idea to use font-lock)
112 ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li>
113 ;; Major mode for editing Whitespace
114 ;; (inspired the idea to use display table)
116 ;; visws.el Miles Bader <miles@gnu.org>
117 ;; Make whitespace visible
118 ;; (handle display table, his code was modified, but the main
119 ;; idea was kept)
122 ;; Using whitespace
123 ;; ----------------
125 ;; There is no problem if you mix local and global minor mode usage.
127 ;; * LOCAL whitespace:
128 ;; + To toggle whitespace options locally, type:
130 ;; M-x whitespace-toggle-options RET
132 ;; + To activate whitespace locally, type:
134 ;; C-u 1 M-x whitespace-mode RET
136 ;; + To deactivate whitespace locally, type:
138 ;; C-u 0 M-x whitespace-mode RET
140 ;; + To toggle whitespace locally, type:
142 ;; M-x whitespace-mode RET
144 ;; * GLOBAL whitespace:
145 ;; + To toggle whitespace options globally, type:
147 ;; M-x global-whitespace-toggle-options RET
149 ;; + To activate whitespace globally, type:
151 ;; C-u 1 M-x global-whitespace-mode RET
153 ;; + To deactivate whitespace globally, type:
155 ;; C-u 0 M-x global-whitespace-mode RET
157 ;; + To toggle whitespace globally, type:
159 ;; M-x global-whitespace-mode RET
161 ;; There are also the following useful commands:
163 ;; `whitespace-newline-mode'
164 ;; Toggle NEWLINE minor mode visualization ("nl" on modeline).
166 ;; `global-whitespace-newline-mode'
167 ;; Toggle NEWLINE global minor mode visualization ("NL" on modeline).
169 ;; `whitespace-report'
170 ;; Report some blank problems in buffer.
172 ;; `whitespace-report-region'
173 ;; Report some blank problems in a region.
175 ;; `whitespace-cleanup'
176 ;; Cleanup some blank problems in all buffer or at region.
178 ;; `whitespace-cleanup-region'
179 ;; Cleanup some blank problems at region.
181 ;; The problems, which are cleaned up, are:
183 ;; 1. empty lines at beginning of buffer.
184 ;; 2. empty lines at end of buffer.
185 ;; If `whitespace-style' includes the value `empty', remove all
186 ;; empty lines at beginning and/or end of buffer.
188 ;; 3. 8 or more SPACEs at beginning of line.
189 ;; If `whitespace-style' includes the value `indentation':
190 ;; replace 8 or more SPACEs at beginning of line by TABs, if
191 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
192 ;; SPACEs.
193 ;; If `whitespace-style' includes the value `indentation::tab',
194 ;; replace 8 or more SPACEs at beginning of line by TABs.
195 ;; If `whitespace-style' includes the value `indentation::space',
196 ;; replace TABs by SPACEs.
198 ;; 4. SPACEs before TAB.
199 ;; If `whitespace-style' includes the value `space-before-tab':
200 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
201 ;; otherwise, replace TABs by SPACEs.
202 ;; If `whitespace-style' includes the value
203 ;; `space-before-tab::tab', replace SPACEs by TABs.
204 ;; If `whitespace-style' includes the value
205 ;; `space-before-tab::space', replace TABs by SPACEs.
207 ;; 5. SPACEs or TABs at end of line.
208 ;; If `whitespace-style' includes the value `trailing', remove all
209 ;; SPACEs or TABs at end of line.
211 ;; 6. 8 or more SPACEs after TAB.
212 ;; If `whitespace-style' includes the value `space-after-tab':
213 ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
214 ;; otherwise, replace TABs by SPACEs.
215 ;; If `whitespace-style' includes the value `space-after-tab::tab',
216 ;; replace SPACEs by TABs.
217 ;; If `whitespace-style' includes the value
218 ;; `space-after-tab::space', replace TABs by SPACEs.
221 ;; Hooks
222 ;; -----
224 ;; whitespace has the following hook variables:
226 ;; `whitespace-mode-hook'
227 ;; It is evaluated always when whitespace is turned on locally.
229 ;; `global-whitespace-mode-hook'
230 ;; It is evaluated always when whitespace is turned on globally.
232 ;; `whitespace-load-hook'
233 ;; It is evaluated after whitespace package is loaded.
236 ;; Options
237 ;; -------
239 ;; Below it's shown a brief description of whitespace options, please,
240 ;; see the options declaration in the code for a long documentation.
242 ;; `whitespace-style' Specify which kind of blank is
243 ;; visualized.
245 ;; `whitespace-space' Face used to visualize SPACE.
247 ;; `whitespace-hspace' Face used to visualize HARD SPACE.
249 ;; `whitespace-tab' Face used to visualize TAB.
251 ;; `whitespace-newline' Face used to visualize NEWLINE char
252 ;; mapping.
254 ;; `whitespace-trailing' Face used to visualize trailing
255 ;; blanks.
257 ;; `whitespace-line' Face used to visualize "long" lines.
259 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
260 ;; before TAB.
262 ;; `whitespace-indentation' Face used to visualize 8 or more
263 ;; SPACEs at beginning of line.
265 ;; `whitespace-empty' Face used to visualize empty lines at
266 ;; beginning and/or end of buffer.
268 ;; `whitespace-space-after-tab' Face used to visualize 8 or more
269 ;; SPACEs after TAB.
271 ;; `whitespace-space-regexp' Specify SPACE characters regexp.
273 ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
275 ;; `whitespace-tab-regexp' Specify TAB characters regexp.
277 ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
279 ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
280 ;; regexp.
282 ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
283 ;; SPACEs at beginning of line.
285 ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
286 ;; at beginning of buffer.
288 ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
289 ;; at end of buffer.
291 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
292 ;; SPACEs after TAB.
294 ;; `whitespace-line-column' Specify column beyond which the line
295 ;; is highlighted.
297 ;; `whitespace-display-mappings' Specify an alist of mappings
298 ;; for displaying characters.
300 ;; `whitespace-global-modes' Modes for which global
301 ;; `whitespace-mode' is automagically
302 ;; turned on.
304 ;; `whitespace-action' Specify which action is taken when a
305 ;; buffer is visited, killed or written.
308 ;; Acknowledgements
309 ;; ----------------
311 ;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a
312 ;; `whitespace-newline' initialization with low contrast relative to
313 ;; the background color.
315 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
316 ;; `indent-tabs-mode' usage suggestion.
318 ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook
319 ;; actions when buffer is written or killed as the original whitespace
320 ;; package had.
322 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
323 ;; lines tail. See EightyColumnRule (EmacsWiki).
325 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
326 ;; * `define-minor-mode'.
327 ;; * `global-whitespace-*' name for global commands.
329 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
331 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
332 ;; suggestion.
334 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
335 ;; helping to fix `find-file-hooks' reference.
337 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
338 ;; indicating defface byte-compilation warnings.
340 ;; Thanks to TimOCallaghan (EmacsWiki) for the idea about highlight
341 ;; "long" lines. See EightyColumnRule (EmacsWiki).
343 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
344 ;; NEWLINE character mapping.
346 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
347 ;; whitespace-mode.el on XEmacs.
349 ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via
350 ;; visws.el (his code was modified, but the main idea was kept).
352 ;; Thanks to:
353 ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el
354 ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el
355 ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el
356 ;; Miles Bader <miles@gnu.org> visws.el
357 ;; And to all people who contributed with them.
360 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
362 ;;; code:
365 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
366 ;;;; User Variables:
369 ;;; Interface to the command system
372 (defgroup whitespace nil
373 "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
374 :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
375 :version "23.1"
376 :group 'wp
377 :group 'data)
380 (defcustom whitespace-style
381 '(tabs spaces trailing lines space-before-tab newline
382 indentation empty space-after-tab
383 space-mark tab-mark newline-mark)
384 "*Specify which kind of blank is visualized.
386 It's a list containing some or all of the following values:
388 trailing trailing blanks are visualized via faces.
390 tabs TABs are visualized via faces.
392 spaces SPACEs and HARD SPACEs are visualized via
393 faces.
395 lines lines whose have columns beyond
396 `whitespace-line-column' are highlighted via
397 faces .
398 Whole line is highlighted.
399 It has precedence over `lines-tail' (see
400 below).
402 lines-tail lines whose have columns beyond
403 `whitespace-line-column' are highlighted via
404 faces.
405 But only the part of line which goes
406 beyond `whitespace-line-column' column.
407 It has effect only if `lines' (see above)
408 is not present in `whitespace-style'.
410 newline NEWLINEs are visualized via faces.
412 empty empty lines at beginning and/or end of buffer
413 are visualized via faces.
415 indentation::tab 8 or more SPACEs at beginning of line are
416 visualized via faces.
418 indentation::space TABs at beginning of line are visualized via
419 faces.
421 indentation 8 or more SPACEs at beginning of line are
422 visualized, if `indent-tabs-mode' (which see)
423 is non-nil; otherwise, TABs at beginning of
424 line are visualized via faces.
426 space-after-tab::tab 8 or more SPACEs after a TAB are
427 visualized via faces.
429 space-after-tab::space TABs are visualized when occurs 8 or
430 more SPACEs after a TAB via faces.
432 space-after-tab 8 or more SPACEs after a TAB are
433 visualized, if `indent-tabs-mode'
434 (which see) is non-nil; otherwise,
435 the TABs are visualized via faces.
437 space-before-tab::tab SPACEs before TAB are visualized via
438 faces.
440 space-before-tab::space TABs are visualized when occurs SPACEs
441 before TAB via faces.
443 space-before-tab SPACEs before TAB are visualized, if
444 `indent-tabs-mode' (which see) is
445 non-nil; otherwise, the TABs are
446 visualized via faces.
448 space-mark SPACEs and HARD SPACEs are visualized via
449 display table.
451 tab-mark TABs are visualized via display table.
453 newline-mark NEWLINEs are visualized via display table.
455 Any other value is ignored.
457 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
458 via display table.
460 There is an evaluation order for some values, if some values are
461 included in `whitespace-style' list. For example, if
462 indentation, indentation::tab and/or indentation::space are
463 included in `whitespace-style' list. The evaluation order for
464 these values is:
466 * For indentation:
467 1. indentation
468 2. indentation::tab
469 3. indentation::space
471 * For SPACEs after TABs:
472 1. space-after-tab
473 2. space-after-tab::tab
474 3. space-after-tab::space
476 * For SPACEs before TABs:
477 1. space-before-tab
478 2. space-before-tab::tab
479 3. space-before-tab::space
481 So, for example, if indentation and indentation::space are
482 included in `whitespace-style' list, the indentation value is
483 evaluated instead of indentation::space value.
485 See also `whitespace-display-mappings' for documentation."
486 :type '(repeat :tag "Kind of Blank"
487 (choice :tag "Kind of Blank Face"
488 (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
489 trailing)
490 (const :tag "(Face) SPACEs and HARD SPACEs"
491 spaces)
492 (const :tag "(Face) TABs" tabs)
493 (const :tag "(Face) Lines" lines)
494 (const :tag "(Face) SPACEs before TAB"
495 space-before-tab)
496 (const :tag "(Face) NEWLINEs" newline)
497 (const :tag "(Face) Indentation SPACEs"
498 indentation)
499 (const :tag "(Face) Empty Lines At BOB And/Or EOB"
500 empty)
501 (const :tag "(Face) SPACEs after TAB"
502 space-after-tab)
503 (const :tag "(Mark) SPACEs and HARD SPACEs"
504 space-mark)
505 (const :tag "(Mark) TABs" tab-mark)
506 (const :tag "(Mark) NEWLINEs" newline-mark)))
507 :group 'whitespace)
510 (defcustom whitespace-space 'whitespace-space
511 "*Symbol face used to visualize SPACE.
513 Used when `whitespace-style' includes the value `spaces'."
514 :type 'face
515 :group 'whitespace)
518 (defface whitespace-space
519 '((((class color) (background dark))
520 (:background "grey20" :foreground "aquamarine3"))
521 (((class color) (background light))
522 (:background "LightYellow" :foreground "aquamarine3"))
523 (t (:inverse-video t)))
524 "Face used to visualize SPACE."
525 :group 'whitespace)
528 (defcustom whitespace-hspace 'whitespace-hspace
529 "*Symbol face used to visualize HARD SPACE.
531 Used when `whitespace-style' includes the value `spaces'."
532 :type 'face
533 :group 'whitespace)
536 (defface whitespace-hspace ; 'nobreak-space
537 '((((class color) (background dark))
538 (:background "grey24" :foreground "aquamarine3"))
539 (((class color) (background light))
540 (:background "LemonChiffon3" :foreground "aquamarine3"))
541 (t (:inverse-video t)))
542 "Face used to visualize HARD SPACE."
543 :group 'whitespace)
546 (defcustom whitespace-tab 'whitespace-tab
547 "*Symbol face used to visualize TAB.
549 Used when `whitespace-style' includes the value `tabs'."
550 :type 'face
551 :group 'whitespace)
554 (defface whitespace-tab
555 '((((class color) (background dark))
556 (:background "grey22" :foreground "aquamarine3"))
557 (((class color) (background light))
558 (:background "beige" :foreground "aquamarine3"))
559 (t (:inverse-video t)))
560 "Face used to visualize TAB."
561 :group 'whitespace)
564 (defcustom whitespace-newline 'whitespace-newline
565 "*Symbol face used to visualize NEWLINE char mapping.
567 See `whitespace-display-mappings'.
569 Used when `whitespace-style' includes the values `newline-mark'
570 and `newline'."
571 :type 'face
572 :group 'whitespace)
575 (defface whitespace-newline
576 '((((class color) (background dark))
577 (:foreground "darkgray" :bold nil))
578 (((class color) (background light))
579 (:foreground "lightgray" :bold nil))
580 (t (:underline t :bold nil)))
581 "Face used to visualize NEWLINE char mapping.
583 See `whitespace-display-mappings'."
584 :group 'whitespace)
587 (defcustom whitespace-trailing 'whitespace-trailing
588 "*Symbol face used to visualize trailing blanks.
590 Used when `whitespace-style' includes the value `trailing'."
591 :type 'face
592 :group 'whitespace)
595 (defface whitespace-trailing ; 'trailing-whitespace
596 '((((class mono)) (:inverse-video t :bold t :underline t))
597 (t (:background "red1" :foreground "yellow" :bold t)))
598 "Face used to visualize trailing blanks."
599 :group 'whitespace)
602 (defcustom whitespace-line 'whitespace-line
603 "*Symbol face used to visualize \"long\" lines.
605 See `whitespace-line-column'.
607 Used when `whitespace-style' includes the value `line'."
608 :type 'face
609 :group 'whitespace)
612 (defface whitespace-line
613 '((((class mono)) (:inverse-video t :bold t :underline t))
614 (t (:background "gray20" :foreground "violet")))
615 "Face used to visualize \"long\" lines.
617 See `whitespace-line-column'."
618 :group 'whitespace)
621 (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
622 "*Symbol face used to visualize SPACEs before TAB.
624 Used when `whitespace-style' includes the value `space-before-tab'."
625 :type 'face
626 :group 'whitespace)
629 (defface whitespace-space-before-tab
630 '((((class mono)) (:inverse-video t :bold t :underline t))
631 (t (:background "DarkOrange" :foreground "firebrick")))
632 "Face used to visualize SPACEs before TAB."
633 :group 'whitespace)
636 (defcustom whitespace-indentation 'whitespace-indentation
637 "*Symbol face used to visualize 8 or more SPACEs at beginning of line.
639 Used when `whitespace-style' includes the value `indentation'."
640 :type 'face
641 :group 'whitespace)
644 (defface whitespace-indentation
645 '((((class mono)) (:inverse-video t :bold t :underline t))
646 (t (:background "yellow" :foreground "firebrick")))
647 "Face used to visualize 8 or more SPACEs at beginning of line."
648 :group 'whitespace)
651 (defcustom whitespace-empty 'whitespace-empty
652 "*Symbol face used to visualize empty lines at beginning and/or end of buffer.
654 Used when `whitespace-style' includes the value `empty'."
655 :type 'face
656 :group 'whitespace)
659 (defface whitespace-empty
660 '((((class mono)) (:inverse-video t :bold t :underline t))
661 (t (:background "yellow" :foreground "firebrick")))
662 "Face used to visualize empty lines at beginning and/or end of buffer."
663 :group 'whitespace)
666 (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
667 "*Symbol face used to visualize 8 or more SPACEs after TAB.
669 Used when `whitespace-style' includes the value `space-after-tab'."
670 :type 'face
671 :group 'whitespace)
674 (defface whitespace-space-after-tab
675 '((((class mono)) (:inverse-video t :bold t :underline t))
676 (t (:background "yellow" :foreground "firebrick")))
677 "Face used to visualize 8 or more SPACEs after TAB."
678 :group 'whitespace)
681 (defcustom whitespace-hspace-regexp
682 "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
683 "*Specify HARD SPACE characters regexp.
685 If you're using `mule' package, there may be other characters besides:
687 \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
689 that should be considered HARD SPACE.
691 Here are some examples:
693 \"\\\\(^\\xA0+\\\\)\" \
694 visualize only leading HARD SPACEs.
695 \"\\\\(\\xA0+$\\\\)\" \
696 visualize only trailing HARD SPACEs.
697 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
698 visualize leading and/or trailing HARD SPACEs.
699 \"\\t\\\\(\\xA0+\\\\)\\t\" \
700 visualize only HARD SPACEs between TABs.
702 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
703 Use exactly one pair of enclosing \\\\( and \\\\).
705 Used when `whitespace-style' includes `spaces'."
706 :type '(regexp :tag "HARD SPACE Chars")
707 :group 'whitespace)
710 (defcustom whitespace-space-regexp "\\( +\\)"
711 "*Specify SPACE characters regexp.
713 If you're using `mule' package, there may be other characters
714 besides \" \" that should be considered SPACE.
716 Here are some examples:
718 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
719 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
720 \"\\\\(^ +\\\\| +$\\\\)\" \
721 visualize leading and/or trailing SPACEs.
722 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
724 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
725 Use exactly one pair of enclosing \\\\( and \\\\).
727 Used when `whitespace-style' includes `spaces'."
728 :type '(regexp :tag "SPACE Chars")
729 :group 'whitespace)
732 (defcustom whitespace-tab-regexp "\\(\t+\\)"
733 "*Specify TAB characters regexp.
735 If you're using `mule' package, there may be other characters
736 besides \"\\t\" that should be considered TAB.
738 Here are some examples:
740 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
741 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
742 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
743 visualize leading and/or trailing TABs.
744 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
746 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
747 Use exactly one pair of enclosing \\\\( and \\\\).
749 Used when `whitespace-style' includes `tabs'."
750 :type '(regexp :tag "TAB Chars")
751 :group 'whitespace)
754 (defcustom whitespace-trailing-regexp
755 "\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$"
756 "*Specify trailing characters regexp.
758 If you're using `mule' package, there may be other characters besides:
760 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
761 \"\\xF20\"
763 that should be considered blank.
765 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
766 Use exactly one pair of enclosing elements above.
768 Used when `whitespace-style' includes `trailing'."
769 :type '(regexp :tag "Trailing Chars")
770 :group 'whitespace)
773 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
774 "*Specify SPACEs before TAB regexp.
776 If you're using `mule' package, there may be other characters besides:
778 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
779 \"\\xF20\"
781 that should be considered blank.
783 Used when `whitespace-style' includes `space-before-tab',
784 `space-before-tab::tab' or `space-before-tab::space'."
785 :type '(regexp :tag "SPACEs Before TAB")
786 :group 'whitespace)
789 (defcustom whitespace-indentation-regexp
790 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
791 . "^ *\\(\t+\\)[^\n]")
792 "*Specify regexp for 8 or more SPACEs at beginning of line.
794 It is a cons where the cons car is used for SPACEs visualization
795 and the cons cdr is used for TABs visualization.
797 If you're using `mule' package, there may be other characters besides:
799 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
800 \"\\xF20\"
802 that should be considered blank.
804 Used when `whitespace-style' includes `indentation',
805 `indentation::tab' or `indentation::space'."
806 :type '(cons (regexp :tag "Indentation SPACEs")
807 (regexp :tag "Indentation TABs"))
808 :group 'whitespace)
811 (defcustom whitespace-empty-at-bob-regexp "\\`\\(\\([ \t]*\n\\)+\\)"
812 "*Specify regexp for empty lines at beginning of buffer.
814 If you're using `mule' package, there may be other characters besides:
816 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
817 \"\\xF20\"
819 that should be considered blank.
821 Used when `whitespace-style' includes `empty'."
822 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
823 :group 'whitespace)
826 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)\\'"
827 "*Specify regexp for empty lines at end of buffer.
829 If you're using `mule' package, there may be other characters besides:
831 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
832 \"\\xF20\"
834 that should be considered blank.
836 Used when `whitespace-style' includes `empty'."
837 :type '(regexp :tag "Empty Lines At End Of Buffer")
838 :group 'whitespace)
841 (defcustom whitespace-space-after-tab-regexp
842 '("\t+\\(\\( \\{%d\\}\\)+\\)"
843 . "\\(\t+\\) +")
844 "*Specify regexp for 8 or more SPACEs after TAB.
846 It is a cons where the cons car is used for SPACEs visualization
847 and the cons cdr is used for TABs visualization.
849 If you're using `mule' package, there may be other characters besides:
851 \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
852 \"\\xF20\"
854 that should be considered blank.
856 Used when `whitespace-style' includes `space-after-tab',
857 `space-after-tab::tab' or `space-after-tab::space'."
858 :type '(regexp :tag "SPACEs After TAB")
859 :group 'whitespace)
862 (defcustom whitespace-line-column 80
863 "*Specify column beyond which the line is highlighted.
865 Used when `whitespace-style' includes `lines' or `lines-tail'."
866 :type '(integer :tag "Line Length")
867 :group 'whitespace)
870 ;; Hacked from `visible-whitespace-mappings' in visws.el
871 (defcustom whitespace-display-mappings
873 (space-mark ?\ [?\xB7] [?.]) ; space - centered dot
874 (space-mark ?\xA0 [?\xA4] [?_]) ; hard space - currency
875 (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency
876 (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency
877 (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency
878 (space-mark ?\xF20 [?\xF24] [?_]) ; hard space - currency
879 ;; NEWLINE is displayed using the face `whitespace-newline'
880 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
881 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
882 ;; (newline-mark ?\n [?\xB6 ?\n] [?$ ?\n]) ; eol - pilcrow
883 ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
884 ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
885 ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
887 ;; WARNING: the mapping below has a problem.
888 ;; When a TAB occupies exactly one column, it will display the
889 ;; character ?\xBB at that column followed by a TAB which goes to
890 ;; the next TAB column.
891 ;; If this is a problem for you, please, comment the line below.
892 (tab-mark ?\t [?\xBB ?\t] [?\\ ?\t]) ; tab - left quote mark
894 "*Specify an alist of mappings for displaying characters.
896 Each element has the following form:
898 (KIND CHAR VECTOR...)
900 Where:
902 KIND is the kind of character.
903 It can be one of the following symbols:
905 tab-mark for TAB character
907 space-mark for SPACE or HARD SPACE character
909 newline-mark for NEWLINE character
911 CHAR is the character to be mapped.
913 VECTOR is a vector of characters to be displayed in place of CHAR.
914 The first display vector that can be displayed is used;
915 if no display vector for a mapping can be displayed, then
916 that character is displayed unmodified.
918 The NEWLINE character is displayed using the face given by
919 `whitespace-newline' variable.
921 Used when `whitespace-style' includes `tab-mark', `space-mark' or
922 `newline-mark'."
923 :type '(repeat
924 (list :tag "Character Mapping"
925 (choice :tag "Char Kind"
926 (const :tag "Tab" tab-mark)
927 (const :tag "Space" space-mark)
928 (const :tag "Newline" newline-mark))
929 (character :tag "Char")
930 (repeat :inline t :tag "Vector List"
931 (vector :tag ""
932 (repeat :inline t
933 :tag "Vector Characters"
934 (character :tag "Char"))))))
935 :group 'whitespace)
938 (defcustom whitespace-global-modes t
939 "*Modes for which global `whitespace-mode' is automagically turned on.
941 Global `whitespace-mode' is controlled by the command
942 `global-whitespace-mode'.
944 If nil, means no modes have `whitespace-mode' automatically
945 turned on.
947 If t, all modes that support `whitespace-mode' have it
948 automatically turned on.
950 Else it should be a list of `major-mode' symbol names for which
951 `whitespace-mode' should be automatically turned on. The sense
952 of the list is negated if it begins with `not'. For example:
954 (c-mode c++-mode)
956 means that `whitespace-mode' is turned on for buffers in C and
957 C++ modes only."
958 :type '(choice :tag "Global Modes"
959 (const :tag "None" nil)
960 (const :tag "All" t)
961 (set :menu-tag "Mode Specific" :tag "Modes"
962 :value (not)
963 (const :tag "Except" not)
964 (repeat :inline t
965 (symbol :tag "Mode"))))
966 :group 'whitespace)
969 (defcustom whitespace-action nil
970 "*Specify which action is taken when a buffer is visited, killed or written.
972 It's a list containing some or all of the following values:
974 nil no action is taken.
976 cleanup cleanup any bogus whitespace always when local
977 whitespace is turned on.
978 See `whitespace-cleanup' and
979 `whitespace-cleanup-region'.
981 report-on-bogus report if there is any bogus whitespace always
982 when local whitespace is turned on.
984 auto-cleanup cleanup any bogus whitespace when buffer is
985 written or killed.
986 See `whitespace-cleanup' and
987 `whitespace-cleanup-region'.
989 abort-on-bogus abort if there is any bogus whitespace and the
990 buffer is written or killed.
992 Any other value is treated as nil."
993 :type '(choice :tag "Actions"
994 (const :tag "None" nil)
995 (repeat :tag "Action List"
996 (choice :tag "Action"
997 (const :tag "Cleanup When On" cleanup)
998 (const :tag "Report On Bogus" report-on-bogus)
999 (const :tag "Auto Cleanup" auto-cleanup)
1000 (const :tag "Abort On Bogus" abort-on-bogus))))
1001 :group 'whitespace)
1004 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1005 ;;;; User commands - Local mode
1008 ;;;###autoload
1009 (define-minor-mode whitespace-mode
1010 "Toggle whitespace minor mode visualization (\"ws\" on modeline).
1012 If ARG is null, toggle whitespace visualization.
1013 If ARG is a number greater than zero, turn on visualization;
1014 otherwise, turn off visualization.
1015 Only useful with a windowing system.
1017 See also `whitespace-style', `whitespace-newline' and
1018 `whitespace-display-mappings'."
1019 :lighter " ws"
1020 :init-value nil
1021 :global nil
1022 :group 'whitespace
1023 (cond
1024 (noninteractive ; running a batch job
1025 (setq whitespace-mode nil))
1026 (whitespace-mode ; whitespace-mode on
1027 (whitespace-turn-on)
1028 (whitespace-action-when-on))
1029 (t ; whitespace-mode off
1030 (whitespace-turn-off))))
1033 ;;;###autoload
1034 (define-minor-mode whitespace-newline-mode
1035 "Toggle NEWLINE minor mode visualization (\"nl\" on modeline).
1037 If ARG is null, toggle NEWLINE visualization.
1038 If ARG is a number greater than zero, turn on visualization;
1039 otherwise, turn off visualization.
1040 Only useful with a windowing system.
1042 Use `whitespace-newline-mode' only for NEWLINE visualization
1043 exclusively. For other visualizations, including NEWLINE
1044 visualization together with (HARD) SPACEs and/or TABs, please,
1045 use `whitespace-mode'.
1047 See also `whitespace-newline' and `whitespace-display-mappings'."
1048 :lighter " nl"
1049 :init-value nil
1050 :global nil
1051 :group 'whitespace
1052 (let ((whitespace-style '(newline-mark newline)))
1053 (whitespace-mode whitespace-newline-mode)
1054 ;; sync states (running a batch job)
1055 (setq whitespace-newline-mode whitespace-mode)))
1058 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1059 ;;;; User commands - Global mode
1062 ;;;###autoload
1063 (define-minor-mode global-whitespace-mode
1064 "Toggle whitespace global minor mode visualization (\"WS\" on modeline).
1066 If ARG is null, toggle whitespace visualization.
1067 If ARG is a number greater than zero, turn on visualization;
1068 otherwise, turn off visualization.
1069 Only useful with a windowing system.
1071 See also `whitespace-style', `whitespace-newline' and
1072 `whitespace-display-mappings'."
1073 :lighter " WS"
1074 :init-value nil
1075 :global t
1076 :group 'whitespace
1077 (cond
1078 (noninteractive ; running a batch job
1079 (setq global-whitespace-mode nil))
1080 (global-whitespace-mode ; global-whitespace-mode on
1081 (save-excursion
1082 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1083 (dolist (buffer (buffer-list)) ; adjust all local mode
1084 (set-buffer buffer)
1085 (unless whitespace-mode
1086 (whitespace-turn-on-if-enabled)))))
1087 (t ; global-whitespace-mode off
1088 (save-excursion
1089 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1090 (dolist (buffer (buffer-list)) ; adjust all local mode
1091 (set-buffer buffer)
1092 (unless whitespace-mode
1093 (whitespace-turn-off)))))))
1096 (defun whitespace-turn-on-if-enabled ()
1097 (when (cond
1098 ((eq whitespace-global-modes t))
1099 ((listp whitespace-global-modes)
1100 (if (eq (car-safe whitespace-global-modes) 'not)
1101 (not (memq major-mode (cdr whitespace-global-modes)))
1102 (memq major-mode whitespace-global-modes)))
1103 (t nil))
1104 (let (inhibit-quit)
1105 ;; Don't turn on whitespace mode if...
1107 ;; ...we don't have a display (we're running a batch job)
1108 noninteractive
1109 ;; ...or if the buffer is invisible (name starts with a space)
1110 (eq (aref (buffer-name) 0) ?\ )
1111 ;; ...or if the buffer is temporary (name starts with *)
1112 (and (eq (aref (buffer-name) 0) ?*)
1113 ;; except the scratch buffer.
1114 (not (string= (buffer-name) "*scratch*")))
1115 ;; Otherwise, turn on whitespace mode.
1116 (whitespace-turn-on)))))
1119 ;;;###autoload
1120 (define-minor-mode global-whitespace-newline-mode
1121 "Toggle NEWLINE global minor mode visualization (\"NL\" on modeline).
1123 If ARG is null, toggle NEWLINE visualization.
1124 If ARG is a number greater than zero, turn on visualization;
1125 otherwise, turn off visualization.
1126 Only useful with a windowing system.
1128 Use `global-whitespace-newline-mode' only for NEWLINE
1129 visualization exclusively. For other visualizations, including
1130 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1131 please, use `global-whitespace-mode'.
1133 See also `whitespace-newline' and `whitespace-display-mappings'."
1134 :lighter " NL"
1135 :init-value nil
1136 :global t
1137 :group 'whitespace
1138 (let ((whitespace-style '(newline-mark newline)))
1139 (global-whitespace-mode global-whitespace-newline-mode)
1140 ;; sync states (running a batch job)
1141 (setq global-whitespace-newline-mode global-whitespace-mode)))
1144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1145 ;;;; User commands - Toggle
1148 (defconst whitespace-style-value-list
1149 '(tabs
1150 spaces
1151 trailing
1152 lines
1153 lines-tail
1154 newline
1155 empty
1156 indentation
1157 indentation::tab
1158 indentation::space
1159 space-after-tab
1160 space-after-tab::tab
1161 space-after-tab::space
1162 space-before-tab
1163 space-before-tab::tab
1164 space-before-tab::space
1165 help-newline ; value used by `whitespace-insert-option-mark'
1166 tab-mark
1167 space-mark
1168 newline-mark
1170 "List of valid `whitespace-style' values.")
1173 (defconst whitespace-toggle-option-alist
1174 '((?t . tabs)
1175 (?s . spaces)
1176 (?r . trailing)
1177 (?l . lines)
1178 (?L . lines-tail)
1179 (?n . newline)
1180 (?e . empty)
1181 (?\C-i . indentation)
1182 (?I . indentation::tab)
1183 (?i . indentation::space)
1184 (?\C-a . space-after-tab)
1185 (?A . space-after-tab::tab)
1186 (?a . space-after-tab::space)
1187 (?\C-b . space-before-tab)
1188 (?B . space-before-tab::tab)
1189 (?b . space-before-tab::space)
1190 (?T . tab-mark)
1191 (?S . space-mark)
1192 (?N . newline-mark)
1193 (?x . whitespace-style)
1195 "Alist of toggle options.
1197 Each element has the form:
1199 (CHAR . SYMBOL)
1201 Where:
1203 CHAR is a char which the user will have to type.
1205 SYMBOL is a valid symbol associated with CHAR.
1206 See `whitespace-style-value-list'.")
1209 (defvar whitespace-active-style nil
1210 "Used to save locally `whitespace-style' value.")
1212 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1213 "Used to save locally `indent-tabs-mode' value.")
1215 (defvar whitespace-tab-width tab-width
1216 "Used to save locally `tab-width' value.")
1219 ;;;###autoload
1220 (defun whitespace-toggle-options (arg)
1221 "Toggle local `whitespace-mode' options.
1223 If local whitespace-mode is off, toggle the option given by ARG
1224 and turn on local whitespace-mode.
1226 If local whitespace-mode is on, toggle the option given by ARG
1227 and restart local whitespace-mode.
1229 Interactively, it reads one of the following chars:
1231 CHAR MEANING
1232 (VIA FACES)
1233 t toggle TAB visualization
1234 s toggle SPACE and HARD SPACE visualization
1235 r toggle trailing blanks visualization
1236 l toggle \"long lines\" visualization
1237 L toggle \"long lines\" tail visualization
1238 n toggle NEWLINE visualization
1239 e toggle empty line at bob and/or eob visualization
1240 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1241 I toggle indentation SPACEs visualization
1242 i toggle indentation TABs visualization
1243 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1244 A toggle SPACEs after TAB: SPACEs visualization
1245 a toggle SPACEs after TAB: TABs visualization
1246 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1247 B toggle SPACEs before TAB: SPACEs visualization
1248 b toggle SPACEs before TAB: TABs visualization
1250 (VIA DISPLAY TABLE)
1251 T toggle TAB visualization
1252 S toggle SPACEs before TAB visualization
1253 N toggle NEWLINE visualization
1255 x restore `whitespace-style' value
1256 ? display brief help
1258 Non-interactively, ARG should be a symbol or a list of symbols.
1259 The valid symbols are:
1261 tabs toggle TAB visualization
1262 spaces toggle SPACE and HARD SPACE visualization
1263 trailing toggle trailing blanks visualization
1264 lines toggle \"long lines\" visualization
1265 lines-tail toggle \"long lines\" tail visualization
1266 newline toggle NEWLINE visualization
1267 empty toggle empty line at bob and/or eob visualization
1268 indentation toggle indentation SPACEs visualization
1269 indentation::tab toggle indentation SPACEs visualization
1270 indentation::space toggle indentation TABs visualization
1271 space-after-tab toggle SPACEs after TAB visualization
1272 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1273 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1274 space-before-tab toggle SPACEs before TAB visualization
1275 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1276 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1278 tab-mark toggle TAB visualization
1279 space-mark toggle SPACEs before TAB visualization
1280 newline-mark toggle NEWLINE visualization
1282 whitespace-style restore `whitespace-style' value
1284 Only useful with a windowing system.
1286 See `whitespace-style' and `indent-tabs-mode' for documentation."
1287 (interactive (whitespace-interactive-char t))
1288 (let ((whitespace-style
1289 (whitespace-toggle-list t arg whitespace-active-style)))
1290 (whitespace-mode 0)
1291 (whitespace-mode 1)))
1294 (defvar whitespace-toggle-style nil
1295 "Used to toggle the global `whitespace-style' value.")
1298 ;;;###autoload
1299 (defun global-whitespace-toggle-options (arg)
1300 "Toggle global `whitespace-mode' options.
1302 If global whitespace-mode is off, toggle the option given by ARG
1303 and turn on global whitespace-mode.
1305 If global whitespace-mode is on, toggle the option given by ARG
1306 and restart global whitespace-mode.
1308 Interactively, it accepts one of the following chars:
1310 CHAR MEANING
1311 (VIA FACES)
1312 t toggle TAB visualization
1313 s toggle SPACE and HARD SPACE visualization
1314 r toggle trailing blanks visualization
1315 l toggle \"long lines\" visualization
1316 L toggle \"long lines\" tail visualization
1317 n toggle NEWLINE visualization
1318 e toggle empty line at bob and/or eob visualization
1319 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1320 I toggle indentation SPACEs visualization
1321 i toggle indentation TABs visualization
1322 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1323 A toggle SPACEs after TAB: SPACEs visualization
1324 a toggle SPACEs after TAB: TABs visualization
1325 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1326 B toggle SPACEs before TAB: SPACEs visualization
1327 b toggle SPACEs before TAB: TABs visualization
1329 (VIA DISPLAY TABLE)
1330 T toggle TAB visualization
1331 S toggle SPACEs before TAB visualization
1332 N toggle NEWLINE visualization
1334 x restore `whitespace-style' value
1335 ? display brief help
1337 Non-interactively, ARG should be a symbol or a list of symbols.
1338 The valid symbols are:
1340 tabs toggle TAB visualization
1341 spaces toggle SPACE and HARD SPACE visualization
1342 trailing toggle trailing blanks visualization
1343 lines toggle \"long lines\" visualization
1344 lines-tail toggle \"long lines\" tail visualization
1345 newline toggle NEWLINE visualization
1346 empty toggle empty line at bob and/or eob visualization
1347 indentation toggle indentation SPACEs visualization
1348 indentation::tab toggle indentation SPACEs visualization
1349 indentation::space toggle indentation TABs visualization
1350 space-after-tab toggle SPACEs after TAB visualization
1351 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1352 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1353 space-before-tab toggle SPACEs before TAB visualization
1354 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1355 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1357 tab-mark toggle TAB visualization
1358 space-mark toggle SPACEs before TAB visualization
1359 newline-mark toggle NEWLINE visualization
1361 whitespace-style restore `whitespace-style' value
1363 Only useful with a windowing system.
1365 See `whitespace-style' and `indent-tabs-mode' for documentation."
1366 (interactive (whitespace-interactive-char nil))
1367 (let ((whitespace-style
1368 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1369 (setq whitespace-toggle-style whitespace-style)
1370 (global-whitespace-mode 0)
1371 (global-whitespace-mode 1)))
1374 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1375 ;;;; User commands - Cleanup
1378 ;;;###autoload
1379 (defun whitespace-cleanup ()
1380 "Cleanup some blank problems in all buffer or at region.
1382 It usually applies to the whole buffer, but in transient mark
1383 mode when the mark is active, it applies to the region. It also
1384 applies to the region when it is not in transiente mark mode, the
1385 mark is active and \\[universal-argument] was pressed just before
1386 calling `whitespace-cleanup' interactively.
1388 See also `whitespace-cleanup-region'.
1390 The problems cleaned up are:
1392 1. empty lines at beginning of buffer.
1393 2. empty lines at end of buffer.
1394 If `whitespace-style' includes the value `empty', remove all
1395 empty lines at beginning and/or end of buffer.
1397 3. 8 or more SPACEs at beginning of line.
1398 If `whitespace-style' includes the value `indentation':
1399 replace 8 or more SPACEs at beginning of line by TABs, if
1400 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1401 SPACEs.
1402 If `whitespace-style' includes the value `indentation::tab',
1403 replace 8 or more SPACEs at beginning of line by TABs.
1404 If `whitespace-style' includes the value `indentation::space',
1405 replace TABs by SPACEs.
1407 4. SPACEs before TAB.
1408 If `whitespace-style' includes the value `space-before-tab':
1409 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1410 otherwise, replace TABs by SPACEs.
1411 If `whitespace-style' includes the value
1412 `space-before-tab::tab', replace SPACEs by TABs.
1413 If `whitespace-style' includes the value
1414 `space-before-tab::space', replace TABs by SPACEs.
1416 5. SPACEs or TABs at end of line.
1417 If `whitespace-style' includes the value `trailing', remove
1418 all SPACEs or TABs at end of line.
1420 6. 8 or more SPACEs after TAB.
1421 If `whitespace-style' includes the value `space-after-tab':
1422 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1423 otherwise, replace TABs by SPACEs.
1424 If `whitespace-style' includes the value
1425 `space-after-tab::tab', replace SPACEs by TABs.
1426 If `whitespace-style' includes the value
1427 `space-after-tab::space', replace TABs by SPACEs.
1429 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1430 documentation."
1431 (interactive "@*")
1432 (if (and (or transient-mark-mode
1433 current-prefix-arg)
1434 mark-active)
1435 ;; region active
1436 ;; PROBLEMs 1 and 2 are not handled in region
1437 ;; PROBLEM 3: 8 or more SPACEs at bol
1438 ;; PROBLEM 4: SPACEs before TAB
1439 ;; PROBLEM 5: SPACEs or TABs at eol
1440 ;; PROBLEM 6: 8 or more SPACEs after TAB
1441 (whitespace-cleanup-region (region-beginning) (region-end))
1442 ;; whole buffer
1443 (save-excursion
1444 (save-match-data
1445 ;; PROBLEM 1: empty lines at bob
1446 ;; PROBLEM 2: empty lines at eob
1447 ;; ACTION: remove all empty lines at bob and/or eob
1448 (when (memq 'empty whitespace-style)
1449 (let (overwrite-mode) ; enforce no overwrite
1450 (goto-char (point-min))
1451 (when (re-search-forward
1452 whitespace-empty-at-bob-regexp nil t)
1453 (delete-region (match-beginning 1) (match-end 1)))
1454 (when (re-search-forward
1455 whitespace-empty-at-eob-regexp nil t)
1456 (delete-region (match-beginning 1) (match-end 1)))))))
1457 ;; PROBLEM 3: 8 or more SPACEs at bol
1458 ;; PROBLEM 4: SPACEs before TAB
1459 ;; PROBLEM 5: SPACEs or TABs at eol
1460 ;; PROBLEM 6: 8 or more SPACEs after TAB
1461 (whitespace-cleanup-region (point-min) (point-max))))
1464 ;;;###autoload
1465 (defun whitespace-cleanup-region (start end)
1466 "Cleanup some blank problems at region.
1468 The problems cleaned up are:
1470 1. 8 or more SPACEs at beginning of line.
1471 If `whitespace-style' includes the value `indentation':
1472 replace 8 or more SPACEs at beginning of line by TABs, if
1473 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1474 SPACEs.
1475 If `whitespace-style' includes the value `indentation::tab',
1476 replace 8 or more SPACEs at beginning of line by TABs.
1477 If `whitespace-style' includes the value `indentation::space',
1478 replace TABs by SPACEs.
1480 2. SPACEs before TAB.
1481 If `whitespace-style' includes the value `space-before-tab':
1482 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1483 otherwise, replace TABs by SPACEs.
1484 If `whitespace-style' includes the value
1485 `space-before-tab::tab', replace SPACEs by TABs.
1486 If `whitespace-style' includes the value
1487 `space-before-tab::space', replace TABs by SPACEs.
1489 3. SPACEs or TABs at end of line.
1490 If `whitespace-style' includes the value `trailing', remove
1491 all SPACEs or TABs at end of line.
1493 4. 8 or more SPACEs after TAB.
1494 If `whitespace-style' includes the value `space-after-tab':
1495 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1496 otherwise, replace TABs by SPACEs.
1497 If `whitespace-style' includes the value
1498 `space-after-tab::tab', replace SPACEs by TABs.
1499 If `whitespace-style' includes the value
1500 `space-after-tab::space', replace TABs by SPACEs.
1502 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1503 documentation."
1504 (interactive "@*r")
1505 (let ((rstart (min start end))
1506 (rend (copy-marker (max start end)))
1507 (indent-tabs-mode whitespace-indent-tabs-mode)
1508 (tab-width whitespace-tab-width)
1509 overwrite-mode ; enforce no overwrite
1510 tmp)
1511 (save-excursion
1512 (save-match-data
1513 ;; PROBLEM 1: 8 or more SPACEs at bol
1514 (cond
1515 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1516 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1517 ;; SPACEs.
1518 ((memq 'indentation whitespace-style)
1519 (let ((regexp (whitespace-indentation-regexp)))
1520 (goto-char rstart)
1521 (while (re-search-forward regexp rend t)
1522 (setq tmp (current-indentation))
1523 (goto-char (match-beginning 0))
1524 (delete-horizontal-space)
1525 (unless (eolp)
1526 (indent-to tmp)))))
1527 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1528 ((memq 'indentation::tab whitespace-style)
1529 (whitespace-replace-action
1530 'tabify rstart rend
1531 (whitespace-indentation-regexp 'tab) 0))
1532 ;; ACTION: replace TABs by SPACEs.
1533 ((memq 'indentation::space whitespace-style)
1534 (whitespace-replace-action
1535 'untabify rstart rend
1536 (whitespace-indentation-regexp 'space) 0)))
1537 ;; PROBLEM 3: SPACEs or TABs at eol
1538 ;; ACTION: remove all SPACEs or TABs at eol
1539 (when (memq 'trailing whitespace-style)
1540 (whitespace-replace-action
1541 'delete-region rstart rend
1542 whitespace-trailing-regexp 1))
1543 ;; PROBLEM 4: 8 or more SPACEs after TAB
1544 (cond
1545 ;; ACTION: replace 8 or more SPACEs by TABs, if
1546 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1547 ;; SPACEs.
1548 ((memq 'space-after-tab whitespace-style)
1549 (whitespace-replace-action
1550 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1551 rstart rend (whitespace-space-after-tab-regexp) 1))
1552 ;; ACTION: replace 8 or more SPACEs by TABs.
1553 ((memq 'space-after-tab::tab whitespace-style)
1554 (whitespace-replace-action
1555 'tabify rstart rend
1556 (whitespace-space-after-tab-regexp 'tab) 1))
1557 ;; ACTION: replace TABs by SPACEs.
1558 ((memq 'space-after-tab::space whitespace-style)
1559 (whitespace-replace-action
1560 'untabify rstart rend
1561 (whitespace-space-after-tab-regexp 'space) 1)))
1562 ;; PROBLEM 2: SPACEs before TAB
1563 (cond
1564 ;; ACTION: replace SPACEs before TAB by TABs, if
1565 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1566 ;; SPACEs.
1567 ((memq 'space-before-tab whitespace-style)
1568 (whitespace-replace-action
1569 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1570 rstart rend whitespace-space-before-tab-regexp
1571 (if whitespace-indent-tabs-mode 1 2)))
1572 ;; ACTION: replace SPACEs before TAB by TABs.
1573 ((memq 'space-before-tab::tab whitespace-style)
1574 (whitespace-replace-action
1575 'tabify rstart rend
1576 whitespace-space-before-tab-regexp 1))
1577 ;; ACTION: replace TABs by SPACEs.
1578 ((memq 'space-before-tab::space whitespace-style)
1579 (whitespace-replace-action
1580 'untabify rstart rend
1581 whitespace-space-before-tab-regexp 2)))))
1582 (set-marker rend nil))) ; point marker to nowhere
1585 (defun whitespace-replace-action (action rstart rend regexp index)
1586 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1588 INDEX is the level group matched by REGEXP and used by ACTION.
1590 See also `tab-width'."
1591 (goto-char rstart)
1592 (while (re-search-forward regexp rend t)
1593 (goto-char (match-end index))
1594 (funcall action (match-beginning index) (match-end index))))
1597 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1598 ;;;; User command - report
1601 (defun whitespace-regexp (regexp &optional kind)
1602 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1603 (cond
1604 ((or (eq kind 'tab)
1605 whitespace-indent-tabs-mode)
1606 (format (car regexp) whitespace-tab-width))
1607 ((or (eq kind 'space)
1608 (not whitespace-indent-tabs-mode))
1609 (cdr regexp))))
1612 (defun whitespace-indentation-regexp (&optional kind)
1613 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1614 (whitespace-regexp whitespace-indentation-regexp kind))
1617 (defun whitespace-space-after-tab-regexp (&optional kind)
1618 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1619 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1622 (defconst whitespace-report-list
1623 (list
1624 (cons 'empty whitespace-empty-at-bob-regexp)
1625 (cons 'empty whitespace-empty-at-eob-regexp)
1626 (cons 'trailing whitespace-trailing-regexp)
1627 (cons 'indentation nil)
1628 (cons 'indentation::tab nil)
1629 (cons 'indentation::space nil)
1630 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1631 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1632 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1633 (cons 'space-after-tab nil)
1634 (cons 'space-after-tab::tab nil)
1635 (cons 'space-after-tab::space nil)
1637 "List of whitespace bogus symbol and corresponding regexp.")
1640 (defconst whitespace-report-text
1641 '( ;; `indent-tabs-mode' has non-nil value
1643 Whitespace Report
1645 Current Setting Whitespace Problem
1647 empty [] [] empty lines at beginning of buffer
1648 empty [] [] empty lines at end of buffer
1649 trailing [] [] SPACEs or TABs at end of line
1650 indentation [] [] 8 or more SPACEs at beginning of line
1651 indentation::tab [] [] 8 or more SPACEs at beginning of line
1652 indentation::space [] [] TABs at beginning of line
1653 space-before-tab [] [] SPACEs before TAB
1654 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1655 space-before-tab::space [] [] SPACEs before TAB: TABs
1656 space-after-tab [] [] 8 or more SPACEs after TAB
1657 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1658 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1660 indent-tabs-mode =
1661 tab-width = \n\n"
1662 . ;; `indent-tabs-mode' has nil value
1664 Whitespace Report
1666 Current Setting Whitespace Problem
1668 empty [] [] empty lines at beginning of buffer
1669 empty [] [] empty lines at end of buffer
1670 trailing [] [] SPACEs or TABs at end of line
1671 indentation [] [] TABs at beginning of line
1672 indentation::tab [] [] 8 or more SPACEs at beginning of line
1673 indentation::space [] [] TABs at beginning of line
1674 space-before-tab [] [] SPACEs before TAB
1675 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1676 space-before-tab::space [] [] SPACEs before TAB: TABs
1677 space-after-tab [] [] 8 or more SPACEs after TAB
1678 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1679 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1681 indent-tabs-mode =
1682 tab-width = \n\n")
1683 "Text for whitespace bogus report.
1685 It is a cons of strings, where the car part is used when
1686 `indent-tabs-mode' is non-nil, and the cdr part is used when
1687 `indent-tabs-mode' is nil.")
1690 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1691 "The buffer name for whitespace bogus report.")
1694 ;;;###autoload
1695 (defun whitespace-report (&optional force report-if-bogus)
1696 "Report some whitespace problems in buffer.
1698 Return nil if there is no whitespace problem; otherwise, return
1699 non-nil.
1701 If FORCE is non-nil or \\[universal-argument] was pressed just
1702 before calling `whitespace-report' interactively, it forces
1703 `whitespace-style' to have:
1705 empty
1706 trailing
1707 indentation
1708 space-before-tab
1709 space-after-tab
1711 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1712 whitespace problems in buffer.
1714 Report if some of the following whitespace problems exist:
1716 * If `indent-tabs-mode' is non-nil:
1717 empty 1. empty lines at beginning of buffer.
1718 empty 2. empty lines at end of buffer.
1719 trailing 3. SPACEs or TABs at end of line.
1720 indentation 4. 8 or more SPACEs at beginning of line.
1721 space-before-tab 5. SPACEs before TAB.
1722 space-after-tab 6. 8 or more SPACEs after TAB.
1724 * If `indent-tabs-mode' is nil:
1725 empty 1. empty lines at beginning of buffer.
1726 empty 2. empty lines at end of buffer.
1727 trailing 3. SPACEs or TABs at end of line.
1728 indentation 4. TABS at beginning of line.
1729 space-before-tab 5. SPACEs before TAB.
1730 space-after-tab 6. 8 or more SPACEs after TAB.
1732 See `whitespace-style' for documentation.
1733 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1734 cleaning up these problems."
1735 (interactive (list current-prefix-arg))
1736 (whitespace-report-region (point-min) (point-max)
1737 force report-if-bogus))
1740 ;;;###autoload
1741 (defun whitespace-report-region (start end &optional force report-if-bogus)
1742 "Report some whitespace problems in a region.
1744 Return nil if there is no whitespace problem; otherwise, return
1745 non-nil.
1747 If FORCE is non-nil or \\[universal-argument] was pressed just
1748 before calling `whitespace-report-region' interactively, it
1749 forces `whitespace-style' to have:
1751 empty
1752 indentation
1753 space-before-tab
1754 trailing
1755 space-after-tab
1757 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1758 whitespace problems in buffer.
1760 Report if some of the following whitespace problems exist:
1762 * If `indent-tabs-mode' is non-nil:
1763 empty 1. empty lines at beginning of buffer.
1764 empty 2. empty lines at end of buffer.
1765 trailing 3. SPACEs or TABs at end of line.
1766 indentation 4. 8 or more SPACEs at beginning of line.
1767 space-before-tab 5. SPACEs before TAB.
1768 space-after-tab 6. 8 or more SPACEs after TAB.
1770 * If `indent-tabs-mode' is nil:
1771 empty 1. empty lines at beginning of buffer.
1772 empty 2. empty lines at end of buffer.
1773 trailing 3. SPACEs or TABs at end of line.
1774 indentation 4. TABS at beginning of line.
1775 space-before-tab 5. SPACEs before TAB.
1776 space-after-tab 6. 8 or more SPACEs after TAB.
1778 See `whitespace-style' for documentation.
1779 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1780 cleaning up these problems."
1781 (interactive "r")
1782 (setq force (or current-prefix-arg force))
1783 (save-excursion
1784 (save-match-data
1785 (let* ((has-bogus nil)
1786 (rstart (min start end))
1787 (rend (max start end))
1788 (bogus-list
1789 (mapcar
1790 #'(lambda (option)
1791 (when force
1792 (add-to-list 'whitespace-style (car option)))
1793 (goto-char rstart)
1794 (let ((regexp
1795 (cond
1796 ((eq (car option) 'indentation)
1797 (whitespace-indentation-regexp))
1798 ((eq (car option) 'indentation::tab)
1799 (whitespace-indentation-regexp 'tab))
1800 ((eq (car option) 'indentation::space)
1801 (whitespace-indentation-regexp 'space))
1802 ((eq (car option) 'space-after-tab)
1803 (whitespace-space-after-tab-regexp))
1804 ((eq (car option) 'space-after-tab::tab)
1805 (whitespace-space-after-tab-regexp 'tab))
1806 ((eq (car option) 'space-after-tab::space)
1807 (whitespace-space-after-tab-regexp 'space))
1809 (cdr option)))))
1810 (and (re-search-forward regexp rend t)
1811 (setq has-bogus t))))
1812 whitespace-report-list)))
1813 (when (if report-if-bogus has-bogus t)
1814 (whitespace-kill-buffer whitespace-report-buffer-name)
1815 ;; `whitespace-indent-tabs-mode' is local to current buffer
1816 ;; `whitespace-tab-width' is local to current buffer
1817 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1818 (ws-tab-width whitespace-tab-width))
1819 (with-current-buffer (get-buffer-create
1820 whitespace-report-buffer-name)
1821 (erase-buffer)
1822 (insert (if ws-indent-tabs-mode
1823 (car whitespace-report-text)
1824 (cdr whitespace-report-text)))
1825 (goto-char (point-min))
1826 (forward-line 3)
1827 (dolist (option whitespace-report-list)
1828 (forward-line 1)
1829 (whitespace-mark-x
1830 27 (memq (car option) whitespace-style))
1831 (whitespace-mark-x 7 (car bogus-list))
1832 (setq bogus-list (cdr bogus-list)))
1833 (forward-line 1)
1834 (whitespace-insert-value ws-indent-tabs-mode)
1835 (whitespace-insert-value ws-tab-width)
1836 (when has-bogus
1837 (goto-char (point-max))
1838 (insert " Type `M-x whitespace-cleanup'"
1839 " to cleanup the buffer.\n\n"
1840 " Type `M-x whitespace-cleanup-region'"
1841 " to cleanup a region.\n\n"))
1842 (whitespace-display-window (current-buffer)))))
1843 has-bogus))))
1846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1847 ;;;; Internal functions
1850 (defvar whitespace-font-lock-mode nil
1851 "Used to remember whether a buffer had font lock mode on or not.")
1853 (defvar whitespace-font-lock nil
1854 "Used to remember whether a buffer initially had font lock on or not.")
1856 (defvar whitespace-font-lock-keywords nil
1857 "Used to save locally `font-lock-keywords' value.")
1860 (defconst whitespace-help-text
1862 Whitespace Toggle Options
1864 FACES
1865 [] t - toggle TAB visualization
1866 [] s - toggle SPACE and HARD SPACE visualization
1867 [] r - toggle trailing blanks visualization
1868 [] l - toggle \"long lines\" visualization
1869 [] L - toggle \"long lines\" tail visualization
1870 [] n - toggle NEWLINE visualization
1871 [] e - toggle empty line at bob and/or eob visualization
1872 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1873 [] I - toggle indentation SPACEs visualization
1874 [] i - toggle indentation TABs visualization
1875 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1876 [] A - toggle SPACEs after TAB: SPACEs visualization
1877 [] a - toggle SPACEs after TAB: TABs visualization
1878 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1879 [] B - toggle SPACEs before TAB: SPACEs visualization
1880 [] b - toggle SPACEs before TAB: TABs visualization
1882 DISPLAY TABLE
1883 [] T - toggle TAB visualization
1884 [] S - toggle SPACE and HARD SPACE visualization
1885 [] N - toggle NEWLINE visualization
1887 x - restore `whitespace-style' value
1889 ? - display this text\n\n"
1890 "Text for whitespace toggle options.")
1893 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1894 "The buffer name for whitespace toggle options.")
1897 (defun whitespace-insert-value (value)
1898 "Insert VALUE at column 20 of next line."
1899 (forward-line 1)
1900 (move-to-column 20 t)
1901 (insert (format "%s" value)))
1904 (defun whitespace-mark-x (nchars condition)
1905 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1906 (forward-char nchars)
1907 (insert (if condition "X" " ")))
1910 (defun whitespace-insert-option-mark (the-list the-value)
1911 "Insert the option mark ('X' or ' ') in toggle options buffer."
1912 (goto-char (point-min))
1913 (forward-line 2)
1914 (dolist (sym the-list)
1915 (if (eq sym 'help-newline)
1916 (forward-line 2)
1917 (forward-line 1)
1918 (whitespace-mark-x 2 (memq sym the-value)))))
1921 (defun whitespace-help-on (style)
1922 "Display the whitespace toggle options."
1923 (unless (get-buffer whitespace-help-buffer-name)
1924 (delete-other-windows)
1925 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
1926 (save-excursion
1927 (set-buffer buffer)
1928 (erase-buffer)
1929 (insert whitespace-help-text)
1930 (whitespace-insert-option-mark
1931 whitespace-style-value-list style)
1932 (whitespace-display-window buffer)))))
1935 (defun whitespace-display-window (buffer)
1936 "Display BUFFER in a new window."
1937 (goto-char (point-min))
1938 (set-buffer-modified-p nil)
1939 (let ((size (- (window-height)
1940 (max window-min-height
1941 (1+ (count-lines (point-min)
1942 (point-max)))))))
1943 (when (<= size 0)
1944 (kill-buffer buffer)
1945 (error "Frame height is too small; \
1946 can't split window to display whitespace toggle options"))
1947 (set-window-buffer (split-window nil size) buffer)))
1950 (defun whitespace-kill-buffer (buffer-name)
1951 "Kill buffer BUFFER-NAME and windows related with it."
1952 (let ((buffer (get-buffer buffer-name)))
1953 (when buffer
1954 (delete-windows-on buffer)
1955 (kill-buffer buffer))))
1958 (defun whitespace-help-off ()
1959 "Remove the buffer and window of the whitespace toggle options."
1960 (whitespace-kill-buffer whitespace-help-buffer-name))
1963 (defun whitespace-interactive-char (local-p)
1964 "Interactive function to read a char and return a symbol.
1966 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1967 uses a global context.
1969 It accepts one of the following chars:
1971 CHAR MEANING
1972 (VIA FACES)
1973 t toggle TAB visualization
1974 s toggle SPACE and HARD SPACE visualization
1975 r toggle trailing blanks visualization
1976 l toggle \"long lines\" visualization
1977 L toggle \"long lines\" tail visualization
1978 n toggle NEWLINE visualization
1979 e toggle empty line at bob and/or eob visualization
1980 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1981 I toggle indentation SPACEs visualization
1982 i toggle indentation TABs visualization
1983 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1984 A toggle SPACEs after TAB: SPACEs visualization
1985 a toggle SPACEs after TAB: TABs visualization
1986 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1987 B toggle SPACEs before TAB: SPACEs visualization
1988 b toggle SPACEs before TAB: TABs visualization
1990 (VIA DISPLAY TABLE)
1991 T toggle TAB visualization
1992 S toggle SPACE and HARD SPACE visualization
1993 N toggle NEWLINE visualization
1995 x restore `whitespace-style' value
1996 ? display brief help
1998 See also `whitespace-toggle-option-alist'."
1999 (let* ((is-off (not (if local-p
2000 whitespace-mode
2001 global-whitespace-mode)))
2002 (style (cond (is-off whitespace-style) ; use default value
2003 (local-p whitespace-active-style)
2004 (t whitespace-toggle-style)))
2005 (prompt
2006 (format "Whitespace Toggle %s (type ? for further options)-"
2007 (if local-p "Local" "Global")))
2008 ch sym)
2009 ;; read a valid option and get the corresponding symbol
2010 (save-window-excursion
2011 (condition-case data
2012 (progn
2013 (while
2014 ;; while condition
2015 (progn
2016 (setq ch (read-char prompt))
2017 (not
2018 (setq sym
2019 (cdr
2020 (assq ch whitespace-toggle-option-alist)))))
2021 ;; while body
2022 (if (eq ch ?\?)
2023 (whitespace-help-on style)
2024 (ding)))
2025 (whitespace-help-off)
2026 (message " ")) ; clean echo area
2027 ;; handler
2028 ((quit error)
2029 (whitespace-help-off)
2030 (error (error-message-string data)))))
2031 (list sym))) ; return the apropriate symbol
2034 (defun whitespace-toggle-list (local-p arg the-list)
2035 "Toggle options in THE-LIST based on list ARG.
2037 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2038 uses a global context.
2040 ARG is a list of options to be toggled.
2042 THE-LIST is a list of options. This list will be toggled and the
2043 resultant list will be returned."
2044 (unless (if local-p whitespace-mode global-whitespace-mode)
2045 (setq the-list whitespace-style))
2046 (setq the-list (copy-sequence the-list)) ; keep original list
2047 (dolist (sym (if (listp arg) arg (list arg)))
2048 (cond
2049 ;; ignore help value
2050 ((eq sym 'help-newline))
2051 ;; restore default values
2052 ((eq sym 'whitespace-style)
2053 (setq the-list whitespace-style))
2054 ;; toggle valid values
2055 ((memq sym whitespace-style-value-list)
2056 (setq the-list (if (memq sym the-list)
2057 (delq sym the-list)
2058 (cons sym the-list))))))
2059 the-list)
2062 (defvar whitespace-display-table nil
2063 "Used to save a local display table.")
2065 (defvar whitespace-display-table-was-local nil
2066 "Used to remember whether a buffer initially had a local display table.")
2069 (defun whitespace-turn-on ()
2070 "Turn on whitespace visualization."
2071 ;; prepare local hooks
2072 (whitespace-add-local-hook)
2073 ;; create whitespace local buffer environment
2074 (set (make-local-variable 'whitespace-font-lock-mode) nil)
2075 (set (make-local-variable 'whitespace-font-lock) nil)
2076 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2077 (set (make-local-variable 'whitespace-display-table) nil)
2078 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2079 (set (make-local-variable 'whitespace-active-style)
2080 (if (listp whitespace-style)
2081 whitespace-style
2082 (list whitespace-style)))
2083 (set (make-local-variable 'whitespace-indent-tabs-mode)
2084 indent-tabs-mode)
2085 (set (make-local-variable 'whitespace-tab-width)
2086 tab-width)
2087 ;; turn on whitespace
2088 (when whitespace-active-style
2089 (whitespace-color-on)
2090 (whitespace-display-char-on)))
2093 (defun whitespace-turn-off ()
2094 "Turn off whitespace visualization."
2095 (whitespace-remove-local-hook)
2096 (when whitespace-active-style
2097 (whitespace-color-off)
2098 (whitespace-display-char-off)))
2101 (defun whitespace-style-face-p ()
2102 "Return t if there is some visualization via face."
2103 (or (memq 'tabs whitespace-active-style)
2104 (memq 'spaces whitespace-active-style)
2105 (memq 'trailing whitespace-active-style)
2106 (memq 'lines whitespace-active-style)
2107 (memq 'lines-tail whitespace-active-style)
2108 (memq 'newline whitespace-active-style)
2109 (memq 'empty whitespace-active-style)
2110 (memq 'indentation whitespace-active-style)
2111 (memq 'indentation::tab whitespace-active-style)
2112 (memq 'indentation::space whitespace-active-style)
2113 (memq 'space-after-tab whitespace-active-style)
2114 (memq 'space-after-tab::tab whitespace-active-style)
2115 (memq 'space-after-tab::space whitespace-active-style)
2116 (memq 'space-before-tab whitespace-active-style)
2117 (memq 'space-before-tab::tab whitespace-active-style)
2118 (memq 'space-before-tab::space whitespace-active-style)))
2121 (defun whitespace-color-on ()
2122 "Turn on color visualization."
2123 (when (whitespace-style-face-p)
2124 (unless whitespace-font-lock
2125 (setq whitespace-font-lock t
2126 whitespace-font-lock-keywords
2127 (copy-sequence font-lock-keywords)))
2128 ;; turn off font lock
2129 (set (make-local-variable 'whitespace-font-lock-mode)
2130 font-lock-mode)
2131 (font-lock-mode 0)
2132 ;; add whitespace-mode color into font lock
2133 (when (memq 'spaces whitespace-active-style)
2134 (font-lock-add-keywords
2136 (list
2137 ;; Show SPACEs
2138 (list whitespace-space-regexp 1 whitespace-space t)
2139 ;; Show HARD SPACEs
2140 (list whitespace-hspace-regexp 1 whitespace-hspace t))
2142 (when (memq 'tabs whitespace-active-style)
2143 (font-lock-add-keywords
2145 (list
2146 ;; Show TABs
2147 (list whitespace-tab-regexp 1 whitespace-tab t))
2149 (when (memq 'trailing whitespace-active-style)
2150 (font-lock-add-keywords
2152 (list
2153 ;; Show trailing blanks
2154 (list whitespace-trailing-regexp 1 whitespace-trailing t))
2156 (when (or (memq 'lines whitespace-active-style)
2157 (memq 'lines-tail whitespace-active-style))
2158 (font-lock-add-keywords
2160 (list
2161 ;; Show "long" lines
2162 (list
2163 (format
2164 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2165 whitespace-tab-width (1- whitespace-tab-width)
2166 (/ whitespace-line-column tab-width)
2167 (let ((rem (% whitespace-line-column whitespace-tab-width)))
2168 (if (zerop rem)
2170 (format ".\\{%d\\}" rem))))
2171 (if (memq 'lines whitespace-active-style)
2172 0 ; whole line
2173 2) ; line tail
2174 whitespace-line t))
2176 (cond
2177 ((memq 'space-before-tab whitespace-active-style)
2178 (font-lock-add-keywords
2180 (list
2181 ;; Show SPACEs before TAB (indent-tabs-mode)
2182 (list whitespace-space-before-tab-regexp
2183 (if whitespace-indent-tabs-mode 1 2)
2184 whitespace-space-before-tab t))
2186 ((memq 'space-before-tab::tab whitespace-active-style)
2187 (font-lock-add-keywords
2189 (list
2190 ;; Show SPACEs before TAB (SPACEs)
2191 (list whitespace-space-before-tab-regexp
2192 1 whitespace-space-before-tab t))
2194 ((memq 'space-before-tab::space whitespace-active-style)
2195 (font-lock-add-keywords
2197 (list
2198 ;; Show SPACEs before TAB (TABs)
2199 (list whitespace-space-before-tab-regexp
2200 2 whitespace-space-before-tab t))
2201 t)))
2202 (cond
2203 ((memq 'indentation whitespace-active-style)
2204 (font-lock-add-keywords
2206 (list
2207 ;; Show indentation SPACEs (indent-tabs-mode)
2208 (list (whitespace-indentation-regexp)
2209 1 whitespace-indentation t))
2211 ((memq 'indentation::tab whitespace-active-style)
2212 (font-lock-add-keywords
2214 (list
2215 ;; Show indentation SPACEs (SPACEs)
2216 (list (whitespace-indentation-regexp 'tab)
2217 1 whitespace-indentation t))
2219 ((memq 'indentation::space whitespace-active-style)
2220 (font-lock-add-keywords
2222 (list
2223 ;; Show indentation SPACEs (TABs)
2224 (list (whitespace-indentation-regexp 'space)
2225 1 whitespace-indentation t))
2226 t)))
2227 (when (memq 'empty whitespace-active-style)
2228 (font-lock-add-keywords
2230 (list
2231 ;; Show empty lines at beginning of buffer
2232 (list whitespace-empty-at-bob-regexp
2233 1 whitespace-empty t))
2235 (font-lock-add-keywords
2237 (list
2238 ;; Show empty lines at end of buffer
2239 (list whitespace-empty-at-eob-regexp
2240 1 whitespace-empty t))
2242 (cond
2243 ((memq 'space-after-tab whitespace-active-style)
2244 (font-lock-add-keywords
2246 (list
2247 ;; Show SPACEs after TAB (indent-tabs-mode)
2248 (list (whitespace-space-after-tab-regexp)
2249 1 whitespace-space-after-tab t))
2251 ((memq 'space-after-tab::tab whitespace-active-style)
2252 (font-lock-add-keywords
2254 (list
2255 ;; Show SPACEs after TAB (SPACEs)
2256 (list (whitespace-space-after-tab-regexp 'tab)
2257 1 whitespace-space-after-tab t))
2259 ((memq 'space-after-tab::space whitespace-active-style)
2260 (font-lock-add-keywords
2262 (list
2263 ;; Show SPACEs after TAB (TABs)
2264 (list (whitespace-space-after-tab-regexp 'space)
2265 1 whitespace-space-after-tab t))
2266 t)))
2267 ;; now turn on font lock and highlight blanks
2268 (font-lock-mode 1)))
2271 (defun whitespace-color-off ()
2272 "Turn off color visualization."
2273 ;; turn off font lock
2274 (when (whitespace-style-face-p)
2275 (font-lock-mode 0)
2276 (when whitespace-font-lock
2277 (setq whitespace-font-lock nil
2278 font-lock-keywords whitespace-font-lock-keywords))
2279 ;; restore original font lock state
2280 (font-lock-mode whitespace-font-lock-mode)))
2283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2284 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2287 (defun whitespace-style-mark-p ()
2288 "Return t if there is some visualization via display table."
2289 (or (memq 'tab-mark whitespace-active-style)
2290 (memq 'space-mark whitespace-active-style)
2291 (memq 'newline-mark whitespace-active-style)))
2294 (defsubst whitespace-char-valid-p (char)
2295 ;; This check should be improved!!!
2296 (or (< char 256)
2297 (characterp char)))
2300 (defun whitespace-display-vector-p (vec)
2301 "Return true if every character in vector VEC can be displayed."
2302 (let ((i (length vec)))
2303 (when (> i 0)
2304 (while (and (>= (setq i (1- i)) 0)
2305 (whitespace-char-valid-p (aref vec i))))
2306 (< i 0))))
2309 (defun whitespace-display-char-on ()
2310 "Turn on character display mapping."
2311 (when (and whitespace-display-mappings
2312 (whitespace-style-mark-p))
2313 (let (vecs vec)
2314 ;; Remember whether a buffer has a local display table.
2315 (unless whitespace-display-table-was-local
2316 (setq whitespace-display-table-was-local t
2317 whitespace-display-table
2318 (copy-sequence buffer-display-table)))
2319 (unless buffer-display-table
2320 (setq buffer-display-table (make-display-table)))
2321 (dolist (entry whitespace-display-mappings)
2322 ;; check if it is to display this mark
2323 (when (memq (car entry) whitespace-style)
2324 ;; Get a displayable mapping.
2325 (setq vecs (cddr entry))
2326 (while (and vecs
2327 (not (whitespace-display-vector-p (car vecs))))
2328 (setq vecs (cdr vecs)))
2329 ;; Display a valid mapping.
2330 (when vecs
2331 (setq vec (copy-sequence (car vecs)))
2332 ;; NEWLINE char
2333 (when (and (eq (cadr entry) ?\n)
2334 (memq 'newline whitespace-active-style))
2335 ;; Only insert face bits on NEWLINE char mapping to avoid
2336 ;; obstruction of other faces like TABs and (HARD) SPACEs
2337 ;; faces, font-lock faces, etc.
2338 (dotimes (i (length vec))
2339 (or (eq (aref vec i) ?\n)
2340 (aset vec i
2341 (make-glyph-code (aref vec i)
2342 whitespace-newline)))))
2343 ;; Display mapping
2344 (aset buffer-display-table (cadr entry) vec)))))))
2347 (defun whitespace-display-char-off ()
2348 "Turn off character display mapping."
2349 (and whitespace-display-mappings
2350 (whitespace-style-mark-p)
2351 whitespace-display-table-was-local
2352 (setq whitespace-display-table-was-local nil
2353 buffer-display-table whitespace-display-table)))
2356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2357 ;;;; Hook
2360 (defun whitespace-action-when-on ()
2361 "Action to be taken always when local whitespace is turned on."
2362 (cond ((memq 'cleanup whitespace-action)
2363 (whitespace-cleanup))
2364 ((memq 'report-on-bogus whitespace-action)
2365 (whitespace-report nil t))))
2368 (defun whitespace-add-local-hook ()
2369 "Add some whitespace hooks locally."
2370 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2371 (add-hook 'kill-buffer-hook 'whitespace-kill-buffer-hook nil t))
2374 (defun whitespace-remove-local-hook ()
2375 "Remove some whitespace hooks locally."
2376 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2377 (remove-hook 'kill-buffer-hook 'whitespace-kill-buffer-hook t))
2380 (defun whitespace-write-file-hook ()
2381 "Action to be taken when buffer is written.
2382 It should be added buffer-locally to `write-file-functions'."
2383 (when (whitespace-action)
2384 (error "Abort write due to whitespace problems in %s"
2385 (buffer-name)))
2386 nil) ; continue hook processing
2389 (defun whitespace-kill-buffer-hook ()
2390 "Action to be taken when buffer is killed.
2391 It should be added buffer-locally to `kill-buffer-hook'."
2392 (whitespace-action)
2393 nil) ; continue hook processing
2396 (defun whitespace-action ()
2397 "Action to be taken when buffer is killed or written.
2398 Return t when the action should be aborted."
2399 (cond ((memq 'auto-cleanup whitespace-action)
2400 (whitespace-cleanup)
2401 nil)
2402 ((memq 'abort-on-bogus whitespace-action)
2403 (whitespace-report nil t))
2405 nil)))
2408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2411 (defun whitespace-unload-function ()
2412 "Unload the whitespace library."
2413 (global-whitespace-mode -1)
2414 ;; be sure all local whitespace mode is turned off
2415 (save-current-buffer
2416 (dolist (buf (buffer-list))
2417 (set-buffer buf)
2418 (whitespace-mode -1)))
2419 nil) ; continue standard unloading
2422 (provide 'whitespace)
2425 (run-hooks 'whitespace-load-hook)
2428 ;; arch-tag: 1b1e2500-dbd4-4a26-8f7a-5a5edfd3c97e
2429 ;;; whitespace.el ends here