27 |
|
|
28 |
;; Whitespace.el URL: http://www.dsmit.com/lisp/ |
;; Whitespace.el URL: http://www.dsmit.com/lisp/ |
29 |
|
|
30 |
|
;; The whitespace library is intended to find and help fix five different types |
31 |
|
;; of whitespace problems that commonly exist in source code. |
32 |
|
|
33 |
|
;; 1. Leading space (empty lines at the top 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 |
36 |
|
;; replaced with TABS). |
37 |
|
;; 4. Spaces followed by a TAB. (Almost always, we never want that). |
38 |
|
;; 5. Spaces or TABS at the end of a line. |
39 |
|
|
40 |
|
;; Whitespace errors are reported in a buffer, and on the modeline. |
41 |
|
|
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: |
44 |
|
|
45 |
|
;; e - End-of-Line whitespace. |
46 |
|
;; i - Indentation whitespace. |
47 |
|
;; l - Leading whitespace. |
48 |
|
;; s - Space followed by Tab. |
49 |
|
;; t - Trailing whitespace. |
50 |
|
|
51 |
|
;; If any of the whitespace checks is turned off, the modeline will display a |
52 |
|
;; !<y>. |
53 |
|
|
54 |
|
;; (since (3) is the most controversial one, here is the rationale: Most |
55 |
|
;; terminal drivers and printer drivers have TAB configured or even |
56 |
|
;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost |
57 |
|
;; always they default to 8.) |
58 |
|
|
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 |
61 |
|
;; even print it. |
62 |
|
|
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 |
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 |
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 |
69 |
|
;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them |
70 |
|
;; to set smarttab.) |
71 |
|
|
72 |
|
;; All the above have caused (and will cause) unwanted codeline integration and |
73 |
|
;; merge problems. |
74 |
|
|
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 |
77 |
|
;; whitespaces during the process of your editing). |
78 |
|
|
79 |
;; Exported functions: |
;; Exported functions: |
80 |
|
|
81 |
;; `whitespace-buffer' - To check the current buffer for whitespace problems. |
;; `whitespace-buffer' - To check the current buffer for whitespace problems. |
84 |
;; problems. |
;; problems. |
85 |
;; `whitespace-cleanup-region' - To cleanup all whitespaces between point |
;; `whitespace-cleanup-region' - To cleanup all whitespaces between point |
86 |
;; and mark in the current buffer. |
;; and mark in the current buffer. |
|
;; `whitespace-describe' - A simple introduction to the library. |
|
87 |
|
|
88 |
;;; Code: |
;;; Code: |
89 |
|
|
312 |
:group 'faces) |
:group 'faces) |
313 |
|
|
314 |
(defface whitespace-highlight-face '((((class color) (background light)) |
(defface whitespace-highlight-face '((((class color) (background light)) |
315 |
(:background "green")) |
(:background "green")) |
316 |
(((class color) (background dark)) |
(((class color) (background dark)) |
317 |
(:background "sea green")) |
(:background "sea green")) |
318 |
(((class grayscale monochrome) |
(((class grayscale mono) |
319 |
(background light)) |
(background light)) |
320 |
(:background "black")) |
(:background "black")) |
321 |
(((class grayscale monochrome) |
(((class grayscale mono) |
322 |
(background dark)) |
(background dark)) |
323 |
(:background "white"))) |
(:background "white"))) |
324 |
"Face used for highlighting the bogus whitespaces that exist in the buffer." |
"Face used for highlighting the bogus whitespaces that exist in the buffer." |
325 |
:group 'whitespace-faces) |
:group 'whitespace-faces) |
326 |
|
|
811 |
(setq whitespace-rescan-timer nil)))) |
(setq whitespace-rescan-timer nil)))) |
812 |
|
|
813 |
;;;###autoload |
;;;###autoload |
814 |
(defcustom whitespace-global-mode nil |
(define-minor-mode whitespace-global-mode |
|
"Toggle global Whitespace mode. |
|
|
|
|
|
Setting this variable directly does not take effect; |
|
|
use either \\[customize] or the function `whitespace-global-mode' |
|
|
\(which see)." |
|
|
:set (lambda (sym val) |
|
|
(whitespace-global-mode (or val 0))) |
|
|
:initialize 'custom-initialize-default |
|
|
:type 'boolean |
|
|
:group 'whitespace |
|
|
:require 'whitespace) |
|
|
|
|
|
;;;###autoload |
|
|
(defun whitespace-global-mode (&optional arg) |
|
815 |
"Toggle using Whitespace mode in new buffers. |
"Toggle using Whitespace mode in new buffers. |
816 |
With ARG, turn the mode on if and only iff ARG is positive. |
With ARG, turn the mode on if and only iff ARG is positive. |
817 |
|
|
818 |
When this mode is active, `whitespace-buffer' is added to |
When this mode is active, `whitespace-buffer' is added to |
819 |
`find-file-hooks' and `kill-buffer-hook'." |
`find-file-hook' and `kill-buffer-hook'." |
820 |
(interactive "P") |
:global t :group 'whitespace |
821 |
(setq arg (if arg |
(if whitespace-global-mode |
|
(> (prefix-numeric-value arg) 0) |
|
|
(not whitespace-global-mode))) |
|
|
(if arg |
|
822 |
(progn |
(progn |
823 |
(add-hook 'find-file-hooks 'whitespace-buffer) |
(add-hook 'find-file-hook 'whitespace-buffer) |
824 |
(add-hook 'local-write-file-hooks 'whitespace-write-file-hook) |
(add-hook 'write-file-functions 'whitespace-write-file-hook nil t) |
825 |
(add-hook 'kill-buffer-hook 'whitespace-buffer)) |
(add-hook 'kill-buffer-hook 'whitespace-buffer)) |
826 |
(remove-hook 'find-file-hooks 'whitespace-buffer) |
(remove-hook 'find-file-hook 'whitespace-buffer) |
827 |
(remove-hook 'local-write-file-hooks 'whitespace-write-file-hook) |
(remove-hook 'write-file-functions 'whitespace-write-file-hook t) |
828 |
(remove-hook 'kill-buffer-hook 'whitespace-buffer))) |
(remove-hook 'kill-buffer-hook 'whitespace-buffer))) |
829 |
|
|
830 |
;;;###autoload |
;;;###autoload |
831 |
(defun whitespace-write-file-hook () |
(defun whitespace-write-file-hook () |
832 |
"The local-write-file-hook to be called on the buffer when |
"Hook function to be called on the buffer when whitespace check is enabled. |
833 |
whitespace check is enabled." |
This is meant to be added buffer-locally to `write-file-functions'." |
834 |
(interactive) |
(interactive) |
835 |
(let ((werr nil)) |
(let ((werr nil)) |
836 |
(if whitespace-auto-cleanup |
(if whitespace-auto-cleanup |
841 |
buffer-file-name)))) |
buffer-file-name)))) |
842 |
nil) |
nil) |
843 |
|
|
|
;;;###autoload |
|
|
(defun whitespace-describe () |
|
|
"A summary of whitespaces and what this library can do about them. |
|
|
|
|
|
The whitespace library is intended to find and help fix five different types |
|
|
of whitespace problems that commonly exist in source code. |
|
|
|
|
|
1. Leading space (empty lines at the top of a file). |
|
|
2. Trailing space (empty lines at the end of a file). |
|
|
3. Indentation space (8 or more spaces at beginning of line, that should be |
|
|
replaced with TABS). |
|
|
4. Spaces followed by a TAB. (Almost always, we never want that). |
|
|
5. Spaces or TABS at the end of a line. |
|
|
|
|
|
Whitespace errors are reported in a buffer, and on the modeline. |
|
|
|
|
|
Modeline will show a W:<x>!<y> to denote a particular type of whitespace, |
|
|
where `x' and `y' can be one (or more) of: |
|
|
|
|
|
e - End-of-Line whitespace. |
|
|
i - Indentation whitespace. |
|
|
l - Leading whitespace. |
|
|
s - Space followed by Tab. |
|
|
t - Trailing whitespace. |
|
|
|
|
|
If any of the whitespace checks is turned off, the modeline will display a |
|
|
!<y>. |
|
|
|
|
|
(since (3) is the most controversial one, here is the rationale: Most |
|
|
terminal drivers and printer drivers have TAB configured or even |
|
|
hardcoded to be 8 spaces. (Some of them allow configuration, but almost |
|
|
always they default to 8.) |
|
|
|
|
|
Changing `tab-width' to other than 8 and editing will cause your code to |
|
|
look different from within Emacs, and say, if you cat it or more it, or |
|
|
even print it. |
|
|
|
|
|
Almost all the popular programming modes let you define an offset (like |
|
|
c-basic-offset or perl-indent-level) to configure the offset, so you |
|
|
should never have to set your `tab-width' to be other than 8 in all these |
|
|
modes. In fact, with an indent level of say, 4, 2 TABS will cause Emacs |
|
|
to replace your 8 spaces with one \t (try it). If vi users in your |
|
|
office complain, tell them to use vim, which distinguishes between |
|
|
tabstop and shiftwidth (vi equivalent of our offsets), and also ask them |
|
|
to set smarttab.) |
|
|
|
|
|
All the above have caused (and will cause) unwanted codeline integration and |
|
|
merge problems. |
|
|
|
|
|
whitespace.el will complain if it detects whitespaces on opening a file, and |
|
|
warn you on closing a file also (in case you had inserted any |
|
|
whitespaces during the process of your editing)." |
|
|
(interactive) |
|
|
(message "Use C-h f whitespace-describe to read about whitespace.el v%s." |
|
|
whitespace-version)) |
|
|
|
|
844 |
(defun whitespace-unload-hook () |
(defun whitespace-unload-hook () |
845 |
(remove-hook 'find-file-hooks 'whitespace-buffer) |
(remove-hook 'find-file-hook 'whitespace-buffer) |
846 |
(remove-hook 'local-write-file-hooks 'whitespace-write-file-hook) |
(remove-hook 'write-file-functions 'whitespace-write-file-hook t) |
847 |
(remove-hook 'kill-buffer-hook 'whitespace-buffer)) |
(remove-hook 'kill-buffer-hook 'whitespace-buffer)) |
848 |
|
|
849 |
(provide 'whitespace) |
(provide 'whitespace) |