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