[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help needed to simplify code for customisation
From: |
Xah Lee |
Subject: |
Re: Help needed to simplify code for customisation |
Date: |
Mon, 9 Mar 2009 23:44:45 -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?
>
> ,----
> | (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))
> | )))
> | )
> `----
perhaps something like the following. The code is tested.
(defun canonicalString (from pairs)
"Returns the canonical string of FROM, determined by the pairs in
PAIRS.
The PAIRS should be a nested vector of the form:
“[[\"a\" \"α\"] [\"b\" \"β\"] [\"γ\" \"g\"] ...]”
where the first element is a regex string to be matched with FROM.
If match, then the second element is returned.
If no match is found, nil is returned.
Example:
(canonicalString \"b\" [[\"a\" \"α\"] [\"b\" \"β\"] [\"γ\" \"g\"]])
returns \"β\".
"
(let (totalItems matchFound i result)
(setq totalItems (length pairs))
(setq foundMatch nil)
(setq i 0)
(while (and (not matchFound)
(not (= i totalItems)))
(if (string-match (elt (elt pairs i) 0) from)
(progn
(setq result (elt (elt pairs i) 1))
(setq matchFound t)))
(setq i (1+ i)))
result))
; testing
(canonicalString "b" [["a" "α"] ["b" "β"] ["γ" "g"]])
Xah
∑ http://xahlee.org/
☄
Re: Help needed to simplify code for customisation, Kevin Rodgers, 2009/03/10