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