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