home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / bbdb / bbdb-rmail.el < prev    next >
Encoding:
Text File  |  1992-09-08  |  5.6 KB  |  144 lines

  1. ;;; -*- Mode:Emacs-Lisp -*-
  2.  
  3. ;;; This file is part of the Insidious Big Brother Database (aka BBDB),
  4. ;;; copyright (c) 1991, 1992 Jamie Zawinski <jwz@lucid.com>.
  5. ;;; Interface to RMAIL.  See bbdb.texinfo.
  6. ;;; last change  8-sep-92.
  7.  
  8. ;;; The Insidious Big Brother Database is free software; you can redistribute
  9. ;;; it and/or modify it under the terms of the GNU General Public License as
  10. ;;; published by the Free Software Foundation; either version 1, or (at your
  11. ;;; option) any later version.
  12. ;;;
  13. ;;; BBDB is distributed in the hope that it will be useful, but WITHOUT ANY
  14. ;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. ;;; FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  16. ;;; details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Emacs; see the file COPYING.  If not, write to
  20. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. (require 'bbdb)
  23. (require 'rmail)
  24. ;(require 'rmailsum)   ; not provided, dammit!
  25. (if (not (fboundp 'rmail-make-summary-line)) (load-library "rmailsum"))
  26.  
  27. (defun bbdb/rmail-update-record (&optional offer-to-create)
  28.   "returns the record corresponding to the current RMAIL message, creating or
  29. modifying it as necessary.  A record will be created if 
  30. bbdb/mail-auto-create-p is non-nil, or if OFFER-TO-CREATE is true and
  31. the user confirms the creation."
  32.   (if bbdb-use-pop-up
  33.       (bbdb/rmail-pop-up-bbdb-buffer offer-to-create)
  34.     (if (and (boundp 'rmail-buffer) rmail-buffer)
  35.     (set-buffer rmail-buffer))
  36.     (if rmail-current-message
  37.       (or (bbdb-message-cache-lookup rmail-current-message nil)
  38.     (save-excursion
  39.          (let ((from (mail-fetch-field "from"))
  40.            name net)
  41.       (if (or (null from)
  42.           (string-match (bbdb-user-mail-names)
  43.                 (mail-strip-quoted-names from)))
  44.           ;; if logged-in user sent this, use recipients.
  45.           (setq from (or (mail-fetch-field "to") from)))
  46.       (if from
  47.           (bbdb-encache-message rmail-current-message
  48.         (bbdb-annotate-message-sender from t
  49.               (or (bbdb-invoke-hook-for-value bbdb/mail-auto-create-p)
  50.               offer-to-create))))))))))
  51.  
  52. (defun bbdb/rmail-annotate-sender (string)
  53.   "Add a line to the end of the Notes field of the BBDB record 
  54. corresponding to the sender of this message."
  55.   (interactive (list (if bbdb-readonly-p
  56.              (error "The Insidious Big Brother Database is read-only.")
  57.              (read-string "Comments: "))))
  58.   (if (and (boundp 'rmail-buffer) rmail-buffer)
  59.       (set-buffer rmail-buffer))
  60.   (bbdb-annotate-notes (bbdb/rmail-update-record t) string))
  61.  
  62. (defun bbdb/rmail-edit-notes (&optional arg)
  63.   "Edit the notes field or (with a prefix arg) a user-defined field
  64. of the BBDB record corresponding to the sender of this message."
  65.   (interactive "P")
  66.   (let ((record (or (bbdb/rmail-update-record t) (error ""))))
  67.     (bbdb-display-records (list record))
  68.     (if arg
  69.     (bbdb-record-edit-property record nil t)
  70.       (bbdb-record-edit-notes record t))))
  71.  
  72.  
  73. (defun bbdb/rmail-show-sender ()
  74.   "Display the contents of the BBDB for the sender of this message.
  75. This buffer will be in bbdb-mode, with associated keybindings."
  76.   (interactive)
  77.   (if (and (boundp 'rmail-buffer) rmail-buffer)
  78.       (set-buffer rmail-buffer))
  79.   (let ((record (bbdb/rmail-update-record t)))
  80.     (if record
  81.     (bbdb-display-records (list record))
  82.     (error "unperson"))))
  83.  
  84.  
  85. (defun bbdb/rmail-pop-up-bbdb-buffer (&optional offer-to-create)
  86.   "Make the *BBDB* buffer be displayed along with the RMAIL window(s),
  87. displaying the record corresponding to the sender of the current message."
  88.   (bbdb-pop-up-bbdb-buffer
  89.     (function (lambda (w)
  90.       (let ((b (current-buffer)))
  91.     (set-buffer (window-buffer w))
  92.     (prog1 (eq major-mode 'rmail-mode)
  93.       (set-buffer b))))))
  94.   (let ((bbdb-gag-messages t)
  95.     (bbdb-use-pop-up nil)
  96.     (bbdb-electric-p nil))
  97.     (let ((record (bbdb/rmail-update-record offer-to-create))
  98.       (bbdb-elided-display (bbdb-pop-up-elided-display))
  99.       (b (current-buffer)))
  100.       (bbdb-display-records (if record (list record) nil))
  101.       (set-buffer b)
  102.       record)))
  103.  
  104. (defun bbdb/rmail-expunge ()
  105.   "Actually erase all deleted messages in the file."
  106.   (interactive)
  107.   (setq bbdb-message-cache nil)
  108.   (bbdb-orig-rmail-expunge))
  109.  
  110. (defun bbdb/undigestify-rmail-message ()
  111.   "Break up a digest message into its constituent messages.
  112. Leaves original message, deleted, before the undigestified messages."
  113.   (interactive)
  114.   (setq bbdb-message-cache nil)
  115.   (bbdb-orig-undigestify-rmail-message))
  116.  
  117. (defun bbdb-insinuate-rmail ()
  118.   "Call this function to hook BBDB into RMAIL."
  119.   (define-key rmail-mode-map ":" 'bbdb/rmail-show-sender)
  120.   (define-key rmail-mode-map ";" 'bbdb/rmail-edit-notes)
  121.   (define-key rmail-summary-mode-map ":" 'bbdb/rmail-show-sender)
  122.   (define-key rmail-summary-mode-map ";" 'bbdb/rmail-edit-notes)
  123.   
  124.   (bbdb-add-hook 'rmail-show-message-hook 'bbdb/rmail-update-record)
  125.   
  126.   ;; We must patch into rmail-expunge to clear the cache, since expunging a 
  127.   ;; message invalidates the cache (which is based on message numbers).
  128.   ;; Same for undigestifying.
  129.   (or (fboundp 'bbdb-orig-rmail-expunge)
  130.       (fset 'bbdb-orig-rmail-expunge (symbol-function 'rmail-expunge)))
  131.   (fset 'rmail-expunge 'bbdb/rmail-expunge)
  132.  
  133.   (or (fboundp 'undigestify-rmail-message)
  134.       (autoload 'undigestify-rmail-message "undigest" nil t))
  135.   (if (eq (car-safe (symbol-function 'undigestify-rmail-message)) 'autoload)
  136.       (load (nth 1 (symbol-function 'undigestify-rmail-message))))
  137.   (or (fboundp 'bbdb-orig-undigestify-rmail-message)
  138.       (fset 'bbdb-orig-undigestify-rmail-message
  139.         (symbol-function 'undigestify-rmail-message)))
  140.   (fset 'undigestify-rmail-message 'bbdb/undigestify-rmail-message)
  141.   )
  142.  
  143. (provide 'bbdb-rmail)
  144.