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