From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Stefan Monnier" Newsgroups: gmane.emacs.devel Subject: Re: Suggestion: Simple way to make conditional key bindings. Date: Fri, 23 Aug 2002 13:22:00 -0400 Sender: emacs-devel-admin@gnu.org Message-ID: <200208231722.g7NHM0M02096@rum.cs.yale.edu> References: <3D49FF140074EFAE@mel-rta7.wanadoo.fr> <5xadnd3eck.fsf@kfs2.cua.dk> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1030123395 26077 127.0.0.1 (23 Aug 2002 17:23:15 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 23 Aug 2002 17:23:15 +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 17iI9R-0006mU-00 for ; Fri, 23 Aug 2002 19:23:13 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 17iIct-0000Ll-00 for ; Fri, 23 Aug 2002 19:53:40 +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 17iIAe-00077w-00; Fri, 23 Aug 2002 13:24:28 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10) id 17iI8L-000754-00 for emacs-devel@gnu.org; Fri, 23 Aug 2002 13:22:05 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10) id 17iI8I-00074j-00 for emacs-devel@gnu.org; Fri, 23 Aug 2002 13:22:04 -0400 Original-Received: from rum.cs.yale.edu ([128.36.229.169]) by monty-python.gnu.org with esmtp (Exim 4.10) id 17iI8I-00074e-00 for emacs-devel@gnu.org; Fri, 23 Aug 2002 13:22:02 -0400 Original-Received: (from monnier@localhost) by rum.cs.yale.edu (8.11.6/8.11.6) id g7NHM0M02096; Fri, 23 Aug 2002 13:22:00 -0400 X-Mailer: exmh version 2.4 06/23/2000 with nmh-1.0.4 Original-To: storm@cua.dk (Kim F. Storm) 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:6806 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:6806 > > > (global-set-key "\C-y" > > > '(cond > > > ((and kill-ring (table-recognize-table (car kill-ring))) > > > yank-with-properties) > > > (t yank))) > > > > > > > > > > It seems that you can do something similar this way: > > > > (global-set-key "\C-y" '(menu-item "my-filter" :filter my-filter)) > > > > (defun my-filter (&rest ignore) > > (cond > > ((and kill-ring (table-recognize-table (car kill-ring))) > > 'yank-with-properties) > > (t 'yank))) > > > > Am I wrong? > > Not at all [that's more or less how it is implemented]. > > 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' ? 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. 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'). > It will then be quite trivial to enhance `define-key' to handle > conditional bindings: But is it desirable ? > (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 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 ? Stefan