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