]> code.delx.au - gnu-emacs/blobdiff - lisp/emulation/viper-macs.el
Merge from trunk.
[gnu-emacs] / lisp / emulation / viper-macs.el
index b13c922188b3a2ee9bea09f74c61ca9ae0d2cb9d..0b96793deb794ee3f5aaf6b19b8e07f6341e6c3c 100644 (file)
@@ -1,15 +1,16 @@
 ;;; viper-macs.el --- functions implementing keyboard macros for Viper
 
-;; Copyright (C) 1994, 95, 96, 97, 2000, 01, 02 Free Software Foundation, Inc.
+;; Copyright (C) 1994-1997, 2000-2012 Free Software Foundation, Inc.
 
 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
+;; Package: viper
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -17,9 +18,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;; in order to spare non-viperized emacs from being viperized
 (if noninteractive
     (eval-when-compile
-      (let ((load-path (cons (expand-file-name ".") load-path)))
-       (or (featurep 'viper-util)
-           (load "viper-util.el" nil nil 'nosuffix))
-       (or (featurep 'viper-keym)
-           (load "viper-keym.el" nil nil 'nosuffix))
-       (or (featurep 'viper-mous)
-           (load "viper-mous.el" nil nil 'nosuffix))
-       (or (featurep 'viper-cmd)
-           (load "viper-cmd.el" nil nil 'nosuffix))
-       )))
+      (require 'viper-cmd)
+      ))
 ;; end pacifier
 
 (require 'viper-util)
@@ -117,7 +108,7 @@ a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
       (define-key viper-vi-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
       (define-key viper-insert-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
       (define-key viper-emacs-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
-      (message "Mapping %S in %s state.  Hit `C-x )' to complete the mapping"
+      (message "Mapping %S in %s state.  Type macro definition followed by `C-x )'"
               (viper-display-macro macro-name)
               (if ins "Insert" "Vi")))
     ))
@@ -169,7 +160,7 @@ a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
              ((stringp macro-name)
               (setq macro-name (vconcat macro-name)))
              (t (setq macro-name (vconcat (prin1-to-string macro-name)))))
-      (message ":map%s <Name>" variant)(sit-for 2)
+      (message ":map%s <Macro Name>" variant)(sit-for 2)
       (while
          (not (member key
                       '(?\C-m ?\n (control m) (control j) return linefeed)))
@@ -178,14 +169,14 @@ a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
        (if (member
             key
             '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
-           (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
+           (setq key-seq (viper-subseq key-seq 0 (- (length key-seq) 2))))
        (setq message
              (format
               ":map%s %s"
               variant (if (> (length key-seq) 0)
                           (prin1-to-string (viper-display-macro key-seq))
                         "")))
-       (message message)
+       (message "%s" message)
        (setq event (viper-read-key))
        ;;(setq event (viper-read-event))
        (setq key
@@ -242,9 +233,9 @@ a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
        (cond ((member
                key
                '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
-              (setq key-seq (subseq key-seq 0 (- (length key-seq) 2))))
+              (setq key-seq (viper-subseq key-seq 0 (- (length key-seq) 2))))
              ((member key '(tab (control i) ?\t))
-              (setq key-seq (subseq key-seq 0 (1- (length key-seq))))
+              (setq key-seq (viper-subseq key-seq 0 (1- (length key-seq))))
               (setq message
                     (format
                      ":unmap%s %s"
@@ -262,7 +253,7 @@ a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
                           (prin1-to-string
                            (viper-display-macro key-seq))
                         "")))
-       (message message)
+       (message "%s" message)
        (setq event (viper-read-key))
        ;;(setq event (viper-read-event))
        (setq key
@@ -328,7 +319,9 @@ a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
 
 ;; Accepts as macro names: strings and vectors.
 ;; strings must be strings of characters; vectors must be vectors of keys
-;; in canonic form.  The canonic form is essentially the form used in XEmacs
+;; in canonical form, which is essentially the form used in XEmacs.
+;; More general definitions are inherited by more specific scopes:
+;; global->major mode->buffer. More specific definitions override more general
 (defun viper-record-kbd-macro (macro-name state macro-body &optional scope)
   "Record a Vi macro.  Can be used in `.viper' file to define permanent macros.
 MACRO-NAME is a string of characters or a vector of keys.  STATE is
@@ -439,7 +432,7 @@ If SCOPE is nil, the user is asked to specify the scope."
                       scope)
               viper-custom-file-name))
 
-         (message msg)
+         (message "%s" msg)
          ))
 
     (setq new-elt
@@ -451,22 +444,22 @@ If SCOPE is nil, the user is asked to specify the scope."
                       (list (list (cons scope nil)) nil (cons t nil))))))
     (setq old-elt (assoc macro-name (eval macro-alist-var)))
 
-      (if (null old-elt)
-         (progn
-           ;; insert new-elt in macro-alist-var and keep the list sorted
-           (define-key
-             keymap
-             (vector (viper-key-to-emacs-key (aref macro-name 0)))
-             'viper-exec-mapped-kbd-macro)
-           (setq lis (eval macro-alist-var))
-           (while (and lis (string< (viper-array-to-string (car (car lis)))
-                                    (viper-array-to-string macro-name)))
-             (setq lis2 (cons (car lis) lis2))
-             (setq lis (cdr lis)))
-
-           (setq lis2 (reverse lis2))
-           (set macro-alist-var (append lis2 (cons new-elt lis)))
-           (setq old-elt new-elt)))
+    (if (null old-elt)
+       (progn
+         ;; insert new-elt in macro-alist-var and keep the list sorted
+         (define-key
+           keymap
+           (vector (viper-key-to-emacs-key (aref macro-name 0)))
+           'viper-exec-mapped-kbd-macro)
+         (setq lis (eval macro-alist-var))
+         (while (and lis (string< (viper-array-to-string (car (car lis)))
+                                  (viper-array-to-string macro-name)))
+           (setq lis2 (cons (car lis) lis2))
+           (setq lis (cdr lis)))
+
+         (setq lis2 (reverse lis2))
+         (set macro-alist-var (append lis2 (cons new-elt lis)))
+         (setq old-elt new-elt)))
     (setq old-sub-elt
          (cond ((eq scope t) (viper-kbd-global-pair old-elt))
                ((symbolp scope) (assoc scope (viper-kbd-mode-alist old-elt)))
@@ -484,6 +477,11 @@ If SCOPE is nil, the user is asked to specify the scope."
 
 
 ;; macro name must be a vector of viper-style keys
+;; viper-unrecord-kbd-macro doesn't have scope. Macro definitions are inherited
+;; from global -> major mode -> buffer
+;; More specific definition overrides more general
+;; Can't unrecord definition for more specific, if a more general definition is
+;; in effect
 (defun viper-unrecord-kbd-macro (macro-name state)
   "Delete macro MACRO-NAME from Viper STATE.
 MACRO-NAME must be a vector of viper-style keys.  This command is used by Viper
@@ -546,7 +544,7 @@ name from there."
           (setq macro-pair mode-mapping)
           (message "%S is unmapped for %s in %S"
                    (viper-display-macro macro-name) state-name major-mode))
-         ((cdr (setq macro-pair (viper-kbd-global-pair macro-entry)))
+         ((cdr (setq macro-pair global-mapping))
           (message
            "Global mapping for %S in %s is removed"
            (viper-display-macro macro-name) state-name))
@@ -560,7 +558,7 @@ name from there."
        (progn
          (set macro-alist-var (delq macro-entry (eval macro-alist-var)))
          (if (viper-can-release-key (aref macro-name 0)
-                                  (eval macro-alist-var))
+                                    (eval macro-alist-var))
              (define-key
                keymap
                (vector (viper-key-to-emacs-key (aref macro-name 0)))
@@ -611,7 +609,7 @@ name from there."
 
     (if (null macro-alist-elt)
        (setq macro-alist-elt (car next-best-match)
-             unmatched-suffix (subseq event-seq (cdr next-best-match))))
+             unmatched-suffix (viper-subseq event-seq (cdr next-best-match))))
 
     (cond ((null macro-alist-elt))
          ((setq macro-body (viper-kbd-buf-definition macro-alist-elt)))
@@ -650,9 +648,9 @@ name from there."
   (interactive)
   (with-output-to-temp-buffer " *viper-info*"
     (princ "Macros in Vi state:\n===================\n")
-    (mapcar 'viper-describe-one-macro viper-vi-kbd-macro-alist)
+    (mapc 'viper-describe-one-macro viper-vi-kbd-macro-alist)
     (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
-    (mapcar 'viper-describe-one-macro viper-insert-kbd-macro-alist)
+    (mapc 'viper-describe-one-macro viper-insert-kbd-macro-alist)
     (princ "\n\nMacros in Emacs state:\n======================\n")
     (mapcar 'viper-describe-one-macro viper-emacs-kbd-macro-alist)
     ))
@@ -662,11 +660,11 @@ name from there."
                 (viper-display-macro (car macro))))
   (princ "   ** Buffer-specific:")
   (if (viper-kbd-buf-alist macro)
-      (mapcar 'viper-describe-one-macro-elt (viper-kbd-buf-alist macro))
+      (mapc 'viper-describe-one-macro-elt (viper-kbd-buf-alist macro))
     (princ "  none\n"))
   (princ "\n   ** Mode-specific:")
   (if (viper-kbd-mode-alist macro)
-      (mapcar 'viper-describe-one-macro-elt (viper-kbd-mode-alist macro))
+      (mapc 'viper-describe-one-macro-elt (viper-kbd-mode-alist macro))
     (princ "  none\n"))
   (princ "\n   ** Global:")
   (if (viper-kbd-global-definition macro)
@@ -694,7 +692,7 @@ name from there."
   (let ((len1 (length seq1))
        (len2 (length seq2)))
     (if (<= len1 len2)
-       (equal seq1 (subseq seq2 0 len1)))))
+       (equal seq1 (viper-subseq seq2 0 len1)))))
 
 ;; find the longest common prefix
 (defun viper-common-seq-prefix (&rest seqs)
@@ -759,7 +757,7 @@ name from there."
       (setq macro-def (car lis)
            def-len (length (car macro-def)))
       (if (and (>= str-len def-len)
-              (equal (car macro-def) (subseq str 0 def-len)))
+              (equal (car macro-def) (viper-subseq str 0 def-len)))
          (if (or (viper-kbd-buf-definition macro-def)
                  (viper-kbd-mode-definition macro-def)
                  (viper-kbd-global-definition macro-def))
@@ -818,7 +816,7 @@ name from there."
 (defun viper-char-array-to-macro (array)
   (let ((vec (vconcat array))
        macro)
-    (if viper-xemacs-p
+    (if (featurep 'xemacs)
        (setq macro (mapcar 'character-to-event vec))
       (setq macro vec))
     (vconcat (mapcar 'viper-event-key macro))))
@@ -871,9 +869,13 @@ name from there."
   (let ((lis (vector event))
        next-event)
     (while (and (viper-fast-keysequence-p)
-               (viper-keyseq-is-a-possible-macro lis macro-alist))
-      (setq next-event (viper-read-key))
-      ;;(setq next-event (viper-read-event))
+           (viper-keyseq-is-a-possible-macro lis macro-alist))
+      ;; Seems that viper-read-event is more robust here. We need to be able to
+      ;; place these events on unread-command-events list. If we use
+      ;; viper-read-key then events will be converted to keys, and sometimes
+      ;; (e.g., (control \[)) those keys differ from the corresponding events.
+      ;; So, do not use (setq next-event (viper-read-key))
+      (setq next-event (viper-read-event))
       (or (viper-mouse-event-p next-event)
          (setq lis (vconcat lis (vector next-event)))))
     lis))
@@ -919,7 +921,7 @@ name from there."
 
 
 (defun viper-global-execute ()
-  "Call last keyboad macro for each line in the region."
+  "Call last keyboard macro for each line in the region."
   (if (> (point) (mark t)) (exchange-point-and-mark))
   (beginning-of-line)
   (call-last-kbd-macro)