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