2015-09-14 02:53:15 +00:00
|
|
|
;;; core-keybindings.el --- Spacemacs Core File
|
|
|
|
;;
|
|
|
|
;; Copyright (c) 2012-2014 Sylvain Benner
|
|
|
|
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
|
|
|
|
;;
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
;;
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;;; License: GPLv3
|
2015-11-18 00:35:32 +00:00
|
|
|
|
|
|
|
(require 'core-funcs)
|
|
|
|
(unless (require 'which-key nil t)
|
|
|
|
(spacemacs/load-or-install-protected-package 'which-key t))
|
|
|
|
(unless (require 'bind-map nil t)
|
|
|
|
(spacemacs/load-or-install-protected-package 'bind-map t))
|
|
|
|
|
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-09-14 02:53:15 +00:00
|
|
|
(defun spacemacs/declare-prefix (prefix name &optional long-name)
|
|
|
|
"Declare a prefix PREFIX. PREFIX is a string describing a key
|
2015-09-16 14:39:01 +00:00
|
|
|
sequence. NAME is a string used as the prefix command.
|
2015-09-14 02:53:15 +00:00
|
|
|
LONG-NAME if given is stored in `spacemacs/prefix-titles'."
|
|
|
|
(let* ((command name)
|
|
|
|
(full-prefix (concat dotspacemacs-leader-key " " prefix))
|
|
|
|
(full-prefix-emacs (concat dotspacemacs-emacs-leader-key " " prefix))
|
|
|
|
(full-prefix-lst (listify-key-sequence (kbd full-prefix)))
|
|
|
|
(full-prefix-emacs-lst (listify-key-sequence
|
|
|
|
(kbd full-prefix-emacs))))
|
|
|
|
;; define the prefix command only if it does not already exist
|
|
|
|
(unless long-name (setq long-name name))
|
2015-11-19 02:05:03 +00:00
|
|
|
(which-key-declare-prefixes
|
|
|
|
full-prefix-emacs (cons name long-name)
|
|
|
|
full-prefix (cons name long-name))))
|
2015-09-14 02:53:15 +00:00
|
|
|
|
|
|
|
(defun spacemacs/declare-prefix-for-mode (mode prefix name &optional long-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."
|
|
|
|
(let ((command (intern (concat (symbol-name mode) name)))
|
|
|
|
(full-prefix (concat dotspacemacs-leader-key " " prefix))
|
2015-09-14 05:47:36 +00:00
|
|
|
(full-prefix-emacs (concat dotspacemacs-emacs-leader-key " " prefix))
|
|
|
|
(is-major-mode-prefix (string-prefix-p "m" prefix))
|
2015-11-28 05:13:31 +00:00
|
|
|
(major-mode-prefix (concat dotspacemacs-major-mode-leader-key
|
|
|
|
" " (substring prefix 1)))
|
|
|
|
(major-mode-prefix-emacs
|
|
|
|
(concat dotspacemacs-major-mode-emacs-leader-key
|
|
|
|
" " (substring prefix 1))))
|
2015-09-14 02:53:15 +00:00
|
|
|
(unless long-name (setq long-name name))
|
2015-09-14 05:47:36 +00:00
|
|
|
(let ((prefix-name (cons name long-name)))
|
2015-11-19 02:05:03 +00:00
|
|
|
(which-key-declare-prefixes-for-mode mode
|
|
|
|
full-prefix-emacs prefix-name
|
|
|
|
full-prefix prefix-name)
|
|
|
|
(when (and is-major-mode-prefix dotspacemacs-major-mode-leader-key)
|
|
|
|
(which-key-declare-prefixes-for-mode mode major-mode-prefix prefix-name))
|
|
|
|
(when (and is-major-mode-prefix dotspacemacs-major-mode-emacs-leader-key)
|
2015-11-28 05:13:31 +00:00
|
|
|
(which-key-declare-prefixes-for-mode
|
|
|
|
mode major-mode-prefix-emacs prefix-name)))))
|
|
|
|
|
2015-11-29 04:08:30 +00:00
|
|
|
(defun spacemacs//handle-terminal-keys ()
|
|
|
|
"Translate on the fly terminal special keys to access the emacs function key.
|
|
|
|
Translation occurs only the function key is effectively bound to a function."
|
|
|
|
(let ((kcode (and (not (display-graphic-p))
|
|
|
|
(aref (this-command-keys-vector) 0))))
|
|
|
|
(cond
|
|
|
|
((eq 9 kcode)
|
|
|
|
(let ((command (key-binding (kbd "<tab>"))))
|
|
|
|
(when command (setq this-command command))))
|
|
|
|
((eq 13 kcode)
|
|
|
|
(let ((command (key-binding (kbd "<return>"))))
|
|
|
|
(when command (setq this-command command)))))))
|
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)
|
|
|
|
|
|
|
|
(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."
|
|
|
|
(let ((prefix (intern (format "%s-prefix" map))))
|
|
|
|
(or (boundp prefix)
|
|
|
|
(progn
|
|
|
|
(eval
|
|
|
|
`(bind-map ,map
|
|
|
|
:prefix-cmd ,prefix
|
|
|
|
,(if minor :minor-modes :major-modes) (,mode)
|
|
|
|
:keys (,dotspacemacs-major-mode-emacs-leader-key
|
2015-11-24 01:52:32 +00:00
|
|
|
,(concat dotspacemacs-emacs-leader-key
|
|
|
|
(unless minor " m")))
|
2015-11-18 00:35:32 +00:00
|
|
|
:evil-keys (,dotspacemacs-major-mode-leader-key
|
2015-11-24 01:52:32 +00:00
|
|
|
,(concat dotspacemacs-leader-key
|
|
|
|
(unless minor " m")))))
|
2015-11-18 00:35:32 +00:00
|
|
|
(boundp prefix)))))
|
|
|
|
|
|
|
|
(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
|
|
|
|
they are in `spacemacs/set-leader-keys'."
|
|
|
|
(let* ((map (intern (format "spacemacs-%s-map" mode))))
|
|
|
|
(when (spacemacs//init-leader-mode-map mode map t)
|
|
|
|
(while key
|
2015-11-29 04:08:30 +00:00
|
|
|
(define-key (symbol-value 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-for-minor-mode 'lisp-indent-function 'defun)
|
2015-09-14 02:53:15 +00:00
|
|
|
|
|
|
|
(provide 'core-keybindings)
|