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