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