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