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