-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathergoemacs-key-description.el
356 lines (333 loc) · 14.1 KB
/
ergoemacs-key-description.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
;;; ergoemacs-key-description.el --- Ergoemacs map interface -*- lexical-binding: t -*-
;; Copyright © 2013-2021 Free Software Foundation, Inc.
;; Filename: ergoemacs-key-description.el
;; Description:
;; Author: Matthew L. Fidler
;; Maintainer: Matthew L. Fidler
;; Created: Sat Sep 28 20:10:56 2013 (-0500)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Ergoemacs-mode key description library.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 3, 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. If not, see <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
;; (require 'guide-key nil t)
(require 'cl-lib)
(eval-when-compile
(require 'ergoemacs-macros))
(require 'descr-text)
(require 'faces)
(require 'help-mode)
(defvar ergoemacs-use-unicode-symbols)
(defvar ergoemacs-display-unicode-characters)
(defvar ergoemacs-display-capitalize-keys)
(defvar ergoemacs-display-key-use-face)
(defvar ergoemacs-display-small-symbols-for-key-modifiers nil)
(defvar ergoemacs-display-use-unicode-brackets-around-keys nil)
(defvar ergoemacs-display-without-brackets nil
"Display the key without brackets.")
(declare-function ergoemacs-translate--escape-to-meta "ergoemacs-translate")
(declare-function ergoemacs-translate--event-modifiers "ergoemacs-translate")
(declare-function ergoemacs-translate--event-basic-type "ergoemacs-translate")
(declare-function ergoemacs-translate--event-modifier-hash "ergoemacs-translate")
(declare-function ergoemacs-map-properties--composed-list "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--map-fixed-plist "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--map-list "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--key-lessp "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--key-hash "ergoemacs-map-properties")
(declare-function ergoemacs-map--cache-- "ergoemacs-map")
(declare-function ergoemacs-map-keymap "ergoemacs-mapkeymap")
(defvar ergoemacs-key-description--display-char-cache nil
"List of characters and fonts and if they display or not.")
(defun ergoemacs-key-description--display-char-p (char)
"Determines if CHAR can be displayed."
(cond
((= 1 (length char))
(ignore-errors
(let* (ret
(buf (current-buffer))
(face (font-xlfd-name (face-attribute 'default :font)))
(found (assoc (list face char window-system) ergoemacs-key-description--display-char-cache)))
(if found
(nth 0 (cdr found))
(switch-to-buffer (get-buffer-create " *ergoemacs-display-char-p*") t)
(delete-region (point-min) (point-max))
(insert char)
(let ((display (describe-char-display (point-min) (char-after (point-min)))))
(if (display-graphic-p (selected-frame))
(if display
(setq ret (font-at (point-min))))
(if display
(setq ret (font-at (point-min))))))
(switch-to-buffer buf)
;; Save it so the user doesn't see the buffer popup very much
;; (if at all).
(push (list (list face char window-system) ret) ergoemacs-key-description--display-char-cache)
ret))))
((stringp char)
(catch 'does-not-display
(dolist (c (mapcar (lambda(x) (make-string 1 x)) (vconcat char)))
(when (not (ergoemacs-key-description--display-char-p c))
(throw 'does-not-display nil)))
t))
(t nil)))
(defun ergoemacs-key-description--unicode-char--internal (char alt-char)
"Return CHAR if it can be displayed, otherwise use ALT-CHAR.
This assumes `ergoemacs-display-unicode-characters' is non-nil. When
`ergoemacs-display-unicode-characters' is nil display ALT-CHAR"
(if (and ergoemacs-display-unicode-characters
(ergoemacs-key-description--display-char-p char))
char
alt-char))
(defun ergoemacs-key-description--unicode-char (&rest chars)
"Return the first displayable character in CHARS.
This uses `ergoemacs-key-description--unicode-char--internal'"
(if ergoemacs-use-unicode-symbols
(let* ((char-list chars)
(test-char (pop char-list))
tmp
(next-char test-char))
(catch 'found-char
(while char-list
(setq next-char (pop char-list)
tmp (ergoemacs-key-description--unicode-char--internal test-char next-char))
(if (string= tmp next-char)
(setq test-char next-char)
(throw 'found-char tmp)))
next-char))
(car (last chars))))
(defun ergoemacs-key-description--key (key mod)
"Key description.
KEY is the fundamental event of a key.
MOD ar the modifiers applied to the key."
(let ((ret ""))
(cond
((eq key 'deletechar)
(setq ret "Del"))
((memq key '(insert insertchar))
(setq ret "Ins"))
((eq key 'home)
(setq ret "Home"))
((eq key 'end)
(setq ret "End"))
((eq key 32)
(setq ret "Space"))
((eq key 127)
(setq ret "←Backspace"))
((eq key 'escape)
(setq ret "Esc"))
((eq key 'tab)
(setq ret "Tab↹"))
((eq key 'return)
(setq ret "Enter↵"))
((memq key '(apps menu))
(setq ret "▤"))
((eq key 'left)
(setq ret "←"))
((eq key 'right)
(setq ret "→"))
((eq key 'up)
(setq ret "↑"))
((eq key 'down)
(setq ret "↓"))
((eq key 'prior)
(setq ret "PgUp"))
((eq key 'next)
(setq ret "PgDn"))
((eq key 'remap)
(setq ret "➩"))
((eq key 'ergoemacs-timeout)
(setq ret "⌚"))
((integerp key)
(setq ret (or (and (or (and (eq ergoemacs-display-capitalize-keys 'with-modifiers)
mod)
(eq ergoemacs-display-capitalize-keys t))
(upcase (make-string 1 key)))
(make-string 1 key))))
((and (symbolp key) (string-match "^f[0-9]+$" (symbol-name key)))
(setq ret (upcase (symbol-name key))))
(t
(setq ret (format "%s" key))))
(setq ret (concat (copy-sequence ret) ""))
(when (and ergoemacs-display-key-use-face
(not ergoemacs-display-small-symbols-for-key-modifiers))
(add-text-properties 0 (length ret)
'(face ergoemacs-display-key-face)
;; Need to make a copy of ret because the
;; (length ret) call makes it sometimes
;; immutable
ret))
ret))
(defun ergoemacs-key-description--modifier (mod)
"Modifier MOD description."
(let (ret)
(cond
;; OSX specific key descriptions
((and (eq mod 'meta) ergoemacs-display-small-symbols-for-key-modifiers
(eq system-type 'darwin)
(or (and (boundp 'mac-command-modifier)
(eq mac-command-modifier 'meta))
(and (boundp 'ns-command-modifier)
(eq ns-command-modifier 'meta))))
(setq ret "⌘"))
((and (eq mod 'meta)
(eq system-type 'darwin)
(or (and (boundp 'mac-command-modifier)
(eq mac-command-modifier 'meta))
(and (boundp 'ns-command-modifier)
(eq ns-command-modifier 'meta))))
(setq ret "⌘Cmd+"))
((and (eq mod 'meta)
(eq system-type 'darwin)
(or (and (boundp 'mac-alternate-modifier)
(eq mac-alternate-modifier 'meta))
(and (boundp 'ns-alternate-modifier)
(eq ns-alternate-modifier 'meta))))
(setq ret "⌥Opt+"))
((and (eq mod 'meta) ergoemacs-display-small-symbols-for-key-modifiers
(eq system-type 'darwin)
(or (and (boundp 'mac-alternate-modifier)
(eq mac-alternate-modifier 'meta))
(and (boundp 'ns-alternate-modifier)
(eq ns-alternate-modifier 'meta))))
(setq ret "⌥"))
((and ergoemacs-display-small-symbols-for-key-modifiers (eq mod 'shift))
(setq ret "⇧"))
((and ergoemacs-display-small-symbols-for-key-modifiers (eq mod 'meta))
(setq ret "♦"))
((and (or (eq system-type 'darwin) ergoemacs-display-small-symbols-for-key-modifiers)
(memq mod '(control ergoemacs-control)))
(setq ret "^"))
((eq mod 'shift)
(setq ret "⇧Shift+"))
((memq mod '(control ergoemacs-control))
(setq ret "Ctrl+"))
((eq mod 'meta)
(setq ret "Alt+"))
((and (eq mod 'super) ergoemacs-display-small-symbols-for-key-modifiers
(eq system-type 'windows-nt))
(setq ret "⊞"))
((and (eq mod 'super)
(eq system-type 'windows-nt))
(setq ret "⊞Win+"))
(t
(setq ret (format "%s+" mod))
(when ergoemacs-display-key-use-face
(add-text-properties 0 (- (length ret) 1)
'(face ergoemacs-display-key-face) ret))))
(when (and ergoemacs-display-key-use-face
(not ergoemacs-display-small-symbols-for-key-modifiers))
(add-text-properties 0 (- (length ret) 1)
'(face ergoemacs-display-key-face) ret))
ret))
(defun ergoemacs-key-description--add-emacs-modifiers-for-ergoemacs-modifiers (mod)
"Change `ergoemacs-mode' special modifiers in MOD to the Emacs modifiers."
(let ((tmp '()))
(dolist (m mod)
(cond
((eq m 'ergoemacs-control)
(push 'control tmp))
((eq m 'control))
(t
(push m tmp))))
tmp))
(defun ergoemacs-key-description--menu (kbd &optional layout)
"Create pretty keyboard bindings for menus.
KBD is the keyboard code, LAYOUT is the keyboard layout."
(let ((ergoemacs-display-without-brackets t)
(ergoemacs-display-key-use-face nil)
(ergoemacs-display-small-symbols-for-key-modifiers nil))
(ergoemacs-key-description kbd layout)))
(defun ergoemacs-key-description (kbd &optional layout)
"Create Pretty keyboard binding from kbd from M- to Alt+.
KBD is the keyboard code. LAYOUT is the layout that is used."
(if (not kbd) ""
(let ((kbd (or (ergoemacs-translate--escape-to-meta kbd)
(and (stringp kbd) (vconcat kbd)) kbd)))
(if (eq kbd (vector)) ""
(let ((ret "")
tmp
mod ev)
(dolist (key (listify-key-sequence kbd))
(setq mod (ergoemacs-translate--event-modifiers key)
ev (ergoemacs-translate--event-basic-type key))
(cond
((and (memq 'control mod) (eq ev ?\[))
(setq mod (ergoemacs-key-description--add-emacs-modifiers-for-ergoemacs-modifiers mod)
ev 'escape))
((and (memq 'control mod) (eq ev ?m))
(setq mod (ergoemacs-key-description--add-emacs-modifiers-for-ergoemacs-modifiers mod)
ev 'return))
((and (memq 'control mod) (eq ev ?i))
(setq mod (ergoemacs-key-description--add-emacs-modifiers-for-ergoemacs-modifiers mod)
ev 'tab))
((memq 'ergoemacs-shift mod)
(setq tmp '())
(dolist (m mod)
(unless (eq m 'ergoemacs-shift)
(push m tmp)))
(setq mod tmp
ev (ergoemacs-gethash (intern (format "s%s" ev))
(ergoemacs-translate--event-modifier-hash layout)))))
(when (memq 'ergoemacs-gui mod)
(setq tmp '())
(dolist (m mod)
(unless (eq m 'ergoemacs-gui)
(push m tmp)))
(setq mod tmp))
(setq tmp (format "%s%s%s%s"
(or (and (or ergoemacs-display-without-brackets ergoemacs-display-key-use-face) "")
(and ergoemacs-display-use-unicode-brackets-around-keys "【")
"[")
(mapconcat #'ergoemacs-key-description--modifier
mod "")
(ergoemacs-key-description--key ev mod)
(or (and (or ergoemacs-display-without-brackets ergoemacs-display-key-use-face) "")
(and ergoemacs-display-use-unicode-brackets-around-keys "】")
"]")))
(when (and ergoemacs-display-small-symbols-for-key-modifiers ergoemacs-display-key-use-face)
(add-text-properties 0 (length tmp)
'(face ergoemacs-display-key-face) tmp))
(setq ret (format "%s%s%s" ret
(or (and (or ergoemacs-display-without-brackets ergoemacs-display-key-use-face) " ")
(and ergoemacs-display-use-unicode-brackets-around-keys "")
" ")
tmp)))
(substring ret (or (and (or ergoemacs-display-without-brackets ergoemacs-display-key-use-face) 1)
(and ergoemacs-display-use-unicode-brackets-around-keys 0)
1)))))))
(defun ergoemacs-key-description-kbd (code)
"Create `ergoemacs-mode' style description of kbd macro CODE."
(if (not code) ""
(save-match-data
(ergoemacs-key-description (read-kbd-macro code t)))))
(provide 'ergoemacs-key-description)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ergoemacs-key-description.el ends here
;; Local Variables:
;; coding: utf-8-emacs
;; End: