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