From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: reading a variable from the user Date: Thu, 11 Oct 2012 11:27:41 +0800 Message-ID: <87y5jdn0wi.fsf@ericabrahamsen.net> References: <1349853875855-266778.post@n5.nabble.com> <1349917790397-266847.post@n5.nabble.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1349926090 19962 80.91.229.3 (11 Oct 2012 03:28:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 11 Oct 2012 03:28:10 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Oct 11 05:28:17 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TM9RW-0000aW-8C for geh-help-gnu-emacs@m.gmane.org; Thu, 11 Oct 2012 05:28:14 +0200 Original-Received: from localhost ([::1]:49941 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TM9RP-0007vL-U1 for geh-help-gnu-emacs@m.gmane.org; Wed, 10 Oct 2012 23:28:07 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:36425) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TM9RJ-0007v8-Rh for help-gnu-emacs@gnu.org; Wed, 10 Oct 2012 23:28:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TM9RI-0000Ff-HB for help-gnu-emacs@gnu.org; Wed, 10 Oct 2012 23:28:01 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:43385) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TM9RI-0000FU-AU for help-gnu-emacs@gnu.org; Wed, 10 Oct 2012 23:28:00 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1TM9RL-0000Ue-3m for help-gnu-emacs@gnu.org; Thu, 11 Oct 2012 05:28:03 +0200 Original-Received: from 50-56-99-223.static.cloud-ips.com ([50.56.99.223]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Oct 2012 05:28:03 +0200 Original-Received: from eric by 50-56-99-223.static.cloud-ips.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Oct 2012 05:28:03 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 74 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 50-56-99-223.static.cloud-ips.com X-Pgp-Key: http://pgp.mit.edu:11371/pks/lookup?op=vindex&search=0xC98BAE7B99D0D373 X-Pgp-Fingerprint: 8E19 28A9 2B51 0C67 565D DB34 C98B AE7B 99D0 D373 User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2.50 (gnu/linux) Cancel-Lock: sha1:KSfBkctoE3Dwc9Ub1jF8PqThxVc= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:87162 Archived-At: 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