vim-mode / vim-insert-mode.el

;;; vim-insert-mode.el - VIM insert-mode.

;; Copyright (C) 2009, 2010 Frank Fischer

;; Author: Frank Fischer <>,
;; This file is not part of GNU Emacs.

;;; TODO :
;; - for some reason GNU Emacs does not show '-- REPLACE --'

(provide 'vim-insert-mode)

(vim:deflocalvar vim:last-insert-undo nil)

(defcustom vim:insert-mode-replace-cursor 'hbar
  "Cursor for replace-mode."
  :group 'vim-mode)

(defconst vim:insert-mode-keymap (vim:make-keymap)
  "VIM insert-mode keymap.")

(defconst vim:insert-mode-local-keymap (vim:make-keymap)
  "VIM insert-mode local keymap.")

(defsubst vim:imap (keys command)
  "Defines a new insert-mode mapping."
  (vim:map keys command :keymap vim:insert-mode-keymap))

(defsubst vim:local-imap (keys command)
  "Defines a new buffer local insert-mode mapping."
  (vim:map keys command :keymap vim:insert-mode-local-keymap))

(vim:define-mode insert "VIM insert-mode"
                 :ident "I"
                 :message "-- INSERT --"
                 :keymap vim:insert-mode-keymap
                 :local-keymap vim:insert-mode-local-keymap
                 :command-function 'vim:insert-mode-command
                 :cursor 'bar)
(add-hook 'vim:insert-mode-on-hook 'vim:insert-mode-activated)
(add-hook 'vim:insert-mode-off-hook 'vim:insert-mode-deactivated)

(vim:defcmd vim:insert-mode-toggle-replace ()
  "Toggles overwrite-mode in insert-mode."
  (unless (vim:insert-mode-p)
    (error "Toggling overwrite-mode only allowed in insert-mode."))
  (overwrite-mode nil)
  (if overwrite-mode
        (let (message-log-max) (message "-- REPLACE --"))
        (setq cursor-type vim:insert-mode-replace-cursor)
        (vim:update-mode-line "R"))
      (let (message-log-max) (message "-- INSERT --"))
      (setq cursor-type vim:insert-mode-cursor)
      (vim:update-mode-line "I"))))

(defun vim:insert-mode-command (command)
  "Executes a simple command in insert mode."
  (case (vim:cmd-type command)
    ('simple (vim:normal-execute-simple-command command))
    ('complex (error "No complex command allowed in insert-mode."))
    (t (vim:normal-execute-motion command))))

(defun vim:insert-mode-activated ()
  "Called when insert-mode is activated."
  (overwrite-mode -1)
  (setq vim:last-insert-undo vim:last-undo)
  (add-hook 'pre-command-hook 'vim:insert-save-key-sequence))
(defun vim:insert-mode-deactivated ()
  "Called when insert-mode is deactivated."
  (overwrite-mode -1)
  (vim:set-mark ?^)
  (remove-hook 'pre-command-hook 'vim:insert-save-key-sequence)
  ;; the command that has just ended insert-mode should NOT be repeatable
  ;; and will therefore NOT override repeat-sequence.
  (setq vim:repeat-events (vconcat vim:repeat-events
  (setq vim:last-undo vim:last-insert-undo))

(defun vim:insert-save-key-sequence ()
  "Called in insert-mode to save key-events."
  (when (and (vim:toplevel-execution)
             (not (eq this-command 'vim:intercept-ESC)))
    (setq vim:current-key-sequence (vconcat vim:current-key-sequence

;;; vim-insert-mode.el ends here