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