From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: Re: Get a command by its keybinding and also respecting key translation Date: Wed, 15 Dec 2010 16:27:03 +0100 Message-ID: <201012151627.04199.tassilo@member.fsf.org> References: <871v5lpkk6.fsf@member.fsf.org> <201012150921.22238.tassilo@member.fsf.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1292427268 11324 80.91.229.12 (15 Dec 2010 15:34:28 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 15 Dec 2010 15:34:28 +0000 (UTC) Cc: Stefan Monnier To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Dec 15 16:34:22 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PStMu-0000ni-Fe for ged-emacs-devel@m.gmane.org; Wed, 15 Dec 2010 16:34:16 +0100 Original-Received: from localhost ([127.0.0.1]:50059 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PStG5-0005xk-Rp for ged-emacs-devel@m.gmane.org; Wed, 15 Dec 2010 10:27:13 -0500 Original-Received: from [140.186.70.92] (port=46341 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PStFz-0005xG-Tl for emacs-devel@gnu.org; Wed, 15 Dec 2010 10:27:09 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PStFy-0004ov-OV for emacs-devel@gnu.org; Wed, 15 Dec 2010 10:27:07 -0500 Original-Received: from deliver.uni-koblenz.de ([141.26.64.15]:2633) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PStFy-0004oG-Fy for emacs-devel@gnu.org; Wed, 15 Dec 2010 10:27:06 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by deliver.uni-koblenz.de (Postfix) with ESMTP id 5D4DA78016C0; Wed, 15 Dec 2010 16:27:05 +0100 (CET) Original-Received: from deliver.uni-koblenz.de ([127.0.0.1]) by localhost (deliver.uni-koblenz.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 13257-07; Wed, 15 Dec 2010 16:27:04 +0100 (CET) X-CHKRCPT: Envelopesender noch tassilo@member.fsf.org Original-Received: from thinkpad.localnet (tsdh.uni-koblenz.de [141.26.67.142]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by deliver.uni-koblenz.de (Postfix) with ESMTP id A54EB78016B8; Wed, 15 Dec 2010 16:27:04 +0100 (CET) User-Agent: KMail/1.13.5 (Linux/2.6.37-rc5+; KDE/4.5.4; x86_64; ; ) In-Reply-To: <201012150921.22238.tassilo@member.fsf.org> X-Virus-Scanned: amavisd-new at uni-koblenz.de X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:133718 Archived-At: On Wednesday 15 December 2010 09:21:21 Tassilo Horn wrote: Hi again! > This is cool, but sadly not equivalent, because I cannot add multiple > binding for one key in the same keymap. That was the whole intention > > In my approach with multiple TAB bindings in one keymap, the last one > defined was the real binding, but if its predicate didn't match, it > delegated to the former binding of TAB (which it had stolen), and if > all predicates of all context keys failed, it would call the original > command. > > So do I go to implement that behavior in a more standard way? Ok, now I've revamped my macro according to your example with some changes. A dispatching form has to be provided that returns the function to use, or nil if the original binding should be triggered. Additionally, I let-bind a variable default-command to that original binding in order to make it possible to trigger context sensitive behavior only after hitting a key twice. What do you think of that? --8<---------------cut here---------------start------------->8--- (defmacro define-context-key (keymap key dispatch) "Define KEY in KEYMAP to execute according to DISPATCH. DISPATCH is a form that is evaluated and should return the command to be executed. If DISPATCH returns nil, then the command normally bound to KEY will be executed. DISPATCH can access a dynamically set variable `default-command', which contains the command that would be executed, if DISPATCH returns nil. This makes it possible to make DISPATCH return some command only if a user wants to trigger it twice in a row. Example: (define-context-key hs-minor-mode-map (kbd \"TAB\") (cond ((and (not (hs-already-hidden-p)) (eq last-command default-command)) 'hs-hide-block) ((hs-already-hidden-p) 'hs-show-block))) This will make TAB show a hidden block. If the block is shown, then the first TAB will act as usual (e.g. indent the code), but the second TAB will hide the block." `(define-key ,keymap ,key (quote (menu-item "context-key" ignore :filter (lambda (&optional ignored) (let ((default-command (key-binding ,key t))) ,dispatch)))))) --8<---------------cut here---------------end--------------->8--- Bye, Tassilo