Synch to 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, 2003
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   (dolist (buffer (gnus-buffers))
681     (gnus-kill-buffer buffer))
682   ;; Remove Gnus frames.
683   (gnus-kill-gnus-frames))
684
685 (defun gnus-no-server-1 (&optional arg slave)
686   "Read network news.
687 If ARG is a positive number, Gnus will use that as the
688 startup level.  If ARG is nil, Gnus will be started at level 2.
689 If ARG is non-nil and not a positive number, Gnus will
690 prompt the user for the name of an NNTP server to use.
691 As opposed to `gnus', this command will not connect to the local server."
692   (interactive "P")
693   (let ((val (or arg (1- gnus-level-default-subscribed))))
694     (gnus val t slave)
695     (make-local-variable 'gnus-group-use-permanent-levels)
696     (setq gnus-group-use-permanent-levels val)))
697
698 (defun gnus-1 (&optional arg dont-connect slave)
699   "Read network news.
700 If ARG is non-nil and a positive number, Gnus will use that as the
701 startup level.  If ARG is non-nil and not a positive number, Gnus will
702 prompt the user for the name of an NNTP server to use."
703   (interactive "P")
704
705   (if (gnus-alive-p)
706       (progn
707         (switch-to-buffer gnus-group-buffer)
708         (gnus-group-get-new-news
709          (and (numberp arg)
710               (> arg 0)
711               (max (car gnus-group-list-mode) arg))))
712
713     (gnus-clear-system)
714     (gnus-splash)
715     (gnus-run-hooks 'gnus-before-startup-hook)
716     (nnheader-init-server-buffer)
717     (setq gnus-slave slave)
718     (gnus-read-init-file)
719     (if gnus-agent
720         (gnus-agentize))
721
722     (when gnus-simple-splash
723       (setq gnus-simple-splash nil)
724       (cond
725        ((featurep 'xemacs)
726         (gnus-xmas-splash))
727        ((and window-system
728              (= (frame-height) (1+ (window-height))))
729         (gnus-x-splash))))
730
731     (let ((level (and (numberp arg) (> arg 0) arg))
732           did-connect)
733       (unwind-protect
734           (progn
735             (unless dont-connect
736               (setq did-connect
737                     (gnus-start-news-server (and arg (not level))))))
738         (if (and (not dont-connect)
739                  (not did-connect))
740             (gnus-group-quit)
741           (gnus-run-hooks 'gnus-startup-hook)
742           ;; NNTP server is successfully open.
743
744           ;; Find the current startup file name.
745           (setq gnus-current-startup-file
746                 (gnus-make-newsrc-file gnus-startup-file))
747
748           ;; Read the dribble file.
749           (when (or gnus-slave gnus-use-dribble-file)
750             (gnus-dribble-read-file))
751
752           ;; Allow using GroupLens predictions.
753           (when gnus-use-grouplens
754             (bbb-login)
755             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
756
757           ;; Do the actual startup.
758           (if gnus-agent
759               (gnus-request-create-group "queue" '(nndraft "")))
760           (gnus-request-create-group "drafts" '(nndraft ""))
761           (gnus-setup-news nil level dont-connect)
762           (gnus-run-hooks 'gnus-setup-news-hook)
763           (gnus-start-draft-setup)
764           ;; Generate the group buffer.
765           (gnus-group-list-groups level)
766           (gnus-group-first-unread-group)
767           (gnus-configure-windows 'group)
768           (gnus-group-set-mode-line)
769           ;; For reading Info.
770           (set-language-info "Japanese" 'gnus-info "gnus-ja")
771           (gnus-run-hooks 'gnus-started-hook))))))
772
773 (defun gnus-start-draft-setup ()
774   "Make sure the draft group exists."
775   (gnus-request-create-group "drafts" '(nndraft ""))
776   (unless (gnus-gethash "nndraft:drafts" gnus-newsrc-hashtb)
777     (let ((gnus-level-default-subscribed 1))
778       (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
779     (gnus-group-set-parameter
780      "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
781
782 ;;;###autoload
783 (defun gnus-unload ()
784   "Unload all Gnus features.
785 \(For some value of `all' or `Gnus'.)  Currently, features whose names
786 have prefixes `gnus-', `nn', `mm-' or `rfc' are unloaded.  Use
787 cautiously -- unloading may cause trouble."
788   (interactive)
789   (dolist (feature features)
790     (if (string-match "^\\(gnus-\\|nn\\|mm-\\|rfc\\)" (symbol-name feature))
791         (unload-feature feature 'force))))
792
793 \f
794 ;;;
795 ;;; Dribble file
796 ;;;
797
798 (defvar gnus-dribble-ignore nil)
799 (defvar gnus-dribble-eval-file nil)
800
801 (defun gnus-dribble-file-name ()
802   "Return the dribble file for the current .newsrc."
803   (concat
804    (if gnus-dribble-directory
805        (concat (file-name-as-directory gnus-dribble-directory)
806                (file-name-nondirectory gnus-current-startup-file))
807      gnus-current-startup-file)
808    "-dribble"))
809
810 (defun gnus-dribble-enter (string)
811   "Enter STRING into the dribble buffer."
812   (when (and (not gnus-dribble-ignore)
813              gnus-dribble-buffer
814              (buffer-name gnus-dribble-buffer))
815     (let ((obuf (current-buffer)))
816       (set-buffer gnus-dribble-buffer)
817       (goto-char (point-max))
818       (insert string "\n")
819       ;; This has been commented by Josh Huber <huber@alum.wpi.edu>
820       ;; It causes problems with both XEmacs and Emacs 21, and doesn't
821       ;; seem to be of much value. (FIXME: remove this after we make sure
822       ;; it's not needed).
823       ;; (set-window-point (get-buffer-window (current-buffer)) (point-max))
824       (bury-buffer gnus-dribble-buffer)
825       (save-excursion
826         (set-buffer gnus-group-buffer)
827         (gnus-group-set-mode-line))
828       (set-buffer obuf))))
829
830 (defun gnus-dribble-touch ()
831   "Touch the dribble buffer."
832   (gnus-dribble-enter ""))
833
834 (defun gnus-dribble-read-file ()
835   "Read the dribble file from disk."
836   (let ((dribble-file (gnus-dribble-file-name)))
837     (save-excursion
838       (set-buffer (setq gnus-dribble-buffer
839                         (gnus-get-buffer-create
840                          (file-name-nondirectory dribble-file))))
841       (erase-buffer)
842       (setq buffer-file-name dribble-file)
843       (auto-save-mode t)
844       (buffer-disable-undo)
845       (bury-buffer (current-buffer))
846       (set-buffer-modified-p nil)
847       (let ((auto (make-auto-save-file-name))
848             (gnus-dribble-ignore t)
849             (purpose nil)
850             modes)
851         (when (or (file-exists-p auto) (file-exists-p dribble-file))
852           ;; Load whichever file is newest -- the auto save file
853           ;; or the "real" file.
854           (if (file-newer-than-file-p auto dribble-file)
855               (nnheader-insert-file-contents auto)
856             (nnheader-insert-file-contents dribble-file))
857           (unless (zerop (buffer-size))
858             (set-buffer-modified-p t))
859           ;; Set the file modes to reflect the .newsrc file modes.
860           (save-buffer)
861           (when (and (file-exists-p gnus-current-startup-file)
862                      (file-exists-p dribble-file)
863                      (setq modes (file-modes gnus-current-startup-file)))
864             (set-file-modes dribble-file modes))
865           (goto-char (point-min))
866           (when (search-forward "Gnus was exited on purpose" nil t)
867             (setq purpose t))
868           ;; Possibly eval the file later.
869           (when (or gnus-always-read-dribble-file
870                     (gnus-y-or-n-p
871                      (if purpose
872                          "Gnus exited on purpose without saving; read auto-save file anyway? "
873                        "Gnus auto-save file exists.  Do you want to read it? ")))
874             (setq gnus-dribble-eval-file t)))))))
875
876 (defun gnus-dribble-eval-file ()
877   (when gnus-dribble-eval-file
878     (setq gnus-dribble-eval-file nil)
879     (save-excursion
880       (let ((gnus-dribble-ignore t))
881         (set-buffer gnus-dribble-buffer)
882         (eval-buffer (current-buffer))))))
883
884 (defun gnus-dribble-delete-file ()
885   (when (file-exists-p (gnus-dribble-file-name))
886     (delete-file (gnus-dribble-file-name)))
887   (when gnus-dribble-buffer
888     (save-excursion
889       (set-buffer gnus-dribble-buffer)
890       (let ((auto (make-auto-save-file-name)))
891         (when (file-exists-p auto)
892           (delete-file auto))
893         (erase-buffer)
894         (set-buffer-modified-p nil)))))
895
896 (defun gnus-dribble-save ()
897   (when (and gnus-dribble-buffer
898              (buffer-name gnus-dribble-buffer))
899     (save-excursion
900       (set-buffer gnus-dribble-buffer)
901       (save-buffer))))
902
903 (defun gnus-dribble-clear ()
904   (when (gnus-buffer-exists-p gnus-dribble-buffer)
905     (save-excursion
906       (set-buffer gnus-dribble-buffer)
907       (erase-buffer)
908       (set-buffer-modified-p nil)
909       (setq buffer-saved-size (buffer-size)))))
910
911 \f
912 ;;;
913 ;;; Active & Newsrc File Handling
914 ;;;
915
916 (defun gnus-setup-news (&optional rawfile level dont-connect)
917   "Setup news information.
918 If RAWFILE is non-nil, the .newsrc file will also be read.
919 If LEVEL is non-nil, the news will be set up at level LEVEL."
920   (require 'nnmail)
921   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
922         ;; Binding this variable will inhibit multiple fetchings
923         ;; of the same mail source.
924         (nnmail-fetched-sources (list t)))
925
926     (when init
927       ;; Clear some variables to re-initialize news information.
928       (setq gnus-newsrc-alist nil
929             gnus-active-hashtb nil)
930       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
931       (gnus-read-newsrc-file rawfile))
932
933     ;; Make sure the archive server is available to all and sundry.
934     (when gnus-message-archive-method
935       (unless (assoc "archive" gnus-server-alist)
936         (push `("archive"
937                 nnfolder
938                 "archive"
939                 (nnfolder-directory
940                  ,(nnheader-concat message-directory "archive"))
941                 (nnfolder-active-file
942                  ,(nnheader-concat message-directory "archive/active"))
943                 (nnfolder-get-new-mail nil)
944                 (nnfolder-inhibit-expiry t))
945               gnus-server-alist)))
946
947     ;; If we don't read the complete active file, we fill in the
948     ;; hashtb here.
949     (when (or (null gnus-read-active-file)
950               (eq gnus-read-active-file 'some))
951       (gnus-update-active-hashtb-from-killed))
952
953     ;; Read the active file and create `gnus-active-hashtb'.
954     ;; If `gnus-read-active-file' is nil, then we just create an empty
955     ;; hash table.  The partial filling out of the hash table will be
956     ;; done in `gnus-get-unread-articles'.
957     (and gnus-read-active-file
958          (not level)
959          (gnus-read-active-file nil dont-connect))
960
961     (unless gnus-active-hashtb
962       (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
963
964     ;; Initialize the cache.
965     (when gnus-use-cache
966       (gnus-cache-open))
967
968     ;; Possibly eval the dribble file.
969     (and init
970          (or gnus-use-dribble-file gnus-slave)
971          (gnus-dribble-eval-file))
972
973     ;; Slave Gnusii should then clear the dribble buffer.
974     (when (and init gnus-slave)
975       (gnus-dribble-clear))
976
977     (gnus-update-format-specifications)
978
979     ;; See whether we need to read the description file.
980     (when (and (boundp 'gnus-group-line-format)
981                (stringp gnus-group-line-format)
982                (let ((case-fold-search nil))
983                  (string-match "%[-,0-9]*D" gnus-group-line-format))
984                (not gnus-description-hashtb)
985                (not dont-connect)
986                gnus-read-active-file)
987       (gnus-read-all-descriptions-files))
988
989     ;; Find new newsgroups and treat them.
990     (when (and init gnus-check-new-newsgroups (not level)
991                (gnus-check-server gnus-select-method)
992                (not gnus-slave)
993                gnus-plugged)
994       (gnus-find-new-newsgroups))
995
996     ;; Check and remove bogus newsgroups.
997     (when (and init gnus-check-bogus-newsgroups
998                gnus-read-active-file (not level)
999                (gnus-server-opened gnus-select-method))
1000       (gnus-check-bogus-newsgroups))
1001
1002     ;; We might read in new NoCeM messages here.
1003     (when (and gnus-use-nocem
1004                (not level)
1005                (not dont-connect))
1006       (gnus-nocem-scan-groups))
1007
1008     ;; Read any slave files.
1009     (gnus-master-read-slave-newsrc)
1010
1011     ;; Find the number of unread articles in each non-dead group.
1012     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
1013       (gnus-get-unread-articles level))))
1014
1015 (defun gnus-call-subscribe-functions (method group)
1016   "Call METHOD to subscribe GROUP.
1017 If no function returns `non-nil', call `gnus-subscribe-zombies'."
1018   (unless (cond
1019            ((gnus-functionp method)
1020             (funcall method group))
1021            ((listp method)
1022             (catch 'found
1023               (dolist (func method)
1024                 (if (funcall func group)
1025                     (throw 'found t)))
1026               nil))
1027            (t nil))
1028     (gnus-subscribe-zombies group)))
1029
1030 (defun gnus-find-new-newsgroups (&optional arg)
1031   "Search for new newsgroups and add them.
1032 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
1033 The `-n' option line from .newsrc is respected.
1034
1035 With 1 C-u, use the `ask-server' method to query the server for new
1036 groups.
1037 With 2 C-u's, use most complete method possible to query the server
1038 for new groups, and subscribe the new groups as zombies."
1039   (interactive "p")
1040   (let* ((gnus-subscribe-newsgroup-method
1041           gnus-subscribe-newsgroup-method)
1042          (check (cond
1043                  ((or (and (= (or arg 1) 4)
1044                            (not (listp gnus-check-new-newsgroups)))
1045                       (null gnus-read-active-file)
1046                       (eq gnus-read-active-file 'some))
1047                   'ask-server)
1048                  ((= (or arg 1) 16)
1049                   (setq gnus-subscribe-newsgroup-method
1050                         'gnus-subscribe-zombies)
1051                   t)
1052                  (t gnus-check-new-newsgroups))))
1053     (unless (gnus-check-first-time-used)
1054       (if (or (consp check)
1055               (eq check 'ask-server))
1056           ;; Ask the server for new groups.
1057           (gnus-ask-server-for-new-groups)
1058         ;; Go through the active hashtb and look for new groups.
1059         (let ((groups 0)
1060               group new-newsgroups)
1061           (gnus-message 5 "Looking for new newsgroups...")
1062           (unless gnus-have-read-active-file
1063             (gnus-read-active-file))
1064           (setq gnus-newsrc-last-checked-date (message-make-date))
1065           (unless gnus-killed-hashtb
1066             (gnus-make-hashtable-from-killed))
1067           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1068           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1069           (mapatoms
1070            (lambda (sym)
1071              (if (or (null (setq group (symbol-name sym)))
1072                      (not (boundp sym))
1073                      (null (symbol-value sym))
1074                      (gnus-gethash group gnus-killed-hashtb)
1075                      (gnus-gethash group gnus-newsrc-hashtb))
1076                  ()
1077                (let ((do-sub (gnus-matches-options-n group)))
1078                  (cond
1079                   ((eq do-sub 'subscribe)
1080                    (setq groups (1+ groups))
1081                    (gnus-sethash group group gnus-killed-hashtb)
1082                    (gnus-call-subscribe-functions
1083                     gnus-subscribe-options-newsgroup-method group))
1084                   ((eq do-sub 'ignore)
1085                    nil)
1086                   (t
1087                    (setq groups (1+ groups))
1088                    (gnus-sethash group group gnus-killed-hashtb)
1089                    (if gnus-subscribe-hierarchical-interactive
1090                        (push group new-newsgroups)
1091                      (gnus-call-subscribe-functions
1092                       gnus-subscribe-newsgroup-method group)))))))
1093            gnus-active-hashtb)
1094           (when new-newsgroups
1095             (gnus-subscribe-hierarchical-interactive new-newsgroups))
1096           (if (> groups 0)
1097               (gnus-message 5 "%d new newsgroup%s arrived."
1098                             groups (if (> groups 1) "s have" " has"))
1099             (gnus-message 5 "No new newsgroups.")))))))
1100
1101 (defun gnus-matches-options-n (group)
1102   ;; Returns `subscribe' if the group is to be unconditionally
1103   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1104   ;; no match for the group.
1105
1106   ;; First we check the two user variables.
1107   (cond
1108    ((and gnus-options-subscribe
1109          (string-match gnus-options-subscribe group))
1110     'subscribe)
1111    ((and gnus-auto-subscribed-groups
1112          (string-match gnus-auto-subscribed-groups group))
1113     'subscribe)
1114    ((and gnus-options-not-subscribe
1115          (string-match gnus-options-not-subscribe group))
1116     'ignore)
1117    ;; Then we go through the list that was retrieved from the .newsrc
1118    ;; file.  This list has elements on the form
1119    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
1120    ;; is in the reverse order of the options line) is returned.
1121    (t
1122     (let ((regs gnus-newsrc-options-n))
1123       (while (and regs
1124                   (not (string-match (caar regs) group)))
1125         (setq regs (cdr regs)))
1126       (and regs (cdar regs))))))
1127
1128 (defun gnus-ask-server-for-new-groups ()
1129   (let* ((new-date (message-make-date))
1130          (date (or gnus-newsrc-last-checked-date new-date))
1131          (methods (cons gnus-select-method
1132                         (nconc
1133                          (when (gnus-archive-server-wanted-p)
1134                            (list "archive"))
1135                          (append
1136                           (and (consp gnus-check-new-newsgroups)
1137                                gnus-check-new-newsgroups)
1138                           gnus-secondary-select-methods))))
1139          (groups 0)
1140          group new-newsgroups got-new method hashtb
1141          gnus-override-subscribe-method)
1142     (unless gnus-killed-hashtb
1143       (gnus-make-hashtable-from-killed))
1144     ;; Go through both primary and secondary select methods and
1145     ;; request new newsgroups.
1146     (while (setq method (gnus-server-get-method nil (pop methods)))
1147       (setq new-newsgroups nil
1148             gnus-override-subscribe-method method)
1149       (when (and (gnus-check-server method)
1150                  (gnus-request-newgroups date method))
1151         (save-excursion
1152           (setq got-new t
1153                 hashtb (gnus-make-hashtable 100))
1154           (set-buffer nntp-server-buffer)
1155           ;; Enter all the new groups into a hashtable.
1156           (gnus-active-to-gnus-format method hashtb 'ignore))
1157         ;; Now all new groups from `method' are in `hashtb'.
1158         (mapatoms
1159          (lambda (group-sym)
1160            (if (or (null (setq group (symbol-name group-sym)))
1161                    (not (boundp group-sym))
1162                    (null (symbol-value group-sym))
1163                    (gnus-gethash group gnus-newsrc-hashtb)
1164                    (member group gnus-zombie-list)
1165                    (member group gnus-killed-list))
1166                ;; The group is already known.
1167                ()
1168              ;; Make this group active.
1169              (when (symbol-value group-sym)
1170                (gnus-set-active group (symbol-value group-sym)))
1171              ;; Check whether we want it or not.
1172              (let ((do-sub (gnus-matches-options-n group)))
1173                (cond
1174                 ((eq do-sub 'subscribe)
1175                  (incf groups)
1176                  (gnus-sethash group group gnus-killed-hashtb)
1177                  (gnus-call-subscribe-functions
1178                   gnus-subscribe-options-newsgroup-method group))
1179                 ((eq do-sub 'ignore)
1180                  nil)
1181                 (t
1182                  (incf groups)
1183                  (gnus-sethash group group gnus-killed-hashtb)
1184                  (if gnus-subscribe-hierarchical-interactive
1185                      (push group new-newsgroups)
1186                    (gnus-call-subscribe-functions
1187                     gnus-subscribe-newsgroup-method group)))))))
1188          hashtb))
1189       (when new-newsgroups
1190         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
1191     (if (> groups 0)
1192         (gnus-message 5 "%d new newsgroup%s arrived"
1193                       groups (if (> groups 1) "s have" " has"))
1194       (gnus-message 5 "No new newsgroups"))
1195     (when got-new
1196       (setq gnus-newsrc-last-checked-date new-date))
1197     got-new))
1198
1199 (defun gnus-check-first-time-used ()
1200   (catch 'ended
1201     ;; First check if any of the following files exist.  If they do,
1202     ;; it's not the first time the user has used Gnus.
1203     (dolist (file (list (concat gnus-current-startup-file ".el")
1204                         (concat gnus-current-startup-file ".eld")
1205                         (concat gnus-startup-file ".el")
1206                         (concat gnus-startup-file ".eld")))
1207       (when (file-exists-p file)
1208         (throw 'ended nil)))
1209     (gnus-message 6 "First time user; subscribing you to default groups")
1210     (unless (gnus-read-active-file-p)
1211       (let ((gnus-read-active-file t))
1212         (gnus-read-active-file)))
1213     (setq gnus-newsrc-last-checked-date (message-make-date))
1214     ;; Subscribe to the default newsgroups.
1215     (let ((groups (or gnus-default-subscribed-newsgroups
1216                       gnus-backup-default-subscribed-newsgroups))
1217           group)
1218       (if (eq groups t)
1219           ;; If t, we subscribe (or not) all groups as if they were new.
1220           (mapatoms
1221            (lambda (sym)
1222              (when (setq group (symbol-name sym))
1223                (let ((do-sub (gnus-matches-options-n group)))
1224                  (cond
1225                   ((eq do-sub 'subscribe)
1226                    (gnus-sethash group group gnus-killed-hashtb)
1227                    (gnus-call-subscribe-functions
1228                     gnus-subscribe-options-newsgroup-method group))
1229                   ((eq do-sub 'ignore)
1230                    nil)
1231                   (t
1232                    (push group gnus-killed-list))))))
1233            gnus-active-hashtb)
1234         (dolist (group groups)
1235           ;; Only subscribe the default groups that are activated.
1236           (when (gnus-active group)
1237             (gnus-group-change-level
1238              group gnus-level-default-subscribed gnus-level-killed)))
1239         (save-excursion
1240           (set-buffer gnus-group-buffer)
1241           ;; Don't error if the group already exists. This happens when a
1242           ;; first-time user types 'F'. -- didier
1243           (gnus-group-make-help-group t))
1244         (when gnus-novice-user
1245           (gnus-message 7 "`A k' to list killed groups"))))))
1246
1247 (defun gnus-subscribe-group (group &optional previous method)
1248   "Subcribe GROUP and put it after PREVIOUS."
1249   (gnus-group-change-level
1250    (if method
1251        (list t group gnus-level-default-subscribed nil nil method)
1252      group)
1253    gnus-level-default-subscribed gnus-level-killed previous t)
1254   t)
1255
1256 ;; `gnus-group-change-level' is the fundamental function for changing
1257 ;; subscription levels of newsgroups.  This might mean just changing
1258 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1259 ;; again, which subscribes/unsubscribes a group, which is equally
1260 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1261 ;; from 8-9 to 1-7 means that you remove the group from the list of
1262 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1263 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1264 ;; which is trivial.
1265 ;; ENTRY can either be a string (newsgroup name) or a list (if
1266 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1267 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1268 ;; entries.
1269 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1270 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1271 ;; after.
1272 (defun gnus-group-change-level (entry level &optional oldlevel
1273                                       previous fromkilled)
1274   (let (group info active num)
1275     ;; Glean what info we can from the arguments
1276     (if (consp entry)
1277         (if fromkilled (setq group (nth 1 entry))
1278           (setq group (car (nth 2 entry))))
1279       (setq group entry))
1280     (when (and (stringp entry)
1281                oldlevel
1282                (< oldlevel gnus-level-zombie))
1283       (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
1284     (if (and (not oldlevel)
1285              (consp entry))
1286         (setq oldlevel (gnus-info-level (nth 2 entry)))
1287       (setq oldlevel (or oldlevel gnus-level-killed)))
1288     (when (stringp previous)
1289       (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
1290
1291     (if (and (>= oldlevel gnus-level-zombie)
1292              (gnus-gethash group gnus-newsrc-hashtb))
1293         ;; We are trying to subscribe a group that is already
1294         ;; subscribed.
1295         ()                              ; Do nothing.
1296
1297       (unless (gnus-ephemeral-group-p group)
1298         (gnus-dribble-enter
1299          (format "(gnus-group-change-level %S %S %S %S %S)"
1300                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1301
1302       ;; Then we remove the newgroup from any old structures, if needed.
1303       ;; If the group was killed, we remove it from the killed or zombie
1304       ;; list.  If not, and it is in fact going to be killed, we remove
1305       ;; it from the newsrc hash table and assoc.
1306       (cond
1307        ((>= oldlevel gnus-level-zombie)
1308         ;; oldlevel could be wrong.
1309         (setq gnus-zombie-list (delete group gnus-zombie-list))
1310         (setq gnus-killed-list (delete group gnus-killed-list)))
1311        (t
1312         (when (and (>= level gnus-level-zombie)
1313                    entry)
1314           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1315           (when (nth 3 entry)
1316             (setcdr (gnus-gethash (car (nth 3 entry))
1317                                   gnus-newsrc-hashtb)
1318                     (cdr entry)))
1319           (setcdr (cdr entry) (cdddr entry)))))
1320
1321       ;; Finally we enter (if needed) the list where it is supposed to
1322       ;; go, and change the subscription level.  If it is to be killed,
1323       ;; we enter it into the killed or zombie list.
1324       (cond
1325        ((>= level gnus-level-zombie)
1326         ;; Remove from the hash table.
1327         (gnus-sethash group nil gnus-newsrc-hashtb)
1328         ;; We do not enter foreign groups into the list of dead
1329         ;; groups.
1330         (unless (gnus-group-foreign-p group)
1331           (if (= level gnus-level-zombie)
1332               (push group gnus-zombie-list)
1333             (if (= oldlevel gnus-level-killed)
1334                 ;; Remove from active hashtb.
1335                 (unintern group gnus-active-hashtb)
1336               ;; Don't add it into killed-list if it was killed.
1337               (push group gnus-killed-list)))))
1338        (t
1339         ;; If the list is to be entered into the newsrc assoc, and
1340         ;; it was killed, we have to create an entry in the newsrc
1341         ;; hashtb format and fix the pointers in the newsrc assoc.
1342         (if (< oldlevel gnus-level-zombie)
1343             ;; It was alive, and it is going to stay alive, so we
1344             ;; just change the level and don't change any pointers or
1345             ;; hash table entries.
1346             (setcar (cdaddr entry) level)
1347           (if (listp entry)
1348               (setq info (cdr entry)
1349                     num (car entry))
1350             (setq active (gnus-active group))
1351             (setq num
1352                   (if active (- (1+ (cdr active)) (car active)) t))
1353             ;; Shorten the select method if possible, if we need to
1354             ;; store it at all (native groups).
1355             (let ((method (gnus-method-simplify
1356                            (or gnus-override-subscribe-method
1357                                (gnus-group-method group)))))
1358               (if method
1359                   (setq info (list group level nil nil method))
1360                 (setq info (list group level nil)))))
1361           (unless previous
1362             (setq previous
1363                   (let ((p gnus-newsrc-alist))
1364                     (while (cddr p)
1365                       (setq p (cdr p)))
1366                     p)))
1367           (setq entry (cons info (cddr previous)))
1368           (if (cdr previous)
1369               (progn
1370                 (setcdr (cdr previous) entry)
1371                 (gnus-sethash group (cons num (cdr previous))
1372                               gnus-newsrc-hashtb))
1373             (setcdr previous entry)
1374             (gnus-sethash group (cons num previous)
1375                           gnus-newsrc-hashtb))
1376           (when (cdr entry)
1377             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1378           (gnus-dribble-enter
1379            (format
1380             "(gnus-group-set-info '%S)" info)))))
1381       (when gnus-group-change-level-function
1382         (funcall gnus-group-change-level-function
1383                  group level oldlevel previous)))))
1384
1385 (defun gnus-kill-newsgroup (newsgroup)
1386   "Obsolete function.  Kills a newsgroup."
1387   (gnus-group-change-level
1388    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1389
1390 (defun gnus-check-bogus-newsgroups (&optional confirm)
1391   "Remove bogus newsgroups.
1392 If CONFIRM is non-nil, the user has to confirm the deletion of every
1393 newsgroup."
1394   (let ((newsrc (cdr gnus-newsrc-alist))
1395         bogus group entry info)
1396     (gnus-message 5 "Checking bogus newsgroups...")
1397     (unless (gnus-read-active-file-p)
1398       (gnus-read-active-file t))
1399     (when (gnus-read-active-file-p)
1400       ;; Find all bogus newsgroup that are subscribed.
1401       (while newsrc
1402         (setq info (pop newsrc)
1403               group (gnus-info-group info))
1404         (unless (or (gnus-active group) ; Active
1405                     (and (gnus-info-method info)
1406                          (not (gnus-secondary-method-p
1407                                (gnus-info-method info))))) ; Foreign
1408           ;; Found a bogus newsgroup.
1409           (push group bogus)))
1410       (if confirm
1411           (map-y-or-n-p
1412            "Remove bogus group %s? "
1413            (lambda (group)
1414              ;; Remove all bogus subscribed groups by first killing them, and
1415              ;; then removing them from the list of killed groups.
1416              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1417                (gnus-group-change-level entry gnus-level-killed)
1418                (setq gnus-killed-list (delete group gnus-killed-list))))
1419            bogus '("group" "groups" "remove"))
1420         (while (setq group (pop bogus))
1421           ;; Remove all bogus subscribed groups by first killing them, and
1422           ;; then removing them from the list of killed groups.
1423           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1424             (gnus-group-change-level entry gnus-level-killed)
1425             (setq gnus-killed-list (delete group gnus-killed-list)))))
1426       ;; Then we remove all bogus groups from the list of killed and
1427       ;; zombie groups.  They are removed without confirmation.
1428       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1429             killed)
1430         (while dead-lists
1431           (setq killed (symbol-value (car dead-lists)))
1432           (while killed
1433             (unless (gnus-active (setq group (pop killed)))
1434               ;; The group is bogus.
1435               ;; !!!Slow as hell.
1436               (set (car dead-lists)
1437                    (delete group (symbol-value (car dead-lists))))))
1438           (setq dead-lists (cdr dead-lists))))
1439       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1440       (gnus-message 5 "Checking bogus newsgroups...done"))))
1441
1442 (defun gnus-check-duplicate-killed-groups ()
1443   "Remove duplicates from the list of killed groups."
1444   (interactive)
1445   (let ((killed gnus-killed-list))
1446     (while killed
1447       (gnus-message 9 "%d" (length killed))
1448       (setcdr killed (delete (car killed) (cdr killed)))
1449       (setq killed (cdr killed)))))
1450
1451 ;; We want to inline a function from gnus-cache, so we cheat here:
1452 (eval-when-compile
1453   (defvar gnus-cache-active-hashtb)
1454   (defun gnus-cache-possibly-alter-active (group active)
1455     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1456     (when gnus-cache-active-hashtb
1457       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1458         (when cache-active
1459           (when (< (car cache-active) (car active))
1460             (setcar active (car cache-active)))
1461           (when (> (cdr cache-active) (cdr active))
1462             (setcdr active (cdr cache-active))))))))
1463
1464 (defun gnus-activate-group (group &optional scan dont-check method)
1465   ;; Check whether a group has been activated or not.
1466   ;; If SCAN, request a scan of that group as well.
1467   (let ((method (or method (inline (gnus-find-method-for-group group))))
1468         active)
1469     (and (inline (gnus-check-server method))
1470          ;; We escape all bugs and quit here to make it possible to
1471          ;; continue if a group is so out-there that it reports bugs
1472          ;; and stuff.
1473          (progn
1474            (and scan
1475                 (gnus-check-backend-function 'request-scan (car method))
1476                 (gnus-request-scan group method))
1477            t)
1478          (if (or debug-on-error debug-on-quit)
1479              (inline (gnus-request-group group dont-check method))
1480            (condition-case ()
1481                (inline (gnus-request-group group dont-check method))
1482              ;;(error nil)
1483              (quit
1484               (message "Quit activating %s" group)
1485               nil)))
1486          (unless dont-check
1487            (setq active (gnus-parse-active))
1488            ;; If there are no articles in the group, the GROUP
1489            ;; command may have responded with the `(0 . 0)'.  We
1490            ;; ignore this if we already have an active entry
1491            ;; for the group.
1492            (if (and (zerop (car active))
1493                     (zerop (cdr active))
1494                     (gnus-active group))
1495                (gnus-active group)
1496              (gnus-set-active group active)
1497              ;; Return the new active info.
1498              active)))))
1499
1500 (defun gnus-get-unread-articles-in-group (info active &optional update)
1501   (when active
1502     ;; Allow the backend to update the info in the group.
1503     (when (and update
1504                (gnus-request-update-info
1505                 info (inline (gnus-find-method-for-group
1506                               (gnus-info-group info)))))
1507       (gnus-activate-group (gnus-info-group info) nil t))
1508
1509     (let* ((range (gnus-info-read info))
1510            (num 0))
1511       ;; If a cache is present, we may have to alter the active info.
1512       (when (and gnus-use-cache info)
1513         (inline (gnus-cache-possibly-alter-active
1514                  (gnus-info-group info) active)))
1515       ;; Modify the list of read articles according to what articles
1516       ;; are available; then tally the unread articles and add the
1517       ;; number to the group hash table entry.
1518       (cond
1519        ((zerop (cdr active))
1520         (setq num 0))
1521        ((not range)
1522         (setq num (- (1+ (cdr active)) (car active))))
1523        ((not (listp (cdr range)))
1524         ;; Fix a single (num . num) range according to the
1525         ;; active hash table.
1526         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1527         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1528         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1529         ;; Compute number of unread articles.
1530         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1531        (t
1532         ;; The read list is a list of ranges.  Fix them according to
1533         ;; the active hash table.
1534         ;; First peel off any elements that are below the lower
1535         ;; active limit.
1536         (while (and (cdr range)
1537                     (>= (car active)
1538                         (or (and (atom (cadr range)) (cadr range))
1539                             (caadr range))))
1540           (if (numberp (car range))
1541               (setcar range
1542                       (cons (car range)
1543                             (or (and (numberp (cadr range))
1544                                      (cadr range))
1545                                 (cdadr range))))
1546             (setcdr (car range)
1547                     (or (and (numberp (nth 1 range)) (nth 1 range))
1548                         (cdadr range))))
1549           (setcdr range (cddr range)))
1550         ;; Adjust the first element to be the same as the lower limit.
1551         (when (and (not (atom (car range)))
1552                    (< (cdar range) (car active)))
1553           (setcdr (car range) (1- (car active))))
1554         ;; Then we want to peel off any elements that are higher
1555         ;; than the upper active limit.
1556         (let ((srange range))
1557           ;; Go past all valid elements.
1558           (while (and (cdr srange)
1559                       (<= (or (and (atom (cadr srange))
1560                                    (cadr srange))
1561                               (caadr srange))
1562                           (cdr active)))
1563             (setq srange (cdr srange)))
1564           (when (cdr srange)
1565             ;; Nuke all remaining invalid elements.
1566             (setcdr srange nil))
1567
1568           ;; Adjust the final element.
1569           (when (and (not (atom (car srange)))
1570                      (> (cdar srange) (cdr active)))
1571             (setcdr (car srange) (cdr active))))
1572         ;; Compute the number of unread articles.
1573         (while range
1574           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1575                                       (cdar range)))
1576                               (or (and (atom (car range)) (car range))
1577                                   (caar range)))))
1578           (setq range (cdr range)))
1579         (setq num (max 0 (- (cdr active) num)))))
1580       ;; Set the number of unread articles.
1581       (when info
1582         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1583       num)))
1584
1585 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1586 ;; and compute how many unread articles there are in each group.
1587 (defun gnus-get-unread-articles (&optional level)
1588   (setq gnus-server-method-cache nil)
1589   (let* ((newsrc (cdr gnus-newsrc-alist))
1590          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1591          (foreign-level
1592           (min
1593            (cond ((and gnus-activate-foreign-newsgroups
1594                        (not (numberp gnus-activate-foreign-newsgroups)))
1595                   (1+ gnus-level-subscribed))
1596                  ((numberp gnus-activate-foreign-newsgroups)
1597                   gnus-activate-foreign-newsgroups)
1598                  (t 0))
1599            level))
1600          scanned-methods info group active method retrieve-groups)
1601     (gnus-message 6 "Checking new news...")
1602
1603     (while newsrc
1604       (setq active (gnus-active (setq group (gnus-info-group
1605                                              (setq info (pop newsrc))))))
1606
1607       ;; Check newsgroups.  If the user doesn't want to check them, or
1608       ;; they can't be checked (for instance, if the news server can't
1609       ;; be reached) we just set the number of unread articles in this
1610       ;; newsgroup to t.  This means that Gnus thinks that there are
1611       ;; unread articles, but it has no idea how many.
1612
1613       ;; To be more explicit:
1614       ;; >0 for an active group with messages
1615       ;; 0 for an active group with no unread messages
1616       ;; nil for non-foreign groups that the user has requested not be checked
1617       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1618       ;;   be checked, for one reason or other.
1619       (if (and (setq method (gnus-info-method info))
1620                (not (inline
1621                       (gnus-server-equal
1622                        gnus-select-method
1623                        (setq method (gnus-server-get-method nil method)))))
1624                (not (gnus-secondary-method-p method)))
1625           ;; These groups are foreign.  Check the level.
1626           (when (and (<= (gnus-info-level info) foreign-level)
1627                      (setq active (gnus-activate-group group 'scan)))
1628             ;; Let the Gnus agent save the active file.
1629             (when (and gnus-agent active (gnus-online method))
1630               (gnus-agent-save-group-info
1631                method (gnus-group-real-name group) active))
1632             (unless (inline (gnus-virtual-group-p group))
1633               (inline (gnus-close-group group)))
1634             (when (fboundp (intern (concat (symbol-name (car method))
1635                                            "-request-update-info")))
1636               (inline (gnus-request-update-info info method))))
1637         ;; These groups are native or secondary.
1638         (cond
1639          ;; We don't want these groups.
1640          ((> (gnus-info-level info) level)
1641           (setq active 'ignore))
1642          ;; Activate groups.
1643          ((not gnus-read-active-file)
1644           (if (gnus-check-backend-function 'retrieve-groups group)
1645               ;; if server support gnus-retrieve-groups we push
1646               ;; the group onto retrievegroups for later checking
1647               (if (assoc method retrieve-groups)
1648                   (setcdr (assoc method retrieve-groups)
1649                           (cons group (cdr (assoc method retrieve-groups))))
1650                 (push (list method group) retrieve-groups))
1651             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1652             ;; on the group, so we must perform a scan for every group
1653             ;; if the users has any directory mail sources.
1654             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1655             ;; for it scan all spool files even when the groups are
1656             ;; not required.
1657             (if (and
1658                  (or nnmail-scan-directory-mail-source-once
1659                      (null (assq 'directory
1660                                  (or mail-sources
1661                                      (if (listp nnmail-spool-file)
1662                                          nnmail-spool-file
1663                                        (list nnmail-spool-file))))))
1664                  (member method scanned-methods))
1665                 (setq active (gnus-activate-group group))
1666               (setq active (gnus-activate-group group 'scan))
1667               (push method scanned-methods))
1668             (when active
1669               (gnus-close-group group))))))
1670
1671       ;; Get the number of unread articles in the group.
1672       (cond
1673        ((eq active 'ignore)
1674         ;; Don't do anything.
1675         )
1676        (active
1677         (inline (gnus-get-unread-articles-in-group info active t)))
1678        (t
1679         ;; The group couldn't be reached, so we nix out the number of
1680         ;; unread articles and stuff.
1681         (gnus-set-active group nil)
1682         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1683           (when tmp
1684             (setcar tmp t))))))
1685
1686     ;; iterate through groups on methods which support gnus-retrieve-groups
1687     ;; and fetch a partial active file and use it to find new news.
1688     (dolist (rg retrieve-groups)
1689       (let ((method (or (car rg) gnus-select-method))
1690             (groups (cdr rg)))
1691         (when (gnus-check-server method)
1692           ;; Request that the backend scan its incoming messages.
1693           (when (gnus-check-backend-function 'request-scan (car method))
1694             (gnus-request-scan nil method))
1695           (gnus-read-active-file-2
1696            (mapcar (lambda (group) (gnus-group-real-name group)) groups)
1697            method)
1698           (dolist (group groups)
1699             (cond
1700              ((setq active (gnus-active (gnus-info-group
1701                                          (setq info (gnus-get-info group)))))
1702               (inline (gnus-get-unread-articles-in-group info active t)))
1703              (t
1704               ;; The group couldn't be reached, so we nix out the number of
1705               ;; unread articles and stuff.
1706               (gnus-set-active group nil)
1707               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1708
1709     (gnus-message 6 "Checking new news...done")))
1710
1711 ;; Create a hash table out of the newsrc alist.  The `car's of the
1712 ;; alist elements are used as keys.
1713 (defun gnus-make-hashtable-from-newsrc-alist ()
1714   (let ((alist gnus-newsrc-alist)
1715         (ohashtb gnus-newsrc-hashtb)
1716         prev)
1717     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1718     (setq alist
1719           (setq prev (setq gnus-newsrc-alist
1720                            (if (equal (caar gnus-newsrc-alist)
1721                                       "dummy.group")
1722                                gnus-newsrc-alist
1723                              (cons (list "dummy.group" 0 nil) alist)))))
1724     (while alist
1725       (gnus-sethash
1726        (caar alist)
1727        ;; Preserve number of unread articles in groups.
1728        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1729              prev)
1730        gnus-newsrc-hashtb)
1731       (setq prev alist
1732             alist (cdr alist)))))
1733
1734 (defun gnus-make-hashtable-from-killed ()
1735   "Create a hash table from the killed and zombie lists."
1736   (let ((lists '(gnus-killed-list gnus-zombie-list))
1737         list)
1738     (setq gnus-killed-hashtb
1739           (gnus-make-hashtable
1740            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1741     (while lists
1742       (setq list (symbol-value (pop lists)))
1743       (while list
1744         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1745
1746 (defun gnus-parse-active ()
1747   "Parse active info in the nntp server buffer."
1748   (save-excursion
1749     (set-buffer nntp-server-buffer)
1750     (goto-char (point-min))
1751     ;; Parse the result we got from `gnus-request-group'.
1752     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1753       (goto-char (match-beginning 1))
1754       (cons (read (current-buffer))
1755             (read (current-buffer))))))
1756
1757 (defun gnus-make-articles-unread (group articles)
1758   "Mark ARTICLES in GROUP as unread."
1759   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1760                           (gnus-gethash (gnus-group-real-name group)
1761                                         gnus-newsrc-hashtb))))
1762          (ranges (gnus-info-read info))
1763          news article)
1764     (while articles
1765       (when (gnus-member-of-range
1766              (setq article (pop articles)) ranges)
1767         (push article news)))
1768     (when news
1769       ;; Enter this list into the group info.
1770       (gnus-info-set-read
1771        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1772
1773       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1774       (gnus-get-unread-articles-in-group info (gnus-active group))
1775
1776       ;; Insert the change into the group buffer and the dribble file.
1777       (gnus-group-update-group group t))))
1778
1779 (defun gnus-make-ascending-articles-unread (group articles)
1780   "Mark ascending ARTICLES in GROUP as unread."
1781   (let* ((entry (or (gnus-gethash group gnus-newsrc-hashtb)
1782                     (gnus-gethash (gnus-group-real-name group)
1783                                   gnus-newsrc-hashtb)))
1784          (info (nth 2 entry))
1785          (ranges (gnus-info-read info))
1786          (r ranges)
1787          modified)
1788
1789     (while articles
1790       (let ((article (pop articles))) ; get the next article to remove from ranges
1791         (while (let ((range (car ranges))) ; note the current range
1792                  (if (atom range)       ; single value range
1793                      (cond ((not range)
1794                             ;; the articles extend past the end of the ranges
1795                             ;; OK - I'm done
1796                             (setq articles nil))
1797                            ((< range article)
1798                             ;; this range preceeds the article. Leave the range unmodified.
1799                             (pop ranges)
1800                             ranges)
1801                            ((= range article)
1802                             ;; this range exactly matches the article; REMOVE THE RANGE.
1803                             ;; NOTE: When the range being removed is the last range, the list is corrupted by inserting null at its end.
1804                             (setcar ranges (cadr ranges))
1805                             (setcdr ranges (cddr ranges))
1806                             (setq modified (if (car ranges) t 'remove-null))
1807                             nil))
1808                    (let ((min (car range))
1809                          (max (cdr range)))
1810                      ;; I have a min/max range to consider
1811                      (cond ((> min max) ; invalid range introduced by splitter
1812                             (setcar ranges (cadr ranges))
1813                             (setcdr ranges (cddr ranges))
1814                             (setq modified (if (car ranges) t 'remove-null))
1815                             ranges)
1816                            ((= min max)
1817                             ;; replace min/max range with a single-value range
1818                             (setcar ranges min)
1819                             ranges)
1820                            ((< max article)
1821                             ;; this range preceeds the article. Leave the range unmodified.
1822                             (pop ranges)
1823                             ranges)
1824                            ((< article min)
1825                             ;; this article preceeds the range.  Return null to move to the
1826                             ;; next article
1827                             nil)
1828                            (t
1829                             ;; this article splits the range into two parts
1830                             (setcdr ranges (cons (cons (1+ article) max) (cdr ranges)))
1831                             (setcdr range (1- article))
1832                             (setq modified t)
1833                             ranges))))))))
1834                   
1835     (when modified
1836       (when (eq modified 'remove-null)
1837         (setq r (delq nil r)))
1838       ;; Enter this list into the group info.
1839       (gnus-info-set-read info r)
1840
1841       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1842       (gnus-get-unread-articles-in-group info (gnus-active group))
1843
1844       ;; Insert the change into the group buffer and the dribble file.
1845       (gnus-group-update-group group t))))
1846
1847 ;; Enter all dead groups into the hashtb.
1848 (defun gnus-update-active-hashtb-from-killed ()
1849   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1850         (lists (list gnus-killed-list gnus-zombie-list))
1851         killed)
1852     (while lists
1853       (setq killed (car lists))
1854       (while killed
1855         (gnus-sethash (car killed) nil hashtb)
1856         (setq killed (cdr killed)))
1857       (setq lists (cdr lists)))))
1858
1859 (defun gnus-get-killed-groups ()
1860   "Go through the active hashtb and mark all unknown groups as killed."
1861   ;; First make sure active file has been read.
1862   (unless (gnus-read-active-file-p)
1863     (let ((gnus-read-active-file t))
1864       (gnus-read-active-file)))
1865   (unless gnus-killed-hashtb
1866     (gnus-make-hashtable-from-killed))
1867   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1868   (mapatoms
1869    (lambda (sym)
1870      (let ((groups 0)
1871            (group (symbol-name sym)))
1872        (if (or (null group)
1873                (gnus-gethash group gnus-killed-hashtb)
1874                (gnus-gethash group gnus-newsrc-hashtb))
1875            ()
1876          (let ((do-sub (gnus-matches-options-n group)))
1877            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1878                ()
1879              (setq groups (1+ groups))
1880              (push group gnus-killed-list)
1881              (gnus-sethash group group gnus-killed-hashtb))))))
1882    gnus-active-hashtb)
1883   (gnus-dribble-touch))
1884
1885 ;; Get the active file(s) from the backend(s).
1886 (defun gnus-read-active-file (&optional force not-native)
1887   (gnus-group-set-mode-line)
1888   (let ((methods
1889          (mapcar
1890           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1891           (append
1892            (if (and (not not-native)
1893                     (gnus-check-server gnus-select-method))
1894                ;; The native server is available.
1895                (cons gnus-select-method gnus-secondary-select-methods)
1896              ;; The native server is down, so we just do the
1897              ;; secondary ones.
1898              gnus-secondary-select-methods)
1899            ;; Also read from the archive server.
1900            (when (gnus-archive-server-wanted-p)
1901              (list "archive")))))
1902         method)
1903     (setq gnus-have-read-active-file nil)
1904     (save-excursion
1905       (set-buffer nntp-server-buffer)
1906       (while (setq method (pop methods))
1907         ;; Only do each method once, in case the methods appear more
1908         ;; than once in this list.
1909         (unless (member method methods)
1910           (if (or debug-on-error debug-on-quit)
1911               (gnus-read-active-file-1 method force)
1912             (condition-case ()
1913                 (gnus-read-active-file-1 method force)
1914               ;; We catch C-g so that we can continue past servers
1915               ;; that do not respond.
1916               (quit
1917                (message "Quit reading the active file")
1918                nil))))))))
1919
1920 (defun gnus-read-active-file-1 (method force)
1921   (let (where mesg)
1922     (setq where (nth 1 method)
1923           mesg (format "Reading active file%s via %s..."
1924                        (if (and where (not (zerop (length where))))
1925                            (concat " from " where) "")
1926                        (car method)))
1927     (gnus-message 5 mesg)
1928     (when (gnus-check-server method)
1929       ;; Request that the backend scan its incoming messages.
1930       (when (gnus-check-backend-function 'request-scan (car method))
1931         (gnus-request-scan nil method))
1932       (cond
1933        ((and (eq gnus-read-active-file 'some)
1934              (gnus-check-backend-function 'retrieve-groups (car method))
1935              (not force))
1936         (let ((newsrc (cdr gnus-newsrc-alist))
1937               (gmethod (gnus-server-get-method nil method))
1938               groups info)
1939           (while (setq info (pop newsrc))
1940             (when (inline
1941                     (gnus-server-equal
1942                      (inline
1943                        (gnus-find-method-for-group
1944                         (gnus-info-group info) info))
1945                      gmethod))
1946               (push (gnus-group-real-name (gnus-info-group info))
1947                     groups)))
1948           (gnus-read-active-file-2 groups method)))
1949        ((null method)
1950         t)
1951        (t
1952         (if (not (gnus-request-list method))
1953             (unless (equal method gnus-message-archive-method)
1954               (gnus-error 1 "Cannot read active file from %s server"
1955                           (car method)))
1956           (gnus-message 5 mesg)
1957           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1958           ;; We mark this active file as read.
1959           (push method gnus-have-read-active-file)
1960           (gnus-message 5 "%sdone" mesg)))))))
1961
1962 (defun gnus-read-active-file-2 (groups method)
1963   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1964   (when groups
1965     (save-excursion
1966       (set-buffer nntp-server-buffer)
1967       (gnus-check-server method)
1968       (let ((list-type (gnus-retrieve-groups groups method)))
1969         (cond ((not list-type)
1970                (gnus-error
1971                 1.2 "Cannot read partial active file from %s server."
1972                 (car method)))
1973               ((eq list-type 'active)
1974                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1975               (t
1976                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
1977
1978 ;; Read an active file and place the results in `gnus-active-hashtb'.
1979 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1980                                              real-active)
1981   (unless method
1982     (setq method gnus-select-method))
1983   (let ((cur (current-buffer))
1984         (hashtb (or hashtb
1985                     (if (and gnus-active-hashtb
1986                              (not (equal method gnus-select-method)))
1987                         gnus-active-hashtb
1988                       (setq gnus-active-hashtb
1989                             (if (equal method gnus-select-method)
1990                                 (gnus-make-hashtable
1991                                  (count-lines (point-min) (point-max)))
1992                               (gnus-make-hashtable 4096)))))))
1993     ;; Delete unnecessary lines.
1994     (goto-char (point-min))
1995     (cond
1996      ((string= gnus-ignored-newsgroups "")
1997       (delete-matching-lines "^to\\."))
1998      (t
1999       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
2000
2001     (goto-char (point-min))
2002     (unless (re-search-forward "[\\\"]" nil t)
2003       ;; Make the group names readable as a lisp expression even if they
2004       ;; contain special characters.
2005       (goto-char (point-max))
2006       (while (re-search-backward "[][';?()#]" nil t)
2007         (insert ?\\)))
2008
2009     ;; Let the Gnus agent save the active file.
2010     (when (and gnus-agent real-active (gnus-online method))
2011       (gnus-agent-save-active method))
2012
2013     ;; If these are groups from a foreign select method, we insert the
2014     ;; group prefix in front of the group names.
2015     (when (not (gnus-server-equal
2016                 (gnus-server-get-method nil method)
2017                 (gnus-server-get-method nil gnus-select-method)))
2018       (let ((prefix (gnus-group-prefixed-name "" method)))
2019         (goto-char (point-min))
2020         (while (and (not (eobp))
2021                     (progn
2022                       (when (= (following-char) ?\")
2023                         (forward-char 1))
2024                       (insert prefix)
2025                       (zerop (forward-line 1)))))))
2026     ;; Store the active file in a hash table.
2027     (goto-char (point-min))
2028     (let (group max min)
2029       (while (not (eobp))
2030         (condition-case ()
2031             (progn
2032               (narrow-to-region (point) (gnus-point-at-eol))
2033               ;; group gets set to a symbol interned in the hash table
2034               ;; (what a hack!!) - jwz
2035               (setq group (let ((obarray hashtb)) (read cur)))
2036               ;; ### The extended group name scheme makes
2037               ;; the previous optimization strategy sort of pointless...
2038               (when (stringp group)
2039                 (setq group (intern group hashtb)))
2040               (if (and (numberp (setq max (read cur)))
2041                        (numberp (setq min (read cur)))
2042                        (progn
2043                          (skip-chars-forward " \t")
2044                          (not
2045                           (or (eq (char-after) ?=)
2046                               (eq (char-after) ?x)
2047                               (eq (char-after) ?j)))))
2048                   (progn
2049                     (set group (cons min max))
2050                     ;; if group is moderated, stick in moderation table
2051                     (when (eq (char-after) ?m)
2052                       (unless gnus-moderated-hashtb
2053                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
2054                       (gnus-sethash (symbol-name group) t
2055                                     gnus-moderated-hashtb)))
2056                 (set group nil)))
2057           (error
2058            (and group
2059                 (symbolp group)
2060                 (set group nil))
2061            (unless ignore-errors
2062              (gnus-message 3 "Warning - invalid active: %s"
2063                            (buffer-substring
2064                             (gnus-point-at-bol) (gnus-point-at-eol))))))
2065         (widen)
2066         (forward-line 1)))))
2067
2068 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
2069   ;; Parse a "groups" active file.
2070   (let ((cur (current-buffer))
2071         (hashtb (or hashtb
2072                     (if (and method gnus-active-hashtb)
2073                         gnus-active-hashtb
2074                       (setq gnus-active-hashtb
2075                             (gnus-make-hashtable
2076                              (count-lines (point-min) (point-max)))))))
2077         (prefix (and method
2078                      (not (gnus-server-equal
2079                            (gnus-server-get-method nil method)
2080                            (gnus-server-get-method nil gnus-select-method)))
2081                      (gnus-group-prefixed-name "" method))))
2082
2083     ;; Let the Gnus agent save the active file.
2084     (if (and gnus-agent
2085              real-active
2086              (gnus-online method)
2087              (gnus-agent-method-p method))
2088         (progn
2089           (gnus-agent-save-groups method)
2090           (gnus-active-to-gnus-format method hashtb nil real-active))
2091
2092       (goto-char (point-min))
2093       ;; We split this into to separate loops, one with the prefix
2094       ;; and one without to speed the reading up somewhat.
2095       (if prefix
2096           (let (min max opoint group)
2097             (while (not (eobp))
2098               (condition-case ()
2099                   (progn
2100                     (read cur) (read cur)
2101                     (setq min (read cur)
2102                           max (read cur)
2103                           opoint (point))
2104                     (skip-chars-forward " \t")
2105                     (insert prefix)
2106                     (goto-char opoint)
2107                     (set (let ((obarray hashtb)) (read cur))
2108                          (cons min max)))
2109                 (error (and group (symbolp group) (set group nil))))
2110               (forward-line 1)))
2111         (let (min max group)
2112           (while (not (eobp))
2113             (condition-case ()
2114                 (when (eq (char-after) ?2)
2115                   (read cur) (read cur)
2116                   (setq min (read cur)
2117                         max (read cur))
2118                   (set (setq group (let ((obarray hashtb)) (read cur)))
2119                        (cons min max)))
2120               (error (and group (symbolp group) (set group nil))))
2121             (forward-line 1)))))))
2122
2123 (defun gnus-read-newsrc-file (&optional force)
2124   "Read startup file.
2125 If FORCE is non-nil, the .newsrc file is read."
2126   ;; Reset variables that might be defined in the .newsrc.eld file.
2127   (gnus-clear-quick-file-variables)
2128   (let* ((newsrc-file gnus-current-startup-file)
2129          (quick-file (concat newsrc-file ".el")))
2130     (save-excursion
2131       ;; We always load the .newsrc.eld file.  If always contains
2132       ;; much information that can not be gotten from the .newsrc
2133       ;; file (ticked articles, killed groups, foreign methods, etc.)
2134       (gnus-read-newsrc-el-file quick-file)
2135
2136       (when (and gnus-read-newsrc-file
2137                  (file-exists-p gnus-current-startup-file)
2138                  (or force
2139                      (and (file-newer-than-file-p newsrc-file quick-file)
2140                           (file-newer-than-file-p newsrc-file
2141                                                   (concat quick-file "d")))
2142                      (not gnus-newsrc-alist)))
2143         ;; We read the .newsrc file.  Note that if there if a
2144         ;; .newsrc.eld file exists, it has already been read, and
2145         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2146         ;; the .newsrc file, Gnus will only use the information it
2147         ;; can find there for changing the data already read -
2148         ;; i. e., reading the .newsrc file will not trash the data
2149         ;; already read (except for read articles).
2150         (save-excursion
2151           (gnus-message 5 "Reading %s..." newsrc-file)
2152           (set-buffer (nnheader-find-file-noselect newsrc-file))
2153           (buffer-disable-undo)
2154           (gnus-newsrc-to-gnus-format)
2155           (kill-buffer (current-buffer))
2156           (gnus-message 5 "Reading %s...done" newsrc-file))))))
2157
2158 (defun gnus-load (file &optional coding-system)
2159   "Load FILE, but in such a way that read errors can be reported."
2160   (with-temp-buffer
2161     (if coding-system
2162         (insert-file-contents-as-coding-system coding-system file)
2163       (insert-file-contents file))
2164     (while (not (eobp))
2165       (condition-case type
2166           (let ((form (read (current-buffer))))
2167             (eval form))
2168         (error
2169          (unless (eq (car type) 'end-of-file)
2170            (let ((error (format "Error in %s line %d" file
2171                                 (count-lines (point-min) (point)))))
2172              (ding)
2173              (unless (gnus-yes-or-no-p (concat error "; continue? "))
2174                (error "%s" error)))))))))
2175
2176 (defun gnus-read-newsrc-el-file (file)
2177   (let ((ding-file (concat file "d")))
2178     (when (file-exists-p ding-file)
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           (gnus-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             (gnus-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