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