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