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