T-gnus 6.15.17 revision 00.
[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 (require '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   (gnus-kill-buffer nntp-server-buffer)
712   ;; Kill Gnus buffers.
713   (dolist (buffer (gnus-buffers))
714     (gnus-kill-buffer buffer))
715   ;; Remove Gnus frames.
716   (gnus-kill-gnus-frames))
717
718 (defun gnus-no-server-1 (&optional arg slave)
719   "Read network news.
720 If ARG is a positive number, Gnus will use that as the
721 startup level.  If ARG is nil, Gnus will be started at level 2.
722 If ARG is non-nil and not a positive number, Gnus will
723 prompt the user for the name of an NNTP server to use.
724 As opposed to `gnus', this command will not connect to the local server."
725   (interactive "P")
726   (let ((val (or arg (1- gnus-level-default-subscribed))))
727     (gnus val t slave)
728     (make-local-variable 'gnus-group-use-permanent-levels)
729     (setq gnus-group-use-permanent-levels val)))
730
731 (defun gnus-1 (&optional arg dont-connect slave)
732   "Read network news.
733 If ARG is non-nil and a positive number, Gnus will use that as the
734 startup level.  If ARG is non-nil and not a positive number, Gnus will
735 prompt the user for the name of an NNTP server to use."
736   (interactive "P")
737
738   (if (gnus-alive-p)
739       (progn
740         (switch-to-buffer gnus-group-buffer)
741         (gnus-group-get-new-news
742          (and (numberp arg)
743               (> arg 0)
744               (max (car gnus-group-list-mode) arg))))
745
746     (gnus-clear-system)
747     (gnus-splash)
748     (gnus-run-hooks 'gnus-before-startup-hook)
749     (nnheader-init-server-buffer)
750     (setq gnus-slave slave)
751     (gnus-read-init-file)
752     (if gnus-agent
753         (gnus-agentize))
754
755     (when gnus-simple-splash
756       (setq gnus-simple-splash nil)
757       (cond
758        ((featurep 'xemacs)
759         (gnus-xmas-splash))
760        ((and window-system
761              (= (frame-height) (1+ (window-height))))
762         (gnus-x-splash))))
763
764     (let ((level (and (numberp arg) (> arg 0) arg))
765           did-connect)
766       (unwind-protect
767           (progn
768             (unless dont-connect
769               (setq did-connect
770                     (gnus-start-news-server (and arg (not level))))))
771         (if (and (not dont-connect)
772                  (not did-connect))
773             (gnus-group-quit)
774           (gnus-run-hooks 'gnus-startup-hook)
775           ;; NNTP server is successfully open.
776
777           ;; Find the current startup file name.
778           (setq gnus-current-startup-file
779                 (gnus-make-newsrc-file gnus-startup-file))
780
781           ;; Read the dribble file.
782           (when (or gnus-slave gnus-use-dribble-file)
783             (gnus-dribble-read-file))
784
785           ;; Allow using GroupLens predictions.
786           (when gnus-use-grouplens
787             (bbb-login)
788             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
789
790           ;; Do the actual startup.
791           (if gnus-agent
792               (gnus-request-create-group "queue" '(nndraft "")))
793           (gnus-request-create-group "drafts" '(nndraft ""))
794           (gnus-setup-news nil level dont-connect)
795           (gnus-run-hooks 'gnus-setup-news-hook)
796           (gnus-start-draft-setup)
797           ;; Generate the group buffer.
798           (gnus-group-list-groups level)
799           (gnus-group-first-unread-group)
800           (gnus-configure-windows 'group)
801           (gnus-group-set-mode-line)
802           ;; For reading Info.
803           (set-language-info "Japanese" 'gnus-info "gnus-ja")
804           (gnus-run-hooks 'gnus-started-hook))))))
805
806 (defun gnus-start-draft-setup ()
807   "Make sure the draft group exists."
808   (gnus-request-create-group "drafts" '(nndraft ""))
809   (unless (gnus-gethash "nndraft:drafts" gnus-newsrc-hashtb)
810     (let ((gnus-level-default-subscribed 1))
811       (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
812     (gnus-group-set-parameter
813      "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
814
815 \f
816 ;;;
817 ;;; Dribble file
818 ;;;
819
820 (defvar gnus-dribble-ignore nil)
821 (defvar gnus-dribble-eval-file nil)
822
823 (defun gnus-dribble-file-name ()
824   "Return the dribble file for the current .newsrc."
825   (concat
826    (if gnus-dribble-directory
827        (concat (file-name-as-directory gnus-dribble-directory)
828                (file-name-nondirectory gnus-current-startup-file))
829      gnus-current-startup-file)
830    "-dribble"))
831
832 (defun gnus-dribble-enter (string)
833   "Enter STRING into the dribble buffer."
834   (when (and (not gnus-dribble-ignore)
835              gnus-dribble-buffer
836              (buffer-name gnus-dribble-buffer))
837     (let ((obuf (current-buffer)))
838       (set-buffer gnus-dribble-buffer)
839       (goto-char (point-max))
840       (insert string "\n")
841       ;; This has been commented by Josh Huber <huber@alum.wpi.edu>
842       ;; It causes problems with both XEmacs and Emacs 21, and doesn't
843       ;; seem to be of much value. (FIXME: remove this after we make sure
844       ;; it's not needed).
845       ;; (set-window-point (get-buffer-window (current-buffer)) (point-max))
846       (bury-buffer gnus-dribble-buffer)
847       (save-excursion
848         (set-buffer gnus-group-buffer)
849         (gnus-group-set-mode-line))
850       (set-buffer obuf))))
851
852 (defun gnus-dribble-touch ()
853   "Touch the dribble buffer."
854   (gnus-dribble-enter ""))
855
856 (defun gnus-dribble-read-file ()
857   "Read the dribble file from disk."
858   (let ((dribble-file (gnus-dribble-file-name)))
859     (save-excursion
860       (set-buffer (setq gnus-dribble-buffer
861                         (gnus-get-buffer-create
862                          (file-name-nondirectory dribble-file))))
863       (erase-buffer)
864       (setq buffer-file-name dribble-file)
865       (auto-save-mode t)
866       (buffer-disable-undo)
867       (bury-buffer (current-buffer))
868       (set-buffer-modified-p nil)
869       (let ((auto (make-auto-save-file-name))
870             (gnus-dribble-ignore t)
871             (purpose nil)
872             modes)
873         (when (or (file-exists-p auto) (file-exists-p dribble-file))
874           ;; Load whichever file is newest -- the auto save file
875           ;; or the "real" file.
876           (if (file-newer-than-file-p auto dribble-file)
877               (nnheader-insert-file-contents auto)
878             (nnheader-insert-file-contents dribble-file))
879           (unless (zerop (buffer-size))
880             (set-buffer-modified-p t))
881           ;; Set the file modes to reflect the .newsrc file modes.
882           (save-buffer)
883           (when (and (file-exists-p gnus-current-startup-file)
884                      (file-exists-p dribble-file)
885                      (setq modes (file-modes gnus-current-startup-file)))
886             (set-file-modes dribble-file modes))
887           (goto-char (point-min))
888           (when (search-forward "Gnus was exited on purpose" nil t)
889             (setq purpose t))
890           ;; Possibly eval the file later.
891           (when (or gnus-always-read-dribble-file
892                     (gnus-y-or-n-p
893                      (if purpose
894                          "Gnus exited on purpose without saving; read auto-save file anyway? "
895                        "Gnus auto-save file exists.  Do you want to read it? ")))
896             (setq gnus-dribble-eval-file t)))))))
897
898 (defun gnus-dribble-eval-file ()
899   (when gnus-dribble-eval-file
900     (setq gnus-dribble-eval-file nil)
901     (save-excursion
902       (let ((gnus-dribble-ignore t))
903         (set-buffer gnus-dribble-buffer)
904         (eval-buffer (current-buffer))))))
905
906 (defun gnus-dribble-delete-file ()
907   (when (file-exists-p (gnus-dribble-file-name))
908     (delete-file (gnus-dribble-file-name)))
909   (when gnus-dribble-buffer
910     (save-excursion
911       (set-buffer gnus-dribble-buffer)
912       (let ((auto (make-auto-save-file-name)))
913         (when (file-exists-p auto)
914           (delete-file auto))
915         (erase-buffer)
916         (set-buffer-modified-p nil)))))
917
918 (defun gnus-dribble-save ()
919   (when (and gnus-dribble-buffer
920              (buffer-name gnus-dribble-buffer))
921     (save-excursion
922       (set-buffer gnus-dribble-buffer)
923       (save-buffer))))
924
925 (defun gnus-dribble-clear ()
926   (when (gnus-buffer-exists-p gnus-dribble-buffer)
927     (save-excursion
928       (set-buffer gnus-dribble-buffer)
929       (erase-buffer)
930       (set-buffer-modified-p nil)
931       (setq buffer-saved-size (buffer-size)))))
932
933 \f
934 ;;;
935 ;;; Active & Newsrc File Handling
936 ;;;
937
938 (defun gnus-setup-news (&optional rawfile level dont-connect)
939   "Setup news information.
940 If RAWFILE is non-nil, the .newsrc file will also be read.
941 If LEVEL is non-nil, the news will be set up at level LEVEL."
942   (require 'nnmail)
943   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
944         ;; Binding this variable will inhibit multiple fetchings
945         ;; of the same mail source.
946         (nnmail-fetched-sources (list t)))
947
948     (when init
949       ;; Clear some variables to re-initialize news information.
950       (setq gnus-newsrc-alist nil
951             gnus-active-hashtb nil)
952       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
953       (gnus-read-newsrc-file rawfile))
954
955     ;; Make sure the archive server is available to all and sundry.
956     (when gnus-message-archive-method
957       (unless (assoc "archive" gnus-server-alist)
958         (push `("archive"
959                 nnfolder
960                 "archive"
961                 (nnfolder-directory
962                  ,(nnheader-concat message-directory "archive"))
963                 (nnfolder-active-file
964                  ,(nnheader-concat message-directory "archive/active"))
965                 (nnfolder-get-new-mail nil)
966                 (nnfolder-inhibit-expiry t))
967               gnus-server-alist)))
968
969     ;; If we don't read the complete active file, we fill in the
970     ;; hashtb here.
971     (when (or (null gnus-read-active-file)
972               (eq gnus-read-active-file 'some))
973       (gnus-update-active-hashtb-from-killed))
974
975     ;; Read the active file and create `gnus-active-hashtb'.
976     ;; If `gnus-read-active-file' is nil, then we just create an empty
977     ;; hash table.  The partial filling out of the hash table will be
978     ;; done in `gnus-get-unread-articles'.
979     (and gnus-read-active-file
980          (not level)
981          (gnus-read-active-file nil dont-connect))
982
983     (unless gnus-active-hashtb
984       (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
985
986     ;; Initialize the cache.
987     (when gnus-use-cache
988       (gnus-cache-open))
989
990     ;; Possibly eval the dribble file.
991     (and init
992          (or gnus-use-dribble-file gnus-slave)
993          (gnus-dribble-eval-file))
994
995     ;; Slave Gnusii should then clear the dribble buffer.
996     (when (and init gnus-slave)
997       (gnus-dribble-clear))
998
999     (gnus-update-format-specifications)
1000
1001     ;; See whether we need to read the description file.
1002     (when (and (boundp 'gnus-group-line-format)
1003                (stringp gnus-group-line-format)
1004                (let ((case-fold-search nil))
1005                  (string-match "%[-,0-9]*D" gnus-group-line-format))
1006                (not gnus-description-hashtb)
1007                (not dont-connect)
1008                gnus-read-active-file)
1009       (gnus-read-all-descriptions-files))
1010
1011     ;; Find new newsgroups and treat them.
1012     (when (and init gnus-check-new-newsgroups (not level)
1013                (gnus-check-server gnus-select-method)
1014                (not gnus-slave)
1015                gnus-plugged)
1016       (gnus-find-new-newsgroups))
1017
1018     ;; Check and remove bogus newsgroups.
1019     (when (and init gnus-check-bogus-newsgroups
1020                gnus-read-active-file (not level)
1021                (gnus-server-opened gnus-select-method))
1022       (gnus-check-bogus-newsgroups))
1023
1024     ;; We might read in new NoCeM messages here.
1025     (when (and gnus-use-nocem
1026                (not level)
1027                (not dont-connect))
1028       (gnus-nocem-scan-groups))
1029
1030     ;; Read any slave files.
1031     (gnus-master-read-slave-newsrc)
1032
1033     ;; Find the number of unread articles in each non-dead group.
1034     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
1035       (gnus-get-unread-articles level))))
1036
1037 (defun gnus-call-subscribe-functions (method group)
1038   "Call METHOD to subscribe GROUP.
1039 If no function returns `non-nil', call `gnus-subscribe-zombies'."
1040   (unless (cond
1041            ((gnus-functionp method)
1042             (funcall method group))
1043            ((listp method)
1044             (catch 'found
1045               (dolist (func method)
1046                 (if (funcall func group)
1047                     (throw 'found t)))
1048               nil))
1049            (t nil))
1050     (gnus-subscribe-zombies group)))
1051
1052 (defun gnus-find-new-newsgroups (&optional arg)
1053   "Search for new newsgroups and add them.
1054 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
1055 The `-n' option line from .newsrc is respected.
1056
1057 With 1 C-u, use the `ask-server' method to query the server for new
1058 groups.
1059 With 2 C-u's, use most complete method possible to query the server
1060 for new groups, and subscribe the new groups as zombies."
1061   (interactive "p")
1062   (let* ((gnus-subscribe-newsgroup-method
1063           gnus-subscribe-newsgroup-method)
1064          (check (cond
1065                  ((or (and (= (or arg 1) 4)
1066                            (not (listp gnus-check-new-newsgroups)))
1067                       (null gnus-read-active-file)
1068                       (eq gnus-read-active-file 'some))
1069                   'ask-server)
1070                  ((= (or arg 1) 16)
1071                   (setq gnus-subscribe-newsgroup-method
1072                         'gnus-subscribe-zombies)
1073                   t)
1074                  (t gnus-check-new-newsgroups))))
1075     (unless (gnus-check-first-time-used)
1076       (if (or (consp check)
1077               (eq check 'ask-server))
1078           ;; Ask the server for new groups.
1079           (gnus-ask-server-for-new-groups)
1080         ;; Go through the active hashtb and look for new groups.
1081         (let ((groups 0)
1082               group new-newsgroups)
1083           (gnus-message 5 "Looking for new newsgroups...")
1084           (unless gnus-have-read-active-file
1085             (gnus-read-active-file))
1086           (setq gnus-newsrc-last-checked-date (message-make-date))
1087           (unless gnus-killed-hashtb
1088             (gnus-make-hashtable-from-killed))
1089           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1090           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1091           (mapatoms
1092            (lambda (sym)
1093              (if (or (null (setq group (symbol-name sym)))
1094                      (not (boundp sym))
1095                      (null (symbol-value sym))
1096                      (gnus-gethash group gnus-killed-hashtb)
1097                      (gnus-gethash group gnus-newsrc-hashtb))
1098                  ()
1099                (let ((do-sub (gnus-matches-options-n group)))
1100                  (cond
1101                   ((eq do-sub 'subscribe)
1102                    (setq groups (1+ groups))
1103                    (gnus-sethash group group gnus-killed-hashtb)
1104                    (gnus-call-subscribe-functions
1105                     gnus-subscribe-options-newsgroup-method group))
1106                   ((eq do-sub 'ignore)
1107                    nil)
1108                   (t
1109                    (setq groups (1+ groups))
1110                    (gnus-sethash group group gnus-killed-hashtb)
1111                    (if gnus-subscribe-hierarchical-interactive
1112                        (push group new-newsgroups)
1113                      (gnus-call-subscribe-functions
1114                       gnus-subscribe-newsgroup-method group)))))))
1115            gnus-active-hashtb)
1116           (when new-newsgroups
1117             (gnus-subscribe-hierarchical-interactive new-newsgroups))
1118           (if (> groups 0)
1119               (gnus-message 5 "%d new newsgroup%s arrived."
1120                             groups (if (> groups 1) "s have" " has"))
1121             (gnus-message 5 "No new newsgroups.")))))))
1122
1123 (defun gnus-matches-options-n (group)
1124   ;; Returns `subscribe' if the group is to be unconditionally
1125   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1126   ;; no match for the group.
1127
1128   ;; First we check the two user variables.
1129   (cond
1130    ((and gnus-options-subscribe
1131          (string-match gnus-options-subscribe group))
1132     'subscribe)
1133    ((and gnus-auto-subscribed-groups
1134          (string-match gnus-auto-subscribed-groups group))
1135     'subscribe)
1136    ((and gnus-options-not-subscribe
1137          (string-match gnus-options-not-subscribe group))
1138     'ignore)
1139    ;; Then we go through the list that was retrieved from the .newsrc
1140    ;; file.  This list has elements on the form
1141    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
1142    ;; is in the reverse order of the options line) is returned.
1143    (t
1144     (let ((regs gnus-newsrc-options-n))
1145       (while (and regs
1146                   (not (string-match (caar regs) group)))
1147         (setq regs (cdr regs)))
1148       (and regs (cdar regs))))))
1149
1150 (defun gnus-ask-server-for-new-groups ()
1151   (let* ((new-date (message-make-date))
1152          (date (or gnus-newsrc-last-checked-date new-date))
1153          (methods (cons gnus-select-method
1154                         (nconc
1155                          (when (gnus-archive-server-wanted-p)
1156                            (list "archive"))
1157                          (append
1158                           (and (consp gnus-check-new-newsgroups)
1159                                gnus-check-new-newsgroups)
1160                           gnus-secondary-select-methods))))
1161          (groups 0)
1162          group new-newsgroups got-new method hashtb
1163          gnus-override-subscribe-method)
1164     (unless gnus-killed-hashtb
1165       (gnus-make-hashtable-from-killed))
1166     ;; Go through both primary and secondary select methods and
1167     ;; request new newsgroups.
1168     (while (setq method (gnus-server-get-method nil (pop methods)))
1169       (setq new-newsgroups nil
1170             gnus-override-subscribe-method method)
1171       (when (and (gnus-check-server method)
1172                  (gnus-request-newgroups date method))
1173         (save-excursion
1174           (setq got-new t
1175                 hashtb (gnus-make-hashtable 100))
1176           (set-buffer nntp-server-buffer)
1177           ;; Enter all the new groups into a hashtable.
1178           (gnus-active-to-gnus-format method hashtb 'ignore))
1179         ;; Now all new groups from `method' are in `hashtb'.
1180         (mapatoms
1181          (lambda (group-sym)
1182            (if (or (null (setq group (symbol-name group-sym)))
1183                    (not (boundp group-sym))
1184                    (null (symbol-value group-sym))
1185                    (gnus-gethash group gnus-newsrc-hashtb)
1186                    (member group gnus-zombie-list)
1187                    (member group gnus-killed-list))
1188                ;; The group is already known.
1189                ()
1190              ;; Make this group active.
1191              (when (symbol-value group-sym)
1192                (gnus-set-active group (symbol-value group-sym)))
1193              ;; Check whether we want it or not.
1194              (let ((do-sub (gnus-matches-options-n group)))
1195                (cond
1196                 ((eq do-sub 'subscribe)
1197                  (incf groups)
1198                  (gnus-sethash group group gnus-killed-hashtb)
1199                  (gnus-call-subscribe-functions
1200                   gnus-subscribe-options-newsgroup-method group))
1201                 ((eq do-sub 'ignore)
1202                  nil)
1203                 (t
1204                  (incf groups)
1205                  (gnus-sethash group group gnus-killed-hashtb)
1206                  (if gnus-subscribe-hierarchical-interactive
1207                      (push group new-newsgroups)
1208                    (gnus-call-subscribe-functions
1209                     gnus-subscribe-newsgroup-method group)))))))
1210          hashtb))
1211       (when new-newsgroups
1212         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
1213     (if (> groups 0)
1214         (gnus-message 5 "%d new newsgroup%s arrived"
1215                       groups (if (> groups 1) "s have" " has"))
1216       (gnus-message 5 "No new newsgroups"))
1217     (when got-new
1218       (setq gnus-newsrc-last-checked-date new-date))
1219     got-new))
1220
1221 (defun gnus-check-first-time-used ()
1222   (catch 'ended
1223     ;; First check if any of the following files exist.  If they do,
1224     ;; it's not the first time the user has used Gnus.
1225     (dolist (file (list (concat gnus-current-startup-file ".el")
1226                         (concat gnus-current-startup-file ".eld")
1227                         (concat gnus-startup-file ".el")
1228                         (concat gnus-startup-file ".eld")))
1229       (when (file-exists-p file)
1230         (throw 'ended nil)))
1231     (gnus-message 6 "First time user; subscribing you to default groups")
1232     (unless (gnus-read-active-file-p)
1233       (let ((gnus-read-active-file t))
1234         (gnus-read-active-file)))
1235     (setq gnus-newsrc-last-checked-date (message-make-date))
1236     ;; Subscribe to the default newsgroups.
1237     (let ((groups (or gnus-default-subscribed-newsgroups
1238                       gnus-backup-default-subscribed-newsgroups))
1239           group)
1240       (if (eq groups t)
1241           ;; If t, we subscribe (or not) all groups as if they were new.
1242           (mapatoms
1243            (lambda (sym)
1244              (when (setq group (symbol-name sym))
1245                (let ((do-sub (gnus-matches-options-n group)))
1246                  (cond
1247                   ((eq do-sub 'subscribe)
1248                    (gnus-sethash group group gnus-killed-hashtb)
1249                    (gnus-call-subscribe-functions
1250                     gnus-subscribe-options-newsgroup-method group))
1251                   ((eq do-sub 'ignore)
1252                    nil)
1253                   (t
1254                    (push group gnus-killed-list))))))
1255            gnus-active-hashtb)
1256         (dolist (group groups)
1257           ;; Only subscribe the default groups that are activated.
1258           (when (gnus-active group)
1259             (gnus-group-change-level
1260              group gnus-level-default-subscribed gnus-level-killed)))
1261         (save-excursion
1262           (set-buffer gnus-group-buffer)
1263           ;; Don't error if the group already exists. This happens when a
1264           ;; first-time user types 'F'. -- didier
1265           (gnus-group-make-help-group t))
1266         (when gnus-novice-user
1267           (gnus-message 7 "`A k' to list killed groups"))))))
1268
1269 (defun gnus-subscribe-group (group &optional previous method)
1270   "Subcribe GROUP and put it after PREVIOUS."
1271   (gnus-group-change-level
1272    (if method
1273        (list t group gnus-level-default-subscribed nil nil method)
1274      group)
1275    gnus-level-default-subscribed gnus-level-killed previous t)
1276   t)
1277
1278 ;; `gnus-group-change-level' is the fundamental function for changing
1279 ;; subscription levels of newsgroups.  This might mean just changing
1280 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1281 ;; again, which subscribes/unsubscribes a group, which is equally
1282 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1283 ;; from 8-9 to 1-7 means that you remove the group from the list of
1284 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1285 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1286 ;; which is trivial.
1287 ;; ENTRY can either be a string (newsgroup name) or a list (if
1288 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1289 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1290 ;; entries.
1291 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1292 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1293 ;; after.
1294 (defun gnus-group-change-level (entry level &optional oldlevel
1295                                       previous fromkilled)
1296   (let (group info active num)
1297     ;; Glean what info we can from the arguments
1298     (if (consp entry)
1299         (if fromkilled (setq group (nth 1 entry))
1300           (setq group (car (nth 2 entry))))
1301       (setq group entry))
1302     (when (and (stringp entry)
1303                oldlevel
1304                (< oldlevel gnus-level-zombie))
1305       (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
1306     (if (and (not oldlevel)
1307              (consp entry))
1308         (setq oldlevel (gnus-info-level (nth 2 entry)))
1309       (setq oldlevel (or oldlevel gnus-level-killed)))
1310     (when (stringp previous)
1311       (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
1312
1313     (if (and (>= oldlevel gnus-level-zombie)
1314              (gnus-gethash group gnus-newsrc-hashtb))
1315         ;; We are trying to subscribe a group that is already
1316         ;; subscribed.
1317         ()                              ; Do nothing.
1318
1319       (unless (gnus-ephemeral-group-p group)
1320         (gnus-dribble-enter
1321          (format "(gnus-group-change-level %S %S %S %S %S)"
1322                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1323
1324       ;; Then we remove the newgroup from any old structures, if needed.
1325       ;; If the group was killed, we remove it from the killed or zombie
1326       ;; list.  If not, and it is in fact going to be killed, we remove
1327       ;; it from the newsrc hash table and assoc.
1328       (cond
1329        ((>= oldlevel gnus-level-zombie)
1330         ;; oldlevel could be wrong.
1331         (setq gnus-zombie-list (delete group gnus-zombie-list))
1332         (setq gnus-killed-list (delete group gnus-killed-list)))
1333        (t
1334         (when (and (>= level gnus-level-zombie)
1335                    entry)
1336           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1337           (when (nth 3 entry)
1338             (setcdr (gnus-gethash (car (nth 3 entry))
1339                                   gnus-newsrc-hashtb)
1340                     (cdr entry)))
1341           (setcdr (cdr entry) (cdddr entry)))))
1342
1343       ;; Finally we enter (if needed) the list where it is supposed to
1344       ;; go, and change the subscription level.  If it is to be killed,
1345       ;; we enter it into the killed or zombie list.
1346       (cond
1347        ((>= level gnus-level-zombie)
1348         ;; Remove from the hash table.
1349         (gnus-sethash group nil gnus-newsrc-hashtb)
1350         ;; We do not enter foreign groups into the list of dead
1351         ;; groups.
1352         (unless (gnus-group-foreign-p group)
1353           (if (= level gnus-level-zombie)
1354               (push group gnus-zombie-list)
1355             (if (= oldlevel gnus-level-killed)
1356                 ;; Remove from active hashtb.
1357                 (unintern group gnus-active-hashtb)
1358               ;; Don't add it into killed-list if it was killed.
1359               (push group gnus-killed-list)))))
1360        (t
1361         ;; If the list is to be entered into the newsrc assoc, and
1362         ;; it was killed, we have to create an entry in the newsrc
1363         ;; hashtb format and fix the pointers in the newsrc assoc.
1364         (if (< oldlevel gnus-level-zombie)
1365             ;; It was alive, and it is going to stay alive, so we
1366             ;; just change the level and don't change any pointers or
1367             ;; hash table entries.
1368             (setcar (cdaddr entry) level)
1369           (if (listp entry)
1370               (setq info (cdr entry)
1371                     num (car entry))
1372             (setq active (gnus-active group))
1373             (setq num
1374                   (if active (- (1+ (cdr active)) (car active)) t))
1375             ;; Shorten the select method if possible, if we need to
1376             ;; store it at all (native groups).
1377             (let ((method (gnus-method-simplify
1378                            (or gnus-override-subscribe-method
1379                                (gnus-group-method group)))))
1380               (if method
1381                   (setq info (list group level nil nil method))
1382                 (setq info (list group level nil)))))
1383           (unless previous
1384             (setq previous
1385                   (let ((p gnus-newsrc-alist))
1386                     (while (cddr p)
1387                       (setq p (cdr p)))
1388                     p)))
1389           (setq entry (cons info (cddr previous)))
1390           (if (cdr previous)
1391               (progn
1392                 (setcdr (cdr previous) entry)
1393                 (gnus-sethash group (cons num (cdr previous))
1394                               gnus-newsrc-hashtb))
1395             (setcdr previous entry)
1396             (gnus-sethash group (cons num previous)
1397                           gnus-newsrc-hashtb))
1398           (when (cdr entry)
1399             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1400           (gnus-dribble-enter
1401            (format
1402             "(gnus-group-set-info '%S)" info)))))
1403       (when gnus-group-change-level-function
1404         (funcall gnus-group-change-level-function
1405                  group level oldlevel previous)))))
1406
1407 (defun gnus-kill-newsgroup (newsgroup)
1408   "Obsolete function.  Kills a newsgroup."
1409   (gnus-group-change-level
1410    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1411
1412 (defun gnus-check-bogus-newsgroups (&optional confirm)
1413   "Remove bogus newsgroups.
1414 If CONFIRM is non-nil, the user has to confirm the deletion of every
1415 newsgroup."
1416   (let ((newsrc (cdr gnus-newsrc-alist))
1417         bogus group entry info)
1418     (gnus-message 5 "Checking bogus newsgroups...")
1419     (unless (gnus-read-active-file-p)
1420       (gnus-read-active-file t))
1421     (when (gnus-read-active-file-p)
1422       ;; Find all bogus newsgroup that are subscribed.
1423       (while newsrc
1424         (setq info (pop newsrc)
1425               group (gnus-info-group info))
1426         (unless (or (gnus-active group) ; Active
1427                     (and (gnus-info-method info)
1428                          (not (gnus-secondary-method-p
1429                                (gnus-info-method info))))) ; Foreign
1430           ;; Found a bogus newsgroup.
1431           (push group bogus)))
1432       (if confirm
1433           (map-y-or-n-p
1434            "Remove bogus group %s? "
1435            (lambda (group)
1436              ;; Remove all bogus subscribed groups by first killing them, and
1437              ;; then removing them from the list of killed groups.
1438              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1439                (gnus-group-change-level entry gnus-level-killed)
1440                (setq gnus-killed-list (delete group gnus-killed-list))))
1441            bogus '("group" "groups" "remove"))
1442         (while (setq group (pop bogus))
1443           ;; Remove all bogus subscribed groups by first killing them, and
1444           ;; then removing them from the list of killed groups.
1445           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1446             (gnus-group-change-level entry gnus-level-killed)
1447             (setq gnus-killed-list (delete group gnus-killed-list)))))
1448       ;; Then we remove all bogus groups from the list of killed and
1449       ;; zombie groups.  They are removed without confirmation.
1450       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1451             killed)
1452         (while dead-lists
1453           (setq killed (symbol-value (car dead-lists)))
1454           (while killed
1455             (unless (gnus-active (setq group (pop killed)))
1456               ;; The group is bogus.
1457               ;; !!!Slow as hell.
1458               (set (car dead-lists)
1459                    (delete group (symbol-value (car dead-lists))))))
1460           (setq dead-lists (cdr dead-lists))))
1461       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1462       (gnus-message 5 "Checking bogus newsgroups...done"))))
1463
1464 (defun gnus-check-duplicate-killed-groups ()
1465   "Remove duplicates from the list of killed groups."
1466   (interactive)
1467   (let ((killed gnus-killed-list))
1468     (while killed
1469       (gnus-message 9 "%d" (length killed))
1470       (setcdr killed (delete (car killed) (cdr killed)))
1471       (setq killed (cdr killed)))))
1472
1473 ;; We want to inline a function from gnus-cache, so we cheat here:
1474 (eval-when-compile
1475   (defvar gnus-cache-active-hashtb)
1476   (defun gnus-cache-possibly-alter-active (group active)
1477     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1478     (when gnus-cache-active-hashtb
1479       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1480         (when cache-active
1481           (when (< (car cache-active) (car active))
1482             (setcar active (car cache-active)))
1483           (when (> (cdr cache-active) (cdr active))
1484             (setcdr active (cdr cache-active))))))))
1485
1486 (defun gnus-activate-group (group &optional scan dont-check method)
1487   ;; Check whether a group has been activated or not.
1488   ;; If SCAN, request a scan of that group as well.
1489   (let ((method (or method (inline (gnus-find-method-for-group group))))
1490         active)
1491     (and (inline (gnus-check-server method))
1492          ;; We escape all bugs and quit here to make it possible to
1493          ;; continue if a group is so out-there that it reports bugs
1494          ;; and stuff.
1495          (progn
1496            (and scan
1497                 (gnus-check-backend-function 'request-scan (car method))
1498                 (gnus-request-scan group method))
1499            t)
1500          (if (or debug-on-error debug-on-quit)
1501              (inline (gnus-request-group group dont-check method))
1502            (condition-case nil
1503                (inline (gnus-request-group group dont-check method))
1504              ;;(error nil)
1505              (quit
1506               (message "Quit activating %s" group)
1507               nil)))
1508          (unless dont-check
1509            (setq active (gnus-parse-active))
1510            ;; If there are no articles in the group, the GROUP
1511            ;; command may have responded with the `(0 . 0)'.  We
1512            ;; ignore this if we already have an active entry
1513            ;; for the group.
1514            (if (and (zerop (car active))
1515                     (zerop (cdr active))
1516                     (gnus-active group))
1517                (gnus-active group)
1518              (gnus-set-active group active)
1519              ;; Return the new active info.
1520              active)))))
1521
1522 (defun gnus-get-unread-articles-in-group (info active &optional update)
1523   (when active
1524     ;; Allow the backend to update the info in the group.
1525     (when (and update
1526                (gnus-request-update-info
1527                 info (inline (gnus-find-method-for-group
1528                               (gnus-info-group info)))))
1529       (gnus-activate-group (gnus-info-group info) nil t))
1530
1531     (let* ((range (gnus-info-read info))
1532            (num 0))
1533       ;; If a cache is present, we may have to alter the active info.
1534       (when (and gnus-use-cache info)
1535         (inline (gnus-cache-possibly-alter-active
1536                  (gnus-info-group info) active)))
1537       ;; Modify the list of read articles according to what articles
1538       ;; are available; then tally the unread articles and add the
1539       ;; number to the group hash table entry.
1540       (cond
1541        ((zerop (cdr active))
1542         (setq num 0))
1543        ((not range)
1544         (setq num (- (1+ (cdr active)) (car active))))
1545        ((not (listp (cdr range)))
1546         ;; Fix a single (num . num) range according to the
1547         ;; active hash table.
1548         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1549         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1550         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1551         ;; Compute number of unread articles.
1552         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1553        (t
1554         ;; The read list is a list of ranges.  Fix them according to
1555         ;; the active hash table.
1556         ;; First peel off any elements that are below the lower
1557         ;; active limit.
1558         (while (and (cdr range)
1559                     (>= (car active)
1560                         (or (and (atom (cadr range)) (cadr range))
1561                             (caadr range))))
1562           (if (numberp (car range))
1563               (setcar range
1564                       (cons (car range)
1565                             (or (and (numberp (cadr range))
1566                                      (cadr range))
1567                                 (cdadr range))))
1568             (setcdr (car range)
1569                     (or (and (numberp (nth 1 range)) (nth 1 range))
1570                         (cdadr range))))
1571           (setcdr range (cddr range)))
1572         ;; Adjust the first element to be the same as the lower limit.
1573         (when (and (not (atom (car range)))
1574                    (< (cdar range) (car active)))
1575           (setcdr (car range) (1- (car active))))
1576         ;; Then we want to peel off any elements that are higher
1577         ;; than the upper active limit.
1578         (let ((srange range))
1579           ;; Go past all valid elements.
1580           (while (and (cdr srange)
1581                       (<= (or (and (atom (cadr srange))
1582                                    (cadr srange))
1583                               (caadr srange))
1584                           (cdr active)))
1585             (setq srange (cdr srange)))
1586           (when (cdr srange)
1587             ;; Nuke all remaining invalid elements.
1588             (setcdr srange nil))
1589
1590           ;; Adjust the final element.
1591           (when (and (not (atom (car srange)))
1592                      (> (cdar srange) (cdr active)))
1593             (setcdr (car srange) (cdr active))))
1594         ;; Compute the number of unread articles.
1595         (while range
1596           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1597                                       (cdar range)))
1598                               (or (and (atom (car range)) (car range))
1599                                   (caar range)))))
1600           (setq range (cdr range)))
1601         (setq num (max 0 (- (cdr active) num)))))
1602       ;; Set the number of unread articles.
1603       (when (and info
1604                  (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb))
1605         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1606       num)))
1607
1608 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1609 ;; and compute how many unread articles there are in each group.
1610 (defun gnus-get-unread-articles (&optional level)
1611   (setq gnus-server-method-cache nil)
1612   (let* ((newsrc (cdr gnus-newsrc-alist))
1613          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1614          (foreign-level
1615           (min
1616            (cond ((and gnus-activate-foreign-newsgroups
1617                        (not (numberp gnus-activate-foreign-newsgroups)))
1618                   (1+ gnus-level-subscribed))
1619                  ((numberp gnus-activate-foreign-newsgroups)
1620                   gnus-activate-foreign-newsgroups)
1621                  (t 0))
1622            level))
1623          scanned-methods info group active method retrieve-groups)
1624     (gnus-message 6 "Checking new news...")
1625
1626     (while newsrc
1627       (setq active (gnus-active (setq group (gnus-info-group
1628                                              (setq info (pop newsrc))))))
1629
1630       ;; Check newsgroups.  If the user doesn't want to check them, or
1631       ;; they can't be checked (for instance, if the news server can't
1632       ;; be reached) we just set the number of unread articles in this
1633       ;; newsgroup to t.  This means that Gnus thinks that there are
1634       ;; unread articles, but it has no idea how many.
1635
1636       ;; To be more explicit:
1637       ;; >0 for an active group with messages
1638       ;; 0 for an active group with no unread messages
1639       ;; nil for non-foreign groups that the user has requested not be checked
1640       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1641       ;;   be checked, for one reason or other.
1642       (if (and (setq method (gnus-info-method info))
1643                (not (inline
1644                       (gnus-server-equal
1645                        gnus-select-method
1646                        (setq method (gnus-server-get-method nil method)))))
1647                (not (gnus-secondary-method-p method)))
1648           ;; These groups are foreign.  Check the level.
1649           (when (and (<= (gnus-info-level info) foreign-level)
1650                      (setq active (gnus-activate-group group 'scan)))
1651             ;; Let the Gnus agent save the active file.
1652             (when (and gnus-agent active (gnus-online method))
1653               (gnus-agent-save-group-info
1654                method (gnus-group-real-name group) active))
1655             (unless (inline (gnus-virtual-group-p group))
1656               (inline (gnus-close-group group)))
1657             (when (fboundp (intern (concat (symbol-name (car method))
1658                                            "-request-update-info")))
1659               (inline (gnus-request-update-info info method))))
1660         ;; These groups are native or secondary.
1661         (cond
1662          ;; We don't want these groups.
1663          ((> (gnus-info-level info) level)
1664           (setq active 'ignore))
1665          ;; Activate groups.
1666          ((not gnus-read-active-file)
1667           (if (gnus-check-backend-function 'retrieve-groups group)
1668               ;; if server support gnus-retrieve-groups we push
1669               ;; the group onto retrievegroups for later checking
1670               (if (assoc method retrieve-groups)
1671                   (setcdr (assoc method retrieve-groups)
1672                           (cons group (cdr (assoc method retrieve-groups))))
1673                 (push (list method group) retrieve-groups))
1674             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1675             ;; on the group, so we must perform a scan for every group
1676             ;; if the users has any directory mail sources.
1677             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1678             ;; for it scan all spool files even when the groups are
1679             ;; not required.
1680             (if (and
1681                  (or nnmail-scan-directory-mail-source-once
1682                      (null (assq 'directory
1683                                  (or mail-sources
1684                                      (if (listp nnmail-spool-file)
1685                                          nnmail-spool-file
1686                                        (list nnmail-spool-file))))))
1687                  (member method scanned-methods))
1688                 (setq active (gnus-activate-group group))
1689               (setq active (gnus-activate-group group 'scan))
1690               (push method scanned-methods))
1691             (when active
1692               (gnus-close-group group))))))
1693
1694       ;; Get the number of unread articles in the group.
1695       (cond
1696        ((eq active 'ignore)
1697         ;; Don't do anything.
1698         )
1699        (active
1700         (inline (gnus-get-unread-articles-in-group info active t)))
1701        (t
1702         ;; The group couldn't be reached, so we nix out the number of
1703         ;; unread articles and stuff.
1704         (gnus-set-active group nil)
1705         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1706           (when tmp
1707             (setcar tmp t))))))
1708
1709     ;; iterate through groups on methods which support gnus-retrieve-groups
1710     ;; and fetch a partial active file and use it to find new news.
1711     (dolist (rg retrieve-groups)
1712       (let ((method (or (car rg) gnus-select-method))
1713             (groups (cdr rg)))
1714         (when (gnus-check-server method)
1715           ;; Request that the backend scan its incoming messages.
1716           (when (gnus-check-backend-function 'request-scan (car method))
1717             (gnus-request-scan nil method))
1718           (gnus-read-active-file-2
1719            (mapcar (lambda (group) (gnus-group-real-name group)) groups)
1720            method)
1721           (dolist (group groups)
1722             (cond
1723              ((setq active (gnus-active (gnus-info-group
1724                                          (setq info (gnus-get-info group)))))
1725               (inline (gnus-get-unread-articles-in-group info active t)))
1726              (t
1727               ;; The group couldn't be reached, so we nix out the number of
1728               ;; unread articles and stuff.
1729               (gnus-set-active group nil)
1730               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1731
1732     (gnus-message 6 "Checking new news...done")))
1733
1734 ;; Create a hash table out of the newsrc alist.  The `car's of the
1735 ;; alist elements are used as keys.
1736 (defun gnus-make-hashtable-from-newsrc-alist ()
1737   (let ((alist gnus-newsrc-alist)
1738         (ohashtb gnus-newsrc-hashtb)
1739         prev)
1740     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1741     (setq alist
1742           (setq prev (setq gnus-newsrc-alist
1743                            (if (equal (caar gnus-newsrc-alist)
1744                                       "dummy.group")
1745                                gnus-newsrc-alist
1746                              (cons (list "dummy.group" 0 nil) alist)))))
1747     (while alist
1748       (gnus-sethash
1749        (caar alist)
1750        ;; Preserve number of unread articles in groups.
1751        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1752              prev)
1753        gnus-newsrc-hashtb)
1754       (setq prev alist
1755             alist (cdr alist)))))
1756
1757 (defun gnus-make-hashtable-from-killed ()
1758   "Create a hash table from the killed and zombie lists."
1759   (let ((lists '(gnus-killed-list gnus-zombie-list))
1760         list)
1761     (setq gnus-killed-hashtb
1762           (gnus-make-hashtable
1763            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1764     (while lists
1765       (setq list (symbol-value (pop lists)))
1766       (while list
1767         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1768
1769 (defun gnus-parse-active ()
1770   "Parse active info in the nntp server buffer."
1771   (save-excursion
1772     (set-buffer nntp-server-buffer)
1773     (goto-char (point-min))
1774     ;; Parse the result we got from `gnus-request-group'.
1775     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1776       (goto-char (match-beginning 1))
1777       (cons (read (current-buffer))
1778             (read (current-buffer))))))
1779
1780 (defun gnus-make-articles-unread (group articles)
1781   "Mark ARTICLES in GROUP as unread."
1782   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1783                           (gnus-gethash (gnus-group-real-name group)
1784                                         gnus-newsrc-hashtb))))
1785          (ranges (gnus-info-read info))
1786          news article)
1787     (while articles
1788       (when (gnus-member-of-range
1789              (setq article (pop articles)) ranges)
1790         (push article news)))
1791     (when news
1792       ;; Enter this list into the group info.
1793       (gnus-info-set-read
1794        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1795
1796       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1797       (gnus-get-unread-articles-in-group info (gnus-active group))
1798
1799       ;; Insert the change into the group buffer and the dribble file.
1800       (gnus-group-update-group group t))))
1801
1802 (defun gnus-make-ascending-articles-unread (group articles)
1803   "Mark ascending ARTICLES in GROUP as unread."
1804   (let* ((entry (or (gnus-gethash group gnus-newsrc-hashtb)
1805                     (gnus-gethash (gnus-group-real-name group)
1806                                   gnus-newsrc-hashtb)))
1807          (info (nth 2 entry))
1808          (ranges (gnus-info-read info))
1809          (r ranges)
1810          modified)
1811
1812     (while articles
1813       (let ((article (pop articles))) ; get the next article to remove from ranges
1814         (while (let ((range (car ranges))) ; note the current range
1815                  (if (atom range)       ; single value range
1816                      (cond ((not range)
1817                             ;; the articles extend past the end of the ranges
1818                             ;; OK - I'm done
1819                             (setq articles nil))
1820                            ((< range article)
1821                             ;; this range preceeds the article. Leave the range unmodified.
1822                             (pop ranges)
1823                             ranges)
1824                            ((= range article)
1825                             ;; this range exactly matches the article; REMOVE THE RANGE.
1826                             ;; NOTE: When the range being removed is the last range, the list is corrupted by inserting null at its end.
1827                             (setcar ranges (cadr ranges))
1828                             (setcdr ranges (cddr ranges))
1829                             (setq modified (if (car ranges) t 'remove-null))
1830                             nil))
1831                    (let ((min (car range))
1832                          (max (cdr range)))
1833                      ;; I have a min/max range to consider
1834                      (cond ((> min max) ; invalid range introduced by splitter
1835                             (setcar ranges (cadr ranges))
1836                             (setcdr ranges (cddr ranges))
1837                             (setq modified (if (car ranges) t 'remove-null))
1838                             ranges)
1839                            ((= min max)
1840                             ;; replace min/max range with a single-value range
1841                             (setcar ranges min)
1842                             ranges)
1843                            ((< max article)
1844                             ;; this range preceeds the article. Leave the range unmodified.
1845                             (pop ranges)
1846                             ranges)
1847                            ((< article min)
1848                             ;; this article preceeds the range.  Return null to move to the
1849                             ;; next article
1850                             nil)
1851                            (t
1852                             ;; this article splits the range into two parts
1853                             (setcdr ranges (cons (cons (1+ article) max) (cdr ranges)))
1854                             (setcdr range (1- article))
1855                             (setq modified t)
1856                             ranges))))))))
1857                   
1858     (when modified
1859       (when (eq modified 'remove-null)
1860         (setq r (delq nil r)))
1861       ;; Enter this list into the group info.
1862       (gnus-info-set-read info r)
1863
1864       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1865       (gnus-get-unread-articles-in-group info (gnus-active group))
1866
1867       ;; Insert the change into the group buffer and the dribble file.
1868       (gnus-group-update-group group t))))
1869
1870 ;; Enter all dead groups into the hashtb.
1871 (defun gnus-update-active-hashtb-from-killed ()
1872   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1873         (lists (list gnus-killed-list gnus-zombie-list))
1874         killed)
1875     (while lists
1876       (setq killed (car lists))
1877       (while killed
1878         (gnus-sethash (car killed) nil hashtb)
1879         (setq killed (cdr killed)))
1880       (setq lists (cdr lists)))))
1881
1882 (defun gnus-get-killed-groups ()
1883   "Go through the active hashtb and mark all unknown groups as killed."
1884   ;; First make sure active file has been read.
1885   (unless (gnus-read-active-file-p)
1886     (let ((gnus-read-active-file t))
1887       (gnus-read-active-file)))
1888   (unless gnus-killed-hashtb
1889     (gnus-make-hashtable-from-killed))
1890   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1891   (mapatoms
1892    (lambda (sym)
1893      (let ((groups 0)
1894            (group (symbol-name sym)))
1895        (if (or (null group)
1896                (gnus-gethash group gnus-killed-hashtb)
1897                (gnus-gethash group gnus-newsrc-hashtb))
1898            ()
1899          (let ((do-sub (gnus-matches-options-n group)))
1900            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1901                ()
1902              (setq groups (1+ groups))
1903              (push group gnus-killed-list)
1904              (gnus-sethash group group gnus-killed-hashtb))))))
1905    gnus-active-hashtb)
1906   (gnus-dribble-touch))
1907
1908 ;; Get the active file(s) from the backend(s).
1909 (defun gnus-read-active-file (&optional force not-native)
1910   (gnus-group-set-mode-line)
1911   (let ((methods
1912          (mapcar
1913           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1914           (append
1915            (if (and (not not-native)
1916                     (gnus-check-server gnus-select-method))
1917                ;; The native server is available.
1918                (cons gnus-select-method gnus-secondary-select-methods)
1919              ;; The native server is down, so we just do the
1920              ;; secondary ones.
1921              gnus-secondary-select-methods)
1922            ;; Also read from the archive server.
1923            (when (gnus-archive-server-wanted-p)
1924              (list "archive")))))
1925         method)
1926     (setq gnus-have-read-active-file nil)
1927     (save-excursion
1928       (set-buffer nntp-server-buffer)
1929       (while (setq method (pop methods))
1930         ;; Only do each method once, in case the methods appear more
1931         ;; than once in this list.
1932         (unless (member method methods)
1933           (if (or debug-on-error debug-on-quit)
1934               (gnus-read-active-file-1 method force)
1935             (condition-case ()
1936                 (gnus-read-active-file-1 method force)
1937               ;; We catch C-g so that we can continue past servers
1938               ;; that do not respond.
1939               (quit
1940                (message "Quit reading the active file")
1941                nil))))))))
1942
1943 (defun gnus-read-active-file-1 (method force)
1944   (let (where mesg)
1945     (setq where (nth 1 method)
1946           mesg (format "Reading active file%s via %s..."
1947                        (if (and where (not (zerop (length where))))
1948                            (concat " from " where) "")
1949                        (car method)))
1950     (gnus-message 5 mesg)
1951     (when (gnus-check-server method)
1952       ;; Request that the backend scan its incoming messages.
1953       (when (gnus-check-backend-function 'request-scan (car method))
1954         (gnus-request-scan nil method))
1955       (cond
1956        ((and (eq gnus-read-active-file 'some)
1957              (gnus-check-backend-function 'retrieve-groups (car method))
1958              (not force))
1959         (let ((newsrc (cdr gnus-newsrc-alist))
1960               (gmethod (gnus-server-get-method nil method))
1961               groups info)
1962           (while (setq info (pop newsrc))
1963             (when (inline
1964                     (gnus-server-equal
1965                      (inline
1966                        (gnus-find-method-for-group
1967                         (gnus-info-group info) info))
1968                      gmethod))
1969               (push (gnus-group-real-name (gnus-info-group info))
1970                     groups)))
1971           (gnus-read-active-file-2 groups method)))
1972        ((null method)
1973         t)
1974        (t
1975         (if (not (gnus-request-list method))
1976             (unless (equal method gnus-message-archive-method)
1977               (gnus-error 1 "Cannot read active file from %s server"
1978                           (car method)))
1979           (gnus-message 5 mesg)
1980           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1981           ;; We mark this active file as read.
1982           (push method gnus-have-read-active-file)
1983           (gnus-message 5 "%sdone" mesg)))))))
1984
1985 (defun gnus-read-active-file-2 (groups method)
1986   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1987   (when groups
1988     (save-excursion
1989       (set-buffer nntp-server-buffer)
1990       (gnus-check-server method)
1991       (let ((list-type (gnus-retrieve-groups groups method)))
1992         (cond ((not list-type)
1993                (gnus-error
1994                 1.2 "Cannot read partial active file from %s server."
1995                 (car method)))
1996               ((eq list-type 'active)
1997                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1998               (t
1999                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
2000
2001 ;; Read an active file and place the results in `gnus-active-hashtb'.
2002 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
2003                                              real-active)
2004   (unless method
2005     (setq method gnus-select-method))
2006   (let ((cur (current-buffer))
2007         (hashtb (or hashtb
2008                     (if (and gnus-active-hashtb
2009                              (not (equal method gnus-select-method)))
2010                         gnus-active-hashtb
2011                       (setq gnus-active-hashtb
2012                             (if (equal method gnus-select-method)
2013                                 (gnus-make-hashtable
2014                                  (count-lines (point-min) (point-max)))
2015                               (gnus-make-hashtable 4096)))))))
2016     ;; Delete unnecessary lines.
2017     (goto-char (point-min))
2018     (cond
2019      ((string= gnus-ignored-newsgroups "")
2020       (delete-matching-lines "^to\\."))
2021      (t
2022       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
2023
2024     (goto-char (point-min))
2025     (unless (re-search-forward "[\\\"]" nil t)
2026       ;; Make the group names readable as a lisp expression even if they
2027       ;; contain special characters.
2028       (goto-char (point-max))
2029       (while (re-search-backward "[][';?()#]" nil t)
2030         (insert ?\\)))
2031
2032     ;; Let the Gnus agent save the active file.
2033     (when (and gnus-agent real-active (gnus-online method))
2034       (gnus-agent-save-active method))
2035
2036     ;; If these are groups from a foreign select method, we insert the
2037     ;; group prefix in front of the group names.
2038     (when (not (gnus-server-equal
2039                 (gnus-server-get-method nil method)
2040                 (gnus-server-get-method nil gnus-select-method)))
2041       (let ((prefix (gnus-group-prefixed-name "" method)))
2042         (goto-char (point-min))
2043         (while (and (not (eobp))
2044                     (progn
2045                       (when (= (following-char) ?\")
2046                         (forward-char 1))
2047                       (insert prefix)
2048                       (zerop (forward-line 1)))))))
2049     ;; Store the active file in a hash table.
2050     (goto-char (point-min))
2051     (let (group max min)
2052       (while (not (eobp))
2053         (condition-case ()
2054             (progn
2055               (narrow-to-region (point) (gnus-point-at-eol))
2056               ;; group gets set to a symbol interned in the hash table
2057               ;; (what a hack!!) - jwz
2058               (setq group (let ((obarray hashtb)) (read cur)))
2059               ;; ### The extended group name scheme makes
2060               ;; the previous optimization strategy sort of pointless...
2061               (when (stringp group)
2062                 (setq group (intern group hashtb)))
2063               (if (and (numberp (setq max (read cur)))
2064                        (numberp (setq min (read cur)))
2065                        (progn
2066                          (skip-chars-forward " \t")
2067                          (not
2068                           (or (eq (char-after) ?=)
2069                               (eq (char-after) ?x)
2070                               (eq (char-after) ?j)))))
2071                   (progn
2072                     (set group (cons min max))
2073                     ;; if group is moderated, stick in moderation table
2074                     (when (eq (char-after) ?m)
2075                       (unless gnus-moderated-hashtb
2076                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
2077                       (gnus-sethash (symbol-name group) t
2078                                     gnus-moderated-hashtb)))
2079                 (set group nil)))
2080           (error
2081            (and group
2082                 (symbolp group)
2083                 (set group nil))
2084            (unless ignore-errors
2085              (gnus-message 3 "Warning - invalid active: %s"
2086                            (buffer-substring
2087                             (gnus-point-at-bol) (gnus-point-at-eol))))))
2088         (widen)
2089         (forward-line 1)))))
2090
2091 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
2092   ;; Parse a "groups" active file.
2093   (let ((cur (current-buffer))
2094         (hashtb (or hashtb
2095                     (if (and method gnus-active-hashtb)
2096                         gnus-active-hashtb
2097                       (setq gnus-active-hashtb
2098                             (gnus-make-hashtable
2099                              (count-lines (point-min) (point-max)))))))
2100         (prefix (and method
2101                      (not (gnus-server-equal
2102                            (gnus-server-get-method nil method)
2103                            (gnus-server-get-method nil gnus-select-method)))
2104                      (gnus-group-prefixed-name "" method))))
2105
2106     ;; Let the Gnus agent save the active file.
2107     (if (and gnus-agent
2108              real-active
2109              (gnus-online method)
2110              (gnus-agent-method-p method))
2111         (progn
2112           (gnus-agent-save-groups method)
2113           (gnus-active-to-gnus-format method hashtb nil real-active))
2114
2115       (goto-char (point-min))
2116       ;; We split this into to separate loops, one with the prefix
2117       ;; and one without to speed the reading up somewhat.
2118       (if prefix
2119           (let (min max opoint group)
2120             (while (not (eobp))
2121               (condition-case ()
2122                   (progn
2123                     (read cur) (read cur)
2124                     (setq min (read cur)
2125                           max (read cur)
2126                           opoint (point))
2127                     (skip-chars-forward " \t")
2128                     (insert prefix)
2129                     (goto-char opoint)
2130                     (set (let ((obarray hashtb)) (read cur))
2131                          (cons min max)))
2132                 (error (and group (symbolp group) (set group nil))))
2133               (forward-line 1)))
2134         (let (min max group)
2135           (while (not (eobp))
2136             (condition-case ()
2137                 (when (eq (char-after) ?2)
2138                   (read cur) (read cur)
2139                   (setq min (read cur)
2140                         max (read cur))
2141                   (set (setq group (let ((obarray hashtb)) (read cur)))
2142                        (cons min max)))
2143               (error (and group (symbolp group) (set group nil))))
2144             (forward-line 1)))))))
2145
2146 (defun gnus-read-newsrc-file (&optional force)
2147   "Read startup file.
2148 If FORCE is non-nil, the .newsrc file is read."
2149   ;; Reset variables that might be defined in the .newsrc.eld file.
2150   (gnus-clear-quick-file-variables)
2151   (let* ((newsrc-file gnus-current-startup-file)
2152          (quick-file (concat newsrc-file ".el")))
2153     (save-excursion
2154       ;; We always load the .newsrc.eld file.  If always contains
2155       ;; much information that can not be gotten from the .newsrc
2156       ;; file (ticked articles, killed groups, foreign methods, etc.)
2157       (gnus-read-newsrc-el-file quick-file)
2158
2159       (when (and gnus-read-newsrc-file
2160                  (file-exists-p gnus-current-startup-file)
2161                  (or force
2162                      (and (file-newer-than-file-p newsrc-file quick-file)
2163                           (file-newer-than-file-p newsrc-file
2164                                                   (concat quick-file "d")))
2165                      (not gnus-newsrc-alist)))
2166         ;; We read the .newsrc file.  Note that if there if a
2167         ;; .newsrc.eld file exists, it has already been read, and
2168         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2169         ;; the .newsrc file, Gnus will only use the information it
2170         ;; can find there for changing the data already read -
2171         ;; i. e., reading the .newsrc file will not trash the data
2172         ;; already read (except for read articles).
2173         (save-excursion
2174           (gnus-message 5 "Reading %s..." newsrc-file)
2175           (set-buffer (nnheader-find-file-noselect newsrc-file))
2176           (buffer-disable-undo)
2177           (gnus-newsrc-to-gnus-format)
2178           (kill-buffer (current-buffer))
2179           (gnus-message 5 "Reading %s...done" newsrc-file))))))
2180
2181 (defun gnus-load (file &optional coding-system)
2182   "Load FILE, but in such a way that read errors can be reported."
2183   (with-temp-buffer
2184     (if coding-system
2185         (insert-file-contents-as-coding-system coding-system file)
2186       (insert-file-contents file))
2187     (while (not (eobp))
2188       (condition-case type
2189           (let ((form (read (current-buffer))))
2190             (eval form))
2191         (error
2192          (unless (eq (car type) 'end-of-file)
2193            (let ((error (format "Error in %s line %d" file
2194                                 (count-lines (point-min) (point)))))
2195              (ding)
2196              (unless (gnus-yes-or-no-p (concat error "; continue? "))
2197                (error "%s" error)))))))))
2198
2199 (defun gnus-read-newsrc-el-file (file)
2200   (let ((ding-file (concat file "d")))
2201     (when (file-exists-p ding-file)
2202       ;; We always, always read the .eld file.
2203       (gnus-message 5 "Reading %s..." ding-file)
2204       (let (gnus-newsrc-assoc)
2205         (gnus-load ding-file gnus-ding-file-coding-system)
2206 ;;      ;; Older versions of `gnus-format-specs' are no longer valid
2207 ;;      ;; in Oort Gnus 0.01.
2208 ;;      (let ((version
2209 ;;             (and gnus-newsrc-file-version
2210 ;;                  (gnus-continuum-version gnus-newsrc-file-version))))
2211 ;;        (when (or (not version)
2212 ;;                  (< version 5.090009))
2213 ;;          (setq gnus-format-specs gnus-default-format-specs)))
2214         (when gnus-newsrc-assoc
2215           (setq gnus-newsrc-alist gnus-newsrc-assoc))))
2216     (gnus-make-hashtable-from-newsrc-alist)
2217     (when (file-newer-than-file-p file ding-file)
2218       ;; Old format quick file
2219       (gnus-message 5 "Reading %s..." file)
2220       ;; The .el file is newer than the .eld file, so we read that one
2221       ;; as well.
2222       (gnus-read-old-newsrc-el-file file)))
2223   (when (and gnus-product-directory
2224              (file-directory-p gnus-product-directory))
2225     (let ((list gnus-product-variable-file-list))
2226       (while list
2227         (apply 'gnus-product-read-variable-file-1 (car list))
2228         (setq list (cdr list)))))
2229   (gnus-run-hooks 'gnus-read-newsrc-el-hook))
2230
2231 ;;(defun gnus-re-read-newsrc-el-file (file)
2232 ;;  "Attempt to re-read .newsrc.eld file.  Returns nil if successful.
2233 ;;The backup file \".newsrc.eld_\" will be created before re-reading."
2234 ;;  (message "Error in %s; retrying..." file)
2235 ;;  (if (and
2236 ;;       (condition-case nil
2237 ;;         (let ((backup (concat file "_")))
2238 ;;           (copy-file file backup 'ok-if-already-exists 'keep-time)
2239 ;;           (message " (The backup file %s has been created)" backup)
2240 ;;           t)
2241 ;;       (error nil))
2242 ;;       (progn
2243 ;;       (insert-file-contents-as-binary file nil nil nil 'replace)
2244 ;;       (goto-char (point-min))
2245 ;;       (when (re-search-forward
2246 ;;              "^[\t ]*([\t\n\r ]*setq[\t\n\r ]+gnus-format-specs" nil t)
2247 ;;         (delete-region (goto-char (match-beginning 0)) (forward-list 1))
2248 ;;         (decode-coding-region (point-min) (point-max)
2249 ;;                               gnus-ding-file-coding-system)
2250 ;;         (condition-case nil
2251 ;;             (progn
2252 ;;               (eval-region (point-min) (point-max))
2253 ;;               t)
2254 ;;           (error nil)))))
2255 ;;      (prog1
2256 ;;        nil
2257 ;;      (message "Error in %s; retrying...done" file))
2258 ;;    (message "Error in %s; retrying...failed" file)
2259 ;;    t))
2260
2261 (defun gnus-product-read-variable-file-1 (file checking-methods coding
2262                                                &rest variables)
2263   (let (error gnus-product-file-version method file-ver)
2264     (when (or (condition-case err
2265                   (let ((coding-system-for-read coding)
2266                         (input-coding-system coding))
2267                     (load (expand-file-name file gnus-product-directory)
2268                           nil nil t)
2269                     nil)
2270                 (error
2271                  (message "Error while reading %s: %s"
2272                           (expand-file-name file gnus-product-directory)
2273                           (error-message-string err))
2274                  (setq error t)))
2275               (and (setq method (assq 'product-version checking-methods))
2276                    (not (and (setq file-ver
2277                                    (cdr (assq 'product-version
2278                                               gnus-product-file-version)))
2279                              (zerop (product-version-compare file-ver
2280                                                              (cadr method))))))
2281               (and (assq 'emacs-version checking-methods)
2282                    (not (and (assq 'emacs-version gnus-product-file-version)
2283                              (string-equal
2284                               emacs-version
2285                               (cdr (assq 'emacs-version
2286                                          gnus-product-file-version))))))
2287               (and (assq 'correct-string-widths checking-methods)
2288                    (not (and (assq 'correct-string-widths
2289                                    gnus-product-file-version)
2290                              (eq (and gnus-use-correct-string-widths t)
2291                                  (and (cdr (assq 'correct-string-widths
2292                                                  gnus-product-file-version))
2293                                       t))))))
2294       (unless error
2295         (message "\"%s\" seems to have mismatched contents, updating..."
2296                  file))
2297       (while variables
2298         (set (car variables) nil)
2299         (gnus-product-variable-touch (car variables))
2300         (setq variables (cdr variables))))))
2301
2302 ;; Parse the old-style quick startup file
2303 (defun gnus-read-old-newsrc-el-file (file)
2304   (let (newsrc killed marked group m info)
2305     (prog1
2306         (let ((gnus-killed-assoc nil)
2307               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2308           (prog1
2309               (ignore-errors
2310                 (load file t t t))
2311             (setq newsrc gnus-newsrc-assoc
2312                   killed gnus-killed-assoc
2313                   marked gnus-marked-assoc)))
2314       (setq gnus-newsrc-alist nil)
2315       (while (setq group (pop newsrc))
2316         (if (setq info (gnus-get-info (car group)))
2317             (progn
2318               (gnus-info-set-read info (cddr group))
2319               (gnus-info-set-level
2320                info (if (nth 1 group) gnus-level-default-subscribed
2321                       gnus-level-default-unsubscribed))
2322               (push info gnus-newsrc-alist))
2323           (push (setq info
2324                       (list (car group)
2325                             (if (nth 1 group) gnus-level-default-subscribed
2326                               gnus-level-default-unsubscribed)
2327                             (cddr group)))
2328                 gnus-newsrc-alist))
2329         ;; Copy marks into info.
2330         (when (setq m (assoc (car group) marked))
2331           (unless (nthcdr 3 info)
2332             (nconc info (list nil)))
2333           (gnus-info-set-marks
2334            info (list (cons 'tick (gnus-compress-sequence
2335                                    (sort (cdr m) '<) t))))))
2336       (setq newsrc killed)
2337       (while newsrc
2338         (setcar newsrc (caar newsrc))
2339         (setq newsrc (cdr newsrc)))
2340       (setq gnus-killed-list killed))
2341     ;; The .el file version of this variable does not begin with
2342     ;; "options", while the .eld version does, so we just add it if it
2343     ;; isn't there.
2344     (when
2345         gnus-newsrc-options
2346       (when (not (string-match "^ *options" gnus-newsrc-options))
2347         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2348       (when (not (string-match "\n$" gnus-newsrc-options))
2349         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2350       ;; Finally, if we read some options lines, we parse them.
2351       (unless (string= gnus-newsrc-options "")
2352         (gnus-newsrc-parse-options gnus-newsrc-options)))
2353
2354     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2355     (gnus-make-hashtable-from-newsrc-alist)))
2356
2357 (defun gnus-make-newsrc-file (file)
2358   "Make server dependent file name by catenating FILE and server host name."
2359   (let* ((file (expand-file-name file nil))
2360          (real-file (concat file "-" (nth 1 gnus-select-method))))
2361     (if (or (file-exists-p real-file)
2362             (file-exists-p (concat real-file ".el"))
2363             (file-exists-p (concat real-file ".eld")))
2364         real-file
2365       file)))
2366
2367 (defun gnus-newsrc-to-gnus-format ()
2368   (setq gnus-newsrc-options "")
2369   (setq gnus-newsrc-options-n nil)
2370
2371   (unless gnus-active-hashtb
2372     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2373   (let ((buf (current-buffer))
2374         (already-read (> (length gnus-newsrc-alist) 1))
2375         group subscribed options-symbol newsrc Options-symbol
2376         symbol reads num1)
2377     (goto-char (point-min))
2378     ;; We intern the symbol `options' in the active hashtb so that we
2379     ;; can `eq' against it later.
2380     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2381     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2382
2383     (while (not (eobp))
2384       ;; We first read the first word on the line by narrowing and
2385       ;; then reading into `gnus-active-hashtb'.  Most groups will
2386       ;; already exist in that hashtb, so this will save some string
2387       ;; space.
2388       (narrow-to-region
2389        (point)
2390        (progn (skip-chars-forward "^ \t!:\n") (point)))
2391       (goto-char (point-min))
2392       (setq symbol
2393             (and (/= (point-min) (point-max))
2394                  (let ((obarray gnus-active-hashtb)) (read buf))))
2395       (widen)
2396       ;; Now, the symbol we have read is either `options' or a group
2397       ;; name.  If it is an options line, we just add it to a string.
2398       (cond
2399        ((or (eq symbol options-symbol)
2400             (eq symbol Options-symbol))
2401         (setq gnus-newsrc-options
2402               ;; This concating is quite inefficient, but since our
2403               ;; thorough studies show that approx 99.37% of all
2404               ;; .newsrc files only contain a single options line, we
2405               ;; don't give a damn, frankly, my dear.
2406               (concat gnus-newsrc-options
2407                       (buffer-substring
2408                        (gnus-point-at-bol)
2409                        ;; Options may continue on the next line.
2410                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2411                                 (progn (beginning-of-line) (point)))
2412                            (point)))))
2413         (forward-line -1))
2414        (symbol
2415         ;; Group names can be just numbers.
2416         (when (numberp symbol)
2417           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2418         (unless (boundp symbol)
2419           (set symbol nil))
2420         ;; It was a group name.
2421         (setq subscribed (eq (char-after) ?:)
2422               group (symbol-name symbol)
2423               reads nil)
2424         (if (eolp)
2425             ;; If the line ends here, this is clearly a buggy line, so
2426             ;; we put point a the beginning of line and let the cond
2427             ;; below do the error handling.
2428             (beginning-of-line)
2429           ;; We skip to the beginning of the ranges.
2430           (skip-chars-forward "!: \t"))
2431         ;; We are now at the beginning of the list of read articles.
2432         ;; We read them range by range.
2433         (while
2434             (cond
2435              ((looking-at "[0-9]+")
2436               ;; We narrow and read a number instead of buffer-substring/
2437               ;; string-to-int because it's faster.  narrow/widen is
2438               ;; faster than save-restriction/narrow, and save-restriction
2439               ;; produces a garbage object.
2440               (setq num1 (progn
2441                            (narrow-to-region (match-beginning 0) (match-end 0))
2442                            (read buf)))
2443               (widen)
2444               ;; If the next character is a dash, then this is a range.
2445               (if (eq (char-after) ?-)
2446                   (progn
2447                     ;; We read the upper bound of the range.
2448                     (forward-char 1)
2449                     (if (not (looking-at "[0-9]+"))
2450                         ;; This is a buggy line, by we pretend that
2451                         ;; it's kinda OK.  Perhaps the user should be
2452                         ;; dinged?
2453                         (push num1 reads)
2454                       (push
2455                        (cons num1
2456                              (progn
2457                                (narrow-to-region (match-beginning 0)
2458                                                  (match-end 0))
2459                                (read buf)))
2460                        reads)
2461                       (widen)))
2462                 ;; It was just a simple number, so we add it to the
2463                 ;; list of ranges.
2464                 (push num1 reads))
2465               ;; If the next char in ?\n, then we have reached the end
2466               ;; of the line and return nil.
2467               (not (eq (char-after) ?\n)))
2468              ((eq (char-after) ?\n)
2469               ;; End of line, so we end.
2470               nil)
2471              (t
2472               ;; Not numbers and not eol, so this might be a buggy
2473               ;; line...
2474               (unless (eobp)
2475                 ;; If it was eob instead of ?\n, we allow it.
2476                 ;; The line was buggy.
2477                 (setq group nil)
2478                 (gnus-error 3.1 "Mangled line: %s"
2479                             (buffer-substring (gnus-point-at-bol)
2480                                               (gnus-point-at-eol))))
2481               nil))
2482           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2483           ;; we allow them, because it's a common mistake to put a
2484           ;; space after the comma.
2485           (skip-chars-forward ", "))
2486
2487         ;; We have already read .newsrc.eld, so we gently update the
2488         ;; data in the hash table with the information we have just
2489         ;; read.
2490         (when group
2491           (let ((info (gnus-get-info group))
2492                 level)
2493             (if info
2494                 ;; There is an entry for this file in the alist.
2495                 (progn
2496                   (gnus-info-set-read info (nreverse reads))
2497                   ;; We update the level very gently.  In fact, we
2498                   ;; only change it if there's been a status change
2499                   ;; from subscribed to unsubscribed, or vice versa.
2500                   (setq level (gnus-info-level info))
2501                   (cond ((and (<= level gnus-level-subscribed)
2502                               (not subscribed))
2503                          (setq level (if reads
2504                                          gnus-level-default-unsubscribed
2505                                        (1+ gnus-level-default-unsubscribed))))
2506                         ((and (> level gnus-level-subscribed) subscribed)
2507                          (setq level gnus-level-default-subscribed)))
2508                   (gnus-info-set-level info level))
2509               ;; This is a new group.
2510               (setq info (list group
2511                                (if subscribed
2512                                    gnus-level-default-subscribed
2513                                  (if reads
2514                                      (1+ gnus-level-subscribed)
2515                                    gnus-level-default-unsubscribed))
2516                                (nreverse reads))))
2517             (push info newsrc)))))
2518       (forward-line 1))
2519
2520     (setq newsrc (nreverse newsrc))
2521
2522     (if (not already-read)
2523         ()
2524       ;; We now have two newsrc lists - `newsrc', which is what we
2525       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2526       ;; what we've read from .newsrc.eld.  We have to merge these
2527       ;; lists.  We do this by "attaching" any (foreign) groups in the
2528       ;; gnus-newsrc-alist to the (native) group that precedes them.
2529       (let ((rc (cdr gnus-newsrc-alist))
2530             (prev gnus-newsrc-alist)
2531             entry mentry)
2532         (while rc
2533           (or (null (nth 4 (car rc)))   ; It's a native group.
2534               (assoc (caar rc) newsrc)  ; It's already in the alist.
2535               (if (setq entry (assoc (caar prev) newsrc))
2536                   (setcdr (setq mentry (memq entry newsrc))
2537                           (cons (car rc) (cdr mentry)))
2538                 (push (car rc) newsrc)))
2539           (setq prev rc
2540                 rc (cdr rc)))))
2541
2542     (setq gnus-newsrc-alist newsrc)
2543     ;; We make the newsrc hashtb.
2544     (gnus-make-hashtable-from-newsrc-alist)
2545
2546     ;; Finally, if we read some options lines, we parse them.
2547     (unless (string= gnus-newsrc-options "")
2548       (gnus-newsrc-parse-options gnus-newsrc-options))))
2549
2550 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2551 ;; The return value will be a list on the form
2552 ;; ((regexp1 . ignore)
2553 ;;  (regexp2 . subscribe)...)
2554 ;; When handling new newsgroups, groups that match a `ignore' regexp
2555 ;; will be ignored, and groups that match a `subscribe' regexp will be
2556 ;; subscribed.  A line like
2557 ;; options -n !all rec.all
2558 ;; will lead to a list that looks like
2559 ;; (("^rec\\..+" . subscribe)
2560 ;;  ("^.+" . ignore))
2561 ;; So all "rec.*" groups will be subscribed, while all the other
2562 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2563 ;; different from "options -n rec.all !all".
2564 (defun gnus-newsrc-parse-options (options)
2565   (let (out eol)
2566     (save-excursion
2567       (gnus-set-work-buffer)
2568       (insert (regexp-quote options))
2569       ;; First we treat all continuation lines.
2570       (goto-char (point-min))
2571       (while (re-search-forward "\n[ \t]+" nil t)
2572         (replace-match " " t t))
2573       ;; Then we transform all "all"s into ".+"s.
2574       (goto-char (point-min))
2575       (while (re-search-forward "\\ball\\b" nil t)
2576         (replace-match ".+" t t))
2577       (goto-char (point-min))
2578       ;; We remove all other options than the "-n" ones.
2579       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2580         (replace-match " ")
2581         (forward-char -1))
2582       (goto-char (point-min))
2583
2584       ;; We are only interested in "options -n" lines - we
2585       ;; ignore the other option lines.
2586       (while (re-search-forward "[ \t]-n" nil t)
2587         (setq eol
2588               (or (save-excursion
2589                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2590                          (- (point) 2)))
2591                   (gnus-point-at-eol)))
2592         ;; Search for all "words"...
2593         (while (re-search-forward "[^ \t,\n]+" eol t)
2594           (if (eq (char-after (match-beginning 0)) ?!)
2595               ;; If the word begins with a bang (!), this is a "not"
2596               ;; spec.  We put this spec (minus the bang) and the
2597               ;; symbol `ignore' into the list.
2598               (push (cons (concat
2599                            "^" (buffer-substring
2600                                 (1+ (match-beginning 0))
2601                                 (match-end 0))
2602                            "\\($\\|\\.\\)")
2603                           'ignore)
2604                     out)
2605             ;; There was no bang, so this is a "yes" spec.
2606             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2607                         'subscribe)
2608                   out))))
2609
2610       (setq gnus-newsrc-options-n out))))
2611
2612 (eval-and-compile
2613   (defalias 'gnus-long-file-names
2614     (if (fboundp 'msdos-long-file-names)
2615       'msdos-long-file-names
2616       (lambda () t))))
2617
2618 (defun gnus-save-newsrc-file (&optional force)
2619   "Save .newsrc file."
2620   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2621   ;; from the variable gnus-newsrc-alist.
2622   (when (and (or gnus-newsrc-alist gnus-killed-list)
2623              gnus-current-startup-file)
2624     (save-excursion
2625       (if (and (or gnus-use-dribble-file gnus-slave)
2626                (not force)
2627                (or (not gnus-dribble-buffer)
2628                    (not (buffer-name gnus-dribble-buffer))
2629                    (zerop (save-excursion
2630                             (set-buffer gnus-dribble-buffer)
2631                             (buffer-size)))))
2632           (gnus-message 4 "(No changes need to be saved)")
2633         (gnus-run-hooks 'gnus-save-newsrc-hook)
2634         (if gnus-slave
2635             (gnus-slave-save-newsrc)
2636           ;; Save .newsrc.
2637           (when gnus-save-newsrc-file
2638             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2639             (gnus-gnus-to-newsrc-format)
2640             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2641           ;; Save .newsrc.eld.
2642           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2643           (make-local-variable 'version-control)
2644           (setq version-control gnus-backup-startup-file)
2645           (setq buffer-file-name
2646                 (concat gnus-current-startup-file ".eld"))
2647           (setq default-directory (file-name-directory buffer-file-name))
2648           (buffer-disable-undo)
2649           (erase-buffer)
2650           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2651
2652           (if gnus-save-startup-file-via-temp-buffer
2653               (let ((coding-system-for-write gnus-ding-file-coding-system)
2654                     (output-coding-system gnus-ding-file-coding-system)
2655                     (standard-output (current-buffer)))
2656                 (gnus-gnus-to-quick-newsrc-format)
2657                 (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2658                 (save-buffer))
2659             (let ((coding-system-for-write gnus-ding-file-coding-system)
2660                   (output-coding-system gnus-ding-file-coding-system)
2661                   (version-control gnus-backup-startup-file)
2662                   (startup-file (concat gnus-current-startup-file ".eld"))
2663                   (working-dir (file-name-directory gnus-current-startup-file))
2664                   working-file
2665                   (i -1))
2666               ;; Generate the name of a non-existent file.
2667               (while (progn (setq working-file
2668                                   (format
2669                                    (if (and (eq system-type 'ms-dos)
2670                                             (not (gnus-long-file-names)))
2671                                        "%s#%d.tm#" ; MSDOS limits files to 8+3
2672                                      (if (memq system-type '(vax-vms axp-vms))
2673                                          "%s$tmp$%d"
2674                                        "%s#tmp#%d"))
2675                                    working-dir (setq i (1+ i))))
2676                             (file-exists-p working-file)))
2677
2678               (unwind-protect
2679                   (progn
2680                     (gnus-with-output-to-file working-file
2681                       (gnus-gnus-to-quick-newsrc-format)
2682                       (gnus-run-hooks 'gnus-save-quick-newsrc-hook))
2683
2684                     ;; These bindings will mislead the current buffer
2685                     ;; into thinking that it is visiting the startup
2686                     ;; file.
2687                     (let ((buffer-backed-up nil)
2688                           (buffer-file-name startup-file)
2689                           (file-precious-flag t)
2690                           (setmodes (file-modes startup-file)))
2691                       ;; Backup the current version of the startup file.
2692                       (backup-buffer)
2693
2694                       ;; Replace the existing startup file with the temp file.
2695                       (rename-file working-file startup-file t)
2696                       (set-file-modes startup-file setmodes)))
2697                 (condition-case nil
2698                     (delete-file working-file)
2699                   (file-error nil)))))
2700
2701           (gnus-kill-buffer (current-buffer))
2702           (gnus-message
2703            5 "Saving %s.eld...done" gnus-current-startup-file))
2704         (gnus-dribble-delete-file)
2705         (gnus-group-set-mode-line))))
2706   (when gnus-product-directory
2707     (gnus-product-save-variable-file)))
2708
2709 ;; Call the function above at C-x C-c.
2710 (defadvice save-buffers-kill-emacs (before save-gnus-newsrc-file-maybe
2711                                            activate preactivate)
2712   "Save .newsrc and .newsrc.eld when Emacs is killed."
2713   (when (gnus-alive-p)
2714     (gnus-run-hooks 'gnus-exit-gnus-hook)
2715     (gnus-offer-save-summaries)
2716     (gnus-save-newsrc-file)))
2717
2718 (defun gnus-gnus-to-quick-newsrc-format ()
2719   "Print Gnus variables such as gnus-newsrc-alist in lisp format."
2720     (princ ";; -*- emacs-lisp -*-\n")
2721     (princ ";; Gnus startup file.\n")
2722     (princ "\
2723 ;; Never delete this file -- if you want to force Gnus to read the
2724 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2725     (princ "(setq gnus-newsrc-file-version ")
2726     (princ (gnus-prin1-to-string gnus-version))
2727     (princ ")\n")
2728     (let* ((print-length nil)
2729            (gnus-killed-list
2730             (if (and gnus-save-killed-list
2731                      (stringp gnus-save-killed-list))
2732                 (gnus-strip-killed-list)
2733               gnus-killed-list))
2734            (variables
2735             (if gnus-save-killed-list gnus-variable-list
2736               ;; Remove the `gnus-killed-list' from the list of variables
2737               ;; to be saved, if required.
2738               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2739            ;; Peel off the "dummy" group.
2740            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2741            variable)
2742       ;; Insert the variables into the file.
2743       (while variables
2744         (when (and (boundp (setq variable (pop variables)))
2745                    (symbol-value variable))
2746           (princ "(setq ")
2747           (princ (symbol-name variable))
2748           (princ " '")
2749           (prin1 (symbol-value variable))
2750           (princ ")\n")))))
2751
2752 (defun gnus-product-variable-touch (&rest variables)
2753   (while variables
2754     (put (pop variables) 'gnus-product-variable 'dirty)))
2755
2756 (defun gnus-product-variables-dirty-p (variables)
2757   (catch 'done
2758     (while variables
2759       (when (eq (get (car variables) 'gnus-product-variable) 'dirty)
2760         (throw 'done t))
2761       (setq variables (cdr variables)))))
2762
2763 (defun gnus-product-save-variable-file (&optional force)
2764   "Save all product variables to files, when need to be saved."
2765   (let ((list gnus-product-variable-file-list))
2766     (gnus-make-directory gnus-product-directory)
2767     (while list
2768       (apply 'gnus-product-save-variable-file-1 force (car list))
2769       (setq list (cdr list)))))
2770
2771 (defun gnus-product-save-variable-file-1 (force file checking-methods coding
2772                                                 &rest variables)
2773   "Save a product variable file, when need to be saved."
2774   (when (or force
2775             (gnus-product-variables-dirty-p variables))
2776     (let ((product (product-find 'gnus-vers)))
2777       (set-buffer (gnus-get-buffer-create " *gnus-product*"))
2778       (make-local-variable 'version-control)
2779       (setq version-control 'never)
2780       (setq file (expand-file-name file gnus-product-directory)
2781             buffer-file-name file
2782             default-directory (file-name-directory file))
2783       (buffer-disable-undo)
2784       (erase-buffer)
2785       (gnus-message 5 "Saving %s..." file)
2786       (apply 'gnus-product-quick-file-format product checking-methods coding
2787              variables)
2788       (save-buffer-as-coding-system coding)
2789       (kill-buffer (current-buffer))
2790       (while variables
2791         (put (car variables) 'gnus-product-variable nil)
2792         (setq variables (cdr variables)))
2793       (gnus-message
2794        5 "Saving %s...done" file))))
2795
2796 (defun gnus-product-quick-file-format (product checking-methods
2797                                                coding &rest variables)
2798   "Insert gnus product depend variables in lisp format."
2799   (let ((print-quoted t)
2800         (print-escape-newlines t)
2801         print-length print-level variable param)
2802     (insert (format ";; -*- Mode: emacs-lisp; coding: %s -*-\n" coding))
2803     (insert (format ";; %s startup file.\n" (product-name product)))
2804     (when (setq param (cdr (assq 'product-version checking-methods)))
2805       (insert "(or (>= (product-version-compare "
2806               "(product-version (product-find 'gnus-vers))\n"
2807               "\t\t\t\t '" (apply 'prin1-to-string param) ")\n"
2808               "\t0)\n"
2809               "    (error \"This file was created by later version of "
2810               "gnus.\"))\n"))
2811     (insert "(setq gnus-product-file-version\n"
2812             "      '((product-version . "
2813             (prin1-to-string (product-version product)) ")\n"
2814             "\t(emacs-version . "
2815             (prin1-to-string emacs-version) ")\n"
2816             "\t(correct-string-widths . "
2817             (if gnus-use-correct-string-widths "t" "nil")
2818             ")))\n")
2819     (while variables
2820       (when (and (boundp (setq variable (pop variables)))
2821                  (symbol-value variable))
2822         (insert "(setq " (symbol-name variable) " '")
2823         (gnus-prin1 (symbol-value variable))
2824         (insert ")\n")))))
2825
2826 (defun gnus-strip-killed-list ()
2827   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2828   (let ((list gnus-killed-list)
2829         olist)
2830     (while list
2831       (when (string-match gnus-save-killed-list (car list))
2832         (push (car list) olist))
2833       (pop list))
2834     (nreverse olist)))
2835
2836 (defun gnus-gnus-to-newsrc-format ()
2837   ;; Generate and save the .newsrc file.
2838   (save-excursion
2839     (set-buffer (create-file-buffer gnus-current-startup-file))
2840     (let ((newsrc (cdr gnus-newsrc-alist))
2841           (standard-output (current-buffer))
2842           info ranges range method)
2843       (setq buffer-file-name gnus-current-startup-file)
2844       (setq default-directory (file-name-directory buffer-file-name))
2845       (buffer-disable-undo)
2846       (erase-buffer)
2847       ;; Write options.
2848       (when gnus-newsrc-options
2849         (insert gnus-newsrc-options))
2850       ;; Write subscribed and unsubscribed.
2851       (while (setq info (pop newsrc))
2852         ;; Don't write foreign groups to .newsrc.
2853         (when (or (null (setq method (gnus-info-method info)))
2854                   (equal method "native")
2855                   (inline (gnus-server-equal method gnus-select-method)))
2856           (insert (gnus-info-group info)
2857                   (if (> (gnus-info-level info) gnus-level-subscribed)
2858                       "!" ":"))
2859           (when (setq ranges (gnus-info-read info))
2860             (insert " ")
2861             (if (not (listp (cdr ranges)))
2862                 (if (= (car ranges) (cdr ranges))
2863                     (princ (car ranges))
2864                   (princ (car ranges))
2865                   (insert "-")
2866                   (princ (cdr ranges)))
2867               (while (setq range (pop ranges))
2868                 (if (or (atom range) (= (car range) (cdr range)))
2869                     (princ (or (and (atom range) range) (car range)))
2870                   (princ (car range))
2871                   (insert "-")
2872                   (princ (cdr range)))
2873                 (when ranges
2874                   (insert ",")))))
2875           (insert "\n")))
2876       (make-local-variable 'version-control)
2877       (setq version-control 'never)
2878       ;; It has been reported that sometime the modtime on the .newsrc
2879       ;; file seems to be off.  We really do want to overwrite it, so
2880       ;; we clear the modtime here before saving.  It's a bit odd,
2881       ;; though...
2882       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2883       ;; delete the silly thing entirely first.  but this fails to provide
2884       ;; such niceties as .newsrc~ creation.
2885       (if gnus-modtime-botch
2886           (delete-file gnus-startup-file)
2887         (clear-visited-file-modtime))
2888       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2889       (save-buffer)
2890       (kill-buffer (current-buffer)))))
2891
2892 \f
2893 ;;;
2894 ;;; Slave functions.
2895 ;;;
2896
2897 (defvar gnus-slave-mode nil)
2898
2899 (defun gnus-slave-mode ()
2900   "Minor mode for slave Gnusae."
2901   (gnus-add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2902   (gnus-run-hooks 'gnus-slave-mode-hook))
2903
2904 (defun gnus-slave-save-newsrc ()
2905   (save-excursion
2906     (set-buffer gnus-dribble-buffer)
2907     (let ((slave-name
2908            (mm-make-temp-file (concat gnus-current-startup-file "-slave-")))
2909           (modes (ignore-errors
2910                    (file-modes (concat gnus-current-startup-file ".eld")))))
2911       (gnus-write-buffer-as-coding-system gnus-ding-file-coding-system
2912                                           slave-name)
2913       (when modes
2914         (set-file-modes slave-name modes)))))
2915
2916 (defun gnus-master-read-slave-newsrc ()
2917   (let ((slave-files
2918          (directory-files
2919           (file-name-directory gnus-current-startup-file)
2920           t (concat
2921              "^" (regexp-quote
2922                   (concat
2923                    (file-name-nondirectory gnus-current-startup-file)
2924                    "-slave-")))
2925           t))
2926         file)
2927     (if (not slave-files)
2928         ()                              ; There are no slave files to read.
2929       (gnus-message 7 "Reading slave newsrcs...")
2930       (save-excursion
2931         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2932         (setq slave-files
2933               (sort (mapcar (lambda (file)
2934                               (list (nth 5 (file-attributes file)) file))
2935                             slave-files)
2936                     (lambda (f1 f2)
2937                       (or (< (caar f1) (caar f2))
2938                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2939         (while slave-files
2940           (erase-buffer)
2941           (setq file (nth 1 (car slave-files)))
2942           (nnheader-insert-file-contents file)
2943           (when (condition-case ()
2944                     (progn
2945                       (eval-buffer (current-buffer))
2946                       t)
2947                   (error
2948                    (gnus-error 3.2 "Possible error in %s" file)
2949                    nil))
2950             (unless gnus-slave          ; Slaves shouldn't delete these files.
2951               (ignore-errors
2952                 (delete-file file))))
2953           (setq slave-files (cdr slave-files))))
2954       (gnus-dribble-touch)
2955       (gnus-message 7 "Reading slave newsrcs...done"))))
2956
2957 \f
2958 ;;;
2959 ;;; Group description.
2960 ;;;
2961
2962 (defun gnus-read-all-descriptions-files ()
2963   (let ((methods (cons gnus-select-method
2964                        (nconc
2965                         (when (gnus-archive-server-wanted-p)
2966                           (list "archive"))
2967                         gnus-secondary-select-methods))))
2968     (while methods
2969       (gnus-read-descriptions-file (car methods))
2970       (setq methods (cdr methods)))
2971     t))
2972
2973 (defun gnus-read-descriptions-file (&optional method)
2974   (let ((method (or method gnus-select-method))
2975         group)
2976     (when (stringp method)
2977       (setq method (gnus-server-to-method method)))
2978     ;; We create the hashtable whether we manage to read the desc file
2979     ;; to avoid trying to re-read after a failed read.
2980     (unless gnus-description-hashtb
2981       (setq gnus-description-hashtb
2982             (gnus-make-hashtable (length gnus-active-hashtb))))
2983     ;; Mark this method's desc file as read.
2984     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2985                   gnus-description-hashtb)
2986
2987     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2988     (cond
2989      ((null (gnus-get-function method 'request-list-newsgroups t))
2990       t)
2991      ((not (gnus-check-server method))
2992       (gnus-message 1 "Couldn't open server")
2993       nil)
2994      ((not (gnus-request-list-newsgroups method))
2995       (gnus-message 1 "Couldn't read newsgroups descriptions")
2996       nil)
2997      (t
2998       (save-excursion
2999         (save-restriction
3000           (set-buffer nntp-server-buffer)
3001           (goto-char (point-min))
3002           (when (or (search-forward "\n.\n" nil t)
3003                     (goto-char (point-max)))
3004             (beginning-of-line)
3005             (narrow-to-region (point-min) (point)))
3006           ;; If these are groups from a foreign select method, we insert the
3007           ;; group prefix in front of the group names.
3008           (and method (not (inline
3009                              (gnus-server-equal
3010                               (gnus-server-get-method nil method)
3011                               (gnus-server-get-method
3012                                nil gnus-select-method))))
3013                (let ((prefix (gnus-group-prefixed-name "" method)))
3014                  (goto-char (point-min))
3015                  (while (and (not (eobp))
3016                              (progn (insert prefix)
3017                                     (zerop (forward-line 1)))))))
3018           (goto-char (point-min))
3019           (while (not (eobp))
3020             ;; If we get an error, we set group to 0, which is not a
3021             ;; symbol...
3022             (setq group
3023                   (condition-case ()
3024                       (let ((obarray gnus-description-hashtb))
3025                         ;; Group is set to a symbol interned in this
3026                         ;; hash table.
3027                         (read nntp-server-buffer))
3028                     (error 0)))
3029             (skip-chars-forward " \t")
3030             ;; ...  which leads to this line being effectively ignored.
3031             (when (symbolp group)
3032               (let* ((str (buffer-substring
3033                            (point) (progn (end-of-line) (point))))
3034                      (name (symbol-name group))
3035                      (charset
3036                       (or (gnus-group-name-charset method name)
3037                           (gnus-parameter-charset name)
3038                           gnus-default-charset)))
3039                 (when (and str charset (featurep 'mule))
3040                   (setq str (decode-coding-string str charset)))
3041                 (set group str)))
3042             (forward-line 1))))
3043       (gnus-message 5 "Reading descriptions file...done")
3044       t))))
3045
3046 (defun gnus-group-get-description (group)
3047   "Get the description of a group by sending XGTITLE to the server."
3048   (when (gnus-request-group-description group)
3049     (save-excursion
3050       (set-buffer nntp-server-buffer)
3051       (goto-char (point-min))
3052       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
3053         (match-string 1)))))
3054
3055 ;;;###autoload
3056 (defun gnus-declare-backend (name &rest abilities)
3057   "Declare backend NAME with ABILITIES as a Gnus backend."
3058   (setq gnus-valid-select-methods
3059         (nconc gnus-valid-select-methods
3060                (list (apply 'list name abilities))))
3061   (gnus-redefine-select-method-widget))
3062
3063 (defun gnus-set-default-directory ()
3064   "Set the default directory in the current buffer to `gnus-default-directory'.
3065 If this variable is nil, don't do anything."
3066   (setq default-directory
3067         (if (and gnus-default-directory
3068                  (file-exists-p gnus-default-directory))
3069             (file-name-as-directory (expand-file-name gnus-default-directory))
3070           default-directory)))
3071
3072 (eval-and-compile
3073 (defalias 'gnus-display-time-event-handler 
3074   (if (gnus-boundp 'display-time-timer)
3075       'display-time-event-handler
3076     (lambda () "Does nothing as `display-time-timer' is not bound.
3077 Would otherwise be an alias for `display-time-event-handler'." nil))))
3078
3079 ;;;###autoload
3080 (defun gnus-fixup-nnimap-unread-after-getting-new-news ()
3081   (let (server group info)
3082     (mapatoms
3083      (lambda (sym)
3084        (when (and (setq group (symbol-name sym))
3085                   (gnus-group-entry group)
3086                   (setq info (symbol-value sym)))
3087          (gnus-sethash group (cons (nth 2 info) (cdr (gnus-group-entry group)))
3088                        gnus-newsrc-hashtb)))
3089      (if (boundp 'nnimap-mailbox-info)
3090          (symbol-value 'nnimap-mailbox-info)
3091        (make-vector 1 0)))))
3092
3093
3094 (provide 'gnus-start)
3095
3096 ;;; gnus-start.el ends here