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