[comp.emacs] deep-copy-keymap

liberte@B.CS.UIUC.EDU (Daniel LaLiberte) (10/28/87)

Here is a function to create a deep copy of a keymap.  As was noted earlier,
copy-keymap does a shallow copy of just the top level.  This does the
job recursively on all subkeymaps as well.  I've used it a bit, but it
is not thoroughly tested.  It is difficult to see if a copy is really different.

dan
----
(defun deep-copy-keymap (keymap)
  "Return a deep copy of KEYMAP.  That is, all levels are copied,
not just the top level."
  (if (not (keymapp keymap))
      keymap
    (cond

     ((listp keymap)
      (let ((new-keymap (copy-alist keymap)))
	(setq keymap (cdr new-keymap))
	(while keymap
	  (let ((binding (car keymap)))
	    (if (keymapp (cdr binding))
		(setcdr binding (deep-copy-keymap (cdr binding))))
	    )
	  (setq keymap (cdr keymap))
	  )
	new-keymap
	))

      ((vectorp keymap)
       (let ((i 0)
	     (n (length keymap))
	     (new-keymap (copy-sequence keymap)))
	 (while (< i n)
	   (if (keymapp (aref keymap i))
	       (aset new-keymap i (deep-copy-keymap (aref keymap i))))
	   (setq i (1+ i)))
	 new-keymap
	 )))))