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