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