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