unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: storm@cua.dk (Kim F. Storm)
Subject: Suggestion: Simple way to make conditional key bindings.
Date: 23 Aug 2002 14:05:13 +0200	[thread overview]
Message-ID: <5x4rdlbx1i.fsf@kfs2.cua.dk> (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

             reply	other threads:[~2002-08-23 12:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-23 12:05 Kim F. Storm [this message]
2002-08-26  0:36 ` Suggestion: Simple way to make conditional key bindings 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5x4rdlbx1i.fsf@kfs2.cua.dk \
    --to=storm@cua.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).