2015-09-14 02:53:15 +00:00
|
|
|
;;; core-keybindings.el --- Spacemacs Core File
|
|
|
|
;;
|
2021-03-22 20:11:29 +00:00
|
|
|
;; Copyright (c) 2012-2021 Sylvain Benner & Contributors
|
2015-09-14 02:53:15 +00:00
|
|
|
;;
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
;;
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
2021-03-24 03:31:44 +00:00
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-11-18 00:35:32 +00:00
|
|
|
|
|
|
|
(require 'core-funcs)
|
|
|
|
|
2015-09-14 02:53:15 +00:00
|
|
|
(defvar spacemacs/prefix-titles nil
|
|
|
|
"alist for mapping command prefixes to long names.")
|
|
|
|
|
2015-11-18 00:35:32 +00:00
|
|
|
(defvar spacemacs-default-map (make-sparse-keymap)
|
|
|
|
"Base keymap for all spacemacs leader key commands.")
|
|
|
|
|
2015-12-02 00:25:37 +00:00
|
|
|
(defun spacemacs/translate-C-i (_)
|
2015-12-04 14:37:14 +00:00
|
|
|
"If `dotspacemacs-distinguish-gui-tab' is non nil, the raw key
|
|
|
|
sequence does not include <tab> or <kp-tab>, and we are in the
|
|
|
|
gui, translate to [C-i]. Otherwise, [9] (TAB)."
|
2015-12-02 00:25:37 +00:00
|
|
|
(interactive)
|
2015-12-04 14:37:14 +00:00
|
|
|
(if (and (not (cl-position 'tab (this-single-command-raw-keys)))
|
|
|
|
(not (cl-position 'kp-tab (this-single-command-raw-keys)))
|
|
|
|
dotspacemacs-distinguish-gui-tab
|
|
|
|
(display-graphic-p))
|
|
|
|
[C-i] [?\C-i]))
|
2015-12-02 00:25:37 +00:00
|
|
|
(define-key key-translation-map [?\C-i] 'spacemacs/translate-C-i)
|
|
|
|
|
|
|
|
;; (defun spacemacs/translate-C-m (_)
|
2015-12-04 14:37:14 +00:00
|
|
|
;; "If `dotspacemacs-distinguish-gui-ret' is non nil, the raw key
|
|
|
|
;; sequence does not include <ret>, and we are in the gui, translate
|
|
|
|
;; to [C-m]. Otherwise, [9] (TAB)."
|
2015-12-02 00:25:37 +00:00
|
|
|
;; (interactive)
|
2015-12-04 14:37:14 +00:00
|
|
|
;; (if (and
|
|
|
|
;; (not (cl-position 'return (this-single-command-raw-keys)))
|
|
|
|
;; (not (cl-position 'kp-enter (this-single-command-raw-keys)))
|
|
|
|
;; dotspacemacs-distinguish-gui-ret
|
|
|
|
;; (display-graphic-p))
|
|
|
|
;; [C-m] [?\C-m]))
|
2015-12-02 00:25:37 +00:00
|
|
|
;; (define-key key-translation-map [?\C-m] 'spacemacs/translate-C-m)
|
|
|
|
|
2020-12-20 17:03:07 +00:00
|
|
|
(defun spacemacs/declare-prefix (prefix name &optional _)
|
2015-09-14 02:53:15 +00:00
|
|
|
"Declare a prefix PREFIX. PREFIX is a string describing a key
|
2020-12-20 17:03:07 +00:00
|
|
|
sequence. NAME is a string used as the prefix command."
|
|
|
|
(which-key-add-keymap-based-replacements spacemacs-default-map
|
|
|
|
prefix name))
|
2017-02-13 04:44:52 +00:00
|
|
|
(put 'spacemacs/declare-prefix 'lisp-indent-function 'defun)
|
2015-09-14 02:53:15 +00:00
|
|
|
|
2020-12-20 17:03:07 +00:00
|
|
|
(defun spacemacs/declare-prefix-for-mode (mode prefix name &optional _)
|
2015-09-14 02:53:15 +00:00
|
|
|
"Declare a prefix PREFIX. MODE is the mode in which this prefix command should
|
|
|
|
be added. PREFIX is a string describing a key sequence. NAME is a symbol name
|
|
|
|
used as the prefix command."
|
2020-12-20 17:03:07 +00:00
|
|
|
(let* ((is-major-mode-prefix (string-prefix-p "m" prefix))
|
|
|
|
(is-minor-mode-prefix (not is-major-mode-prefix))
|
|
|
|
(smap (intern (format "spacemacs-%s-map" mode))))
|
|
|
|
(when (spacemacs//init-leader-mode-map mode smap is-minor-mode-prefix)
|
|
|
|
(which-key-add-keymap-based-replacements (symbol-value smap)
|
|
|
|
(if is-major-mode-prefix (substring prefix 1) prefix) name))))
|
2017-02-13 04:44:52 +00:00
|
|
|
(put 'spacemacs/declare-prefix-for-mode 'lisp-indent-function 'defun)
|
2015-11-28 05:13:31 +00:00
|
|
|
|
2015-11-18 00:35:32 +00:00
|
|
|
(defun spacemacs/set-leader-keys (key def &rest bindings)
|
|
|
|
"Add KEY and DEF as key bindings under
|
|
|
|
`dotspacemacs-leader-key' and `dotspacemacs-emacs-leader-key'.
|
|
|
|
KEY should be a string suitable for passing to `kbd', and it
|
|
|
|
should not include the leaders. DEF is most likely a quoted
|
|
|
|
command. See `define-key' for more information about the possible
|
|
|
|
choices for DEF. This function simply uses `define-key' to add
|
|
|
|
the bindings.
|
|
|
|
|
|
|
|
For convenience, this function will accept additional KEY DEF
|
|
|
|
pairs. For example,
|
|
|
|
|
|
|
|
\(spacemacs/set-leader-keys
|
|
|
|
\"a\" 'command1
|
|
|
|
\"C-c\" 'command2
|
|
|
|
\"bb\" 'command3\)"
|
|
|
|
(while key
|
2015-11-29 04:08:30 +00:00
|
|
|
(define-key spacemacs-default-map (kbd key) def)
|
2015-11-18 00:35:32 +00:00
|
|
|
(setq key (pop bindings) def (pop bindings))))
|
2015-11-21 17:32:53 +00:00
|
|
|
(put 'spacemacs/set-leader-keys 'lisp-indent-function 'defun)
|
2015-11-18 00:35:32 +00:00
|
|
|
|
|
|
|
(defalias 'evil-leader/set-key 'spacemacs/set-leader-keys)
|
|
|
|
|
2015-12-17 16:06:37 +00:00
|
|
|
(defun spacemacs//acceptable-leader-p (key)
|
|
|
|
"Return t if key is a string and non-empty."
|
|
|
|
(and (stringp key) (not (string= key ""))))
|
|
|
|
|
2015-11-18 00:35:32 +00:00
|
|
|
(defun spacemacs//init-leader-mode-map (mode map &optional minor)
|
|
|
|
"Check for MAP-prefix. If it doesn't exist yet, use `bind-map'
|
|
|
|
to create it and bind it to `dotspacemacs-major-mode-leader-key'
|
|
|
|
and `dotspacemacs-major-mode-emacs-leader-key'. If MODE is a
|
|
|
|
minor-mode, the third argument should be non nil."
|
2015-12-17 16:06:37 +00:00
|
|
|
(let* ((prefix (intern (format "%s-prefix" map)))
|
|
|
|
(leader1 (when (spacemacs//acceptable-leader-p
|
|
|
|
dotspacemacs-major-mode-leader-key)
|
|
|
|
dotspacemacs-major-mode-leader-key))
|
|
|
|
(leader2 (when (spacemacs//acceptable-leader-p
|
|
|
|
dotspacemacs-leader-key)
|
2016-12-16 13:52:32 +00:00
|
|
|
(concat dotspacemacs-leader-key " m")))
|
2015-12-17 16:06:37 +00:00
|
|
|
(emacs-leader1 (when (spacemacs//acceptable-leader-p
|
|
|
|
dotspacemacs-major-mode-emacs-leader-key)
|
|
|
|
dotspacemacs-major-mode-emacs-leader-key))
|
|
|
|
(emacs-leader2 (when (spacemacs//acceptable-leader-p
|
|
|
|
dotspacemacs-emacs-leader-key)
|
2016-12-16 13:52:32 +00:00
|
|
|
(concat dotspacemacs-emacs-leader-key " m")))
|
2015-12-21 06:30:48 +00:00
|
|
|
(leaders (delq nil (list leader1 leader2)))
|
|
|
|
(emacs-leaders (delq nil (list emacs-leader1 emacs-leader2))))
|
2015-11-18 00:35:32 +00:00
|
|
|
(or (boundp prefix)
|
|
|
|
(progn
|
|
|
|
(eval
|
|
|
|
`(bind-map ,map
|
|
|
|
:prefix-cmd ,prefix
|
|
|
|
,(if minor :minor-modes :major-modes) (,mode)
|
2015-12-17 16:06:37 +00:00
|
|
|
:keys ,emacs-leaders
|
2016-01-08 19:07:00 +00:00
|
|
|
:evil-keys ,leaders
|
|
|
|
:evil-states (normal motion visual evilified)))
|
2015-11-18 00:35:32 +00:00
|
|
|
(boundp prefix)))))
|
|
|
|
|
2021-05-12 00:45:45 +00:00
|
|
|
(defun spacemacs/inherit-leader-keys-from-parent-mode (mode &optional parent-mode)
|
|
|
|
"Make derived mode MODE inherit leader key bindings from PARENT-MODE.
|
|
|
|
If omitted, PARENT-MODE defaults to the parent mode of MODE.
|
|
|
|
Signal an error if MODE is not a derived mode (for example if the
|
|
|
|
package defining the mode has not yet been loaded)."
|
|
|
|
(unless parent-mode
|
|
|
|
(setq parent-mode (or (get mode 'derived-mode-parent)
|
|
|
|
(error "Mode %s has no parent" mode))))
|
|
|
|
(let ((map (intern (format "spacemacs-%s-map" mode)))
|
|
|
|
(parent-map (intern (format "spacemacs-%s-map" parent-mode))))
|
|
|
|
(when (and (spacemacs//init-leader-mode-map mode map)
|
|
|
|
(spacemacs//init-leader-mode-map parent-mode parent-map))
|
|
|
|
(set-keymap-parent (symbol-value map) (symbol-value parent-map)))))
|
|
|
|
|
2015-11-18 00:35:32 +00:00
|
|
|
(defun spacemacs/set-leader-keys-for-major-mode (mode key def &rest bindings)
|
|
|
|
"Add KEY and DEF as key bindings under
|
|
|
|
`dotspacemacs-major-mode-leader-key' and
|
|
|
|
`dotspacemacs-major-mode-emacs-leader-key' for the major-mode
|
|
|
|
MODE. MODE should be a quoted symbol corresponding to a valid
|
|
|
|
major mode. The rest of the arguments are treated exactly like
|
|
|
|
they are in `spacemacs/set-leader-keys'."
|
|
|
|
(let* ((map (intern (format "spacemacs-%s-map" mode))))
|
|
|
|
(when (spacemacs//init-leader-mode-map mode map)
|
|
|
|
(while key
|
|
|
|
(define-key (symbol-value map) (kbd key) def)
|
|
|
|
(setq key (pop bindings) def (pop bindings))))))
|
2015-11-21 17:32:53 +00:00
|
|
|
(put 'spacemacs/set-leader-keys-for-major-mode 'lisp-indent-function 'defun)
|
2015-11-18 00:35:32 +00:00
|
|
|
|
2015-11-28 05:13:31 +00:00
|
|
|
(defalias
|
|
|
|
'evil-leader/set-key-for-mode
|
|
|
|
'spacemacs/set-leader-keys-for-major-mode)
|
2015-09-14 02:53:15 +00:00
|
|
|
|
2015-11-18 00:35:32 +00:00
|
|
|
(defun spacemacs/set-leader-keys-for-minor-mode (mode key def &rest bindings)
|
|
|
|
"Add KEY and DEF as key bindings under
|
|
|
|
`dotspacemacs-major-mode-leader-key' and
|
|
|
|
`dotspacemacs-major-mode-emacs-leader-key' for the minor-mode
|
|
|
|
MODE. MODE should be a quoted symbol corresponding to a valid
|
|
|
|
minor mode. The rest of the arguments are treated exactly like
|
2020-11-21 06:34:55 +00:00
|
|
|
they are in `spacemacs/set-leader-keys'. If DEF is string, then
|
|
|
|
it is treated as a prefix not a command."
|
2015-11-18 00:35:32 +00:00
|
|
|
(let* ((map (intern (format "spacemacs-%s-map" mode))))
|
|
|
|
(when (spacemacs//init-leader-mode-map mode map t)
|
2020-11-21 06:34:55 +00:00
|
|
|
(let ((map-value (symbol-value map)))
|
|
|
|
(while key
|
|
|
|
(if (stringp def)
|
|
|
|
(which-key-add-keymap-based-replacements map-value key def)
|
|
|
|
(define-key map-value (kbd key) def))
|
|
|
|
(setq key (pop bindings) def (pop bindings)))))))
|
2015-11-21 17:32:53 +00:00
|
|
|
(put 'spacemacs/set-leader-keys-for-minor-mode 'lisp-indent-function 'defun)
|
2015-09-14 02:53:15 +00:00
|
|
|
|
2020-11-21 06:34:55 +00:00
|
|
|
(defun spacemacs/declare-prefix-for-minor-mode (mode prefix name)
|
|
|
|
"Declare a prefix PREFIX. MODE is the mode in which this prefix command should
|
|
|
|
be added. PREFIX is a string describing a key sequence. NAME is a symbol name
|
|
|
|
used as the prefix command.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
\(spacemacs/declare-prefix-for-minor-mode 'tide-mode \"E\" \"errors\"\)"
|
|
|
|
|
|
|
|
(let* ((map (intern (format "spacemacs-%s-map" mode))))
|
|
|
|
(when (spacemacs//init-leader-mode-map mode map t)
|
|
|
|
(which-key-add-keymap-based-replacements (symbol-value map) prefix name))))
|
|
|
|
|
2015-09-14 02:53:15 +00:00
|
|
|
(provide 'core-keybindings)
|