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: TomSW
Subject: Re: Help needed to simplify code for customisation
Date: Tue, 10 Mar 2009 03:02:50 -0700 (PDT)
User-agent: G2/1.0

> On Mar 9, 7:14 pm, Richard Riley <rileyrg...@gmail.com> 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?

(require 'cl)

(defvar my-accounts-alist
  '(("richardriley"  "root" "richardriley")
    ("rileyrgdev"    "rileyrgdev"))
  "Associate email accounts with sender addresses: an alist each item
of
which is a list whose first member is the account name and any
following
members are regular expressions to match against a sender address.")

(defun my-get-account (from)
  (car (find-if (lambda (regexps)
                  (some (lambda (regexp)
                          (string-match regexp from))
                        regexps))
                from my-accounts-alist
                :key 'cdr)))

...
 (if (message-mail-p)
     (save-excursion
       (let* ((account
               (save-restriction
                 (message-narrow-to-headers)
                 (my-get-account (message-fetch-field "from"))))
              ;; use let to set the value of message-sendmail-extra-
arguments
              ;; dynamically
              ;; - this assumes that whatever uses the arguments will
be called
              ;; inside the let. Otherwise use setq.
              (message-sendmail-extra-arguments
               (append (when account (list "-a" account))
                       message-sendmail-extra-arguments)))
         ;; do message stuff
         )))
...

You can then push extra associations to the front of my-accounts-
alist, or even add regular expressions to a particular account, as
long as you know it's already in the list:

(push '("richardriley" "anotherrileyaddr")
      my-accounts-alist)

(push "foo"
      (cdr  (assoc "richardriley" my-accounts-alist)))

Note that the first match wins, so the sorting of the alist may become
significant if your regular expressions are too greedy.

regards,
Tom SW


reply via email to

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