1 ;;; bind-key.el --- A simple way to manage personal keybindings
3 ;; Copyright (c) 2012-2015 john wiegley
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Maintainer: John Wiegley <jwiegley@gmail.com>
7 ;; Created: 16 Jun 2012
9 ;; Package-Version: 20160227.48
10 ;; Keywords: keys keybinding config dotemacs
11 ;; URL: https://github.com/jwiegley/use-package
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the gnu general public license as
15 ;; published by the free software foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; without any warranty; without even the implied warranty of
20 ;; merchantability or fitness for a particular purpose. see the gnu
21 ;; general public license for more details.
23 ;; You should have received a copy of the gnu general public license
24 ;; along with gnu emacs; see the file copying. if not, write to the
25 ;; free software foundation, inc., 59 temple place - suite 330,
26 ;; boston, ma 02111-1307, usa.
30 ;; If you have lots of keybindings set in your .emacs file, it can be hard to
31 ;; know which ones you haven't set yet, and which may now be overriding some
32 ;; new default in a new emacs version. This module aims to solve that
35 ;; Bind keys as follows in your .emacs:
37 ;; (require 'bind-key)
39 ;; (bind-key "C-c x" 'my-ctrl-c-x-command)
41 ;; If you want the keybinding to override all minor modes that may also bind
42 ;; the same key, use the `bind-key*' form:
44 ;; (bind-key* "<C-return>" 'other-window)
46 ;; If you want to rebind a key only in a particular keymap, use:
48 ;; (bind-key "C-c x" 'my-ctrl-c-x-command some-other-mode-map)
50 ;; To unbind a key within a keymap (for example, to stop your favorite major
51 ;; mode from changing a binding that you don't want to override everywhere),
54 ;; (unbind-key "C-c x" some-other-mode-map)
56 ;; To bind multiple keys at once, or set up a prefix map, a `bind-keys' macro
57 ;; is provided. It accepts keyword arguments, please see its documentation
58 ;; for a detailed description.
60 ;; To add keys into a specific map, use :map argument
62 ;; (bind-keys :map dired-mode-map
63 ;; ("o" . dired-omit-mode)
64 ;; ("a" . some-custom-dired-function))
66 ;; To set up a prefix map, use `:prefix-map' and `:prefix' arguments (both are
69 ;; (bind-keys :prefix-map my-customize-prefix-map
71 ;; ("f" . customize-face)
72 ;; ("v" . customize-variable))
74 ;; You can combine all the keywords together. Additionally,
75 ;; `:prefix-docstring' can be specified to set documentation of created
76 ;; `:prefix-map' variable.
78 ;; To bind multiple keys in a `bind-key*' way (to be sure that your bindings
79 ;; will not be overridden by other modes), you may use `bind-keys*' macro:
82 ;; ("C-o" . other-window)
83 ;; ("C-M-n" . forward-page)
84 ;; ("C-M-p" . backward-page))
86 ;; After Emacs loads, you can see a summary of all your personal keybindings
87 ;; currently in effect with this command:
89 ;; M-x describe-personal-keybindings
91 ;; This display will tell you if you've overriden a default keybinding, and
92 ;; what the default was. Also, it will tell you if the key was rebound after
93 ;; your binding it with `bind-key', and what it was rebound it to.
98 (defgroup bind-key nil
99 "A simple way to manage personal keybindings"
102 (defcustom bind-key-column-widths '(18 . 40)
103 "Width of columns in `describe-personal-keybindings'."
104 :type '(cons integer integer)
107 (defcustom bind-key-segregation-regexp
108 "\\`\\(\\(C-[chx] \\|M-[gso] \\)\\([CM]-\\)?\\|.+-\\)"
109 "Regular expression used to divide key sets in the output from
110 \\[describe-personal-keybindings]."
114 (defcustom bind-key-describe-special-forms nil
115 "If non-nil, extract docstrings from lambdas, closures and keymaps if possible."
119 ;; Create override-global-mode to force key remappings
121 (defvar override-global-map (make-keymap)
122 "override-global-mode keymap")
124 (define-minor-mode override-global-mode
125 "A minor mode so that keymap settings override other modes."
128 ;; the keymaps in `emulation-mode-map-alists' take precedence over
129 ;; `minor-mode-map-alist'
130 (add-to-list 'emulation-mode-map-alists
131 `((override-global-mode . ,override-global-map)))
133 (defvar personal-keybindings nil
134 "List of bindings performed by `bind-key'.
136 Elements have the form ((KEY . [MAP]) CMD ORIGINAL-CMD)")
139 (defmacro bind-key (key-name command &optional keymap predicate)
140 "Bind KEY-NAME to COMMAND in KEYMAP (`global-map' if not passed).
142 KEY-NAME may be a vector, in which case it is passed straight to
143 `define-key'. Or it may be a string to be interpreted as
144 spelled-out keystrokes, e.g., \"C-c C-z\". See documentation of
145 `edmacro-mode' for details.
147 If PREDICATE is non-nil, it is a form evaluated to determine when
148 a key should be bound. It must return non-nil in such cases.
149 Emacs can evaluate this form at any time that it does redisplay
150 or operates on menu data structures, so you should write it so it
151 can safely be called at any time."
152 (let ((namevar (make-symbol "name"))
153 (keyvar (make-symbol "key"))
154 (kdescvar (make-symbol "kdesc"))
155 (bindingvar (make-symbol "binding")))
156 `(let* ((,namevar ,key-name)
157 (,keyvar (if (vectorp ,namevar) ,namevar
158 (read-kbd-macro ,namevar)))
159 (,kdescvar (cons (if (stringp ,namevar) ,namevar
160 (key-description ,namevar))
162 (,bindingvar (lookup-key (or ,keymap global-map) ,keyvar)))
163 (add-to-list 'personal-keybindings
164 (list ,kdescvar ,command
165 (unless (numberp ,bindingvar) ,bindingvar)))
167 `(define-key (or ,keymap global-map) ,keyvar
168 '(menu-item "" nil :filter (lambda (&optional _)
171 `(define-key (or ,keymap global-map) ,keyvar ,command)))))
174 (defmacro unbind-key (key-name &optional keymap)
175 "Unbind the given KEY-NAME, within the KEYMAP (if specified).
176 See `bind-key' for more details."
178 (bind-key ,key-name nil ,keymap)
179 (setq personal-keybindings
180 (cl-delete-if #'(lambda (k)
182 `(and (consp (car k))
183 (string= (caar k) ,key-name)
184 (eq (cdar k) ',keymap))
185 `(and (stringp (car k))
186 (string= (car k) ,key-name))))
187 personal-keybindings))))
190 (defmacro bind-key* (key-name command &optional predicate)
191 "Similar to `bind-key', but overrides any mode-specific bindings."
192 `(bind-key ,key-name ,command override-global-map ,predicate))
194 (defun bind-keys-form (args)
195 "Bind multiple keys at once.
197 Accepts keyword arguments:
198 :map MAP - a keymap into which the keybindings should be
200 :prefix KEY - prefix key for these bindings
201 :prefix-map MAP - name of the prefix map that should be created
203 :prefix-docstring STR - docstring for the prefix-map variable
204 :menu-name NAME - optional menu string for prefix map
205 :filter FORM - optional form to determine when bindings apply
207 The rest of the arguments are conses of keybinding string and a
208 function symbol (unquoted)."
209 ;; jww (2016-02-26): This is a hack; this whole function needs to be
210 ;; rewritten to normalize arguments the way that use-package.el does.
211 (if (and (eq (car args) :package)
212 (not (eq (car (cdr (cdr args))) :map)))
213 (setq args (cons :map (cons 'global-map args))))
214 (let* ((map (plist-get args :map))
215 (doc (plist-get args :prefix-docstring))
216 (prefix-map (plist-get args :prefix-map))
217 (prefix (plist-get args :prefix))
218 (filter (plist-get args :filter))
219 (menu-name (plist-get args :menu-name))
220 (pkg (plist-get args :package))
222 (while (keywordp (car args))
226 (when (or (and prefix-map (not prefix))
227 (and prefix (not prefix-map)))
228 (error "Both :prefix-map and :prefix must be supplied"))
229 (when (and menu-name (not prefix))
230 (error "If :menu-name is supplied, :prefix must be too"))
231 (let ((args key-bindings)
234 (if (keywordp (car args))
239 (nconc first (list (car args)))
240 (setq first (list (car args))))
241 (setq args (cdr args))))
243 ((wrap (map bindings)
244 (if (and map pkg (not (eq map 'global-map)))
248 ,(if (symbolp pkg) `',pkg pkg)
249 '(progn ,@bindings))))
253 `((defvar ,prefix-map)
254 ,@(when doc `((put ',prefix-map 'variable-documentation ,doc)))
256 `((define-prefix-command ',prefix-map nil ,menu-name))
257 `((define-prefix-command ',prefix-map)))
258 ,@(if (and map (not (eq map 'global-map)))
259 (wrap map `((bind-key ,prefix ',prefix-map ,map ,filter)))
260 `((bind-key ,prefix ',prefix-map nil ,filter)))))
265 `((bind-key ,(car form) ',(cdr form) ,prefix-map ,filter))
266 (if (and map (not (eq map 'global-map)))
267 `((bind-key ,(car form) ',(cdr form) ,map ,filter))
268 `((bind-key ,(car form) ',(cdr form) nil ,filter)))))
273 (cons :package (cons pkg next))
277 (defmacro bind-keys (&rest args)
278 "Bind multiple keys at once.
280 Accepts keyword arguments:
281 :map MAP - a keymap into which the keybindings should be
283 :prefix KEY - prefix key for these bindings
284 :prefix-map MAP - name of the prefix map that should be created
286 :prefix-docstring STR - docstring for the prefix-map variable
287 :menu-name NAME - optional menu string for prefix map
288 :filter FORM - optional form to determine when bindings apply
290 The rest of the arguments are conses of keybinding string and a
291 function symbol (unquoted)."
292 (macroexp-progn (bind-keys-form args)))
295 (defmacro bind-keys* (&rest args)
297 (bind-keys-form `(:map override-global-map ,@args))))
299 (defun get-binding-description (elem)
303 ((eq 'lambda (car elem))
304 (if (and bind-key-describe-special-forms
305 (stringp (nth 2 elem)))
308 ((eq 'closure (car elem))
309 (if (and bind-key-describe-special-forms
310 (stringp (nth 3 elem)))
313 ((eq 'keymap (car elem))
317 ;; must be a symbol, non-symbol keymap case covered above
318 ((and bind-key-describe-special-forms (keymapp elem))
319 (let ((doc (get elem 'variable-documentation)))
320 (if (stringp doc) doc elem)))
324 "#<byte-compiled lambda>")))
326 (defun compare-keybindings (l r)
327 (let* ((regex bind-key-segregation-regexp)
328 (lgroup (and (string-match regex (caar l))
329 (match-string 0 (caar l))))
330 (rgroup (and (string-match regex (caar r))
331 (match-string 0 (caar r))))
335 ((and (null lkeymap) rkeymap)
337 ((and lkeymap (null rkeymap))
339 ((and lkeymap rkeymap
340 (not (string= (symbol-name lkeymap) (symbol-name rkeymap))))
341 (cons (string< (symbol-name lkeymap) (symbol-name rkeymap)) t))
342 ((and (null lgroup) rgroup)
344 ((and lgroup (null rgroup))
347 (if (string= lgroup rgroup)
348 (cons (string< (caar l) (caar r)) nil)
349 (cons (string< lgroup rgroup) t)))
351 (cons (string< (caar l) (caar r)) nil)))))
354 (defun describe-personal-keybindings ()
355 "Display all the personal keybindings defined by `bind-key'."
357 (with-output-to-temp-buffer "*Personal Keybindings*"
358 (princ (format (concat "Key name%s Command%s Comments\n%s %s "
359 "---------------------\n")
360 (make-string (- (car bind-key-column-widths) 9) ? )
361 (make-string (- (cdr bind-key-column-widths) 8) ? )
362 (make-string (1- (car bind-key-column-widths)) ?-)
363 (make-string (1- (cdr bind-key-column-widths)) ?-)))
366 (setq personal-keybindings
367 (sort personal-keybindings
369 (car (compare-keybindings l r))))))
371 (if (not (eq (cdar last-binding) (cdar binding)))
372 (princ (format "\n\n%s\n%s\n\n"
374 (make-string (+ 21 (car bind-key-column-widths)
375 (cdr bind-key-column-widths)) ?-)))
376 (if (and last-binding
377 (cdr (compare-keybindings last-binding binding)))
380 (let* ((key-name (caar binding))
381 (at-present (lookup-key (or (symbol-value (cdar binding))
382 (current-global-map))
383 (read-kbd-macro key-name)))
384 (command (nth 1 binding))
385 (was-command (nth 2 binding))
386 (command-desc (get-binding-description command))
387 (was-command-desc (and was-command
388 (get-binding-description was-command)))
389 (at-present-desc (get-binding-description at-present))
393 (format "%%-%ds%%-%ds%%s\n" (car bind-key-column-widths)
394 (cdr bind-key-column-widths))
395 key-name (format "`%s\'" command-desc)
396 (if (string= command-desc at-present-desc)
397 (if (or (null was-command)
398 (string= command-desc was-command-desc))
400 (format "was `%s\'" was-command-desc))
401 (format "[now: `%s\']" at-present)))))
402 (princ (if (string-match "[ \t]+\n" line)
403 (replace-match "\n" t t line)
406 (setq last-binding binding)))))
411 ;; indent-tabs-mode: nil
414 ;;; bind-key.el ends here