unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Suggestion: Simple way to make conditional key bindings.
@ 2002-08-23 12:05 Kim F. Storm
  2002-08-26  0:36 ` Richard Stallman
  0 siblings, 1 reply; 17+ messages in thread
From: Kim F. Storm @ 2002-08-23 12:05 UTC (permalink / raw)



Sometimes you would like a specific key binding to be controlled
by some global or local state, but you really don't want to mess
with minor-mode keymaps etc.

The following method - illustrated by example - is simple, yet very
powerful and flexible.

The basic idea is to interpret a '(cond ...) binding "the natural way":

(setq a nil b nil c nil)
(global-set-key [f11] 
		'(cond
		  (a delete-char)
		  (b find-file)
		  (c nil)
		  (t (keymap (?a . "abc") (?b . "bcd")))))
[f11 a] => "abc"
[f11 b] => "bcd"
[f11 c] => undefined

(setq c t)
[f11] => undefined

(setq b t)
[f11] => find-char

(setq a t)
[f11] => delete-char


Here is a patch [against a fairly old keymap.c] to implement this feature.
The simplicity of the approach is proven by the size of the patch :-)


One use would be to bind C-y to `yank' except when the first element
in the kill-ring is a table, in which case we want to run
`yank-with-properties':


(global-set-key "\C-y"
        '(cond
          ((and kill-ring (table-recognize-table (car kill-ring)))
            yank-with-properties)
          (t yank)))


One advantage (IMO) is that C-h k C-y will report yank or
yank-with-properties depending on the actual function taken if
you hit C-y.

WDYT ?



Index: keymap.c
===================================================================
RCS file: /cvs/emacs/src/keymap.c,v
retrieving revision 1.260
diff -c -r1.260 keymap.c
*** keymap.c	10 May 2002 23:57:14 -0000	1.260
--- keymap.c	13 May 2002 10:08:23 -0000
***************
*** 93,98 ****
--- 93,99 ----
  Lisp_Object Vdefine_key_rebound_commands;
  
  Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item, Qremap;
+ Lisp_Object Qcond;
  
  /* Alist of elements like (DEL . "\d").  */
  static Lisp_Object exclude_keys;
***************
*** 690,695 ****
--- 691,718 ----
  	    return object;
  	}
  
+       /* If the keymap contents looks like (cond (COND DEFN)...)
+ 	 then find first non-nil COND and use its DEFN.*/
+ 
+       else if (EQ (XCAR (object), Qcond))
+ 	{
+ 	  register Lisp_Object args = XCDR (object);
+ 	  object = Qnil;
+ 	  for ( ; CONSP (args); args = XCDR (args))
+ 	    {
+ 	      register Lisp_Object clause, val;
+ 	      clause = XCAR (args);
+ 	      if (!CONSP (clause) || !CONSP (XCDR (clause)))
+ 		continue;
+ 	      val = menu_item_eval_property (XCAR (clause));
+ 	      if (!NILP (val))
+ 		{
+ 		  object = XCAR (XCDR (clause));
+ 		  break;
+ 		}
+ 	    }
+ 	}
+ 
        /* If the keymap contents looks like (STRING . DEFN), use DEFN.
  	 Keymap alist elements like (CHAR MENUSTRING . DEFN)
  	 will be used by HierarKey menus.  */
***************
*** 3650,3655 ****
--- 3673,3681 ----
  
    Qremap = intern ("remap");
    staticpro (&Qremap);
+ 
+   Qcond = intern ("cond");
+   staticpro (&Qcond);
  
    remap_command_vector = Fmake_vector (make_number (2), Qremap);
    staticpro (&remap_command_vector);





-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

^ permalink raw reply	[flat|nested] 17+ messages in thread
* Re: Suggestion: Simple way to make conditional key bindings.
@ 2002-08-23 12:52 David PONCE
  0 siblings, 0 replies; 17+ messages in thread
From: David PONCE @ 2002-08-23 12:52 UTC (permalink / raw)
  Cc: emacs-devel

Hi Kim,

> Sometimes you would like a specific key binding to be controlled
> by some global or local state, but you really don't want to mess
> with minor-mode keymaps etc.
>
[...]
>
> One use would be to bind C-y to `yank' except when the first element
> in the kill-ring is a table, in which case we want to run
> `yank-with-properties':
>
>
> (global-set-key "\C-y"
>         '(cond
>           ((and kill-ring (table-recognize-table (car kill-ring)))
>             yank-with-properties)
>           (t yank)))
>
>
> One advantage (IMO) is that C-h k C-y will report yank or
> yank-with-properties depending on the actual function taken if
> you hit C-y.
>
> WDYT ?

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?

Sincerely,
David

^ permalink raw reply	[flat|nested] 17+ messages in thread
[parent not found: <3D49FF140074EFAE@mel-rta7.wanadoo.fr>]

end of thread, other threads:[~2002-08-30 10:27 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-23 12:05 Suggestion: Simple way to make conditional key bindings Kim F. Storm
2002-08-26  0:36 ` Richard Stallman
2002-08-26 16:21   ` Stefan Monnier
2002-08-30 10:27     ` Robert J. Chassell
  -- strict thread matches above, loose matches on Subject: below --
2002-08-23 12:52 David PONCE
     [not found] <3D49FF140074EFAE@mel-rta7.wanadoo.fr>
2002-08-23 13:16 ` Kim F. Storm
2002-08-23 17:22   ` Stefan Monnier
2002-08-25 23:33     ` Kim F. Storm
2002-08-26 15:47       ` Stefan Monnier
2002-08-26 19:33         ` Miles Bader
2002-08-27 19:05           ` Richard Stallman
2002-08-27 22:58             ` Kim F. Storm
2002-08-28 23:32               ` Richard Stallman
2002-08-29  8:54                 ` Kim F. Storm
2002-08-28  1:00             ` Miles Bader
2002-08-28  1:22               ` Stefan Monnier
2002-08-27 11:23         ` Kim F. Storm

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).