]> code.delx.au - gnu-emacs/blob - lisp/whitespace.el
Merge branch 'master' into cairo
[gnu-emacs] / lisp / whitespace.el
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
2
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
4
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
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
28 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;
30 ;; Introduction
31 ;; ------------
32 ;;
33 ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
34 ;; and NEWLINE).
35 ;;
36 ;; whitespace uses two ways to visualize blanks: faces and display
37 ;; table.
38 ;;
39 ;; * Faces are used to highlight the background with a color.
40 ;; whitespace uses font-lock to highlight blank characters.
41 ;;
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).
45 ;;
46 ;; The `whitespace-style' variable selects which way blanks are
47 ;; visualized.
48 ;;
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.
57 ;;
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).
61 ;;
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.
66 ;;
67 ;; There are two ways of using whitespace: local and global.
68 ;;
69 ;; * Local whitespace affects only the current buffer.
70 ;;
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.
76 ;;
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.
81 ;;
82 ;; When global and local whitespace are on:
83 ;;
84 ;; * if local whitespace is turned off, whitespace is turned off for
85 ;; the current buffer only.
86 ;;
87 ;; * if global whitespace is turned off, whitespace continues on only
88 ;; in the buffers in which local whitespace is on.
89 ;;
90 ;; To use whitespace, insert in your ~/.emacs:
91 ;;
92 ;; (require 'whitespace)
93 ;;
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:
97 ;;
98 ;; (autoload 'whitespace-mode "whitespace"
99 ;; "Toggle whitespace visualization." t)
100 ;; (autoload 'whitespace-toggle-options "whitespace"
101 ;; "Toggle local `whitespace-mode' options." t)
102 ;;
103 ;; whitespace was inspired by:
104 ;;
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'.
111 ;;
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)
115 ;;
116 ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li>
117 ;; Major mode for editing Whitespace
118 ;; (inspired the idea to use display table)
119 ;;
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)
124 ;;
125 ;;
126 ;; Using whitespace
127 ;; ----------------
128 ;;
129 ;; There is no problem if you mix local and global minor mode usage.
130 ;;
131 ;; * LOCAL whitespace:
132 ;; + To toggle whitespace options locally, type:
133 ;;
134 ;; M-x whitespace-toggle-options RET
135 ;;
136 ;; + To activate whitespace locally, type:
137 ;;
138 ;; C-u 1 M-x whitespace-mode RET
139 ;;
140 ;; + To deactivate whitespace locally, type:
141 ;;
142 ;; C-u 0 M-x whitespace-mode RET
143 ;;
144 ;; + To toggle whitespace locally, type:
145 ;;
146 ;; M-x whitespace-mode RET
147 ;;
148 ;; * GLOBAL whitespace:
149 ;; + To toggle whitespace options globally, type:
150 ;;
151 ;; M-x global-whitespace-toggle-options RET
152 ;;
153 ;; + To activate whitespace globally, type:
154 ;;
155 ;; C-u 1 M-x global-whitespace-mode RET
156 ;;
157 ;; + To deactivate whitespace globally, type:
158 ;;
159 ;; C-u 0 M-x global-whitespace-mode RET
160 ;;
161 ;; + To toggle whitespace globally, type:
162 ;;
163 ;; M-x global-whitespace-mode RET
164 ;;
165 ;; There are also the following useful commands:
166 ;;
167 ;; `whitespace-newline-mode'
168 ;; Toggle NEWLINE minor mode visualization ("nl" on mode line).
169 ;;
170 ;; `global-whitespace-newline-mode'
171 ;; Toggle NEWLINE global minor mode visualization ("NL" on mode line).
172 ;;
173 ;; `whitespace-report'
174 ;; Report some blank problems in buffer.
175 ;;
176 ;; `whitespace-report-region'
177 ;; Report some blank problems in a region.
178 ;;
179 ;; `whitespace-cleanup'
180 ;; Cleanup some blank problems in all buffer or at region.
181 ;;
182 ;; `whitespace-cleanup-region'
183 ;; Cleanup some blank problems at region.
184 ;;
185 ;; The problems, which are cleaned up, are:
186 ;;
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.
191 ;;
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.
201 ;;
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.
210 ;;
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.
214 ;;
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.
223 ;;
224 ;;
225 ;; Hooks
226 ;; -----
227 ;;
228 ;; whitespace has the following hook variables:
229 ;;
230 ;; `whitespace-mode-hook'
231 ;; It is evaluated always when whitespace is turned on locally.
232 ;;
233 ;; `global-whitespace-mode-hook'
234 ;; It is evaluated always when whitespace is turned on globally.
235 ;;
236 ;; `whitespace-load-hook'
237 ;; It is evaluated after whitespace package is loaded.
238 ;;
239 ;;
240 ;; Options
241 ;; -------
242 ;;
243 ;; Below it's shown a brief description of whitespace options, please,
244 ;; see the options declaration in the code for a long documentation.
245 ;;
246 ;; `whitespace-style' Specify which kind of blank is
247 ;; visualized.
248 ;;
249 ;; `whitespace-space' Face used to visualize SPACE.
250 ;;
251 ;; `whitespace-hspace' Face used to visualize HARD SPACE.
252 ;;
253 ;; `whitespace-tab' Face used to visualize TAB.
254 ;;
255 ;; `whitespace-newline' Face used to visualize NEWLINE char
256 ;; mapping.
257 ;;
258 ;; `whitespace-trailing' Face used to visualize trailing
259 ;; blanks.
260 ;;
261 ;; `whitespace-line' Face used to visualize "long" lines.
262 ;;
263 ;; `whitespace-space-before-tab' Face used to visualize SPACEs
264 ;; before TAB.
265 ;;
266 ;; `whitespace-indentation' Face used to visualize 8 or more
267 ;; SPACEs at beginning of line.
268 ;;
269 ;; `whitespace-big-indent' Face used to visualize big indentation.
270 ;;
271 ;; `whitespace-empty' Face used to visualize empty lines at
272 ;; beginning and/or end of buffer.
273 ;;
274 ;; `whitespace-space-after-tab' Face used to visualize 8 or more
275 ;; SPACEs after TAB.
276 ;;
277 ;; `whitespace-space-regexp' Specify SPACE characters regexp.
278 ;;
279 ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
280 ;;
281 ;; `whitespace-tab-regexp' Specify TAB characters regexp.
282 ;;
283 ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
284 ;;
285 ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
286 ;; regexp.
287 ;;
288 ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
289 ;; SPACEs at beginning of line.
290 ;;
291 ;; `whitespace-big-indent-regexp' Specify big indentation at beginning of line
292 ;; regexp.
293 ;;
294 ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
295 ;; at beginning of buffer.
296 ;;
297 ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
298 ;; at end of buffer.
299 ;;
300 ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
301 ;; SPACEs after TAB.
302 ;;
303 ;; `whitespace-line-column' Specify column beyond which the line
304 ;; is highlighted.
305 ;;
306 ;; `whitespace-display-mappings' Specify an alist of mappings
307 ;; for displaying characters.
308 ;;
309 ;; `whitespace-global-modes' Modes for which global
310 ;; `whitespace-mode' is automagically
311 ;; turned on.
312 ;;
313 ;; `whitespace-action' Specify which action is taken when a
314 ;; buffer is visited or written.
315 ;;
316 ;;
317 ;; Acknowledgments
318 ;; ---------------
319 ;;
320 ;; Thanks to felix (EmacsWiki) for keeping highlight when switching between
321 ;; major modes on a file.
322 ;;
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.
326 ;;
327 ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
328 ;; `indent-tabs-mode' usage suggestion.
329 ;;
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.
333 ;;
334 ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
335 ;; lines tail. See EightyColumnRule (EmacsWiki).
336 ;;
337 ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
338 ;; * `define-minor-mode'.
339 ;; * `global-whitespace-*' name for global commands.
340 ;;
341 ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
342 ;;
343 ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
344 ;; suggestion.
345 ;;
346 ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
347 ;; helping to fix `find-file-hooks' reference.
348 ;;
349 ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
350 ;; indicating defface byte-compilation warnings.
351 ;;
352 ;; Thanks to Tim O'Callaghan (EmacsWiki) for the idea about highlight
353 ;; "long" lines. See EightyColumnRule (EmacsWiki).
354 ;;
355 ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
356 ;; NEWLINE character mapping.
357 ;;
358 ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
359 ;; whitespace-mode.el on XEmacs.
360 ;;
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).
363 ;;
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.
370 ;;
371 ;;
372 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373
374 ;;; code:
375
376 \f
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;;;; User Variables:
379
380
381 ;;; Interface to the command system
382
383
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)
389
390
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.
397
398 It's a list containing some or all of the following values:
399
400 face enable all visualization via faces (see below).
401
402 trailing trailing blanks are visualized via faces.
403 It has effect only if `face' (see above)
404 is present in `whitespace-style'.
405
406 tabs TABs are visualized via faces.
407 It has effect only if `face' (see above)
408 is present in `whitespace-style'.
409
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'.
414
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'.
423
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'.
433
434 newline NEWLINEs are visualized via faces.
435 It has effect only if `face' (see above)
436 is present in `whitespace-style'.
437
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'.
442
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'.
447
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'.
452
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'.
459
460 big-indent Big indentations are visualized via faces.
461 It has effect only if `face' (see above)
462 is present in `whitespace-style'.
463
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'.
468
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'.
473
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'.
480
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'.
485
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'.
490
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'.
497
498 space-mark SPACEs and HARD SPACEs are visualized via
499 display table.
500
501 tab-mark TABs are visualized via display table.
502
503 newline-mark NEWLINEs are visualized via display table.
504
505 Any other value is ignored.
506
507 If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
508 via display table.
509
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:
515
516 * For indentation:
517 1. indentation
518 2. indentation::tab
519 3. indentation::space
520
521 * For SPACEs after TABs:
522 1. space-after-tab
523 2. space-after-tab::tab
524 3. space-after-tab::space
525
526 * For SPACEs before TABs:
527 1. space-before-tab
528 2. space-before-tab::tab
529 3. space-before-tab::space
530
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.
534
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.
539
540 See also `whitespace-display-mappings' for documentation."
541 :type '(set :tag "Kind of Blank"
542 (const :tag "(Face) Face visualization" face)
543 (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
544 trailing)
545 (const :tag "(Face) TABs" tabs)
546 (const :tag "(Face) SPACEs and HARD SPACEs" spaces)
547 (const :tag "(Face) Lines" lines)
548 (const :tag "(Face) Lines, only overlong part" lines-tail)
549 (const :tag "(Face) NEWLINEs" newline)
550 (const :tag "(Face) Empty Lines At BOB And/Or EOB" empty)
551 (const :tag "(Face) Indentation SPACEs" indentation::tab)
552 (const :tag "(Face) Indentation TABs"
553 indentation::space)
554 (const :tag "(Face) Indentation TABs or SPACEs" indentation)
555 (const :tag "(Face) Too much line indentation" big-indent)
556 (const :tag "(Face) SPACEs after TAB: SPACEs"
557 space-after-tab::tab)
558 (const :tag "(Face) SPACEs after TAB: TABs"
559 space-after-tab::space)
560 (const :tag "(Face) SPACEs after TAB" space-after-tab)
561 (const :tag "(Face) SPACEs before TAB: SPACEs"
562 space-before-tab::tab)
563 (const :tag "(Face) SPACEs before TAB: TABs"
564 space-before-tab::space)
565 (const :tag "(Face) SPACEs before TAB" space-before-tab)
566 (const :tag "(Mark) SPACEs and HARD SPACEs" space-mark)
567 (const :tag "(Mark) TABs" tab-mark)
568 (const :tag "(Mark) NEWLINEs" newline-mark))
569 :group 'whitespace)
570
571 (defvar whitespace-space 'whitespace-space
572 "Symbol face used to visualize SPACE.
573 Used when `whitespace-style' includes the value `spaces'.")
574 (make-obsolete-variable 'whitespace-space "use the face instead." "24.4")
575
576
577 (defface whitespace-space
578 '((((class color) (background dark))
579 :background "grey20" :foreground "darkgray")
580 (((class color) (background light))
581 :background "LightYellow" :foreground "lightgray")
582 (t :inverse-video t))
583 "Face used to visualize SPACE."
584 :group 'whitespace)
585
586
587 (defvar whitespace-hspace 'whitespace-hspace
588 "Symbol face used to visualize HARD SPACE.
589 Used when `whitespace-style' includes the value `spaces'.")
590 (make-obsolete-variable 'whitespace-hspace "use the face instead." "24.4")
591
592 (defface whitespace-hspace ; 'nobreak-space
593 '((((class color) (background dark))
594 :background "grey24" :foreground "darkgray")
595 (((class color) (background light))
596 :background "LemonChiffon3" :foreground "lightgray")
597 (t :inverse-video t))
598 "Face used to visualize HARD SPACE."
599 :group 'whitespace)
600
601
602 (defvar whitespace-tab 'whitespace-tab
603 "Symbol face used to visualize TAB.
604 Used when `whitespace-style' includes the value `tabs'.")
605 (make-obsolete-variable 'whitespace-tab "use the face instead." "24.4")
606
607 (defface whitespace-tab
608 '((((class color) (background dark))
609 :background "grey22" :foreground "darkgray")
610 (((class color) (background light))
611 :background "beige" :foreground "lightgray")
612 (t :inverse-video t))
613 "Face used to visualize TAB."
614 :group 'whitespace)
615
616
617 (defvar whitespace-newline 'whitespace-newline
618 "Symbol face used to visualize NEWLINE char mapping.
619 See `whitespace-display-mappings'.
620 Used when `whitespace-style' includes the values `newline-mark'
621 and `newline'.")
622 (make-obsolete-variable 'whitespace-newline "use the face instead." "24.4")
623
624 (defface whitespace-newline
625 '((default :weight normal)
626 (((class color) (background dark)) :foreground "darkgray")
627 (((class color) (min-colors 88) (background light)) :foreground "lightgray")
628 ;; Displays with 16 colors use lightgray as background, so using a
629 ;; lightgray foreground makes the newline mark invisible.
630 (((class color) (background light)) :foreground "brown")
631 (t :underline t))
632 "Face used to visualize NEWLINE char mapping.
633
634 See `whitespace-display-mappings'."
635 :group 'whitespace)
636
637
638 (defvar whitespace-trailing 'whitespace-trailing
639 "Symbol face used to visualize trailing blanks.
640 Used when `whitespace-style' includes the value `trailing'.")
641 (make-obsolete-variable 'whitespace-trailing "use the face instead." "24.4")
642
643 (defface whitespace-trailing ; 'trailing-whitespace
644 '((default :weight bold)
645 (((class mono)) :inverse-video t :underline t)
646 (t :background "red1" :foreground "yellow"))
647 "Face used to visualize trailing blanks."
648 :group 'whitespace)
649
650
651 (defvar whitespace-line 'whitespace-line
652 "Symbol face used to visualize \"long\" lines.
653 See `whitespace-line-column'.
654 Used when `whitespace-style' includes the value `line'.")
655 (make-obsolete-variable 'whitespace-line "use the face instead." "24.4")
656
657 (defface whitespace-line
658 '((((class mono)) :inverse-video t :weight bold :underline t)
659 (t :background "gray20" :foreground "violet"))
660 "Face used to visualize \"long\" lines.
661
662 See `whitespace-line-column'."
663 :group 'whitespace)
664
665
666 (defvar whitespace-space-before-tab 'whitespace-space-before-tab
667 "Symbol face used to visualize SPACEs before TAB.
668 Used when `whitespace-style' includes the value `space-before-tab'.")
669 (make-obsolete-variable 'whitespace-space-before-tab
670 "use the face instead." "24.4")
671
672 (defface whitespace-space-before-tab
673 '((((class mono)) :inverse-video t :weight bold :underline t)
674 (t :background "DarkOrange" :foreground "firebrick"))
675 "Face used to visualize SPACEs before TAB."
676 :group 'whitespace)
677
678
679 (defvar whitespace-indentation 'whitespace-indentation
680 "Symbol face used to visualize 8 or more SPACEs at beginning of line.
681 Used when `whitespace-style' includes the value `indentation'.")
682 (make-obsolete-variable 'whitespace-indentation "use the face instead." "24.4")
683
684 (defface whitespace-indentation
685 '((((class mono)) :inverse-video t :weight bold :underline t)
686 (t :background "yellow" :foreground "firebrick"))
687 "Face used to visualize 8 or more SPACEs at beginning of line."
688 :group 'whitespace)
689
690 (defface whitespace-big-indent
691 '((((class mono)) :inverse-video t :weight bold :underline t)
692 (t :background "red" :foreground "firebrick"))
693 "Face used to visualize big indentation."
694 :group 'whitespace)
695
696
697 (defvar whitespace-empty 'whitespace-empty
698 "Symbol face used to visualize empty lines at beginning and/or end of buffer.
699 Used when `whitespace-style' includes the value `empty'.")
700 (make-obsolete-variable 'whitespace-empty "use the face instead." "24.4")
701
702 (defface whitespace-empty
703 '((((class mono)) :inverse-video t :weight bold :underline t)
704 (t :background "yellow" :foreground "firebrick"))
705 "Face used to visualize empty lines at beginning and/or end of buffer."
706 :group 'whitespace)
707
708
709 (defvar whitespace-space-after-tab 'whitespace-space-after-tab
710 "Symbol face used to visualize 8 or more SPACEs after TAB.
711 Used when `whitespace-style' includes the value `space-after-tab'.")
712 (make-obsolete-variable 'whitespace-space-after-tab
713 "use the face instead." "24.4")
714
715 (defface whitespace-space-after-tab
716 '((((class mono)) :inverse-video t :weight bold :underline t)
717 (t :background "yellow" :foreground "firebrick"))
718 "Face used to visualize 8 or more SPACEs after TAB."
719 :group 'whitespace)
720
721
722 (defcustom whitespace-hspace-regexp
723 "\\(\u00A0+\\)"
724 "Specify HARD SPACE characters regexp.
725
726 Here are some examples:
727
728 \"\\\\(^\\xA0+\\\\)\" \
729 visualize only leading HARD SPACEs.
730 \"\\\\(\\xA0+$\\\\)\" \
731 visualize only trailing HARD SPACEs.
732 \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
733 visualize leading and/or trailing HARD SPACEs.
734 \"\\t\\\\(\\xA0+\\\\)\\t\" \
735 visualize only HARD SPACEs between TABs.
736
737 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
738 Use exactly one pair of enclosing \\\\( and \\\\).
739
740 Used when `whitespace-style' includes `spaces'."
741 :type '(regexp :tag "HARD SPACE Chars")
742 :group 'whitespace)
743
744
745 (defcustom whitespace-space-regexp "\\( +\\)"
746 "Specify SPACE characters regexp.
747
748 If you're using `mule' package, there may be other characters
749 besides \" \" that should be considered SPACE.
750
751 Here are some examples:
752
753 \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
754 \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
755 \"\\\\(^ +\\\\| +$\\\\)\" \
756 visualize leading and/or trailing SPACEs.
757 \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
758
759 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
760 Use exactly one pair of enclosing \\\\( and \\\\).
761
762 Used when `whitespace-style' includes `spaces'."
763 :type '(regexp :tag "SPACE Chars")
764 :group 'whitespace)
765
766
767 (defcustom whitespace-tab-regexp "\\(\t+\\)"
768 "Specify TAB characters regexp.
769
770 If you're using `mule' package, there may be other characters
771 besides \"\\t\" that should be considered TAB.
772
773 Here are some examples:
774
775 \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
776 \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
777 \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
778 visualize leading and/or trailing TABs.
779 \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
780
781 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
782 Use exactly one pair of enclosing \\\\( and \\\\).
783
784 Used when `whitespace-style' includes `tabs'."
785 :type '(regexp :tag "TAB Chars")
786 :group 'whitespace)
787
788
789 (defcustom whitespace-trailing-regexp
790 "\\([\t \u00A0]+\\)$"
791 "Specify trailing characters regexp.
792
793 There may be other characters besides:
794
795 \" \" \"\\t\" \"\\u00A0\"
796
797 that should be considered blank.
798
799 NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
800 Use exactly one pair of enclosing elements above.
801
802 Used when `whitespace-style' includes `trailing'."
803 :type '(regexp :tag "Trailing Chars")
804 :group 'whitespace)
805
806
807 (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
808 "Specify SPACEs before TAB regexp.
809
810 Used when `whitespace-style' includes `space-before-tab',
811 `space-before-tab::tab' or `space-before-tab::space'."
812 :type '(regexp :tag "SPACEs Before TAB")
813 :group 'whitespace)
814
815
816 (defcustom whitespace-indentation-regexp
817 '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
818 . "^ *\\(\t+\\)[^\n]")
819 "Specify regexp for 8 or more SPACEs at beginning of line.
820
821 It is a cons where the cons car is used for SPACEs visualization
822 and the cons cdr is used for TABs visualization.
823
824 Used when `whitespace-style' includes `indentation',
825 `indentation::tab' or `indentation::space'."
826 :type '(cons (string :tag "Indentation SPACEs")
827 (string :tag "Indentation TABs"))
828 :group 'whitespace)
829
830
831 (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
832 "Specify regexp for empty lines at beginning of buffer.
833
834 Used when `whitespace-style' includes `empty'."
835 :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
836 :group 'whitespace)
837
838
839 (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
840 "Specify regexp for empty lines at end of buffer.
841
842 Used when `whitespace-style' includes `empty'."
843 :type '(regexp :tag "Empty Lines At End Of Buffer")
844 :group 'whitespace)
845
846
847 (defcustom whitespace-space-after-tab-regexp
848 '("\t+\\(\\( \\{%d\\}\\)+\\)"
849 . "\\(\t+\\) +")
850 "Specify regexp for 8 or more SPACEs after TAB.
851
852 It is a cons where the cons car is used for SPACEs visualization
853 and the cons cdr is used for TABs visualization.
854
855 Used when `whitespace-style' includes `space-after-tab',
856 `space-after-tab::tab' or `space-after-tab::space'."
857 :type '(cons (string :tag "SPACEs After TAB")
858 string)
859 :group 'whitespace)
860
861 (defcustom whitespace-big-indent-regexp
862 "^\\(\\(?:\t\\{4,\\}\\| \\{32,\\}\\)[\t ]*\\)"
863 "Specify big indentation regexp.
864
865 If you're using `mule' package, there may be other characters
866 besides \"\\t\" that should be considered TAB.
867
868 NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
869 Use exactly one pair of enclosing \\\\( and \\\\).
870
871 Used when `whitespace-style' includes `big-indent'."
872 :version "25.1"
873 :type '(regexp :tag "Detect too much indentation at the beginning of a line")
874 :group 'whitespace)
875
876
877 (defcustom whitespace-line-column 80
878 "Specify column beyond which the line is highlighted.
879
880 It must be an integer or nil. If nil, the `fill-column' variable value is
881 used.
882
883 Used when `whitespace-style' includes `lines' or `lines-tail'."
884 :type '(choice :tag "Line Length Limit"
885 (integer :tag "Line Length")
886 (const :tag "Use fill-column" nil))
887 :group 'whitespace)
888
889
890 ;; Hacked from `visible-whitespace-mappings' in visws.el
891 (defcustom whitespace-display-mappings
892 '(
893 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
894 (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
895 ;; NEWLINE is displayed using the face `whitespace-newline'
896 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
897 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
898 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
899 ;; (newline-mark ?\n [?\u00AF ?\n] [?$ ?\n]) ; eol - overscore
900 ;; (newline-mark ?\n [?\u00AC ?\n] [?$ ?\n]) ; eol - negation
901 ;; (newline-mark ?\n [?\u00B0 ?\n] [?$ ?\n]) ; eol - degrees
902 ;;
903 ;; WARNING: the mapping below has a problem.
904 ;; When a TAB occupies exactly one column, it will display the
905 ;; character ?\xBB at that column followed by a TAB which goes to
906 ;; the next TAB column.
907 ;; If this is a problem for you, please, comment the line below.
908 (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
909 )
910 "Specify an alist of mappings for displaying characters.
911
912 Each element has the following form:
913
914 (KIND CHAR VECTOR...)
915
916 Where:
917
918 KIND is the kind of character.
919 It can be one of the following symbols:
920
921 tab-mark for TAB character
922
923 space-mark for SPACE or HARD SPACE character
924
925 newline-mark for NEWLINE character
926
927 CHAR is the character to be mapped.
928
929 VECTOR is a vector of characters to be displayed in place of CHAR.
930 The first display vector that can be displayed is used;
931 if no display vector for a mapping can be displayed, then
932 that character is displayed unmodified.
933
934 The NEWLINE character is displayed using the face given by
935 `whitespace-newline' variable.
936
937 Used when `whitespace-style' includes `tab-mark', `space-mark' or
938 `newline-mark'."
939 :type '(repeat
940 (list :tag "Character Mapping"
941 (choice :tag "Char Kind"
942 (const :tag "Tab" tab-mark)
943 (const :tag "Space" space-mark)
944 (const :tag "Newline" newline-mark))
945 (character :tag "Char")
946 (repeat :inline t :tag "Vector List"
947 (vector :tag ""
948 (repeat :inline t
949 :tag "Vector Characters"
950 (character :tag "Char"))))))
951 :group 'whitespace)
952
953
954 (defcustom whitespace-global-modes t
955 "Modes for which global `whitespace-mode' is automagically turned on.
956
957 Global `whitespace-mode' is controlled by the command
958 `global-whitespace-mode'.
959
960 If nil, means no modes have `whitespace-mode' automatically
961 turned on.
962
963 If t, all modes that support `whitespace-mode' have it
964 automatically turned on.
965
966 Else it should be a list of `major-mode' symbol names for which
967 `whitespace-mode' should be automatically turned on. The sense
968 of the list is negated if it begins with `not'. For example:
969
970 (c-mode c++-mode)
971
972 means that `whitespace-mode' is turned on for buffers in C and
973 C++ modes only."
974 :type '(choice :tag "Global Modes"
975 (const :tag "None" nil)
976 (const :tag "All" t)
977 (set :menu-tag "Mode Specific" :tag "Modes"
978 :value (not)
979 (const :tag "Except" not)
980 (repeat :inline t
981 (symbol :tag "Mode"))))
982 :group 'whitespace)
983
984
985 (defcustom whitespace-action nil
986 "Specify which action is taken when a buffer is visited or written.
987
988 It's a list containing some or all of the following values:
989
990 nil no action is taken.
991
992 cleanup cleanup any bogus whitespace always when local
993 whitespace is turned on.
994 See `whitespace-cleanup' and
995 `whitespace-cleanup-region'.
996
997 report-on-bogus report if there is any bogus whitespace always
998 when local whitespace is turned on.
999
1000 auto-cleanup cleanup any bogus whitespace when buffer is
1001 written.
1002 See `whitespace-cleanup' and
1003 `whitespace-cleanup-region'.
1004
1005 abort-on-bogus abort if there is any bogus whitespace and the
1006 buffer is written.
1007
1008 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
1009 is included in `whitespace-action' and the
1010 buffer is read-only.
1011
1012 Any other value is treated as nil."
1013 :type '(choice :tag "Actions"
1014 (const :tag "None" nil)
1015 (repeat :tag "Action List"
1016 (choice :tag "Action"
1017 (const :tag "Cleanup When On" cleanup)
1018 (const :tag "Report On Bogus" report-on-bogus)
1019 (const :tag "Auto Cleanup" auto-cleanup)
1020 (const :tag "Abort On Bogus" abort-on-bogus)
1021 (const :tag "Warn If Read-Only" warn-if-read-only))))
1022 :group 'whitespace)
1023
1024 \f
1025 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1026 ;;;; User commands - Local mode
1027
1028
1029 ;;;###autoload
1030 (define-minor-mode whitespace-mode
1031 "Toggle whitespace visualization (Whitespace mode).
1032 With a prefix argument ARG, enable Whitespace mode if ARG is
1033 positive, and disable it otherwise. If called from Lisp, enable
1034 the mode if ARG is omitted or nil.
1035
1036 See also `whitespace-style', `whitespace-newline' and
1037 `whitespace-display-mappings'."
1038 :lighter " ws"
1039 :init-value nil
1040 :global nil
1041 :group 'whitespace
1042 (cond
1043 (noninteractive ; running a batch job
1044 (setq whitespace-mode nil))
1045 (whitespace-mode ; whitespace-mode on
1046 (whitespace-turn-on)
1047 (whitespace-action-when-on))
1048 (t ; whitespace-mode off
1049 (whitespace-turn-off))))
1050
1051
1052 ;;;###autoload
1053 (define-minor-mode whitespace-newline-mode
1054 "Toggle newline visualization (Whitespace Newline mode).
1055 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1056 is positive, and disable it otherwise. If called from Lisp,
1057 enable the mode if ARG is omitted or nil.
1058
1059 Use `whitespace-newline-mode' only for NEWLINE visualization
1060 exclusively. For other visualizations, including NEWLINE
1061 visualization together with (HARD) SPACEs and/or TABs, please,
1062 use `whitespace-mode'.
1063
1064 See also `whitespace-newline' and `whitespace-display-mappings'."
1065 :lighter " nl"
1066 :init-value nil
1067 :global nil
1068 :group 'whitespace
1069 (let ((whitespace-style '(face newline-mark newline)))
1070 (whitespace-mode (if whitespace-newline-mode
1071 1 -1)))
1072 ;; sync states (running a batch job)
1073 (setq whitespace-newline-mode whitespace-mode))
1074
1075 \f
1076 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1077 ;;;; User commands - Global mode
1078
1079
1080 ;;;###autoload
1081 (define-minor-mode global-whitespace-mode
1082 "Toggle whitespace visualization globally (Global Whitespace mode).
1083 With a prefix argument ARG, enable Global Whitespace mode if ARG
1084 is positive, and disable it otherwise. If called from Lisp,
1085 enable it if ARG is omitted or nil.
1086
1087 See also `whitespace-style', `whitespace-newline' and
1088 `whitespace-display-mappings'."
1089 :lighter " WS"
1090 :init-value nil
1091 :global t
1092 :group 'whitespace
1093 (cond
1094 (noninteractive ; running a batch job
1095 (setq global-whitespace-mode nil))
1096 (global-whitespace-mode ; global-whitespace-mode on
1097 (save-current-buffer
1098 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1099 (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1100 (dolist (buffer (buffer-list)) ; adjust all local mode
1101 (set-buffer buffer)
1102 (unless whitespace-mode
1103 (whitespace-turn-on-if-enabled)))))
1104 (t ; global-whitespace-mode off
1105 (save-current-buffer
1106 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1107 (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1108 (dolist (buffer (buffer-list)) ; adjust all local mode
1109 (set-buffer buffer)
1110 (unless whitespace-mode
1111 (whitespace-turn-off)))))))
1112
1113 (defvar whitespace-enable-predicate
1114 (lambda ()
1115 (and (cond
1116 ((eq whitespace-global-modes t))
1117 ((listp whitespace-global-modes)
1118 (if (eq (car-safe whitespace-global-modes) 'not)
1119 (not (memq major-mode (cdr whitespace-global-modes)))
1120 (memq major-mode whitespace-global-modes)))
1121 (t nil))
1122 ;; ...we have a display (not running a batch job)
1123 (not noninteractive)
1124 ;; ...the buffer is not internal (name starts with a space)
1125 (not (eq (aref (buffer-name) 0) ?\ ))
1126 ;; ...the buffer is not special (name starts with *)
1127 (or (not (eq (aref (buffer-name) 0) ?*))
1128 ;; except the scratch buffer.
1129 (string= (buffer-name) "*scratch*"))))
1130 "Predicate to decide which buffers obey `global-whitespace-mode'.
1131 This function is called with no argument and should return non-nil
1132 if the current buffer should obey `global-whitespace-mode'.
1133 This variable is normally modified via `add-function'.")
1134
1135 (defun whitespace-turn-on-if-enabled ()
1136 (when (funcall whitespace-enable-predicate)
1137 (whitespace-turn-on)))
1138
1139 ;;;###autoload
1140 (define-minor-mode global-whitespace-newline-mode
1141 "Toggle global newline visualization (Global Whitespace Newline mode).
1142 With a prefix argument ARG, enable Global Whitespace Newline mode
1143 if ARG is positive, and disable it otherwise. If called from
1144 Lisp, enable it if ARG is omitted or nil.
1145
1146 Use `global-whitespace-newline-mode' only for NEWLINE
1147 visualization exclusively. For other visualizations, including
1148 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1149 please use `global-whitespace-mode'.
1150
1151 See also `whitespace-newline' and `whitespace-display-mappings'."
1152 :lighter " NL"
1153 :init-value nil
1154 :global t
1155 :group 'whitespace
1156 (let ((whitespace-style '(newline-mark newline)))
1157 (global-whitespace-mode (if global-whitespace-newline-mode
1158 1 -1))
1159 ;; sync states (running a batch job)
1160 (setq global-whitespace-newline-mode global-whitespace-mode)))
1161
1162 \f
1163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1164 ;;;; User commands - Toggle
1165
1166
1167 (defconst whitespace-style-value-list
1168 '(face
1169 tabs
1170 spaces
1171 trailing
1172 lines
1173 lines-tail
1174 newline
1175 empty
1176 indentation
1177 indentation::tab
1178 indentation::space
1179 big-indent
1180 space-after-tab
1181 space-after-tab::tab
1182 space-after-tab::space
1183 space-before-tab
1184 space-before-tab::tab
1185 space-before-tab::space
1186 help-newline ; value used by `whitespace-insert-option-mark'
1187 tab-mark
1188 space-mark
1189 newline-mark
1190 )
1191 "List of valid `whitespace-style' values.")
1192
1193
1194 (defconst whitespace-toggle-option-alist
1195 '((?f . face)
1196 (?t . tabs)
1197 (?s . spaces)
1198 (?r . trailing)
1199 (?l . lines)
1200 (?L . lines-tail)
1201 (?n . newline)
1202 (?e . empty)
1203 (?\C-i . indentation)
1204 (?I . indentation::tab)
1205 (?i . indentation::space)
1206 (?\C-t . big-indent)
1207 (?\C-a . space-after-tab)
1208 (?A . space-after-tab::tab)
1209 (?a . space-after-tab::space)
1210 (?\C-b . space-before-tab)
1211 (?B . space-before-tab::tab)
1212 (?b . space-before-tab::space)
1213 (?T . tab-mark)
1214 (?S . space-mark)
1215 (?N . newline-mark)
1216 (?x . whitespace-style)
1217 )
1218 "Alist of toggle options.
1219
1220 Each element has the form:
1221
1222 (CHAR . SYMBOL)
1223
1224 Where:
1225
1226 CHAR is a char which the user will have to type.
1227
1228 SYMBOL is a valid symbol associated with CHAR.
1229 See `whitespace-style-value-list'.")
1230
1231
1232 (defvar whitespace-active-style nil
1233 "Used to save locally `whitespace-style' value.")
1234
1235 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1236 "Used to save locally `indent-tabs-mode' value.")
1237
1238 (defvar whitespace-tab-width tab-width
1239 "Used to save locally `tab-width' value.")
1240
1241 (defvar whitespace-point (point)
1242 "Used to save locally current point value.
1243 Used by function `whitespace-trailing-regexp' (which see).")
1244 (defvar-local whitespace-point--used nil
1245 "Region whose highlighting depends on `whitespace-point'.")
1246
1247 (defvar whitespace-font-lock-refontify nil
1248 "Used to save locally the font-lock refontify state.
1249 Used by function `whitespace-post-command-hook' (which see).")
1250
1251 (defvar whitespace-bob-marker nil
1252 "Used to save locally the bob marker value.
1253 Used by function `whitespace-post-command-hook' (which see).")
1254
1255 (defvar whitespace-eob-marker nil
1256 "Used to save locally the eob marker value.
1257 Used by function `whitespace-post-command-hook' (which see).")
1258
1259 (defvar whitespace-buffer-changed nil
1260 "Used to indicate locally if buffer changed.
1261 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1262 functions (which see).")
1263
1264
1265 ;;;###autoload
1266 (defun whitespace-toggle-options (arg)
1267 "Toggle local `whitespace-mode' options.
1268
1269 If local whitespace-mode is off, toggle the option given by ARG
1270 and turn on local whitespace-mode.
1271
1272 If local whitespace-mode is on, toggle the option given by ARG
1273 and restart local whitespace-mode.
1274
1275 Interactively, it reads one of the following chars:
1276
1277 CHAR MEANING
1278 (VIA FACES)
1279 f toggle face visualization
1280 t toggle TAB visualization
1281 s toggle SPACE and HARD SPACE visualization
1282 r toggle trailing blanks visualization
1283 l toggle \"long lines\" visualization
1284 L toggle \"long lines\" tail visualization
1285 n toggle NEWLINE visualization
1286 e toggle empty line at bob and/or eob visualization
1287 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1288 I toggle indentation SPACEs visualization
1289 i toggle indentation TABs visualization
1290 C-t toggle big indentation visualization
1291 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1292 A toggle SPACEs after TAB: SPACEs visualization
1293 a toggle SPACEs after TAB: TABs visualization
1294 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1295 B toggle SPACEs before TAB: SPACEs visualization
1296 b toggle SPACEs before TAB: TABs visualization
1297
1298 (VIA DISPLAY TABLE)
1299 T toggle TAB visualization
1300 S toggle SPACEs before TAB visualization
1301 N toggle NEWLINE visualization
1302
1303 x restore `whitespace-style' value
1304 ? display brief help
1305
1306 Non-interactively, ARG should be a symbol or a list of symbols.
1307 The valid symbols are:
1308
1309 face toggle face visualization
1310 tabs toggle TAB visualization
1311 spaces toggle SPACE and HARD SPACE visualization
1312 trailing toggle trailing blanks visualization
1313 lines toggle \"long lines\" visualization
1314 lines-tail toggle \"long lines\" tail visualization
1315 newline toggle NEWLINE visualization
1316 empty toggle empty line at bob and/or eob visualization
1317 indentation toggle indentation SPACEs visualization
1318 indentation::tab toggle indentation SPACEs visualization
1319 indentation::space toggle indentation TABs visualization
1320 big-indent toggle big indentation visualization
1321 space-after-tab toggle SPACEs after TAB visualization
1322 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1323 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1324 space-before-tab toggle SPACEs before TAB visualization
1325 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1326 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1327
1328 tab-mark toggle TAB visualization
1329 space-mark toggle SPACEs before TAB visualization
1330 newline-mark toggle NEWLINE visualization
1331
1332 whitespace-style restore `whitespace-style' value
1333
1334 See `whitespace-style' and `indent-tabs-mode' for documentation."
1335 (interactive (whitespace-interactive-char t))
1336 (let ((whitespace-style
1337 (whitespace-toggle-list t arg whitespace-active-style)))
1338 (whitespace-mode 0)
1339 (whitespace-mode 1)))
1340
1341
1342 (defvar whitespace-toggle-style nil
1343 "Used to toggle the global `whitespace-style' value.")
1344
1345
1346 ;;;###autoload
1347 (defun global-whitespace-toggle-options (arg)
1348 "Toggle global `whitespace-mode' options.
1349
1350 If global whitespace-mode is off, toggle the option given by ARG
1351 and turn on global whitespace-mode.
1352
1353 If global whitespace-mode is on, toggle the option given by ARG
1354 and restart global whitespace-mode.
1355
1356 Interactively, it accepts one of the following chars:
1357
1358 CHAR MEANING
1359 (VIA FACES)
1360 f toggle face visualization
1361 t toggle TAB visualization
1362 s toggle SPACE and HARD SPACE visualization
1363 r toggle trailing blanks visualization
1364 l toggle \"long lines\" visualization
1365 L toggle \"long lines\" tail visualization
1366 n toggle NEWLINE visualization
1367 e toggle empty line at bob and/or eob visualization
1368 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1369 I toggle indentation SPACEs visualization
1370 i toggle indentation TABs visualization
1371 C-t toggle big indentation visualization
1372 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1373 A toggle SPACEs after TAB: SPACEs visualization
1374 a toggle SPACEs after TAB: TABs visualization
1375 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1376 B toggle SPACEs before TAB: SPACEs visualization
1377 b toggle SPACEs before TAB: TABs visualization
1378
1379 (VIA DISPLAY TABLE)
1380 T toggle TAB visualization
1381 S toggle SPACEs before TAB visualization
1382 N toggle NEWLINE visualization
1383
1384 x restore `whitespace-style' value
1385 ? display brief help
1386
1387 Non-interactively, ARG should be a symbol or a list of symbols.
1388 The valid symbols are:
1389
1390 face toggle face visualization
1391 tabs toggle TAB visualization
1392 spaces toggle SPACE and HARD SPACE visualization
1393 trailing toggle trailing blanks visualization
1394 lines toggle \"long lines\" visualization
1395 lines-tail toggle \"long lines\" tail visualization
1396 newline toggle NEWLINE visualization
1397 empty toggle empty line at bob and/or eob visualization
1398 indentation toggle indentation SPACEs visualization
1399 indentation::tab toggle indentation SPACEs visualization
1400 indentation::space toggle indentation TABs visualization
1401 big-indent toggle big indentation visualization
1402 space-after-tab toggle SPACEs after TAB visualization
1403 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1404 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1405 space-before-tab toggle SPACEs before TAB visualization
1406 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1407 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1408
1409 tab-mark toggle TAB visualization
1410 space-mark toggle SPACEs before TAB visualization
1411 newline-mark toggle NEWLINE visualization
1412
1413 whitespace-style restore `whitespace-style' value
1414
1415 See `whitespace-style' and `indent-tabs-mode' for documentation."
1416 (interactive (whitespace-interactive-char nil))
1417 (let ((whitespace-style
1418 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1419 (setq whitespace-toggle-style whitespace-style)
1420 (global-whitespace-mode 0)
1421 (global-whitespace-mode 1)))
1422
1423 \f
1424 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1425 ;;;; User commands - Cleanup
1426
1427
1428 ;;;###autoload
1429 (defun whitespace-cleanup ()
1430 "Cleanup some blank problems in all buffer or at region.
1431
1432 It usually applies to the whole buffer, but in transient mark
1433 mode when the mark is active, it applies to the region. It also
1434 applies to the region when it is not in transient mark mode, the
1435 mark is active and \\[universal-argument] was pressed just before
1436 calling `whitespace-cleanup' interactively.
1437
1438 See also `whitespace-cleanup-region'.
1439
1440 The problems cleaned up are:
1441
1442 1. empty lines at beginning of buffer.
1443 2. empty lines at end of buffer.
1444 If `whitespace-style' includes the value `empty', remove all
1445 empty lines at beginning and/or end of buffer.
1446
1447 3. 8 or more SPACEs at beginning of line.
1448 If `whitespace-style' includes the value `indentation':
1449 replace 8 or more SPACEs at beginning of line by TABs, if
1450 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1451 SPACEs.
1452 If `whitespace-style' includes the value `indentation::tab',
1453 replace 8 or more SPACEs at beginning of line by TABs.
1454 If `whitespace-style' includes the value `indentation::space',
1455 replace TABs by SPACEs.
1456
1457 4. SPACEs before TAB.
1458 If `whitespace-style' includes the value `space-before-tab':
1459 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1460 otherwise, replace TABs by SPACEs.
1461 If `whitespace-style' includes the value
1462 `space-before-tab::tab', replace SPACEs by TABs.
1463 If `whitespace-style' includes the value
1464 `space-before-tab::space', replace TABs by SPACEs.
1465
1466 5. SPACEs or TABs at end of line.
1467 If `whitespace-style' includes the value `trailing', remove
1468 all SPACEs or TABs at end of line.
1469
1470 6. 8 or more SPACEs after TAB.
1471 If `whitespace-style' includes the value `space-after-tab':
1472 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1473 otherwise, replace TABs by SPACEs.
1474 If `whitespace-style' includes the value
1475 `space-after-tab::tab', replace SPACEs by TABs.
1476 If `whitespace-style' includes the value
1477 `space-after-tab::space', replace TABs by SPACEs.
1478
1479 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1480 documentation."
1481 (interactive "@")
1482 (cond
1483 ;; read-only buffer
1484 (buffer-read-only
1485 (whitespace-warn-read-only "cleanup"))
1486 ;; region active
1487 ((and (or transient-mark-mode
1488 current-prefix-arg)
1489 mark-active)
1490 ;; PROBLEMs 1 and 2 are not handled in region
1491 ;; PROBLEM 3: 8 or more SPACEs at bol
1492 ;; PROBLEM 4: SPACEs before TAB
1493 ;; PROBLEM 5: SPACEs or TABs at eol
1494 ;; PROBLEM 6: 8 or more SPACEs after TAB
1495 (whitespace-cleanup-region (region-beginning) (region-end)))
1496 ;; whole buffer
1497 (t
1498 (save-excursion
1499 (save-match-data ;FIXME: Why?
1500 ;; PROBLEM 1: empty lines at bob
1501 ;; PROBLEM 2: empty lines at eob
1502 ;; ACTION: remove all empty lines at bob and/or eob
1503 (when (memq 'empty whitespace-style)
1504 (let (overwrite-mode) ; enforce no overwrite
1505 (goto-char (point-min))
1506 (when (looking-at whitespace-empty-at-bob-regexp)
1507 (delete-region (match-beginning 1) (match-end 1)))
1508 (when (re-search-forward
1509 (concat whitespace-empty-at-eob-regexp "\\'") nil t)
1510 (delete-region (match-beginning 1) (match-end 1)))))))
1511 ;; PROBLEM 3: 8 or more SPACEs at bol
1512 ;; PROBLEM 4: SPACEs before TAB
1513 ;; PROBLEM 5: SPACEs or TABs at eol
1514 ;; PROBLEM 6: 8 or more SPACEs after TAB
1515 (whitespace-cleanup-region (point-min) (point-max)))))
1516
1517 (defun whitespace-ensure-local-variables ()
1518 "Set `whitespace-indent-tabs-mode' and `whitespace-tab-width' locally."
1519 (set (make-local-variable 'whitespace-indent-tabs-mode)
1520 indent-tabs-mode)
1521 (set (make-local-variable 'whitespace-tab-width)
1522 tab-width))
1523
1524 ;;;###autoload
1525 (defun whitespace-cleanup-region (start end)
1526 "Cleanup some blank problems at region.
1527
1528 The problems cleaned up are:
1529
1530 1. 8 or more SPACEs at beginning of line.
1531 If `whitespace-style' includes the value `indentation':
1532 replace 8 or more SPACEs at beginning of line by TABs, if
1533 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1534 SPACEs.
1535 If `whitespace-style' includes the value `indentation::tab',
1536 replace 8 or more SPACEs at beginning of line by TABs.
1537 If `whitespace-style' includes the value `indentation::space',
1538 replace TABs by SPACEs.
1539
1540 2. SPACEs before TAB.
1541 If `whitespace-style' includes the value `space-before-tab':
1542 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1543 otherwise, replace TABs by SPACEs.
1544 If `whitespace-style' includes the value
1545 `space-before-tab::tab', replace SPACEs by TABs.
1546 If `whitespace-style' includes the value
1547 `space-before-tab::space', replace TABs by SPACEs.
1548
1549 3. SPACEs or TABs at end of line.
1550 If `whitespace-style' includes the value `trailing', remove
1551 all SPACEs or TABs at end of line.
1552
1553 4. 8 or more SPACEs after TAB.
1554 If `whitespace-style' includes the value `space-after-tab':
1555 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1556 otherwise, replace TABs by SPACEs.
1557 If `whitespace-style' includes the value
1558 `space-after-tab::tab', replace SPACEs by TABs.
1559 If `whitespace-style' includes the value
1560 `space-after-tab::space', replace TABs by SPACEs.
1561
1562 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1563 documentation."
1564 (interactive "@r")
1565 (if buffer-read-only
1566 ;; read-only buffer
1567 (whitespace-warn-read-only "cleanup region")
1568 ;; non-read-only buffer
1569 (whitespace-ensure-local-variables)
1570 (let ((rstart (min start end))
1571 (rend (copy-marker (max start end)))
1572 (indent-tabs-mode whitespace-indent-tabs-mode)
1573 (tab-width whitespace-tab-width)
1574 overwrite-mode ; enforce no overwrite
1575 tmp)
1576 (save-excursion
1577 (save-match-data ;FIXME: Why?
1578 ;; PROBLEM 1: 8 or more SPACEs at bol
1579 (cond
1580 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1581 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1582 ;; by SPACEs.
1583 ((memq 'indentation whitespace-style)
1584 (let ((regexp (whitespace-indentation-regexp)))
1585 (goto-char rstart)
1586 (while (re-search-forward regexp rend t)
1587 (setq tmp (current-indentation))
1588 (goto-char (match-beginning 0))
1589 (delete-horizontal-space)
1590 (unless (eolp)
1591 (indent-to tmp)))))
1592 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1593 ((memq 'indentation::tab whitespace-style)
1594 (whitespace-replace-action
1595 'tabify rstart rend
1596 (whitespace-indentation-regexp 'tab) 0))
1597 ;; ACTION: replace TABs by SPACEs.
1598 ((memq 'indentation::space whitespace-style)
1599 (whitespace-replace-action
1600 'untabify rstart rend
1601 (whitespace-indentation-regexp 'space) 0)))
1602 ;; PROBLEM 3: SPACEs or TABs at eol
1603 ;; ACTION: remove all SPACEs or TABs at eol
1604 (when (memq 'trailing whitespace-style)
1605 (whitespace-replace-action
1606 'delete-region rstart rend
1607 whitespace-trailing-regexp 1))
1608 ;; PROBLEM 4: 8 or more SPACEs after TAB
1609 (cond
1610 ;; ACTION: replace 8 or more SPACEs by TABs, if
1611 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1612 ;; by SPACEs.
1613 ((memq 'space-after-tab whitespace-style)
1614 (whitespace-replace-action
1615 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1616 rstart rend (whitespace-space-after-tab-regexp) 1))
1617 ;; ACTION: replace 8 or more SPACEs by TABs.
1618 ((memq 'space-after-tab::tab whitespace-style)
1619 (whitespace-replace-action
1620 'tabify rstart rend
1621 (whitespace-space-after-tab-regexp 'tab) 1))
1622 ;; ACTION: replace TABs by SPACEs.
1623 ((memq 'space-after-tab::space whitespace-style)
1624 (whitespace-replace-action
1625 'untabify rstart rend
1626 (whitespace-space-after-tab-regexp 'space) 1)))
1627 ;; PROBLEM 2: SPACEs before TAB
1628 (cond
1629 ;; ACTION: replace SPACEs before TAB by TABs, if
1630 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1631 ;; by SPACEs.
1632 ((memq 'space-before-tab whitespace-style)
1633 (whitespace-replace-action
1634 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1635 rstart rend whitespace-space-before-tab-regexp
1636 (if whitespace-indent-tabs-mode 0 2)))
1637 ;; ACTION: replace SPACEs before TAB by TABs.
1638 ((memq 'space-before-tab::tab whitespace-style)
1639 (whitespace-replace-action
1640 'tabify rstart rend
1641 whitespace-space-before-tab-regexp 0))
1642 ;; ACTION: replace TABs by SPACEs.
1643 ((memq 'space-before-tab::space whitespace-style)
1644 (whitespace-replace-action
1645 'untabify rstart rend
1646 whitespace-space-before-tab-regexp 2)))))
1647 (set-marker rend nil)))) ; point marker to nowhere
1648
1649
1650 (defun whitespace-replace-action (action rstart rend regexp index)
1651 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1652
1653 INDEX is the level group matched by REGEXP and used by ACTION.
1654
1655 See also `tab-width'."
1656 (goto-char rstart)
1657 (while (re-search-forward regexp rend t)
1658 (goto-char (match-end index))
1659 (funcall action (match-beginning index) (match-end index))))
1660
1661 \f
1662 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1663 ;;;; User command - report
1664
1665
1666 (defun whitespace-regexp (regexp &optional kind)
1667 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1668 (cond
1669 ((or (eq kind 'tab)
1670 whitespace-indent-tabs-mode)
1671 (format (car regexp) whitespace-tab-width))
1672 ((or (eq kind 'space)
1673 (not whitespace-indent-tabs-mode))
1674 (cdr regexp))))
1675
1676
1677 (defun whitespace-indentation-regexp (&optional kind)
1678 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1679 (whitespace-regexp whitespace-indentation-regexp kind))
1680
1681
1682 (defun whitespace-space-after-tab-regexp (&optional kind)
1683 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1684 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1685
1686
1687 (defconst whitespace-report-list
1688 (list
1689 (cons 'empty whitespace-empty-at-bob-regexp)
1690 (cons 'empty whitespace-empty-at-eob-regexp)
1691 (cons 'trailing whitespace-trailing-regexp)
1692 (cons 'indentation nil)
1693 (cons 'indentation::tab nil)
1694 (cons 'indentation::space nil)
1695 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1696 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1697 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1698 (cons 'space-after-tab nil)
1699 (cons 'space-after-tab::tab nil)
1700 (cons 'space-after-tab::space nil)
1701 )
1702 "List of whitespace bogus symbol and corresponding regexp.")
1703
1704
1705 (defconst whitespace-report-text
1706 '( ;; `indent-tabs-mode' has non-nil value
1707 "\
1708 Whitespace Report
1709
1710 Current Setting Whitespace Problem
1711
1712 empty [] [] empty lines at beginning of buffer
1713 empty [] [] empty lines at end of buffer
1714 trailing [] [] SPACEs or TABs at end of line
1715 indentation [] [] 8 or more SPACEs at beginning of line
1716 indentation::tab [] [] 8 or more SPACEs at beginning of line
1717 indentation::space [] [] TABs at beginning of line
1718 space-before-tab [] [] SPACEs before TAB
1719 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1720 space-before-tab::space [] [] SPACEs before TAB: TABs
1721 space-after-tab [] [] 8 or more SPACEs after TAB
1722 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1723 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1724
1725 indent-tabs-mode =
1726 tab-width = \n\n"
1727 . ;; `indent-tabs-mode' has nil value
1728 "\
1729 Whitespace Report
1730
1731 Current Setting Whitespace Problem
1732
1733 empty [] [] empty lines at beginning of buffer
1734 empty [] [] empty lines at end of buffer
1735 trailing [] [] SPACEs or TABs at end of line
1736 indentation [] [] TABs at beginning of line
1737 indentation::tab [] [] 8 or more SPACEs at beginning of line
1738 indentation::space [] [] TABs at beginning of line
1739 space-before-tab [] [] SPACEs before TAB
1740 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1741 space-before-tab::space [] [] SPACEs before TAB: TABs
1742 space-after-tab [] [] 8 or more SPACEs after TAB
1743 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1744 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1745
1746 indent-tabs-mode =
1747 tab-width = \n\n")
1748 "Text for whitespace bogus report.
1749
1750 It is a cons of strings, where the car part is used when
1751 `indent-tabs-mode' is non-nil, and the cdr part is used when
1752 `indent-tabs-mode' is nil.")
1753
1754
1755 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1756 "The buffer name for whitespace bogus report.")
1757
1758
1759 ;;;###autoload
1760 (defun whitespace-report (&optional force report-if-bogus)
1761 "Report some whitespace problems in buffer.
1762
1763 Perform `whitespace-report-region' on the current buffer."
1764 (interactive (list current-prefix-arg))
1765 (whitespace-report-region (point-min) (point-max)
1766 force report-if-bogus))
1767
1768
1769 ;;;###autoload
1770 (defun whitespace-report-region (start end &optional force report-if-bogus)
1771 "Report some whitespace problems in a region.
1772
1773 Return nil if there is no whitespace problem; otherwise, return
1774 non-nil.
1775
1776 If FORCE is non-nil or \\[universal-argument] was pressed just
1777 before calling `whitespace-report-region' interactively, it
1778 forces `whitespace-style' to have:
1779
1780 empty
1781 trailing
1782 indentation
1783 space-before-tab
1784 space-after-tab
1785
1786 If REPORT-IF-BOGUS is t, it reports only when there are any
1787 whitespace problems in buffer; if it is `never', it does not
1788 report problems.
1789
1790 Report if some of the following whitespace problems exist:
1791
1792 * If `indent-tabs-mode' is non-nil:
1793 empty 1. empty lines at beginning of buffer.
1794 empty 2. empty lines at end of buffer.
1795 trailing 3. SPACEs or TABs at end of line.
1796 indentation 4. 8 or more SPACEs at beginning of line.
1797 space-before-tab 5. SPACEs before TAB.
1798 space-after-tab 6. 8 or more SPACEs after TAB.
1799
1800 * If `indent-tabs-mode' is nil:
1801 empty 1. empty lines at beginning of buffer.
1802 empty 2. empty lines at end of buffer.
1803 trailing 3. SPACEs or TABs at end of line.
1804 indentation 4. TABS at beginning of line.
1805 space-before-tab 5. SPACEs before TAB.
1806 space-after-tab 6. 8 or more SPACEs after TAB.
1807
1808 See `whitespace-style' for documentation.
1809 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1810 cleaning up these problems."
1811 (interactive "r")
1812 (setq force (or current-prefix-arg force))
1813 (save-excursion
1814 (save-match-data ;FIXME: Why?
1815 (let* ((has-bogus nil)
1816 (rstart (min start end))
1817 (rend (max start end))
1818 (bogus-list
1819 (mapcar
1820 #'(lambda (option)
1821 (when force
1822 (add-to-list 'whitespace-style (car option)))
1823 (goto-char rstart)
1824 (let ((regexp
1825 (cond
1826 ((eq (car option) 'indentation)
1827 (whitespace-indentation-regexp))
1828 ((eq (car option) 'indentation::tab)
1829 (whitespace-indentation-regexp 'tab))
1830 ((eq (car option) 'indentation::space)
1831 (whitespace-indentation-regexp 'space))
1832 ((eq (car option) 'space-after-tab)
1833 (whitespace-space-after-tab-regexp))
1834 ((eq (car option) 'space-after-tab::tab)
1835 (whitespace-space-after-tab-regexp 'tab))
1836 ((eq (car option) 'space-after-tab::space)
1837 (whitespace-space-after-tab-regexp 'space))
1838 (t
1839 (cdr option)))))
1840 (and (re-search-forward regexp rend t)
1841 (setq has-bogus t))))
1842 whitespace-report-list)))
1843 (when (pcase report-if-bogus (`nil t) (`never nil) (_ has-bogus))
1844 (whitespace-kill-buffer whitespace-report-buffer-name)
1845 ;; `whitespace-indent-tabs-mode' is local to current buffer
1846 ;; `whitespace-tab-width' is local to current buffer
1847 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1848 (ws-tab-width whitespace-tab-width))
1849 (with-current-buffer (get-buffer-create
1850 whitespace-report-buffer-name)
1851 (erase-buffer)
1852 (insert (if ws-indent-tabs-mode
1853 (car whitespace-report-text)
1854 (cdr whitespace-report-text)))
1855 (goto-char (point-min))
1856 (forward-line 3)
1857 (dolist (option whitespace-report-list)
1858 (forward-line 1)
1859 (whitespace-mark-x
1860 27 (memq (car option) whitespace-style))
1861 (whitespace-mark-x 7 (car bogus-list))
1862 (setq bogus-list (cdr bogus-list)))
1863 (forward-line 1)
1864 (whitespace-insert-value ws-indent-tabs-mode)
1865 (whitespace-insert-value ws-tab-width)
1866 (when has-bogus
1867 (goto-char (point-max))
1868 (insert " Type `M-x whitespace-cleanup'"
1869 " to cleanup the buffer.\n\n"
1870 " Type `M-x whitespace-cleanup-region'"
1871 " to cleanup a region.\n\n"))
1872 (whitespace-display-window (current-buffer)))))
1873 has-bogus))))
1874
1875 \f
1876 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1877 ;;;; Internal functions
1878
1879
1880 (defvar whitespace-font-lock-keywords nil
1881 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1882
1883
1884 (defconst whitespace-help-text
1885 "\
1886 Whitespace Toggle Options | scroll up : SPC or > |
1887 | scroll down: M-SPC or < |
1888 FACES \\__________________________/
1889 [] f - toggle face visualization
1890 [] t - toggle TAB visualization
1891 [] s - toggle SPACE and HARD SPACE visualization
1892 [] r - toggle trailing blanks visualization
1893 [] l - toggle \"long lines\" visualization
1894 [] L - toggle \"long lines\" tail visualization
1895 [] n - toggle NEWLINE visualization
1896 [] e - toggle empty line at bob and/or eob visualization
1897 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1898 [] I - toggle indentation SPACEs visualization
1899 [] i - toggle indentation TABs visualization
1900 [] C-t - toggle big indentation visualization
1901 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1902 [] A - toggle SPACEs after TAB: SPACEs visualization
1903 [] a - toggle SPACEs after TAB: TABs visualization
1904 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1905 [] B - toggle SPACEs before TAB: SPACEs visualization
1906 [] b - toggle SPACEs before TAB: TABs visualization
1907
1908 DISPLAY TABLE
1909 [] T - toggle TAB visualization
1910 [] S - toggle SPACE and HARD SPACE visualization
1911 [] N - toggle NEWLINE visualization
1912
1913 x - restore `whitespace-style' value
1914
1915 ? - display this text\n\n"
1916 "Text for whitespace toggle options.")
1917
1918
1919 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1920 "The buffer name for whitespace toggle options.")
1921
1922
1923 (defun whitespace-insert-value (value)
1924 "Insert VALUE at column 20 of next line."
1925 (forward-line 1)
1926 (move-to-column 20 t)
1927 (insert (format "%s" value)))
1928
1929
1930 (defun whitespace-mark-x (nchars condition)
1931 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1932 (forward-char nchars)
1933 (insert (if condition "X" " ")))
1934
1935
1936 (defun whitespace-insert-option-mark (the-list the-value)
1937 "Insert the option mark ('X' or ' ') in toggle options buffer."
1938 (goto-char (point-min))
1939 (forward-line 2)
1940 (dolist (sym the-list)
1941 (if (eq sym 'help-newline)
1942 (forward-line 2)
1943 (forward-line 1)
1944 (whitespace-mark-x 2 (memq sym the-value)))))
1945
1946
1947 (defun whitespace-help-on (style)
1948 "Display the whitespace toggle options."
1949 (unless (get-buffer whitespace-help-buffer-name)
1950 (delete-other-windows)
1951 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
1952 (with-current-buffer buffer
1953 (erase-buffer)
1954 (insert whitespace-help-text)
1955 (whitespace-insert-option-mark
1956 whitespace-style-value-list style)
1957 (whitespace-display-window buffer)))))
1958
1959
1960 (defun whitespace-display-window (buffer)
1961 "Display BUFFER in a new window."
1962 (goto-char (point-min))
1963 (set-buffer-modified-p nil)
1964 (when (< (window-height) (* 2 window-min-height))
1965 (kill-buffer buffer)
1966 (error "Window height is too small; \
1967 can't split window to display whitespace toggle options"))
1968 (let ((win (split-window)))
1969 (set-window-buffer win buffer)
1970 (shrink-window-if-larger-than-buffer win)))
1971
1972
1973 (defun whitespace-kill-buffer (buffer-name)
1974 "Kill buffer BUFFER-NAME and windows related with it."
1975 (let ((buffer (get-buffer buffer-name)))
1976 (when buffer
1977 (delete-windows-on buffer)
1978 (kill-buffer buffer))))
1979
1980
1981 (defun whitespace-help-off ()
1982 "Remove the buffer and window of the whitespace toggle options."
1983 (whitespace-kill-buffer whitespace-help-buffer-name))
1984
1985
1986 (defun whitespace-help-scroll (&optional up)
1987 "Scroll help window, if it exists.
1988
1989 If UP is non-nil, scroll up; otherwise, scroll down."
1990 (condition-case nil
1991 (let ((buffer (get-buffer whitespace-help-buffer-name)))
1992 (if buffer
1993 (with-selected-window (get-buffer-window buffer)
1994 (if up
1995 (scroll-up 3)
1996 (scroll-down 3)))
1997 (ding)))
1998 ;; handler
1999 ((error)
2000 ;; just ignore error
2001 )))
2002
2003
2004 (defun whitespace-interactive-char (local-p)
2005 "Interactive function to read a char and return a symbol.
2006
2007 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2008 uses a global context.
2009
2010 It accepts one of the following chars:
2011
2012 CHAR MEANING
2013 (VIA FACES)
2014 f toggle face visualization
2015 t toggle TAB visualization
2016 s toggle SPACE and HARD SPACE visualization
2017 r toggle trailing blanks visualization
2018 l toggle \"long lines\" visualization
2019 L toggle \"long lines\" tail visualization
2020 n toggle NEWLINE visualization
2021 e toggle empty line at bob and/or eob visualization
2022 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2023 I toggle indentation SPACEs visualization
2024 i toggle indentation TABs visualization
2025 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2026 A toggle SPACEs after TAB: SPACEs visualization
2027 a toggle SPACEs after TAB: TABs visualization
2028 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2029 B toggle SPACEs before TAB: SPACEs visualization
2030 b toggle SPACEs before TAB: TABs visualization
2031
2032 (VIA DISPLAY TABLE)
2033 T toggle TAB visualization
2034 S toggle SPACE and HARD SPACE visualization
2035 N toggle NEWLINE visualization
2036
2037 x restore `whitespace-style' value
2038 ? display brief help
2039
2040 See also `whitespace-toggle-option-alist'."
2041 (let* ((is-off (not (if local-p
2042 whitespace-mode
2043 global-whitespace-mode)))
2044 (style (cond (is-off whitespace-style) ; use default value
2045 (local-p whitespace-active-style)
2046 (t whitespace-toggle-style)))
2047 (prompt
2048 (format "Whitespace Toggle %s (type ? for further options)-"
2049 (if local-p "Local" "Global")))
2050 ch sym)
2051 ;; read a valid option and get the corresponding symbol
2052 (save-window-excursion
2053 (condition-case data
2054 (progn
2055 (while
2056 ;; while condition
2057 (progn
2058 (setq ch (read-char prompt))
2059 (not
2060 (setq sym
2061 (cdr
2062 (assq ch whitespace-toggle-option-alist)))))
2063 ;; while body
2064 (cond
2065 ((eq ch ?\?) (whitespace-help-on style))
2066 ((eq ch ?\ ) (whitespace-help-scroll t))
2067 ((eq ch ?\M- ) (whitespace-help-scroll))
2068 ((eq ch ?>) (whitespace-help-scroll t))
2069 ((eq ch ?<) (whitespace-help-scroll))
2070 (t (ding))))
2071 (whitespace-help-off)
2072 (message " ")) ; clean echo area
2073 ;; handler
2074 ((quit error)
2075 (whitespace-help-off)
2076 (error (error-message-string data)))))
2077 (list sym))) ; return the appropriate symbol
2078
2079
2080 (defun whitespace-toggle-list (local-p arg the-list)
2081 "Toggle options in THE-LIST based on list ARG.
2082
2083 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2084 uses a global context.
2085
2086 ARG is a list of options to be toggled.
2087
2088 THE-LIST is a list of options. This list will be toggled and the
2089 resultant list will be returned."
2090 (unless (if local-p whitespace-mode global-whitespace-mode)
2091 (setq the-list whitespace-style))
2092 (setq the-list (copy-sequence the-list)) ; keep original list
2093 (dolist (sym (if (listp arg) arg (list arg)))
2094 (cond
2095 ;; ignore help value
2096 ((eq sym 'help-newline))
2097 ;; restore default values
2098 ((eq sym 'whitespace-style)
2099 (setq the-list whitespace-style))
2100 ;; toggle valid values
2101 ((memq sym whitespace-style-value-list)
2102 (setq the-list (if (memq sym the-list)
2103 (delq sym the-list)
2104 (cons sym the-list))))))
2105 the-list)
2106
2107
2108 (defvar whitespace-display-table nil
2109 "Used to save a local display table.")
2110
2111 (defvar whitespace-display-table-was-local nil
2112 "Used to remember whether a buffer initially had a local display table.")
2113
2114 (defun whitespace-turn-on ()
2115 "Turn on whitespace visualization."
2116 ;; prepare local hooks
2117 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2118 ;; create whitespace local buffer environment
2119 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2120 (set (make-local-variable 'whitespace-display-table) nil)
2121 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2122 (set (make-local-variable 'whitespace-active-style)
2123 (if (listp whitespace-style)
2124 whitespace-style
2125 (list whitespace-style)))
2126 (whitespace-ensure-local-variables)
2127 ;; turn on whitespace
2128 (when whitespace-active-style
2129 (whitespace-color-on)
2130 (whitespace-display-char-on)))
2131
2132
2133 (defun whitespace-turn-off ()
2134 "Turn off whitespace visualization."
2135 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2136 (when whitespace-active-style
2137 (whitespace-color-off)
2138 (whitespace-display-char-off)))
2139
2140
2141 (defun whitespace-style-face-p ()
2142 "Return t if there is some visualization via face."
2143 (and (memq 'face whitespace-active-style)
2144 (or (memq 'tabs whitespace-active-style)
2145 (memq 'spaces whitespace-active-style)
2146 (memq 'trailing whitespace-active-style)
2147 (memq 'lines whitespace-active-style)
2148 (memq 'lines-tail whitespace-active-style)
2149 (memq 'newline whitespace-active-style)
2150 (memq 'empty whitespace-active-style)
2151 (memq 'indentation whitespace-active-style)
2152 (memq 'indentation::tab whitespace-active-style)
2153 (memq 'indentation::space whitespace-active-style)
2154 (memq 'big-indent whitespace-active-style)
2155 (memq 'space-after-tab whitespace-active-style)
2156 (memq 'space-after-tab::tab whitespace-active-style)
2157 (memq 'space-after-tab::space whitespace-active-style)
2158 (memq 'space-before-tab whitespace-active-style)
2159 (memq 'space-before-tab::tab whitespace-active-style)
2160 (memq 'space-before-tab::space whitespace-active-style))))
2161
2162
2163 (defun whitespace-color-on ()
2164 "Turn on color visualization."
2165 (when (whitespace-style-face-p)
2166 ;; save current point and refontify when necessary
2167 (set (make-local-variable 'whitespace-point)
2168 (point))
2169 (setq whitespace-point--used
2170 (let ((ol (make-overlay (point) (point) nil nil t)))
2171 (delete-overlay ol) ol))
2172 (set (make-local-variable 'whitespace-font-lock-refontify)
2173 0)
2174 (set (make-local-variable 'whitespace-bob-marker)
2175 (point-min-marker))
2176 (set (make-local-variable 'whitespace-eob-marker)
2177 (point-max-marker))
2178 (set (make-local-variable 'whitespace-buffer-changed)
2179 nil)
2180 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
2181 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
2182 ;; Add whitespace-mode color into font lock.
2183 (setq
2184 whitespace-font-lock-keywords
2185 `(
2186 (whitespace-point--flush-used)
2187 ,@(when (memq 'spaces whitespace-active-style)
2188 ;; Show SPACEs.
2189 `((,whitespace-space-regexp 1 whitespace-space t)
2190 ;; Show HARD SPACEs.
2191 (,whitespace-hspace-regexp 1 whitespace-hspace t)))
2192 ,@(when (memq 'tabs whitespace-active-style)
2193 ;; Show TABs.
2194 `((,whitespace-tab-regexp 1 whitespace-tab t)))
2195 ,@(when (memq 'trailing whitespace-active-style)
2196 ;; Show trailing blanks.
2197 `((,#'whitespace-trailing-regexp 1 whitespace-trailing t)))
2198 ,@(when (or (memq 'lines whitespace-active-style)
2199 (memq 'lines-tail whitespace-active-style))
2200 ;; Show "long" lines.
2201 `((,(let ((line-column (or whitespace-line-column fill-column)))
2202 (format
2203 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2204 whitespace-tab-width
2205 (1- whitespace-tab-width)
2206 (/ line-column whitespace-tab-width)
2207 (let ((rem (% line-column whitespace-tab-width)))
2208 (if (zerop rem)
2209 ""
2210 (format ".\\{%d\\}" rem)))))
2211 ,(if (memq 'lines whitespace-active-style)
2212 0 ; whole line
2213 2) ; line tail
2214 whitespace-line prepend)))
2215 ,@(when (or (memq 'space-before-tab whitespace-active-style)
2216 (memq 'space-before-tab::tab whitespace-active-style)
2217 (memq 'space-before-tab::space whitespace-active-style))
2218 `((,whitespace-space-before-tab-regexp
2219 ,(cond
2220 ((memq 'space-before-tab whitespace-active-style)
2221 ;; Show SPACEs before TAB (indent-tabs-mode).
2222 (if whitespace-indent-tabs-mode 1 2))
2223 ((memq 'space-before-tab::tab whitespace-active-style)
2224 1)
2225 ((memq 'space-before-tab::space whitespace-active-style)
2226 2))
2227 whitespace-space-before-tab t)))
2228 ,@(when (or (memq 'indentation whitespace-active-style)
2229 (memq 'indentation::tab whitespace-active-style)
2230 (memq 'indentation::space whitespace-active-style))
2231 `((,(cond
2232 ((memq 'indentation whitespace-active-style)
2233 ;; Show indentation SPACEs (indent-tabs-mode).
2234 (whitespace-indentation-regexp))
2235 ((memq 'indentation::tab whitespace-active-style)
2236 ;; Show indentation SPACEs (SPACEs).
2237 (whitespace-indentation-regexp 'tab))
2238 ((memq 'indentation::space whitespace-active-style)
2239 ;; Show indentation SPACEs (TABs).
2240 (whitespace-indentation-regexp 'space)))
2241 1 whitespace-indentation t)))
2242 ,@(when (memq 'big-indent whitespace-active-style)
2243 ;; Show big indentation.
2244 `((,whitespace-big-indent-regexp 1 'whitespace-big-indent t)))
2245 ,@(when (memq 'empty whitespace-active-style)
2246 ;; Show empty lines at beginning of buffer.
2247 `((,#'whitespace-empty-at-bob-regexp
2248 1 whitespace-empty t)
2249 ;; Show empty lines at end of buffer.
2250 (,#'whitespace-empty-at-eob-regexp
2251 1 whitespace-empty t)))
2252 ,@(when (or (memq 'space-after-tab whitespace-active-style)
2253 (memq 'space-after-tab::tab whitespace-active-style)
2254 (memq 'space-after-tab::space whitespace-active-style))
2255 `((,(cond
2256 ((memq 'space-after-tab whitespace-active-style)
2257 ;; Show SPACEs after TAB (indent-tabs-mode).
2258 (whitespace-space-after-tab-regexp))
2259 ((memq 'space-after-tab::tab whitespace-active-style)
2260 ;; Show SPACEs after TAB (SPACEs).
2261 (whitespace-space-after-tab-regexp 'tab))
2262 ((memq 'space-after-tab::space whitespace-active-style)
2263 ;; Show SPACEs after TAB (TABs).
2264 (whitespace-space-after-tab-regexp 'space)))
2265 1 whitespace-space-after-tab t)))))
2266 (font-lock-add-keywords nil whitespace-font-lock-keywords t)
2267 (font-lock-flush)))
2268
2269
2270 (defun whitespace-color-off ()
2271 "Turn off color visualization."
2272 ;; turn off font lock
2273 (kill-local-variable 'whitespace-point--used)
2274 (when (whitespace-style-face-p)
2275 (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
2276 (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
2277 (font-lock-remove-keywords nil whitespace-font-lock-keywords)
2278 (font-lock-flush)))
2279
2280 (defun whitespace-point--used (start end)
2281 (let ((ostart (overlay-start whitespace-point--used)))
2282 (if ostart
2283 (move-overlay whitespace-point--used
2284 (min start ostart)
2285 (max end (overlay-end whitespace-point--used)))
2286 (move-overlay whitespace-point--used start end))))
2287
2288 (defun whitespace-point--flush-used (limit)
2289 (let ((ostart (overlay-start whitespace-point--used)))
2290 ;; Strip parts of whitespace-point--used we're about to refresh.
2291 (when ostart
2292 (let ((oend (overlay-end whitespace-point--used)))
2293 (if (<= (point) ostart)
2294 (if (<= oend limit)
2295 (delete-overlay whitespace-point--used)
2296 (move-overlay whitespace-point--used limit oend)))
2297 (if (<= oend limit)
2298 (move-overlay whitespace-point--used ostart (point))))))
2299 nil)
2300
2301 (defun whitespace-trailing-regexp (limit)
2302 "Match trailing spaces which do not contain the point at end of line."
2303 (let ((status t))
2304 (while (if (re-search-forward whitespace-trailing-regexp limit t)
2305 (when (= whitespace-point (match-end 1)) ; Loop if point at eol.
2306 (whitespace-point--used (match-beginning 0) (match-end 0))
2307 t)
2308 (setq status nil))) ;; end of buffer
2309 status))
2310
2311
2312 (defun whitespace-empty-at-bob-regexp (limit)
2313 "Match spaces at beginning of buffer which do not contain the point at \
2314 beginning of buffer."
2315 (let ((b (point))
2316 r)
2317 (cond
2318 ;; at bob
2319 ((= b 1)
2320 (setq r (and (looking-at whitespace-empty-at-bob-regexp)
2321 (or (/= whitespace-point 1)
2322 (progn (whitespace-point--used (match-beginning 0)
2323 (match-end 0))
2324 nil))))
2325 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2326 ;; inside bob empty region
2327 ((<= limit whitespace-bob-marker)
2328 (setq r (looking-at whitespace-empty-at-bob-regexp))
2329 (if r
2330 (when (< (match-end 1) limit)
2331 (set-marker whitespace-bob-marker (match-end 1)))
2332 (set-marker whitespace-bob-marker b)))
2333 ;; intersection with end of bob empty region
2334 ((<= b whitespace-bob-marker)
2335 (setq r (looking-at whitespace-empty-at-bob-regexp))
2336 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2337 ;; it is not inside bob empty region
2338 (t
2339 (setq r nil)))
2340 ;; move to end of matching
2341 (and r (goto-char (match-end 1)))
2342 r))
2343
2344
2345 (defsubst whitespace-looking-back (regexp limit)
2346 (save-excursion
2347 (when (/= 0 (skip-chars-backward " \t\n" limit))
2348 (unless (bolp)
2349 (forward-line 1))
2350 (looking-at regexp))))
2351
2352
2353 (defun whitespace-empty-at-eob-regexp (limit)
2354 "Match spaces at end of buffer which do not contain the point at end of \
2355 buffer."
2356 (let ((b (point))
2357 (e (1+ (buffer-size)))
2358 r)
2359 (cond
2360 ;; at eob
2361 ((= limit e)
2362 (goto-char limit)
2363 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2364 (when (and r (= whitespace-point e))
2365 (setq r nil)
2366 (whitespace-point--used (match-beginning 0) (match-end 0)))
2367 (if r
2368 (set-marker whitespace-eob-marker (match-beginning 1))
2369 (set-marker whitespace-eob-marker limit)
2370 (goto-char b))) ; return back to initial position
2371 ;; inside eob empty region
2372 ((>= b whitespace-eob-marker)
2373 (goto-char limit)
2374 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2375 (if r
2376 (when (> (match-beginning 1) b)
2377 (set-marker whitespace-eob-marker (match-beginning 1)))
2378 (set-marker whitespace-eob-marker limit)
2379 (goto-char b))) ; return back to initial position
2380 ;; intersection with beginning of eob empty region
2381 ((>= limit whitespace-eob-marker)
2382 (goto-char limit)
2383 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2384 (if r
2385 (set-marker whitespace-eob-marker (match-beginning 1))
2386 (set-marker whitespace-eob-marker limit)
2387 (goto-char b))) ; return back to initial position
2388 ;; it is not inside eob empty region
2389 (t
2390 (setq r nil)))
2391 r))
2392
2393
2394 (defun whitespace-buffer-changed (_beg _end)
2395 "Set `whitespace-buffer-changed' variable to t."
2396 (setq whitespace-buffer-changed t))
2397
2398
2399 (defun whitespace-post-command-hook ()
2400 "Save current point into `whitespace-point' variable.
2401 Also refontify when necessary."
2402 (unless (and (eq whitespace-point (point))
2403 (not whitespace-buffer-changed))
2404 (setq whitespace-point (point)) ; current point position
2405 (let ((refontify
2406 (cond
2407 ;; It is at end of buffer (eob).
2408 ((= whitespace-point (1+ (buffer-size)))
2409 (when (whitespace-looking-back whitespace-empty-at-eob-regexp
2410 nil)
2411 (match-beginning 0)))
2412 ;; It is at end of line ...
2413 ((and (eolp)
2414 ;; ... with trailing SPACE or TAB
2415 (or (memq (preceding-char) '(?\s ?\t))))
2416 (line-beginning-position))
2417 ;; It is at beginning of buffer (bob).
2418 ((and (= whitespace-point 1)
2419 (looking-at whitespace-empty-at-bob-regexp))
2420 (match-end 0))))
2421 (ostart (overlay-start whitespace-point--used)))
2422 (cond
2423 ((not refontify)
2424 ;; New point does not affect highlighting: just refresh the
2425 ;; highlighting of old point, if needed.
2426 (when ostart
2427 (font-lock-flush ostart
2428 (overlay-end whitespace-point--used))
2429 (delete-overlay whitespace-point--used)))
2430 ((not ostart)
2431 ;; Old point did not affect highlighting, but new one does: refresh the
2432 ;; highlighting of new point.
2433 (font-lock-flush (min refontify (point)) (max refontify (point))))
2434 ((save-excursion
2435 (goto-char ostart)
2436 (setq ostart (line-beginning-position))
2437 (and (<= ostart (max refontify (point)))
2438 (progn
2439 (goto-char (overlay-end whitespace-point--used))
2440 (let ((oend (line-beginning-position 2)))
2441 (<= (min refontify (point)) oend)))))
2442 ;; The old point highlighting and the new point highlighting
2443 ;; cover a contiguous region: do a single refresh.
2444 (font-lock-flush (min refontify (point) ostart)
2445 (max refontify (point)
2446 (overlay-end whitespace-point--used)))
2447 (delete-overlay whitespace-point--used))
2448 (t
2449 (font-lock-flush (min refontify (point))
2450 (max refontify (point)))
2451 (font-lock-flush ostart (overlay-end whitespace-point--used))
2452 (delete-overlay whitespace-point--used))))))
2453
2454 \f
2455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2456 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2457
2458
2459 (defun whitespace-style-mark-p ()
2460 "Return t if there is some visualization via display table."
2461 (or (memq 'tab-mark whitespace-active-style)
2462 (memq 'space-mark whitespace-active-style)
2463 (memq 'newline-mark whitespace-active-style)))
2464
2465
2466 (defsubst whitespace-char-valid-p (char)
2467 ;; This check should be improved!!!
2468 (or (< char 256)
2469 (characterp char)))
2470
2471
2472 (defun whitespace-display-vector-p (vec)
2473 "Return true if every character in vector VEC can be displayed."
2474 (let ((i (length vec)))
2475 (when (> i 0)
2476 (while (and (>= (setq i (1- i)) 0)
2477 (whitespace-char-valid-p (aref vec i))))
2478 (< i 0))))
2479
2480
2481 (defun whitespace-display-char-on ()
2482 "Turn on character display mapping."
2483 (when (and whitespace-display-mappings
2484 (whitespace-style-mark-p))
2485 (let (vecs vec)
2486 ;; Remember whether a buffer has a local display table.
2487 (unless whitespace-display-table-was-local
2488 (setq whitespace-display-table-was-local t
2489 whitespace-display-table
2490 (copy-sequence buffer-display-table))
2491 ;; Assure `buffer-display-table' is unique
2492 ;; when two or more windows are visible.
2493 (setq buffer-display-table
2494 (copy-sequence buffer-display-table)))
2495 (unless buffer-display-table
2496 (setq buffer-display-table (make-display-table)))
2497 (dolist (entry whitespace-display-mappings)
2498 ;; check if it is to display this mark
2499 (when (memq (car entry) whitespace-style)
2500 ;; Get a displayable mapping.
2501 (setq vecs (cddr entry))
2502 (while (and vecs
2503 (not (whitespace-display-vector-p (car vecs))))
2504 (setq vecs (cdr vecs)))
2505 ;; Display a valid mapping.
2506 (when vecs
2507 (setq vec (copy-sequence (car vecs)))
2508 ;; NEWLINE char
2509 (when (and (eq (cadr entry) ?\n)
2510 (memq 'newline whitespace-active-style))
2511 ;; Only insert face bits on NEWLINE char mapping to avoid
2512 ;; obstruction of other faces like TABs and (HARD) SPACEs
2513 ;; faces, font-lock faces, etc.
2514 (dotimes (i (length vec))
2515 (or (eq (aref vec i) ?\n)
2516 (aset vec i
2517 (make-glyph-code (aref vec i)
2518 whitespace-newline)))))
2519 ;; Display mapping
2520 (aset buffer-display-table (cadr entry) vec)))))))
2521
2522
2523 (defun whitespace-display-char-off ()
2524 "Turn off character display mapping."
2525 (and whitespace-display-mappings
2526 (whitespace-style-mark-p)
2527 whitespace-display-table-was-local
2528 (setq whitespace-display-table-was-local nil
2529 buffer-display-table whitespace-display-table)))
2530
2531 \f
2532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2533 ;;;; Hook
2534
2535
2536 (defun whitespace-action-when-on ()
2537 "Action to be taken always when local whitespace is turned on."
2538 (cond ((memq 'cleanup whitespace-action)
2539 (whitespace-cleanup))
2540 ((memq 'report-on-bogus whitespace-action)
2541 (whitespace-report nil t))))
2542
2543
2544 (defun whitespace-write-file-hook ()
2545 "Action to be taken when buffer is written.
2546 It should be added buffer-locally to `write-file-functions'."
2547 (cond ((memq 'auto-cleanup whitespace-action)
2548 (whitespace-cleanup))
2549 ((memq 'abort-on-bogus whitespace-action)
2550 (when (whitespace-report nil t)
2551 (error "Abort write due to whitespace problems in %s"
2552 (buffer-name)))))
2553 nil) ; continue hook processing
2554
2555
2556 (defun whitespace-warn-read-only (msg)
2557 "Warn if buffer is read-only."
2558 (when (memq 'warn-if-read-only whitespace-action)
2559 (message "Can't %s: %s is read-only" msg (buffer-name))))
2560
2561 \f
2562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2563
2564
2565 (defun whitespace-unload-function ()
2566 "Unload the whitespace library."
2567 (global-whitespace-mode -1)
2568 ;; be sure all local whitespace mode is turned off
2569 (save-current-buffer
2570 (dolist (buf (buffer-list))
2571 (set-buffer buf)
2572 (whitespace-mode -1)))
2573 nil) ; continue standard unloading
2574
2575
2576 (provide 'whitespace)
2577
2578
2579 (run-hooks 'whitespace-load-hook)
2580
2581
2582 ;;; whitespace.el ends here