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