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

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

Re: Help needed to simplify code for customisation


From: Kevin Rodgers
Subject: Re: Help needed to simplify code for customisation
Date: Tue, 10 Mar 2009 20:39:21 -0600
User-agent: Thunderbird 2.0.0.19 (Macintosh/20081209)

Richard Riley wrote:
Could someone please recommend the best way to remove the 3 similar lines
doing string-match on the "account" assign and iterate a variable list to
which I can "add-to-list" in other .el libraries for example?

,----
|  (if (message-mail-p)
|       (save-excursion
|       (let* ((from
|               (save-restriction
|                 (message-narrow-to-headers)
|                 (message-fetch-field "from")))
|              (account
|               (cond
|                ((string-match ".*root.*" from)"richardriley")
|                ((string-match ".*richardriley.*" from)"richardriley")
|                ((string-match ".*rileyrgdev.*" from)"rileyrgdev")
|                ))
|              )
|         (setq message-sendmail-extra-arguments (list "-a" account))
|         )))
|   )
`----

Thanks for any pointers,

Why not use message-field-value instead of message-fetch-field, since
it takes care of the narrowing as well?

(string-match ".*foo.*" BAR) is equivalent to (string-match "foo" BAR)
when used as a predicate.

Your patterns are not very specific and admit false positives e.g.
"address@hidden" and "address@hidden (not richardriley)".
At the very least, they should be anchored with \< and \>.

Better yet, use mail-header-parse-address to split the header into
MAILBOX and COMMENT components, then match only on MAILBOX.

Here's what I came up with:

(defvar my-sendmail-accounts
  '(("root" . "richardriley") ("richardriley") ("rileyrgdev"))
  "Alist of (MAILBOX . ACCOUNT) pairs.  If ACCOUNT is nil, use MAILBOX.")

(when (message-mail-p)
(let ((sender (car (mail-header-parse-address (message-field-value "From"))))
        (accounts-regexp (format "\\`\\(%s\\)\\>"
                                 (mapconcat (function car)
                                            my-sendmail-accounts
                                            "\\|")
                                 )))
    (when (string-match accounts-regexp sender)
      (let ((mailbox-account (assoc (match-string 0 sender)
                                    my-sendmail-accounts)))
        (setq message-sendmail-extra-arguments
              (list "-a" (or (cdr mailbox-account)
                             (car mailbox-account))))))))

--
Kevin Rodgers
Denver, Colorado, USA





reply via email to

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