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