lisp/ChangeLog addition:
[gnus] / lisp / gnus-start.el
1 ;;; gnus-start.el --- startup functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (require 'gnus-win)
31 (require 'gnus-int)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-util)
35 (require 'message)
36 (eval-when-compile (require 'cl))
37
38 (defcustom gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
39   "Your `.newsrc' file.
40 `.newsrc-SERVER' will be used instead if that exists."
41   :group 'gnus-start
42   :type 'file)
43
44 (defcustom gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
45   "Your Gnus Emacs-Lisp startup file name.
46 If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
47   :group 'gnus-start
48   :type 'file)
49
50 (defcustom gnus-site-init-file
51   (condition-case nil
52       (concat (file-name-directory
53                (directory-file-name installation-directory))
54               "site-lisp/gnus-init")
55     (error nil))
56   "The site-wide Gnus Emacs-Lisp startup file name, or nil if none.
57 If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
58   :group 'gnus-start
59   :type '(choice file (const nil)))
60
61 (defcustom gnus-default-subscribed-newsgroups nil
62   "List of newsgroups to subscribe, when a user runs Gnus the first time.
63 The value should be a list of strings.
64 If it is t, Gnus will not do anything special the first time it is
65 started; it'll just use the normal newsgroups subscription methods."
66   :group 'gnus-start
67   :type '(choice (repeat string) (const :tag "Nothing special" t)))
68
69 (defcustom gnus-use-dribble-file t
70   "*Non-nil means that Gnus will use a dribble file to store user updates.
71 If Emacs should crash without saving the .newsrc files, complete
72 information can be restored from the dribble file."
73   :group 'gnus-dribble-file
74   :type 'boolean)
75
76 (defcustom gnus-dribble-directory nil
77   "*The directory where dribble files will be saved.
78 If this variable is nil, the directory where the .newsrc files are
79 saved will be used."
80   :group 'gnus-dribble-file
81   :type '(choice directory (const nil)))
82
83 (defcustom gnus-check-new-newsgroups 'ask-server
84   "*Non-nil means that Gnus will run `gnus-find-new-newsgroups' at startup.
85 This normally finds new newsgroups by comparing the active groups the
86 servers have already reported with those Gnus already knows, either alive
87 or killed.
88
89 When any of the following are true, `gnus-find-new-newsgroups' will instead
90 ask the servers (primary, secondary, and archive servers) to list new
91 groups since the last time it checked:
92   1. This variable is `ask-server'.
93   2. This variable is a list of select methods (see below).
94   3. `gnus-read-active-file' is nil or `some'.
95   4. A prefix argument is given to `gnus-find-new-newsgroups' interactively.
96
97 Thus, if this variable is `ask-server' or a list of select methods or
98 `gnus-read-active-file' is nil or `some', then the killed list is no
99 longer necessary, so you could safely set `gnus-save-killed-list' to nil.
100
101 This variable can be a list of select methods which Gnus will query with
102 the `ask-server' method in addition to the primary, secondary, and archive
103 servers.
104
105 Eg.
106   (setq gnus-check-new-newsgroups
107         '((nntp \"some.server\") (nntp \"other.server\")))
108
109 If this variable is nil, then you have to tell Gnus explicitly to
110 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups]."
111   :group 'gnus-start
112   :type '(choice (const :tag "no" nil)
113                  (const :tag "by brute force" t)
114                  (const :tag "ask servers" ask-server)
115                  (repeat :menu-tag "ask additional servers"
116                          :tag "ask additional servers"
117                          :value ((nntp ""))
118                          (sexp :format "%v"))))
119
120 (defcustom gnus-check-bogus-newsgroups nil
121   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
122 If this variable is nil, then you have to tell Gnus explicitly to
123 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups]."
124   :group 'gnus-start-server
125   :type 'boolean)
126
127 (defcustom gnus-read-active-file 'some
128   "*Non-nil means that Gnus will read the entire active file at startup.
129 If this variable is nil, Gnus will only know about the groups in your
130 `.newsrc' file.
131
132 If this variable is `some', Gnus will try to only read the relevant
133 parts of the active file from the server.  Not all servers support
134 this, and it might be quite slow with other servers, but this should
135 generally be faster than both the t and nil value.
136
137 If you set this variable to nil or `some', you probably still want to
138 be told about new newsgroups that arrive.  To do that, set
139 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
140 properly with all servers."
141   :group 'gnus-start-server
142   :type '(choice (const nil)
143                  (const some)
144                  (const t)))
145
146 (defconst gnus-level-subscribed 5
147   "Groups with levels less than or equal to this variable are subscribed.")
148
149 (defconst gnus-level-unsubscribed 7
150   "Groups with levels less than or equal to this variable are unsubscribed.
151 Groups with levels less than `gnus-level-subscribed', which should be
152 less than this variable, are subscribed.")
153
154 (defconst gnus-level-zombie 8
155   "Groups with this level are zombie groups.")
156
157 (defconst gnus-level-killed 9
158   "Groups with this level are killed.")
159
160 (defcustom gnus-level-default-subscribed 3
161   "*New subscribed groups will be subscribed at this level."
162   :group 'gnus-group-levels
163   :type 'integer)
164
165 (defcustom gnus-level-default-unsubscribed 6
166   "*New unsubscribed groups will be unsubscribed at this level."
167   :group 'gnus-group-levels
168   :type 'integer)
169
170 (defcustom gnus-activate-level (1+ gnus-level-subscribed)
171   "*Groups higher than this level won't be activated on startup.
172 Setting this variable to something low might save lots of time when
173 you have many groups that you aren't interested in."
174   :group 'gnus-group-levels
175   :type 'integer)
176
177 (defcustom gnus-activate-foreign-newsgroups 4
178   "*If nil, Gnus will not check foreign newsgroups at startup.
179 If it is non-nil, it should be a number between one and nine.  Foreign
180 newsgroups that have a level lower or equal to this number will be
181 activated on startup.  For instance, if you want to active all
182 subscribed newsgroups, but not the rest, you'd set this variable to
183 `gnus-level-subscribed'.
184
185 If you subscribe to lots of newsgroups from different servers, startup
186 might take a while.  By setting this variable to nil, you'll save time,
187 but you won't be told how many unread articles there are in the
188 groups."
189   :group 'gnus-group-levels
190   :type '(choice integer
191                  (const :tag "none" nil)))
192
193 (defcustom gnus-read-newsrc-file t
194   "*Non-nil means that Gnus will read the `.newsrc' file.
195 Gnus always reads its own startup file, which is called
196 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
197 be readily understood by other newsreaders.  If you don't plan on
198 using other newsreaders, set this variable to nil to save some time on
199 entry."
200   :version "21.1"
201   :group 'gnus-newsrc
202   :type 'boolean)
203
204 (defcustom gnus-save-newsrc-file t
205   "*Non-nil means that Gnus will save the `.newsrc' file.
206 Gnus always saves its own startup file, which is called
207 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
208 be readily understood by other newsreaders.  If you don't plan on
209 using other newsreaders, set this variable to nil to save some time on
210 exit."
211   :group 'gnus-newsrc
212   :type 'boolean)
213
214 (defcustom gnus-save-killed-list t
215   "*If non-nil, save the list of killed groups to the startup file.
216 If you set this variable to nil, you'll save both time (when starting
217 and quitting) and space (both memory and disk), but it will also mean
218 that Gnus has no record of which groups are new and which are old, so
219 the automatic new newsgroups subscription methods become meaningless.
220
221 You should always set `gnus-check-new-newsgroups' to `ask-server' or
222 nil if you set this variable to nil.
223
224 This variable can also be a regexp.  In that case, all groups that do
225 not match this regexp will be removed before saving the list."
226   :group 'gnus-newsrc
227   :type 'boolean)
228
229 (defcustom gnus-ignored-newsgroups
230   (mapconcat 'identity
231              '("^to\\."                 ; not "real" groups
232                "^[0-9. \t]+\\( \\|$\\)" ; all digits in name
233                "^[\"][]\"[#'()]"        ; bogus characters
234                )
235              "\\|")
236   "*A regexp to match uninteresting newsgroups in the active file.
237 Any lines in the active file matching this regular expression are
238 removed from the newsgroup list before anything else is done to it,
239 thus making them effectively non-existent."
240   :group 'gnus-group-new
241   :type 'regexp)
242
243 (defcustom gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
244   "*Function(s) called with a group name when new group is detected.
245 A few pre-made functions are supplied: `gnus-subscribe-randomly'
246 inserts new groups at the beginning of the list of groups;
247 `gnus-subscribe-alphabetically' inserts new groups in strict
248 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
249 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
250 for your decision; `gnus-subscribe-killed' kills all new groups;
251 `gnus-subscribe-zombies' will make all new groups into zombies;
252 `gnus-subscribe-topics' will enter groups into the topics that
253 claim them."
254   :group 'gnus-group-new
255   :type '(radio (function-item gnus-subscribe-randomly)
256                 (function-item gnus-subscribe-alphabetically)
257                 (function-item gnus-subscribe-hierarchically)
258                 (function-item gnus-subscribe-interactively)
259                 (function-item gnus-subscribe-killed)
260                 (function-item gnus-subscribe-zombies)
261                 (function-item gnus-subscribe-topics)
262                 function
263                 (repeat function)))
264
265 (defcustom gnus-subscribe-newsgroup-hooks nil
266   "*Hooks run after you subscribe to a new group. The hooks will be called
267 with new group's name as argument."
268   :group 'gnus-group-new
269   :type 'hook)
270
271 (defcustom gnus-subscribe-options-newsgroup-method
272   'gnus-subscribe-alphabetically
273   "*Function(s) called to subscribe newsgroups mentioned on \"options -n\" lines.
274 If, for instance, you want to subscribe to all newsgroups in the
275 \"no\" and \"alt\" hierarchies, you'd put the following in your
276 .newsrc file:
277
278 options -n no.all alt.all
279
280 Gnus will the subscribe all new newsgroups in these hierarchies with
281 the subscription method in this variable."
282   :group 'gnus-group-new
283   :type '(radio (function-item gnus-subscribe-randomly)
284                 (function-item gnus-subscribe-alphabetically)
285                 (function-item gnus-subscribe-hierarchically)
286                 (function-item gnus-subscribe-interactively)
287                 (function-item gnus-subscribe-killed)
288                 (function-item gnus-subscribe-zombies)
289                 (function-item gnus-subscribe-topics)
290                 function
291                 (repeat function)))
292
293 (defcustom gnus-subscribe-hierarchical-interactive nil
294   "*If non-nil, Gnus will offer to subscribe hierarchically.
295 When a new hierarchy appears, Gnus will ask the user:
296
297 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
298
299 If the user pressed `d', Gnus will descend the hierarchy, `y' will
300 subscribe to all newsgroups in the hierarchy and `s' will skip this
301 hierarchy in its entirety."
302   :group 'gnus-group-new
303   :type 'boolean)
304
305 (defcustom gnus-auto-subscribed-groups
306   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
307   "*All new groups that match this regexp will be subscribed automatically.
308 Note that this variable only deals with new groups.  It has no effect
309 whatsoever on old groups.
310
311 New groups that match this regexp will not be handled by
312 `gnus-subscribe-newsgroup-method'.  Instead, they will
313 be subscribed using `gnus-subscribe-options-newsgroup-method'."
314   :group 'gnus-group-new
315   :type 'regexp)
316
317 (defcustom gnus-options-subscribe nil
318   "*All new groups matching this regexp will be subscribed unconditionally.
319 Note that this variable deals only with new newsgroups.  This variable
320 does not affect old newsgroups.
321
322 New groups that match this regexp will not be handled by
323 `gnus-subscribe-newsgroup-method'.  Instead, they will
324 be subscribed using `gnus-subscribe-options-newsgroup-method'."
325   :group 'gnus-group-new
326   :type '(choice regexp
327                  (const :tag "none" nil)))
328
329 (defcustom gnus-options-not-subscribe nil
330   "*All new groups matching this regexp will be ignored.
331 Note that this variable deals only with new newsgroups.  This variable
332 does not affect old (already subscribed) newsgroups."
333   :group 'gnus-group-new
334   :type '(choice regexp
335                  (const :tag "none" nil)))
336
337 (defcustom gnus-modtime-botch nil
338   "*Non-nil means .newsrc should be deleted prior to save.
339 Its use is due to the bogus appearance that .newsrc was modified on
340 disc."
341   :group 'gnus-newsrc
342   :type 'boolean)
343
344 (defcustom gnus-check-bogus-groups-hook nil
345   "A hook run after removing bogus groups."
346   :group 'gnus-start-server
347   :type 'hook)
348
349 (defcustom gnus-startup-hook nil
350   "A hook called at startup.
351 This hook is called after Gnus is connected to the NNTP server."
352   :group 'gnus-start
353   :type 'hook)
354
355 (defcustom gnus-before-startup-hook nil
356   "A hook called at before startup.
357 This hook is called as the first thing when Gnus is started."
358   :group 'gnus-start
359   :type 'hook)
360
361 (defcustom gnus-started-hook nil
362   "A hook called as the last thing after startup."
363   :group 'gnus-start
364   :type 'hook)
365
366 (defcustom gnus-setup-news-hook nil
367   "A hook after reading the .newsrc file, but before generating the buffer."
368   :group 'gnus-start
369   :type 'hook)
370
371 (defcustom gnus-get-new-news-hook nil
372   "A hook run just before Gnus checks for new news."
373   :group 'gnus-group-new
374   :type 'hook)
375
376 (defcustom gnus-after-getting-new-news-hook
377   '(gnus-display-time-event-handler)
378   "*A hook run after Gnus checks for new news when Gnus is already running."
379   :group 'gnus-group-new
380   :type 'hook)
381
382 (defcustom gnus-save-newsrc-hook nil
383   "A hook called before saving any of the newsrc files."
384   :group 'gnus-newsrc
385   :type 'hook)
386
387 (defcustom gnus-save-quick-newsrc-hook nil
388   "A hook called just before saving the quick newsrc file.
389 Can be used to turn version control on or off."
390   :group 'gnus-newsrc
391   :type 'hook)
392
393 (defcustom gnus-save-standard-newsrc-hook nil
394   "A hook called just before saving the standard newsrc file.
395 Can be used to turn version control on or off."
396   :group 'gnus-newsrc
397   :type 'hook)
398
399 (defcustom gnus-group-mode-hook nil
400   "Hook for Gnus group mode."
401   :group 'gnus-group-various
402   :options '(gnus-topic-mode)
403   :type 'hook)
404
405 (defcustom gnus-always-read-dribble-file nil
406   "Unconditionally read the dribble file."
407   :group 'gnus-newsrc
408   :type 'boolean)
409
410 ;;; Internal variables
411
412 (defvar gnus-startup-file-coding-system 'binary
413   "*Coding system for startup file.")
414
415 (defvar gnus-ding-file-coding-system mm-universal-coding-system
416   "*Coding system for ding file.")
417
418 (defvar gnus-newsrc-file-version nil)
419 (defvar gnus-override-subscribe-method nil)
420 (defvar gnus-dribble-buffer nil)
421 (defvar gnus-newsrc-options nil
422   "Options line in the .newsrc file.")
423
424 (defvar gnus-newsrc-options-n nil
425   "List of regexps representing groups to be subscribed/ignored unconditionally.")
426
427 (defvar gnus-newsrc-last-checked-date nil
428   "Date Gnus last asked server for new newsgroups.")
429
430 (defvar gnus-current-startup-file nil
431   "Startup file for the current host.")
432
433 ;; Byte-compiler warning.
434 (defvar gnus-group-line-format)
435
436 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
437 (defvar gnus-init-inhibit nil)
438 (defun gnus-read-init-file (&optional inhibit-next)
439   ;; Don't load .gnus if the -q option was used.
440   (when init-file-user
441     (if gnus-init-inhibit
442         (setq gnus-init-inhibit nil)
443       (setq gnus-init-inhibit inhibit-next)
444       (let ((files (list gnus-site-init-file gnus-init-file))
445             file)
446         (while files
447           (and (setq file (pop files))
448                (or (and (file-exists-p file)
449                         ;; Don't try to load a directory.
450                         (not (file-directory-p file)))
451                    (file-exists-p (concat file ".el"))
452                    (file-exists-p (concat file ".elc")))
453                (if (or debug-on-error debug-on-quit)
454                    (let ((coding-system-for-read
455                           gnus-startup-file-coding-system))
456                      (load file nil t))
457                  (condition-case var
458                      (let ((coding-system-for-read
459                             gnus-startup-file-coding-system))
460                        (load file nil t))
461                    (error
462                     (error "Error in %s: %s" file var))))))))))
463
464 ;; For subscribing new newsgroup
465
466 (defun gnus-subscribe-hierarchical-interactive (groups)
467   (let ((groups (sort groups 'string<))
468         prefixes prefix start ans group starts)
469     (while groups
470       (setq prefixes (list "^"))
471       (while (and groups prefixes)
472         (while (not (string-match (car prefixes) (car groups)))
473           (setq prefixes (cdr prefixes)))
474         (setq prefix (car prefixes))
475         (setq start (1- (length prefix)))
476         (if (and (string-match "[^\\.]\\." (car groups) start)
477                  (cdr groups)
478                  (setq prefix
479                        (concat "^" (substring (car groups) 0 (match-end 0))))
480                  (string-match prefix (cadr groups)))
481             (progn
482               (push prefix prefixes)
483               (message "Descend hierarchy %s? ([y]nsq): "
484                        (substring prefix 1 (1- (length prefix))))
485               (while (not (memq (setq ans (read-char-exclusive))
486                                 '(?y ?\n ?\r ?n ?s ?q)))
487                 (ding)
488                 (message "Descend hierarchy %s? ([y]nsq): "
489                          (substring prefix 1 (1- (length prefix)))))
490               (cond ((= ans ?n)
491                      (while (and groups
492                                  (string-match prefix
493                                                (setq group (car groups))))
494                        (push group gnus-killed-list)
495                        (gnus-sethash group group gnus-killed-hashtb)
496                        (setq groups (cdr groups)))
497                      (setq starts (cdr starts)))
498                     ((= ans ?s)
499                      (while (and groups
500                                  (string-match prefix
501                                                (setq group (car groups))))
502                        (gnus-sethash group group gnus-killed-hashtb)
503                        (gnus-subscribe-alphabetically (car groups))
504                        (setq groups (cdr groups)))
505                      (setq starts (cdr starts)))
506                     ((= ans ?q)
507                      (while groups
508                        (setq group (car groups))
509                        (push group gnus-killed-list)
510                        (gnus-sethash group group gnus-killed-hashtb)
511                        (setq groups (cdr groups))))
512                     (t nil)))
513           (message "Subscribe %s? ([n]yq)" (car groups))
514           (while (not (memq (setq ans (read-char-exclusive))
515                             '(?y ?\n ?\r ?q ?n)))
516             (ding)
517             (message "Subscribe %s? ([n]yq)" (car groups)))
518           (setq group (car groups))
519           (cond ((= ans ?y)
520                  (gnus-subscribe-alphabetically (car groups))
521                  (gnus-sethash group group gnus-killed-hashtb))
522                 ((= ans ?q)
523                  (while groups
524                    (setq group (car groups))
525                    (push group gnus-killed-list)
526                    (gnus-sethash group group gnus-killed-hashtb)
527                    (setq groups (cdr groups))))
528                 (t
529                  (push group gnus-killed-list)
530                  (gnus-sethash group group gnus-killed-hashtb)))
531           (setq groups (cdr groups)))))))
532
533 (defun gnus-subscribe-randomly (newsgroup)
534   "Subscribe new NEWSGROUP by making it the first newsgroup."
535   (gnus-subscribe-newsgroup newsgroup))
536
537 (defun gnus-subscribe-alphabetically (newgroup)
538   "Subscribe new NEWSGROUP and insert it in alphabetical order."
539   (let ((groups (cdr gnus-newsrc-alist))
540         before)
541     (while (and (not before) groups)
542       (if (string< newgroup (caar groups))
543           (setq before (caar groups))
544         (setq groups (cdr groups))))
545     (gnus-subscribe-newsgroup newgroup before)))
546
547 (defun gnus-subscribe-hierarchically (newgroup)
548   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
549   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
550   (save-excursion
551     (set-buffer (nnheader-find-file-noselect gnus-current-startup-file))
552     (prog1
553         (let ((groupkey newgroup) before)
554           (while (and (not before) groupkey)
555             (goto-char (point-min))
556             (let ((groupkey-re
557                    (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
558               (while (and (re-search-forward groupkey-re nil t)
559                           (progn
560                             (setq before (match-string 1))
561                             (string< before newgroup)))))
562             ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
563             (setq groupkey
564                   (when (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
565                     (substring groupkey (match-beginning 1) (match-end 1)))))
566           (gnus-subscribe-newsgroup newgroup before))
567       (kill-buffer (current-buffer)))))
568
569 (defun gnus-subscribe-interactively (group)
570   "Subscribe the new GROUP interactively.
571 It is inserted in hierarchical newsgroup order if subscribed.  If not,
572 it is killed."
573   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
574       (gnus-subscribe-hierarchically group)
575     (push group gnus-killed-list)))
576
577 (defun gnus-subscribe-zombies (group)
578   "Make the new GROUP into a zombie group."
579   (push group gnus-zombie-list))
580
581 (defun gnus-subscribe-killed (group)
582   "Make the new GROUP a killed group."
583   (push group gnus-killed-list))
584
585 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
586   "Subscribe new NEWSGROUP.
587 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
588 the first newsgroup."
589   (save-excursion
590     (goto-char (point-min))
591     ;; We subscribe the group by changing its level to `subscribed'.
592     (gnus-group-change-level
593      newsgroup gnus-level-default-subscribed
594      gnus-level-killed (gnus-gethash (or next "dummy.group")
595                                      gnus-newsrc-hashtb))
596     (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)
597     (run-hook-with-args 'gnus-subscribe-newsgroup-hooks newsgroup)
598     t))
599
600 (defun gnus-read-active-file-p ()
601   "Say whether the active file has been read from `gnus-select-method'."
602   (memq gnus-select-method gnus-have-read-active-file))
603
604 ;;; General various misc type functions.
605
606 ;; Silence byte-compiler.
607 (defvar gnus-current-headers)
608 (defvar gnus-thread-indent-array)
609 (defvar gnus-newsgroup-name)
610 (defvar gnus-newsgroup-headers)
611 (defvar gnus-group-list-mode)
612 (defvar gnus-group-mark-positions)
613 (defvar gnus-newsgroup-data)
614 (defvar gnus-newsgroup-unreads)
615 (defvar nnoo-state-alist)
616 (defvar gnus-current-select-method)
617
618 (defun gnus-close-all-servers ()
619   "Close all servers."
620   (interactive)
621   (dolist (server gnus-opened-servers)
622     (gnus-close-server (car server))))
623
624 (defun gnus-clear-system ()
625   "Clear all variables and buffers."
626   ;; Clear Gnus variables.
627   (let ((variables gnus-variable-list))
628     (while variables
629       (set (car variables) nil)
630       (setq variables (cdr variables))))
631   ;; Clear other internal variables.
632   (setq gnus-list-of-killed-groups nil
633         gnus-have-read-active-file nil
634         gnus-newsrc-alist nil
635         gnus-newsrc-hashtb nil
636         gnus-killed-list nil
637         gnus-zombie-list nil
638         gnus-killed-hashtb nil
639         gnus-active-hashtb nil
640         gnus-moderated-hashtb nil
641         gnus-description-hashtb nil
642         gnus-current-headers nil
643         gnus-thread-indent-array nil
644         gnus-newsgroup-headers nil
645         gnus-newsgroup-name nil
646         gnus-server-alist nil
647         gnus-group-list-mode nil
648         gnus-opened-servers nil
649         gnus-group-mark-positions nil
650         gnus-newsgroup-data nil
651         gnus-newsgroup-unreads nil
652         nnoo-state-alist nil
653         gnus-current-select-method nil
654         nnmail-split-history nil
655         gnus-ephemeral-servers nil)
656   (gnus-shutdown 'gnus)
657   ;; Kill the startup file.
658   (and gnus-current-startup-file
659        (get-file-buffer gnus-current-startup-file)
660        (kill-buffer (get-file-buffer gnus-current-startup-file)))
661   ;; Clear the dribble buffer.
662   (gnus-dribble-clear)
663   ;; Kill global KILL file buffer.
664   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
665     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
666   (gnus-kill-buffer nntp-server-buffer)
667   ;; Kill Gnus buffers.
668   (let ((buffers (gnus-buffers)))
669     (when buffers
670       (mapcar 'kill-buffer buffers)))
671   ;; Remove Gnus frames.
672   (gnus-kill-gnus-frames))
673
674 (defun gnus-no-server-1 (&optional arg slave)
675   "Read network news.
676 If ARG is a positive number, Gnus will use that as the
677 startup level.  If ARG is nil, Gnus will be started at level 2.
678 If ARG is non-nil and not a positive number, Gnus will
679 prompt the user for the name of an NNTP server to use.
680 As opposed to `gnus', this command will not connect to the local server."
681   (interactive "P")
682   (let ((val (or arg (1- gnus-level-default-subscribed))))
683     (gnus val t slave)
684     (make-local-variable 'gnus-group-use-permanent-levels)
685     (setq gnus-group-use-permanent-levels val)))
686
687 (defun gnus-1 (&optional arg dont-connect slave)
688   "Read network news.
689 If ARG is non-nil and a positive number, Gnus will use that as the
690 startup level.  If ARG is non-nil and not a positive number, Gnus will
691 prompt the user for the name of an NNTP server to use."
692   (interactive "P")
693
694   (if (gnus-alive-p)
695       (progn
696         (switch-to-buffer gnus-group-buffer)
697         (gnus-group-get-new-news
698          (and (numberp arg)
699               (> arg 0)
700               (max (car gnus-group-list-mode) arg))))
701
702     (gnus-clear-system)
703     (gnus-splash)
704     (gnus-run-hooks 'gnus-before-startup-hook)
705     (nnheader-init-server-buffer)
706     (setq gnus-slave slave)
707     (gnus-read-init-file)
708
709     (when gnus-simple-splash
710       (setq gnus-simple-splash nil)
711       (cond
712        ((featurep 'xemacs)
713         (gnus-xmas-splash))
714        ((and window-system
715              (= (frame-height) (1+ (window-height))))
716         (gnus-x-splash))))
717
718     (let ((level (and (numberp arg) (> arg 0) arg))
719           did-connect)
720       (unwind-protect
721           (progn
722             (unless dont-connect
723               (setq did-connect
724                     (gnus-start-news-server (and arg (not level))))))
725         (if (and (not dont-connect)
726                  (not did-connect))
727             (gnus-group-quit)
728           (gnus-run-hooks 'gnus-startup-hook)
729           ;; NNTP server is successfully open.
730
731           ;; Find the current startup file name.
732           (setq gnus-current-startup-file
733                 (gnus-make-newsrc-file gnus-startup-file))
734
735           ;; Read the dribble file.
736           (when (or gnus-slave gnus-use-dribble-file)
737             (gnus-dribble-read-file))
738
739           ;; Allow using GroupLens predictions.
740           (when gnus-use-grouplens
741             (bbb-login)
742             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
743
744           ;; Do the actual startup.
745           (gnus-setup-news nil level dont-connect)
746           (gnus-run-hooks 'gnus-setup-news-hook)
747           (gnus-start-draft-setup)
748           ;; Generate the group buffer.
749           (gnus-group-list-groups level)
750           (gnus-group-first-unread-group)
751           (gnus-configure-windows 'group)
752           (gnus-group-set-mode-line)
753           (gnus-run-hooks 'gnus-started-hook))))))
754
755 (defun gnus-start-draft-setup ()
756   "Make sure the draft group exists."
757   (gnus-request-create-group "drafts" '(nndraft ""))
758   (unless (gnus-gethash "nndraft:drafts" gnus-newsrc-hashtb)
759     (let ((gnus-level-default-subscribed 1))
760       (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
761     (gnus-group-set-parameter
762      "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
763
764 ;;;###autoload
765 (defun gnus-unload ()
766   "Unload all Gnus features.
767 \(For some value of `all' or `Gnus'.)  Currently, features whose names
768 have prefixes `gnus-', `nn', `mm-' or `rfc' are unloaded.  Use
769 cautiously -- unloading may cause trouble."
770   (interactive)
771   (dolist (feature features)
772     (if (string-match "^\\(gnus-\\|nn\\|mm-\\|rfc\\)" (symbol-name feature))
773         (unload-feature feature 'force))))
774
775 \f
776 ;;;
777 ;;; Dribble file
778 ;;;
779
780 (defvar gnus-dribble-ignore nil)
781 (defvar gnus-dribble-eval-file nil)
782
783 (defun gnus-dribble-file-name ()
784   "Return the dribble file for the current .newsrc."
785   (concat
786    (if gnus-dribble-directory
787        (concat (file-name-as-directory gnus-dribble-directory)
788                (file-name-nondirectory gnus-current-startup-file))
789      gnus-current-startup-file)
790    "-dribble"))
791
792 (defun gnus-dribble-enter (string)
793   "Enter STRING into the dribble buffer."
794   (when (and (not gnus-dribble-ignore)
795              gnus-dribble-buffer
796              (buffer-name gnus-dribble-buffer))
797     (let ((obuf (current-buffer)))
798       (set-buffer gnus-dribble-buffer)
799       (goto-char (point-max))
800       (insert string "\n")
801       (set-window-point (get-buffer-window (current-buffer)) (point-max))
802       (bury-buffer gnus-dribble-buffer)
803       (save-excursion
804         (set-buffer gnus-group-buffer)
805         (gnus-group-set-mode-line))
806       (set-buffer obuf))))
807
808 (defun gnus-dribble-touch ()
809   "Touch the dribble buffer."
810   (gnus-dribble-enter ""))
811
812 (defun gnus-dribble-read-file ()
813   "Read the dribble file from disk."
814   (let ((dribble-file (gnus-dribble-file-name)))
815     (save-excursion
816       (set-buffer (setq gnus-dribble-buffer
817                         (gnus-get-buffer-create
818                          (file-name-nondirectory dribble-file))))
819       (erase-buffer)
820       (setq buffer-file-name dribble-file)
821       (auto-save-mode t)
822       (buffer-disable-undo)
823       (bury-buffer (current-buffer))
824       (set-buffer-modified-p nil)
825       (let ((auto (make-auto-save-file-name))
826             (gnus-dribble-ignore t)
827             (purpose nil)
828             modes)
829         (when (or (file-exists-p auto) (file-exists-p dribble-file))
830           ;; Load whichever file is newest -- the auto save file
831           ;; or the "real" file.
832           (if (file-newer-than-file-p auto dribble-file)
833               (nnheader-insert-file-contents auto)
834             (nnheader-insert-file-contents dribble-file))
835           (unless (zerop (buffer-size))
836             (set-buffer-modified-p t))
837           ;; Set the file modes to reflect the .newsrc file modes.
838           (save-buffer)
839           (when (and (file-exists-p gnus-current-startup-file)
840                      (file-exists-p dribble-file)
841                      (setq modes (file-modes gnus-current-startup-file)))
842             (set-file-modes dribble-file modes))
843           (goto-char (point-min))
844           (when (search-forward "Gnus was exited on purpose" nil t)
845             (setq purpose t))
846           ;; Possibly eval the file later.
847           (when (or gnus-always-read-dribble-file
848                     (gnus-y-or-n-p
849                      (if purpose
850                          "Gnus exited on purpose without saving; read auto-save file anyway? "
851                      "Gnus auto-save file exists.  Do you want to read it? ")))
852             (setq gnus-dribble-eval-file t)))))))
853
854 (defun gnus-dribble-eval-file ()
855   (when gnus-dribble-eval-file
856     (setq gnus-dribble-eval-file nil)
857     (save-excursion
858       (let ((gnus-dribble-ignore t))
859         (set-buffer gnus-dribble-buffer)
860         (eval-buffer (current-buffer))))))
861
862 (defun gnus-dribble-delete-file ()
863   (when (file-exists-p (gnus-dribble-file-name))
864     (delete-file (gnus-dribble-file-name)))
865   (when gnus-dribble-buffer
866     (save-excursion
867       (set-buffer gnus-dribble-buffer)
868       (let ((auto (make-auto-save-file-name)))
869         (when (file-exists-p auto)
870           (delete-file auto))
871         (erase-buffer)
872         (set-buffer-modified-p nil)))))
873
874 (defun gnus-dribble-save ()
875   (when (and gnus-dribble-buffer
876              (buffer-name gnus-dribble-buffer))
877     (save-excursion
878       (set-buffer gnus-dribble-buffer)
879       (save-buffer))))
880
881 (defun gnus-dribble-clear ()
882   (when (gnus-buffer-exists-p gnus-dribble-buffer)
883     (save-excursion
884       (set-buffer gnus-dribble-buffer)
885       (erase-buffer)
886       (set-buffer-modified-p nil)
887       (setq buffer-saved-size (buffer-size)))))
888
889 \f
890 ;;;
891 ;;; Active & Newsrc File Handling
892 ;;;
893
894 (defun gnus-setup-news (&optional rawfile level dont-connect)
895   "Setup news information.
896 If RAWFILE is non-nil, the .newsrc file will also be read.
897 If LEVEL is non-nil, the news will be set up at level LEVEL."
898   (require 'nnmail)
899   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
900         ;; Binding this variable will inhibit multiple fetchings
901         ;; of the same mail source.
902         (nnmail-fetched-sources (list t)))
903
904     (when init
905       ;; Clear some variables to re-initialize news information.
906       (setq gnus-newsrc-alist nil
907             gnus-active-hashtb nil)
908       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
909       (gnus-read-newsrc-file rawfile))
910
911     ;; Make sure the archive server is available to all and sundry.
912     (when gnus-message-archive-method
913       (unless (assoc "archive" gnus-server-alist)
914         (push `("archive"
915                 nnfolder
916                 "archive"
917                 (nnfolder-directory
918                  ,(nnheader-concat message-directory "archive"))
919                 (nnfolder-active-file
920                  ,(nnheader-concat message-directory "archive/active"))
921                 (nnfolder-get-new-mail nil)
922                 (nnfolder-inhibit-expiry t))
923               gnus-server-alist)))
924
925     ;; If we don't read the complete active file, we fill in the
926     ;; hashtb here.
927     (when (or (null gnus-read-active-file)
928               (eq gnus-read-active-file 'some))
929       (gnus-update-active-hashtb-from-killed))
930
931     ;; Read the active file and create `gnus-active-hashtb'.
932     ;; If `gnus-read-active-file' is nil, then we just create an empty
933     ;; hash table.  The partial filling out of the hash table will be
934     ;; done in `gnus-get-unread-articles'.
935     (and gnus-read-active-file
936          (not level)
937          (gnus-read-active-file nil dont-connect))
938
939     (unless gnus-active-hashtb
940       (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
941
942     ;; Initialize the cache.
943     (when gnus-use-cache
944       (gnus-cache-open))
945
946     ;; Possibly eval the dribble file.
947     (and init
948          (or gnus-use-dribble-file gnus-slave)
949          (gnus-dribble-eval-file))
950
951     ;; Slave Gnusii should then clear the dribble buffer.
952     (when (and init gnus-slave)
953       (gnus-dribble-clear))
954
955     (gnus-update-format-specifications)
956
957     ;; See whether we need to read the description file.
958     (when (and (boundp 'gnus-group-line-format)
959                (stringp gnus-group-line-format)
960                (let ((case-fold-search nil))
961                  (string-match "%[-,0-9]*D" gnus-group-line-format))
962                (not gnus-description-hashtb)
963                (not dont-connect)
964                gnus-read-active-file)
965       (gnus-read-all-descriptions-files))
966
967     ;; Find new newsgroups and treat them.
968     (when (and init gnus-check-new-newsgroups (not level)
969                (gnus-check-server gnus-select-method)
970                (not gnus-slave)
971                gnus-plugged)
972       (gnus-find-new-newsgroups))
973
974     ;; Check and remove bogus newsgroups.
975     (when (and init gnus-check-bogus-newsgroups
976                gnus-read-active-file (not level)
977                (gnus-server-opened gnus-select-method))
978       (gnus-check-bogus-newsgroups))
979
980     ;; We might read in new NoCeM messages here.
981     (when (and gnus-use-nocem
982                (not level)
983                (not dont-connect))
984       (gnus-nocem-scan-groups))
985
986     ;; Read any slave files.
987     (gnus-master-read-slave-newsrc)
988
989     ;; Find the number of unread articles in each non-dead group.
990     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
991       (gnus-get-unread-articles level))))
992
993 (defun gnus-call-subscribe-functions (method group)
994   "Call METHOD to subscribe GROUP.
995 If no function returns `non-nil', call `gnus-subscribe-zombies'."
996   (unless (cond
997            ((gnus-functionp method)
998             (funcall method group))
999            ((listp method)
1000             (catch 'found
1001               (dolist (func method)
1002                 (if (funcall func group)
1003                     (throw 'found t)))
1004               nil))
1005            (t nil))
1006     (gnus-subscribe-zombies group)))
1007
1008 (defun gnus-find-new-newsgroups (&optional arg)
1009   "Search for new newsgroups and add them.
1010 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
1011 The `-n' option line from .newsrc is respected.
1012
1013 With 1 C-u, use the `ask-server' method to query the server for new
1014 groups.
1015 With 2 C-u's, use most complete method possible to query the server
1016 for new groups, and subscribe the new groups as zombies."
1017   (interactive "p")
1018   (let* ((gnus-subscribe-newsgroup-method
1019           gnus-subscribe-newsgroup-method)
1020          (check (cond
1021                  ((or (and (= (or arg 1) 4)
1022                            (not (listp gnus-check-new-newsgroups)))
1023                       (null gnus-read-active-file)
1024                       (eq gnus-read-active-file 'some))
1025                   'ask-server)
1026                  ((= (or arg 1) 16)
1027                   (setq gnus-subscribe-newsgroup-method
1028                         'gnus-subscribe-zombies)
1029                   t)
1030                  (t gnus-check-new-newsgroups))))
1031     (unless (gnus-check-first-time-used)
1032       (if (or (consp check)
1033               (eq check 'ask-server))
1034           ;; Ask the server for new groups.
1035           (gnus-ask-server-for-new-groups)
1036         ;; Go through the active hashtb and look for new groups.
1037         (let ((groups 0)
1038               group new-newsgroups)
1039           (gnus-message 5 "Looking for new newsgroups...")
1040           (unless gnus-have-read-active-file
1041             (gnus-read-active-file))
1042           (setq gnus-newsrc-last-checked-date (message-make-date))
1043           (unless gnus-killed-hashtb
1044             (gnus-make-hashtable-from-killed))
1045           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1046           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1047           (mapatoms
1048            (lambda (sym)
1049              (if (or (null (setq group (symbol-name sym)))
1050                      (not (boundp sym))
1051                      (null (symbol-value sym))
1052                      (gnus-gethash group gnus-killed-hashtb)
1053                      (gnus-gethash group gnus-newsrc-hashtb))
1054                  ()
1055                (let ((do-sub (gnus-matches-options-n group)))
1056                  (cond
1057                   ((eq do-sub 'subscribe)
1058                    (setq groups (1+ groups))
1059                    (gnus-sethash group group gnus-killed-hashtb)
1060                    (gnus-call-subscribe-functions
1061                     gnus-subscribe-options-newsgroup-method group))
1062                   ((eq do-sub 'ignore)
1063                    nil)
1064                   (t
1065                    (setq groups (1+ groups))
1066                    (gnus-sethash group group gnus-killed-hashtb)
1067                    (if gnus-subscribe-hierarchical-interactive
1068                        (push group new-newsgroups)
1069                      (gnus-call-subscribe-functions
1070                       gnus-subscribe-newsgroup-method group)))))))
1071            gnus-active-hashtb)
1072           (when new-newsgroups
1073             (gnus-subscribe-hierarchical-interactive new-newsgroups))
1074           (if (> groups 0)
1075               (gnus-message 5 "%d new newsgroup%s arrived."
1076                             groups (if (> groups 1) "s have" " has"))
1077             (gnus-message 5 "No new newsgroups.")))))))
1078
1079 (defun gnus-matches-options-n (group)
1080   ;; Returns `subscribe' if the group is to be unconditionally
1081   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1082   ;; no match for the group.
1083
1084   ;; First we check the two user variables.
1085   (cond
1086    ((and gnus-options-subscribe
1087          (string-match gnus-options-subscribe group))
1088     'subscribe)
1089    ((and gnus-auto-subscribed-groups
1090          (string-match gnus-auto-subscribed-groups group))
1091     'subscribe)
1092    ((and gnus-options-not-subscribe
1093          (string-match gnus-options-not-subscribe group))
1094     'ignore)
1095    ;; Then we go through the list that was retrieved from the .newsrc
1096    ;; file.  This list has elements on the form
1097    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
1098    ;; is in the reverse order of the options line) is returned.
1099    (t
1100     (let ((regs gnus-newsrc-options-n))
1101       (while (and regs
1102                   (not (string-match (caar regs) group)))
1103         (setq regs (cdr regs)))
1104       (and regs (cdar regs))))))
1105
1106 (defun gnus-ask-server-for-new-groups ()
1107   (let* ((new-date (message-make-date))
1108          (date (or gnus-newsrc-last-checked-date new-date))
1109          (methods (cons gnus-select-method
1110                         (nconc
1111                          (when (gnus-archive-server-wanted-p)
1112                            (list "archive"))
1113                          (append
1114                           (and (consp gnus-check-new-newsgroups)
1115                                gnus-check-new-newsgroups)
1116                           gnus-secondary-select-methods))))
1117          (groups 0)
1118          group new-newsgroups got-new method hashtb
1119          gnus-override-subscribe-method)
1120     (unless gnus-killed-hashtb
1121       (gnus-make-hashtable-from-killed))
1122     ;; Go through both primary and secondary select methods and
1123     ;; request new newsgroups.
1124     (while (setq method (gnus-server-get-method nil (pop methods)))
1125       (setq new-newsgroups nil
1126             gnus-override-subscribe-method method)
1127       (when (and (gnus-check-server method)
1128                  (gnus-request-newgroups date method))
1129         (save-excursion
1130           (setq got-new t
1131                 hashtb (gnus-make-hashtable 100))
1132           (set-buffer nntp-server-buffer)
1133           ;; Enter all the new groups into a hashtable.
1134           (gnus-active-to-gnus-format method hashtb 'ignore))
1135         ;; Now all new groups from `method' are in `hashtb'.
1136         (mapatoms
1137          (lambda (group-sym)
1138            (if (or (null (setq group (symbol-name group-sym)))
1139                    (not (boundp group-sym))
1140                    (null (symbol-value group-sym))
1141                    (gnus-gethash group gnus-newsrc-hashtb)
1142                    (member group gnus-zombie-list)
1143                    (member group gnus-killed-list))
1144                ;; The group is already known.
1145                ()
1146              ;; Make this group active.
1147              (when (symbol-value group-sym)
1148                (gnus-set-active group (symbol-value group-sym)))
1149              ;; Check whether we want it or not.
1150              (let ((do-sub (gnus-matches-options-n group)))
1151                (cond
1152                 ((eq do-sub 'subscribe)
1153                  (incf groups)
1154                  (gnus-sethash group group gnus-killed-hashtb)
1155                  (gnus-call-subscribe-functions
1156                   gnus-subscribe-options-newsgroup-method group))
1157                 ((eq do-sub 'ignore)
1158                  nil)
1159                 (t
1160                  (incf groups)
1161                  (gnus-sethash group group gnus-killed-hashtb)
1162                  (if gnus-subscribe-hierarchical-interactive
1163                      (push group new-newsgroups)
1164                    (gnus-call-subscribe-functions
1165                     gnus-subscribe-newsgroup-method group)))))))
1166          hashtb))
1167       (when new-newsgroups
1168         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
1169     (if (> groups 0)
1170         (gnus-message 5 "%d new newsgroup%s arrived"
1171                       groups (if (> groups 1) "s have" " has"))
1172       (gnus-message 5 "No new newsgroups"))
1173     (when got-new
1174       (setq gnus-newsrc-last-checked-date new-date))
1175     got-new))
1176
1177 (defun gnus-check-first-time-used ()
1178   (catch 'ended
1179     ;; First check if any of the following files exist.  If they do,
1180     ;; it's not the first time the user has used Gnus.
1181     (dolist (file (list (concat gnus-current-startup-file ".el")
1182                         (concat gnus-current-startup-file ".eld")
1183                         (concat gnus-startup-file ".el")
1184                         (concat gnus-startup-file ".eld")))
1185       (when (file-exists-p file)
1186         (throw 'ended nil)))
1187     (gnus-message 6 "First time user; subscribing you to default groups")
1188     (unless (gnus-read-active-file-p)
1189       (let ((gnus-read-active-file t))
1190         (gnus-read-active-file)))
1191     (setq gnus-newsrc-last-checked-date (message-make-date))
1192     ;; Subscribe to the default newsgroups.
1193     (let ((groups (or gnus-default-subscribed-newsgroups
1194                       gnus-backup-default-subscribed-newsgroups))
1195           group)
1196       (if (eq groups t)
1197           ;; If t, we subscribe (or not) all groups as if they were new.
1198           (mapatoms
1199            (lambda (sym)
1200              (when (setq group (symbol-name sym))
1201                (let ((do-sub (gnus-matches-options-n group)))
1202                  (cond
1203                   ((eq do-sub 'subscribe)
1204                    (gnus-sethash group group gnus-killed-hashtb)
1205                    (gnus-call-subscribe-functions
1206                     gnus-subscribe-options-newsgroup-method group))
1207                   ((eq do-sub 'ignore)
1208                    nil)
1209                   (t
1210                    (push group gnus-killed-list))))))
1211            gnus-active-hashtb)
1212         (dolist (group groups)
1213           ;; Only subscribe the default groups that are activated.
1214           (when (gnus-active group)
1215             (gnus-group-change-level
1216              group gnus-level-default-subscribed gnus-level-killed)))
1217         (save-excursion
1218           (set-buffer gnus-group-buffer)
1219           ;; Don't error if the group already exists. This happens when a
1220           ;; first-time user types 'F'. -- didier
1221           (gnus-group-make-help-group t))
1222         (when gnus-novice-user
1223           (gnus-message 7 "`A k' to list killed groups"))))))
1224
1225 (defun gnus-subscribe-group (group &optional previous method)
1226   "Subcribe GROUP and put it after PREVIOUS."
1227   (gnus-group-change-level
1228    (if method
1229        (list t group gnus-level-default-subscribed nil nil method)
1230      group)
1231    gnus-level-default-subscribed gnus-level-killed previous t)
1232   t)
1233
1234 ;; `gnus-group-change-level' is the fundamental function for changing
1235 ;; subscription levels of newsgroups.  This might mean just changing
1236 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1237 ;; again, which subscribes/unsubscribes a group, which is equally
1238 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1239 ;; from 8-9 to 1-7 means that you remove the group from the list of
1240 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1241 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1242 ;; which is trivial.
1243 ;; ENTRY can either be a string (newsgroup name) or a list (if
1244 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1245 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1246 ;; entries.
1247 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1248 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1249 ;; after.
1250 (defun gnus-group-change-level (entry level &optional oldlevel
1251                                       previous fromkilled)
1252   (let (group info active num)
1253     ;; Glean what info we can from the arguments
1254     (if (consp entry)
1255         (if fromkilled (setq group (nth 1 entry))
1256           (setq group (car (nth 2 entry))))
1257       (setq group entry))
1258     (when (and (stringp entry)
1259                oldlevel
1260                (< oldlevel gnus-level-zombie))
1261       (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
1262     (if (and (not oldlevel)
1263              (consp entry))
1264         (setq oldlevel (gnus-info-level (nth 2 entry)))
1265       (setq oldlevel (or oldlevel gnus-level-killed)))
1266     (when (stringp previous)
1267       (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
1268
1269     (if (and (>= oldlevel gnus-level-zombie)
1270              (gnus-gethash group gnus-newsrc-hashtb))
1271         ;; We are trying to subscribe a group that is already
1272         ;; subscribed.
1273         ()                              ; Do nothing.
1274
1275       (unless (gnus-ephemeral-group-p group)
1276         (gnus-dribble-enter
1277          (format "(gnus-group-change-level %S %S %S %S %S)"
1278                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1279
1280       ;; Then we remove the newgroup from any old structures, if needed.
1281       ;; If the group was killed, we remove it from the killed or zombie
1282       ;; list.  If not, and it is in fact going to be killed, we remove
1283       ;; it from the newsrc hash table and assoc.
1284       (cond
1285        ((>= oldlevel gnus-level-zombie)
1286         ;; oldlevel could be wrong.
1287         (setq gnus-zombie-list (delete group gnus-zombie-list))
1288         (setq gnus-killed-list (delete group gnus-killed-list)))
1289        (t
1290         (when (and (>= level gnus-level-zombie)
1291                    entry)
1292           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1293           (when (nth 3 entry)
1294             (setcdr (gnus-gethash (car (nth 3 entry))
1295                                   gnus-newsrc-hashtb)
1296                     (cdr entry)))
1297           (setcdr (cdr entry) (cdddr entry)))))
1298
1299       ;; Finally we enter (if needed) the list where it is supposed to
1300       ;; go, and change the subscription level.  If it is to be killed,
1301       ;; we enter it into the killed or zombie list.
1302       (cond
1303        ((>= level gnus-level-zombie)
1304         ;; Remove from the hash table.
1305         (gnus-sethash group nil gnus-newsrc-hashtb)
1306         ;; We do not enter foreign groups into the list of dead
1307         ;; groups.
1308         (unless (gnus-group-foreign-p group)
1309           (if (= level gnus-level-zombie)
1310               (push group gnus-zombie-list)
1311             (if (= oldlevel gnus-level-killed)
1312                 ;; Remove from active hashtb.
1313                 (unintern group gnus-active-hashtb)
1314               ;; Don't add it into killed-list if it was killed.
1315               (push group gnus-killed-list)))))
1316        (t
1317         ;; If the list is to be entered into the newsrc assoc, and
1318         ;; it was killed, we have to create an entry in the newsrc
1319         ;; hashtb format and fix the pointers in the newsrc assoc.
1320         (if (< oldlevel gnus-level-zombie)
1321             ;; It was alive, and it is going to stay alive, so we
1322             ;; just change the level and don't change any pointers or
1323             ;; hash table entries.
1324             (setcar (cdaddr entry) level)
1325           (if (listp entry)
1326               (setq info (cdr entry)
1327                     num (car entry))
1328             (setq active (gnus-active group))
1329             (setq num
1330                   (if active (- (1+ (cdr active)) (car active)) t))
1331             ;; Shorten the select method if possible, if we need to
1332             ;; store it at all (native groups).
1333             (let ((method (gnus-method-simplify
1334                            (or gnus-override-subscribe-method
1335                                (gnus-group-method group)))))
1336               (if method
1337                   (setq info (list group level nil nil method))
1338                 (setq info (list group level nil)))))
1339           (unless previous
1340             (setq previous
1341                   (let ((p gnus-newsrc-alist))
1342                     (while (cddr p)
1343                       (setq p (cdr p)))
1344                     p)))
1345           (setq entry (cons info (cddr previous)))
1346           (if (cdr previous)
1347               (progn
1348                 (setcdr (cdr previous) entry)
1349                 (gnus-sethash group (cons num (cdr previous))
1350                               gnus-newsrc-hashtb))
1351             (setcdr previous entry)
1352             (gnus-sethash group (cons num previous)
1353                           gnus-newsrc-hashtb))
1354           (when (cdr entry)
1355             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1356           (gnus-dribble-enter
1357            (format
1358             "(gnus-group-set-info '%S)" info)))))
1359       (when gnus-group-change-level-function
1360         (funcall gnus-group-change-level-function
1361                  group level oldlevel previous)))))
1362
1363 (defun gnus-kill-newsgroup (newsgroup)
1364   "Obsolete function.  Kills a newsgroup."
1365   (gnus-group-change-level
1366    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1367
1368 (defun gnus-check-bogus-newsgroups (&optional confirm)
1369   "Remove bogus newsgroups.
1370 If CONFIRM is non-nil, the user has to confirm the deletion of every
1371 newsgroup."
1372   (let ((newsrc (cdr gnus-newsrc-alist))
1373         bogus group entry info)
1374     (gnus-message 5 "Checking bogus newsgroups...")
1375     (unless (gnus-read-active-file-p)
1376       (gnus-read-active-file t))
1377     (when (gnus-read-active-file-p)
1378       ;; Find all bogus newsgroup that are subscribed.
1379       (while newsrc
1380         (setq info (pop newsrc)
1381               group (gnus-info-group info))
1382         (unless (or (gnus-active group) ; Active
1383                     (and (gnus-info-method info)
1384                          (not (gnus-secondary-method-p
1385                                (gnus-info-method info))))) ; Foreign
1386           ;; Found a bogus newsgroup.
1387           (push group bogus)))
1388       (if confirm
1389           (map-y-or-n-p
1390            "Remove bogus group %s? "
1391            (lambda (group)
1392              ;; Remove all bogus subscribed groups by first killing them, and
1393              ;; then removing them from the list of killed groups.
1394              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1395                (gnus-group-change-level entry gnus-level-killed)
1396                (setq gnus-killed-list (delete group gnus-killed-list))))
1397            bogus '("group" "groups" "remove"))
1398         (while (setq group (pop bogus))
1399           ;; Remove all bogus subscribed groups by first killing them, and
1400           ;; then removing them from the list of killed groups.
1401           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1402             (gnus-group-change-level entry gnus-level-killed)
1403             (setq gnus-killed-list (delete group gnus-killed-list)))))
1404       ;; Then we remove all bogus groups from the list of killed and
1405       ;; zombie groups.  They are removed without confirmation.
1406       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1407             killed)
1408         (while dead-lists
1409           (setq killed (symbol-value (car dead-lists)))
1410           (while killed
1411             (unless (gnus-active (setq group (pop killed)))
1412               ;; The group is bogus.
1413               ;; !!!Slow as hell.
1414               (set (car dead-lists)
1415                    (delete group (symbol-value (car dead-lists))))))
1416           (setq dead-lists (cdr dead-lists))))
1417       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1418       (gnus-message 5 "Checking bogus newsgroups...done"))))
1419
1420 (defun gnus-check-duplicate-killed-groups ()
1421   "Remove duplicates from the list of killed groups."
1422   (interactive)
1423   (let ((killed gnus-killed-list))
1424     (while killed
1425       (gnus-message 9 "%d" (length killed))
1426       (setcdr killed (delete (car killed) (cdr killed)))
1427       (setq killed (cdr killed)))))
1428
1429 ;; We want to inline a function from gnus-cache, so we cheat here:
1430 (eval-when-compile
1431   (defvar gnus-cache-active-hashtb)
1432   (defun gnus-cache-possibly-alter-active (group active)
1433     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1434     (when gnus-cache-active-hashtb
1435       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1436         (when cache-active
1437           (when (< (car cache-active) (car active))
1438             (setcar active (car cache-active)))
1439           (when (> (cdr cache-active) (cdr active))
1440             (setcdr active (cdr cache-active))))))))
1441
1442 (defun gnus-activate-group (group &optional scan dont-check method)
1443   ;; Check whether a group has been activated or not.
1444   ;; If SCAN, request a scan of that group as well.
1445   (let ((method (or method (inline (gnus-find-method-for-group group))))
1446         active)
1447     (and (inline (gnus-check-server method))
1448          ;; We escape all bugs and quit here to make it possible to
1449          ;; continue if a group is so out-there that it reports bugs
1450          ;; and stuff.
1451          (progn
1452            (and scan
1453                 (gnus-check-backend-function 'request-scan (car method))
1454                 (gnus-request-scan group method))
1455            t)
1456          (if (or debug-on-error debug-on-quit)
1457              (inline (gnus-request-group group dont-check method))
1458            (condition-case ()
1459                (inline (gnus-request-group group dont-check method))
1460              ;;(error nil)
1461              (quit
1462               (message "Quit activating %s" group)
1463               nil)))
1464          (unless dont-check
1465            (setq active (gnus-parse-active))
1466            ;; If there are no articles in the group, the GROUP
1467            ;; command may have responded with the `(0 . 0)'.  We
1468            ;; ignore this if we already have an active entry
1469            ;; for the group.
1470            (if (and (zerop (car active))
1471                     (zerop (cdr active))
1472                     (gnus-active group))
1473                (gnus-active group)
1474              (gnus-set-active group active)
1475              ;; Return the new active info.
1476              active)))))
1477
1478 (defun gnus-get-unread-articles-in-group (info active &optional update)
1479   (when active
1480     ;; Allow the backend to update the info in the group.
1481     (when (and update
1482                (gnus-request-update-info
1483                 info (inline (gnus-find-method-for-group
1484                               (gnus-info-group info)))))
1485       (gnus-activate-group (gnus-info-group info) nil t))
1486
1487     (let* ((range (gnus-info-read info))
1488            (num 0))
1489       ;; If a cache is present, we may have to alter the active info.
1490       (when (and gnus-use-cache info)
1491         (inline (gnus-cache-possibly-alter-active
1492                  (gnus-info-group info) active)))
1493       ;; Modify the list of read articles according to what articles
1494       ;; are available; then tally the unread articles and add the
1495       ;; number to the group hash table entry.
1496       (cond
1497        ((zerop (cdr active))
1498         (setq num 0))
1499        ((not range)
1500         (setq num (- (1+ (cdr active)) (car active))))
1501        ((not (listp (cdr range)))
1502         ;; Fix a single (num . num) range according to the
1503         ;; active hash table.
1504         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1505         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1506         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1507         ;; Compute number of unread articles.
1508         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1509        (t
1510         ;; The read list is a list of ranges.  Fix them according to
1511         ;; the active hash table.
1512         ;; First peel off any elements that are below the lower
1513         ;; active limit.
1514         (while (and (cdr range)
1515                     (>= (car active)
1516                         (or (and (atom (cadr range)) (cadr range))
1517                             (caadr range))))
1518           (if (numberp (car range))
1519               (setcar range
1520                       (cons (car range)
1521                             (or (and (numberp (cadr range))
1522                                      (cadr range))
1523                                 (cdadr range))))
1524             (setcdr (car range)
1525                     (or (and (numberp (nth 1 range)) (nth 1 range))
1526                         (cdadr range))))
1527           (setcdr range (cddr range)))
1528         ;; Adjust the first element to be the same as the lower limit.
1529         (when (and (not (atom (car range)))
1530                    (< (cdar range) (car active)))
1531           (setcdr (car range) (1- (car active))))
1532         ;; Then we want to peel off any elements that are higher
1533         ;; than the upper active limit.
1534         (let ((srange range))
1535           ;; Go past all valid elements.
1536           (while (and (cdr srange)
1537                       (<= (or (and (atom (cadr srange))
1538                                    (cadr srange))
1539                               (caadr srange))
1540                           (cdr active)))
1541             (setq srange (cdr srange)))
1542           (when (cdr srange)
1543             ;; Nuke all remaining invalid elements.
1544             (setcdr srange nil))
1545
1546           ;; Adjust the final element.
1547           (when (and (not (atom (car srange)))
1548                      (> (cdar srange) (cdr active)))
1549             (setcdr (car srange) (cdr active))))
1550         ;; Compute the number of unread articles.
1551         (while range
1552           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1553                                       (cdar range)))
1554                               (or (and (atom (car range)) (car range))
1555                                   (caar range)))))
1556           (setq range (cdr range)))
1557         (setq num (max 0 (- (cdr active) num)))))
1558       ;; Set the number of unread articles.
1559       (when info
1560         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1561       num)))
1562
1563 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1564 ;; and compute how many unread articles there are in each group.
1565 (defun gnus-get-unread-articles (&optional level)
1566   (let* ((newsrc (cdr gnus-newsrc-alist))
1567          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1568          (foreign-level
1569           (min
1570            (cond ((and gnus-activate-foreign-newsgroups
1571                        (not (numberp gnus-activate-foreign-newsgroups)))
1572                   (1+ gnus-level-subscribed))
1573                  ((numberp gnus-activate-foreign-newsgroups)
1574                   gnus-activate-foreign-newsgroups)
1575                  (t 0))
1576            level))
1577          scanned-methods info group active method retrieve-groups)
1578     (gnus-message 5 "Checking new news...")
1579
1580     (while newsrc
1581       (setq active (gnus-active (setq group (gnus-info-group
1582                                              (setq info (pop newsrc))))))
1583
1584       ;; Check newsgroups.  If the user doesn't want to check them, or
1585       ;; they can't be checked (for instance, if the news server can't
1586       ;; be reached) we just set the number of unread articles in this
1587       ;; newsgroup to t.  This means that Gnus thinks that there are
1588       ;; unread articles, but it has no idea how many.
1589
1590       ;; To be more explicit:
1591       ;; >0 for an active group with messages
1592       ;; 0 for an active group with no unread messages
1593       ;; nil for non-foreign groups that the user has requested not be checked
1594       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1595       ;;   be checked, for one reason or other.
1596       (if (and (setq method (gnus-info-method info))
1597                (not (inline
1598                       (gnus-server-equal
1599                        gnus-select-method
1600                        (setq method (gnus-server-get-method nil method)))))
1601                (not (gnus-secondary-method-p method)))
1602           ;; These groups are foreign.  Check the level.
1603           (when (and (<= (gnus-info-level info) foreign-level)
1604                      (setq active (gnus-activate-group group 'scan)))
1605             ;; Let the Gnus agent save the active file.
1606             (when (and gnus-agent gnus-plugged active)
1607               (gnus-agent-save-group-info
1608                method (gnus-group-real-name group) active))
1609             (unless (inline (gnus-virtual-group-p group))
1610               (inline (gnus-close-group group)))
1611             (when (fboundp (intern (concat (symbol-name (car method))
1612                                            "-request-update-info")))
1613               (inline (gnus-request-update-info info method))))
1614         ;; These groups are native or secondary.
1615         (cond
1616          ;; We don't want these groups.
1617          ((> (gnus-info-level info) level)
1618           (setq active 'ignore))
1619          ;; Activate groups.
1620          ((not gnus-read-active-file)
1621           (if (gnus-check-backend-function 'retrieve-groups group)
1622               ;; if server support gnus-retrieve-groups we push
1623               ;; the group onto retrievegroups for later checking
1624               (if (assoc method retrieve-groups)
1625                   (setcdr (assoc method retrieve-groups)
1626                           (cons group (cdr (assoc method retrieve-groups))))
1627                 (push (list method group) retrieve-groups))
1628             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1629             ;; on the group, so we must perform a scan for every group
1630             ;; if the users has any directory mail sources.
1631             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1632             ;; for it scan all spool files even when the groups are
1633             ;; not required.
1634             (if (and
1635                  (or nnmail-scan-directory-mail-source-once
1636                      (null (assq 'directory
1637                                  (or mail-sources
1638                                      (if (listp nnmail-spool-file)
1639                                          nnmail-spool-file
1640                                        (list nnmail-spool-file))))))
1641                  (member method scanned-methods))
1642                 (setq active (gnus-activate-group group))
1643               (setq active (gnus-activate-group group 'scan))
1644               (push method scanned-methods))
1645             (when active
1646               (gnus-close-group group))))))
1647
1648       ;; Get the number of unread articles in the group.
1649       (cond
1650        ((eq active 'ignore)
1651         ;; Don't do anything.
1652         )
1653        (active
1654         (inline (gnus-get-unread-articles-in-group info active t)))
1655        (t
1656         ;; The group couldn't be reached, so we nix out the number of
1657         ;; unread articles and stuff.
1658         (gnus-set-active group nil)
1659         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1660           (when tmp
1661             (setcar tmp t))))))
1662
1663     ;; iterate through groups on methods which support gnus-retrieve-groups
1664     ;; and fetch a partial active file and use it to find new news.
1665     (dolist (rg retrieve-groups)
1666       (let ((method (or (car rg) gnus-select-method))
1667             (groups (cdr rg)))
1668         (when (gnus-check-server method)
1669           ;; Request that the backend scan its incoming messages.
1670           (when (gnus-check-backend-function 'request-scan (car method))
1671             (gnus-request-scan nil method))
1672           (gnus-read-active-file-2
1673            (mapcar (lambda (group) (gnus-group-real-name group)) groups)
1674            method)
1675           (dolist (group groups)
1676             (cond
1677              ((setq active (gnus-active (gnus-info-group
1678                                          (setq info (gnus-get-info group)))))
1679               (inline (gnus-get-unread-articles-in-group info active t)))
1680              (t
1681               ;; The group couldn't be reached, so we nix out the number of
1682               ;; unread articles and stuff.
1683               (gnus-set-active group nil)
1684               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1685
1686     (gnus-message 5 "Checking new news...done")))
1687
1688 ;; Create a hash table out of the newsrc alist.  The `car's of the
1689 ;; alist elements are used as keys.
1690 (defun gnus-make-hashtable-from-newsrc-alist ()
1691   (let ((alist gnus-newsrc-alist)
1692         (ohashtb gnus-newsrc-hashtb)
1693         prev)
1694     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1695     (setq alist
1696           (setq prev (setq gnus-newsrc-alist
1697                            (if (equal (caar gnus-newsrc-alist)
1698                                       "dummy.group")
1699                                gnus-newsrc-alist
1700                              (cons (list "dummy.group" 0 nil) alist)))))
1701     (while alist
1702       (gnus-sethash
1703        (caar alist)
1704        ;; Preserve number of unread articles in groups.
1705        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1706              prev)
1707        gnus-newsrc-hashtb)
1708       (setq prev alist
1709             alist (cdr alist)))))
1710
1711 (defun gnus-make-hashtable-from-killed ()
1712   "Create a hash table from the killed and zombie lists."
1713   (let ((lists '(gnus-killed-list gnus-zombie-list))
1714         list)
1715     (setq gnus-killed-hashtb
1716           (gnus-make-hashtable
1717            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1718     (while lists
1719       (setq list (symbol-value (pop lists)))
1720       (while list
1721         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1722
1723 (defun gnus-parse-active ()
1724   "Parse active info in the nntp server buffer."
1725   (save-excursion
1726     (set-buffer nntp-server-buffer)
1727     (goto-char (point-min))
1728     ;; Parse the result we got from `gnus-request-group'.
1729     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1730       (goto-char (match-beginning 1))
1731       (cons (read (current-buffer))
1732             (read (current-buffer))))))
1733
1734 (defun gnus-make-articles-unread (group articles)
1735   "Mark ARTICLES in GROUP as unread."
1736   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1737                           (gnus-gethash (gnus-group-real-name group)
1738                                         gnus-newsrc-hashtb))))
1739          (ranges (gnus-info-read info))
1740          news article)
1741     (while articles
1742       (when (gnus-member-of-range
1743              (setq article (pop articles)) ranges)
1744         (push article news)))
1745     (when news
1746       (gnus-info-set-read
1747        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1748       (gnus-group-update-group group t))))
1749
1750 ;; Enter all dead groups into the hashtb.
1751 (defun gnus-update-active-hashtb-from-killed ()
1752   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1753         (lists (list gnus-killed-list gnus-zombie-list))
1754         killed)
1755     (while lists
1756       (setq killed (car lists))
1757       (while killed
1758         (gnus-sethash (car killed) nil hashtb)
1759         (setq killed (cdr killed)))
1760       (setq lists (cdr lists)))))
1761
1762 (defun gnus-get-killed-groups ()
1763   "Go through the active hashtb and mark all unknown groups as killed."
1764   ;; First make sure active file has been read.
1765   (unless (gnus-read-active-file-p)
1766     (let ((gnus-read-active-file t))
1767       (gnus-read-active-file)))
1768   (unless gnus-killed-hashtb
1769     (gnus-make-hashtable-from-killed))
1770   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1771   (mapatoms
1772    (lambda (sym)
1773      (let ((groups 0)
1774            (group (symbol-name sym)))
1775        (if (or (null group)
1776                (gnus-gethash group gnus-killed-hashtb)
1777                (gnus-gethash group gnus-newsrc-hashtb))
1778            ()
1779          (let ((do-sub (gnus-matches-options-n group)))
1780            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1781                ()
1782              (setq groups (1+ groups))
1783              (push group gnus-killed-list)
1784              (gnus-sethash group group gnus-killed-hashtb))))))
1785    gnus-active-hashtb)
1786   (gnus-dribble-touch))
1787
1788 ;; Get the active file(s) from the backend(s).
1789 (defun gnus-read-active-file (&optional force not-native)
1790   (gnus-group-set-mode-line)
1791   (let ((methods
1792          (mapcar
1793           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1794           (append
1795            (if (and (not not-native)
1796                     (gnus-check-server gnus-select-method))
1797                ;; The native server is available.
1798                (cons gnus-select-method gnus-secondary-select-methods)
1799              ;; The native server is down, so we just do the
1800              ;; secondary ones.
1801              gnus-secondary-select-methods)
1802            ;; Also read from the archive server.
1803            (when (gnus-archive-server-wanted-p)
1804              (list "archive")))))
1805         method)
1806     (setq gnus-have-read-active-file nil)
1807     (save-excursion
1808       (set-buffer nntp-server-buffer)
1809       (while (setq method (pop methods))
1810         ;; Only do each method once, in case the methods appear more
1811         ;; than once in this list.
1812         (unless (member method methods)
1813           (if (or debug-on-error debug-on-quit)
1814               (gnus-read-active-file-1 method force)
1815             (condition-case ()
1816                 (gnus-read-active-file-1 method force)
1817               ;; We catch C-g so that we can continue past servers
1818               ;; that do not respond.
1819               (quit
1820                (message "Quit reading the active file")
1821                nil))))))))
1822
1823 (defun gnus-read-active-file-1 (method force)
1824   (let (where mesg)
1825     (setq where (nth 1 method)
1826           mesg (format "Reading active file%s via %s..."
1827                        (if (and where (not (zerop (length where))))
1828                            (concat " from " where) "")
1829                        (car method)))
1830     (gnus-message 5 mesg)
1831     (when (gnus-check-server method)
1832       ;; Request that the backend scan its incoming messages.
1833       (when (gnus-check-backend-function 'request-scan (car method))
1834         (gnus-request-scan nil method))
1835       (cond
1836        ((and (eq gnus-read-active-file 'some)
1837              (gnus-check-backend-function 'retrieve-groups (car method))
1838              (not force))
1839         (let ((newsrc (cdr gnus-newsrc-alist))
1840               (gmethod (gnus-server-get-method nil method))
1841               groups info)
1842           (while (setq info (pop newsrc))
1843             (when (inline
1844                     (gnus-server-equal
1845                      (inline
1846                        (gnus-find-method-for-group
1847                         (gnus-info-group info) info))
1848                      gmethod))
1849               (push (gnus-group-real-name (gnus-info-group info))
1850                     groups)))
1851           (gnus-read-active-file-2 groups method)))
1852        ((null method)
1853         t)
1854        (t
1855         (if (not (gnus-request-list method))
1856             (unless (equal method gnus-message-archive-method)
1857               (gnus-error 1 "Cannot read active file from %s server"
1858                           (car method)))
1859           (gnus-message 5 mesg)
1860           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1861           ;; We mark this active file as read.
1862           (push method gnus-have-read-active-file)
1863           (gnus-message 5 "%sdone" mesg)))))))
1864
1865 (defun gnus-read-active-file-2 (groups method)
1866   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1867   (when groups
1868     (save-excursion
1869       (set-buffer nntp-server-buffer)
1870       (gnus-check-server method)
1871       (let ((list-type (gnus-retrieve-groups groups method)))
1872         (cond ((not list-type)
1873                (gnus-error
1874                 1.2 "Cannot read partial active file from %s server."
1875                 (car method)))
1876               ((eq list-type 'active)
1877                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1878               (t
1879                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
1880
1881 ;; Read an active file and place the results in `gnus-active-hashtb'.
1882 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1883                                              real-active)
1884   (unless method
1885     (setq method gnus-select-method))
1886   (let ((cur (current-buffer))
1887         (hashtb (or hashtb
1888                     (if (and gnus-active-hashtb
1889                              (not (equal method gnus-select-method)))
1890                         gnus-active-hashtb
1891                       (setq gnus-active-hashtb
1892                             (if (equal method gnus-select-method)
1893                                 (gnus-make-hashtable
1894                                  (count-lines (point-min) (point-max)))
1895                               (gnus-make-hashtable 4096)))))))
1896     ;; Delete unnecessary lines.
1897     (goto-char (point-min))
1898     (cond
1899      ((string= gnus-ignored-newsgroups "")
1900       (delete-matching-lines "^to\\."))
1901      (t
1902       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
1903
1904     (goto-char (point-min))
1905     (unless (re-search-forward "[\\\"]" nil t)
1906       ;; Make the group names readable as a lisp expression even if they
1907       ;; contain special characters.
1908       (goto-char (point-max))
1909       (while (re-search-backward "[][';?()#]" nil t)
1910         (insert ?\\)))
1911
1912     ;; Let the Gnus agent save the active file.
1913     (when (and gnus-agent real-active gnus-plugged)
1914       (gnus-agent-save-active method))
1915
1916     ;; If these are groups from a foreign select method, we insert the
1917     ;; group prefix in front of the group names.
1918     (when (not (gnus-server-equal
1919                 (gnus-server-get-method nil method)
1920                 (gnus-server-get-method nil gnus-select-method)))
1921       (let ((prefix (gnus-group-prefixed-name "" method)))
1922         (goto-char (point-min))
1923         (while (and (not (eobp))
1924                     (progn
1925                       (when (= (following-char) ?\")
1926                         (forward-char 1))
1927                       (insert prefix)
1928                       (zerop (forward-line 1)))))))
1929     ;; Store the active file in a hash table.
1930     (goto-char (point-min))
1931     (let (group max min)
1932       (while (not (eobp))
1933         (condition-case err
1934             (progn
1935               (narrow-to-region (point) (gnus-point-at-eol))
1936               ;; group gets set to a symbol interned in the hash table
1937               ;; (what a hack!!) - jwz
1938               (setq group (let ((obarray hashtb)) (read cur)))
1939               ;; ### The extended group name scheme makes
1940               ;; the previous optimization strategy sort of pointless...
1941               (when (stringp group)
1942                 (setq group (intern group hashtb)))
1943               (if (and (numberp (setq max (read cur)))
1944                        (numberp (setq min (read cur)))
1945                        (progn
1946                          (skip-chars-forward " \t")
1947                          (not
1948                           (or (eq (char-after) ?=)
1949                               (eq (char-after) ?x)
1950                               (eq (char-after) ?j)))))
1951                   (progn
1952                     (set group (cons min max))
1953                     ;; if group is moderated, stick in moderation table
1954                     (when (eq (char-after) ?m)
1955                       (unless gnus-moderated-hashtb
1956                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
1957                       (gnus-sethash (symbol-name group) t
1958                                     gnus-moderated-hashtb)))
1959                 (set group nil)))
1960           (error
1961            (and group
1962                 (symbolp group)
1963                 (set group nil))
1964            (unless ignore-errors
1965              (gnus-message 3 "Warning - invalid active: %s"
1966                            (buffer-substring
1967                             (gnus-point-at-bol) (gnus-point-at-eol))))))
1968         (widen)
1969         (forward-line 1)))))
1970
1971 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
1972   ;; Parse a "groups" active file.
1973   (let ((cur (current-buffer))
1974         (hashtb (or hashtb
1975                     (if (and method gnus-active-hashtb)
1976                         gnus-active-hashtb
1977                       (setq gnus-active-hashtb
1978                             (gnus-make-hashtable
1979                              (count-lines (point-min) (point-max)))))))
1980         (prefix (and method
1981                      (not (gnus-server-equal
1982                            (gnus-server-get-method nil method)
1983                            (gnus-server-get-method nil gnus-select-method)))
1984                      (gnus-group-prefixed-name "" method))))
1985
1986     ;; Let the Gnus agent save the active file.
1987     (if (and gnus-agent
1988              real-active
1989              gnus-plugged
1990              (gnus-agent-method-p method))
1991         (progn
1992           (gnus-agent-save-groups method)
1993           (gnus-active-to-gnus-format method hashtb nil real-active))
1994
1995       (goto-char (point-min))
1996       ;; We split this into to separate loops, one with the prefix
1997       ;; and one without to speed the reading up somewhat.
1998       (if prefix
1999           (let (min max opoint group)
2000             (while (not (eobp))
2001               (condition-case ()
2002                   (progn
2003                     (read cur) (read cur)
2004                     (setq min (read cur)
2005                           max (read cur)
2006                           opoint (point))
2007                     (skip-chars-forward " \t")
2008                     (insert prefix)
2009                     (goto-char opoint)
2010                     (set (let ((obarray hashtb)) (read cur))
2011                          (cons min max)))
2012                 (error (and group (symbolp group) (set group nil))))
2013               (forward-line 1)))
2014         (let (min max group)
2015           (while (not (eobp))
2016             (condition-case ()
2017                 (when (eq (char-after) ?2)
2018                   (read cur) (read cur)
2019                   (setq min (read cur)
2020                         max (read cur))
2021                   (set (setq group (let ((obarray hashtb)) (read cur)))
2022                        (cons min max)))
2023               (error (and group (symbolp group) (set group nil))))
2024             (forward-line 1)))))))
2025
2026 (defun gnus-read-newsrc-file (&optional force)
2027   "Read startup file.
2028 If FORCE is non-nil, the .newsrc file is read."
2029   ;; Reset variables that might be defined in the .newsrc.eld file.
2030   (let ((variables gnus-variable-list))
2031     (while variables
2032       (set (car variables) nil)
2033       (setq variables (cdr variables))))
2034   (let* ((newsrc-file gnus-current-startup-file)
2035          (quick-file (concat newsrc-file ".el")))
2036     (save-excursion
2037       ;; We always load the .newsrc.eld file.  If always contains
2038       ;; much information that can not be gotten from the .newsrc
2039       ;; file (ticked articles, killed groups, foreign methods, etc.)
2040       (gnus-read-newsrc-el-file quick-file)
2041
2042       (when (and gnus-read-newsrc-file
2043                  (file-exists-p gnus-current-startup-file)
2044                  (or force
2045                      (and (file-newer-than-file-p newsrc-file quick-file)
2046                           (file-newer-than-file-p newsrc-file
2047                                                   (concat quick-file "d")))
2048                      (not gnus-newsrc-alist)))
2049         ;; We read the .newsrc file.  Note that if there if a
2050         ;; .newsrc.eld file exists, it has already been read, and
2051         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2052         ;; the .newsrc file, Gnus will only use the information it
2053         ;; can find there for changing the data already read -
2054         ;; i. e., reading the .newsrc file will not trash the data
2055         ;; already read (except for read articles).
2056         (save-excursion
2057           (gnus-message 5 "Reading %s..." newsrc-file)
2058           (set-buffer (nnheader-find-file-noselect newsrc-file))
2059           (buffer-disable-undo)
2060           (gnus-newsrc-to-gnus-format)
2061           (kill-buffer (current-buffer))
2062           (gnus-message 5 "Reading %s...done" newsrc-file)))
2063
2064       ;; Convert old to new.
2065       (gnus-convert-old-newsrc))))
2066
2067 (defun gnus-convert-old-newsrc ()
2068   "Convert old newsrc into the new format, if needed."
2069   (let ((fcv (and gnus-newsrc-file-version
2070                   (gnus-continuum-version gnus-newsrc-file-version))))
2071     (cond
2072      ;; No .newsrc.eld file was loaded.
2073      ((null fcv) nil)
2074      ;; Gnus 5 .newsrc.eld was loaded.
2075      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
2076       (gnus-convert-old-ticks)))))
2077
2078 (defun gnus-convert-old-ticks ()
2079   (let ((newsrc (cdr gnus-newsrc-alist))
2080         marks info dormant ticked)
2081     (while (setq info (pop newsrc))
2082       (when (setq marks (gnus-info-marks info))
2083         (setq dormant (cdr (assq 'dormant marks))
2084               ticked (cdr (assq 'tick marks)))
2085         (when (or dormant ticked)
2086           (gnus-info-set-read
2087            info
2088            (gnus-add-to-range
2089             (gnus-info-read info)
2090             (nconc (gnus-uncompress-range dormant)
2091                    (gnus-uncompress-range ticked)))))))))
2092
2093 (defun gnus-read-newsrc-el-file (file)
2094   (let ((ding-file (concat file "d")))
2095     ;; We always, always read the .eld file.
2096     (gnus-message 5 "Reading %s..." ding-file)
2097     (let (gnus-newsrc-assoc)
2098       (if (or debug-on-error debug-on-quit)
2099           (let ((coding-system-for-read gnus-ding-file-coding-system))
2100             (load ding-file t t t))
2101         (condition-case nil
2102             (let ((coding-system-for-read gnus-ding-file-coding-system))
2103               (load ding-file t t t))
2104           (error
2105            (ding)
2106            (unless (gnus-yes-or-no-p
2107                     (format "Error in %s; continue? " ding-file))
2108              (error "Error in %s" ding-file)))))
2109       ;; Older versions of `gnus-format-specs' are no longer valid
2110       ;; in Oort Gnus 0.01.
2111       (let ((version
2112              (and gnus-newsrc-file-version
2113                   (gnus-continuum-version gnus-newsrc-file-version))))
2114         (when (or (not version)
2115                   (< version 5.090002))
2116           (setq gnus-format-specs nil)))
2117       (when gnus-newsrc-assoc
2118         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
2119     (gnus-make-hashtable-from-newsrc-alist)
2120     (when (file-newer-than-file-p file ding-file)
2121       ;; Old format quick file
2122       (gnus-message 5 "Reading %s..." file)
2123       ;; The .el file is newer than the .eld file, so we read that one
2124       ;; as well.
2125       (gnus-read-old-newsrc-el-file file))))
2126
2127 ;; Parse the old-style quick startup file
2128 (defun gnus-read-old-newsrc-el-file (file)
2129   (let (newsrc killed marked group m info)
2130     (prog1
2131         (let ((gnus-killed-assoc nil)
2132               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2133           (prog1
2134               (ignore-errors
2135                 (load file t t t))
2136             (setq newsrc gnus-newsrc-assoc
2137                   killed gnus-killed-assoc
2138                   marked gnus-marked-assoc)))
2139       (setq gnus-newsrc-alist nil)
2140       (while (setq group (pop newsrc))
2141         (if (setq info (gnus-get-info (car group)))
2142             (progn
2143               (gnus-info-set-read info (cddr group))
2144               (gnus-info-set-level
2145                info (if (nth 1 group) gnus-level-default-subscribed
2146                       gnus-level-default-unsubscribed))
2147               (push info gnus-newsrc-alist))
2148           (push (setq info
2149                       (list (car group)
2150                             (if (nth 1 group) gnus-level-default-subscribed
2151                               gnus-level-default-unsubscribed)
2152                             (cddr group)))
2153                 gnus-newsrc-alist))
2154         ;; Copy marks into info.
2155         (when (setq m (assoc (car group) marked))
2156           (unless (nthcdr 3 info)
2157             (nconc info (list nil)))
2158           (gnus-info-set-marks
2159            info (list (cons 'tick (gnus-compress-sequence
2160                                    (sort (cdr m) '<) t))))))
2161       (setq newsrc killed)
2162       (while newsrc
2163         (setcar newsrc (caar newsrc))
2164         (setq newsrc (cdr newsrc)))
2165       (setq gnus-killed-list killed))
2166     ;; The .el file version of this variable does not begin with
2167     ;; "options", while the .eld version does, so we just add it if it
2168     ;; isn't there.
2169     (when
2170         gnus-newsrc-options
2171       (when (not (string-match "^ *options" gnus-newsrc-options))
2172         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2173       (when (not (string-match "\n$" gnus-newsrc-options))
2174         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2175       ;; Finally, if we read some options lines, we parse them.
2176       (unless (string= gnus-newsrc-options "")
2177         (gnus-newsrc-parse-options gnus-newsrc-options)))
2178
2179     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2180     (gnus-make-hashtable-from-newsrc-alist)))
2181
2182 (defun gnus-make-newsrc-file (file)
2183   "Make server dependent file name by catenating FILE and server host name."
2184   (let* ((file (expand-file-name file nil))
2185          (real-file (concat file "-" (nth 1 gnus-select-method))))
2186     (if (or (file-exists-p real-file)
2187             (file-exists-p (concat real-file ".el"))
2188             (file-exists-p (concat real-file ".eld")))
2189         real-file
2190       file)))
2191
2192 (defun gnus-newsrc-to-gnus-format ()
2193   (setq gnus-newsrc-options "")
2194   (setq gnus-newsrc-options-n nil)
2195
2196   (unless gnus-active-hashtb
2197     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2198   (let ((buf (current-buffer))
2199         (already-read (> (length gnus-newsrc-alist) 1))
2200         group subscribed options-symbol newsrc Options-symbol
2201         symbol reads num1)
2202     (goto-char (point-min))
2203     ;; We intern the symbol `options' in the active hashtb so that we
2204     ;; can `eq' against it later.
2205     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2206     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2207
2208     (while (not (eobp))
2209       ;; We first read the first word on the line by narrowing and
2210       ;; then reading into `gnus-active-hashtb'.  Most groups will
2211       ;; already exist in that hashtb, so this will save some string
2212       ;; space.
2213       (narrow-to-region
2214        (point)
2215        (progn (skip-chars-forward "^ \t!:\n") (point)))
2216       (goto-char (point-min))
2217       (setq symbol
2218             (and (/= (point-min) (point-max))
2219                  (let ((obarray gnus-active-hashtb)) (read buf))))
2220       (widen)
2221       ;; Now, the symbol we have read is either `options' or a group
2222       ;; name.  If it is an options line, we just add it to a string.
2223       (cond
2224        ((or (eq symbol options-symbol)
2225             (eq symbol Options-symbol))
2226         (setq gnus-newsrc-options
2227               ;; This concating is quite inefficient, but since our
2228               ;; thorough studies show that approx 99.37% of all
2229               ;; .newsrc files only contain a single options line, we
2230               ;; don't give a damn, frankly, my dear.
2231               (concat gnus-newsrc-options
2232                       (buffer-substring
2233                        (gnus-point-at-bol)
2234                        ;; Options may continue on the next line.
2235                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2236                                 (progn (beginning-of-line) (point)))
2237                            (point)))))
2238         (forward-line -1))
2239        (symbol
2240         ;; Group names can be just numbers.
2241         (when (numberp symbol)
2242           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2243         (unless (boundp symbol)
2244           (set symbol nil))
2245         ;; It was a group name.
2246         (setq subscribed (eq (char-after) ?:)
2247               group (symbol-name symbol)
2248               reads nil)
2249         (if (eolp)
2250             ;; If the line ends here, this is clearly a buggy line, so
2251             ;; we put point a the beginning of line and let the cond
2252             ;; below do the error handling.
2253             (beginning-of-line)
2254           ;; We skip to the beginning of the ranges.
2255           (skip-chars-forward "!: \t"))
2256         ;; We are now at the beginning of the list of read articles.
2257         ;; We read them range by range.
2258         (while
2259             (cond
2260              ((looking-at "[0-9]+")
2261               ;; We narrow and read a number instead of buffer-substring/
2262               ;; string-to-int because it's faster.  narrow/widen is
2263               ;; faster than save-restriction/narrow, and save-restriction
2264               ;; produces a garbage object.
2265               (setq num1 (progn
2266                            (narrow-to-region (match-beginning 0) (match-end 0))
2267                            (read buf)))
2268               (widen)
2269               ;; If the next character is a dash, then this is a range.
2270               (if (eq (char-after) ?-)
2271                   (progn
2272                     ;; We read the upper bound of the range.
2273                     (forward-char 1)
2274                     (if (not (looking-at "[0-9]+"))
2275                         ;; This is a buggy line, by we pretend that
2276                         ;; it's kinda OK.  Perhaps the user should be
2277                         ;; dinged?
2278                         (push num1 reads)
2279                       (push
2280                        (cons num1
2281                              (progn
2282                                (narrow-to-region (match-beginning 0)
2283                                                  (match-end 0))
2284                                (read buf)))
2285                        reads)
2286                       (widen)))
2287                 ;; It was just a simple number, so we add it to the
2288                 ;; list of ranges.
2289                 (push num1 reads))
2290               ;; If the next char in ?\n, then we have reached the end
2291               ;; of the line and return nil.
2292               (not (eq (char-after) ?\n)))
2293              ((eq (char-after) ?\n)
2294               ;; End of line, so we end.
2295               nil)
2296              (t
2297               ;; Not numbers and not eol, so this might be a buggy
2298               ;; line...
2299               (unless (eobp)
2300                 ;; If it was eob instead of ?\n, we allow it.
2301                 ;; The line was buggy.
2302                 (setq group nil)
2303                 (gnus-error 3.1 "Mangled line: %s"
2304                             (buffer-substring (gnus-point-at-bol)
2305                                               (gnus-point-at-eol))))
2306               nil))
2307           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2308           ;; we allow them, because it's a common mistake to put a
2309           ;; space after the comma.
2310           (skip-chars-forward ", "))
2311
2312         ;; We have already read .newsrc.eld, so we gently update the
2313         ;; data in the hash table with the information we have just
2314         ;; read.
2315         (when group
2316           (let ((info (gnus-get-info group))
2317                 level)
2318             (if info
2319                 ;; There is an entry for this file in the alist.
2320                 (progn
2321                   (gnus-info-set-read info (nreverse reads))
2322                   ;; We update the level very gently.  In fact, we
2323                   ;; only change it if there's been a status change
2324                   ;; from subscribed to unsubscribed, or vice versa.
2325                   (setq level (gnus-info-level info))
2326                   (cond ((and (<= level gnus-level-subscribed)
2327                               (not subscribed))
2328                          (setq level (if reads
2329                                          gnus-level-default-unsubscribed
2330                                        (1+ gnus-level-default-unsubscribed))))
2331                         ((and (> level gnus-level-subscribed) subscribed)
2332                          (setq level gnus-level-default-subscribed)))
2333                   (gnus-info-set-level info level))
2334               ;; This is a new group.
2335               (setq info (list group
2336                                (if subscribed
2337                                    gnus-level-default-subscribed
2338                                  (if reads
2339                                      (1+ gnus-level-subscribed)
2340                                    gnus-level-default-unsubscribed))
2341                                (nreverse reads))))
2342             (push info newsrc)))))
2343       (forward-line 1))
2344
2345     (setq newsrc (nreverse newsrc))
2346
2347     (if (not already-read)
2348         ()
2349       ;; We now have two newsrc lists - `newsrc', which is what we
2350       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2351       ;; what we've read from .newsrc.eld.  We have to merge these
2352       ;; lists.  We do this by "attaching" any (foreign) groups in the
2353       ;; gnus-newsrc-alist to the (native) group that precedes them.
2354       (let ((rc (cdr gnus-newsrc-alist))
2355             (prev gnus-newsrc-alist)
2356             entry mentry)
2357         (while rc
2358           (or (null (nth 4 (car rc)))   ; It's a native group.
2359               (assoc (caar rc) newsrc)  ; It's already in the alist.
2360               (if (setq entry (assoc (caar prev) newsrc))
2361                   (setcdr (setq mentry (memq entry newsrc))
2362                           (cons (car rc) (cdr mentry)))
2363                 (push (car rc) newsrc)))
2364           (setq prev rc
2365                 rc (cdr rc)))))
2366
2367     (setq gnus-newsrc-alist newsrc)
2368     ;; We make the newsrc hashtb.
2369     (gnus-make-hashtable-from-newsrc-alist)
2370
2371     ;; Finally, if we read some options lines, we parse them.
2372     (unless (string= gnus-newsrc-options "")
2373       (gnus-newsrc-parse-options gnus-newsrc-options))))
2374
2375 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2376 ;; The return value will be a list on the form
2377 ;; ((regexp1 . ignore)
2378 ;;  (regexp2 . subscribe)...)
2379 ;; When handling new newsgroups, groups that match a `ignore' regexp
2380 ;; will be ignored, and groups that match a `subscribe' regexp will be
2381 ;; subscribed.  A line like
2382 ;; options -n !all rec.all
2383 ;; will lead to a list that looks like
2384 ;; (("^rec\\..+" . subscribe)
2385 ;;  ("^.+" . ignore))
2386 ;; So all "rec.*" groups will be subscribed, while all the other
2387 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2388 ;; different from "options -n rec.all !all".
2389 (defun gnus-newsrc-parse-options (options)
2390   (let (out eol)
2391     (save-excursion
2392       (gnus-set-work-buffer)
2393       (insert (regexp-quote options))
2394       ;; First we treat all continuation lines.
2395       (goto-char (point-min))
2396       (while (re-search-forward "\n[ \t]+" nil t)
2397         (replace-match " " t t))
2398       ;; Then we transform all "all"s into ".+"s.
2399       (goto-char (point-min))
2400       (while (re-search-forward "\\ball\\b" nil t)
2401         (replace-match ".+" t t))
2402       (goto-char (point-min))
2403       ;; We remove all other options than the "-n" ones.
2404       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2405         (replace-match " ")
2406         (forward-char -1))
2407       (goto-char (point-min))
2408
2409       ;; We are only interested in "options -n" lines - we
2410       ;; ignore the other option lines.
2411       (while (re-search-forward "[ \t]-n" nil t)
2412         (setq eol
2413               (or (save-excursion
2414                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2415                          (- (point) 2)))
2416                   (gnus-point-at-eol)))
2417         ;; Search for all "words"...
2418         (while (re-search-forward "[^ \t,\n]+" eol t)
2419           (if (eq (char-after (match-beginning 0)) ?!)
2420               ;; If the word begins with a bang (!), this is a "not"
2421               ;; spec.  We put this spec (minus the bang) and the
2422               ;; symbol `ignore' into the list.
2423               (push (cons (concat
2424                            "^" (buffer-substring
2425                                 (1+ (match-beginning 0))
2426                                 (match-end 0))
2427                            "\\($\\|\\.\\)")
2428                           'ignore)
2429                     out)
2430             ;; There was no bang, so this is a "yes" spec.
2431             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2432                         'subscribe)
2433                   out))))
2434
2435       (setq gnus-newsrc-options-n out))))
2436
2437 (defun gnus-save-newsrc-file (&optional force)
2438   "Save .newsrc file."
2439   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2440   ;; from the variable gnus-newsrc-alist.
2441   (when (and (or gnus-newsrc-alist gnus-killed-list)
2442              gnus-current-startup-file)
2443     (save-excursion
2444       (if (and (or gnus-use-dribble-file gnus-slave)
2445                (not force)
2446                (or (not gnus-dribble-buffer)
2447                    (not (buffer-name gnus-dribble-buffer))
2448                    (zerop (save-excursion
2449                             (set-buffer gnus-dribble-buffer)
2450                             (buffer-size)))))
2451           (gnus-message 4 "(No changes need to be saved)")
2452         (gnus-run-hooks 'gnus-save-newsrc-hook)
2453         (if gnus-slave
2454             (gnus-slave-save-newsrc)
2455           ;; Save .newsrc.
2456           (when gnus-save-newsrc-file
2457             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2458             (gnus-gnus-to-newsrc-format)
2459             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2460           ;; Save .newsrc.eld.
2461           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2462           (make-local-variable 'version-control)
2463           (setq version-control 'never)
2464           (setq buffer-file-name
2465                 (concat gnus-current-startup-file ".eld"))
2466           (setq default-directory (file-name-directory buffer-file-name))
2467           (buffer-disable-undo)
2468           (erase-buffer)
2469           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2470           (gnus-gnus-to-quick-newsrc-format)
2471           (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2472           (let ((coding-system-for-write gnus-ding-file-coding-system))
2473             (save-buffer))
2474           (kill-buffer (current-buffer))
2475           (gnus-message
2476            5 "Saving %s.eld...done" gnus-current-startup-file))
2477         (gnus-dribble-delete-file)
2478         (gnus-group-set-mode-line)))))
2479
2480 (defun gnus-gnus-to-quick-newsrc-format ()
2481   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
2482   (let ((print-quoted t)
2483         (print-escape-newlines t))
2484
2485     (insert ";; -*- emacs-lisp -*-\n")
2486     (insert ";; Gnus startup file.\n")
2487     (insert "\
2488 ;; Never delete this file -- if you want to force Gnus to read the
2489 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2490     (insert "(setq gnus-newsrc-file-version "
2491             (prin1-to-string gnus-version) ")\n")
2492     (let* ((gnus-killed-list
2493             (if (and gnus-save-killed-list
2494                      (stringp gnus-save-killed-list))
2495                 (gnus-strip-killed-list)
2496               gnus-killed-list))
2497            (variables
2498             (if gnus-save-killed-list gnus-variable-list
2499               ;; Remove the `gnus-killed-list' from the list of variables
2500               ;; to be saved, if required.
2501               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2502            ;; Peel off the "dummy" group.
2503            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2504            variable)
2505       ;; Insert the variables into the file.
2506       (while variables
2507         (when (and (boundp (setq variable (pop variables)))
2508                    (symbol-value variable))
2509           (insert "(setq " (symbol-name variable) " '")
2510           (gnus-prin1 (symbol-value variable))
2511           (insert ")\n"))))))
2512
2513 (defun gnus-strip-killed-list ()
2514   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2515   (let ((list gnus-killed-list)
2516         olist)
2517     (while list
2518       (when (string-match gnus-save-killed-list (car list))
2519         (push (car list) olist))
2520       (pop list))
2521     (nreverse olist)))
2522
2523 (defun gnus-gnus-to-newsrc-format ()
2524   ;; Generate and save the .newsrc file.
2525   (save-excursion
2526     (set-buffer (create-file-buffer gnus-current-startup-file))
2527     (let ((newsrc (cdr gnus-newsrc-alist))
2528           (standard-output (current-buffer))
2529           info ranges range method)
2530       (setq buffer-file-name gnus-current-startup-file)
2531       (setq default-directory (file-name-directory buffer-file-name))
2532       (buffer-disable-undo)
2533       (erase-buffer)
2534       ;; Write options.
2535       (when gnus-newsrc-options
2536         (insert gnus-newsrc-options))
2537       ;; Write subscribed and unsubscribed.
2538       (while (setq info (pop newsrc))
2539         ;; Don't write foreign groups to .newsrc.
2540         (when (or (null (setq method (gnus-info-method info)))
2541                   (equal method "native")
2542                   (inline (gnus-server-equal method gnus-select-method)))
2543           (insert (gnus-info-group info)
2544                   (if (> (gnus-info-level info) gnus-level-subscribed)
2545                       "!" ":"))
2546           (when (setq ranges (gnus-info-read info))
2547             (insert " ")
2548             (if (not (listp (cdr ranges)))
2549                 (if (= (car ranges) (cdr ranges))
2550                     (princ (car ranges))
2551                   (princ (car ranges))
2552                   (insert "-")
2553                   (princ (cdr ranges)))
2554               (while (setq range (pop ranges))
2555                 (if (or (atom range) (= (car range) (cdr range)))
2556                     (princ (or (and (atom range) range) (car range)))
2557                   (princ (car range))
2558                   (insert "-")
2559                   (princ (cdr range)))
2560                 (when ranges
2561                   (insert ",")))))
2562           (insert "\n")))
2563       (make-local-variable 'version-control)
2564       (setq version-control 'never)
2565       ;; It has been reported that sometime the modtime on the .newsrc
2566       ;; file seems to be off.  We really do want to overwrite it, so
2567       ;; we clear the modtime here before saving.  It's a bit odd,
2568       ;; though...
2569       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2570       ;; delete the silly thing entirely first.  but this fails to provide
2571       ;; such niceties as .newsrc~ creation.
2572       (if gnus-modtime-botch
2573           (delete-file gnus-startup-file)
2574         (clear-visited-file-modtime))
2575       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2576       (save-buffer)
2577       (kill-buffer (current-buffer)))))
2578
2579 \f
2580 ;;;
2581 ;;; Slave functions.
2582 ;;;
2583
2584 (defvar gnus-slave-mode nil)
2585
2586 (defun gnus-slave-mode ()
2587   "Minor mode for slave Gnusae."
2588   (gnus-add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2589   (gnus-run-hooks 'gnus-slave-mode-hook))
2590
2591 (defun gnus-slave-save-newsrc ()
2592   (save-excursion
2593     (set-buffer gnus-dribble-buffer)
2594     (let ((slave-name
2595            (make-temp-name (concat gnus-current-startup-file "-slave-")))
2596           (modes (ignore-errors
2597                    (file-modes (concat gnus-current-startup-file ".eld")))))
2598       (let ((coding-system-for-write gnus-ding-file-coding-system))
2599         (gnus-write-buffer slave-name))
2600       (when modes
2601         (set-file-modes slave-name modes)))))
2602
2603 (defun gnus-master-read-slave-newsrc ()
2604   (let ((slave-files
2605          (directory-files
2606           (file-name-directory gnus-current-startup-file)
2607           t (concat
2608              "^" (regexp-quote
2609                   (concat
2610                    (file-name-nondirectory gnus-current-startup-file)
2611                    "-slave-")))
2612           t))
2613         file)
2614     (if (not slave-files)
2615         ()                              ; There are no slave files to read.
2616       (gnus-message 7 "Reading slave newsrcs...")
2617       (save-excursion
2618         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2619         (setq slave-files
2620               (sort (mapcar (lambda (file)
2621                               (list (nth 5 (file-attributes file)) file))
2622                             slave-files)
2623                     (lambda (f1 f2)
2624                       (or (< (caar f1) (caar f2))
2625                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2626         (while slave-files
2627           (erase-buffer)
2628           (setq file (nth 1 (car slave-files)))
2629           (nnheader-insert-file-contents file)
2630           (when (condition-case ()
2631                     (progn
2632                       (eval-buffer (current-buffer))
2633                       t)
2634                   (error
2635                    (gnus-error 3.2 "Possible error in %s" file)
2636                    nil))
2637             (unless gnus-slave          ; Slaves shouldn't delete these files.
2638               (ignore-errors
2639                 (delete-file file))))
2640           (setq slave-files (cdr slave-files))))
2641       (gnus-dribble-touch)
2642       (gnus-message 7 "Reading slave newsrcs...done"))))
2643
2644 \f
2645 ;;;
2646 ;;; Group description.
2647 ;;;
2648
2649 (defun gnus-read-all-descriptions-files ()
2650   (let ((methods (cons gnus-select-method
2651                        (nconc
2652                         (when (gnus-archive-server-wanted-p)
2653                           (list "archive"))
2654                         gnus-secondary-select-methods))))
2655     (while methods
2656       (gnus-read-descriptions-file (car methods))
2657       (setq methods (cdr methods)))
2658     t))
2659
2660 (defun gnus-read-descriptions-file (&optional method)
2661   (let ((method (or method gnus-select-method))
2662         group)
2663     (when (stringp method)
2664       (setq method (gnus-server-to-method method)))
2665     ;; We create the hashtable whether we manage to read the desc file
2666     ;; to avoid trying to re-read after a failed read.
2667     (unless gnus-description-hashtb
2668       (setq gnus-description-hashtb
2669             (gnus-make-hashtable (length gnus-active-hashtb))))
2670     ;; Mark this method's desc file as read.
2671     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2672                   gnus-description-hashtb)
2673
2674     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2675     (cond
2676      ((null (gnus-get-function method 'request-list-newsgroups t))
2677       t)
2678      ((not (gnus-check-server method))
2679       (gnus-message 1 "Couldn't open server")
2680       nil)
2681      ((not (gnus-request-list-newsgroups method))
2682       (gnus-message 1 "Couldn't read newsgroups descriptions")
2683       nil)
2684      (t
2685       (save-excursion
2686         (save-restriction
2687           (set-buffer nntp-server-buffer)
2688           (goto-char (point-min))
2689           (when (or (search-forward "\n.\n" nil t)
2690                     (goto-char (point-max)))
2691             (beginning-of-line)
2692             (narrow-to-region (point-min) (point)))
2693           ;; If these are groups from a foreign select method, we insert the
2694           ;; group prefix in front of the group names.
2695           (and method (not (inline
2696                              (gnus-server-equal
2697                               (gnus-server-get-method nil method)
2698                               (gnus-server-get-method
2699                                nil gnus-select-method))))
2700                (let ((prefix (gnus-group-prefixed-name "" method)))
2701                  (goto-char (point-min))
2702                  (while (and (not (eobp))
2703                              (progn (insert prefix)
2704                                     (zerop (forward-line 1)))))))
2705           (goto-char (point-min))
2706           (while (not (eobp))
2707             ;; If we get an error, we set group to 0, which is not a
2708             ;; symbol...
2709             (setq group
2710                   (condition-case ()
2711                       (let ((obarray gnus-description-hashtb))
2712                         ;; Group is set to a symbol interned in this
2713                         ;; hash table.
2714                         (read nntp-server-buffer))
2715                     (error 0)))
2716             (skip-chars-forward " \t")
2717             ;; ...  which leads to this line being effectively ignored.
2718             (when (symbolp group)
2719               (let* ((str (buffer-substring
2720                            (point) (progn (end-of-line) (point))))
2721                      (name (symbol-name group))
2722                      (charset
2723                       (or (gnus-group-name-charset method name)
2724                           (gnus-parameter-charset name))))
2725                 (when (and str charset (featurep 'mule))
2726                   (setq str (mm-decode-coding-string str charset)))
2727                 (set group str)))
2728             (forward-line 1))))
2729       (gnus-message 5 "Reading descriptions file...done")
2730       t))))
2731
2732 (defun gnus-group-get-description (group)
2733   "Get the description of a group by sending XGTITLE to the server."
2734   (when (gnus-request-group-description group)
2735     (save-excursion
2736       (set-buffer nntp-server-buffer)
2737       (goto-char (point-min))
2738       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
2739         (match-string 1)))))
2740
2741 ;;;###autoload
2742 (defun gnus-declare-backend (name &rest abilities)
2743   "Declare backend NAME with ABILITIES as a Gnus backend."
2744   (setq gnus-valid-select-methods
2745         (nconc gnus-valid-select-methods
2746                (list (apply 'list name abilities))))
2747   (gnus-redefine-select-method-widget))
2748
2749 (defun gnus-set-default-directory ()
2750   "Set the default directory in the current buffer to `gnus-default-directory'.
2751 If this variable is nil, don't do anything."
2752   (setq default-directory
2753         (if (and gnus-default-directory
2754                  (file-exists-p gnus-default-directory))
2755             (file-name-as-directory (expand-file-name gnus-default-directory))
2756           default-directory)))
2757
2758 (defun gnus-display-time-event-handler ()
2759   "Like `display-time-event-handler', but test `display-time-timer'."
2760   (when (gnus-boundp 'display-time-timer)
2761     (display-time-event-handler)))
2762
2763 (provide 'gnus-start)
2764
2765 ;;; gnus-start.el ends here