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