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.

455 lines
17 KiB

4 years ago
  1. ;;; bind-key.el --- A simple way to manage personal keybindings
  2. ;; Copyright (c) 2012-2017 John Wiegley
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Maintainer: John Wiegley <johnw@newartisans.com>
  5. ;; Created: 16 Jun 2012
  6. ;; Modified: 29 Nov 2017
  7. ;; Version: 2.4
  8. ;; Package-Version: 20180513.430
  9. ;; Keywords: keys keybinding config dotemacs
  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. ;; If you have lots of keybindings set in your .emacs file, it can be hard to
  25. ;; know which ones you haven't set yet, and which may now be overriding some
  26. ;; new default in a new emacs version. This module aims to solve that
  27. ;; problem.
  28. ;;
  29. ;; Bind keys as follows in your .emacs:
  30. ;;
  31. ;; (require 'bind-key)
  32. ;;
  33. ;; (bind-key "C-c x" 'my-ctrl-c-x-command)
  34. ;;
  35. ;; If the keybinding argument is a vector, it is passed straight to
  36. ;; `define-key', so remapping a key with `[remap COMMAND]' works as
  37. ;; expected:
  38. ;;
  39. ;; (bind-key [remap original-ctrl-c-x-command] '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. ;;; Code:
  95. (require 'cl-lib)
  96. (require 'easy-mmode)
  97. (defgroup bind-key nil
  98. "A simple way to manage personal keybindings"
  99. :group 'emacs)
  100. (defcustom bind-key-column-widths '(18 . 40)
  101. "Width of columns in `describe-personal-keybindings'."
  102. :type '(cons integer integer)
  103. :group 'bind-key)
  104. (defcustom bind-key-segregation-regexp
  105. "\\`\\(\\(C-[chx] \\|M-[gso] \\)\\([CM]-\\)?\\|.+-\\)"
  106. "Regular expression used to divide key sets in the output from
  107. \\[describe-personal-keybindings]."
  108. :type 'regexp
  109. :group 'bind-key)
  110. (defcustom bind-key-describe-special-forms nil
  111. "If non-nil, extract docstrings from lambdas, closures and keymaps if possible."
  112. :type 'boolean
  113. :group 'bind-key)
  114. ;; Create override-global-mode to force key remappings
  115. (defvar override-global-map (make-keymap)
  116. "override-global-mode keymap")
  117. (define-minor-mode override-global-mode
  118. "A minor mode so that keymap settings override other modes."
  119. t "")
  120. ;; the keymaps in `emulation-mode-map-alists' take precedence over
  121. ;; `minor-mode-map-alist'
  122. (add-to-list 'emulation-mode-map-alists
  123. `((override-global-mode . ,override-global-map)))
  124. (defvar personal-keybindings nil
  125. "List of bindings performed by `bind-key'.
  126. Elements have the form ((KEY . [MAP]) CMD ORIGINAL-CMD)")
  127. ;;;###autoload
  128. (defmacro bind-key (key-name command &optional keymap predicate)
  129. "Bind KEY-NAME to COMMAND in KEYMAP (`global-map' if not passed).
  130. KEY-NAME may be a vector, in which case it is passed straight to
  131. `define-key'. Or it may be a string to be interpreted as
  132. spelled-out keystrokes, e.g., \"C-c C-z\". See documentation of
  133. `edmacro-mode' for details.
  134. COMMAND must be an interactive function or lambda form.
  135. KEYMAP, if present, should be a keymap and not a quoted symbol.
  136. For example:
  137. (bind-key \"M-h\" #'some-interactive-function my-mode-map)
  138. If PREDICATE is non-nil, it is a form evaluated to determine when
  139. a key should be bound. It must return non-nil in such cases.
  140. Emacs can evaluate this form at any time that it does redisplay
  141. or operates on menu data structures, so you should write it so it
  142. can safely be called at any time."
  143. (let ((namevar (make-symbol "name"))
  144. (keyvar (make-symbol "key"))
  145. (kdescvar (make-symbol "kdesc"))
  146. (bindingvar (make-symbol "binding")))
  147. `(let* ((,namevar ,key-name)
  148. (,keyvar (if (vectorp ,namevar) ,namevar
  149. (read-kbd-macro ,namevar)))
  150. (,kdescvar (cons (if (stringp ,namevar) ,namevar
  151. (key-description ,namevar))
  152. (quote ,keymap)))
  153. (,bindingvar (lookup-key (or ,keymap global-map) ,keyvar)))
  154. (let ((entry (assoc ,kdescvar personal-keybindings))
  155. (details (list ,command
  156. (unless (numberp ,bindingvar)
  157. ,bindingvar))))
  158. (if entry
  159. (setcdr entry details)
  160. (add-to-list 'personal-keybindings (cons ,kdescvar details))))
  161. ,(if predicate
  162. `(define-key (or ,keymap global-map) ,keyvar
  163. '(menu-item "" nil :filter (lambda (&optional _)
  164. (when ,predicate
  165. ,command))))
  166. `(define-key (or ,keymap global-map) ,keyvar ,command)))))
  167. ;;;###autoload
  168. (defmacro unbind-key (key-name &optional keymap)
  169. "Unbind the given KEY-NAME, within the KEYMAP (if specified).
  170. See `bind-key' for more details."
  171. `(progn
  172. (bind-key ,key-name nil ,keymap)
  173. (setq personal-keybindings
  174. (cl-delete-if #'(lambda (k)
  175. ,(if keymap
  176. `(and (consp (car k))
  177. (string= (caar k) ,key-name)
  178. (eq (cdar k) ',keymap))
  179. `(and (stringp (car k))
  180. (string= (car k) ,key-name))))
  181. personal-keybindings))))
  182. ;;;###autoload
  183. (defmacro bind-key* (key-name command &optional predicate)
  184. "Similar to `bind-key', but overrides any mode-specific bindings."
  185. `(bind-key ,key-name ,command override-global-map ,predicate))
  186. (defun bind-keys-form (args keymap)
  187. "Bind multiple keys at once.
  188. Accepts keyword arguments:
  189. :map MAP - a keymap into which the keybindings should be
  190. added
  191. :prefix KEY - prefix key for these bindings
  192. :prefix-map MAP - name of the prefix map that should be created
  193. for these bindings
  194. :prefix-docstring STR - docstring for the prefix-map variable
  195. :menu-name NAME - optional menu string for prefix map
  196. :filter FORM - optional form to determine when bindings apply
  197. The rest of the arguments are conses of keybinding string and a
  198. function symbol (unquoted)."
  199. (let (map
  200. doc
  201. prefix-map
  202. prefix
  203. filter
  204. menu-name
  205. pkg)
  206. ;; Process any initial keyword arguments
  207. (let ((cont t))
  208. (while (and cont args)
  209. (if (cond ((and (eq :map (car args))
  210. (not prefix-map))
  211. (setq map (cadr args)))
  212. ((eq :prefix-docstring (car args))
  213. (setq doc (cadr args)))
  214. ((and (eq :prefix-map (car args))
  215. (not (memq map '(global-map
  216. override-global-map))))
  217. (setq prefix-map (cadr args)))
  218. ((eq :prefix (car args))
  219. (setq prefix (cadr args)))
  220. ((eq :filter (car args))
  221. (setq filter (cadr args)) t)
  222. ((eq :menu-name (car args))
  223. (setq menu-name (cadr args)))
  224. ((eq :package (car args))
  225. (setq pkg (cadr args))))
  226. (setq args (cddr args))
  227. (setq cont nil))))
  228. (when (or (and prefix-map (not prefix))
  229. (and prefix (not prefix-map)))
  230. (error "Both :prefix-map and :prefix must be supplied"))
  231. (when (and menu-name (not prefix))
  232. (error "If :menu-name is supplied, :prefix must be too"))
  233. (unless map (setq map keymap))
  234. ;; Process key binding arguments
  235. (let (first next)
  236. (while args
  237. (if (keywordp (car args))
  238. (progn
  239. (setq next args)
  240. (setq args nil))
  241. (if first
  242. (nconc first (list (car args)))
  243. (setq first (list (car args))))
  244. (setq args (cdr args))))
  245. (cl-flet
  246. ((wrap (map bindings)
  247. (if (and map pkg (not (memq map '(global-map
  248. override-global-map))))
  249. `((if (boundp ',map)
  250. ,(macroexp-progn bindings)
  251. (eval-after-load
  252. ,(if (symbolp pkg) `',pkg pkg)
  253. ',(macroexp-progn bindings))))
  254. bindings)))
  255. (append
  256. (when prefix-map
  257. `((defvar ,prefix-map)
  258. ,@(when doc `((put ',prefix-map 'variable-documentation ,doc)))
  259. ,@(if menu-name
  260. `((define-prefix-command ',prefix-map nil ,menu-name))
  261. `((define-prefix-command ',prefix-map)))
  262. ,@(if (and map (not (eq map 'global-map)))
  263. (wrap map `((bind-key ,prefix ',prefix-map ,map ,filter)))
  264. `((bind-key ,prefix ',prefix-map nil ,filter)))))
  265. (wrap map
  266. (cl-mapcan
  267. (lambda (form)
  268. (let ((fun (and (cdr form) (list 'function (cdr form)))))
  269. (if prefix-map
  270. `((bind-key ,(car form) ,fun ,prefix-map ,filter))
  271. (if (and map (not (eq map 'global-map)))
  272. `((bind-key ,(car form) ,fun ,map ,filter))
  273. `((bind-key ,(car form) ,fun nil ,filter))))))
  274. first))
  275. (when next
  276. (bind-keys-form (if pkg
  277. (cons :package (cons pkg next))
  278. next) map)))))))
  279. ;;;###autoload
  280. (defmacro bind-keys (&rest args)
  281. "Bind multiple keys at once.
  282. Accepts keyword arguments:
  283. :map MAP - a keymap into which the keybindings should be
  284. added
  285. :prefix KEY - prefix key for these bindings
  286. :prefix-map MAP - name of the prefix map that should be created
  287. for these bindings
  288. :prefix-docstring STR - docstring for the prefix-map variable
  289. :menu-name NAME - optional menu string for prefix map
  290. :filter FORM - optional form to determine when bindings apply
  291. The rest of the arguments are conses of keybinding string and a
  292. function symbol (unquoted)."
  293. (macroexp-progn (bind-keys-form args nil)))
  294. ;;;###autoload
  295. (defmacro bind-keys* (&rest args)
  296. (macroexp-progn (bind-keys-form args 'override-global-map)))
  297. (defun get-binding-description (elem)
  298. (cond
  299. ((listp elem)
  300. (cond
  301. ((memq (car elem) '(lambda function))
  302. (if (and bind-key-describe-special-forms
  303. (stringp (nth 2 elem)))
  304. (nth 2 elem)
  305. "#<lambda>"))
  306. ((eq 'closure (car elem))
  307. (if (and bind-key-describe-special-forms
  308. (stringp (nth 3 elem)))
  309. (nth 3 elem)
  310. "#<closure>"))
  311. ((eq 'keymap (car elem))
  312. "#<keymap>")
  313. (t
  314. elem)))
  315. ;; must be a symbol, non-symbol keymap case covered above
  316. ((and bind-key-describe-special-forms (keymapp elem))
  317. (let ((doc (get elem 'variable-documentation)))
  318. (if (stringp doc) doc elem)))
  319. ((symbolp elem)
  320. elem)
  321. (t
  322. "#<byte-compiled lambda>")))
  323. (defun compare-keybindings (l r)
  324. (let* ((regex bind-key-segregation-regexp)
  325. (lgroup (and (string-match regex (caar l))
  326. (match-string 0 (caar l))))
  327. (rgroup (and (string-match regex (caar r))
  328. (match-string 0 (caar r))))
  329. (lkeymap (cdar l))
  330. (rkeymap (cdar r)))
  331. (cond
  332. ((and (null lkeymap) rkeymap)
  333. (cons t t))
  334. ((and lkeymap (null rkeymap))
  335. (cons nil t))
  336. ((and lkeymap rkeymap
  337. (not (string= (symbol-name lkeymap) (symbol-name rkeymap))))
  338. (cons (string< (symbol-name lkeymap) (symbol-name rkeymap)) t))
  339. ((and (null lgroup) rgroup)
  340. (cons t t))
  341. ((and lgroup (null rgroup))
  342. (cons nil t))
  343. ((and lgroup rgroup)
  344. (if (string= lgroup rgroup)
  345. (cons (string< (caar l) (caar r)) nil)
  346. (cons (string< lgroup rgroup) t)))
  347. (t
  348. (cons (string< (caar l) (caar r)) nil)))))
  349. ;;;###autoload
  350. (defun describe-personal-keybindings ()
  351. "Display all the personal keybindings defined by `bind-key'."
  352. (interactive)
  353. (with-output-to-temp-buffer "*Personal Keybindings*"
  354. (princ (format (concat "Key name%s Command%s Comments\n%s %s "
  355. "---------------------\n")
  356. (make-string (- (car bind-key-column-widths) 9) ? )
  357. (make-string (- (cdr bind-key-column-widths) 8) ? )
  358. (make-string (1- (car bind-key-column-widths)) ?-)
  359. (make-string (1- (cdr bind-key-column-widths)) ?-)))
  360. (let (last-binding)
  361. (dolist (binding
  362. (setq personal-keybindings
  363. (sort personal-keybindings
  364. (lambda (l r)
  365. (car (compare-keybindings l r))))))
  366. (if (not (eq (cdar last-binding) (cdar binding)))
  367. (princ (format "\n\n%s: %s\n%s\n\n"
  368. (cdar binding) (caar binding)
  369. (make-string (+ 21 (car bind-key-column-widths)
  370. (cdr bind-key-column-widths)) ?-)))
  371. (if (and last-binding
  372. (cdr (compare-keybindings last-binding binding)))
  373. (princ "\n")))
  374. (let* ((key-name (caar binding))
  375. (at-present (lookup-key (or (symbol-value (cdar binding))
  376. (current-global-map))
  377. (read-kbd-macro key-name)))
  378. (command (nth 1 binding))
  379. (was-command (nth 2 binding))
  380. (command-desc (get-binding-description command))
  381. (was-command-desc (and was-command
  382. (get-binding-description was-command)))
  383. (at-present-desc (get-binding-description at-present))
  384. )
  385. (let ((line
  386. (format
  387. (format "%%-%ds%%-%ds%%s\n" (car bind-key-column-widths)
  388. (cdr bind-key-column-widths))
  389. key-name (format "`%s\'" command-desc)
  390. (if (string= command-desc at-present-desc)
  391. (if (or (null was-command)
  392. (string= command-desc was-command-desc))
  393. ""
  394. (format "was `%s\'" was-command-desc))
  395. (format "[now: `%s\']" at-present)))))
  396. (princ (if (string-match "[ \t]+\n" line)
  397. (replace-match "\n" t t line)
  398. line))))
  399. (setq last-binding binding)))))
  400. (provide 'bind-key)
  401. ;; Local Variables:
  402. ;; outline-regexp: ";;;\\(;* [^\s\t\n]\\|###autoload\\)\\|("
  403. ;; indent-tabs-mode: nil
  404. ;; End:
  405. ;;; bind-key.el ends here