 
 ;; Copyright (C) 2014-2015 syl20bnr
 ;;
-;; Author: Justin Burkett <justin@burkett.cc>
+;; Author: Justin Burkett <justin@burkett.cc>, Chris Ewald <chrisewald@gmail.com>
 ;; Keywords: convenience editing
 ;; Created: 12 Aug 2015
 ;; Version: 1.00

      (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
        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))

  "Cursor spec for hybrid mode in insert state."
  :group 'hybrid-mode)

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

(defvar hybrid-mode-emacs-state-map (make-sparse-keymap)
  "Keymap that only applies in insert mode with hybrid mode
activated. Inherits bindings from `evil-emacs-state-map', which
may be overridden here.")

(defvar hybrid-mode-insert-state-local-map (make-sparse-keymap)
  "Local keymap for hybrid-mode. May be customized. Sets
  `evil-insert-state-local-map' as its parent. Takes precedence
  over `hybrid-mode-emacs-state-map'.")

;;;###autoload
(define-minor-mode hybrid-mode
  "Global minor mode to repulse the evil from spacemacs (in insert mode).
Emacs in insert mode. Replaces the `evil-insert-state' keymap
with `hybrid-mode-emacs-state-map'."
  :global t
  :lighter " hybrid"
  :group 'spacemacs
