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