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