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