aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/use-package/bind-key.el
diff options
context:
space:
mode:
authorStefan Kangas2022-11-29 18:01:53 +0100
committerStefan Kangas2022-11-29 18:01:53 +0100
commit0d7e6b1d101a8e87e1157ba8aa4c698ced1b39f1 (patch)
tree29455d59b3bc60f6b2d10360777ac12ffc9520f7 /lisp/use-package/bind-key.el
parent7939184f8e0370e7a3397d492812c6d202c2a193 (diff)
parent58cc931e92ece70c3e64131ee12a799d65409100 (diff)
downloademacs-scratch/use-package.tar.gz
emacs-scratch/use-package.zip
; Merge from https://github.com/jwiegley/use-packagescratch/use-package
Diffstat (limited to 'lisp/use-package/bind-key.el')
-rw-r--r--lisp/use-package/bind-key.el544
1 files changed, 544 insertions, 0 deletions
diff --git a/lisp/use-package/bind-key.el b/lisp/use-package/bind-key.el
new file mode 100644
index 00000000000..3168f686a09
--- /dev/null
+++ b/lisp/use-package/bind-key.el
@@ -0,0 +1,544 @@
1;;; bind-key.el --- A simple way to manage personal keybindings -*- lexical-binding: t; -*-
2
3;; Copyright (c) 2012-2022 Free Software Foundation, Inc.
4
5;; Author: John Wiegley <johnw@newartisans.com>
6;; Maintainer: John Wiegley <johnw@newartisans.com>
7;; Created: 16 Jun 2012
8;; Version: 2.4.1
9;; Package-Requires: ((emacs "24.3"))
10;; Keywords: keys keybinding config dotemacs extensions
11;; URL: https://github.com/jwiegley/use-package
12
13;; This program is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
17
18;; This program is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with this program. If not, see <https://www.gnu.org/licenses/>.
25
26;;; Commentary:
27
28;; If you have lots of keybindings set in your .emacs file, it can be hard to
29;; know which ones you haven't set yet, and which may now be overriding some
30;; new default in a new Emacs version. This module aims to solve that
31;; problem.
32;;
33;; Bind keys as follows in your .emacs:
34;;
35;; (require 'bind-key)
36;;
37;; (bind-key "C-c x" 'my-ctrl-c-x-command)
38;;
39;; If the keybinding argument is a vector, it is passed straight to
40;; `define-key', so remapping a key with `[remap COMMAND]' works as
41;; expected:
42;;
43;; (bind-key [remap original-ctrl-c-x-command] 'my-ctrl-c-x-command)
44;;
45;; If you want the keybinding to override all minor modes that may also bind
46;; the same key, use the `bind-key*' form:
47;;
48;; (bind-key* "<C-return>" 'other-window)
49;;
50;; If you want to rebind a key only in a particular keymap, use:
51;;
52;; (bind-key "C-c x" 'my-ctrl-c-x-command some-other-mode-map)
53;;
54;; To unbind a key within a keymap (for example, to stop your favorite major
55;; mode from changing a binding that you don't want to override everywhere),
56;; use `unbind-key':
57;;
58;; (unbind-key "C-c x" some-other-mode-map)
59;;
60;; To bind multiple keys at once, or set up a prefix map, a `bind-keys' macro
61;; is provided. It accepts keyword arguments, please see its documentation
62;; for a detailed description.
63;;
64;; To add keys into a specific map, use :map argument
65;;
66;; (bind-keys :map dired-mode-map
67;; ("o" . dired-omit-mode)
68;; ("a" . some-custom-dired-function))
69;;
70;; To set up a prefix map, use `:prefix-map' and `:prefix' arguments (both are
71;; required)
72;;
73;; (bind-keys :prefix-map my-customize-prefix-map
74;; :prefix "C-c c"
75;; ("f" . customize-face)
76;; ("v" . customize-variable))
77;;
78;; You can combine all the keywords together. Additionally,
79;; `:prefix-docstring' can be specified to set documentation of created
80;; `:prefix-map' variable.
81;;
82;; To bind multiple keys in a `bind-key*' way (to be sure that your bindings
83;; will not be overridden by other modes), you may use `bind-keys*' macro:
84;;
85;; (bind-keys*
86;; ("C-o" . other-window)
87;; ("C-M-n" . forward-page)
88;; ("C-M-p" . backward-page))
89;;
90;; After Emacs loads, you can see a summary of all your personal keybindings
91;; currently in effect with this command:
92;;
93;; M-x describe-personal-keybindings
94;;
95;; This display will tell you if you've overridden a default keybinding, and
96;; what the default was. Also, it will tell you if the key was rebound after
97;; your binding it with `bind-key', and what it was rebound it to.
98
99;;; Code:
100
101(require 'cl-lib)
102(require 'easy-mmode)
103
104(defgroup bind-key nil
105 "A simple way to manage personal keybindings."
106 :group 'emacs)
107
108(defcustom bind-key-column-widths '(18 . 40)
109 "Width of columns in `describe-personal-keybindings'."
110 :type '(cons integer integer)
111 :group 'bind-key)
112
113(defcustom bind-key-segregation-regexp
114 "\\`\\(\\(C-[chx] \\|M-[gso] \\)\\([CM]-\\)?\\|.+-\\)"
115 "Regular expression used to divide key sets in the output from
116\\[describe-personal-keybindings]."
117 :type 'regexp
118 :group 'bind-key)
119
120(defcustom bind-key-describe-special-forms nil
121 "If non-nil, extract docstrings from lambdas, closures and keymaps if possible."
122 :type 'boolean
123 :group 'bind-key)
124
125;; Create override-global-mode to force key remappings
126
127(defvar override-global-map (make-keymap)
128 "Keymap for `override-global-mode'.")
129
130(define-minor-mode override-global-mode
131 "A minor mode so that keymap settings override other modes."
132 :init-value t
133 :lighter "")
134
135;; the keymaps in `emulation-mode-map-alists' take precedence over
136;; `minor-mode-map-alist'
137(add-to-list 'emulation-mode-map-alists
138 `((override-global-mode . ,override-global-map)))
139
140(defvar personal-keybindings nil
141 "List of bindings performed by `bind-key'.
142
143Elements have the form ((KEY . [MAP]) CMD ORIGINAL-CMD)")
144
145;;;###autoload
146(defmacro bind-key (key-name command &optional keymap predicate)
147 "Bind KEY-NAME to COMMAND in KEYMAP (`global-map' if not passed).
148
149KEY-NAME may be a vector, in which case it is passed straight to
150`define-key'. Or it may be a string to be interpreted as
151spelled-out keystrokes, e.g., `C-c C-z'. See documentation of
152`edmacro-mode' for details.
153
154COMMAND must be an interactive function or lambda form.
155
156KEYMAP, if present, should be a keymap variable or symbol.
157For example:
158
159 (bind-key \"M-h\" #\\='some-interactive-function my-mode-map)
160
161 (bind-key \"M-h\" #\\='some-interactive-function \\='my-mode-map)
162
163If PREDICATE is non-nil, it is a form evaluated to determine when
164a key should be bound. It must return non-nil in such cases.
165Emacs can evaluate this form at any time that it does redisplay
166or operates on menu data structures, so you should write it so it
167can safely be called at any time."
168 (let ((namevar (make-symbol "name"))
169 (keyvar (make-symbol "key"))
170 (kmapvar (make-symbol "kmap"))
171 (kdescvar (make-symbol "kdesc"))
172 (bindingvar (make-symbol "binding")))
173 `(let* ((,namevar ,key-name)
174 (,keyvar ,(if (stringp key-name) (read-kbd-macro key-name)
175 `(if (vectorp ,namevar) ,namevar
176 (read-kbd-macro ,namevar))))
177 (,kmapvar (or (if (and ,keymap (symbolp ,keymap))
178 (symbol-value ,keymap) ,keymap)
179 global-map))
180 (,kdescvar (cons (if (stringp ,namevar) ,namevar
181 (key-description ,namevar))
182 (if (symbolp ,keymap) ,keymap (quote ,keymap))))
183 (,bindingvar (lookup-key ,kmapvar ,keyvar)))
184 (let ((entry (assoc ,kdescvar personal-keybindings))
185 (details (list ,command
186 (unless (numberp ,bindingvar)
187 ,bindingvar))))
188 (if entry
189 (setcdr entry details)
190 (add-to-list 'personal-keybindings (cons ,kdescvar details))))
191 ,(if predicate
192 `(define-key ,kmapvar ,keyvar
193 '(menu-item "" nil :filter (lambda (&optional _)
194 (when ,predicate
195 ,command))))
196 `(define-key ,kmapvar ,keyvar ,command)))))
197
198;;;###autoload
199(defmacro unbind-key (key-name &optional keymap)
200 "Unbind the given KEY-NAME, within the KEYMAP (if specified).
201See `bind-key' for more details."
202 (let ((namevar (make-symbol "name"))
203 (kdescvar (make-symbol "kdesc")))
204 `(let* ((,namevar ,key-name)
205 (,kdescvar (cons (if (stringp ,namevar) ,namevar
206 (key-description ,namevar))
207 (if (symbolp ,keymap) ,keymap (quote ,keymap)))))
208 (bind-key--remove (if (vectorp ,namevar) ,namevar
209 (read-kbd-macro ,namevar))
210 (or (if (and ,keymap (symbolp ,keymap))
211 (symbol-value ,keymap) ,keymap)
212 global-map))
213 (setq personal-keybindings
214 (cl-delete-if (lambda (k) (equal (car k) ,kdescvar))
215 personal-keybindings))
216 nil)))
217
218(defun bind-key--remove (key keymap)
219 "Remove KEY from KEYMAP.
220
221In contrast to `define-key', this function removes the binding from the keymap."
222 (define-key keymap key nil)
223 ;; Split M-key in ESC key
224 (setq key (cl-mapcan (lambda (k)
225 (if (and (integerp k) (/= (logand k ?\M-\0) 0))
226 (list ?\e (logxor k ?\M-\0))
227 (list k)))
228 key))
229 ;; Delete single keys directly
230 (if (= (length key) 1)
231 (delete key keymap)
232 ;; Lookup submap and delete key from there
233 (let* ((prefix (vconcat (butlast key)))
234 (submap (lookup-key keymap prefix)))
235 (unless (keymapp submap)
236 (error "Not a keymap for %s" key))
237 (when (symbolp submap)
238 (setq submap (symbol-function submap)))
239 (delete (last key) submap)
240 ;; Delete submap if it is empty
241 (when (= 1 (length submap))
242 (bind-key--remove prefix keymap)))))
243
244;;;###autoload
245(defmacro bind-key* (key-name command &optional predicate)
246 "Similar to `bind-key', but overrides any mode-specific bindings."
247 `(bind-key ,key-name ,command override-global-map ,predicate))
248
249(defun bind-keys-form (args keymap)
250 "Bind multiple keys at once.
251
252Accepts keyword arguments:
253:map MAP - a keymap into which the keybindings should be
254 added
255:prefix KEY - prefix key for these bindings
256:prefix-map MAP - name of the prefix map that should be created
257 for these bindings
258:prefix-docstring STR - docstring for the prefix-map variable
259:menu-name NAME - optional menu string for prefix map
260:repeat-docstring STR - docstring for the repeat-map variable
261:repeat-map MAP - name of the repeat map that should be created
262 for these bindings. If specified, the
263 `repeat-map' property of each command bound
264 (within the scope of the `:repeat-map' keyword)
265 is set to this map.
266:exit BINDINGS - Within the scope of `:repeat-map' will bind the
267 key in the repeat map, but will not set the
268 `repeat-map' property of the bound command.
269:continue BINDINGS - Within the scope of `:repeat-map' forces the
270 same behaviour as if no special keyword had
271 been used (that is, the command is bound, and
272 it's `repeat-map' property set)
273:filter FORM - optional form to determine when bindings apply
274
275The rest of the arguments are conses of keybinding string and a
276function symbol (unquoted)."
277 (let (map
278 prefix-doc
279 prefix-map
280 prefix
281 repeat-map
282 repeat-doc
283 repeat-type ;; Only used internally
284 filter
285 menu-name
286 pkg)
287
288 ;; Process any initial keyword arguments
289 (let ((cont t)
290 (arg-change-func 'cddr))
291 (while (and cont args)
292 (if (cond ((and (eq :map (car args))
293 (not prefix-map))
294 (setq map (cadr args)))
295 ((eq :prefix-docstring (car args))
296 (setq prefix-doc (cadr args)))
297 ((and (eq :prefix-map (car args))
298 (not (memq map '(global-map
299 override-global-map))))
300 (setq prefix-map (cadr args)))
301 ((eq :repeat-docstring (car args))
302 (setq repeat-doc (cadr args)))
303 ((and (eq :repeat-map (car args))
304 (not (memq map '(global-map
305 override-global-map))))
306 (setq repeat-map (cadr args))
307 (setq map repeat-map))
308 ((eq :continue (car args))
309 (setq repeat-type :continue
310 arg-change-func 'cdr))
311 ((eq :exit (car args))
312 (setq repeat-type :exit
313 arg-change-func 'cdr))
314 ((eq :prefix (car args))
315 (setq prefix (cadr args)))
316 ((eq :filter (car args))
317 (setq filter (cadr args)) t)
318 ((eq :menu-name (car args))
319 (setq menu-name (cadr args)))
320 ((eq :package (car args))
321 (setq pkg (cadr args))))
322 (setq args (funcall arg-change-func args))
323 (setq cont nil))))
324
325 (when (or (and prefix-map (not prefix))
326 (and prefix (not prefix-map)))
327 (error "Both :prefix-map and :prefix must be supplied"))
328
329 (when repeat-type
330 (unless repeat-map
331 (error ":continue and :exit require specifying :repeat-map")))
332
333 (when (and menu-name (not prefix))
334 (error "If :menu-name is supplied, :prefix must be too"))
335
336 (unless map (setq map keymap))
337
338 ;; Process key binding arguments
339 (let (first next)
340 (while args
341 (if (keywordp (car args))
342 (progn
343 (setq next args)
344 (setq args nil))
345 (if first
346 (nconc first (list (car args)))
347 (setq first (list (car args))))
348 (setq args (cdr args))))
349
350 (cl-flet
351 ((wrap (map bindings)
352 (if (and map pkg (not (memq map '(global-map
353 override-global-map))))
354 `((if (boundp ',map)
355 ,(macroexp-progn bindings)
356 (eval-after-load
357 ,(if (symbolp pkg) `',pkg pkg)
358 ',(macroexp-progn bindings))))
359 bindings)))
360
361 (append
362 (when prefix-map
363 `((defvar ,prefix-map)
364 ,@(when prefix-doc `((put ',prefix-map 'variable-documentation ,prefix-doc)))
365 ,@(if menu-name
366 `((define-prefix-command ',prefix-map nil ,menu-name))
367 `((define-prefix-command ',prefix-map)))
368 ,@(if (and map (not (eq map 'global-map)))
369 (wrap map `((bind-key ,prefix ',prefix-map ,map ,filter)))
370 `((bind-key ,prefix ',prefix-map nil ,filter)))))
371 (when repeat-map
372 `((defvar ,repeat-map (make-sparse-keymap)
373 ,@(when repeat-doc `(,repeat-doc)))))
374 (wrap map
375 (cl-mapcan
376 (lambda (form)
377 (let ((fun (and (cdr form) (list 'function (cdr form)))))
378 (if prefix-map
379 `((bind-key ,(car form) ,fun ,prefix-map ,filter))
380 (if (and map (not (eq map 'global-map)))
381 ;; Only needed in this branch, since when
382 ;; repeat-map is non-nil, map is always
383 ;; non-nil
384 `(,@(when (and repeat-map (not (eq repeat-type :exit)))
385 `((put ,fun 'repeat-map ',repeat-map)))
386 (bind-key ,(car form) ,fun ,map ,filter))
387 `((bind-key ,(car form) ,fun nil ,filter))))))
388 first))
389 (when next
390 (bind-keys-form `(,@(when repeat-map `(:repeat-map ,repeat-map))
391 ,@(if pkg
392 (cons :package (cons pkg next))
393 next)) map)))))))
394
395;;;###autoload
396(defmacro bind-keys (&rest args)
397 "Bind multiple keys at once.
398
399Accepts keyword arguments:
400:map MAP - a keymap into which the keybindings should be
401 added
402:prefix KEY - prefix key for these bindings
403:prefix-map MAP - name of the prefix map that should be created
404 for these bindings
405:prefix-docstring STR - docstring for the prefix-map variable
406:menu-name NAME - optional menu string for prefix map
407:repeat-docstring STR - docstring for the repeat-map variable
408:repeat-map MAP - name of the repeat map that should be created
409 for these bindings. If specified, the
410 `repeat-map' property of each command bound
411 (within the scope of the `:repeat-map' keyword)
412 is set to this map.
413:exit BINDINGS - Within the scope of `:repeat-map' will bind the
414 key in the repeat map, but will not set the
415 `repeat-map' property of the bound command.
416:continue BINDINGS - Within the scope of `:repeat-map' forces the
417 same behaviour as if no special keyword had
418 been used (that is, the command is bound, and
419 it's `repeat-map' property set)
420:filter FORM - optional form to determine when bindings apply
421
422The rest of the arguments are conses of keybinding string and a
423function symbol (unquoted)."
424 (macroexp-progn (bind-keys-form args nil)))
425
426;;;###autoload
427(defmacro bind-keys* (&rest args)
428 (macroexp-progn (bind-keys-form args 'override-global-map)))
429
430(defun get-binding-description (elem)
431 (cond
432 ((listp elem)
433 (cond
434 ((memq (car elem) '(lambda function))
435 (if (and bind-key-describe-special-forms
436 (stringp (nth 2 elem)))
437 (nth 2 elem)
438 "#<lambda>"))
439 ((eq 'closure (car elem))
440 (if (and bind-key-describe-special-forms
441 (stringp (nth 3 elem)))
442 (nth 3 elem)
443 "#<closure>"))
444 ((eq 'keymap (car elem))
445 "#<keymap>")
446 (t
447 elem)))
448 ;; must be a symbol, non-symbol keymap case covered above
449 ((and bind-key-describe-special-forms (keymapp elem))
450 (let ((doc (get elem 'variable-documentation)))
451 (if (stringp doc) doc elem)))
452 ((symbolp elem)
453 elem)
454 (t
455 "#<byte-compiled lambda>")))
456
457(defun compare-keybindings (l r)
458 (let* ((regex bind-key-segregation-regexp)
459 (lgroup (and (string-match regex (caar l))
460 (match-string 0 (caar l))))
461 (rgroup (and (string-match regex (caar r))
462 (match-string 0 (caar r))))
463 (lkeymap (cdar l))
464 (rkeymap (cdar r)))
465 (cond
466 ((and (null lkeymap) rkeymap)
467 (cons t t))
468 ((and lkeymap (null rkeymap))
469 (cons nil t))
470 ((and lkeymap rkeymap
471 (not (string= (symbol-name lkeymap) (symbol-name rkeymap))))
472 (cons (string< (symbol-name lkeymap) (symbol-name rkeymap)) t))
473 ((and (null lgroup) rgroup)
474 (cons t t))
475 ((and lgroup (null rgroup))
476 (cons nil t))
477 ((and lgroup rgroup)
478 (if (string= lgroup rgroup)
479 (cons (string< (caar l) (caar r)) nil)
480 (cons (string< lgroup rgroup) t)))
481 (t
482 (cons (string< (caar l) (caar r)) nil)))))
483
484;;;###autoload
485(defun describe-personal-keybindings ()
486 "Display all the personal keybindings defined by `bind-key'."
487 (interactive)
488 (with-output-to-temp-buffer "*Personal Keybindings*"
489 (princ (format (concat "Key name%s Command%s Comments\n%s %s "
490 "---------------------\n")
491 (make-string (- (car bind-key-column-widths) 9) ? )
492 (make-string (- (cdr bind-key-column-widths) 8) ? )
493 (make-string (1- (car bind-key-column-widths)) ?-)
494 (make-string (1- (cdr bind-key-column-widths)) ?-)))
495 (let (last-binding)
496 (dolist (binding
497 (setq personal-keybindings
498 (sort personal-keybindings
499 (lambda (l r)
500 (car (compare-keybindings l r))))))
501
502 (if (not (eq (cdar last-binding) (cdar binding)))
503 (princ (format "\n\n%s: %s\n%s\n\n"
504 (cdar binding) (caar binding)
505 (make-string (+ 21 (car bind-key-column-widths)
506 (cdr bind-key-column-widths)) ?-)))
507 (if (and last-binding
508 (cdr (compare-keybindings last-binding binding)))
509 (princ "\n")))
510
511 (let* ((key-name (caar binding))
512 (at-present (lookup-key (or (symbol-value (cdar binding))
513 (current-global-map))
514 (read-kbd-macro key-name)))
515 (command (nth 1 binding))
516 (was-command (nth 2 binding))
517 (command-desc (get-binding-description command))
518 (was-command-desc (and was-command
519 (get-binding-description was-command)))
520 (at-present-desc (get-binding-description at-present)))
521 (let ((line
522 (format
523 (format "%%-%ds%%-%ds%%s\n" (car bind-key-column-widths)
524 (cdr bind-key-column-widths))
525 key-name (format "`%s\'" command-desc)
526 (if (string= command-desc at-present-desc)
527 (if (or (null was-command)
528 (string= command-desc was-command-desc))
529 ""
530 (format "was `%s\'" was-command-desc))
531 (format "[now: `%s\']" at-present)))))
532 (princ (if (string-match "[ \t]+\n" line)
533 (replace-match "\n" t t line)
534 line))))
535
536 (setq last-binding binding)))))
537
538(provide 'bind-key)
539
540;; Local Variables:
541;; outline-regexp: ";;;\\(;* [^\s\t\n]\\|###autoload\\)\\|("
542;; End:
543
544;;; bind-key.el ends here