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