comparison lisp/whitespace.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 37ddf84a7528
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file 1 ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
2 2
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. 3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
4 5
5 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org> 6 ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
6 ;; Keywords: convenience 7 ;; Keywords: convenience
7 8
8 ;; $Id: whitespace.el,v 1.21 2002/09/13 06:21:32 lektu Exp $
9 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
10 10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify 11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by 12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option) 13 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details. 19 ;; GNU General Public License for more details.
20 20
21 ;; You should have received a copy of the GNU General Public License 21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02111-1307, USA. 24 ;; Boston, MA 02110-1301, USA.
25 25
26 ;;; Commentary: 26 ;;; Commentary:
27 27 ;;
28 ;; Whitespace.el URL: http://www.dsmit.com/lisp/ 28 ;; URL: http://www.dsmit.com/lisp/
29 29 ;;
30 ;; The whitespace library is intended to find and help fix five different types 30 ;; The whitespace library is intended to find and help fix five different types
31 ;; of whitespace problems that commonly exist in source code. 31 ;; of whitespace problems that commonly exist in source code.
32 32 ;;
33 ;; 1. Leading space (empty lines at the top of a file). 33 ;; 1. Leading space (empty lines at the top of a file).
34 ;; 2. Trailing space (empty lines at the end of a file). 34 ;; 2. Trailing space (empty lines at the end of a file).
35 ;; 3. Indentation space (8 or more spaces at beginning of line, that should be 35 ;; 3. Indentation space (8 or more spaces at beginning of line, that should be
36 ;; replaced with TABS). 36 ;; replaced with TABS).
37 ;; 4. Spaces followed by a TAB. (Almost always, we never want that). 37 ;; 4. Spaces followed by a TAB. (Almost always, we never want that).
38 ;; 5. Spaces or TABS at the end of a line. 38 ;; 5. Spaces or TABS at the end of a line.
39 39 ;;
40 ;; Whitespace errors are reported in a buffer, and on the modeline. 40 ;; Whitespace errors are reported in a buffer, and on the modeline.
41 41 ;;
42 ;; Modeline will show a W:<x>!<y> to denote a particular type of whitespace, 42 ;; Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
43 ;; where `x' and `y' can be one (or more) of: 43 ;; where `x' and `y' can be one (or more) of:
44 44 ;;
45 ;; e - End-of-Line whitespace. 45 ;; e - End-of-Line whitespace.
46 ;; i - Indentation whitespace. 46 ;; i - Indentation whitespace.
47 ;; l - Leading whitespace. 47 ;; l - Leading whitespace.
48 ;; s - Space followed by Tab. 48 ;; s - Space followed by Tab.
49 ;; t - Trailing whitespace. 49 ;; t - Trailing whitespace.
50 50 ;;
51 ;; If any of the whitespace checks is turned off, the modeline will display a 51 ;; If any of the whitespace checks is turned off, the modeline will display a
52 ;; !<y>. 52 ;; !<y>.
53 53 ;;
54 ;; (since (3) is the most controversial one, here is the rationale: Most 54 ;; (since (3) is the most controversial one, here is the rationale: Most
55 ;; terminal drivers and printer drivers have TAB configured or even 55 ;; terminal drivers and printer drivers have TAB configured or even
56 ;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost 56 ;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost
57 ;; always they default to 8.) 57 ;; always they default to 8.)
58 58 ;;
59 ;; Changing `tab-width' to other than 8 and editing will cause your code to 59 ;; Changing `tab-width' to other than 8 and editing will cause your code to
60 ;; look different from within Emacs, and say, if you cat it or more it, or 60 ;; look different from within Emacs, and say, if you cat it or more it, or
61 ;; even print it. 61 ;; even print it.
62 62 ;;
63 ;; Almost all the popular programming modes let you define an offset (like 63 ;; Almost all the popular programming modes let you define an offset (like
64 ;; c-basic-offset or perl-indent-level) to configure the offset, so you 64 ;; c-basic-offset or perl-indent-level) to configure the offset, so you
65 ;; should never have to set your `tab-width' to be other than 8 in all 65 ;; should never have to set your `tab-width' to be other than 8 in all
66 ;; these modes. In fact, with an indent level of say, 4, 2 TABS will cause 66 ;; these modes. In fact, with an indent level of say, 4, 2 TABS will cause
67 ;; Emacs to replace your 8 spaces with one \t (try it). If vi users in 67 ;; Emacs to replace your 8 spaces with one \t (try it). If vi users in
68 ;; your office complain, tell them to use vim, which distinguishes between 68 ;; your office complain, tell them to use vim, which distinguishes between
69 ;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them 69 ;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
70 ;; to set smarttab.) 70 ;; to set smarttab.)
71 71 ;;
72 ;; All the above have caused (and will cause) unwanted codeline integration and 72 ;; All the above have caused (and will cause) unwanted codeline integration and
73 ;; merge problems. 73 ;; merge problems.
74 74 ;;
75 ;; whitespace.el will complain if it detects whitespaces on opening a file, and 75 ;; whitespace.el will complain if it detects whitespaces on opening a file, and
76 ;; warn you on closing a file also (in case you had inserted any 76 ;; warn you on closing a file also (in case you had inserted any
77 ;; whitespaces during the process of your editing). 77 ;; whitespaces during the process of your editing).
78 78 ;;
79 ;; Exported functions: 79 ;; Exported functions:
80 80 ;;
81 ;; `whitespace-buffer' - To check the current buffer for whitespace problems. 81 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
82 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer. 82 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
83 ;; `whitespace-region' - To check between point and mark for whitespace 83 ;; `whitespace-region' - To check between point and mark for whitespace
84 ;; problems. 84 ;; problems.
85 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point 85 ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
86 ;; and mark in the current buffer. 86 ;; and mark in the current buffer.
87 87
88 ;;; Code: 88 ;;; Code:
89 89
90 (defvar whitespace-version "3.2" "Version of the whitespace library.") 90 (defvar whitespace-version "3.5" "Version of the whitespace library.")
91 91
92 (defvar whitespace-all-buffer-files nil 92 (defvar whitespace-all-buffer-files nil
93 "An associated list of buffers and files checked for whitespace cleanliness. 93 "An associated list of buffers and files checked for whitespace cleanliness.
94 94
95 This is to enable periodic checking of whitespace cleanliness in the files 95 This is to enable periodic checking of whitespace cleanliness in the files
108 "String to display in the mode line for Whitespace mode.") 108 "String to display in the mode line for Whitespace mode.")
109 (make-variable-buffer-local 'whitespace-mode-line) 109 (make-variable-buffer-local 'whitespace-mode-line)
110 (put 'whitespace-mode-line 'permanent-local nil) 110 (put 'whitespace-mode-line 'permanent-local nil)
111 111
112 (defvar whitespace-check-buffer-leading nil 112 (defvar whitespace-check-buffer-leading nil
113 "Test leading whitespace for file in current buffer if t") 113 "Test leading whitespace for file in current buffer if t.")
114 (make-variable-buffer-local 'whitespace-check-buffer-leading) 114 (make-variable-buffer-local 'whitespace-check-buffer-leading)
115 (put 'whitespace-check-buffer-leading 'permanent-local nil) 115 (put 'whitespace-check-buffer-leading 'permanent-local nil)
116 116
117 (defvar whitespace-check-buffer-trailing nil 117 (defvar whitespace-check-buffer-trailing nil
118 "Test trailing whitespace for file in current buffer if t") 118 "Test trailing whitespace for file in current buffer if t.")
119 (make-variable-buffer-local 'whitespace-check-buffer-trailing) 119 (make-variable-buffer-local 'whitespace-check-buffer-trailing)
120 (put 'whitespace-check-buffer-trailing 'permanent-local nil) 120 (put 'whitespace-check-buffer-trailing 'permanent-local nil)
121 121
122 (defvar whitespace-check-buffer-indent nil 122 (defvar whitespace-check-buffer-indent nil
123 "Test indentation whitespace for file in current buffer if t") 123 "Test indentation whitespace for file in current buffer if t.")
124 (make-variable-buffer-local 'whitespace-check-buffer-indent) 124 (make-variable-buffer-local 'whitespace-check-buffer-indent)
125 (put 'whitespace-check-buffer-indent 'permanent-local nil) 125 (put 'whitespace-check-buffer-indent 'permanent-local nil)
126 126
127 (defvar whitespace-check-buffer-spacetab nil 127 (defvar whitespace-check-buffer-spacetab nil
128 "Test Space-followed-by-TABS whitespace for file in current buffer if t") 128 "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
129 (make-variable-buffer-local 'whitespace-check-buffer-spacetab) 129 (make-variable-buffer-local 'whitespace-check-buffer-spacetab)
130 (put 'whitespace-check-buffer-spacetab 'permanent-local nil) 130 (put 'whitespace-check-buffer-spacetab 'permanent-local nil)
131 131
132 (defvar whitespace-check-buffer-ateol nil 132 (defvar whitespace-check-buffer-ateol nil
133 "Test end-of-line whitespace for file in current buffer if t") 133 "Test end-of-line whitespace for file in current buffer if t.")
134 (make-variable-buffer-local 'whitespace-check-buffer-ateol) 134 (make-variable-buffer-local 'whitespace-check-buffer-ateol)
135 (put 'whitespace-check-buffer-ateol 'permanent-local nil) 135 (put 'whitespace-check-buffer-ateol 'permanent-local nil)
136 136
137 (defvar whitespace-highlighted-space nil 137 (defvar whitespace-highlighted-space nil
138 "The variable to store the extent to highlight") 138 "The variable to store the extent to highlight.")
139 (make-variable-buffer-local 'whitespace-highlighted-space) 139 (make-variable-buffer-local 'whitespace-highlighted-space)
140 (put 'whitespace-highlighted-space 'permanent-local nil) 140 (put 'whitespace-highlighted-space 'permanent-local nil)
141 141
142 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'. 142 ;; For flavors of Emacs which don't define `defgroup' and `defcustom'.
143 (eval-when-compile 143 (eval-when-compile
144 (if (not (fboundp 'defgroup)) 144 (if (not (fboundp 'defgroup))
145 (defmacro defgroup (sym memb doc &rest args) 145 (defmacro defgroup (sym memb doc &rest args)
146 "Null macro for defgroup in all versions of Emacs that don't define 146 "Null macro for `defgroup' in all versions of Emacs that don't define it."
147 defgroup"
148 t)) 147 t))
149 (if (not (fboundp 'defcustom)) 148 (if (not (fboundp 'defcustom))
150 (defmacro defcustom (sym val doc &rest args) 149 (defmacro defcustom (sym val doc &rest args)
151 "Macro to alias defcustom to defvar in all versions of Emacs that 150 "Macro to alias `defcustom' to `defvar' in all versions of Emacs that
152 don't define defcustom" 151 don't define it."
153 `(defvar ,sym ,val ,doc)))) 152 `(defvar ,sym ,val ,doc))))
154 153
155 (if (fboundp 'make-overlay) 154 (defalias 'whitespace-make-overlay
156 (progn 155 (if (featurep 'xemacs) 'make-extent 'make-overlay))
157 (defalias 'whitespace-make-overlay 'make-overlay) 156 (defalias 'whitespace-overlay-put
158 (defalias 'whitespace-overlay-put 'overlay-put) 157 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
159 (defalias 'whitespace-delete-overlay 'delete-overlay) 158 (defalias 'whitespace-delete-overlay
160 (defalias 'whitespace-overlay-start 'overlay-start) 159 (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
161 (defalias 'whitespace-overlay-end 'overlay-end) 160 (defalias 'whitespace-overlay-start
162 (defalias 'whitespace-mode-line-update 'force-mode-line-update)) 161 (if (featurep 'xemacs) 'extent-start 'overlay-start))
163 (defalias 'whitespace-make-overlay 'make-extent) 162 (defalias 'whitespace-overlay-end
164 (defalias 'whitespace-overlay-put 'set-extent-property) 163 (if (featurep 'xemacs) 'extent-end 'overlay-end))
165 (defalias 'whitespace-delete-overlay 'delete-extent) 164 (defalias 'whitespace-mode-line-update
166 (defalias 'whitespace-overlay-start 'extent-start) 165 (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
167 (defalias 'whitespace-overlay-end 'extent-end) 166
168 (defalias 'whitespace-mode-line-update 'redraw-modeline))
169
170 (if (featurep 'xemacs)
171 (defgroup whitespace nil
172 "Check for and fix five different types of whitespaces in source code."
173 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
174 ;; which is 'editing?
175 :group 'editing)
176 (defgroup whitespace nil 167 (defgroup whitespace nil
177 "Check for and fix five different types of whitespaces in source code." 168 "Check for and fix five different types of whitespaces in source code."
178 :version "21.1" 169 :version "21.1"
179 :group 'convenience)) 170 :link '(emacs-commentary-link "whitespace.el")
171 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
172 ;; which is 'editing?
173 :group (if (featurep 'xemacs) 'editing 'convenience))
180 174
181 (defcustom whitespace-check-leading-whitespace t 175 (defcustom whitespace-check-leading-whitespace t
182 "Flag to check leading whitespace. This is the global for the system. 176 "Flag to check leading whitespace. This is the global for the system.
183 It can be overriden by setting a buffer local variable 177 It can be overriden by setting a buffer local variable
184 `whitespace-check-buffer-leading'" 178 `whitespace-check-buffer-leading'."
185 :type 'boolean 179 :type 'boolean
186 :group 'whitespace) 180 :group 'whitespace)
187 181
188 (defcustom whitespace-check-trailing-whitespace t 182 (defcustom whitespace-check-trailing-whitespace t
189 "Flag to check trailing whitespace. This is the global for the system. 183 "Flag to check trailing whitespace. This is the global for the system.
190 It can be overriden by setting a buffer local variable 184 It can be overriden by setting a buffer local variable
191 `whitespace-check-buffer-trailing'" 185 `whitespace-check-buffer-trailing'."
192 :type 'boolean 186 :type 'boolean
193 :group 'whitespace) 187 :group 'whitespace)
194 188
195 (defcustom whitespace-check-spacetab-whitespace t 189 (defcustom whitespace-check-spacetab-whitespace t
196 "Flag to check space followed by a TAB. This is the global for the system. 190 "Flag to check space followed by a TAB. This is the global for the system.
197 It can be overriden by setting a buffer local variable 191 It can be overriden by setting a buffer local variable
198 `whitespace-check-buffer-spacetab'" 192 `whitespace-check-buffer-spacetab'."
199 :type 'boolean 193 :type 'boolean
200 :group 'whitespace) 194 :group 'whitespace)
201 195
202 (defcustom whitespace-spacetab-regexp "[ ]+\t" 196 (defcustom whitespace-spacetab-regexp "[ ]+\t"
203 "Regexp to match a space followed by a TAB." 197 "Regexp to match a space followed by a TAB."
204 :type 'regexp 198 :type 'regexp
205 :group 'whitespace) 199 :group 'whitespace)
206 200
207 (defcustom whitespace-check-indent-whitespace indent-tabs-mode 201 (defcustom whitespace-check-indent-whitespace indent-tabs-mode
208 "Flag to check indentation whitespace. This is the global for the system. 202 "Flag to check indentation whitespace. This is the global for the system.
209 It can be overriden by setting a buffer local variable 203 It can be overriden by setting a buffer local variable
210 `whitespace-check-buffer-indent'" 204 `whitespace-check-buffer-indent'."
211 :type 'boolean 205 :type 'boolean
212 :group 'whitespace) 206 :group 'whitespace)
213 207
214 (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ") 208 (defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ")
215 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line." 209 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
216 :type 'regexp 210 :type 'regexp
217 :group 'whitespace) 211 :group 'whitespace)
218 212
219 (defcustom whitespace-check-ateol-whitespace t 213 (defcustom whitespace-check-ateol-whitespace t
220 "Flag to check end-of-line whitespace. This is the global for the system. 214 "Flag to check end-of-line whitespace. This is the global for the system.
221 It can be overriden by setting a buffer local variable 215 It can be overriden by setting a buffer local variable
222 `whitespace-check-buffer-ateol'" 216 `whitespace-check-buffer-ateol'."
223 :type 'boolean 217 :type 'boolean
224 :group 'whitespace) 218 :group 'whitespace)
225 219
226 ;; (defcustom whitespace-ateol-regexp "[ \t]$" 220 ;; (defcustom whitespace-ateol-regexp "[ \t]$"
227 (defcustom whitespace-ateol-regexp "[ \t]+$" 221 (defcustom whitespace-ateol-regexp "[ \t]+$"
232 (defcustom whitespace-errbuf "*Whitespace Errors*" 226 (defcustom whitespace-errbuf "*Whitespace Errors*"
233 "The name of the buffer where whitespace related messages will be logged." 227 "The name of the buffer where whitespace related messages will be logged."
234 :type 'string 228 :type 'string
235 :group 'whitespace) 229 :group 'whitespace)
236 230
231 (defcustom whitespace-clean-msg "clean."
232 "If non-nil, this message will be displayed after a whitespace check
233 determines a file to be clean."
234 :type 'string
235 :group 'whitespace)
236
237 (defcustom whitespace-abort-on-error nil 237 (defcustom whitespace-abort-on-error nil
238 "While writing a file, abort if the file is unclean. If 238 "While writing a file, abort if the file is unclean.
239 `whitespace-auto-cleanup' is set, that takes precedence over this 239 If `whitespace-auto-cleanup' is set, that takes precedence over
240 variable." 240 this variable."
241 :type 'boolean 241 :type 'boolean
242 :group 'whitespace) 242 :group 'whitespace)
243 243
244 (defcustom whitespace-auto-cleanup nil 244 (defcustom whitespace-auto-cleanup nil
245 "Cleanup a buffer automatically on finding it whitespace unclean." 245 "Cleanup a buffer automatically on finding it whitespace unclean."
268 python-mode scheme-mode sgml-mode 268 python-mode scheme-mode sgml-mode
269 sh-mode shell-script-mode simula-mode 269 sh-mode shell-script-mode simula-mode
270 tcl-mode tex-mode texinfo-mode 270 tcl-mode tex-mode texinfo-mode
271 vrml-mode xml-mode) 271 vrml-mode xml-mode)
272 272
273 "Major Modes in which we turn on whitespace checking. 273 "Major modes in which we turn on whitespace checking.
274 274
275 These are mostly programming and documentation modes. But you may add other 275 These are mostly programming and documentation modes. But you may add other
276 modes that you want whitespaces checked in by adding something like the 276 modes that you want whitespaces checked in by adding something like the
277 following to your `.emacs': 277 following to your `.emacs':
278 278
298 "Display whitespace errors on the modeline." 298 "Display whitespace errors on the modeline."
299 :type 'boolean 299 :type 'boolean
300 :group 'whitespace) 300 :group 'whitespace)
301 301
302 (defcustom whitespace-display-spaces-in-color t 302 (defcustom whitespace-display-spaces-in-color t
303 "Display the bogus whitespaces by coloring them with 303 "Display the bogus whitespaces by coloring them with the face
304 `whitespace-highlight-face'." 304 `whitespace-highlight'."
305 :type 'boolean 305 :type 'boolean
306 :group 'whitespace) 306 :group 'whitespace)
307 307
308 (defgroup whitespace-faces nil 308 (defgroup whitespace-faces nil
309 "Faces used in whitespace." 309 "Faces used in whitespace."
310 :prefix "whitespace-" 310 :prefix "whitespace-"
311 :group 'whitespace 311 :group 'whitespace
312 :group 'faces) 312 :group 'faces)
313 313
314 (defface whitespace-highlight-face '((((class color) (background light)) 314 (defface whitespace-highlight '((((class color) (background light))
315 (:background "green")) 315 (:background "green1"))
316 (((class color) (background dark)) 316 (((class color) (background dark))
317 (:background "sea green")) 317 (:background "sea green"))
318 (((class grayscale mono) 318 (((class grayscale mono)
319 (background light)) 319 (background light))
320 (:background "black")) 320 (:background "black"))
321 (((class grayscale mono) 321 (((class grayscale mono)
322 (background dark)) 322 (background dark))
323 (:background "white"))) 323 (:background "white")))
324 "Face used for highlighting the bogus whitespaces that exist in the buffer." 324 "Face used for highlighting the bogus whitespaces that exist in the buffer."
325 :group 'whitespace-faces) 325 :group 'whitespace-faces)
326 ;; backward-compatibility alias
327 (put 'whitespace-highlight-face 'face-alias 'whitespace-highlight)
326 328
327 (if (not (assoc 'whitespace-mode minor-mode-alist)) 329 (if (not (assoc 'whitespace-mode minor-mode-alist))
328 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line) 330 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
329 minor-mode-alist))) 331 minor-mode-alist)))
330 332
480 (if whitespace-indent "i") 482 (if whitespace-indent "i")
481 (if whitespace-leading "l") 483 (if whitespace-leading "l")
482 (if whitespace-spacetab "s") 484 (if whitespace-spacetab "s")
483 (if whitespace-trailing "t"))))) 485 (if whitespace-trailing "t")))))
484 (whitespace-update-modeline whitespace-this-modeline) 486 (whitespace-update-modeline whitespace-this-modeline)
485 (save-excursion 487 (if (get-buffer whitespace-errbuf)
486 (get-buffer-create whitespace-errbuf) 488 (kill-buffer whitespace-errbuf))
487 (kill-buffer whitespace-errbuf) 489 (with-current-buffer (get-buffer-create whitespace-errbuf)
488 (get-buffer-create whitespace-errbuf)
489 (set-buffer whitespace-errbuf)
490 (if whitespace-errmsg 490 (if whitespace-errmsg
491 (progn 491 (progn
492 (insert whitespace-errmsg) 492 (insert whitespace-errmsg)
493 (if (not (or quiet whitespace-silent)) 493 (if (not (or quiet whitespace-silent))
494 (display-buffer whitespace-errbuf t)) 494 (display-buffer (current-buffer) t))
495 (if (not quiet) 495 (if (not quiet)
496 (message "Whitespaces: [%s%s] in %s" 496 (message "Whitespaces: [%s%s] in %s"
497 whitespace-this-modeline 497 whitespace-this-modeline
498 (let ((whitespace-unchecked 498 (let ((whitespace-unchecked
499 (whitespace-unchecked-whitespaces))) 499 (whitespace-unchecked-whitespaces)))
500 (if whitespace-unchecked 500 (if whitespace-unchecked
501 (concat "!" whitespace-unchecked) 501 (concat "!" whitespace-unchecked)
502 "")) 502 ""))
503 whitespace-filename))) 503 whitespace-filename)))
504 (if (not quiet) 504 (if (and (not quiet) (not (equal whitespace-clean-msg "")))
505 (message "%s clean" whitespace-filename)))))))) 505 (message "%s %s" whitespace-filename
506 (if whitespace-error 506 whitespace-clean-msg))))))))
507 t 507 whitespace-error))
508 nil)))
509 508
510 ;;;###autoload 509 ;;;###autoload
511 (defun whitespace-region (s e) 510 (defun whitespace-region (s e)
512 "Check the region for whitespace errors." 511 "Check the region for whitespace errors."
513 (interactive "r") 512 (interactive "r")
517 (whitespace-buffer)))) 516 (whitespace-buffer))))
518 517
519 ;;;###autoload 518 ;;;###autoload
520 (defun whitespace-cleanup () 519 (defun whitespace-cleanup ()
521 "Cleanup the five different kinds of whitespace problems. 520 "Cleanup the five different kinds of whitespace problems.
522 521 See `whitespace-buffer' docstring for a summary of the problems."
523 Use \\[describe-function] whitespace-describe to read a summary of the
524 whitespace problems."
525 (interactive) 522 (interactive)
526 ;; If this buffer really contains a file, then run, else quit. 523 ;; If this buffer really contains a file, then run, else quit.
527 (whitespace-check-whitespace-mode current-prefix-arg) 524 (whitespace-check-whitespace-mode current-prefix-arg)
528 (if (and buffer-file-name whitespace-mode) 525 (if (and buffer-file-name whitespace-mode)
529 (let ((whitespace-any nil) 526 (let ((whitespace-any nil)
566 563
567 ;; Call this recursively till everything is taken care of 564 ;; Call this recursively till everything is taken care of
568 (if whitespace-any 565 (if whitespace-any
569 (whitespace-cleanup) 566 (whitespace-cleanup)
570 (progn 567 (progn
571 (message "%s clean" buffer-file-name) 568 (if (not whitespace-silent)
569 (message "%s clean" buffer-file-name))
572 (whitespace-update-modeline))) 570 (whitespace-update-modeline)))
573 (setq tab-width whitespace-tabwith-saved)))) 571 (setq tab-width whitespace-tabwith-saved))))
574 572
575 ;;;###autoload 573 ;;;###autoload
576 (defun whitespace-cleanup-region (s e) 574 (defun whitespace-cleanup-region (s e)
592 (setq pmin (point)) 590 (setq pmin (point))
593 (end-of-line) 591 (end-of-line)
594 (setq pmax (point)) 592 (setq pmax (point))
595 (if (equal pmin pmax) 593 (if (equal pmin pmax)
596 (progn 594 (progn
597 (whitespace-highlight-the-space pmin pmax) 595 (whitespace-highlight-the-space pmin (1+ pmax))
598 t) 596 t)
599 nil)))) 597 nil))))
600 598
601 (defun whitespace-buffer-leading-cleanup () 599 (defun whitespace-buffer-leading-cleanup ()
602 "Remove any empty lines at the top of the file." 600 "Remove any empty lines at the top of the file."
603 (save-excursion 601 (save-excursion
604 (let ((pmin nil) 602 (goto-char (point-min))
605 (pmax nil)) 603 (skip-chars-forward "\n")
606 (goto-char (point-min)) 604 (delete-region (point-min) (point))))
607 (beginning-of-line)
608 (setq pmin (point))
609 (end-of-line)
610 (setq pmax (point))
611 (if (equal pmin pmax)
612 (progn
613 (kill-line)
614 (whitespace-buffer-leading-cleanup))))))
615 605
616 (defun whitespace-buffer-trailing () 606 (defun whitespace-buffer-trailing ()
617 "Check to see if are is more than one empty line at the bottom." 607 "Check to see if are is more than one empty line at the bottom."
618 (save-excursion 608 (save-excursion
619 (let ((pmin nil) 609 (let ((pmin nil)
630 (setq pmin (point)) 620 (setq pmin (point))
631 (end-of-line) 621 (end-of-line)
632 (setq pmax (point)) 622 (setq pmax (point))
633 (if (equal pmin pmax) 623 (if (equal pmin pmax)
634 (progn 624 (progn
635 (whitespace-highlight-the-space pmin pmax) 625 (whitespace-highlight-the-space (- pmin 1) pmax)
636 t) 626 t)
637 nil)) 627 nil))
638 nil)))) 628 nil))))
639 629
640 (defun whitespace-buffer-trailing-cleanup () 630 (defun whitespace-buffer-trailing-cleanup ()
641 "Delete all the empty lines at the bottom." 631 "Delete all the empty lines at the bottom."
642 (save-excursion 632 (save-excursion
643 (let ((pmin nil) 633 (goto-char (point-max))
644 (pmax nil)) 634 (skip-chars-backward "\n")
645 (goto-char (point-max)) 635 (if (not (bolp))
646 (beginning-of-line) 636 (forward-char 1))
647 (setq pmin (point)) 637 (delete-region (point) (point-max))))
648 (end-of-line)
649 (setq pmax (point))
650 (if (equal pmin pmax)
651 (progn
652 (goto-char (1- pmin))
653 (beginning-of-line)
654 (setq pmin (point))
655 (end-of-line)
656 (setq pmax (point))
657 (if (equal pmin pmax)
658 (progn
659 (goto-char (1- (point-max)))
660 (beginning-of-line)
661 (kill-line)
662 (whitespace-buffer-trailing-cleanup))))))))
663 638
664 (defun whitespace-buffer-search (regexp) 639 (defun whitespace-buffer-search (regexp)
665 "Search for any given whitespace REGEXP." 640 "Search for any given whitespace REGEXP."
666 (let ((whitespace-retval "")) 641 (let ((whitespace-retval ""))
667 (save-excursion 642 (save-excursion
725 (whitespace-mode-line-update)))) 700 (whitespace-mode-line-update))))
726 701
727 (defun whitespace-highlight-the-space (b e) 702 (defun whitespace-highlight-the-space (b e)
728 "Highlight the current line, unhighlighting a previously jumped to line." 703 "Highlight the current line, unhighlighting a previously jumped to line."
729 (if whitespace-display-spaces-in-color 704 (if whitespace-display-spaces-in-color
730 (progn 705 (let ((ol (whitespace-make-overlay b e)))
731 (whitespace-unhighlight-the-space) 706 (push ol whitespace-highlighted-space)
732 (add-to-list 'whitespace-highlighted-space 707 (whitespace-overlay-put ol 'face 'whitespace-highlight))))
733 (whitespace-make-overlay b e))
734 (whitespace-overlay-put (whitespace-make-overlay b e) 'face
735 'whitespace-highlight-face))))
736 ;; (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space)) 708 ;; (add-hook 'pre-command-hook 'whitespace-unhighlight-the-space))
737 709
738 (defun whitespace-unhighlight-the-space () 710 (defun whitespace-unhighlight-the-space()
739 "Unhighlight the currently highlight line." 711 "Unhighlight the currently highlight line."
740 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space) 712 (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
741 (let ((whitespace-this-space nil)) 713 (progn
742 (while whitespace-highlighted-space 714 (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
743 (setq whitespace-this-space (car whitespace-highlighted-space))
744 (setq whitespace-highlighted-space
745 (cdr whitespace-highlighted-space))
746 (whitespace-delete-overlay whitespace-this-space))
747 (setq whitespace-highlighted-space nil)) 715 (setq whitespace-highlighted-space nil))
748 (remove-hook 'pre-command-hook 'whitespace-unhighlight-the-space))) 716 (remove-hook 'pre-command-hook 'whitespace-unhighlight-the-space)))
749 717
750 (defun whitespace-check-buffer-list (buf-name buf-file) 718 (defun whitespace-check-buffer-list (buf-name buf-file)
751 "Add a buffer and its file to the whitespace monitor list. 719 "Add a buffer and its file to the whitespace monitor list.
809 (when whitespace-rescan-timer 777 (when whitespace-rescan-timer
810 (disable-timeout whitespace-rescan-timer) 778 (disable-timeout whitespace-rescan-timer)
811 (setq whitespace-rescan-timer nil)))) 779 (setq whitespace-rescan-timer nil))))
812 780
813 ;;;###autoload 781 ;;;###autoload
782 (defalias 'global-whitespace-mode 'whitespace-global-mode)
783
784 ;;;###autoload
814 (define-minor-mode whitespace-global-mode 785 (define-minor-mode whitespace-global-mode
815 "Toggle using Whitespace mode in new buffers. 786 "Toggle using Whitespace mode in new buffers.
816 With ARG, turn the mode on if and only iff ARG is positive. 787 With ARG, turn the mode on iff ARG is positive.
817 788
818 When this mode is active, `whitespace-buffer' is added to 789 When this mode is active, `whitespace-buffer' is added to
819 `find-file-hook' and `kill-buffer-hook'." 790 `find-file-hook' and `kill-buffer-hook'."
820 :global t :group 'whitespace 791 :global t
792 :group 'whitespace
821 (if whitespace-global-mode 793 (if whitespace-global-mode
822 (progn 794 (progn
823 (add-hook 'find-file-hook 'whitespace-buffer) 795 (add-hook 'find-file-hook 'whitespace-buffer)
824 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t) 796 (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
825 (add-hook 'kill-buffer-hook 'whitespace-buffer)) 797 (add-hook 'kill-buffer-hook 'whitespace-buffer))
844 (defun whitespace-unload-hook () 816 (defun whitespace-unload-hook ()
845 (remove-hook 'find-file-hook 'whitespace-buffer) 817 (remove-hook 'find-file-hook 'whitespace-buffer)
846 (remove-hook 'write-file-functions 'whitespace-write-file-hook t) 818 (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
847 (remove-hook 'kill-buffer-hook 'whitespace-buffer)) 819 (remove-hook 'kill-buffer-hook 'whitespace-buffer))
848 820
821 (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
822
849 (provide 'whitespace) 823 (provide 'whitespace)
824
825 ;; arch-tag: 4ff44e87-b63c-402d-95a6-15e51e58bd0c
850 ;;; whitespace.el ends here 826 ;;; whitespace.el ends here