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