unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* elisp newbie: simplifying from cl structures?
@ 2015-02-07 22:39 Tory S. Anderson
  2015-02-07 23:16 ` John Mastro
  0 siblings, 1 reply; 6+ messages in thread
From: Tory S. Anderson @ 2015-02-07 22:39 UTC (permalink / raw)
  To: emacs list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-02-08  3:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-07 22:39 elisp newbie: simplifying from cl structures? Tory S. Anderson
2015-02-07 23:16 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).