-
Notifications
You must be signed in to change notification settings - Fork 1
/
original-pretty-mode.el
271 lines (233 loc) · 8.83 KB
/
original-pretty-mode.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
;;; pretty-mode.el --- redisplay parts of the buffer as pretty symbols
;;; -*- coding: utf-8 -*-
;;; Commentary:
;;
;; Minor mode for redisplaying parts of the buffer as pretty symbols
;; originally modified from Trent Buck's version at http://paste.lisp.org/display/42335,2/raw
;; Also includes code from `sml-mode'
;; See also http://www.emacswiki.org/cgi-bin/wiki/PrettyLambda
;;
;; Released under the GPL. No implied warranties, etc. Use at your own risk.
;; Arthur Danskin <[email protected]>, March 2008
;;
;; to install:
;; (require 'pretty-mode)
;; and
;; (global-pretty-mode 1)
;; or
;; (add-hook 'my-pretty-language-hook 'turn-on-pretty-mode)
;;; Code:
(require 'cl)
(defvar pretty-syntax-types '(?_ ?w))
;; modified from `sml-mode'
(defun pretty-font-lock-compose-symbol (alist)
"Compose a sequence of ascii chars into a symbol."
(let* ((start (match-beginning 0))
(end (match-end 0))
(syntax (char-syntax (char-after start))))
(if (or (if (memq syntax pretty-syntax-types)
(or (memq (char-syntax (char-before start)) pretty-syntax-types)
(memq (char-syntax (char-after end)) pretty-syntax-types))
(memq (char-syntax (char-before start)) '(?. ?\\)))
(memq (get-text-property start 'face)
'(font-lock-doc-face font-lock-string-face
font-lock-comment-face)))
(remove-text-properties start end '(composition))
(compose-region start end (cdr (assoc (match-string 0) alist)))
;;; (add-text-properties start end `(display ,repl)))
))
;; Return nil because we're not adding any face property.
nil)
(defvar pretty-interaction-mode-alist
'((inferior-scheme-mode . scheme-mode)
(lisp-interaction-mode . emacs-lisp-mode)
(inferior-lisp-mode . lisp-mode)
(inferior-ess-mode . ess-mode)
(inf-haskell-mode . haskell-mode)
(tuareg-interactive-mode . tuareg-mode)
(inferior-python-mode . python-mode)
(inferior-octave-mode . octave-mode)
(inferior-ruby-mode . ruby-mode))
"Alist mapping from inferior process interaction modes to their
corresponding script editing modes.")
(defun pretty-font-lock-keywords (alist)
"Return a `font-lock-keywords' style entry for replacing
regular expressions with symbols. ALIST has the form ((STRING .
REPLACE-CHAR) ...)."
(when alist
`((,(regexp-opt (mapcar 'car alist))
(0 (pretty-font-lock-compose-symbol
',alist))))))
(defun pretty-keywords (&optional mode)
"Return the font-lock keywords for MODE, or the current mode if
MODE is nil. Return nil if there are no keywords."
(let* ((mode (or mode major-mode))
(kwds (cdr-safe
(or (assoc mode pretty-patterns)
(assoc (cdr-safe
(assoc mode pretty-interaction-mode-alist))
pretty-patterns)))))
(pretty-font-lock-keywords kwds)))
(defgroup pretty nil "Minor mode for replacing text with symbols "
:group 'faces)
(define-minor-mode pretty-mode
"Toggle Pretty minor mode.
With arg, turn Pretty minor mode on if arg is positive, off otherwise.
Pretty mode builds on `font-lock-mode'. Instead of highlighting
keywords, it replaces them with symbols. For example, lambda is
displayed as λ in lisp modes."
:group 'pretty
; :lighter " λ"
(if pretty-mode
(progn
(font-lock-add-keywords nil (pretty-keywords) t)
(font-lock-fontify-buffer))
(font-lock-remove-keywords nil (pretty-keywords))
(remove-text-properties (point-min) (point-max) '(composition nil))))
(defun turn-on-pretty-if-desired ()
"Turn on `pretty-mode' if the current major mode supports it."
(if (pretty-keywords)
(pretty-mode 1)))
(define-globalized-minor-mode global-pretty-mode
pretty-mode turn-on-pretty-if-desired
:init-value t)
(defun turn-off-pretty-mode ()
(interactive)
(pretty-mode -1))
(defun turn-on-pretty-mode ()
(interactive)
(pretty-mode +1))
(defun pretty-compile-patterns (patterns)
"Set pretty patterns in a convenient way.
PATTERNS should be of the form ((GLYPH (REGEXP MODE ...) ...)
...). GLYPH should be a character. MODE should be the name of a
major mode without the \"-mode\". Returns patterns in the form
expected by `pretty-patterns'"
(let ((pretty-patterns))
(loop for (glyph . pairs) in patterns do
(loop for (regexp . major-modes) in pairs do
(loop for mode in major-modes do
(let* ((mode (intern (concat (symbol-name mode)
"-mode")))
(assoc-pair (assoc mode pretty-patterns))
(entry (cons regexp glyph)))
(if assoc-pair
(push entry (cdr assoc-pair))
(push (cons mode (list entry))
pretty-patterns))))))
pretty-patterns))
(defvar pretty-patterns
(let* ((lispy '(scheme emacs-lisp lisp))
(mley '(tuareg haskell sml))
(c-like '(c c++ perl sh python java ess ruby))
(all (append lispy mley c-like (list 'octave))))
(pretty-compile-patterns
`(
(?≠ ("!=" ,@c-like scheme octave)
("<>" tuareg octave)
("~=" octave)
("/=" haskell emacs-lisp))
(?≤ ("<=" ,@all))
(?≥ (">=" ,@all))
(?← ("<-" ,@mley ess))
(?➛ ("->" ,@mley ess c c++ perl))
(?↑ ("\\^" tuareg))
(?⟹ ("=>" sml perl ruby haskell))
(?∅ ("nil" emacs-lisp ruby)
("null" scheme java)
("NULL" c c++)
;;; ("None" python)
("()" ,@mley))
;;; (?… ("\\.\\.\\." scheme))
;;; (?∀ ("List.for_all" tuareg))
;;; (?∃ ("List.exists" tuareg))
;;; (?∈ ("List.mem" tuareg)
;;; ("member" ,@lispy))
;;; (?∉ ())
(?√ ("sqrt" ,@all))
(?∑ ("sum" python))
(?α ("alpha" ,@all)
("'a" ,@mley))
(?β ("beta" ,@all)
("'b" ,@mley))
(?γ ("gamma" ,@all)
("'c" ,@mley))
(?Δ ("delta" ,@all)
("'d" ,@mley))
(?ε ("epsilon" ,@all))
(?θ ("theta" ,@all))
(?λ ("lambda" ,@all)
;;; ("case-\\(lambda\\)" scheme)
("fn" sml)
("fun" tuareg)
("\\" haskell))
(?π ("pi" ,@all)
("M_PI" c c++))
(?φ ("psi" ,@all))
(?² ("**2" python tuareg octave)
("^2" octave haskell))
(?³ ("**3" python tuareg octave)
("^3" octave haskell))
(?ⁿ ("**n" python tuareg octave)
("^n" octave haskell))
;; (?₀ ("[0]" ,@c-like)
;; ("(0)" octave)
;; (".(0)" tuareg))
;; (?₁ ("[1]" ,@c-like)
;; ("(1)" octave)
;; (".(1)" tuareg))
;; (?₂ ("[2]" ,@c-like)
;; ("(2)" octave)
;; (".(2)" tuareg))
;; (?₃ ("[3]" ,@c-like)
;; ("(3)" octave)
;; (".(3)" tuareg))
;; (?₄ ("[4]" ,@c-like)
;; ("(4)" octave)
;; (".(4)" tuareg))
(?∞ ("HUGE_VAL" c c++))
;;; (?∙ ())
;;; (?× ())
;;; (?ₐ ("[a]" ,@c-like)
;;; ("(a)" octave))
;;; (?ₓ ("[x]" ,@c-like)
;;; ("(x)" octave))
;;; (?₅ ("[5]") ,@c-like)
;;; (?₆ ("[6]") ,@c-like)
;;; (?₇ ("[7]") ,@c-like)
;;; (?₈ ("[8]") ,@c-like)
;;; (?₉ ("[9]") ,@c-like)
;;; (?⋂ "\\<intersection\\>" (,@lispen))
;;; (?⋃ "\\<union\\>" (,@lispen))
;;; (?∧ ("\\<And\\>" emacs-lisp lisp python)
;;; ("\\<andalso\\>" sml)
;;; ("&&" c c++ perl haskell))
;;; (?∨ ("\\<or\\>" emacs-lisp lisp)
;;; ("\\<orelse\\>" sml)
;;; ("||" c c++ perl haskell))
;;; (?¬ ("\\<!\\>" c c++ perl sh)
;;; ("\\<not\\>" lisp emacs-lisp scheme haskell sml))
)))
"*List of pretty patterns.
Should be a list of the form ((MODE ((REGEXP . GLYPH) ...)) ...)")
(defun pretty-add-keywords (mode keywords)
"Add pretty character KEYWORDS to MODE
MODE should be a symbol, the major mode command name, such as
`c-mode' or nil. If nil, pretty keywords are added to the current
buffer. KEYWORDS should be a list where each element has the
form (REGEXP . CHAR). REGEXP will be replaced with CHAR in the
relevant buffer(s)."
(font-lock-add-keywords
mode (mapcar (lambda (kw) `(,(car kw)
(0 (prog1 nil
(compose-region (match-beginning 0)
(match-end 0)
,(cdr kw))))))
keywords)))
(defun pretty-regexp (regexp glyph)
"Replace REGEXP with GLYPH in buffer."
(interactive "MRegexp to replace:
MCharacter to replace with: ")
(pretty-add-keywords nil `((,regexp . ,(string-to-char glyph))))
(font-lock-fontify-buffer))
(provide 'pretty-mode)