help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

elisp newbie: simplifying from cl structures?


From: Tory S. Anderson
Subject: elisp newbie: simplifying from cl structures?
Date: Sat, 7 Feb 2015 17:39:35 -0500

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."
  '(
    ("address@hidden" "mail.mine.com" 26);; Personal
    ("address@hidden" "mail.mine3.com" 26);; Professional
    ("address@hidden" "mail.mine2.com" 26) ;; Web development
    ("address@hidden" "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 via email to

[Prev in Thread] Current Thread [Next in Thread]