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