]> code.delx.au - gnu-emacs/blob - lisp/whitespace.el
68625e06acbd3dff4208df16771f4ef2a9084528
[gnu-emacs] / lisp / whitespace.el
1 ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
2
3 ;; Copyright (C) 2000-2013 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 TimOCallaghan (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 (regexp :tag "Indentation SPACEs")
807 (regexp :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 '(regexp :tag "SPACEs After TAB")
838 :group 'whitespace)
839
840
841 (defcustom whitespace-line-column 80
842 "Specify column beyond which the line is highlighted.
843
844 It must be an integer or nil. If nil, the `fill-column' variable value is
845 used.
846
847 Used when `whitespace-style' includes `lines' or `lines-tail'."
848 :type '(choice :tag "Line Length Limit"
849 (integer :tag "Line Length")
850 (const :tag "Use fill-column" nil))
851 :group 'whitespace)
852
853
854 ;; Hacked from `visible-whitespace-mappings' in visws.el
855 (defcustom whitespace-display-mappings
856 '(
857 (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
858 (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
859 ;; NEWLINE is displayed using the face `whitespace-newline'
860 (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
861 ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
862 ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
863 ;; (newline-mark ?\n [?\u00AF ?\n] [?$ ?\n]) ; eol - overscore
864 ;; (newline-mark ?\n [?\u00AC ?\n] [?$ ?\n]) ; eol - negation
865 ;; (newline-mark ?\n [?\u00B0 ?\n] [?$ ?\n]) ; eol - degrees
866 ;;
867 ;; WARNING: the mapping below has a problem.
868 ;; When a TAB occupies exactly one column, it will display the
869 ;; character ?\xBB at that column followed by a TAB which goes to
870 ;; the next TAB column.
871 ;; If this is a problem for you, please, comment the line below.
872 (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
873 )
874 "Specify an alist of mappings for displaying characters.
875
876 Each element has the following form:
877
878 (KIND CHAR VECTOR...)
879
880 Where:
881
882 KIND is the kind of character.
883 It can be one of the following symbols:
884
885 tab-mark for TAB character
886
887 space-mark for SPACE or HARD SPACE character
888
889 newline-mark for NEWLINE character
890
891 CHAR is the character to be mapped.
892
893 VECTOR is a vector of characters to be displayed in place of CHAR.
894 The first display vector that can be displayed is used;
895 if no display vector for a mapping can be displayed, then
896 that character is displayed unmodified.
897
898 The NEWLINE character is displayed using the face given by
899 `whitespace-newline' variable.
900
901 Used when `whitespace-style' includes `tab-mark', `space-mark' or
902 `newline-mark'."
903 :type '(repeat
904 (list :tag "Character Mapping"
905 (choice :tag "Char Kind"
906 (const :tag "Tab" tab-mark)
907 (const :tag "Space" space-mark)
908 (const :tag "Newline" newline-mark))
909 (character :tag "Char")
910 (repeat :inline t :tag "Vector List"
911 (vector :tag ""
912 (repeat :inline t
913 :tag "Vector Characters"
914 (character :tag "Char"))))))
915 :group 'whitespace)
916
917
918 (defcustom whitespace-global-modes t
919 "Modes for which global `whitespace-mode' is automagically turned on.
920
921 Global `whitespace-mode' is controlled by the command
922 `global-whitespace-mode'.
923
924 If nil, means no modes have `whitespace-mode' automatically
925 turned on.
926
927 If t, all modes that support `whitespace-mode' have it
928 automatically turned on.
929
930 Else it should be a list of `major-mode' symbol names for which
931 `whitespace-mode' should be automatically turned on. The sense
932 of the list is negated if it begins with `not'. For example:
933
934 (c-mode c++-mode)
935
936 means that `whitespace-mode' is turned on for buffers in C and
937 C++ modes only."
938 :type '(choice :tag "Global Modes"
939 (const :tag "None" nil)
940 (const :tag "All" t)
941 (set :menu-tag "Mode Specific" :tag "Modes"
942 :value (not)
943 (const :tag "Except" not)
944 (repeat :inline t
945 (symbol :tag "Mode"))))
946 :group 'whitespace)
947
948
949 (defcustom whitespace-action nil
950 "Specify which action is taken when a buffer is visited or written.
951
952 It's a list containing some or all of the following values:
953
954 nil no action is taken.
955
956 cleanup cleanup any bogus whitespace always when local
957 whitespace is turned on.
958 See `whitespace-cleanup' and
959 `whitespace-cleanup-region'.
960
961 report-on-bogus report if there is any bogus whitespace always
962 when local whitespace is turned on.
963
964 auto-cleanup cleanup any bogus whitespace when buffer is
965 written.
966 See `whitespace-cleanup' and
967 `whitespace-cleanup-region'.
968
969 abort-on-bogus abort if there is any bogus whitespace and the
970 buffer is written.
971
972 warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
973 is included in `whitespace-action' and the
974 buffer is read-only.
975
976 Any other value is treated as nil."
977 :type '(choice :tag "Actions"
978 (const :tag "None" nil)
979 (repeat :tag "Action List"
980 (choice :tag "Action"
981 (const :tag "Cleanup When On" cleanup)
982 (const :tag "Report On Bogus" report-on-bogus)
983 (const :tag "Auto Cleanup" auto-cleanup)
984 (const :tag "Abort On Bogus" abort-on-bogus)
985 (const :tag "Warn If Read-Only" warn-if-read-only))))
986 :group 'whitespace)
987
988 \f
989 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
990 ;;;; User commands - Local mode
991
992
993 ;;;###autoload
994 (define-minor-mode whitespace-mode
995 "Toggle whitespace visualization (Whitespace mode).
996 With a prefix argument ARG, enable Whitespace mode if ARG is
997 positive, and disable it otherwise. If called from Lisp, enable
998 the mode if ARG is omitted or nil.
999
1000 See also `whitespace-style', `whitespace-newline' and
1001 `whitespace-display-mappings'."
1002 :lighter " ws"
1003 :init-value nil
1004 :global nil
1005 :group 'whitespace
1006 (cond
1007 (noninteractive ; running a batch job
1008 (setq whitespace-mode nil))
1009 (whitespace-mode ; whitespace-mode on
1010 (whitespace-turn-on)
1011 (whitespace-action-when-on))
1012 (t ; whitespace-mode off
1013 (whitespace-turn-off))))
1014
1015
1016 ;;;###autoload
1017 (define-minor-mode whitespace-newline-mode
1018 "Toggle newline visualization (Whitespace Newline mode).
1019 With a prefix argument ARG, enable Whitespace Newline mode if ARG
1020 is positive, and disable it otherwise. If called from Lisp,
1021 enable the mode if ARG is omitted or nil.
1022
1023 Use `whitespace-newline-mode' only for NEWLINE visualization
1024 exclusively. For other visualizations, including NEWLINE
1025 visualization together with (HARD) SPACEs and/or TABs, please,
1026 use `whitespace-mode'.
1027
1028 See also `whitespace-newline' and `whitespace-display-mappings'."
1029 :lighter " nl"
1030 :init-value nil
1031 :global nil
1032 :group 'whitespace
1033 (let ((whitespace-style '(face newline-mark newline)))
1034 (whitespace-mode (if whitespace-newline-mode
1035 1 -1)))
1036 ;; sync states (running a batch job)
1037 (setq whitespace-newline-mode whitespace-mode))
1038
1039 \f
1040 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1041 ;;;; User commands - Global mode
1042
1043
1044 ;;;###autoload
1045 (define-minor-mode global-whitespace-mode
1046 "Toggle whitespace visualization globally (Global Whitespace mode).
1047 With a prefix argument ARG, enable Global Whitespace mode if ARG
1048 is positive, and disable it otherwise. If called from Lisp,
1049 enable it if ARG is omitted or nil.
1050
1051 See also `whitespace-style', `whitespace-newline' and
1052 `whitespace-display-mappings'."
1053 :lighter " WS"
1054 :init-value nil
1055 :global t
1056 :group 'whitespace
1057 (cond
1058 (noninteractive ; running a batch job
1059 (setq global-whitespace-mode nil))
1060 (global-whitespace-mode ; global-whitespace-mode on
1061 (save-current-buffer
1062 (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1063 (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1064 (dolist (buffer (buffer-list)) ; adjust all local mode
1065 (set-buffer buffer)
1066 (unless whitespace-mode
1067 (whitespace-turn-on-if-enabled)))))
1068 (t ; global-whitespace-mode off
1069 (save-current-buffer
1070 (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
1071 (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
1072 (dolist (buffer (buffer-list)) ; adjust all local mode
1073 (set-buffer buffer)
1074 (unless whitespace-mode
1075 (whitespace-turn-off)))))))
1076
1077 (defvar whitespace-enable-predicate
1078 (lambda ()
1079 (and (cond
1080 ((eq whitespace-global-modes t))
1081 ((listp whitespace-global-modes)
1082 (if (eq (car-safe whitespace-global-modes) 'not)
1083 (not (memq major-mode (cdr whitespace-global-modes)))
1084 (memq major-mode whitespace-global-modes)))
1085 (t nil))
1086 ;; ...we have a display (we're running a batch job)
1087 (not noninteractive)
1088 ;; ...the buffer is not internal (name starts with a space)
1089 (not (eq (aref (buffer-name) 0) ?\ ))
1090 ;; ...the buffer is not special (name starts with *)
1091 (or (not (eq (aref (buffer-name) 0) ?*))
1092 ;; except the scratch buffer.
1093 (string= (buffer-name) "*scratch*"))))
1094 "Predicate to decide which buffers obey `global-whitespace-mode'.
1095 This function is called with no argument and should return non-nil
1096 if the current buffer should obey `global-whitespace-mode'.
1097 This variable is normally modified via `add-function'.")
1098
1099 (defun whitespace-turn-on-if-enabled ()
1100 (when (funcall whitespace-enable-predicate)
1101 (whitespace-turn-on)))
1102
1103 ;;;###autoload
1104 (define-minor-mode global-whitespace-newline-mode
1105 "Toggle global newline visualization (Global Whitespace Newline mode).
1106 With a prefix argument ARG, enable Global Whitespace Newline mode
1107 if ARG is positive, and disable it otherwise. If called from
1108 Lisp, enable it if ARG is omitted or nil.
1109
1110 Use `global-whitespace-newline-mode' only for NEWLINE
1111 visualization exclusively. For other visualizations, including
1112 NEWLINE visualization together with (HARD) SPACEs and/or TABs,
1113 please use `global-whitespace-mode'.
1114
1115 See also `whitespace-newline' and `whitespace-display-mappings'."
1116 :lighter " NL"
1117 :init-value nil
1118 :global t
1119 :group 'whitespace
1120 (let ((whitespace-style '(newline-mark newline)))
1121 (global-whitespace-mode (if global-whitespace-newline-mode
1122 1 -1))
1123 ;; sync states (running a batch job)
1124 (setq global-whitespace-newline-mode global-whitespace-mode)))
1125
1126 \f
1127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1128 ;;;; User commands - Toggle
1129
1130
1131 (defconst whitespace-style-value-list
1132 '(face
1133 tabs
1134 spaces
1135 trailing
1136 lines
1137 lines-tail
1138 newline
1139 empty
1140 indentation
1141 indentation::tab
1142 indentation::space
1143 space-after-tab
1144 space-after-tab::tab
1145 space-after-tab::space
1146 space-before-tab
1147 space-before-tab::tab
1148 space-before-tab::space
1149 help-newline ; value used by `whitespace-insert-option-mark'
1150 tab-mark
1151 space-mark
1152 newline-mark
1153 )
1154 "List of valid `whitespace-style' values.")
1155
1156
1157 (defconst whitespace-toggle-option-alist
1158 '((?f . face)
1159 (?t . tabs)
1160 (?s . spaces)
1161 (?r . trailing)
1162 (?l . lines)
1163 (?L . lines-tail)
1164 (?n . newline)
1165 (?e . empty)
1166 (?\C-i . indentation)
1167 (?I . indentation::tab)
1168 (?i . indentation::space)
1169 (?\C-a . space-after-tab)
1170 (?A . space-after-tab::tab)
1171 (?a . space-after-tab::space)
1172 (?\C-b . space-before-tab)
1173 (?B . space-before-tab::tab)
1174 (?b . space-before-tab::space)
1175 (?T . tab-mark)
1176 (?S . space-mark)
1177 (?N . newline-mark)
1178 (?x . whitespace-style)
1179 )
1180 "Alist of toggle options.
1181
1182 Each element has the form:
1183
1184 (CHAR . SYMBOL)
1185
1186 Where:
1187
1188 CHAR is a char which the user will have to type.
1189
1190 SYMBOL is a valid symbol associated with CHAR.
1191 See `whitespace-style-value-list'.")
1192
1193
1194 (defvar whitespace-active-style nil
1195 "Used to save locally `whitespace-style' value.")
1196
1197 (defvar whitespace-indent-tabs-mode indent-tabs-mode
1198 "Used to save locally `indent-tabs-mode' value.")
1199
1200 (defvar whitespace-tab-width tab-width
1201 "Used to save locally `tab-width' value.")
1202
1203 (defvar whitespace-point (point)
1204 "Used to save locally current point value.
1205 Used by function `whitespace-trailing-regexp' (which see).")
1206
1207 (defvar whitespace-font-lock-refontify nil
1208 "Used to save locally the font-lock refontify state.
1209 Used by function `whitespace-post-command-hook' (which see).")
1210
1211 (defvar whitespace-bob-marker nil
1212 "Used to save locally the bob marker value.
1213 Used by function `whitespace-post-command-hook' (which see).")
1214
1215 (defvar whitespace-eob-marker nil
1216 "Used to save locally the eob marker value.
1217 Used by function `whitespace-post-command-hook' (which see).")
1218
1219 (defvar whitespace-buffer-changed nil
1220 "Used to indicate locally if buffer changed.
1221 Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
1222 functions (which see).")
1223
1224
1225 ;;;###autoload
1226 (defun whitespace-toggle-options (arg)
1227 "Toggle local `whitespace-mode' options.
1228
1229 If local whitespace-mode is off, toggle the option given by ARG
1230 and turn on local whitespace-mode.
1231
1232 If local whitespace-mode is on, toggle the option given by ARG
1233 and restart local whitespace-mode.
1234
1235 Interactively, it reads one of the following chars:
1236
1237 CHAR MEANING
1238 (VIA FACES)
1239 f toggle face visualization
1240 t toggle TAB visualization
1241 s toggle SPACE and HARD SPACE visualization
1242 r toggle trailing blanks visualization
1243 l toggle \"long lines\" visualization
1244 L toggle \"long lines\" tail visualization
1245 n toggle NEWLINE visualization
1246 e toggle empty line at bob and/or eob visualization
1247 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1248 I toggle indentation SPACEs visualization
1249 i toggle indentation TABs visualization
1250 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1251 A toggle SPACEs after TAB: SPACEs visualization
1252 a toggle SPACEs after TAB: TABs visualization
1253 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1254 B toggle SPACEs before TAB: SPACEs visualization
1255 b toggle SPACEs before TAB: TABs visualization
1256
1257 (VIA DISPLAY TABLE)
1258 T toggle TAB visualization
1259 S toggle SPACEs before TAB visualization
1260 N toggle NEWLINE visualization
1261
1262 x restore `whitespace-style' value
1263 ? display brief help
1264
1265 Non-interactively, ARG should be a symbol or a list of symbols.
1266 The valid symbols are:
1267
1268 face toggle face visualization
1269 tabs toggle TAB visualization
1270 spaces toggle SPACE and HARD SPACE visualization
1271 trailing toggle trailing blanks visualization
1272 lines toggle \"long lines\" visualization
1273 lines-tail toggle \"long lines\" tail visualization
1274 newline toggle NEWLINE visualization
1275 empty toggle empty line at bob and/or eob visualization
1276 indentation toggle indentation SPACEs visualization
1277 indentation::tab toggle indentation SPACEs visualization
1278 indentation::space toggle indentation TABs visualization
1279 space-after-tab toggle SPACEs after TAB visualization
1280 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1281 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1282 space-before-tab toggle SPACEs before TAB visualization
1283 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1284 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1285
1286 tab-mark toggle TAB visualization
1287 space-mark toggle SPACEs before TAB visualization
1288 newline-mark toggle NEWLINE visualization
1289
1290 whitespace-style restore `whitespace-style' value
1291
1292 See `whitespace-style' and `indent-tabs-mode' for documentation."
1293 (interactive (whitespace-interactive-char t))
1294 (let ((whitespace-style
1295 (whitespace-toggle-list t arg whitespace-active-style)))
1296 (whitespace-mode 0)
1297 (whitespace-mode 1)))
1298
1299
1300 (defvar whitespace-toggle-style nil
1301 "Used to toggle the global `whitespace-style' value.")
1302
1303
1304 ;;;###autoload
1305 (defun global-whitespace-toggle-options (arg)
1306 "Toggle global `whitespace-mode' options.
1307
1308 If global whitespace-mode is off, toggle the option given by ARG
1309 and turn on global whitespace-mode.
1310
1311 If global whitespace-mode is on, toggle the option given by ARG
1312 and restart global whitespace-mode.
1313
1314 Interactively, it accepts one of the following chars:
1315
1316 CHAR MEANING
1317 (VIA FACES)
1318 f toggle face visualization
1319 t toggle TAB visualization
1320 s toggle SPACE and HARD SPACE visualization
1321 r toggle trailing blanks visualization
1322 l toggle \"long lines\" visualization
1323 L toggle \"long lines\" tail visualization
1324 n toggle NEWLINE visualization
1325 e toggle empty line at bob and/or eob visualization
1326 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
1327 I toggle indentation SPACEs visualization
1328 i toggle indentation TABs visualization
1329 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1330 A toggle SPACEs after TAB: SPACEs visualization
1331 a toggle SPACEs after TAB: TABs visualization
1332 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1333 B toggle SPACEs before TAB: SPACEs visualization
1334 b toggle SPACEs before TAB: TABs visualization
1335
1336 (VIA DISPLAY TABLE)
1337 T toggle TAB visualization
1338 S toggle SPACEs before TAB visualization
1339 N toggle NEWLINE visualization
1340
1341 x restore `whitespace-style' value
1342 ? display brief help
1343
1344 Non-interactively, ARG should be a symbol or a list of symbols.
1345 The valid symbols are:
1346
1347 face toggle face visualization
1348 tabs toggle TAB visualization
1349 spaces toggle SPACE and HARD SPACE visualization
1350 trailing toggle trailing blanks visualization
1351 lines toggle \"long lines\" visualization
1352 lines-tail toggle \"long lines\" tail visualization
1353 newline toggle NEWLINE visualization
1354 empty toggle empty line at bob and/or eob visualization
1355 indentation toggle indentation SPACEs visualization
1356 indentation::tab toggle indentation SPACEs visualization
1357 indentation::space toggle indentation TABs visualization
1358 space-after-tab toggle SPACEs after TAB visualization
1359 space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
1360 space-after-tab::space toggle SPACEs after TAB: TABs visualization
1361 space-before-tab toggle SPACEs before TAB visualization
1362 space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
1363 space-before-tab::space toggle SPACEs before TAB: TABs visualization
1364
1365 tab-mark toggle TAB visualization
1366 space-mark toggle SPACEs before TAB visualization
1367 newline-mark toggle NEWLINE visualization
1368
1369 whitespace-style restore `whitespace-style' value
1370
1371 See `whitespace-style' and `indent-tabs-mode' for documentation."
1372 (interactive (whitespace-interactive-char nil))
1373 (let ((whitespace-style
1374 (whitespace-toggle-list nil arg whitespace-toggle-style)))
1375 (setq whitespace-toggle-style whitespace-style)
1376 (global-whitespace-mode 0)
1377 (global-whitespace-mode 1)))
1378
1379 \f
1380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1381 ;;;; User commands - Cleanup
1382
1383
1384 ;;;###autoload
1385 (defun whitespace-cleanup ()
1386 "Cleanup some blank problems in all buffer or at region.
1387
1388 It usually applies to the whole buffer, but in transient mark
1389 mode when the mark is active, it applies to the region. It also
1390 applies to the region when it is not in transient mark mode, the
1391 mark is active and \\[universal-argument] was pressed just before
1392 calling `whitespace-cleanup' interactively.
1393
1394 See also `whitespace-cleanup-region'.
1395
1396 The problems cleaned up are:
1397
1398 1. empty lines at beginning of buffer.
1399 2. empty lines at end of buffer.
1400 If `whitespace-style' includes the value `empty', remove all
1401 empty lines at beginning and/or end of buffer.
1402
1403 3. 8 or more SPACEs at beginning of line.
1404 If `whitespace-style' includes the value `indentation':
1405 replace 8 or more SPACEs at beginning of line by TABs, if
1406 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1407 SPACEs.
1408 If `whitespace-style' includes the value `indentation::tab',
1409 replace 8 or more SPACEs at beginning of line by TABs.
1410 If `whitespace-style' includes the value `indentation::space',
1411 replace TABs by SPACEs.
1412
1413 4. SPACEs before TAB.
1414 If `whitespace-style' includes the value `space-before-tab':
1415 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1416 otherwise, replace TABs by SPACEs.
1417 If `whitespace-style' includes the value
1418 `space-before-tab::tab', replace SPACEs by TABs.
1419 If `whitespace-style' includes the value
1420 `space-before-tab::space', replace TABs by SPACEs.
1421
1422 5. SPACEs or TABs at end of line.
1423 If `whitespace-style' includes the value `trailing', remove
1424 all SPACEs or TABs at end of line.
1425
1426 6. 8 or more SPACEs after TAB.
1427 If `whitespace-style' includes the value `space-after-tab':
1428 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1429 otherwise, replace TABs by SPACEs.
1430 If `whitespace-style' includes the value
1431 `space-after-tab::tab', replace SPACEs by TABs.
1432 If `whitespace-style' includes the value
1433 `space-after-tab::space', replace TABs by SPACEs.
1434
1435 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1436 documentation."
1437 (interactive "@")
1438 (cond
1439 ;; read-only buffer
1440 (buffer-read-only
1441 (whitespace-warn-read-only "cleanup"))
1442 ;; region active
1443 ((and (or transient-mark-mode
1444 current-prefix-arg)
1445 mark-active)
1446 ;; PROBLEMs 1 and 2 are not handled in region
1447 ;; PROBLEM 3: 8 or more SPACEs at bol
1448 ;; PROBLEM 4: SPACEs before TAB
1449 ;; PROBLEM 5: SPACEs or TABs at eol
1450 ;; PROBLEM 6: 8 or more SPACEs after TAB
1451 (whitespace-cleanup-region (region-beginning) (region-end)))
1452 ;; whole buffer
1453 (t
1454 (save-excursion
1455 (save-match-data ;FIXME: Why?
1456 ;; PROBLEM 1: empty lines at bob
1457 ;; PROBLEM 2: empty lines at eob
1458 ;; ACTION: remove all empty lines at bob and/or eob
1459 (when (memq 'empty whitespace-style)
1460 (let (overwrite-mode) ; enforce no overwrite
1461 (goto-char (point-min))
1462 (when (looking-at whitespace-empty-at-bob-regexp)
1463 (delete-region (match-beginning 1) (match-end 1)))
1464 (when (re-search-forward
1465 (concat whitespace-empty-at-eob-regexp "\\'") nil t)
1466 (delete-region (match-beginning 1) (match-end 1)))))))
1467 ;; PROBLEM 3: 8 or more SPACEs at bol
1468 ;; PROBLEM 4: SPACEs before TAB
1469 ;; PROBLEM 5: SPACEs or TABs at eol
1470 ;; PROBLEM 6: 8 or more SPACEs after TAB
1471 (whitespace-cleanup-region (point-min) (point-max)))))
1472
1473
1474 ;;;###autoload
1475 (defun whitespace-cleanup-region (start end)
1476 "Cleanup some blank problems at region.
1477
1478 The problems cleaned up are:
1479
1480 1. 8 or more SPACEs at beginning of line.
1481 If `whitespace-style' includes the value `indentation':
1482 replace 8 or more SPACEs at beginning of line by TABs, if
1483 `indent-tabs-mode' is non-nil; otherwise, replace TABs by
1484 SPACEs.
1485 If `whitespace-style' includes the value `indentation::tab',
1486 replace 8 or more SPACEs at beginning of line by TABs.
1487 If `whitespace-style' includes the value `indentation::space',
1488 replace TABs by SPACEs.
1489
1490 2. SPACEs before TAB.
1491 If `whitespace-style' includes the value `space-before-tab':
1492 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1493 otherwise, replace TABs by SPACEs.
1494 If `whitespace-style' includes the value
1495 `space-before-tab::tab', replace SPACEs by TABs.
1496 If `whitespace-style' includes the value
1497 `space-before-tab::space', replace TABs by SPACEs.
1498
1499 3. SPACEs or TABs at end of line.
1500 If `whitespace-style' includes the value `trailing', remove
1501 all SPACEs or TABs at end of line.
1502
1503 4. 8 or more SPACEs after TAB.
1504 If `whitespace-style' includes the value `space-after-tab':
1505 replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
1506 otherwise, replace TABs by SPACEs.
1507 If `whitespace-style' includes the value
1508 `space-after-tab::tab', replace SPACEs by TABs.
1509 If `whitespace-style' includes the value
1510 `space-after-tab::space', replace TABs by SPACEs.
1511
1512 See `whitespace-style', `indent-tabs-mode' and `tab-width' for
1513 documentation."
1514 (interactive "@r")
1515 (if buffer-read-only
1516 ;; read-only buffer
1517 (whitespace-warn-read-only "cleanup region")
1518 ;; non-read-only buffer
1519 (let ((rstart (min start end))
1520 (rend (copy-marker (max start end)))
1521 (indent-tabs-mode whitespace-indent-tabs-mode)
1522 (tab-width whitespace-tab-width)
1523 overwrite-mode ; enforce no overwrite
1524 tmp)
1525 (save-excursion
1526 (save-match-data ;FIXME: Why?
1527 ;; PROBLEM 1: 8 or more SPACEs at bol
1528 (cond
1529 ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
1530 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1531 ;; by SPACEs.
1532 ((memq 'indentation whitespace-style)
1533 (let ((regexp (whitespace-indentation-regexp)))
1534 (goto-char rstart)
1535 (while (re-search-forward regexp rend t)
1536 (setq tmp (current-indentation))
1537 (goto-char (match-beginning 0))
1538 (delete-horizontal-space)
1539 (unless (eolp)
1540 (indent-to tmp)))))
1541 ;; ACTION: replace 8 or more SPACEs at bol by TABs.
1542 ((memq 'indentation::tab whitespace-style)
1543 (whitespace-replace-action
1544 'tabify rstart rend
1545 (whitespace-indentation-regexp 'tab) 0))
1546 ;; ACTION: replace TABs by SPACEs.
1547 ((memq 'indentation::space whitespace-style)
1548 (whitespace-replace-action
1549 'untabify rstart rend
1550 (whitespace-indentation-regexp 'space) 0)))
1551 ;; PROBLEM 3: SPACEs or TABs at eol
1552 ;; ACTION: remove all SPACEs or TABs at eol
1553 (when (memq 'trailing whitespace-style)
1554 (whitespace-replace-action
1555 'delete-region rstart rend
1556 whitespace-trailing-regexp 1))
1557 ;; PROBLEM 4: 8 or more SPACEs after TAB
1558 (cond
1559 ;; ACTION: replace 8 or more SPACEs by TABs, if
1560 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1561 ;; by SPACEs.
1562 ((memq 'space-after-tab whitespace-style)
1563 (whitespace-replace-action
1564 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1565 rstart rend (whitespace-space-after-tab-regexp) 1))
1566 ;; ACTION: replace 8 or more SPACEs by TABs.
1567 ((memq 'space-after-tab::tab whitespace-style)
1568 (whitespace-replace-action
1569 'tabify rstart rend
1570 (whitespace-space-after-tab-regexp 'tab) 1))
1571 ;; ACTION: replace TABs by SPACEs.
1572 ((memq 'space-after-tab::space whitespace-style)
1573 (whitespace-replace-action
1574 'untabify rstart rend
1575 (whitespace-space-after-tab-regexp 'space) 1)))
1576 ;; PROBLEM 2: SPACEs before TAB
1577 (cond
1578 ;; ACTION: replace SPACEs before TAB by TABs, if
1579 ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
1580 ;; by SPACEs.
1581 ((memq 'space-before-tab whitespace-style)
1582 (whitespace-replace-action
1583 (if whitespace-indent-tabs-mode 'tabify 'untabify)
1584 rstart rend whitespace-space-before-tab-regexp
1585 (if whitespace-indent-tabs-mode 0 2)))
1586 ;; ACTION: replace SPACEs before TAB by TABs.
1587 ((memq 'space-before-tab::tab whitespace-style)
1588 (whitespace-replace-action
1589 'tabify rstart rend
1590 whitespace-space-before-tab-regexp 0))
1591 ;; ACTION: replace TABs by SPACEs.
1592 ((memq 'space-before-tab::space whitespace-style)
1593 (whitespace-replace-action
1594 'untabify rstart rend
1595 whitespace-space-before-tab-regexp 2)))))
1596 (set-marker rend nil)))) ; point marker to nowhere
1597
1598
1599 (defun whitespace-replace-action (action rstart rend regexp index)
1600 "Do ACTION in the string matched by REGEXP between RSTART and REND.
1601
1602 INDEX is the level group matched by REGEXP and used by ACTION.
1603
1604 See also `tab-width'."
1605 (goto-char rstart)
1606 (while (re-search-forward regexp rend t)
1607 (goto-char (match-end index))
1608 (funcall action (match-beginning index) (match-end index))))
1609
1610 \f
1611 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1612 ;;;; User command - report
1613
1614
1615 (defun whitespace-regexp (regexp &optional kind)
1616 "Return REGEXP depending on `whitespace-indent-tabs-mode'."
1617 (cond
1618 ((or (eq kind 'tab)
1619 whitespace-indent-tabs-mode)
1620 (format (car regexp) whitespace-tab-width))
1621 ((or (eq kind 'space)
1622 (not whitespace-indent-tabs-mode))
1623 (cdr regexp))))
1624
1625
1626 (defun whitespace-indentation-regexp (&optional kind)
1627 "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
1628 (whitespace-regexp whitespace-indentation-regexp kind))
1629
1630
1631 (defun whitespace-space-after-tab-regexp (&optional kind)
1632 "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
1633 (whitespace-regexp whitespace-space-after-tab-regexp kind))
1634
1635
1636 (defconst whitespace-report-list
1637 (list
1638 (cons 'empty whitespace-empty-at-bob-regexp)
1639 (cons 'empty whitespace-empty-at-eob-regexp)
1640 (cons 'trailing whitespace-trailing-regexp)
1641 (cons 'indentation nil)
1642 (cons 'indentation::tab nil)
1643 (cons 'indentation::space nil)
1644 (cons 'space-before-tab whitespace-space-before-tab-regexp)
1645 (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
1646 (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
1647 (cons 'space-after-tab nil)
1648 (cons 'space-after-tab::tab nil)
1649 (cons 'space-after-tab::space nil)
1650 )
1651 "List of whitespace bogus symbol and corresponding regexp.")
1652
1653
1654 (defconst whitespace-report-text
1655 '( ;; `indent-tabs-mode' has non-nil value
1656 "\
1657 Whitespace Report
1658
1659 Current Setting Whitespace Problem
1660
1661 empty [] [] empty lines at beginning of buffer
1662 empty [] [] empty lines at end of buffer
1663 trailing [] [] SPACEs or TABs at end of line
1664 indentation [] [] 8 or more SPACEs at beginning of line
1665 indentation::tab [] [] 8 or more SPACEs at beginning of line
1666 indentation::space [] [] TABs at beginning of line
1667 space-before-tab [] [] SPACEs before TAB
1668 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1669 space-before-tab::space [] [] SPACEs before TAB: TABs
1670 space-after-tab [] [] 8 or more SPACEs after TAB
1671 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1672 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1673
1674 indent-tabs-mode =
1675 tab-width = \n\n"
1676 . ;; `indent-tabs-mode' has nil value
1677 "\
1678 Whitespace Report
1679
1680 Current Setting Whitespace Problem
1681
1682 empty [] [] empty lines at beginning of buffer
1683 empty [] [] empty lines at end of buffer
1684 trailing [] [] SPACEs or TABs at end of line
1685 indentation [] [] TABs at beginning of line
1686 indentation::tab [] [] 8 or more SPACEs at beginning of line
1687 indentation::space [] [] TABs at beginning of line
1688 space-before-tab [] [] SPACEs before TAB
1689 space-before-tab::tab [] [] SPACEs before TAB: SPACEs
1690 space-before-tab::space [] [] SPACEs before TAB: TABs
1691 space-after-tab [] [] 8 or more SPACEs after TAB
1692 space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
1693 space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
1694
1695 indent-tabs-mode =
1696 tab-width = \n\n")
1697 "Text for whitespace bogus report.
1698
1699 It is a cons of strings, where the car part is used when
1700 `indent-tabs-mode' is non-nil, and the cdr part is used when
1701 `indent-tabs-mode' is nil.")
1702
1703
1704 (defconst whitespace-report-buffer-name "*Whitespace Report*"
1705 "The buffer name for whitespace bogus report.")
1706
1707
1708 ;;;###autoload
1709 (defun whitespace-report (&optional force report-if-bogus)
1710 "Report some whitespace problems in buffer.
1711
1712 Return nil if there is no whitespace problem; otherwise, return
1713 non-nil.
1714
1715 If FORCE is non-nil or \\[universal-argument] was pressed just
1716 before calling `whitespace-report' interactively, it forces
1717 `whitespace-style' to have:
1718
1719 empty
1720 trailing
1721 indentation
1722 space-before-tab
1723 space-after-tab
1724
1725 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1726 whitespace problems in buffer.
1727
1728 Report if some of the following whitespace problems exist:
1729
1730 * If `indent-tabs-mode' is non-nil:
1731 empty 1. empty lines at beginning of buffer.
1732 empty 2. empty lines at end of buffer.
1733 trailing 3. SPACEs or TABs at end of line.
1734 indentation 4. 8 or more SPACEs at beginning of line.
1735 space-before-tab 5. SPACEs before TAB.
1736 space-after-tab 6. 8 or more SPACEs after TAB.
1737
1738 * If `indent-tabs-mode' is nil:
1739 empty 1. empty lines at beginning of buffer.
1740 empty 2. empty lines at end of buffer.
1741 trailing 3. SPACEs or TABs at end of line.
1742 indentation 4. TABS at beginning of line.
1743 space-before-tab 5. SPACEs before TAB.
1744 space-after-tab 6. 8 or more SPACEs after TAB.
1745
1746 See `whitespace-style' for documentation.
1747 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1748 cleaning up these problems."
1749 (interactive (list current-prefix-arg))
1750 (whitespace-report-region (point-min) (point-max)
1751 force report-if-bogus))
1752
1753
1754 ;;;###autoload
1755 (defun whitespace-report-region (start end &optional force report-if-bogus)
1756 "Report some whitespace problems in a region.
1757
1758 Return nil if there is no whitespace problem; otherwise, return
1759 non-nil.
1760
1761 If FORCE is non-nil or \\[universal-argument] was pressed just
1762 before calling `whitespace-report-region' interactively, it
1763 forces `whitespace-style' to have:
1764
1765 empty
1766 indentation
1767 space-before-tab
1768 trailing
1769 space-after-tab
1770
1771 If REPORT-IF-BOGUS is non-nil, it reports only when there are any
1772 whitespace problems in buffer.
1773
1774 Report if some of the following whitespace problems exist:
1775
1776 * If `indent-tabs-mode' is non-nil:
1777 empty 1. empty lines at beginning of buffer.
1778 empty 2. empty lines at end of buffer.
1779 trailing 3. SPACEs or TABs at end of line.
1780 indentation 4. 8 or more SPACEs at beginning of line.
1781 space-before-tab 5. SPACEs before TAB.
1782 space-after-tab 6. 8 or more SPACEs after TAB.
1783
1784 * If `indent-tabs-mode' is nil:
1785 empty 1. empty lines at beginning of buffer.
1786 empty 2. empty lines at end of buffer.
1787 trailing 3. SPACEs or TABs at end of line.
1788 indentation 4. TABS at beginning of line.
1789 space-before-tab 5. SPACEs before TAB.
1790 space-after-tab 6. 8 or more SPACEs after TAB.
1791
1792 See `whitespace-style' for documentation.
1793 See also `whitespace-cleanup' and `whitespace-cleanup-region' for
1794 cleaning up these problems."
1795 (interactive "r")
1796 (setq force (or current-prefix-arg force))
1797 (save-excursion
1798 (save-match-data ;FIXME: Why?
1799 (let* ((has-bogus nil)
1800 (rstart (min start end))
1801 (rend (max start end))
1802 (bogus-list
1803 (mapcar
1804 #'(lambda (option)
1805 (when force
1806 (add-to-list 'whitespace-style (car option)))
1807 (goto-char rstart)
1808 (let ((regexp
1809 (cond
1810 ((eq (car option) 'indentation)
1811 (whitespace-indentation-regexp))
1812 ((eq (car option) 'indentation::tab)
1813 (whitespace-indentation-regexp 'tab))
1814 ((eq (car option) 'indentation::space)
1815 (whitespace-indentation-regexp 'space))
1816 ((eq (car option) 'space-after-tab)
1817 (whitespace-space-after-tab-regexp))
1818 ((eq (car option) 'space-after-tab::tab)
1819 (whitespace-space-after-tab-regexp 'tab))
1820 ((eq (car option) 'space-after-tab::space)
1821 (whitespace-space-after-tab-regexp 'space))
1822 (t
1823 (cdr option)))))
1824 (and (re-search-forward regexp rend t)
1825 (setq has-bogus t))))
1826 whitespace-report-list)))
1827 (when (if report-if-bogus has-bogus t)
1828 (whitespace-kill-buffer whitespace-report-buffer-name)
1829 ;; `whitespace-indent-tabs-mode' is local to current buffer
1830 ;; `whitespace-tab-width' is local to current buffer
1831 (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
1832 (ws-tab-width whitespace-tab-width))
1833 (with-current-buffer (get-buffer-create
1834 whitespace-report-buffer-name)
1835 (erase-buffer)
1836 (insert (if ws-indent-tabs-mode
1837 (car whitespace-report-text)
1838 (cdr whitespace-report-text)))
1839 (goto-char (point-min))
1840 (forward-line 3)
1841 (dolist (option whitespace-report-list)
1842 (forward-line 1)
1843 (whitespace-mark-x
1844 27 (memq (car option) whitespace-style))
1845 (whitespace-mark-x 7 (car bogus-list))
1846 (setq bogus-list (cdr bogus-list)))
1847 (forward-line 1)
1848 (whitespace-insert-value ws-indent-tabs-mode)
1849 (whitespace-insert-value ws-tab-width)
1850 (when has-bogus
1851 (goto-char (point-max))
1852 (insert " Type `M-x whitespace-cleanup'"
1853 " to cleanup the buffer.\n\n"
1854 " Type `M-x whitespace-cleanup-region'"
1855 " to cleanup a region.\n\n"))
1856 (whitespace-display-window (current-buffer)))))
1857 has-bogus))))
1858
1859 \f
1860 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1861 ;;;; Internal functions
1862
1863
1864 (defvar whitespace-font-lock-keywords nil
1865 "Used to save the value `whitespace-color-on' adds to `font-lock-keywords'.")
1866
1867
1868 (defconst whitespace-help-text
1869 "\
1870 Whitespace Toggle Options | scroll up : SPC or > |
1871 | scroll down: M-SPC or < |
1872 FACES \\__________________________/
1873 [] f - toggle face visualization
1874 [] t - toggle TAB visualization
1875 [] s - toggle SPACE and HARD SPACE visualization
1876 [] r - toggle trailing blanks visualization
1877 [] l - toggle \"long lines\" visualization
1878 [] L - toggle \"long lines\" tail visualization
1879 [] n - toggle NEWLINE visualization
1880 [] e - toggle empty line at bob and/or eob visualization
1881 [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
1882 [] I - toggle indentation SPACEs visualization
1883 [] i - toggle indentation TABs visualization
1884 [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
1885 [] A - toggle SPACEs after TAB: SPACEs visualization
1886 [] a - toggle SPACEs after TAB: TABs visualization
1887 [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
1888 [] B - toggle SPACEs before TAB: SPACEs visualization
1889 [] b - toggle SPACEs before TAB: TABs visualization
1890
1891 DISPLAY TABLE
1892 [] T - toggle TAB visualization
1893 [] S - toggle SPACE and HARD SPACE visualization
1894 [] N - toggle NEWLINE visualization
1895
1896 x - restore `whitespace-style' value
1897
1898 ? - display this text\n\n"
1899 "Text for whitespace toggle options.")
1900
1901
1902 (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
1903 "The buffer name for whitespace toggle options.")
1904
1905
1906 (defun whitespace-insert-value (value)
1907 "Insert VALUE at column 20 of next line."
1908 (forward-line 1)
1909 (move-to-column 20 t)
1910 (insert (format "%s" value)))
1911
1912
1913 (defun whitespace-mark-x (nchars condition)
1914 "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
1915 (forward-char nchars)
1916 (insert (if condition "X" " ")))
1917
1918
1919 (defun whitespace-insert-option-mark (the-list the-value)
1920 "Insert the option mark ('X' or ' ') in toggle options buffer."
1921 (goto-char (point-min))
1922 (forward-line 2)
1923 (dolist (sym the-list)
1924 (if (eq sym 'help-newline)
1925 (forward-line 2)
1926 (forward-line 1)
1927 (whitespace-mark-x 2 (memq sym the-value)))))
1928
1929
1930 (defun whitespace-help-on (style)
1931 "Display the whitespace toggle options."
1932 (unless (get-buffer whitespace-help-buffer-name)
1933 (delete-other-windows)
1934 (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
1935 (with-current-buffer buffer
1936 (erase-buffer)
1937 (insert whitespace-help-text)
1938 (whitespace-insert-option-mark
1939 whitespace-style-value-list style)
1940 (whitespace-display-window buffer)))))
1941
1942
1943 (defun whitespace-display-window (buffer)
1944 "Display BUFFER in a new window."
1945 (goto-char (point-min))
1946 (set-buffer-modified-p nil)
1947 (when (< (window-height) (* 2 window-min-height))
1948 (kill-buffer buffer)
1949 (error "Window height is too small; \
1950 can't split window to display whitespace toggle options"))
1951 (let ((win (split-window)))
1952 (set-window-buffer win buffer)
1953 (shrink-window-if-larger-than-buffer win)))
1954
1955
1956 (defun whitespace-kill-buffer (buffer-name)
1957 "Kill buffer BUFFER-NAME and windows related with it."
1958 (let ((buffer (get-buffer buffer-name)))
1959 (when buffer
1960 (delete-windows-on buffer)
1961 (kill-buffer buffer))))
1962
1963
1964 (defun whitespace-help-off ()
1965 "Remove the buffer and window of the whitespace toggle options."
1966 (whitespace-kill-buffer whitespace-help-buffer-name))
1967
1968
1969 (defun whitespace-help-scroll (&optional up)
1970 "Scroll help window, if it exists.
1971
1972 If UP is non-nil, scroll up; otherwise, scroll down."
1973 (condition-case nil
1974 (let ((buffer (get-buffer whitespace-help-buffer-name)))
1975 (if buffer
1976 (with-selected-window (get-buffer-window buffer)
1977 (if up
1978 (scroll-up 3)
1979 (scroll-down 3)))
1980 (ding)))
1981 ;; handler
1982 ((error)
1983 ;; just ignore error
1984 )))
1985
1986
1987 (defun whitespace-interactive-char (local-p)
1988 "Interactive function to read a char and return a symbol.
1989
1990 If LOCAL-P is non-nil, it uses a local context; otherwise, it
1991 uses a global context.
1992
1993 It accepts one of the following chars:
1994
1995 CHAR MEANING
1996 (VIA FACES)
1997 f toggle face visualization
1998 t toggle TAB visualization
1999 s toggle SPACE and HARD SPACE visualization
2000 r toggle trailing blanks visualization
2001 l toggle \"long lines\" visualization
2002 L toggle \"long lines\" tail visualization
2003 n toggle NEWLINE visualization
2004 e toggle empty line at bob and/or eob visualization
2005 C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
2006 I toggle indentation SPACEs visualization
2007 i toggle indentation TABs visualization
2008 C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
2009 A toggle SPACEs after TAB: SPACEs visualization
2010 a toggle SPACEs after TAB: TABs visualization
2011 C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
2012 B toggle SPACEs before TAB: SPACEs visualization
2013 b toggle SPACEs before TAB: TABs visualization
2014
2015 (VIA DISPLAY TABLE)
2016 T toggle TAB visualization
2017 S toggle SPACE and HARD SPACE visualization
2018 N toggle NEWLINE visualization
2019
2020 x restore `whitespace-style' value
2021 ? display brief help
2022
2023 See also `whitespace-toggle-option-alist'."
2024 (let* ((is-off (not (if local-p
2025 whitespace-mode
2026 global-whitespace-mode)))
2027 (style (cond (is-off whitespace-style) ; use default value
2028 (local-p whitespace-active-style)
2029 (t whitespace-toggle-style)))
2030 (prompt
2031 (format "Whitespace Toggle %s (type ? for further options)-"
2032 (if local-p "Local" "Global")))
2033 ch sym)
2034 ;; read a valid option and get the corresponding symbol
2035 (save-window-excursion
2036 (condition-case data
2037 (progn
2038 (while
2039 ;; while condition
2040 (progn
2041 (setq ch (read-char prompt))
2042 (not
2043 (setq sym
2044 (cdr
2045 (assq ch whitespace-toggle-option-alist)))))
2046 ;; while body
2047 (cond
2048 ((eq ch ?\?) (whitespace-help-on style))
2049 ((eq ch ?\ ) (whitespace-help-scroll t))
2050 ((eq ch ?\M- ) (whitespace-help-scroll))
2051 ((eq ch ?>) (whitespace-help-scroll t))
2052 ((eq ch ?<) (whitespace-help-scroll))
2053 (t (ding))))
2054 (whitespace-help-off)
2055 (message " ")) ; clean echo area
2056 ;; handler
2057 ((quit error)
2058 (whitespace-help-off)
2059 (error (error-message-string data)))))
2060 (list sym))) ; return the appropriate symbol
2061
2062
2063 (defun whitespace-toggle-list (local-p arg the-list)
2064 "Toggle options in THE-LIST based on list ARG.
2065
2066 If LOCAL-P is non-nil, it uses a local context; otherwise, it
2067 uses a global context.
2068
2069 ARG is a list of options to be toggled.
2070
2071 THE-LIST is a list of options. This list will be toggled and the
2072 resultant list will be returned."
2073 (unless (if local-p whitespace-mode global-whitespace-mode)
2074 (setq the-list whitespace-style))
2075 (setq the-list (copy-sequence the-list)) ; keep original list
2076 (dolist (sym (if (listp arg) arg (list arg)))
2077 (cond
2078 ;; ignore help value
2079 ((eq sym 'help-newline))
2080 ;; restore default values
2081 ((eq sym 'whitespace-style)
2082 (setq the-list whitespace-style))
2083 ;; toggle valid values
2084 ((memq sym whitespace-style-value-list)
2085 (setq the-list (if (memq sym the-list)
2086 (delq sym the-list)
2087 (cons sym the-list))))))
2088 the-list)
2089
2090
2091 (defvar whitespace-display-table nil
2092 "Used to save a local display table.")
2093
2094 (defvar whitespace-display-table-was-local nil
2095 "Used to remember whether a buffer initially had a local display table.")
2096
2097
2098 (defun whitespace-turn-on ()
2099 "Turn on whitespace visualization."
2100 ;; prepare local hooks
2101 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
2102 ;; create whitespace local buffer environment
2103 (set (make-local-variable 'whitespace-font-lock-keywords) nil)
2104 (set (make-local-variable 'whitespace-display-table) nil)
2105 (set (make-local-variable 'whitespace-display-table-was-local) nil)
2106 (set (make-local-variable 'whitespace-active-style)
2107 (if (listp whitespace-style)
2108 whitespace-style
2109 (list whitespace-style)))
2110 (set (make-local-variable 'whitespace-indent-tabs-mode)
2111 indent-tabs-mode)
2112 (set (make-local-variable 'whitespace-tab-width)
2113 tab-width)
2114 ;; turn on whitespace
2115 (when whitespace-active-style
2116 (whitespace-color-on)
2117 (whitespace-display-char-on)))
2118
2119
2120 (defun whitespace-turn-off ()
2121 "Turn off whitespace visualization."
2122 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
2123 (when whitespace-active-style
2124 (whitespace-color-off)
2125 (whitespace-display-char-off)))
2126
2127
2128 (defun whitespace-style-face-p ()
2129 "Return t if there is some visualization via face."
2130 (and (memq 'face whitespace-active-style)
2131 (or (memq 'tabs whitespace-active-style)
2132 (memq 'spaces whitespace-active-style)
2133 (memq 'trailing whitespace-active-style)
2134 (memq 'lines whitespace-active-style)
2135 (memq 'lines-tail whitespace-active-style)
2136 (memq 'newline whitespace-active-style)
2137 (memq 'empty whitespace-active-style)
2138 (memq 'indentation whitespace-active-style)
2139 (memq 'indentation::tab whitespace-active-style)
2140 (memq 'indentation::space whitespace-active-style)
2141 (memq 'space-after-tab whitespace-active-style)
2142 (memq 'space-after-tab::tab whitespace-active-style)
2143 (memq 'space-after-tab::space whitespace-active-style)
2144 (memq 'space-before-tab whitespace-active-style)
2145 (memq 'space-before-tab::tab whitespace-active-style)
2146 (memq 'space-before-tab::space whitespace-active-style))))
2147
2148
2149 (defun whitespace-color-on ()
2150 "Turn on color visualization."
2151 (when (whitespace-style-face-p)
2152 ;; save current point and refontify when necessary
2153 (set (make-local-variable 'whitespace-point)
2154 (point))
2155 (set (make-local-variable 'whitespace-font-lock-refontify)
2156 0)
2157 (set (make-local-variable 'whitespace-bob-marker)
2158 (point-min-marker))
2159 (set (make-local-variable 'whitespace-eob-marker)
2160 (point-max-marker))
2161 (set (make-local-variable 'whitespace-buffer-changed)
2162 nil)
2163 (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
2164 (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
2165 ;; Add whitespace-mode color into font lock.
2166 (setq
2167 whitespace-font-lock-keywords
2168 `(
2169 ,@(when (memq 'spaces whitespace-active-style)
2170 ;; Show SPACEs.
2171 `((,whitespace-space-regexp 1 whitespace-space t)
2172 ;; Show HARD SPACEs.
2173 (,whitespace-hspace-regexp 1 whitespace-hspace t)))
2174 ,@(when (memq 'tabs whitespace-active-style)
2175 ;; Show TABs.
2176 `((,whitespace-tab-regexp 1 whitespace-tab t)))
2177 ,@(when (memq 'trailing whitespace-active-style)
2178 ;; Show trailing blanks.
2179 `((,#'whitespace-trailing-regexp 1 whitespace-trailing t)))
2180 ,@(when (or (memq 'lines whitespace-active-style)
2181 (memq 'lines-tail whitespace-active-style))
2182 ;; Show "long" lines.
2183 `((,(let ((line-column (or whitespace-line-column fill-column)))
2184 (format
2185 "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
2186 whitespace-tab-width
2187 (1- whitespace-tab-width)
2188 (/ line-column whitespace-tab-width)
2189 (let ((rem (% line-column whitespace-tab-width)))
2190 (if (zerop rem)
2191 ""
2192 (format ".\\{%d\\}" rem)))))
2193 ,(if (memq 'lines whitespace-active-style)
2194 0 ; whole line
2195 2) ; line tail
2196 whitespace-line prepend)))
2197 ,@(when (or (memq 'space-before-tab whitespace-active-style)
2198 (memq 'space-before-tab::tab whitespace-active-style)
2199 (memq 'space-before-tab::space whitespace-active-style))
2200 `((,whitespace-space-before-tab-regexp
2201 ,(cond
2202 ((memq 'space-before-tab whitespace-active-style)
2203 ;; Show SPACEs before TAB (indent-tabs-mode).
2204 (if whitespace-indent-tabs-mode 1 2))
2205 ((memq 'space-before-tab::tab whitespace-active-style)
2206 1)
2207 ((memq 'space-before-tab::space whitespace-active-style)
2208 2))
2209 whitespace-space-before-tab t)))
2210 ,@(when (or (memq 'indentation whitespace-active-style)
2211 (memq 'indentation::tab whitespace-active-style)
2212 (memq 'indentation::space whitespace-active-style))
2213 `((,(cond
2214 ((memq 'indentation whitespace-active-style)
2215 ;; Show indentation SPACEs (indent-tabs-mode).
2216 (whitespace-indentation-regexp))
2217 ((memq 'indentation::tab whitespace-active-style)
2218 ;; Show indentation SPACEs (SPACEs).
2219 (whitespace-indentation-regexp 'tab))
2220 ((memq 'indentation::space whitespace-active-style)
2221 ;; Show indentation SPACEs (TABs).
2222 (whitespace-indentation-regexp 'space)))
2223 1 whitespace-indentation t)))
2224 ,@(when (memq 'empty whitespace-active-style)
2225 ;; Show empty lines at beginning of buffer.
2226 `((,#'whitespace-empty-at-bob-regexp
2227 1 whitespace-empty t)
2228 ;; Show empty lines at end of buffer.
2229 (,#'whitespace-empty-at-eob-regexp
2230 1 whitespace-empty t)))
2231 ,@(when (or (memq 'space-after-tab whitespace-active-style)
2232 (memq 'space-after-tab::tab whitespace-active-style)
2233 (memq 'space-after-tab::space whitespace-active-style))
2234 `((,(cond
2235 ((memq 'space-after-tab whitespace-active-style)
2236 ;; Show SPACEs after TAB (indent-tabs-mode).
2237 (whitespace-space-after-tab-regexp))
2238 ((memq 'space-after-tab::tab whitespace-active-style)
2239 ;; Show SPACEs after TAB (SPACEs).
2240 (whitespace-space-after-tab-regexp 'tab))
2241 ((memq 'space-after-tab::space whitespace-active-style)
2242 ;; Show SPACEs after TAB (TABs).
2243 (whitespace-space-after-tab-regexp 'space)))
2244 1 whitespace-space-after-tab t)))))
2245 (font-lock-add-keywords nil whitespace-font-lock-keywords t)
2246 (when font-lock-mode
2247 (font-lock-fontify-buffer))))
2248
2249
2250 (defun whitespace-color-off ()
2251 "Turn off color visualization."
2252 ;; turn off font lock
2253 (when (whitespace-style-face-p)
2254 (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
2255 (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
2256 (font-lock-remove-keywords nil whitespace-font-lock-keywords)
2257 (when font-lock-mode
2258 (font-lock-fontify-buffer))))
2259
2260
2261 (defun whitespace-trailing-regexp (limit)
2262 "Match trailing spaces which do not contain the point at end of line."
2263 (let ((status t))
2264 (while (if (re-search-forward whitespace-trailing-regexp limit t)
2265 (= whitespace-point (match-end 1)) ;; loop if point at eol
2266 (setq status nil))) ;; end of buffer
2267 status))
2268
2269
2270 (defun whitespace-empty-at-bob-regexp (limit)
2271 "Match spaces at beginning of buffer which do not contain the point at \
2272 beginning of buffer."
2273 (let ((b (point))
2274 r)
2275 (cond
2276 ;; at bob
2277 ((= b 1)
2278 (setq r (and (/= whitespace-point 1)
2279 (looking-at whitespace-empty-at-bob-regexp)))
2280 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2281 ;; inside bob empty region
2282 ((<= limit whitespace-bob-marker)
2283 (setq r (looking-at whitespace-empty-at-bob-regexp))
2284 (if r
2285 (when (< (match-end 1) limit)
2286 (set-marker whitespace-bob-marker (match-end 1)))
2287 (set-marker whitespace-bob-marker b)))
2288 ;; intersection with end of bob empty region
2289 ((<= b whitespace-bob-marker)
2290 (setq r (looking-at whitespace-empty-at-bob-regexp))
2291 (set-marker whitespace-bob-marker (if r (match-end 1) b)))
2292 ;; it is not inside bob empty region
2293 (t
2294 (setq r nil)))
2295 ;; move to end of matching
2296 (and r (goto-char (match-end 1)))
2297 r))
2298
2299
2300 (defsubst whitespace-looking-back (regexp limit)
2301 (save-excursion
2302 (when (/= 0 (skip-chars-backward " \t\n" limit))
2303 (unless (bolp)
2304 (forward-line 1))
2305 (looking-at regexp))))
2306
2307
2308 (defun whitespace-empty-at-eob-regexp (limit)
2309 "Match spaces at end of buffer which do not contain the point at end of \
2310 buffer."
2311 (let ((b (point))
2312 (e (1+ (buffer-size)))
2313 r)
2314 (cond
2315 ;; at eob
2316 ((= limit e)
2317 (when (/= whitespace-point e)
2318 (goto-char limit)
2319 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
2320 (if r
2321 (set-marker whitespace-eob-marker (match-beginning 1))
2322 (set-marker whitespace-eob-marker limit)
2323 (goto-char b))) ; return back to initial position
2324 ;; inside eob empty region
2325 ((>= b whitespace-eob-marker)
2326 (goto-char limit)
2327 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2328 (if r
2329 (when (> (match-beginning 1) b)
2330 (set-marker whitespace-eob-marker (match-beginning 1)))
2331 (set-marker whitespace-eob-marker limit)
2332 (goto-char b))) ; return back to initial position
2333 ;; intersection with beginning of eob empty region
2334 ((>= limit whitespace-eob-marker)
2335 (goto-char limit)
2336 (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
2337 (if r
2338 (set-marker whitespace-eob-marker (match-beginning 1))
2339 (set-marker whitespace-eob-marker limit)
2340 (goto-char b))) ; return back to initial position
2341 ;; it is not inside eob empty region
2342 (t
2343 (setq r nil)))
2344 r))
2345
2346
2347 (defun whitespace-buffer-changed (_beg _end)
2348 "Set `whitespace-buffer-changed' variable to t."
2349 (setq whitespace-buffer-changed t))
2350
2351
2352 (defun whitespace-post-command-hook ()
2353 "Save current point into `whitespace-point' variable.
2354 Also refontify when necessary."
2355 (setq whitespace-point (point)) ; current point position
2356 (let ((refontify
2357 (or
2358 ;; it is at end of line ...
2359 (and (eolp)
2360 ;; ... with trailing SPACE or TAB
2361 (or (= (preceding-char) ?\ )
2362 (= (preceding-char) ?\t)))
2363 ;; it is at beginning of buffer (bob)
2364 (= whitespace-point 1)
2365 ;; the buffer was modified and ...
2366 (and whitespace-buffer-changed
2367 (or
2368 ;; ... or inside bob whitespace region
2369 (<= whitespace-point whitespace-bob-marker)
2370 ;; ... or at bob whitespace region border
2371 (and (= whitespace-point (1+ whitespace-bob-marker))
2372 (= (preceding-char) ?\n))))
2373 ;; it is at end of buffer (eob)
2374 (= whitespace-point (1+ (buffer-size)))
2375 ;; the buffer was modified and ...
2376 (and whitespace-buffer-changed
2377 (or
2378 ;; ... or inside eob whitespace region
2379 (>= whitespace-point whitespace-eob-marker)
2380 ;; ... or at eob whitespace region border
2381 (and (= whitespace-point (1- whitespace-eob-marker))
2382 (= (following-char) ?\n)))))))
2383 (when (or refontify (> whitespace-font-lock-refontify 0))
2384 (setq whitespace-buffer-changed nil)
2385 ;; adjust refontify counter
2386 (setq whitespace-font-lock-refontify
2387 (if refontify
2388 1
2389 (1- whitespace-font-lock-refontify)))
2390 ;; refontify
2391 (jit-lock-refontify))))
2392
2393 \f
2394 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2395 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
2396
2397
2398 (defun whitespace-style-mark-p ()
2399 "Return t if there is some visualization via display table."
2400 (or (memq 'tab-mark whitespace-active-style)
2401 (memq 'space-mark whitespace-active-style)
2402 (memq 'newline-mark whitespace-active-style)))
2403
2404
2405 (defsubst whitespace-char-valid-p (char)
2406 ;; This check should be improved!!!
2407 (or (< char 256)
2408 (characterp char)))
2409
2410
2411 (defun whitespace-display-vector-p (vec)
2412 "Return true if every character in vector VEC can be displayed."
2413 (let ((i (length vec)))
2414 (when (> i 0)
2415 (while (and (>= (setq i (1- i)) 0)
2416 (whitespace-char-valid-p (aref vec i))))
2417 (< i 0))))
2418
2419
2420 (defun whitespace-display-char-on ()
2421 "Turn on character display mapping."
2422 (when (and whitespace-display-mappings
2423 (whitespace-style-mark-p))
2424 (let (vecs vec)
2425 ;; Remember whether a buffer has a local display table.
2426 (unless whitespace-display-table-was-local
2427 (setq whitespace-display-table-was-local t
2428 whitespace-display-table
2429 (copy-sequence buffer-display-table))
2430 ;; Assure `buffer-display-table' is unique
2431 ;; when two or more windows are visible.
2432 (setq buffer-display-table
2433 (copy-sequence buffer-display-table)))
2434 (unless buffer-display-table
2435 (setq buffer-display-table (make-display-table)))
2436 (dolist (entry whitespace-display-mappings)
2437 ;; check if it is to display this mark
2438 (when (memq (car entry) whitespace-style)
2439 ;; Get a displayable mapping.
2440 (setq vecs (cddr entry))
2441 (while (and vecs
2442 (not (whitespace-display-vector-p (car vecs))))
2443 (setq vecs (cdr vecs)))
2444 ;; Display a valid mapping.
2445 (when vecs
2446 (setq vec (copy-sequence (car vecs)))
2447 ;; NEWLINE char
2448 (when (and (eq (cadr entry) ?\n)
2449 (memq 'newline whitespace-active-style))
2450 ;; Only insert face bits on NEWLINE char mapping to avoid
2451 ;; obstruction of other faces like TABs and (HARD) SPACEs
2452 ;; faces, font-lock faces, etc.
2453 (dotimes (i (length vec))
2454 (or (eq (aref vec i) ?\n)
2455 (aset vec i
2456 (make-glyph-code (aref vec i)
2457 whitespace-newline)))))
2458 ;; Display mapping
2459 (aset buffer-display-table (cadr entry) vec)))))))
2460
2461
2462 (defun whitespace-display-char-off ()
2463 "Turn off character display mapping."
2464 (and whitespace-display-mappings
2465 (whitespace-style-mark-p)
2466 whitespace-display-table-was-local
2467 (setq whitespace-display-table-was-local nil
2468 buffer-display-table whitespace-display-table)))
2469
2470 \f
2471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2472 ;;;; Hook
2473
2474
2475 (defun whitespace-action-when-on ()
2476 "Action to be taken always when local whitespace is turned on."
2477 (cond ((memq 'cleanup whitespace-action)
2478 (whitespace-cleanup))
2479 ((memq 'report-on-bogus whitespace-action)
2480 (whitespace-report nil t))))
2481
2482
2483 (defun whitespace-write-file-hook ()
2484 "Action to be taken when buffer is written.
2485 It should be added buffer-locally to `write-file-functions'."
2486 (cond ((memq 'auto-cleanup whitespace-action)
2487 (whitespace-cleanup))
2488 ((memq 'abort-on-bogus whitespace-action)
2489 (when (whitespace-report nil t)
2490 (error "Abort write due to whitespace problems in %s"
2491 (buffer-name)))))
2492 nil) ; continue hook processing
2493
2494
2495 (defun whitespace-warn-read-only (msg)
2496 "Warn if buffer is read-only."
2497 (when (memq 'warn-if-read-only whitespace-action)
2498 (message "Can't %s: %s is read-only" msg (buffer-name))))
2499
2500 \f
2501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2502
2503
2504 (defun whitespace-unload-function ()
2505 "Unload the whitespace library."
2506 (global-whitespace-mode -1)
2507 ;; be sure all local whitespace mode is turned off
2508 (save-current-buffer
2509 (dolist (buf (buffer-list))
2510 (set-buffer buf)
2511 (whitespace-mode -1)))
2512 nil) ; continue standard unloading
2513
2514
2515 (provide 'whitespace)
2516
2517
2518 (run-hooks 'whitespace-load-hook)
2519
2520
2521 ;;; whitespace.el ends here