2020-06-21 17:46:12 +00:00
|
|
|
;;; bind-key.el --- A simple way to manage personal keybindings -*- lexical-binding: t; -*-
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2022-10-25 14:32:35 +00:00
|
|
|
;; Copyright (c) 2012-2022 Free Software Foundation, Inc.
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2017-11-30 00:37:55 +00:00
|
|
|
;; Author: John Wiegley <johnw@newartisans.com>
|
|
|
|
;; Maintainer: John Wiegley <johnw@newartisans.com>
|
2015-03-21 08:57:09 +00:00
|
|
|
;; Created: 16 Jun 2012
|
2022-10-28 05:07:18 +00:00
|
|
|
;; Version: 2.4.1
|
2022-11-17 11:05:58 +00:00
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2022-11-17 11:06:22 +00:00
|
|
|
;; Keywords: keys keybinding config dotemacs extensions
|
2015-03-21 08:57:09 +00:00
|
|
|
;; URL: https://github.com/jwiegley/use-package
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2022-11-16 07:37:27 +00:00
|
|
|
;; 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.
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2022-11-16 07:37:27 +00:00
|
|
|
;; 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 <https://www.gnu.org/licenses/>.
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2015-03-21 08:57:09 +00:00
|
|
|
;;; Commentary:
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2015-03-21 08:57:09 +00:00
|
|
|
;; If you have lots of keybindings set in your .emacs file, it can be hard to
|
2012-06-17 09:40:25 +00:00
|
|
|
;; know which ones you haven't set yet, and which may now be overriding some
|
2022-11-14 01:15:19 +00:00
|
|
|
;; new default in a new Emacs version. This module aims to solve that
|
2012-06-17 09:40:25 +00:00
|
|
|
;; problem.
|
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; Bind keys as follows in your .emacs:
|
2012-06-17 09:40:25 +00:00
|
|
|
;;
|
|
|
|
;; (require 'bind-key)
|
|
|
|
;;
|
2016-02-06 02:40:34 +00:00
|
|
|
;; (bind-key "C-c x" 'my-ctrl-c-x-command)
|
2012-06-17 09:40:25 +00:00
|
|
|
;;
|
2018-04-30 16:54:12 +00:00
|
|
|
;; If the keybinding argument is a vector, it is passed straight to
|
|
|
|
;; `define-key', so remapping a key with `[remap COMMAND]' works as
|
|
|
|
;; expected:
|
|
|
|
;;
|
|
|
|
;; (bind-key [remap original-ctrl-c-x-command] 'my-ctrl-c-x-command)
|
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; If you want the keybinding to override all minor modes that may also bind
|
2012-06-17 09:40:25 +00:00
|
|
|
;; the same key, use the `bind-key*' form:
|
|
|
|
;;
|
2016-02-06 02:40:34 +00:00
|
|
|
;; (bind-key* "<C-return>" 'other-window)
|
2012-06-17 09:40:25 +00:00
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; If you want to rebind a key only in a particular keymap, use:
|
2012-06-17 09:40:25 +00:00
|
|
|
;;
|
2016-02-06 02:40:34 +00:00
|
|
|
;; (bind-key "C-c x" 'my-ctrl-c-x-command some-other-mode-map)
|
2012-06-17 09:40:25 +00:00
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; To unbind a key within a keymap (for example, to stop your favorite major
|
2012-06-17 09:40:25 +00:00
|
|
|
;; mode from changing a binding that you don't want to override everywhere),
|
|
|
|
;; use `unbind-key':
|
|
|
|
;;
|
2016-02-06 02:40:34 +00:00
|
|
|
;; (unbind-key "C-c x" some-other-mode-map)
|
2012-06-17 09:40:25 +00:00
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; To bind multiple keys at once, or set up a prefix map, a `bind-keys' macro
|
|
|
|
;; is provided. It accepts keyword arguments, please see its documentation
|
|
|
|
;; for a detailed description.
|
2014-02-13 11:55:17 +00:00
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; To add keys into a specific map, use :map argument
|
2014-02-13 11:55:17 +00:00
|
|
|
;;
|
|
|
|
;; (bind-keys :map dired-mode-map
|
|
|
|
;; ("o" . dired-omit-mode)
|
|
|
|
;; ("a" . some-custom-dired-function))
|
|
|
|
;;
|
2015-03-21 08:57:09 +00:00
|
|
|
;; To set up a prefix map, use `:prefix-map' and `:prefix' arguments (both are
|
2015-03-21 08:30:04 +00:00
|
|
|
;; required)
|
2014-02-13 11:55:17 +00:00
|
|
|
;;
|
|
|
|
;; (bind-keys :prefix-map my-customize-prefix-map
|
|
|
|
;; :prefix "C-c c"
|
|
|
|
;; ("f" . customize-face)
|
|
|
|
;; ("v" . customize-variable))
|
|
|
|
;;
|
2015-03-21 08:30:04 +00:00
|
|
|
;; You can combine all the keywords together. Additionally,
|
|
|
|
;; `:prefix-docstring' can be specified to set documentation of created
|
|
|
|
;; `:prefix-map' variable.
|
2014-02-13 11:55:17 +00:00
|
|
|
;;
|
2014-05-25 09:04:00 +00:00
|
|
|
;; To bind multiple keys in a `bind-key*' way (to be sure that your bindings
|
|
|
|
;; will not be overridden by other modes), you may use `bind-keys*' macro:
|
|
|
|
;;
|
|
|
|
;; (bind-keys*
|
|
|
|
;; ("C-o" . other-window)
|
|
|
|
;; ("C-M-n" . forward-page)
|
|
|
|
;; ("C-M-p" . backward-page))
|
|
|
|
;;
|
2012-06-17 09:40:25 +00:00
|
|
|
;; After Emacs loads, you can see a summary of all your personal keybindings
|
|
|
|
;; currently in effect with this command:
|
|
|
|
;;
|
|
|
|
;; M-x describe-personal-keybindings
|
|
|
|
;;
|
2019-11-06 12:49:46 +00:00
|
|
|
;; This display will tell you if you've overridden a default keybinding, and
|
2012-06-17 09:40:25 +00:00
|
|
|
;; what the default was. Also, it will tell you if the key was rebound after
|
|
|
|
;; your binding it with `bind-key', and what it was rebound it to.
|
|
|
|
|
2016-12-17 14:26:15 +00:00
|
|
|
;;; Code:
|
|
|
|
|
2015-03-21 09:13:02 +00:00
|
|
|
(require 'cl-lib)
|
2012-06-17 09:40:25 +00:00
|
|
|
(require 'easy-mmode)
|
|
|
|
|
|
|
|
(defgroup bind-key nil
|
2022-11-14 01:15:19 +00:00
|
|
|
"A simple way to manage personal keybindings."
|
2012-06-17 09:40:25 +00:00
|
|
|
:group 'emacs)
|
|
|
|
|
2014-02-18 12:52:25 +00:00
|
|
|
(defcustom bind-key-column-widths '(18 . 40)
|
|
|
|
"Width of columns in `describe-personal-keybindings'."
|
|
|
|
:type '(cons integer integer)
|
|
|
|
:group 'bind-key)
|
|
|
|
|
2012-06-17 09:40:25 +00:00
|
|
|
(defcustom bind-key-segregation-regexp
|
|
|
|
"\\`\\(\\(C-[chx] \\|M-[gso] \\)\\([CM]-\\)?\\|.+-\\)"
|
|
|
|
"Regular expression used to divide key sets in the output from
|
|
|
|
\\[describe-personal-keybindings]."
|
|
|
|
:type 'regexp
|
|
|
|
:group 'bind-key)
|
|
|
|
|
2014-02-18 12:40:25 +00:00
|
|
|
(defcustom bind-key-describe-special-forms nil
|
|
|
|
"If non-nil, extract docstrings from lambdas, closures and keymaps if possible."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'bind-key)
|
|
|
|
|
2012-06-17 09:40:25 +00:00
|
|
|
;; Create override-global-mode to force key remappings
|
|
|
|
|
|
|
|
(defvar override-global-map (make-keymap)
|
2022-11-14 01:15:19 +00:00
|
|
|
"Keymap for `override-global-mode'.")
|
2012-06-17 09:40:25 +00:00
|
|
|
|
|
|
|
(define-minor-mode override-global-mode
|
|
|
|
"A minor mode so that keymap settings override other modes."
|
2022-08-10 08:59:44 +00:00
|
|
|
:init-value t
|
2021-05-18 13:44:34 +00:00
|
|
|
:lighter "")
|
2013-04-13 20:09:27 +00:00
|
|
|
|
|
|
|
;; the keymaps in `emulation-mode-map-alists' take precedence over
|
|
|
|
;; `minor-mode-map-alist'
|
|
|
|
(add-to-list 'emulation-mode-map-alists
|
|
|
|
`((override-global-mode . ,override-global-map)))
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2014-05-31 19:03:17 +00:00
|
|
|
(defvar personal-keybindings nil
|
|
|
|
"List of bindings performed by `bind-key'.
|
|
|
|
|
|
|
|
Elements have the form ((KEY . [MAP]) CMD ORIGINAL-CMD)")
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2015-03-21 08:50:51 +00:00
|
|
|
;;;###autoload
|
2016-01-12 06:38:31 +00:00
|
|
|
(defmacro bind-key (key-name command &optional keymap predicate)
|
2014-03-02 01:16:59 +00:00
|
|
|
"Bind KEY-NAME to COMMAND in KEYMAP (`global-map' if not passed).
|
|
|
|
|
2014-05-31 19:03:17 +00:00
|
|
|
KEY-NAME may be a vector, in which case it is passed straight to
|
2014-03-02 01:16:59 +00:00
|
|
|
`define-key'. Or it may be a string to be interpreted as
|
2022-11-14 01:15:19 +00:00
|
|
|
spelled-out keystrokes, e.g., `C-c C-z'. See documentation of
|
2016-01-12 06:38:31 +00:00
|
|
|
`edmacro-mode' for details.
|
|
|
|
|
2017-12-04 23:55:41 +00:00
|
|
|
COMMAND must be an interactive function or lambda form.
|
|
|
|
|
2020-06-09 04:26:09 +00:00
|
|
|
KEYMAP, if present, should be a keymap variable or symbol.
|
2017-12-04 23:55:41 +00:00
|
|
|
For example:
|
|
|
|
|
2022-09-10 16:16:18 +00:00
|
|
|
(bind-key \"M-h\" #\\='some-interactive-function my-mode-map)
|
2017-12-04 23:55:41 +00:00
|
|
|
|
2022-09-10 16:16:18 +00:00
|
|
|
(bind-key \"M-h\" #\\='some-interactive-function \\='my-mode-map)
|
2020-06-09 04:26:09 +00:00
|
|
|
|
2016-01-12 06:38:31 +00:00
|
|
|
If PREDICATE is non-nil, it is a form evaluated to determine when
|
|
|
|
a key should be bound. It must return non-nil in such cases.
|
|
|
|
Emacs can evaluate this form at any time that it does redisplay
|
|
|
|
or operates on menu data structures, so you should write it so it
|
|
|
|
can safely be called at any time."
|
2012-06-17 09:40:25 +00:00
|
|
|
(let ((namevar (make-symbol "name"))
|
|
|
|
(keyvar (make-symbol "key"))
|
2021-02-10 13:01:59 +00:00
|
|
|
(kmapvar (make-symbol "kmap"))
|
2014-05-31 19:21:44 +00:00
|
|
|
(kdescvar (make-symbol "kdesc"))
|
2015-03-21 09:13:02 +00:00
|
|
|
(bindingvar (make-symbol "binding")))
|
2014-04-14 17:56:54 +00:00
|
|
|
`(let* ((,namevar ,key-name)
|
2020-03-20 13:25:35 +00:00
|
|
|
(,keyvar ,(if (stringp key-name) (read-kbd-macro key-name)
|
|
|
|
`(if (vectorp ,namevar) ,namevar
|
|
|
|
(read-kbd-macro ,namevar))))
|
2021-02-10 13:01:59 +00:00
|
|
|
(,kmapvar (or (if (and ,keymap (symbolp ,keymap))
|
|
|
|
(symbol-value ,keymap) ,keymap)
|
|
|
|
global-map))
|
2014-05-31 19:21:44 +00:00
|
|
|
(,kdescvar (cons (if (stringp ,namevar) ,namevar
|
|
|
|
(key-description ,namevar))
|
2020-06-09 04:26:09 +00:00
|
|
|
(if (symbolp ,keymap) ,keymap (quote ,keymap))))
|
2021-02-10 13:01:59 +00:00
|
|
|
(,bindingvar (lookup-key ,kmapvar ,keyvar)))
|
2017-11-29 04:52:29 +00:00
|
|
|
(let ((entry (assoc ,kdescvar personal-keybindings))
|
|
|
|
(details (list ,command
|
|
|
|
(unless (numberp ,bindingvar)
|
|
|
|
,bindingvar))))
|
|
|
|
(if entry
|
|
|
|
(setcdr entry details)
|
|
|
|
(add-to-list 'personal-keybindings (cons ,kdescvar details))))
|
2016-01-12 06:38:31 +00:00
|
|
|
,(if predicate
|
2021-02-10 13:01:59 +00:00
|
|
|
`(define-key ,kmapvar ,keyvar
|
2016-01-12 06:38:31 +00:00
|
|
|
'(menu-item "" nil :filter (lambda (&optional _)
|
|
|
|
(when ,predicate
|
|
|
|
,command))))
|
2021-02-10 13:01:59 +00:00
|
|
|
`(define-key ,kmapvar ,keyvar ,command)))))
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2015-03-21 08:50:51 +00:00
|
|
|
;;;###autoload
|
2012-06-17 09:40:25 +00:00
|
|
|
(defmacro unbind-key (key-name &optional keymap)
|
2016-01-12 06:38:31 +00:00
|
|
|
"Unbind the given KEY-NAME, within the KEYMAP (if specified).
|
|
|
|
See `bind-key' for more details."
|
2021-02-10 13:13:36 +00:00
|
|
|
(let ((namevar (make-symbol "name"))
|
|
|
|
(kdescvar (make-symbol "kdesc")))
|
|
|
|
`(let* ((,namevar ,key-name)
|
|
|
|
(,kdescvar (cons (if (stringp ,namevar) ,namevar
|
|
|
|
(key-description ,namevar))
|
|
|
|
(if (symbolp ,keymap) ,keymap (quote ,keymap)))))
|
|
|
|
(bind-key--remove (if (vectorp ,namevar) ,namevar
|
|
|
|
(read-kbd-macro ,namevar))
|
|
|
|
(or (if (and ,keymap (symbolp ,keymap))
|
|
|
|
(symbol-value ,keymap) ,keymap)
|
|
|
|
global-map))
|
|
|
|
(setq personal-keybindings
|
|
|
|
(cl-delete-if (lambda (k) (equal (car k) ,kdescvar))
|
|
|
|
personal-keybindings))
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
(defun bind-key--remove (key keymap)
|
|
|
|
"Remove KEY from KEYMAP.
|
|
|
|
|
|
|
|
In contrast to `define-key', this function removes the binding from the keymap."
|
|
|
|
(define-key keymap key nil)
|
|
|
|
;; Split M-key in ESC key
|
2022-11-13 22:16:05 +00:00
|
|
|
(setq key (cl-mapcan (lambda (k)
|
|
|
|
(if (and (integerp k) (/= (logand k ?\M-\0) 0))
|
|
|
|
(list ?\e (logxor k ?\M-\0))
|
|
|
|
(list k)))
|
|
|
|
key))
|
2021-02-10 13:13:36 +00:00
|
|
|
;; Delete single keys directly
|
|
|
|
(if (= (length key) 1)
|
|
|
|
(delete key keymap)
|
|
|
|
;; Lookup submap and delete key from there
|
|
|
|
(let* ((prefix (vconcat (butlast key)))
|
|
|
|
(submap (lookup-key keymap prefix)))
|
|
|
|
(unless (keymapp submap)
|
|
|
|
(error "Not a keymap for %s" key))
|
|
|
|
(when (symbolp submap)
|
|
|
|
(setq submap (symbol-function submap)))
|
|
|
|
(delete (last key) submap)
|
|
|
|
;; Delete submap if it is empty
|
|
|
|
(when (= 1 (length submap))
|
2022-11-13 22:16:05 +00:00
|
|
|
(bind-key--remove prefix keymap)))))
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2015-03-21 08:50:51 +00:00
|
|
|
;;;###autoload
|
2016-01-12 06:38:31 +00:00
|
|
|
(defmacro bind-key* (key-name command &optional predicate)
|
|
|
|
"Similar to `bind-key', but overrides any mode-specific bindings."
|
2016-01-12 15:58:06 +00:00
|
|
|
`(bind-key ,key-name ,command override-global-map ,predicate))
|
2012-06-17 09:40:25 +00:00
|
|
|
|
2022-11-15 23:24:12 +00:00
|
|
|
(defun bind-keys-form (args keymap)
|
2014-02-13 11:55:17 +00:00
|
|
|
"Bind multiple keys at once.
|
|
|
|
|
|
|
|
Accepts keyword arguments:
|
2016-01-12 06:38:31 +00:00
|
|
|
:map MAP - a keymap into which the keybindings should be
|
2022-11-15 23:24:12 +00:00
|
|
|
added
|
2016-01-12 06:38:31 +00:00
|
|
|
:prefix KEY - prefix key for these bindings
|
|
|
|
:prefix-map MAP - name of the prefix map that should be created
|
|
|
|
for these bindings
|
|
|
|
:prefix-docstring STR - docstring for the prefix-map variable
|
|
|
|
:menu-name NAME - optional menu string for prefix map
|
2022-01-16 00:21:36 +00:00
|
|
|
:repeat-docstring STR - docstring for the repeat-map variable
|
|
|
|
:repeat-map MAP - name of the repeat map that should be created
|
|
|
|
for these bindings. If specified, the
|
2022-08-08 11:33:17 +00:00
|
|
|
`repeat-map' property of each command bound
|
|
|
|
(within the scope of the `:repeat-map' keyword)
|
2022-01-16 00:21:36 +00:00
|
|
|
is set to this map.
|
2022-08-08 11:33:17 +00:00
|
|
|
:exit BINDINGS - Within the scope of `:repeat-map' will bind the
|
2022-01-17 15:41:35 +00:00
|
|
|
key in the repeat map, but will not set the
|
2022-08-08 11:33:17 +00:00
|
|
|
`repeat-map' property of the bound command.
|
|
|
|
:continue BINDINGS - Within the scope of `:repeat-map' forces the
|
2022-01-20 10:59:37 +00:00
|
|
|
same behaviour as if no special keyword had
|
|
|
|
been used (that is, the command is bound, and
|
2022-08-08 11:33:17 +00:00
|
|
|
it's `repeat-map' property set)
|
2016-01-12 06:38:31 +00:00
|
|
|
:filter FORM - optional form to determine when bindings apply
|
2014-02-13 11:55:17 +00:00
|
|
|
|
|
|
|
The rest of the arguments are conses of keybinding string and a
|
|
|
|
function symbol (unquoted)."
|
2022-11-15 23:24:12 +00:00
|
|
|
(let (map
|
2022-01-16 00:21:36 +00:00
|
|
|
prefix-doc
|
2017-12-04 23:21:41 +00:00
|
|
|
prefix-map
|
|
|
|
prefix
|
2022-01-16 00:21:36 +00:00
|
|
|
repeat-map
|
|
|
|
repeat-doc
|
2022-01-17 15:41:35 +00:00
|
|
|
repeat-type ;; Only used internally
|
2017-12-04 23:21:41 +00:00
|
|
|
filter
|
|
|
|
menu-name
|
|
|
|
pkg)
|
|
|
|
|
|
|
|
;; Process any initial keyword arguments
|
2022-01-17 15:41:35 +00:00
|
|
|
(let ((cont t)
|
|
|
|
(arg-change-func 'cddr))
|
2017-12-04 23:21:41 +00:00
|
|
|
(while (and cont args)
|
2017-12-11 05:25:37 +00:00
|
|
|
(if (cond ((and (eq :map (car args))
|
|
|
|
(not prefix-map))
|
2022-11-15 23:24:12 +00:00
|
|
|
(setq map (cadr args)))
|
2017-12-05 19:10:16 +00:00
|
|
|
((eq :prefix-docstring (car args))
|
2022-01-16 00:21:36 +00:00
|
|
|
(setq prefix-doc (cadr args)))
|
2022-11-15 23:24:12 +00:00
|
|
|
((and (eq :prefix-map (car args))
|
|
|
|
(not (memq map '(global-map
|
|
|
|
override-global-map))))
|
2017-12-05 19:10:16 +00:00
|
|
|
(setq prefix-map (cadr args)))
|
2022-01-16 00:21:36 +00:00
|
|
|
((eq :repeat-docstring (car args))
|
|
|
|
(setq repeat-doc (cadr args)))
|
2022-11-15 23:24:12 +00:00
|
|
|
((and (eq :repeat-map (car args))
|
|
|
|
(not (memq map '(global-map
|
|
|
|
override-global-map))))
|
2022-01-16 00:21:36 +00:00
|
|
|
(setq repeat-map (cadr args))
|
2022-11-15 23:24:12 +00:00
|
|
|
(setq map repeat-map))
|
2022-01-20 10:59:37 +00:00
|
|
|
((eq :continue (car args))
|
|
|
|
(setq repeat-type :continue
|
|
|
|
arg-change-func 'cdr))
|
2022-01-17 15:41:35 +00:00
|
|
|
((eq :exit (car args))
|
|
|
|
(setq repeat-type :exit
|
|
|
|
arg-change-func 'cdr))
|
2017-12-05 19:10:16 +00:00
|
|
|
((eq :prefix (car args))
|
|
|
|
(setq prefix (cadr args)))
|
|
|
|
((eq :filter (car args))
|
|
|
|
(setq filter (cadr args)) t)
|
|
|
|
((eq :menu-name (car args))
|
|
|
|
(setq menu-name (cadr args)))
|
|
|
|
((eq :package (car args))
|
|
|
|
(setq pkg (cadr args))))
|
2022-01-17 15:41:35 +00:00
|
|
|
(setq args (funcall arg-change-func args))
|
2017-12-04 23:21:41 +00:00
|
|
|
(setq cont nil))))
|
|
|
|
|
2015-03-21 08:46:26 +00:00
|
|
|
(when (or (and prefix-map (not prefix))
|
|
|
|
(and prefix (not prefix-map)))
|
2014-02-13 11:55:17 +00:00
|
|
|
(error "Both :prefix-map and :prefix must be supplied"))
|
2017-12-04 23:21:41 +00:00
|
|
|
|
2022-01-28 22:26:41 +00:00
|
|
|
(when repeat-type
|
|
|
|
(unless repeat-map
|
|
|
|
(error ":continue and :exit require specifying :repeat-map")))
|
|
|
|
|
2014-06-20 04:45:51 +00:00
|
|
|
(when (and menu-name (not prefix))
|
|
|
|
(error "If :menu-name is supplied, :prefix must be too"))
|
2017-12-04 23:21:41 +00:00
|
|
|
|
2022-11-15 23:24:12 +00:00
|
|
|
(unless map (setq map keymap))
|
2017-12-11 05:25:37 +00:00
|
|
|
|
2017-12-04 23:21:41 +00:00
|
|
|
;; Process key binding arguments
|
|
|
|
(let (first next)
|
2016-02-25 23:57:50 +00:00
|
|
|
(while args
|
|
|
|
(if (keywordp (car args))
|
|
|
|
(progn
|
|
|
|
(setq next args)
|
|
|
|
(setq args nil))
|
|
|
|
(if first
|
|
|
|
(nconc first (list (car args)))
|
|
|
|
(setq first (list (car args))))
|
|
|
|
(setq args (cdr args))))
|
2017-12-04 23:21:41 +00:00
|
|
|
|
2022-11-15 23:24:12 +00:00
|
|
|
(cl-flet
|
|
|
|
((wrap (map bindings)
|
|
|
|
(if (and map pkg (not (memq map '(global-map
|
|
|
|
override-global-map))))
|
|
|
|
`((if (boundp ',map)
|
2017-12-11 05:25:37 +00:00
|
|
|
,(macroexp-progn bindings)
|
2016-12-18 14:47:36 +00:00
|
|
|
(eval-after-load
|
2016-02-27 08:48:29 +00:00
|
|
|
,(if (symbolp pkg) `',pkg pkg)
|
2017-12-11 05:25:37 +00:00
|
|
|
',(macroexp-progn bindings))))
|
2016-02-27 08:48:29 +00:00
|
|
|
bindings)))
|
2022-11-15 23:24:12 +00:00
|
|
|
|
2016-02-26 00:37:34 +00:00
|
|
|
(append
|
2022-11-15 23:24:12 +00:00
|
|
|
(when prefix-map
|
|
|
|
`((defvar ,prefix-map)
|
|
|
|
,@(when prefix-doc `((put ',prefix-map 'variable-documentation ,prefix-doc)))
|
|
|
|
,@(if menu-name
|
|
|
|
`((define-prefix-command ',prefix-map nil ,menu-name))
|
|
|
|
`((define-prefix-command ',prefix-map)))
|
|
|
|
,@(if (and map (not (eq map 'global-map)))
|
|
|
|
(wrap map `((bind-key ,prefix ',prefix-map ,map ,filter)))
|
|
|
|
`((bind-key ,prefix ',prefix-map nil ,filter)))))
|
2022-01-16 00:21:36 +00:00
|
|
|
(when repeat-map
|
|
|
|
`((defvar ,repeat-map (make-sparse-keymap)
|
|
|
|
,@(when repeat-doc `(,repeat-doc)))))
|
2022-11-15 23:24:12 +00:00
|
|
|
(wrap map
|
|
|
|
(cl-mapcan
|
|
|
|
(lambda (form)
|
|
|
|
(let ((fun (and (cdr form) (list 'function (cdr form)))))
|
|
|
|
(if prefix-map
|
|
|
|
`((bind-key ,(car form) ,fun ,prefix-map ,filter))
|
|
|
|
(if (and map (not (eq map 'global-map)))
|
|
|
|
;; Only needed in this branch, since when
|
|
|
|
;; repeat-map is non-nil, map is always
|
|
|
|
;; non-nil
|
|
|
|
`(,@(when (and repeat-map (not (eq repeat-type :exit)))
|
|
|
|
`((put ,fun 'repeat-map ',repeat-map)))
|
|
|
|
(bind-key ,(car form) ,fun ,map ,filter))
|
|
|
|
`((bind-key ,(car form) ,fun nil ,filter))))))
|
|
|
|
first))
|
2016-02-26 00:37:34 +00:00
|
|
|
(when next
|
2022-11-15 23:24:12 +00:00
|
|
|
(bind-keys-form `(,@(when repeat-map `(:repeat-map ,repeat-map))
|
|
|
|
,@(if pkg
|
|
|
|
(cons :package (cons pkg next))
|
|
|
|
next)) map)))))))
|
2016-02-25 23:57:50 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defmacro bind-keys (&rest args)
|
|
|
|
"Bind multiple keys at once.
|
|
|
|
|
|
|
|
Accepts keyword arguments:
|
|
|
|
:map MAP - a keymap into which the keybindings should be
|
2022-11-15 23:24:12 +00:00
|
|
|
added
|
2016-02-25 23:57:50 +00:00
|
|
|
:prefix KEY - prefix key for these bindings
|
|
|
|
:prefix-map MAP - name of the prefix map that should be created
|
|
|
|
for these bindings
|
|
|
|
:prefix-docstring STR - docstring for the prefix-map variable
|
|
|
|
:menu-name NAME - optional menu string for prefix map
|
2022-01-16 00:21:36 +00:00
|
|
|
:repeat-docstring STR - docstring for the repeat-map variable
|
|
|
|
:repeat-map MAP - name of the repeat map that should be created
|
|
|
|
for these bindings. If specified, the
|
2022-08-08 11:33:17 +00:00
|
|
|
`repeat-map' property of each command bound
|
|
|
|
(within the scope of the `:repeat-map' keyword)
|
2022-01-16 00:21:36 +00:00
|
|
|
is set to this map.
|
2022-08-08 11:33:17 +00:00
|
|
|
:exit BINDINGS - Within the scope of `:repeat-map' will bind the
|
2022-01-17 15:41:35 +00:00
|
|
|
key in the repeat map, but will not set the
|
2022-08-08 11:33:17 +00:00
|
|
|
`repeat-map' property of the bound command.
|
|
|
|
:continue BINDINGS - Within the scope of `:repeat-map' forces the
|
2022-01-20 10:59:37 +00:00
|
|
|
same behaviour as if no special keyword had
|
|
|
|
been used (that is, the command is bound, and
|
2022-08-08 11:33:17 +00:00
|
|
|
it's `repeat-map' property set)
|
2016-02-25 23:57:50 +00:00
|
|
|
:filter FORM - optional form to determine when bindings apply
|
|
|
|
|
|
|
|
The rest of the arguments are conses of keybinding string and a
|
|
|
|
function symbol (unquoted)."
|
2017-12-04 23:21:41 +00:00
|
|
|
(macroexp-progn (bind-keys-form args nil)))
|
2014-02-13 11:55:17 +00:00
|
|
|
|
2015-03-21 08:50:51 +00:00
|
|
|
;;;###autoload
|
2014-05-25 09:04:00 +00:00
|
|
|
(defmacro bind-keys* (&rest args)
|
2017-12-05 18:29:04 +00:00
|
|
|
(macroexp-progn (bind-keys-form args 'override-global-map)))
|
2014-05-25 09:04:00 +00:00
|
|
|
|
2012-06-17 09:40:25 +00:00
|
|
|
(defun get-binding-description (elem)
|
|
|
|
(cond
|
|
|
|
((listp elem)
|
|
|
|
(cond
|
2017-11-29 22:41:12 +00:00
|
|
|
((memq (car elem) '(lambda function))
|
2014-02-18 12:40:25 +00:00
|
|
|
(if (and bind-key-describe-special-forms
|
|
|
|
(stringp (nth 2 elem)))
|
|
|
|
(nth 2 elem)
|
|
|
|
"#<lambda>"))
|
2012-06-17 09:40:25 +00:00
|
|
|
((eq 'closure (car elem))
|
2014-02-18 12:40:25 +00:00
|
|
|
(if (and bind-key-describe-special-forms
|
|
|
|
(stringp (nth 3 elem)))
|
|
|
|
(nth 3 elem)
|
|
|
|
"#<closure>"))
|
2012-06-17 09:40:25 +00:00
|
|
|
((eq 'keymap (car elem))
|
|
|
|
"#<keymap>")
|
|
|
|
(t
|
|
|
|
elem)))
|
2015-03-19 15:25:18 +00:00
|
|
|
;; must be a symbol, non-symbol keymap case covered above
|
|
|
|
((and bind-key-describe-special-forms (keymapp elem))
|
2015-06-22 16:43:55 +00:00
|
|
|
(let ((doc (get elem 'variable-documentation)))
|
|
|
|
(if (stringp doc) doc elem)))
|
2012-06-17 09:40:25 +00:00
|
|
|
((symbolp elem)
|
|
|
|
elem)
|
|
|
|
(t
|
|
|
|
"#<byte-compiled lambda>")))
|
|
|
|
|
|
|
|
(defun compare-keybindings (l r)
|
|
|
|
(let* ((regex bind-key-segregation-regexp)
|
|
|
|
(lgroup (and (string-match regex (caar l))
|
|
|
|
(match-string 0 (caar l))))
|
|
|
|
(rgroup (and (string-match regex (caar r))
|
|
|
|
(match-string 0 (caar r))))
|
|
|
|
(lkeymap (cdar l))
|
|
|
|
(rkeymap (cdar r)))
|
|
|
|
(cond
|
|
|
|
((and (null lkeymap) rkeymap)
|
|
|
|
(cons t t))
|
|
|
|
((and lkeymap (null rkeymap))
|
|
|
|
(cons nil t))
|
|
|
|
((and lkeymap rkeymap
|
|
|
|
(not (string= (symbol-name lkeymap) (symbol-name rkeymap))))
|
|
|
|
(cons (string< (symbol-name lkeymap) (symbol-name rkeymap)) t))
|
|
|
|
((and (null lgroup) rgroup)
|
|
|
|
(cons t t))
|
|
|
|
((and lgroup (null rgroup))
|
|
|
|
(cons nil t))
|
|
|
|
((and lgroup rgroup)
|
|
|
|
(if (string= lgroup rgroup)
|
|
|
|
(cons (string< (caar l) (caar r)) nil)
|
|
|
|
(cons (string< lgroup rgroup) t)))
|
|
|
|
(t
|
|
|
|
(cons (string< (caar l) (caar r)) nil)))))
|
|
|
|
|
2015-03-21 08:50:51 +00:00
|
|
|
;;;###autoload
|
2012-06-17 09:40:25 +00:00
|
|
|
(defun describe-personal-keybindings ()
|
2013-09-04 14:40:55 +00:00
|
|
|
"Display all the personal keybindings defined by `bind-key'."
|
2012-06-17 09:40:25 +00:00
|
|
|
(interactive)
|
2013-09-04 14:40:55 +00:00
|
|
|
(with-output-to-temp-buffer "*Personal Keybindings*"
|
2015-03-21 09:13:02 +00:00
|
|
|
(princ (format (concat "Key name%s Command%s Comments\n%s %s "
|
|
|
|
"---------------------\n")
|
2014-02-18 12:52:25 +00:00
|
|
|
(make-string (- (car bind-key-column-widths) 9) ? )
|
|
|
|
(make-string (- (cdr bind-key-column-widths) 8) ? )
|
|
|
|
(make-string (1- (car bind-key-column-widths)) ?-)
|
|
|
|
(make-string (1- (cdr bind-key-column-widths)) ?-)))
|
2012-06-17 09:40:25 +00:00
|
|
|
(let (last-binding)
|
|
|
|
(dolist (binding
|
|
|
|
(setq personal-keybindings
|
|
|
|
(sort personal-keybindings
|
2014-12-20 04:12:47 +00:00
|
|
|
(lambda (l r)
|
|
|
|
(car (compare-keybindings l r))))))
|
2015-03-21 08:30:52 +00:00
|
|
|
|
2012-06-17 09:40:25 +00:00
|
|
|
(if (not (eq (cdar last-binding) (cdar binding)))
|
2017-11-29 04:58:04 +00:00
|
|
|
(princ (format "\n\n%s: %s\n%s\n\n"
|
|
|
|
(cdar binding) (caar binding)
|
2015-03-21 09:13:02 +00:00
|
|
|
(make-string (+ 21 (car bind-key-column-widths)
|
|
|
|
(cdr bind-key-column-widths)) ?-)))
|
2012-06-17 09:40:25 +00:00
|
|
|
(if (and last-binding
|
|
|
|
(cdr (compare-keybindings last-binding binding)))
|
2013-09-04 14:40:55 +00:00
|
|
|
(princ "\n")))
|
2015-03-21 08:30:52 +00:00
|
|
|
|
2012-06-17 09:40:25 +00:00
|
|
|
(let* ((key-name (caar binding))
|
|
|
|
(at-present (lookup-key (or (symbol-value (cdar binding))
|
|
|
|
(current-global-map))
|
|
|
|
(read-kbd-macro key-name)))
|
|
|
|
(command (nth 1 binding))
|
|
|
|
(was-command (nth 2 binding))
|
|
|
|
(command-desc (get-binding-description command))
|
|
|
|
(was-command-desc (and was-command
|
|
|
|
(get-binding-description was-command)))
|
2022-11-17 11:05:58 +00:00
|
|
|
(at-present-desc (get-binding-description at-present)))
|
2014-02-05 16:19:56 +00:00
|
|
|
(let ((line
|
|
|
|
(format
|
2015-03-21 09:13:02 +00:00
|
|
|
(format "%%-%ds%%-%ds%%s\n" (car bind-key-column-widths)
|
|
|
|
(cdr bind-key-column-widths))
|
2014-02-05 16:19:56 +00:00
|
|
|
key-name (format "`%s\'" command-desc)
|
|
|
|
(if (string= command-desc at-present-desc)
|
|
|
|
(if (or (null was-command)
|
|
|
|
(string= command-desc was-command-desc))
|
|
|
|
""
|
|
|
|
(format "was `%s\'" was-command-desc))
|
|
|
|
(format "[now: `%s\']" at-present)))))
|
|
|
|
(princ (if (string-match "[ \t]+\n" line)
|
|
|
|
(replace-match "\n" t t line)
|
|
|
|
line))))
|
2015-03-21 08:30:52 +00:00
|
|
|
|
2013-09-04 14:40:55 +00:00
|
|
|
(setq last-binding binding)))))
|
2012-06-17 09:40:25 +00:00
|
|
|
|
|
|
|
(provide 'bind-key)
|
2015-03-21 08:50:51 +00:00
|
|
|
|
2013-04-27 15:04:47 +00:00
|
|
|
;; Local Variables:
|
2016-12-17 14:26:15 +00:00
|
|
|
;; outline-regexp: ";;;\\(;* [^\s\t\n]\\|###autoload\\)\\|("
|
2013-04-27 15:04:47 +00:00
|
|
|
;; End:
|
2015-03-21 08:50:51 +00:00
|
|
|
|
2012-06-17 09:40:25 +00:00
|
|
|
;;; bind-key.el ends here
|