2009-03-10

emacs lisp; pairs

2009-03-10

On Mar 9, 7:14 pm, 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))
> | )))
> | )
> `----

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/

No comments:

Post a Comment