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