[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Suggestion: Simple way to make conditional key bindings.

From: Stefan Monnier
Subject: Re: Suggestion: Simple way to make conditional key bindings.
Date: Fri, 23 Aug 2002 13:22:00 -0400

> > > (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 ?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]