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