From 5a008df3f16ca7941097a966365f551b91b0d3fa Mon Sep 17 00:00:00 2001
From: York Zhao <gtdplatform@gmail.com>
Date: Thu, 21 Apr 2011 16:01:15 -0400
Subject: [PATCH 3/3] 'Ctrl-o' in insert mode

Implemented 'Ctrl-o' key in insert mode to switch to command mode for one
command then switch back to insert mode.

Note: Currently there is a problem with this command that changes before
pressing 'Ctrl-o' will no longer be available for 'dot repeating'.
---
 vimpulse-misc-keybindings.el |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/vimpulse-misc-keybindings.el b/vimpulse-misc-keybindings.el
index d488c53..ac88199 100644
--- a/vimpulse-misc-keybindings.el
+++ b/vimpulse-misc-keybindings.el
@@ -117,6 +117,8 @@ Equivalent to Vim's C-w prefix.")
 
 ;; Insert mode paste by preesing "C-r register"
 (define-key viper-insert-basic-map "\C-r" 'paste-in-insert-mode)
+;; Escape to command mode termporarily by pressing "C-o" in insert mode
+(define-key viper-insert-basic-map "\C-o" 'viper-escape-to-vi-state)
 
 ;;; "
 
@@ -623,4 +625,40 @@ COL defaults to the current column."
   (forward-char)
  )
 
+;; Escape to command mode termporarily
+(defun viper-escape-to-vi-state (arg)
+  "Escape from insert mode to command mode for one Vi command.
+If the Vi command that the user types has a prefix argument,
+e.g., `d2w', then Vi's prefix argument will be used. Otherwise,
+the prefix argument passed to 'viper-escape-to-vi-state' is used."
+  (interactive "P")
+  (message "Switched to command mode for the next command...")
+  (viper-change-state-to-vi)
+  (if viper-ESC-moves-cursor-back
+      (viper-forward-char 1))
+  (let (com key)
+    ;; protect against keyboard quit and other errors
+    (condition-case nil
+	(progn
+	  (unwind-protect
+	      (progn
+		(setq com
+		      (key-binding (setq key (viper-read-key-sequence nil))))
+		;; In case of binding indirection--chase definitions.
+		;; Have to do it here because we execute this command under
+		;; different keymaps, so command-execute may not do the
+		;; right thing there
+		(while (vectorp com) (setq com (key-binding com))))
+	    nil)
+
+	  (if (commandp com)
+	      (progn 
+		(setq prefix-arg (or prefix-arg arg))
+		(command-execute com)))
+  	  )
+      (quit (ding))
+      (error (beep 1))))
+    (viper-change-state-to-insert)
+  )
+
 (provide 'vimpulse-misc-keybindings)
-- 
1.7.4

