all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kevin Rodgers <ihs_4664@yahoo.com>
Subject: Re: Getting functions into define-key...
Date: Mon, 29 Sep 2003 16:21:56 -0600	[thread overview]
Message-ID: <3F78B084.6020705@yahoo.com> (raw)
In-Reply-To: 87y8w7ycm2.fsf@nwalsh.com

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

      reply	other threads:[~2003-09-29 22:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-29 12:59 Getting functions into define-key Norman Walsh
2003-09-29 22:21 ` Kevin Rodgers [this message]

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

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

  git send-email \
    --in-reply-to=3F78B084.6020705@yahoo.com \
    --to=ihs_4664@yahoo.com \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.