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