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