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