-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-wspace.el
executable file
·257 lines (235 loc) · 9.83 KB
/
show-wspace.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
;;; show-wspace.el --- Highlight whitespace of various kinds.
;;
;; Filename: show-wspace.el
;; Description: Highlight whitespace of various kinds.
;; Author: Peter Steiner <[email protected]>, Drew Adams
;; Maintainer: Drew Adams
;; Copyright (C) 2000-2010, Drew Adams, all rights reserved.
;; Created: Wed Jun 21 08:54:53 2000
;; Version: 21.0
;; Last-Updated: Fri Jan 15 13:44:05 2010 (-0800)
;; By: dradams
;; Update #: 283
;; URL: http://www.emacswiki.org/cgi-bin/wiki/show-wspace.el
;; Keywords: highlight, whitespace
;; Compatibility: GNU Emacs: 20.x, 21.x, 22.x, 23.x
;;
;; Features that might be required by this library:
;;
;; None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Highlight whitespace of various kinds.
;;
;; To use this library:
;;
;; Add this to your initialization file (~/.emacs or ~/_emacs):
;;
;; (require 'show-wspace) ; Load this library.
;;
;; Then you can use commands `toggle-*' (see below) to turn the
;; various kinds of whitespace highlighting on and off in Font-Lock
;; mode.
;;
;; If you want to always use a particular kind of whitespace
;; highlighting, by default, then add the corresponding
;; `show-ws-highlight-*' function (see below) to the hook
;; `font-lock-mode-hook'. Then, whenever Font-Lock mode is turned on,
;; whitespace highlighting will also be turned on.
;;
;; For example, you can turn on tab highlighting by default by adding
;; function `show-ws-highlight-tabs' to `font-lock-mode-hook' in your
;; .emacs file, as follows:
;;
;; (add-hook 'font-lock-mode-hook 'show-ws-highlight-tabs)
;;
;;
;; Faces defined here:
;;
;; `show-ws-hard-space', `show-ws-tab', `show-ws-trailing-whitespace'.
;;
;; Commands defined here:
;;
;; `show-ws-toggle-show-hard-spaces', `show-ws-toggle-show-tabs',
;; `show-ws-toggle-show-trailing-whitespace',
;; `toggle-show-hard-spaces-show-ws' (alias),
;; `toggle-show-tabs-show-ws' (alias),
;; `toggle-show-trailing-whitespace-show-ws' (alias).
;;
;; Non-interactive functions defined here:
;;
;; `show-ws-dont-highlight-hard-spaces',
;; `show-ws-dont-highlight-tabs',
;; `show-ws-dont-highlight-trailing-whitespace',
;; `show-ws-highlight-hard-spaces', `show-ws-highlight-tabs',
;; `show-ws-highlight-trailing-whitespace'.
;;
;; Internal variables defined here:
;;
;; `show-ws-highlight-hard-spaces-p', `show-ws-highlight-tabs-p',
;; `show-ws-highlight-trailing-whitespace-p'.
;;
;; Drew Adams wrote the `toggle-*' commands and `*-p' variables.
;;
;; Peter Steiner wrote the original code that did the equivalent of
;; the `show-ws-highlight-*' commands here in his `hilite-trail.el'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;; 2009/06/25 dadams
;; show-ws-dont-*: Should be no-op's for Emacs 20, 21.
;; 2009/06/17 dadams
;; Added: show-ws-dont-highlight-*.
;; show-ws-toggle-show-*: Remove the font-lock keywords. Needed for Emacs 22+.
;; 2007/09/25 dadams
;; Renamed to use prefix show-ws-. Thx to Cyril Brulebois.
;; 2006/11/11 dadams
;; Corrected doc strings. Clarified: hard space is non-breaking space, \240.
;; Included hard space in highlight-trailing-whitespace.
;; 2006/04/06 dadams
;; highlight-*: Use font-lock-add-keywords. Thanks to Karl Chen.
;; 2006/02/20 dadams
;; Mentioned in Commentary how to use non-interactively.
;; 2006/01/07 dadams
;; Added :link for sending bug report.
;; 2006/01/06 dadams
;; Added defgroup and use it.
;; 2005/12/30 dadams
;; Removed require of def-face-const.el.
;; Renamed faces, without "-face".
;; 2005/01/25 dadams
;; Removed ###autoload for defvars.
;; 2004/06/10 dadams
;; Fixed minor bug in highlight-* functions.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(and (< emacs-major-version 20) (eval-when-compile (require 'cl))) ;; when, push
;;;;;;;;;;;;;;;;;;;;;;;;;
(defgroup Show-Whitespace nil
"Highlight whitespace of various kinds."
:prefix "show-ws-"
:group 'convenience :group 'matching
:link `(url-link :tag "Send Bug Report"
,(concat "mailto:" "drew.adams" "@" "oracle" ".com?subject=\
show-wspace.el bug: \
&body=Describe bug here, starting with `emacs -q'. \
Don't forget to mention your Emacs and library versions."))
:link '(url-link :tag "Other Libraries by Drew"
"http://www.emacswiki.org/cgi-bin/wiki/DrewsElispLibraries")
:link '(url-link :tag "Download"
"http://www.emacswiki.org/cgi-bin/wiki/show-wspace.el")
:link '(url-link :tag "Description"
"http://www.emacswiki.org/cgi-bin/wiki/ShowWhiteSpace#ShowWspace")
:link '(emacs-commentary-link :tag "Commentary" "show-wspace")
)
(defface show-ws-tab '((t (:background "LemonChiffon")))
"*Face for highlighting tab characters (`C-i') in Font-Lock mode."
:group 'Show-Whitespace :group 'font-lock :group 'faces)
(defface show-ws-trailing-whitespace '((t (:background "Gold")))
"*Face for highlighting whitespace at line ends in Font-Lock mode."
:group 'Show-Whitespace :group 'font-lock :group 'faces)
(defface show-ws-hard-space '((t (:background "PaleGreen")))
"*Face for highlighting non-breaking spaces (`\240')in Font-Lock mode."
:group 'Show-Whitespace :group 'font-lock :group 'faces)
(defvar show-ws-highlight-tabs-p nil
"Non-nil means font-lock mode highlights TAB characters (`C-i').")
(defvar show-ws-highlight-trailing-whitespace-p nil
"Non-nil means font-lock mode highlights whitespace at line ends.")
(defvar show-ws-highlight-hard-spaces-p nil
"Non-nil means font-lock mode highlights non-breaking spaces (`\240').")
;;;###autoload
(defalias 'toggle-show-tabs-show-ws 'show-ws-toggle-show-tabs)
;;;###autoload
(defun show-ws-toggle-show-tabs ()
"Toggle highlighting of TABs, using face `show-ws-tab'."
(interactive)
(setq show-ws-highlight-tabs-p (not show-ws-highlight-tabs-p))
(if show-ws-highlight-tabs-p
(add-hook 'font-lock-mode-hook 'show-ws-highlight-tabs)
(remove-hook 'font-lock-mode-hook 'show-ws-highlight-tabs)
(show-ws-dont-highlight-tabs))
(font-lock-mode) (font-lock-mode)
(message "TAB highlighting is now %s." (if show-ws-highlight-tabs-p "ON" "OFF")))
;;;###autoload
(defalias 'toggle-show-hard-spaces-show-ws 'show-ws-toggle-show-hard-spaces)
;;;###autoload
(defun show-ws-toggle-show-hard-spaces ()
"Toggle highlighting of non-breaking space characters (`\240').
Uses face `show-ws-hard-space'."
(interactive)
(setq show-ws-highlight-hard-spaces-p (not show-ws-highlight-hard-spaces-p))
(if show-ws-highlight-hard-spaces-p
(add-hook 'font-lock-mode-hook 'show-ws-highlight-hard-spaces)
(remove-hook 'font-lock-mode-hook 'show-ws-highlight-hard-spaces)
(show-ws-dont-highlight-hard-spaces))
(font-lock-mode) (font-lock-mode)
(message "Hard (non-breaking) space highlighting is now %s."
(if show-ws-highlight-hard-spaces-p "ON" "OFF")))
;;;###autoload
(defalias 'toggle-show-trailing-whitespace-show-ws
'show-ws-toggle-show-trailing-whitespace)
;;;###autoload
(defun show-ws-toggle-show-trailing-whitespace ()
"Toggle highlighting of trailing whitespace.
Uses face `show-ws-trailing-whitespace'."
(interactive)
(setq show-ws-highlight-trailing-whitespace-p
(not show-ws-highlight-trailing-whitespace-p))
(if show-ws-highlight-trailing-whitespace-p
(add-hook 'font-lock-mode-hook 'show-ws-highlight-trailing-whitespace)
(remove-hook 'font-lock-mode-hook 'show-ws-highlight-trailing-whitespace)
(show-ws-dont-highlight-trailing-whitespace))
(font-lock-mode) (font-lock-mode)
(message "Trailing whitespace highlighting is now %s."
(if show-ws-highlight-trailing-whitespace-p "ON" "OFF")))
(defun show-ws-highlight-tabs ()
"Highlight tab characters (`C-i')."
(font-lock-add-keywords nil '(("[\t]+" (0 'show-ws-tab t)))))
(defun show-ws-highlight-hard-spaces ()
"Highlight hard (non-breaking) space characters (`\240')."
(font-lock-add-keywords nil '(("[\240]+" (0 'show-ws-hard-space t)))))
(defun show-ws-highlight-trailing-whitespace ()
"Highlight whitespace characters at line ends."
(font-lock-add-keywords
nil '(("[\240\040\t]+$" (0 'show-ws-trailing-whitespace t)))))
;; These are no-ops for Emacs 20, 21:
;; `font-lock-remove-keywords' is not defined, and we don't need to use it.
(defun show-ws-dont-highlight-tabs ()
"Don't highlight tab characters (`C-i')."
(when (fboundp 'font-lock-remove-keywords)
(font-lock-remove-keywords nil '(("[\t]+" (0 'show-ws-tab t))))))
(defun show-ws-dont-highlight-hard-spaces ()
"Don't highlight hard (non-breaking) space characters (`\240')."
(when (fboundp 'font-lock-remove-keywords)
(font-lock-remove-keywords nil '(("[\240]+" (0 'show-ws-hard-space t))))))
(defun show-ws-dont-highlight-trailing-whitespace ()
"Don't highlight whitespace characters at line ends."
(when (fboundp 'font-lock-remove-keywords)
(font-lock-remove-keywords
nil '(("[\240\040\t]+$" (0 'show-ws-trailing-whitespace t))))))
;;;;;;;;;;;;;;;;;;;;;;;
(provide 'show-wspace)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; show-wspace.el ends here