home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / emacs / sources / 922 < prev    next >
Encoding:
Text File  |  1993-01-07  |  2.9 KB  |  69 lines

  1. Newsgroups: gnu.emacs.sources
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!traffic.den.mmc.com!kevin
  3. From: kevin@traffic.den.mmc.com (Kevin Rodgers)
  4. Subject: Emacs: recursive subsitute-key-definition
  5. Message-ID: <9301072222.AA02485@traffic>
  6. Sender: daemon@cis.ohio-state.edu
  7. Reply-To: kevin@traffic.den.mmc.com
  8. Organization: Source only  Discussion and requests in gnu.emacs.help.
  9. Distribution: gnu
  10. Date: Thu, 7 Jan 1993 22:22:53 GMT
  11. Lines: 56
  12.  
  13.  
  14. The Emacs Lisp manual (1.03) 'Changing Key Bindings' node says this
  15. about substitute-key-definition:
  16.  
  17.      Prefix keymaps that appear within KEYMAP are not checked
  18.      recursively for keys bound to OLDDEF; they are not changed at all.
  19.      Perhaps it would be better to check nested keymaps recursively.
  20.  
  21. I had a desire to substitute key definitions recursively, so I rewrote
  22. substitute-key-definition from the Emacs (18.58) subr.el file.  It
  23. preserves the current interface and behavior, but adds an optional
  24. argument to control recursive substitution.  Since the diff is as large
  25. as the whole function, I'll just post the code rather than a patch.
  26. (The paragraph cited above from the Emacs Lisp manual should also be
  27. changed to reflect the added second paragraph of substitute-key-
  28. definition's documentation string, if you decide to use this.)
  29.  
  30.  
  31. (defun substitute-key-definition (olddef newdef keymap &optional recur)
  32.   "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
  33. In other words, OLDDEF is replaced with NEWDEF where ever it appears.
  34.  
  35. If the optional argument RECUR is non-nil, also recursively substitute
  36. NEWDEF for OLDEF in keymaps accessible from KEYMAP via a non-empty
  37. prefix key.  If RECUR is t, do so for all accessible keymaps; otherwise,
  38. it should be a predicate \(i.e. a function of one argument\) which is
  39. applied to each accessible keymap to determine \(according to whether
  40. the result is non-nil\) whether the substitution will be performed in it."
  41. (if (keymapp keymap)
  42.     (progn
  43.       (cond ((and (vectorp keymap) (= (length keymap) 128))
  44.          (let ((i 0))
  45.            (while (< i 128)
  46.          (if (eq (aref keymap i) olddef)
  47.              (aset keymap i newdef))
  48.          (setq i (1+ i)))))
  49.         ((and (consp keymap) (eq (car keymap) 'keymap))
  50.          (let ((key-defs (cdr keymap)))
  51.            (while key-defs
  52.          (if (eq (cdr-safe (car-safe key-defs)) olddef)
  53.              (setcdr (car key-defs) newdef))
  54.          (setq key-defs (cdr key-defs))))))
  55.       (if recur
  56.       (let ((sub-keymap-alist (cdr (accessible-keymaps keymap))))
  57.         (while sub-keymap-alist
  58.           (if (or (eq recur t)
  59.               (funcall recur (cdr (car sub-keymap-alist))))
  60.           (substitute-key-definition olddef newdef
  61.                          (cdr (car sub-keymap-alist))
  62.                          nil))
  63.           (setq sub-keymap-alist (cdr sub-keymap-alist))))))))
  64. --
  65. Kevin Rodgers                kevin@traffic.den.mmc.com
  66. Martin Marietta MS A16401        (303) 790-3971
  67. 116 Inverness Dr. East
  68. Englewood CO 80112 USA            GO BUFFS!
  69.