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