-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpel-numkpad.el
260 lines (225 loc) · 9.27 KB
/
pel-numkpad.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
;;; pel-numkpad.el --- PEL Numeric Keypad Key Control -*-lexical-binding: t-*-
;; Copyright (C) 2020, 2021, 2022, 2024 Pierre Rouleau
;; Author: Pierre Rouleau <[email protected]>
;; This file is part of the PEL package
;; 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; --------------------------------------------------------------------------
;;; Commentary:
;;
;; Dynamic control of the keypad keys to allow both numlocking and non
;; numlocking use.
;; Note: when using Emacs inside a macOS Terminal application, running
;; `emacs -nw`, the key labelled 'clear' which is labelled 'Numlock' in my
;; keypad layout below will effectively behave as a numlock key, but does not
;; register as a key inside Emacs.
;;
;;; --------------------------------------------------------------------------
;;; Dependencies:
(require 'pel--base) ; use pel-toggle
;; pel-scroll functions are only needed if the pel-3 or pel-9 commands
;; are used. Avoid loading pel-scroll until those commands are used.
(eval-when-compile
(require 'pel-scroll)) ; use: pel-scroll-up, pel-scroll-down
(require 'pel-ccp) ; use pel-overwrite-yank
;;; --------------------------------------------------------------------------
;;; Code:
(defvar pel-mac-keypad-numlocked nil
"Identify whether macOS numeric keypad is used as numlocked or not.
If t, macOS keypad is used as the original numlocked keypad,
otherwise, it's a cursor centric keypad. By default, the value
is t to identify the native behaviour: NumLocked.
This is valid for:
- macOS when Emacs is running in graphics mode,
- Other OS that support a NumLock key.
When using terminal Emacs under macOS, or Linux under a macOS hosted VM,
the NumLock key does not exists. Terminal.app can be configured to
map the <clear> key to NumLock but Emacs only sees the impact to the
keypad keys; it does not see the <clear> key events.
The following keys are used in each mode:
+==============================+==========================+
| Not NumLocked | NumLocked |
+==============================+==========================+
|NumLock = / * |Numlock = / * |
|home up ScrlUp - | 7 8 9 - |
|right center right + | 4 5 6 + |
|End down ScrlDn Enter | 1 2 3 Enter |
|---insert---- Del Enter | ---0--- . Enter |
+==============================+==========================+
where the keys for '/', '*', '-' and '+' have their natural
meaning in Numlock mode but have other meanings in Non Numlocked
mode.
Use `pel-toggle-mac-numlock' to activate/deactivate numlock on
environments that can't bind the clear key such as macOS
terminal.")
;;-pel-autoload
(defun pel-show-mac-numlock ()
"Display state of PEL Keypad num-lock mode.
In macOS TTY mode, the function shows an error as Emacs does not
receive NumLock keys events for the <clear> key used as NumLock.
BUG: If running Linux in a VM hosted on macOS, the code does not
detect macOS and thinks it receives NumLock notification, allowing
the function to display potentially invalid information."
(interactive)
(if (and pel-system-is-macos-p
pel-emacs-is-a-tty-p)
(user-error
"NumLock state unknown: can't detect <clear> key events in macOS TTY")
(if pel-mac-keypad-numlocked
(message "Num-Lock ON")
(message "Num-Lock off; cursor mode available"))))
;;-pel-autoload
(defun pel-toggle-mac-numlock ()
"Toggle PEL numlock mode.
Nothing done in macOS TTY.
BUG: If running Linux in a VM hosted on macOS, the code does not
detect macOS and thinks it receives NumLock notification, allowing
the function to proceed which may cause out-of-sync status and
prevent proper handling of the NumPad keys."
(interactive)
(if (and pel-system-is-macos-p
pel-emacs-is-a-tty-p)
(user-error
"Can't toggle NumLock on macOS TTY: use <clear> & switch blindly.")
(pel-toggle 'pel-mac-keypad-numlocked)
(pel-show-mac-numlock)) )
;;-pel-autoload
(defun pel-0 (&optional n)
"Keypad 0 key handler, with N as numeric prefix to support repetition."
(interactive "*P")
(if pel-mac-keypad-numlocked
(insert-char ?0 (abs (prefix-numeric-value n)) t)
(pel-overwrite-yank)))
;;-pel-autoload
(defun pel-1 (&optional n)
"Keypad 1 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(if pel-mac-keypad-numlocked
(insert-char ?1 (abs (prefix-numeric-value n)) t)
(if (and (require 'pel-navigate nil :no-error)
(fboundp 'pel-end))
(pel-end)
(error "Function pel-end is not loaded"))))
;;-pel-autoload
(defun pel-2 (&optional n)
"Keypad 2 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?2 (abs n) t)
(forward-line n))))
;;-pel-autoload
(defun pel-3 (&optional n)
"Keypad 3 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?3 (abs n) t)
(if (require 'pel-scroll nil :no-error)
(if (> 0)
(when (fboundp 'pel-scroll-up)
(pel-scroll-up n))
(when (fboundp 'pel-scroll-down)
(pel-scroll-down (abs n))))
(error "File not loaded: pel-scroll")))))
;;-pel-autoload
(defun pel-4 (&optional n)
"Keypad 4 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?4 (abs n) t)
(left-char n))))
;;-pel-autoload
(defun pel-5 (&optional n)
"Keypad 5 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?5 (abs n) t)
(recenter-top-bottom))))
;;-pel-autoload
(defun pel-6 (&optional n)
"Keypad 6 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?6 (abs n) t)
(right-char n))))
;;-pel-autoload
(defun pel-7 (&optional n)
"Keypad 7 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?7 (abs n) t)
(if (and (require 'pel-navigate nil :no-error)
(fboundp 'pel-home))
(pel-home)
(error "Function pel-home is not loaded")))))
;;-pel-autoload
(defun pel-8 (&optional n)
"Keypad 8 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?8 (abs n) t)
(forward-line (- n)))))
;;-pel-autoload
(defun pel-9 (&optional n)
"Keypad 9 key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?9 (abs n) t)
(if (require 'pel-scroll nil :no-error)
(if (> 0)
(when (fboundp 'pel-scroll-down)
(pel-scroll-down n))
(when (fboundp 'pel-scroll-up)
(pel-scroll-up (abs n))))
(error "File not loaded: pel-scroll")))))
;;-pel-autoload
(defun pel-kp-decimal (&optional n)
"Keypad '.' key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?. (abs n) t)
(delete-char n))))
;;-pel-autoload
(defun pel-kp-subtract (&optional n)
"Keypad '-' key handler, with N as numeric prefix to support repetition."
(interactive "*P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?- (abs n) t)
(if (and (require 'pel-ccp nil :no-error)
(fboundp 'pel-kill-or-delete-marked-or-whole-line))
(pel-kill-or-delete-marked-or-whole-line n)
(error
"Function pel-kill-or-delete-marked-or-whole-line not loaded")))))
;;-pel-autoload
(defun pel-kp-add (&optional n)
"Keypad '+' key handler, with N as numeric prefix to support repetition."
(interactive "P")
(let ((n (prefix-numeric-value n)))
(if pel-mac-keypad-numlocked
(insert-char ?+ (abs n) t)
(if (and (require 'pel-ccp nil :no-error)
(fboundp 'pel-copy-marked-or-whole-line))
(pel-copy-marked-or-whole-line)))))
;;; --------------------------------------------------------------------------
(provide 'pel-numkpad)
;;; pel-numkpad.el ends here