Skip to content

Commit

Permalink
hybrid-mode: Only remove default insert commands
Browse files Browse the repository at this point in the history
Add hybrid-mode-insert-state-map

Factor out spacemacs stuff

All spacemacs specific configuration is done in spacemacs/packages.el
now. This makes hybrid-mode effectively a standalone package.

Also add a comment explaining the keymap setup code.
  • Loading branch information
justbur authored and syl20bnr committed Aug 15, 2015
1 parent 5b04a46 commit 84ad24f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
88 changes: 62 additions & 26 deletions spacemacs/local/hybrid-mode/hybrid-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,51 @@

;;; Code:

(defvar hybrid-mode-evil-insert-state-keys
'("\C-v" "\C-k" "\C-o" "\C-r" "\C-y" "\C-e" "\C-n" "\C-p" "\C-x\C-n"
"\C-x\C-p" "\C-t" "\C-d" "\C-a" "\C-w" [remap delete-backward-char]
[delete])
(require 'evil)

(defgroup hybrid-mode nil
"Customization options for hybrid-mode"
:group 'emulations
:prefix "hybrid-mode-")

(defcustom hybrid-mode-insert-state-cursor 'bar
"Cursor spec for hybrid mode in insert state."
:group 'hybrid-mode)

(defvar hybrid-mode-default-evil-insert-state-bindings
`(("\C-v" . quoted-insert)
("\C-k" . evil-insert-digraph)
("\C-o" . evil-execute-in-normal-state)
("\C-r" . evil-paste-from-register)
("\C-y" . evil-copy-from-above)
("\C-e" . evil-copy-from-below)
("\C-n" . evil-complete-next)
("\C-p" . evil-complete-previous)
("\C-p" . hippie-expand) ;; auto-complete layer sets this
("\C-w" . evil-window-map)
("\C-w" . evil-delete-backward-word)
("\C-x\C-n" . evil-complete-next-line)
("\C-x\C-p" . evil-complete-previous-line)
("\C-t" . evil-shift-right-line)
("\C-d" . evil-shift-left-line)
("\C-a" . evil-paste-last-insertion)
([remap delete-backward-char] . evil-delete-backward-char-and-join)
([delete] . delete-char)
(,(read-kbd-macro evil-toggle-key) . evil-emacs-state))
"Taken from evil-maps.el")

(defvar hybrid-mode-insert-state-map-backup nil
"Backup of `evil-insert-state-map'.")

(defvar hybrid-mode-insert-state-cursor-backup nil
(defvar hybrid-mode-insert-state-cursor-backup evil-insert-state-cursor
"Backup of `evil-insert-state-cursor'")

(defface hybrid-mode-insert-face-backup
'((t . (:inherit spacemacs-insert-face)))
"Backup of `spacemacs-insert-face'")
(defvar hybrid-mode-insert-state-map (make-sparse-keymap)
"Keymap that only applies in insert mode with hybrid mode
activated.")

(defvar hybrid-mode-insert-state-map-active nil
"Flag for acitvating `hybrid-mode-insert-state-map'")

;;;###autoload
(define-minor-mode hybrid-mode
Expand All @@ -56,28 +86,34 @@
(hybrid-mode-setup-keymaps)
(hybrid-mode-restore-keymaps)))

(defun hybrid-mode-activate-keymap ()
"Activate `hybrid-mode-insert-state-map'"
(setq hybrid-mode-insert-state-map-active t))

(defun hybrid-mode-deactivate-keymap ()
"Deactivate `hybrid-mode-insert-state-map'"
(setq hybrid-mode-insert-state-map-active nil))

(defun hybrid-mode-setup-keymaps ()
"Enter the church of Emacs in insert mode only."
(add-hook 'evil-insert-state-entry-hook 'hybrid-mode-activate-keymap)
(add-hook 'evil-insert-state-exit-hook 'hybrid-mode-deactivate-keymap)
(add-to-list 'minor-mode-map-alist
`(hybrid-mode-insert-state-map-active . ,hybrid-mode-insert-state-map))
(setq hybrid-mode-insert-state-map-backup evil-insert-state-map
hybrid-mode-insert-state-cursor-backup evil-insert-state-cursor)
(when (display-graphic-p)
(setq evil-insert-state-cursor (list (car evil-emacs-state-cursor)
(cadr evil-insert-state-cursor))))
(copy-face 'spacemacs-insert-face 'hybrid-mode-insert-face-backup)
(copy-face 'spacemacs-emacs-face 'spacemacs-insert-face)
;; (setcdr evil-insert-state-map nil)
;; (define-key evil-insert-state-map [escape] 'evil-normal-state)
(dolist (key hybrid-mode-evil-insert-state-keys)
(define-key evil-insert-state-map key nil))
(define-key evil-insert-state-map
(read-kbd-macro evil-toggle-key) nil)
;; see if the above works withou this
;; (when (bound-and-true-p evil-escape-mode)
;; (define-key evil-insert-state-map "f" 'evil-escape-insert-state))
)
hybrid-mode-insert-state-cursor-backup evil-insert-state-cursor
evil-insert-state-cursor hybrid-mode-insert-state-cursor)
;; Remove default evil insert mode bindings from `evil-insert-state-map'. This
;; will not remove any bindings that the user explicitly set in this keymap,
;; the important ones being any bindings related to escaping insert mode.
(dolist (key-bnd hybrid-mode-default-evil-insert-state-bindings)
(when (eq (cdr key-bnd) (lookup-key evil-insert-state-map (car key-bnd)))
(define-key evil-insert-state-map (car key-bnd) nil))))

(defun hybrid-mode-restore-keymaps ()
"Go home."
(hybrid-mode-deactivate-keymap)
(remove-hook 'evil-insert-state-entry-hook 'hybrid-mode-activate-keymap)
(remove-hook 'evil-insert-state-exit-hook 'hybrid-mode-deactivate-keymap)
(setq evil-insert-state-map hybrid-mode-insert-state-map-backup
evil-insert-state-cursor hybrid-mode-insert-state-cursor-backup)
(copy-face 'hybrid-mode-insert-face-backup 'spacemacs-insert-face))
evil-insert-state-cursor hybrid-mode-insert-state-cursor-backup))
15 changes: 15 additions & 0 deletions spacemacs/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,21 @@ Put (global-hungry-delete-mode) in dotspacemacs/config to enable by default."
:commands hybrid-mode
:init
(progn
(add-to-list 'spacemacs-evil-cursor-colors
`(hybrid . ,(spacemacs/state-color 'emacs)))
(spacemacs/set-state-faces)
(setq hybrid-mode-insert-state-cursor
(if (and (display-graphic-p)
dotspacemacs-colorize-cursor-according-to-state)
`(,(spacemacs/state-color 'hybrid) (bar . 2))
'bar))
(copy-face 'spacemacs-insert-face 'spacemacs-insert-face-backup)
(add-hook 'hybrid-mode-hook
(lambda ()
(copy-face (if hybrid-mode
'spacemacs-emacs-face
'spacemacs-insert-face-backup)
'spacemacs-insert-face)))
(when (eq 'hybrid dotspacemacs-editing-style)
(hybrid-mode))
(spacemacs|add-toggle hybrid-mode
Expand Down

0 comments on commit 84ad24f

Please sign in to comment.