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