Commits

Frank Fischer  committed c32b4da

add repeating of insert-mode

  • Participants
  • Parent commits c7a4e30
  • Tags v0.4

Comments (0)

Files changed (2)

File vim-commands.el

 ;;; vim-commands.el - Implementation of VIM commands.
 
-;; Copyright (C) 2009, 2010 Frank Fischer
+;; Copyright (C) 2009, 2010, 2011 Frank Fischer
 
 ;; Author: Frank Fischer <frank.fischer@mathematik.tu-chemnitz.de>,
 ;;
 
 (vim:defcmd vim:cmd-insert (count)
   "Switches to insert-mode before point."
-  (vim:activate-insert-mode))
+  (vim:start-insert-mode count))
 
 (vim:defcmd vim:cmd-append (count)
   "Switches to insert-mode after point."
   (unless (eolp) (forward-char))
-  (vim:activate-insert-mode))
+  (vim:start-insert-mode count))
 
 (vim:defcmd vim:cmd-Insert (count)
   "Moves the cursor to the beginning of the current line
 
 (vim:defcmd vim:cmd-insert-line-above (count)
   "Inserts a new line above the current one and goes to insert mode."
-  (vim:motion-beginning-of-line)
-  (newline)
-  (forward-line -1)
-  (indent-according-to-mode)
-  (vim:cmd-Insert))
+  (vim:start-insert-mode count 'above))
 
 (vim:defcmd vim:cmd-insert-line-below (count)
   "Inserts a new line below the current one and goes to insert mode."
-  (vim:motion-end-of-line)
-  (newline)
-  (indent-according-to-mode)
-  (vim:cmd-insert))
+  (vim:start-insert-mode count 'below))
 
 (vim:defcmd vim:cmd-replace (count)
   "Goes to replace-mode."
   (vim:reset-key-state)
   ;;(dotimes (i (or count 1))
     (let ((repeat-events vim:repeat-events)
-          (vim:repeat-events nil))
+	  (current-key-sequence vim:current-key-sequence)
+          vim:repeat-events
+	  vim:current-key-sequence)
       (execute-kbd-macro repeat-events)))
 
 

File vim-insert-mode.el

 ;;; vim-insert-mode.el - VIM insert-mode.
 
-;; Copyright (C) 2009, 2010 Frank Fischer
+;; Copyright (C) 2009, 2010, 2011 Frank Fischer
 
 ;; Author: Frank Fischer <frank.fischer@mathematik.tu-chemnitz.de>,
 ;;
 
 (vim:deflocalvar vim:last-insert-undo nil)
 
+(vim:deflocalvar vim:insert-count nil
+  "The number of times the insertion should be repeated.")
+
+(vim:deflocalvar vim:insert-marker nil
+  "A marker which is placed at the point where insertion started.")
+
 (defcustom vim:insert-mode-replace-cursor 'hbar
   "Cursor for replace-mode."
   :group 'vim-cursors)
       (setq cursor-type vim:insert-mode-cursor)
       (vim:update-mode-line "I"))))
 
+(defun vim:insert-mode-insert-newline ()
+  "Inserts a newline according to current insert-mode direction."
+  (case vim:insert-newline
+    (above
+     (vim:motion-beginning-of-line)
+     (newline)
+     (forward-line -1)
+     (indent-according-to-mode))
+    (below
+     (vim:motion-end-of-line)
+     (newline)
+     (indent-according-to-mode))))
+
+(defun vim:start-insert-mode (&optional count newline)
+  "Activates insert-mode with a certain repeat `count'.
+`newline' should be 'above or 'below or nil which determines
+where to insert a newline."
+  (setq vim:insert-count count
+	vim:insert-newline newline)
+  (vim:insert-mode-insert-newline)
+  (when (eq vim:insert-newline 'above)
+    (setq vim:insert-newline 'below))
+  (if (eobp)
+      (setq vim:insert-marker 'eob)
+    (setq vim:insert-marker (make-marker))
+    (move-marker vim:insert-marker (1+ (point))))
+  (vim:activate-insert-mode))
+
 (defun vim:insert-mode-command (command)
   "Executes a simple command in insert mode."
   (case (vim:cmd-type command)
   ;; and will therefore NOT override repeat-sequence.
   (setq vim:repeat-events (vconcat vim:repeat-events
                                    vim:current-key-sequence))
-  (setq vim:last-undo vim:last-insert-undo))
+  (setq vim:last-undo vim:last-insert-undo)
+
+  ;; repeat insertion
+  (dotimes (i (1- (or vim:insert-count 1)))
+    (goto-char (if (eq vim:insert-marker 'eob)
+		   (point-max)
+		 (1- vim:insert-marker)))
+    (vim:insert-mode-insert-newline)
+    (execute-kbd-macro vim:current-key-sequence))
+  (when (and vim:insert-marker
+	     (not (eq vim:insert-marker 'eob)))
+    (move-marker vim:insert-marker nil))
+  (setq vim:insert-marker nil
+	vim:insert-count nil))
+
 
 (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)))
+  (unless (eq this-command 'vim:intercept-ESC)
     (setq vim:current-key-sequence (vconcat vim:current-key-sequence
                                             (vim:this-command-keys)))))