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