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