;;
;; .gnus.el - Gnus configuration file for Nix
;;

(if (featurep 'xemacs)
    (error "This Gnus configuration only works with GNU Emacs."))

(require 'tc)                                    ; Trivialcite. Nice.

;; Gnus global options

; No primary select method; this is sitewide (`nntp')

(setq gnus-save-killed-list nil                  ; Don't need to remember old groups...
      gnus-check-new-newsgroups 'ask-server      ; ... as this is better with a local server
      gnus-check-bogus-newsgroups t              ; Check for bogus newsgroups as you start
      gnus-subscribe-newsgroup-method 'gnus-subscribe-interactively ; Ask me about new groups
      gnus-mailing-list-groups "^esperi\\\\.list-in\\\\." ; These groups are really mailing lists
      gnus-dribble-directory "~/News/"           ; Don't dribble in my $HOME
      gnus-play-startup-jingle nil               ; It's horrible, ugh.
      gnus-interactive-exit nil                  ; Bloody Nerd-O-Grams
      gnus-novice-user nil                       ; Ditto
      gnus-visual t                              ; Eye candy! Ooo!
      gnus-mouse-face 'highlight)                ; Face for highlighting

;; Gnus group buffer options

(setq gnus-group-line-format "%M%m%S%o%5y:%(%30g%)  %D\n" ; A nicer format
      gnus-large-newsgroup 400)                   ; many groups hit >400

(add-hook 'gnus-group-mode-hook 'gnus-topic-mode) ; Subscribed to many groups, so this makes sense

(require 'dot-gnus-colourization)
(require 'dot-gnus-bbdb)
(require 'dot-gnus-sa)

;; Group parameters

;; All the mailing lists and mail groups that do not explicitly specify in their parameters
;; (or their topic parameters) that they should be threaded in a particular way shall forevermore be
;; gathered by Subject:, because most email doesn't have a decently done References: header.

;; FIXME: we want a gather-by-references-else-by-subject function. This may be a bastard to write :(

(setq gnus-parameters
      `((,(concat "^" (regexp-opt '("nnml:" "nnmaildir:" "nnimap:")))
         (gnus-summary-thread-gathering-function gnus-gather-threads-by-subject))
        ;; Stop syntax checks for Approved: in automoderated groups
        ("^bofh\\.\\|^alt\\.sysadmin\\.recovery$\\|alt\\.dev\\.null$"
         (message-syntax-checks (approved . disabled)))))

;; A function for prefetching (cribbed from the manual) that says to prefetch only short unread articles.
;; Always byte-compile it; it is called a *lot*.

(byte-compile
 (defun nix-async-short-unread-p (data)
   "Return non-nil for short, unread articles."
   (and (gnus-data-unread-p data)
        (< (mail-header-lines (gnus-data-header data))
           1000))))

;; Gnus sitewide options

(setq gnus-agent nil)                            ; We don't need offline newsreading, even for Web groups

;; Gnus summary buffer options

(setq gnus-use-trees t                           ; Tree mode a la trn
      gnus-generate-tree-function 'gnus-generate-horizontal-tree  ; A la trn, again
      gnus-summary-line-format "%U%R%ub%5t%z%I%(%[%4L: %-20,20uB%]%) %s\n" ; How many articles? added
      gnus-extract-address-components 'mail-extract-address-components ; Correct, but sluggish
      gnus-auto-select-next 'almost-quietly      ; Go to the next unread group when this one is done
      gnus-auto-center-summary nil               ; Yuck!
      gnus-summary-goto-unread nil               ; Even more yuck!
      gnus-auto-select-first 'best               ; Show me the best article first
      gnus-asynchronous t                        ; *No* delay when I hop betwixt articles
      gnus-async-prefetch-article-p 'nix-async-short-unread-p ; Only prefetch short articles
      gnus-save-all-headers t                    ; Preserve headers when saving out
      gnus-prompt-before-saving t                ; Reasonable saving prompting, not insane prompting
      gnus-use-long-file-name '(not-save)        ; Store saved articles in a spool-like hierarchy
      gnus-default-article-saver 'gnus-summary-save-in-file ; Don't encode saved articles, just stick them on disk
      gnus-score-find-score-files-function '(bbdb/gnus-score gnus-score-find-bnews) ; Add a scorer
      gnus-signature-limit 10.0                  ; Some people put lots of stuff *below* the .sig... bah.
      gnus-tree-minimize-window 10               ; Set a maximum size for the gnus Tree mode window
      gnus-refer-thread-limit t                  ; If I ask for a complete thread, it should be *complete* dammit.
      gnus-single-article-buffer nil             ; We want one article buffer per summary, so that multiple summaries work
      gnus-summary-display-while-building 50)    ; Show us what you're doing, every fifty articles

;; Threading options

(setq gnus-fetch-old-headers 'some               ; Try to connect threads with the minimum number of old headers
      gnus-build-sparse-threads 'some            ; Include not-received articles too with References:
      gnus-summary-gather-subject-limit 'fuzzy   ; Use a smart fuzzy Subject-matcher
      gnus-summary-thread-gathering-function 'gnus-gather-threads-by-references ; Use the References: header to thread
      gnus-summary-make-false-root 'dummy        ; Make a false root by dummy-article-creation
      gnus-treat-display-smileys nil             ; Urgh. You must be joking.
      gnus-thread-ignore-subject t               ; Ignore Subject: changes
      gnus-thread-sort-functions '(gnus-thread-sort-by-number
                                   gnus-thread-sort-by-date  ; Dates matter...
                                   gnus-thread-sort-by-total-score) ; .. and scores matter more
      gnus-thread-hide-killed t)                 ; Hiding propagates to subtrees

;; Stop the tree window from eating the world.

(gnus-add-configuration '(article (vertical 1.0 (summary 0.15 point)       ; Shrink the horribly greedy tree buffer
                                                (tree 10)
                                                (article 1.0))))

(require 'dot-gnus-articles)
(require 'dot-gnus-mail)

(gnus-demon-add-rescan)                          ; Scan for new news every two hours
(gnus-demon-add-scanmail)                        ; Likewise for mail

(gnus-compile)                                   ; Compile the menu specs &c