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