]> code.delx.au - gnu-emacs/blobdiff - lisp/emulation/viper-cmd.el
* lisp/simple.el (undo-amalgamate-change-group): New function
[gnu-emacs] / lisp / emulation / viper-cmd.el
index 359b3ff751d2d85829bac72f5e0c90c676f005f9..3ce1b4d6a75e31910412f307bba24f2289adffa3 100644 (file)
@@ -1,6 +1,6 @@
-;;; viper-cmd.el --- Vi command support for Viper
+;;; viper-cmd.el --- Vi command support for Viper  -*- lexical-binding:t -*-
 
-;; Copyright (C) 1997-201 Free Software Foundation, Inc.
+;; Copyright (C) 1997-2016 Free Software Foundation, Inc.
 
 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
 ;; Package: viper
 (defvar quail-mode)
 (defvar quail-current-str)
 (defvar mark-even-if-inactive)
-(defvar init-message)
+(defvar viper--init-message)
 (defvar viper-initial)
 (defvar undo-beg-posn)
 (defvar undo-end-posn)
 
 (eval-and-compile
-  (unless (fboundp 'declare-function) (defmacro declare-function (&rest  r))))
+  (unless (fboundp 'declare-function) (defmacro declare-function (&rest _))))
 ;; end pacifier
 
 
 
 ;; This also takes care of the annoying incomplete lines in files.
 ;; Also, this fixes `undo' to work vi-style for complex commands.
-(defun viper-change-state-to-vi ()
+(defun viper-change-state-to-vi (&rest _)
   "Change Viper state to Vi."
   (interactive)
   (if (and viper-first-time (not (viper-is-in-minibuffer)))
   )
 
 
-(defun viper-change-state-to-emacs ()
+(defun viper-change-state-to-emacs (&rest _)
   "Change Viper state to Emacs."
   (interactive)
   (or (viper-overlay-p viper-replace-overlay)
     (error
      (viper-message-conditions conds))))
 
-;; escape to emacs mode termporarily
+;; escape to emacs mode temporarily
 (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
@@ -726,7 +726,7 @@ EVENTS is a list of events, which become the beginning of the command."
       (message "Switched to EMACS state for the next command..."))
   (viper-escape-to-state arg events 'emacs-state))
 
-;; escape to Vi mode termporarily
+;; escape to Vi mode temporarily
 (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
@@ -961,11 +961,11 @@ Suffixes such as .el or .elc should be stripped."
 (defun viper-ESC (arg)
   "Emulate ESC key in Emacs.
 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.
+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 works
+a Meta key in this case.  When `viper-no-multiple-ESC' is nil, ESC works
 as a Meta key and any number of multiple escapes are allowed."
   (interactive "P")
   (let (char)
@@ -996,98 +996,15 @@ as a Meta key and any number of multiple escapes are allowed."
        (suspend-emacs))
     (viper-change-state-to-emacs)))
 
-\f
-;; Intercept ESC sequences on dumb terminals.
-;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
-
-;; Check if last key was ESC and if so try to reread it as a function key.
-;; But only if there are characters to read during a very short time.
-;; Returns the last event, if any.
-(defun viper-envelop-ESC-key ()
-  (let ((event last-input-event)
-       (keyseq [nil])
-       (inhibit-quit t))
-    (if (viper-ESC-event-p event)
-       (progn
-         ;; Some versions of Emacs (eg., 22.50.8 have a bug, which makes even
-         ;; a single ESC into ;; a fast keyseq. To guard against this, we
-         ;; added a check if there are other events as well. Keep the next
-         ;; line for the next time the bug reappears, so that will remember to
-         ;; report it.
-         ;;(if (and (viper-fast-keysequence-p) unread-command-events)
-         (if (viper-fast-keysequence-p) ;; for Emacsen without the above bug
-             (progn
-               (let (minor-mode-map-alist emulation-mode-map-alists)
-                 (viper-set-unread-command-events event)
-                 (setq keyseq (read-key-sequence nil 'continue-echo))
-                 ) ; let
-               ;; If keyseq translates into something that still has ESC
-               ;; at the beginning, separate ESC from the rest of the seq.
-               ;; In XEmacs we check for events that are keypress meta-key
-               ;; and convert them into [escape key]
-               ;;
-               ;; This is needed for the following reason:
-               ;; If ESC is the first symbol, we interpret it as if the
-               ;; user typed ESC and then quickly some other symbols.
-               ;; If ESC is not the first one, then the key sequence
-               ;; entered was apparently translated into a function key or
-               ;; something (e.g., one may have
-               ;; (define-key function-key-map "\e[192z" [f11])
-               ;; which would translate the escape-sequence generated by
-               ;; f11 in an xterm window into the symbolic key f11.
-               ;;
-               ;; 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
-               ;; 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 ((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 (viper-subseq keyseq 1))
-                        (setq last-input-event event
-                              keyseq (if (featurep 'emacs)
-                                         "\e"
-                                       (vector (character-to-event ?\e)))))
-                       ((and (featurep 'xemacs)
-                             (key-press-event-p first-key)
-                             (equal '(meta) key-mod))
-                        (viper-set-unread-command-events
-                         (vconcat (vector
-                                   (character-to-event (event-key first-key)))
-                                  (viper-subseq keyseq 1)))
-                        (setq last-input-event event
-                              keyseq (vector (character-to-event ?\e))))
-                       ((eventp first-key)
-                        (setq last-command-event
-                              (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)
-           (setq keyseq (read-key-sequence nil))
-           ))
-      ;; not an escape event
-      (setq keyseq (vector event)))
-    keyseq))
-
-
-
 ;; Listen to ESC key.
-;; If a sequence of keys starting with ESC is issued with very short delays,
-;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
 (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))
-                (lambda () (interactive) (error "Viper bell")))))
+  ;; `key-binding' needs to be called in a context where Viper's
+  ;; minor-mode map(s) have been temporarily disabled so the ESC
+  ;; binding to viper-intercept-ESC-key doesn't hide the binding we're
+  ;; looking for (Bug#9146):
+  (let* ((cmd 'viper-intercept-ESC-key))
 
     ;; 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
@@ -1377,7 +1294,7 @@ as a Meta key and any number of multiple escapes are allowed."
 ;; 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.
@@ -1399,7 +1316,7 @@ as a Meta key and any number of multiple escapes are allowed."
     (viper-change (mark t) (point))))
 
 ;; this is invoked by viper-substitute-line
-(defun viper-exec-Change (m-com com)
+(defun viper-exec-Change (_m-com _com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
@@ -1421,7 +1338,7 @@ as a Meta key and any number of multiple escapes are allowed."
     (viper-change-state-to-insert)
     ))
 
-(defun viper-exec-delete (m-com com)
+(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)))
   (let (chars-deleted)
@@ -1447,7 +1364,7 @@ as a Meta key and any number of multiple escapes are allowed."
     (if viper-ex-style-motion
        (if (and (eolp) (not (bolp))) (backward-char 1)))))
 
-(defun viper-exec-Delete (m-com com)
+(defun viper-exec-Delete (m-com _com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
@@ -1474,7 +1391,7 @@ as a Meta key and any number of multiple escapes are allowed."
     (back-to-indentation)))
 
 ;; save region
-(defun viper-exec-yank (m-com com)
+(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)
@@ -1498,7 +1415,7 @@ as a Meta key and any number of multiple escapes are allowed."
     (goto-char viper-com-point)))
 
 ;; save lines
-(defun viper-exec-Yank (m-com com)
+(defun viper-exec-Yank (_m-com _com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
@@ -1523,7 +1440,7 @@ as a Meta key and any number of multiple escapes are allowed."
   (viper-deactivate-mark)
   (goto-char viper-com-point))
 
-(defun viper-exec-bang (m-com com)
+(defun viper-exec-bang (_m-com com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
@@ -1539,16 +1456,16 @@ as a Meta key and any number of multiple escapes are allowed."
                (car viper-shell-history)
                ))
        viper-last-shell-com)
-     t)))
+     t t)))
 
-(defun viper-exec-equals (m-com com)
+(defun viper-exec-equals (_m-com _com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
     (if (> (mark t) (point)) (exchange-point-and-mark))
     (indent-region (mark t) (point) nil)))
 
-(defun viper-exec-shift (m-com com)
+(defun viper-exec-shift (_m-com com)
   (save-excursion
     (set-mark viper-com-point)
     (viper-enlarge-region (mark t) (point))
@@ -1562,10 +1479,10 @@ as a Meta key and any number of multiple escapes are allowed."
 
 ;; this is needed because some commands fake com by setting it to ?r, which
 ;; denotes repeated insert command.
-(defsubst viper-exec-dummy (m-com com)
+(defsubst viper-exec-dummy (_m-com _com)
   nil)
 
-(defun viper-exec-buffer-search (m-com com)
+(defun viper-exec-buffer-search (_m-com _com)
   (setq viper-s-string
        (regexp-quote (buffer-substring (point) viper-com-point)))
   (setq viper-s-forward t)
@@ -1599,7 +1516,7 @@ as a Meta key and any number of multiple escapes are allowed."
 ;; call viper-execute-com to execute viper-exec-change, which eventually will
 ;; call viper-change to invoke the replace mode on the region.
 ;;
-;; The var viper-d-com is set to (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS)
+;; The var viper-d-com is set to (M-COM VAL COM REG INSERTED-TEXT COMMAND-KEYS)
 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
 (defun viper-execute-com (m-com val com)
   (let ((reg viper-use-register))
@@ -1619,7 +1536,7 @@ as a Meta key and any number of multiple escapes are allowed."
 (defun viper-repeat (arg)
   "Re-execute last destructive command.
 Use the info in viper-d-com, which has the form
-\(com val ch reg inserted-text command-keys\),
+\(com val ch reg inserted-text command-keys),
 where `com' is the command to be re-executed, `val' is the
 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
 if it exists, it is the name of the register for `com'.
@@ -1731,7 +1648,7 @@ invokes the command before that, etc."
 (add-hook 'after-change-functions 'viper-undo-sentinel)
 
 ;; Hook used in viper-undo
-(defun viper-after-change-undo-hook (beg end len)
+(defun viper-after-change-undo-hook (beg end _len)
   (if (and (boundp 'undo-in-progress) undo-in-progress)
       (setq undo-beg-posn beg
            undo-end-posn (or end beg))
@@ -1745,8 +1662,7 @@ invokes the command before that, etc."
   "Undo previous change."
   (interactive)
   (message "undo!")
-  (let ((modified (buffer-modified-p))
-        (before-undo-pt (point-marker))
+  (let ((before-undo-pt (point-marker))
        undo-beg-posn undo-end-posn)
 
     ;; the viper-after-change-undo-hook removes itself after the 1st invocation
@@ -1793,36 +1709,20 @@ 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.
-(defun viper-adjust-undo ()
-  (if viper-undo-needs-adjustment
-      (let ((inhibit-quit t)
-           tmp tmp2)
-       (setq viper-undo-needs-adjustment nil)
-       (if (listp buffer-undo-list)
-           (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))
-                 ;; restore tail of buffer-undo-list
-                 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
-             (setq buffer-undo-list (delq nil buffer-undo-list)))))
-    ))
+(viper-deflocalvar viper--undo-change-group-handle nil)
+(put 'viper--undo-change-group-handle 'permanent-local t)
 
+(defun viper-adjust-undo ()
+  (when viper--undo-change-group-handle
+    (undo-amalgamate-change-group
+     (prog1 viper--undo-change-group-handle
+       (setq viper--undo-change-group-handle nil)))))
 
 (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
-                 (cons viper-buffer-undo-list-mark buffer-undo-list))
-           (setq viper-undo-needs-adjustment t)))))
+  (and (listp buffer-undo-list)
+       (not viper--undo-change-group-handle)
+       (setq viper--undo-change-group-handle
+             (prepare-change-group))))
 
 
 ;;; Viper's destructive Command ring utilities
@@ -1834,8 +1734,8 @@ invokes the command before that, etc."
 
     (setq this-command 'viper-display-current-destructive-command)
 
-    (message " `.' runs  %s%s"
-            (concat "`" (viper-array-to-string keys) "'")
+    (message " `.' runs  `%s'%s"
+            (viper-array-to-string keys)
             (viper-abbreviate-string
              (if (featurep 'xemacs)
                  (replace-in-string ; xemacs
@@ -1846,7 +1746,8 @@ invokes the command before that, etc."
                text ; emacs
                )
              max-text-len
-             "  inserting  `" "'" "    ......."))
+             (format-message "  inserting  `") (format-message "'")
+             "    ......."))
     ))
 
 
@@ -1979,21 +1880,22 @@ Undo previous insertion and inserts new."
 ;; Quote region by each line with a user supplied string.
 (defun viper-quote-region ()
   (let ((quote-str viper-quote-string)
-       (donot-change-dafault t))
+       (do-not-change-default t))
     (setq quote-str
          (viper-read-string-with-history
           "Quote string: "
           nil
           'viper-quote-region-history
+           ;; FIXME: Use comment-region.
           (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)
+                (t (setq do-not-change-default nil)
                    quote-str))))
-    (or donot-change-dafault
+    (or do-not-change-default
        (setq viper-quote-string quote-str))
     (viper-enlarge-region (point) (mark t))
     (if (> (point) (mark t)) (exchange-point-and-mark))
@@ -2066,13 +1968,13 @@ Undo previous insertion and inserts new."
     (funcall hook)
     ))
 
-;; This is a temp hook that uses free variables init-message and viper-initial.
+;; This is a temp hook that uses free variables viper--init-message and viper-initial.
 ;; A dirty feature, but it is the simplest way to have it do the right thing.
-;; The INIT-MESSAGE and VIPER-INITIAL vars come from the scope set by
+;; The VIPER--INIT-MESSAGE and VIPER-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 viper--init-message)
+      (viper-tmp-insert-at-eob viper--init-message))
   (when (stringp viper-initial)
     ;; don't wait if we have unread events or in kbd macro
     (or unread-command-events
@@ -2110,7 +2012,7 @@ Undo previous insertion and inserts new."
 (defcustom viper-smart-suffix-list
   '("" "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 `.'.
+  "List of suffixes that Viper tries to append to filenames ending with a `.'.
 This is useful when 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 `.'
@@ -2136,7 +2038,7 @@ To turn this feature off, set this variable to nil."
               (viper-minibuffer-real-start) (point-max)))
        found key cmd suff)
     (goto-char (point-max))
-    (if (and viper-smart-suffix-list (string-match "\\.$" file))
+    (if (and viper-smart-suffix-list (string-match "\\.\\'" file))
        (progn
          (while (and (not found) (< count len))
            (setq suff (nth count viper-smart-suffix-list)
@@ -2180,10 +2082,10 @@ problems."
 \f
 ;;; Reading string with history
 
-(defun viper-read-string-with-history (prompt &optional viper-initial
+(defun viper-read-string-with-history (prompt &optional initial
                                              history-var default keymap
                                              init-message)
-  ;; Read string, prompting with PROMPT and inserting the VIPER-INITIAL
+  ;; 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.
   ;; Default value is displayed until the user types something in the
@@ -2191,14 +2093,16 @@ problems."
   ;; 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
+  (let ((viper-initial initial)
+        (viper--init-message init-message)
+        (minibuffer-setup-hook
         ;; stolen from add-hook
         (let ((old
                (if (boundp 'minibuffer-setup-hook)
                    minibuffer-setup-hook
                  nil)))
           (cons
-           'viper-minibuffer-standard-hook
+           #'viper-minibuffer-standard-hook
            (if (or (not (listp old)) (eq (car old) 'lambda))
                (list old) old))))
        (val "")
@@ -2206,14 +2110,15 @@ problems."
        temp-msg)
 
     (setq keymap (or keymap minibuffer-local-map)
-         viper-initial (or viper-initial "")
+         initial (or initial "")
+         viper-initial initial
          temp-msg (if default
                       (format "(default %s) " default)
                     ""))
 
     (setq viper-incomplete-ex-cmd nil)
     (setq val (read-from-minibuffer prompt
-                                   (concat temp-msg viper-initial val padding)
+                                   (concat temp-msg initial val padding)
                                    keymap nil history-var))
     (setq minibuffer-setup-hook nil
          padding (viper-array-to-string (this-command-keys))
@@ -2407,7 +2312,7 @@ problems."
    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
+  ;; guard against a smarty who switched from R-replace to normal replace
   (remove-hook
    'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
   (if overwrite-mode (overwrite-mode -1))
@@ -2531,7 +2436,7 @@ problems."
    '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
+  ;; guard against a smarty who switched from R-replace to normal replace
   (remove-hook
    'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
   )
@@ -2914,7 +2819,7 @@ On reaching beginning of line, stop and signal error."
               (viper-looking-at-alphasep))))))
 
 
-(defun viper-end-of-word (arg &optional careful)
+(defun viper-end-of-word (arg &optional _careful)
   "Move point to end of current word."
   (interactive "P")
   (viper-leave-region-active)
@@ -3453,7 +3358,7 @@ controlled by the sign of prefix numeric value."
 (defun viper-adjust-window ()
   (let ((win-height (if (featurep 'xemacs)
                        (window-displayed-height)
-                     (1- (window-height)))) ; adjust for modeline
+                     (1- (window-height)))) ; adjust for mode line
        (pt (point))
        at-top-p at-bottom-p
        min-scroll direction)
@@ -3506,7 +3411,7 @@ controlled by the sign of prefix numeric value."
              ((re-search-backward "[][(){}]" beg-lim t))
              (t
               (error "No matching character on line"))))
-      (cond ((looking-at "[\(\[{]")
+      (cond ((looking-at "[([{]")
             (if com (viper-move-marker-locally 'viper-com-point (point)))
             (forward-sexp 1)
             (if com
@@ -3530,7 +3435,7 @@ controlled by the sign of prefix numeric value."
   (interactive)
   (setq viper-parse-sexp-ignore-comments
        (not viper-parse-sexp-ignore-comments))
-  (princ (format
+  (princ (format-message
          "From now on, `%%' will %signore parentheses inside comment fields"
          (if viper-parse-sexp-ignore-comments "" "NOT "))))
 
@@ -3715,31 +3620,33 @@ 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
-toggle case-fold-search and hitting `/' three times witth toggle regexp
+toggle case-fold-search and hitting `/' three times with toggle regexp
 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'? "
-                                     (if viper-case-fold-search
-                                         "case-insensitive" "case-sensitive")
-                                     (if viper-case-fold-search
-                                         "case-sensitive"
-                                       "case-insensitive")))))
+                   (y-or-n-p (format-message
+                               "Search style: `%s'.  Want `%s'? "
+                               (if viper-case-fold-search
+                                   "case-insensitive" "case-sensitive")
+                               (if viper-case-fold-search
+                                   "case-sensitive"
+                                 "case-insensitive")))))
           (setq viper-case-fold-search (null viper-case-fold-search))
           (if viper-case-fold-search
               (setq msg "Search becomes case-insensitive")
             (setq msg "Search becomes case-sensitive")))
          ((or (eq arg 2)
               (and (null arg)
-                   (y-or-n-p (format "Search style: '%s'.  Want '%s'? "
-                                     (if viper-re-search
-                                         "regexp-search" "vanilla-search")
-                                     (if viper-re-search
-                                         "vanilla-search"
-                                       "regexp-search")))))
+                   (y-or-n-p (format-message
+                               "Search style: `%s'.  Want `%s'? "
+                               (if viper-re-search
+                                   "regexp-search" "vanilla-search")
+                               (if viper-re-search
+                                   "vanilla-search"
+                                 "regexp-search")))))
           (setq viper-re-search (null viper-re-search))
           (if viper-re-search
               (setq msg "Search becomes regexp-style")
@@ -3748,17 +3655,14 @@ the Emacs binding of `/'."
           (setq msg "Search style remains unchanged")))
     (princ msg t)))
 
-(defun viper-set-searchstyle-toggling-macros (unset &optional major-mode)
+(defun viper-set-searchstyle-toggling-macros (unset &optional mode)
   "Set the macros for toggling the search style in Viper's vi-state.
 The macro that toggles case sensitivity is bound to `//', and the one that
 toggles regexp search is bound to `///'.
 With a prefix argument, this function unsets the macros.
-If MAJOR-MODE is set, set the macros only in that major mode."
+If MODE is set, set the macros only in that major mode."
   (interactive "P")
-  (let (scope)
-    (if (and major-mode (symbolp major-mode))
-       (setq scope major-mode)
-      (setq scope 't))
+  (let ((scope (if (and mode (symbolp mode)) mode t)))
     (or noninteractive
        (if (not unset)
            (progn
@@ -3767,14 +3671,14 @@ If MAJOR-MODE is set, set the macros only in that major mode."
               "//" 'vi-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]
               scope)
-             ;; toggle regexp/vanila search
+             ;; toggle regexp/vanilla search
              (viper-record-kbd-macro
               "///" 'vi-state
               [2 (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]
               scope)
-             ;; XEmacs has no called-interactively-p
-             ;; (if (called-interactively-p 'interactive)
-             (if (interactive-p)
+             (if (if (featurep 'xemacs)
+                     (interactive-p)
+                   (called-interactively-p 'interactive))
                  (message
                   "// and /// now toggle case-sensitivity and regexp search")))
          (viper-unrecord-kbd-macro "//" 'vi-state)
@@ -3797,10 +3701,9 @@ With a prefix argument, unsets the macro."
             "%%%" 'vi-state
             [(meta x) v i p e r - t o g g l e - p a r s e - s e x p - i g n o r e - c o m m e n t s return]
             't)
-           ;; XEmacs has no called-interactively-p. And interactive-p
-           ;; works fine here.
-           ;; (if (called-interactively-p 'interactive)
-           (if (interactive-p)
+           (if (if (featurep 'xemacs)
+                   (interactive-p)
+                 (called-interactively-p 'interactive))
                (message
                 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
        (viper-unrecord-kbd-macro "%%%" 'vi-state))))
@@ -3814,7 +3717,7 @@ With a prefix argument, this function unsets the macros.
 If the optional prefix argument is non-nil and specifies a valid major mode,
 this sets the macros only in the macros in that major mode.  Otherwise,
 the macros are set in the current major mode.
-\(When unsetting the macros, the second argument has no effect.\)"
+\(When unsetting the macros, the second argument has no effect.)"
   (interactive "P")
   (or noninteractive
       (if (not unset)
@@ -3824,15 +3727,14 @@ the macros are set in the current major mode.
             "//" '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]
             (or arg-majormode major-mode))
-           ;; toggle regexp/vanila search
+           ;; toggle regexp/vanilla search
            (viper-record-kbd-macro
             "///" 'emacs-state
             [2 (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))
-           ;; called-interactively-p does not work for
-           ;; XEmacs. interactive-p is ok here.
-           ;; (if (called-interactively-p 'interactive)
-           (if (interactive-p)
+           (if (if (featurep 'xemacs)
+                   (interactive-p)
+                 (called-interactively-p 'interactive))
                (message
                 "// and /// now toggle case-sensitivity and regexp search.")))
        (viper-unrecord-kbd-macro "//" 'emacs-state)
@@ -4017,7 +3919,7 @@ Null string will repeat previous search."
   (setq viper-prefix-commands
        (cons viper-buffer-search-char viper-prefix-commands)))
 
-;; This is a Viper wraper for isearch-forward.
+;; This is a Viper wrapper for isearch-forward.
 (defun viper-isearch-forward (arg)
   "Do incremental search forward."
   (interactive "P")
@@ -4025,7 +3927,7 @@ Null string will repeat previous search."
   (if (listp arg) (setq arg (car arg)))
   (viper-exec-form-in-emacs (list 'isearch-forward arg)))
 
-;; This is a Viper wraper for isearch-backward."
+;; This is a Viper wrapper for isearch-backward."
 (defun viper-isearch-backward (arg)
   "Do incremental search backward."
   (interactive "P")
@@ -4062,7 +3964,7 @@ Null string will repeat previous search."
   (let (buffer buffer-name)
     (setq buffer-name
          (funcall viper-read-buffer-function
-                  (format "Kill buffer \(%s\): "
+                  (format "Kill buffer (%s): "
                           (buffer-name (current-buffer)))))
     (setq buffer
          (if (null buffer-name)
@@ -4071,7 +3973,7 @@ Null string will repeat previous search."
     (if (null buffer) (error "`%s': No such buffer" buffer-name))
     (if (or (not (buffer-modified-p buffer))
            (y-or-n-p
-            (format
+            (format-message
              "Buffer `%s' is modified, are you sure you want to kill it? "
              buffer-name)))
        (kill-buffer buffer)
@@ -4282,6 +4184,11 @@ cursor move past the beginning of line."
        (t
         (backward-char 1))))
 
+(defun viper-del-forward-char-in-insert ()
+  "Delete 1 char forward if in insert or replace state."
+  (interactive)
+  ;; don't put on kill ring
+  (delete-char 1 nil))
 
 \f
 ;; join lines.
@@ -4397,7 +4304,7 @@ cursor move past the beginning of line."
 
 (defun viper-query-replace ()
   "Query replace.
-If a null string is suplied as the string to be replaced,
+If a null string is supplied as the string to be replaced,
 the query replace mode will toggle between string replace
 and regexp replace."
   (interactive)
@@ -4419,7 +4326,7 @@ and regexp replace."
          (query-replace-regexp
           str
           (viper-read-string-with-history
-           (format "Query replace regexp `%s' with: " str)
+           (format-message "Query replace regexp `%s' with: " str)
            nil  ; no initial
            'viper-replace1-history
            (car viper-replace1-history) ; default
@@ -4427,7 +4334,7 @@ and regexp replace."
        (query-replace
         str
         (viper-read-string-with-history
-         (format "Query replace `%s' with: " str)
+         (format-message "Query replace `%s' with: " str)
          nil  ; no initial
          'viper-replace1-history
          (car viper-replace1-history) ; default
@@ -4480,7 +4387,7 @@ and regexp replace."
 ;; etc.
 (defun viper-cycle-through-mark-ring ()
   "Visit previous locations on the mark ring.
-One can use `` and '' to temporarily jump 1 step back."
+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
@@ -4599,10 +4506,7 @@ One can use `` and '' to temporarily jump 1 step back."
 
 ;; Input Mode Indentation
 
-;; Returns t, if the string before point matches the regexp STR.
-(defsubst viper-looking-back (str)
-  (and (save-excursion (re-search-backward str nil t))
-       (= (point) (match-end 0))))
+(define-obsolete-function-alias 'viper-looking-back 'looking-back "24.4")
 
 
 (defun viper-forward-indent ()
@@ -4616,7 +4520,7 @@ One can use `` and '' to temporarily jump 1 step back."
   (interactive)
   (if viper-cted
       (let ((p (point)) (c (current-column)) bol (indent t))
-       (if (viper-looking-back "[0^]")
+       (if (looking-back "[0^]" (1- (point)))
            (progn
              (if (eq ?^ (preceding-char))
                  (setq viper-preserve-indent t))
@@ -4628,7 +4532,7 @@ One can use `` and '' to temporarily jump 1 step back."
        (delete-region (point) p)
        (if indent
            (indent-to (- c viper-shift-width)))
-       (if (or (bolp) (viper-looking-back "[^ \t]"))
+       (if (or (bolp) (looking-back "[^ \t]" (1- (point))))
            (setq viper-cted nil)))))
 
 ;; do smart indent
@@ -4668,7 +4572,7 @@ 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))
@@ -4685,7 +4589,7 @@ One can use `` and '' to temporarily jump 1 step back."
              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 ((viper= ?\[ reg)
@@ -4719,12 +4623,12 @@ One can use `` and '' to temporarily jump 1 step back."
                                          (substring text 0 (- pos s))
                                          reg (substring text (- pos s)))))
                     (princ
-                     (format
+                     (format-message
                       "Textmarker `%c' is in buffer `%s' at line %d.\n"
                                     reg (buffer-name buf) line-no))
                     (princ (format "Here is some text around %c:\n\n %s"
                                     reg text)))
-                (princ (format viper-EmptyTextmarker reg))))
+                (princ (format-message viper-EmptyTextmarker reg))))
             ))
          (t (error viper-InvalidTextmarker reg)))))
 
@@ -4865,10 +4769,10 @@ sensitive for VI-style look-and-feel."
          (setq repeated t))
        (setq dont-change-unless t
              level-changed t)
-       (insert "
+       (insert (substitute-command-keys "
 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'
+You can change it at any time by typing `\\[viper-set-expert-level]'
 
  1 -- BEGINNER: Almost all Emacs features are suppressed.
        Feels almost like straight Vi.  File name completion and
@@ -4886,7 +4790,7 @@ You can change it at any time by typing `M-x viper-set-expert-level RET'
        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.
 
-Please, specify your level now: ")
+Please, specify your level now: "))
 
        (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
        ) ; end while
@@ -4914,6 +4818,7 @@ Please, specify your level now: ")
   (beep 1))
 
 
+;; FIXME Use register-read-with-preview?
 ;; 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."
@@ -4943,40 +4848,43 @@ Please, specify your level now: ")
   (interactive)
   (if (< viper-expert-level 2)
       (save-buffers-kill-emacs)
-    (save-buffer)
+    (if (buffer-modified-p) (save-buffer))
     (kill-buffer (current-buffer))))
 
 
 \f
 ;;; Bug Report
 
+(defvar reporter-prompt-for-summary-p)
+
 (defun viper-submit-report ()
   "Submit bug report on Viper."
   (interactive)
+  (defvar viper-device-type)
+  (defvar viper-color-display-p)
+  (defvar viper-frame-parameters)
+  (defvar viper-minibuffer-emacs-face)
+  (defvar viper-minibuffer-vi-face)
+  (defvar viper-minibuffer-insert-face)
   (let ((reporter-prompt-for-summary-p t)
        (viper-device-type (viper-device-type))
-       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)
+       (viper-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-minibuffer-insert-face)
-                                  'non-x)
-         minibuffer-emacs-face (if (viper-has-face-support-p)
-                                   (viper-get-face
-                                    viper-minibuffer-emacs-face)
-                                 'non-x)
-         frame-parameters (if (fboundp 'frame-parameters)
-                              (frame-parameters (selected-frame))))
+                              'non-x))
+        (viper-frame-parameters (if (fboundp 'frame-parameters)
+                                    (frame-parameters (selected-frame))))
+       (viper-minibuffer-emacs-face (if (viper-has-face-support-p)
+                                         (viper-get-face
+                                          viper-minibuffer-emacs-face)
+                                       'non-x))
+        (viper-minibuffer-vi-face (if (viper-has-face-support-p)
+                                      (viper-get-face viper-minibuffer-vi-face)
+                                    'non-x))
+        (viper-minibuffer-insert-face (if (viper-has-face-support-p)
+                                          (viper-get-face
+                                           viper-minibuffer-insert-face)
+                                        'non-x))
+       varlist salutation window-config)
 
     (setq varlist (list 'viper-vi-minibuffer-minor-mode
                        'viper-insert-minibuffer-minor-mode
@@ -5021,11 +4929,11 @@ Please, specify your level now: ")
                        'viper-expert-level
                        'major-mode
                        'viper-device-type
-                       'color-display-p
-                       'frame-parameters
-                       'minibuffer-vi-face
-                       'minibuffer-insert-face
-                       'minibuffer-emacs-face
+                       'viper-color-display-p
+                       'viper-frame-parameters
+                       'viper-minibuffer-vi-face
+                       'viper-minibuffer-insert-face
+                       'viper-minibuffer-emacs-face
                        ))
          (setq salutation "
 Congratulations! You may have unearthed a bug in Viper!
@@ -5060,7 +4968,7 @@ 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:
+\(e.g., Emacs freezes), type:
 
     M-x set-variable <Return> debug-on-quit <Return> t <Return>
 
@@ -5076,12 +4984,12 @@ Mail anyway (y or n)? ")
          (require 'reporter)
          (set-window-configuration window-config)
 
-         (reporter-submit-bug-report "kifer@cs.stonybrook.edu"
-                                     (viper-version)
-                                     varlist
-                                     nil 'delete-other-windows
-                                     salutation)
-         ))
+         (reporter-submit-bug-report
+          "kifer@cs.stonybrook.edu, bug-gnu-emacs@gnu.org"
+          (viper-version)
+          varlist
+          nil 'delete-other-windows
+          salutation)))