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