Frank Fischer  committed 4a6e08f

add :setmode command to change default-mode from ex

  • Participants
  • Parent commits e38293d

Comments (0)

Files changed (5)

File vim-ex-commands.el

   (vim:cmd-write :argument file :force force)
+(defun vim:ex-complete-mode-argument (mode predicate flag)
+  "Completes a registered vim-mode submode."
+  (when mode
+    (let ((modes (mapcar #'cdr vim:mode-alist)))
+      (with-current-buffer vim:ex-current-buffer
+	(case flag
+	  ((nil) (try-completion mode modes predicate))
+	  ((t) (all-completions mode modes predicate))
+	  ((lambda) (vim:test-completion mode modes predicate)))))))
+(vim:define-arg-handler 'mode
+			:complete 'vim:ex-complete-mode-argument)
+(vim:defcmd vim:cmd-setmode ((argument:mode mode) nonrepeatable)
+  "Changes the default start mode of the current major-mode."
+  (when (zerop (length mode)) (setq mode nil))
+  (let ((valid-mode (rassoc mode vim:mode-alist))
+	(start-mode (and mode (intern mode))))
+    (if (and start-mode (not valid-mode))
+	(error "No such vim-mode submode: %s" start-mode)
+      (with-current-buffer vim:ex-current-buffer
+	(let* ((mmode (assoc major-mode vim:initial-modes))
+	       (current-mode (cdr-safe mmode)))
+	  (unless (eq current-mode start-mode)
+	    ;; only if we selected a new mode
+	    (when (y-or-n-p (format "Major-mode `%s' has initial mode `%s'. Change to `%s'? "
+				    major-mode
+				    (or current-mode "DEFAULT")
+				    (or start-mode "DEFAULT")))
+	      (setq vim:initial-modes (assq-delete-all major-mode vim:initial-modes))
+	      (when start-mode
+		(push (cons major-mode start-mode) vim:initial-modes))
+	      (when (y-or-n-p "Save setting in customization file? ")
+		(customize-save-variable 'vim:initial-modes vim:initial-modes)))))))))
 (provide 'vim-ex-commands)
 ;;; vim-ex-commands.el ends here
   "Defines a new argument handler `arg-type'."
   (let ((newah (vim:make-arg-handler :complete complete
-					 :activate activate
-					 :deactivate deactivate
-					 :update update))
+				     :activate activate
+				     :deactivate deactivate
+				     :update update))
 	(ah (assoc arg-type vim:argument-handlers-alist)))
     (if ah
 	(setcdr ah newah)
 (vim:emap "ju" "jumps")
 (vim:emap "noh" "nohlsearch")
 (vim:emap "nohlsearch" 'vim:cmd-nohighlight)
+(vim:emap "setmode" 'vim:cmd-setmode)
 (vim:nmap (vector vim:down-mouse-1) 'vim:visual-mouse-clicked)
 (vim:vmap (vector vim:down-mouse-1) 'vim:visual-mouse-clicked)

File vim-modes.el

 (vim:deflocalvar vim:active-command-function nil
   "The command function of the currently active vim-mode.")
+(defvar vim:mode-alist nil
+  "Assocative list of all registered submodes, (mode-symbol . mode-text).")
 (defvar vim:emulation-mode-alist nil
   "List of all keymaps used by some modes.")
          ,(concat "The cursor-type for vim-mode " (symbol-name name) ".")
          :group 'vim-cursors)
+       (push (cons ',mode-name ,(symbol-name name)) vim:mode-alist)
        (define-minor-mode ,mode-name ,doc nil nil nil
          (when ,mode-name
            ,@(when ident `((vim:update-mode-line ,ident)))
 ;;; Changelog:
 ;; version 0.5
+;;     * add :setmode ex-command for setting vim-mode's start-mode
+;;       for the current major-mode
 ;;     * enable search commands /, ?, *, #, g*, g# in motion-mode,
 ;;       repeat search with C-n and C-N instead of n and N
 ;;     * : starts ex-mode in motion-mode