diff lisp/viper/viper.el @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents e04119814345
children c7528f8e288d
line wrap: on
line diff
--- a/lisp/viper/viper.el	Mon Aug 13 09:00:04 2007 +0200
+++ b/lisp/viper/viper.el	Mon Aug 13 09:02:59 2007 +0200
@@ -6,9 +6,9 @@
 ;;  Keywords: emulations
 ;;  Author: Michael Kifer <kifer@cs.sunysb.edu>
 
-;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
-
-(defconst viper-version "2.93 of March 9, 1997"
+;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
+
+(defconst viper-version "2.90 of June 19, 1996"
   "The current version of Viper")
 
 ;; This file is part of GNU Emacs.
@@ -24,9 +24,8 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to
+;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
 ;;; Commentary:
 
@@ -89,7 +88,7 @@
 ;;    The same macro name can have several different definitions:
 ;;    one global, several definitions for various major modes, and
 ;;    definitions for specific buffers.
-;;    Buffer-specific definitions override mode-specific
+;;    Bffer-specific definitions override mode-specific
 ;;    definitions, which, in turn, override global definitions.
 ;;
 ;;
@@ -101,8 +100,8 @@
 
 ;;; Acknowledgements:
 ;;  -----------------
-;;  Bug reports and ideas contributed by many users have helped
-;;  improve Viper and the various versions of VIP.
+;;  Bug reports and ideas contributed by the following users
+;;  have helped improve Viper and the various versions of VIP.
 ;;  See the on-line manual for a complete list of contributors. 
 ;;
 ;;
@@ -156,8 +155,8 @@
 ;;  For instance,  C-c will not have its standard Emacs binding
 ;;  and so many of the goodies of Emacs are not available.
 ;;
-;;  A skilled user should set vip-expert-level to at least 3. This will
-;;  enable `C-c' and many Emacs facilities will become available.
+;;  An skilled user, should set vip-expert-level to at least 3. This will
+;;  enable ;;  C-c and many Emacs facilities will become available.
 ;;  In this case, vip-vi-diehard-minor-mode is inactive.
 ;;
 ;;  Viper gurus should have at least
@@ -300,7 +299,7 @@
 (require 'cl)
 (require 'ring)
 
-(provide 'viper)
+(require 'viper-util)
 
 ;; Compiler pacifier
 (defvar vip-minibuffer-current-face)
@@ -309,32 +308,599 @@
 (defvar vip-minibuffer-emacs-face)
 (defvar iso-accents-mode)
 (defvar zmacs-region-stays)
-(defvar mark-even-if-inactive)
-
-(eval-when-compile
-  (let ((load-path (cons (expand-file-name ".") load-path)))
-    (or (featurep 'viper-util)
-	(load "viper-util.el" nil nil 'nosuffix))
-    (or (featurep 'viper-keym)
-	(load "viper-keym.el" nil nil 'nosuffix))
-    (or (featurep 'viper-mous)
-	(load "viper-mous.el" nil nil 'nosuffix))
-    (or (featurep 'viper-macs)
-	(load "viper-macs.el" nil nil 'nosuffix))
-    (or (featurep 'viper-ex)
-	(load "viper-ex.el" nil nil 'nosuffix))
-    ))
 ;; end pacifier
 
-
-(require 'viper-util)
-(require 'viper-keym)
-(require 'viper-mous)
-(require 'viper-macs)
-(require 'viper-ex)
+
+;;; Variables
+	      
+;; Is t until viper-mode executes for the very first time. 
+;; Prevents recursive descend into startup messages.
+(defvar vip-first-time t)
+
+(defvar vip-expert-level 0
+  "User's expert level.
+The minor mode vip-vi-diehard-minor-mode is in effect when
+vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t.
+The minor mode vip-insert-diehard-minor-mode is in effect when
+vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t.
+Use `M-x vip-set-expert-level' to change this.")
+
+;; Max expert level supported by Viper. This is NOT a user option.
+;; It is here to make it hard for the user from resetting it.
+(defconst vip-max-expert-level 5)
+
+;; Contains user settings for vars affected by vip-set-expert-level function.
+;; Not a user option.
+(defvar vip-saved-user-settings nil)
+	       
+
+;;; Viper minor modes
+
+;; This is not local in Emacs, so we make it local.
+(make-variable-buffer-local 'minor-mode-map-alist)
+
+;; Mode for vital things like \e, C-z.
+(vip-deflocalvar vip-vi-intercept-minor-mode nil)
+
+(vip-deflocalvar vip-vi-basic-minor-mode nil
+  "Viper's minor mode for Vi bindings.")
+  
+(vip-deflocalvar vip-vi-local-user-minor-mode nil
+  "Auxiliary minor mode for user-defined local bindings in Vi state.")
+
+(vip-deflocalvar vip-vi-global-user-minor-mode nil
+  "Auxiliary minor mode for user-defined global bindings in Vi state.")
+
+(vip-deflocalvar vip-vi-state-modifier-minor-mode nil
+  "Minor mode used to make major-mode-specific modification to Vi state.")
+
+(vip-deflocalvar vip-vi-diehard-minor-mode nil
+  "This minor mode is in effect when the user wants Viper to be Vi.")
+
+(vip-deflocalvar vip-vi-kbd-minor-mode nil
+  "Minor mode for Ex command macros in Vi state.
+The corresponding keymap stores key bindings of Vi macros defined with
+the Ex command :map.")
+
+;; Mode for vital things like \e, C-z.
+(vip-deflocalvar vip-insert-intercept-minor-mode nil)
+
+(vip-deflocalvar vip-insert-basic-minor-mode nil
+  "Viper's minor mode for bindings in Insert mode.")
+
+(vip-deflocalvar vip-insert-local-user-minor-mode nil
+  "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
+This is a way to overshadow normal Insert mode bindings locally to certain
+designated buffers.")
+
+(vip-deflocalvar vip-insert-global-user-minor-mode nil
+  "Auxiliary minor mode for global user-defined bindings in Insert state.")
+
+(vip-deflocalvar vip-insert-state-modifier-minor-mode nil
+  "Minor mode used to make major-mode-specific modification to Insert state.")
+
+(vip-deflocalvar vip-insert-diehard-minor-mode nil
+  "Minor mode that simulates Vi very closely.
+Not recommened, except for the novice user.")
+
+(vip-deflocalvar vip-insert-kbd-minor-mode nil
+"Minor mode for Ex command macros Insert state.
+The corresponding keymap stores key bindings of Vi macros defined with
+the Ex command :map!.")
+
+(vip-deflocalvar vip-replace-minor-mode nil
+  "Minor mode in effect in replace state (cw, C, and the like commands).")
+
+;; Mode for vital things like \C-z and \C-x)
+;; This is t, by default. So, any new buffer will have C-z defined as
+;; switch to Vi, unless we switched states in this buffer
+(vip-deflocalvar vip-emacs-intercept-minor-mode t)
+  
+(vip-deflocalvar vip-emacs-local-user-minor-mode t
+  "Minor mode for local user bindings effective in Emacs state.
+Users can use it to override Emacs bindings when Viper is in its Emacs
+state.")  
+  
+(vip-deflocalvar vip-emacs-global-user-minor-mode t
+  "Minor mode for global user bindings in effect in Emacs state.
+Users can use it to override Emacs bindings when Viper is in its Emacs
+state.")  
+
+(vip-deflocalvar vip-emacs-kbd-minor-mode t
+  "Minor mode for Vi style macros in Emacs state.
+The corresponding keymap stores key bindings of Vi macros defined with
+`vip-record-kbd-macro' command. There is no Ex-level command to do this
+interactively.")
+
+(vip-deflocalvar vip-emacs-state-modifier-minor-mode t
+  "Minor mode used to make major-mode-specific modification to Emacs state.
+For instance, a Vi purist may want to bind `dd' in Dired mode to a function
+that deletes a file.")
+
+
+
+;;; ISO characters
+  
+(vip-deflocalvar vip-automatic-iso-accents nil
+  "*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state. 
+For some users, this behavior may be too primitive. In this case, use
+insert/emacs/vi state hooks.")
+  
+  
+;;; Emacs keys in other states.  
+
+(defvar vip-want-emacs-keys-in-insert t
+  "*Set to nil if you want complete Vi compatibility in insert mode.
+Complete compatibility with Vi is not recommended for power use of Viper.")
+
+(defvar vip-want-emacs-keys-in-vi t
+  "*Set to nil if you want complete Vi compatibility in Vi mode.
+Full Vi compatibility is not recommended for power use of Viper.")
+
+
+
+;; VI-style Undo
+
+;; Used to 'undo' complex commands, such as replace and insert commands.
+(vip-deflocalvar vip-undo-needs-adjustment nil)
+(put 'vip-undo-needs-adjustment 'permanent-local t)
+
+;; A mark that Viper puts on buffer-undo-list.  Marks the beginning of a
+;; complex command that must be undone atomically. If inserted, it is
+;; erased by vip-change-state-to-vi and vip-repeat.
+(defconst vip-buffer-undo-list-mark 'viper)
+
+(defvar vip-keep-point-on-undo nil
+  "*Non-nil means not to move point while undoing commands.
+This style is different from Emacs and Vi. Try it to see if
+it better fits your working style.")  
+
+;; Replace mode and changing text
+
+;; Viper's own after/before change functions, which get vip-add-hook'ed to
+;; Emacs's
+(vip-deflocalvar vip-after-change-functions nil "")
+(vip-deflocalvar vip-before-change-functions nil "")
+(vip-deflocalvar vip-post-command-hooks nil "")
+(vip-deflocalvar vip-pre-command-hooks nil "")
+
+;; Can be used to pass global states around for short period of time
+(vip-deflocalvar vip-intermediate-command nil "")
+
+;; Indicates that the current destructive command has started in replace mode.
+(vip-deflocalvar vip-began-as-replace nil "")
+
+(defvar vip-replace-overlay-cursor-color "Red"
+  "*Cursor color to use in Replace state")
+
+  
+(vip-deflocalvar vip-replace-overlay nil "")
+(put 'vip-replace-overlay 'permanent-local t)
+
+(if (vip-has-face-support-p)
+    (progn
+      (make-face 'vip-replace-overlay-face)
+      (vip-hide-face 'vip-replace-overlay-face)
+      (or (face-differs-from-default-p 'vip-replace-overlay-face)
+	  (progn
+	    (if (vip-can-use-colors "darkseagreen2" "Black")
+		(progn
+		  (set-face-background
+		   'vip-replace-overlay-face "darkseagreen2")
+		  (set-face-foreground 'vip-replace-overlay-face "Black")))
+	    (set-face-underline-p 'vip-replace-overlay-face t))
+	  )))
+	    
+(defvar vip-replace-overlay-face 'vip-replace-overlay-face
+  "*Face for highlighting replace regions on a window display.")
+  
+(defvar vip-replace-region-end-delimiter "$"
+  "A string marking the end of replacement regions.
+It is used only with TTYs or if `vip-use-replace-region-delimiters'
+is non-nil.")
+(defvar vip-replace-region-start-delimiter ""
+  "A string marking the beginning of replacement regions.
+It is used only with TTYs or if `vip-use-replace-region-delimiters'
+is non-nil.")
+(defvar vip-use-replace-region-delimiters 
+  (or (not (vip-has-face-support-p)) (not (vip-color-display-p)))
+  "*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
+`vip-replace-region-start-delimiter' to delimit replacement regions, even on
+color displays. By default, the delimiters are used only on TTYs or
+monochrome displays.")
+  
+;; XEmacs requires glyphs
+(if vip-xemacs-p
+    (progn
+      (or (glyphp vip-replace-region-end-delimiter)
+	  (setq vip-replace-region-end-delimiter
+		(make-glyph vip-replace-region-end-delimiter)))
+      (or (glyphp vip-replace-region-start-delimiter)
+	  (setq vip-replace-region-start-delimiter
+		(make-glyph vip-replace-region-start-delimiter)))
+      ))
+      
+  
+;; These are local marker that must be initialized to nil and moved with
+;; `vip-move-marker-locally'
+;;
+;; Remember the last position inside the replace region.
+(vip-deflocalvar vip-last-posn-in-replace-region nil)
+;; Remember the last position while inserting
+(vip-deflocalvar vip-last-posn-while-in-insert-state nil)
+(put 'vip-last-posn-in-replace-region 'permanent-local t)
+(put 'vip-last-posn-while-in-insert-state 'permanent-local t)
+
+(vip-deflocalvar vip-sitting-in-replace nil "")
+(put 'vip-sitting-in-replace 'permanent-local t)
+  
+;; Remember the number of characters that have to be deleted in replace
+;; mode to compensate for the inserted characters.
+(vip-deflocalvar vip-replace-chars-to-delete 0 "")
+(vip-deflocalvar vip-replace-chars-deleted 0 "")
+
+;; Insertion ring and command ring
+(defvar vip-insertion-ring-size 14
+  "The size of the insertion ring.")
+;; The insertion ring.
+(defvar vip-insertion-ring nil)
+;; This is temp insertion ring. Used to do rotation for display purposes.
+;; When rotation just started, it is initialized to vip-insertion-ring.
+(defvar vip-temp-insertion-ring nil)
+(defvar vip-last-inserted-string-from-insertion-ring "")
+
+(defvar vip-command-ring-size 14
+  "The size of the command ring.")
+;; The command ring.
+(defvar vip-command-ring nil)
+;; This is temp command ring. Used to do rotation for display purposes.
+;; When rotation just started, it is initialized to vip-command-ring.
+(defvar vip-temp-command-ring nil)
+
+;; Modes and related variables
+
+;; Current mode.  One of: `emacs-state', `vi-state', `insert-state'
+(vip-deflocalvar vip-current-state 'emacs-state)
+
+
+(defvar vip-toggle-key "\C-z"
+  "The key used to change states from emacs to Vi and back.
+In insert mode, this key also functions as Meta. 
+Must be set in .vip file or prior to loading Viper.
+This setting cannot be changed interactively.")
+
+(defvar vip-ESC-key "\e" 
+  "Key used to ESC. 
+Must be set in .vip file or prior to loading Viper.
+This setting cannot be changed interactively.")
+
+(defvar vip-no-multiple-ESC  t
+  "*If true, multiple ESC in Vi mode will cause bell to ring.
+\_ is then mapped to Meta.
+This is set to t on a windowing terminal and to 'twice on a dumb
+terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
+enables cursor keys and is generally more convenient, as terminals usually
+don't have a convenient Meta key.
+Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
+as is allowed by the major mode in effect.") 
+
+
+(defvar vip-want-ctl-h-help nil
+  "*If t then C-h is bound to help-command in insert mode, if nil then it is
+bound to delete-backward-char.")
+
+;; Autoindent in insert
+
+;; Variable that keeps track of whether C-t has been pressed.
+(vip-deflocalvar vip-cted nil "")
+
+;; Preserve the indent value, used by C-d in insert mode.
+(vip-deflocalvar vip-current-indent 0)
+
+;; Whether to preserve the indent, used by C-d in insert mode.
+(vip-deflocalvar vip-preserve-indent nil)
+
+(vip-deflocalvar vip-auto-indent nil
+  "*Autoindent if t.")
+(vip-deflocalvar vip-electric-mode t
+  "*If t, enable electric behavior. 
+Currently only enables auto-indentation `according to mode'.")
+
+(defconst vip-shift-width 8
+  "*The shiftwidth variable.")
+
+;; Variables for repeating destructive commands
+
+(defconst vip-keep-point-on-repeat t
+  "*If t, don't move point when repeating previous command.
+This is useful for doing repeated changes with the '.' key.
+The user can change this to nil, if she likes when the cursor moves
+to a new place after repeating previous Vi command.") 
+
+;; Remember insert point as a marker.  This is a local marker that must be
+;; initialized to nil and moved with `vip-move-marker-locally'.
+(vip-deflocalvar vip-insert-point nil)
+(put 'vip-insert-point 'permanent-local t)
+
+;; This remembers the point before dabbrev-expand was called.
+;; If vip-insert-point turns out to be bigger than that, it is reset
+;; back to vip-pre-command-point.
+;; The reason this is needed is because dabbrev-expand (and possibly
+;; others) may jump to before the insertion point, delete something and
+;; then reinsert a bigger piece. For instance:  bla^blo
+;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
+;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
+;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
+;; will insert the expansion, and we get: blablo^
+;; Whatever we insert next goes before the ^, i.e., before the
+;; vip-insert-point marker. So, Viper will think that nothing was
+;; inserted. Remembering the orig position of the marker circumvents the
+;; problem.
+;; We don't know of any command, except dabbrev-expand, that has the same
+;; problem. However, the same trick can be used if such a command is
+;; discovered later.
+;;
+(vip-deflocalvar vip-pre-command-point nil)
+(put 'vip-pre-command-point 'permanent-local t) ; this is probably an overkill
+
+;; This is used for saving inserted text.
+(defvar vip-last-insertion  nil)
+  
+;; Remembers the last replaced region.
+(defvar vip-last-replace-region "")
+  
+;; Remember com point as a marker.
+;; This is a local marker. Should be moved with `vip-move-marker-locally'
+(vip-deflocalvar vip-com-point nil)
+
+;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
+;; It is used to re-execute last destructive command.
+;; M-COM is a Lisp symbol representing the function to be executed.
+;; VAL is the prefix argument that was used with that command.
+;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
+;; additional information on how the function in M-COM is to be handled.
+;; REG is the register used by command
+;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
+;; commands).
+;; COMMAND-KEYS are the keys that were typed to invoke the command.
+(defvar vip-d-com nil)
+
+;; The character remembered by the Vi `r' command.
+(defvar vip-d-char nil)
+
+;; Name of register to store deleted or yanked strings
+(defvar vip-use-register nil)
+
+
+
+;; Variables for Moves and Searches
+
+;; For use by `;' command.
+(defvar vip-f-char nil)
+
+;; For use by `.' command.
+(defvar vip-F-char nil)
+
+;; For use by `;' command.
+(defvar vip-f-forward nil)
+
+;; For use by `;' command.
+(defvar vip-f-offset nil)
+
+;; Last search string
+(defvar vip-s-string "")
+
+(defvar vip-quote-string "> "
+  "String inserted at the beginning of quoted region.")
+
+;; If t, search is forward.
+(defvar vip-s-forward nil)
+
+(defconst vip-case-fold-search nil
+  "*If t, search ignores cases.")
+
+(defconst vip-re-search t
+  "*If t, search is reg-exp search, otherwise vanilla search.")
+
+(defconst vip-re-query-replace t
+  "*If t then do regexp replace, if nil then do string replace.")
+
+(defconst vip-re-replace t
+  "*If t, do regexp replace. nil means do string replace.")
+
+(vip-deflocalvar vip-ex-style-motion t
+  "*Ex-style: the commands l,h do not cross lines, etc.")
+
+(vip-deflocalvar vip-ex-style-editing-in-insert t
+  "*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
+Note: this doesn't preclude ^H and ^? from deleting characters by moving
+past the insertion point. This is a feature, not a bug. ")
+
+(vip-deflocalvar vip-delete-backwards-in-replace nil
+  "*If t, DEL key will delete characters while moving the cursor backwards.
+If nil, the cursor will move backwards without deleting anything.")
+
+(defconst vip-buffer-search-char nil
+  "*Key bound for buffer-searching.")
+
+(defconst vip-search-wrap-around-t t
+  "*If t, search wraps around.")
+  
+(vip-deflocalvar vip-related-files-and-buffers-ring nil
+  "*Ring of file and buffer names that are considered to be related to the
+current buffer.
+These buffers can be cycled through via :R and :P commands.")
+(put 'vip-related-files-and-buffers-ring 'permanent-local t)
+
+;; Used to find out if we are done with searching the current buffer.
+(vip-deflocalvar vip-local-search-start-marker nil)
+;; As above, but global
+(defvar vip-search-start-marker (make-marker))
+
+;; the search overlay
+(vip-deflocalvar vip-search-overlay nil)
+
+
+(defvar vip-heading-start 
+  (concat "^\\s-*(\\s-*defun\\s-\\|"			        ; lisp
+	  "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|"	        ; C/C++
+	  "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
+	  "^\\\\[sb][a-z]*{.*}\\s-*$\\|"	    		; latex
+	  "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|"	; texinfo
+	  "^.+:-")			                        ; prolog
+  "*Regexps for Headings. Used by \[\[ and \]\].")
+
+(defvar vip-heading-end 
+  (concat "^}\\|"						; C/C++
+	  "^\\\\end{\\|"					; latex
+	  "^@end \\|"						; texinfo
+	  ")\n\n[ \t\n]*\\|"					; lisp
+	  "\\.\\s-*$")						; prolog
+      "*Regexps to end Headings/Sections. Used by \[\].")
+
+
+;; These two vars control the interaction of jumps performed by ' and `.
+;; In this new version, '' doesn't erase the marks set by ``, so one can
+;; use both kinds of jumps interchangeably and without loosing positions
+;; inside the lines.
+
+;; Remembers position of the last jump done using ``'.
+(vip-deflocalvar vip-last-jump  nil)
+;; Remembers position of the last jump done using `''.
+(vip-deflocalvar vip-last-jump-ignore 0)
+
+;; Some common error messages
+
+(defconst vip-SpuriousText "Spurious text after command"  "")
+(defconst vip-BadExCommand "Not an editor command"   "")
+(defconst vip-InvalidCommandArgument "Invalid command argument"   "")
+(defconst vip-NoPrevSearch "No previous search string"   "")
+(defconst vip-EmptyRegister "`%c': Nothing in this register"   "")
+(defconst vip-InvalidRegister "`%c': Invalid register"   "")
+(defconst vip-EmptyTextmarker "`%c': Text marker doesn't point anywhere"   "")
+(defconst vip-InvalidTextmarker "`%c': Invalid text marker"   "")
+(defconst vip-InvalidViCommand "Invalid command"   "")
+(defconst vip-BadAddress "Ill-formed address"   "")
+(defconst vip-FirstAddrExceedsSecond "First address exceeds second"   "")
+(defconst vip-NoFileSpecified "No file specified"   "")
+
+
+;; History variables
+
+;; History of search strings.
+(defvar vip-search-history  (list ""))
+;; History of query-replace strings used as a source.
+(defvar vip-replace1-history nil)
+;; History of query-replace strings used as replacement.
+(defvar vip-replace2-history nil)
+;; History of region quoting strings.
+(defvar vip-quote-region-history (list vip-quote-string))
+;; History of Ex-style commands.
+(defvar vip-ex-history nil)
+;; History of shell commands.
+(defvar vip-shell-history nil)
+
+
+;; Last shell command. There are two of these, one for Ex (in viper-ex)
+;; and one for Vi.
+
+;; Last shell command executed with ! command.
+(defvar vip-last-shell-com nil)
 
 
 
+;;; Miscellaneous
+
+;; don't bark when mark is inactive
+(setq mark-even-if-inactive t)
+
+(defvar vip-inhibit-startup-message nil
+  "Whether Viper startup message should be inhibited.")
+
+(defvar vip-always t
+  "t means, arrange that vi-state will be a default.")
+
+(defvar vip-ms-style-os-p (memq system-type '(ms-dos windows-nt windows-95))
+  "Tells is Emacs is running under an MS-style OS: ms-dos, window-nt, W95.")
+(defvar vip-vms-os-p (memq system-type '(vax-vms axp-vms))
+  "Tells if Emacs is running under VMS.")
+
+(defvar vip-custom-file-name (cond (vip-vms-os-p "sys$login:.vip")
+				   ((memq system-type '(emx ms-dos))
+				    "/_vip")
+				   ((memq system-type '(windows-nt windows-95))
+				    "~/_vip")
+				   (t ; Unix
+				    "~/.vip"))
+  "Viper customisation file.
+This variable must be set _before_ loading Viper.")
+
+
+(defvar vip-spell-function 'ispell-region
+  "Spell function used by #s<move> command to spell.")
+
+(defvar vip-tags-file-name "TAGS"
+  "The tags file used by Viper.")
+
+;; Minibuffer
+
+(defvar vip-vi-style-in-minibuffer t
+  "If t, use vi-style editing in minibuffer.
+Should be set in `~/.vip' file.")
+  
+;; overlay used in the minibuffer to indicate which state it is in
+(vip-deflocalvar vip-minibuffer-overlay nil)
+
+;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
+;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
+;; *after* exiting the minibuffer
+(defvar vip-minibuffer-exit-hook nil)
+
+(vip-deflocalvar vip-vi-minibuffer-minor-mode nil
+   "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
+(vip-deflocalvar vip-insert-minibuffer-minor-mode nil
+   "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
+  
+;; setup emacs-supported vi-style feel
+(setq next-line-add-newlines nil
+      require-final-newline t)
+
+(make-variable-buffer-local 'require-final-newline)
+       
+
+;; Mode line
+(defconst vip-vi-state-id  	"<V> "
+  "Mode line tag identifying the Vi mode of Viper.")
+(defconst vip-emacs-state-id	"<E> "
+  "Mode line tag identifying the Emacs mode of Viper.")
+(defconst vip-insert-state-id	"<I> "
+  "Mode line tag identifying the Insert mode of Viper.")
+(defconst vip-replace-state-id	"<R> "
+  "Mode line tag identifying the Replace mode of Viper.")
+
+;; Viper changes the default mode-line-buffer-identification
+(setq-default mode-line-buffer-identification '(" %b"))
+
+;; Variable displaying the current Viper state in the mode line.
+(vip-deflocalvar vip-mode-string vip-emacs-state-id)
+(or (memq 'vip-mode-string global-mode-string)
+    (setq global-mode-string
+	  (append '("" vip-mode-string) (cdr global-mode-string))))
+
+
+(defvar vip-vi-state-hook nil
+  "*Hooks run just before the switch to Vi mode is completed.")
+(defvar vip-insert-state-hook nil
+  "*Hooks run just before the switch to Insert mode is completed.")
+(defvar vip-replace-state-hook nil
+  "*Hooks run just before the switch to Replace mode is completed.")
+(defvar vip-emacs-state-hook nil
+  "*Hooks run just before the switch to Emacs mode is completed.")
+  
+(defvar vip-load-hook nil
+  "Hooks run just after loading Viper.")
+  
+
 ;; Generic predicates
 
 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
@@ -356,53 +922,36 @@
 
 ;; Modifying commands that can be prefixes to movement commands
 (defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
-;; define vip-prefix-command-p
 (vip-test-com-defun vip-prefix-command)
   
 ;; Commands that are pairs eg. dd. r and R here are a hack
 (defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
-;; define vip-charpair-command-p
 (vip-test-com-defun vip-charpair-command)
 
 (defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
-				     ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
+				     ?H ?M ?n ?t ?T ?w ?W ?$ ?%
 				     ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
 				     ?; ?, ?0 ?? ?/
 				     )
 				     "Movement commands")
-;; define vip-movement-command-p
 (vip-test-com-defun vip-movement-command)
 
-(defconst vip-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)
-  "Digit commands")
-;; define vip-digit-command-p
-(vip-test-com-defun vip-digit-command)
-
-;; Commands that can be repeated by . (dotted)
-(defconst vip-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
-;; define vip-dotable-command-p
+;; Commands that can be repeated by .(dotted)
+(defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<))
 (vip-test-com-defun vip-dotable-command)
 
 ;; Commands that can follow a #
-(defconst vip-hash-commands '(?c ?C ?g ?q ?s))
-;; define vip-hash-command-p
-(vip-test-com-defun vip-hash-command)
+(defconst vip-hash-cmds '(?c ?C ?g ?q ?S))
+(vip-test-com-defun vip-hash-cmd)
 
 ;; Commands that may have registers as prefix
 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
-;; define vip-regsuffix-command-p
 (vip-test-com-defun vip-regsuffix-command)
 
-(defconst vip-vi-commands (append vip-movement-commands
-				  vip-digit-commands
-				  vip-dotable-commands
-				  vip-charpair-commands
-				  vip-hash-commands
-				  vip-prefix-commands
-				  vip-regsuffix-commands)
-  "The list of all commands in Vi-state.")
-;; define vip-vi-command-p
-(vip-test-com-defun vip-vi-command)
+
+
+;;; Arrange the keymaps
+(require 'viper-keym)
 
 
 ;;; CODE
@@ -436,14 +985,6 @@
 	   vip-insert-point
 	   (>= (point) vip-insert-point))
       (setq vip-last-posn-while-in-insert-state (point-marker)))
-  (if (eq vip-current-state 'insert-state)
-      (progn
-	(or (stringp vip-saved-cursor-color)
-	    (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
-	    (setq vip-saved-cursor-color (vip-get-cursor-color)))
-	(if (stringp vip-saved-cursor-color)
-	    (vip-change-cursor-color vip-insert-state-cursor-color))
-	))
   (if (and (eq this-command 'dabbrev-expand)
 	   (integerp vip-pre-command-point)
 	   (> vip-insert-point vip-pre-command-point))
@@ -451,11 +992,6 @@
   )
   
 (defsubst vip-insert-state-pre-command-sentinel ()
-  (or (memq this-command '(self-insert-command))
-      (memq (vip-event-key last-command-event)
-	    '(up down left right (meta f) (meta b)
-		 (control n) (control p) (control f) (control b)))
-      (vip-restore-cursor-color-after-insert))
   (if (and (eq this-command 'dabbrev-expand)
 	   (markerp vip-insert-point)
 	   (marker-position vip-insert-point))
@@ -471,7 +1007,7 @@
   (if (and (<= (vip-replace-start) (point))
 	   (<=  (point) (vip-replace-end)))
       (vip-change-cursor-color vip-replace-overlay-cursor-color)
-    (vip-restore-cursor-color-after-replace)
+    (vip-restore-cursor-color)
     ))
 
 ;; to speed up, don't change cursor color before self-insert
@@ -481,7 +1017,7 @@
       (memq (vip-event-key last-command-event)
 	    '(up down left right (meta f) (meta b)
 		 (control n) (control p) (control f) (control b)))
-      (vip-restore-cursor-color-after-replace)))
+      (vip-restore-cursor-color)))
   
 (defun vip-replace-state-post-command-sentinel ()
   ;; Restoring cursor color is needed despite
@@ -495,7 +1031,7 @@
   ;; cursor color or, if they terminate replace mode, the color will be changed
   ;; in vip-finish-change
   (or (memq this-command '(self-insert-command))
-      (vip-restore-cursor-color-after-replace))
+      (vip-restore-cursor-color))
   (cond 
    ((eq vip-current-state 'replace-state)
     ;; delete characters to compensate for inserted chars.
@@ -607,27 +1143,25 @@
 		   ;; expert
 		   (define-key vip-insert-basic-map "\C-j" nil)
 		 ;; novice
-		 (define-key vip-insert-basic-map "\C-j" 'vip-autoindent)))
-	   (define-key vip-insert-basic-map "\C-m" nil)
-	   (define-key vip-insert-basic-map "\C-j" nil))
+		 (define-key vip-insert-basic-map "\C-j" 'vip-autoindent))))
 		    
 	 (setq vip-insert-diehard-minor-mode
 	       (not vip-want-emacs-keys-in-insert))
 		   
 	 (if vip-want-ctl-h-help
 	     (progn 
-	       (define-key vip-insert-basic-map [(control h)] 'help-command)
-	       (define-key vip-replace-map [(control h)] 'help-command))
+	       (define-key vip-insert-basic-map "\C-h" 'help-command)
+	       (define-key vip-replace-map "\C-h" 'help-command))
 	   (define-key vip-insert-basic-map 
-	     [(control h)] 'vip-del-backward-char-in-insert)
+	     "\C-h" 'vip-del-backward-char-in-insert)
 	   (define-key vip-replace-map
-	     [(control h)] 'vip-del-backward-char-in-replace)))
+	     "\C-h" 'vip-del-backward-char-in-replace)))
 		     
-	(t ; Vi state
+	(t
 	 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
 	 (if vip-want-ctl-h-help
-	     (define-key vip-vi-basic-map [(control h)] 'help-command)
-	   (define-key vip-vi-basic-map [(control h)] 'vip-backward-char)))
+	     (define-key vip-vi-basic-map "\C-h" 'help-command)
+	   (define-key vip-vi-basic-map "\C-h" 'vip-backward-char)))
 	))
 	     
     
@@ -747,7 +1281,7 @@
    4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
       undo. Another `u' changes direction.
    
-   6. Emacs Meta functions are invoked by typing `C-\\' or `\\ ESC'.
+   6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'.
       On a window system, the best way is to use the Meta-key.
    7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
       something funny happens. This would abort the current editing command. 
@@ -897,8 +1431,6 @@
     ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
     (if (and (boundp 'iso-accents-mode) iso-accents-mode)
 	(iso-accents-mode -1))
-
-    (vip-restore-cursor-color-after-insert)
     
     ;; Protection against user errors in hooks
     (condition-case conds
@@ -913,14 +1445,6 @@
   (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
       (iso-accents-mode 1)) ; turn iso accents on
   
-  (or (stringp vip-saved-cursor-color)
-      (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
-      (setq vip-saved-cursor-color (vip-get-cursor-color)))
-  ;; Commented out, because if vip-change-state-to-insert is executed
-  ;; non-interactively then the old cursor color may get lost. Same old Emacs
-  ;; bug related to local variables?
-;;;(if (stringp vip-saved-cursor-color)
-;;;      (vip-change-cursor-color vip-insert-state-cursor-color))
   ;; Protection against user errors in hooks
   (condition-case conds
       (run-hooks 'vip-insert-state-hook)
@@ -972,24 +1496,20 @@
 ARG is used as the prefix value for the executed command.  If
 EVENTS is a list of events, which become the beginning of the command."
   (interactive "P")
-  (if (= last-command-char ?\\)
-      (message "Switched to EMACS state for the next command..."))
   (vip-escape-to-state arg events 'emacs-state))
   
 ;; escape to Vi mode termporarily
-(defun vip-escape-to-vi (arg)
+(defun vip-escape-to-vi ()
   "Escape from Emacs state to Vi state for one Vi 1-character 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
-`vip-escape-to-vi' is used."
-  (interactive "P")
-  (message "Switched to VI state for the next command...")
-  (vip-escape-to-state arg nil 'vi-state))
+This doesn't work with prefix arguments or most complex commands like
+cw, dw,  etc. But it does work with some 2-character commands,
+like dd or dr."
+  (interactive)
+  (vip-escape-to-state nil nil 'vi-state))  
   
 ;; Escape to STATE mode for one Emacs command.
 (defun vip-escape-to-state (arg events state)
-  ;;(let (com key prefix-arg)
-  (let (com key)
+  (let (com key prefix-arg)
     ;; this temporarily turns off Viper's minor mode keymaps
     (vip-set-mode-vars-for state)
     (vip-normalize-minor-mode-map-alist)
@@ -997,65 +1517,43 @@
     
     ;; protect against keyboard quit and other errors
     (condition-case nil
-	(let (vip-vi-kbd-minor-mode 
-	      vip-insert-kbd-minor-mode
-	      vip-emacs-kbd-minor-mode)
+	(progn
 	  (unwind-protect
 	      (progn
 		(setq com (key-binding (setq key 
 					     (if vip-xemacs-p
 						 (read-key-sequence nil)
 					       (read-key-sequence nil t)))))
-		;; In case of binding indirection--chase definitions.
+		;; In case of 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)
-	  ;; Execute command com in the original Viper state, not in state
-	  ;; `state'. Otherwise, if we switch buffers while executing the
-	  ;; escaped to command, Viper's mode vars will remain those of
-	  ;; `state'. When we return to the orig buffer, the bindings will be
-	  ;; screwed up.
+	  ;; exec command in the right Viper state
+	  ;; otherwise, if we switch buffers in the escaped command,
+	  ;; Viper's mode vars will remain those of `state'. When we return
+	  ;; to the orig buffer, the bindings will be screwed up.
 	  (vip-set-mode-vars-for vip-current-state)
 	  
 	  ;; this-command, last-command-char, last-command-event
 	  (setq this-command com)
 	  (if vip-xemacs-p ; XEmacs represents key sequences as vectors
-	      (setq last-command-event (vip-copy-event (vip-seq-last-elt key))
+	      (setq last-command-event (vip-seq-last-elt key)
 		    last-command-char (event-to-character last-command-event))
 	    ;; Emacs represents them as sequences (str or vec)
-	    (setq last-command-event (vip-copy-event (vip-seq-last-elt key))
+	    (setq last-command-event (vip-seq-last-elt key)
 		  last-command-char last-command-event))
 	    
 	  (if (commandp com)
 	      (progn
-		(setq prefix-arg (or prefix-arg arg))
+		(setq prefix-arg arg)
 		(command-execute com)))
 	  )
       (quit (ding))
       (error (beep 1))))
-  ;; set state in the new buffer
-  (vip-set-mode-vars-for vip-current-state))
+  (vip-set-mode-vars-for vip-current-state)) ; set state in new buffer
       
-(defun vip-exec-form-in-vi  (form)
-  "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
-  (let ((buff (current-buffer))
-	result)
-    (vip-set-mode-vars-for 'vi-state)
-
-    (condition-case nil
-	(setq result (eval form))
-      (error
-       (signal 'quit nil)))
-
-    (if (not (equal buff (current-buffer))) ; cmd switched buffer
-	(save-excursion
-	  (set-buffer buff)
-	  (vip-set-mode-vars-for vip-current-state)))
-    (vip-set-mode-vars-for vip-current-state)
-    result))
-
 (defun vip-exec-form-in-emacs  (form)
   "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
@@ -1098,13 +1596,13 @@
 
 (defun vip-ESC (arg)
   "Emulate ESC key in Emacs.
-Prevents multiple escape keystrokes if vip-no-multiple-ESC is true.
-If vip-no-multiple-ESC is 'twice double ESC would ding in vi-state.
-Other ESC sequences are emulated via the current Emacs's major mode
-keymap. This is more convenient on TTYs, since this won't block
-function keys such as up,down, etc. ESC will also will also work as
-a Meta key in this case. When vip-no-multiple-ESC is nil, ESC functions
-as a Meta key and any number of multiple escapes is allowed."
+Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that
+case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC
+would dings in vi-state. Other ESC sequences are emulated via the current
+Emacs's major mode keymap. This is more convenient on dumb terminals and in
+Emacs -nw, since this won't block functional keys such as up,down,
+etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key
+behaves as in Emacs, any number of multiple escapes is allowed."
   (interactive "P")
   (let (char)
     (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
@@ -1119,21 +1617,11 @@
 	  (t (ding)))
     ))
 
-(defun vip-alternate-Meta-key (arg)
-  "Simulate Emacs Meta key."
+(defun vip-alternate-ESC (arg)
+  "ESC key without checking for multiple keystrokes."
   (interactive "P")
-  (sit-for 1) (message "ESC-")
   (vip-escape-to-emacs arg '(?\e)))
-
-(defun vip-toggle-key-action ()
-  "Action bound to `vip-toggle-key'."
-  (interactive)
-  (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
-      (if (vip-window-display-p)
-	  (vip-iconify)
-	(suspend-emacs))
-    (vip-change-state-to-emacs)))
-
+  
 
 ;; Intercept ESC sequences on dumb terminals.
 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
@@ -1197,7 +1685,7 @@
 			 (setq last-input-event event
 			       keyseq (vector (character-to-event ?\e))))
 			((eventp first-key)
-			 (setq last-command-event (vip-copy-event first-key)))
+			 (setq last-command-event first-key))
 			))
 		) ; end progn
 		
@@ -1266,7 +1754,6 @@
 		    (t 'vip-change-state-to-vi)
 		    )))
     (call-interactively cmd)))
-
 	   
 
 
@@ -1305,12 +1792,12 @@
 
 
 ;; Compute numeric prefix arg value. 
-;; Invoked by EVENT. COM is the command part obtained so far.
+;; Invoked by CHAR. COM is the command part obtained so far.
 (defun vip-prefix-arg-value (event com)
-  (let (value func)
+  (let (value)
     ;; read while number
     (while (and (vip-characterp event) (>= event ?0) (<= event ?9))
-      (setq value (+ (* (if (integerp value) value 0) 10) (- event ?0)))
+      (setq value (+ (* (if (vip-characterp value) value 0) 10) (- event ?0)))
       (setq event (vip-read-event-convert-to-char)))
     
     (setq prefix-arg value)
@@ -1318,39 +1805,11 @@
     (while (eq event ?U)
       (vip-describe-arg prefix-arg)
       (setq event (vip-read-event-convert-to-char)))
-    
-    (if (or com (and (not (eq vip-current-state 'vi-state))
-		     ;; make sure it is a Vi command
-		     (vip-characterp event) (vip-vi-command-p event)
-		     ))
-	;; If appears to be one of the vi commands,
-	;; then execute it with funcall and clear prefix-arg in order to not
-	;; confuse subsequent commands
-	(progn
-	  ;; last-command-char is the char we want emacs to think was typed
-	  ;; last. If com is not nil, the vip-digit-argument command was called
-	  ;; from within vip-prefix-arg command, such as `d', `w', etc., i.e., 
-	  ;; the user typed, say, d2. In this case, `com' would be `d', `w',
-	  ;; etc.
-	  ;; If vip-digit-argument was invoked by vip-escape-to-vi (which is
-	  ;; indicated by the fact that the current state is not vi-state),
-	  ;; then `event' represents the vi command to be executed (e.g., `d',
-	  ;; `w', etc). Again, last-command-char must make emacs believe that
-	  ;; this is the command we typed.
-	  (setq last-command-char (or com event))
-	  (setq func (vip-exec-form-in-vi 
-		      (` (key-binding (char-to-string (, event))))))
-	  (funcall func prefix-arg)
-	  (setq prefix-arg nil))
-      ;; some other command -- let emacs do it in its own way
-      (vip-set-unread-command-events event))
-    ))
-		     
+    (vip-set-unread-command-events event)))
 
 ;; Vi operator as prefix argument."
 (defun vip-prefix-arg-com (char value com)
-  (let ((cont t)
-	cmd-info mv-or-digit-cmd)
+  (let ((cont t))
     (while (and cont
 		(memq char
 		      (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
@@ -1384,27 +1843,20 @@
 		 (setq char (read-char))))
 	      (t
 	       (setq com char)
-	       (setq char (read-char))))))
-
+	       (setq char (vip-read-char-exclusive)))))))
   (if (atom com)
-      ;; `com' is a single char, so we construct the command argument
-      ;; and if `char' is `?', we describe the arg; otherwise 
-      ;; we prepare the command that will be executed at the end.
+      ;; com is a single char, so we construct prefix-arg 
+      ;; and if char is ?, describe prefix arg, otherwise exit by
+      ;; pushing the char back into vip-set-unread-command-events
+      ;; Since char is a command, the command will execute with the prefix
+      ;; argument that we just constructed.
       (progn
-	(setq cmd-info (cons value com))
+	(setq prefix-arg (cons value com))
 	(while (= char ?U)
-	  (vip-describe-arg cmd-info)
+	  (vip-describe-arg prefix-arg)
 	  (setq char (read-char)))
-	;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
-	;; execute it at the very end 
-	(or (vip-movement-command-p char)
-	    (vip-digit-command-p char)
-	    (vip-regsuffix-command-p char)
-	    (error ""))
-	(setq mv-or-digit-cmd
-	      (vip-exec-form-in-vi 
-	       (` (key-binding (char-to-string (, char)))))))
-    
+	(vip-set-unread-command-events char)
+	)
     ;; as com is non-nil, this means that we have a command to execute
     (if (memq (car com) '(?r ?R))
 	;; execute apropriate region command.
@@ -1425,19 +1877,7 @@
 	    ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
 	    ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
 	    ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
-	    (t (error "")))))
-  
-  (if mv-or-digit-cmd
-      (progn
-	(setq last-command-char char)
-	(setq last-command-event 
-	      (vip-copy-event
-	       (if vip-xemacs-p (character-to-event char) char)))
-	(condition-case nil
-	    (funcall mv-or-digit-cmd cmd-info)
-	  (error
-	   (error "")))))
-  ))
+	    (t (error ""))))))
 
 (defun vip-describe-arg (arg)
   (let (val com)
@@ -1455,27 +1895,26 @@
   "Begin numeric argument for the next command."
   (interactive "P")
   (vip-leave-region-active)
-  (vip-prefix-arg-value
-   last-command-char (if (consp arg) (cdr arg) nil)))
+  (vip-prefix-arg-value last-command-char
+			(if (consp arg) (cdr arg) nil)))
 
 (defun vip-command-argument (arg)
   "Accept a motion command as an argument."
   (interactive "P")
-  (let ((vip-inside-command-argument-action t))
-    (condition-case nil
-	(vip-prefix-arg-com
-	 last-command-char   
-	 (cond ((null arg) nil)
-	       ((consp arg) (car arg))
-	       ((integerp arg) arg)
-	       (t (error vip-InvalidCommandArgument)))
-	 (cond ((null arg) nil)
-	       ((consp arg) (cdr arg))
-	       ((integerp arg) nil)
-	       (t (error vip-InvalidCommandArgument))))
-      (quit (setq vip-use-register nil)
-	    (signal 'quit nil)))
-    (vip-deactivate-mark)))
+  (condition-case nil
+      (vip-prefix-arg-com
+       last-command-char   
+       (cond ((null arg) nil)
+	     ((consp arg) (car arg))
+	     ((integerp arg) arg)
+	     (t (error vip-InvalidCommandArgument)))
+       (cond ((null arg) nil)
+	     ((consp arg) (cdr arg))
+	     ((integerp arg) nil)
+	     (t (error vip-InvalidCommandArgument))))
+    (quit (setq vip-use-register nil)
+	  (signal 'quit nil)))
+  (vip-deactivate-mark))
 
 
 ;; repeat last destructive command
@@ -1512,8 +1951,6 @@
 
 ;; invoked by the `C' command
 (defun vip-exec-change (m-com com) 
-  (or (and (markerp vip-com-point) (marker-position vip-com-point))
-      (set-marker vip-com-point (point) (current-buffer)))
   ;; handle C cmd at the eol and at eob.
   (if (or (and (eolp) (= vip-com-point (point)))
 	  (= vip-com-point (point-max)))
@@ -1547,8 +1984,6 @@
   (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
 
 (defun vip-exec-delete (m-com com)
-  (or (and (markerp vip-com-point) (marker-position vip-com-point))
-      (set-marker vip-com-point (point) (current-buffer)))
   (if vip-use-register
       (progn
 	(cond ((vip-valid-register vip-use-register '(letter digit))
@@ -1591,8 +2026,6 @@
   (back-to-indentation))
 
 (defun vip-exec-yank (m-com com)
-  (or (and (markerp vip-com-point) (marker-position vip-com-point))
-      (set-marker vip-com-point (point) (current-buffer)))
   (if vip-use-register
       (progn
 	(cond ((vip-valid-register vip-use-register '(letter digit))
@@ -1801,8 +2234,7 @@
     ))
       
 
-;; The hash-command. It is invoked interactively by the key sequence #<char>.
-;; The chars that can follow `#' are determined by vip-hash-command-p
+;; This command is invoked interactively by the key sequence #<char>
 (defun vip-special-prefix-com (char)
   (cond ((= char ?c)
 	 (downcase-region (min vip-com-point (point))
@@ -2048,23 +2480,20 @@
 ;;  Tells whether BEG is on the same line as END.
 ;;  If one of the args is nil, it'll return nil.
 (defun vip-same-line (beg end)
-   (let ((selective-display nil)
-	 (incr 0)
-	 temp)
-     (if (and beg end (> beg end))
-	 (setq temp beg
-	       beg end
-	       end temp))
-     (if (and beg end)
-	 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
-		nil)
-	       (t
-		;; This 'if' is needed because Emacs treats the next empty line
-		;; as part of the previous line.
-		(if (= (vip-line-pos 'start) end)
-		    (setq incr 1))
-		(<= (+ incr (count-lines beg end)) 1))))
-     ))
+   (let ((selective-display nil))
+     (cond ((and beg end)
+	    ;; This 'if' is needed because Emacs treats the next empty line
+	    ;; as part of the previous line.
+	    (if (or (> beg (point-max)) (> end (point-max))) ; out of range
+		()
+	      (if (and (> end beg) (= (vip-line-pos 'start) end))
+		  (setq end (min (1+ end) (point-max))))
+	      (if (and (> beg end) (= (vip-line-pos 'start) beg))
+		  (setq beg (min (1+ beg) (point-max))))
+	      (<= (count-lines beg end) 1) ))
+	   
+	   (t nil))
+	 ))
 	 
 	 
 ;; Check if the string ends with a newline.
@@ -2104,6 +2533,100 @@
 	(command-execute command)
       (exit-minibuffer))))
   
+
+(defun vip-set-search-face ()
+  (if (vip-has-face-support-p)
+      (defvar vip-search-face
+	(progn
+	  (make-face 'vip-search-face)
+	  (vip-hide-face 'vip-search-face)
+	  (or (face-differs-from-default-p 'vip-search-face)
+	      ;; face wasn't set in .vip or .Xdefaults
+	      (if (vip-can-use-colors "Black" "khaki")
+		  (progn
+		    (set-face-background 'vip-search-face "khaki")
+		    (set-face-foreground 'vip-search-face "Black"))
+		(copy-face 'italic 'vip-search-face)
+		(set-face-underline-p 'vip-search-face t)))
+	  'vip-search-face)
+        "*Face used to flash out the search pattern.")
+    ))
+  
+  
+(defun vip-set-minibuffer-faces ()
+  (if (not (vip-has-face-support-p))
+      ()
+    (defvar vip-minibuffer-emacs-face
+      (progn
+	(make-face 'vip-minibuffer-emacs-face)
+	(vip-hide-face 'vip-minibuffer-emacs-face)
+	(or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
+	    ;; face wasn't set in .vip or .Xdefaults
+	    (if vip-vi-style-in-minibuffer
+		;; emacs state is an exception in the minibuffer
+		(if (vip-can-use-colors "darkseagreen2" "Black")
+		    (progn
+		      (set-face-background
+		       'vip-minibuffer-emacs-face "darkseagreen2")
+		      (set-face-foreground
+		       'vip-minibuffer-emacs-face "Black"))
+		  (copy-face 'modeline 'vip-minibuffer-emacs-face))
+	      ;; emacs state is the main state in the minibuffer
+	      (if (vip-can-use-colors "Black" "pink")
+		  (progn
+		    (set-face-background 'vip-minibuffer-emacs-face "pink") 
+		    (set-face-foreground
+		     'vip-minibuffer-emacs-face "Black"))
+		(copy-face 'italic 'vip-minibuffer-emacs-face))
+	      ))
+	'vip-minibuffer-emacs-face)
+      "Face used in the Minibuffer when it is in Emacs state.")
+    
+    (defvar vip-minibuffer-insert-face
+      (progn
+	(make-face 'vip-minibuffer-insert-face)
+	(vip-hide-face 'vip-minibuffer-insert-face)
+	(or (face-differs-from-default-p 'vip-minibuffer-insert-face)
+	    (if vip-vi-style-in-minibuffer
+		(if (vip-can-use-colors "Black" "pink")
+		    (progn
+		      (set-face-background 'vip-minibuffer-insert-face "pink") 
+		      (set-face-foreground
+		       'vip-minibuffer-insert-face "Black"))
+		  (copy-face 'italic 'vip-minibuffer-insert-face))
+	      ;; If Insert state is an exception
+	      (if (vip-can-use-colors "darkseagreen2" "Black")
+		  (progn
+		    (set-face-background
+		     'vip-minibuffer-insert-face "darkseagreen2")
+		    (set-face-foreground
+		     'vip-minibuffer-insert-face "Black"))
+		(copy-face 'modeline 'vip-minibuffer-insert-face))
+	      (vip-italicize-face 'vip-minibuffer-insert-face)))
+	'vip-minibuffer-insert-face)
+      "Face used in the Minibuffer when it is in Insert state.")
+    
+    (defvar vip-minibuffer-vi-face
+      (progn
+	(make-face 'vip-minibuffer-vi-face)
+	(vip-hide-face 'vip-minibuffer-vi-face)
+	(or (face-differs-from-default-p 'vip-minibuffer-vi-face)
+	    (if vip-vi-style-in-minibuffer
+		(if (vip-can-use-colors "Black" "grey")
+		    (progn
+		      (set-face-background 'vip-minibuffer-vi-face "grey")
+		      (set-face-foreground 'vip-minibuffer-vi-face "Black"))
+		  (copy-face 'bold 'vip-minibuffer-vi-face))
+	      (copy-face 'bold 'vip-minibuffer-vi-face)
+	      (invert-face 'vip-minibuffer-vi-face)))
+	'vip-minibuffer-vi-face)
+      "Face used in the Minibuffer when it is in Vi state.")
+    
+    ;; the current face used in the minibuffer
+    (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
+    ))
+    
+  
 
 ;;; Reading string with history  
     
@@ -2258,8 +2781,10 @@
 	      (setq vip-cted t)
 	      (if vip-electric-mode
 		  (indent-according-to-mode)
-		(indent-to col))))
-	(vip-change-state-to-insert)))))
+		(indent-to col))
+	      ))
+	(vip-change-state-to-insert)
+	))))
 
 (defun vip-Open-line (arg)
   "Open line above."
@@ -2345,10 +2870,6 @@
    'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
   (vip-add-hook
    'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
-  ;; guard against a smartie who switched from R-replace to normal replace
-  (vip-remove-hook
-   'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
-  (if overwrite-mode (overwrite-mode nil))
   )
   
 
@@ -2424,11 +2945,9 @@
 	    ))
       
       (setq vip-replace-chars-to-delete
-	    (max 0
-		 (min vip-replace-chars-to-delete
-		      (- (vip-replace-end) vip-last-posn-in-replace-region)
-		      (- (vip-line-pos 'end) vip-last-posn-in-replace-region)
-		      )))
+	    (max 0 (min vip-replace-chars-to-delete
+			(- (vip-replace-end)
+			   vip-last-posn-in-replace-region))))
       )))
 
 
@@ -2441,7 +2960,7 @@
 		   'vip-replace-state-post-command-sentinel) 
   (vip-remove-hook
    'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel) 
-  (vip-restore-cursor-color-after-replace)
+  (vip-restore-cursor-color)
   (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
   (save-excursion
     (if (and 
@@ -2480,9 +2999,6 @@
    'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
   (vip-add-hook
    'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
-  ;; guard against a smartie who switched from R-replace to normal replace
-  (vip-remove-hook
-   'vip-post-command-hooks 'vip-replace-state-post-command-sentinel)
   )
 
 
@@ -2507,29 +3023,6 @@
     )
   (vip-hide-replace-overlay))
 
-(defun vip-replace-state-carriage-return ()
-  "Implements carriage return in Viper replace state."
-  (interactive)
-  ;; If Emacs start supporting overlay maps, as it currently supports
-  ;; text-property maps, we could do away with vip-replace-minor-mode and
-  ;; just have keymap attached to replace overlay. Then the "if part" of this
-  ;; statement can be deleted.
-  (if (or (< (point) (vip-replace-start))
-	  (> (point) (vip-replace-end)))
-      (let (vip-replace-minor-mode com)
-	(vip-set-unread-command-events last-input-char)
-	(setq com (key-binding (read-key-sequence nil)))
-	(condition-case conds
-	    (command-execute com)
-	  (error
-	   (vip-message-conditions conds))))
-    (if (not vip-allow-multiline-replace-regions)
-	(vip-replace-state-exit-cmd)
-      (if (vip-same-line (point) (vip-replace-end))
-	  (vip-replace-state-exit-cmd)
-	(vip-kill-line nil)
-	(vip-next-line-at-bol nil)))))
-
   
 ;; This is the function bound to 'R'---unlimited replace.
 ;; Similar to Emacs's own overwrite-mode.
@@ -3283,36 +3776,6 @@
   (interactive "p")
   (recenter (- (window-height) (1+ arg))))
 
-;; If point is within vip-search-scroll-threshold of window top or bottom,
-;; scroll up or down 1/7 of window height, depending on whether we are at the
-;; bottom or at the top of the  window. This function is called by vip-search
-;; (which is called from vip-search-forward/backward/next). If the value of
-;; vip-search-scroll-threshold is negative - don't scroll.
-(defun vip-adjust-window ()
-  (let ((win-height (if vip-emacs-p
-			(1- (window-height)) ; adjust for modeline
-		      (window-displayed-height)))
-	(pt (point))
-	at-top-p at-bottom-p
-	min-scroll direction)
-    (save-excursion
-      (move-to-window-line 0) ; top
-      (setq at-top-p
-	    (<= (count-lines pt (point))
-		vip-search-scroll-threshold))
-      (move-to-window-line -1) ; bottom
-      (setq at-bottom-p
-	    (<= (count-lines pt (point)) vip-search-scroll-threshold))
-      )
-    (cond (at-top-p (setq min-scroll (1- vip-search-scroll-threshold)
-			  direction  1))
-	  (at-bottom-p (setq min-scroll (1+ vip-search-scroll-threshold)
-			     direction -1)))
-    (if min-scroll
-	(recenter
-	 (* (max min-scroll (/ win-height 7)) direction)))
-    ))
-
 
 ;; paren match
 ;; must correct this to only match ( to ) etc. On the other hand
@@ -3322,9 +3785,8 @@
 (defun vip-paren-match (arg)
   "Go to the matching parenthesis."
   (interactive "P")
-  (vip-leave-region-active)
   (let ((com (vip-getcom arg))
-	parse-sexp-ignore-comments anchor-point)
+	anchor-point)
     (if (integerp arg)
 	(if (or (> arg 99) (< arg 1))
 	    (error "Prefix must be between 1 and 99")
@@ -3452,30 +3914,21 @@
 
 (setq scroll-step 1)
 
-(defun vip-scroll-screen (arg)
+(defun vip-scroll (arg)
   "Scroll to next screen."
   (interactive "p")
-  (condition-case nil
-      (if (> arg 0)
-	  (while (> arg 0)
-	    (scroll-up)
-	    (setq arg (1- arg)))
-	(while (> 0 arg)
-	  (scroll-down)
-	  (setq arg (1+ arg))))
-    (error (beep 1)
-	   (if (> arg 0)
-	       (progn
-		 (message "End of buffer")
-		 (goto-char (point-max)))
-	     (message "Beginning of buffer")
-	     (goto-char (point-min))))
-    ))
-
-(defun vip-scroll-screen-back (arg)
+  (if (> arg 0)
+      (while (> arg 0)
+	(scroll-up)
+	(setq arg (1- arg)))
+    (while (> 0 arg)
+      (scroll-down)
+      (setq arg (1+ arg)))))
+
+(defun vip-scroll-back (arg)
   "Scroll to previous screen."
   (interactive "p")
-  (vip-scroll-screen (- arg)))
+  (vip-scroll (- arg)))
 
 (defun vip-scroll-down (arg)
   "Pull down half screen."
@@ -3564,62 +4017,6 @@
 	   (setq msg "Search style remains unchanged")))
     (prin1 msg t)))
 
-(defun vip-set-vi-search-style-macros (unset)
-  "Set the macros for toggling the search style in Viper's vi-state.
-The macro that toggles case sensitivity is bound to `//', and the one that
-toggles regexp search is bound to `///'.
-With a prefix argument, this function unsets the macros. "
-  (interactive "P")
-  (or noninteractive
-      (if (not unset)
-	  (progn
-	    ;; toggle case sensitivity in search
-	    (vip-record-kbd-macro
-	     "//" 'vi-state
-	     [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
-	     't)
-	    ;; toggle regexp/vanila search
-	    (vip-record-kbd-macro
-	     "///" 'vi-state
-	     [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
-	     't)
-	    (if (interactive-p)
-		(message
-		 "// and /// now toggle case-sensitivity and regexp search.")))
-	(vip-unrecord-kbd-macro "//" 'vi-state)
-	(sit-for 2)
-	(vip-unrecord-kbd-macro "///" 'vi-state))))
-
-(defun vip-set-emacs-search-style-macros (unset &optional arg-majormode)
-  "Set the macros for toggling the search style in Viper's emacs-state.
-The macro that toggles case sensitivity is bound to `//', and the one that
-toggles regexp search is bound to `///'.
-With a prefix argument, this function unsets the macros. 
-If the optional prefix argument is non-nil and specifies a valid major mode,
-this sets the macros only in the macros in that major mode. Otherwise,
-the macros are set in the current major mode.
-\(When unsetting the macros, the second argument has no effect.\)"
-  (interactive "P")
-  (or noninteractive
-      (if (not unset)
-	  (progn
-	    ;; toggle case sensitivity in search
-	    (vip-record-kbd-macro
-	     "//" 'emacs-state
-	     [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 
-	     (or arg-majormode major-mode))
-	    ;; toggle regexp/vanila search
-	    (vip-record-kbd-macro
-	     "///" 'emacs-state
-	     [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
-	     (or arg-majormode major-mode))
-	    (if (interactive-p)
-		(message
-		 "// and /// now toggle case-sensitivity and regexp search.")))
-	(vip-unrecord-kbd-macro "//" 'emacs-state)
-	(sit-for 2)
-	(vip-unrecord-kbd-macro "///" 'emacs-state))))
-
 
 (defun vip-search-forward (arg)
   "Search a string forward. 
@@ -3688,12 +4085,15 @@
 		      (re-search-backward string))
 		  (search-forward string nil nil val)
 		  (search-backward string))
+		;; don't wait and don't flash in macros
+		(or executing-kbd-macro
+		    (vip-flash-search-pattern))
 		(if (not (equal start-point (point)))
 		    (push-mark start-point t))) 
 	    (search-failed
 	     (if (and (not fail-if-not-found) vip-search-wrap-around-t)
 	         (progn
-		   (message "Search wrapped around BOTTOM of buffer")
+		   (message "Search wrapped around end of buffer")
 		   (goto-char (point-min))
 		   (vip-search string forward (cons 1 com) t start-point 'fail)
 		   ;; don't wait in macros
@@ -3712,12 +4112,15 @@
 	      (if vip-re-search
 		  (re-search-backward string nil nil val)
 	        (search-backward string nil nil val))
+	      ;; don't wait and don't flash in macros
+	      (or executing-kbd-macro
+		  (vip-flash-search-pattern))
 	      (if (not (equal start-point (point)))
 		  (push-mark start-point t))) 
 	  (search-failed
 	   (if (and (not fail-if-not-found) vip-search-wrap-around-t)
 	       (progn
-		 (message "Search wrapped around TOP of buffer")
+		 (message "Search wrapped around beginning of buffer")
 	         (goto-char (point-max))
 	         (vip-search string forward (cons 1 com) t start-point 'fail)
 		 ;; don't wait in macros
@@ -3729,15 +4132,7 @@
 	     (error "`%s': %s not found"
 		    string
 		    (if vip-re-search "Pattern" "String"))
-	     ))))
-      ;; pull up or down if at top/bottom of window
-      (vip-adjust-window)
-      ;; highlight the result of search
-      ;; don't wait and don't highlight in macros
-      (or executing-kbd-macro
-	  vip-inside-command-argument-action
-	  (vip-flash-search-pattern))
-      )))
+	     )))))))
 
 (defun vip-search-next (arg)
   "Repeat previous search."
@@ -3838,8 +4233,7 @@
       (error "Buffer not killed"))))
 
 
-(defvar vip-smart-suffix-list
-  '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "P" "p")
+(defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
   "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
 This is useful when you the current directory contains files with the same
 prefix and many different suffixes. Usually, only one of the suffixes
@@ -3897,38 +4291,18 @@
 
 (defadvice find-file (before vip-add-suffix-advice activate)
   "Use `read-file-name' for reading arguments."
-  (interactive (cons (read-file-name "Find file: " nil default-directory)
-		     ;; if Mule and prefix argument, ask for coding system
-		     (if (or (boundp 'MULE)    ; mule integrated Emacs 19
-			     (featurep 'mule)) ; mule integrated XEmacs 20
-			 (list
-			  (and current-prefix-arg
-			       (read-coding-system "Coding-system: "))))
-		     )))
+  (interactive (list (read-file-name "Find file: "
+				     nil default-directory))))
     
 (defadvice find-file-other-window (before vip-add-suffix-advice activate)
   "Use `read-file-name' for reading arguments."
-  (interactive (cons (read-file-name "Find file in other window: "
-				     nil default-directory)
-		     ;; if Mule and prefix argument, ask for coding system
-		     (if (or (boundp 'MULE)    ; mule integrated Emacs 19
-			     (featurep 'mule)) ; mule integrated XEmacs 20
-			 (list
-			  (and current-prefix-arg
-			       (read-coding-system "Coding-system: "))))
-		     )))
+  (interactive (list (read-file-name "Find file in other window: "
+				     nil default-directory))))
     
 (defadvice find-file-other-frame (before vip-add-suffix-advice activate)
   "Use `read-file-name' for reading arguments."
-  (interactive (cons (read-file-name "Find file in other frame: "
-				     nil default-directory)
-		     ;; if Mule and prefix argument, ask for coding system
-		     (if (or (boundp 'MULE)    ; mule integrated Emacs 19
-			     (featurep 'mule)) ; mule integrated XEmacs 20
-			 (list
-			  (and current-prefix-arg
-			       (read-coding-system "Coding-system: "))))
-		     )))
+  (interactive (list (read-file-name "Find file in other frame: "
+				     nil default-directory))))
 
 (defadvice read-file-name (around vip-suffix-advice activate)
   "Tell `exit-minibuffer' to run `vip-file-add-suffix' as a hook."
@@ -3964,7 +4338,6 @@
     (setq vip-use-register nil)
     (if (vip-end-with-a-newline-p text)
 	(progn
-	  (end-of-line)
 	  (if (eobp)
 	      (insert "\n")
 	    (forward-line 1))
@@ -4158,9 +4531,8 @@
   ;; protect against error while inserting "@" and other disasters
   ;; (e.g., read-only buff)
   (condition-case conds
-      (if (or vip-allow-multiline-replace-regions
-	      (vip-same-line (vip-replace-start)
-			     (vip-replace-end)))
+      (if (vip-same-line (vip-replace-start)
+			 (vip-replace-end))
 	  (progn
 	    ;; tabs cause problems in replace, so untabify
 	    (goto-char (vip-replace-end))
@@ -4280,16 +4652,16 @@
 (defun vip-mark-point ()
   "Set mark at point of buffer."
   (interactive)
-  (let ((char (read-char)))
-    (cond ((and (<= ?a char) (<= char ?z))
-	   (point-to-register (1+ (- char ?a))))
-	  ((= char ?<) (vip-mark-beginning-of-buffer))
-	  ((= char ?>) (vip-mark-end-of-buffer))
-	  ((= char ?.) (vip-set-mark-if-necessary))
-	  ((= char ?,) (vip-cycle-through-mark-ring))
-	  ((= char ?D) (mark-defun))
-	  (t (error ""))
-	  )))
+  (let ((char (vip-read-char-exclusive)))
+  (cond ((and (<= ?a char) (<= char ?z))
+	 (point-to-register (1+ (- char ?a))))
+	((= char ?<) (vip-mark-beginning-of-buffer))
+	((= char ?>) (vip-mark-end-of-buffer))
+	((= char ?.) (vip-set-mark-if-necessary))
+	((= char ?,) (vip-cycle-through-mark-ring))
+	((= char ?D) (mark-defun))
+	(t (error ""))
+	)))
 	
 ;; Algorithm: If first invocation of this command save mark on ring, goto
 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
@@ -4380,7 +4752,6 @@
 	 (if (and (vip-same-line (point) vip-last-jump)
 		  (= (point) vip-last-jump-ignore))
 	     (goto-char vip-last-jump))
-	 (if (null (mark t)) (error "Mark is not set in this buffer"))
 	 (if (= (point) (mark t)) (pop-mark))
 	 (exchange-point-and-mark)
 	 (setq vip-last-jump (point-marker)
@@ -4448,7 +4819,6 @@
   "Auto Indentation, Vi-style."
   (interactive)
   (let ((col (current-indentation)))
-    (if abbrev-mode (expand-abbrev))
     (if vip-preserve-indent
 	(setq vip-preserve-indent nil)
       (setq vip-current-indent col))
@@ -4458,9 +4828,7 @@
 		vip-open-line vip-Open-line
 		vip-replace-state-exit-cmd))
 	(indent-to-left-margin))
-    ;; use \n instead of newline, or else <Return> will move the insert point
-    ;;(newline 1)
-    (insert "\n")
+    (newline 1)
     (if vip-auto-indent
 	(progn
 	  (setq vip-cted t)
@@ -4584,10 +4952,13 @@
 	vip-ex-style-editing-in-insert  t
 	vip-want-ctl-h-help nil)
 
-  (cond ((eq vip-expert-level 1) ; novice or beginner
-	 (global-set-key   ; in emacs-state 
-	  vip-toggle-key
-	  (if (vip-window-display-p) 'vip-iconify 'suspend-emacs))
+  (cond
+	;; a novice or a beginner
+	((eq vip-expert-level 1)
+	 (global-set-key vip-toggle-key   ;; in emacs-state
+			 (if (vip-window-display-p)
+			     'vip-iconify
+			   'suspend-emacs))
 	 (setq vip-no-multiple-ESC	     t
 	       vip-re-search	    	     t
 	       vip-vi-style-in-minibuffer    t
@@ -4595,8 +4966,8 @@
 	       vip-want-emacs-keys-in-vi     nil
 	       vip-want-emacs-keys-in-insert nil))
 	
+	;; an intermediate to guru
 	((and (> vip-expert-level 1) (< vip-expert-level 5))
-	 ;; intermediate to guru
 	 (setq vip-no-multiple-ESC     (if (vip-window-display-p) t 'twice)
 	       vip-want-emacs-keys-in-vi     t
 	       vip-want-emacs-keys-in-insert (> vip-expert-level 2))
@@ -4621,9 +4992,9 @@
 		     (cdr (assoc 'vip-no-multiple-ESC
 				 vip-saved-user-settings))))))
 	       
-	;; A wizard!!
+	;; A wizard
 	;; Ideally, if 5 is selected, a buffer should pop up to let the
-	;; user toggle the values of variables.
+	;; user toggle variable values.
 	(t (setq-default vip-ex-style-editing-in-insert
 			 (cdr (assoc 'vip-ex-style-editing-in-insert
 				     vip-saved-user-settings))
@@ -4984,6 +5355,13 @@
   
   
 
+;;; Bring in the rest of the files
+(require 'viper-mous)
+(require 'viper-macs)
+(require 'viper-ex)
+
+
+
 ;; The following is provided for compatibility with older VIP's
 
 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
@@ -4996,11 +5374,17 @@
 
 ;; This hook designed to enable Vi-style editing in comint-based modes."
 (defun vip-comint-mode-hook ()
-  (setq require-final-newline nil
-	vip-ex-style-editing-in-insert nil
+  (setq require-final-newline nil)
+  (setq vip-ex-style-editing-in-insert nil
 	vip-ex-style-motion nil)
-  (vip-change-state-to-insert))
-
+  (vip-add-local-keys 'vi-state
+		      '(("\C-m" . comint-send-input) ; return
+			("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
+  (vip-add-local-keys 'insert-state
+		      '(("\C-m" . comint-send-input) ; return
+			("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
+  )
+  
 
 ;; This sets major mode hooks to make them come up in vi-state.
 (defun vip-set-hooks ()
@@ -5021,7 +5405,6 @@
 
   (defvar help-mode-hook)
   (add-hook 'help-mode-hook 'viper-mode)
-  (vip-modify-major-mode 'help-mode 'vi-state vip-help-modifier-map)
 
   (defvar awk-mode-hook)
   (add-hook 'awk-mode-hook 'viper-mode)
@@ -5030,8 +5413,6 @@
   (add-hook 'html-mode-hook 'viper-mode)
   (defvar html-helper-mode-hook)
   (add-hook 'html-helper-mode-hook 'viper-mode)
-  (defvar java-mode-hook)
-  (add-hook 'java-mode-hook 'viper-mode)
   
   (defvar emacs-lisp-mode-hook)
   (add-hook 'emacs-lisp-mode-hook 'viper-mode)
@@ -5056,11 +5437,6 @@
 
   (defvar fortran-mode-hook)
   (add-hook 'fortran-mode-hook 'vip-mode)
-
-  (defvar basic-mode-hook)
-  (add-hook 'basic-mode-hook 'vip-mode)
-  (defvar bat-mode-hook)
-  (add-hook 'bat-mode-hook 'vip-mode)
       
   (defvar text-mode-hook)
   (add-hook 'text-mode-hook 'viper-mode)
@@ -5073,18 +5449,7 @@
   
   (defvar emerge-startup-hook)
   (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
-
-  ;; Tell vc-diff to put *vc* in Vi mode
-  (if (featurep 'vc)
-      (defadvice vc-diff (after vip-vc-ad activate)
-	"Force Vi state in VC diff buffer."
-	(vip-change-state-to-vi))
-    (vip-eval-after-load
-     "vc"
-     '(defadvice vc-diff (after vip-vc-ad activate)
-	"Force Vi state in VC diff buffer."
-	(vip-change-state-to-vi))))
-    
+  ;; Run vip-change-state-to-vi after quitting emerge.
   (vip-eval-after-load
    "emerge"
    '(defadvice emerge-quit (after vip-emerge-advice activate)
@@ -5117,25 +5482,7 @@
   
   ;; Emacs shell, ange-ftp, and comint-based modes
   (defvar comint-mode-hook)
-  (vip-modify-major-mode 
-   'comint-mode 'insert-state vip-comint-mode-modifier-map)
-  (vip-modify-major-mode 
-   'comint-mode 'vi-state vip-comint-mode-modifier-map)
-  (vip-modify-major-mode 
-   'shell-mode 'insert-state vip-comint-mode-modifier-map)
-  (vip-modify-major-mode 
-   'shell-mode 'vi-state vip-comint-mode-modifier-map)
-  ;; ange-ftp in XEmacs
-  (vip-modify-major-mode 
-   'ange-ftp-shell-mode 'insert-state vip-comint-mode-modifier-map)
-  (vip-modify-major-mode 
-   'ange-ftp-shell-mode 'vi-state vip-comint-mode-modifier-map)
-  ;; ange-ftp in Emacs
-  (vip-modify-major-mode 
-   'internal-ange-ftp-mode 'insert-state vip-comint-mode-modifier-map)
-  (vip-modify-major-mode 
-   'internal-ange-ftp-mode 'vi-state vip-comint-mode-modifier-map)
-  ;; set hook
+  (add-hook 'comint-mode-hook 'vip-change-state-to-insert)
   (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
   
   ;; Shell scripts
@@ -5145,50 +5492,9 @@
   (add-hook 'ksh-mode-hook 'viper-mode)
   
   ;; Dired
-  (vip-modify-major-mode 'dired-mode 'emacs-state vip-dired-modifier-map)
-  (vip-set-emacs-search-style-macros nil 'dired-mode)
+  ;; This is only necessary when the user uses vip-modify-major-mode
   (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
 
-  ;; Tar
-  (vip-modify-major-mode 'tar-mode 'emacs-state vip-slash-and-colon-map)
-  (vip-set-emacs-search-style-macros nil 'tar-mode)
-
-  ;; MH-E
-  (vip-modify-major-mode 'mh-folder-mode 'emacs-state vip-slash-and-colon-map)
-  (vip-set-emacs-search-style-macros nil 'mh-folder-mode)
-  ;; changing state to emacs is needed so the preceding will take hold
-  (add-hook 'mh-folder-mode-hook 'vip-change-state-to-emacs)
-  (add-hook 'mh-show-mode-hook 'viper-mode)
-
-  ;; Gnus
-  (vip-modify-major-mode 'gnus-group-mode 'emacs-state vip-slash-and-colon-map)
-  (vip-set-emacs-search-style-macros nil 'gnus-group-mode)
-  (vip-modify-major-mode 
-   'gnus-summary-mode 'emacs-state vip-slash-and-colon-map)
-  (vip-set-emacs-search-style-macros nil 'gnus-summary-mode)
-  ;; changing state to emacs is needed so the preceding will take hold
-  (add-hook 'gnus-group-mode-hook 'vip-change-state-to-emacs)
-  (add-hook 'gnus-summary-mode-hook 'vip-change-state-to-emacs)
-  (add-hook 'gnus-article-mode-hook 'viper-mode)
-
-  ;; Info
-  (vip-modify-major-mode 'Info-mode 'emacs-state vip-slash-and-colon-map)
-  (vip-set-emacs-search-style-macros nil 'Info-mode)
-  ;; Switching to emacs is needed  so the above will take hold
-  (defadvice Info-mode (after vip-Info-ad activate)
-    "Switch to emacs mode."
-    (vip-change-state-to-emacs))
-
-  ;; Buffer menu
-  (vip-modify-major-mode 
-   'Buffer-menu-mode 'emacs-state vip-slash-and-colon-map)
-  (vip-set-emacs-search-style-macros nil 'Buffer-menu-mode)
-  ;; Switching to emacs is needed  so the above will take hold
-  (defadvice Buffer-menu-mode (after vip-Buffer-menu-ad activate)
-    "Switch to emacs mode."
-    (vip-change-state-to-emacs))
-
-  ;; View mode
   (if vip-emacs-p
       (progn
 	(defvar view-mode-hook)
@@ -5231,8 +5537,14 @@
  (vector vip-repeat-from-history-key '\2) 'vi-state
  [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
  
-;; set the toggle case sensitivity and regexp search macros
-(vip-set-vi-search-style-macros nil)
+;; toggle case sensitivity in search
+(vip-record-kbd-macro
+ "//" 'vi-state
+ [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't)
+;; toggle regexp/vanila search
+(vip-record-kbd-macro
+ "///" 'vi-state
+ [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't)
  
 
 ;; ~/.vip is loaded if it exists
@@ -5261,6 +5573,8 @@
 	      
 
 (vip-set-minibuffer-style)
+(vip-set-minibuffer-faces)
+(vip-set-search-face)
 (if vip-buffer-search-char
     (vip-buffer-search-enable))
 (vip-update-alphanumeric-class)
@@ -5289,10 +5603,15 @@
 ;; This is taken care of by vip-insert-global-user-map.
 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
 
-
+(define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC)
 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
 ;; iconifies Emacs
-(define-key vip-vi-intercept-map vip-toggle-key 'vip-toggle-key-action)
+(define-key vip-vi-intercept-map vip-toggle-key
+  '(lambda () (interactive)
+     (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
+	 (if (vip-window-display-p) (vip-iconify) (suspend-emacs))
+       (vip-change-state-to-emacs))))
+
 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
 
 
@@ -5313,6 +5632,7 @@
 
 (run-hooks 'vip-load-hook) ; the last chance to change something
 
+(provide 'viper)
 (provide 'vip19)
 (provide 'vip)