From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: help-gnu-emacs@gnu.org
Subject: Re: reading a variable from the user
Date: Thu, 11 Oct 2012 11:27:41 +0800 [thread overview]
Message-ID: <87y5jdn0wi.fsf@ericabrahamsen.net> (raw)
In-Reply-To: 1349917790397-266847.post@n5.nabble.com
On Thu, Oct 11 2012, drain wrote:
> strings.el and simple+.el are new to me: thanks. They may contain the answer,
> and I need to pick through them carefully, but I'll just be forthright about
> what I am trying to do. Very new to Emacs Lisp...
>
> I want to prompt myself with "To:", enter a contact name and a topic, then
> pass them as arguments to compose-mail.
>
> This was no problem, but the goal right now is to cut out the redundant
> conditional statement, and instead match the name string I enter to its
> variable directly, for example "William" to the William variable. Might
> require pointers or arrays of some sort, but I haven't gotten that far in
> the Emacs Lisp Intro.
There are many, many ways to do this obviously (hash tables, BBDB, etc),
but for the purposes of learning elisp it might be best to start with
some kind of association list (alist, section 5.8 of the Elisp manual).
That would look like this:
(setq names '((william "xx@xxxxxxxxxxx.org")
(david "xxxxxxxxxxx@gmail.com")
(carl "xxxxxxxxxx@gmail.com")))
or like this:
(setq names '((william . "xx@xxxxxxxxxxx.org")
(david . "xxxxxxxxxxx@gmail.com")
(carl . "xxxxxxxxxx@gmail.com")))
The difference, as far as I know, is that in the first method you can
have multiple values per list (so something like (william "William"
"White" "xx@xxxxxx.org")), whereas the dotted cons cell notation (the
second one) only allows two atoms.
If you look at the association list section of the manual, you'll see a
bunch of functions for working with lists like these. The main one is
`assoc':
(assoc 'william names) -> (william "sdfsdf@sdfsdf")
(second (assoc 'william names)) -> "sdfsdf@sdfsdf"
It's generally better to use a `let' instead of a `setq' to make
temporary variables in your functions. Here I've used let*, which is
only different in that it evaluates the statements one by one, so later
statements can refer to values established in earlier statements.
As far as I can tell you can't use symbols in the assoc list set up by
`let' (is this wrong?). So your function might look like:
(defun custom-compose-mail (contact topic)
(interactive "sTo: \nsTopic: ")
(let* ((names '(("William" "xx@xxxxxxxxxxx.org")
("David" "xxxxxxxxxxx@gmail.com")
("Jason" "xxxxxxxxxx@gmail.com")
("Carl" "xxxxxx@gmail.com")))
(contact (second (assoc contact names))))
(compose-mail contact topic)
(end-of-buffer)
(newline)))
The next thing to do, for the sake of usability, would be to choose a
name using `completing-read', so that you could type a few letters and
use TAB to complete.
Hope that's useful,
Eric
--
GNU Emacs 24.2.50.1 (i686-pc-linux-gnu, GTK+ Version 3.4.4)
of 2012-10-10 on pellet
next prev parent reply other threads:[~2012-10-11 3:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-10 7:24 reading a variable from the user drain
2012-10-10 9:08 ` Francesco Mazzoli
2012-10-10 9:16 ` Tassilo Horn
2012-10-10 9:22 ` horse_rivers
2012-10-10 13:59 ` Doug Lewan
2012-10-10 14:13 ` Drew Adams
2012-10-11 1:09 ` drain
2012-10-11 3:27 ` Eric Abrahamsen [this message]
2012-10-11 6:08 ` PJ Weisberg
2012-10-11 6:50 ` Eric Abrahamsen
2012-10-11 6:16 ` drain
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=87y5jdn0wi.fsf@ericabrahamsen.net \
--to=eric@ericabrahamsen.net \
--cc=help-gnu-emacs@gnu.org \
/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.