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