Personal emacs config
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.

459 lines
17 KiB

  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: 2.4.1
  9. ;; Package-Commit: caa92f1d64fc25480551757d854b4b49981dfa6b
  10. ;; Keywords: keys keybinding config dotemacs
  11. ;; URL: https://github.com/jwiegley/use-package
  12. ;; This program is free software; you can redistribute it and/or
  13. ;; modify it under the terms of the gnu general public license as
  14. ;; published by the free software foundation; either version 3, or (at
  15. ;; your option) any later version.
  16. ;; This program is distributed in the hope that it will be useful, but
  17. ;; without any warranty; without even the implied warranty of
  18. ;; merchantability or fitness for a particular purpose. see the gnu
  19. ;; general public license for more details.
  20. ;; You should have received a copy of the gnu general public license
  21. ;; along with gnu emacs; see the file copying. if not, write to the
  22. ;; free software foundation, inc., 59 temple place - suite 330,
  23. ;; boston, ma 02111-1307, usa.
  24. ;;; Commentary:
  25. ;; If you have lots of keybindings set in your .emacs file, it can be hard to
  26. ;; know which ones you haven't set yet, and which may now be overriding some
  27. ;; new default in a new emacs version. This module aims to solve that
  28. ;; problem.
  29. ;;
  30. ;; Bind keys as follows in your .emacs:
  31. ;;
  32. ;; (require 'bind-key)
  33. ;;
  34. ;; (bind-key "C-c x" 'my-ctrl-c-x-command)
  35. ;;
  36. ;; If the keybinding argument is a vector, it is passed straight to
  37. ;; `define-key', so remapping a key with `[remap COMMAND]' works as
  38. ;; expected:
  39. ;;
  40. ;; (bind-key [remap original-ctrl-c-x-command] 'my-ctrl-c-x-command)
  41. ;;
  42. ;; If you want the keybinding to override all minor modes that may also bind
  43. ;; the same key, use the `bind-key*' form:
  44. ;;
  45. ;; (bind-key* "<C-return>" 'other-window)
  46. ;;
  47. ;; If you want to rebind a key only in a particular keymap, use:
  48. ;;
  49. ;; (bind-key "C-c x" 'my-ctrl-c-x-command some-other-mode-map)
  50. ;;
  51. ;; To unbind a key within a keymap (for example, to stop your favorite major
  52. ;; mode from changing a binding that you don't want to override everywhere),
  53. ;; use `unbind-key':
  54. ;;
  55. ;; (unbind-key "C-c x" some-other-mode-map)
  56. ;;
  57. ;; To bind multiple keys at once, or set up a prefix map, a `bind-keys' macro
  58. ;; is provided. It accepts keyword arguments, please see its documentation
  59. ;; for a detailed description.
  60. ;;
  61. ;; To add keys into a specific map, use :map argument
  62. ;;
  63. ;; (bind-keys :map dired-mode-map
  64. ;; ("o" . dired-omit-mode)
  65. ;; ("a" . some-custom-dired-function))
  66. ;;
  67. ;; To set up a prefix map, use `:prefix-map' and `:prefix' arguments (both are
  68. ;; required)
  69. ;;
  70. ;; (bind-keys :prefix-map my-customize-prefix-map
  71. ;; :prefix "C-c c"
  72. ;; ("f" . customize-face)
  73. ;; ("v" . customize-variable))
  74. ;;
  75. ;; You can combine all the keywords together. Additionally,
  76. ;; `:prefix-docstring' can be specified to set documentation of created
  77. ;; `:prefix-map' variable.
  78. ;;
  79. ;; To bind multiple keys in a `bind-key*' way (to be sure that your bindings
  80. ;; will not be overridden by other modes), you may use `bind-keys*' macro:
  81. ;;
  82. ;; (bind-keys*
  83. ;; ("C-o" . other-window)
  84. ;; ("C-M-n" . forward-page)
  85. ;; ("C-M-p" . backward-page))
  86. ;;
  87. ;; After Emacs loads, you can see a summary of all your personal keybindings
  88. ;; currently in effect with this command:
  89. ;;
  90. ;; M-x describe-personal-keybindings
  91. ;;
  92. ;; This display will tell you if you've overridden a default keybinding, and
  93. ;; what the default was. Also, it will tell you if the key was rebound after
  94. ;; your binding it with `bind-key', and what it was rebound it to.
  95. ;;; Code:
  96. (require 'cl-lib)
  97. (require 'easy-mmode)
  98. (defgroup bind-key nil
  99. "A simple way to manage personal keybindings"
  100. :group 'emacs)
  101. (defcustom bind-key-column-widths '(18 . 40)
  102. "Width of columns in `describe-personal-keybindings'."
  103. :type '(cons integer integer)
  104. :group 'bind-key)
  105. (defcustom bind-key-segregation-regexp
  106. "\\`\\(\\(C-[chx] \\|M-[gso] \\)\\([CM]-\\)?\\|.+-\\)"
  107. "Regular expression used to divide key sets in the output from
  108. \\[describe-personal-keybindings]."
  109. :type 'regexp
  110. :group 'bind-key)
  111. (defcustom bind-key-describe-special-forms nil
  112. "If non-nil, extract docstrings from lambdas, closures and keymaps if possible."
  113. :type 'boolean
  114. :group 'bind-key)
  115. ;; Create override-global-mode to force key remappings
  116. (defvar override-global-map (make-keymap)
  117. "override-global-mode keymap")
  118. (define-minor-mode override-global-mode
  119. "A minor mode so that keymap settings override other modes."
  120. t "")
  121. ;; the keymaps in `emulation-mode-map-alists' take precedence over
  122. ;; `minor-mode-map-alist'
  123. (add-to-list 'emulation-mode-map-alists
  124. `((override-global-mode . ,override-global-map)))
  125. (defvar personal-keybindings nil
  126. "List of bindings performed by `bind-key'.
  127. Elements have the form ((KEY . [MAP]) CMD ORIGINAL-CMD)")
  128. ;;;###autoload
  129. (defmacro bind-key (key-name command &optional keymap predicate)
  130. "Bind KEY-NAME to COMMAND in KEYMAP (`global-map' if not passed).
  131. KEY-NAME may be a vector, in which case it is passed straight to
  132. `define-key'. Or it may be a string to be interpreted as
  133. spelled-out keystrokes, e.g., \"C-c C-z\". See documentation of
  134. `edmacro-mode' for details.
  135. COMMAND must be an interactive function or lambda form.
  136. KEYMAP, if present, should be a keymap variable or symbol.
  137. For example:
  138. (bind-key \"M-h\" #'some-interactive-function my-mode-map)
  139. (bind-key \"M-h\" #'some-interactive-function 'my-mode-map)
  140. If PREDICATE is non-nil, it is a form evaluated to determine when
  141. a key should be bound. It must return non-nil in such cases.
  142. Emacs can evaluate this form at any time that it does redisplay
  143. or operates on menu data structures, so you should write it so it
  144. can safely be called at any time."
  145. (let ((namevar (make-symbol "name"))
  146. (keyvar (make-symbol "key"))
  147. (kdescvar (make-symbol "kdesc"))
  148. (bindingvar (make-symbol "binding")))
  149. `(let* ((,namevar ,key-name)
  150. (,keyvar (if (vectorp ,namevar) ,namevar
  151. (read-kbd-macro ,namevar)))
  152. (kmap (if (and ,keymap (symbolp ,keymap)) (symbol-value ,keymap) ,keymap))
  153. (,kdescvar (cons (if (stringp ,namevar) ,namevar
  154. (key-description ,namevar))
  155. (if (symbolp ,keymap) ,keymap (quote ,keymap))))
  156. (,bindingvar (lookup-key (or kmap global-map) ,keyvar)))
  157. (let ((entry (assoc ,kdescvar personal-keybindings))
  158. (details (list ,command
  159. (unless (numberp ,bindingvar)
  160. ,bindingvar))))
  161. (if entry
  162. (setcdr entry details)
  163. (add-to-list 'personal-keybindings (cons ,kdescvar details))))
  164. ,(if predicate
  165. `(define-key (or kmap global-map) ,keyvar
  166. '(menu-item "" nil :filter (lambda (&optional _)
  167. (when ,predicate
  168. ,command))))
  169. `(define-key (or kmap global-map) ,keyvar ,command)))))
  170. ;;;###autoload
  171. (defmacro unbind-key (key-name &optional keymap)
  172. "Unbind the given KEY-NAME, within the KEYMAP (if specified).
  173. See `bind-key' for more details."
  174. `(progn
  175. (bind-key ,key-name nil ,keymap)
  176. (setq personal-keybindings
  177. (cl-delete-if #'(lambda (k)
  178. ,(if keymap
  179. `(and (consp (car k))
  180. (string= (caar k) ,key-name)
  181. (eq (cdar k) ',keymap))
  182. `(and (stringp (car k))
  183. (string= (car k) ,key-name))))
  184. personal-keybindings))))
  185. ;;;###autoload
  186. (defmacro bind-key* (key-name command &optional predicate)
  187. "Similar to `bind-key', but overrides any mode-specific bindings."
  188. `(bind-key ,key-name ,command override-global-map ,predicate))
  189. (defun bind-keys-form (args keymap)
  190. "Bind multiple keys at once.
  191. Accepts keyword arguments:
  192. :map MAP - a keymap into which the keybindings should be
  193. added
  194. :prefix KEY - prefix key for these bindings
  195. :prefix-map MAP - name of the prefix map that should be created
  196. for these bindings
  197. :prefix-docstring STR - docstring for the prefix-map variable
  198. :menu-name NAME - optional menu string for prefix map
  199. :filter FORM - optional form to determine when bindings apply
  200. The rest of the arguments are conses of keybinding string and a
  201. function symbol (unquoted)."
  202. (let (map
  203. doc
  204. prefix-map
  205. prefix
  206. filter
  207. menu-name
  208. pkg)
  209. ;; Process any initial keyword arguments
  210. (let ((cont t))
  211. (while (and cont args)
  212. (if (cond ((and (eq :map (car args))
  213. (not prefix-map))
  214. (setq map (cadr args)))
  215. ((eq :prefix-docstring (car args))
  216. (setq doc (cadr args)))
  217. ((and (eq :prefix-map (car args))
  218. (not (memq map '(global-map
  219. override-global-map))))
  220. (setq prefix-map (cadr args)))
  221. ((eq :prefix (car args))
  222. (setq prefix (cadr args)))
  223. ((eq :filter (car args))
  224. (setq filter (cadr args)) t)
  225. ((eq :menu-name (car args))
  226. (setq menu-name (cadr args)))
  227. ((eq :package (car args))
  228. (setq pkg (cadr args))))
  229. (setq args (cddr args))
  230. (setq cont nil))))
  231. (when (or (and prefix-map (not prefix))
  232. (and prefix (not prefix-map)))
  233. (error "Both :prefix-map and :prefix must be supplied"))
  234. (when (and menu-name (not prefix))
  235. (error "If :menu-name is supplied, :prefix must be too"))
  236. (unless map (setq map keymap))
  237. ;; Process key binding arguments
  238. (let (first next)
  239. (while args
  240. (if (keywordp (car args))
  241. (progn
  242. (setq next args)
  243. (setq args nil))
  244. (if first
  245. (nconc first (list (car args)))
  246. (setq first (list (car args))))
  247. (setq args (cdr args))))
  248. (cl-flet
  249. ((wrap (map bindings)
  250. (if (and map pkg (not (memq map '(global-map
  251. override-global-map))))
  252. `((if (boundp ',map)
  253. ,(macroexp-progn bindings)
  254. (eval-after-load
  255. ,(if (symbolp pkg) `',pkg pkg)
  256. ',(macroexp-progn bindings))))
  257. bindings)))
  258. (append
  259. (when prefix-map
  260. `((defvar ,prefix-map)
  261. ,@(when doc `((put ',prefix-map 'variable-documentation ,doc)))
  262. ,@(if menu-name
  263. `((define-prefix-command ',prefix-map nil ,menu-name))
  264. `((define-prefix-command ',prefix-map)))
  265. ,@(if (and map (not (eq map 'global-map)))
  266. (wrap map `((bind-key ,prefix ',prefix-map ,map ,filter)))
  267. `((bind-key ,prefix ',prefix-map nil ,filter)))))
  268. (wrap map
  269. (cl-mapcan
  270. (lambda (form)
  271. (let ((fun (and (cdr form) (list 'function (cdr form)))))
  272. (if prefix-map
  273. `((bind-key ,(car form) ,fun ,prefix-map ,filter))
  274. (if (and map (not (eq map 'global-map)))
  275. `((bind-key ,(car form) ,fun ,map ,filter))
  276. `((bind-key ,(car form) ,fun nil ,filter))))))
  277. first))
  278. (when next
  279. (bind-keys-form (if pkg
  280. (cons :package (cons pkg next))
  281. next) map)))))))
  282. ;;;###autoload
  283. (defmacro bind-keys (&rest args)
  284. "Bind multiple keys at once.
  285. Accepts keyword arguments:
  286. :map MAP - a keymap into which the keybindings should be
  287. added
  288. :prefix KEY - prefix key for these bindings
  289. :prefix-map MAP - name of the prefix map that should be created
  290. for these bindings
  291. :prefix-docstring STR - docstring for the prefix-map variable
  292. :menu-name NAME - optional menu string for prefix map
  293. :filter FORM - optional form to determine when bindings apply
  294. The rest of the arguments are conses of keybinding string and a
  295. function symbol (unquoted)."
  296. (macroexp-progn (bind-keys-form args nil)))
  297. ;;;###autoload
  298. (defmacro bind-keys* (&rest args)
  299. (macroexp-progn (bind-keys-form args 'override-global-map)))
  300. (defun get-binding-description (elem)
  301. (cond
  302. ((listp elem)
  303. (cond
  304. ((memq (car elem) '(lambda function))
  305. (if (and bind-key-describe-special-forms
  306. (stringp (nth 2 elem)))
  307. (nth 2 elem)
  308. "#<lambda>"))
  309. ((eq 'closure (car elem))
  310. (if (and bind-key-describe-special-forms
  311. (stringp (nth 3 elem)))
  312. (nth 3 elem)
  313. "#<closure>"))
  314. ((eq 'keymap (car elem))
  315. "#<keymap>")
  316. (t
  317. elem)))
  318. ;; must be a symbol, non-symbol keymap case covered above
  319. ((and bind-key-describe-special-forms (keymapp elem))
  320. (let ((doc (get elem 'variable-documentation)))
  321. (if (stringp doc) doc elem)))
  322. ((symbolp elem)
  323. elem)
  324. (t
  325. "#<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))))
  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. ;;;###autoload
  353. (defun describe-personal-keybindings ()
  354. "Display all the personal keybindings defined by `bind-key'."
  355. (interactive)
  356. (with-output-to-temp-buffer "*Personal Keybindings*"
  357. (princ (format (concat "Key name%s Command%s Comments\n%s %s "
  358. "---------------------\n")
  359. (make-string (- (car bind-key-column-widths) 9) ? )
  360. (make-string (- (cdr bind-key-column-widths) 8) ? )
  361. (make-string (1- (car bind-key-column-widths)) ?-)
  362. (make-string (1- (cdr bind-key-column-widths)) ?-)))
  363. (let (last-binding)
  364. (dolist (binding
  365. (setq personal-keybindings
  366. (sort personal-keybindings
  367. (lambda (l r)
  368. (car (compare-keybindings l r))))))
  369. (if (not (eq (cdar last-binding) (cdar binding)))
  370. (princ (format "\n\n%s: %s\n%s\n\n"
  371. (cdar binding) (caar binding)
  372. (make-string (+ 21 (car bind-key-column-widths)
  373. (cdr bind-key-column-widths)) ?-)))
  374. (if (and last-binding
  375. (cdr (compare-keybindings last-binding binding)))
  376. (princ "\n")))
  377. (let* ((key-name (caar binding))
  378. (at-present (lookup-key (or (symbol-value (cdar binding))
  379. (current-global-map))
  380. (read-kbd-macro key-name)))
  381. (command (nth 1 binding))
  382. (was-command (nth 2 binding))
  383. (command-desc (get-binding-description command))
  384. (was-command-desc (and was-command
  385. (get-binding-description was-command)))
  386. (at-present-desc (get-binding-description at-present))
  387. )
  388. (let ((line
  389. (format
  390. (format "%%-%ds%%-%ds%%s\n" (car bind-key-column-widths)
  391. (cdr bind-key-column-widths))
  392. key-name (format "`%s\'" command-desc)
  393. (if (string= command-desc at-present-desc)
  394. (if (or (null was-command)
  395. (string= command-desc was-command-desc))
  396. ""
  397. (format "was `%s\'" was-command-desc))
  398. (format "[now: `%s\']" at-present)))))
  399. (princ (if (string-match "[ \t]+\n" line)
  400. (replace-match "\n" t t line)
  401. line))))
  402. (setq last-binding binding)))))
  403. (provide 'bind-key)
  404. ;; Local Variables:
  405. ;; outline-regexp: ";;;\\(;* [^\s\t\n]\\|###autoload\\)\\|("
  406. ;; indent-tabs-mode: nil
  407. ;; End:
  408. ;;; bind-key.el ends here