Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting functions into define-key...

0 views
Skip to first unread message

Norman Walsh

unread,
Sep 29, 2003, 8:59:17 AM9/29/03
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm guessing there must be some way to do this other than brute force.

I have an alist:

(defvar my-alist
'(("choice1" . "opt1")
("choice2" . 35)
("choice3" . "opt3")))

I want to make a menu-bar menu that contains choice1, choice2, choice3.
If choice1 is selected, I want to evaluate (my-function "opt1"),
If choice2 is selected, I want to evaluate (my-function 35), etc.

I can see a brute-force solution:

(defun my-function-opt1 ()
(interactive)
(my-function "opt1"))

(define-key menu-bar-my-menu [my-choice1]
'("choice1" . my-function-opt1))

But it seems to me that it should be possible to build the menu bar
from the alist. Alas, it's just beyond my elisp skills.

Clues, please?

Be seeing you,
norm

- --
Norman Walsh <n...@nwalsh.com> | Design and programming are human
http://nwalsh.com/ | activities; forget that and all is
| lost.--B. Stroustrup
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQE/eCylOyltUcwYWjsRAuaLAKCE1rUhSBghvetIfOhSGMAgvrUjuACfR1MK
m4eG4w1CZC2jpPYB1kji4s0=
=pLtC
-----END PGP SIGNATURE-----

Kevin Rodgers

unread,
Sep 29, 2003, 6:21:56 PM9/29/03
to
Norman Walsh wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I'm guessing there must be some way to do this other than brute force.
>
> I have an alist:
>
> (defvar my-alist
> '(("choice1" . "opt1")
> ("choice2" . 35)
> ("choice3" . "opt3")))
>
> I want to make a menu-bar menu that contains choice1, choice2, choice3.
> If choice1 is selected, I want to evaluate (my-function "opt1"),
> If choice2 is selected, I want to evaluate (my-function 35), etc.
>
> I can see a brute-force solution:
>
> (defun my-function-opt1 ()
> (interactive)
> (my-function "opt1"))
>
> (define-key menu-bar-my-menu [my-choice1]
> '("choice1" . my-function-opt1))
>
> But it seems to me that it should be possible to build the menu bar
> from the alist. Alas, it's just beyond my elisp skills.

Here's how to handle ("choice1" . "opt1"):

(define-key menu-bar-my-menu [my-choice1]
'("choice1" . (lambda () (interactive) (my-function "opt1"))))

So given my-alist:

(let ((alist my-alist)
choice
option)
(while alist
(setq choice (car (car alist)) ; string
option (cadr (car alist))) ; constant (self-evaluating)
(define-key menu-bar-my-menu (vector (intern choice))
`(,choice . (lambda () (interactive) (my-function ,option))))
(setq alist (cdr alist))))

--
Kevin Rodgers

0 new messages