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