From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: storm@cua.dk (Kim F. Storm) Newsgroups: gmane.emacs.devel Subject: Re: Suggestion: Simple way to make conditional key bindings. Date: 26 Aug 2002 01:33:12 +0200 Sender: emacs-devel-admin@gnu.org Message-ID: <5xptw6wm2v.fsf@kfs2.cua.dk> References: <3D49FF140074EFAE@mel-rta7.wanadoo.fr> <5xadnd3eck.fsf@kfs2.cua.dk> <200208231722.g7NHM0M02096@rum.cs.yale.edu> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1030314835 13918 127.0.0.1 (25 Aug 2002 22:33:55 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sun, 25 Aug 2002 22:33:55 +0000 (UTC) Cc: "David PONCE" , emacs-devel@gnu.org Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 17j5xB-0003cM-00 for ; Mon, 26 Aug 2002 00:33:53 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 17j6Ri-0008T0-00 for ; Mon, 26 Aug 2002 01:05:26 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 17j5yS-0000eN-00; Sun, 25 Aug 2002 18:35:12 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10) id 17j5wB-0000Uy-00 for emacs-devel@gnu.org; Sun, 25 Aug 2002 18:32:51 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10) id 17j5w8-0000Ul-00 for emacs-devel@gnu.org; Sun, 25 Aug 2002 18:32:50 -0400 Original-Received: from mail.filanet.dk ([195.215.206.179]) by monty-python.gnu.org with esmtp (Exim 4.10) id 17j5w8-0000Ug-00 for emacs-devel@gnu.org; Sun, 25 Aug 2002 18:32:48 -0400 Original-Received: from kfs2.cua.dk.cua.dk (unknown [10.1.82.3]) by mail.filanet.dk (Postfix) with SMTP id 042727C017; Sun, 25 Aug 2002 22:32:46 +0000 (GMT) Original-To: "Stefan Monnier" In-Reply-To: <200208231722.g7NHM0M02096@rum.cs.yale.edu> Original-Lines: 92 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 Errors-To: emacs-devel-admin@gnu.org X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.devel:6879 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:6879 "Stefan Monnier" writes: > > > > But IMO, the `menu-item' syntax is awful, and using a > > If the problem is only syntax, feel free to create an appropriate macro. > > > filter function is an added complexity which is pretty > > unflexible. > > What do you mean by `complexity' ? Using code is not "self-contained"; in contrast, key bindings made by one elisp package is typically independent of the key bindings made by other packages. So using code is more complex than key bindings. > You want to have a code. That's what functions are for. I think `eval' > should generally be avoided, and `funcall' used instead. > This is especially true if we care about lexical scoping. I don't quite follow, but I take your word for it :-) > > One problem with your change is "what binding do we use when we don't want > to run code?". The `menu-item' syntax provides a binding (in the example > above it's "my-filter" which is not very useful indeed) for the case > where code should not be evalled (for example in `where-is'). Who said we don't want to run code :-) Ok, I can see that could be a problem if we cache the result. But I guess we can just use the "default" binding, i.e. the last element (t ...) in the cond [and nil if no such element is present]. > > > It will then be quite trivial to enhance `define-key' to handle > > conditional bindings: > > But is it desirable ? Don't really know... It seems like a simple approach to allow packages to hook into "standard bindings". > > > (define-key global-map "\C-y" 'yank) ; this sets the default > > > > (define-key global-map "\C-y" 'yank-with-properties > > '(and kill-ring (table-recognize-table (car kill-ring)))) > > > > The second call would automatically changes the non-cond binding into > > a cond binding with the previous binding as default. > > Why not > > (define-key global-map "\C-y" 'yank-careful) > (defun yank-careful (...) > "Reinsert the last stretch of killed text, like `yank'. > Contrary to `yank' this function is careful to preserve some important > text properties when yanking tables." > ...) The point is that you can install a package - like table.el - which is then able to install its own conditional binding on C-y *without* interferring with (or even knowning) the standard binding. Suppose we have a conditional binding like this (define-key global-map "\C-y" 'yank-rectangle '(rectangle-p (car kill-ring))) to be able to insert rectangles from the kill-ring using C-y. Then table.el would still be able to install its own conditional binding on C-y. > > The advantage is that C-h k C-y doesn't just give you one of the two > bindings but a docstring that describes both. Of course we could also > improve C-h k to recognize your `cond' construct, etc... but is it > really worth the trouble ? I didn't think about that, but it would be a nice way to report such "multiple" bindings on a key... But you may also consider this as a different approach than using a minor-mode-keymap, and in that case, I think C-h k doesn't report all possible bindings for a key -- only the "currently active" binding, so why does `cond' have to behave differently? -- Kim F. Storm http://www.cua.dk