all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Help needed to simplify code for customisation
@ 2009-03-10  2:14 Richard Riley
  2009-03-10  6:44 ` Xah Lee
  2009-03-11  2:39 ` Kevin Rodgers
  0 siblings, 2 replies; 6+ messages in thread
From: Richard Riley @ 2009-03-10  2:14 UTC (permalink / raw)
  To: help-gnu-emacs


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,

r.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Help needed to simplify code for customisation
  2009-03-10  2:14 Help needed to simplify code for customisation Richard Riley
@ 2009-03-10  6:44 ` Xah Lee
       [not found]   ` <b5ecffc4-3770-4c6e-8f1c-38042d65d09e@a39g2000yqc.googlegroups.com>
  2009-03-11  2:39 ` Kevin Rodgers
  1 sibling, 1 reply; 6+ messages in thread
From: Xah Lee @ 2009-03-10  6:44 UTC (permalink / raw)
  To: help-gnu-emacs

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/^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Help needed to simplify code for customisation
       [not found]   ` <b5ecffc4-3770-4c6e-8f1c-38042d65d09e@a39g2000yqc.googlegroups.com>
@ 2009-03-10 11:42     ` William James
  2009-03-10 16:53       ` Marco Antoniotti
  0 siblings, 1 reply; 6+ messages in thread
From: William James @ 2009-03-10 11:42 UTC (permalink / raw)
  To: help-gnu-emacs

TomSW wrote:

> (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)))

Clojure:

(def my-accounts-map
  { "richardriley"  [#"root" #"richardriley"],
    "rileyrgdev"    [#"rileyrgdev"] } )

(defn my-get-account [from]
  (ffirst
    (filter
      (fn [[acct re-list]] (some #(re-find % from) re-list))
      my-accounts-map)))




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Help needed to simplify code for customisation
  2009-03-10 11:42     ` William James
@ 2009-03-10 16:53       ` Marco Antoniotti
  2009-03-11  6:37         ` Kenneth Tilton
  0 siblings, 1 reply; 6+ messages in thread
From: Marco Antoniotti @ 2009-03-10 16:53 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 10, 12:42 pm, "William James" <w_a_x_...@yahoo.com> wrote:
> TomSW wrote:
> > (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)))
>
> Clojure:
>
> (def my-accounts-map
>   { "richardriley"  [#"root" #"richardriley"],
>     "rileyrgdev"    [#"rileyrgdev"] } )
>
> (defn my-get-account [from]
>   (ffirst
>     (filter
>       (fn [[acct re-list]] (some #(re-find % from) re-list))
>       my-accounts-map)))

Hey.  That is not the homework you are supposed to hand in.

Cheers
--
Marco





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Help needed to simplify code for customisation
  2009-03-10  2:14 Help needed to simplify code for customisation Richard Riley
  2009-03-10  6:44 ` Xah Lee
@ 2009-03-11  2:39 ` Kevin Rodgers
  1 sibling, 0 replies; 6+ messages in thread
From: Kevin Rodgers @ 2009-03-11  2:39 UTC (permalink / raw)
  To: help-gnu-emacs

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.
"FOO@BARrichardrileyBAZ.com" and "foo@bar.com (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





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Help needed to simplify code for customisation
  2009-03-10 16:53       ` Marco Antoniotti
@ 2009-03-11  6:37         ` Kenneth Tilton
  0 siblings, 0 replies; 6+ messages in thread
From: Kenneth Tilton @ 2009-03-11  6:37 UTC (permalink / raw)
  To: help-gnu-emacs

Marco Antoniotti wrote:
> On Mar 10, 12:42 pm, "William James" <w_a_x_...@yahoo.com> wrote:
>> TomSW wrote:
>>> (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)))
>> Clojure:
>>
>> (def my-accounts-map
>>   { "richardriley"  [#"root" #"richardriley"],
>>     "rileyrgdev"    [#"rileyrgdev"] } )
>>
>> (defn my-get-account [from]
>>   (ffirst
>>     (filter
>>       (fn [[acct re-list]] (some #(re-find % from) re-list))
>>       my-accounts-map)))
> 
> Hey.  That is not the homework you are supposed to hand in.


<sigh> What are we going to do with our boy Willy? He never does his 
Ruby assignmens any more and just plays with a Lisp all day. I say no 
television until we seem some work.

kt


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-03-11  6:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-10  2:14 Help needed to simplify code for customisation Richard Riley
2009-03-10  6:44 ` Xah Lee
     [not found]   ` <b5ecffc4-3770-4c6e-8f1c-38042d65d09e@a39g2000yqc.googlegroups.com>
2009-03-10 11:42     ` William James
2009-03-10 16:53       ` Marco Antoniotti
2009-03-11  6:37         ` Kenneth Tilton
2009-03-11  2:39 ` Kevin Rodgers

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.