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