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