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