-
Notifications
You must be signed in to change notification settings - Fork 0
/
miao-core.el
152 lines (128 loc) · 4.63 KB
/
miao-core.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
;;; miao-core.el --- Miao variables -*- lexical-binding: t; -*-
;; This file is not part of GNU Emacs.
;; 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
;; of the License, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Internal variables and customizable variables.
;;; Code:
(defun miao--switch-state (state)
"Switch to STATE execute 'miao-switch-state-hook unless NO-HOOK is non-nil."
(unless (eq state (miao--current-state))
(let ((mode (alist-get state miao-state-mode-alist)))
(funcall mode 1))))
(defun miao--current-state ()
miao--current-state)
(defun miao-switch-to-previous-state ()
(miao--switch-state miao--leader-previous-state))
(defun miao--event-key (event)
(let ((c (event-basic-type event)))
(if (and (char-or-string-p c)
(member 'shift (event-modifiers event)))
(upcase c)
c)))
(defun miao--toggle-bypass-mode ()
(interactive)
(if (bound-and-true-p miao-bypass-mode)
(miao-normal-mode)
(miao-bypass-mode)))
(defun miao-self-insert-command (N &optional C)
(interactive "p")
(self-insert-command N C))
(defun miao--parse-input-event (event)
(cond
((equal event 32)
"SPC")
((characterp event)
(string event))
((equal 'tab event)
"TAB")
((equal 'return event)
"RET")
((equal 'backspace event)
"DEL")
((equal 'escape event)
"ESC")
((symbolp event)
(format "<%s>" event))
(t nil)))
(defun miao-leader-self-insert ()
"Default command when leader state is enabled."
(interactive)
(setq this-command last-command)
(when-let ((event (miao--event-key last-input-event))
(key (miao--parse-input-event event)))
(push (cons 'literal key) miao--leader-keys)
;; Try execute if the input is valid.
(miao--leader-try-execute)))
(defun miao--leader-lookup-key (keys)
(let* ((overriding-local-map miao-leader-state-keymap)
(keybind (key-binding keys))
(leader-major-keymap (gethash major-mode miao-leader-major-keymap-hash)))
(if leader-major-keymap
(let* ((overriding-local-map leader-major-keymap)
(major-keybind (key-binding keys)))
(if (or (not major-keybind)
(equal major-keybind 'undefined))
keybind
major-keybind))
keybind)))
(defun miao--leader-describe-keymap (keymap)
(when (or
miao--leader-keymap-description-activated
(setq miao--leader-keymap-description-activated
(sit-for miao-leader-describe-delay t)))
(which-key--create-buffer-and-show nil keymap nil (concat "Miao: " (miao--leader-format-keys)))))
(defun miao--leader-try-execute ()
"Try execute command.
If there is a command available on the current key binding,
try replacing the last modifier and try again."
(let* ((key-str (miao--leader-format-keys nil))
(cmd (miao--leader-lookup-key (read-kbd-macro key-str))))
(cond
((commandp cmd t)
(setq current-prefix-arg miao--prefix-arg
miao--prefix-arg nil
real-this-command cmd
this-command cmd)
(miao-leader-quit)
(call-interactively cmd))
((keymapp cmd)
(miao--leader-describe-keymap cmd))
(t
(setq miao--prefix-arg nil)
(message "[Miao] %s is undefined" (miao--leader-format-keys nil))
(miao-leader-quit)))))
(defun miao--leader-format-single-key (key)
"Return a display format for input KEY."
(cl-case (car key)
(literal (cdr key))))
(defun miao--leader-format-upcase (k)
"Return S-k for upcase k."
(let ((case-fold-search nil))
(if (and (stringp k)
(string-match-p "^[A-Z]$" k))
(format "S-%s" (downcase k))
k)))
(defun miao--leader-format-keys (&optional prompt)
"Return a display format for current input keys."
(let ((result ""))
(setq result
(thread-first
(mapcar #'miao--leader-format-single-key miao--leader-keys)
(reverse)
(string-join " ")))
(cond
(prompt
(setq result (concat result " C-"))))
result))
(provide 'miao-core)