changeset 1176:60e0dc538df3

entered into RCS
author Richard M. Stallman <rms@gnu.org>
date Sun, 20 Sep 1992 02:22:09 +0000
parents c4535b4417d2
children 835b1e570621
files lisp/subr.el
diffstat 1 files changed, 57 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/subr.el	Sun Sep 20 00:41:38 1992 +0000
+++ b/lisp/subr.el	Sun Sep 20 02:22:09 1992 +0000
@@ -134,22 +134,65 @@
 ;      (copy-sequence keymap)
 ;      (copy-alist keymap)))
 
-(defun substitute-key-definition (olddef newdef keymap)
+(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.
-Prefix keymaps reached from KEYMAP are not checked recursively;
-perhaps they ought to be."
-  (if (arrayp keymap)
-      (let ((len (length keymap))
-	    (i 0))
-	(while (< i len)
-	  (if (eq (aref keymap i) olddef)
-	      (aset keymap i newdef))
-	  (setq i (1+ i))))
-    (while keymap
-      (if (eq (cdr-safe (car-safe keymap)) olddef)
-	  (setcdr (car keymap) newdef))
-      (setq keymap (cdr keymap)))))
+If optional fourth argument OLDMAP is specified, we redefine
+in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
+  (or prefix (setq prefix ""))
+  (let* ((scan (or oldmap keymap))
+	 (vec1 (vector nil))
+	 (prefix1 (vconcat prefix vec1)))
+    ;; Scan OLDMAP, finding each char or event-symbol that
+    ;; has any definition, and act on it with hack-key.
+    (while (consp scan)
+      (if (consp (car scan))
+	  (let ((char (car (car scan)))
+		(defn (cdr (car scan))))
+	    ;; The inside of this let duplicates exactly
+	    ;; the inside of the following let that handles array elements.
+	    (aset vec1 0 char)
+	    (aset prefix1 (length prefix) char)
+	    (let (inner-def)
+	      ;; Skip past menu-prompt.
+	      (while (stringp (car-safe defn))
+		(setq defn (cdr defn)))
+	      (setq inner-def defn)
+	      (while (and (symbolp inner-def)
+			  (fboundp inner-def))
+		(setq inner-def (symbol-function inner-def)))
+	      (if (eq defn olddef)
+		  (define-key keymap prefix1 newdef)
+		(if (keymapp defn)
+		    (substitute-key-definition olddef newdef keymap
+					       inner-def
+					       prefix1)))))
+	(if (arrayp (car scan))
+	    (let* ((array (car scan))
+		   (len (length array))
+		   (i 0))
+	      (while (< i len)
+		(let ((char i) (defn (aref array i)))
+		  ;; The inside of this let duplicates exactly
+		  ;; the inside of the previous let.
+		  (aset vec1 0 char)
+		  (aset prefix1 (length prefix) char)
+		  (let (inner-def)
+		    ;; Skip past menu-prompt.
+		    (while (stringp (car-safe defn))
+		      (setq defn (cdr defn)))
+		    (setq inner-def defn)
+		    (while (and (symbolp inner-def)
+				(fboundp inner-def))
+		      (setq inner-def (symbol-function inner-def)))
+		    (if (eq defn olddef)
+			(define-key keymap prefix1 newdef)
+		      (if (keymapp defn)
+			  (substitute-key-definition olddef newdef keymap
+						     inner-def
+						     prefix1)))))
+		(setq i (1+ i))))))
+      (setq scan (cdr scan)))))
 
 (defmacro save-match-data (&rest body)
   "Execute the BODY forms, restoring the global value of the match data."