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