all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Tory S. Anderson" <torys.anderson@gmail.com>
To: emacs list <help-gnu-emacs@gnu.org>
Subject: elisp newbie: simplifying from cl structures?
Date: Sat, 7 Feb 2015 17:39:35 -0500	[thread overview]
Message-ID: <CAFbKxvFE9CRqQ7pv8obVcX6hQSw80QJ2gfxjP1gsbXV2XZhA4w@mail.gmail.com> (raw)

This is a simple code question: I've got myself a little turned around and
Practical Common Lisp just isn't coming to my mind right now (if it would
apply anyway). Here's the situation: I've vastly simplified a set of
functions, no longer needing various variables and assuming things (like
the use of SSL). But I've gotten myself tangled up trying to simplify the
old functions; now I get errors about expecting cl-list functions. I'm sure
an experienced elisper will be able to set me straight with a glance. The
purpose of this function is to look at my "From:" line in an email, compare
it against a table, and use that to set the smtp variables that will be
needed to grab the right info from my .authinfo. Here's the original, which
I grabbed from someone's site[1] and have used successfully in older
versions of emacs for years (note: it's mostly just the control structure
that's confused me):

--8<---------------cut here---------------start------------->8---
;; (defun change-smtp ()
;;   "Change the SMTP server according to the current from line."
;;   (save-excursion
;;     (loop with from = (save-restriction
;;             (message-narrow-to-headers)
;;             (message-fetch-field "from"))
;;       for (auth-mech address . auth-spec) in smtp-accounts
;;       when (string-match address from) do (cond
;;                            ((memq auth-mech '(cram-md5 plain login))
;;                         (return (apply 'set-smtp (cons auth-mech
auth-spec))))
;;                            ((eql auth-mech 'ssl)
;;                         (return (apply 'set-smtp-ssl auth-spec)))
;;                            (t (error "Unrecognized SMTP auth. mechanism:
;; `%s'." auth-mech))) finally (error "Cannot infer SMTP information."))))
--8<---------------cut here---------------end--------------->8---

Here is the not-quite working (won't evaluate) version I'm trying to build,
now that I assume ssl and have dropped the variables in auth-spec:
--8<---------------cut here---------------start------------->8---
(defun change-smtp ()
  "Change the SMTP server according to the current from line."
  (save-excursion
    (loop with from = (save-restriction
            (message-narrow-to-headers)
            (message-fetch-field "from"))
      for (address server . port) in smtp-accounts
      if (string-match address from) (return (apply 'set-smtp server port
address)))
    (error "Cannot infer SMTP information.")))
--8<---------------cut here---------------end--------------->8---

And, to be complete, here is the data and function backing the problem
function above, although they should be fine:

--8<---------------cut here---------------start------------->8---
(defvar smtp-accounts
  "Accounts to be matched against outgoing messages' 'from' field. Assumes
SSL. Of format `%address' `%server' `%port'.
This will be used to match against the .authinfo file."
  '(
    ("me@mine.com" "mail.mine.com" 26);; Personal
    ("mail@me.com" "mail.mine3.com" 26);; Professional
    ("me2@mine.com" "mail.mine2.com" 26) ;; Web development
    ("mine@gmail.com" "smtp.gmail.com" 587) ;; Public
    ))

(defun set-smtp (server port user)
  "Set related SMTP variables for supplied parameters. String `user' will
not be evaluated."
  (setq smtpmail-smtp-server server smtpmail-smtp-service port)
  (message "Setting SMTP server to `%s:%s' for user `%s'."
       server port user))
--8<---------------cut here---------------end--------------->8---

A little help (with brief explanation) with the change-smtp function would
be much appreciated.

Footnotes:
[1]
http://www.mostlymaths.net/2010/12/emacs-30-day-challenge-using-gnus-to.html


             reply	other threads:[~2015-02-07 22:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-07 22:39 Tory S. Anderson [this message]
2015-02-07 23:16 ` elisp newbie: simplifying from cl structures? John Mastro
2015-02-07 23:26   ` John Mastro
     [not found]   ` <mailman.19493.1423351600.1147.help-gnu-emacs@gnu.org>
2015-02-08  0:37     ` Pascal J. Bourguignon
2015-02-08  3:33       ` Tory S. Anderson
     [not found]       ` <mailman.19507.1423366427.1147.help-gnu-emacs@gnu.org>
2015-02-08  3:37         ` Pascal J. Bourguignon

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=CAFbKxvFE9CRqQ7pv8obVcX6hQSw80QJ2gfxjP1gsbXV2XZhA4w@mail.gmail.com \
    --to=torys.anderson@gmail.com \
    --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.