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