Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.2 KiB

пре 4 година
  1. ;;; use-package-bind-key.el --- Support for the :bind/:bind-keymap keywords -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2017 John Wiegley
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Maintainer: John Wiegley <johnw@newartisans.com>
  5. ;; Created: 17 Jun 2012
  6. ;; Modified: 4 Dec 2017
  7. ;; Version: 1.0
  8. ;; Package-Requires: ((emacs "24.3") (use-package "2.4") (bind-key "2.4"))
  9. ;; Keywords: dotemacs startup speed config package
  10. ;; URL: https://github.com/jwiegley/use-package
  11. ;; This program is free software; you can redistribute it and/or
  12. ;; modify it under the terms of the GNU General Public License as
  13. ;; published by the Free Software Foundation; either version 3, or (at
  14. ;; your option) any later version.
  15. ;; This program is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. ;; General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23. ;;; Commentary:
  24. ;; Provides support for the :bind, :bind*, :bind-keymap and :bind-keymap*
  25. ;; keywords. Note that these are currently still baked into
  26. ;; `use-package-keywords' and `use-package-deferring-keywords', although this
  27. ;; is harmless if they are never used.
  28. ;;; Code:
  29. (require 'use-package-core)
  30. (require 'bind-key)
  31. ;;;###autoload
  32. (defun use-package-autoload-keymap (keymap-symbol package override)
  33. "Loads PACKAGE and then binds the key sequence used to invoke
  34. this function to KEYMAP-SYMBOL. It then simulates pressing the
  35. same key sequence a again, so that the next key pressed is routed
  36. to the newly loaded keymap.
  37. This function supports use-package's :bind-keymap keyword. It
  38. works by binding the given key sequence to an invocation of this
  39. function for a particular keymap. The keymap is expected to be
  40. defined by the package. In this way, loading the package is
  41. deferred until the prefix key sequence is pressed."
  42. (if (not (require package nil t))
  43. (use-package-error (format "Cannot load package.el: %s" package))
  44. (if (and (boundp keymap-symbol)
  45. (keymapp (symbol-value keymap-symbol)))
  46. (let* ((kv (this-command-keys-vector))
  47. (key (key-description kv))
  48. (keymap (symbol-value keymap-symbol)))
  49. (if override
  50. (bind-key* key keymap)
  51. (bind-key key keymap))
  52. (setq unread-command-events
  53. (mapcar (lambda (ev) (cons t ev))
  54. (listify-key-sequence kv))))
  55. (use-package-error
  56. (format "package.el %s failed to define keymap %s"
  57. package keymap-symbol)))))
  58. ;;;###autoload
  59. (defun use-package-normalize-binder (name keyword args)
  60. (let ((arg args)
  61. args*)
  62. (while arg
  63. (let ((x (car arg)))
  64. (cond
  65. ;; (KEY . COMMAND)
  66. ((and (consp x)
  67. (or (stringp (car x))
  68. (vectorp (car x)))
  69. (or (use-package-recognize-function (cdr x) t #'stringp)))
  70. (setq args* (nconc args* (list x)))
  71. (setq arg (cdr arg)))
  72. ;; KEYWORD
  73. ;; :map KEYMAP
  74. ;; :prefix-docstring STRING
  75. ;; :prefix-map SYMBOL
  76. ;; :prefix STRING
  77. ;; :filter SEXP
  78. ;; :menu-name STRING
  79. ;; :package SYMBOL
  80. ((or (and (eq x :map) (symbolp (cadr arg)))
  81. (and (eq x :prefix) (stringp (cadr arg)))
  82. (and (eq x :prefix-map) (symbolp (cadr arg)))
  83. (and (eq x :prefix-docstring) (stringp (cadr arg)))
  84. (eq x :filter)
  85. (and (eq x :menu-name) (stringp (cadr arg)))
  86. (and (eq x :package) (symbolp (cadr arg))))
  87. (setq args* (nconc args* (list x (cadr arg))))
  88. (setq arg (cddr arg)))
  89. ((listp x)
  90. (setq args*
  91. (nconc args* (use-package-normalize-binder name keyword x)))
  92. (setq arg (cdr arg)))
  93. (t
  94. ;; Error!
  95. (use-package-error
  96. (concat (symbol-name name)
  97. " wants arguments acceptable to the `bind-keys' macro,"
  98. " or a list of such values"))))))
  99. args*))
  100. ;;;; :bind, :bind*
  101. ;;;###autoload
  102. (defalias 'use-package-normalize/:bind 'use-package-normalize-binder)
  103. ;;;###autoload
  104. (defalias 'use-package-normalize/:bind* 'use-package-normalize-binder)
  105. ;; jww (2017-12-07): This is too simplistic. It will fail to determine
  106. ;; autoloads in this situation:
  107. ;; (use-package foo
  108. ;; :bind (:map foo-map (("C-a" . func))))
  109. ;;;###autoload
  110. (defalias 'use-package-autoloads/:bind 'use-package-autoloads-mode)
  111. ;;;###autoload
  112. (defalias 'use-package-autoloads/:bind* 'use-package-autoloads-mode)
  113. ;;;###autoload
  114. (defun use-package-handler/:bind
  115. (name _keyword args rest state &optional bind-macro)
  116. (use-package-concat
  117. (use-package-process-keywords name rest state)
  118. `(,@(mapcar
  119. #'(lambda (xs)
  120. `(,(if bind-macro bind-macro 'bind-keys)
  121. :package ,name ,@(use-package-normalize-commands xs)))
  122. (use-package-split-list-at-keys :break args)))))
  123. (defun use-package-handler/:bind* (name keyword arg rest state)
  124. (use-package-handler/:bind name keyword arg rest state 'bind-keys*))
  125. ;;;; :bind-keymap, :bind-keymap*
  126. ;;;###autoload
  127. (defalias 'use-package-normalize/:bind-keymap 'use-package-normalize-binder)
  128. ;;;###autoload
  129. (defalias 'use-package-normalize/:bind-keymap* 'use-package-normalize-binder)
  130. ;;;###autoload
  131. (defun use-package-handler/:bind-keymap
  132. (name _keyword args rest state &optional override)
  133. (use-package-concat
  134. (use-package-process-keywords name rest state)
  135. (mapcar
  136. #'(lambda (binding)
  137. `(,(if override 'bind-key* 'bind-key)
  138. ,(car binding)
  139. #'(lambda ()
  140. (interactive)
  141. (use-package-autoload-keymap
  142. ',(cdr binding) ',(use-package-as-symbol name)
  143. ,override))))
  144. args)))
  145. ;;;###autoload
  146. (defun use-package-handler/:bind-keymap* (name keyword arg rest state)
  147. (use-package-handler/:bind-keymap name keyword arg rest state t))
  148. (provide 'use-package-bind-key)
  149. ;;; use-package-bind-key.el ends here