forked from ergoemacs/ergoemacs-mode
/
ergoemacs-component.el
288 lines (253 loc) · 10.9 KB
/
ergoemacs-component.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
;;; ergoemacs-component.el --- Ergoemacs map interface -*- lexical-binding: t -*-
;; Copyright © 2013-2015 Free Software Foundation, Inc.
;; Filename: ergoemacs-component.el
;; Description:
;; Author: Matthew L. Fidler
;; Maintainer: Matthew L. Fidler
;; Created: Sat Sep 28 20:10:56 2013 (-0500)
;;
;;; Commentary:
;;
;; Code for ergoemacs components.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'cl-lib)
(eval-when-compile
(require 'ergoemacs-macros))
(require 'help-mode)
(require 'find-func)
(defvar ergoemacs-command-loop--minibuffer-unsupported-p)
(defvar ergoemacs-map-properties--label-atoms-maps)
(defvar ergoemacs--last-start-emacs-state-2)
(defvar ergoemacs--start-emacs-state-2)
(defvar ergoemacs-component-hash)
(defvar ergoemacs-display-key-use-face-p)
(defvar ergoemacs-keyboard-layout)
(defvar ergoemacs-keymap)
(defvar ergoemacs-map-properties--known-maps)
(defvar ergoemacs-mode--fast-p)
(defvar ergoemacs-mode-version)
(defvar ergoemacs-saved-global-map)
(defvar ergoemacs-theme-hash)
(defvar ergoemacs-theme-version)
(defvar ergoemacs-translate--translation-hash)
(defvar ergoemacs-translation-hash)
(declare-function ergoemacs--emacs-state "ergoemacs-mode")
(declare-function ergoemacs-mode--setup-hash-tables--setq "ergoemacs-mode")
(declare-function ergoemacs-mode-clear-cache "ergoemacs-mode")
(declare-function ergoemacs-set "ergoemacs-lib")
(declare-function ergoemacs-reset "ergoemacs-lib")
(declare-function ergoemacs-warn "ergoemacs-lib")
(declare-function ergoemacs-theme-components "ergoemacs-theme-engine")
(declare-function ergoemacs-theme--regexp "ergoemacs-theme-engine")
(declare-function ergoemacs-translate "ergoemacs-translate")
(declare-function ergoemacs-translate--apply-key "ergoemacs-translate")
(declare-function ergoemacs-translate--define-key "ergoemacs-translate")
(declare-function ergoemacs-map-properties--label-known "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--original "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--map-list "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--put "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--key-hash "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--map-regexp "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--empty-p "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--label "ergoemacs-map-properties")
(declare-function ergoemacs-map-keymap "ergoemacs-mapkeymap")
(declare-function ergoemacs-key-description "ergoemacs-key-description")
(declare-function ergoemacs-layout--regexp "ergoemacs-layouts")
;; ergoemacs-translate
(defcustom ergoemacs-ignore-prev-global t
"If non-nil, the ergoemacs-mode will ignore previously defined
global keybindings."
:type 'boolean
:group 'ergoemacs-mode)
;; for compatability
;;;###autoload
(defun ergoemacs-ignore-prev-global ()
"Ignore previously defined global keys."
(setq ergoemacs-ignore-prev-global t))
(defun ergoemacs-remap (function)
"Remap the FUNCTION to the appropriate key and then call that function."
(let ((key (where-is-internal function ergoemacs-keymap t)))
(call-interactively (key-binding key t nil (point)))))
;;; Translation between layouts
(defvar ergoemacs-component-struct--define-key-current nil)
(defun ergoemacs-component-struct--parse-list (list function &rest args)
"Handle :bind LIST and call FUNCTION.
The FUNCTION calls the with the first argument as the string
piece and the second argument the symbol piece of the definition.
It also passes ARGS if any are specified."
(let (arg1 arg2)
(cond
;; :list ("a" b "c" d)
((ignore-errors (and (consp list) (> (length list) 2)
(stringp (car list))
(setq arg1 (pop list))
(setq arg2 (pop list))))
(while (and arg1 arg2)
(apply function arg1 arg2 args)
(setq arg1 (pop list)
arg2 (pop list))))
((and (consp list) (stringp (car list)))
(apply function (car list)
(or (and (consp (cdr list)) (nth 1 list))
(cdr list))
args))
((and (consp list) (consp (car list)))
(dolist (elt list)
(when (and (consp elt) (stringp (car elt)))
(apply function (car elt)
(or (and (consp (cdr elt)) (nth 1 elt))
(cdr elt))
args)))))))
(defvar ergoemacs-component-struct--define-key-temp-map nil)
(defun ergoemacs-component-struct--define-key-get-def (def)
"Gets the `ergoemacs-mode' function definition for DEF."
(let (tmp)
(cond
((and (consp def) (memq (nth 0 def) '(kbd read-kbd-macro))
(stringp (nth 1 def)))
(read-kbd-macro (nth 1 def)))
((and (consp def) (= 1 (length def)) (symbolp (nth 0 def)))
(nth 0 def))
((and (consp def) (= 1 (length def)) (consp (nth 0 def))
(= 2 (length (nth 0 def)))
(eq (nth 0 (nth 0 def)) 'quote)
(symbolp (nth 1 (nth 0 def))))
(nth 1 (nth 0 def)))
((and (consp def) (= 2 (length def)) (eq (nth 1 def) 'quote) (symbolp (nth 1 def)))
(nth 1 def))
((and (consp def)
(= 2 (length def))
(stringp (nth 0 def))
(eq (nth 1 def) :emacs)
(setq tmp (lookup-key global-map (read-kbd-macro (nth 0 def))))
(commandp tmp))
tmp)
((ergoemacs-keymapp (ergoemacs-sv def))
(ergoemacs-sv def))
(t def))))
(defun ergoemacs-component-struct--closest-version (version version-list)
"Return the closest version to VERSION in VERSION-LIST.
Formatted for use with `ergoemacs-theme-component-hash' it will
return ::version or an empty string"
(if (or (not version) (string= "nil" version)) ""
(if version-list
(let ((use-version (version-to-list version))
biggest-version
biggest-version-list
smallest-version
smallest-version-list
best-version
best-version-list
test-version-list
ret)
(dolist (v version-list)
(setq test-version-list (version-to-list v))
(if (not biggest-version)
(setq biggest-version v
biggest-version-list test-version-list)
(when (version-list-< biggest-version-list test-version-list)
(setq biggest-version v
biggest-version-list test-version-list)))
(if (not smallest-version)
(setq smallest-version v
smallest-version-list test-version-list)
(when (version-list-< test-version-list smallest-version-list)
(setq smallest-version v
smallest-version-list test-version-list)))
(cond
((and (not best-version)
(version-list-<= test-version-list use-version))
(setq best-version v
best-version-list test-version-list))
((and (version-list-<= best-version-list test-version-list) ;; Better than best
(version-list-<= test-version-list use-version))
(setq best-version v
best-version-list test-version-list))))
(if (version-list-< biggest-version-list use-version)
(setq ret "")
(if best-version
(setq ret (concat "::" best-version))
(setq ret (concat "::" smallest-version))))
ret)
"")))
(defun ergoemacs-component-struct--lookup-hash (map-or-map-list)
"Lookup `ergoemacs-component-hash' from MAP-OR-MAP-LIST if necessary.
VERSION is the version of the `ergoemacs-mode' keys that you wish
to lookup.
This takes into consideration any versions defined, and the
closest `ergoemacs-theme-version' calculated from
`ergoemacs-component-struct--closest-version' by using
`ergoemacs-component-struct--lookup-closest'"
(if (consp map-or-map-list)
(mapcar #'ergoemacs-component-struct--lookup-hash map-or-map-list)
(let ((map map-or-map-list)
ret)
(when (symbolp map) ;; If map is a symbol, change to string.
(setq map (symbol-name map)))
(when (stringp map) ;; If map is a string, get the component from `ergoemacs-component-hash'
(setq ret (ergoemacs-gethash map ergoemacs-component-hash))
(when (and ret (functionp ret))
(funcall ret)
(setq ret (ergoemacs-gethash map ergoemacs-component-hash))))
(if (and map (string-match-p "::" map)) ret nil)
)
)
)
(defun ergoemacs-component-struct--minor-mode-map-alist-hash (&optional obj)
"Get `minor-mode-map-alist' additions in hash-table form.
OBJ is the ergoemacs theme components. Defaults to the value
returned from the function `ergoemacs-theme-components'.
LAYOUT is the current keyboard layout. Defaults to
`ergoemacs-keyboard-layout'"
(let ((obj (ergoemacs-component-struct--lookup-hash (or obj (ergoemacs-theme-components))))
(hash (make-hash-table)))
(cond
((consp obj)
(dolist (cur-obj obj)
(maphash
(lambda(key value)
(puthash key (append (ergoemacs-gethash key hash) value) hash))
(ergoemacs-component-struct--minor-mode-map-alist-hash cur-obj)))
hash)
(t
hash))))
(defun ergoemacs-component-struct--minor-mode-map-alist (&optional obj)
"Get the ending maps for `minor-mode-map-alist' using the
ergoemacs structures OBJ."
(let (ret map parent)
(maphash
(lambda(key value)
(setq parent (make-composed-keymap value)
map (make-sparse-keymap))
(ergoemacs map :label (list 'cond-map key (intern ergoemacs-keyboard-layout)))
(set-keymap-parent map parent)
(if (boundp key)
(push (cons key map) ret)
)
)
(ergoemacs-component-struct--minor-mode-map-alist-hash obj))
ret))
(defvar ergoemacs-component-struct--refresh-variables nil
"To reset a current theme, the variables are refreshed when this is non-nil.")
(provide 'ergoemacs-component)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ergoemacs-component.el ends here
;; Local Variables:
;; coding: utf-8-emacs
;; End: