emacs-pretest-bug
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: xterm.el


From: Stefan Monnier
Subject: Re: xterm.el
Date: Fri, 29 Apr 2005 12:30:51 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (darwin)

>>>>> "Nick" == Nick Roberts <address@hidden> writes:

> xterm.el makes Emacs take about seven times as long to load for me (about 14
> seconds on my 200MHz PC). I have tested this by setting term-file-prefix to
> nil as described in startup.el

> I think this is due to the recent changes in xterm.el e.g
> substitute-key-definition is invoked 48 times.

We could speed this up significantly by doing a single traversal of the
keymap, doing the 48 substitutions simultaneously.
The patch below defines a new function substitute-key-definitions which
xterm.el could use to good advantage.
The interest of this new function is that it is a *simultaneous*
substitution, so it can be used to swap FOO and BAR, whereas doing it with
substitute-key-definition requires substituting TOTO for FOO, than FOO for
BAR and then BAR for TOTO.


        Stefan


--- orig/lisp/subr.el
+++ mod/lisp/subr.el
@@ -363,14 +363,10 @@
 (defvar key-substitution-in-progress nil
  "Used internally by substitute-key-definition.")
 
-(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
-  "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
-In other words, OLDDEF is replaced with NEWDEF where ever it appears.
-Alternatively, if optional fourth argument OLDMAP is specified, we redefine
-in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP.
-
-For most uses, it is simpler and safer to use command remappping like this:
-  \(define-key KEYMAP [remap OLDDEF] NEWDEF)"
+(defun substitute-key-definitions (subst keymap &optional oldmap prefix)
+  "Applies the SUBST remapping to key bindings in KEYMAP.
+SUBST will be a list of elements of the form (OLDDEF . NEWDEF).
+See `substitue-key-definition'."
   ;; Don't document PREFIX in the doc string because we don't want to
   ;; advertise it.  It's meant for recursive calls only.  Here's its
   ;; meaning
@@ -388,11 +384,28 @@
     (map-keymap
      (lambda (char defn)
        (aset prefix1 (length prefix) char)
-       (substitute-key-definition-key defn olddef newdef prefix1 keymap))
+       (substitute-key-definitions-key defn subst prefix1 keymap))
      scan)))
 
-(defun substitute-key-definition-key (defn olddef newdef prefix keymap)
-  (let (inner-def skipped menu-item)
+(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
+  "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
+In other words, OLDDEF is replaced with NEWDEF where ever it appears.
+Alternatively, if optional fourth argument OLDMAP is specified, we redefine
+in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP.
+
+For most uses, it is simpler and safer to use command remappping like this:
+  \(define-key KEYMAP [remap OLDDEF] NEWDEF)"
+  ;; Don't document PREFIX in the doc string because we don't want to
+  ;; advertise it.  It's meant for recursive calls only.  Here's its
+  ;; meaning
+
+  ;; If optional argument PREFIX is specified, it should be a key
+  ;; prefix, a string.  Redefined bindings will then be bound to the
+  ;; original key, with PREFIX added at the front.
+  (substitute-key-definitions (list (cons olddef newdef)) keymap oldmap 
prefix))
+
+(defun substitute-key-definitions-key (defn subst prefix keymap)
+  (let (inner-def skipped menu-item mapping)
     ;; Find the actual command name within the binding.
     (if (eq (car-safe defn) 'menu-item)
        (setq menu-item defn defn (nth 2 defn))
@@ -402,17 +415,17 @@
       ;; Skip past cached key-equivalence data for menu items.
       (if (consp (car-safe defn))
          (setq defn (cdr defn))))
-    (if (or (eq defn olddef)
+    (if (or (setq mapping (assq defn subst))
            ;; Compare with equal if definition is a key sequence.
            ;; That is useful for operating on function-key-map.
            (and (or (stringp defn) (vectorp defn))
-                (equal defn olddef)))
+                (setq mapping (assoc defn subst))))
        (define-key keymap prefix
          (if menu-item
              (let ((copy (copy-sequence menu-item)))
-               (setcar (nthcdr 2 copy) newdef)
+               (setcar (nthcdr 2 copy) (cdr mapping))
                copy)
-           (nconc (nreverse skipped) newdef)))
+           (nconc (nreverse skipped) (cdr mapping))))
       ;; Look past a symbol that names a keymap.
       (setq inner-def
            (and defn
@@ -428,7 +441,7 @@
               ;; Avoid recursively rescanning keymap being scanned.
               (not (memq inner-def key-substitution-in-progress)))
          ;; If this one isn't being scanned already, scan it now.
-         (substitute-key-definition olddef newdef keymap inner-def prefix)))))
+         (substitute-key-definitions subst keymap inner-def prefix)))))
 
 (defun define-key-after (keymap key definition &optional after)
   "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]