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