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