[gnu.emacs] another rebind-this-key

merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) (05/16/89)

In article <BBC.89May13182800@titan.rice.edu>, bbc@titan (Benjamin Chase) writes:
| (defun rebind-this-key ()
|   "Ask for a new binding for the key sequence that invoked this function.
| This function is a useful binding for unused or unallocated function keys."
|   (interactive)
|   (let ((tck (this-command-keys)))
|     (rebind-key global-map tck
|                 (command-execute
|                  '(lambda (func)
|                     (interactive "CFunction to bind to this key: ") func)))))

That 'command-execute' looked a little ugly.  Here's my hack:

(defun rebind-this-key (key command)
  "Set KEY to invoke COMMAND, preferring a local map if present.
Interactively, KEY is the key that invoked this routine, while
COMMAND is prompted for, making this function useful to bind unused
or unallocated keys."
  (interactive
   (list
    (this-command-keys)
    (read-command "Function to bind to this key: ")))
  (define-key
    (if (lookup-key (current-local-map) key)
	(current-local-map) (current-global-map))
    key command))

It's a standalone, and doesn't use your functions, but you can
hack it around to do so.

Enjoy.
-- 
***** PLEASE IGNORE THE ADDRESS IN THE HEADER *****
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095===\
{ <merlyn@agora.hf.intel.com> ...!uunet!agora.hf.intel.com!merlyn    }
\=Cute quote: "Welcome to Oregon... home of the California Raisins!"=/

merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) (05/17/89)

In article <BBC.89May13182800@titan.rice.edu>, bbc@titan (Benjamin Chase) writes:
| (defun rebind-this-key ()
|   "Ask for a new binding for the key sequence that invoked this function.
| This function is a useful binding for unused or unallocated function keys."
|   (interactive)
|   (let ((tck (this-command-keys)))
|     (rebind-key global-map tck
|                 (command-execute
|                  '(lambda (func)
|                     (interactive "CFunction to bind to this key: ") func)))))

That 'command-execute' looked a little ugly.  Here's my hack:

(defun rebind-this-key (key command)
  "Set KEY to invoke COMMAND, preferring a local map if present.
Interactively, KEY is the key that invoked this routine, while
COMMAND is prompted for, making this function useful to bind unused
or unallocated keys."
  (interactive
   (let ((tck (this-command-keys)))
     (list
      tck
      (read-command (format "%s is currently undefined; define as: "
			    (key-description tck))))))
  (let ((map (current-local-map)))
    (if map
	(let ((lk (lookup-key map key)))
	  (if (or (null lk) (numberp lk))
	      (setq map (current-global-map))))
      (setq map (current-global-map)))
    (define-key map key command)))

It's a standalone, and doesn't use your functions, but you can
hack it around to do so.

Enjoy.
-- 
***** PLEASE IGNORE THE ADDRESS IN THE HEADER *****
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095===\
{ <merlyn@agora.hf.intel.com> ...!uunet!agora.hf.intel.com!merlyn    }
\=Cute quote: "Welcome to Oregon... home of the California Raisins!"=/