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