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