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