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