[gnu.emacs.gnus] Automaticly selecting your NNTP server

sra@lcs.mit.edu (Rob Austein) (11/12/89)

Here's a short function I wrote that might be useful to others.  The
effect is that GNUS remembers which server I was using most recently
and defaults to using that one automaticly.

I've been having to switch back and forth between several different
NNTP servers, due to local problems that aren't relevant here.  Having
a separate .newsrc-HOSTNAME file for each machine helps a lot (thanks,
Umeda-san), but it's a pain to have to specify which server to use
each time, and I can't just wire it in because it keeps changing.
Hence the following code, which compares the write dates of all of the
.newrc-HOSTNAME.el files in my home directory and picks the server
associated with the most recently written one.

Anyway, here's the code.

(defun my-gnus-autoselect-server ()
  "Pick an NNTP server for GNUS by seeing which one we used most recently."
  (let* ((l (directory-files "~" t "^\\.newsrc-.+\\.el$"))
	 (f (car l)))
    (while (setq l (cdr l))
      (if (file-newer-than-file-p (car l) f)
	  (setq f (car l))))
    (and (stringp f)
	 (string-match "/\\.newsrc-\\([^/]+\\)\\.el$" f)
	 (substring f (match-beginning 1) (match-end 1)))))

;; If (my-gnus-autoselect-nntp-server) can't figure out a good server
;; to use, it returns nil, so you can use an (or...) form here if you
;; want a default for this case.

(setq gnus-nntp-server (my-gnus-autoselect-nntp-server))

--Rob Austein