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