* lisp/gnus-msg.el (gnus-inews-yank-articles): Make it to work with multiple
[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 (current-time-string))
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* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
1109          (methods (cons gnus-select-method
1110                         (nconc
1111                          (when (gnus-archive-server-wanted-p)
1112                            (list "archive"))
1113                          (append
1114                           (and (consp gnus-check-new-newsgroups)
1115                                gnus-check-new-newsgroups)
1116                           gnus-secondary-select-methods))))
1117          (groups 0)
1118          (new-date (current-time-string))
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 (current-time-string))
1195     ;; Subscribe to the default newsgroups.
1196     (let ((groups (or gnus-default-subscribed-newsgroups
1197                       gnus-backup-default-subscribed-newsgroups))
1198           group)
1199       (when (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                     (gnus-info-method info)) ; Foreign
1385           ;; Found a bogus newsgroup.
1386           (push group bogus)))
1387       (if confirm
1388           (map-y-or-n-p
1389            "Remove bogus group %s? "
1390            (lambda (group)
1391              ;; Remove all bogus subscribed groups by first killing them, and
1392              ;; then removing them from the list of killed groups.
1393              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1394                (gnus-group-change-level entry gnus-level-killed)
1395                (setq gnus-killed-list (delete group gnus-killed-list))))
1396            bogus '("group" "groups" "remove"))
1397         (while (setq group (pop bogus))
1398           ;; Remove all bogus subscribed groups by first killing them, and
1399           ;; then removing them from the list of killed groups.
1400           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1401             (gnus-group-change-level entry gnus-level-killed)
1402             (setq gnus-killed-list (delete group gnus-killed-list)))))
1403       ;; Then we remove all bogus groups from the list of killed and
1404       ;; zombie groups.  They are removed without confirmation.
1405       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1406             killed)
1407         (while dead-lists
1408           (setq killed (symbol-value (car dead-lists)))
1409           (while killed
1410             (unless (gnus-active (setq group (pop killed)))
1411               ;; The group is bogus.
1412               ;; !!!Slow as hell.
1413               (set (car dead-lists)
1414                    (delete group (symbol-value (car dead-lists))))))
1415           (setq dead-lists (cdr dead-lists))))
1416       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1417       (gnus-message 5 "Checking bogus newsgroups...done"))))
1418
1419 (defun gnus-check-duplicate-killed-groups ()
1420   "Remove duplicates from the list of killed groups."
1421   (interactive)
1422   (let ((killed gnus-killed-list))
1423     (while killed
1424       (gnus-message 9 "%d" (length killed))
1425       (setcdr killed (delete (car killed) (cdr killed)))
1426       (setq killed (cdr killed)))))
1427
1428 ;; We want to inline a function from gnus-cache, so we cheat here:
1429 (eval-when-compile
1430   (defvar gnus-cache-active-hashtb)
1431   (defun gnus-cache-possibly-alter-active (group active)
1432     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1433     (when gnus-cache-active-hashtb
1434       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1435         (when cache-active
1436           (when (< (car cache-active) (car active))
1437             (setcar active (car cache-active)))
1438           (when (> (cdr cache-active) (cdr active))
1439             (setcdr active (cdr cache-active))))))))
1440
1441 (defun gnus-activate-group (group &optional scan dont-check method)
1442   ;; Check whether a group has been activated or not.
1443   ;; If SCAN, request a scan of that group as well.
1444   (let ((method (or method (inline (gnus-find-method-for-group group))))
1445         active)
1446     (and (inline (gnus-check-server method))
1447          ;; We escape all bugs and quit here to make it possible to
1448          ;; continue if a group is so out-there that it reports bugs
1449          ;; and stuff.
1450          (progn
1451            (and scan
1452                 (gnus-check-backend-function 'request-scan (car method))
1453                 (gnus-request-scan group method))
1454            t)
1455          (condition-case ()
1456              (inline (gnus-request-group group dont-check method))
1457            ;;(error nil)
1458            (quit
1459             (message "Quit activating %s" group)
1460             nil))
1461          (setq active (gnus-parse-active))
1462          ;; If there are no articles in the group, the GROUP
1463          ;; command may have responded with the `(0 . 0)'.  We
1464          ;; ignore this if we already have an active entry
1465          ;; for the group.
1466          (if (and (zerop (car active))
1467                   (zerop (cdr active))
1468                   (gnus-active group))
1469              (gnus-active group)
1470            (gnus-set-active group active)
1471            ;; Return the new active info.
1472            active))))
1473
1474 (defun gnus-get-unread-articles-in-group (info active &optional update)
1475   (when active
1476     ;; Allow the backend to update the info in the group.
1477     (when (and update
1478                (gnus-request-update-info
1479                 info (inline (gnus-find-method-for-group
1480                               (gnus-info-group info)))))
1481       (gnus-activate-group (gnus-info-group info) nil t))
1482
1483     (let* ((range (gnus-info-read info))
1484            (num 0))
1485       ;; If a cache is present, we may have to alter the active info.
1486       (when (and gnus-use-cache info)
1487         (inline (gnus-cache-possibly-alter-active
1488                  (gnus-info-group info) active)))
1489       ;; Modify the list of read articles according to what articles
1490       ;; are available; then tally the unread articles and add the
1491       ;; number to the group hash table entry.
1492       (cond
1493        ((zerop (cdr active))
1494         (setq num 0))
1495        ((not range)
1496         (setq num (- (1+ (cdr active)) (car active))))
1497        ((not (listp (cdr range)))
1498         ;; Fix a single (num . num) range according to the
1499         ;; active hash table.
1500         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1501         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1502         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1503         ;; Compute number of unread articles.
1504         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1505        (t
1506         ;; The read list is a list of ranges.  Fix them according to
1507         ;; the active hash table.
1508         ;; First peel off any elements that are below the lower
1509         ;; active limit.
1510         (while (and (cdr range)
1511                     (>= (car active)
1512                         (or (and (atom (cadr range)) (cadr range))
1513                             (caadr range))))
1514           (if (numberp (car range))
1515               (setcar range
1516                       (cons (car range)
1517                             (or (and (numberp (cadr range))
1518                                      (cadr range))
1519                                 (cdadr range))))
1520             (setcdr (car range)
1521                     (or (and (numberp (nth 1 range)) (nth 1 range))
1522                         (cdadr range))))
1523           (setcdr range (cddr range)))
1524         ;; Adjust the first element to be the same as the lower limit.
1525         (when (and (not (atom (car range)))
1526                    (< (cdar range) (car active)))
1527           (setcdr (car range) (1- (car active))))
1528         ;; Then we want to peel off any elements that are higher
1529         ;; than the upper active limit.
1530         (let ((srange range))
1531           ;; Go past all valid elements.
1532           (while (and (cdr srange)
1533                       (<= (or (and (atom (cadr srange))
1534                                    (cadr srange))
1535                               (caadr srange))
1536                           (cdr active)))
1537             (setq srange (cdr srange)))
1538           (when (cdr srange)
1539             ;; Nuke all remaining invalid elements.
1540             (setcdr srange nil))
1541
1542           ;; Adjust the final element.
1543           (when (and (not (atom (car srange)))
1544                      (> (cdar srange) (cdr active)))
1545             (setcdr (car srange) (cdr active))))
1546         ;; Compute the number of unread articles.
1547         (while range
1548           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1549                                       (cdar range)))
1550                               (or (and (atom (car range)) (car range))
1551                                   (caar range)))))
1552           (setq range (cdr range)))
1553         (setq num (max 0 (- (cdr active) num)))))
1554       ;; Set the number of unread articles.
1555       (when info
1556         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1557       num)))
1558
1559 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1560 ;; and compute how many unread articles there are in each group.
1561 (defun gnus-get-unread-articles (&optional level)
1562   (let* ((newsrc (cdr gnus-newsrc-alist))
1563          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1564          (foreign-level
1565           (min
1566            (cond ((and gnus-activate-foreign-newsgroups
1567                        (not (numberp gnus-activate-foreign-newsgroups)))
1568                   (1+ gnus-level-subscribed))
1569                  ((numberp gnus-activate-foreign-newsgroups)
1570                   gnus-activate-foreign-newsgroups)
1571                  (t 0))
1572            level))
1573          scanned-methods info group active method retrieve-groups)
1574     (gnus-message 5 "Checking new news...")
1575
1576     (while newsrc
1577       (setq active (gnus-active (setq group (gnus-info-group
1578                                              (setq info (pop newsrc))))))
1579
1580       ;; Check newsgroups.  If the user doesn't want to check them, or
1581       ;; they can't be checked (for instance, if the news server can't
1582       ;; be reached) we just set the number of unread articles in this
1583       ;; newsgroup to t.  This means that Gnus thinks that there are
1584       ;; unread articles, but it has no idea how many.
1585
1586       ;; To be more explicit:
1587       ;; >0 for an active group with messages
1588       ;; 0 for an active group with no unread messages
1589       ;; nil for non-foreign groups that the user has requested not be checked
1590       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1591       ;;   be checked, for one reason or other.
1592       (if (and (setq method (gnus-info-method info))
1593                (not (inline
1594                       (gnus-server-equal
1595                        gnus-select-method
1596                        (setq method (gnus-server-get-method nil method)))))
1597                (not (gnus-secondary-method-p method)))
1598           ;; These groups are foreign.  Check the level.
1599           (when (and (<= (gnus-info-level info) foreign-level)
1600                      (setq active (gnus-activate-group group 'scan)))
1601             ;; Let the Gnus agent save the active file.
1602             (when (and gnus-agent gnus-plugged active)
1603               (gnus-agent-save-group-info
1604                method (gnus-group-real-name group) active))
1605             (unless (inline (gnus-virtual-group-p group))
1606               (inline (gnus-close-group group)))
1607             (when (fboundp (intern (concat (symbol-name (car method))
1608                                            "-request-update-info")))
1609               (inline (gnus-request-update-info info method))))
1610         ;; These groups are native or secondary.
1611         (cond
1612          ;; We don't want these groups.
1613          ((> (gnus-info-level info) level)
1614           (setq active 'ignore))
1615          ;; Activate groups.
1616          ((not gnus-read-active-file)
1617           (if (gnus-check-backend-function 'retrieve-groups group)
1618               ;; if server support gnus-retrieve-groups we push
1619               ;; the group onto retrievegroups for later checking
1620               (if (assoc method retrieve-groups)
1621                   (setcdr (assoc method retrieve-groups)
1622                           (cons group (cdr (assoc method retrieve-groups))))
1623                 (push (list method group) retrieve-groups))
1624             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1625             ;; on the group, so we must perform a scan for every group
1626             ;; if the users has any directory mail sources.
1627             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1628             ;; for it scan all spool files even when the groups are
1629             ;; not required.
1630             (if (and
1631                  (or nnmail-scan-directory-mail-source-once
1632                      (null (assq 'directory
1633                                  (or mail-sources
1634                                      (if (listp nnmail-spool-file)
1635                                          nnmail-spool-file
1636                                        (list nnmail-spool-file))))))
1637                  (member method scanned-methods))
1638                 (setq active (gnus-activate-group group))
1639               (setq active (gnus-activate-group group 'scan))
1640               (push method scanned-methods))
1641             (when active
1642               (gnus-close-group group))))))
1643
1644       ;; Get the number of unread articles in the group.
1645       (cond
1646        ((eq active 'ignore)
1647         ;; Don't do anything.
1648         )
1649        (active
1650         (inline (gnus-get-unread-articles-in-group info active t)))
1651        (t
1652         ;; The group couldn't be reached, so we nix out the number of
1653         ;; unread articles and stuff.
1654         (gnus-set-active group nil)
1655         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1656           (when tmp
1657             (setcar tmp t))))))
1658
1659     ;; iterate through groups on methods which support gnus-retrieve-groups
1660     ;; and fetch a partial active file and use it to find new news.
1661     (dolist (rg retrieve-groups)
1662       (let ((method (or (car rg) gnus-select-method))
1663             (groups (cdr rg)))
1664         (when (gnus-check-server method)
1665           ;; Request that the backend scan its incoming messages.
1666           (when (gnus-check-backend-function 'request-scan (car method))
1667             (gnus-request-scan nil method))
1668           (gnus-read-active-file-2
1669            (mapcar (lambda (group) (gnus-group-real-name group)) groups)
1670            method)
1671           (dolist (group groups)
1672             (cond
1673              ((setq active (gnus-active (gnus-info-group
1674                                          (setq info (gnus-get-info group)))))
1675               (inline (gnus-get-unread-articles-in-group info active t)))
1676              (t
1677               ;; The group couldn't be reached, so we nix out the number of
1678               ;; unread articles and stuff.
1679               (gnus-set-active group nil)
1680               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1681
1682     (gnus-message 5 "Checking new news...done")))
1683
1684 ;; Create a hash table out of the newsrc alist.  The `car's of the
1685 ;; alist elements are used as keys.
1686 (defun gnus-make-hashtable-from-newsrc-alist ()
1687   (let ((alist gnus-newsrc-alist)
1688         (ohashtb gnus-newsrc-hashtb)
1689         prev)
1690     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1691     (setq alist
1692           (setq prev (setq gnus-newsrc-alist
1693                            (if (equal (caar gnus-newsrc-alist)
1694                                       "dummy.group")
1695                                gnus-newsrc-alist
1696                              (cons (list "dummy.group" 0 nil) alist)))))
1697     (while alist
1698       (gnus-sethash
1699        (caar alist)
1700        ;; Preserve number of unread articles in groups.
1701        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1702              prev)
1703        gnus-newsrc-hashtb)
1704       (setq prev alist
1705             alist (cdr alist)))))
1706
1707 (defun gnus-make-hashtable-from-killed ()
1708   "Create a hash table from the killed and zombie lists."
1709   (let ((lists '(gnus-killed-list gnus-zombie-list))
1710         list)
1711     (setq gnus-killed-hashtb
1712           (gnus-make-hashtable
1713            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1714     (while lists
1715       (setq list (symbol-value (pop lists)))
1716       (while list
1717         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1718
1719 (defun gnus-parse-active ()
1720   "Parse active info in the nntp server buffer."
1721   (save-excursion
1722     (set-buffer nntp-server-buffer)
1723     (goto-char (point-min))
1724     ;; Parse the result we got from `gnus-request-group'.
1725     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1726       (goto-char (match-beginning 1))
1727       (cons (read (current-buffer))
1728             (read (current-buffer))))))
1729
1730 (defun gnus-make-articles-unread (group articles)
1731   "Mark ARTICLES in GROUP as unread."
1732   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1733                           (gnus-gethash (gnus-group-real-name group)
1734                                         gnus-newsrc-hashtb))))
1735          (ranges (gnus-info-read info))
1736          news article)
1737     (while articles
1738       (when (gnus-member-of-range
1739              (setq article (pop articles)) ranges)
1740         (push article news)))
1741     (when news
1742       (gnus-info-set-read
1743        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1744       (gnus-group-update-group group t))))
1745
1746 ;; Enter all dead groups into the hashtb.
1747 (defun gnus-update-active-hashtb-from-killed ()
1748   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1749         (lists (list gnus-killed-list gnus-zombie-list))
1750         killed)
1751     (while lists
1752       (setq killed (car lists))
1753       (while killed
1754         (gnus-sethash (car killed) nil hashtb)
1755         (setq killed (cdr killed)))
1756       (setq lists (cdr lists)))))
1757
1758 (defun gnus-get-killed-groups ()
1759   "Go through the active hashtb and mark all unknown groups as killed."
1760   ;; First make sure active file has been read.
1761   (unless (gnus-read-active-file-p)
1762     (let ((gnus-read-active-file t))
1763       (gnus-read-active-file)))
1764   (unless gnus-killed-hashtb
1765     (gnus-make-hashtable-from-killed))
1766   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1767   (mapatoms
1768    (lambda (sym)
1769      (let ((groups 0)
1770            (group (symbol-name sym)))
1771        (if (or (null group)
1772                (gnus-gethash group gnus-killed-hashtb)
1773                (gnus-gethash group gnus-newsrc-hashtb))
1774            ()
1775          (let ((do-sub (gnus-matches-options-n group)))
1776            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1777                ()
1778              (setq groups (1+ groups))
1779              (push group gnus-killed-list)
1780              (gnus-sethash group group gnus-killed-hashtb))))))
1781    gnus-active-hashtb)
1782   (gnus-dribble-touch))
1783
1784 ;; Get the active file(s) from the backend(s).
1785 (defun gnus-read-active-file (&optional force not-native)
1786   (gnus-group-set-mode-line)
1787   (let ((methods
1788          (mapcar
1789           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1790           (append
1791            (if (and (not not-native)
1792                     (gnus-check-server gnus-select-method))
1793                ;; The native server is available.
1794                (cons gnus-select-method gnus-secondary-select-methods)
1795              ;; The native server is down, so we just do the
1796              ;; secondary ones.
1797              gnus-secondary-select-methods)
1798            ;; Also read from the archive server.
1799            (when (gnus-archive-server-wanted-p)
1800              (list "archive")))))
1801         method)
1802     (setq gnus-have-read-active-file nil)
1803     (save-excursion
1804       (set-buffer nntp-server-buffer)
1805       (while (setq method (pop methods))
1806         ;; Only do each method once, in case the methods appear more
1807         ;; than once in this list.
1808         (unless (member method methods)
1809           (condition-case ()
1810               (gnus-read-active-file-1 method force)
1811             ;; We catch C-g so that we can continue past servers
1812             ;; that do not respond.
1813             (quit
1814              (message "Quit reading the active file")
1815              nil)))))))
1816
1817 (defun gnus-read-active-file-1 (method force)
1818   (let (where mesg)
1819     (setq where (nth 1 method)
1820           mesg (format "Reading active file%s via %s..."
1821                        (if (and where (not (zerop (length where))))
1822                            (concat " from " where) "")
1823                        (car method)))
1824     (gnus-message 5 mesg)
1825     (when (gnus-check-server method)
1826       ;; Request that the backend scan its incoming messages.
1827       (when (gnus-check-backend-function 'request-scan (car method))
1828         (gnus-request-scan nil method))
1829       (cond
1830        ((and (eq gnus-read-active-file 'some)
1831              (gnus-check-backend-function 'retrieve-groups (car method))
1832              (not force))
1833         (let ((newsrc (cdr gnus-newsrc-alist))
1834               (gmethod (gnus-server-get-method nil method))
1835               groups info)
1836           (while (setq info (pop newsrc))
1837             (when (inline
1838                     (gnus-server-equal
1839                      (inline
1840                        (gnus-find-method-for-group
1841                         (gnus-info-group info) info))
1842                      gmethod))
1843               (push (gnus-group-real-name (gnus-info-group info))
1844                     groups)))
1845           (gnus-read-active-file-2 groups method)))
1846        ((null method)
1847         t)
1848        (t
1849         (if (not (gnus-request-list method))
1850             (unless (equal method gnus-message-archive-method)
1851               (gnus-error 1 "Cannot read active file from %s server"
1852                           (car method)))
1853           (gnus-message 5 mesg)
1854           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1855           ;; We mark this active file as read.
1856           (push method gnus-have-read-active-file)
1857           (gnus-message 5 "%sdone" mesg)))))))
1858
1859 (defun gnus-read-active-file-2 (groups method)
1860   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1861   (when groups
1862     (save-excursion
1863       (set-buffer nntp-server-buffer)
1864       (gnus-check-server method)
1865       (let ((list-type (gnus-retrieve-groups groups method)))
1866         (cond ((not list-type)
1867                (gnus-error
1868                 1.2 "Cannot read partial active file from %s server."
1869                 (car method)))
1870               ((eq list-type 'active)
1871                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1872               (t
1873                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
1874
1875 ;; Read an active file and place the results in `gnus-active-hashtb'.
1876 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1877                                              real-active)
1878   (unless method
1879     (setq method gnus-select-method))
1880   (let ((cur (current-buffer))
1881         (hashtb (or hashtb
1882                     (if (and gnus-active-hashtb
1883                              (not (equal method gnus-select-method)))
1884                         gnus-active-hashtb
1885                       (setq gnus-active-hashtb
1886                             (if (equal method gnus-select-method)
1887                                 (gnus-make-hashtable
1888                                  (count-lines (point-min) (point-max)))
1889                               (gnus-make-hashtable 4096)))))))
1890     ;; Delete unnecessary lines.
1891     (goto-char (point-min))
1892     (cond
1893      ((string= gnus-ignored-newsgroups "")
1894       (delete-matching-lines "^to\\."))
1895      (t
1896       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
1897
1898     (goto-char (point-min))
1899     (unless (re-search-forward "[\\\"]" nil t)
1900       ;; Make the group names readable as a lisp expression even if they
1901       ;; contain special characters.
1902       (goto-char (point-max))
1903       (while (re-search-backward "[][';?()#]" nil t)
1904         (insert ?\\)))
1905
1906     ;; Let the Gnus agent save the active file.
1907     (when (and gnus-agent real-active gnus-plugged)
1908       (gnus-agent-save-active method))
1909
1910     ;; If these are groups from a foreign select method, we insert the
1911     ;; group prefix in front of the group names.
1912     (when (not (gnus-server-equal
1913                 (gnus-server-get-method nil method)
1914                 (gnus-server-get-method nil gnus-select-method)))
1915       (let ((prefix (gnus-group-prefixed-name "" method)))
1916         (goto-char (point-min))
1917         (while (and (not (eobp))
1918                     (progn
1919                       (when (= (following-char) ?\")
1920                         (forward-char 1))
1921                       (insert prefix)
1922                       (zerop (forward-line 1)))))))
1923     ;; Store the active file in a hash table.
1924     (goto-char (point-min))
1925     (let (group max min)
1926       (while (not (eobp))
1927         (condition-case err
1928             (progn
1929               (narrow-to-region (point) (gnus-point-at-eol))
1930               ;; group gets set to a symbol interned in the hash table
1931               ;; (what a hack!!) - jwz
1932               (setq group (let ((obarray hashtb)) (read cur)))
1933               ;; ### The extended group name scheme makes
1934               ;; the previous optimization strategy sort of pointless...
1935               (when (stringp group)
1936                 (setq group (intern group hashtb)))
1937               (if (and (numberp (setq max (read cur)))
1938                        (numberp (setq min (read cur)))
1939                        (progn
1940                          (skip-chars-forward " \t")
1941                          (not
1942                           (or (eq (char-after) ?=)
1943                               (eq (char-after) ?x)
1944                               (eq (char-after) ?j)))))
1945                   (progn
1946                     (set group (cons min max))
1947                     ;; if group is moderated, stick in moderation table
1948                     (when (eq (char-after) ?m)
1949                       (unless gnus-moderated-hashtb
1950                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
1951                       (gnus-sethash (symbol-name group) t
1952                                     gnus-moderated-hashtb)))
1953                 (set group nil)))
1954           (error
1955            (and group
1956                 (symbolp group)
1957                 (set group nil))
1958            (unless ignore-errors
1959              (gnus-message 3 "Warning - invalid active: %s"
1960                            (buffer-substring
1961                             (gnus-point-at-bol) (gnus-point-at-eol))))))
1962         (widen)
1963         (forward-line 1)))))
1964
1965 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
1966   ;; Parse a "groups" active file.
1967   (let ((cur (current-buffer))
1968         (hashtb (or hashtb
1969                     (if (and method gnus-active-hashtb)
1970                         gnus-active-hashtb
1971                       (setq gnus-active-hashtb
1972                             (gnus-make-hashtable
1973                              (count-lines (point-min) (point-max)))))))
1974         (prefix (and method
1975                      (not (gnus-server-equal
1976                            (gnus-server-get-method nil method)
1977                            (gnus-server-get-method nil gnus-select-method)))
1978                      (gnus-group-prefixed-name "" method))))
1979
1980     ;; Let the Gnus agent save the active file.
1981     (if (and gnus-agent
1982              real-active
1983              gnus-plugged
1984              (gnus-agent-method-p method))
1985         (progn
1986           (gnus-agent-save-groups method)
1987           (gnus-active-to-gnus-format method hashtb nil real-active))
1988
1989       (goto-char (point-min))
1990       ;; We split this into to separate loops, one with the prefix
1991       ;; and one without to speed the reading up somewhat.
1992       (if prefix
1993           (let (min max opoint group)
1994             (while (not (eobp))
1995               (condition-case ()
1996                   (progn
1997                     (read cur) (read cur)
1998                     (setq min (read cur)
1999                           max (read cur)
2000                           opoint (point))
2001                     (skip-chars-forward " \t")
2002                     (insert prefix)
2003                     (goto-char opoint)
2004                     (set (let ((obarray hashtb)) (read cur))
2005                          (cons min max)))
2006                 (error (and group (symbolp group) (set group nil))))
2007               (forward-line 1)))
2008         (let (min max group)
2009           (while (not (eobp))
2010             (condition-case ()
2011                 (when (eq (char-after) ?2)
2012                   (read cur) (read cur)
2013                   (setq min (read cur)
2014                         max (read cur))
2015                   (set (setq group (let ((obarray hashtb)) (read cur)))
2016                        (cons min max)))
2017               (error (and group (symbolp group) (set group nil))))
2018             (forward-line 1)))))))
2019
2020 (defun gnus-read-newsrc-file (&optional force)
2021   "Read startup file.
2022 If FORCE is non-nil, the .newsrc file is read."
2023   ;; Reset variables that might be defined in the .newsrc.eld file.
2024   (gnus-clear-quick-file-variables)
2025   (let* ((newsrc-file gnus-current-startup-file)
2026          (quick-file (concat newsrc-file ".el")))
2027     (save-excursion
2028       ;; We always load the .newsrc.eld file.  If always contains
2029       ;; much information that can not be gotten from the .newsrc
2030       ;; file (ticked articles, killed groups, foreign methods, etc.)
2031       (gnus-read-newsrc-el-file quick-file)
2032
2033       (when (and gnus-read-newsrc-file
2034                  (file-exists-p gnus-current-startup-file)
2035                  (or force
2036                      (and (file-newer-than-file-p newsrc-file quick-file)
2037                           (file-newer-than-file-p newsrc-file
2038                                                   (concat quick-file "d")))
2039                      (not gnus-newsrc-alist)))
2040         ;; We read the .newsrc file.  Note that if there if a
2041         ;; .newsrc.eld file exists, it has already been read, and
2042         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2043         ;; the .newsrc file, Gnus will only use the information it
2044         ;; can find there for changing the data already read -
2045         ;; i. e., reading the .newsrc file will not trash the data
2046         ;; already read (except for read articles).
2047         (save-excursion
2048           (gnus-message 5 "Reading %s..." newsrc-file)
2049           (set-buffer (nnheader-find-file-noselect newsrc-file))
2050           (buffer-disable-undo)
2051           (gnus-newsrc-to-gnus-format)
2052           (kill-buffer (current-buffer))
2053           (gnus-message 5 "Reading %s...done" newsrc-file))))))
2054
2055 (defun gnus-read-newsrc-el-file (file)
2056   (let ((ding-file (concat file "d")))
2057     ;; We always, always read the .eld file.
2058     (gnus-message 5 "Reading %s..." ding-file)
2059     (let (gnus-newsrc-assoc)
2060       (when (file-exists-p ding-file)
2061         (with-temp-buffer
2062           (condition-case nil
2063               (progn
2064                 (insert-file-contents-as-coding-system
2065                  gnus-startup-file-coding-system ding-file)
2066                 (eval-region (point-min) (point-max)))
2067             (error
2068              (ding)
2069              (or (not (or (zerop (buffer-size))
2070                           (eq 'binary gnus-startup-file-coding-system)
2071                           (gnus-re-read-newsrc-el-file ding-file)))
2072                  (gnus-yes-or-no-p
2073                   (format "Error in %s; continue? " ding-file))
2074                  (error "Error in %s" ding-file)))))
2075         (when gnus-newsrc-assoc
2076           (setq gnus-newsrc-alist gnus-newsrc-assoc))))
2077     (gnus-make-hashtable-from-newsrc-alist)
2078     (when (file-newer-than-file-p file ding-file)
2079       ;; Old format quick file
2080       (gnus-message 5 "Reading %s..." file)
2081       ;; The .el file is newer than the .eld file, so we read that one
2082       ;; as well.
2083       (gnus-read-old-newsrc-el-file file)))
2084   (when (and gnus-product-directory
2085              (file-directory-p gnus-product-directory))
2086     (let ((list gnus-product-variable-file-list))
2087       (while list
2088         (apply 'gnus-product-read-variable-file-1 (car list))
2089         (setq list (cdr list))))))
2090
2091 (defun gnus-re-read-newsrc-el-file (file)
2092   "Attempt to re-read .newsrc.eld file.  Returns `nil' if successful.
2093 The backup file \".newsrc.eld_\" will be created before re-reading."
2094   (message "Error in %s; retrying..." file)
2095   (if (and
2096        (condition-case nil
2097            (let ((backup (concat file "_")))
2098              (copy-file file backup 'ok-if-already-exists 'keep-time)
2099              (message " (The backup file %s has been created)" backup)
2100              t)
2101          (error nil))
2102        (progn
2103          (insert-file-contents-as-binary file nil nil nil 'replace)
2104          (when (re-search-forward
2105                 "^[\t ]*([\t\n\r ]*setq[\t\n\r ]+gnus-format-specs" nil t)
2106            (delete-region (goto-char (match-beginning 0)) (forward-list 1))
2107            (decode-coding-region (point-min) (point-max)
2108                                  gnus-startup-file-coding-system)
2109            (condition-case nil
2110                (progn
2111                  (eval-region (point-min) (point-max))
2112                  t)
2113              (error nil)))))
2114       (prog1
2115           nil
2116         (message "Error in %s; retrying...done" file))
2117     (message "Error in %s; retrying...failed" file)
2118     t))
2119
2120 (defun gnus-product-read-variable-file-1 (file checking-methods coding
2121                                                &rest variables)
2122   (let (error gnus-product-file-version method file-ver)
2123     (when (or
2124            (condition-case err
2125                (let ((coding-system-for-read coding)
2126                      (input-coding-system coding))
2127                  (load (expand-file-name file gnus-product-directory) t nil t)
2128                  nil)
2129              (error
2130               (message "%s" err)
2131               (setq error t)))
2132            (and (assq 'emacs-version checking-methods)
2133                 (not (string= emacs-version
2134                               (cdr (assq 'emacs-version
2135                                          gnus-product-file-version)))))
2136            (and (setq method (assq 'product-version checking-methods))
2137                 (or (not (setq file-ver
2138                                (cdr (assq 'product-version
2139                                           gnus-product-file-version))))
2140                     (< (product-version-compare file-ver (cadr method)) 0))))
2141       (unless error
2142         (message "\"%s\" seems to have mismatched contents, updating..."
2143                  file))
2144       (while variables
2145         (set (car variables) nil)
2146         (gnus-product-variable-touch (car variables))
2147         (setq variables (cdr variables))))))
2148
2149 ;; Parse the old-style quick startup file
2150 (defun gnus-read-old-newsrc-el-file (file)
2151   (let (newsrc killed marked group m info)
2152     (prog1
2153         (let ((gnus-killed-assoc nil)
2154               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2155           (prog1
2156               (ignore-errors
2157                 (load file t t t))
2158             (setq newsrc gnus-newsrc-assoc
2159                   killed gnus-killed-assoc
2160                   marked gnus-marked-assoc)))
2161       (setq gnus-newsrc-alist nil)
2162       (while (setq group (pop newsrc))
2163         (if (setq info (gnus-get-info (car group)))
2164             (progn
2165               (gnus-info-set-read info (cddr group))
2166               (gnus-info-set-level
2167                info (if (nth 1 group) gnus-level-default-subscribed
2168                       gnus-level-default-unsubscribed))
2169               (push info gnus-newsrc-alist))
2170           (push (setq info
2171                       (list (car group)
2172                             (if (nth 1 group) gnus-level-default-subscribed
2173                               gnus-level-default-unsubscribed)
2174                             (cddr group)))
2175                 gnus-newsrc-alist))
2176         ;; Copy marks into info.
2177         (when (setq m (assoc (car group) marked))
2178           (unless (nthcdr 3 info)
2179             (nconc info (list nil)))
2180           (gnus-info-set-marks
2181            info (list (cons 'tick (gnus-compress-sequence
2182                                    (sort (cdr m) '<) t))))))
2183       (setq newsrc killed)
2184       (while newsrc
2185         (setcar newsrc (caar newsrc))
2186         (setq newsrc (cdr newsrc)))
2187       (setq gnus-killed-list killed))
2188     ;; The .el file version of this variable does not begin with
2189     ;; "options", while the .eld version does, so we just add it if it
2190     ;; isn't there.
2191     (when
2192         gnus-newsrc-options
2193       (when (not (string-match "^ *options" gnus-newsrc-options))
2194         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2195       (when (not (string-match "\n$" gnus-newsrc-options))
2196         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2197       ;; Finally, if we read some options lines, we parse them.
2198       (unless (string= gnus-newsrc-options "")
2199         (gnus-newsrc-parse-options gnus-newsrc-options)))
2200
2201     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2202     (gnus-make-hashtable-from-newsrc-alist)))
2203
2204 (defun gnus-make-newsrc-file (file)
2205   "Make server dependent file name by catenating FILE and server host name."
2206   (let* ((file (expand-file-name file nil))
2207          (real-file (concat file "-" (nth 1 gnus-select-method))))
2208     (if (or (file-exists-p real-file)
2209             (file-exists-p (concat real-file ".el"))
2210             (file-exists-p (concat real-file ".eld")))
2211         real-file
2212       file)))
2213
2214 (defun gnus-newsrc-to-gnus-format ()
2215   (setq gnus-newsrc-options "")
2216   (setq gnus-newsrc-options-n nil)
2217
2218   (unless gnus-active-hashtb
2219     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2220   (let ((buf (current-buffer))
2221         (already-read (> (length gnus-newsrc-alist) 1))
2222         group subscribed options-symbol newsrc Options-symbol
2223         symbol reads num1)
2224     (goto-char (point-min))
2225     ;; We intern the symbol `options' in the active hashtb so that we
2226     ;; can `eq' against it later.
2227     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2228     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2229
2230     (while (not (eobp))
2231       ;; We first read the first word on the line by narrowing and
2232       ;; then reading into `gnus-active-hashtb'.  Most groups will
2233       ;; already exist in that hashtb, so this will save some string
2234       ;; space.
2235       (narrow-to-region
2236        (point)
2237        (progn (skip-chars-forward "^ \t!:\n") (point)))
2238       (goto-char (point-min))
2239       (setq symbol
2240             (and (/= (point-min) (point-max))
2241                  (let ((obarray gnus-active-hashtb)) (read buf))))
2242       (widen)
2243       ;; Now, the symbol we have read is either `options' or a group
2244       ;; name.  If it is an options line, we just add it to a string.
2245       (cond
2246        ((or (eq symbol options-symbol)
2247             (eq symbol Options-symbol))
2248         (setq gnus-newsrc-options
2249               ;; This concating is quite inefficient, but since our
2250               ;; thorough studies show that approx 99.37% of all
2251               ;; .newsrc files only contain a single options line, we
2252               ;; don't give a damn, frankly, my dear.
2253               (concat gnus-newsrc-options
2254                       (buffer-substring
2255                        (gnus-point-at-bol)
2256                        ;; Options may continue on the next line.
2257                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2258                                 (progn (beginning-of-line) (point)))
2259                            (point)))))
2260         (forward-line -1))
2261        (symbol
2262         ;; Group names can be just numbers.
2263         (when (numberp symbol)
2264           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2265         (unless (boundp symbol)
2266           (set symbol nil))
2267         ;; It was a group name.
2268         (setq subscribed (eq (char-after) ?:)
2269               group (symbol-name symbol)
2270               reads nil)
2271         (if (eolp)
2272             ;; If the line ends here, this is clearly a buggy line, so
2273             ;; we put point a the beginning of line and let the cond
2274             ;; below do the error handling.
2275             (beginning-of-line)
2276           ;; We skip to the beginning of the ranges.
2277           (skip-chars-forward "!: \t"))
2278         ;; We are now at the beginning of the list of read articles.
2279         ;; We read them range by range.
2280         (while
2281             (cond
2282              ((looking-at "[0-9]+")
2283               ;; We narrow and read a number instead of buffer-substring/
2284               ;; string-to-int because it's faster.  narrow/widen is
2285               ;; faster than save-restriction/narrow, and save-restriction
2286               ;; produces a garbage object.
2287               (setq num1 (progn
2288                            (narrow-to-region (match-beginning 0) (match-end 0))
2289                            (read buf)))
2290               (widen)
2291               ;; If the next character is a dash, then this is a range.
2292               (if (eq (char-after) ?-)
2293                   (progn
2294                     ;; We read the upper bound of the range.
2295                     (forward-char 1)
2296                     (if (not (looking-at "[0-9]+"))
2297                         ;; This is a buggy line, by we pretend that
2298                         ;; it's kinda OK.  Perhaps the user should be
2299                         ;; dinged?
2300                         (push num1 reads)
2301                       (push
2302                        (cons num1
2303                              (progn
2304                                (narrow-to-region (match-beginning 0)
2305                                                  (match-end 0))
2306                                (read buf)))
2307                        reads)
2308                       (widen)))
2309                 ;; It was just a simple number, so we add it to the
2310                 ;; list of ranges.
2311                 (push num1 reads))
2312               ;; If the next char in ?\n, then we have reached the end
2313               ;; of the line and return nil.
2314               (not (eq (char-after) ?\n)))
2315              ((eq (char-after) ?\n)
2316               ;; End of line, so we end.
2317               nil)
2318              (t
2319               ;; Not numbers and not eol, so this might be a buggy
2320               ;; line...
2321               (unless (eobp)
2322                 ;; If it was eob instead of ?\n, we allow it.
2323                 ;; The line was buggy.
2324                 (setq group nil)
2325                 (gnus-error 3.1 "Mangled line: %s"
2326                             (buffer-substring (gnus-point-at-bol)
2327                                               (gnus-point-at-eol))))
2328               nil))
2329           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2330           ;; we allow them, because it's a common mistake to put a
2331           ;; space after the comma.
2332           (skip-chars-forward ", "))
2333
2334         ;; We have already read .newsrc.eld, so we gently update the
2335         ;; data in the hash table with the information we have just
2336         ;; read.
2337         (when group
2338           (let ((info (gnus-get-info group))
2339                 level)
2340             (if info
2341                 ;; There is an entry for this file in the alist.
2342                 (progn
2343                   (gnus-info-set-read info (nreverse reads))
2344                   ;; We update the level very gently.  In fact, we
2345                   ;; only change it if there's been a status change
2346                   ;; from subscribed to unsubscribed, or vice versa.
2347                   (setq level (gnus-info-level info))
2348                   (cond ((and (<= level gnus-level-subscribed)
2349                               (not subscribed))
2350                          (setq level (if reads
2351                                          gnus-level-default-unsubscribed
2352                                        (1+ gnus-level-default-unsubscribed))))
2353                         ((and (> level gnus-level-subscribed) subscribed)
2354                          (setq level gnus-level-default-subscribed)))
2355                   (gnus-info-set-level info level))
2356               ;; This is a new group.
2357               (setq info (list group
2358                                (if subscribed
2359                                    gnus-level-default-subscribed
2360                                  (if reads
2361                                      (1+ gnus-level-subscribed)
2362                                    gnus-level-default-unsubscribed))
2363                                (nreverse reads))))
2364             (push info newsrc)))))
2365       (forward-line 1))
2366
2367     (setq newsrc (nreverse newsrc))
2368
2369     (if (not already-read)
2370         ()
2371       ;; We now have two newsrc lists - `newsrc', which is what we
2372       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2373       ;; what we've read from .newsrc.eld.  We have to merge these
2374       ;; lists.  We do this by "attaching" any (foreign) groups in the
2375       ;; gnus-newsrc-alist to the (native) group that precedes them.
2376       (let ((rc (cdr gnus-newsrc-alist))
2377             (prev gnus-newsrc-alist)
2378             entry mentry)
2379         (while rc
2380           (or (null (nth 4 (car rc)))   ; It's a native group.
2381               (assoc (caar rc) newsrc)  ; It's already in the alist.
2382               (if (setq entry (assoc (caar prev) newsrc))
2383                   (setcdr (setq mentry (memq entry newsrc))
2384                           (cons (car rc) (cdr mentry)))
2385                 (push (car rc) newsrc)))
2386           (setq prev rc
2387                 rc (cdr rc)))))
2388
2389     (setq gnus-newsrc-alist newsrc)
2390     ;; We make the newsrc hashtb.
2391     (gnus-make-hashtable-from-newsrc-alist)
2392
2393     ;; Finally, if we read some options lines, we parse them.
2394     (unless (string= gnus-newsrc-options "")
2395       (gnus-newsrc-parse-options gnus-newsrc-options))))
2396
2397 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2398 ;; The return value will be a list on the form
2399 ;; ((regexp1 . ignore)
2400 ;;  (regexp2 . subscribe)...)
2401 ;; When handling new newsgroups, groups that match a `ignore' regexp
2402 ;; will be ignored, and groups that match a `subscribe' regexp will be
2403 ;; subscribed.  A line like
2404 ;; options -n !all rec.all
2405 ;; will lead to a list that looks like
2406 ;; (("^rec\\..+" . subscribe)
2407 ;;  ("^.+" . ignore))
2408 ;; So all "rec.*" groups will be subscribed, while all the other
2409 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2410 ;; different from "options -n rec.all !all".
2411 (defun gnus-newsrc-parse-options (options)
2412   (let (out eol)
2413     (save-excursion
2414       (gnus-set-work-buffer)
2415       (insert (regexp-quote options))
2416       ;; First we treat all continuation lines.
2417       (goto-char (point-min))
2418       (while (re-search-forward "\n[ \t]+" nil t)
2419         (replace-match " " t t))
2420       ;; Then we transform all "all"s into ".+"s.
2421       (goto-char (point-min))
2422       (while (re-search-forward "\\ball\\b" nil t)
2423         (replace-match ".+" t t))
2424       (goto-char (point-min))
2425       ;; We remove all other options than the "-n" ones.
2426       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2427         (replace-match " ")
2428         (forward-char -1))
2429       (goto-char (point-min))
2430
2431       ;; We are only interested in "options -n" lines - we
2432       ;; ignore the other option lines.
2433       (while (re-search-forward "[ \t]-n" nil t)
2434         (setq eol
2435               (or (save-excursion
2436                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2437                          (- (point) 2)))
2438                   (gnus-point-at-eol)))
2439         ;; Search for all "words"...
2440         (while (re-search-forward "[^ \t,\n]+" eol t)
2441           (if (eq (char-after (match-beginning 0)) ?!)
2442               ;; If the word begins with a bang (!), this is a "not"
2443               ;; spec.  We put this spec (minus the bang) and the
2444               ;; symbol `ignore' into the list.
2445               (push (cons (concat
2446                            "^" (buffer-substring
2447                                 (1+ (match-beginning 0))
2448                                 (match-end 0))
2449                            "\\($\\|\\.\\)")
2450                           'ignore)
2451                     out)
2452             ;; There was no bang, so this is a "yes" spec.
2453             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2454                         'subscribe)
2455                   out))))
2456
2457       (setq gnus-newsrc-options-n out))))
2458
2459 (defun gnus-save-newsrc-file (&optional force)
2460   "Save .newsrc file."
2461   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2462   ;; from the variable gnus-newsrc-alist.
2463   (when (and (or gnus-newsrc-alist gnus-killed-list)
2464              gnus-current-startup-file)
2465     (save-excursion
2466       (if (and (or gnus-use-dribble-file gnus-slave)
2467                (not force)
2468                (or (not gnus-dribble-buffer)
2469                    (not (buffer-name gnus-dribble-buffer))
2470                    (zerop (save-excursion
2471                             (set-buffer gnus-dribble-buffer)
2472                             (buffer-size)))))
2473           (gnus-message 4 "(No changes need to be saved)")
2474         (gnus-run-hooks 'gnus-save-newsrc-hook)
2475         (if gnus-slave
2476             (gnus-slave-save-newsrc)
2477           ;; Save .newsrc.
2478           (when gnus-save-newsrc-file
2479             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2480             (gnus-gnus-to-newsrc-format)
2481             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2482           ;; Save .newsrc.eld.
2483           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2484           (make-local-variable 'version-control)
2485           (setq version-control 'never)
2486           (setq buffer-file-name
2487                 (concat gnus-current-startup-file ".eld"))
2488           (setq default-directory (file-name-directory buffer-file-name))
2489           (buffer-disable-undo)
2490           (erase-buffer)
2491           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2492           (gnus-gnus-to-quick-newsrc-format)
2493           (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2494           (save-buffer-as-coding-system gnus-startup-file-coding-system)
2495           (kill-buffer (current-buffer))
2496           (gnus-message
2497            5 "Saving %s.eld...done" gnus-current-startup-file))
2498         (gnus-dribble-delete-file)
2499         (gnus-group-set-mode-line))))
2500   (when gnus-product-directory
2501     (gnus-product-save-variable-file)))
2502
2503 ;; Call the function above at C-x C-c.
2504 (defadvice save-buffers-kill-emacs (before save-gnus-newsrc-file-maybe
2505                                            activate preactivate)
2506   "Save .newsrc and .newsrc.eld when Emacs is killed."
2507   (when (gnus-alive-p)
2508     (gnus-run-hooks 'gnus-exit-gnus-hook)
2509     (gnus-offer-save-summaries)
2510     (gnus-save-newsrc-file)))
2511
2512 (defun gnus-gnus-to-quick-newsrc-format ()
2513   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
2514   (let ((print-quoted t)
2515         (print-escape-newlines t))
2516
2517     (insert ";; -*- emacs-lisp -*-\n")
2518     (insert ";; Gnus startup file.\n")
2519     (insert "\
2520 ;; Never delete this file -- if you want to force Gnus to read the
2521 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2522     (insert "(setq gnus-newsrc-file-version "
2523             (prin1-to-string gnus-version) ")\n")
2524     (let* ((gnus-killed-list
2525             (if (and gnus-save-killed-list
2526                      (stringp gnus-save-killed-list))
2527                 (gnus-strip-killed-list)
2528               gnus-killed-list))
2529            (variables
2530             (if gnus-save-killed-list gnus-variable-list
2531               ;; Remove the `gnus-killed-list' from the list of variables
2532               ;; to be saved, if required.
2533               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2534            ;; Peel off the "dummy" group.
2535            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2536            variable)
2537       ;; Insert the variables into the file.
2538       (while variables
2539         (when (and (boundp (setq variable (pop variables)))
2540                    (symbol-value variable))
2541           (insert "(setq " (symbol-name variable) " '")
2542           (gnus-prin1 (symbol-value variable))
2543           (insert ")\n"))))))
2544
2545 (defun gnus-product-variable-touch (&rest variables)
2546   (while variables
2547     (put (pop variables) 'gnus-product-variable 'dirty)))
2548
2549 (defun gnus-product-variables-dirty-p (variables)
2550   (catch 'done
2551     (while variables
2552       (when (eq (get (car variables) 'gnus-product-variable) 'dirty)
2553         (throw 'done t))
2554       (setq variables (cdr variables)))))
2555
2556 (defun gnus-product-save-variable-file (&optional force)
2557   "Save all product variables to files, when need to be saved."
2558   (let ((list gnus-product-variable-file-list))
2559     (gnus-make-directory gnus-product-directory)
2560     (while list
2561       (apply 'gnus-product-save-variable-file-1 force (car list))
2562       (setq list (cdr list)))))
2563
2564 (defun gnus-product-save-variable-file-1 (force file checking-methods coding
2565                                                 &rest variables)
2566   "Save a product variable file, when need to be saved."
2567   (when (or force
2568             (gnus-product-variables-dirty-p variables))
2569     (let ((product (product-find 'gnus-vers)))
2570       (set-buffer (gnus-get-buffer-create " *gnus-product*"))
2571       (make-local-variable 'version-control)
2572       (setq version-control 'never)
2573       (setq file (expand-file-name file gnus-product-directory)
2574             buffer-file-name file
2575             default-directory (file-name-directory file))
2576       (buffer-disable-undo)
2577       (erase-buffer)
2578       (gnus-message 5 "Saving %s..." file)
2579       (apply 'gnus-product-quick-file-format product checking-methods coding
2580              variables)
2581       (save-buffer-as-coding-system coding)
2582       (kill-buffer (current-buffer))
2583       (while variables
2584         (put (car variables) 'gnus-product-variable nil)
2585         (setq variables (cdr variables)))
2586       (gnus-message
2587        5 "Saving %s...done" file))))
2588
2589 (defun gnus-product-quick-file-format (product checking-methods
2590                                                coding &rest variables)
2591   "Insert gnus product depend variables in lisp format."
2592   (let ((print-quoted t)
2593         (print-escape-newlines t)
2594         variable param)
2595     (insert (format ";; -*- Mode: emacs-lisp; coding: %s -*-\n" coding))
2596     (insert (format ";; %s startup file.\n" (product-name product)))
2597     (when (setq param (cdr (assq 'product-version checking-methods)))
2598       (insert "(or (>= (product-version-compare "
2599               "(product-version (product-find 'gnus-vers))\n"
2600               "\t\t\t\t '" (apply 'prin1-to-string param) ")\n"
2601               "\t0)\n"
2602               "    (error \"This file was created by later version of "
2603               "gnus.\"))\n"))
2604     (insert "(setq gnus-product-file-version \n"
2605             "      '((product-version . "
2606             (prin1-to-string (product-version product)) ")\n"
2607             "\t(emacs-version . " (prin1-to-string emacs-version) ")))\n")
2608     (while variables
2609       (when (and (boundp (setq variable (pop variables)))
2610                  (symbol-value variable))
2611           (insert "(setq " (symbol-name variable) " '")
2612           (gnus-prin1 (symbol-value variable))
2613           (insert ")\n")))))
2614
2615 (defun gnus-strip-killed-list ()
2616   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2617   (let ((list gnus-killed-list)
2618         olist)
2619     (while list
2620       (when (string-match gnus-save-killed-list (car list))
2621         (push (car list) olist))
2622       (pop list))
2623     (nreverse olist)))
2624
2625 (defun gnus-gnus-to-newsrc-format ()
2626   ;; Generate and save the .newsrc file.
2627   (save-excursion
2628     (set-buffer (create-file-buffer gnus-current-startup-file))
2629     (let ((newsrc (cdr gnus-newsrc-alist))
2630           (standard-output (current-buffer))
2631           info ranges range method)
2632       (setq buffer-file-name gnus-current-startup-file)
2633       (setq default-directory (file-name-directory buffer-file-name))
2634       (buffer-disable-undo)
2635       (erase-buffer)
2636       ;; Write options.
2637       (when gnus-newsrc-options
2638         (insert gnus-newsrc-options))
2639       ;; Write subscribed and unsubscribed.
2640       (while (setq info (pop newsrc))
2641         ;; Don't write foreign groups to .newsrc.
2642         (when (or (null (setq method (gnus-info-method info)))
2643                   (equal method "native")
2644                   (inline (gnus-server-equal method gnus-select-method)))
2645           (insert (gnus-info-group info)
2646                   (if (> (gnus-info-level info) gnus-level-subscribed)
2647                       "!" ":"))
2648           (when (setq ranges (gnus-info-read info))
2649             (insert " ")
2650             (if (not (listp (cdr ranges)))
2651                 (if (= (car ranges) (cdr ranges))
2652                     (princ (car ranges))
2653                   (princ (car ranges))
2654                   (insert "-")
2655                   (princ (cdr ranges)))
2656               (while (setq range (pop ranges))
2657                 (if (or (atom range) (= (car range) (cdr range)))
2658                     (princ (or (and (atom range) range) (car range)))
2659                   (princ (car range))
2660                   (insert "-")
2661                   (princ (cdr range)))
2662                 (when ranges
2663                   (insert ",")))))
2664           (insert "\n")))
2665       (make-local-variable 'version-control)
2666       (setq version-control 'never)
2667       ;; It has been reported that sometime the modtime on the .newsrc
2668       ;; file seems to be off.  We really do want to overwrite it, so
2669       ;; we clear the modtime here before saving.  It's a bit odd,
2670       ;; though...
2671       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2672       ;; delete the silly thing entirely first.  but this fails to provide
2673       ;; such niceties as .newsrc~ creation.
2674       (if gnus-modtime-botch
2675           (delete-file gnus-startup-file)
2676         (clear-visited-file-modtime))
2677       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2678       (save-buffer)
2679       (kill-buffer (current-buffer)))))
2680
2681 \f
2682 ;;;
2683 ;;; Slave functions.
2684 ;;;
2685
2686 (defvar gnus-slave-mode nil)
2687
2688 (defun gnus-slave-mode ()
2689   "Minor mode for slave Gnusae."
2690   (gnus-add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2691   (gnus-run-hooks 'gnus-slave-mode-hook))
2692
2693 (defun gnus-slave-save-newsrc ()
2694   (save-excursion
2695     (set-buffer gnus-dribble-buffer)
2696     (let ((slave-name
2697            (make-temp-name (concat gnus-current-startup-file "-slave-")))
2698           (modes (ignore-errors
2699                    (file-modes (concat gnus-current-startup-file ".eld")))))
2700       (gnus-write-buffer-as-coding-system
2701        gnus-startup-file-coding-system slave-name)
2702       (when modes
2703         (set-file-modes slave-name modes)))))
2704
2705 (defun gnus-master-read-slave-newsrc ()
2706   (let ((slave-files
2707          (directory-files
2708           (file-name-directory gnus-current-startup-file)
2709           t (concat
2710              "^" (regexp-quote
2711                   (concat
2712                    (file-name-nondirectory gnus-current-startup-file)
2713                    "-slave-")))
2714           t))
2715         file)
2716     (if (not slave-files)
2717         ()                              ; There are no slave files to read.
2718       (gnus-message 7 "Reading slave newsrcs...")
2719       (save-excursion
2720         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2721         (setq slave-files
2722               (sort (mapcar (lambda (file)
2723                               (list (nth 5 (file-attributes file)) file))
2724                             slave-files)
2725                     (lambda (f1 f2)
2726                       (or (< (caar f1) (caar f2))
2727                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2728         (while slave-files
2729           (erase-buffer)
2730           (setq file (nth 1 (car slave-files)))
2731           (nnheader-insert-file-contents file)
2732           (when (condition-case ()
2733                     (progn
2734                       (eval-buffer (current-buffer))
2735                       t)
2736                   (error
2737                    (gnus-error 3.2 "Possible error in %s" file)
2738                    nil))
2739             (unless gnus-slave          ; Slaves shouldn't delete these files.
2740               (ignore-errors
2741                 (delete-file file))))
2742           (setq slave-files (cdr slave-files))))
2743       (gnus-dribble-touch)
2744       (gnus-message 7 "Reading slave newsrcs...done"))))
2745
2746 \f
2747 ;;;
2748 ;;; Group description.
2749 ;;;
2750
2751 (defun gnus-read-all-descriptions-files ()
2752   (let ((methods (cons gnus-select-method
2753                        (nconc
2754                         (when (gnus-archive-server-wanted-p)
2755                           (list "archive"))
2756                         gnus-secondary-select-methods))))
2757     (while methods
2758       (gnus-read-descriptions-file (car methods))
2759       (setq methods (cdr methods)))
2760     t))
2761
2762 (defun gnus-read-descriptions-file (&optional method)
2763   (let ((method (or method gnus-select-method))
2764         group)
2765     (when (stringp method)
2766       (setq method (gnus-server-to-method method)))
2767     ;; We create the hashtable whether we manage to read the desc file
2768     ;; to avoid trying to re-read after a failed read.
2769     (unless gnus-description-hashtb
2770       (setq gnus-description-hashtb
2771             (gnus-make-hashtable (length gnus-active-hashtb))))
2772     ;; Mark this method's desc file as read.
2773     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2774                   gnus-description-hashtb)
2775
2776     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2777     (cond
2778      ((null (gnus-get-function method 'request-list-newsgroups t))
2779       t)
2780      ((not (gnus-check-server method))
2781       (gnus-message 1 "Couldn't open server")
2782       nil)
2783      ((not (gnus-request-list-newsgroups method))
2784       (gnus-message 1 "Couldn't read newsgroups descriptions")
2785       nil)
2786      (t
2787       (save-excursion
2788         (save-restriction
2789           (set-buffer nntp-server-buffer)
2790           (goto-char (point-min))
2791           (when (or (search-forward "\n.\n" nil t)
2792                     (goto-char (point-max)))
2793             (beginning-of-line)
2794             (narrow-to-region (point-min) (point)))
2795           ;; If these are groups from a foreign select method, we insert the
2796           ;; group prefix in front of the group names.
2797           (and method (not (inline
2798                              (gnus-server-equal
2799                               (gnus-server-get-method nil method)
2800                               (gnus-server-get-method
2801                                nil gnus-select-method))))
2802                (let ((prefix (gnus-group-prefixed-name "" method)))
2803                  (goto-char (point-min))
2804                  (while (and (not (eobp))
2805                              (progn (insert prefix)
2806                                     (zerop (forward-line 1)))))))
2807           (goto-char (point-min))
2808           (while (not (eobp))
2809             ;; If we get an error, we set group to 0, which is not a
2810             ;; symbol...
2811             (setq group
2812                   (condition-case ()
2813                       (let ((obarray gnus-description-hashtb))
2814                         ;; Group is set to a symbol interned in this
2815                         ;; hash table.
2816                         (read nntp-server-buffer))
2817                     (error 0)))
2818             (skip-chars-forward " \t")
2819             ;; ...  which leads to this line being effectively ignored.
2820             (when (symbolp group)
2821               (let* ((str (buffer-substring
2822                            (point) (progn (end-of-line) (point))))
2823                      (name (symbol-name group))
2824                      (charset
2825                       (or (gnus-group-name-charset method name)
2826                           (gnus-parameter-charset name))))
2827                 (when (and str charset (featurep 'mule))
2828                   (setq str (decode-coding-string str charset)))
2829                 (set group str)))
2830             (forward-line 1))))
2831       (gnus-message 5 "Reading descriptions file...done")
2832       t))))
2833
2834 (defun gnus-group-get-description (group)
2835   "Get the description of a group by sending XGTITLE to the server."
2836   (when (gnus-request-group-description group)
2837     (save-excursion
2838       (set-buffer nntp-server-buffer)
2839       (goto-char (point-min))
2840       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
2841         (match-string 1)))))
2842
2843 ;;;###autoload
2844 (defun gnus-declare-backend (name &rest abilities)
2845   "Declare backend NAME with ABILITIES as a Gnus backend."
2846   (setq gnus-valid-select-methods
2847         (nconc gnus-valid-select-methods
2848                (list (apply 'list name abilities))))
2849   (gnus-redefine-select-method-widget))
2850
2851 (defun gnus-set-default-directory ()
2852   "Set the default directory in the current buffer to `gnus-default-directory'.
2853 If this variable is nil, don't do anything."
2854   (setq default-directory
2855         (if (and gnus-default-directory
2856                  (file-exists-p gnus-default-directory))
2857             (file-name-as-directory (expand-file-name gnus-default-directory))
2858           default-directory)))
2859
2860 (defun gnus-display-time-event-handler ()
2861   "Like `display-time-event-handler', but test `display-time-timer'."
2862   (when (gnus-boundp 'display-time-timer)
2863     (display-time-event-handler)))
2864
2865 (provide 'gnus-start)
2866
2867 ;;; gnus-start.el ends here