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