Matthew Liggett said:

   someone before mentioned a chunk of code (using advice I believe)
   that would allow you to add a mail-folder property to entries in
   the DB for VM's default folder name thing.

I use (in .vm):

(defun gec-fetch-vm-field (field)
  "return requested mail field from the current vm message"
  (let
      ((msg (car vm-message-pointer)))
    (save-excursion
      (save-restriction
        (set-buffer (vm-buffer-of msg))
        (widen)
        (narrow-to-region (vm-start-of msg) (vm-end-of msg))
        (mail-fetch-field field)))))


;; logic to use bbdb mail-folder property to set the default for
;; saving message.
(defadvice vm-auto-select-folder (around bbdb-auto-select activate compile)
  "If the message sender's BBDB entry has a `mail-folder' property, use that."
  (require 'bbdb-com)
  (let* ((record (bbdb/vm-update-record t))
         (folder (and record (bbdb-record-getprop record 'mail-folder)))
         (header-folder (gec-fetch-vm-field "x-vm-folder"))
         )
    (if (or header-folder folder)
        (setq ad-return-value (or header-folder folder))
      ;; ad-do-it is the original body of vm-auto-select-folder.
      ad-do-it)))


And I also do (amidst my bbdb customizations):

(add-hook 'bbdb-notice-hook 'bbdb-auto-notes-hook)

(setq
   bbdb-auto-notes-alist '(
                           ("Newsgroups" (".*" newsgroup 0))
                           ("Organization" (".*" company 0 t))
                           ("Subject" (".*" last-subj 0 t))
                           ("From" (".*" mail-folder gec-auto-folder-create t))
                           )
)
;; these regexps also appear in vm-auto-folder-alist
;; this function when called by bbdb-auto-notes-hook will add a
;; mail-folder field to the current entry based on the current From
;; header.  bbdb doesn't seem to offer a way to not do this if the
;; field doesn't already exist, so we have to make that check
;; ourselves.  If the clever (written by someone else) regexps fail,
;; prompt the user for a value.

;; added first level attempt to use first.last as the mail folder
;; since I've been doing that myself manually recently.
(defun gec-auto-folder-create (string)
  (let
      (
       (old-field-value (bbdb-record-getprop record 'mail-folder))
       (fname (bbdb-record-name record))
       )
    (setq string
          (cond
           ;; already have a value, so never change it automatically
           (old-field-value old-field-value)
           ;; see if we can get a first.last out of bbdb's name.
           (fname
            (setq fname (or (bbdb-record-getprop record 'mail-name)
                            fname))
            (setq fname (downcase fname))
            (while (string-match "^\\([^ ]*\\)[ ]+\\(.*\\)$" fname)
              (setq fname
                    (concat (substring fname (match-beginning 1) (match-end 1))
                            "."
                            (substring fname (match-beginning 2) (match-end 2)))))
            fname)
           ;; blah de blah <person@whatever> return person
           ((string-match "<\\([^>@%]+\\)" string)
            (substring string (match-beginning 1) (match-end 1)))
           ;; person@whatever or person%whatever return person
           ((string-match "\\([^@%]+\\)[@%]" string)
            (substring string (match-beginning 1) (match-end 1)))
           ;; word followed by space (?)
           ((string-match "\\(\\w+\\) " string)
            (substring string (match-beginning 1) (match-end 1)))
           ;; give up and ask the user
           (t (read-string "Enter mail-folder for this entry: " nil))))
    string))


The net effect of this is when bbdb sees a record from vm, it creates
a mail-folder property for the bbdb record (if it doesn't already have
one).  The preferred value is first.last, but if the record doesn't
have a full name, it uses those ugly regexps to try to extract the
login name component of the email address.  Failing that, it prompts
you.

The advice around vm-auto-select-folder uses the value of a message's
X-VM-Folder: header (if there is one), or the mail-folder property of
the bbdb record for the sender/recipient of the message.  I sometimes
interactively set X-Vm-Folder: on outgoing messages so my bcc'ed copy
will end up in a different folder from the default.

   also, I have installed the mail-abbrevs package that came with the bbdb
   and C-c C-a is working in mail-mode but not vm-mail-mode
   I thought vm-mail-mode inherited mail-mode's map.
   Maybe I don't have the require in the right hook:

   mail-setup-hook's value is (bbdb-define-all-aliases (lambda nil (require (quote 
mail-abbrevs))) bbdb-insinuate-sendmail)

I think this happens too late to get inherited.  I have in my .vm:

(define-key vm-mail-mode-map "\M-\t" 'bbdb-complete-name)

instead of the insinuate sendmail in mail-setup-hook (the define-key
to the wrong map is all that happens in bbdb-insinuate-sendmail).

So I suspect you'll want to add a define-key for c-c c-a as well.

Looking at my copy of mail-abbrevs.el, I see I've whacked it to use
vm-mail-mode-map anyway.  I'm not sure I even know how to invoke
non-vm-mail-mode these days.

Caveats: vm 5.68 (*important* installed with make noautoload), bbdb
1.50 (with recently posted jwz hacks to properly deal with virtual
folders), emacs 19.22 under irix.

Reply via email to