* 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
* Re: elisp newbie: simplifying from cl structures?
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>
0 siblings, 2 replies; 6+ messages in thread
From: John Mastro @ 2015-02-07 23:16 UTC (permalink / raw)
To: Tory S. Anderson; +Cc: emacs list
Tory S. Anderson <torys.anderson@gmail.com> wrote:
> (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.")))
Try this (untested, sorry):
(defun change-smtp ()
"Change the SMTP server according to the current from line."
(save-excursion
(cl-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 (funcall 'set-smtp server port address))
(error "Cannot infer SMTP information.")))
I think there were three issues:
1. return is a cl-loop keyword, so it shouldn't be in parenthesis
as if it were a function. That's the error you were getting.
2. The use of apply needed to change to funcall
3. And then it made sense to change from `for (address server . port)'
to `for (address server port)'. The difference (given the shape of
smtp-accounts) is that, in the former case, port will be a list
containing a single integer, whereas in the latter case it will be an
integer.
I also changed `loop' to `cl-loop', as is now preferred.
--
john
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: elisp newbie: simplifying from cl structures?
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>
1 sibling, 0 replies; 6+ messages in thread
From: John Mastro @ 2015-02-07 23:26 UTC (permalink / raw)
To: Tory S. Anderson; +Cc: emacs list
> (defun change-smtp ()
> "Change the SMTP server according to the current from line."
> (save-excursion
> (cl-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 (funcall 'set-smtp server port address))
> (error "Cannot infer SMTP information.")))
Second try. The return won't escape the `error' in the above.
(defun change-smtp ()
"Change the SMTP server according to the current from line."
(save-excursion
(cl-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 (funcall 'set-smtp server port address)
finally (error "Cannot infer SMTP information."))))
--
john
^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <mailman.19493.1423351600.1147.help-gnu-emacs@gnu.org>]
* Re: elisp newbie: simplifying from cl structures?
[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>
0 siblings, 2 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2015-02-08 0:37 UTC (permalink / raw)
To: help-gnu-emacs
John Mastro <john.b.mastro@gmail.com> writes:
>> (defun change-smtp ()
>> "Change the SMTP server according to the current from line."
>> (save-excursion
>> (cl-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 (funcall 'set-smtp server port address))
>> (error "Cannot infer SMTP information.")))
>
> Second try. The return won't escape the `error' in the above.
>
> (defun change-smtp ()
> "Change the SMTP server according to the current from line."
> (save-excursion
> (cl-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 (funcall 'set-smtp server port address)
> finally (error "Cannot infer SMTP information."))))
do (if (string-match address from)
(return (funcall 'set-smtp server port address)))
finally (error "Cannot infer SMTP information.")
will.
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: elisp newbie: simplifying from cl structures?
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>
1 sibling, 0 replies; 6+ messages in thread
From: Tory S. Anderson @ 2015-02-08 3:33 UTC (permalink / raw)
To: Pascal J. Bourguignon, John Mastro; +Cc: help-gnu-emacs
Thanks for helping me straighten the code out and gain better understanding cl-tools and structure; it loads and mostly works. Thanks!
--8<---------------cut here---------------start------------->8---
(defun change-smtp ()
"Change the SMTP server according to the current from line."
(save-excursion
(cl-loop with from = (save-restriction
(message-narrow-to-headers)
(message-fetch-field "from"))
for (address server port) in smtp-accounts
do (if (string-match address from)
(return (funcall 'set-smtp server port address))
(message "Failed to match %s with %s" address from))
finally (error "Cannot infer SMTP information."))))
(add-hook 'message-send-mail-hook 'change-smtp)
--8<---------------cut here---------------end--------------->8---
"Pascal J. Bourguignon" <pjb@informatimago.com> writes:
> John Mastro <john.b.mastro@gmail.com> writes:
>
>>> (defun change-smtp ()
>>> "Change the SMTP server according to the current from line."
>>> (save-excursion
>>> (cl-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 (funcall 'set-smtp server port address))
>>> (error "Cannot infer SMTP information.")))
>>
>> Second try. The return won't escape the `error' in the above.
>>
>> (defun change-smtp ()
>> "Change the SMTP server according to the current from line."
>> (save-excursion
>> (cl-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 (funcall 'set-smtp server port address)
>> finally (error "Cannot infer SMTP information."))))
>
> do (if (string-match address from)
> (return (funcall 'set-smtp server port address)))
> finally (error "Cannot infer SMTP information.")
>
> will.
^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <mailman.19507.1423366427.1147.help-gnu-emacs@gnu.org>]
* Re: elisp newbie: simplifying from cl structures?
[not found] ` <mailman.19507.1423366427.1147.help-gnu-emacs@gnu.org>
@ 2015-02-08 3:37 ` Pascal J. Bourguignon
0 siblings, 0 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2015-02-08 3:37 UTC (permalink / raw)
To: help-gnu-emacs
torys.anderson@gmail.com (Tory S. Anderson) writes:
> Thanks for helping me straighten the code out and gain better
> understanding cl-tools and structure; it loads and mostly
> works. Thanks!
>
> (defun change-smtp ()
> "Change the SMTP server according to the current from line."
> (save-excursion
> (cl-loop with from = (save-restriction
> (message-narrow-to-headers)
> (message-fetch-field "from"))
> for (address server port) in smtp-accounts
> do (if (string-match address from)
> (return (funcall 'set-smtp server port address))
> (message "Failed to match %s with %s" address from))
> finally (error "Cannot infer SMTP information."))))
>
> (add-hook 'message-send-mail-hook 'change-smtp)
Ok, but now, you should consider that this functions is performing
several tasks of different order: some parsing of a message is done, as
well as some searching for smtp configuration, and some setting of smtp
configuration. That's a lot for a single function.
It would be better if you split it as follow, which would give you two
nice reusable functions, and makes change-smtp clearer:
(defun message-from-field ()
"Find the from field in the current buffer that must contain a message."
(save-excursion
(save-restriction
(message-narrow-to-headers)
(message-fetch-field "from"))))
(defun find-smtp-account (address accounts)
"Find the first account in `accounts' whose address matches `address'."
(find from accounts :key (function first)
:test (lambda (from address)
(string-match address from))))
(defun change-smtp ()
(let ((from (message-from-field)))
(destructuring-bind (address server port) (find-smtp-account from smtp-accounts)
(if address
(funcall 'set-smtp server port address)
(error "Cannot not infer SMTP information")))))
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
^ 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
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.