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