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