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