;;; bbdb-init.el --- Initialize BBDB

;; This file is part of Michael Olson's Emacs settings.

;; The code in this file may be used, distributed, and modified
;; without restriction.

;; I use initsplit.el to separate customize settings on a per-project
;; basis.

;; Add working directories to load path

;(add-to-list 'load-path "/stuff/proj/emacs/bbdb/bits")
;(add-to-list 'load-path "/stuff/proj/emacs/bbdb/lisp")

;; Initialize
(require 'bbdb)

;;; Hacks

;; Show X-Face header

(autoload 'gnus-convert-face-to-png "gnus-fun")
(defun my-bbdb-display-faces ()
  "Search for face properties and display the faces."
  (let ((inhibit-read-only t)
        (default-enable-multibyte-characters nil)
        (all-records bbdb-records)
        face x-face record)
    (goto-char (point-min))
    (mapc
     (lambda (record)
       (setq x-face (bbdb-record-getprop (car record) 'x-face)
             face (bbdb-record-getprop (car record) 'face))
       ;; Display Face
       (when face
         (insert-image (create-image (gnus-convert-face-to-png face)
                                     nil t))
         (insert " "))
       ;; Display X-Face
       (when x-face
         (insert-image (gnus-create-image (uncompface x-face)
                                          nil t :face 'tooltip))
         (insert " "))
       ;; Move to the next record, suppress error on reaching last
       (condition-case nil
           (bbdb-next-record 1)
         (error nil)))
     all-records)
    ;; Remove all x-face and face lines from the display
    (goto-char (point-min))
    (save-match-data
      (while (re-search-forward "^ *\\(x-\\)?face: " nil t)
        (beginning-of-line)
        (kill-line 1)))))
(add-hook 'bbdb-list-hook 'my-bbdb-display-faces)

;;; Variables

;; Declare which header lines to assimilate

(setq bbdb-auto-notes-alist
 (list
  '("Organization" (".*" company 0))
  '("Newsgroups"   ("[^,]+" newsgroups 0))
  '("Subject"      (".*" last-subj 0 t))
  '("User-Agent"   (".*" mailer 0 t))
  '("X-Mailer"     (".*" mailer 0 t))
  '("X-Newsreader" (".*" mailer 0 t))
  '("X-URL"        (".*" www 0))
  '("X-Face"       (".+" x-face 0 'replace))
  '("Face"         (".+" face 0 'replace))
  '("X-PGP-Fingerprint"        (".*" pgp-fingerprint 0))
  '("X-PGP-Public-Key-ID"      (".*" pgp-key 0))
  '("X-Pgp-Fingerprint"        (".*" pgp-fingerprint 0))
  '("X-Pgp-Public-Key-Id"      (".*" pgp-key 0))
  '("X-Gpg-Fingerprint"        (".*" pgp-fingerprint 0))
  '("X-Gpg-Public-Key-Id"      (".*" pgp-key 0))
  '("X-Gpg-Key-ID"             (".*" pgp-key 0))
  ))

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

;;; Customizations

(custom-set-variables
 '(bbdb-completion-display-record nil)
 '(bbdb-default-country "")
 '(bbdb-file "~/.emacs.d/.bbdb")
 '(bbdb-save-db-timeout 0)
 '(bbdb-send-mail-style (quote message))
 '(bbdb-use-pop-up nil))
(custom-set-faces)

;;; bbdb-init.el ends here