 
 ;; 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

  "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 evil-insert-state-cursor
  "Backup of `evil-insert-state-cursor'")

(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
  "Global minor mode to repulse the evil from spacemacs (in insert mode).

`evil-insert-state-map' is prevented from shadowing emacs key bindings."
  :global t
  :lighter " hybrid"
  :group 'spacemacs

      (hybrid-mode-setup-keymaps)
    (hybrid-mode-restore-keymaps)))

(defun hybrid-mode-setup-keymaps ()
  "Enter the church of Emacs in insert mode only."

  ;; Backup and set the insert-state-cursor to the hybrid variant.
  (setq hybrid-mode-insert-state-cursor-backup evil-insert-state-cursor
        evil-insert-state-cursor hybrid-mode-insert-state-cursor)

  ;; Create the hybrid mode parent keymap by overriding the essential changes
  ;; from `evil-emacs-state-map'. Set this as the parent keymap of
  ;; `hybrid-mode-emacs-state-map' so that all key bindings may be overridden.
  (setq hybrid-mode-parent-map (make-sparse-keymap))
  (set-keymap-parent hybrid-mode-parent-map evil-emacs-state-map)
  (define-key hybrid-mode-parent-map (read-kbd-macro evil-toggle-key) nil)
  (define-key hybrid-mode-parent-map [escape] 'evil-normal-state)
  (set-keymap-parent hybrid-mode-emacs-state-map hybrid-mode-parent-map)

  ;; Set evil-insert-state-local-map to be the parent map of
  ;; hybrid-mode-insert-state-local-map. Makes for a more clear category
  ;; `helm-descbinds'.
  (set-keymap-parent hybrid-mode-insert-state-local-map evil-insert-state-local-map)

  ;; Override the mode and keymap of evil-insert-state to use the hybrid-mode
  ;; variants.
  (evil-put-property 'evil-state-properties 'insert
                     :mode 'hybrid-mode-emacs-state
                     :keymap 'hybrid-mode-emacs-state-map
                     :local 'hybrid-mode-local-insert-state
                     :local-keymap 'hybrid-mode-insert-state-local-map))

(defun hybrid-mode-restore-keymaps ()
  "Restore `evil-insert-state' to it's original form."
  ;; Return evil-insert-state cursor and properties to how they are defined in
  ;; evil-states.el
  (setq evil-insert-state-cursor hybrid-mode-insert-state-cursor-backup)
  (evil-put-property 'evil-state-properties 'insert
                     :mode 'evil-insert-state-minor-mode
                     :keymap 'evil-insert-state-map
                     :local 'evil-insert-state-local-minor-mode
                     :local-keymap 'evil-insert-state-local-map))
