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