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