;; -*- emacs-lisp -*-

;; don't let gnus destroy our window configuration!
;; (setq gnus-use-full-window t)

(setq user-mail-address "cjb@laptop.org")
'(query-user-mail-address nil)

(setq gnus-verbose 10)
(setq gnus-verbose-backends 10) 
(setq garbage-collection-messages nil)

;; This marks mail I send as read.
(setq gnus-gcc-mark-as-read t)

;; The CVS of Oort hides References: from me while composing.  It didn't
;; always, but it does now.  We don't need to delay generating it until
;; until send-time (unlike, say, Date:), so let's not; that way I can
;; check that I'm including one, too.  Yay.

(setq message-generate-headers-first '(References))

;; GC.  This keeps things somewhere around:
;;    PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
;;  18291 chris      0   0 56768  55M  2980 S     0.0 22.0  15:34 xemacs

(setq gc-cons-threshold 6000000)

;; General speedups.

(setq gnus-read-active-file nil) 
(setq gnus-check-new-newsgroups nil) 
(setq gnus-nov-is-evil nil) 
(setq gnus-save-newsrc-file t)

(setq user-full-name "Chris Ball")
(setq message-from-style 'angles) 

;; Using %G (default is %g) avoids the nnfoo:bar+ prefix
(setq gnus-group-line-format "%M\%S\%p\%5y: %G\n")

;; Topic headings for groups.
(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)

;; Wrap at 70 cols.
(add-hook 'message-mode-hook
	'(lambda()
		(turn-on-auto-fill)
		(setq fill-column 70)))

(setq message-send-mail-function 'smtpmail-send-it)
(setq smtpmail-smtp-server "printf.net")
(setq gnus-message-archive-group "nnimap+printf:2009mbox")

(setq gnus-select-method '(nnimap "printf"
                (nnimap-address "printf.net")
	        (nnimap-authenticator login)
                (nnimap-stream ssl)
                (remove-prefix "INBOX.") 
                (nnimap-authinfo-file "/home/cjb/.authinfo")
               )
)

(setq gnus-secondary-select-methods '((nntp "news.gmane.org")))

;; Supercite.
(setq sc-preferred-attribution-list
      `("x-attribution" "initials" "sc-lastchoice" "firstname" "lastname"))
(setq sc-preferred-header-style 1)
(setq sc-citation-leader "   ")
(setq sc-reference-tag-string ">> ")
;(setq news-reply-header-hook nil)
(autoload `sc-cite-original     "supercite" "Supercite 3.1" t)
(add-hook `mail-citation-hook `sc-cite-original)

(setq sc-nested-citation-p t)
(setq sc-cite-regexp "\\s *[-a-zA-Z0-9_.]*>+\\s *[> ]*")
(setq sc-auto-fill-region-p t)
(add-hook 'mail-citation-hook 'sc-cite-original)

;; Show the time since the article came in.
(setq gnus-treat-date-lapsed 'head)

;; Make sure Gnus doesn't display smiley graphics.
(setq gnus-treat-display-smileys nil)

;; I keep hitting "b" by mistake in the group view, and it messes things up.
(define-key gnus-group-mode-map "b" 'gnus-group-get-new-news)

;; Use a second connection to grab the next article when I read one, so
;; I don't have to wait for it be downloaded.  
(setq gnus-asynchronous t)

;; Avoid "Here's an attachment oops I forget to attach it augh" embarrassment.
;; Taken from <http://ww.telent.net/diary/2003/1/>.
(defun check-attachments-attached () 
  (interactive) 
  (save-excursion 
    (goto-char (point-min)) 
    (let* ( 
           ;; Nil when message came from outside (eg calling emacs as editor) 
           ;; Non-nil marker of end of headers. 
           (internal-messagep 
            (re-search-forward 
             (concat "^" (regexp-quote mail-header-separator) "$") nil t)) 
           (end-of-headers              ; Start of body. 
            (copy-marker 
             (or internal-messagep 
                 (re-search-forward "^$" nil t) 
                 (point-min)))) 
           (limit  
            (or (re-search-forward "^-- $" nil t)  
                (point-max)))               
           (old-case-fold-search case-fold-search)) 
      (unwind-protect 
          (progn 
            (goto-char end-of-headers) 
            (when (search-forward "attach" limit t) 
              (goto-char end-of-headers) 
              ;; the word 'attach' has been used, can we find an 
              ;; attachment? 
              (unless  
                  (or (re-search-forward "^<#/" limit t) 
                      (y-or-n-p 
                       "Found the word `attach' but no MIME attachment: send anyway? " 
                      ) 
                     (error "Aborted send"))))) 
        (set-marker end-of-headers nil))))) 
 
(add-hook 'message-send-hook 'check-attachments-attached) 

;; Add formalities for me.
(defadvice gnus-summary-reply (after formalities () activate)
  (cjb-add-formalities))

(defun cjb-add-formalities ()
  "Add the sender's first name and my tag to e-mail."
  ;; Modified from <http://www.repose.cx/conf/.elisp/de-gnus.el>
  (save-excursion
    (message-goto-signature)
    (previous-line 2)
    (when (not (looking-at "- Chris."))
         (insert "\n\n- Chris.")))
    (let* ((to (message-fetch-field "To"))
         (address-comp (mail-extract-address-components to))
         (name (car address-comp))
         (first (or (and name (concat "" (car (split-string name)))) "")))
         
         (when first
           ;; Go to the first line of the message body. 
           (message-goto-body)
           (insert "Hi,\n\n")
           (kill-line)
           (kill-line)
           (kill-line)
           (message-goto-signature)
           (previous-line 4)
           (newline)
	)))
           ;;(insert (concat (capitalize first) ",\n\n")))))


(defun gnus-demon-scan-mail-or-news-and-update ()
  "Scan for new mail/news and update the *Group* buffer"
  (when (gnus-alive-p)  
    (save-window-excursion
      (save-excursion
 (set-buffer gnus-group-buffer)
 (gnus-group-get-new-news)))))

(defun gnus-demon-scan-and-update ()
  (gnus-demon-scan-mail-or-news-and-update))

;; Sort threads by the date of the root node.
(setq gnus-thread-sort-functions `(gnus-thread-sort-by-date))

;; BBDB
(require 'bbdb)
(bbdb-initialize)
(add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)

(setq bbdb-use-pop-up nil)
(setq gnus-use-full-window nil)

;; Save BBDB addresses from the people I reply to.
;; Taken from <http://ichimusai.org/emacs/.gnus>
(defun cjb-bbdb-add-recipients-to-bbdb ()
  (mapcar
   (lambda (address)
     (let* ((parsed (mail-header-parse-address (bbdb-string-trim address)))
            (net (car parsed))
            (name (cdr parsed)))
       (when (not (bbdb-search-simple name net))
         (bbdb-create-internal (cdr parsed) nil (car parsed) nil nil nil))))
   (append (message-tokenize-header (message-fetch-field "to"))
           (message-tokenize-header (message-fetch-field "cc"))
           (message-tokenize-header (message-fetch-field "bcc")))))
(add-hook 'message-send-mail-hook 'cjb-bbdb-add-recipients-to-bbdb)

;; Initialize the Gnus daemon, check new mail every one minute.
(gnus-demon-add-handler 'gnus-demon-scan-and-update 1 1)
(setq gnus-use-demon t)
(gnus-demon-init)
(gnus-compile)
