🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: gnu.emacs.sources
1 messages
1 total messages Started by Kevin Rodgers Thu, 07 Jan 1993 22:22
Emacs: recursive subsitute-key-definition
#311
Author: Kevin Rodgers
Date: Thu, 07 Jan 1993 22:22
56 lines
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!
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads