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