Function: viper-change-state

viper-change-state is a byte-compiled function defined in viper-cmd.el.gz.

Signature

(viper-change-state NEW-STATE)

Source Code

;; Defined in /usr/src/emacs/lisp/emulation/viper-cmd.el.gz
;; changing mode

;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
(defun viper-change-state (new-state)
  ;; Keep viper-post/pre-command-hooks fresh.
  ;; We remove then add viper-post/pre-command-sentinel since it is very
  ;; desirable that viper-pre-command-sentinel is the last hook and
  ;; viper-post-command-sentinel is the first hook.

  (remove-hook 'post-command-hook #'viper-post-command-sentinel)
  (add-hook 'post-command-hook #'viper-post-command-sentinel)
  (remove-hook 'pre-command-hook #'viper-pre-command-sentinel)
  (add-hook 'pre-command-hook #'viper-pre-command-sentinel t)
  ;; These hooks will be added back if switching to insert/replace mode
  (remove-hook 'viper-post-command-hooks
	       #'viper-insert-state-post-command-sentinel 'local)
  (remove-hook 'viper-pre-command-hooks
	       #'viper-insert-state-pre-command-sentinel 'local)
  (setq viper-intermediate-command nil)
  (cond ((eq new-state 'vi-state)
	 (cond ((member viper-current-state '(insert-state replace-state))

		;; move viper-last-posn-while-in-insert-state
		;; This is a normal hook that is executed in insert/replace
		;; states after each command.  In Vi/Emacs state, it does
		;; nothing.  We need to execute it here to make sure that
		;; the last posn was recorded when we hit ESC.
		;; It may be left unrecorded if the last thing done in
		;; insert/repl state was dabbrev-expansion or abbrev
		;; expansion caused by hitting ESC
		(viper-insert-state-post-command-sentinel)

		(condition-case conds
		    (progn
		      (viper-save-last-insertion
		       viper-insert-point
		       viper-last-posn-while-in-insert-state)
		      (if viper-began-as-replace
			  (setq viper-began-as-replace nil)
			;; repeat insert commands if numerical arg > 1
			(save-excursion
			  (viper-repeat-insert-command))))
		  (error
		   (viper-message-conditions conds)))

		(if (> (length viper-last-insertion) 0)
		    (viper-push-onto-ring viper-last-insertion
					  'viper-insertion-ring))

		(if viper-ESC-moves-cursor-back
		    (or (bolp) (viper-beginning-of-field) (backward-char 1))))
	       ))

	;; insert or replace
	((memq new-state '(insert-state replace-state))
	 (if (memq viper-current-state '(emacs-state vi-state))
	     (viper-move-marker-locally 'viper-insert-point (point)))
	 (viper-move-marker-locally
	  'viper-last-posn-while-in-insert-state (point))
	 (add-hook 'viper-post-command-hooks
		   #'viper-insert-state-post-command-sentinel t 'local)
	 (add-hook 'viper-pre-command-hooks
		   #'viper-insert-state-pre-command-sentinel t 'local))
	) ; outermost cond

  ;; Nothing needs to be done to switch to emacs mode! Just set some
  ;; variables, which is already done in viper-change-state-to-emacs!

  ;; ISO accents
  ;; always turn off iso-accents-mode in vi-state, or else we won't be able to
  ;; use the keys `,',^ , as they will do accents instead of Vi actions.
  (cond ((eq new-state 'vi-state) (viper-set-iso-accents-mode nil));accents off
	(viper-automatic-iso-accents (viper-set-iso-accents-mode t));accents on
	(t (viper-set-iso-accents-mode nil)))
  ;; Always turn off quail mode in vi state
  (cond ((eq new-state 'vi-state) (viper-set-input-method nil)) ;intl input off
	(viper-special-input-method (viper-set-input-method t)) ;intl input on
	(t (viper-set-input-method nil)))

  (setq viper-current-state new-state)

  (viper-update-syntax-classes)
  (viper-normalize-minor-mode-map-alist)
  (viper-adjust-keys-for new-state)
  (viper-set-mode-vars-for new-state)
  (viper-refresh-mode-line)
  )