-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisws.el
95 lines (80 loc) · 3.52 KB
/
visws.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
;;; visws.el --- Make whitespace visible -*- coding: latin-1 -*-
;;
;; Copyright (C) 2001 Free Software Foundation, Inc.
;;
;; Author: Miles Bader <[email protected]>
;; Keywords: convenience
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; Defines the mode `visible-whitespace-mode'.
;;
;; When active, normally invisible whitespace characters are made visible.
;;; Code:
(require 'latin1-disp)
(defface visible-whitespace '((t :foreground "blue" :bold t))
"Face for control-characters revealed by `visible-whitespace-mode'.")
(defcustom visible-whitespace-mappings
'((?\n [?$ ?\n])
;; Note that TAB still tabs, but with a graphic indicator before the
;; tab; we only use single-character graphic indicator to reduce the
;; number of cases where the indicator causes the tabbing to be
;; screwed up.
(?\t [?» ?\t] [?\\ ?\t])
(? [?·] [?.]))
"An alist of mappings for displaying whitespace in `visible-whitespace-mode'.
The car of each mapping is a whitespace character, and the cdr is a list of
display vectors (a vector of characters). The first display vector the
list that can be displayed is used; if no display vector for a mapping can
be displayed, then that character is displayed unmodified.
The characters in are displayed using the `visible-whitespace' face."
:type 'list)
(defun visws-legal-display-vector-p (vec)
"Return true if every character in the display vector VEC can be displayed."
(let ((i 0) (len (length vec)))
(while (and (< i len) (latin1-char-displayable-p (aref vec i)))
(setq i (1+ i)))
(= i len)))
;; Buffer local variable used to remember whether a buffer initially had
;; a local display table or not.
(defvar visws-display-table-was-local nil)
(define-minor-mode visible-whitespace-mode
"Toggle Visible Whitespace mode
When active, normally invisible whitespace characters are made visible.
With prefix argument ARG, turn on if positive, otherwise off.
Returns non-nil if the new state is enabled."
nil
" VisWS"
nil
(if visible-whitespace-mode
(let ((face-bits (ash (face-id 'visible-whitespace) 19)))
(set (make-local-variable 'visws-display-table-was-local)
buffer-display-table)
(unless buffer-display-table
(setq buffer-display-table (make-display-table)))
(dolist (entry visible-whitespace-mappings)
(let ((vecs (cdr entry)))
(while (and vecs (not (visws-legal-display-vector-p (car vecs))))
(setq vecs (cdr vecs)))
(when vecs
(let ((vec (copy-sequence (car vecs))))
(dotimes (i (length vec))
(when (not (eq (aref vec i) (car entry)))
(aset vec i (logior (aref vec i) face-bits))))
(aset buffer-display-table (car entry) vec))))))
(if visws-display-table-was-local
(dolist (entry visible-whitespace-mappings)
(aset buffer-display-table (car entry) nil))
(setq buffer-display-table nil))))
;;; visws.el ends here