Article View: gnu.emacs.sources
Article #311Emacs: recursive subsitute-key-definition
From: Kevin Rodgers
Date: Thu, 07 Jan 1993 22:22
Date: Thu, 07 Jan 1993 22:22
56 lines
2989 bytes
2989 bytes
The Emacs Lisp manual (1.03) 'Changing Key Bindings' node says this
about substitute-key-definition:
Prefix keymaps that appear within KEYMAP are not checked
recursively for keys bound to OLDDEF; they are not changed at all.
Perhaps it would be better to check nested keymaps recursively.
I had a desire to substitute key definitions recursively, so I rewrote
substitute-key-definition from the Emacs (18.58) subr.el file. It
preserves the current interface and behavior, but adds an optional
argument to control recursive substitution. Since the diff is as large
as the whole function, I'll just post the code rather than a patch.
(The paragraph cited above from the Emacs Lisp manual should also be
changed to reflect the added second paragraph of substitute-key-
definition's documentation string, if you decide to use this.)
(defun substitute-key-definition (olddef newdef keymap &optional recur)
"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.
If the optional argument RECUR is non-nil, also recursively substitute
NEWDEF for OLDEF in keymaps accessible from KEYMAP via a non-empty
prefix key. If RECUR is t, do so for all accessible keymaps; otherwise,
it should be a predicate \(i.e. a function of one argument\) which is
applied to each accessible keymap to determine \(according to whether
the result is non-nil\) whether the substitution will be performed in it."
(if (keymapp keymap)
(progn
(cond ((and (vectorp keymap) (= (length keymap) 128))
(let ((i 0))
(while (< i 128)
(if (eq (aref keymap i) olddef)
(aset keymap i newdef))
(setq i (1+ i)))))
((and (consp keymap) (eq (car keymap) 'keymap))
(let ((key-defs (cdr keymap)))
(while key-defs
(if (eq (cdr-safe (car-safe key-defs)) olddef)
(setcdr (car key-defs) newdef))
(setq key-defs (cdr key-defs))))))
(if recur
(let ((sub-keymap-alist (cdr (accessible-keymaps keymap))))
(while sub-keymap-alist
(if (or (eq recur t)
(funcall recur (cdr (car sub-keymap-alist))))
(substitute-key-definition olddef newdef
(cdr (car sub-keymap-alist))
nil))
(setq sub-keymap-alist (cdr sub-keymap-alist))))))))
--
Kevin Rodgers kevin@traffic.den.mmc.com
Martin Marietta MS A16401 (303) 790-3971
116 Inverness Dr. East
Englewood CO 80112 USA GO BUFFS!
Technical Details
Message-ID:
<9301072222.AA02485@traffic>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!traffic.den.mmc.com!kevinShow Full Headers
Complete Article Headers
Newsgroups: gnu.emacs.sources Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!traffic.den.mmc.com!kevin From: kevin@traffic.den.mmc.com (Kevin Rodgers) Subject: Emacs: recursive subsitute-key-definition Message-ID: <9301072222.AA02485@traffic> Sender: daemon@cis.ohio-state.edu Reply-To: kevin@traffic.den.mmc.com Organization: Source only Discussion and requests in gnu.emacs.help. Distribution: gnu Date: Thu, 7 Jan 1993 22:22:53 GMT Lines: 56