add gitigonre file
[dotfiles.git] / emacs.d_2 / elpa / bind-key-20160227.48 / bind-key.el
1 ;;; bind-key.el --- A simple way to manage personal keybindings
2
3 ;; Copyright (c) 2012-2015 john wiegley
4
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Maintainer: John Wiegley <jwiegley@gmail.com>
7 ;; Created: 16 Jun 2012
8 ;; Version: 1.0
9 ;; Package-Version: 20160227.48
10 ;; Keywords: keys keybinding config dotemacs
11 ;; URL: https://github.com/jwiegley/use-package
12
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.
17
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.
22
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.
27 \f
28 ;;; Commentary:
29
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
33 ;; problem.
34 ;;
35 ;; Bind keys as follows in your .emacs:
36 ;;
37 ;;   (require 'bind-key)
38 ;;
39 ;;   (bind-key "C-c x" 'my-ctrl-c-x-command)
40 ;;
41 ;; If you want the keybinding to override all minor modes that may also bind
42 ;; the same key, use the `bind-key*' form:
43 ;;
44 ;;   (bind-key* "<C-return>" 'other-window)
45 ;;
46 ;; If you want to rebind a key only in a particular keymap, use:
47 ;;
48 ;;   (bind-key "C-c x" 'my-ctrl-c-x-command some-other-mode-map)
49 ;;
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),
52 ;; use `unbind-key':
53 ;;
54 ;;   (unbind-key "C-c x" some-other-mode-map)
55 ;;
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.
59 ;;
60 ;; To add keys into a specific map, use :map argument
61 ;;
62 ;;    (bind-keys :map dired-mode-map
63 ;;               ("o" . dired-omit-mode)
64 ;;               ("a" . some-custom-dired-function))
65 ;;
66 ;; To set up a prefix map, use `:prefix-map' and `:prefix' arguments (both are
67 ;; required)
68 ;;
69 ;;    (bind-keys :prefix-map my-customize-prefix-map
70 ;;               :prefix "C-c c"
71 ;;               ("f" . customize-face)
72 ;;               ("v" . customize-variable))
73 ;;
74 ;; You can combine all the keywords together.  Additionally,
75 ;; `:prefix-docstring' can be specified to set documentation of created
76 ;; `:prefix-map' variable.
77 ;;
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:
80 ;;
81 ;;    (bind-keys*
82 ;;     ("C-o" . other-window)
83 ;;     ("C-M-n" . forward-page)
84 ;;     ("C-M-p" . backward-page))
85 ;;
86 ;; After Emacs loads, you can see a summary of all your personal keybindings
87 ;; currently in effect with this command:
88 ;;
89 ;;   M-x describe-personal-keybindings
90 ;;
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.
94
95 (require 'cl-lib)
96 (require 'easy-mmode)
97
98 (defgroup bind-key nil
99   "A simple way to manage personal keybindings"
100   :group 'emacs)
101
102 (defcustom bind-key-column-widths '(18 . 40)
103   "Width of columns in `describe-personal-keybindings'."
104   :type '(cons integer integer)
105   :group 'bind-key)
106
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]."
111   :type 'regexp
112   :group 'bind-key)
113
114 (defcustom bind-key-describe-special-forms nil
115   "If non-nil, extract docstrings from lambdas, closures and keymaps if possible."
116   :type 'boolean
117   :group 'bind-key)
118
119 ;; Create override-global-mode to force key remappings
120
121 (defvar override-global-map (make-keymap)
122   "override-global-mode keymap")
123
124 (define-minor-mode override-global-mode
125   "A minor mode so that keymap settings override other modes."
126   t "")
127
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)))
132
133 (defvar personal-keybindings nil
134   "List of bindings performed by `bind-key'.
135
136 Elements have the form ((KEY . [MAP]) CMD ORIGINAL-CMD)")
137
138 ;;;###autoload
139 (defmacro bind-key (key-name command &optional keymap predicate)
140   "Bind KEY-NAME to COMMAND in KEYMAP (`global-map' if not passed).
141
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.
146
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))
161                              (quote ,keymap)))
162             (,bindingvar (lookup-key (or ,keymap global-map) ,keyvar)))
163        (add-to-list 'personal-keybindings
164                     (list ,kdescvar ,command
165                           (unless (numberp ,bindingvar) ,bindingvar)))
166        ,(if predicate
167             `(define-key (or ,keymap global-map) ,keyvar
168                '(menu-item "" nil :filter (lambda (&optional _)
169                                             (when ,predicate
170                                               ,command))))
171           `(define-key (or ,keymap global-map) ,keyvar ,command)))))
172
173 ;;;###autoload
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."
177   `(progn
178      (bind-key ,key-name nil ,keymap)
179      (setq personal-keybindings
180            (cl-delete-if #'(lambda (k)
181                              ,(if keymap
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))))
188
189 ;;;###autoload
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))
193
194 (defun bind-keys-form (args)
195   "Bind multiple keys at once.
196
197 Accepts keyword arguments:
198 :map MAP               - a keymap into which the keybindings should be
199                          added
200 :prefix KEY            - prefix key for these bindings
201 :prefix-map MAP        - name of the prefix map that should be created
202                          for these bindings
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
206
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))
221          (key-bindings (progn
222                          (while (keywordp (car args))
223                            (pop args)
224                            (pop args))
225                          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)
232           saw-map first next)
233       (while args
234         (if (keywordp (car args))
235             (progn
236               (setq next args)
237               (setq args nil))
238           (if first
239               (nconc first (list (car args)))
240             (setq first (list (car args))))
241           (setq args (cdr args))))
242       (cl-flet
243           ((wrap (map bindings)
244                  (if (and map pkg (not (eq map 'global-map)))
245                      (if (boundp map)
246                          bindings
247                        `((eval-after-load
248                              ,(if (symbolp pkg) `',pkg pkg)
249                            '(progn ,@bindings))))
250                    bindings)))
251         (append
252          (when prefix-map
253            `((defvar ,prefix-map)
254              ,@(when doc `((put ',prefix-map 'variable-documentation ,doc)))
255              ,@(if menu-name
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)))))
261          (wrap map
262                (cl-mapcan
263                 (lambda (form)
264                   (if prefix-map
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)))))
269                 first))
270          (when next
271            (bind-keys-form
272             (if pkg
273                 (cons :package (cons pkg next))
274               next))))))))
275
276 ;;;###autoload
277 (defmacro bind-keys (&rest args)
278   "Bind multiple keys at once.
279
280 Accepts keyword arguments:
281 :map MAP               - a keymap into which the keybindings should be
282                          added
283 :prefix KEY            - prefix key for these bindings
284 :prefix-map MAP        - name of the prefix map that should be created
285                          for these bindings
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
289
290 The rest of the arguments are conses of keybinding string and a
291 function symbol (unquoted)."
292   (macroexp-progn (bind-keys-form args)))
293
294 ;;;###autoload
295 (defmacro bind-keys* (&rest args)
296   (macroexp-progn
297    (bind-keys-form `(:map override-global-map ,@args))))
298
299 (defun get-binding-description (elem)
300   (cond
301    ((listp elem)
302     (cond
303      ((eq 'lambda (car elem))
304       (if (and bind-key-describe-special-forms
305                (stringp (nth 2 elem)))
306           (nth 2 elem)
307         "#<lambda>"))
308      ((eq 'closure (car elem))
309       (if (and bind-key-describe-special-forms
310                (stringp (nth 3 elem)))
311           (nth 3 elem)
312         "#<closure>"))
313      ((eq 'keymap (car elem))
314       "#<keymap>")
315      (t
316       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)))
321    ((symbolp elem)
322     elem)
323    (t
324     "#<byte-compiled lambda>")))
325
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))))
332          (lkeymap (cdar l))
333          (rkeymap (cdar r)))
334     (cond
335      ((and (null lkeymap) rkeymap)
336       (cons t t))
337      ((and lkeymap (null rkeymap))
338       (cons nil t))
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)
343       (cons t t))
344      ((and lgroup (null rgroup))
345       (cons nil t))
346      ((and lgroup rgroup)
347       (if (string= lgroup rgroup)
348           (cons (string< (caar l) (caar r)) nil)
349         (cons (string< lgroup rgroup) t)))
350      (t
351       (cons (string< (caar l) (caar r)) nil)))))
352
353 ;;;###autoload
354 (defun describe-personal-keybindings ()
355   "Display all the personal keybindings defined by `bind-key'."
356   (interactive)
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)) ?-)))
364     (let (last-binding)
365       (dolist (binding
366                (setq personal-keybindings
367                      (sort personal-keybindings
368                            (lambda (l r)
369                              (car (compare-keybindings l r))))))
370
371         (if (not (eq (cdar last-binding) (cdar binding)))
372             (princ (format "\n\n%s\n%s\n\n"
373                            (cdar binding)
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)))
378               (princ "\n")))
379
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))
390                )
391           (let ((line
392                  (format
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))
399                           ""
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)
404                      line))))
405
406         (setq last-binding binding)))))
407
408 (provide 'bind-key)
409
410 ;; Local Variables:
411 ;; indent-tabs-mode: nil
412 ;; End:
413
414 ;;; bind-key.el ends here