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