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