forked from meow-edit/meow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeow-core.el
209 lines (181 loc) · 5.8 KB
/
meow-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
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
;;; meow-core.el --- Mode definitions for Meow
;;; -*- 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:
;;; Modes definition in Meow.
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'meow-util)
(require 'meow-keypad)
(require 'meow-var)
(require 'meow-eldoc)
(require 'meow-wgrep)
(require 'meow-yas)
(require 'meow-company)
;;;###autoload
(define-minor-mode meow-insert-mode
"Meow Insert state."
nil
" [I]"
meow-insert-state-keymap
(meow--insert-init))
;;;###autoload
(define-minor-mode meow-normal-mode
"Meow Normal state."
nil
" [N]"
meow-normal-state-keymap
(meow--normal-init))
;;;###autoload
(define-minor-mode meow-keypad-mode
"Meow keypad state."
nil
" [K]"
meow-keypad-state-keymap
(meow--keypad-init))
;;;###autoload
(define-minor-mode meow-motion-mode
"Meow motion state."
nil
" [M]"
meow-motion-state-keymap
(meow--motion-init))
;;;###autoload
(define-minor-mode meow-mode
"Meow minor mode.
This minor mode is used by meow-global-mode, should not be enabled directly."
nil
nil
meow-keymap
(if meow-mode
(meow--enable)
(meow--disable)))
(defun meow-indicator ()
"Indicator show current mode."
(interactive)
(when (bound-and-true-p meow-global-mode)
(cond
(meow-keypad-mode
(concat
(propertize "KEYPAD [" 'face 'meow-keypad-indicator)
(meow--keypad-format-prefix)
(meow--keypad-format-keys)
(propertize "] " 'face 'meow-keypad-indicator)))
(meow-normal-mode
(concat
(propertize
(if (meow--direction-backward-p)
"NORMAL«"
"NORMAL")
'face 'meow-normal-indicator)
(when-let ((sel-type (meow--selection-type)))
(concat (propertize " [" 'face 'meow-normal-indicator)
(symbol-name sel-type)
(propertize "]" 'face 'meow-normal-indicator)))))
(meow-motion-mode
(propertize "MOTION" 'face 'meow-motion-indicator))
(meow-insert-mode
(cond
;; Vterm's vterm-mode is read-only.
((and buffer-read-only (not (equal major-mode 'vterm-mode)))
(propertize "READONLY" 'face 'meow-insert-indicator))
((bound-and-true-p overwrite-mode)
(propertize "OVERWRITE" 'face 'meow-insert-indicator))
(t (propertize "INSERT" 'face 'meow-insert-indicator))))
(t ""))))
;;;###autoload
(define-global-minor-mode meow-global-mode meow-mode
(lambda ()
(unless (minibufferp)
(meow-mode 1)))
(if meow-mode
(meow--global-enable)
(meow--global-disable)))
(defun meow--normal-init ()
"Init normal state."
(when meow-normal-mode
(meow-insert-mode -1)
(meow-motion-mode -1)
(unless meow--keymap-loaded
(let ((keymap (meow--get-mode-leader-keymap major-mode t)))
(define-key meow-normal-state-keymap (kbd "SPC") keymap)
(setq-local meow--keymap-loaded t)))))
(defun meow--insert-init ()
"Init insert state."
(when meow-insert-mode
(meow-normal-mode -1)
(meow-motion-mode -1)))
(defun meow--motion-init ()
"Init motion state."
(when meow-motion-mode
(meow-normal-mode -1)
(meow-insert-mode -1)
(unless meow--keymap-loaded
(let ((keymap (meow--get-mode-leader-keymap major-mode t)))
(define-key meow-motion-state-keymap (kbd "SPC") keymap))
(setq-local meow--keymap-loaded t))))
(defun meow--keypad-init ()
"Init keypad state.
We have to remember previous state, so that we can restore it."
(cond
((meow-motion-mode-p)
(setq meow--keypad-previous-state 'motion)
(meow-motion-mode -1))
((meow-normal-mode-p)
(setq meow--keypad-previous-state 'normal)
(meow-normal-mode -1)))
(setq meow--prefix-arg current-prefix-arg
meow--keypad-keys nil
meow--use-literal nil
meow--use-meta nil))
(defun meow--enable ()
"Enable Meow.
We will save command on SPC to variable `meow--space-command'
before activate any state.
then SPC will be bound to LEADER."
(unless meow--space-command
(let ((cmd (key-binding (read-kbd-macro "SPC"))))
(when (and (commandp cmd)
(not (equal cmd 'undefined)))
(setq-local meow--space-command cmd))))
(if (apply #'derived-mode-p meow-normal-state-mode-list)
(meow--switch-state 'normal)
(meow--switch-state 'motion)))
(defun meow--disable ()
"Disable Meow."
(meow-normal-mode -1)
(meow-insert-mode -1)
(meow-motion-mode -1))
(defun meow--global-enable ()
"Enable meow globally."
(global-set-key (kbd "<escape>") 'meow-escape-or-normal-modal)
(setq delete-active-region nil)
(meow--eldoc-setup)
(when (featurep 'wgrep)
(meow--wgrep-setup))
(when (featurep 'yasnippet)
(meow--yas-setup))
(when (featurep 'company)
(meow--company-setup))
(add-hook 'pre-command-hook #'meow--pre-command-function)
(add-hook 'post-command-hook #'meow--post-command-function))
(defun meow--global-disable ()
"Disable Meow globally."
(global-unset-key (kbd "<escape>"))
(remove-hook 'pre-command-hook #'meow--pre-command-function)
(remove-hook 'post-command-hook #'meow--post-command-function))
(provide 'meow-core)
;;; meow-core.el ends here