aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1999-09-10 02:42:59 +0000
committerRichard M. Stallman1999-09-10 02:42:59 +0000
commit24b72a45b79fdeec02e68ceb19efc21c741317f6 (patch)
tree779be66e6d18f0977b487bafa06b8f91165f83b7
parent774b98f30d8d21ddd939e0afa1b2417c96ac3c6d (diff)
downloademacs-24b72a45b79fdeec02e68ceb19efc21c741317f6.tar.gz
emacs-24b72a45b79fdeec02e68ceb19efc21c741317f6.zip
Finish making it fit Emacs conventions. Add autoloads.
-rw-r--r--lisp/whitespace.el233
1 files changed, 146 insertions, 87 deletions
diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index 8b67cfd7dbb..4d417983e4d 100644
--- a/lisp/whitespace.el
+++ b/lisp/whitespace.el
@@ -30,6 +30,11 @@
30 30
31;; `whitespace-buffer' - To check the current buffer for whitespace problems. 31;; `whitespace-buffer' - To check the current buffer for whitespace problems.
32;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer. 32;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
33;; `whitespace-region' - To check between point and mark for whitespace
34;; problems.
35;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
36;; and mark in the current buffer.
37;; `whitespace-describe' - A simple introduction to the library.
33 38
34;;; Code: 39;;; Code:
35 40
@@ -37,73 +42,72 @@
37(add-hook 'find-file-hooks 'whitespace-buffer) 42(add-hook 'find-file-hooks 'whitespace-buffer)
38(add-hook 'kill-buffer-hook 'whitespace-buffer) 43(add-hook 'kill-buffer-hook 'whitespace-buffer)
39 44
40(defvar whitespace-version "1.9" "Version of the whitespace library.") 45(defvar whitespace-version "2.3" "Version of the whitespace library.")
41(defvar whitespace-indent-regexp (concat "^\\( *\\) " " ") 46;; Find out what type of Emacs we are running in.
42 "Any 8 or more spaces that can be replaced with a TAB")
43(defvar whitespace-spacetab-regexp " \t" "A TAB followed by a space")
44(defvar whitespace-ateol-regexp "[ \t]$" "A TAB or a space at the EOL")
45(defvar whitespace-errbuf "*Whitespace Errors*"
46 "The buffer where the errors will appear")
47
48;; Find out what type of emacs we are running in.
49(defvar whitespace-running-emacs (if (string-match "XEmacs\\|Lucid" 47(defvar whitespace-running-emacs (if (string-match "XEmacs\\|Lucid"
50 emacs-version) nil t) 48 emacs-version) nil t)
51 "If the current Emacs is not XEmacs, then, this is t.") 49 "If this is Emacs, not XEmacs, this is t.")
52
53;; For users of Emacs 19.x, defgroup and defcustom are not defined.
54 50
55(if (< (string-to-int emacs-version) 20) 51(if whitespace-running-emacs (require 'timer))
56 (progn
57 (defmacro defgroup (sym memb doc &rest args)
58 t)
59 (defmacro defcustom (sym val doc &rest args)
60 `(defvar ,sym ,val ,doc))))
61 52
62(defgroup whitespace nil 53(defvar whitespace-all-buffer-files nil
63 "Check for five different types of whitespaces in source code. 54 "An associated list of buffers and files checked for whitespace cleanliness.
64 55
651. Leading space \(empty lines at the top of a file\). 56This is to enable periodic checking of whitespace cleanliness in the files
662. Trailing space \(empty lines at the end of a file\). 57visited by the buffers.")
673. Indentation space \(8 or more spaces at beginning of line, that should be
68 replaced with TABS\).
694. Spaces followed by a TAB. \(Almost always, we never want that\).
705. Spaces or TABS at the end of a line.
71 58
72Whitespace errors are reported in a buffer, and on the modeline. 59(defvar whitespace-rescan-timer nil
60 "Timer object used to rescan the files in buffers that have been modified.")
73 61
74Modeline will show a W:<x> to denote a particular type of whitespace, where 62;; For users of Emacs 19.x, defgroup and defcustom are not defined.
75`x' can be one \(or more\) of:
76 63
77e - End-of-Line whitespace. 64(eval-when-compile
78i - Indentation whitespace. 65 (if (< (string-to-int emacs-version) 20)
79l - Leading whitespace. 66 (progn
80s - Space followed by Tab. 67 (defmacro defgroup (sym memb doc &rest args)
81t - Trailing whitespace. 68 "Null macro for defgroup in all versions of Emacs < 20.x"
69 t)
70 (defmacro defcustom (sym val doc &rest args)
71 "Macro to alias defcustom to defvar in all versions of Emacs < 20.x"
72 `(defvar ,sym ,val ,doc)))))
82 73
83" 74(defgroup whitespace nil
75 "Check for and fix five different types of whitespaces in source code."
84 ;; Since XEmacs doesn't have a 'convenience group, use the next best group 76 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
85 ;; which is 'editing? 77 ;; which is 'editing?
86 :group (if whitespace-running-emacs 'convenience 'editing)) 78 :group (if whitespace-running-emacs 'convenience 'editing))
87 79
88(defcustom whitespace-auto-cleanup nil 80(defcustom whitespace-spacetab-regexp " \t"
89 "Setting this will cleanup a buffer automatically on finding it whitespace 81 "Regexp to match a TAB followed by a space."
90unclean. 82 :type 'string
83 :group 'whitespace)
91 84
92Use the emacs `customize' command to set this. 85(defcustom whitespace-indent-regexp (concat "^\\(\t*\\) " " ")
93" 86 "Regexp to match (any TABS followed by) 8/more whitespaces at start of line."
87 :type 'string
88 :group 'whitespace)
89
90(defcustom whitespace-ateol-regexp "[ \t]$"
91 "Regexp to match a TAB or a space at the EOL."
92 :type 'string
93 :group 'whitespace)
94
95(defcustom whitespace-errbuf "*Whitespace Errors*"
96 "The buffer where whitespace related messages will be logged."
97 :type 'string
98 :group 'whitespace)
99
100(defcustom whitespace-auto-cleanup nil
101 "Cleanup a buffer automatically on finding it whitespace unclean."
94 :type 'boolean 102 :type 'boolean
95 :group 'whitespace) 103 :group 'whitespace)
96 104
97(defcustom whitespace-silent nil 105(defcustom whitespace-silent nil
98 "Setting this to t will cause the whitespace error buffer not to pop 106 "All whitespace errors will be shown only in the modeline when t.
99up. All whitespace errors will be shown only in the modeline.
100 107
101Note that setting this may cause all whitespaces introduced in a file to go 108Note that setting this may cause all whitespaces introduced in a file to go
102unnoticed when the buffer is killed, unless the user visits the `*Whitespace 109unnoticed when the buffer is killed, unless the user visits the `*Whitespace
103Errors*' buffer before opening \(or closing\) another file. 110Errors*' buffer before opening (or closing) another file."
104
105Use the emacs `customize' command to set this.
106"
107 :type 'boolean 111 :type 'boolean
108 :group 'whitespace) 112 :group 'whitespace)
109 113
@@ -121,37 +125,27 @@ Use the emacs `customize' command to set this.
121 simula-mode tcl-mode tex-mode 125 simula-mode tcl-mode tex-mode
122 texinfo-mode vrml-mode xml-mode) 126 texinfo-mode vrml-mode xml-mode)
123 127
124 "Modes that we check whitespace in. These are mostly programming and 128 "Major Modes in which we turn on whitespace checking.
125documentation modes. But you may add other modes that you want whitespaces
126checked in by adding something like the following to your `.emacs':
127
128\(setq whitespace-modes \(cons 'my-mode \(cons 'my-other-mode
129 whitespace-modes\)\)
130
131Or, alternately, you can use the Emacs `customize' command to set this.
132"
133 :group 'whitespace)
134 129
135(if whitespace-running-emacs (require 'timer)) 130These are mostly programming and documentation modes. But you may add other
131modes that you want whitespaces checked in by adding something like the
132following to your `.emacs':
136 133
137(defvar whitespace-all-buffer-files nil 134\(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
138 "An associated list of all buffers 135 whitespace-modes))\)
139and theirs files checked for whitespace cleanliness. This is to enable
140periodic checking of whitespace cleanliness in the files visited by the
141buffers.")
142 136
143(defvar whitespace-rescan-timer nil 137Or, alternately, you can use the Emacs `customize' command to set this."
144 "Timer object that will be set to 138 :group 'whitespace)
145rescan the files in Emacs buffers that have been modified.")
146 139
147(defcustom whitespace-rescan-timer-time 60 140(defcustom whitespace-rescan-timer-time 60
148 "seconds after which 141 "Period in seconds to rescan modified buffers for whitespace creep.
149`whitespace-rescan-files-in-buffers' will check for modified files in Emacs 142
150buffers." 143This is the period after which the timer will fire causing
144`whitespace-rescan-files-in-buffers' to check for whitespace creep in
145modified buffers."
151 :type 'integer 146 :type 'integer
152 :group 'whitespace) 147 :group 'whitespace)
153 148
154
155;; Tell Emacs about this new kind of minor mode 149;; Tell Emacs about this new kind of minor mode
156(make-variable-buffer-local 'whitespace-mode) 150(make-variable-buffer-local 'whitespace-mode)
157(put 'whitespace-mode 'permanent-local nil) 151(put 'whitespace-mode 'permanent-local nil)
@@ -166,12 +160,14 @@ buffers."
166 minor-mode-alist))) 160 minor-mode-alist)))
167 161
168(defun whitespace-check-whitespace-mode (&optional arg) 162(defun whitespace-check-whitespace-mode (&optional arg)
163 "Test and set the whitespace-mode in qualifying buffers."
169 (if (null whitespace-mode) 164 (if (null whitespace-mode)
170 (setq whitespace-mode 165 (setq whitespace-mode
171 (if (or arg (member major-mode whitespace-modes)) 166 (if (or arg (member major-mode whitespace-modes))
172 t 167 t
173 nil)))) 168 nil))))
174 169
170;;;###autoload
175(defun whitespace-buffer (&optional quiet) 171(defun whitespace-buffer (&optional quiet)
176 "Find five different types of white spaces in buffer: 172 "Find five different types of white spaces in buffer:
177 173
@@ -184,8 +180,7 @@ buffers."
184Check for whitespace only if this buffer really contains a non-empty file 180Check for whitespace only if this buffer really contains a non-empty file
185and: 181and:
1861. the major mode is one of the whitespace-modes, or 1821. the major mode is one of the whitespace-modes, or
1872. `whitespace-buffer' was explicitly called with a prefix argument. 1832. `whitespace-buffer' was explicitly called with a prefix argument."
188"
189 (interactive) 184 (interactive)
190 (whitespace-check-whitespace-mode current-prefix-arg) 185 (whitespace-check-whitespace-mode current-prefix-arg)
191 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode) 186 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
@@ -249,7 +244,7 @@ and:
249 (if whitespace-errmsg 244 (if whitespace-errmsg
250 (progn 245 (progn
251 (insert whitespace-errmsg) 246 (insert whitespace-errmsg)
252 (if (not (and quiet whitespace-silent)) 247 (if (not (or quiet whitespace-silent))
253 (display-buffer whitespace-errbuf t)) 248 (display-buffer whitespace-errbuf t))
254 (if (not quiet) 249 (if (not quiet)
255 (message "Whitespaces: [%s] in %s" 250 (message "Whitespaces: [%s] in %s"
@@ -258,17 +253,21 @@ and:
258 (if (not quiet) 253 (if (not quiet)
259 (message "%s clean" whitespace-filename))))))))) 254 (message "%s clean" whitespace-filename)))))))))
260 255
256;;;###autoload
261(defun whitespace-region (s e) 257(defun whitespace-region (s e)
262 "To check a region specified by point and mark for whitespace errors." 258 "Check a region specified by point and mark for whitespace errors."
263 (interactive "r") 259 (interactive "r")
264 (save-excursion 260 (save-excursion
265 (save-restriction 261 (save-restriction
266 (narrow-to-region s e) 262 (narrow-to-region s e)
267 (whitespace-buffer)))) 263 (whitespace-buffer))))
268 264
265;;;###autoload
269(defun whitespace-cleanup () 266(defun whitespace-cleanup ()
270 "To cleanup the five different kinds of whitespace problems that 267 "Cleanup the five different kinds of whitespace problems.
271are defined in \\[whitespace-buffer]" 268
269Use \\[describe-function] whitespace-describe to read a summary of the
270whitespace problems."
272 (interactive) 271 (interactive)
273 ;; If this buffer really contains a file, then run, else quit. 272 ;; If this buffer really contains a file, then run, else quit.
274 (whitespace-check-whitespace-mode current-prefix-arg) 273 (whitespace-check-whitespace-mode current-prefix-arg)
@@ -314,8 +313,9 @@ are defined in \\[whitespace-buffer]"
314 (whitespace-force-mode-line-update))) 313 (whitespace-force-mode-line-update)))
315 (setq tab-width whitespace-tabwith-saved)))) 314 (setq tab-width whitespace-tabwith-saved))))
316 315
316;;;###autoload
317(defun whitespace-cleanup-region (s e) 317(defun whitespace-cleanup-region (s e)
318 "To do a whitespace cleanup on a region specified by point and mark." 318 "Whitespace cleanup on a region specified by point and mark."
319 (interactive "r") 319 (interactive "r")
320 (save-excursion 320 (save-excursion
321 (save-restriction 321 (save-restriction
@@ -338,7 +338,7 @@ are defined in \\[whitespace-buffer]"
338 nil)))) 338 nil))))
339 339
340(defun whitespace-buffer-leading-cleanup () 340(defun whitespace-buffer-leading-cleanup ()
341 "To remove any empty lines at the top of the file." 341 "Remove any empty lines at the top of the file."
342 (save-excursion 342 (save-excursion
343 (let ((pmin nil) 343 (let ((pmin nil)
344 (pmax nil)) 344 (pmax nil))
@@ -418,9 +418,7 @@ are defined in \\[whitespace-buffer]"
418 (replace-match newregexp)))) 418 (replace-match newregexp))))
419 419
420(defun whitespace-indent-cleanup () 420(defun whitespace-indent-cleanup ()
421 "Search for any 8 or more whitespaces at the beginning of a line and 421 "Search for 8/more spaces at the start of a line and replace it with tabs."
422replace it with tabs."
423 (interactive)
424 (save-excursion 422 (save-excursion
425 (goto-char (point-min)) 423 (goto-char (point-min))
426 (while (re-search-forward whitespace-indent-regexp nil t) 424 (while (re-search-forward whitespace-indent-regexp nil t)
@@ -431,17 +429,26 @@ replace it with tabs."
431 429
432;; Force mode line updation for different Emacs versions 430;; Force mode line updation for different Emacs versions
433(defun whitespace-force-mode-line-update () 431(defun whitespace-force-mode-line-update ()
434 "To Force the mode line update for different flavors of Emacs." 432 "Force the mode line update for different flavors of Emacs."
435 (if whitespace-running-emacs 433 (if whitespace-running-emacs
436 (force-mode-line-update) ; Emacs 434 (force-mode-line-update) ; Emacs
437 (redraw-modeline))) ; XEmacs 435 (redraw-modeline))) ; XEmacs
438 436
439(defun whitespace-check-buffer-list (buf-name buf-file) 437(defun whitespace-check-buffer-list (buf-name buf-file)
438 "Add a buffer and its file to the whitespace monitor list.
439
440The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
441periodically for whitespace."
440 (if (and whitespace-mode (not (member (list buf-file buf-name) 442 (if (and whitespace-mode (not (member (list buf-file buf-name)
441 whitespace-all-buffer-files))) 443 whitespace-all-buffer-files)))
442 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name)))) 444 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
443 445
444(defun whitespace-tickle-timer () 446(defun whitespace-tickle-timer ()
447 "Tickle timer to periodically to scan qualifying files for whitespace creep.
448
449If timer is not set, then set it to scan the files in
450`whitespace-all-buffer-files' periodically (defined by
451`whitespace-rescan-timer-time') for whitespace creep."
445 (if (not whitespace-rescan-timer) 452 (if (not whitespace-rescan-timer)
446 (setq whitespace-rescan-timer 453 (setq whitespace-rescan-timer
447 (if whitespace-running-emacs 454 (if whitespace-running-emacs
@@ -452,10 +459,9 @@ replace it with tabs."
452 whitespace-rescan-timer-time))))) 459 whitespace-rescan-timer-time)))))
453 460
454(defun whitespace-rescan-files-in-buffers (&optional arg) 461(defun whitespace-rescan-files-in-buffers (&optional arg)
455 "Check to see if all the files that are whitespace clean are 462 "Check monitored files for whitespace creep since last scan."
456actually clean still, if in buffers, or need rescaning."
457 (let ((whitespace-all-my-files whitespace-all-buffer-files) 463 (let ((whitespace-all-my-files whitespace-all-buffer-files)
458 buffile bufname thiselt buf) 464 buffile bufname thiselt buf)
459 (if (not whitespace-all-my-files) 465 (if (not whitespace-all-my-files)
460 (progn 466 (progn
461 (if whitespace-running-emacs 467 (if whitespace-running-emacs
@@ -488,7 +494,7 @@ actually clean still, if in buffers, or need rescaning."
488 (whitespace-refresh-rescan-list buffile bufname)))))) 494 (whitespace-refresh-rescan-list buffile bufname))))))
489 495
490(defun whitespace-refresh-rescan-list (buffile bufname) 496(defun whitespace-refresh-rescan-list (buffile bufname)
491 "Refresh the list of files to be rescaned." 497 "Refresh the list of files to be rescaned for whitespace creep."
492 (if whitespace-all-buffer-files 498 (if whitespace-all-buffer-files
493 (progn 499 (progn
494 (setq whitespace-all-buffer-files 500 (setq whitespace-all-buffer-files
@@ -501,6 +507,59 @@ actually clean still, if in buffers, or need rescaning."
501 (if whitespace-rescan-timer 507 (if whitespace-rescan-timer
502 (setq whitespace-rescan-timer nil))))) 508 (setq whitespace-rescan-timer nil)))))
503 509
510;;;###autoload
511(defun whitespace-describe ()
512 "A summary of whitespaces and what this library can do about them.
513
514The whitespace library is intended to find and help fix five different types
515of whitespace problems that commonly exist in source code.
516
5171. Leading space (empty lines at the top of a file).
5182. Trailing space (empty lines at the end of a file).
5193. Indentation space (8 or more spaces at beginning of line, that should be
520 replaced with TABS).
5214. Spaces followed by a TAB. (Almost always, we never want that).
5225. Spaces or TABS at the end of a line.
523
524Whitespace errors are reported in a buffer, and on the modeline.
525
526Modeline will show a W:<x> to denote a particular type of whitespace, where
527`x' can be one (or more) of:
528
529e - End-of-Line whitespace.
530i - Indentation whitespace.
531l - Leading whitespace.
532s - Space followed by Tab.
533t - Trailing whitespace.
534
535 (since (3) is the most controversial one, here is the rationale: Most
536 terminal drivers and printer drivers have TAB configured or even
537 hardcoded to be 8 spaces. (Some of them allow configuration, but almost
538 always they default to 8.)
539
540 Changing tab-width to other than 8 and editing will cause your code to
541 look different from within Emacs, and say, if you cat it or more it, or
542 even print it.
543
544 Almost all the popular programming modes let you define an offset (like
545 c-basic-offset or perl-indent-level) to configure the offset, so you
546 should never have to set your tab-width to be other than 8 in all these
547 modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs
548 to replace your 8 spaces with one \t (try it). If vi users in your
549 office complain, tell them to use vim, which distinguishes between
550 tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
551 to set smarttab.)
552
553All the above have caused (and will cause) unwanted codeline integration and
554merge problems.
555
556whitespace.el will complain if it detects whitespaces on opening a file, and
557warn you on closing a file also. (if in case you had inserted any
558whitespaces during the process of your editing.)"
559 (interactive)
560 (message "Use C-h f whitespace-describe to read about whitespace.el v%s."
561 whitespace-version))
562
504(provide 'whitespace) 563(provide 'whitespace)
505 564
506;;; whitespace.el ends here 565;;; whitespace.el ends here