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