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