Path: utzoo!utgpu!watmath!uunet!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!apple!mips!wyse!td2cad!mipos3!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: gnu.emacs.bug Subject: Re: can a command know its invoking key sequence? Message-ID: <5207@omepd.UUCP> Date: 16 Nov 89 21:26:25 GMT References: <8911151740.AA16000@hummingbird.LCS.MIT.EDU> Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: gnu Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 41 In-reply-to: mic@THEORY.LCS.MIT.EDU In article <8911151740.AA16000@hummingbird.LCS.MIT.EDU>, mic@THEORY writes: | This is a help request. | | If a command is bound to several different key sequences, is there a way | for it to know which way it was called? The closest thing I've found is to | parse over the last hundred keystrokes reported by (recent-keys), saving | the last valid key sequence (there are pathological examples showing that | this method cannot always work correctly). I am hoping for a more | efficient and correct solution. (describe-function 'this-command-keys) ==> | this-command-keys: | Return string of the keystrokes that invoked this command. Here's some code I wrote last summer that uses it: ;;; original by merlyn -- LastEditDate="Tue May 16 12:45:11 1989" (defun rebind-this-key (key command) "Set KEY to invoke COMMAND, preferring a local binding if present. Interactively, KEY is the key that invoked this routine, while COMMAND is prompted for, making this function useful to bind to 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))) -- /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\ | on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/