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