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