Commit | Line | Data |
---|---|---|
c9dff4e3 | 1 | ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE |
30d0ade9 | 2 | |
5df4f04c | 3 | ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
30d0ade9 VJL |
4 | ;; Free Software Foundation, Inc. |
5 | ||
6 | ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br> | |
7 | ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br> | |
8 | ;; Keywords: data, wp | |
16498102 | 9 | ;; Version: 13.2 |
30d0ade9 VJL |
10 | ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre |
11 | ||
12 | ;; This file is part of GNU Emacs. | |
13 | ||
eb3fa2cf GM |
14 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
15 | ;; it under the terms of the GNU General Public License as published by | |
16 | ;; the Free Software Foundation, either version 3 of the License, or | |
17 | ;; (at your option) any later version. | |
30d0ade9 | 18 | |
eb3fa2cf GM |
19 | ;; GNU Emacs is distributed in the hope that it will be useful, |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
30d0ade9 VJL |
23 | |
24 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 25 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
30d0ade9 VJL |
26 | |
27 | ;;; Commentary: | |
28 | ||
29 | ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
30 | ;; | |
31 | ;; Introduction | |
32 | ;; ------------ | |
33 | ;; | |
040f578c | 34 | ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE |
30d0ade9 VJL |
35 | ;; and NEWLINE). |
36 | ;; | |
c9dff4e3 | 37 | ;; whitespace uses two ways to visualize blanks: faces and display |
30d0ade9 VJL |
38 | ;; table. |
39 | ;; | |
40 | ;; * Faces are used to highlight the background with a color. | |
c9dff4e3 | 41 | ;; whitespace uses font-lock to highlight blank characters. |
30d0ade9 VJL |
42 | ;; |
43 | ;; * Display table changes the way a character is displayed, that is, | |
44 | ;; it provides a visual mark for characters, for example, at the end | |
45 | ;; of line (?\xB6), at SPACEs (?\xB7) and at TABs (?\xBB). | |
46 | ;; | |
556885cd VJL |
47 | ;; The `whitespace-style' variable selects which way blanks are |
48 | ;; visualized. | |
30d0ade9 | 49 | ;; |
c9dff4e3 | 50 | ;; Note that when whitespace is turned on, whitespace saves the |
30d0ade9 | 51 | ;; font-lock state, that is, if font-lock is on or off. And |
c9dff4e3 VJL |
52 | ;; whitespace restores the font-lock state when it is turned off. So, |
53 | ;; if whitespace is turned on and font-lock is off, whitespace also | |
30d0ade9 | 54 | ;; turns on the font-lock to highlight blanks, but the font-lock will |
c9dff4e3 VJL |
55 | ;; be turned off when whitespace is turned off. Thus, turn on |
56 | ;; font-lock before whitespace is on, if you want that font-lock | |
57 | ;; continues on after whitespace is turned off. | |
30d0ade9 | 58 | ;; |
c9dff4e3 | 59 | ;; When whitespace is on, it takes care of highlighting some special |
30d0ade9 VJL |
60 | ;; characters over the default mechanism of `nobreak-char-display' |
61 | ;; (which see) and `show-trailing-whitespace' (which see). | |
62 | ;; | |
102b1598 VJL |
63 | ;; The trailing spaces are not highlighted while point is at end of line. |
64 | ;; Also the spaces at beginning of buffer are not highlighted while point is at | |
65 | ;; beginning of buffer; and the spaces at end of buffer are not highlighted | |
66 | ;; while point is at end of buffer. | |
67 | ;; | |
c9dff4e3 | 68 | ;; There are two ways of using whitespace: local and global. |
30d0ade9 | 69 | ;; |
c9dff4e3 | 70 | ;; * Local whitespace affects only the current buffer. |
30d0ade9 | 71 | ;; |
c9dff4e3 VJL |
72 | ;; * Global whitespace affects all current and future buffers. That |
73 | ;; is, if you turn on global whitespace and then create a new | |
74 | ;; buffer, the new buffer will also have whitespace on. The | |
75 | ;; `whitespace-global-modes' variable controls which major-mode will | |
76 | ;; be automagically turned on. | |
30d0ade9 VJL |
77 | ;; |
78 | ;; You can mix the local and global usage without any conflict. But | |
c9dff4e3 VJL |
79 | ;; local whitespace has priority over global whitespace. Whitespace |
80 | ;; mode is active in a buffer if you have enabled it in that buffer or | |
81 | ;; if you have enabled it globally. | |
30d0ade9 | 82 | ;; |
c9dff4e3 | 83 | ;; When global and local whitespace are on: |
30d0ade9 | 84 | ;; |
c9dff4e3 | 85 | ;; * if local whitespace is turned off, whitespace is turned off for |
30d0ade9 VJL |
86 | ;; the current buffer only. |
87 | ;; | |
c9dff4e3 VJL |
88 | ;; * if global whitespace is turned off, whitespace continues on only |
89 | ;; in the buffers in which local whitespace is on. | |
30d0ade9 | 90 | ;; |
c9dff4e3 | 91 | ;; To use whitespace, insert in your ~/.emacs: |
30d0ade9 | 92 | ;; |
a025644d | 93 | ;; (require 'whitespace) |
30d0ade9 | 94 | ;; |
c9dff4e3 VJL |
95 | ;; Or autoload at least one of the commands`whitespace-mode', |
96 | ;; `whitespace-toggle-options', `global-whitespace-mode' or | |
97 | ;; `global-whitespace-toggle-options'. For example: | |
30d0ade9 | 98 | ;; |
c9dff4e3 VJL |
99 | ;; (autoload 'whitespace-mode "whitespace" |
100 | ;; "Toggle whitespace visualization." t) | |
101 | ;; (autoload 'whitespace-toggle-options "whitespace" | |
102 | ;; "Toggle local `whitespace-mode' options." t) | |
30d0ade9 | 103 | ;; |
c9dff4e3 | 104 | ;; whitespace was inspired by: |
30d0ade9 | 105 | ;; |
97a739d5 VJL |
106 | ;; whitespace.el Rajesh Vaidheeswarran <rv@gnu.org> |
107 | ;; Warn about and clean bogus whitespaces in the file | |
108 | ;; (inspired the idea to warn and clean some blanks) | |
c9dff4e3 VJL |
109 | ;; This was the original `whitespace.el' which was replaced by |
110 | ;; `blank-mode.el'. And later `blank-mode.el' was renamed to | |
111 | ;; `whitespace.el'. | |
97a739d5 | 112 | ;; |
30d0ade9 VJL |
113 | ;; show-whitespace-mode.el Aurelien Tisne <aurelien.tisne@free.fr> |
114 | ;; Simple mode to highlight whitespaces | |
115 | ;; (inspired the idea to use font-lock) | |
116 | ;; | |
117 | ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li> | |
118 | ;; Major mode for editing Whitespace | |
119 | ;; (inspired the idea to use display table) | |
120 | ;; | |
121 | ;; visws.el Miles Bader <miles@gnu.org> | |
122 | ;; Make whitespace visible | |
123 | ;; (handle display table, his code was modified, but the main | |
124 | ;; idea was kept) | |
125 | ;; | |
126 | ;; | |
c9dff4e3 | 127 | ;; Using whitespace |
30d0ade9 VJL |
128 | ;; ---------------- |
129 | ;; | |
130 | ;; There is no problem if you mix local and global minor mode usage. | |
131 | ;; | |
c9dff4e3 VJL |
132 | ;; * LOCAL whitespace: |
133 | ;; + To toggle whitespace options locally, type: | |
30d0ade9 | 134 | ;; |
c9dff4e3 | 135 | ;; M-x whitespace-toggle-options RET |
30d0ade9 | 136 | ;; |
c9dff4e3 | 137 | ;; + To activate whitespace locally, type: |
30d0ade9 | 138 | ;; |
c9dff4e3 | 139 | ;; C-u 1 M-x whitespace-mode RET |
30d0ade9 | 140 | ;; |
c9dff4e3 | 141 | ;; + To deactivate whitespace locally, type: |
30d0ade9 | 142 | ;; |
c9dff4e3 | 143 | ;; C-u 0 M-x whitespace-mode RET |
30d0ade9 | 144 | ;; |
c9dff4e3 | 145 | ;; + To toggle whitespace locally, type: |
30d0ade9 | 146 | ;; |
c9dff4e3 | 147 | ;; M-x whitespace-mode RET |
30d0ade9 | 148 | ;; |
c9dff4e3 VJL |
149 | ;; * GLOBAL whitespace: |
150 | ;; + To toggle whitespace options globally, type: | |
30d0ade9 | 151 | ;; |
c9dff4e3 | 152 | ;; M-x global-whitespace-toggle-options RET |
30d0ade9 | 153 | ;; |
c9dff4e3 | 154 | ;; + To activate whitespace globally, type: |
30d0ade9 | 155 | ;; |
c9dff4e3 | 156 | ;; C-u 1 M-x global-whitespace-mode RET |
30d0ade9 | 157 | ;; |
c9dff4e3 | 158 | ;; + To deactivate whitespace globally, type: |
30d0ade9 | 159 | ;; |
c9dff4e3 | 160 | ;; C-u 0 M-x global-whitespace-mode RET |
30d0ade9 | 161 | ;; |
c9dff4e3 | 162 | ;; + To toggle whitespace globally, type: |
30d0ade9 | 163 | ;; |
c9dff4e3 | 164 | ;; M-x global-whitespace-mode RET |
30d0ade9 | 165 | ;; |
97a739d5 VJL |
166 | ;; There are also the following useful commands: |
167 | ;; | |
8b40bb55 | 168 | ;; `whitespace-newline-mode' |
cd222e44 | 169 | ;; Toggle NEWLINE minor mode visualization ("nl" on modeline). |
8b40bb55 VJL |
170 | ;; |
171 | ;; `global-whitespace-newline-mode' | |
cd222e44 | 172 | ;; Toggle NEWLINE global minor mode visualization ("NL" on modeline). |
8b40bb55 | 173 | ;; |
94dc593f VJL |
174 | ;; `whitespace-report' |
175 | ;; Report some blank problems in buffer. | |
176 | ;; | |
177 | ;; `whitespace-report-region' | |
178 | ;; Report some blank problems in a region. | |
179 | ;; | |
c9dff4e3 | 180 | ;; `whitespace-cleanup' |
97a739d5 VJL |
181 | ;; Cleanup some blank problems in all buffer or at region. |
182 | ;; | |
c9dff4e3 | 183 | ;; `whitespace-cleanup-region' |
97a739d5 VJL |
184 | ;; Cleanup some blank problems at region. |
185 | ;; | |
186 | ;; The problems, which are cleaned up, are: | |
187 | ;; | |
188 | ;; 1. empty lines at beginning of buffer. | |
189 | ;; 2. empty lines at end of buffer. | |
ae68f2d8 VJL |
190 | ;; If `whitespace-style' includes the value `empty', remove all |
191 | ;; empty lines at beginning and/or end of buffer. | |
97a739d5 VJL |
192 | ;; |
193 | ;; 3. 8 or more SPACEs at beginning of line. | |
ae68f2d8 | 194 | ;; If `whitespace-style' includes the value `indentation': |
55d1cfe8 VJL |
195 | ;; replace 8 or more SPACEs at beginning of line by TABs, if |
196 | ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by | |
197 | ;; SPACEs. | |
ae68f2d8 VJL |
198 | ;; If `whitespace-style' includes the value `indentation::tab', |
199 | ;; replace 8 or more SPACEs at beginning of line by TABs. | |
200 | ;; If `whitespace-style' includes the value `indentation::space', | |
201 | ;; replace TABs by SPACEs. | |
97a739d5 VJL |
202 | ;; |
203 | ;; 4. SPACEs before TAB. | |
ae68f2d8 VJL |
204 | ;; If `whitespace-style' includes the value `space-before-tab': |
205 | ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil; | |
206 | ;; otherwise, replace TABs by SPACEs. | |
207 | ;; If `whitespace-style' includes the value | |
55d1cfe8 | 208 | ;; `space-before-tab::tab', replace SPACEs by TABs. |
ae68f2d8 | 209 | ;; If `whitespace-style' includes the value |
55d1cfe8 | 210 | ;; `space-before-tab::space', replace TABs by SPACEs. |
97a739d5 VJL |
211 | ;; |
212 | ;; 5. SPACEs or TABs at end of line. | |
ae68f2d8 VJL |
213 | ;; If `whitespace-style' includes the value `trailing', remove all |
214 | ;; SPACEs or TABs at end of line. | |
97a739d5 VJL |
215 | ;; |
216 | ;; 6. 8 or more SPACEs after TAB. | |
ae68f2d8 VJL |
217 | ;; If `whitespace-style' includes the value `space-after-tab': |
218 | ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil; | |
219 | ;; otherwise, replace TABs by SPACEs. | |
220 | ;; If `whitespace-style' includes the value `space-after-tab::tab', | |
221 | ;; replace SPACEs by TABs. | |
222 | ;; If `whitespace-style' includes the value | |
55d1cfe8 | 223 | ;; `space-after-tab::space', replace TABs by SPACEs. |
97a739d5 | 224 | ;; |
30d0ade9 VJL |
225 | ;; |
226 | ;; Hooks | |
227 | ;; ----- | |
228 | ;; | |
c9dff4e3 | 229 | ;; whitespace has the following hook variables: |
30d0ade9 | 230 | ;; |
c9dff4e3 VJL |
231 | ;; `whitespace-mode-hook' |
232 | ;; It is evaluated always when whitespace is turned on locally. | |
30d0ade9 | 233 | ;; |
c9dff4e3 VJL |
234 | ;; `global-whitespace-mode-hook' |
235 | ;; It is evaluated always when whitespace is turned on globally. | |
30d0ade9 | 236 | ;; |
c9dff4e3 VJL |
237 | ;; `whitespace-load-hook' |
238 | ;; It is evaluated after whitespace package is loaded. | |
30d0ade9 VJL |
239 | ;; |
240 | ;; | |
241 | ;; Options | |
242 | ;; ------- | |
243 | ;; | |
c9dff4e3 | 244 | ;; Below it's shown a brief description of whitespace options, please, |
30d0ade9 VJL |
245 | ;; see the options declaration in the code for a long documentation. |
246 | ;; | |
ae68f2d8 VJL |
247 | ;; `whitespace-style' Specify which kind of blank is |
248 | ;; visualized. | |
30d0ade9 | 249 | ;; |
c9dff4e3 | 250 | ;; `whitespace-space' Face used to visualize SPACE. |
30d0ade9 | 251 | ;; |
c9dff4e3 | 252 | ;; `whitespace-hspace' Face used to visualize HARD SPACE. |
30d0ade9 | 253 | ;; |
c9dff4e3 | 254 | ;; `whitespace-tab' Face used to visualize TAB. |
30d0ade9 | 255 | ;; |
c9dff4e3 | 256 | ;; `whitespace-newline' Face used to visualize NEWLINE char |
30d0ade9 VJL |
257 | ;; mapping. |
258 | ;; | |
c9dff4e3 | 259 | ;; `whitespace-trailing' Face used to visualize trailing |
30d0ade9 VJL |
260 | ;; blanks. |
261 | ;; | |
c9dff4e3 | 262 | ;; `whitespace-line' Face used to visualize "long" lines. |
30d0ade9 | 263 | ;; |
c9dff4e3 VJL |
264 | ;; `whitespace-space-before-tab' Face used to visualize SPACEs |
265 | ;; before TAB. | |
30d0ade9 | 266 | ;; |
c9dff4e3 | 267 | ;; `whitespace-indentation' Face used to visualize 8 or more |
97a739d5 VJL |
268 | ;; SPACEs at beginning of line. |
269 | ;; | |
c9dff4e3 | 270 | ;; `whitespace-empty' Face used to visualize empty lines at |
97a739d5 VJL |
271 | ;; beginning and/or end of buffer. |
272 | ;; | |
c9dff4e3 | 273 | ;; `whitespace-space-after-tab' Face used to visualize 8 or more |
97a739d5 VJL |
274 | ;; SPACEs after TAB. |
275 | ;; | |
c9dff4e3 | 276 | ;; `whitespace-space-regexp' Specify SPACE characters regexp. |
30d0ade9 | 277 | ;; |
c9dff4e3 | 278 | ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp. |
30d0ade9 | 279 | ;; |
c9dff4e3 | 280 | ;; `whitespace-tab-regexp' Specify TAB characters regexp. |
30d0ade9 | 281 | ;; |
c9dff4e3 | 282 | ;; `whitespace-trailing-regexp' Specify trailing characters regexp. |
30d0ade9 | 283 | ;; |
c9dff4e3 | 284 | ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB |
30d0ade9 VJL |
285 | ;; regexp. |
286 | ;; | |
c9dff4e3 VJL |
287 | ;; `whitespace-indentation-regexp' Specify regexp for 8 or more |
288 | ;; SPACEs at beginning of line. | |
97a739d5 | 289 | ;; |
c9dff4e3 VJL |
290 | ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines |
291 | ;; at beginning of buffer. | |
97a739d5 | 292 | ;; |
c9dff4e3 VJL |
293 | ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines |
294 | ;; at end of buffer. | |
97a739d5 | 295 | ;; |
c9dff4e3 | 296 | ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more |
97a739d5 VJL |
297 | ;; SPACEs after TAB. |
298 | ;; | |
c9dff4e3 | 299 | ;; `whitespace-line-column' Specify column beyond which the line |
30d0ade9 VJL |
300 | ;; is highlighted. |
301 | ;; | |
c9dff4e3 VJL |
302 | ;; `whitespace-display-mappings' Specify an alist of mappings |
303 | ;; for displaying characters. | |
30d0ade9 | 304 | ;; |
4a4b61e2 VJL |
305 | ;; `whitespace-global-modes' Modes for which global |
306 | ;; `whitespace-mode' is automagically | |
307 | ;; turned on. | |
30d0ade9 | 308 | ;; |
94dc593f | 309 | ;; `whitespace-action' Specify which action is taken when a |
97f6e1ad | 310 | ;; buffer is visited or written. |
94dc593f | 311 | ;; |
30d0ade9 VJL |
312 | ;; |
313 | ;; Acknowledgements | |
314 | ;; ---------------- | |
315 | ;; | |
24deb97d VJL |
316 | ;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a |
317 | ;; `whitespace-newline' initialization with low contrast relative to | |
318 | ;; the background color. | |
319 | ;; | |
55d1cfe8 VJL |
320 | ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the |
321 | ;; `indent-tabs-mode' usage suggestion. | |
322 | ;; | |
323 | ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook | |
97f6e1ad VJL |
324 | ;; actions when buffer is written as the original whitespace package |
325 | ;; had. | |
94dc593f | 326 | ;; |
9f3b76d5 VJL |
327 | ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long" |
328 | ;; lines tail. See EightyColumnRule (EmacsWiki). | |
329 | ;; | |
30d0ade9 VJL |
330 | ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting: |
331 | ;; * `define-minor-mode'. | |
c9dff4e3 | 332 | ;; * `global-whitespace-*' name for global commands. |
30d0ade9 VJL |
333 | ;; |
334 | ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing. | |
335 | ;; | |
336 | ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands | |
337 | ;; suggestion. | |
338 | ;; | |
339 | ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for | |
340 | ;; helping to fix `find-file-hooks' reference. | |
341 | ;; | |
342 | ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for | |
343 | ;; indicating defface byte-compilation warnings. | |
344 | ;; | |
345 | ;; Thanks to TimOCallaghan (EmacsWiki) for the idea about highlight | |
9f3b76d5 | 346 | ;; "long" lines. See EightyColumnRule (EmacsWiki). |
30d0ade9 VJL |
347 | ;; |
348 | ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new | |
cd222e44 | 349 | ;; NEWLINE character mapping. |
30d0ade9 VJL |
350 | ;; |
351 | ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating | |
c9dff4e3 | 352 | ;; whitespace-mode.el on XEmacs. |
30d0ade9 VJL |
353 | ;; |
354 | ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via | |
355 | ;; visws.el (his code was modified, but the main idea was kept). | |
356 | ;; | |
357 | ;; Thanks to: | |
c9dff4e3 | 358 | ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el |
30d0ade9 VJL |
359 | ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el |
360 | ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el | |
361 | ;; Miles Bader <miles@gnu.org> visws.el | |
362 | ;; And to all people who contributed with them. | |
363 | ;; | |
364 | ;; | |
365 | ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
366 | ||
367 | ;;; code: | |
368 | ||
369 | \f | |
370 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
371 | ;;;; User Variables: | |
372 | ||
373 | ||
374 | ;;; Interface to the command system | |
375 | ||
376 | ||
c9dff4e3 | 377 | (defgroup whitespace nil |
040f578c | 378 | "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)." |
c9dff4e3 | 379 | :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el") |
b502217b | 380 | :version "23.1" |
eba5b4dd | 381 | :group 'convenience) |
30d0ade9 VJL |
382 | |
383 | ||
ae68f2d8 | 384 | (defcustom whitespace-style |
85cc3d4f VJL |
385 | '(face |
386 | tabs spaces trailing lines space-before-tab newline | |
387 | indentation empty space-after-tab | |
388 | space-mark tab-mark newline-mark) | |
9201cc28 | 389 | "Specify which kind of blank is visualized. |
30d0ade9 | 390 | |
b502217b | 391 | It's a list containing some or all of the following values: |
30d0ade9 | 392 | |
85cc3d4f VJL |
393 | face enable all visualization via faces (see below). |
394 | ||
ae68f2d8 | 395 | trailing trailing blanks are visualized via faces. |
85cc3d4f VJL |
396 | It has effect only if `face' (see above) |
397 | is present in `whitespace-style'. | |
30d0ade9 | 398 | |
ae68f2d8 | 399 | tabs TABs are visualized via faces. |
85cc3d4f VJL |
400 | It has effect only if `face' (see above) |
401 | is present in `whitespace-style'. | |
30d0ade9 | 402 | |
ae68f2d8 VJL |
403 | spaces SPACEs and HARD SPACEs are visualized via |
404 | faces. | |
85cc3d4f VJL |
405 | It has effect only if `face' (see above) |
406 | is present in `whitespace-style'. | |
30d0ade9 | 407 | |
db9e401b | 408 | lines lines which have columns beyond |
ae68f2d8 | 409 | `whitespace-line-column' are highlighted via |
db9e401b | 410 | faces. |
9f3b76d5 | 411 | Whole line is highlighted. |
ae68f2d8 VJL |
412 | It has precedence over `lines-tail' (see |
413 | below). | |
85cc3d4f VJL |
414 | It has effect only if `face' (see above) |
415 | is present in `whitespace-style'. | |
9f3b76d5 | 416 | |
db9e401b | 417 | lines-tail lines which have columns beyond |
ae68f2d8 VJL |
418 | `whitespace-line-column' are highlighted via |
419 | faces. | |
9f3b76d5 | 420 | But only the part of line which goes |
c9dff4e3 | 421 | beyond `whitespace-line-column' column. |
9f3b76d5 | 422 | It has effect only if `lines' (see above) |
85cc3d4f VJL |
423 | is not present in `whitespace-style' |
424 | and if `face' (see above) is present in | |
425 | `whitespace-style'. | |
30d0ade9 | 426 | |
ae68f2d8 | 427 | newline NEWLINEs are visualized via faces. |
85cc3d4f VJL |
428 | It has effect only if `face' (see above) |
429 | is present in `whitespace-style'. | |
30d0ade9 | 430 | |
97a739d5 | 431 | empty empty lines at beginning and/or end of buffer |
ae68f2d8 | 432 | are visualized via faces. |
85cc3d4f VJL |
433 | It has effect only if `face' (see above) |
434 | is present in `whitespace-style'. | |
97a739d5 | 435 | |
55d1cfe8 | 436 | indentation::tab 8 or more SPACEs at beginning of line are |
ae68f2d8 | 437 | visualized via faces. |
85cc3d4f VJL |
438 | It has effect only if `face' (see above) |
439 | is present in `whitespace-style'. | |
55d1cfe8 | 440 | |
ae68f2d8 VJL |
441 | indentation::space TABs at beginning of line are visualized via |
442 | faces. | |
85cc3d4f VJL |
443 | It has effect only if `face' (see above) |
444 | is present in `whitespace-style'. | |
97a739d5 | 445 | |
55d1cfe8 VJL |
446 | indentation 8 or more SPACEs at beginning of line are |
447 | visualized, if `indent-tabs-mode' (which see) | |
448 | is non-nil; otherwise, TABs at beginning of | |
ae68f2d8 | 449 | line are visualized via faces. |
85cc3d4f VJL |
450 | It has effect only if `face' (see above) |
451 | is present in `whitespace-style'. | |
55d1cfe8 VJL |
452 | |
453 | space-after-tab::tab 8 or more SPACEs after a TAB are | |
ae68f2d8 | 454 | visualized via faces. |
85cc3d4f VJL |
455 | It has effect only if `face' (see above) |
456 | is present in `whitespace-style'. | |
55d1cfe8 | 457 | |
f24b8bf3 CY |
458 | space-after-tab::space TABs are visualized when 8 or more |
459 | SPACEs occur after a TAB, via faces. | |
85cc3d4f VJL |
460 | It has effect only if `face' (see above) |
461 | is present in `whitespace-style'. | |
55d1cfe8 VJL |
462 | |
463 | space-after-tab 8 or more SPACEs after a TAB are | |
464 | visualized, if `indent-tabs-mode' | |
465 | (which see) is non-nil; otherwise, | |
ae68f2d8 | 466 | the TABs are visualized via faces. |
85cc3d4f VJL |
467 | It has effect only if `face' (see above) |
468 | is present in `whitespace-style'. | |
55d1cfe8 | 469 | |
ae68f2d8 VJL |
470 | space-before-tab::tab SPACEs before TAB are visualized via |
471 | faces. | |
85cc3d4f VJL |
472 | It has effect only if `face' (see above) |
473 | is present in `whitespace-style'. | |
30d0ade9 | 474 | |
f24b8bf3 CY |
475 | space-before-tab::space TABs are visualized when SPACEs occur |
476 | before TAB, via faces. | |
85cc3d4f VJL |
477 | It has effect only if `face' (see above) |
478 | is present in `whitespace-style'. | |
30d0ade9 | 479 | |
55d1cfe8 VJL |
480 | space-before-tab SPACEs before TAB are visualized, if |
481 | `indent-tabs-mode' (which see) is | |
482 | non-nil; otherwise, the TABs are | |
ae68f2d8 | 483 | visualized via faces. |
85cc3d4f VJL |
484 | It has effect only if `face' (see above) |
485 | is present in `whitespace-style'. | |
ae68f2d8 VJL |
486 | |
487 | space-mark SPACEs and HARD SPACEs are visualized via | |
488 | display table. | |
489 | ||
490 | tab-mark TABs are visualized via display table. | |
491 | ||
492 | newline-mark NEWLINEs are visualized via display table. | |
55d1cfe8 VJL |
493 | |
494 | Any other value is ignored. | |
495 | ||
ae68f2d8 VJL |
496 | If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and |
497 | via display table. | |
55d1cfe8 | 498 | |
db9e401b | 499 | There is an evaluation order for some values, if they are |
ae68f2d8 | 500 | included in `whitespace-style' list. For example, if |
55d1cfe8 | 501 | indentation, indentation::tab and/or indentation::space are |
ae68f2d8 VJL |
502 | included in `whitespace-style' list. The evaluation order for |
503 | these values is: | |
55d1cfe8 VJL |
504 | |
505 | * For indentation: | |
506 | 1. indentation | |
507 | 2. indentation::tab | |
508 | 3. indentation::space | |
509 | ||
510 | * For SPACEs after TABs: | |
511 | 1. space-after-tab | |
512 | 2. space-after-tab::tab | |
513 | 3. space-after-tab::space | |
514 | ||
515 | * For SPACEs before TABs: | |
516 | 1. space-before-tab | |
517 | 2. space-before-tab::tab | |
518 | 3. space-before-tab::space | |
519 | ||
520 | So, for example, if indentation and indentation::space are | |
ae68f2d8 VJL |
521 | included in `whitespace-style' list, the indentation value is |
522 | evaluated instead of indentation::space value. | |
523 | ||
85cc3d4f VJL |
524 | One reason for not visualize spaces via faces (if `face' is not |
525 | included in `whitespace-style') is to use exclusively for | |
526 | cleanning up a buffer. See `whitespace-cleanup' and | |
527 | `whitespace-cleanup-region' for documentation. | |
528 | ||
ae68f2d8 VJL |
529 | See also `whitespace-display-mappings' for documentation." |
530 | :type '(repeat :tag "Kind of Blank" | |
55d1cfe8 | 531 | (choice :tag "Kind of Blank Face" |
1a868076 VJL |
532 | (const :tag "(Face) Face visualization" |
533 | face) | |
ae68f2d8 | 534 | (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs" |
30d0ade9 | 535 | trailing) |
ae68f2d8 VJL |
536 | (const :tag "(Face) SPACEs and HARD SPACEs" |
537 | spaces) | |
538 | (const :tag "(Face) TABs" tabs) | |
539 | (const :tag "(Face) Lines" lines) | |
540 | (const :tag "(Face) SPACEs before TAB" | |
30d0ade9 | 541 | space-before-tab) |
ae68f2d8 VJL |
542 | (const :tag "(Face) NEWLINEs" newline) |
543 | (const :tag "(Face) Indentation SPACEs" | |
544 | indentation) | |
545 | (const :tag "(Face) Empty Lines At BOB And/Or EOB" | |
97a739d5 | 546 | empty) |
ae68f2d8 VJL |
547 | (const :tag "(Face) SPACEs after TAB" |
548 | space-after-tab) | |
549 | (const :tag "(Mark) SPACEs and HARD SPACEs" | |
550 | space-mark) | |
551 | (const :tag "(Mark) TABs" tab-mark) | |
552 | (const :tag "(Mark) NEWLINEs" newline-mark))) | |
c9dff4e3 | 553 | :group 'whitespace) |
30d0ade9 VJL |
554 | |
555 | ||
c9dff4e3 | 556 | (defcustom whitespace-space 'whitespace-space |
9201cc28 | 557 | "Symbol face used to visualize SPACE. |
30d0ade9 | 558 | |
ae68f2d8 | 559 | Used when `whitespace-style' includes the value `spaces'." |
30d0ade9 | 560 | :type 'face |
c9dff4e3 | 561 | :group 'whitespace) |
30d0ade9 VJL |
562 | |
563 | ||
c9dff4e3 | 564 | (defface whitespace-space |
30d0ade9 | 565 | '((((class color) (background dark)) |
85cc3d4f | 566 | (:background "grey20" :foreground "darkgray")) |
30d0ade9 | 567 | (((class color) (background light)) |
85cc3d4f | 568 | (:background "LightYellow" :foreground "lightgray")) |
30d0ade9 | 569 | (t (:inverse-video t))) |
040f578c | 570 | "Face used to visualize SPACE." |
c9dff4e3 | 571 | :group 'whitespace) |
30d0ade9 VJL |
572 | |
573 | ||
c9dff4e3 | 574 | (defcustom whitespace-hspace 'whitespace-hspace |
9201cc28 | 575 | "Symbol face used to visualize HARD SPACE. |
30d0ade9 | 576 | |
ae68f2d8 | 577 | Used when `whitespace-style' includes the value `spaces'." |
30d0ade9 | 578 | :type 'face |
c9dff4e3 | 579 | :group 'whitespace) |
30d0ade9 VJL |
580 | |
581 | ||
c9dff4e3 | 582 | (defface whitespace-hspace ; 'nobreak-space |
30d0ade9 | 583 | '((((class color) (background dark)) |
85cc3d4f | 584 | (:background "grey24" :foreground "darkgray")) |
30d0ade9 | 585 | (((class color) (background light)) |
85cc3d4f | 586 | (:background "LemonChiffon3" :foreground "lightgray")) |
30d0ade9 | 587 | (t (:inverse-video t))) |
040f578c | 588 | "Face used to visualize HARD SPACE." |
c9dff4e3 | 589 | :group 'whitespace) |
30d0ade9 VJL |
590 | |
591 | ||
c9dff4e3 | 592 | (defcustom whitespace-tab 'whitespace-tab |
9201cc28 | 593 | "Symbol face used to visualize TAB. |
30d0ade9 | 594 | |
ae68f2d8 | 595 | Used when `whitespace-style' includes the value `tabs'." |
30d0ade9 | 596 | :type 'face |
c9dff4e3 | 597 | :group 'whitespace) |
30d0ade9 VJL |
598 | |
599 | ||
c9dff4e3 | 600 | (defface whitespace-tab |
30d0ade9 | 601 | '((((class color) (background dark)) |
85cc3d4f | 602 | (:background "grey22" :foreground "darkgray")) |
30d0ade9 | 603 | (((class color) (background light)) |
85cc3d4f | 604 | (:background "beige" :foreground "lightgray")) |
30d0ade9 | 605 | (t (:inverse-video t))) |
040f578c | 606 | "Face used to visualize TAB." |
c9dff4e3 | 607 | :group 'whitespace) |
30d0ade9 VJL |
608 | |
609 | ||
c9dff4e3 | 610 | (defcustom whitespace-newline 'whitespace-newline |
9201cc28 | 611 | "Symbol face used to visualize NEWLINE char mapping. |
30d0ade9 | 612 | |
c9dff4e3 | 613 | See `whitespace-display-mappings'. |
30d0ade9 | 614 | |
ae68f2d8 VJL |
615 | Used when `whitespace-style' includes the values `newline-mark' |
616 | and `newline'." | |
30d0ade9 | 617 | :type 'face |
c9dff4e3 | 618 | :group 'whitespace) |
30d0ade9 VJL |
619 | |
620 | ||
c9dff4e3 | 621 | (defface whitespace-newline |
30d0ade9 | 622 | '((((class color) (background dark)) |
17862016 | 623 | (:foreground "darkgray" :bold nil)) |
30d0ade9 | 624 | (((class color) (background light)) |
17862016 VJL |
625 | (:foreground "lightgray" :bold nil)) |
626 | (t (:underline t :bold nil))) | |
040f578c | 627 | "Face used to visualize NEWLINE char mapping. |
30d0ade9 | 628 | |
c9dff4e3 VJL |
629 | See `whitespace-display-mappings'." |
630 | :group 'whitespace) | |
30d0ade9 VJL |
631 | |
632 | ||
c9dff4e3 | 633 | (defcustom whitespace-trailing 'whitespace-trailing |
9201cc28 | 634 | "Symbol face used to visualize trailing blanks. |
30d0ade9 | 635 | |
ae68f2d8 | 636 | Used when `whitespace-style' includes the value `trailing'." |
30d0ade9 | 637 | :type 'face |
c9dff4e3 | 638 | :group 'whitespace) |
30d0ade9 VJL |
639 | |
640 | ||
c9dff4e3 | 641 | (defface whitespace-trailing ; 'trailing-whitespace |
30d0ade9 VJL |
642 | '((((class mono)) (:inverse-video t :bold t :underline t)) |
643 | (t (:background "red1" :foreground "yellow" :bold t))) | |
040f578c | 644 | "Face used to visualize trailing blanks." |
c9dff4e3 | 645 | :group 'whitespace) |
30d0ade9 VJL |
646 | |
647 | ||
c9dff4e3 | 648 | (defcustom whitespace-line 'whitespace-line |
9201cc28 | 649 | "Symbol face used to visualize \"long\" lines. |
30d0ade9 | 650 | |
c9dff4e3 | 651 | See `whitespace-line-column'. |
30d0ade9 | 652 | |
ae68f2d8 | 653 | Used when `whitespace-style' includes the value `line'." |
30d0ade9 | 654 | :type 'face |
c9dff4e3 | 655 | :group 'whitespace) |
30d0ade9 VJL |
656 | |
657 | ||
c9dff4e3 | 658 | (defface whitespace-line |
30d0ade9 VJL |
659 | '((((class mono)) (:inverse-video t :bold t :underline t)) |
660 | (t (:background "gray20" :foreground "violet"))) | |
040f578c | 661 | "Face used to visualize \"long\" lines. |
30d0ade9 | 662 | |
c9dff4e3 VJL |
663 | See `whitespace-line-column'." |
664 | :group 'whitespace) | |
30d0ade9 VJL |
665 | |
666 | ||
c9dff4e3 | 667 | (defcustom whitespace-space-before-tab 'whitespace-space-before-tab |
9201cc28 | 668 | "Symbol face used to visualize SPACEs before TAB. |
30d0ade9 | 669 | |
ae68f2d8 | 670 | Used when `whitespace-style' includes the value `space-before-tab'." |
30d0ade9 | 671 | :type 'face |
c9dff4e3 | 672 | :group 'whitespace) |
30d0ade9 VJL |
673 | |
674 | ||
c9dff4e3 | 675 | (defface whitespace-space-before-tab |
30d0ade9 VJL |
676 | '((((class mono)) (:inverse-video t :bold t :underline t)) |
677 | (t (:background "DarkOrange" :foreground "firebrick"))) | |
040f578c | 678 | "Face used to visualize SPACEs before TAB." |
c9dff4e3 | 679 | :group 'whitespace) |
30d0ade9 VJL |
680 | |
681 | ||
c9dff4e3 | 682 | (defcustom whitespace-indentation 'whitespace-indentation |
9201cc28 | 683 | "Symbol face used to visualize 8 or more SPACEs at beginning of line. |
97a739d5 | 684 | |
ae68f2d8 | 685 | Used when `whitespace-style' includes the value `indentation'." |
97a739d5 | 686 | :type 'face |
c9dff4e3 | 687 | :group 'whitespace) |
97a739d5 VJL |
688 | |
689 | ||
c9dff4e3 | 690 | (defface whitespace-indentation |
97a739d5 VJL |
691 | '((((class mono)) (:inverse-video t :bold t :underline t)) |
692 | (t (:background "yellow" :foreground "firebrick"))) | |
693 | "Face used to visualize 8 or more SPACEs at beginning of line." | |
c9dff4e3 | 694 | :group 'whitespace) |
97a739d5 VJL |
695 | |
696 | ||
c9dff4e3 | 697 | (defcustom whitespace-empty 'whitespace-empty |
9201cc28 | 698 | "Symbol face used to visualize empty lines at beginning and/or end of buffer. |
97a739d5 | 699 | |
ae68f2d8 | 700 | Used when `whitespace-style' includes the value `empty'." |
97a739d5 | 701 | :type 'face |
c9dff4e3 | 702 | :group 'whitespace) |
97a739d5 VJL |
703 | |
704 | ||
c9dff4e3 | 705 | (defface whitespace-empty |
97a739d5 VJL |
706 | '((((class mono)) (:inverse-video t :bold t :underline t)) |
707 | (t (:background "yellow" :foreground "firebrick"))) | |
708 | "Face used to visualize empty lines at beginning and/or end of buffer." | |
c9dff4e3 | 709 | :group 'whitespace) |
97a739d5 VJL |
710 | |
711 | ||
c9dff4e3 | 712 | (defcustom whitespace-space-after-tab 'whitespace-space-after-tab |
9201cc28 | 713 | "Symbol face used to visualize 8 or more SPACEs after TAB. |
97a739d5 | 714 | |
ae68f2d8 | 715 | Used when `whitespace-style' includes the value `space-after-tab'." |
97a739d5 | 716 | :type 'face |
c9dff4e3 | 717 | :group 'whitespace) |
97a739d5 VJL |
718 | |
719 | ||
c9dff4e3 | 720 | (defface whitespace-space-after-tab |
97a739d5 VJL |
721 | '((((class mono)) (:inverse-video t :bold t :underline t)) |
722 | (t (:background "yellow" :foreground "firebrick"))) | |
723 | "Face used to visualize 8 or more SPACEs after TAB." | |
c9dff4e3 | 724 | :group 'whitespace) |
97a739d5 VJL |
725 | |
726 | ||
c9dff4e3 | 727 | (defcustom whitespace-hspace-regexp |
30d0ade9 | 728 | "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)" |
9201cc28 | 729 | "Specify HARD SPACE characters regexp. |
30d0ade9 | 730 | |
b502217b | 731 | If you're using `mule' package, there may be other characters besides: |
30d0ade9 VJL |
732 | |
733 | \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\" | |
734 | ||
040f578c | 735 | that should be considered HARD SPACE. |
30d0ade9 VJL |
736 | |
737 | Here are some examples: | |
738 | ||
739 | \"\\\\(^\\xA0+\\\\)\" \ | |
040f578c | 740 | visualize only leading HARD SPACEs. |
30d0ade9 | 741 | \"\\\\(\\xA0+$\\\\)\" \ |
040f578c | 742 | visualize only trailing HARD SPACEs. |
30d0ade9 | 743 | \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \ |
040f578c | 744 | visualize leading and/or trailing HARD SPACEs. |
30d0ade9 | 745 | \"\\t\\\\(\\xA0+\\\\)\\t\" \ |
040f578c | 746 | visualize only HARD SPACEs between TABs. |
30d0ade9 VJL |
747 | |
748 | NOTE: Enclose always by \\\\( and \\\\) the elements to highlight. | |
749 | Use exactly one pair of enclosing \\\\( and \\\\). | |
750 | ||
ae68f2d8 | 751 | Used when `whitespace-style' includes `spaces'." |
30d0ade9 | 752 | :type '(regexp :tag "HARD SPACE Chars") |
c9dff4e3 | 753 | :group 'whitespace) |
30d0ade9 VJL |
754 | |
755 | ||
c9dff4e3 | 756 | (defcustom whitespace-space-regexp "\\( +\\)" |
9201cc28 | 757 | "Specify SPACE characters regexp. |
30d0ade9 | 758 | |
b502217b | 759 | If you're using `mule' package, there may be other characters |
040f578c | 760 | besides \" \" that should be considered SPACE. |
30d0ade9 VJL |
761 | |
762 | Here are some examples: | |
763 | ||
040f578c VJL |
764 | \"\\\\(^ +\\\\)\" visualize only leading SPACEs. |
765 | \"\\\\( +$\\\\)\" visualize only trailing SPACEs. | |
30d0ade9 | 766 | \"\\\\(^ +\\\\| +$\\\\)\" \ |
040f578c VJL |
767 | visualize leading and/or trailing SPACEs. |
768 | \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs. | |
30d0ade9 VJL |
769 | |
770 | NOTE: Enclose always by \\\\( and \\\\) the elements to highlight. | |
771 | Use exactly one pair of enclosing \\\\( and \\\\). | |
772 | ||
ae68f2d8 | 773 | Used when `whitespace-style' includes `spaces'." |
30d0ade9 | 774 | :type '(regexp :tag "SPACE Chars") |
c9dff4e3 | 775 | :group 'whitespace) |
30d0ade9 VJL |
776 | |
777 | ||
c9dff4e3 | 778 | (defcustom whitespace-tab-regexp "\\(\t+\\)" |
9201cc28 | 779 | "Specify TAB characters regexp. |
30d0ade9 | 780 | |
b502217b | 781 | If you're using `mule' package, there may be other characters |
040f578c | 782 | besides \"\\t\" that should be considered TAB. |
30d0ade9 VJL |
783 | |
784 | Here are some examples: | |
785 | ||
040f578c VJL |
786 | \"\\\\(^\\t+\\\\)\" visualize only leading TABs. |
787 | \"\\\\(\\t+$\\\\)\" visualize only trailing TABs. | |
30d0ade9 | 788 | \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \ |
040f578c VJL |
789 | visualize leading and/or trailing TABs. |
790 | \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs. | |
30d0ade9 VJL |
791 | |
792 | NOTE: Enclose always by \\\\( and \\\\) the elements to highlight. | |
793 | Use exactly one pair of enclosing \\\\( and \\\\). | |
794 | ||
ae68f2d8 | 795 | Used when `whitespace-style' includes `tabs'." |
30d0ade9 | 796 | :type '(regexp :tag "TAB Chars") |
c9dff4e3 | 797 | :group 'whitespace) |
30d0ade9 VJL |
798 | |
799 | ||
c9dff4e3 | 800 | (defcustom whitespace-trailing-regexp |
2788143d | 801 | "\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$" |
9201cc28 | 802 | "Specify trailing characters regexp. |
30d0ade9 | 803 | |
b502217b | 804 | If you're using `mule' package, there may be other characters besides: |
30d0ade9 VJL |
805 | |
806 | \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | |
807 | \"\\xF20\" | |
808 | ||
040f578c | 809 | that should be considered blank. |
30d0ade9 | 810 | |
385da4e7 | 811 | NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight. |
2788143d | 812 | Use exactly one pair of enclosing elements above. |
30d0ade9 | 813 | |
ae68f2d8 | 814 | Used when `whitespace-style' includes `trailing'." |
30d0ade9 | 815 | :type '(regexp :tag "Trailing Chars") |
c9dff4e3 | 816 | :group 'whitespace) |
30d0ade9 VJL |
817 | |
818 | ||
55d1cfe8 | 819 | (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)" |
9201cc28 | 820 | "Specify SPACEs before TAB regexp. |
30d0ade9 | 821 | |
b502217b | 822 | If you're using `mule' package, there may be other characters besides: |
30d0ade9 VJL |
823 | |
824 | \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | |
825 | \"\\xF20\" | |
826 | ||
040f578c | 827 | that should be considered blank. |
30d0ade9 | 828 | |
ae68f2d8 | 829 | Used when `whitespace-style' includes `space-before-tab', |
55d1cfe8 | 830 | `space-before-tab::tab' or `space-before-tab::space'." |
30d0ade9 | 831 | :type '(regexp :tag "SPACEs Before TAB") |
c9dff4e3 | 832 | :group 'whitespace) |
30d0ade9 VJL |
833 | |
834 | ||
c9dff4e3 | 835 | (defcustom whitespace-indentation-regexp |
55d1cfe8 VJL |
836 | '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]" |
837 | . "^ *\\(\t+\\)[^\n]") | |
9201cc28 | 838 | "Specify regexp for 8 or more SPACEs at beginning of line. |
97a739d5 | 839 | |
55d1cfe8 VJL |
840 | It is a cons where the cons car is used for SPACEs visualization |
841 | and the cons cdr is used for TABs visualization. | |
842 | ||
b502217b | 843 | If you're using `mule' package, there may be other characters besides: |
97a739d5 VJL |
844 | |
845 | \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | |
846 | \"\\xF20\" | |
847 | ||
848 | that should be considered blank. | |
849 | ||
ae68f2d8 | 850 | Used when `whitespace-style' includes `indentation', |
55d1cfe8 VJL |
851 | `indentation::tab' or `indentation::space'." |
852 | :type '(cons (regexp :tag "Indentation SPACEs") | |
853 | (regexp :tag "Indentation TABs")) | |
c9dff4e3 | 854 | :group 'whitespace) |
97a739d5 VJL |
855 | |
856 | ||
80525855 | 857 | (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)" |
9201cc28 | 858 | "Specify regexp for empty lines at beginning of buffer. |
97a739d5 | 859 | |
b502217b | 860 | If you're using `mule' package, there may be other characters besides: |
97a739d5 VJL |
861 | |
862 | \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | |
863 | \"\\xF20\" | |
864 | ||
865 | that should be considered blank. | |
866 | ||
ae68f2d8 | 867 | Used when `whitespace-style' includes `empty'." |
97a739d5 | 868 | :type '(regexp :tag "Empty Lines At Beginning Of Buffer") |
c9dff4e3 | 869 | :group 'whitespace) |
97a739d5 VJL |
870 | |
871 | ||
80525855 | 872 | (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)" |
9201cc28 | 873 | "Specify regexp for empty lines at end of buffer. |
97a739d5 | 874 | |
b502217b | 875 | If you're using `mule' package, there may be other characters besides: |
97a739d5 VJL |
876 | |
877 | \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | |
878 | \"\\xF20\" | |
879 | ||
880 | that should be considered blank. | |
881 | ||
ae68f2d8 | 882 | Used when `whitespace-style' includes `empty'." |
97a739d5 | 883 | :type '(regexp :tag "Empty Lines At End Of Buffer") |
c9dff4e3 | 884 | :group 'whitespace) |
97a739d5 VJL |
885 | |
886 | ||
55d1cfe8 VJL |
887 | (defcustom whitespace-space-after-tab-regexp |
888 | '("\t+\\(\\( \\{%d\\}\\)+\\)" | |
889 | . "\\(\t+\\) +") | |
9201cc28 | 890 | "Specify regexp for 8 or more SPACEs after TAB. |
97a739d5 | 891 | |
55d1cfe8 VJL |
892 | It is a cons where the cons car is used for SPACEs visualization |
893 | and the cons cdr is used for TABs visualization. | |
894 | ||
b502217b | 895 | If you're using `mule' package, there may be other characters besides: |
97a739d5 VJL |
896 | |
897 | \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \ | |
898 | \"\\xF20\" | |
899 | ||
900 | that should be considered blank. | |
901 | ||
ae68f2d8 | 902 | Used when `whitespace-style' includes `space-after-tab', |
55d1cfe8 | 903 | `space-after-tab::tab' or `space-after-tab::space'." |
97a739d5 | 904 | :type '(regexp :tag "SPACEs After TAB") |
c9dff4e3 | 905 | :group 'whitespace) |
97a739d5 VJL |
906 | |
907 | ||
c9dff4e3 | 908 | (defcustom whitespace-line-column 80 |
9201cc28 | 909 | "Specify column beyond which the line is highlighted. |
30d0ade9 | 910 | |
85cc3d4f VJL |
911 | It must be an integer or nil. If nil, the `fill-column' variable value is |
912 | used. | |
913 | ||
ae68f2d8 | 914 | Used when `whitespace-style' includes `lines' or `lines-tail'." |
85cc3d4f VJL |
915 | :type '(choice :tag "Line Length Limit" |
916 | (integer :tag "Line Length") | |
917 | (const :tag "Use fill-column" nil)) | |
c9dff4e3 | 918 | :group 'whitespace) |
30d0ade9 VJL |
919 | |
920 | ||
921 | ;; Hacked from `visible-whitespace-mappings' in visws.el | |
c9dff4e3 | 922 | (defcustom whitespace-display-mappings |
30d0ade9 | 923 | '( |
32a9841c VJL |
924 | (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot |
925 | (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency | |
55d1cfe8 VJL |
926 | (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency |
927 | (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency | |
928 | (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency | |
929 | (space-mark ?\xF20 [?\xF24] [?_]) ; 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 |
55d1cfe8 VJL |
934 | ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore |
935 | ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation | |
936 | ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade | |
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 | |
947 | Each element has the following form: | |
948 | ||
55d1cfe8 | 949 | (KIND CHAR VECTOR...) |
30d0ade9 VJL |
950 | |
951 | Where: | |
952 | ||
55d1cfe8 VJL |
953 | KIND 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 |
962 | CHAR is the character to be mapped. |
963 | ||
964 | VECTOR 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 | ||
969 | The NEWLINE character is displayed using the face given by | |
55d1cfe8 | 970 | `whitespace-newline' variable. |
30d0ade9 | 971 | |
ae68f2d8 VJL |
972 | Used 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 |
992 | Global `whitespace-mode' is controlled by the command |
993 | `global-whitespace-mode'. | |
30d0ade9 | 994 | |
c9dff4e3 | 995 | If nil, means no modes have `whitespace-mode' automatically |
30d0ade9 | 996 | turned on. |
c9dff4e3 VJL |
997 | |
998 | If t, all modes that support `whitespace-mode' have it | |
999 | automatically turned on. | |
1000 | ||
1001 | Else it should be a list of `major-mode' symbol names for which | |
1002 | `whitespace-mode' should be automatically turned on. The sense | |
30d0ade9 VJL |
1003 | of the list is negated if it begins with `not'. For example: |
1004 | ||
1005 | (c-mode c++-mode) | |
1006 | ||
c9dff4e3 VJL |
1007 | means that `whitespace-mode' is turned on for buffers in C and |
1008 | C++ 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 | |
1023 | It'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 |
1047 | Any 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 VJL |
1065 | (define-minor-mode whitespace-mode |
1066 | "Toggle whitespace minor mode visualization (\"ws\" on modeline). | |
30d0ade9 | 1067 | |
c9dff4e3 | 1068 | If ARG is null, toggle whitespace visualization. |
040f578c VJL |
1069 | If ARG is a number greater than zero, turn on visualization; |
1070 | otherwise, turn off visualization. | |
cd222e44 VJL |
1071 | |
1072 | See also `whitespace-style', `whitespace-newline' and | |
1073 | `whitespace-display-mappings'." | |
c9dff4e3 | 1074 | :lighter " ws" |
30d0ade9 VJL |
1075 | :init-value nil |
1076 | :global nil | |
c9dff4e3 | 1077 | :group 'whitespace |
30d0ade9 VJL |
1078 | (cond |
1079 | (noninteractive ; running a batch job | |
c9dff4e3 VJL |
1080 | (setq whitespace-mode nil)) |
1081 | (whitespace-mode ; whitespace-mode on | |
94dc593f VJL |
1082 | (whitespace-turn-on) |
1083 | (whitespace-action-when-on)) | |
c9dff4e3 VJL |
1084 | (t ; whitespace-mode off |
1085 | (whitespace-turn-off)))) | |
30d0ade9 | 1086 | |
8b40bb55 VJL |
1087 | |
1088 | ;;;###autoload | |
1089 | (define-minor-mode whitespace-newline-mode | |
cd222e44 | 1090 | "Toggle NEWLINE minor mode visualization (\"nl\" on modeline). |
8b40bb55 | 1091 | |
cd222e44 | 1092 | If ARG is null, toggle NEWLINE visualization. |
8b40bb55 VJL |
1093 | If ARG is a number greater than zero, turn on visualization; |
1094 | otherwise, turn off visualization. | |
8b40bb55 | 1095 | |
cd222e44 VJL |
1096 | Use `whitespace-newline-mode' only for NEWLINE visualization |
1097 | exclusively. For other visualizations, including NEWLINE | |
1098 | visualization together with (HARD) SPACEs and/or TABs, please, | |
1099 | use `whitespace-mode'. | |
1100 | ||
1101 | See also `whitespace-newline' and `whitespace-display-mappings'." | |
8b40bb55 VJL |
1102 | :lighter " nl" |
1103 | :init-value nil | |
1104 | :global nil | |
1105 | :group 'whitespace | |
650453a9 | 1106 | (let ((whitespace-style '(face newline-mark newline))) |
8b40bb55 VJL |
1107 | (whitespace-mode whitespace-newline-mode) |
1108 | ;; sync states (running a batch job) | |
1109 | (setq whitespace-newline-mode whitespace-mode))) | |
1110 | ||
30d0ade9 VJL |
1111 | \f |
1112 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
1113 | ;;;; User commands - Global mode | |
1114 | ||
1115 | ||
55d1cfe8 | 1116 | ;;;###autoload |
c9dff4e3 VJL |
1117 | (define-minor-mode global-whitespace-mode |
1118 | "Toggle whitespace global minor mode visualization (\"WS\" on modeline). | |
30d0ade9 | 1119 | |
c9dff4e3 | 1120 | If ARG is null, toggle whitespace visualization. |
040f578c VJL |
1121 | If ARG is a number greater than zero, turn on visualization; |
1122 | otherwise, turn off visualization. | |
cd222e44 VJL |
1123 | |
1124 | See also `whitespace-style', `whitespace-newline' and | |
1125 | `whitespace-display-mappings'." | |
acaf02dd | 1126 | :lighter " WS" |
30d0ade9 VJL |
1127 | :init-value nil |
1128 | :global t | |
c9dff4e3 | 1129 | :group 'whitespace |
30d0ade9 VJL |
1130 | (cond |
1131 | (noninteractive ; running a batch job | |
c9dff4e3 VJL |
1132 | (setq global-whitespace-mode nil)) |
1133 | (global-whitespace-mode ; global-whitespace-mode on | |
30d0ade9 | 1134 | (save-excursion |
94dc593f | 1135 | (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled) |
30d0ade9 VJL |
1136 | (dolist (buffer (buffer-list)) ; adjust all local mode |
1137 | (set-buffer buffer) | |
c9dff4e3 VJL |
1138 | (unless whitespace-mode |
1139 | (whitespace-turn-on-if-enabled))))) | |
1140 | (t ; global-whitespace-mode off | |
30d0ade9 | 1141 | (save-excursion |
4a4b61e2 | 1142 | (remove-hook 'find-file-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 | |
cd222e44 | 1174 | "Toggle NEWLINE global minor mode visualization (\"NL\" on modeline). |
8b40bb55 | 1175 | |
cd222e44 | 1176 | If ARG is null, toggle NEWLINE visualization. |
8b40bb55 VJL |
1177 | If ARG is a number greater than zero, turn on visualization; |
1178 | otherwise, turn off visualization. | |
8b40bb55 | 1179 | |
cd222e44 VJL |
1180 | Use `global-whitespace-newline-mode' only for NEWLINE |
1181 | visualization exclusively. For other visualizations, including | |
1182 | NEWLINE visualization together with (HARD) SPACEs and/or TABs, | |
db9e401b | 1183 | please use `global-whitespace-mode'. |
cd222e44 VJL |
1184 | |
1185 | See also `whitespace-newline' and `whitespace-display-mappings'." | |
8b40bb55 VJL |
1186 | :lighter " NL" |
1187 | :init-value nil | |
1188 | :global t | |
1189 | :group 'whitespace | |
1190 | (let ((whitespace-style '(newline-mark newline))) | |
1191 | (global-whitespace-mode global-whitespace-newline-mode) | |
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 | ||
1251 | Each element has the form: | |
1252 | ||
1253 | (CHAR . SYMBOL) | |
1254 | ||
1255 | Where: | |
1256 | ||
1257 | CHAR is a char which the user will have to type. | |
1258 | ||
1259 | SYMBOL 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. | |
1274 | Used 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. | |
1278 | Used 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. | |
1282 | Used by `whitespace-post-command-hook' function (which see).") | |
1283 | ||
1284 | (defvar whitespace-eob-marker nil | |
1285 | "Used to save locally the eob marker value. | |
1286 | Used by `whitespace-post-command-hook' function (which see).") | |
1287 | ||
1288 | (defvar whitespace-buffer-changed nil | |
1289 | "Used to indicate locally if buffer changed. | |
1290 | Used by `whitespace-post-command-hook' and `whitespace-buffer-changed' | |
1291 | functions (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 |
1298 | If local whitespace-mode is off, toggle the option given by ARG |
1299 | and turn on local whitespace-mode. | |
30d0ade9 | 1300 | |
c9dff4e3 VJL |
1301 | If local whitespace-mode is on, toggle the option given by ARG |
1302 | and restart local whitespace-mode. | |
30d0ade9 VJL |
1303 | |
1304 | Interactively, 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 | 1334 | Non-interactively, ARG should be a symbol or a list of symbols. |
30d0ade9 VJL |
1335 | The 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 | 1361 | See `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 |
1377 | If global whitespace-mode is off, toggle the option given by ARG |
1378 | and turn on global whitespace-mode. | |
30d0ade9 | 1379 | |
c9dff4e3 VJL |
1380 | If global whitespace-mode is on, toggle the option given by ARG |
1381 | and restart global whitespace-mode. | |
30d0ade9 | 1382 | |
b502217b | 1383 | Interactively, 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 | 1413 | Non-interactively, ARG should be a symbol or a list of symbols. |
30d0ade9 VJL |
1414 | The 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 | 1440 | See `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 | ||
1457 | It usually applies to the whole buffer, but in transient mark | |
1458 | mode when the mark is active, it applies to the region. It also | |
c23dcd9f | 1459 | applies to the region when it is not in transient mark mode, the |
55d1cfe8 VJL |
1460 | mark is active and \\[universal-argument] was pressed just before |
1461 | calling `whitespace-cleanup' interactively. | |
97a739d5 | 1462 | |
c9dff4e3 | 1463 | See also `whitespace-cleanup-region'. |
97a739d5 | 1464 | |
b502217b | 1465 | The problems cleaned up are: |
97a739d5 VJL |
1466 | |
1467 | 1. empty lines at beginning of buffer. | |
1468 | 2. 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 | |
1472 | 3. 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 | |
1482 | 4. 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 | |
1491 | 5. 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 | |
1495 | 6. 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 |
1504 | See `whitespace-style', `indent-tabs-mode' and `tab-width' for |
1505 | documentation." | |
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 |
9f3b76d5 | 1524 | (save-match-data |
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)) | |
c9dff4e3 | 1531 | (when (re-search-forward |
80525855 | 1532 | (concat "\\`" whitespace-empty-at-bob-regexp) nil t) |
9f3b76d5 | 1533 | (delete-region (match-beginning 1) (match-end 1))) |
c9dff4e3 | 1534 | (when (re-search-forward |
80525855 | 1535 | (concat whitespace-empty-at-eob-regexp "\\'") nil t) |
9f3b76d5 | 1536 | (delete-region (match-beginning 1) (match-end 1))))))) |
55d1cfe8 VJL |
1537 | ;; PROBLEM 3: 8 or more SPACEs at bol |
1538 | ;; PROBLEM 4: SPACEs before TAB | |
1539 | ;; PROBLEM 5: SPACEs or TABs at eol | |
1540 | ;; PROBLEM 6: 8 or more SPACEs after TAB | |
bc27c677 | 1541 | (whitespace-cleanup-region (point-min) (point-max))))) |
97a739d5 VJL |
1542 | |
1543 | ||
1544 | ;;;###autoload | |
c9dff4e3 | 1545 | (defun whitespace-cleanup-region (start end) |
97a739d5 VJL |
1546 | "Cleanup some blank problems at region. |
1547 | ||
b502217b | 1548 | The problems cleaned up are: |
97a739d5 VJL |
1549 | |
1550 | 1. 8 or more SPACEs at beginning of line. | |
ae68f2d8 | 1551 | If `whitespace-style' includes the value `indentation': |
55d1cfe8 VJL |
1552 | replace 8 or more SPACEs at beginning of line by TABs, if |
1553 | `indent-tabs-mode' is non-nil; otherwise, replace TABs by | |
1554 | SPACEs. | |
ae68f2d8 VJL |
1555 | If `whitespace-style' includes the value `indentation::tab', |
1556 | replace 8 or more SPACEs at beginning of line by TABs. | |
1557 | If `whitespace-style' includes the value `indentation::space', | |
1558 | replace TABs by SPACEs. | |
97a739d5 VJL |
1559 | |
1560 | 2. SPACEs before TAB. | |
ae68f2d8 | 1561 | If `whitespace-style' includes the value `space-before-tab': |
55d1cfe8 VJL |
1562 | replace SPACEs by TABs, if `indent-tabs-mode' is non-nil; |
1563 | otherwise, replace TABs by SPACEs. | |
ae68f2d8 | 1564 | If `whitespace-style' includes the value |
55d1cfe8 | 1565 | `space-before-tab::tab', replace SPACEs by TABs. |
ae68f2d8 | 1566 | If `whitespace-style' includes the value |
55d1cfe8 | 1567 | `space-before-tab::space', replace TABs by SPACEs. |
97a739d5 VJL |
1568 | |
1569 | 3. SPACEs or TABs at end of line. | |
ae68f2d8 | 1570 | If `whitespace-style' includes the value `trailing', remove |
55d1cfe8 | 1571 | all SPACEs or TABs at end of line. |
97a739d5 VJL |
1572 | |
1573 | 4. 8 or more SPACEs after TAB. | |
ae68f2d8 | 1574 | If `whitespace-style' includes the value `space-after-tab': |
55d1cfe8 VJL |
1575 | replace SPACEs by TABs, if `indent-tabs-mode' is non-nil; |
1576 | otherwise, replace TABs by SPACEs. | |
ae68f2d8 | 1577 | If `whitespace-style' includes the value |
55d1cfe8 | 1578 | `space-after-tab::tab', replace SPACEs by TABs. |
ae68f2d8 | 1579 | If `whitespace-style' includes the value |
55d1cfe8 VJL |
1580 | `space-after-tab::space', replace TABs by SPACEs. |
1581 | ||
ae68f2d8 VJL |
1582 | See `whitespace-style', `indent-tabs-mode' and `tab-width' for |
1583 | documentation." | |
bc27c677 VJL |
1584 | (interactive "@r") |
1585 | (if buffer-read-only | |
1586 | ;; read-only buffer | |
1587 | (whitespace-warn-read-only "cleanup region") | |
1588 | ;; non-read-only buffer | |
1589 | (let ((rstart (min start end)) | |
1590 | (rend (copy-marker (max start end))) | |
1591 | (indent-tabs-mode whitespace-indent-tabs-mode) | |
1592 | (tab-width whitespace-tab-width) | |
1593 | overwrite-mode ; enforce no overwrite | |
1594 | tmp) | |
1595 | (save-excursion | |
1596 | (save-match-data | |
1597 | ;; PROBLEM 1: 8 or more SPACEs at bol | |
1598 | (cond | |
1599 | ;; ACTION: replace 8 or more SPACEs at bol by TABs, if | |
1600 | ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs | |
1601 | ;; by SPACEs. | |
1602 | ((memq 'indentation whitespace-style) | |
1603 | (let ((regexp (whitespace-indentation-regexp))) | |
1604 | (goto-char rstart) | |
1605 | (while (re-search-forward regexp rend t) | |
1606 | (setq tmp (current-indentation)) | |
1607 | (goto-char (match-beginning 0)) | |
1608 | (delete-horizontal-space) | |
1609 | (unless (eolp) | |
1610 | (indent-to tmp))))) | |
1611 | ;; ACTION: replace 8 or more SPACEs at bol by TABs. | |
1612 | ((memq 'indentation::tab whitespace-style) | |
1613 | (whitespace-replace-action | |
1614 | 'tabify rstart rend | |
1615 | (whitespace-indentation-regexp 'tab) 0)) | |
1616 | ;; ACTION: replace TABs by SPACEs. | |
1617 | ((memq 'indentation::space whitespace-style) | |
1618 | (whitespace-replace-action | |
1619 | 'untabify rstart rend | |
1620 | (whitespace-indentation-regexp 'space) 0))) | |
1621 | ;; PROBLEM 3: SPACEs or TABs at eol | |
1622 | ;; ACTION: remove all SPACEs or TABs at eol | |
1623 | (when (memq 'trailing whitespace-style) | |
1624 | (whitespace-replace-action | |
1625 | 'delete-region rstart rend | |
1626 | whitespace-trailing-regexp 1)) | |
1627 | ;; PROBLEM 4: 8 or more SPACEs after TAB | |
1628 | (cond | |
1629 | ;; ACTION: replace 8 or more SPACEs by TABs, if | |
1630 | ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs | |
1631 | ;; by SPACEs. | |
1632 | ((memq 'space-after-tab whitespace-style) | |
1633 | (whitespace-replace-action | |
1634 | (if whitespace-indent-tabs-mode 'tabify 'untabify) | |
1635 | rstart rend (whitespace-space-after-tab-regexp) 1)) | |
1636 | ;; ACTION: replace 8 or more SPACEs by TABs. | |
1637 | ((memq 'space-after-tab::tab whitespace-style) | |
1638 | (whitespace-replace-action | |
1639 | 'tabify rstart rend | |
1640 | (whitespace-space-after-tab-regexp 'tab) 1)) | |
1641 | ;; ACTION: replace TABs by SPACEs. | |
1642 | ((memq 'space-after-tab::space whitespace-style) | |
1643 | (whitespace-replace-action | |
1644 | 'untabify rstart rend | |
1645 | (whitespace-space-after-tab-regexp 'space) 1))) | |
1646 | ;; PROBLEM 2: SPACEs before TAB | |
1647 | (cond | |
1648 | ;; ACTION: replace SPACEs before TAB by TABs, if | |
1649 | ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs | |
1650 | ;; by SPACEs. | |
1651 | ((memq 'space-before-tab whitespace-style) | |
1652 | (whitespace-replace-action | |
1653 | (if whitespace-indent-tabs-mode 'tabify 'untabify) | |
1654 | rstart rend whitespace-space-before-tab-regexp | |
16498102 | 1655 | (if whitespace-indent-tabs-mode 0 2))) |
bc27c677 VJL |
1656 | ;; ACTION: replace SPACEs before TAB by TABs. |
1657 | ((memq 'space-before-tab::tab whitespace-style) | |
1658 | (whitespace-replace-action | |
1659 | 'tabify rstart rend | |
16498102 | 1660 | whitespace-space-before-tab-regexp 0)) |
bc27c677 VJL |
1661 | ;; ACTION: replace TABs by SPACEs. |
1662 | ((memq 'space-before-tab::space whitespace-style) | |
1663 | (whitespace-replace-action | |
1664 | 'untabify rstart rend | |
1665 | whitespace-space-before-tab-regexp 2))))) | |
1666 | (set-marker rend nil)))) ; point marker to nowhere | |
97a739d5 | 1667 | |
9f3b76d5 | 1668 | |
55d1cfe8 VJL |
1669 | (defun whitespace-replace-action (action rstart rend regexp index) |
1670 | "Do ACTION in the string matched by REGEXP between RSTART and REND. | |
1671 | ||
1672 | INDEX is the level group matched by REGEXP and used by ACTION. | |
1673 | ||
1674 | See also `tab-width'." | |
9f3b76d5 VJL |
1675 | (goto-char rstart) |
1676 | (while (re-search-forward regexp rend t) | |
55d1cfe8 VJL |
1677 | (goto-char (match-end index)) |
1678 | (funcall action (match-beginning index) (match-end index)))) | |
9f3b76d5 | 1679 | |
97a739d5 | 1680 | \f |
c9dff4e3 | 1681 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
94dc593f VJL |
1682 | ;;;; User command - report |
1683 | ||
1684 | ||
55d1cfe8 VJL |
1685 | (defun whitespace-regexp (regexp &optional kind) |
1686 | "Return REGEXP depending on `whitespace-indent-tabs-mode'." | |
1687 | (cond | |
1688 | ((or (eq kind 'tab) | |
1689 | whitespace-indent-tabs-mode) | |
1690 | (format (car regexp) whitespace-tab-width)) | |
1691 | ((or (eq kind 'space) | |
1692 | (not whitespace-indent-tabs-mode)) | |
1693 | (cdr regexp)))) | |
1694 | ||
1695 | ||
1696 | (defun whitespace-indentation-regexp (&optional kind) | |
1697 | "Return the indentation regexp depending on `whitespace-indent-tabs-mode'." | |
1698 | (whitespace-regexp whitespace-indentation-regexp kind)) | |
1699 | ||
1700 | ||
1701 | (defun whitespace-space-after-tab-regexp (&optional kind) | |
1702 | "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'." | |
1703 | (whitespace-regexp whitespace-space-after-tab-regexp kind)) | |
1704 | ||
1705 | ||
94dc593f VJL |
1706 | (defconst whitespace-report-list |
1707 | (list | |
55d1cfe8 VJL |
1708 | (cons 'empty whitespace-empty-at-bob-regexp) |
1709 | (cons 'empty whitespace-empty-at-eob-regexp) | |
2788143d | 1710 | (cons 'trailing whitespace-trailing-regexp) |
55d1cfe8 VJL |
1711 | (cons 'indentation nil) |
1712 | (cons 'indentation::tab nil) | |
1713 | (cons 'indentation::space nil) | |
1714 | (cons 'space-before-tab whitespace-space-before-tab-regexp) | |
1715 | (cons 'space-before-tab::tab whitespace-space-before-tab-regexp) | |
1716 | (cons 'space-before-tab::space whitespace-space-before-tab-regexp) | |
1717 | (cons 'space-after-tab nil) | |
1718 | (cons 'space-after-tab::tab nil) | |
1719 | (cons 'space-after-tab::space nil) | |
94dc593f VJL |
1720 | ) |
1721 | "List of whitespace bogus symbol and corresponding regexp.") | |
1722 | ||
1723 | ||
1724 | (defconst whitespace-report-text | |
55d1cfe8 VJL |
1725 | '( ;; `indent-tabs-mode' has non-nil value |
1726 | "\ | |
1727 | Whitespace Report | |
1728 | ||
1729 | Current Setting Whitespace Problem | |
1730 | ||
1731 | empty [] [] empty lines at beginning of buffer | |
1732 | empty [] [] empty lines at end of buffer | |
1733 | trailing [] [] SPACEs or TABs at end of line | |
1734 | indentation [] [] 8 or more SPACEs at beginning of line | |
1735 | indentation::tab [] [] 8 or more SPACEs at beginning of line | |
1736 | indentation::space [] [] TABs at beginning of line | |
1737 | space-before-tab [] [] SPACEs before TAB | |
1738 | space-before-tab::tab [] [] SPACEs before TAB: SPACEs | |
1739 | space-before-tab::space [] [] SPACEs before TAB: TABs | |
1740 | space-after-tab [] [] 8 or more SPACEs after TAB | |
1741 | space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs | |
1742 | space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs | |
1743 | ||
1744 | indent-tabs-mode = | |
1745 | tab-width = \n\n" | |
1746 | . ;; `indent-tabs-mode' has nil value | |
1747 | "\ | |
1748 | Whitespace Report | |
1749 | ||
1750 | Current Setting Whitespace Problem | |
1751 | ||
1752 | empty [] [] empty lines at beginning of buffer | |
1753 | empty [] [] empty lines at end of buffer | |
1754 | trailing [] [] SPACEs or TABs at end of line | |
1755 | indentation [] [] TABs at beginning of line | |
1756 | indentation::tab [] [] 8 or more SPACEs at beginning of line | |
1757 | indentation::space [] [] TABs at beginning of line | |
1758 | space-before-tab [] [] SPACEs before TAB | |
1759 | space-before-tab::tab [] [] SPACEs before TAB: SPACEs | |
1760 | space-before-tab::space [] [] SPACEs before TAB: TABs | |
1761 | space-after-tab [] [] 8 or more SPACEs after TAB | |
1762 | space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs | |
1763 | space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs | |
1764 | ||
1765 | indent-tabs-mode = | |
1766 | tab-width = \n\n") | |
1767 | "Text for whitespace bogus report. | |
1768 | ||
1769 | It is a cons of strings, where the car part is used when | |
1770 | `indent-tabs-mode' is non-nil, and the cdr part is used when | |
1771 | `indent-tabs-mode' is nil.") | |
94dc593f VJL |
1772 | |
1773 | ||
1774 | (defconst whitespace-report-buffer-name "*Whitespace Report*" | |
1775 | "The buffer name for whitespace bogus report.") | |
c9dff4e3 VJL |
1776 | |
1777 | ||
1778 | ;;;###autoload | |
94dc593f VJL |
1779 | (defun whitespace-report (&optional force report-if-bogus) |
1780 | "Report some whitespace problems in buffer. | |
c9dff4e3 | 1781 | |
94dc593f VJL |
1782 | Return nil if there is no whitespace problem; otherwise, return |
1783 | non-nil. | |
c9dff4e3 | 1784 | |
55d1cfe8 VJL |
1785 | If FORCE is non-nil or \\[universal-argument] was pressed just |
1786 | before calling `whitespace-report' interactively, it forces | |
ae68f2d8 | 1787 | `whitespace-style' to have: |
c9dff4e3 | 1788 | |
94dc593f | 1789 | empty |
55d1cfe8 | 1790 | trailing |
c9dff4e3 VJL |
1791 | indentation |
1792 | space-before-tab | |
94dc593f VJL |
1793 | space-after-tab |
1794 | ||
1795 | If REPORT-IF-BOGUS is non-nil, it reports only when there are any | |
1796 | whitespace problems in buffer. | |
1797 | ||
1798 | Report if some of the following whitespace problems exist: | |
1799 | ||
55d1cfe8 | 1800 | * If `indent-tabs-mode' is non-nil: |
94dc593f VJL |
1801 | empty 1. empty lines at beginning of buffer. |
1802 | empty 2. empty lines at end of buffer. | |
55d1cfe8 VJL |
1803 | trailing 3. SPACEs or TABs at end of line. |
1804 | indentation 4. 8 or more SPACEs at beginning of line. | |
1805 | space-before-tab 5. SPACEs before TAB. | |
94dc593f VJL |
1806 | space-after-tab 6. 8 or more SPACEs after TAB. |
1807 | ||
55d1cfe8 VJL |
1808 | * If `indent-tabs-mode' is nil: |
1809 | empty 1. empty lines at beginning of buffer. | |
1810 | empty 2. empty lines at end of buffer. | |
1811 | trailing 3. SPACEs or TABs at end of line. | |
1812 | indentation 4. TABS at beginning of line. | |
1813 | space-before-tab 5. SPACEs before TAB. | |
1814 | space-after-tab 6. 8 or more SPACEs after TAB. | |
1815 | ||
ae68f2d8 | 1816 | See `whitespace-style' for documentation. |
94dc593f VJL |
1817 | See also `whitespace-cleanup' and `whitespace-cleanup-region' for |
1818 | cleaning up these problems." | |
1819 | (interactive (list current-prefix-arg)) | |
1820 | (whitespace-report-region (point-min) (point-max) | |
1821 | force report-if-bogus)) | |
1822 | ||
1823 | ||
1824 | ;;;###autoload | |
1825 | (defun whitespace-report-region (start end &optional force report-if-bogus) | |
1826 | "Report some whitespace problems in a region. | |
1827 | ||
1828 | Return nil if there is no whitespace problem; otherwise, return | |
1829 | non-nil. | |
1830 | ||
55d1cfe8 VJL |
1831 | If FORCE is non-nil or \\[universal-argument] was pressed just |
1832 | before calling `whitespace-report-region' interactively, it | |
ae68f2d8 | 1833 | forces `whitespace-style' to have: |
94dc593f | 1834 | |
c9dff4e3 | 1835 | empty |
94dc593f VJL |
1836 | indentation |
1837 | space-before-tab | |
1838 | trailing | |
c9dff4e3 VJL |
1839 | space-after-tab |
1840 | ||
94dc593f VJL |
1841 | If REPORT-IF-BOGUS is non-nil, it reports only when there are any |
1842 | whitespace problems in buffer. | |
1843 | ||
1844 | Report if some of the following whitespace problems exist: | |
c9dff4e3 | 1845 | |
55d1cfe8 VJL |
1846 | * If `indent-tabs-mode' is non-nil: |
1847 | empty 1. empty lines at beginning of buffer. | |
1848 | empty 2. empty lines at end of buffer. | |
1849 | trailing 3. SPACEs or TABs at end of line. | |
1850 | indentation 4. 8 or more SPACEs at beginning of line. | |
1851 | space-before-tab 5. SPACEs before TAB. | |
1852 | space-after-tab 6. 8 or more SPACEs after TAB. | |
1853 | ||
1854 | * If `indent-tabs-mode' is nil: | |
c9dff4e3 VJL |
1855 | empty 1. empty lines at beginning of buffer. |
1856 | empty 2. empty lines at end of buffer. | |
55d1cfe8 VJL |
1857 | trailing 3. SPACEs or TABs at end of line. |
1858 | indentation 4. TABS at beginning of line. | |
1859 | space-before-tab 5. SPACEs before TAB. | |
c9dff4e3 VJL |
1860 | space-after-tab 6. 8 or more SPACEs after TAB. |
1861 | ||
ae68f2d8 | 1862 | See `whitespace-style' for documentation. |
c9dff4e3 VJL |
1863 | See also `whitespace-cleanup' and `whitespace-cleanup-region' for |
1864 | cleaning up these problems." | |
94dc593f VJL |
1865 | (interactive "r") |
1866 | (setq force (or current-prefix-arg force)) | |
1867 | (save-excursion | |
1868 | (save-match-data | |
55d1cfe8 VJL |
1869 | (let* ((has-bogus nil) |
1870 | (rstart (min start end)) | |
1871 | (rend (max start end)) | |
1872 | (bogus-list | |
1873 | (mapcar | |
1874 | #'(lambda (option) | |
1875 | (when force | |
ae68f2d8 | 1876 | (add-to-list 'whitespace-style (car option))) |
55d1cfe8 VJL |
1877 | (goto-char rstart) |
1878 | (let ((regexp | |
1879 | (cond | |
1880 | ((eq (car option) 'indentation) | |
1881 | (whitespace-indentation-regexp)) | |
1882 | ((eq (car option) 'indentation::tab) | |
1883 | (whitespace-indentation-regexp 'tab)) | |
1884 | ((eq (car option) 'indentation::space) | |
1885 | (whitespace-indentation-regexp 'space)) | |
1886 | ((eq (car option) 'space-after-tab) | |
1887 | (whitespace-space-after-tab-regexp)) | |
1888 | ((eq (car option) 'space-after-tab::tab) | |
1889 | (whitespace-space-after-tab-regexp 'tab)) | |
1890 | ((eq (car option) 'space-after-tab::space) | |
1891 | (whitespace-space-after-tab-regexp 'space)) | |
1892 | (t | |
1893 | (cdr option))))) | |
1894 | (and (re-search-forward regexp rend t) | |
1895 | (setq has-bogus t)))) | |
1896 | whitespace-report-list))) | |
94dc593f | 1897 | (when (if report-if-bogus has-bogus t) |
55d1cfe8 VJL |
1898 | (whitespace-kill-buffer whitespace-report-buffer-name) |
1899 | ;; `whitespace-indent-tabs-mode' is local to current buffer | |
ce203001 VJL |
1900 | ;; `whitespace-tab-width' is local to current buffer |
1901 | (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode) | |
1902 | (ws-tab-width whitespace-tab-width)) | |
55d1cfe8 VJL |
1903 | (with-current-buffer (get-buffer-create |
1904 | whitespace-report-buffer-name) | |
1905 | (erase-buffer) | |
1906 | (insert (if ws-indent-tabs-mode | |
1907 | (car whitespace-report-text) | |
1908 | (cdr whitespace-report-text))) | |
1909 | (goto-char (point-min)) | |
1910 | (forward-line 3) | |
1911 | (dolist (option whitespace-report-list) | |
1912 | (forward-line 1) | |
1913 | (whitespace-mark-x | |
ae68f2d8 | 1914 | 27 (memq (car option) whitespace-style)) |
55d1cfe8 VJL |
1915 | (whitespace-mark-x 7 (car bogus-list)) |
1916 | (setq bogus-list (cdr bogus-list))) | |
94dc593f | 1917 | (forward-line 1) |
55d1cfe8 | 1918 | (whitespace-insert-value ws-indent-tabs-mode) |
ce203001 | 1919 | (whitespace-insert-value ws-tab-width) |
55d1cfe8 VJL |
1920 | (when has-bogus |
1921 | (goto-char (point-max)) | |
1922 | (insert " Type `M-x whitespace-cleanup'" | |
1923 | " to cleanup the buffer.\n\n" | |
1924 | " Type `M-x whitespace-cleanup-region'" | |
1925 | " to cleanup a region.\n\n")) | |
1926 | (whitespace-display-window (current-buffer))))) | |
94dc593f | 1927 | has-bogus)))) |
c9dff4e3 VJL |
1928 | |
1929 | \f | |
30d0ade9 VJL |
1930 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1931 | ;;;; Internal functions | |
1932 | ||
1933 | ||
c9dff4e3 | 1934 | (defvar whitespace-font-lock-mode nil |
30d0ade9 | 1935 | "Used to remember whether a buffer had font lock mode on or not.") |
30d0ade9 | 1936 | |
c9dff4e3 | 1937 | (defvar whitespace-font-lock nil |
30d0ade9 | 1938 | "Used to remember whether a buffer initially had font lock on or not.") |
30d0ade9 | 1939 | |
c9dff4e3 | 1940 | (defvar whitespace-font-lock-keywords nil |
30d0ade9 | 1941 | "Used to save locally `font-lock-keywords' value.") |
30d0ade9 VJL |
1942 | |
1943 | ||
c9dff4e3 | 1944 | (defconst whitespace-help-text |
30d0ade9 | 1945 | "\ |
85cc3d4f VJL |
1946 | Whitespace Toggle Options | scroll up : SPC or > | |
1947 | | scroll down: M-SPC or < | | |
1948 | FACES \\__________________________/ | |
1949 | [] f - toggle face visualization | |
55d1cfe8 VJL |
1950 | [] t - toggle TAB visualization |
1951 | [] s - toggle SPACE and HARD SPACE visualization | |
1952 | [] r - toggle trailing blanks visualization | |
1953 | [] l - toggle \"long lines\" visualization | |
1954 | [] L - toggle \"long lines\" tail visualization | |
1955 | [] n - toggle NEWLINE visualization | |
1956 | [] e - toggle empty line at bob and/or eob visualization | |
1957 | [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode') | |
1958 | [] I - toggle indentation SPACEs visualization | |
1959 | [] i - toggle indentation TABs visualization | |
1960 | [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode') | |
1961 | [] A - toggle SPACEs after TAB: SPACEs visualization | |
1962 | [] a - toggle SPACEs after TAB: TABs visualization | |
1963 | [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode') | |
1964 | [] B - toggle SPACEs before TAB: SPACEs visualization | |
1965 | [] b - toggle SPACEs before TAB: TABs visualization | |
1966 | ||
1967 | DISPLAY TABLE | |
1968 | [] T - toggle TAB visualization | |
1969 | [] S - toggle SPACE and HARD SPACE visualization | |
1970 | [] N - toggle NEWLINE visualization | |
1971 | ||
ae68f2d8 | 1972 | x - restore `whitespace-style' value |
30d0ade9 VJL |
1973 | |
1974 | ? - display this text\n\n" | |
c9dff4e3 | 1975 | "Text for whitespace toggle options.") |
30d0ade9 VJL |
1976 | |
1977 | ||
c9dff4e3 VJL |
1978 | (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*" |
1979 | "The buffer name for whitespace toggle options.") | |
30d0ade9 VJL |
1980 | |
1981 | ||
55d1cfe8 VJL |
1982 | (defun whitespace-insert-value (value) |
1983 | "Insert VALUE at column 20 of next line." | |
1984 | (forward-line 1) | |
1985 | (move-to-column 20 t) | |
1986 | (insert (format "%s" value))) | |
1987 | ||
1988 | ||
94dc593f VJL |
1989 | (defun whitespace-mark-x (nchars condition) |
1990 | "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION." | |
1991 | (forward-char nchars) | |
1992 | (insert (if condition "X" " "))) | |
1993 | ||
1994 | ||
c9dff4e3 | 1995 | (defun whitespace-insert-option-mark (the-list the-value) |
30d0ade9 | 1996 | "Insert the option mark ('X' or ' ') in toggle options buffer." |
ae68f2d8 | 1997 | (goto-char (point-min)) |
55d1cfe8 | 1998 | (forward-line 2) |
30d0ade9 | 1999 | (dolist (sym the-list) |
ae68f2d8 VJL |
2000 | (if (eq sym 'help-newline) |
2001 | (forward-line 2) | |
2002 | (forward-line 1) | |
2003 | (whitespace-mark-x 2 (memq sym the-value))))) | |
30d0ade9 VJL |
2004 | |
2005 | ||
ae68f2d8 | 2006 | (defun whitespace-help-on (style) |
c9dff4e3 VJL |
2007 | "Display the whitespace toggle options." |
2008 | (unless (get-buffer whitespace-help-buffer-name) | |
30d0ade9 | 2009 | (delete-other-windows) |
c9dff4e3 | 2010 | (let ((buffer (get-buffer-create whitespace-help-buffer-name))) |
7fdbcd83 | 2011 | (with-current-buffer buffer |
30d0ade9 | 2012 | (erase-buffer) |
c9dff4e3 | 2013 | (insert whitespace-help-text) |
c9dff4e3 | 2014 | (whitespace-insert-option-mark |
ae68f2d8 | 2015 | whitespace-style-value-list style) |
94dc593f VJL |
2016 | (whitespace-display-window buffer))))) |
2017 | ||
2018 | ||
2019 | (defun whitespace-display-window (buffer) | |
2020 | "Display BUFFER in a new window." | |
2021 | (goto-char (point-min)) | |
2022 | (set-buffer-modified-p nil) | |
85cc3d4f VJL |
2023 | (when (< (window-height) (* 2 window-min-height)) |
2024 | (kill-buffer buffer) | |
2025 | (error "Window height is too small; \ | |
c9dff4e3 | 2026 | can't split window to display whitespace toggle options")) |
85cc3d4f VJL |
2027 | (let ((win (split-window))) |
2028 | (set-window-buffer win buffer) | |
2029 | (shrink-window-if-larger-than-buffer win))) | |
30d0ade9 VJL |
2030 | |
2031 | ||
55d1cfe8 VJL |
2032 | (defun whitespace-kill-buffer (buffer-name) |
2033 | "Kill buffer BUFFER-NAME and windows related with it." | |
2034 | (let ((buffer (get-buffer buffer-name))) | |
30d0ade9 VJL |
2035 | (when buffer |
2036 | (delete-windows-on buffer) | |
2037 | (kill-buffer buffer)))) | |
2038 | ||
2039 | ||
55d1cfe8 VJL |
2040 | (defun whitespace-help-off () |
2041 | "Remove the buffer and window of the whitespace toggle options." | |
2042 | (whitespace-kill-buffer whitespace-help-buffer-name)) | |
2043 | ||
2044 | ||
85cc3d4f VJL |
2045 | (defun whitespace-help-scroll (&optional up) |
2046 | "Scroll help window, if it exists. | |
2047 | ||
2048 | If UP is non-nil, scroll up; otherwise, scroll down." | |
2049 | (condition-case data-help | |
2050 | (let ((buffer (get-buffer whitespace-help-buffer-name))) | |
2051 | (if buffer | |
2052 | (with-selected-window (get-buffer-window buffer) | |
2053 | (if up | |
2054 | (scroll-up 3) | |
2055 | (scroll-down 3))) | |
2056 | (ding))) | |
2057 | ;; handler | |
2058 | ((error) | |
2059 | ;; just ignore error | |
2060 | ))) | |
2061 | ||
2062 | ||
c9dff4e3 | 2063 | (defun whitespace-interactive-char (local-p) |
30d0ade9 VJL |
2064 | "Interactive function to read a char and return a symbol. |
2065 | ||
2066 | If LOCAL-P is non-nil, it uses a local context; otherwise, it | |
2067 | uses a global context. | |
2068 | ||
b502217b | 2069 | It accepts one of the following chars: |
30d0ade9 VJL |
2070 | |
2071 | CHAR MEANING | |
55d1cfe8 | 2072 | (VIA FACES) |
85cc3d4f | 2073 | f toggle face visualization |
040f578c VJL |
2074 | t toggle TAB visualization |
2075 | s toggle SPACE and HARD SPACE visualization | |
2076 | r toggle trailing blanks visualization | |
040f578c | 2077 | l toggle \"long lines\" visualization |
9f3b76d5 | 2078 | L toggle \"long lines\" tail visualization |
040f578c | 2079 | n toggle NEWLINE visualization |
97a739d5 | 2080 | e toggle empty line at bob and/or eob visualization |
55d1cfe8 VJL |
2081 | C-i toggle indentation SPACEs visualization (via `indent-tabs-mode') |
2082 | I toggle indentation SPACEs visualization | |
2083 | i toggle indentation TABs visualization | |
2084 | C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode') | |
2085 | A toggle SPACEs after TAB: SPACEs visualization | |
2086 | a toggle SPACEs after TAB: TABs visualization | |
2087 | C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode') | |
2088 | B toggle SPACEs before TAB: SPACEs visualization | |
2089 | b toggle SPACEs before TAB: TABs visualization | |
2090 | ||
2091 | (VIA DISPLAY TABLE) | |
2092 | T toggle TAB visualization | |
2093 | S toggle SPACE and HARD SPACE visualization | |
2094 | N toggle NEWLINE visualization | |
2095 | ||
ae68f2d8 | 2096 | x restore `whitespace-style' value |
30d0ade9 VJL |
2097 | ? display brief help |
2098 | ||
c9dff4e3 VJL |
2099 | See also `whitespace-toggle-option-alist'." |
2100 | (let* ((is-off (not (if local-p | |
2101 | whitespace-mode | |
2102 | global-whitespace-mode))) | |
ae68f2d8 VJL |
2103 | (style (cond (is-off whitespace-style) ; use default value |
2104 | (local-p whitespace-active-style) | |
2105 | (t whitespace-toggle-style))) | |
30d0ade9 | 2106 | (prompt |
c9dff4e3 | 2107 | (format "Whitespace Toggle %s (type ? for further options)-" |
30d0ade9 VJL |
2108 | (if local-p "Local" "Global"))) |
2109 | ch sym) | |
2110 | ;; read a valid option and get the corresponding symbol | |
2111 | (save-window-excursion | |
2112 | (condition-case data | |
2113 | (progn | |
2114 | (while | |
2115 | ;; while condition | |
2116 | (progn | |
2117 | (setq ch (read-char prompt)) | |
2118 | (not | |
2119 | (setq sym | |
c9dff4e3 VJL |
2120 | (cdr |
2121 | (assq ch whitespace-toggle-option-alist))))) | |
30d0ade9 | 2122 | ;; while body |
85cc3d4f VJL |
2123 | (cond |
2124 | ((eq ch ?\?) (whitespace-help-on style)) | |
2125 | ((eq ch ?\ ) (whitespace-help-scroll t)) | |
2126 | ((eq ch ?\M- ) (whitespace-help-scroll)) | |
2127 | ((eq ch ?>) (whitespace-help-scroll t)) | |
2128 | ((eq ch ?<) (whitespace-help-scroll)) | |
2129 | (t (ding)))) | |
c9dff4e3 | 2130 | (whitespace-help-off) |
30d0ade9 VJL |
2131 | (message " ")) ; clean echo area |
2132 | ;; handler | |
2133 | ((quit error) | |
c9dff4e3 | 2134 | (whitespace-help-off) |
30d0ade9 | 2135 | (error (error-message-string data))))) |
db9e401b | 2136 | (list sym))) ; return the appropriate symbol |
30d0ade9 VJL |
2137 | |
2138 | ||
ae68f2d8 | 2139 | (defun whitespace-toggle-list (local-p arg the-list) |
30d0ade9 VJL |
2140 | "Toggle options in THE-LIST based on list ARG. |
2141 | ||
2142 | If LOCAL-P is non-nil, it uses a local context; otherwise, it | |
2143 | uses a global context. | |
2144 | ||
2145 | ARG is a list of options to be toggled. | |
2146 | ||
2147 | THE-LIST is a list of options. This list will be toggled and the | |
ae68f2d8 | 2148 | resultant list will be returned." |
c9dff4e3 | 2149 | (unless (if local-p whitespace-mode global-whitespace-mode) |
ae68f2d8 | 2150 | (setq the-list whitespace-style)) |
30d0ade9 VJL |
2151 | (setq the-list (copy-sequence the-list)) ; keep original list |
2152 | (dolist (sym (if (listp arg) arg (list arg))) | |
2153 | (cond | |
ae68f2d8 VJL |
2154 | ;; ignore help value |
2155 | ((eq sym 'help-newline)) | |
30d0ade9 | 2156 | ;; restore default values |
ae68f2d8 VJL |
2157 | ((eq sym 'whitespace-style) |
2158 | (setq the-list whitespace-style)) | |
30d0ade9 | 2159 | ;; toggle valid values |
ae68f2d8 | 2160 | ((memq sym whitespace-style-value-list) |
30d0ade9 VJL |
2161 | (setq the-list (if (memq sym the-list) |
2162 | (delq sym the-list) | |
2163 | (cons sym the-list)))))) | |
2164 | the-list) | |
2165 | ||
ae68f2d8 | 2166 | |
1ed216db GM |
2167 | (defvar whitespace-display-table nil |
2168 | "Used to save a local display table.") | |
2169 | ||
2170 | (defvar whitespace-display-table-was-local nil | |
2171 | "Used to remember whether a buffer initially had a local display table.") | |
30d0ade9 | 2172 | |
ae68f2d8 | 2173 | |
c9dff4e3 VJL |
2174 | (defun whitespace-turn-on () |
2175 | "Turn on whitespace visualization." | |
55d1cfe8 | 2176 | ;; prepare local hooks |
97f6e1ad | 2177 | (add-hook 'write-file-functions 'whitespace-write-file-hook nil t) |
55d1cfe8 VJL |
2178 | ;; create whitespace local buffer environment |
2179 | (set (make-local-variable 'whitespace-font-lock-mode) nil) | |
2180 | (set (make-local-variable 'whitespace-font-lock) nil) | |
2181 | (set (make-local-variable 'whitespace-font-lock-keywords) nil) | |
2182 | (set (make-local-variable 'whitespace-display-table) nil) | |
2183 | (set (make-local-variable 'whitespace-display-table-was-local) nil) | |
ae68f2d8 VJL |
2184 | (set (make-local-variable 'whitespace-active-style) |
2185 | (if (listp whitespace-style) | |
2186 | whitespace-style | |
2187 | (list whitespace-style))) | |
55d1cfe8 VJL |
2188 | (set (make-local-variable 'whitespace-indent-tabs-mode) |
2189 | indent-tabs-mode) | |
2190 | (set (make-local-variable 'whitespace-tab-width) | |
2191 | tab-width) | |
2192 | ;; turn on whitespace | |
ae68f2d8 VJL |
2193 | (when whitespace-active-style |
2194 | (whitespace-color-on) | |
c9dff4e3 | 2195 | (whitespace-display-char-on))) |
30d0ade9 VJL |
2196 | |
2197 | ||
c9dff4e3 | 2198 | (defun whitespace-turn-off () |
b502217b | 2199 | "Turn off whitespace visualization." |
97f6e1ad | 2200 | (remove-hook 'write-file-functions 'whitespace-write-file-hook t) |
ae68f2d8 VJL |
2201 | (when whitespace-active-style |
2202 | (whitespace-color-off) | |
c9dff4e3 | 2203 | (whitespace-display-char-off))) |
30d0ade9 VJL |
2204 | |
2205 | ||
ae68f2d8 VJL |
2206 | (defun whitespace-style-face-p () |
2207 | "Return t if there is some visualization via face." | |
85cc3d4f VJL |
2208 | (and (memq 'face whitespace-active-style) |
2209 | (or (memq 'tabs whitespace-active-style) | |
2210 | (memq 'spaces whitespace-active-style) | |
2211 | (memq 'trailing whitespace-active-style) | |
2212 | (memq 'lines whitespace-active-style) | |
2213 | (memq 'lines-tail whitespace-active-style) | |
2214 | (memq 'newline whitespace-active-style) | |
2215 | (memq 'empty whitespace-active-style) | |
2216 | (memq 'indentation whitespace-active-style) | |
2217 | (memq 'indentation::tab whitespace-active-style) | |
2218 | (memq 'indentation::space whitespace-active-style) | |
2219 | (memq 'space-after-tab whitespace-active-style) | |
2220 | (memq 'space-after-tab::tab whitespace-active-style) | |
2221 | (memq 'space-after-tab::space whitespace-active-style) | |
2222 | (memq 'space-before-tab whitespace-active-style) | |
2223 | (memq 'space-before-tab::tab whitespace-active-style) | |
2224 | (memq 'space-before-tab::space whitespace-active-style)))) | |
ae68f2d8 VJL |
2225 | |
2226 | ||
c9dff4e3 | 2227 | (defun whitespace-color-on () |
040f578c | 2228 | "Turn on color visualization." |
ae68f2d8 | 2229 | (when (whitespace-style-face-p) |
c9dff4e3 VJL |
2230 | (unless whitespace-font-lock |
2231 | (setq whitespace-font-lock t | |
2232 | whitespace-font-lock-keywords | |
30d0ade9 | 2233 | (copy-sequence font-lock-keywords))) |
32a9841c VJL |
2234 | ;; save current point and refontify when necessary |
2235 | (set (make-local-variable 'whitespace-point) | |
2236 | (point)) | |
2237 | (set (make-local-variable 'whitespace-font-lock-refontify) | |
80525855 VJL |
2238 | 0) |
2239 | (set (make-local-variable 'whitespace-bob-marker) | |
2240 | (point-min-marker)) | |
2241 | (set (make-local-variable 'whitespace-eob-marker) | |
2242 | (point-max-marker)) | |
2243 | (set (make-local-variable 'whitespace-buffer-changed) | |
32a9841c VJL |
2244 | nil) |
2245 | (add-hook 'post-command-hook #'whitespace-post-command-hook nil t) | |
80525855 | 2246 | (add-hook 'before-change-functions #'whitespace-buffer-changed nil t) |
30d0ade9 | 2247 | ;; turn off font lock |
55d1cfe8 VJL |
2248 | (set (make-local-variable 'whitespace-font-lock-mode) |
2249 | font-lock-mode) | |
30d0ade9 | 2250 | (font-lock-mode 0) |
c9dff4e3 | 2251 | ;; add whitespace-mode color into font lock |
ae68f2d8 | 2252 | (when (memq 'spaces whitespace-active-style) |
30d0ade9 VJL |
2253 | (font-lock-add-keywords |
2254 | nil | |
2255 | (list | |
2256 | ;; Show SPACEs | |
80525855 | 2257 | (list whitespace-space-regexp 1 whitespace-space t) |
30d0ade9 | 2258 | ;; Show HARD SPACEs |
c9dff4e3 | 2259 | (list whitespace-hspace-regexp 1 whitespace-hspace t)) |
30d0ade9 | 2260 | t)) |
ae68f2d8 | 2261 | (when (memq 'tabs whitespace-active-style) |
30d0ade9 VJL |
2262 | (font-lock-add-keywords |
2263 | nil | |
2264 | (list | |
2265 | ;; Show TABs | |
80525855 | 2266 | (list whitespace-tab-regexp 1 whitespace-tab t)) |
30d0ade9 | 2267 | t)) |
ae68f2d8 | 2268 | (when (memq 'trailing whitespace-active-style) |
30d0ade9 VJL |
2269 | (font-lock-add-keywords |
2270 | nil | |
2271 | (list | |
2272 | ;; Show trailing blanks | |
32a9841c | 2273 | (list #'whitespace-trailing-regexp 1 whitespace-trailing t)) |
30d0ade9 | 2274 | t)) |
ae68f2d8 VJL |
2275 | (when (or (memq 'lines whitespace-active-style) |
2276 | (memq 'lines-tail whitespace-active-style)) | |
30d0ade9 VJL |
2277 | (font-lock-add-keywords |
2278 | nil | |
2279 | (list | |
2280 | ;; Show "long" lines | |
9f3b76d5 | 2281 | (list |
85cc3d4f VJL |
2282 | (let ((line-column (or whitespace-line-column fill-column))) |
2283 | (format | |
2284 | "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$" | |
2285 | whitespace-tab-width | |
2286 | (1- whitespace-tab-width) | |
2287 | (/ line-column whitespace-tab-width) | |
2288 | (let ((rem (% line-column whitespace-tab-width))) | |
2289 | (if (zerop rem) | |
2290 | "" | |
2291 | (format ".\\{%d\\}" rem))))) | |
ae68f2d8 | 2292 | (if (memq 'lines whitespace-active-style) |
9f3b76d5 VJL |
2293 | 0 ; whole line |
2294 | 2) ; line tail | |
c9dff4e3 | 2295 | whitespace-line t)) |
30d0ade9 | 2296 | t)) |
55d1cfe8 | 2297 | (cond |
ae68f2d8 | 2298 | ((memq 'space-before-tab whitespace-active-style) |
55d1cfe8 VJL |
2299 | (font-lock-add-keywords |
2300 | nil | |
2301 | (list | |
2302 | ;; Show SPACEs before TAB (indent-tabs-mode) | |
2303 | (list whitespace-space-before-tab-regexp | |
2304 | (if whitespace-indent-tabs-mode 1 2) | |
2305 | whitespace-space-before-tab t)) | |
2306 | t)) | |
ae68f2d8 | 2307 | ((memq 'space-before-tab::tab whitespace-active-style) |
30d0ade9 VJL |
2308 | (font-lock-add-keywords |
2309 | nil | |
2310 | (list | |
55d1cfe8 | 2311 | ;; Show SPACEs before TAB (SPACEs) |
c9dff4e3 VJL |
2312 | (list whitespace-space-before-tab-regexp |
2313 | 1 whitespace-space-before-tab t)) | |
30d0ade9 | 2314 | t)) |
ae68f2d8 | 2315 | ((memq 'space-before-tab::space whitespace-active-style) |
97a739d5 VJL |
2316 | (font-lock-add-keywords |
2317 | nil | |
2318 | (list | |
55d1cfe8 VJL |
2319 | ;; Show SPACEs before TAB (TABs) |
2320 | (list whitespace-space-before-tab-regexp | |
2321 | 2 whitespace-space-before-tab t)) | |
2322 | t))) | |
2323 | (cond | |
ae68f2d8 | 2324 | ((memq 'indentation whitespace-active-style) |
55d1cfe8 VJL |
2325 | (font-lock-add-keywords |
2326 | nil | |
2327 | (list | |
2328 | ;; Show indentation SPACEs (indent-tabs-mode) | |
2329 | (list (whitespace-indentation-regexp) | |
c9dff4e3 | 2330 | 1 whitespace-indentation t)) |
97a739d5 | 2331 | t)) |
ae68f2d8 | 2332 | ((memq 'indentation::tab whitespace-active-style) |
55d1cfe8 VJL |
2333 | (font-lock-add-keywords |
2334 | nil | |
2335 | (list | |
2336 | ;; Show indentation SPACEs (SPACEs) | |
2337 | (list (whitespace-indentation-regexp 'tab) | |
2338 | 1 whitespace-indentation t)) | |
2339 | t)) | |
ae68f2d8 | 2340 | ((memq 'indentation::space whitespace-active-style) |
55d1cfe8 VJL |
2341 | (font-lock-add-keywords |
2342 | nil | |
2343 | (list | |
2344 | ;; Show indentation SPACEs (TABs) | |
2345 | (list (whitespace-indentation-regexp 'space) | |
2346 | 1 whitespace-indentation t)) | |
2347 | t))) | |
ae68f2d8 | 2348 | (when (memq 'empty whitespace-active-style) |
97a739d5 VJL |
2349 | (font-lock-add-keywords |
2350 | nil | |
2351 | (list | |
2352 | ;; Show empty lines at beginning of buffer | |
32a9841c | 2353 | (list #'whitespace-empty-at-bob-regexp |
c9dff4e3 | 2354 | 1 whitespace-empty t)) |
97a739d5 VJL |
2355 | t) |
2356 | (font-lock-add-keywords | |
2357 | nil | |
2358 | (list | |
2359 | ;; Show empty lines at end of buffer | |
32a9841c | 2360 | (list #'whitespace-empty-at-eob-regexp |
c9dff4e3 | 2361 | 1 whitespace-empty t)) |
97a739d5 | 2362 | t)) |
55d1cfe8 | 2363 | (cond |
ae68f2d8 | 2364 | ((memq 'space-after-tab whitespace-active-style) |
97a739d5 VJL |
2365 | (font-lock-add-keywords |
2366 | nil | |
2367 | (list | |
55d1cfe8 VJL |
2368 | ;; Show SPACEs after TAB (indent-tabs-mode) |
2369 | (list (whitespace-space-after-tab-regexp) | |
c9dff4e3 | 2370 | 1 whitespace-space-after-tab t)) |
97a739d5 | 2371 | t)) |
ae68f2d8 | 2372 | ((memq 'space-after-tab::tab whitespace-active-style) |
55d1cfe8 VJL |
2373 | (font-lock-add-keywords |
2374 | nil | |
2375 | (list | |
2376 | ;; Show SPACEs after TAB (SPACEs) | |
2377 | (list (whitespace-space-after-tab-regexp 'tab) | |
2378 | 1 whitespace-space-after-tab t)) | |
2379 | t)) | |
ae68f2d8 | 2380 | ((memq 'space-after-tab::space whitespace-active-style) |
55d1cfe8 VJL |
2381 | (font-lock-add-keywords |
2382 | nil | |
2383 | (list | |
2384 | ;; Show SPACEs after TAB (TABs) | |
2385 | (list (whitespace-space-after-tab-regexp 'space) | |
2386 | 1 whitespace-space-after-tab t)) | |
2387 | t))) | |
30d0ade9 VJL |
2388 | ;; now turn on font lock and highlight blanks |
2389 | (font-lock-mode 1))) | |
2390 | ||
2391 | ||
c9dff4e3 | 2392 | (defun whitespace-color-off () |
040f578c | 2393 | "Turn off color visualization." |
ae68f2d8 VJL |
2394 | ;; turn off font lock |
2395 | (when (whitespace-style-face-p) | |
040f578c | 2396 | (font-lock-mode 0) |
80525855 VJL |
2397 | (remove-hook 'post-command-hook #'whitespace-post-command-hook t) |
2398 | (remove-hook 'before-change-functions #'whitespace-buffer-changed t) | |
c9dff4e3 VJL |
2399 | (when whitespace-font-lock |
2400 | (setq whitespace-font-lock nil | |
2401 | font-lock-keywords whitespace-font-lock-keywords)) | |
30d0ade9 | 2402 | ;; restore original font lock state |
c9dff4e3 | 2403 | (font-lock-mode whitespace-font-lock-mode))) |
30d0ade9 | 2404 | |
32a9841c VJL |
2405 | |
2406 | (defun whitespace-trailing-regexp (limit) | |
748e3d67 | 2407 | "Match trailing spaces which do not contain the point at end of line." |
32a9841c VJL |
2408 | (let ((status t)) |
2409 | (while (if (re-search-forward whitespace-trailing-regexp limit t) | |
2410 | (save-match-data | |
2411 | (= whitespace-point (match-end 1))) ;; loop if point at eol | |
2412 | (setq status nil))) ;; end of buffer | |
2413 | status)) | |
2414 | ||
2415 | ||
2416 | (defun whitespace-empty-at-bob-regexp (limit) | |
748e3d67 | 2417 | "Match spaces at beginning of buffer which do not contain the point at \ |
32a9841c | 2418 | beginning of buffer." |
80525855 VJL |
2419 | (let ((b (point)) |
2420 | r) | |
2421 | (cond | |
2422 | ;; at bob | |
2423 | ((= b 1) | |
2424 | (setq r (and (/= whitespace-point 1) | |
2425 | (looking-at whitespace-empty-at-bob-regexp))) | |
2426 | (if r | |
2427 | (set-marker whitespace-bob-marker (match-end 1)) | |
2428 | (set-marker whitespace-bob-marker b))) | |
2429 | ;; inside bob empty region | |
2430 | ((<= limit whitespace-bob-marker) | |
2431 | (setq r (looking-at whitespace-empty-at-bob-regexp)) | |
2432 | (if r | |
2433 | (when (< (match-end 1) limit) | |
2434 | (set-marker whitespace-bob-marker (match-end 1))) | |
2435 | (set-marker whitespace-bob-marker b))) | |
2436 | ;; intersection with end of bob empty region | |
2437 | ((<= b whitespace-bob-marker) | |
2438 | (setq r (looking-at whitespace-empty-at-bob-regexp)) | |
2439 | (if r | |
2440 | (set-marker whitespace-bob-marker (match-end 1)) | |
2441 | (set-marker whitespace-bob-marker b))) | |
2442 | ;; it is not inside bob empty region | |
2443 | (t | |
2444 | (setq r nil))) | |
2445 | ;; move to end of matching | |
2446 | (and r (goto-char (match-end 1))) | |
2447 | r)) | |
2448 | ||
2449 | ||
2450 | (defsubst whitespace-looking-back (regexp limit) | |
2451 | (save-excursion | |
2452 | (when (/= 0 (skip-chars-backward " \t\n" limit)) | |
2453 | (unless (bolp) | |
2454 | (forward-line 1)) | |
2455 | (looking-at regexp)))) | |
32a9841c VJL |
2456 | |
2457 | ||
2458 | (defun whitespace-empty-at-eob-regexp (limit) | |
748e3d67 | 2459 | "Match spaces at end of buffer which do not contain the point at end of \ |
32a9841c | 2460 | buffer." |
80525855 VJL |
2461 | (let ((b (point)) |
2462 | (e (1+ (buffer-size))) | |
2463 | r) | |
2464 | (cond | |
2465 | ;; at eob | |
2466 | ((= limit e) | |
2467 | (when (/= whitespace-point e) | |
2468 | (goto-char limit) | |
2469 | (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))) | |
2470 | (if r | |
2471 | (set-marker whitespace-eob-marker (match-beginning 1)) | |
2472 | (set-marker whitespace-eob-marker limit) | |
2473 | (goto-char b))) ; return back to initial position | |
2474 | ;; inside eob empty region | |
2475 | ((>= b whitespace-eob-marker) | |
2476 | (goto-char limit) | |
2477 | (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)) | |
2478 | (if r | |
2479 | (when (> (match-beginning 1) b) | |
2480 | (set-marker whitespace-eob-marker (match-beginning 1))) | |
2481 | (set-marker whitespace-eob-marker limit) | |
2482 | (goto-char b))) ; return back to initial position | |
2483 | ;; intersection with beginning of eob empty region | |
2484 | ((>= limit whitespace-eob-marker) | |
2485 | (goto-char limit) | |
2486 | (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)) | |
2487 | (if r | |
2488 | (set-marker whitespace-eob-marker (match-beginning 1)) | |
2489 | (set-marker whitespace-eob-marker limit) | |
2490 | (goto-char b))) ; return back to initial position | |
2491 | ;; it is not inside eob empty region | |
2492 | (t | |
2493 | (setq r nil))) | |
2494 | r)) | |
2495 | ||
2496 | ||
2497 | (defun whitespace-buffer-changed (beg end) | |
2498 | "Set `whitespace-buffer-changed' variable to t." | |
2499 | (setq whitespace-buffer-changed t)) | |
32a9841c VJL |
2500 | |
2501 | ||
2502 | (defun whitespace-post-command-hook () | |
2503 | "Save current point into `whitespace-point' variable. | |
2504 | Also refontify when necessary." | |
80525855 VJL |
2505 | (setq whitespace-point (point)) ; current point position |
2506 | (let ((refontify | |
2507 | (or | |
2508 | ;; it is at end of line ... | |
2509 | (and (eolp) | |
2510 | ;; ... with trailing SPACE or TAB | |
2511 | (or (= (preceding-char) ?\ ) | |
2512 | (= (preceding-char) ?\t))) | |
2513 | ;; it is at beginning of buffer (bob) | |
2514 | (= whitespace-point 1) | |
2515 | ;; the buffer was modified and ... | |
2516 | (and whitespace-buffer-changed | |
2517 | (or | |
2518 | ;; ... or inside bob whitespace region | |
2519 | (<= whitespace-point whitespace-bob-marker) | |
2520 | ;; ... or at bob whitespace region border | |
2521 | (and (= whitespace-point (1+ whitespace-bob-marker)) | |
2522 | (= (preceding-char) ?\n)))) | |
2523 | ;; it is at end of buffer (eob) | |
2524 | (= whitespace-point (1+ (buffer-size))) | |
2525 | ;; the buffer was modified and ... | |
2526 | (and whitespace-buffer-changed | |
2527 | (or | |
2528 | ;; ... or inside eob whitespace region | |
2529 | (>= whitespace-point whitespace-eob-marker) | |
2530 | ;; ... or at eob whitespace region border | |
2531 | (and (= whitespace-point (1- whitespace-eob-marker)) | |
2532 | (= (following-char) ?\n))))))) | |
2533 | (when (or refontify (> whitespace-font-lock-refontify 0)) | |
2534 | (setq whitespace-buffer-changed nil) | |
2535 | ;; adjust refontify counter | |
2536 | (setq whitespace-font-lock-refontify | |
2537 | (if refontify | |
2538 | 1 | |
2539 | (1- whitespace-font-lock-refontify))) | |
2540 | ;; refontify | |
32a9841c VJL |
2541 | (jit-lock-refontify)))) |
2542 | ||
30d0ade9 VJL |
2543 | \f |
2544 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2545 | ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>) | |
2546 | ||
2547 | ||
ae68f2d8 VJL |
2548 | (defun whitespace-style-mark-p () |
2549 | "Return t if there is some visualization via display table." | |
2550 | (or (memq 'tab-mark whitespace-active-style) | |
2551 | (memq 'space-mark whitespace-active-style) | |
2552 | (memq 'newline-mark whitespace-active-style))) | |
2553 | ||
2554 | ||
c9dff4e3 | 2555 | (defsubst whitespace-char-valid-p (char) |
30d0ade9 VJL |
2556 | ;; This check should be improved!!! |
2557 | (or (< char 256) | |
434b9acb | 2558 | (characterp char))) |
30d0ade9 VJL |
2559 | |
2560 | ||
c9dff4e3 | 2561 | (defun whitespace-display-vector-p (vec) |
30d0ade9 VJL |
2562 | "Return true if every character in vector VEC can be displayed." |
2563 | (let ((i (length vec))) | |
2564 | (when (> i 0) | |
2565 | (while (and (>= (setq i (1- i)) 0) | |
c9dff4e3 | 2566 | (whitespace-char-valid-p (aref vec i)))) |
30d0ade9 VJL |
2567 | (< i 0)))) |
2568 | ||
2569 | ||
c9dff4e3 | 2570 | (defun whitespace-display-char-on () |
30d0ade9 | 2571 | "Turn on character display mapping." |
ae68f2d8 VJL |
2572 | (when (and whitespace-display-mappings |
2573 | (whitespace-style-mark-p)) | |
30d0ade9 VJL |
2574 | (let (vecs vec) |
2575 | ;; Remember whether a buffer has a local display table. | |
c9dff4e3 VJL |
2576 | (unless whitespace-display-table-was-local |
2577 | (setq whitespace-display-table-was-local t | |
2578 | whitespace-display-table | |
80525855 VJL |
2579 | (copy-sequence buffer-display-table)) |
2580 | ;; asure `buffer-display-table' is unique | |
2581 | ;; when two or more windows are visible. | |
2582 | (setq buffer-display-table | |
30d0ade9 VJL |
2583 | (copy-sequence buffer-display-table))) |
2584 | (unless buffer-display-table | |
2585 | (setq buffer-display-table (make-display-table))) | |
c9dff4e3 | 2586 | (dolist (entry whitespace-display-mappings) |
55d1cfe8 | 2587 | ;; check if it is to display this mark |
ae68f2d8 | 2588 | (when (memq (car entry) whitespace-style) |
55d1cfe8 VJL |
2589 | ;; Get a displayable mapping. |
2590 | (setq vecs (cddr entry)) | |
2591 | (while (and vecs | |
2592 | (not (whitespace-display-vector-p (car vecs)))) | |
2593 | (setq vecs (cdr vecs))) | |
2594 | ;; Display a valid mapping. | |
2595 | (when vecs | |
2596 | (setq vec (copy-sequence (car vecs))) | |
2597 | ;; NEWLINE char | |
2598 | (when (and (eq (cadr entry) ?\n) | |
ae68f2d8 | 2599 | (memq 'newline whitespace-active-style)) |
55d1cfe8 VJL |
2600 | ;; Only insert face bits on NEWLINE char mapping to avoid |
2601 | ;; obstruction of other faces like TABs and (HARD) SPACEs | |
2602 | ;; faces, font-lock faces, etc. | |
30d0ade9 | 2603 | (dotimes (i (length vec)) |
30d0ade9 | 2604 | (or (eq (aref vec i) ?\n) |
c9dff4e3 VJL |
2605 | (aset vec i |
2606 | (make-glyph-code (aref vec i) | |
2607 | whitespace-newline))))) | |
30d0ade9 | 2608 | ;; Display mapping |
55d1cfe8 | 2609 | (aset buffer-display-table (cadr entry) vec))))))) |
30d0ade9 VJL |
2610 | |
2611 | ||
c9dff4e3 | 2612 | (defun whitespace-display-char-off () |
30d0ade9 | 2613 | "Turn off character display mapping." |
c9dff4e3 | 2614 | (and whitespace-display-mappings |
ae68f2d8 | 2615 | (whitespace-style-mark-p) |
c9dff4e3 VJL |
2616 | whitespace-display-table-was-local |
2617 | (setq whitespace-display-table-was-local nil | |
2618 | buffer-display-table whitespace-display-table))) | |
596c0ee1 VJL |
2619 | |
2620 | \f | |
94dc593f VJL |
2621 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2622 | ;;;; Hook | |
2623 | ||
2624 | ||
2625 | (defun whitespace-action-when-on () | |
2626 | "Action to be taken always when local whitespace is turned on." | |
2627 | (cond ((memq 'cleanup whitespace-action) | |
2628 | (whitespace-cleanup)) | |
2629 | ((memq 'report-on-bogus whitespace-action) | |
2630 | (whitespace-report nil t)))) | |
2631 | ||
2632 | ||
94dc593f VJL |
2633 | (defun whitespace-write-file-hook () |
2634 | "Action to be taken when buffer is written. | |
2635 | It should be added buffer-locally to `write-file-functions'." | |
94dc593f | 2636 | (cond ((memq 'auto-cleanup whitespace-action) |
97f6e1ad | 2637 | (whitespace-cleanup)) |
94dc593f | 2638 | ((memq 'abort-on-bogus whitespace-action) |
97f6e1ad VJL |
2639 | (when (whitespace-report nil t) |
2640 | (error "Abort write due to whitespace problems in %s" | |
2641 | (buffer-name))))) | |
2642 | nil) ; continue hook processing | |
94dc593f | 2643 | |
bc27c677 VJL |
2644 | |
2645 | (defun whitespace-warn-read-only (msg) | |
2646 | "Warn if buffer is read-only." | |
97f6e1ad | 2647 | (when (memq 'warn-if-read-only whitespace-action) |
bc27c677 VJL |
2648 | (message "Can't %s: %s is read-only" msg (buffer-name)))) |
2649 | ||
94dc593f | 2650 | \f |
30d0ade9 VJL |
2651 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2652 | ||
2653 | ||
1e2b96c2 | 2654 | (defun whitespace-unload-function () |
94dc593f VJL |
2655 | "Unload the whitespace library." |
2656 | (global-whitespace-mode -1) | |
2657 | ;; be sure all local whitespace mode is turned off | |
2658 | (save-current-buffer | |
2659 | (dolist (buf (buffer-list)) | |
2660 | (set-buffer buf) | |
2661 | (whitespace-mode -1))) | |
2662 | nil) ; continue standard unloading | |
2663 | ||
1e2b96c2 | 2664 | |
c9dff4e3 | 2665 | (provide 'whitespace) |
30d0ade9 VJL |
2666 | |
2667 | ||
c9dff4e3 | 2668 | (run-hooks 'whitespace-load-hook) |
30d0ade9 VJL |
2669 | |
2670 | ||
17f6865b | 2671 | ;; arch-tag: 1b1e2500-dbd4-4a26-8f7a-5a5edfd3c97e |
c9dff4e3 | 2672 | ;;; whitespace.el ends here |