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