*** empty log message ***
[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      (if (> groups 0)
1080          (gnus-message 5 "%d new newsgroup%s arrived"
1081                        groups (if (> groups 1) "s have" " has"))
1082        (gnus-message 5 "No new newsgroups"))
1083     (when got-new
1084       (setq gnus-newsrc-last-checked-date new-date))
1085     got-new))
1086
1087 (defun gnus-check-first-time-used ()
1088   (if (or (> (length gnus-newsrc-alist) 1)
1089           (file-exists-p gnus-startup-file)
1090           (file-exists-p (concat gnus-startup-file ".el"))
1091           (file-exists-p (concat gnus-startup-file ".eld")))
1092       nil
1093     (gnus-message 6 "First time user; subscribing you to default groups")
1094     (unless (gnus-read-active-file-p)
1095       (let ((gnus-read-active-file t))
1096         (gnus-read-active-file)))
1097     (setq gnus-newsrc-last-checked-date (current-time-string))
1098     (let ((groups gnus-default-subscribed-newsgroups)
1099           group)
1100       (if (eq groups t)
1101           nil
1102         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
1103         (mapatoms
1104          (lambda (sym)
1105            (if (null (setq group (symbol-name sym)))
1106                ()
1107              (let ((do-sub (gnus-matches-options-n group)))
1108                (cond
1109                 ((eq do-sub 'subscribe)
1110                  (gnus-sethash group group gnus-killed-hashtb)
1111                  (funcall gnus-subscribe-options-newsgroup-method group))
1112                 ((eq do-sub 'ignore)
1113                  nil)
1114                 (t
1115                  (push group gnus-killed-list))))))
1116          gnus-active-hashtb)
1117         (while groups
1118           (when (gnus-active (car groups))
1119             (gnus-group-change-level
1120              (car groups) gnus-level-default-subscribed gnus-level-killed))
1121           (setq groups (cdr groups)))
1122         (gnus-group-make-help-group)
1123         (when gnus-novice-user
1124           (gnus-message 7 "`A k' to list killed groups"))))))
1125
1126 (defun gnus-subscribe-group (group previous &optional method)
1127   (gnus-group-change-level
1128    (if method
1129        (list t group gnus-level-default-subscribed nil nil method)
1130      group)
1131    gnus-level-default-subscribed gnus-level-killed previous t))
1132
1133 ;; `gnus-group-change-level' is the fundamental function for changing
1134 ;; subscription levels of newsgroups.  This might mean just changing
1135 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1136 ;; again, which subscribes/unsubscribes a group, which is equally
1137 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1138 ;; from 8-9 to 1-7 means that you remove the group from the list of
1139 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1140 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1141 ;; which is trivial.
1142 ;; ENTRY can either be a string (newsgroup name) or a list (if
1143 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1144 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1145 ;; entries.
1146 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1147 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1148 ;; after.
1149 (defun gnus-group-change-level (entry level &optional oldlevel
1150                                       previous fromkilled)
1151   (let (group info active num)
1152     ;; Glean what info we can from the arguments
1153     (if (consp entry)
1154         (if fromkilled (setq group (nth 1 entry))
1155           (setq group (car (nth 2 entry))))
1156       (setq group entry))
1157     (when (and (stringp entry)
1158                oldlevel
1159                (< oldlevel gnus-level-zombie))
1160       (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
1161     (if (and (not oldlevel)
1162              (consp entry))
1163         (setq oldlevel (gnus-info-level (nth 2 entry)))
1164       (setq oldlevel (or oldlevel gnus-level-killed)))
1165     (when (stringp previous)
1166       (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
1167
1168     (if (and (>= oldlevel gnus-level-zombie)
1169              (gnus-gethash group gnus-newsrc-hashtb))
1170         ;; We are trying to subscribe a group that is already
1171         ;; subscribed.
1172         ()                              ; Do nothing.
1173
1174       (unless (gnus-ephemeral-group-p group)
1175         (gnus-dribble-enter
1176          (format "(gnus-group-change-level %S %S %S %S %S)"
1177                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1178
1179       ;; Then we remove the newgroup from any old structures, if needed.
1180       ;; If the group was killed, we remove it from the killed or zombie
1181       ;; list.  If not, and it is in fact going to be killed, we remove
1182       ;; it from the newsrc hash table and assoc.
1183       (cond
1184        ((>= oldlevel gnus-level-zombie)
1185         (if (= oldlevel gnus-level-zombie)
1186             (setq gnus-zombie-list (delete group gnus-zombie-list))
1187           (setq gnus-killed-list (delete group gnus-killed-list))))
1188        (t
1189         (when (and (>= level gnus-level-zombie)
1190                    entry)
1191           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1192           (when (nth 3 entry)
1193             (setcdr (gnus-gethash (car (nth 3 entry))
1194                                   gnus-newsrc-hashtb)
1195                     (cdr entry)))
1196           (setcdr (cdr entry) (cdddr entry)))))
1197
1198       ;; Finally we enter (if needed) the list where it is supposed to
1199       ;; go, and change the subscription level.  If it is to be killed,
1200       ;; we enter it into the killed or zombie list.
1201       (cond
1202        ((>= level gnus-level-zombie)
1203         ;; Remove from the hash table.
1204         (gnus-sethash group nil gnus-newsrc-hashtb)
1205         ;; We do not enter foreign groups into the list of dead
1206         ;; groups.
1207         (unless (gnus-group-foreign-p group)
1208           (if (= level gnus-level-zombie)
1209               (push group gnus-zombie-list)
1210             (push group gnus-killed-list))))
1211        (t
1212         ;; If the list is to be entered into the newsrc assoc, and
1213         ;; it was killed, we have to create an entry in the newsrc
1214         ;; hashtb format and fix the pointers in the newsrc assoc.
1215         (if (< oldlevel gnus-level-zombie)
1216             ;; It was alive, and it is going to stay alive, so we
1217             ;; just change the level and don't change any pointers or
1218             ;; hash table entries.
1219             (setcar (cdaddr entry) level)
1220           (if (listp entry)
1221               (setq info (cdr entry)
1222                     num (car entry))
1223             (setq active (gnus-active group))
1224             (setq num
1225                   (if active (- (1+ (cdr active)) (car active)) t))
1226             ;; Check whether the group is foreign.  If so, the
1227             ;; foreign select method has to be entered into the
1228             ;; info.
1229             (let ((method (or gnus-override-subscribe-method
1230                               (gnus-group-method group))))
1231               (if (eq method gnus-select-method)
1232                   (setq info (list group level nil))
1233                 (setq info (list group level nil nil method)))))
1234           (unless previous
1235             (setq previous
1236                   (let ((p gnus-newsrc-alist))
1237                     (while (cddr p)
1238                       (setq p (cdr p)))
1239                     p)))
1240           (setq entry (cons info (cddr previous)))
1241           (if (cdr previous)
1242               (progn
1243                 (setcdr (cdr previous) entry)
1244                 (gnus-sethash group (cons num (cdr previous))
1245                               gnus-newsrc-hashtb))
1246             (setcdr previous entry)
1247             (gnus-sethash group (cons num previous)
1248                           gnus-newsrc-hashtb))
1249           (when (cdr entry)
1250             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1251           (gnus-dribble-enter
1252            (format
1253             "(gnus-group-set-info '%S)" info)))))
1254       (when gnus-group-change-level-function
1255         (funcall gnus-group-change-level-function
1256                  group level oldlevel previous)))))
1257
1258 (defun gnus-kill-newsgroup (newsgroup)
1259   "Obsolete function.  Kills a newsgroup."
1260   (gnus-group-change-level
1261    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1262
1263 (defun gnus-check-bogus-newsgroups (&optional confirm)
1264   "Remove bogus newsgroups.
1265 If CONFIRM is non-nil, the user has to confirm the deletion of every
1266 newsgroup."
1267   (let ((newsrc (cdr gnus-newsrc-alist))
1268         bogus group entry info)
1269     (gnus-message 5 "Checking bogus newsgroups...")
1270     (unless (gnus-read-active-file-p)
1271       (gnus-read-active-file t))
1272     (when (gnus-read-active-file-p)
1273       ;; Find all bogus newsgroup that are subscribed.
1274       (while newsrc
1275         (setq info (pop newsrc)
1276               group (gnus-info-group info))
1277         (unless (or (gnus-active group) ; Active
1278                     (gnus-info-method info)) ; Foreign
1279           ;; Found a bogus newsgroup.
1280           (push group bogus)))
1281       (if confirm
1282           (map-y-or-n-p
1283            "Remove bogus group %s? "
1284            (lambda (group)
1285              ;; Remove all bogus subscribed groups by first killing them, and
1286              ;; then removing them from the list of killed groups.
1287              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1288                (gnus-group-change-level entry gnus-level-killed)
1289                (setq gnus-killed-list (delete group gnus-killed-list))))
1290            bogus '("group" "groups" "remove"))
1291         (while (setq group (pop bogus))
1292           ;; Remove all bogus subscribed groups by first killing them, and
1293           ;; then removing them from the list of killed groups.
1294           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1295             (gnus-group-change-level entry gnus-level-killed)
1296             (setq gnus-killed-list (delete group gnus-killed-list)))))
1297       ;; Then we remove all bogus groups from the list of killed and
1298       ;; zombie groups.  They are removed without confirmation.
1299       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1300             killed)
1301         (while dead-lists
1302           (setq killed (symbol-value (car dead-lists)))
1303           (while killed
1304             (unless (gnus-active (setq group (pop killed)))
1305               ;; The group is bogus.
1306               ;; !!!Slow as hell.
1307               (set (car dead-lists)
1308                    (delete group (symbol-value (car dead-lists))))))
1309           (setq dead-lists (cdr dead-lists))))
1310       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1311       (gnus-message 5 "Checking bogus newsgroups...done"))))
1312
1313 (defun gnus-check-duplicate-killed-groups ()
1314   "Remove duplicates from the list of killed groups."
1315   (interactive)
1316   (let ((killed gnus-killed-list))
1317     (while killed
1318       (gnus-message 9 "%d" (length killed))
1319       (setcdr killed (delete (car killed) (cdr killed)))
1320       (setq killed (cdr killed)))))
1321
1322 ;; We want to inline a function from gnus-cache, so we cheat here:
1323 (eval-when-compile
1324   (defvar gnus-cache-active-hashtb)
1325   (defun gnus-cache-possibly-alter-active (group active)
1326     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1327     (when gnus-cache-active-hashtb
1328       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1329         (when cache-active
1330           (when (< (car cache-active) (car active))
1331             (setcar active (car cache-active)))
1332           (when (> (cdr cache-active) (cdr active))
1333             (setcdr active (cdr cache-active))))))))
1334
1335 (defun gnus-activate-group (group &optional scan dont-check method)
1336   ;; Check whether a group has been activated or not.
1337   ;; If SCAN, request a scan of that group as well.
1338   (let ((method (or method (inline (gnus-find-method-for-group group))))
1339         active)
1340     (and (inline (gnus-check-server method))
1341          ;; We escape all bugs and quit here to make it possible to
1342          ;; continue if a group is so out-there that it reports bugs
1343          ;; and stuff.
1344          (progn
1345            (and scan
1346                 (gnus-check-backend-function 'request-scan (car method))
1347                 (gnus-request-scan group method))
1348            t)
1349          (condition-case ()
1350              (inline (gnus-request-group group dont-check method))
1351            (error nil)
1352            (quit nil))
1353          (setq active (gnus-parse-active))
1354          ;; If there are no articles in the group, the GROUP
1355          ;; command may have responded with the `(0 . 0)'.  We
1356          ;; ignore this if we already have an active entry
1357          ;; for the group.
1358          (if (and (zerop (car active))
1359                   (zerop (cdr active))
1360                   (gnus-active group))
1361              (gnus-active group)
1362            (gnus-set-active group active)
1363            ;; Return the new active info.
1364            active))))
1365
1366 (defun gnus-get-unread-articles-in-group (info active &optional update)
1367   (when active
1368     ;; Allow the backend to update the info in the group.
1369     (when (and update
1370                (gnus-request-update-info
1371                 info (inline (gnus-find-method-for-group
1372                               (gnus-info-group info)))))
1373       (gnus-activate-group (gnus-info-group info) nil t))
1374     (let* ((range (gnus-info-read info))
1375            (num 0))
1376       ;; If a cache is present, we may have to alter the active info.
1377       (when (and gnus-use-cache info)
1378         (inline (gnus-cache-possibly-alter-active
1379                  (gnus-info-group info) active)))
1380       ;; Modify the list of read articles according to what articles
1381       ;; are available; then tally the unread articles and add the
1382       ;; number to the group hash table entry.
1383       (cond
1384        ((zerop (cdr active))
1385         (setq num 0))
1386        ((not range)
1387         (setq num (- (1+ (cdr active)) (car active))))
1388        ((not (listp (cdr range)))
1389         ;; Fix a single (num . num) range according to the
1390         ;; active hash table.
1391         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1392         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1393         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1394         ;; Compute number of unread articles.
1395         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1396        (t
1397         ;; The read list is a list of ranges.  Fix them according to
1398         ;; the active hash table.
1399         ;; First peel off any elements that are below the lower
1400         ;; active limit.
1401         (while (and (cdr range)
1402                     (>= (car active)
1403                         (or (and (atom (cadr range)) (cadr range))
1404                             (caadr range))))
1405           (if (numberp (car range))
1406               (setcar range
1407                       (cons (car range)
1408                             (or (and (numberp (cadr range))
1409                                      (cadr range))
1410                                 (cdadr range))))
1411             (setcdr (car range)
1412                     (or (and (numberp (nth 1 range)) (nth 1 range))
1413                         (cdadr range))))
1414           (setcdr range (cddr range)))
1415         ;; Adjust the first element to be the same as the lower limit.
1416         (when (and (not (atom (car range)))
1417                    (< (cdar range) (car active)))
1418           (setcdr (car range) (1- (car active))))
1419         ;; Then we want to peel off any elements that are higher
1420         ;; than the upper active limit.
1421         (let ((srange range))
1422           ;; Go past all legal elements.
1423           (while (and (cdr srange)
1424                       (<= (or (and (atom (cadr srange))
1425                                    (cadr srange))
1426                               (caadr srange))
1427                           (cdr active)))
1428             (setq srange (cdr srange)))
1429           (when (cdr srange)
1430             ;; Nuke all remaining illegal elements.
1431             (setcdr srange nil))
1432
1433           ;; Adjust the final element.
1434           (when (and (not (atom (car srange)))
1435                      (> (cdar srange) (cdr active)))
1436             (setcdr (car srange) (cdr active))))
1437         ;; Compute the number of unread articles.
1438         (while range
1439           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1440                                       (cdar range)))
1441                               (or (and (atom (car range)) (car range))
1442                                   (caar range)))))
1443           (setq range (cdr range)))
1444         (setq num (max 0 (- (cdr active) num)))))
1445       ;; Set the number of unread articles.
1446       (when info
1447         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1448       num)))
1449
1450 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1451 ;; and compute how many unread articles there are in each group.
1452 (defun gnus-get-unread-articles (&optional level)
1453   (let* ((newsrc (cdr gnus-newsrc-alist))
1454          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1455          (foreign-level
1456           (min
1457            (cond ((and gnus-activate-foreign-newsgroups
1458                        (not (numberp gnus-activate-foreign-newsgroups)))
1459                   (1+ gnus-level-subscribed))
1460                  ((numberp gnus-activate-foreign-newsgroups)
1461                   gnus-activate-foreign-newsgroups)
1462                  (t 0))
1463            level))
1464          info group active method)
1465     (gnus-message 5 "Checking new news...")
1466
1467     (while newsrc
1468       (setq active (gnus-active (setq group (gnus-info-group
1469                                              (setq info (pop newsrc))))))
1470
1471       ;; Check newsgroups.  If the user doesn't want to check them, or
1472       ;; they can't be checked (for instance, if the news server can't
1473       ;; be reached) we just set the number of unread articles in this
1474       ;; newsgroup to t.  This means that Gnus thinks that there are
1475       ;; unread articles, but it has no idea how many.
1476       (if (and (setq method (gnus-info-method info))
1477                (not (inline
1478                       (gnus-server-equal
1479                        gnus-select-method
1480                        (setq method (gnus-server-get-method nil method)))))
1481                (not (gnus-secondary-method-p method)))
1482           ;; These groups are foreign.  Check the level.
1483           (when (<= (gnus-info-level info) foreign-level)
1484             (setq active (gnus-activate-group group 'scan))
1485             (unless (inline (gnus-virtual-group-p group))
1486               (inline (gnus-close-group group)))
1487             (when (fboundp (intern (concat (symbol-name (car method))
1488                                            "-request-update-info")))
1489               (inline (gnus-request-update-info info method))))
1490         ;; These groups are native or secondary.
1491         (when (and (<= (gnus-info-level info) level)
1492                    (not gnus-read-active-file))
1493           (setq active (gnus-activate-group group 'scan))
1494           (inline (gnus-close-group group))))
1495
1496       ;; Get the number of unread articles in the group.
1497       (if active
1498           (inline (gnus-get-unread-articles-in-group info active t))
1499         ;; The group couldn't be reached, so we nix out the number of
1500         ;; unread articles and stuff.
1501         (gnus-set-active group nil)
1502         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
1503
1504     (gnus-message 5 "Checking new news...done")))
1505
1506 ;; Create a hash table out of the newsrc alist.  The `car's of the
1507 ;; alist elements are used as keys.
1508 (defun gnus-make-hashtable-from-newsrc-alist ()
1509   (let ((alist gnus-newsrc-alist)
1510         (ohashtb gnus-newsrc-hashtb)
1511         prev)
1512     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1513     (setq alist
1514           (setq prev (setq gnus-newsrc-alist
1515                            (if (equal (caar gnus-newsrc-alist)
1516                                       "dummy.group")
1517                                gnus-newsrc-alist
1518                              (cons (list "dummy.group" 0 nil) alist)))))
1519     (while alist
1520       (gnus-sethash
1521        (caar alist)
1522        ;; Preserve number of unread articles in groups.
1523        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1524              prev)
1525        gnus-newsrc-hashtb)
1526       (setq prev alist
1527             alist (cdr alist)))))
1528
1529 (defun gnus-make-hashtable-from-killed ()
1530   "Create a hash table from the killed and zombie lists."
1531   (let ((lists '(gnus-killed-list gnus-zombie-list))
1532         list)
1533     (setq gnus-killed-hashtb
1534           (gnus-make-hashtable
1535            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1536     (while lists
1537       (setq list (symbol-value (pop lists)))
1538       (while list
1539         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1540
1541 (defun gnus-parse-active ()
1542   "Parse active info in the nntp server buffer."
1543   (save-excursion
1544     (set-buffer nntp-server-buffer)
1545     (goto-char (point-min))
1546     ;; Parse the result we got from `gnus-request-group'.
1547     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1548       (goto-char (match-beginning 1))
1549       (cons (read (current-buffer))
1550             (read (current-buffer))))))
1551
1552 (defun gnus-make-articles-unread (group articles)
1553   "Mark ARTICLES in GROUP as unread."
1554   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1555                           (gnus-gethash (gnus-group-real-name group)
1556                                         gnus-newsrc-hashtb))))
1557          (ranges (gnus-info-read info))
1558          news article)
1559     (while articles
1560       (when (gnus-member-of-range
1561              (setq article (pop articles)) ranges)
1562         (push article news)))
1563     (when news
1564       (gnus-info-set-read
1565        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1566       (gnus-group-update-group group t))))
1567
1568 ;; Enter all dead groups into the hashtb.
1569 (defun gnus-update-active-hashtb-from-killed ()
1570   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1571         (lists (list gnus-killed-list gnus-zombie-list))
1572         killed)
1573     (while lists
1574       (setq killed (car lists))
1575       (while killed
1576         (gnus-sethash (car killed) nil hashtb)
1577         (setq killed (cdr killed)))
1578       (setq lists (cdr lists)))))
1579
1580 (defun gnus-get-killed-groups ()
1581   "Go through the active hashtb and mark all unknown groups as killed."
1582   ;; First make sure active file has been read.
1583   (unless (gnus-read-active-file-p)
1584     (let ((gnus-read-active-file t))
1585       (gnus-read-active-file)))
1586   (unless gnus-killed-hashtb
1587     (gnus-make-hashtable-from-killed))
1588   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1589   (mapatoms
1590    (lambda (sym)
1591      (let ((groups 0)
1592            (group (symbol-name sym)))
1593        (if (or (null group)
1594                (gnus-gethash group gnus-killed-hashtb)
1595                (gnus-gethash group gnus-newsrc-hashtb))
1596            ()
1597          (let ((do-sub (gnus-matches-options-n group)))
1598            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1599                ()
1600              (setq groups (1+ groups))
1601              (push group gnus-killed-list)
1602              (gnus-sethash group group gnus-killed-hashtb))))))
1603    gnus-active-hashtb)
1604   (gnus-dribble-touch))
1605
1606 ;; Get the active file(s) from the backend(s).
1607 (defun gnus-read-active-file (&optional force not-native)
1608   (gnus-group-set-mode-line)
1609   (let ((methods
1610          (append
1611           (if (and (not not-native)
1612                    (gnus-check-server gnus-select-method))
1613               ;; The native server is available.
1614               (cons gnus-select-method gnus-secondary-select-methods)
1615             ;; The native server is down, so we just do the
1616             ;; secondary ones.
1617             gnus-secondary-select-methods)
1618           ;; Also read from the archive server.
1619           (when (gnus-archive-server-wanted-p)
1620             (list "archive"))))
1621         list-type)
1622     (setq gnus-have-read-active-file nil)
1623     (save-excursion
1624       (set-buffer nntp-server-buffer)
1625       (while methods
1626         (let* ((method (if (stringp (car methods))
1627                            (gnus-server-get-method nil (car methods))
1628                          (car methods)))
1629                (where (nth 1 method))
1630                (mesg (format "Reading active file%s via %s..."
1631                              (if (and where (not (zerop (length where))))
1632                                  (concat " from " where) "")
1633                              (car method))))
1634           (gnus-message 5 mesg)
1635           (when (gnus-check-server method)
1636             ;; Request that the backend scan its incoming messages.
1637             (when (gnus-check-backend-function 'request-scan (car method))
1638               (gnus-request-scan nil method))
1639             (cond
1640              ((and (eq gnus-read-active-file 'some)
1641                    (gnus-check-backend-function 'retrieve-groups (car method))
1642                    (not force))
1643               (let ((newsrc (cdr gnus-newsrc-alist))
1644                     (gmethod (gnus-server-get-method nil method))
1645                     groups info)
1646                 (while (setq info (pop newsrc))
1647                   (when (inline
1648                           (gnus-server-equal
1649                            (inline
1650                              (gnus-find-method-for-group
1651                               (gnus-info-group info) info))
1652                            gmethod))
1653                     (push (gnus-group-real-name (gnus-info-group info))
1654                           groups)))
1655                 (when groups
1656                   (gnus-check-server method)
1657                   (setq list-type (gnus-retrieve-groups groups method))
1658                   (cond
1659                    ((not list-type)
1660                     (gnus-error
1661                      1.2 "Cannot read partial active file from %s server."
1662                      (car method)))
1663                    ((eq list-type 'active)
1664                     (gnus-active-to-gnus-format
1665                      method gnus-active-hashtb nil t))
1666                    (t
1667                     (gnus-groups-to-gnus-format
1668                      method gnus-active-hashtb t))))))
1669              ((null method)
1670               t)
1671              (t
1672               (if (not (gnus-request-list method))
1673                   (unless (equal method gnus-message-archive-method)
1674                     (gnus-error 1 "Cannot read active file from %s server"
1675                                 (car method)))
1676                 (gnus-message 5 mesg)
1677                 (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1678                 ;; We mark this active file as read.
1679                 (push method gnus-have-read-active-file)
1680                 (gnus-message 5 "%sdone" mesg))))))
1681         (setq methods (cdr methods))))))
1682
1683
1684 (defun gnus-ignored-newsgroups-has-to-p ()
1685   "T only when gnus-ignored-newsgroups includes \"^to\\\\.\" as an element."
1686   ;; note this regexp is the same as:
1687   ;; (concat (regexp-quote "^to\\.") "\\($\\|" (regexp-quote "\\|") "\\)")
1688   (string-match "\\^to\\\\\\.\\($\\|\\\\|\\)"
1689                 gnus-ignored-newsgroups))
1690
1691 ;; Read an active file and place the results in `gnus-active-hashtb'.
1692 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1693                                              real-active)
1694   (unless method
1695     (setq method gnus-select-method))
1696   (let ((cur (current-buffer))
1697         (hashtb (or hashtb
1698                     (if (and gnus-active-hashtb
1699                              (not (equal method gnus-select-method)))
1700                         gnus-active-hashtb
1701                       (setq gnus-active-hashtb
1702                             (if (equal method gnus-select-method)
1703                                 (gnus-make-hashtable
1704                                  (count-lines (point-min) (point-max)))
1705                               (gnus-make-hashtable 4096)))))))
1706     ;; Delete unnecessary lines.
1707     (goto-char (point-min))
1708     (cond ((gnus-ignored-newsgroups-has-to-p)
1709            (delete-matching-lines gnus-ignored-newsgroups))
1710           ((string= gnus-ignored-newsgroups "")
1711            (delete-matching-lines "^to\\."))
1712           (t
1713            (delete-matching-lines (concat "^to\\.\\|"
1714                                           gnus-ignored-newsgroups))))
1715
1716     ;; Make the group names readable as a lisp expression even if they
1717     ;; contain special characters.
1718     (goto-char (point-max))
1719     (while (re-search-backward "[][';?()#]" nil t)
1720       (insert ?\\))
1721
1722     ;; Let the Gnus agent save the active file.
1723     (when (and gnus-agent real-active)
1724       (gnus-agent-save-active method))
1725
1726     ;; If these are groups from a foreign select method, we insert the
1727     ;; group prefix in front of the group names.
1728     (when (not (gnus-server-equal
1729                 (gnus-server-get-method nil method)
1730                 (gnus-server-get-method nil gnus-select-method)))
1731       (let ((prefix (gnus-group-prefixed-name "" method)))
1732         (goto-char (point-min))
1733         (while (and (not (eobp))
1734                     (progn (insert prefix)
1735                            (zerop (forward-line 1)))))))
1736     ;; Store the active file in a hash table.
1737     (goto-char (point-min))
1738     (let (group max min)
1739       (while (not (eobp))
1740         (condition-case ()
1741             (progn
1742               (narrow-to-region (point) (gnus-point-at-eol))
1743               ;; group gets set to a symbol interned in the hash table
1744               ;; (what a hack!!) - jwz
1745               (setq group (let ((obarray hashtb)) (read cur)))
1746               (if (and (numberp (setq max (read cur)))
1747                        (numberp (setq min (read cur)))
1748                        (progn
1749                          (skip-chars-forward " \t")
1750                          (not
1751                           (or (= (following-char) ?=)
1752                               (= (following-char) ?x)
1753                               (= (following-char) ?j)))))
1754                   (progn
1755                     (set group (cons min max))
1756                     ;; if group is moderated, stick in moderation table
1757                     (when (= (following-char) ?m)
1758                       (unless gnus-moderated-hashtb
1759                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
1760                       (gnus-sethash (symbol-name group) t
1761                                     gnus-moderated-hashtb)))
1762                 (set group nil)))
1763           (error
1764            (and group
1765                 (symbolp group)
1766                 (set group nil))
1767            (unless ignore-errors
1768              (gnus-message 3 "Warning - illegal active: %s"
1769                            (buffer-substring
1770                             (gnus-point-at-bol) (gnus-point-at-eol))))))
1771         (widen)
1772         (forward-line 1)))))
1773
1774 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
1775   ;; Parse a "groups" active file.
1776   (let ((cur (current-buffer))
1777         (hashtb (or hashtb
1778                     (if (and method gnus-active-hashtb)
1779                         gnus-active-hashtb
1780                       (setq gnus-active-hashtb
1781                             (gnus-make-hashtable
1782                              (count-lines (point-min) (point-max)))))))
1783         (prefix (and method
1784                      (not (gnus-server-equal
1785                            (gnus-server-get-method nil method)
1786                            (gnus-server-get-method nil gnus-select-method)))
1787                      (gnus-group-prefixed-name "" method))))
1788
1789     ;; Let the Gnus agent save the active file.
1790     (when (and gnus-agent real-active)
1791       (gnus-agent-save-groups method))
1792     
1793     (goto-char (point-min))
1794     ;; We split this into to separate loops, one with the prefix
1795     ;; and one without to speed the reading up somewhat.
1796     (if prefix
1797         (let (min max opoint group)
1798           (while (not (eobp))
1799             (condition-case ()
1800                 (progn
1801                   (read cur) (read cur)
1802                   (setq min (read cur)
1803                         max (read cur)
1804                         opoint (point))
1805                   (skip-chars-forward " \t")
1806                   (insert prefix)
1807                   (goto-char opoint)
1808                   (set (let ((obarray hashtb)) (read cur))
1809                        (cons min max)))
1810               (error (and group (symbolp group) (set group nil))))
1811             (forward-line 1)))
1812       (let (min max group)
1813         (while (not (eobp))
1814           (condition-case ()
1815               (when (= (following-char) ?2)
1816                 (read cur) (read cur)
1817                 (setq min (read cur)
1818                       max (read cur))
1819                 (set (setq group (let ((obarray hashtb)) (read cur)))
1820                      (cons min max)))
1821             (error (and group (symbolp group) (set group nil))))
1822           (forward-line 1))))))
1823
1824 (defun gnus-read-newsrc-file (&optional force)
1825   "Read startup file.
1826 If FORCE is non-nil, the .newsrc file is read."
1827   ;; Reset variables that might be defined in the .newsrc.eld file.
1828   (let ((variables gnus-variable-list))
1829     (while variables
1830       (set (car variables) nil)
1831       (setq variables (cdr variables))))
1832   (let* ((newsrc-file gnus-current-startup-file)
1833          (quick-file (concat newsrc-file ".el")))
1834     (save-excursion
1835       ;; We always load the .newsrc.eld file.  If always contains
1836       ;; much information that can not be gotten from the .newsrc
1837       ;; file (ticked articles, killed groups, foreign methods, etc.)
1838       (gnus-read-newsrc-el-file quick-file)
1839
1840       (when (and (file-exists-p gnus-current-startup-file)
1841                  (or force
1842                      (and (file-newer-than-file-p newsrc-file quick-file)
1843                           (file-newer-than-file-p newsrc-file
1844                                                   (concat quick-file "d")))
1845                      (not gnus-newsrc-alist)))
1846         ;; We read the .newsrc file.  Note that if there if a
1847         ;; .newsrc.eld file exists, it has already been read, and
1848         ;; the `gnus-newsrc-hashtb' has been created.  While reading
1849         ;; the .newsrc file, Gnus will only use the information it
1850         ;; can find there for changing the data already read -
1851         ;; i. e., reading the .newsrc file will not trash the data
1852         ;; already read (except for read articles).
1853         (save-excursion
1854           (gnus-message 5 "Reading %s..." newsrc-file)
1855           (set-buffer (nnheader-find-file-noselect newsrc-file))
1856           (buffer-disable-undo (current-buffer))
1857           (gnus-newsrc-to-gnus-format)
1858           (kill-buffer (current-buffer))
1859           (gnus-message 5 "Reading %s...done" newsrc-file)))
1860
1861       ;; Convert old to new.
1862       (gnus-convert-old-newsrc))))
1863
1864 (defun gnus-convert-old-newsrc ()
1865   "Convert old newsrc into the new format, if needed."
1866   (let ((fcv (and gnus-newsrc-file-version
1867                   (gnus-continuum-version gnus-newsrc-file-version))))
1868     (cond
1869      ;; No .newsrc.eld file was loaded.
1870      ((null fcv) nil)
1871      ;; Gnus 5 .newsrc.eld was loaded.
1872      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
1873       (gnus-convert-old-ticks)))))
1874
1875 (defun gnus-convert-old-ticks ()
1876   (let ((newsrc (cdr gnus-newsrc-alist))
1877         marks info dormant ticked)
1878     (while (setq info (pop newsrc))
1879       (when (setq marks (gnus-info-marks info))
1880         (setq dormant (cdr (assq 'dormant marks))
1881               ticked (cdr (assq 'tick marks)))
1882         (when (or dormant ticked)
1883           (gnus-info-set-read
1884            info
1885            (gnus-add-to-range
1886             (gnus-info-read info)
1887             (nconc (gnus-uncompress-range dormant)
1888                    (gnus-uncompress-range ticked)))))))))
1889
1890 (defun gnus-read-newsrc-el-file (file)
1891   (let ((ding-file (concat file "d")))
1892     ;; We always, always read the .eld file.
1893     (gnus-message 5 "Reading %s..." ding-file)
1894     (let (gnus-newsrc-assoc)
1895       (condition-case nil
1896           (load ding-file t t t)
1897         (error
1898          (ding)
1899          (unless (gnus-yes-or-no-p
1900                   (format "Error in %s; continue? " ding-file))
1901            (error "Error in %s" ding-file))))
1902       (when gnus-newsrc-assoc
1903         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
1904     (gnus-make-hashtable-from-newsrc-alist)
1905     (when (file-newer-than-file-p file ding-file)
1906       ;; Old format quick file
1907       (gnus-message 5 "Reading %s..." file)
1908       ;; The .el file is newer than the .eld file, so we read that one
1909       ;; as well.
1910       (gnus-read-old-newsrc-el-file file))))
1911
1912 ;; Parse the old-style quick startup file
1913 (defun gnus-read-old-newsrc-el-file (file)
1914   (let (newsrc killed marked group m info)
1915     (prog1
1916         (let ((gnus-killed-assoc nil)
1917               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
1918           (prog1
1919               (ignore-errors
1920                 (load file t t t))
1921             (setq newsrc gnus-newsrc-assoc
1922                   killed gnus-killed-assoc
1923                   marked gnus-marked-assoc)))
1924       (setq gnus-newsrc-alist nil)
1925       (while (setq group (pop newsrc))
1926         (if (setq info (gnus-get-info (car group)))
1927             (progn
1928               (gnus-info-set-read info (cddr group))
1929               (gnus-info-set-level
1930                info (if (nth 1 group) gnus-level-default-subscribed
1931                       gnus-level-default-unsubscribed))
1932               (push info gnus-newsrc-alist))
1933           (push (setq info
1934                       (list (car group)
1935                             (if (nth 1 group) gnus-level-default-subscribed
1936                               gnus-level-default-unsubscribed)
1937                             (cddr group)))
1938                 gnus-newsrc-alist))
1939         ;; Copy marks into info.
1940         (when (setq m (assoc (car group) marked))
1941           (unless (nthcdr 3 info)
1942             (nconc info (list nil)))
1943           (gnus-info-set-marks
1944            info (list (cons 'tick (gnus-compress-sequence
1945                                    (sort (cdr m) '<) t))))))
1946       (setq newsrc killed)
1947       (while newsrc
1948         (setcar newsrc (caar newsrc))
1949         (setq newsrc (cdr newsrc)))
1950       (setq gnus-killed-list killed))
1951     ;; The .el file version of this variable does not begin with
1952     ;; "options", while the .eld version does, so we just add it if it
1953     ;; isn't there.
1954     (when
1955         gnus-newsrc-options
1956       (when (not (string-match "^ *options" gnus-newsrc-options))
1957         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
1958       (when (not (string-match "\n$" gnus-newsrc-options))
1959         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
1960       ;; Finally, if we read some options lines, we parse them.
1961       (unless (string= gnus-newsrc-options "")
1962         (gnus-newsrc-parse-options gnus-newsrc-options)))
1963
1964     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
1965     (gnus-make-hashtable-from-newsrc-alist)))
1966
1967 (defun gnus-make-newsrc-file (file)
1968   "Make server dependent file name by catenating FILE and server host name."
1969   (let* ((file (expand-file-name file nil))
1970          (real-file (concat file "-" (nth 1 gnus-select-method))))
1971     (if (or (file-exists-p real-file)
1972             (file-exists-p (concat real-file ".el"))
1973             (file-exists-p (concat real-file ".eld")))
1974         real-file file)))
1975
1976 (defun gnus-newsrc-to-gnus-format ()
1977   (setq gnus-newsrc-options "")
1978   (setq gnus-newsrc-options-n nil)
1979
1980   (unless gnus-active-hashtb
1981     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1982   (let ((buf (current-buffer))
1983         (already-read (> (length gnus-newsrc-alist) 1))
1984         group subscribed options-symbol newsrc Options-symbol
1985         symbol reads num1)
1986     (goto-char (point-min))
1987     ;; We intern the symbol `options' in the active hashtb so that we
1988     ;; can `eq' against it later.
1989     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
1990     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
1991
1992     (while (not (eobp))
1993       ;; We first read the first word on the line by narrowing and
1994       ;; then reading into `gnus-active-hashtb'.  Most groups will
1995       ;; already exist in that hashtb, so this will save some string
1996       ;; space.
1997       (narrow-to-region
1998        (point)
1999        (progn (skip-chars-forward "^ \t!:\n") (point)))
2000       (goto-char (point-min))
2001       (setq symbol
2002             (and (/= (point-min) (point-max))
2003                  (let ((obarray gnus-active-hashtb)) (read buf))))
2004       (widen)
2005       ;; Now, the symbol we have read is either `options' or a group
2006       ;; name.  If it is an options line, we just add it to a string.
2007       (cond
2008        ((or (eq symbol options-symbol)
2009             (eq symbol Options-symbol))
2010         (setq gnus-newsrc-options
2011               ;; This concating is quite inefficient, but since our
2012               ;; thorough studies show that approx 99.37% of all
2013               ;; .newsrc files only contain a single options line, we
2014               ;; don't give a damn, frankly, my dear.
2015               (concat gnus-newsrc-options
2016                       (buffer-substring
2017                        (gnus-point-at-bol)
2018                        ;; Options may continue on the next line.
2019                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2020                                 (progn (beginning-of-line) (point)))
2021                            (point)))))
2022         (forward-line -1))
2023        (symbol
2024         ;; Group names can be just numbers.
2025         (when (numberp symbol)
2026           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2027         (unless (boundp symbol)
2028           (set symbol nil))
2029         ;; It was a group name.
2030         (setq subscribed (= (following-char) ?:)
2031               group (symbol-name symbol)
2032               reads nil)
2033         (if (eolp)
2034             ;; If the line ends here, this is clearly a buggy line, so
2035             ;; we put point a the beginning of line and let the cond
2036             ;; below do the error handling.
2037             (beginning-of-line)
2038           ;; We skip to the beginning of the ranges.
2039           (skip-chars-forward "!: \t"))
2040         ;; We are now at the beginning of the list of read articles.
2041         ;; We read them range by range.
2042         (while
2043             (cond
2044              ((looking-at "[0-9]+")
2045               ;; We narrow and read a number instead of buffer-substring/
2046               ;; string-to-int because it's faster.  narrow/widen is
2047               ;; faster than save-restriction/narrow, and save-restriction
2048               ;; produces a garbage object.
2049               (setq num1 (progn
2050                            (narrow-to-region (match-beginning 0) (match-end 0))
2051                            (read buf)))
2052               (widen)
2053               ;; If the next character is a dash, then this is a range.
2054               (if (= (following-char) ?-)
2055                   (progn
2056                     ;; We read the upper bound of the range.
2057                     (forward-char 1)
2058                     (if (not (looking-at "[0-9]+"))
2059                         ;; This is a buggy line, by we pretend that
2060                         ;; it's kinda OK.  Perhaps the user should be
2061                         ;; dinged?
2062                         (push num1 reads)
2063                       (push
2064                        (cons num1
2065                              (progn
2066                                (narrow-to-region (match-beginning 0)
2067                                                  (match-end 0))
2068                                (read buf)))
2069                        reads)
2070                       (widen)))
2071                 ;; It was just a simple number, so we add it to the
2072                 ;; list of ranges.
2073                 (push num1 reads))
2074               ;; If the next char in ?\n, then we have reached the end
2075               ;; of the line and return nil.
2076               (/= (following-char) ?\n))
2077              ((= (following-char) ?\n)
2078               ;; End of line, so we end.
2079               nil)
2080              (t
2081               ;; Not numbers and not eol, so this might be a buggy
2082               ;; line...
2083               (unless (eobp)
2084                 ;; If it was eob instead of ?\n, we allow it.
2085                 ;; The line was buggy.
2086                 (setq group nil)
2087                 (gnus-error 3.1 "Mangled line: %s"
2088                             (buffer-substring (gnus-point-at-bol)
2089                                               (gnus-point-at-eol))))
2090               nil))
2091           ;; Skip past ", ".  Spaces are illegal in these ranges, but
2092           ;; we allow them, because it's a common mistake to put a
2093           ;; space after the comma.
2094           (skip-chars-forward ", "))
2095
2096         ;; We have already read .newsrc.eld, so we gently update the
2097         ;; data in the hash table with the information we have just
2098         ;; read.
2099         (when group
2100           (let ((info (gnus-get-info group))
2101                 level)
2102             (if info
2103                 ;; There is an entry for this file in the alist.
2104                 (progn
2105                   (gnus-info-set-read info (nreverse reads))
2106                   ;; We update the level very gently.  In fact, we
2107                   ;; only change it if there's been a status change
2108                   ;; from subscribed to unsubscribed, or vice versa.
2109                   (setq level (gnus-info-level info))
2110                   (cond ((and (<= level gnus-level-subscribed)
2111                               (not subscribed))
2112                          (setq level (if reads
2113                                          gnus-level-default-unsubscribed
2114                                        (1+ gnus-level-default-unsubscribed))))
2115                         ((and (> level gnus-level-subscribed) subscribed)
2116                          (setq level gnus-level-default-subscribed)))
2117                   (gnus-info-set-level info level))
2118               ;; This is a new group.
2119               (setq info (list group
2120                                (if subscribed
2121                                    gnus-level-default-subscribed
2122                                  (if reads
2123                                      (1+ gnus-level-subscribed)
2124                                    gnus-level-default-unsubscribed))
2125                                (nreverse reads))))
2126             (push info newsrc)))))
2127       (forward-line 1))
2128
2129     (setq newsrc (nreverse newsrc))
2130
2131     (if (not already-read)
2132         ()
2133       ;; We now have two newsrc lists - `newsrc', which is what we
2134       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2135       ;; what we've read from .newsrc.eld.  We have to merge these
2136       ;; lists.  We do this by "attaching" any (foreign) groups in the
2137       ;; gnus-newsrc-alist to the (native) group that precedes them.
2138       (let ((rc (cdr gnus-newsrc-alist))
2139             (prev gnus-newsrc-alist)
2140             entry mentry)
2141         (while rc
2142           (or (null (nth 4 (car rc)))   ; It's a native group.
2143               (assoc (caar rc) newsrc)  ; It's already in the alist.
2144               (if (setq entry (assoc (caar prev) newsrc))
2145                   (setcdr (setq mentry (memq entry newsrc))
2146                           (cons (car rc) (cdr mentry)))
2147                 (push (car rc) newsrc)))
2148           (setq prev rc
2149                 rc (cdr rc)))))
2150
2151     (setq gnus-newsrc-alist newsrc)
2152     ;; We make the newsrc hashtb.
2153     (gnus-make-hashtable-from-newsrc-alist)
2154
2155     ;; Finally, if we read some options lines, we parse them.
2156     (unless (string= gnus-newsrc-options "")
2157       (gnus-newsrc-parse-options gnus-newsrc-options))))
2158
2159 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2160 ;; The return value will be a list on the form
2161 ;; ((regexp1 . ignore)
2162 ;;  (regexp2 . subscribe)...)
2163 ;; When handling new newsgroups, groups that match a `ignore' regexp
2164 ;; will be ignored, and groups that match a `subscribe' regexp will be
2165 ;; subscribed.  A line like
2166 ;; options -n !all rec.all
2167 ;; will lead to a list that looks like
2168 ;; (("^rec\\..+" . subscribe)
2169 ;;  ("^.+" . ignore))
2170 ;; So all "rec.*" groups will be subscribed, while all the other
2171 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2172 ;; different from "options -n rec.all !all".
2173 (defun gnus-newsrc-parse-options (options)
2174   (let (out eol)
2175     (save-excursion
2176       (gnus-set-work-buffer)
2177       (insert (regexp-quote options))
2178       ;; First we treat all continuation lines.
2179       (goto-char (point-min))
2180       (while (re-search-forward "\n[ \t]+" nil t)
2181         (replace-match " " t t))
2182       ;; Then we transform all "all"s into ".+"s.
2183       (goto-char (point-min))
2184       (while (re-search-forward "\\ball\\b" nil t)
2185         (replace-match ".+" t t))
2186       (goto-char (point-min))
2187       ;; We remove all other options than the "-n" ones.
2188       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2189         (replace-match " ")
2190         (forward-char -1))
2191       (goto-char (point-min))
2192
2193       ;; We are only interested in "options -n" lines - we
2194       ;; ignore the other option lines.
2195       (while (re-search-forward "[ \t]-n" nil t)
2196         (setq eol
2197               (or (save-excursion
2198                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2199                          (- (point) 2)))
2200                   (gnus-point-at-eol)))
2201         ;; Search for all "words"...
2202         (while (re-search-forward "[^ \t,\n]+" eol t)
2203           (if (= (char-after (match-beginning 0)) ?!)
2204               ;; If the word begins with a bang (!), this is a "not"
2205               ;; spec.  We put this spec (minus the bang) and the
2206               ;; symbol `ignore' into the list.
2207               (push (cons (concat
2208                            "^" (buffer-substring
2209                                 (1+ (match-beginning 0))
2210                                 (match-end 0))
2211                            "\\($\\|\\.\\)")
2212                           'ignore)
2213                     out)
2214             ;; There was no bang, so this is a "yes" spec.
2215             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2216                         'subscribe)
2217                   out))))
2218
2219       (setq gnus-newsrc-options-n out))))
2220
2221 (defun gnus-save-newsrc-file (&optional force)
2222   "Save .newsrc file."
2223   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2224   ;; from the variable gnus-newsrc-alist.
2225   (when (and (or gnus-newsrc-alist gnus-killed-list)
2226              gnus-current-startup-file)
2227     (save-excursion
2228       (if (and (or gnus-use-dribble-file gnus-slave)
2229                (not force)
2230                (or (not gnus-dribble-buffer)
2231                    (not (buffer-name gnus-dribble-buffer))
2232                    (zerop (save-excursion
2233                             (set-buffer gnus-dribble-buffer)
2234                             (buffer-size)))))
2235           (gnus-message 4 "(No changes need to be saved)")
2236         (gnus-run-hooks 'gnus-save-newsrc-hook)
2237         (if gnus-slave
2238             (gnus-slave-save-newsrc)
2239           ;; Save .newsrc.
2240           (when gnus-save-newsrc-file
2241             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2242             (gnus-gnus-to-newsrc-format)
2243             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2244           ;; Save .newsrc.eld.
2245           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
2246           (make-local-variable 'version-control)
2247           (setq version-control 'never)
2248           (setq buffer-file-name
2249                 (concat gnus-current-startup-file ".eld"))
2250           (setq default-directory (file-name-directory buffer-file-name))
2251           (gnus-add-current-to-buffer-list)
2252           (buffer-disable-undo (current-buffer))
2253           (erase-buffer)
2254           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2255           (gnus-gnus-to-quick-newsrc-format)
2256           (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2257           (save-buffer)
2258           (kill-buffer (current-buffer))
2259           (gnus-message
2260            5 "Saving %s.eld...done" gnus-current-startup-file))
2261         (gnus-dribble-delete-file)
2262         (gnus-group-set-mode-line)))))
2263
2264 (defun gnus-gnus-to-quick-newsrc-format ()
2265   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
2266   (let ((print-quoted t)
2267         (print-escape-newlines t))
2268     (insert ";; -*- emacs-lisp -*-\n")
2269     (insert ";; Gnus startup file.\n")
2270     (insert "\
2271 ;; Never delete this file -- if you want to force Gnus to read the
2272 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2273     (insert "(setq gnus-newsrc-file-version "
2274             (prin1-to-string gnus-version) ")\n")
2275     (let* ((gnus-killed-list
2276             (if (and gnus-save-killed-list
2277                      (stringp gnus-save-killed-list))
2278                 (gnus-strip-killed-list)
2279               gnus-killed-list))
2280            (variables
2281             (if gnus-save-killed-list gnus-variable-list
2282               ;; Remove the `gnus-killed-list' from the list of variables
2283               ;; to be saved, if required.
2284               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2285            ;; Peel off the "dummy" group.
2286            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2287            variable)
2288       ;; Insert the variables into the file.
2289       (while variables
2290         (when (and (boundp (setq variable (pop variables)))
2291                    (symbol-value variable))
2292           (insert "(setq " (symbol-name variable) " '")
2293           (gnus-prin1 (symbol-value variable))
2294           (insert ")\n"))))))
2295
2296 (defun gnus-strip-killed-list ()
2297   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2298   (let ((list gnus-killed-list)
2299         olist)
2300     (while list
2301       (when (string-match gnus-save-killed-list (car list))
2302         (push (car list) olist))
2303       (pop list))
2304     (nreverse olist)))
2305
2306 (defun gnus-gnus-to-newsrc-format ()
2307   ;; Generate and save the .newsrc file.
2308   (save-excursion
2309     (set-buffer (create-file-buffer gnus-current-startup-file))
2310     (let ((newsrc (cdr gnus-newsrc-alist))
2311           (standard-output (current-buffer))
2312           info ranges range method)
2313       (setq buffer-file-name gnus-current-startup-file)
2314       (setq default-directory (file-name-directory buffer-file-name))
2315       (buffer-disable-undo (current-buffer))
2316       (erase-buffer)
2317       ;; Write options.
2318       (when gnus-newsrc-options
2319         (insert gnus-newsrc-options))
2320       ;; Write subscribed and unsubscribed.
2321       (while (setq info (pop newsrc))
2322         ;; Don't write foreign groups to .newsrc.
2323         (when (or (null (setq method (gnus-info-method info)))
2324                   (equal method "native")
2325                   (inline (gnus-server-equal method gnus-select-method)))
2326           (insert (gnus-info-group info)
2327                   (if (> (gnus-info-level info) gnus-level-subscribed)
2328                       "!" ":"))
2329           (when (setq ranges (gnus-info-read info))
2330             (insert " ")
2331             (if (not (listp (cdr ranges)))
2332                 (if (= (car ranges) (cdr ranges))
2333                     (princ (car ranges))
2334                   (princ (car ranges))
2335                   (insert "-")
2336                   (princ (cdr ranges)))
2337               (while (setq range (pop ranges))
2338                 (if (or (atom range) (= (car range) (cdr range)))
2339                     (princ (or (and (atom range) range) (car range)))
2340                   (princ (car range))
2341                   (insert "-")
2342                   (princ (cdr range)))
2343                 (when ranges
2344                   (insert ",")))))
2345           (insert "\n")))
2346       (make-local-variable 'version-control)
2347       (setq version-control 'never)
2348       ;; It has been reported that sometime the modtime on the .newsrc
2349       ;; file seems to be off.  We really do want to overwrite it, so
2350       ;; we clear the modtime here before saving.  It's a bit odd,
2351       ;; though...
2352       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2353       ;; delete the silly thing entirely first.  but this fails to provide
2354       ;; such niceties as .newsrc~ creation.
2355       (if gnus-modtime-botch
2356           (delete-file gnus-startup-file)
2357         (clear-visited-file-modtime))
2358       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2359       (save-buffer)
2360       (kill-buffer (current-buffer)))))
2361
2362 \f
2363 ;;;
2364 ;;; Slave functions.
2365 ;;;
2366
2367 (defun gnus-slave-save-newsrc ()
2368   (save-excursion
2369     (set-buffer gnus-dribble-buffer)
2370     (let ((slave-name
2371            (make-temp-name (concat gnus-current-startup-file "-slave-")))
2372           (modes (ignore-errors
2373                    (file-modes (concat gnus-current-startup-file ".eld")))))
2374       (gnus-write-buffer slave-name)
2375       (when modes
2376         (set-file-modes slave-name modes)))))
2377
2378 (defun gnus-master-read-slave-newsrc ()
2379   (let ((slave-files
2380          (directory-files
2381           (file-name-directory gnus-current-startup-file)
2382           t (concat
2383              "^" (regexp-quote
2384                   (concat
2385                    (file-name-nondirectory gnus-current-startup-file)
2386                    "-slave-")))
2387           t))
2388         file)
2389     (if (not slave-files)
2390         ()                              ; There are no slave files to read.
2391       (gnus-message 7 "Reading slave newsrcs...")
2392       (save-excursion
2393         (set-buffer (get-buffer-create " *gnus slave*"))
2394         (buffer-disable-undo (current-buffer))
2395         (setq slave-files
2396               (sort (mapcar (lambda (file)
2397                               (list (nth 5 (file-attributes file)) file))
2398                             slave-files)
2399                     (lambda (f1 f2)
2400                       (or (< (caar f1) (caar f2))
2401                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2402         (while slave-files
2403           (erase-buffer)
2404           (setq file (nth 1 (car slave-files)))
2405           (insert-file-contents file)
2406           (when (condition-case ()
2407                     (progn
2408                       (eval-buffer (current-buffer))
2409                       t)
2410                   (error
2411                    (gnus-error 3.2 "Possible error in %s" file)
2412                    nil))
2413             (unless gnus-slave          ; Slaves shouldn't delete these files.
2414               (ignore-errors
2415                 (delete-file file))))
2416           (setq slave-files (cdr slave-files))))
2417       (gnus-dribble-touch)
2418       (gnus-message 7 "Reading slave newsrcs...done"))))
2419
2420 \f
2421 ;;;
2422 ;;; Group description.
2423 ;;;
2424
2425 (defun gnus-read-all-descriptions-files ()
2426   (let ((methods (cons gnus-select-method
2427                        (nconc
2428                         (when (gnus-archive-server-wanted-p)
2429                           (list "archive"))
2430                         gnus-secondary-select-methods))))
2431     (while methods
2432       (gnus-read-descriptions-file (car methods))
2433       (setq methods (cdr methods)))
2434     t))
2435
2436 (defun gnus-read-descriptions-file (&optional method)
2437   (let ((method (or method gnus-select-method))
2438         group)
2439     (when (stringp method)
2440       (setq method (gnus-server-to-method method)))
2441     ;; We create the hashtable whether we manage to read the desc file
2442     ;; to avoid trying to re-read after a failed read.
2443     (unless gnus-description-hashtb
2444       (setq gnus-description-hashtb
2445             (gnus-make-hashtable (length gnus-active-hashtb))))
2446     ;; Mark this method's desc file as read.
2447     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2448                   gnus-description-hashtb)
2449
2450     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2451     (cond
2452      ((not (gnus-check-server method))
2453       (gnus-message 1 "Couldn't open server")
2454       nil)
2455      ((not (gnus-request-list-newsgroups method))
2456       (gnus-message 1 "Couldn't read newsgroups descriptions")
2457       nil)
2458      (t
2459       (save-excursion
2460         (save-restriction
2461           (set-buffer nntp-server-buffer)
2462           (goto-char (point-min))
2463           (when (or (search-forward "\n.\n" nil t)
2464                     (goto-char (point-max)))
2465             (beginning-of-line)
2466             (narrow-to-region (point-min) (point)))
2467           ;; If these are groups from a foreign select method, we insert the
2468           ;; group prefix in front of the group names.
2469           (and method (not (inline
2470                              (gnus-server-equal
2471                               (gnus-server-get-method nil method)
2472                               (gnus-server-get-method
2473                                nil gnus-select-method))))
2474                (let ((prefix (gnus-group-prefixed-name "" method)))
2475                  (goto-char (point-min))
2476                  (while (and (not (eobp))
2477                              (progn (insert prefix)
2478                                     (zerop (forward-line 1)))))))
2479           (goto-char (point-min))
2480           (while (not (eobp))
2481             ;; If we get an error, we set group to 0, which is not a
2482             ;; symbol...
2483             (setq group
2484                   (condition-case ()
2485                       (let ((obarray gnus-description-hashtb))
2486                         ;; Group is set to a symbol interned in this
2487                         ;; hash table.
2488                         (read nntp-server-buffer))
2489                     (error 0)))
2490             (skip-chars-forward " \t")
2491             ;; ...  which leads to this line being effectively ignored.
2492             (when (symbolp group)
2493               (let ((str (buffer-substring
2494                           (point) (progn (end-of-line) (point))))
2495                     (coding
2496                      (and (boundp 'enable-multibyte-characters)
2497                           enable-multibyte-characters
2498                           (fboundp 'gnus-mule-get-coding-system)
2499                           (gnus-mule-get-coding-system (symbol-name group)))))
2500                 (if coding
2501                     (setq str (gnus-decode-coding-string str (car coding))))
2502                 (set group str)))
2503             (forward-line 1))))
2504       (gnus-message 5 "Reading descriptions file...done")
2505       t))))
2506
2507 (defun gnus-group-get-description (group)
2508   "Get the description of a group by sending XGTITLE to the server."
2509   (when (gnus-request-group-description group)
2510     (save-excursion
2511       (set-buffer nntp-server-buffer)
2512       (goto-char (point-min))
2513       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
2514         (match-string 1)))))
2515
2516 ;;;###autoload
2517 (defun gnus-declare-backend (name &rest abilities)
2518   "Declare backend NAME with ABILITIES as a Gnus backend."
2519   (setq gnus-valid-select-methods
2520         (nconc gnus-valid-select-methods
2521                (list (apply 'list name abilities)))))
2522
2523 (defun gnus-set-default-directory ()
2524   "Set the default directory in the current buffer to `gnus-default-directory'.
2525 If this variable is nil, don't do anything."
2526   (setq default-directory
2527         (if (and gnus-default-directory
2528                  (file-exists-p gnus-default-directory))
2529             (file-name-as-directory (expand-file-name gnus-default-directory))
2530           default-directory)))
2531
2532 (provide 'gnus-start)
2533
2534 ;;; gnus-start.el ends here