all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Lisp hints with VM, BBDB and Personality Crisis
@ 2003-09-14 22:16 spam
  2003-09-15  2:28 ` Jesper Harder
  0 siblings, 1 reply; 3+ messages in thread
From: spam @ 2003-09-14 22:16 UTC (permalink / raw)


Hello,

I have been playing around with the Personality Crisis package for
VM. I have written the following code which basically is a test to see
if the recipient of the e-mail is a friend. It goes in the bbdb record
for the e-mail handle and sees if there is a string like "efriend" in
the note field.

I am not an expert in elisp and I was wondering if there is a way to
shorten the code? Especially, in checking nil variables, I tend to do
a lot of (unless (eq thingy nil). Any other comments are appreciated.

(defun my-check-efriend()
  "Fetch the 'to' address from the e-mail. Look up in bbdb for the given
address. Look in the note field and check for the string
'efriend'. Returns t when that's the case"
  (interactive)
  (let ((header (vmpc-get-current-header-contents "to")))
	(unless (eq header nil)
	  (when (string-match "<\\(.*\\)>" header)
		(let* ((email (match-string 1 header))
			   (record (bbdb-search-simple "" email)))
		  (unless (eq record nil)
			(let ((note (bbdb-record-notes record)))
			  (unless (eq note nil)
				(when (string-match "efriend" note) t)))))))))

-- 
/-----------------------------------------------------------------------------*
|   "At Group L, Stoffel oversees six first-rate   |         Ivan Kanis       |
|   programmers, a managerial challenge roughly    |    Software Developper   |
|   comparable to herding cats."                   |        www.kanis.cc      |
|   (The Wasington Post, June 9, 1985)             |                          |
*-----------------------------------------------------------------------------/

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

* Re: Lisp hints with VM, BBDB and Personality Crisis
  2003-09-14 22:16 Lisp hints with VM, BBDB and Personality Crisis spam
@ 2003-09-15  2:28 ` Jesper Harder
  2003-09-18 13:07   ` Ivan Kanis
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper Harder @ 2003-09-15  2:28 UTC (permalink / raw)


spam@juliva.com writes:


> I am not an expert in elisp and I was wondering if there is a way to
> shorten the code? Especially, in checking nil variables, I tend to do
> a lot of (unless (eq thingy nil).

You can express it more succinctly as:

        (when thingy
           ...)

which is equivalent to (unless (eq thingy nil) ...).

There's also `null', which is the usual way of testing for a nil
value:

        (null thingy) == (eq thingy nil)

Sometimes `not' (which is just another name for `null') might express
your intent better.

> Any other comments are appreciated.
>
> (defun my-check-efriend()
>   "Fetch the 'to' address from the e-mail. Look up in bbdb for the given
> address. Look in the note field and check for the string
> 'efriend'. Returns t when that's the case"

The style guideline is to write the first line of a docstring as a
self-contained sentence.  Because in some cases -- e.g. when you use
`M-x apropos' -- only the first line is displayed.

>    (when (string-match "efriend" note) t)

`when' is redundant here.  Just returning the result of

    (string-match "efriend" note)

is the conventional way of doing it (the docstring should then say
"Return non-nil when ...", of course).

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

* Re: Lisp hints with VM, BBDB and Personality Crisis
  2003-09-15  2:28 ` Jesper Harder
@ 2003-09-18 13:07   ` Ivan Kanis
  0 siblings, 0 replies; 3+ messages in thread
From: Ivan Kanis @ 2003-09-18 13:07 UTC (permalink / raw)


Thanks for all the info! Here is the code cleaned up. It is really
handy used in conjunction with Personality Crisis to decide on what
your "from" address should be. In the example given below my friend
have the string "efriend" in the bbdb note field. They will get my
personal address when I write them.

As usual comments on style or anything are welcome.

(defun my-check-bbdb-note(note-keyword)
  "Return non-nil when recipent has `note-keyword' in BBDB.  
This function is used in a VM composition buffer. It fetches the
recpipient addres from the buffer. It looks up in bbdb for the given
address. It looks in the note field and check that it contains
`note-keyword'. It is useful with Personality Crisis to generate a
different sender address depending on the recipient name."
  (interactive)
  (let ((header (vmpc-get-current-header-contents "to")))
    (when header
      (when (string-match "<\\(.*\\)>" header)
        (let* ((email (match-string 1 header))
               (record (bbdb-search-simple "" email)))
          (when record
            (let ((note (bbdb-record-notes record)))
              (when note
                (string-match note-keyword note)))))))))

;; Personal crisis settings

(setq vmpc-conditions 
      '(("to friend" (my-check-bbdb-note "efriend"))
))

(setq vmpc-actions
      '(("personal"
         (vmpc-substitute-header 
          "From" (concat "Ivan Kanis <" my-personal-email ">"))
         (vmpc-pre-signature "\nIvan")
         (vmpc-signature "~/.signature.friend")))
)

(setq vmpc-automorph-alist
      '(("to friend" "personal")
        ("to job" "job")
))

;; Hooks to put in vm compose buffer

(defun my-mail-down-and-automorph()
  (interactive)
  (if (save-excursion
        (search-forward
         (concat "\n" mail-header-separator "\n")
         nil t))
      (vmpc-automorph))
  (next-line 1))

(defun my-mail-text-and-automorph ()
  (interactive)
  (mail-text)
  (vmpc-automorph))

(defun my-define-vm-mail-key()
  (define-key vm-mail-mode-map "\C-c\C-i" 'ispell-message)
  (define-key vm-mail-mode-map "\C-c\C-s" nil)
  (define-key vm-mail-mode-map [down] 'my-mail-down-and-automorph)
  (define-key vm-mail-mode-map "\C-n" 'my-mail-down-and-automorph)
  (define-key vm-mail-mode-map "\C-c\C-t" 'my-mail-text-and-automorph)
  (define-key vm-mail-mode-map "\C-ca" 'vm-mime-attach-file)
    )

(add-hook 'vm-mail-mode-hook 'my-define-vm-mail-key)

-- 
/-----------------------------------------------------------------------------*
| "Do not try to live forever. You will not succeed."  |       Ivan Kanis     |
| (George Bernard Shaw)                                |  Software Developper |
|                                                      |      www.kanis.cc    |
*-----------------------------------------------------------------------------/

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

end of thread, other threads:[~2003-09-18 13:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-14 22:16 Lisp hints with VM, BBDB and Personality Crisis spam
2003-09-15  2:28 ` Jesper Harder
2003-09-18 13:07   ` Ivan Kanis

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.