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