emacs-devel
[Top][All Lists]
Advanced

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

Suggestion: Simple way to make conditional key bindings.


From: Kim F. Storm
Subject: Suggestion: Simple way to make conditional key bindings.
Date: 23 Aug 2002 14:05:13 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

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 <address@hidden> http://www.cua.dk





reply via email to

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