gnus-start.el 113 KB
Newer Older
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1
;;; gnus-start.el --- startup functions for Gnus
2

3
;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
4

5
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
6 7 8 9
;; Keywords: news

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
14 15 16

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
18 19 20
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
22 23 24 25 26 27 28 29 30 31 32

;;; Commentary:

;;; Code:

(require 'gnus)
(require 'gnus-win)
(require 'gnus-int)
(require 'gnus-spec)
(require 'gnus-range)
(require 'gnus-util)
33 34
(autoload 'message-make-date "message")
(autoload 'gnus-agent-read-servers-validate "gnus-agent")
35
(autoload 'gnus-agent-save-local "gnus-agent")
36
(autoload 'gnus-agent-possibly-alter-active "gnus-agent")
37

38
(eval-when-compile
Dan Nicolaescu's avatar
Dan Nicolaescu committed
39
  (require 'cl))
40

Dan Nicolaescu's avatar
Dan Nicolaescu committed
41 42 43
(defvar gnus-agent-covered-methods)
(defvar gnus-agent-file-loading-local)
(defvar gnus-agent-file-loading-cache)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
44 45 46 47 48 49 50

(defcustom gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
  "Your `.newsrc' file.
`.newsrc-SERVER' will be used instead if that exists."
  :group 'gnus-start
  :type 'file)

51
(defcustom gnus-backup-startup-file 'never
Miles Bader's avatar
Miles Bader committed
52
  "Control use of version numbers for backups of `gnus-startup-file'.
53 54
This variable takes the same values as the `version-control'
variable."
55
  :version "22.1"
56 57 58 59 60 61 62 63 64 65
  :group 'gnus-start
  :type '(choice (const :tag "Never" never)
		 (const :tag "If existing" nil)
		 (other :tag "Always" t)))

(defcustom gnus-save-startup-file-via-temp-buffer t
  "Whether to write the startup file contents to a buffer then save
the buffer or write directly to the file.  The buffer is faster
because all of the contents are written at once.  The direct write
uses considerably less memory."
66
  :version "22.1"
67 68 69 70
  :group 'gnus-start
  :type '(choice (const :tag "Write via buffer" t)
                 (const :tag "Write directly to file" nil)))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
71
(defcustom gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
72 73
  "Your Gnus Emacs-Lisp startup file name.
If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
74 75 76 77
  :group 'gnus-start
  :type 'file)

(defcustom gnus-site-init-file
78
  (condition-case nil
79 80 81
      (concat (file-name-directory
	       (directory-file-name installation-directory))
	      "site-lisp/gnus-init")
82
    (error nil))
83
  "The site-wide Gnus Emacs-Lisp startup file name, or nil if none.
84
If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
85
  :group 'gnus-start
86
  :type '(choice file (const nil)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

(defcustom gnus-use-dribble-file t
  "*Non-nil means that Gnus will use a dribble file to store user updates.
If Emacs should crash without saving the .newsrc files, complete
information can be restored from the dribble file."
  :group 'gnus-dribble-file
  :type 'boolean)

(defcustom gnus-dribble-directory nil
  "*The directory where dribble files will be saved.
If this variable is nil, the directory where the .newsrc files are
saved will be used."
  :group 'gnus-dribble-file
  :type '(choice directory (const nil)))

102
(defcustom gnus-check-new-newsgroups 'ask-server
103
  "*Non-nil means that Gnus will run `gnus-find-new-newsgroups' at startup.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
104 105 106 107
This normally finds new newsgroups by comparing the active groups the
servers have already reported with those Gnus already knows, either alive
or killed.

108
When any of the following are true, `gnus-find-new-newsgroups' will instead
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
109 110 111 112 113
ask the servers (primary, secondary, and archive servers) to list new
groups since the last time it checked:
  1. This variable is `ask-server'.
  2. This variable is a list of select methods (see below).
  3. `gnus-read-active-file' is nil or `some'.
114
  4. A prefix argument is given to `gnus-find-new-newsgroups' interactively.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

Thus, if this variable is `ask-server' or a list of select methods or
`gnus-read-active-file' is nil or `some', then the killed list is no
longer necessary, so you could safely set `gnus-save-killed-list' to nil.

This variable can be a list of select methods which Gnus will query with
the `ask-server' method in addition to the primary, secondary, and archive
servers.

Eg.
  (setq gnus-check-new-newsgroups
	'((nntp \"some.server\") (nntp \"other.server\")))

If this variable is nil, then you have to tell Gnus explicitly to
check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups]."
  :group 'gnus-start
  :type '(choice (const :tag "no" nil)
		 (const :tag "by brute force" t)
		 (const :tag "ask servers" ask-server)
		 (repeat :menu-tag "ask additional servers"
			 :tag "ask additional servers"
			 :value ((nntp ""))
			 (sexp :format "%v"))))

(defcustom gnus-check-bogus-newsgroups nil
  "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
If this variable is nil, then you have to tell Gnus explicitly to
check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups]."
  :group 'gnus-start-server
  :type 'boolean)

146
(defcustom gnus-read-active-file 'some
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  "*Non-nil means that Gnus will read the entire active file at startup.
If this variable is nil, Gnus will only know about the groups in your
`.newsrc' file.

If this variable is `some', Gnus will try to only read the relevant
parts of the active file from the server.  Not all servers support
this, and it might be quite slow with other servers, but this should
generally be faster than both the t and nil value.

If you set this variable to nil or `some', you probably still want to
be told about new newsgroups that arrive.  To do that, set
`gnus-check-new-newsgroups' to `ask-server'.  This may not work
properly with all servers."
  :group 'gnus-start-server
  :type '(choice (const nil)
		 (const some)
		 (const t)))

165 166
(defconst gnus-level-subscribed 5
  "Groups with levels less than or equal to this variable are subscribed.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
167

168 169
(defconst gnus-level-unsubscribed 7
  "Groups with levels less than or equal to this variable are unsubscribed.
Miles Bader's avatar
Miles Bader committed
170 171 172 173 174

Groups with levels less than `gnus-level-subscribed', which
should be less than this variable, are subscribed.  Groups with
levels from `gnus-level-subscribed' (exclusive) upto this
variable (inclusive) are unsubscribed.  See also
175
`gnus-level-zombie', `gnus-level-killed' and the Info node `(gnus)Group
Miles Bader's avatar
Miles Bader committed
176
Levels' for details.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
177

178 179
(defconst gnus-level-zombie 8
  "Groups with this level are zombie groups.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
180

181 182
(defconst gnus-level-killed 9
  "Groups with this level are killed.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

(defcustom gnus-level-default-subscribed 3
  "*New subscribed groups will be subscribed at this level."
  :group 'gnus-group-levels
  :type 'integer)

(defcustom gnus-level-default-unsubscribed 6
  "*New unsubscribed groups will be unsubscribed at this level."
  :group 'gnus-group-levels
  :type 'integer)

(defcustom gnus-activate-level (1+ gnus-level-subscribed)
  "*Groups higher than this level won't be activated on startup.
Setting this variable to something low might save lots of time when
you have many groups that you aren't interested in."
  :group 'gnus-group-levels
  :type 'integer)

(defcustom gnus-activate-foreign-newsgroups 4
  "*If nil, Gnus will not check foreign newsgroups at startup.
If it is non-nil, it should be a number between one and nine.  Foreign
newsgroups that have a level lower or equal to this number will be
activated on startup.  For instance, if you want to active all
subscribed newsgroups, but not the rest, you'd set this variable to
`gnus-level-subscribed'.

If you subscribe to lots of newsgroups from different servers, startup
might take a while.  By setting this variable to nil, you'll save time,
but you won't be told how many unread articles there are in the
groups."
  :group 'gnus-group-levels
214 215
  :type '(choice integer
		 (const :tag "none" nil)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
216

217 218 219 220 221 222 223
(defcustom gnus-read-newsrc-file t
  "*Non-nil means that Gnus will read the `.newsrc' file.
Gnus always reads its own startup file, which is called
\".newsrc.eld\".  The file called \".newsrc\" is in a format that can
be readily understood by other newsreaders.  If you don't plan on
using other newsreaders, set this variable to nil to save some time on
entry."
224
  :version "21.1"
225 226 227
  :group 'gnus-newsrc
  :type 'boolean)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
(defcustom gnus-save-newsrc-file t
  "*Non-nil means that Gnus will save the `.newsrc' file.
Gnus always saves its own startup file, which is called
\".newsrc.eld\".  The file called \".newsrc\" is in a format that can
be readily understood by other newsreaders.  If you don't plan on
using other newsreaders, set this variable to nil to save some time on
exit."
  :group 'gnus-newsrc
  :type 'boolean)

(defcustom gnus-save-killed-list t
  "*If non-nil, save the list of killed groups to the startup file.
If you set this variable to nil, you'll save both time (when starting
and quitting) and space (both memory and disk), but it will also mean
that Gnus has no record of which groups are new and which are old, so
the automatic new newsgroups subscription methods become meaningless.

You should always set `gnus-check-new-newsgroups' to `ask-server' or
nil if you set this variable to nil.

This variable can also be a regexp.  In that case, all groups that do
not match this regexp will be removed before saving the list."
  :group 'gnus-newsrc
251 252 253 254 255
  :type '(radio (sexp :format "Non-nil\n"
		      :match (lambda (widget value)
			       (and value (not (stringp value))))
		      :value t)
		(const nil)
256
		regexp))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
257 258

(defcustom gnus-ignored-newsgroups
259 260
  (mapconcat 'identity
	     '("^to\\."			; not "real" groups
261
	       "^[0-9. \t]+\\( \\|$\\)"	; all digits in name
262
	       "^[\"][\"#'()]"	; bogus characters
263 264
	       )
	     "\\|")
265
  "*A regexp to match uninteresting newsgroups in the active file.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
266 267 268 269 270 271 272
Any lines in the active file matching this regular expression are
removed from the newsgroup list before anything else is done to it,
thus making them effectively non-existent."
  :group 'gnus-group-new
  :type 'regexp)

(defcustom gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
273
  "*Function(s) called with a group name when new group is detected.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
274 275 276 277 278 279
A few pre-made functions are supplied: `gnus-subscribe-randomly'
inserts new groups at the beginning of the list of groups;
`gnus-subscribe-alphabetically' inserts new groups in strict
alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
for your decision; `gnus-subscribe-killed' kills all new groups;
280 281 282
`gnus-subscribe-zombies' will make all new groups into zombies;
`gnus-subscribe-topics' will enter groups into the topics that
claim them."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
283 284 285 286 287 288 289
  :group 'gnus-group-new
  :type '(radio (function-item gnus-subscribe-randomly)
		(function-item gnus-subscribe-alphabetically)
		(function-item gnus-subscribe-hierarchically)
		(function-item gnus-subscribe-interactively)
		(function-item gnus-subscribe-killed)
		(function-item gnus-subscribe-zombies)
290
		(function-item gnus-subscribe-topics)
291 292 293 294 295 296
		function
		(repeat function)))

(defcustom gnus-subscribe-newsgroup-hooks nil
  "*Hooks run after you subscribe to a new group.
The hooks will be called with new group's name as argument."
297
  :version "22.1"
298 299
  :group 'gnus-group-new
  :type 'hook)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
300 301 302

(defcustom gnus-subscribe-options-newsgroup-method
  'gnus-subscribe-alphabetically
303
  "*Function(s) called to subscribe newsgroups mentioned on \"options -n\" lines.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
304 305 306 307 308 309
If, for instance, you want to subscribe to all newsgroups in the
\"no\" and \"alt\" hierarchies, you'd put the following in your
.newsrc file:

options -n no.all alt.all

Miles Bader's avatar
Miles Bader committed
310 311
Gnus will then subscribe all new newsgroups in these hierarchies
with the subscription method in this variable."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
312 313 314 315 316 317 318
  :group 'gnus-group-new
  :type '(radio (function-item gnus-subscribe-randomly)
		(function-item gnus-subscribe-alphabetically)
		(function-item gnus-subscribe-hierarchically)
		(function-item gnus-subscribe-interactively)
		(function-item gnus-subscribe-killed)
		(function-item gnus-subscribe-zombies)
319 320 321
		(function-item gnus-subscribe-topics)
		function
		(repeat function)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
322 323 324 325 326 327 328 329 330 331 332 333 334

(defcustom gnus-subscribe-hierarchical-interactive nil
  "*If non-nil, Gnus will offer to subscribe hierarchically.
When a new hierarchy appears, Gnus will ask the user:

'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):

If the user pressed `d', Gnus will descend the hierarchy, `y' will
subscribe to all newsgroups in the hierarchy and `s' will skip this
hierarchy in its entirety."
  :group 'gnus-group-new
  :type 'boolean)

335 336 337 338 339 340 341 342 343
(defcustom gnus-auto-subscribed-categories '(mail post-mail)
  "*New groups from methods of these categories will be subscribed automatically.
Note that this variable only deals with new groups.  It has no
effect whatsoever on old groups.  The default is to automatically
subscribe all groups from mail-like backends."
  :version "24.1"
  :group 'gnus-group-new
  :type '(repeat symbol))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
344
(defcustom gnus-auto-subscribed-groups
345
  "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl\\|^nnmaildir\\|^nnimap"
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
346 347 348 349 350 351 352 353 354 355 356 357
  "*All new groups that match this regexp will be subscribed automatically.
Note that this variable only deals with new groups.  It has no effect
whatsoever on old groups.

New groups that match this regexp will not be handled by
`gnus-subscribe-newsgroup-method'.  Instead, they will
be subscribed using `gnus-subscribe-options-newsgroup-method'."
  :group 'gnus-group-new
  :type 'regexp)

(defcustom gnus-options-subscribe nil
  "*All new groups matching this regexp will be subscribed unconditionally.
358
Note that this variable deals only with new newsgroups.  This variable
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
359 360 361 362 363 364 365 366 367 368 369
does not affect old newsgroups.

New groups that match this regexp will not be handled by
`gnus-subscribe-newsgroup-method'.  Instead, they will
be subscribed using `gnus-subscribe-options-newsgroup-method'."
  :group 'gnus-group-new
  :type '(choice regexp
		 (const :tag "none" nil)))

(defcustom gnus-options-not-subscribe nil
  "*All new groups matching this regexp will be ignored.
370
Note that this variable deals only with new newsgroups.  This variable
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
371 372 373 374 375 376 377 378 379 380 381 382
does not affect old (already subscribed) newsgroups."
  :group 'gnus-group-new
  :type '(choice regexp
		 (const :tag "none" nil)))

(defcustom gnus-modtime-botch nil
  "*Non-nil means .newsrc should be deleted prior to save.
Its use is due to the bogus appearance that .newsrc was modified on
disc."
  :group 'gnus-newsrc
  :type 'boolean)

383 384 385 386 387 388 389
(defcustom gnus-use-backend-marks nil
  "If non-nil, Gnus will store and retrieve marks from the backends.
This means that marks will be stored both in .newsrc.eld and in
the backend, and will slow operation down somewhat."
  :group 'gnus-newsrc
  :type 'boolean)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
390 391 392 393 394 395 396 397 398 399 400
(defcustom gnus-check-bogus-groups-hook nil
  "A hook run after removing bogus groups."
  :group 'gnus-start-server
  :type 'hook)

(defcustom gnus-startup-hook nil
  "A hook called at startup.
This hook is called after Gnus is connected to the NNTP server."
  :group 'gnus-start
  :type 'hook)

401
(defcustom gnus-before-startup-hook nil
Miles Bader's avatar
Miles Bader committed
402
  "A hook called before startup.
403 404 405 406
This hook is called as the first thing when Gnus is started."
  :group 'gnus-start
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
407 408 409 410 411
(defcustom gnus-started-hook nil
  "A hook called as the last thing after startup."
  :group 'gnus-start
  :type 'hook)

412
(defcustom gnus-setup-news-hook nil
413 414 415 416
  "A hook after reading the .newsrc file, but before generating the buffer."
  :group 'gnus-start
  :type 'hook)

417 418
(defcustom gnus-get-top-new-news-hook nil
  "A hook run just before Gnus checks for new news globally."
419
  :version "22.1"
420 421 422
  :group 'gnus-group-new
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
423 424 425 426 427 428
(defcustom gnus-get-new-news-hook nil
  "A hook run just before Gnus checks for new news."
  :group 'gnus-group-new
  :type 'hook)

(defcustom gnus-after-getting-new-news-hook
429
  '(gnus-display-time-event-handler)
430
  "*A hook run after Gnus checks for new news when Gnus is already running."
431
  :version "24.1"
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
432 433 434
  :group 'gnus-group-new
  :type 'hook)

435 436 437 438 439
(defcustom gnus-read-newsrc-el-hook nil
  "A hook called after reading the newsrc.eld? file."
  :group 'gnus-newsrc
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
(defcustom gnus-save-newsrc-hook nil
  "A hook called before saving any of the newsrc files."
  :group 'gnus-newsrc
  :type 'hook)

(defcustom gnus-save-quick-newsrc-hook nil
  "A hook called just before saving the quick newsrc file.
Can be used to turn version control on or off."
  :group 'gnus-newsrc
  :type 'hook)

(defcustom gnus-save-standard-newsrc-hook nil
  "A hook called just before saving the standard newsrc file.
Can be used to turn version control on or off."
  :group 'gnus-newsrc
  :type 'hook)

457 458 459 460 461 462
(defcustom gnus-group-mode-hook nil
  "Hook for Gnus group mode."
  :group 'gnus-group-various
  :options '(gnus-topic-mode)
  :type 'hook)

463
(defcustom gnus-always-read-dribble-file nil
464
  "Unconditionally read the dribble file."
465 466 467
  :group 'gnus-newsrc
  :type 'boolean)

468 469
;;; Internal variables

470 471
;; Fixme: deal with old emacs-mule when mm-universal-coding-system is
;; utf-8-emacs.
472 473
(defvar gnus-ding-file-coding-system mm-universal-coding-system
  "Coding system for ding file.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

(defvar gnus-newsrc-file-version nil)
(defvar gnus-override-subscribe-method nil)
(defvar gnus-dribble-buffer nil)
(defvar gnus-newsrc-options nil
  "Options line in the .newsrc file.")

(defvar gnus-newsrc-options-n nil
  "List of regexps representing groups to be subscribed/ignored unconditionally.")

(defvar gnus-newsrc-last-checked-date nil
  "Date Gnus last asked server for new newsgroups.")

(defvar gnus-current-startup-file nil
  "Startup file for the current host.")

;; Byte-compiler warning.
(defvar gnus-group-line-format)

;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
(defvar gnus-init-inhibit nil)
(defun gnus-read-init-file (&optional inhibit-next)
  ;; Don't load .gnus if the -q option was used.
  (when init-file-user
    (if gnus-init-inhibit
	(setq gnus-init-inhibit nil)
      (setq gnus-init-inhibit inhibit-next)
501 502 503 504 505 506 507 508
      (dolist (file (list gnus-site-init-file gnus-init-file))
	(when (and file
		   (locate-library file))
	  (if (or debug-on-error debug-on-quit)
	      (load file nil t)
	    (condition-case var
		(load file nil t)
	      (error
509
	       (error "Error in %s: %s" file (cadr var))))))))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
510 511 512 513 514

;; For subscribing new newsgroup

(defun gnus-subscribe-hierarchical-interactive (groups)
  (let ((groups (sort groups 'string<))
515
	prefixes prefix start ans group starts real-group)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
516 517 518
    (while groups
      (setq prefixes (list "^"))
      (while (and groups prefixes)
519 520
	(while (not (string-match (car prefixes)
				  (gnus-group-real-name (car groups))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
521 522 523
	  (setq prefixes (cdr prefixes)))
	(setq prefix (car prefixes))
	(setq start (1- (length prefix)))
524 525
	(if (and (string-match "[^\\.]\\." (gnus-group-real-name (car groups))
			       start)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
526 527
		 (cdr groups)
		 (setq prefix
528 529 530 531
		       (concat "^" (substring
				    (gnus-group-real-name (car groups))
				    0 (match-end 0))))
		 (string-match prefix (gnus-group-real-name (cadr groups))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
532 533 534 535
	    (progn
	      (push prefix prefixes)
	      (message "Descend hierarchy %s? ([y]nsq): "
		       (substring prefix 1 (1- (length prefix))))
536 537
	      (while (not (memq (setq ans (read-char-exclusive))
				'(?y ?\n ?\r ?n ?s ?q)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
538 539 540 541 542
		(ding)
		(message "Descend hierarchy %s? ([y]nsq): "
			 (substring prefix 1 (1- (length prefix)))))
	      (cond ((= ans ?n)
		     (while (and groups
543 544 545
				 (setq group (car groups)
				       real-group (gnus-group-real-name group))
				 (string-match prefix real-group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
546 547 548 549 550 551
		       (push group gnus-killed-list)
		       (gnus-sethash group group gnus-killed-hashtb)
		       (setq groups (cdr groups)))
		     (setq starts (cdr starts)))
		    ((= ans ?s)
		     (while (and groups
552 553 554
				 (setq group (car groups)
				       real-group (gnus-group-real-name group))
				 (string-match prefix real-group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
555 556 557 558 559 560 561 562 563 564 565 566
		       (gnus-sethash group group gnus-killed-hashtb)
		       (gnus-subscribe-alphabetically (car groups))
		       (setq groups (cdr groups)))
		     (setq starts (cdr starts)))
		    ((= ans ?q)
		     (while groups
		       (setq group (car groups))
		       (push group gnus-killed-list)
		       (gnus-sethash group group gnus-killed-hashtb)
		       (setq groups (cdr groups))))
		    (t nil)))
	  (message "Subscribe %s? ([n]yq)" (car groups))
567 568
	  (while (not (memq (setq ans (read-char-exclusive))
			    '(?y ?\n ?\r ?q ?n)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	    (ding)
	    (message "Subscribe %s? ([n]yq)" (car groups)))
	  (setq group (car groups))
	  (cond ((= ans ?y)
		 (gnus-subscribe-alphabetically (car groups))
		 (gnus-sethash group group gnus-killed-hashtb))
		((= ans ?q)
		 (while groups
		   (setq group (car groups))
		   (push group gnus-killed-list)
		   (gnus-sethash group group gnus-killed-hashtb)
		   (setq groups (cdr groups))))
		(t
		 (push group gnus-killed-list)
		 (gnus-sethash group group gnus-killed-hashtb)))
	  (setq groups (cdr groups)))))))

(defun gnus-subscribe-randomly (newsgroup)
  "Subscribe new NEWSGROUP by making it the first newsgroup."
  (gnus-subscribe-newsgroup newsgroup))

(defun gnus-subscribe-alphabetically (newgroup)
591
  "Subscribe new NEWGROUP and insert it in alphabetical order."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
592 593 594 595 596 597 598 599 600
  (let ((groups (cdr gnus-newsrc-alist))
	before)
    (while (and (not before) groups)
      (if (string< newgroup (caar groups))
	  (setq before (caar groups))
	(setq groups (cdr groups))))
    (gnus-subscribe-newsgroup newgroup before)))

(defun gnus-subscribe-hierarchically (newgroup)
601
  "Subscribe new NEWGROUP and insert it in hierarchical newsgroup order."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
602
  ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
603
  (with-current-buffer (nnheader-find-file-noselect gnus-current-startup-file)
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    (prog1
	(let ((groupkey newgroup) before)
	  (while (and (not before) groupkey)
	    (goto-char (point-min))
	    (let ((groupkey-re
		   (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
	      (while (and (re-search-forward groupkey-re nil t)
			  (progn
			    (setq before (match-string 1))
			    (string< before newgroup)))))
	    ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
	    (setq groupkey
		  (when (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
		    (substring groupkey (match-beginning 1) (match-end 1)))))
	  (gnus-subscribe-newsgroup newgroup before))
      (kill-buffer (current-buffer)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
620 621 622 623 624

(defun gnus-subscribe-interactively (group)
  "Subscribe the new GROUP interactively.
It is inserted in hierarchical newsgroup order if subscribed.  If not,
it is killed."
625
  (if (gnus-y-or-n-p (format "Subscribe new newsgroup %s? " group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
626 627 628 629 630 631 632 633 634 635 636 637 638
      (gnus-subscribe-hierarchically group)
    (push group gnus-killed-list)))

(defun gnus-subscribe-zombies (group)
  "Make the new GROUP into a zombie group."
  (push group gnus-zombie-list))

(defun gnus-subscribe-killed (group)
  "Make the new GROUP a killed group."
  (push group gnus-killed-list))

(defun gnus-subscribe-newsgroup (newsgroup &optional next)
  "Subscribe new NEWSGROUP.
639
If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
640 641 642 643 644 645
the first newsgroup."
  (save-excursion
    (goto-char (point-min))
    ;; We subscribe the group by changing its level to `subscribed'.
    (gnus-group-change-level
     newsgroup gnus-level-default-subscribed
646
     gnus-level-killed (gnus-group-entry (or next "dummy.group")))
647
    (gnus-request-update-group-status newsgroup 'subscribe)
648 649 650
    (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)
    (run-hook-with-args 'gnus-subscribe-newsgroup-hooks newsgroup)
    t))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
651 652 653 654 655 656 657 658

(defun gnus-read-active-file-p ()
  "Say whether the active file has been read from `gnus-select-method'."
  (memq gnus-select-method gnus-have-read-active-file))

;;; General various misc type functions.

;; Silence byte-compiler.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672
(defvar gnus-current-headers)
(defvar gnus-thread-indent-array)
(defvar gnus-newsgroup-name)
(defvar gnus-newsgroup-headers)
(defvar gnus-group-list-mode)
(defvar gnus-group-mark-positions)
(defvar gnus-newsgroup-data)
(defvar gnus-newsgroup-unreads)
(defvar nnoo-state-alist)
(defvar gnus-current-select-method)
(defvar mail-sources)
(defvar nnmail-scan-directory-mail-source-once)
(defvar nnmail-split-history)
(defvar nnmail-spool-file)
673 674 675 676 677 678

(defun gnus-close-all-servers ()
  "Close all servers."
  (interactive)
  (dolist (server gnus-opened-servers)
    (gnus-close-server (car server))))
679

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
680 681 682
(defun gnus-clear-system ()
  "Clear all variables and buffers."
  ;; Clear Gnus variables.
683
  (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
684 685 686 687 688 689
    (while variables
      (set (car variables) nil)
      (setq variables (cdr variables))))
  ;; Clear other internal variables.
  (setq gnus-list-of-killed-groups nil
	gnus-have-read-active-file nil
690
        gnus-agent-covered-methods nil
691 692
        gnus-agent-file-loading-local nil
        gnus-agent-file-loading-cache nil
693
        gnus-server-method-cache nil
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	gnus-newsrc-alist nil
	gnus-newsrc-hashtb nil
	gnus-killed-list nil
	gnus-zombie-list nil
	gnus-killed-hashtb nil
	gnus-active-hashtb nil
	gnus-moderated-hashtb nil
	gnus-description-hashtb nil
	gnus-current-headers nil
	gnus-thread-indent-array nil
	gnus-newsgroup-headers nil
	gnus-newsgroup-name nil
	gnus-server-alist nil
	gnus-group-list-mode nil
	gnus-opened-servers nil
	gnus-group-mark-positions nil
	gnus-newsgroup-data nil
	gnus-newsgroup-unreads nil
	nnoo-state-alist nil
713
	gnus-current-select-method nil
714
	nnmail-split-history nil
715
	gnus-extended-servers nil
716
	gnus-ephemeral-servers nil)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
717 718 719 720 721 722 723 724 725 726 727 728
  (gnus-shutdown 'gnus)
  ;; Kill the startup file.
  (and gnus-current-startup-file
       (get-file-buffer gnus-current-startup-file)
       (kill-buffer (get-file-buffer gnus-current-startup-file)))
  ;; Clear the dribble buffer.
  (gnus-dribble-clear)
  ;; Kill global KILL file buffer.
  (when (get-file-buffer (gnus-newsgroup-kill-file nil))
    (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
  (gnus-kill-buffer nntp-server-buffer)
  ;; Kill Gnus buffers.
729 730
  (dolist (buffer (gnus-buffers))
    (gnus-kill-buffer buffer))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
731 732 733 734 735
  ;; Remove Gnus frames.
  (gnus-kill-gnus-frames))

(defun gnus-no-server-1 (&optional arg slave)
  "Read network news.
736 737 738 739 740 741
If ARG is a positive number, Gnus will use that as the startup
level.  If ARG is nil, Gnus will be started at level 2
\(`gnus-level-default-subscribed' minus one).  If ARG is non-nil
and not a positive number, Gnus will prompt the user for the name
of an NNTP server to use.  As opposed to \\[gnus], this command
will not connect to the local server."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
742 743 744 745 746 747 748 749 750
  (interactive "P")
  (let ((val (or arg (1- gnus-level-default-subscribed))))
    (gnus val t slave)
    (make-local-variable 'gnus-group-use-permanent-levels)
    (setq gnus-group-use-permanent-levels val)))

(defun gnus-1 (&optional arg dont-connect slave)
  "Read network news.
If ARG is non-nil and a positive number, Gnus will use that as the
751
startup level.  If ARG is non-nil and not a positive number, Gnus will
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
752 753 754
prompt the user for the name of an NNTP server to use."
  (interactive "P")

755
  (if (gnus-alive-p)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
756 757 758 759 760 761 762 763
      (progn
	(switch-to-buffer gnus-group-buffer)
	(gnus-group-get-new-news
	 (and (numberp arg)
	      (> arg 0)
	      (max (car gnus-group-list-mode) arg))))

    (gnus-clear-system)
764 765
    (gnus-splash)
    (gnus-run-hooks 'gnus-before-startup-hook)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
766 767
    (nnheader-init-server-buffer)
    (setq gnus-slave slave)
768
    (gnus-read-init-file)
769 770 771 772 773 774

    ;; Add "native" to gnus-predefined-server-alist just to have a
    ;; name for the native select method.
    (when gnus-select-method
      (push (cons "native" gnus-select-method)
	    gnus-predefined-server-alist))
775

776 777
    (if gnus-agent
	(gnus-agentize))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
778 779 780 781 782 783 784 785 786 787

    (let ((level (and (numberp arg) (> arg 0) arg))
	  did-connect)
      (unwind-protect
	  (progn
	    (unless dont-connect
	      (setq did-connect
		    (gnus-start-news-server (and arg (not level))))))
	(if (and (not dont-connect)
		 (not did-connect))
788
	    ;; Couldn't connect to the server, so bail out.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
789
	    (gnus-group-quit)
790
	  (gnus-run-hooks 'gnus-startup-hook)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
791 792 793 794 795 796 797 798 799 800
	  ;; Find the current startup file name.
	  (setq gnus-current-startup-file
		(gnus-make-newsrc-file gnus-startup-file))

	  ;; Read the dribble file.
	  (when (or gnus-slave gnus-use-dribble-file)
	    (gnus-dribble-read-file))

	  ;; Do the actual startup.
	  (gnus-setup-news nil level dont-connect)
801
	  (gnus-run-hooks 'gnus-setup-news-hook)
802 803
	  (when gnus-agent
	    (gnus-request-create-group "queue" '(nndraft "")))
804
	  (gnus-start-draft-setup)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
805 806 807 808 809
	  ;; Generate the group buffer.
	  (gnus-group-list-groups level)
	  (gnus-group-first-unread-group)
	  (gnus-configure-windows 'group)
	  (gnus-group-set-mode-line)
810 811 812 813
	  (gnus-run-hooks 'gnus-started-hook))))))

(defun gnus-start-draft-setup ()
  "Make sure the draft group exists."
814
  (interactive)
815
  (gnus-request-create-group "drafts" '(nndraft ""))
816
  (unless (gnus-group-entry "nndraft:drafts")
817
    (let ((gnus-level-default-subscribed 1))
818 819
      (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
    (setcar (gnus-group-entry "nndraft:drafts") 0))
820 821
  (unless (equal (gnus-group-get-parameter "nndraft:drafts" 'gnus-dummy t)
		 '((gnus-draft-mode)))
822 823
    (gnus-group-set-parameter
     "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850


;;;
;;; Dribble file
;;;

(defvar gnus-dribble-ignore nil)
(defvar gnus-dribble-eval-file nil)

(defun gnus-dribble-file-name ()
  "Return the dribble file for the current .newsrc."
  (concat
   (if gnus-dribble-directory
       (concat (file-name-as-directory gnus-dribble-directory)
	       (file-name-nondirectory gnus-current-startup-file))
     gnus-current-startup-file)
   "-dribble"))

(defun gnus-dribble-enter (string)
  "Enter STRING into the dribble buffer."
  (when (and (not gnus-dribble-ignore)
	     gnus-dribble-buffer
	     (buffer-name gnus-dribble-buffer))
    (let ((obuf (current-buffer)))
      (set-buffer gnus-dribble-buffer)
      (goto-char (point-max))
      (insert string "\n")
851 852 853 854 855
      ;; This has been commented by Josh Huber <huber@alum.wpi.edu>
      ;; It causes problems with both XEmacs and Emacs 21, and doesn't
      ;; seem to be of much value. (FIXME: remove this after we make sure
      ;; it's not needed).
      ;; (set-window-point (get-buffer-window (current-buffer)) (point-max))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
856
      (bury-buffer gnus-dribble-buffer)
857
      (with-current-buffer gnus-group-buffer
858
	(gnus-group-set-mode-line))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
859 860 861 862 863 864 865 866 867
      (set-buffer obuf))))

(defun gnus-dribble-touch ()
  "Touch the dribble buffer."
  (gnus-dribble-enter ""))

(defun gnus-dribble-read-file ()
  "Read the dribble file from disk."
  (let ((dribble-file (gnus-dribble-file-name)))
868 869
    (unless (file-exists-p (file-name-directory dribble-file))
      (make-directory (file-name-directory dribble-file) t))
870 871 872
    (with-current-buffer (setq gnus-dribble-buffer
			       (gnus-get-buffer-create
				(file-name-nondirectory dribble-file)))
873
      (set (make-local-variable 'file-precious-flag) t)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
874 875 876
      (erase-buffer)
      (setq buffer-file-name dribble-file)
      (auto-save-mode t)
877
      (buffer-disable-undo)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
878 879 880 881
      (bury-buffer (current-buffer))
      (set-buffer-modified-p nil)
      (let ((auto (make-auto-save-file-name))
	    (gnus-dribble-ignore t)
882
	    (purpose nil)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
883 884 885 886 887 888 889 890 891 892 893 894
	    modes)
	(when (or (file-exists-p auto) (file-exists-p dribble-file))
	  ;; Load whichever file is newest -- the auto save file
	  ;; or the "real" file.
	  (if (file-newer-than-file-p auto dribble-file)
	      (nnheader-insert-file-contents auto)
	    (nnheader-insert-file-contents dribble-file))
	  (unless (zerop (buffer-size))
	    (set-buffer-modified-p t))
	  ;; Set the file modes to reflect the .newsrc file modes.
	  (save-buffer)
	  (when (and (file-exists-p gnus-current-startup-file)
895
		     (file-exists-p dribble-file)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
896
		     (setq modes (file-modes gnus-current-startup-file)))
897
	    (gnus-set-file-modes dribble-file modes))
898 899 900
	  (goto-char (point-min))
	  (when (search-forward "Gnus was exited on purpose" nil t)
	    (setq purpose t))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
901
	  ;; Possibly eval the file later.
902 903
	  (when (or gnus-always-read-dribble-file
		    (gnus-y-or-n-p
904 905 906
		     (if purpose
			 "Gnus exited on purpose without saving; read auto-save file anyway? "
		     "Gnus auto-save file exists.  Do you want to read it? ")))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920
	    (setq gnus-dribble-eval-file t)))))))

(defun gnus-dribble-eval-file ()
  (when gnus-dribble-eval-file
    (setq gnus-dribble-eval-file nil)
    (save-excursion
      (let ((gnus-dribble-ignore t))
	(set-buffer gnus-dribble-buffer)
	(eval-buffer (current-buffer))))))

(defun gnus-dribble-delete-file ()
  (when (file-exists-p (gnus-dribble-file-name))
    (delete-file (gnus-dribble-file-name)))
  (when gnus-dribble-buffer
921
    (with-current-buffer gnus-dribble-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
922 923 924 925 926 927 928 929 930
      (let ((auto (make-auto-save-file-name)))
	(when (file-exists-p auto)
	  (delete-file auto))
	(erase-buffer)
	(set-buffer-modified-p nil)))))

(defun gnus-dribble-save ()
  (when (and gnus-dribble-buffer
	     (buffer-name gnus-dribble-buffer))
931
    (with-current-buffer gnus-dribble-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
932 933 934 935
      (save-buffer))))

(defun gnus-dribble-clear ()
  (when (gnus-buffer-exists-p gnus-dribble-buffer)
936
    (with-current-buffer gnus-dribble-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
937 938 939 940 941 942 943 944 945 946 947 948 949
      (erase-buffer)
      (set-buffer-modified-p nil)
      (setq buffer-saved-size (buffer-size)))))


;;;
;;; Active & Newsrc File Handling
;;;

(defun gnus-setup-news (&optional rawfile level dont-connect)
  "Setup news information.
If RAWFILE is non-nil, the .newsrc file will also be read.
If LEVEL is non-nil, the news will be set up at level LEVEL."
950 951 952 953 954
  (require 'nnmail)
  (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
	;; Binding this variable will inhibit multiple fetchings
	;; of the same mail source.
	(nnmail-fetched-sources (list t)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
955 956 957 958 959 960 961 962

    (when init
      ;; Clear some variables to re-initialize news information.
      (setq gnus-newsrc-alist nil
	    gnus-active-hashtb nil)
      ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
      (gnus-read-newsrc-file rawfile))

963
    ;; Make sure the archive server is available to all and sundry.
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    (let ((method (or (and (stringp gnus-message-archive-method)
			   (gnus-server-to-method
			    gnus-message-archive-method))
		      gnus-message-archive-method)))
      ;; Check whether the archive method is writable.
      (unless (or (not method)
		  (stringp method)
		  (memq 'respool (assoc (format "%s" (car method))
					gnus-valid-select-methods)))
	(setq method "archive")) ;; The default.
      (when (stringp method)
	(setq method `(nnfolder
		       ,method
		       (nnfolder-directory
			,(nnheader-concat message-directory method))
		       (nnfolder-active-file
			,(nnheader-concat message-directory
					  (concat method "/active")))
		       (nnfolder-get-new-mail nil)
		       (nnfolder-inhibit-expiry t))))
      (if (assoc "archive" gnus-server-alist)
	  (when gnus-update-message-archive-method
	    (if method
		(setcdr (assoc "archive" gnus-server-alist) method)
	      (setq gnus-server-alist (delq (assoc "archive" gnus-server-alist)
					    gnus-server-alist))))
	(when method
	  (push (cons "archive" method) gnus-server-alist))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

    ;; If we don't read the complete active file, we fill in the
    ;; hashtb here.
    (when (or (null gnus-read-active-file)
	      (eq gnus-read-active-file 'some))
      (gnus-update-active-hashtb-from-killed))
    (unless gnus-active-hashtb
      (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
    ;; Initialize the cache.
    (when gnus-use-cache
      (gnus-cache-open))

    ;; Possibly eval the dribble file.
    (and init
	 (or gnus-use-dribble-file gnus-slave)
	 (gnus-dribble-eval-file))

    ;; Slave Gnusii should then clear the dribble buffer.
    (when (and init gnus-slave)
      (gnus-dribble-clear))

    (gnus-update-format-specifications)

    ;; See whether we need to read the description file.
    (when (and (boundp 'gnus-group-line-format)
1017
	       (stringp gnus-group-line-format)
1018 1019
	       (let ((case-fold-search nil))
		 (string-match "%[-,0-9]*D" gnus-group-line-format))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1020 1021 1022 1023 1024 1025 1026 1027
	       (not gnus-description-hashtb)
	       (not dont-connect)
	       gnus-read-active-file)
      (gnus-read-all-descriptions-files))

    ;; Find new newsgroups and treat them.
    (when (and init gnus-check-new-newsgroups (not level)
	       (gnus-check-server gnus-select-method)
1028 1029
	       (not gnus-slave)
	       gnus-plugged)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1030 1031
      (gnus-find-new-newsgroups))

1032 1033 1034 1035 1036 1037
    ;; Check and remove bogus newsgroups.
    (when (and init gnus-check-bogus-newsgroups
	       gnus-read-active-file (not level)
	       (gnus-server-opened gnus-select-method))
      (gnus-check-bogus-newsgroups))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1038 1039 1040 1041 1042
    ;; Read any slave files.
    (gnus-master-read-slave-newsrc)

    ;; Find the number of unread articles in each non-dead group.
    (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
      (gnus-get-unread-articles level))))

(defun gnus-call-subscribe-functions (method group)
  "Call METHOD to subscribe GROUP.
If no function returns `non-nil', call `gnus-subscribe-zombies'."
  (unless (cond
	   ((functionp method)
	    (funcall method group))
	   ((listp method)
	    (catch 'found
	      (dolist (func method)
		(if (funcall func group)
		    (throw 'found t)))
	      nil))
	   (t nil))
    (gnus-subscribe-zombies group)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1059 1060 1061

(defun gnus-find-new-newsgroups (&optional arg)
  "Search for new newsgroups and add them.
1062
Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1063
The `-n' option line from .newsrc is respected.
1064 1065 1066 1067 1068 1069 1070 1071 1072

With 1 C-u, use the `ask-server' method to query the server for new
groups.
With 2 C-u's, use most complete method possible to query the server
for new groups, and subscribe the new groups as zombies."
  (interactive "p")
  (let* ((gnus-subscribe-newsgroup-method
	  gnus-subscribe-newsgroup-method)
	 (check (cond
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
		 ((or (and (= (or arg 1) 4)
			   (not (listp gnus-check-new-newsgroups)))
		      (null gnus-read-active-file)
		      (eq gnus-read-active-file 'some))
		  'ask-server)
		 ((= (or arg 1) 16)
		  (setq gnus-subscribe-newsgroup-method
			'gnus-subscribe-zombies)
		  t)
		 (t gnus-check-new-newsgroups))))
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    (if (or (consp check)
            (eq check 'ask-server))
        ;; Ask the server for new groups.
        (gnus-ask-server-for-new-groups)
      ;; Go through the active hashtb and look for new groups.
      (let ((groups 0)
            group new-newsgroups)
        (gnus-message 5 "Looking for new newsgroups...")
        (unless gnus-have-read-active-file
          (gnus-read-active-file))
        (setq gnus-newsrc-last-checked-date (message-make-date))
        (unless gnus-killed-hashtb
          (gnus-make-hashtable-from-killed))
        ;; Go though every newsgroup in `gnus-active-hashtb' and compare
        ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
        (mapatoms
         (lambda (sym)
           (if (or (null (setq group (symbol-name sym)))
                   (not (boundp sym))
                   (null (symbol-value sym))
                   (gnus-gethash group gnus-killed-hashtb)
                   (gnus-gethash group gnus-newsrc-hashtb))
               ()
             (let ((do-sub (gnus-matches-options-n group)))
               (cond
                ((eq do-sub 'subscribe)
                 (setq groups (1+ groups))
                 (gnus-sethash group group gnus-killed-hashtb)
                 (gnus-call-subscribe-functions
                  gnus-subscribe-options-newsgroup-method group))
                ((eq do-sub 'ignore)
                 nil)
                (t
                 (setq groups (1+ groups))
                 (gnus-sethash group group gnus-killed-hashtb)
                 (if gnus-subscribe-hierarchical-interactive
                     (push group new-newsgroups)
                   (gnus-call-subscribe-functions
                    gnus-subscribe-newsgroup-method group)))))))
         gnus-active-hashtb)
        (when new-newsgroups
          (gnus-subscribe-hierarchical-interactive new-newsgroups))
        (if (> groups 0)
            (gnus-message 5 "%d new newsgroup%s arrived."
                          groups (if (> groups 1) "s have" " has"))
1128 1129
          (gnus-message 5 "No new newsgroups."))
	groups))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140

(defun gnus-matches-options-n (group)
  ;; Returns `subscribe' if the group is to be unconditionally
  ;; subscribed, `ignore' if it is to be ignored, and nil if there is
  ;; no match for the group.

  ;; First we check the two user variables.
  (cond
   ((and gnus-options-subscribe
	 (string-match gnus-options-subscribe group))
    'subscribe)
1141 1142 1143 1144 1145 1146
   ((let ((do-subscribe nil))
      (dolist (category gnus-auto-subscribed-categories)
	(when (gnus-member-of-valid category group)
	  (setq do-subscribe t)))
      do-subscribe)
    'subscribe)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
   ((and gnus-auto-subscribed-groups
	 (string-match gnus-auto-subscribed-groups group))
    'subscribe)
   ((and gnus-options-not-subscribe
	 (string-match gnus-options-not-subscribe group))
    'ignore)
   ;; Then we go through the list that was retrieved from the .newsrc
   ;; file.  This list has elements on the form
   ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
   ;; is in the reverse order of the options line) is returned.
   (t
    (let ((regs gnus-newsrc-options-n))
      (while (and regs
		  (not (string-match (caar regs) group)))
	(setq regs (cdr regs)))
      (and regs (cdar regs))))))

(defun gnus-ask-server-for-new-groups ()
1165 1166
  (let* ((new-date (message-make-date))
	 (date (or gnus-newsrc-last-checked-date new-date))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	 (methods (cons gnus-select-method
			(nconc
			 (when (gnus-archive-server-wanted-p)
			   (list "archive"))
			 (append
			  (and (consp gnus-check-new-newsgroups)
			       gnus-check-new-newsgroups)
			  gnus-secondary-select-methods))))
	 (groups 0)
	 group new-newsgroups got-new method hashtb
	 gnus-override-subscribe-method)
1178 1179
    (unless gnus-killed-hashtb
      (gnus-make-hashtable-from-killed))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1180 1181 1182
    ;; Go through both primary and secondary select methods and
    ;; request new newsgroups.
    (while (setq method (gnus-server-get-method nil (pop methods)))
1183 1184
      (setq new-newsgroups nil
	    gnus-override-subscribe-method method)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1185 1186 1187
      (when (and (gnus-check-server method)
		 (gnus-request-newgroups date method))
	(save-excursion
1188 1189
	  (setq got-new t
		hashtb (gnus-make-hashtable 100))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
	  (set-buffer nntp-server-buffer)
	  ;; Enter all the new groups into a hashtable.
	  (gnus-active-to-gnus-format method hashtb 'ignore))
	;; Now all new groups from `method' are in `hashtb'.
	(mapatoms
	 (lambda (group-sym)
	   (if (or (null (setq group (symbol-name group-sym)))
		   (not (boundp group-sym))
		   (null (symbol-value group-sym))
		   (gnus-gethash group gnus-newsrc-hashtb)
		   (member group gnus-zombie-list)
		   (member group gnus-killed-list))
	       ;; The group is already known.
	       ()
	     ;; Make this group active.
	     (when (symbol-value group-sym)
	       (gnus-set-active group (symbol-value group-sym)))
	     ;; Check whether we want it or not.
	     (let ((do-sub (gnus-matches-options-n group)))
	       (cond
		((eq do-sub 'subscribe)
		 (incf groups)
		 (gnus-sethash group group gnus-killed-hashtb)
1213 1214
		 (gnus-call-subscribe-functions
		  gnus-subscribe-options-newsgroup-method group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1215 1216 1217 1218 1219 1220 1221
		((eq do-sub 'ignore)
		 nil)
		(t
		 (incf groups)
		 (gnus-sethash group group gnus-killed-hashtb)
		 (if gnus-subscribe-hierarchical-interactive
		     (push group new-newsgroups)
1222 1223
		   (gnus-call-subscribe-functions
		    gnus-subscribe-newsgroup-method group)))))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1224 1225 1226
	 hashtb))
      (when new-newsgroups
	(gnus-subscribe-hierarchical-interactive new-newsgroups)))
1227 1228 1229 1230
    (if (> groups 0)
	(gnus-message 5 "%d new newsgroup%s arrived"
		      groups (if (> groups 1) "s have" " has"))
      (gnus-message 5 "No new newsgroups"))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1231 1232
    (when got-new
      (setq gnus-newsrc-last-checked-date new-date))
1233
    new-newsgroups))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1234

1235
(defun gnus-subscribe-group (group &optional previous method)
1236
  "Subscribe GROUP and put it after PREVIOUS."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1237 1238 1239 1240
  (gnus-group-change-level
   (if method
       (list t group gnus-level-default-subscribed nil nil method)
     group)
1241 1242
   gnus-level-default-subscribed gnus-level-killed previous t)
  t)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

;; `gnus-group-change-level' is the fundamental function for changing
;; subscription levels of newsgroups.  This might mean just changing
;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
;; again, which subscribes/unsubscribes a group, which is equally
;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
;; from 8-9 to 1-7 means that you remove the group from the list of
;; killed (or zombie) groups and add them to the (kinda) subscribed
;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
;; which is trivial.
;; ENTRY can either be a string (newsgroup name) or a list (if
;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
;; entries.
;; LEVEL is the new level of the group, OLDLEVEL is the old level and
;; PREVIOUS is the group (in hashtb entry format) to insert this group
;; after.
(defun gnus-group-change-level (entry level &optional oldlevel
				      previous fromkilled)
  (let (group info active num)
    ;; Glean what info we can from the arguments
    (if (consp entry)
	(if fromkilled (setq group (nth 1 entry))
	  (setq group (car (nth 2