-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeywork.el
112 lines (91 loc) Β· 3.08 KB
/
keywork.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
(require 'cl-lib)
(require 'eieio)
(defvar keywork--map (make-sparse-keymap)
"Initial map of keywork.")
(define-minor-mode keywork-mode
"Toggle keywork minor mode."
:global t
:lighter " keywork"
:keymap keywork--map
:group 'keywork)
(defun keywork--priority-minor-mode-map (_file)
"Try to ensure that keybindings retain priority over other minor modes.
Called via the `after-load-functions' special hook."
(unless (eq (caar minor-mode-map-alist) 'keywork-mode)
(let ((mykeys (assq 'keywork-mode minor-mode-map-alist)))
(assq-delete-all 'keywork-mode minor-mode-map-alist)
(add-to-list 'minor-mode-map-alist mykeys))))
(add-hook 'after-load-functions 'keywork--priority-minor-mode-map)
(defun keywork-on (list-of-map)
(let* ((colors-and-map (keywork--activate-compile list-of-map))
(colors (car colors-and-map))
(map (cdr colors-and-map)))
(set-cursor-color (car colors))
(setf (cdr (assq 'keywork-mode minor-mode-map-alist))
map)))
(cl-defstruct map
(binds nil)
(pred nil :type function)
(col nil :type string))
;; returns (colour keymap)
(defun keywork--activate-compile (list-of-map)
(let* ((true-maps (cl-remove-if-not
(lambda (map) (funcall (map-pred map)))
list-of-map))
(colors-list nil)
(list-of-keymaps
(mapcar
(lambda (child-map)
(push (map-col child-map) colors-list)
(let ((compiled-child-map (make-sparse-keymap)))
(mapcar
(lambda (bind)
(let ((key (car bind))
(rhs (cadr bind)))
(cl-etypecase rhs
((list-of map)
(let* ((colors-and-map (keywork--activate-compile rhs))
(_colors (car colors-and-map))
(map (cdr colors-and-map)))
(define-key compiled-child-map (kbd key) map)))
(t (define-key compiled-child-map (kbd key) rhs)))))
(map-binds child-map))
compiled-child-map))
true-maps)))
(cons colors-list
(make-composed-keymap
list-of-keymaps (make-sparse-keymap)))))
(defun tt (&rest _) t)
(defvar keywork-default-cursor-color "#ffffff")
(cl-defun keywork--make-map (&key (pred 'tt)
(color keywork-default-cursor-color)
map)
(list (make-map
:binds map
:pred pred
:col color)))
(defun keywork--make-list-of-map (map-symbol)
(let* ((child-lists-of-map
(apply 'append
(mapcar 'keywork--make-list-of-map
(get map-symbol 'children)))))
(append child-lists-of-map (eval map-symbol))))
(defun keywork--add-child (map-symbol child-symbol)
(put map-symbol 'children
(append (list child-symbol) (get map-symbol 'children))))
(setq keywork--last-activated nil)
(defmacro kw-on (map-symbol)
`(lambda () (interactive)
(setq keywork--last-activated ,map-symbol)
(keywork-on (keywork--make-list-of-map ,map-symbol))))
(defun keywork-refresh ()
(when keywork--last-activated
(keywork-on (keywork--make-list-of-map keywork--last-activated))))
(defmacro kw-c (&rest body)
`(lambda () (interactive)
,@body))
(defmacro kw-seq (map-symbol)
`(keywork--make-list-of-map ,map-symbol))
(defun kw-m (binds)
(keywork--make-map :map binds))
(provide 'keywork)