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