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