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