Update comment about _XOPEN_SOURCE.
[bpt/emacs.git] / lisp / emulation / viper-cmd.el
index 53f12d7..ce3b24c 100644 (file)
   (let* ((snm (symbol-name name))
         (nm-p (intern (concat snm "-p")))
         (nms (intern (concat snm "s"))))
-    (` (defun (, nm-p) (com) 
-        (consp (memq (if (and (viper-characterp com) (< com 0))
-                         (- com) com) (, nms)))))))
-  
+    `(defun ,nm-p (com)
+      (consp (viper-memq-char
+             (if (and (viper-characterp com) (< com 0))
+                 (- com) com)
+             ,nms)
+            ))))
+
 ;; Variables for defining VI commands
 
 ;; Modifying commands that can be prefixes to movement commands
 (defconst viper-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
 ;; define viper-prefix-command-p
 (viper-test-com-defun viper-prefix-command)
-  
+
 ;; Commands that are pairs eg. dd. r and R here are a hack
 (defconst viper-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
 ;; define viper-charpair-command-p
 (defconst viper-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
                                     ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
                                     ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
-                                    ?; ?, ?0 ?? ?/ ?\C-m ?\ 
+                                    ?\; ?, ?0 ?? ?/ ?\  ?\C-m
                                     space return
                                     delete backspace
                                     )
 ;; define viper-vi-command-p
 (viper-test-com-defun viper-vi-command)
 
+;; Where viper saves mark. This mark is resurrected by m^
+(defvar viper-saved-mark nil)
+
+
 \f
 ;;; CODE
 
 
 ;; Runs viper-after-change-functions inside after-change-functions
 (defun viper-after-change-sentinel (beg end len)
-  (let ((list viper-after-change-functions))
-    (while list
-      (funcall (car list) beg end len)
-      (setq list (cdr list)))))
-      
+  (run-hook-with-args 'viper-after-change-functions beg end len))
+
 ;; Runs viper-before-change-functions inside before-change-functions
 (defun viper-before-change-sentinel (beg end)
-  (let ((list viper-before-change-functions))
-    (while list
-      (funcall (car list) beg end)
-      (setq list (cdr list)))))
+  (run-hook-with-args 'viper-before-change-functions beg end))
 
 (defsubst viper-post-command-sentinel ()
-  (run-hooks 'viper-post-command-hooks))
-  
+  (run-hooks 'viper-post-command-hooks)
+  (if (eq viper-current-state 'vi-state)
+      (viper-restore-cursor-color 'after-insert-mode)))
+
 (defsubst viper-pre-command-sentinel ()
   (run-hooks 'viper-pre-command-hooks))
-  
+
 ;; Needed so that Viper will be able to figure the last inserted
 ;; chunk of text with reasonable accuracy.
 (defsubst viper-insert-state-post-command-sentinel ()
           viper-insert-point
           (>= (point) viper-insert-point))
       (setq viper-last-posn-while-in-insert-state (point-marker)))
-  (if (eq viper-current-state 'insert-state)
+  (or (viper-overlay-p viper-replace-overlay)
       (progn
-       (or (stringp viper-saved-cursor-color)
+       (viper-set-replace-overlay (point-min) (point-min))
+       (viper-hide-replace-overlay)))
+  (if (eq viper-current-state 'insert-state)
+      (let ((has-saved-cursor-color-in-insert-mode
+            (stringp (viper-get-saved-cursor-color-in-insert-mode))))
+       (or has-saved-cursor-color-in-insert-mode
            (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
-           (setq viper-saved-cursor-color (viper-get-cursor-color)))
-       (if (stringp viper-saved-cursor-color)
-           (viper-change-cursor-color viper-insert-state-cursor-color))
-       ))
-  (if (and (eq this-command 'dabbrev-expand)
+           ;; save current color, if not already saved
+           (viper-save-cursor-color 'before-insert-mode))
+       ;; set insert mode cursor color
+       (viper-change-cursor-color viper-insert-state-cursor-color)))
+
+  (if (and (memq this-command '(dabbrev-expand hippie-expand))
           (integerp viper-pre-command-point)
           (markerp viper-insert-point)
           (marker-position viper-insert-point)
           (> viper-insert-point viper-pre-command-point))
       (viper-move-marker-locally viper-insert-point viper-pre-command-point))
   )
-  
-(defsubst viper-insert-state-pre-command-sentinel ()
-  (or (memq this-command '(self-insert-command))
+
+(defsubst viper-preserve-cursor-color ()
+  (or (memq this-command '(self-insert-command
+                          viper-del-backward-char-in-insert
+                          viper-del-backward-char-in-replace
+                          viper-delete-backward-char
+                          viper-join-lines
+                          viper-delete-char))
       (memq (viper-event-key last-command-event)
            '(up down left right (meta f) (meta b)
-                (control n) (control p) (control f) (control b)))
-      (viper-restore-cursor-color-after-insert))
-  (if (and (eq this-command 'dabbrev-expand)
+                (control n) (control p) (control f) (control b)))))
+
+(defsubst viper-insert-state-pre-command-sentinel ()
+  (or (viper-preserve-cursor-color)
+      (viper-restore-cursor-color 'after-insert-mode))
+  (if (and (memq this-command '(dabbrev-expand hippie-expand))
           (markerp viper-insert-point)
           (marker-position viper-insert-point))
       (setq viper-pre-command-point (marker-position viper-insert-point))))
-       
+
 (defsubst viper-R-state-post-command-sentinel ()
   ;; Restoring cursor color is needed despite
   ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
   ;; in another frame, the pre-command hook won't change cursor color to
   ;; default in that other frame.  So, if the second frame cursor was red and
   ;; we set the point outside the replacement region, then the cursor color
-  ;; will remain red. Restoring the default, below, prevents this.
+  ;; will remain red.  Restoring the default, below, prevents this.
   (if (and (<= (viper-replace-start) (point))
           (<=  (point) (viper-replace-end)))
       (viper-change-cursor-color viper-replace-overlay-cursor-color)
-    (viper-restore-cursor-color-after-replace)
+    (viper-restore-cursor-color 'after-replace-mode)
     ))
 
 ;; to speed up, don't change cursor color before self-insert
 ;; and common move commands
 (defsubst viper-replace-state-pre-command-sentinel ()
-  (or (memq this-command '(self-insert-command))
-      (memq (viper-event-key last-command-event)
-           '(up down left right (meta f) (meta b)
-                (control n) (control p) (control f) (control b)))
-      (viper-restore-cursor-color-after-replace)))
-  
+  (or (viper-preserve-cursor-color)
+      (viper-restore-cursor-color 'after-replace-mode)))
+
+
+;; Make sure we don't delete more than needed.
+;; This is executed at viper-last-posn-in-replace-region
+(defsubst viper-trim-replace-chars-to-delete-if-necessary ()
+  (setq viper-replace-chars-to-delete
+       (max 0
+            (min viper-replace-chars-to-delete
+                 ;; Don't delete more than to the end of repl overlay
+                 (viper-chars-in-region
+                  (viper-replace-end) viper-last-posn-in-replace-region)
+                 ;; point is viper-last-posn-in-replace-region now
+                 ;; So, this limits deletion to the end of line
+                 (viper-chars-in-region (point) (viper-line-pos 'end))
+                 ))))
+
+
 (defun viper-replace-state-post-command-sentinel ()
   ;; Restoring cursor color is needed despite
   ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
   ;; in another frame, the pre-command hook won't change cursor color to
   ;; default in that other frame.  So, if the second frame cursor was red and
   ;; we set the point outside the replacement region, then the cursor color
-  ;; will remain red. Restoring the default, below, fixes this problem.
+  ;; will remain red.  Restoring the default, below, fixes this problem.
   ;;
-  ;; We optimize for self-insert-command's here, since they either don't change
+  ;; We optimize for some commands, like self-insert-command,
+  ;; viper-delete-backward-char, etc., since they either don't change
   ;; cursor color or, if they terminate replace mode, the color will be changed
   ;; in viper-finish-change
-  (or (memq this-command '(self-insert-command))
-      (viper-restore-cursor-color-after-replace))
-  (cond 
+  (or (viper-preserve-cursor-color)
+      (viper-restore-cursor-color 'after-replace-mode))
+  (cond
    ((eq viper-current-state 'replace-state)
     ;; delete characters to compensate for inserted chars.
     (let ((replace-boundary (viper-replace-end)))
        ;; terminate replace mode if reached replace limit
        (if (= viper-last-posn-in-replace-region (viper-replace-end))
            (viper-finish-change)))
-      
+
       (if (viper-pos-within-region
           (point) (viper-replace-start) replace-boundary)
          (progn
   ;; 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.
+
+  (make-local-hook 'viper-after-change-functions)
+  (make-local-hook 'viper-before-change-functions)
+  (make-local-hook 'viper-post-command-hooks)
+  (make-local-hook 'viper-pre-command-hooks)
+
   (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
-  (viper-remove-hook 'viper-post-command-hooks
-                    'viper-insert-state-post-command-sentinel)
-  (viper-remove-hook 'viper-pre-command-hooks
-                    'viper-insert-state-pre-command-sentinel)
+  (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
+               ;; 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-insert-point
                       viper-last-posn-while-in-insert-state)
                      (if viper-began-as-replace
                          (setq viper-began-as-replace nil)
                          (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-ex-style-editing
+
+               (if viper-ESC-moves-cursor-back
                    (or (bolp) (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))
-        (viper-add-hook 'viper-post-command-hooks
-                        'viper-insert-state-post-command-sentinel t)
-        (viper-add-hook 'viper-pre-command-hooks
-                        'viper-insert-state-pre-command-sentinel t))
+        (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!
 
   )
 
 
-    
+
 (defun viper-adjust-keys-for (state)
   "Make necessary adjustments to keymaps before entering STATE."
   (cond ((memq state '(insert-state replace-state))
                 (define-key viper-insert-basic-map "\C-j" 'viper-autoindent)))
           (define-key viper-insert-basic-map "\C-m" nil)
           (define-key viper-insert-basic-map "\C-j" nil))
-                   
+
         (setq viper-insert-diehard-minor-mode
               (not viper-want-emacs-keys-in-insert))
-                  
+
         (if viper-want-ctl-h-help
             (progn
               (define-key viper-insert-basic-map "\C-h" 'help-command)
               (define-key viper-replace-map "\C-h" 'help-command))
-          (define-key viper-insert-basic-map 
+          (define-key viper-insert-basic-map
             "\C-h" 'viper-del-backward-char-in-insert)
           (define-key viper-replace-map
             "\C-h" 'viper-del-backward-char-in-replace))
         ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
         (define-key viper-vi-basic-map [backspace] 'viper-backward-char))
        ))
-            
-   
+
+
 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
 ;; This ensures that Viper bindings are in effect, regardless of which minor
 ;; modes were turned on by the user or by other packages.
 (defun viper-normalize-minor-mode-map-alist ()
-  (setq minor-mode-map-alist 
+  (setq minor-mode-map-alist
        (viper-append-filter-alist
-        (list
-              (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
-              (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map) 
+        (list (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
+              (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map)
               (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map)
               (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map)
               (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map)
               (cons 'viper-vi-state-modifier-minor-mode
                     (if (keymapp
                          (cdr (assoc major-mode
-                                     viper-vi-state-modifier-alist))) 
+                                     viper-vi-state-modifier-alist)))
                         (cdr (assoc major-mode viper-vi-state-modifier-alist))
                       viper-empty-keymap))
               (cons 'viper-vi-diehard-minor-mode  viper-vi-diehard-map)
               (cons 'viper-vi-basic-minor-mode     viper-vi-basic-map)
               (cons 'viper-insert-intercept-minor-mode
-                    viper-insert-intercept-map) 
+                    viper-insert-intercept-map)
               (cons 'viper-replace-minor-mode  viper-replace-map)
               ;; viper-insert-minibuffer-minor-mode must come after
-              ;; viper-replace-minor-mode 
+              ;; viper-replace-minor-mode
               (cons 'viper-insert-minibuffer-minor-mode
-                    viper-minibuffer-map) 
+                    viper-minibuffer-map)
               (cons 'viper-insert-local-user-minor-mode
                     viper-insert-local-user-map)
               (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map)
               (cons 'viper-insert-state-modifier-minor-mode
                     (if (keymapp
                          (cdr (assoc major-mode
-                                     viper-insert-state-modifier-alist))) 
+                                     viper-insert-state-modifier-alist)))
                         (cdr (assoc major-mode
                                     viper-insert-state-modifier-alist))
                       viper-empty-keymap))
                       viper-empty-keymap))
               )
         minor-mode-map-alist)))
-        
 
 
 \f
 
 ;; Modifies mode-line-buffer-identification.
 (defun viper-refresh-mode-line ()
-  (setq viper-mode-string      
+  (setq viper-mode-string
        (cond ((eq viper-current-state 'emacs-state) viper-emacs-state-id)
              ((eq viper-current-state 'vi-state) viper-vi-state-id)
              ((eq viper-current-state 'replace-state) viper-replace-state-id)
              ((eq viper-current-state 'insert-state) viper-insert-state-id)))
-    
+
   ;; Sets Viper mode string in global-mode-string
   (force-mode-line-update))
-       
+
 
 ;; Switch from Insert state to Vi state.
 (defun viper-exit-insert-state ()
 
 (defun viper-set-mode-vars-for (state)
   "Sets Viper minor mode variables to put Viper's state STATE in effect."
-  
+
   ;; Emacs state
   (setq viper-vi-minibuffer-minor-mode      nil
         viper-insert-minibuffer-minor-mode   nil
        viper-vi-intercept-minor-mode        nil
        viper-insert-intercept-minor-mode    nil
-       
+
        viper-vi-local-user-minor-mode       nil
        viper-vi-kbd-minor-mode              nil
        viper-vi-global-user-minor-mode      nil
        viper-vi-state-modifier-minor-mode   nil
        viper-vi-diehard-minor-mode          nil
         viper-vi-basic-minor-mode            nil
-       
+
        viper-replace-minor-mode               nil
-       
+
        viper-insert-local-user-minor-mode     nil
        viper-insert-kbd-minor-mode            nil
        viper-insert-global-user-minor-mode    nil
        viper-emacs-global-user-minor-mode     t
        viper-emacs-state-modifier-minor-mode  t
        )
-  
+
   ;; Vi state
   (if (eq state 'vi-state) ; adjust for vi-state
-      (setq 
-       viper-vi-intercept-minor-mode      t 
+      (setq
+       viper-vi-intercept-minor-mode      t
        viper-vi-minibuffer-minor-mode     (viper-is-in-minibuffer)
        viper-vi-local-user-minor-mode     t
        viper-vi-kbd-minor-mode            (not (viper-is-in-minibuffer))
        viper-vi-global-user-minor-mode    t
        viper-vi-state-modifier-minor-mode    t
-       ;; don't let the diehard keymap block command completion 
+       ;; don't let the diehard keymap block command completion
        ;; and other things in the minibuffer
        viper-vi-diehard-minor-mode        (not
                                            (or viper-want-emacs-keys-in-vi
                                                (viper-is-in-minibuffer)))
-       viper-vi-basic-minor-mode             t 
+       viper-vi-basic-minor-mode             t
        viper-emacs-intercept-minor-mode       nil
        viper-emacs-local-user-minor-mode      nil
        viper-emacs-kbd-minor-mode            nil
        viper-emacs-global-user-minor-mode     nil
        viper-emacs-state-modifier-minor-mode  nil
        ))
-  
+
   ;; Insert and Replace states
   (if (member state '(insert-state replace-state))
-      (setq 
-       viper-insert-intercept-minor-mode      t 
+      (setq
+       viper-insert-intercept-minor-mode      t
        viper-replace-minor-mode                      (eq state 'replace-state)
        viper-insert-minibuffer-minor-mode     (viper-is-in-minibuffer)
        viper-insert-local-user-minor-mode     t
        viper-insert-kbd-minor-mode                   (not (viper-is-in-minibuffer))
        viper-insert-global-user-minor-mode     t
        viper-insert-state-modifier-minor-mode  t
-       ;; don't let the diehard keymap block command completion 
+       ;; don't let the diehard keymap block command completion
        ;; and other things in the minibuffer
        viper-insert-diehard-minor-mode               (not
                                               (or
        viper-emacs-global-user-minor-mode     nil
        viper-emacs-state-modifier-minor-mode  nil
        ))
-       
+
   ;; minibuffer faces
   (if (viper-has-face-support-p)
       (setq viper-minibuffer-current-face
                  ((eq state 'vi-state) viper-minibuffer-vi-face)
                  ((memq state '(insert-state replace-state))
                   viper-minibuffer-insert-face))))
-  
+
   (if (viper-is-in-minibuffer)
       (viper-set-minibuffer-overlay))
   )
   (interactive)
   (if (and viper-first-time (not (viper-is-in-minibuffer)))
       (viper-mode)
-    (if overwrite-mode (overwrite-mode nil))
+    (if overwrite-mode (overwrite-mode -1))
+    (or (viper-overlay-p viper-replace-overlay)
+      (viper-set-replace-overlay (point-min) (point-min)))
+    (viper-hide-replace-overlay)
     (if abbrev-mode (expand-abbrev))
     (if (and auto-fill-function (> (current-column) fill-column))
        (funcall auto-fill-function))
     (viper-adjust-undo)
     (viper-change-state 'vi-state)
 
-    (viper-restore-cursor-color-after-insert)
-    
+    (viper-restore-cursor-color 'after-insert-mode)
+
     ;; Protect against user errors in hooks
     (condition-case conds
        (run-hooks 'viper-vi-state-hook)
   "Change Viper state to Insert."
   (interactive)
   (viper-change-state 'insert-state)
-  
-  (or (stringp viper-saved-cursor-color)
-      (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
-      (setq viper-saved-cursor-color (viper-get-cursor-color)))
-  ;; Commented out, because if viper-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 viper-saved-cursor-color)
-;;;      (viper-change-cursor-color viper-insert-state-cursor-color))
+
+  (or (viper-overlay-p viper-replace-overlay)
+      (viper-set-replace-overlay (point-min) (point-min)))
+  (viper-hide-replace-overlay)
+
+  (let ((has-saved-cursor-color-in-insert-mode
+        (stringp (viper-get-saved-cursor-color-in-insert-mode))))
+    (or has-saved-cursor-color-in-insert-mode
+       (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
+       (viper-save-cursor-color 'before-insert-mode))
+    (viper-change-cursor-color viper-insert-state-cursor-color))
 
   ;; Protect against user errors in hooks
   (condition-case conds
       (run-hooks 'viper-insert-state-hook)
     (error
      (viper-message-conditions conds))))
-     
+
 (defsubst viper-downgrade-to-insert ()
- (setq viper-current-state 'insert-state
-       viper-replace-minor-mode nil))
+  ;; Protect against user errors in hooks
+  (condition-case conds
+      (run-hooks 'viper-insert-state-hook)
+    (error
+     (viper-message-conditions conds)))
+  (setq viper-current-state 'insert-state
+       viper-replace-minor-mode nil))
 
-    
-  
-;; Change to replace state. When the end of replacement region is reached,
+
+
+;; Change to replace state.  When the end of replacement region is reached,
 ;; replace state changes to insert state.
 (defun viper-change-state-to-replace (&optional non-R-cmd)
   (viper-change-state 'replace-state)
       (run-hooks 'viper-insert-state-hook 'viper-replace-state-hook)
     (error
      (viper-message-conditions conds)))
-  
+
   (if non-R-cmd
       (viper-start-replace)
     ;; 'R' is implemented using Emacs's overwrite-mode
     (viper-start-R-mode))
   )
 
-    
+
 (defun viper-change-state-to-emacs ()
   "Change Viper state to Emacs."
   (interactive)
+  (or (viper-overlay-p viper-replace-overlay)
+      (viper-set-replace-overlay (point-min) (point-min)))
+  (viper-hide-replace-overlay)
   (viper-change-state 'emacs-state)
-  
+
   ;; Protect agains user errors in hooks
   (condition-case conds
       (run-hooks 'viper-emacs-state-hook)
     (error
      (viper-message-conditions conds))))
-  
+
 ;; escape to emacs mode termporarily
 (defun viper-escape-to-emacs (arg &optional events)
   "Escape to Emacs state from Vi state for one Emacs command.
 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 ?\\)
+  (if (viper= last-command-char ?\\)
       (message "Switched to EMACS state for the next command..."))
   (viper-escape-to-state arg events 'emacs-state))
-  
+
 ;; escape to Vi mode termporarily
 (defun viper-escape-to-vi (arg)
   "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
+Vi's prefix argument will be used.  Otherwise, the prefix argument passed to
 `viper-escape-to-vi' is used."
   (interactive "P")
   (message "Switched to VI state for the next command...")
   (viper-escape-to-state arg nil 'vi-state))
-  
+
 ;; Escape to STATE mode for one Emacs command.
 (defun viper-escape-to-state (arg events state)
   ;;(let (com key prefix-arg)
@@ -668,15 +715,15 @@ Vi's prefix argument will be used. Otherwise, the prefix argument passed to
     (viper-set-mode-vars-for state)
     (viper-normalize-minor-mode-map-alist)
     (if events (viper-set-unread-command-events events))
-    
+
     ;; protect against keyboard quit and other errors
     (condition-case nil
-       (let (viper-vi-kbd-minor-mode 
+       (let (viper-vi-kbd-minor-mode
              viper-insert-kbd-minor-mode
              viper-emacs-kbd-minor-mode)
          (unwind-protect
              (progn
-               (setq com (key-binding (setq key 
+               (setq com (key-binding (setq key
                                             (if viper-xemacs-p
                                                 (read-key-sequence nil)
                                               (read-key-sequence nil t)))))
@@ -687,12 +734,12 @@ Vi's prefix argument will be used. Otherwise, the prefix argument passed to
                (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
+         ;; `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
+         ;; `state'.  When we return to the orig buffer, the bindings will be
          ;; screwed up.
          (viper-set-mode-vars-for viper-current-state)
-         
+
          ;; this-command, last-command-char, last-command-event
          (setq this-command com)
          (if viper-xemacs-p ; XEmacs represents key sequences as vectors
@@ -703,7 +750,7 @@ Vi's prefix argument will be used. Otherwise, the prefix argument passed to
            (setq last-command-event
                  (viper-copy-event (viper-seq-last-elt key))
                  last-command-char last-command-event))
-           
+
          (if (commandp com)
              (progn
                (setq prefix-arg (or prefix-arg arg))
@@ -713,7 +760,7 @@ Vi's prefix argument will be used. Otherwise, the prefix argument passed to
       (error (beep 1))))
   ;; set state in the new buffer
   (viper-set-mode-vars-for viper-current-state))
-      
+
 (defun viper-exec-form-in-vi  (form)
   "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
   (let ((buff (current-buffer))
@@ -748,9 +795,9 @@ Similar to viper-escape-to-emacs, but accepts forms rather than keystrokes."
     (viper-set-mode-vars-for viper-current-state)
     result))
 
-  
+
 ;; This is needed because minor modes sometimes override essential Viper
-;; bindings. By letting Viper know which files these modes are in, it will
+;; bindings.  By letting Viper know which files these modes are in, it will
 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
 (defun viper-harness-minor-mode (load-file)
   "Familiarize Viper with a minor mode defined in LOAD_FILE.
@@ -764,9 +811,9 @@ LOAD-FILE is a name of the file where the specific minor mode is defined.
 Suffixes such as .el or .elc should be stripped."
 
   (interactive "sEnter name of the load file: ")
-  
+
   (eval-after-load load-file '(viper-normalize-minor-mode-map-alist))
-  
+
   ;; Change the default for minor-mode-map-alist each time a harnessed minor
   ;; mode adds its own keymap to the a-list.
   (eval-after-load
@@ -779,16 +826,16 @@ Suffixes such as .el or .elc should be stripped."
 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
 If viper-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 viper-no-multiple-ESC is nil, ESC functions
+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 viper-no-multiple-ESC is nil, ESC functions
 as a Meta key and any number of multiple escapes is allowed."
   (interactive "P")
   (let (char)
     (cond ((and (not viper-no-multiple-ESC) (eq viper-current-state 'vi-state))
           (setq char (viper-read-char-exclusive))
           (viper-escape-to-emacs arg (list ?\e char) ))
-         ((and (eq viper-no-multiple-ESC 'twice) 
+         ((and (eq viper-no-multiple-ESC 'twice)
                (eq viper-current-state 'vi-state))
           (setq char (viper-read-char-exclusive))
           (if (= char (string-to-char viper-ESC-key))
@@ -824,7 +871,7 @@ as a Meta key and any number of multiple escapes is allowed."
        (keyseq [nil])
        inhibit-quit)
     (if (viper-ESC-event-p event)
-       (progn 
+       (progn
          (if (viper-fast-keysequence-p)
              (progn
                (let (minor-mode-map-alist)
@@ -850,14 +897,15 @@ as a Meta key and any number of multiple escapes is allowed."
                ;;
                ;; If `first-key' is not an ESC event, we make it into the
                ;; last-command-event in order to pretend that this key was
-               ;; pressed. This is needed to allow arrow keys to be bound to
-               ;; macros. Otherwise, viper-exec-mapped-kbd-macro will think
+               ;; pressed.  This is needed to allow arrow keys to be bound to
+               ;; macros.  Otherwise, viper-exec-mapped-kbd-macro will think
                ;; that the last event was ESC and so it'll execute whatever is
                ;; bound to ESC. (Viper macros can't be bound to
                ;; ESC-sequences).
                (let* ((first-key (elt keyseq 0))
                       (key-mod (event-modifiers first-key)))
-                 (cond ((viper-ESC-event-p first-key)
+                 (cond ((and (viper-ESC-event-p first-key)
+                             (not viper-translate-all-ESC-keysequences))
                         ;; put keys following ESC on the unread list
                         ;; and return ESC as the key-sequence
                         (viper-set-unread-command-events (subseq keyseq 1))
@@ -868,7 +916,7 @@ as a Meta key and any number of multiple escapes is allowed."
                        ((and viper-xemacs-p
                              (key-press-event-p first-key)
                              (equal '(meta) key-mod))
-                        (viper-set-unread-command-events 
+                        (viper-set-unread-command-events
                          (vconcat (vector
                                    (character-to-event (event-key first-key)))
                                   (subseq keyseq 1)))
@@ -879,7 +927,7 @@ as a Meta key and any number of multiple escapes is allowed."
                               (viper-copy-event first-key)))
                        ))
                ) ; end progn
-               
+
            ;; this is escape event with nothing after it
            ;; put in unread-command-event and then re-read
            (viper-set-unread-command-events event)
@@ -890,7 +938,7 @@ as a Meta key and any number of multiple escapes is allowed."
       (setq keyseq (vector event)))
     keyseq))
 
-    
+
 
 ;; Listen to ESC key.
 ;; If a sequence of keys starting with ESC is issued with very short delays,
@@ -898,9 +946,9 @@ as a Meta key and any number of multiple escapes is allowed."
 (defun viper-intercept-ESC-key ()
   "Function that implements ESC key in Viper emulation of Vi."
   (interactive)
-  (let ((cmd (or (key-binding (viper-envelop-ESC-key)) 
+  (let ((cmd (or (key-binding (viper-envelop-ESC-key))
                 '(lambda () (interactive) (error "")))))
-    
+
     ;; call the actual function to execute ESC (if no other symbols followed)
     ;; or the key bound to the ESC sequence (if the sequence was issued
     ;; with very short delay between characters.
@@ -909,14 +957,14 @@ as a Meta key and any number of multiple escapes is allowed."
              (cond ((eq viper-current-state 'vi-state)
                     'viper-ESC)
                    ((eq viper-current-state 'insert-state)
-                    'viper-exit-insert-state) 
+                    'viper-exit-insert-state)
                    ((eq viper-current-state 'replace-state)
                     'viper-replace-state-exit-cmd)
                    (t 'viper-change-state-to-vi)
                    )))
     (call-interactively cmd)))
 
-          
+
 
 \f
 ;; prefix argument for Vi mode
@@ -947,94 +995,96 @@ as a Meta key and any number of multiple escapes is allowed."
 ;; Get com part of prefix-argument ARG and modify it.
 (defun viper-getCom (arg)
   (let ((com (viper-getcom arg)))
-    (cond ((equal com ?c) ?c)
+    (cond ((viper= com ?c) ?c)
          ;; Previously, ?c was being converted to ?C, but this prevented
          ;; multiline replace regions.
-         ;;((equal com ?c) ?C)
-         ((equal com ?d) ?D)
-         ((equal com ?y) ?Y)
+         ;;((viper= com ?c) ?C)
+         ((viper= com ?d) ?D)
+         ((viper= com ?y) ?Y)
          (t com))))
 
 
-;; Compute numeric prefix arg value. 
-;; Invoked by EVENT. COM is the command part obtained so far.
-(defun viper-prefix-arg-value (event com)
+;; Compute numeric prefix arg value.
+;; Invoked by EVENT-CHAR.  COM is the command part obtained so far.
+(defun viper-prefix-arg-value (event-char com)
   (let ((viper-intermediate-command 'viper-digit-argument)
        value func)
     ;; read while number
-    (while (and (viper-characterp event) (>= event ?0) (<= event ?9))
-      (setq value (+ (* (if (integerp value) value 0) 10) (- event ?0)))
-      (setq event (viper-read-event-convert-to-char)))
-    
+    (while (and (viper-characterp event-char)
+               (>= event-char ?0) (<= event-char ?9))
+      (setq value (+ (* (if (integerp value) value 0) 10) (- event-char ?0)))
+      (setq event-char (viper-read-event-convert-to-char)))
+
     (setq prefix-arg value)
     (if com (setq prefix-arg (cons prefix-arg com)))
-    (while (eq event ?U)
+    (while (eq event-char ?U)
       (viper-describe-arg prefix-arg)
-      (setq event (viper-read-event-convert-to-char)))
-    
+      (setq event-char (viper-read-event-convert-to-char)))
+
     (if (or com (and (not (eq viper-current-state 'vi-state))
                     ;; make sure it is a Vi command
-                    (viper-characterp event) (viper-vi-command-p event)
+                    (viper-characterp event-char)
+                    (viper-vi-command-p event-char)
                     ))
        ;; 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 viper-digit-argument command was
+         ;; last.  If com is not nil, the viper-digit-argument command was
          ;; called from within viper-prefix-arg command, such as `d', `w',
-         ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
+         ;; etc., i.e., the user typed, say, d2.  In this case, `com' would be
          ;; `d', `w', etc.  If viper-digit-argument was invoked by
          ;; viper-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,
+         ;; current state is not vi-state), then `event-char' 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.
-         (cond ((eq event 'return) (setq event ?\C-m))
-               ((eq event 'delete) (setq event ?\C-?))
-               ((eq event 'backspace) (setq event ?\C-h))
-               ((eq event 'space) (setq event ?\ )))
-         (setq last-command-char (or com event))
-         (setq func (viper-exec-form-in-vi 
-                     (` (key-binding (char-to-string (, event))))))
+         (cond ((eq event-char 'return) (setq event-char ?\C-m))
+               ((eq event-char 'delete) (setq event-char ?\C-?))
+               ((eq event-char 'backspace) (setq event-char ?\C-h))
+               ((eq event-char 'space) (setq event-char ?\ )))
+         (setq last-command-char (or com event-char))
+         (setq func (viper-exec-form-in-vi
+                     `(key-binding (char-to-string ,event-char))))
          (funcall func prefix-arg)
          (setq prefix-arg nil))
       ;; some other command -- let emacs do it in its own way
-      (viper-set-unread-command-events event))
+      (viper-set-unread-command-events event-char))
     ))
-                    
+
 
 ;; Vi operator as prefix argument."
 (defun viper-prefix-arg-com (char value com)
   (let ((cont t)
-       cmd-info 
+       cmd-info
        cmd-to-exec-at-end)
     (while (and cont
-               (memq char
-                     (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
-                           viper-buffer-search-char)))
+               (viper-memq-char char
+                                (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
+                                      viper-buffer-search-char)))
       (if com
          ;; this means that we already have a command character, so we
          ;; construct a com list and exit while.  however, if char is "
          ;; it is an error.
          (progn
            ;; new com is (CHAR . OLDCOM)
-           (if (memq char '(?# ?\")) (error ""))
+           (if (viper-memq-char char '(?# ?\")) (error ""))
            (setq com (cons char com))
            (setq cont nil))
        ;; If com is nil we set com as char, and read more.  Again, if char is
        ;; ", we read the name of register and store it in viper-use-register.
        ;; if char is !, =, or #, a complete com is formed so we exit the while
        ;; loop.
-       (cond ((memq char '(?! ?=))
+       (cond ((viper-memq-char char '(?! ?=))
               (setq com char)
               (setq char (read-char))
               (setq cont nil))
-             ((= char ?#)
+             ((viper= char ?#)
               ;; read a char and encode it as com
               (setq com (+ 128 (read-char)))
               (setq char (read-char)))
-             ((= char ?\")
+             ((viper= char ?\")
               (let ((reg (read-char)))
                 (if (viper-valid-register reg)
                     (setq viper-use-register reg)
@@ -1050,7 +1100,7 @@ as a Meta key and any number of multiple escapes is allowed."
       ;; we prepare the command that will be executed at the end.
       (progn
        (setq cmd-info (cons value com))
-       (while (= char ?U)
+       (while (viper= char ?U)
          (viper-describe-arg cmd-info)
          (setq char (read-char)))
        ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
@@ -1058,18 +1108,18 @@ as a Meta key and any number of multiple escapes is allowed."
        (or (viper-movement-command-p char)
            (viper-digit-command-p char)
            (viper-regsuffix-command-p char)
-           (= char ?!) ; bang command
+           (viper= char ?!) ; bang command
            (error ""))
        (setq cmd-to-exec-at-end
              (viper-exec-form-in-vi 
-              (` (key-binding (char-to-string (, char)))))))
+              `(key-binding (char-to-string ,char)))))
     
     ;; as com is non-nil, this means that we have a command to execute
-    (if (memq (car com) '(?r ?R))
+    (if (viper-memq-char (car com) '(?r ?R))
        ;; execute apropriate region command.
        (let ((char (car com)) (com (cdr com)))
          (setq prefix-arg (cons value com))
-         (if (= char ?r) (viper-region prefix-arg)
+         (if (viper= char ?r) (viper-region prefix-arg)
            (viper-Region prefix-arg))
          ;; reset prefix-arg
          (setq prefix-arg nil))
@@ -1078,8 +1128,8 @@ as a Meta key and any number of multiple escapes is allowed."
       (setq prefix-arg nil)
       (cond 
        ;; If we change ?C to ?c here, then cc will enter replacement mode
-       ;; rather than deleting lines. However, it will affect 1 less line than
-       ;; normal. We decided to not use replacement mode here and follow Vi,
+       ;; rather than deleting lines.  However, it will affect 1 less line than
+       ;; normal.  We decided to not use replacement mode here and follow Vi,
        ;; since replacement mode on n full lines can be achieved with nC.
        ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
        ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
@@ -1128,7 +1178,7 @@ as a Meta key and any number of multiple escapes is allowed."
   (let ((viper-intermediate-command 'viper-command-argument))
     (condition-case nil
        (viper-prefix-arg-com
-        last-command-char   
+        last-command-char
         (cond ((null arg) nil)
               ((consp arg) (car arg))
               ((integerp arg) arg)
@@ -1169,18 +1219,18 @@ as a Meta key and any number of multiple escapes is allowed."
        ;; del most recent elt, if identical to the second most-recent
        (viper-cleanup-ring viper-command-ring)))
   )
-    
+
 (defsubst viper-yank-last-insertion ()
   "Inserts the text saved by the previous viper-save-last-insertion command."
   (condition-case nil
       (insert viper-last-insertion)
     (error nil)))
-  
-                           
+
+
 ;; define functions to be executed
 
 ;; invoked by the `C' command
-(defun viper-exec-change (m-com com) 
+(defun viper-exec-change (m-com com)
   (or (and (markerp viper-com-point) (marker-position viper-com-point))
       (set-marker viper-com-point (point) (current-buffer)))
   ;; handle C cmd at the eol and at eob.
@@ -1197,7 +1247,7 @@ as a Meta key and any number of multiple escapes is allowed."
       (exchange-point-and-mark))
   (if (eq (preceding-char) ?\n)
       (viper-backward-char-carefully)) ; give back the newline
-  (if (= com ?c)
+  (if (viper= com ?c)
       (viper-change (mark t) (point))
     (viper-change-subr (mark t) (point))))
 
@@ -1219,87 +1269,105 @@ as a Meta key and any number of multiple escapes is allowed."
          (setq viper-use-register nil)))
     (delete-region (mark t) (point)))
   (open-line 1)
-  (if (= com ?C)
+  (if (viper= com ?C)
       (viper-change-state-to-insert)
     (viper-yank-last-insertion)))
 
 (defun viper-exec-delete (m-com com)
   (or (and (markerp viper-com-point) (marker-position viper-com-point))
       (set-marker viper-com-point (point) (current-buffer)))
-  (if viper-use-register
-      (progn
-       (cond ((viper-valid-register viper-use-register '(letter digit))
-              (copy-to-register
-               viper-use-register viper-com-point (point) nil))
-             ((viper-valid-register viper-use-register '(Letter))
-              (viper-append-to-register
-               (downcase viper-use-register) viper-com-point (point)))
-             (t (setq viper-use-register nil)
-                (error viper-InvalidRegister viper-use-register)))
-       (setq viper-use-register nil)))
-  (setq last-command
-       (if (eq last-command 'd-command) 'kill-region nil))
-  (kill-region viper-com-point (point))
-  (setq this-command 'd-command)
-  (if viper-ex-style-motion
-      (if (and (eolp) (not (bolp))) (backward-char 1))))
-
-(defun viper-exec-Delete (m-com com)
-  (save-excursion
-    (set-mark viper-com-point)
-    (viper-enlarge-region (mark t) (point))
+  (let (chars-deleted)
     (if viper-use-register
        (progn
          (cond ((viper-valid-register viper-use-register '(letter digit))
                 (copy-to-register
-                 viper-use-register (mark t) (point) nil))
+                 viper-use-register viper-com-point (point) nil))
                ((viper-valid-register viper-use-register '(Letter))
                 (viper-append-to-register
-                 (downcase viper-use-register) (mark t) (point)))
+                 (downcase viper-use-register) viper-com-point (point)))
                (t (setq viper-use-register nil)
-                  (error viper-InvalidRegister viper-use-register)))
+                (error viper-InvalidRegister viper-use-register)))
          (setq viper-use-register nil)))
     (setq last-command
-         (if (eq last-command 'D-command) 'kill-region nil))
-    (kill-region (mark t) (point))
-    (if (eq m-com 'viper-line) (setq this-command 'D-command)))
-  (back-to-indentation))
-
-(defun viper-exec-yank (m-com com)
-  (or (and (markerp viper-com-point) (marker-position viper-com-point))
-      (set-marker viper-com-point (point) (current-buffer)))
-  (if viper-use-register
-      (progn
-       (cond ((viper-valid-register viper-use-register '(letter digit))
-              (copy-to-register
-               viper-use-register viper-com-point (point) nil))
-             ((viper-valid-register viper-use-register '(Letter))
-              (viper-append-to-register
-               (downcase viper-use-register) viper-com-point (point)))
-             (t (setq viper-use-register nil)
-                (error viper-InvalidRegister viper-use-register)))
-       (setq viper-use-register nil)))
-  (setq last-command nil)
-  (copy-region-as-kill viper-com-point (point))
-  (goto-char viper-com-point))
+         (if (eq last-command 'd-command) 'kill-region nil))
+    (setq chars-deleted (abs (- (point) viper-com-point)))
+    (if (> chars-deleted viper-change-notification-threshold)
+       (message "Deleted %d characters" chars-deleted))
+    (kill-region viper-com-point (point))
+    (setq this-command 'd-command)
+    (if viper-ex-style-motion
+       (if (and (eolp) (not (bolp))) (backward-char 1)))))
 
-(defun viper-exec-Yank (m-com com)
+(defun viper-exec-Delete (m-com com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
+    (let (lines-deleted)
+      (if viper-use-register
+         (progn
+           (cond ((viper-valid-register viper-use-register '(letter digit))
+                  (copy-to-register
+                   viper-use-register (mark t) (point) nil))
+                 ((viper-valid-register viper-use-register '(Letter))
+                  (viper-append-to-register
+                   (downcase viper-use-register) (mark t) (point)))
+                 (t (setq viper-use-register nil)
+                    (error viper-InvalidRegister viper-use-register)))
+           (setq viper-use-register nil)))
+      (setq last-command
+           (if (eq last-command 'D-command) 'kill-region nil))
+      (setq lines-deleted (count-lines (point) viper-com-point))
+      (if (> lines-deleted viper-change-notification-threshold)
+         (message "Deleted %d lines" lines-deleted))
+      (kill-region (mark t) (point))
+      (if (eq m-com 'viper-line) (setq this-command 'D-command)))
+    (back-to-indentation)))
+
+;; save region
+(defun viper-exec-yank (m-com com)
+  (or (and (markerp viper-com-point) (marker-position viper-com-point))
+      (set-marker viper-com-point (point) (current-buffer)))
+  (let (chars-saved)
     (if viper-use-register
        (progn
          (cond ((viper-valid-register viper-use-register '(letter digit))
                 (copy-to-register
-                 viper-use-register (mark t) (point) nil))
+                 viper-use-register viper-com-point (point) nil))
                ((viper-valid-register viper-use-register '(Letter))
                 (viper-append-to-register
-                 (downcase viper-use-register) (mark t) (point)))
+                 (downcase viper-use-register) viper-com-point (point)))
                (t (setq viper-use-register nil)
-                  (error viper-InvalidRegister  viper-use-register)))
+                  (error viper-InvalidRegister viper-use-register)))
          (setq viper-use-register nil)))
     (setq last-command nil)
-    (copy-region-as-kill (mark t) (point)))
+    (copy-region-as-kill viper-com-point (point))
+    (setq chars-saved (abs (- (point) viper-com-point)))
+    (if (> chars-saved viper-change-notification-threshold)
+       (message "Saved %d characters" chars-saved))
+    (goto-char viper-com-point)))
+
+;; save lines
+(defun viper-exec-Yank (m-com com)
+  (save-excursion
+    (set-mark viper-com-point)
+    (viper-enlarge-region (mark t) (point))
+    (let (lines-saved)
+      (if viper-use-register
+         (progn
+           (cond ((viper-valid-register viper-use-register '(letter digit))
+                  (copy-to-register
+                   viper-use-register (mark t) (point) nil))
+                 ((viper-valid-register viper-use-register '(Letter))
+                  (viper-append-to-register
+                   (downcase viper-use-register) (mark t) (point)))
+                 (t (setq viper-use-register nil)
+                    (error viper-InvalidRegister  viper-use-register)))
+           (setq viper-use-register nil)))
+      (setq last-command nil)
+      (copy-region-as-kill (mark t) (point))
+      (setq lines-saved (count-lines (mark t) (point)))
+      (if (> lines-saved viper-change-notification-threshold)
+         (message "Saved %d lines" lines-saved))))
   (viper-deactivate-mark)
   (goto-char viper-com-point))
 
@@ -1310,9 +1378,9 @@ as a Meta key and any number of multiple escapes is allowed."
     (exchange-point-and-mark)
     (shell-command-on-region
      (mark t) (point)
-     (if (= com ?!)
+     (if (viper= com ?!)
         (setq viper-last-shell-com
-              (viper-read-string-with-history 
+              (viper-read-string-with-history
                "!"
                nil
                'viper-shell-history
@@ -1333,8 +1401,8 @@ as a Meta key and any number of multiple escapes is allowed."
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
     (if (> (mark t) (point)) (exchange-point-and-mark))
-    (indent-rigidly (mark t) (point) 
-                   (if (= com ?>)
+    (indent-rigidly (mark t) (point)
+                   (if (viper= com ?>)
                        viper-shift-width
                      (- viper-shift-width))))
   ;; return point to where it was before shift
@@ -1349,12 +1417,13 @@ as a Meta key and any number of multiple escapes is allowed."
   (setq viper-s-string (buffer-substring (point) viper-com-point))
   (setq viper-s-forward t)
   (setq viper-search-history (cons viper-s-string viper-search-history))
+  (setq viper-intermediate-command 'viper-exec-buffer-search)
   (viper-search viper-s-string viper-s-forward 1))
 
 (defvar viper-exec-array (make-vector 128 nil))
 
 ;; Using a dispatch array allows adding functions like buffer search
-;; without affecting other functions. Buffer search can now be bound
+;; without affecting other functions.  Buffer search can now be bound
 ;; to any character.
 
 (aset viper-exec-array ?c 'viper-exec-change)
@@ -1372,7 +1441,7 @@ as a Meta key and any number of multiple escapes is allowed."
 
 
 ;; This function is called by various movement commands to execute a
-;; destructive command on the region specified by the movement command. For
+;; destructive command on the region specified by the movement command.  For
 ;; instance, if the user types cw, then the command viper-forward-word will
 ;; call viper-execute-com to execute viper-exec-change, which eventually will
 ;; call viper-change to invoke the replace mode on the region.
@@ -1391,7 +1460,7 @@ as a Meta key and any number of multiple escapes is allowed."
     (if (viper-dotable-command-p com)
        (viper-set-destructive-command
         (list m-com val
-              (if (memq com (list ?c ?C ?!)) (- com) com)
+              (if (viper-memq-char com (list ?c ?C ?!)) (- com) com)
               reg nil nil)))
     ))
 
@@ -1435,22 +1504,22 @@ If the prefix argument, ARG, is non-nil, it is used instead of `val'."
      ))
   (viper-adjust-undo) ; take care of undo
   ;; If the prev cmd was rotating the command ring, this means that `.' has
-  ;; just executed a command from that ring. So, push it on the ring again.
+  ;; just executed a command from that ring.  So, push it on the ring again.
   ;; If we are just executing previous command , then don't push viper-d-com
   ;; because viper-d-com is not fully constructed in this case (its keys and
-  ;; the inserted text may be nil). Besides, in this case, the command
+  ;; the inserted text may be nil).  Besides, in this case, the command
   ;; executed by `.' is already on the ring.
   (if (eq last-command 'viper-display-current-destructive-command)
       (viper-push-onto-ring viper-d-com 'viper-command-ring))
   (viper-deactivate-mark)
   ))
-  
+
 (defun viper-repeat-from-history ()
   "Repeat a destructive command from history.
 Doesn't change viper-command-ring in any way, so `.' will work as before
 executing this command.
 This command is supposed to be bound to a two-character Vi macro where
-the second character is a digit 0 to 9. The digit indicates which
+the second character is a digit 0 to 9.  The digit indicates which
 history command to execute. `<char>0' is equivalent to `.', `<char>1'
 invokes the command before that, etc."
   (interactive)
@@ -1476,24 +1545,25 @@ invokes the command before that, etc."
       (viper-special-ring-rotate1 viper-command-ring 1)
       (setq num (1+ num)))
     ))
-      
 
-;; The hash-command. It is invoked interactively by the key sequence #<char>.
+
+;; The hash-command.  It is invoked interactively by the key sequence #<char>.
 ;; The chars that can follow `#' are determined by viper-hash-command-p
 (defun viper-special-prefix-com (char)
-  (cond ((= char ?c)
+  (cond ((viper= char ?c)
         (downcase-region (min viper-com-point (point))
                          (max viper-com-point (point))))
-       ((= char ?C)
+       ((viper= char ?C)
         (upcase-region (min viper-com-point (point))
                        (max viper-com-point (point))))
-       ((= char ?g)
+       ((viper= char ?g)
         (push-mark viper-com-point t)
         (viper-global-execute))
-       ((= char ?q)
+       ((viper= char ?q)
         (push-mark viper-com-point t)
         (viper-quote-region))
-       ((= char ?s) (funcall viper-spell-function viper-com-point (point)))
+       ((viper= char ?s)
+        (funcall viper-spell-function viper-com-point (point)))
        (t (error "#%c: %s" char viper-InvalidViCommand))))
 
 \f
@@ -1507,24 +1577,24 @@ invokes the command before that, etc."
         (before-undo-pt (point-marker))
        (after-change-functions after-change-functions)
        undo-beg-posn undo-end-posn)
-       
+
     ;; no need to remove this hook, since this var has scope inside a let.
     (add-hook 'after-change-functions
              '(lambda (beg end len)
                 (setq undo-beg-posn beg
                       undo-end-posn (or end beg))))
-  
+
     (undo-start)
     (undo-more 2)
     (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
          undo-end-posn (or undo-end-posn undo-beg-posn))
-    
+
     (goto-char undo-beg-posn)
     (sit-for 0)
     (if (and viper-keep-point-on-undo
             (pos-visible-in-window-p before-undo-pt))
        (progn
-         (push-mark (point-marker) t) 
+         (push-mark (point-marker) t)
          (viper-sit-for-short 300)
          (goto-char undo-end-posn)
          (viper-sit-for-short 300)
@@ -1549,7 +1619,7 @@ invokes the command before that, etc."
 
 ;; The following two functions are used to set up undo properly.
 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
-;; they are undone all at once.  
+;; they are undone all at once.
 (defun viper-adjust-undo ()
   (if viper-undo-needs-adjustment
       (let ((inhibit-quit t)
@@ -1559,11 +1629,11 @@ invokes the command before that, etc."
            (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
                (progn
                  (setq tmp2 (cdr tmp)) ; the part after mark
-                 
+
                  ;; cut tail from buffer-undo-list temporarily by direct
                  ;; manipulation with pointers in buffer-undo-list
                  (setcdr tmp nil)
-                 
+
                  (setq buffer-undo-list (delq nil buffer-undo-list))
                  (setq buffer-undo-list
                        (delq viper-buffer-undo-list-mark buffer-undo-list))
@@ -1571,31 +1641,31 @@ invokes the command before that, etc."
                  (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
              (setq buffer-undo-list (delq nil buffer-undo-list)))))
     ))
-  
 
-(defun viper-set-complex-command-for-undo ()  
+
+(defun viper-set-complex-command-for-undo ()
   (if (listp buffer-undo-list)
       (if (not viper-undo-needs-adjustment)
          (let ((inhibit-quit t))
-           (setq buffer-undo-list 
+           (setq buffer-undo-list
                  (cons viper-buffer-undo-list-mark buffer-undo-list))
            (setq viper-undo-needs-adjustment t)))))
 
 
 
-      
+
 (defun viper-display-current-destructive-command ()
   (let ((text (nth 4 viper-d-com))
        (keys (nth 5 viper-d-com))
        (max-text-len 30))
-    
+
     (setq this-command 'viper-display-current-destructive-command)
-       
+
     (message " `.' runs  %s%s"
             (concat "`" (viper-array-to-string keys) "'")
-            (viper-abbreviate-string 
+            (viper-abbreviate-string
              (if viper-xemacs-p
-                 (replace-in-string 
+                 (replace-in-string
                   (cond ((characterp text) (char-to-string text))
                         ((stringp text) text)
                         (t ""))
@@ -1604,8 +1674,8 @@ invokes the command before that, etc."
              max-text-len
              "  inserting  `" "'" "    ......."))
     ))
-    
-    
+
+
 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
 ;; or in some other way (non-interactively).
 (defun viper-set-destructive-command (list)
@@ -1618,7 +1688,8 @@ invokes the command before that, etc."
                                         (this-command-keys))))
        (viper-push-onto-ring viper-d-com 'viper-command-ring)))
   (setq viper-this-command-keys nil))
-    
+
+
 (defun viper-prev-destructive-command (next)
   "Find previous destructive command in the history of destructive commands.
 With prefix argument, find next destructive command."
@@ -1629,7 +1700,7 @@ With prefix argument, find next destructive command."
        (setq viper-intermediate-command
              'repeating-display-destructive-command)
       ;; first search through command history--set temp ring
-      (setq viper-temp-command-ring (copy-list viper-command-ring))) 
+      (setq viper-temp-command-ring (copy-list viper-command-ring)))
     (setq cmd (if next
                  (viper-special-ring-rotate1 viper-temp-command-ring 1)
                (viper-special-ring-rotate1 viper-temp-command-ring -1)))
@@ -1637,12 +1708,14 @@ With prefix argument, find next destructive command."
        ()
       (setq viper-d-com cmd))
     (viper-display-current-destructive-command)))
-      
+
+
 (defun viper-next-destructive-command ()
   "Find next destructive command in the history of destructive commands."
   (interactive)
   (viper-prev-destructive-command 'next))
-  
+
+
 (defun viper-insert-prev-from-insertion-ring (arg)
   "Cycle through insertion ring in the direction of older insertions.
 Undoes previous insertion and inserts new.
@@ -1667,7 +1740,7 @@ to in the global map, instead of cycling through the insertion ring."
     (setq buffer-undo-list (cons nil buffer-undo-list))
     (setq viper-last-inserted-string-from-insertion-ring
          (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
-    
+
     ;; this change of viper-intermediate-command must come after
     ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
     ;; insertion.
@@ -1681,7 +1754,8 @@ to in the global map, instead of cycling through the insertion ring."
 Undo previous insertion and inserts new."
   (interactive)
   (viper-insert-prev-from-insertion-ring 'next))
-    
+
+
 \f
 ;; some region utilities
 
@@ -1706,7 +1780,7 @@ Undo previous insertion and inserts new."
 (defun viper-enlarge-region (beg end)
   (or beg (setq beg end)) ; if beg is nil, set to end
   (or end (setq end beg)) ; if end is nil, set to beg
-  
+
   (if (< beg end)
       (progn (goto-char beg) (set-mark end))
     (goto-char end)
@@ -1720,21 +1794,32 @@ Undo previous insertion and inserts new."
 
 ;; Quote region by each line with a user supplied string.
 (defun viper-quote-region ()
-  (setq viper-quote-string
-       (viper-read-string-with-history
-        "Quote string: "
-        nil
-        'viper-quote-region-history
-        viper-quote-string))
-  (viper-enlarge-region (point) (mark t))
-  (if (> (point) (mark t)) (exchange-point-and-mark))
-  (insert viper-quote-string)
-  (beginning-of-line)
-  (forward-line 1)
-  (while (and (< (point) (mark t)) (bolp))
-    (insert viper-quote-string)
+  (let ((quote-str viper-quote-string)
+       (donot-change-dafault t))
+    (setq quote-str
+         (viper-read-string-with-history
+          "Quote string: "
+          nil
+          'viper-quote-region-history
+          (cond ((string-match "tex.*-mode" (symbol-name major-mode)) "%%")
+                ((string-match "java.*-mode" (symbol-name major-mode)) "//")
+                ((string-match "perl.*-mode" (symbol-name major-mode)) "#")
+                ((string-match "lisp.*-mode" (symbol-name major-mode)) ";;")
+                ((memq major-mode '(c-mode cc-mode c++-mode)) "//")
+                ((memq major-mode '(sh-mode shell-mode)) "#")
+                (t (setq donot-change-dafault nil)
+                   quote-str))))
+    (or donot-change-dafault
+       (setq viper-quote-string quote-str))
+    (viper-enlarge-region (point) (mark t))
+    (if (> (point) (mark t)) (exchange-point-and-mark))
+    (insert quote-str)
     (beginning-of-line)
-    (forward-line 1)))
+    (forward-line 1)
+    (while (and (< (point) (mark t)) (bolp))
+      (insert quote-str)
+      (beginning-of-line)
+      (forward-line 1))))
 
 ;;  Tells whether BEG is on the same line as END.
 ;;  If one of the args is nil, it'll return nil.
@@ -1756,8 +1841,8 @@ Undo previous insertion and inserts new."
                    (setq incr 1))
                (<= (+ incr (count-lines beg end)) 1))))
      ))
-        
-        
+
+
 ;; Check if the string ends with a newline.
 (defun viper-end-with-a-newline-p (string)
   (or (string= string "")
@@ -1769,23 +1854,48 @@ Undo previous insertion and inserts new."
       (insert msg)
       (sit-for 2)
       (goto-char savemax) (delete-region (point) (point-max))
-      ))  
-      
+      ))
+
 
 \f
 ;;; Minibuffer business
-           
+
 (defsubst viper-set-minibuffer-style ()
   (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel))
-  
-  
+
+
 (defun viper-minibuffer-setup-sentinel ()
   (let ((hook (if viper-vi-style-in-minibuffer
                  'viper-change-state-to-insert
                'viper-change-state-to-emacs)))
     (funcall hook)
     ))
-  
+
+;; Thie is a temp hook that uses free variables init-message and initial.
+;; A dirty feature, but it is the simplest way to have it do the right thing.
+;; The INIT-MESSAGE and INITIAL vars come from the scope set by 
+;; viper-read-string-with-history
+(defun viper-minibuffer-standard-hook ()
+  (if (stringp init-message)
+      (viper-tmp-insert-at-eob init-message))
+  (if (stringp initial)
+      (progn
+       ;; don't wait if we have unread events or in kbd macro
+       (or unread-command-events
+           executing-kbd-macro
+           (sit-for 840))
+       (if (fboundp 'minibuffer-prompt-end)
+           (delete-region (minibuffer-prompt-end) (point-max))
+         (erase-buffer))
+       (insert initial)))
+  (viper-minibuffer-setup-sentinel))
+
+(defsubst viper-minibuffer-real-start ()
+  (if (fboundp 'minibuffer-prompt-end)
+      (minibuffer-prompt-end)
+    (point-min)))
+
+
 ;; Interpret last event in the local map first; if fails, use exit-minibuffer.
 ;; Run viper-minibuffer-exit-hook before exiting.
 (defun viper-exit-minibuffer ()
@@ -1797,25 +1907,26 @@ Undo previous insertion and inserts new."
     (if command
        (command-execute command)
       (exit-minibuffer))))
-  
+
 
 (defcustom viper-smart-suffix-list
-  '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "P" "p")
+  '("" "tex" "c" "cc" "C" "java" "el" "html" "htm" "xml"
+    "pl" "flr" "P" "p" "h" "H")
   "*List of suffixes that Viper 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
-represents an editable file. However, file completion will stop at the `.'
+prefix and many different suffixes.  Usually, only one of the suffixes
+represents an editable file.  However, file completion will stop at the `.'
 The smart suffix feature lets you hit RET in such a case, and Viper will
 select the appropriate suffix.
 
 Suffixes are tried in the order given and the first suffix for which a
-corresponding file exists is selected. If no file exists for any of the
+corresponding file exists is selected.  If no file exists for any of the
 suffixes, the user is asked to confirm.
 
 To turn this feature off, set this variable to nil."
   :type '(repeat string)
-  :group 'viper)
-    
+  :group 'viper-misc)
+
 
 ;; Try to add a suitable suffix to files whose name ends with a `.'
 ;; Useful when the user hits RET on a non-completed file name.
@@ -1823,7 +1934,8 @@ To turn this feature off, set this variable to nil."
 (defun viper-file-add-suffix ()
   (let ((count 0)
        (len (length viper-smart-suffix-list))
-       (file (buffer-string))
+       (file (buffer-substring-no-properties
+              (viper-minibuffer-real-start) (point-max)))
        found key cmd suff)
     (goto-char (point-max))
     (if (and viper-smart-suffix-list (string-match "\\.$" file))
@@ -1836,11 +1948,11 @@ To turn this feature off, set this variable to nil."
                (progn
                  (setq found t)
                  (insert suff))))
-         
+
          (if found
              ()
            (viper-tmp-insert-at-eob " [Please complete file name]")
-           (unwind-protect 
+           (unwind-protect
                (while (not (memq cmd
                                  '(exit-minibuffer viper-exit-minibuffer)))
                  (setq cmd
@@ -1862,44 +1974,37 @@ Remove this function from `viper-minibuffer-exit-hook', if this causes
 problems."
   (if (viper-is-in-minibuffer)
       (progn
-       (goto-char (point-min))
+       (goto-char (viper-minibuffer-real-start))
        (end-of-line)
        (delete-region (point) (point-max)))))
 
 \f
-;;; Reading string with history  
-    
-(defun viper-read-string-with-history (prompt &optional initial 
-                                           history-var default keymap)
+;;; Reading string with history
+
+(defun viper-read-string-with-history (prompt &optional initial
+                                             history-var default keymap
+                                             init-message)
   ;; Read string, prompting with PROMPT and inserting the INITIAL
-  ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
-  ;; input is an empty string. Use KEYMAP, if given, or the
-  ;; minibuffer-local-map.
+  ;; value.  Uses HISTORY-VAR.  DEFAULT is the default value to accept if the
+  ;; input is an empty string.
   ;; Default value is displayed until the user types something in the
-  ;; minibuffer. 
-  (let ((minibuffer-setup-hook 
-        '(lambda ()
-           (if (stringp initial)
-               (progn
-                 ;; don't wait if we have unread events or in kbd macro
-                 (or unread-command-events
-                     executing-kbd-macro
-                     (sit-for 840))
-                 (erase-buffer)
-                 (insert initial)))
-           (viper-minibuffer-setup-sentinel)))
+  ;; minibuffer.
+  ;; KEYMAP is used, if given, instead of minibuffer-local-map.
+  ;; INIT-MESSAGE is the message temporarily displayed after entering the
+  ;; minibuffer.
+  (let ((minibuffer-setup-hook 'viper-minibuffer-standard-hook)
        (val "")
        (padding "")
        temp-msg)
-    
+
     (setq keymap (or keymap minibuffer-local-map)
          initial (or initial "")
          temp-msg (if default
                       (format "(default: %s) " default)
                     ""))
-                  
+
     (setq viper-incomplete-ex-cmd nil)
-    (setq val (read-from-minibuffer prompt 
+    (setq val (read-from-minibuffer prompt
                                    (concat temp-msg initial val padding)
                                    keymap nil history-var))
     (setq minibuffer-setup-hook nil
@@ -1912,8 +2017,8 @@ problems."
            (string= (nth 0 (eval history-var)) ""))
        (set history-var (cdr (eval history-var))))
     ;; If the user enters nothing but the prev cmd wasn't viper-ex,
-    ;; viper-command-argument, or `! shell-command', this probably means 
-    ;; that the user typed something then erased. Return "" in this case, not
+    ;; viper-command-argument, or `! shell-command', this probably means
+    ;; that the user typed something then erased.  Return "" in this case, not
     ;; the default---the default is too confusing in this case.
     (cond ((and (string= val "")
                (not (string= prompt "!")) ; was a `! shell-command'
@@ -1926,7 +2031,7 @@ problems."
          ((string= val "") (or default ""))
          (t val))
     ))
-  
+
 
 \f
 ;; insertion commands
@@ -1939,7 +2044,7 @@ problems."
        (val   (nth 1 viper-d-com))
        (char  (nth 2 viper-d-com)))
     (if (and val (> val 1)) ; first check that val is non-nil
-       (progn        
+       (progn
          (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
          (viper-repeat nil)
          (setq viper-d-com (list i-com val char nil nil nil))
@@ -1964,7 +2069,7 @@ problems."
        (com (viper-getcom arg)))
     (viper-set-destructive-command (list 'viper-append val ?r nil nil nil))
     (if (not (eolp)) (forward-char))
-    (if (equal com ?r)
+    (if (viper= com ?r)
        (viper-loop val (viper-yank-last-insertion))
       (viper-change-state-to-insert))))
 
@@ -1976,7 +2081,7 @@ problems."
        (com (viper-getcom arg)))
     (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
     (end-of-line)
-    (if (equal com ?r)
+    (if (viper= com ?r)
        (viper-loop val (viper-yank-last-insertion))
       (viper-change-state-to-insert))))
 
@@ -1988,7 +2093,7 @@ problems."
        (com (viper-getcom arg)))
     (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
     (back-to-indentation)
-    (if (equal com ?r)
+    (if (viper= com ?r)
        (viper-loop val (viper-yank-last-insertion))
       (viper-change-state-to-insert))))
 
@@ -2000,11 +2105,11 @@ problems."
        (com (viper-getcom arg)))
     (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
     (let ((col (current-indentation)))
-      (if (equal com ?r)
+      (if (viper= com ?r)
          (viper-loop val
                      (end-of-line)
                      (newline 1)
-                     (if viper-auto-indent 
+                     (if viper-auto-indent
                          (progn
                            (setq viper-cted t)
                            (if viper-electric-mode
@@ -2030,11 +2135,11 @@ problems."
        (com (viper-getcom arg)))
     (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
     (let ((col (current-indentation)))
-      (if (equal com ?r)
+      (if (viper= com ?r)
          (viper-loop val
                      (beginning-of-line)
                      (open-line 1)
-                     (if viper-auto-indent 
+                     (if viper-auto-indent
                          (progn
                            (setq viper-cted t)
                            (if viper-electric-mode
@@ -2061,7 +2166,7 @@ problems."
        (com (viper-getcom arg)))
     (viper-set-destructive-command
      (list 'viper-open-line-at-point val ?r nil nil nil))
-    (if (equal com ?r)
+    (if (viper= com ?r)
        (viper-loop val
                    (open-line 1)
                    (viper-yank-last-insertion))
@@ -2075,7 +2180,7 @@ problems."
        (com (viper-getcom arg)))
     (push-mark nil t)
     (forward-char val)
-    (if (equal com ?r)
+    (if (viper= com ?r)
        (viper-change-subr (mark t) (point))
       (viper-change (mark t) (point)))
     (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
@@ -2093,44 +2198,46 @@ problems."
   (setq viper-began-as-replace t
        viper-sitting-in-replace t
        viper-replace-chars-to-delete 0)
-  (viper-add-hook
-   'viper-after-change-functions 'viper-replace-mode-spy-after t)
-  (viper-add-hook
-   'viper-before-change-functions 'viper-replace-mode-spy-before t)
+  (add-hook
+   'viper-after-change-functions 'viper-replace-mode-spy-after t 'local)
+  (add-hook
+   'viper-before-change-functions 'viper-replace-mode-spy-before t 'local)
   ;; this will get added repeatedly, but no harm
   (add-hook 'after-change-functions 'viper-after-change-sentinel t)
   (add-hook 'before-change-functions 'viper-before-change-sentinel t)
-  (viper-move-marker-locally 'viper-last-posn-in-replace-region
-                            (viper-replace-start))
-  (viper-add-hook
-   'viper-post-command-hooks 'viper-replace-state-post-command-sentinel t)
-  (viper-add-hook
-   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t)
+  (viper-move-marker-locally
+   'viper-last-posn-in-replace-region (viper-replace-start))
+  (add-hook
+   'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
+   t 'local)
+  (add-hook
+   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
   ;; guard against a smartie who switched from R-replace to normal replace
-  (viper-remove-hook
-   'viper-post-command-hooks 'viper-R-state-post-command-sentinel)
-  (if overwrite-mode (overwrite-mode nil))
+  (remove-hook
+   'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
+  (if overwrite-mode (overwrite-mode -1))
   )
-  
+
 
 (defun viper-replace-mode-spy-before (beg end)
   (setq viper-replace-region-chars-deleted (viper-chars-in-region beg end))
   )
 
 ;; Invoked as an after-change-function to calculate how many chars have to be
-;; deleted. This function may be called several times within a single command,
-;; if this command performs several separate buffer changes. Therefore, if adds
-;; up the number of chars inserted and subtracts the number of chars deleted.
+;; deleted.  This function may be called several times within a single command,
+;; if this command performs several separate buffer changes.  Therefore, if
+;; adds up the number of chars inserted and subtracts the number of chars
+;; deleted.
 (defun viper-replace-mode-spy-after (beg end length)
-  (if (memq viper-intermediate-command 
-           '(dabbrev-expand repeating-insertion-from-ring))
+  (if (memq viper-intermediate-command
+           '(dabbrev-expand hippie-expand repeating-insertion-from-ring))
       ;; Take special care of text insertion from insertion ring inside
       ;; replacement overlays.
       (progn
        (setq viper-replace-chars-to-delete 0)
-       (viper-move-marker-locally 
+       (viper-move-marker-locally
         'viper-last-posn-in-replace-region (point)))
-    
+
     (let* ((real-end (min end (viper-replace-end)))
           (column-shift (- (save-excursion (goto-char real-end)
                                            (current-column))
@@ -2143,16 +2250,17 @@ problems."
       (setq viper-replace-region-chars-deleted 0)
       (setq viper-replace-chars-to-delete
            (+ viper-replace-chars-to-delete
-              (- 
+              (-
                ;; if column shift is bigger, due to a TAB insertion, take
                ;; column-shift instead of the number of inserted chars
                (max (viper-chars-in-region beg real-end)
                     ;; This test accounts for Chinese/Japanese/...  chars,
-                    ;; which occupy 2 columns instead of one. If we use
+                    ;; which occupy 2 columns instead of one.  If we use
                     ;; column-shift here, we may delete two chars instead of
-                    ;; one when the user types one Chinese character. Deleting
-                    ;; two would be OK, if they were European chars, but it is
-                    ;; not OK if they are Chinese chars. Since it is hard to
+                    ;; one when the user types one Chinese character.
+                    ;; Deleting two would be OK, if they were European chars,
+                    ;; but it is not OK if they are Chinese chars.
+                    ;; Since it is hard to
                     ;; figure out which characters are being deleted in any
                     ;; given region, we decided to treat Eastern and European
                     ;; characters equally, even though Eastern chars may
@@ -2164,28 +2272,14 @@ problems."
                ;; the number of deleted chars
                chars-deleted)))
 
-      (viper-move-marker-locally 
+      (viper-move-marker-locally
        'viper-last-posn-in-replace-region
        (max (if (> end (viper-replace-end)) (viper-replace-end) end)
            (or (marker-position viper-last-posn-in-replace-region)
-               (viper-replace-start)) 
+               (viper-replace-start))
            ))
-      
-      )))
 
-;; Make sure we don't delete more than needed.
-;; This is executed at viper-last-posn-in-replace-region
-(defsubst viper-trim-replace-chars-to-delete-if-necessary ()
-  (setq viper-replace-chars-to-delete
-       (max 0
-            (min viper-replace-chars-to-delete
-                 ;; Don't delete more than to the end of repl overlay
-                 (viper-chars-in-region
-                  (viper-replace-end) viper-last-posn-in-replace-region)
-                 ;; point is viper-last-posn-in-replace-region now
-                 ;; So, this limits deletion to the end of line
-                 (viper-chars-in-region (point) (viper-line-pos 'end))
-                 ))))
+      )))
 
 
 ;; Delete stuff between viper-last-posn-in-replace-region and the end of
@@ -2193,25 +2287,25 @@ problems."
 ;; the overlay and current point is before the end of the overlay.
 ;; Don't delete anything if current point is past the end of the overlay.
 (defun viper-finish-change ()
-  (viper-remove-hook
-   'viper-after-change-functions 'viper-replace-mode-spy-after)
-  (viper-remove-hook
-   'viper-before-change-functions 'viper-replace-mode-spy-before)
-  (viper-remove-hook
-   'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
-  (viper-remove-hook
-   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel
-  (viper-restore-cursor-color-after-replace)
+  (remove-hook
+   'viper-after-change-functions 'viper-replace-mode-spy-after 'local)
+  (remove-hook
+   'viper-before-change-functions 'viper-replace-mode-spy-before 'local)
+  (remove-hook
+   'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
+  (remove-hook
+   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
+  (viper-restore-cursor-color 'after-replace-mode)
   (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
   (save-excursion
     (if (and viper-replace-overlay
             (viper-pos-within-region viper-last-posn-in-replace-region
                                      (viper-replace-start)
-                                     (viper-replace-end)) 
+                                     (viper-replace-end))
             (< (point) (viper-replace-end)))
        (delete-region
         viper-last-posn-in-replace-region (viper-replace-end))))
-  
+
   (if (eq viper-current-state 'replace-state)
       (viper-downgrade-to-insert))
   ;; replace mode ended => nullify viper-last-posn-in-replace-region
@@ -2229,26 +2323,26 @@ problems."
   (setq kill-ring-yank-pointer kill-ring))
 
 (defun viper-finish-R-mode ()
-  (viper-remove-hook
-   'viper-post-command-hooks 'viper-R-state-post-command-sentinel)
-  (viper-remove-hook
-   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel)
+  (remove-hook
+   'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
+  (remove-hook
+   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
   (viper-downgrade-to-insert))
-  
+
 (defun viper-start-R-mode ()
   ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
   (overwrite-mode 1)
-  (viper-add-hook
-   'viper-post-command-hooks 'viper-R-state-post-command-sentinel t)
-  (viper-add-hook
-   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t)
+  (add-hook
+   'viper-post-command-hooks 'viper-R-state-post-command-sentinel t 'local)
+  (add-hook
+   'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
   ;; guard against a smartie who switched from R-replace to normal replace
-  (viper-remove-hook
-   'viper-post-command-hooks 'viper-replace-state-post-command-sentinel)
+  (remove-hook
+   'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
   )
 
 
-  
+
 (defun viper-replace-state-exit-cmd ()
   "Binding for keys that cause Replace state to switch to Vi or to Insert.
 These keys are ESC, RET, and LineFeed"
@@ -2261,7 +2355,7 @@ These keys are ESC, RET, and LineFeed"
        (setq com 'viper-exit-insert-state)
       (viper-set-unread-command-events last-input-char)
       (setq com (key-binding (read-key-sequence nil))))
-      
+
     (condition-case conds
        (command-execute com)
       (error
@@ -2275,7 +2369,7 @@ These keys are ESC, RET, and LineFeed"
   (interactive)
   ;; If Emacs start supporting overlay maps, as it currently supports
   ;; text-property maps, we could do away with viper-replace-minor-mode and
-  ;; just have keymap attached to replace overlay. Then the "if part" of this
+  ;; just have keymap attached to replace overlay.  Then the "if part" of this
   ;; statement can be deleted.
   (if (or (< (point) (viper-replace-start))
          (> (point) (viper-replace-end)))
@@ -2300,20 +2394,20 @@ These keys are ESC, RET, and LineFeed"
        (back-to-indentation)
        ))))
 
-  
+
 ;; This is the function bound to 'R'---unlimited replace.
 ;; Similar to Emacs's own overwrite-mode.
-(defun viper-overwrite (arg) 
+(defun viper-overwrite (arg)
   "Begin overwrite mode."
   (interactive "P")
   (let ((val (viper-p-val arg))
        (com (viper-getcom arg)) (len))
     (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
     (if com
-       (progn 
+       (progn
          ;; Viper saves inserted text in viper-last-insertion
          (setq len (length viper-last-insertion))
-         (delete-char len)     
+         (delete-char len)
          (viper-loop val (viper-yank-last-insertion)))
       (setq last-command 'viper-overwrite)
       (viper-set-complex-command-for-undo)
@@ -2330,8 +2424,8 @@ These keys are ESC, RET, and LineFeed"
     (viper-move-marker-locally 'viper-com-point (point))
     (if (not (eobp))
        (viper-next-line-carefully (1- val)))
-    ;; this ensures that dd, cc, D, yy will do the right thing on the last
-    ;; line of buffer when this line has no \n.
+    ;; the following ensures that dd, cc, D, yy will do the right thing on the
+    ;; last line of buffer when this line has no \n.
     (viper-add-newline-at-eob-if-necessary)
     (viper-execute-com 'viper-line val com))
   (if (and (eobp) (not (bobp))) (forward-line -1))
@@ -2380,11 +2474,12 @@ These keys are ESC, RET, and LineFeed"
 
 (defun viper-replace-char-subr (com arg)
   (let (char)
-    (setq char (if (equal com ?r)
+    (setq char (if (viper= com ?r)
                   viper-d-char
                 (read-char)))
     (let (inhibit-quit) ; preserve consistency of undo-list and iso-accents
-      (if (and  viper-automatic-iso-accents (memq char '(?' ?\" ?^ ?~)))
+      (if (and  viper-automatic-iso-accents
+               (viper-memq-char char '(?' ?\" ?^ ?~)))
          ;; get European characters
          (progn
            (viper-set-iso-accents-mode t)
@@ -2397,7 +2492,7 @@ These keys are ESC, RET, and LineFeed"
          ;; get Intl. characters
          (progn
            (viper-set-input-method t)
-           (setq last-command-event 
+           (setq last-command-event
                  (viper-copy-event
                   (if viper-xemacs-p (character-to-event char) char)))
            (delete-char 1 t)
@@ -2428,7 +2523,7 @@ These keys are ESC, RET, and LineFeed"
            (viper-set-input-method nil))
        (delete-char arg t)
        (setq viper-d-char char)
-       (viper-loop (if (> arg 0) arg (- arg)) 
+       (viper-loop (if (> arg 0) arg (- arg))
                    (insert char)))
       (viper-adjust-undo)
       (backward-char arg))))
@@ -2458,8 +2553,9 @@ On reaching end of line, stop and signal error."
       (forward-char val)
       (if com (viper-execute-com 'viper-forward-char val com)))))
 
+
 (defun viper-backward-char (arg)
-  "Move point left ARG characters (right if ARG negative). 
+  "Move point left ARG characters (right if ARG negative).
 On reaching beginning of line, stop and signal error."
   (interactive "P")
   (viper-leave-region-active)
@@ -2472,11 +2568,12 @@ On reaching beginning of line, stop and signal error."
          (if com (viper-execute-com 'viper-backward-char val com)))
       (backward-char val)
       (if com (viper-execute-com 'viper-backward-char val com)))))
-      
+
+
 ;; Like forward-char, but doesn't move at end of buffer.
-;; Returns distance traveled 
+;; Returns distance traveled
 ;; (positive or 0, if arg positive; negative if arg negative).
-(defun viper-forward-char-carefully (&optional arg)      
+(defun viper-forward-char-carefully (&optional arg)
   (setq arg (or arg 1))
   (let ((pt (point)))
     (condition-case nil
@@ -2485,11 +2582,12 @@ On reaching beginning of line, stop and signal error."
     (if (< (point) pt) ; arg was negative
        (- (viper-chars-in-region pt (point)))
       (viper-chars-in-region pt (point)))))
-      
+
+
 ;; Like backward-char, but doesn't move at beg of buffer.
 ;; Returns distance traveled
 ;; (negative or 0, if arg positive; positive if arg negative).
-(defun viper-backward-char-carefully (&optional arg)      
+(defun viper-backward-char-carefully (&optional arg)
   (setq arg (or arg 1))
   (let ((pt (point)))
     (condition-case nil
@@ -2509,9 +2607,9 @@ On reaching beginning of line, stop and signal error."
 ;;; Word command
 
 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
-;; word movement. When executed with a destructive command, \n is usually left
+;; word movement.  When executed with a destructive command, \n is usually left
 ;; untouched for the last word.  Viper uses syntax table to determine what is a
-;; word and what is a separator. However, \n is always a separator. Also, if
+;; word and what is a separator.  However, \n is always a separator.  Also, if
 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
 
 ;; skip only one \n
@@ -2523,12 +2621,18 @@ On reaching beginning of line, stop and signal error."
            (progn
              (forward-char)
              (viper-skip-all-separators-forward  'within-line))))
+    ;; check for eob and white space before it.  move off of eob
+    (if (and (eobp) (save-excursion
+                     (viper-backward-char-carefully)
+                     (viper-looking-at-separator)))
+       (viper-backward-char-carefully))
     (viper-skip-all-separators-backward 'within-line)
     (viper-backward-char-carefully)
     (if (looking-at "\n")
        (viper-skip-all-separators-backward 'within-line)
-      (forward-char))))
-      
+      (or (bobp) (forward-char)))))
+
+
 (defun viper-forward-word-kernel (val)
   (while (> val 0)
     (cond ((viper-looking-at-alpha)
@@ -2541,18 +2645,18 @@ On reaching beginning of line, stop and signal error."
           (viper-skip-separators t)))
     (setq val (1- val))))
 
-;; first skip non-newline separators backward, then skip \n. Then, if TWICE is
+;; first skip non-newline separators backward, then skip \n.  Then, if TWICE is
 ;; non-nil, skip non-\n back again, but don't overshoot the limit LIM.
 (defun viper-separator-skipback-special (twice lim)
   (let ((prev-char (viper-char-at-pos 'backward))
        (saved-point (point)))
     ;; skip non-newline separators backward
-    (while (and (not (memq prev-char '(nil \n)))
+    (while (and (not (viper-memq-char prev-char '(nil \n)))
                (< lim (point))
                ;; must be non-newline separator
                (if (eq viper-syntax-preference 'strict-vi)
-                   (memq prev-char '(?\  ?\t))
-                 (memq (char-syntax prev-char) '(?\  ?-))))
+                   (viper-memq-char prev-char '(?\  ?\t))
+                 (viper-memq-char (char-syntax prev-char) '(?\  ?-))))
       (viper-backward-char-carefully)
       (setq prev-char (viper-char-at-pos 'backward)))
 
@@ -2566,12 +2670,12 @@ On reaching beginning of line, stop and signal error."
 
     ;; skip again, but make sure we don't overshoot the limit
     (if twice
-       (while (and (not (memq prev-char '(nil \n)))
+       (while (and (not (viper-memq-char prev-char '(nil \n)))
                    (< lim (point))
                    ;; must be non-newline separator
                    (if (eq viper-syntax-preference 'strict-vi)
-                       (memq prev-char '(?\  ?\t))
-                     (memq (char-syntax prev-char) '(?\  ?-))))
+                       (viper-memq-char prev-char '(?\  ?\t))
+                     (viper-memq-char (char-syntax prev-char) '(?\  ?-))))
          (viper-backward-char-carefully)
          (setq prev-char (viper-char-at-pos 'backward))))
 
@@ -2579,7 +2683,7 @@ On reaching beginning of line, stop and signal error."
        (viper-forward-char-carefully))
     ))
 
-         
+
 (defun viper-forward-word (arg)
   "Forward word."
   (interactive "P")
@@ -2589,15 +2693,15 @@ On reaching beginning of line, stop and signal error."
     (if com (viper-move-marker-locally 'viper-com-point (point)))
     (viper-forward-word-kernel val)
     (if com (progn
-             (cond ((memq com (list ?c (- ?c)))
+             (cond ((viper-memq-char com (list ?c (- ?c)))
                     (viper-separator-skipback-special 'twice viper-com-point))
                    ;; Yank words including the whitespace, but not newline
-                   ((memq com (list ?y (- ?y)))
+                   ((viper-memq-char com (list ?y (- ?y)))
                     (viper-separator-skipback-special nil viper-com-point))
                    ((viper-dotable-command-p com)
                     (viper-separator-skipback-special nil viper-com-point)))
              (viper-execute-com 'viper-forward-word val com)))))
-         
+
 
 (defun viper-forward-Word (arg)
   "Forward word delimited by white characters."
@@ -2610,29 +2714,29 @@ On reaching beginning of line, stop and signal error."
                (viper-skip-nonseparators 'forward)
                (viper-skip-separators t))
     (if com (progn
-             (cond ((memq com (list ?c (- ?c)))
+             (cond ((viper-memq-char com (list ?c (- ?c)))
                     (viper-separator-skipback-special 'twice viper-com-point))
                    ;; Yank words including the whitespace, but not newline
-                   ((memq com (list ?y (- ?y)))
+                   ((viper-memq-char com (list ?y (- ?y)))
                     (viper-separator-skipback-special nil viper-com-point))
                    ((viper-dotable-command-p com)
                     (viper-separator-skipback-special nil viper-com-point)))
              (viper-execute-com 'viper-forward-Word val com)))))
 
 
-;; this is a bit different from Vi, but Vi's end of word 
+;; this is a bit different from Vi, but Vi's end of word
 ;; makes no sense whatsoever
 (defun viper-end-of-word-kernel ()
   (if (viper-end-of-word-p) (forward-char))
   (if (viper-looking-at-separator)
       (viper-skip-all-separators-forward))
-  
+
   (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
        ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
   (viper-backward-char-carefully))
 
 (defun viper-end-of-word-p ()
-  (or (eobp) 
+  (or (eobp)
       (save-excursion
        (cond ((viper-looking-at-alpha)
               (forward-char)
@@ -2650,7 +2754,7 @@ On reaching beginning of line, stop and signal error."
        (com (viper-getcom arg)))
     (if com (viper-move-marker-locally 'viper-com-point (point)))
     (viper-loop val (viper-end-of-word-kernel))
-    (if com 
+    (if com
        (progn
          (forward-char)
          (viper-execute-com 'viper-end-of-word val com)))))
@@ -2666,7 +2770,7 @@ On reaching beginning of line, stop and signal error."
                (viper-end-of-word-kernel)
                (viper-skip-nonseparators 'forward)
                (backward-char))
-    (if com 
+    (if com
        (progn
          (forward-char)
          (viper-execute-com 'viper-end-of-Word val com)))))
@@ -2756,7 +2860,7 @@ On reaching beginning of line, stop and signal error."
     (end-of-line val)
     (if com (viper-execute-com 'viper-goto-eol val com))
     (if viper-ex-style-motion
-       (if (and (eolp) (not (bolp)) 
+       (if (and (eolp) (not (bolp))
                 ;; a fix for viper-change-to-eol
                 (not (equal viper-current-state 'insert-state)))
            (backward-char 1)
@@ -2783,7 +2887,7 @@ On reaching beginning of line, stop and signal error."
       (end-of-line)
       (if (> val (current-column)) (error "")))
     ))
-    
+
 
 (defun viper-next-line (arg)
   "Go to next line."
@@ -2812,8 +2916,9 @@ On reaching beginning of line, stop and signal error."
     (back-to-indentation)
     (if com (viper-execute-com 'viper-next-line-at-bol val com))))
 
-(defun viper-previous-line (arg)        
-  "Go to previous line."       
+
+(defun viper-previous-line (arg)
+  "Go to previous line."
   (interactive "P")
   (viper-leave-region-active)
   (let ((val (viper-p-val arg))
@@ -2870,18 +2975,18 @@ On reaching beginning of line, stop and signal error."
        (goto-char (point-max))
       (goto-char (point-min))
       (forward-line (1- val)))
-    
+
     ;; positioning is done twice: before and after command execution
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
-    
+
     (if com (viper-execute-com 'viper-goto-line val com))
-    
+
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
     ))
 
-;; Find ARG's occurrence of CHAR on the current line. 
+;; Find ARG's occurrence of CHAR on the current line.
 ;; If FORWARD then search is forward, otherwise backward.  OFFSET is used to
 ;; adjust point after search.
 (defun viper-find-char (arg char forward offset)
@@ -2890,21 +2995,37 @@ On reaching beginning of line, stop and signal error."
        (cmd (if (eq viper-intermediate-command 'viper-repeat)
                 (nth 5 viper-d-com)
               (viper-array-to-string (this-command-keys))))
-       point)
+       point region-beg region-end)
     (save-excursion
       (save-restriction
-       (if (> arg 0)
-           (narrow-to-region
-            ;; forward search begins here
-            (if (eolp) (error "Command `%s':  At end of line" cmd) (point))
-            ;; forward search ends here
-            (progn (end-of-line) (point)))
-         (narrow-to-region
-          ;; backward search begins from here
-          (if (bolp)
-              (error "Command `%s':  At beginning of line" cmd) (point))
-          ;; backward search ends here
-          (progn (beginning-of-line) (point))))
+       (if (> arg 0) ; forward
+           (progn
+             (setq region-beg (point))
+             (if viper-allow-multiline-replace-regions
+                 (viper-forward-paragraph 1)
+               (end-of-line))
+             (setq region-end (point)))
+         (setq region-end (point))
+             (if viper-allow-multiline-replace-regions
+                 (viper-backward-paragraph 1)
+               (beginning-of-line))
+             (setq region-beg (point)))
+       (if (or (and (< arg 0)
+                    (< (- region-end region-beg)
+                       (if viper-allow-multiline-replace-regions
+                           2 1))
+                    (bolp))
+               (and (> arg 0)
+                    (< (- region-end region-beg)
+                       (if viper-allow-multiline-replace-regions
+                           3 2))
+                    (eolp)))
+           (error "Command `%s':  At %s of %s"
+                  cmd
+                  (if (> arg 0) "end" "beginning")
+                  (if viper-allow-multiline-replace-regions
+                      "paragraph" "line")))
+       (narrow-to-region region-beg region-end)
        ;; if arg > 0, point is forwarded before search.
        (if (> arg 0) (goto-char (1+ (point-min)))
          (goto-char (point-max)))
@@ -2918,7 +3039,7 @@ On reaching beginning of line, stop and signal error."
       (forward-char (if offset 1 0)))))
 
 (defun viper-find-char-forward (arg)
-  "Find char on the line. 
+  "Find char on the line.
 If called interactively read the char to find from the terminal, and if
 called from viper-repeat, the char last used is used.  This behaviour is
 controlled by the sign of prefix numeric value."
@@ -3060,16 +3181,17 @@ controlled by the sign of prefix numeric value."
   (interactive "P")
   (let ((val (viper-p-val arg))
        (com (viper-getCom arg)))
+    (viper-leave-region-active)
     (if com (viper-move-marker-locally 'viper-com-point (point)))
-    (push-mark nil t) 
+    (push-mark nil t)
     (move-to-window-line (1- val))
 
     ;; positioning is done twice: before and after command execution
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
-    
+
     (if com (viper-execute-com 'viper-window-top val com))
-    
+
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
     ))
@@ -3078,21 +3200,18 @@ controlled by the sign of prefix numeric value."
   "Go to middle window line."
   (interactive "P")
   (let ((val (viper-p-val arg))
-       (com (viper-getCom arg))
-       lines)
+       (com (viper-getCom arg)))
+    (viper-leave-region-active)
     (if com (viper-move-marker-locally 'viper-com-point (point)))
-    (push-mark nil t) 
-    (if (not (pos-visible-in-window-p (point-max)))
-       (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
-      (setq lines (count-lines (window-start) (point-max)))
-      (move-to-window-line (+ (/ lines 2) (1- val))))
-      
+    (push-mark nil t)
+    (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
+
     ;; positioning is done twice: before and after command execution
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
 
     (if com (viper-execute-com 'viper-window-middle val com))
-    
+
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
     ))
@@ -3102,16 +3221,17 @@ controlled by the sign of prefix numeric value."
   (interactive "P")
   (let ((val (viper-p-val arg))
        (com (viper-getCom arg)))
+    (viper-leave-region-active)
     (if com (viper-move-marker-locally 'viper-com-point (point)))
-    (push-mark nil t) 
+    (push-mark nil t)
     (move-to-window-line (- val))
-    
+
     ;; positioning is done twice: before and after command execution
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
 
     (if com (viper-execute-com 'viper-window-bottom val com))
-    
+
     (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
     (back-to-indentation)
     ))
@@ -3133,8 +3253,8 @@ controlled by the sign of prefix numeric value."
 
 ;; If point is within viper-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 viper-search
-;; (which is called from viper-search-forward/backward/next). If the value of
+;; bottom or at the top of the  window.  This function is called by viper-search
+;; (which is called from viper-search-forward/backward/next).  If the value of
 ;; viper-search-scroll-threshold is negative - don't scroll.
 (defun viper-adjust-window ()
   (let ((win-height (if viper-emacs-p
@@ -3163,9 +3283,9 @@ controlled by the sign of prefix numeric value."
 
 \f
 ;; paren match
-;; must correct this to only match ( to ) etc. On the other hand
+;; must correct this to only match ( to ) etc.  On the other hand
 ;; it is good that paren match gets confused, because that way you
-;; catch _all_ imbalances. 
+;; catch _all_ imbalances.
 
 (defun viper-paren-match (arg)
   "Go to the matching parenthesis."
@@ -3191,7 +3311,7 @@ controlled by the sign of prefix numeric value."
          (setq beg-lim (point))
          (end-of-line)
          (setq end-lim (point)))
-       (cond ((re-search-forward "[][(){}]" end-lim t) 
+       (cond ((re-search-forward "[][(){}]" end-lim t)
               (backward-char) )
              ((re-search-backward "[][(){}]" beg-lim t))
              (t
@@ -3225,12 +3345,13 @@ controlled by the sign of prefix numeric value."
          (if viper-parse-sexp-ignore-comments "" "NOT "))))
 
 \f
-;; sentence ,paragraph and heading
+;; sentenceparagraph and heading
 
 (defun viper-forward-sentence (arg)
   "Forward sentence."
   (interactive "P")
-  (push-mark nil t) 
+  (or (eq last-command this-command)
+      (push-mark nil t))
   (let ((val (viper-p-val arg))
        (com (viper-getcom arg)))
     (if com (viper-move-marker-locally 'viper-com-point (point)))
@@ -3240,7 +3361,8 @@ controlled by the sign of prefix numeric value."
 (defun viper-backward-sentence (arg)
   "Backward sentence."
   (interactive "P")
-  (push-mark nil t) 
+  (or (eq last-command this-command)
+      (push-mark nil t))
   (let ((val (viper-p-val arg))
        (com (viper-getcom arg)))
     (if com (viper-move-marker-locally 'viper-com-point (point)))
@@ -3250,9 +3372,12 @@ controlled by the sign of prefix numeric value."
 (defun viper-forward-paragraph (arg)
   "Forward paragraph."
   (interactive "P")
-  (push-mark nil t) 
+  (or (eq last-command this-command)
+      (push-mark nil t))
   (let ((val (viper-p-val arg))
-       (com (viper-getCom arg)))
+       ;; if you want d} operate on whole lines, change viper-getcom to
+       ;; viper-getCom below
+       (com (viper-getcom arg)))
     (if com (viper-move-marker-locally 'viper-com-point (point)))
     (forward-paragraph val)
     (if com
@@ -3263,9 +3388,12 @@ controlled by the sign of prefix numeric value."
 (defun viper-backward-paragraph (arg)
   "Backward paragraph."
   (interactive "P")
-  (push-mark nil t) 
+  (or (eq last-command this-command)
+      (push-mark nil t))
   (let ((val (viper-p-val arg))
-       (com (viper-getCom arg)))
+       ;; if you want d{ operate on whole lines, change viper-getcom to
+       ;; viper-getCom below
+       (com (viper-getcom arg)))
     (if com (viper-move-marker-locally 'viper-com-point (point)))
     (backward-paragraph val)
     (if com
@@ -3274,8 +3402,7 @@ controlled by the sign of prefix numeric value."
          (viper-execute-com 'viper-backward-paragraph nil com)
          (backward-char 1)))))
 
-;; should be mode-specific etc.
-
+;; should be mode-specific
 (defun viper-prev-heading (arg)
   (interactive "P")
   (let ((val (viper-p-val arg))
@@ -3377,26 +3504,26 @@ controlled by the sign of prefix numeric value."
            'viper-search-history
            (car viper-search-history))))
     (if (not (string= s ""))
-       (setq viper-s-string s))))  
-       
-    
-(defun viper-toggle-search-style (arg) 
+       (setq viper-s-string s))))
+
+
+(defun viper-toggle-search-style (arg)
   "Toggle the value of viper-case-fold-search/viper-re-search.
-Without prefix argument, will ask which search style to toggle. With prefix
+Without prefix argument, will ask which search style to toggle.  With prefix
 arg 1,toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
 
 Although this function is bound to \\[viper-toggle-search-style], the most
 convenient way to use it is to bind `//' to the macro
 `1 M-x viper-toggle-search-style' and `///' to
-`2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
+`2 M-x viper-toggle-search-style'.  In this way, hitting `//' quickly will
 toggle case-fold-search and hitting `/' three times witth toggle regexp
-search. Macros are more convenient in this case because they don't affect
+search.  Macros are more convenient in this case because they don't affect
 the Emacs binding of `/'."
   (interactive "P")
   (let (msg)
     (cond ((or (eq arg 1)
               (and (null arg)
-                   (y-or-n-p (format "Search style: '%s'. Want '%s'? "
+                   (y-or-n-p (format "Search style: '%s'.  Want '%s'? "
                                      (if viper-case-fold-search
                                          "case-insensitive" "case-sensitive")
                                      (if viper-case-fold-search
@@ -3408,7 +3535,7 @@ the Emacs binding of `/'."
             (setq msg "Search becomes case-sensitive")))
          ((or (eq arg 2)
               (and (null arg)
-                   (y-or-n-p (format "Search style: '%s'. Want '%s'? "
+                   (y-or-n-p (format "Search style: '%s'.  Want '%s'? "
                                      (if viper-re-search
                                          "regexp-search" "vanilla-search")
                                      (if viper-re-search
@@ -3473,9 +3600,9 @@ With a prefix argument, unsets the macro."
   "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. 
+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,
+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")
@@ -3485,7 +3612,7 @@ the macros are set in the current major mode.
            ;; toggle case sensitivity in search
            (viper-record-kbd-macro
             "//" 'emacs-state
-            [1 (meta x) v i p e r - t o g g l e - s e a r c h - s t y l e return] 
+            [1 (meta x) v i p e r - 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
            (viper-record-kbd-macro
@@ -3501,7 +3628,7 @@ the macros are set in the current major mode.
 
 
 (defun viper-search-forward (arg)
-  "Search a string forward. 
+  "Search a string forward.
 ARG is used to find the ARG's occurrence of the string.
 Null string will repeat previous search."
   (interactive "P")
@@ -3522,7 +3649,7 @@ Null string will repeat previous search."
          (viper-execute-com 'viper-search-next val com)))))
 
 (defun viper-search-backward (arg)
-  "Search a string backward. 
+  "Search a string backward.
 ARG is used to find the ARG's occurrence of the string.
 Null string will repeat previous search."
   (interactive "P")
@@ -3541,7 +3668,7 @@ Null string will repeat previous search."
        (progn
          (viper-move-marker-locally 'viper-com-point (mark t))
          (viper-execute-com 'viper-search-next val com)))))
-         
+
 
 ;; Search for COUNT's occurrence of STRING.
 ;; Search is forward if FORWARD is non-nil, otherwise backward.
@@ -3568,7 +3695,7 @@ Null string will repeat previous search."
                  (search-forward string nil nil val)
                  (search-backward string))
                (if (not (equal start-point (point)))
-                   (push-mark start-point t))) 
+                   (push-mark start-point t)))
            (search-failed
             (if (and (not fail-if-not-found) viper-search-wrap-around-t)
                 (progn
@@ -3577,7 +3704,7 @@ Null string will repeat previous search."
                   (viper-search string forward (cons 1 com) t start-point 'fail)
                   ;; don't wait in macros
                   (or executing-kbd-macro
-                      (memq viper-intermediate-command 
+                      (memq viper-intermediate-command
                             '(viper-repeat
                               viper-digit-argument
                               viper-command-argument))
@@ -3597,7 +3724,7 @@ Null string will repeat previous search."
                  (re-search-backward string nil nil val)
                (search-backward string nil nil val))
              (if (not (equal start-point (point)))
-                 (push-mark start-point t))) 
+                 (push-mark start-point t)))
          (search-failed
           (if (and (not fail-if-not-found) viper-search-wrap-around-t)
               (progn
@@ -3606,7 +3733,7 @@ Null string will repeat previous search."
                 (viper-search string forward (cons 1 com) t start-point 'fail)
                 ;; don't wait in macros
                 (or executing-kbd-macro
-                    (memq viper-intermediate-command 
+                    (memq viper-intermediate-command
                           '(viper-repeat
                             viper-digit-argument
                             viper-command-argument))
@@ -3624,7 +3751,7 @@ Null string will repeat previous search."
       ;; highlight the result of search
       ;; don't wait and don't highlight in macros
       (or executing-kbd-macro
-         (memq viper-intermediate-command 
+         (memq viper-intermediate-command
                '(viper-repeat viper-digit-argument viper-command-argument))
          (viper-flash-search-pattern))
       )))
@@ -3661,7 +3788,11 @@ Null string will repeat previous search."
        ((null viper-buffer-search-char)
         (setq viper-buffer-search-char ?g)))
   (define-key viper-vi-basic-map
-    (char-to-string viper-buffer-search-char) 'viper-command-argument)
+    (cond ((viper-characterp viper-buffer-search-char)
+          (char-to-string viper-buffer-search-char))
+         (t (error "viper-buffer-search-char: wrong value type, %s"
+                   viper-buffer-search-char)))
+    'viper-command-argument)
   (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
   (setq viper-prefix-commands
        (cons viper-buffer-search-char viper-prefix-commands)))
@@ -3688,52 +3819,50 @@ Null string will repeat previous search."
 (defun viper-switch-to-buffer ()
   "Switch to buffer in the current window."
   (interactive)
-  (let (buffer)
+  (let ((other-buffer (other-buffer (current-buffer)))
+       buffer)
     (setq buffer
-         (read-buffer
-          (format "Switch to buffer in this window \(%s\): "
-                  (buffer-name (other-buffer (current-buffer))))))
-    (switch-to-buffer buffer)
-    ))
+         (funcall viper-read-buffer-function
+                  "Switch to buffer in this window: " other-buffer))
+    (switch-to-buffer buffer)))
 
 (defun viper-switch-to-buffer-other-window ()
   "Switch to buffer in another window."
   (interactive)
-  (let (buffer)
+  (let ((other-buffer (other-buffer (current-buffer)))
+       buffer)
     (setq buffer
-         (read-buffer
-          (format "Switch to buffer in another window \(%s\): "
-                  (buffer-name (other-buffer (current-buffer))))))
-    (switch-to-buffer-other-window buffer)
-    ))
+         (funcall viper-read-buffer-function
+                  "Switch to buffer in another window: " other-buffer))
+    (switch-to-buffer-other-window buffer)))
 
 (defun viper-kill-buffer ()
   "Kill a buffer."
   (interactive)
   (let (buffer buffer-name)
     (setq buffer-name
-         (read-buffer
-          (format "Kill buffer \(%s\): "
-                  (buffer-name (current-buffer)))))
+         (funcall viper-read-buffer-function
+                  (format "Kill buffer \(%s\): "
+                          (buffer-name (current-buffer)))))
     (setq buffer
          (if (null buffer-name)
              (current-buffer)
            (get-buffer buffer-name)))
     (if (null buffer) (error "`%s': No such buffer" buffer-name))
     (if (or (not (buffer-modified-p buffer))
-           (y-or-n-p 
+           (y-or-n-p
             (format
              "Buffer `%s' is modified, are you sure you want to kill it? "
              buffer-name)))
        (kill-buffer buffer)
       (error "Buffer not killed"))))
 
-     
+
 \f
 ;; yank and pop
 
 (defsubst viper-yank (text)
-  "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
+  "Yank TEXT silently.  This works correctly with Emacs's yank-pop command."
     (insert text)
     (setq this-command 'yank))
 
@@ -3748,7 +3877,8 @@ Null string will repeat previous search."
                        ((viper-valid-register viper-use-register)
                         (get-register (downcase viper-use-register)))
                        (t (error viper-InvalidRegister viper-use-register)))
-               (current-kill 0))))
+               (current-kill 0)))
+       sv-point chars-inserted lines-inserted)
     (if (null text)
        (if viper-use-register
            (let ((reg viper-use-register))
@@ -3767,9 +3897,16 @@ Null string will repeat previous search."
     (set-marker (viper-mark-marker) (point) (current-buffer))
     (viper-set-destructive-command
      (list 'viper-put-back val nil viper-use-register nil nil))
-    (viper-loop val (viper-yank text)))
+    (setq sv-point (point))
+    (viper-loop val (viper-yank text))
+    (setq chars-inserted (abs (- (point) sv-point))
+         lines-inserted (abs (count-lines (point) sv-point)))
+    (if (or (> chars-inserted viper-change-notification-threshold)
+           (> lines-inserted viper-change-notification-threshold))
+       (message "Inserted %d character(s), %d line(s)"
+                chars-inserted lines-inserted)))
   ;; Vi puts cursor on the last char when the yanked text doesn't contain a
-  ;; newline; it leaves the cursor at the beginning when the text contains 
+  ;; newline; it leaves the cursor at the beginning when the text contains
   ;; a newline
   (if (viper-same-line (point) (mark))
       (or (= (point) (mark)) (viper-backward-char-carefully))
@@ -3789,7 +3926,8 @@ Null string will repeat previous search."
                        ((viper-valid-register viper-use-register)
                         (get-register (downcase viper-use-register)))
                        (t (error viper-InvalidRegister viper-use-register)))
-               (current-kill 0))))
+               (current-kill 0)))
+       sv-point chars-inserted lines-inserted)
     (if (null text)
        (if viper-use-register
            (let ((reg viper-use-register))
@@ -3801,9 +3939,16 @@ Null string will repeat previous search."
     (viper-set-destructive-command
      (list 'viper-Put-back val nil viper-use-register nil nil))
     (set-marker (viper-mark-marker) (point) (current-buffer))
-    (viper-loop val (viper-yank text)))
+    (setq sv-point (point))
+    (viper-loop val (viper-yank text))
+    (setq chars-inserted (abs (- (point) sv-point))
+         lines-inserted (abs (count-lines (point) sv-point)))
+    (if (or (> chars-inserted viper-change-notification-threshold)
+           (> lines-inserted viper-change-notification-threshold))
+       (message "Inserted %d character(s), %d line(s)"
+                chars-inserted lines-inserted)))
   ;; Vi puts cursor on the last char when the yanked text doesn't contain a
-  ;; newline; it leaves the cursor at the beginning when the text contains 
+  ;; newline; it leaves the cursor at the beginning when the text contains
   ;; a newline
   (if (viper-same-line (point) (mark))
       (or (= (point) (mark)) (viper-backward-char-carefully))
@@ -3811,7 +3956,7 @@ Null string will repeat previous search."
     (if (bolp)
        (back-to-indentation)))
   (viper-deactivate-mark))
-    
+
 
 ;; Copy region to kill-ring.
 ;; If BEG and END do not belong to the same buffer, copy empty region.
@@ -3819,7 +3964,7 @@ Null string will repeat previous search."
   (condition-case nil
       (copy-region-as-kill beg end)
     (error (copy-region-as-kill beg beg))))
-    
+
 
 (defun viper-delete-char (arg)
   "Delete next character."
@@ -3853,13 +3998,13 @@ Null string will repeat previous search."
     ))
 
 (defun viper-delete-backward-char (arg)
-  "Delete previous character. On reaching beginning of line, stop and beep."
+  "Delete previous character.  On reaching beginning of line, stop and beep."
   (interactive "P")
   (let ((val (viper-p-val arg))
        end-del-pos)
     (viper-set-destructive-command
      (list 'viper-delete-backward-char val nil nil nil nil))
-    (if (and 
+    (if (and
         viper-ex-style-editing
         (> val (viper-chars-in-region (viper-line-pos 'start) (point))))
        (setq val (viper-chars-in-region (viper-line-pos 'start) (point))))
@@ -3879,19 +4024,21 @@ Null string will repeat previous search."
     (if (and (bolp) viper-ex-style-editing)
        (ding))
     (delete-backward-char val t)))
-      
+
+
 (defun viper-del-backward-char-in-insert ()
   "Delete 1 char backwards while in insert mode."
-  (interactive)      
+  (interactive)
   (if (and viper-ex-style-editing (bolp))
       (beep 1)
     (delete-backward-char 1 t)))
-      
+
+
 (defun viper-del-backward-char-in-replace ()
   "Delete one character in replace mode.
 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
-charecters. If it is nil, then the cursor just moves backwards, similarly
-to Vi. The variable `viper-ex-style-editing', if t, doesn't let the
+charecters.  If it is nil, then the cursor just moves backwards, similarly
+to Vi.  The variable `viper-ex-style-editing', if t, doesn't let the
 cursor move past the beginning of line."
   (interactive)
   (cond (viper-delete-backwards-in-replace
@@ -3907,7 +4054,7 @@ cursor move past the beginning of line."
         (if (bolp)
             (beep 1)
           (backward-char 1)))
-       (t 
+       (t
         (backward-char 1))))
 
 
@@ -3942,7 +4089,7 @@ cursor move past the beginning of line."
   (if (markerp end) (setq end (marker-position end)))
   ;; beg is sometimes (mark t), which may be nil
   (or beg (setq beg end))
-  
+
   (viper-set-complex-command-for-undo)
   (if viper-use-register
       (progn
@@ -3950,14 +4097,14 @@ cursor move past the beginning of line."
        (setq viper-use-register nil)))
   (viper-set-replace-overlay beg end)
   (setq last-command nil) ; separate repl text from prev kills
-  
+
   (if (= (viper-replace-start) (point-max))
       (error "End of buffer"))
-      
+
   (setq viper-last-replace-region
        (buffer-substring (viper-replace-start)
                          (viper-replace-end)))
-  
+
   ;; protect against error while inserting "@" and other disasters
   ;; (e.g., read-only buff)
   (condition-case conds
@@ -3969,17 +4116,17 @@ cursor move past the beginning of line."
            (goto-char (viper-replace-end))
            (insert-before-markers "@") ; put placeholder after the TAB
            (untabify (viper-replace-start) (point))
-           ;; del @, don't put on kill ring 
+           ;; del @, don't put on kill ring
            (delete-backward-char 1)
-           
+
            (viper-set-replace-overlay-glyphs
             viper-replace-region-start-delimiter
             viper-replace-region-end-delimiter)
            ;; this move takes care of the last posn in the overlay, which
-           ;; has to be shifted because of insert. We can't simply insert
+           ;; has to be shifted because of insert.  We can't simply insert
            ;; "$" before-markers because then overlay-start will shift the
            ;; beginning of the overlay in case we are replacing a single
-           ;; character. This fixes the bug with `s' and `cl' commands.
+           ;; character.  This fixes the bug with `s' and `cl' commands.
            (viper-move-replace-overlay (viper-replace-start) (point))
            (goto-char (viper-replace-start))
            (viper-change-state-to-replace t))
@@ -4024,7 +4171,7 @@ cursor move past the beginning of line."
 ;; query replace
 
 (defun viper-query-replace ()
-  "Query replace. 
+  "Query replace.
 If a null string is suplied as the string to be replaced,
 the query replace mode will toggle between string replace
 and regexp replace."
@@ -4086,14 +4233,15 @@ and regexp replace."
   (let ((char (read-char)))
     (cond ((and (<= ?a char) (<= char ?z))
           (point-to-register (1+ (- char ?a))))
-         ((= char ?<) (viper-mark-beginning-of-buffer))
-         ((= char ?>) (viper-mark-end-of-buffer))
-         ((= char ?.) (viper-set-mark-if-necessary))
-         ((= char ?,) (viper-cycle-through-mark-ring))
-         ((= char ?D) (mark-defun))
+         ((viper= char ?<) (viper-mark-beginning-of-buffer))
+         ((viper= char ?>) (viper-mark-end-of-buffer))
+         ((viper= char ?.) (viper-set-mark-if-necessary))
+         ((viper= char ?,) (viper-cycle-through-mark-ring))
+         ((viper= char ?^) (push-mark viper-saved-mark t t))
+         ((viper= 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,
 ;; making it into the new mark, M1.
@@ -4101,7 +4249,7 @@ and regexp replace."
 ;; So, if you hit '' or `` then you can return to p1.
 ;;
 ;; If repeated command, pop top elt from the ring into mark and
-;; jump there. This forgets the position, p1, and puts M1 back into mark.
+;; jump there.  This forgets the position, p1, and puts M1 back into mark.
 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
 ;; the ring into mark.  Push M2 back on the ring and set mark to M0.
 ;; etc.
@@ -4110,12 +4258,12 @@ and regexp replace."
 One can use `` and '' to temporarily jump 1 step back."
   (let* ((sv-pt (point)))
        ;; if repeated `m,' command, pop the previously saved mark.
-       ;; Prev saved mark is actually prev saved point. It is used if the
-       ;; user types `` or '' and is discarded 
-       ;; from the mark ring by the next `m,' command. 
+       ;; Prev saved mark is actually prev saved point.  It is used if the
+       ;; user types `` or '' and is discarded
+       ;; from the mark ring by the next `m,' command.
        ;; In any case, go to the previous or previously saved mark.
        ;; Then push the current mark (popped off the ring) and set current
-       ;; point to be the mark. Current pt as mark is discarded by the next
+       ;; point to be the mark.  Current pt as mark is discarded by the next
        ;; m, command.
        (if (eq last-command 'viper-cycle-through-mark-ring)
           ()
@@ -4131,7 +4279,7 @@ One can use `` and '' to temporarily jump 1 step back."
        (viper-deactivate-mark)
        (setq this-command 'viper-cycle-through-mark-ring)
        ))
-       
+
 
 (defun viper-goto-mark (arg)
   "Go to mark."
@@ -4148,7 +4296,7 @@ One can use `` and '' to temporarily jump 1 step back."
     (viper-goto-mark-subr char com t)))
 
 (defun viper-goto-mark-subr (char com skip-white)
-  (if (eobp) 
+  (if (eobp)
       (if (bobp)
          (error "Empty buffer")
        (backward-char 1)))
@@ -4156,16 +4304,29 @@ One can use `` and '' to temporarily jump 1 step back."
         (let* ((buff (current-buffer))
                (reg (1+ (- char ?a)))
                (text-marker (get-register reg)))
+          ;; If marker points to file that had markers set (and those markers
+          ;; were saved (as e.g., in session.el), then restore those markers
+          (if (and (consp text-marker)
+                   (eq (car text-marker) 'file-query)
+                   (or (find-buffer-visiting (nth 1 text-marker))
+                       (y-or-n-p (format "Visit file %s again? "
+                                         (nth 1 text-marker)))))
+              (save-excursion
+                (find-file (nth 1 text-marker))
+                (when (and (<= (nth 2 text-marker) (point-max))
+                           (<= (point-min) (nth 2 text-marker)))
+                  (setq text-marker (copy-marker (nth 2 text-marker)))
+                  (set-register reg text-marker))))
           (if com (viper-move-marker-locally 'viper-com-point (point)))
           (if (not (viper-valid-marker text-marker))
               (error viper-EmptyTextmarker char))
           (if (and (viper-same-line (point) viper-last-jump)
                    (= (point) viper-last-jump-ignore))
-              (push-mark viper-last-jump t) 
+              (push-mark viper-last-jump t)
             (push-mark nil t)) ; no msg
           (viper-register-to-point reg)
           (setq viper-last-jump (point-marker))
-          (cond (skip-white 
+          (cond (skip-white
                  (back-to-indentation)
                  (setq viper-last-jump-ignore (point))))
           (if com
@@ -4178,7 +4339,7 @@ One can use `` and '' to temporarily jump 1 step back."
                 (goto-char viper-com-point)
                 (viper-change-state-to-vi)
                 (error "")))))
-       ((and (not skip-white) (= char ?`))
+       ((and (not skip-white) (viper= char ?`))
         (if com (viper-move-marker-locally 'viper-com-point (point)))
         (if (and (viper-same-line (point) viper-last-jump)
                  (= (point) viper-last-jump-ignore))
@@ -4189,7 +4350,7 @@ One can use `` and '' to temporarily jump 1 step back."
         (setq viper-last-jump (point-marker)
               viper-last-jump-ignore 0)
         (if com (viper-execute-com 'viper-goto-mark nil com)))
-       ((and skip-white (= char ?'))
+       ((and skip-white (viper= char ?'))
         (if com (viper-move-marker-locally 'viper-com-point (point)))
         (if (and (viper-same-line (point) viper-last-jump)
                  (= (point) viper-last-jump-ignore))
@@ -4201,7 +4362,7 @@ One can use `` and '' to temporarily jump 1 step back."
         (setq viper-last-jump-ignore (point))
         (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
        (t (error viper-InvalidTextmarker char))))
-       
+
 (defun viper-insert-tab ()
   (interactive)
   (insert-tab))
@@ -4277,50 +4438,45 @@ One can use `` and '' to temporarily jump 1 step back."
          ))
     ))
 
-          
+
 ;; Viewing registers
 
 (defun viper-ket-function (arg)
-  "Function called by \], the ket. View registers and call \]\]."
+  "Function called by \], the ket.  View registers and call \]\]."
   (interactive "P")
   (let ((reg (read-char)))
     (cond ((viper-valid-register reg '(letter Letter))
           (view-register (downcase reg)))
          ((viper-valid-register reg '(digit))
           (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
-            (save-excursion 
-              (set-buffer (get-buffer-create "*Output*"))
-              (delete-region (point-min) (point-max))
-              (insert (format "Register %c contains the string:\n" reg))
-              (insert text)
-              (goto-char (point-min)))
-            (display-buffer "*Output*")))
-         ((= ?\] reg)
+            (with-output-to-temp-buffer " *viper-info*"
+              (princ (format "Register %c contains the string:\n" reg))
+              (princ text))
+            ))
+         ((viper= ?\] reg)
           (viper-next-heading arg))
          (t (error
              viper-InvalidRegister reg)))))
 
 (defun viper-brac-function (arg)
-  "Function called by \[, the brac. View textmarkers and call \[\["
+  "Function called by \[, the brac.  View textmarkers and call \[\["
   (interactive "P")
   (let ((reg (read-char)))
-    (cond ((= ?\[ reg)
+    (cond ((viper= ?\[ reg)
           (viper-prev-heading arg))
-         ((= ?\] reg)
+         ((viper= ?\] reg)
           (viper-heading-end arg))
          ((viper-valid-register reg '(letter))
           (let* ((val (get-register (1+ (- reg ?a))))
-                 (buf (if (not val) 
+                 (buf (if (not (markerp val))
                           (error viper-EmptyTextmarker reg)
                         (marker-buffer val)))
                  (pos (marker-position val))
                  line-no text (s pos) (e pos))
-            (save-excursion 
-              (set-buffer (get-buffer-create "*Output*"))
-              (delete-region (point-min) (point-max))
+            (with-output-to-temp-buffer " *viper-info*"
               (if (and buf pos)
                   (progn
-                    (save-excursion 
+                    (save-excursion
                       (set-buffer buf)
                       (setq line-no (1+ (count-lines (point-min) val)))
                       (goto-char pos)
@@ -4336,20 +4492,19 @@ One can use `` and '' to temporarily jump 1 step back."
                             (end-of-line)
                             (setq e (point))))
                       (setq text (buffer-substring s e))
-                      (setq text (format "%s<%c>%s" 
-                                         (substring text 0 (- pos s)) 
+                      (setq text (format "%s<%c>%s"
+                                         (substring text 0 (- pos s))
                                          reg (substring text (- pos s)))))
-                    (insert
+                    (princ
                      (format
                       "Textmarker `%c' is in buffer `%s' at line %d.\n"
                                     reg (buffer-name buf) line-no))
-                    (insert (format "Here is some text around %c:\n\n %s" 
+                    (princ (format "Here is some text around %c:\n\n %s"
                                     reg text)))
-                (insert (format viper-EmptyTextmarker reg)))
-              (goto-char (point-min)))
-            (display-buffer "*Output*")))
+                (princ (format viper-EmptyTextmarker reg))))
+            ))
          (t (error viper-InvalidTextmarker reg)))))
-  
+
 
 \f
 ;; commands in insertion mode
@@ -4375,24 +4530,24 @@ meaningless (i.e., not one of 1,2,3,4,5).
 
 User level determines the setting of Viper variables that are most
 sensitive for VI-style look-and-feel."
-  
+
   (interactive)
-  
+
   (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
-  
+
   (save-window-excursion
     (delete-other-windows)
     ;; if 0 < viper-expert-level < viper-max-expert-level
     ;;    & dont-change-unless = t -- use it; else ask
     (viper-ask-level dont-change-unless))
-  
+
   (setq viper-always                           t
        viper-ex-style-motion                   t
-       viper-ex-style-editing                  t    
+       viper-ex-style-editing                  t
        viper-want-ctl-h-help nil)
 
   (cond ((eq viper-expert-level 1) ; novice or beginner
-        (global-set-key   ; in emacs-state 
+        (global-set-key   ; in emacs-state
          viper-toggle-key
          (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
         (setq viper-no-multiple-ESC           t
@@ -4402,7 +4557,7 @@ sensitive for VI-style look-and-feel."
               viper-electric-mode             nil
               viper-want-emacs-keys-in-vi     nil
               viper-want-emacs-keys-in-insert nil))
-       
+
        ((and (> viper-expert-level 1) (< viper-expert-level 5))
         ;; intermediate to guru
         (setq viper-no-multiple-ESC           (if (viper-window-display-p)
@@ -4419,15 +4574,15 @@ sensitive for VI-style look-and-feel."
                (viper-standard-value 'viper-ex-style-editing)
                viper-ex-style-motion
                (viper-standard-value 'viper-ex-style-motion))
-              (setq viper-ex-style-motion 
+              (setq viper-ex-style-motion
                     (viper-standard-value 'viper-ex-style-motion)
                     viper-ex-style-editing
                     (viper-standard-value 'viper-ex-style-editing)
                     viper-re-search
                     (viper-standard-value 'viper-re-search)
-                    viper-no-multiple-ESC 
+                    viper-no-multiple-ESC
                     (viper-standard-value 'viper-no-multiple-ESC)))))
-       
+
        ;; A wizard!!
        ;; Ideally, if 5 is selected, a buffer should pop up to let the
        ;; user toggle the values of variables.
@@ -4435,37 +4590,38 @@ sensitive for VI-style look-and-feel."
                         (viper-standard-value 'viper-ex-style-editing)
                         viper-ex-style-motion
                         (viper-standard-value 'viper-ex-style-motion))
-          (setq  viper-want-ctl-h-help 
+          (setq  viper-want-ctl-h-help
                  (viper-standard-value 'viper-want-ctl-h-help)
                  viper-always
                  (viper-standard-value 'viper-always)
-                 viper-no-multiple-ESC 
+                 viper-no-multiple-ESC
                  (viper-standard-value 'viper-no-multiple-ESC)
-                 viper-ex-style-motion 
+                 viper-ex-style-motion
                  (viper-standard-value 'viper-ex-style-motion)
                  viper-ex-style-editing
                  (viper-standard-value 'viper-ex-style-editing)
                  viper-re-search
                  (viper-standard-value 'viper-re-search)
-                 viper-electric-mode 
+                 viper-electric-mode
                  (viper-standard-value 'viper-electric-mode)
-                 viper-want-emacs-keys-in-vi 
+                 viper-want-emacs-keys-in-vi
                  (viper-standard-value 'viper-want-emacs-keys-in-vi)
                  viper-want-emacs-keys-in-insert
                  (viper-standard-value 'viper-want-emacs-keys-in-insert))))
-  
+
   (viper-set-mode-vars-for viper-current-state)
   (if (or viper-always
          (and (> viper-expert-level 0) (> 5 viper-expert-level)))
       (viper-set-hooks)))
 
+
 ;; Ask user expert level.
 (defun viper-ask-level (dont-change-unless)
   (let ((ask-buffer " *viper-ask-level*")
        level-changed repeated)
     (save-window-excursion
       (switch-to-buffer ask-buffer)
-             
+
       (while (or (> viper-expert-level viper-max-expert-level)
                 (< viper-expert-level 1)
                 (null dont-change-unless))
@@ -4481,10 +4637,10 @@ sensitive for VI-style look-and-feel."
 Please specify your level of familiarity with the venomous VI PERil
 (and the VI Plan for Emacs Rescue).
 You can change it at any time by typing `M-x viper-set-expert-level RET'
-       
+
  1 -- BEGINNER: Almost all Emacs features are suppressed.
-       Feels almost like straight Vi. File name completion and
-       command history in the minibuffer are thrown in as a bonus. 
+       Feels almost like straight Vi.  File name completion and
+       command history in the minibuffer are thrown in as a bonus.
        To use Emacs productively, you must reach level 3 or higher.
  2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
        so most Emacs commands can be used when Viper is in Vi state.
@@ -4493,16 +4649,16 @@ You can change it at any time by typing `M-x viper-set-expert-level RET'
        in Viper's insert state.
  4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
        viper-ex-style-motion, viper-ex-style-editing, and
-       viper-re-search variables. Adjust these settings to your taste.
+       viper-re-search variables.  Adjust these settings to your taste.
  5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
        viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
-       and viper-want-emacs-keys-in-insert. Adjust these to your taste.
-      
+       and viper-want-emacs-keys-in-insert.  Adjust these to your taste.
+
 Please, specify your level now: ")
-         
+
        (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
        ) ; end while
-      
+
       ;; tell the user if level was changed
       (and level-changed
           (progn
@@ -4524,8 +4680,8 @@ Please, specify your level now: ")
 (defun viper-nil ()
   (interactive)
   (beep 1))
-  
-    
+
+
 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
 (defun viper-register-to-point (char &optional enforce-buffer)
   "Like jump-to-register, but switches to another buffer in another window."
@@ -4570,17 +4726,17 @@ Please, specify your level now: ")
        color-display-p frame-parameters
        minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
        varlist salutation window-config)
-    
+
     ;; If mode info is needed, add variable to `let' and then set it below,
     ;; like we did with color-display-p.
-    (setq color-display-p (if (viper-window-display-p) 
+    (setq color-display-p (if (viper-window-display-p)
                              (viper-color-display-p)
                            'non-x)
          minibuffer-vi-face (if (viper-has-face-support-p)
                                 (viper-get-face viper-minibuffer-vi-face)
                               'non-x)
          minibuffer-insert-face (if (viper-has-face-support-p)
-                                    (viper-get-face 
+                                    (viper-get-face
                                      viper-minibuffer-insert-face)
                                   'non-x)
          minibuffer-emacs-face (if (viper-has-face-support-p)
@@ -4589,27 +4745,27 @@ Please, specify your level now: ")
                                  'non-x)
          frame-parameters (if (fboundp 'frame-parameters)
                               (frame-parameters (selected-frame))))
-    
+
     (setq varlist (list 'viper-vi-minibuffer-minor-mode
                        'viper-insert-minibuffer-minor-mode
                        'viper-vi-intercept-minor-mode
-                       'viper-vi-local-user-minor-mode     
-                       'viper-vi-kbd-minor-mode                
+                       'viper-vi-local-user-minor-mode
+                       'viper-vi-kbd-minor-mode
                        'viper-vi-global-user-minor-mode
                        'viper-vi-state-modifier-minor-mode
-                       'viper-vi-diehard-minor-mode   
-                       'viper-vi-basic-minor-mode    
-                       'viper-replace-minor-mode         
+                       'viper-vi-diehard-minor-mode
+                       'viper-vi-basic-minor-mode
+                       'viper-replace-minor-mode
                        'viper-insert-intercept-minor-mode
-                       'viper-insert-local-user-minor-mode 
-                       'viper-insert-kbd-minor-mode            
+                       'viper-insert-local-user-minor-mode
+                       'viper-insert-kbd-minor-mode
                        'viper-insert-global-user-minor-mode
                        'viper-insert-state-modifier-minor-mode
-                       'viper-insert-diehard-minor-mode        
-                       'viper-insert-basic-minor-mode   
-                       'viper-emacs-intercept-minor-mode 
-                       'viper-emacs-local-user-minor-mode 
-                       'viper-emacs-kbd-minor-mode 
+                       'viper-insert-diehard-minor-mode
+                       'viper-insert-basic-minor-mode
+                       'viper-emacs-intercept-minor-mode
+                       'viper-emacs-local-user-minor-mode
+                       'viper-emacs-kbd-minor-mode
                        'viper-emacs-global-user-minor-mode
                        'viper-emacs-state-modifier-minor-mode
                        'viper-automatic-iso-accents
@@ -4656,28 +4812,28 @@ Before reporting a bug, please verify that it is related to Viper, and is
 not cause by other packages you are using.
 
 Don't report compilation warnings, unless you are certain that there is a
-problem. These warnings are normal and unavoidable.
+problem.  These warnings are normal and unavoidable.
 
 Please note that users should not modify variables and keymaps other than
-those advertised in the manual. Such `customization' is likely to crash
+those advertised in the manual.  Such `customization' is likely to crash
 Viper, as it would any other improperly customized Emacs package.
 
 If you are reporting an error message received while executing one of the
 Viper commands, type:
 
     M-x set-variable <Return> debug-on-error <Return> t <Return>
-       
-Then reproduce the error. The above command will cause Emacs to produce a
-back trace of the execution that leads to the error. Please include this
+
+Then reproduce the error.  The above command will cause Emacs to produce a
+back trace of the execution that leads to the error.  Please include this
 trace in your bug report.
 
 If you believe that one of Viper's commands goes into an infinite loop
 \(e.g., Emacs freezes\), type:
 
     M-x set-variable <Return> debug-on-quit <Return> t <Return>
-       
-Then reproduce the problem. Wait for a few seconds, then type C-g to abort
-the current command. Include the resulting back trace in the bug report.
+
+Then reproduce the problem.  Wait for a few seconds, then type C-g to abort
+the current command.  Include the resulting back trace in the bug report.
 
 Mail anyway (y or n)? ")
            (if (y-or-n-p "Mail anyway? ")
@@ -4687,24 +4843,23 @@ Mail anyway (y or n)? ")
 
          (require 'reporter)
          (set-window-configuration window-config)
-    
+
          (reporter-submit-bug-report "kifer@cs.sunysb.edu"
                                      (viper-version)
                                      varlist
                                      nil 'delete-other-windows
                                      salutation)
          ))
-                   
 
-    
-               
+
+
 ;; Smoothes out the difference between Emacs' unread-command-events
-;; and XEmacs unread-command-event. Arg is a character, an event, a list of
+;; and XEmacs unread-command-event.  Arg is a character, an event, a list of
 ;; events or a sequence of keys.
 ;;
 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
-;; into an event. Below, we delete nil from event lists, since nil is the most
+;; into an event.  Below, we delete nil from event lists, since nil is the most
 ;; common symbol that might appear in this wrong context.
 (defun viper-set-unread-command-events (arg)
   (if viper-emacs-p
@@ -4737,12 +4892,12 @@ Mail anyway (y or n)? ")
 ;; list is assumed to be a list of events of characters
 (defun viper-eventify-list-xemacs (lis)
   (mapcar
-   (function (lambda (elt)
-              (cond ((viper-characterp elt) (character-to-event elt))
-                    ((eventp elt)  elt)
-                    (t (error
-                        "viper-eventify-list-xemacs: can't convert to event, %S"
-                        elt)))))
+   (lambda (elt)
+     (cond ((viper-characterp elt) (character-to-event elt))
+          ((eventp elt)  elt)
+          (t (error
+              "viper-eventify-list-xemacs: can't convert to event, %S"
+              elt))))
    lis))