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