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