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