*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'nnoo)
36
37 (eval-when-compile (require 'cl))
38
39 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
40   "*Directory variable from which all other Gnus file variables are derived.")
41
42 ;; Site dependent variables.  These variables should be defined in
43 ;; paths.el.
44
45 (defvar gnus-default-nntp-server nil
46   "Specify a default NNTP server.
47 This variable should be defined in paths.el, and should never be set
48 by the user.
49 If you want to change servers, you should use `gnus-select-method'.
50 See the documentation to that variable.")
51
52 (defvar gnus-backup-default-subscribed-newsgroups
53   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
54   "Default default new newsgroups the first time Gnus is run.
55 Should be set in paths.el, and shouldn't be touched by the user.")
56
57 (defvar gnus-local-domain nil
58   "Local domain name without a host name.
59 The DOMAINNAME environment variable is used instead if it is defined.
60 If the `system-name' function returns the full Internet name, there is
61 no need to set this variable.")
62
63 (defvar gnus-local-organization nil
64   "String with a description of what organization (if any) the user belongs to.
65 The ORGANIZATION environment variable is used instead if it is defined.
66 If this variable contains a function, this function will be called
67 with the current newsgroup name as the argument.  The function should
68 return a string.
69
70 In any case, if the string (either in the variable, in the environment
71 variable, or returned by the function) is a file name, the contents of
72 this file will be used as the organization.")
73
74 ;; Customization variables
75
76 ;; Don't touch this variable.
77 (defvar gnus-nntp-service "nntp"
78   "*NNTP service name (\"nntp\" or 119).
79 This is an obsolete variable, which is scarcely used.  If you use an
80 nntp server for your newsgroup and want to change the port number
81 used to 899, you would say something along these lines:
82
83  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
84
85 (defvar gnus-nntpserver-file "/etc/nntpserver"
86   "*A file with only the name of the nntp server in it.")
87
88 ;; This function is used to check both the environment variable
89 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
90 ;; an nntp server name default.
91 (defun gnus-getenv-nntpserver ()
92   (or (getenv "NNTPSERVER")
93       (and (file-readable-p gnus-nntpserver-file)
94            (save-excursion
95              (set-buffer (get-buffer-create " *gnus nntp*"))
96              (buffer-disable-undo (current-buffer))
97              (insert-file-contents gnus-nntpserver-file)
98              (let ((name (buffer-string)))
99                (prog1
100                    (if (string-match "^[ \t\n]*$" name)
101                        nil
102                      name)
103                  (kill-buffer (current-buffer))))))))
104
105 (defvar gnus-select-method
106   (nconc
107    (list 'nntp (or (condition-case ()
108                        (gnus-getenv-nntpserver)
109                      (error nil))
110                    (if (and gnus-default-nntp-server
111                             (not (string= gnus-default-nntp-server "")))
112                        gnus-default-nntp-server)
113                    (system-name)))
114    (if (or (null gnus-nntp-service)
115            (equal gnus-nntp-service "nntp"))
116        nil
117      (list gnus-nntp-service)))
118   "*Default method for selecting a newsgroup.
119 This variable should be a list, where the first element is how the
120 news is to be fetched, the second is the address.
121
122 For instance, if you want to get your news via NNTP from
123 \"flab.flab.edu\", you could say:
124
125 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
126
127 If you want to use your local spool, say:
128
129 (setq gnus-select-method (list 'nnspool (system-name)))
130
131 If you use this variable, you must set `gnus-nntp-server' to nil.
132
133 There is a lot more to know about select methods and virtual servers -
134 see the manual for details.")
135
136 (defvar gnus-message-archive-method 
137   `(nnfolder
138     "archive"
139     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
140     (nnfolder-active-file 
141      ,(nnheader-concat message-directory "archive/active"))
142     (nnfolder-get-new-mail nil)
143     (nnfolder-inhibit-expiry t))
144   "*Method used for archiving messages you've sent.
145 This should be a mail method.
146
147 It's probably not a very effective to change this variable once you've
148 run Gnus once.  After doing that, you must edit this server from the
149 server buffer.")
150
151 (defvar gnus-message-archive-group nil
152   "*Name of the group in which to save the messages you've written.
153 This can either be a string, a list of strings; or an alist
154 of regexps/functions/forms to be evaluated to return a string (or a list
155 of strings).  The functions are called with the name of the current
156 group (or nil) as a parameter.
157
158 If you want to save your mail in one group and the news articles you
159 write in another group, you could say something like:
160
161  \(setq gnus-message-archive-group 
162         '((if (message-news-p)
163               \"misc-news\" 
164             \"misc-mail\")))
165
166 Normally the group names returned by this variable should be
167 unprefixed -- which implictly means \"store on the archive server\".
168 However, you may wish to store the message on some other server.  In
169 that case, just return a fully prefixed name of the group --
170 \"nnml+private:mail.misc\", for instance.")
171
172 (defvar gnus-refer-article-method nil
173   "*Preferred method for fetching an article by Message-ID.
174 If you are reading news from the local spool (with nnspool), fetching
175 articles by Message-ID is painfully slow.  By setting this method to an
176 nntp method, you might get acceptable results.
177
178 The value of this variable must be a valid select method as discussed
179 in the documentation of `gnus-select-method'.")
180
181 (defvar gnus-secondary-select-methods nil
182   "*A list of secondary methods that will be used for reading news.
183 This is a list where each element is a complete select method (see
184 `gnus-select-method').
185
186 If, for instance, you want to read your mail with the nnml backend,
187 you could set this variable:
188
189 (setq gnus-secondary-select-methods '((nnml \"\")))")
190
191 (defvar gnus-secondary-servers nil
192   "*List of NNTP servers that the user can choose between interactively.
193 To make Gnus query you for a server, you have to give `gnus' a
194 non-numeric prefix - `C-u M-x gnus', in short.")
195
196 (defvar gnus-nntp-server nil
197   "*The name of the host running the NNTP server.
198 This variable is semi-obsolete.  Use the `gnus-select-method'
199 variable instead.")
200
201 (defvar gnus-startup-file "~/.newsrc"
202   "*Your `.newsrc' file.
203 `.newsrc-SERVER' will be used instead if that exists.")
204
205 (defvar gnus-init-file "~/.gnus"
206   "*Your Gnus elisp startup file.
207 If a file with the .el or .elc suffixes exist, it will be read
208 instead.")
209
210 (defvar gnus-group-faq-directory
211   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
212     "/ftp@sunsite.auc.dk:/pub/usenet/"
213     "/ftp@sunsite.doc.ic.ac.uk:/pub/usenet/news-faqs/"
214     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
215     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
216     "/ftp@rtfm.mit.edu:/pub/usenet/"
217     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
218     "/ftp@ftp.sunet.se:/pub/usenet/"
219     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
220     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
221     "/ftp@ftp.hk.super.net:/mirror/faqs/")
222   "*Directory where the group FAQs are stored.
223 This will most commonly be on a remote machine, and the file will be
224 fetched by ange-ftp.
225
226 This variable can also be a list of directories.  In that case, the
227 first element in the list will be used by default.  The others can
228 be used when being prompted for a site.
229
230 Note that Gnus uses an aol machine as the default directory.  If this
231 feels fundamentally unclean, just think of it as a way to finally get
232 something of value back from them.
233
234 If the default site is too slow, try one of these:
235
236    North America: mirrors.aol.com                /pub/rtfm/usenet
237                   ftp.seas.gwu.edu               /pub/rtfm
238                   rtfm.mit.edu                   /pub/usenet
239    Europe:        ftp.uni-paderborn.de           /pub/FAQ
240                   src.doc.ic.ac.uk               /usenet/news-FAQS
241                   ftp.sunet.se                   /pub/usenet
242                   sunsite.auc.dk                 /pub/usenet
243    Asia:          nctuccca.edu.tw                /USENET/FAQ
244                   hwarang.postech.ac.kr          /pub/usenet
245                   ftp.hk.super.net               /mirror/faqs")
246
247 (defvar gnus-group-archive-directory
248   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
249   "*The address of the (ding) archives.")
250
251 (defvar gnus-group-recent-archive-directory
252   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
253   "*The address of the most recent (ding) articles.")
254
255 (defvar gnus-default-subscribed-newsgroups nil
256   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
257 It should be a list of strings.
258 If it is `t', Gnus will not do anything special the first time it is
259 started; it'll just use the normal newsgroups subscription methods.")
260
261 (defvar gnus-use-cross-reference t
262   "*Non-nil means that cross referenced articles will be marked as read.
263 If nil, ignore cross references.  If t, mark articles as read in
264 subscribed newsgroups.  If neither t nor nil, mark as read in all
265 newsgroups.")
266
267 (defvar gnus-single-article-buffer t
268   "*If non-nil, display all articles in the same buffer.
269 If nil, each group will get its own article buffer.")
270
271 (defvar gnus-use-dribble-file t
272   "*Non-nil means that Gnus will use a dribble file to store user updates.
273 If Emacs should crash without saving the .newsrc files, complete
274 information can be restored from the dribble file.")
275
276 (defvar gnus-dribble-directory nil
277   "*The directory where dribble files will be saved.
278 If this variable is nil, the directory where the .newsrc files are
279 saved will be used.")
280
281 (defvar gnus-asynchronous nil
282   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
283
284 (defvar gnus-kill-summary-on-exit t
285   "*If non-nil, kill the summary buffer when you exit from it.
286 If nil, the summary will become a \"*Dead Summary*\" buffer, and
287 it will be killed sometime later.")
288
289 (defvar gnus-large-newsgroup 200
290   "*The number of articles which indicates a large newsgroup.
291 If the number of articles in a newsgroup is greater than this value,
292 confirmation is required for selecting the newsgroup.")
293
294 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
295 (defvar gnus-no-groups-message "No news is horrible news"
296   "*Message displayed by Gnus when no groups are available.")
297
298 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
299   "*Non-nil means that the default name of a file to save articles in is the group name.
300 If it's nil, the directory form of the group name is used instead.
301
302 If this variable is a list, and the list contains the element
303 `not-score', long file names will not be used for score files; if it
304 contains the element `not-save', long file names will not be used for
305 saving; and if it contains the element `not-kill', long file names
306 will not be used for kill files.
307
308 Note that the default for this variable varies according to what system
309 type you're using.  On `usg-unix-v' and `xenix' this variable defaults
310 to nil while on all other systems it defaults to t.")
311
312 (defvar gnus-article-save-directory gnus-directory
313   "*Name of the directory articles will be saved in (default \"~/News\").")
314
315 (defvar gnus-kill-files-directory gnus-directory
316   "*Name of the directory where kill files will be stored (default \"~/News\").")
317
318 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
319   "*A function to save articles in your favorite format.
320 The function must be interactively callable (in other words, it must
321 be an Emacs command).
322
323 Gnus provides the following functions:
324
325 * gnus-summary-save-in-rmail (Rmail format)
326 * gnus-summary-save-in-mail (Unix mail format)
327 * gnus-summary-save-in-folder (MH folder)
328 * gnus-summary-save-in-file (article format).
329 * gnus-summary-save-in-vm (use VM's folder format).")
330
331 (defvar gnus-prompt-before-saving 'always
332   "*This variable says how much prompting is to be done when saving articles.
333 If it is nil, no prompting will be done, and the articles will be
334 saved to the default files.  If this variable is `always', each and
335 every article that is saved will be preceded by a prompt, even when
336 saving large batches of articles.  If this variable is neither nil not
337 `always', there the user will be prompted once for a file name for
338 each invocation of the saving commands.")
339
340 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
341   "*A function generating a file name to save articles in Rmail format.
342 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
343
344 (defvar gnus-mail-save-name (function gnus-plain-save-name)
345   "*A function generating a file name to save articles in Unix mail format.
346 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
347
348 (defvar gnus-folder-save-name (function gnus-folder-save-name)
349   "*A function generating a file name to save articles in MH folder.
350 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
351
352 (defvar gnus-file-save-name (function gnus-numeric-save-name)
353   "*A function generating a file name to save articles in article format.
354 The function is called with NEWSGROUP, HEADERS, and optional
355 LAST-FILE.")
356
357 (defvar gnus-split-methods
358   '((gnus-article-archive-name))
359   "*Variable used to suggest where articles are to be saved.
360 For instance, if you would like to save articles related to Gnus in
361 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
362 you could set this variable to something like:
363
364  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
365    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
366
367 This variable is an alist where the where the key is the match and the
368 value is a list of possible files to save in if the match is non-nil.
369
370 If the match is a string, it is used as a regexp match on the
371 article.  If the match is a symbol, that symbol will be funcalled
372 from the buffer of the article to be saved with the newsgroup as the
373 parameter.  If it is a list, it will be evaled in the same buffer.
374
375 If this form or function returns a string, this string will be used as
376 a possible file name; and if it returns a non-nil list, that list will
377 be used as possible file names.")
378
379 (defvar gnus-move-split-methods nil
380   "*Variable used to suggest where articles are to be moved to.
381 It uses the same syntax as the `gnus-split-methods' variable.")
382
383 (defvar gnus-save-score nil
384   "*If non-nil, save group scoring info.")
385
386 (defvar gnus-use-adaptive-scoring nil
387   "*If non-nil, use some adaptive scoring scheme.")
388
389 (defvar gnus-use-cache 'passive
390   "*If nil, Gnus will ignore the article cache.
391 If `passive', it will allow entering (and reading) articles
392 explicitly entered into the cache.  If anything else, use the
393 cache to the full extent of the law.")
394
395 (defvar gnus-use-trees nil
396   "*If non-nil, display a thread tree buffer.")
397
398 (defvar gnus-use-grouplens nil
399   "*If non-nil, use GroupLens ratings.")
400
401 (defvar gnus-keep-backlog nil
402   "*If non-nil, Gnus will keep read articles for later re-retrieval.
403 If it is a number N, then Gnus will only keep the last N articles
404 read.  If it is neither nil nor a number, Gnus will keep all read
405 articles.  This is not a good idea.")
406
407 (defvar gnus-use-nocem nil
408   "*If non-nil, Gnus will read NoCeM cancel messages.")
409
410 (defvar gnus-use-demon nil
411   "If non-nil, Gnus might use some demons.")
412
413 (defvar gnus-use-scoring t
414   "*If non-nil, enable scoring.")
415
416 (defvar gnus-use-picons nil
417   "*If non-nil, display picons.")
418
419 (defvar gnus-fetch-old-headers nil
420   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
421 If an unread article in the group refers to an older, already read (or
422 just marked as read) article, the old article will not normally be
423 displayed in the Summary buffer.  If this variable is non-nil, Gnus
424 will attempt to grab the headers to the old articles, and thereby
425 build complete threads.  If it has the value `some', only enough
426 headers to connect otherwise loose threads will be displayed.
427 This variable can also be a number.  In that case, no more than that
428 number of old headers will be fetched.
429
430 The server has to support NOV for any of this to work.")
431
432 ;see gnus-cus.el
433 ;(defvar gnus-visual t
434 ;  "*If non-nil, will do various highlighting.
435 ;If nil, no mouse highlights (or any other highlights) will be
436 ;performed.  This might speed up Gnus some when generating large group
437 ;and summary buffers.")
438
439 (defvar gnus-novice-user t
440   "*Non-nil means that you are a usenet novice.
441 If non-nil, verbose messages may be displayed and confirmations may be
442 required.")
443
444 (defvar gnus-expert-user nil
445   "*Non-nil means that you will never be asked for confirmation about anything.
446 And that means *anything*.")
447
448 (defvar gnus-verbose 7
449   "*Integer that says how verbose Gnus should be.
450 The higher the number, the more messages Gnus will flash to say what
451 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
452 display most important messages; and at ten, Gnus will keep on
453 jabbering all the time.")
454
455 (defvar gnus-keep-same-level nil
456   "*Non-nil means that the next newsgroup after the current will be on the same level.
457 When you type, for instance, `n' after reading the last article in the
458 current newsgroup, you will go to the next newsgroup.  If this variable
459 is nil, the next newsgroup will be the next from the group
460 buffer.
461 If this variable is non-nil, Gnus will either put you in the
462 next newsgroup with the same level, or, if no such newsgroup is
463 available, the next newsgroup with the lowest possible level higher
464 than the current level.
465 If this variable is `best', Gnus will make the next newsgroup the one
466 with the best level.")
467
468 (defvar gnus-summary-make-false-root 'adopt
469   "*nil means that Gnus won't gather loose threads.
470 If the root of a thread has expired or been read in a previous
471 session, the information necessary to build a complete thread has been
472 lost.  Instead of having many small sub-threads from this original thread
473 scattered all over the summary buffer, Gnus can gather them.
474
475 If non-nil, Gnus will try to gather all loose sub-threads from an
476 original thread into one large thread.
477
478 If this variable is non-nil, it should be one of `none', `adopt',
479 `dummy' or `empty'.
480
481 If this variable is `none', Gnus will not make a false root, but just
482 present the sub-threads after another.
483 If this variable is `dummy', Gnus will create a dummy root that will
484 have all the sub-threads as children.
485 If this variable is `adopt', Gnus will make one of the \"children\"
486 the parent and mark all the step-children as such.
487 If this variable is `empty', the \"children\" are printed with empty
488 subject fields.  (Or rather, they will be printed with a string
489 given by the `gnus-summary-same-subject' variable.)")
490
491 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
492   "*A regexp to match subjects to be excluded from loose thread gathering.
493 As loose thread gathering is done on subjects only, that means that
494 there can be many false gatherings performed.  By rooting out certain
495 common subjects, gathering might become saner.")
496
497 (defvar gnus-summary-gather-subject-limit nil
498   "*Maximum length of subject comparisons when gathering loose threads.
499 Use nil to compare full subjects.  Setting this variable to a low
500 number will help gather threads that have been corrupted by
501 newsreaders chopping off subject lines, but it might also mean that
502 unrelated articles that have subject that happen to begin with the
503 same few characters will be incorrectly gathered.
504
505 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
506 comparing subjects.")
507
508 (defvar gnus-simplify-ignored-prefixes nil
509   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily.")
510
511 (defvar gnus-build-sparse-threads nil
512   "*If non-nil, fill in the gaps in threads.
513 If `some', only fill in the gaps that are needed to tie loose threads
514 together.  If `more', fill in all leaf nodes that Gnus can find.  If
515 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
516
517 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
518   "Function used for gathering loose threads.
519 There are two pre-defined functions: `gnus-gather-threads-by-subject',
520 which only takes Subjects into consideration; and
521 `gnus-gather-threads-by-references', which compared the References
522 headers of the articles to find matches.")
523
524 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
525 (defvar gnus-summary-same-subject ""
526   "*String indicating that the current article has the same subject as the previous.
527 This variable will only be used if the value of
528 `gnus-summary-make-false-root' is `empty'.")
529
530 (defvar gnus-summary-goto-unread t
531   "*If non-nil, marking commands will go to the next unread article.
532 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
533 whether it is read or not.")
534
535 (defvar gnus-group-goto-unread t
536   "*If non-nil, movement commands will go to the next unread and subscribed group.")
537
538 (defvar gnus-goto-next-group-when-activating t
539   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
540
541 (defvar gnus-check-new-newsgroups t
542   "*Non-nil means that Gnus will run gnus-find-new-newsgroups at startup.
543 This normally finds new newsgroups by comparing the active groups the
544 servers have already reported with those Gnus already knows, either alive
545 or killed.
546
547 When any of the following are true, gnus-find-new-newsgroups will instead
548 ask the servers (primary, secondary, and archive servers) to list new
549 groups since the last time it checked:
550   1. This variable is `ask-server'.
551   2. This variable is a list of select methods (see below).
552   3. `gnus-read-active-file' is nil or `some'.
553   4. A prefix argument is given to gnus-find-new-newsgroups interactively.
554
555 Thus, if this variable is `ask-server' or a list of select methods or
556 `gnus-read-active-file' is nil or `some', then the killed list is no
557 longer necessary, so you could safely set `gnus-save-killed-list' to nil.
558
559 This variable can be a list of select methods which Gnus will query with
560 the `ask-server' method in addition to the primary, secondary, and archive
561 servers.
562
563 Eg.
564   (setq gnus-check-new-newsgroups
565         '((nntp \"some.server\") (nntp \"other.server\")))
566
567 If this variable is nil, then you have to tell Gnus explicitly to
568 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
569
570 (defvar gnus-check-bogus-newsgroups nil
571   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
572 If this variable is nil, then you have to tell Gnus explicitly to
573 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
574
575 (defvar gnus-read-active-file t
576   "*Non-nil means that Gnus will read the entire active file at startup.
577 If this variable is nil, Gnus will only know about the groups in your
578 `.newsrc' file.
579
580 If this variable is `some', Gnus will try to only read the relevant
581 parts of the active file from the server.  Not all servers support
582 this, and it might be quite slow with other servers, but this should
583 generally be faster than both the t and nil value.
584
585 If you set this variable to nil or `some', you probably still want to
586 be told about new newsgroups that arrive.  To do that, set
587 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
588 properly with all servers.")
589
590 (defvar gnus-level-subscribed 5
591   "*Groups with levels less than or equal to this variable are subscribed.")
592
593 (defvar gnus-level-unsubscribed 7
594   "*Groups with levels less than or equal to this variable are unsubscribed.
595 Groups with levels less than `gnus-level-subscribed', which should be
596 less than this variable, are subscribed.")
597
598 (defvar gnus-level-zombie 8
599   "*Groups with this level are zombie groups.")
600
601 (defvar gnus-level-killed 9
602   "*Groups with this level are killed.")
603
604 (defvar gnus-level-default-subscribed 3
605   "*New subscribed groups will be subscribed at this level.")
606
607 (defvar gnus-level-default-unsubscribed 6
608   "*New unsubscribed groups will be unsubscribed at this level.")
609
610 (defvar gnus-activate-level (1+ gnus-level-subscribed)
611   "*Groups higher than this level won't be activated on startup.
612 Setting this variable to something log might save lots of time when
613 you have many groups that you aren't interested in.")
614
615 (defvar gnus-activate-foreign-newsgroups 4
616   "*If nil, Gnus will not check foreign newsgroups at startup.
617 If it is non-nil, it should be a number between one and nine.  Foreign
618 newsgroups that have a level lower or equal to this number will be
619 activated on startup.  For instance, if you want to active all
620 subscribed newsgroups, but not the rest, you'd set this variable to
621 `gnus-level-subscribed'.
622
623 If you subscribe to lots of newsgroups from different servers, startup
624 might take a while.  By setting this variable to nil, you'll save time,
625 but you won't be told how many unread articles there are in the
626 groups.")
627
628 (defvar gnus-save-newsrc-file t
629   "*Non-nil means that Gnus will save the `.newsrc' file.
630 Gnus always saves its own startup file, which is called
631 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
632 be readily understood by other newsreaders.  If you don't plan on
633 using other newsreaders, set this variable to nil to save some time on
634 exit.")
635
636 (defvar gnus-save-killed-list t
637   "*If non-nil, save the list of killed groups to the startup file.
638 If you set this variable to nil, you'll save both time (when starting
639 and quitting) and space (both memory and disk), but it will also mean
640 that Gnus has no record of which groups are new and which are old, so
641 the automatic new newsgroups subscription methods become meaningless.
642
643 You should always set `gnus-check-new-newsgroups' to `ask-server' or
644 nil if you set this variable to nil.")
645
646 (defvar gnus-interactive-catchup t
647   "*If non-nil, require your confirmation when catching up a group.")
648
649 (defvar gnus-interactive-exit t
650   "*If non-nil, require your confirmation when exiting Gnus.")
651
652 (defvar gnus-kill-killed t
653   "*If non-nil, Gnus will apply kill files to already killed articles.
654 If it is nil, Gnus will never apply kill files to articles that have
655 already been through the scoring process, which might very well save lots
656 of time.")
657
658 (defvar gnus-extract-address-components 'gnus-extract-address-components
659   "*Function for extracting address components from a From header.
660 Two pre-defined function exist: `gnus-extract-address-components',
661 which is the default, quite fast, and too simplistic solution, and
662 `mail-extract-address-components', which works much better, but is
663 slower.")
664
665 (defvar gnus-summary-default-score 0
666   "*Default article score level.
667 If this variable is nil, scoring will be disabled.")
668
669 (defvar gnus-summary-zcore-fuzz 0
670   "*Fuzziness factor for the zcore in the summary buffer.
671 Articles with scores closer than this to `gnus-summary-default-score'
672 will not be marked.")
673
674 (defvar gnus-simplify-subject-fuzzy-regexp nil
675   "*Strings to be removed when doing fuzzy matches.
676 This can either be a regular expression or list of regular expressions
677 that will be removed from subject strings if fuzzy subject
678 simplification is selected.")
679
680 (defvar gnus-permanently-visible-groups nil
681   "*Regexp to match groups that should always be listed in the group buffer.
682 This means that they will still be listed when there are no unread
683 articles in the groups.")
684
685 (defvar gnus-list-groups-with-ticked-articles t
686   "*If non-nil, list groups that have only ticked articles.
687 If nil, only list groups that have unread articles.")
688
689 (defvar gnus-group-default-list-level gnus-level-subscribed
690   "*Default listing level.
691 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
692
693 (defvar gnus-group-use-permanent-levels nil
694   "*If non-nil, once you set a level, Gnus will use this level.")
695
696 (defvar gnus-group-list-inactive-groups t
697   "*If non-nil, inactive groups will be listed.")
698
699 (defvar gnus-show-mime nil
700   "*If non-nil, do mime processing of articles.
701 The articles will simply be fed to the function given by
702 `gnus-show-mime-method'.")
703
704 (defvar gnus-strict-mime t
705   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
706
707 (defvar gnus-show-mime-method 'metamail-buffer
708   "*Function to process a MIME message.
709 The function is called from the article buffer.")
710
711 (defvar gnus-decode-encoded-word-method (lambda ())
712   "*Function to decode a MIME encoded-words.
713 The function is called from the article buffer.")
714
715 (defvar gnus-show-threads t
716   "*If non-nil, display threads in summary mode.")
717
718 (defvar gnus-thread-hide-subtree nil
719   "*If non-nil, hide all threads initially.
720 If threads are hidden, you have to run the command
721 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
722 to expose hidden threads.")
723
724 (defvar gnus-thread-hide-killed t
725   "*If non-nil, hide killed threads automatically.")
726
727 (defvar gnus-thread-ignore-subject nil
728   "*If non-nil, ignore subjects and do all threading based on the Reference header.
729 If nil, which is the default, articles that have different subjects
730 from their parents will start separate threads.")
731
732 (defvar gnus-thread-operation-ignore-subject t
733   "*If non-nil, subjects will be ignored when doing thread commands.
734 This affects commands like `gnus-summary-kill-thread' and
735 `gnus-summary-lower-thread'.
736
737 If this variable is nil, articles in the same thread with different
738 subjects will not be included in the operation in question.  If this
739 variable is `fuzzy', only articles that have subjects that are fuzzily
740 equal will be included.")
741
742 (defvar gnus-thread-indent-level 4
743   "*Number that says how much each sub-thread should be indented.")
744
745 (defvar gnus-ignored-newsgroups
746   (purecopy (mapconcat 'identity
747                        '("^to\\."       ; not "real" groups
748                          "^[0-9. \t]+ " ; all digits in name
749                          "[][\"#'()]"   ; bogus characters
750                          )
751                        "\\|"))
752   "*A regexp to match uninteresting newsgroups in the active file.
753 Any lines in the active file matching this regular expression are
754 removed from the newsgroup list before anything else is done to it,
755 thus making them effectively non-existent.")
756
757 (defvar gnus-ignored-headers
758   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
759   "*All headers that match this regexp will be hidden.
760 This variable can also be a list of regexps of headers to be ignored.
761 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
762
763 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
764   "*All headers that do not match this regexp will be hidden.
765 This variable can also be a list of regexp of headers to remain visible.
766 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
767
768 (defvar gnus-sorted-header-list
769   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
770     "^Cc:" "^Date:" "^Organization:")
771   "*This variable is a list of regular expressions.
772 If it is non-nil, headers that match the regular expressions will
773 be placed first in the article buffer in the sequence specified by
774 this list.")
775
776 (defvar gnus-boring-article-headers
777   '(empty followup-to reply-to)
778   "*Headers that are only to be displayed if they have interesting data.
779 Possible values in this list are `empty', `newsgroups', `followup-to',
780 `reply-to', and `date'.")
781
782 (defvar gnus-show-all-headers nil
783   "*If non-nil, don't hide any headers.")
784
785 (defvar gnus-save-all-headers t
786   "*If non-nil, don't remove any headers before saving.")
787
788 (defvar gnus-saved-headers gnus-visible-headers
789   "*Headers to keep if `gnus-save-all-headers' is nil.
790 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
791 If that variable is nil, however, all headers that match this regexp
792 will be kept while the rest will be deleted before saving.")
793
794 (defvar gnus-inhibit-startup-message nil
795   "*If non-nil, the startup message will not be displayed.")
796
797 (defvar gnus-signature-separator "^-- *$"
798   "Regexp matching signature separator.")
799
800 (defvar gnus-signature-limit nil
801   "Provide a limit to what is considered a signature.
802 If it is a number, no signature may not be longer (in characters) than
803 that number.  If it is a function, the function will be called without
804 any parameters, and if it returns nil, there is no signature in the
805 buffer.  If it is a string, it will be used as a regexp.  If it
806 matches, the text in question is not a signature.")
807
808 (defvar gnus-auto-extend-newsgroup t
809   "*If non-nil, extend newsgroup forward and backward when requested.")
810
811 (defvar gnus-auto-select-first t
812   "*If nil, don't select the first unread article when entering a group.
813 If this variable is `best', select the highest-scored unread article
814 in the group.  If neither nil nor `best', select the first unread
815 article.
816
817 If you want to prevent automatic selection of the first unread article
818 in some newsgroups, set the variable to nil in
819 `gnus-select-group-hook'.")
820
821 (defvar gnus-auto-select-next t
822   "*If non-nil, offer to go to the next group from the end of the previous.
823 If the value is t and the next newsgroup is empty, Gnus will exit
824 summary mode and go back to group mode.  If the value is neither nil
825 nor t, Gnus will select the following unread newsgroup.  In
826 particular, if the value is the symbol `quietly', the next unread
827 newsgroup will be selected without any confirmation, and if it is
828 `almost-quietly', the next group will be selected without any
829 confirmation if you are located on the last article in the group.
830 Finally, if this variable is `slightly-quietly', the `Z n' command
831 will go to the next group without confirmation.")
832
833 (defvar gnus-auto-select-same nil
834   "*If non-nil, select the next article with the same subject.")
835
836 (defvar gnus-summary-check-current nil
837   "*If non-nil, consider the current article when moving.
838 The \"unread\" movement commands will stay on the same line if the
839 current article is unread.")
840
841 (defvar gnus-auto-center-summary t
842   "*If non-nil, always center the current summary buffer.
843 In particular, if `vertical' do only vertical recentering.  If non-nil
844 and non-`vertical', do both horizontal and vertical recentering.")
845
846 (defvar gnus-break-pages t
847   "*If non-nil, do page breaking on articles.
848 The page delimiter is specified by the `gnus-page-delimiter'
849 variable.")
850
851 (defvar gnus-page-delimiter "^\^L"
852   "*Regexp describing what to use as article page delimiters.
853 The default value is \"^\^L\", which is a form linefeed at the
854 beginning of a line.")
855
856 (defvar gnus-use-full-window t
857   "*If non-nil, use the entire Emacs screen.")
858
859 (defvar gnus-window-configuration nil
860   "Obsolete variable.  See `gnus-buffer-configuration'.")
861
862 (defvar gnus-window-min-width 2
863   "*Minimum width of Gnus buffers.")
864
865 (defvar gnus-window-min-height 1
866   "*Minimum height of Gnus buffers.")
867
868 (defvar gnus-buffer-configuration
869   '((group
870      (vertical 1.0
871                (group 1.0 point)
872                (if gnus-carpal '(group-carpal 4))))
873     (summary
874      (vertical 1.0
875                (summary 1.0 point)
876                (if gnus-carpal '(summary-carpal 4))))
877     (article
878      (cond 
879       (gnus-use-picons
880        '(frame 1.0
881                (vertical 1.0
882                          (summary 0.25 point)
883                          (if gnus-carpal '(summary-carpal 4))
884                          (article 1.0))
885                (vertical ((height . 5) (width . 15)
886                           (user-position . t)
887                           (left . -1) (top . 1))
888                          (picons 1.0))))
889       (gnus-use-trees
890        '(vertical 1.0
891                   (summary 0.25 point)
892                   (tree 0.25)
893                   (article 1.0)))
894       (t
895        '(vertical 1.0
896                  (summary 0.25 point)
897                  (if gnus-carpal '(summary-carpal 4))
898                  (article 1.0)))))
899     (server
900      (vertical 1.0
901                (server 1.0 point)
902                (if gnus-carpal '(server-carpal 2))))
903     (browse
904      (vertical 1.0
905                (browse 1.0 point)
906                (if gnus-carpal '(browse-carpal 2))))
907     (message
908      (vertical 1.0
909                (message 1.0 point)))
910     (pick
911      (vertical 1.0
912                (article 1.0 point)))
913     (info
914      (vertical 1.0
915                (info 1.0 point)))
916     (summary-faq
917      (vertical 1.0
918                (summary 0.25)
919                (faq 1.0 point)))
920     (edit-group
921      (vertical 1.0
922                (group 0.5)
923                (edit-group 1.0 point)))
924     (edit-server
925      (vertical 1.0
926                (server 0.5)
927                (edit-server 1.0 point)))
928     (edit-score
929      (vertical 1.0
930                (summary 0.25)
931                (edit-score 1.0 point)))
932     (post
933      (vertical 1.0
934                (post 1.0 point)))
935     (reply
936      (vertical 1.0
937                (article-copy 0.5)
938                (message 1.0 point)))
939     (forward
940      (vertical 1.0
941                (message 1.0 point)))
942     (reply-yank
943      (vertical 1.0
944                (message 1.0 point)))
945     (mail-bounce
946      (vertical 1.0
947                (article 0.5)
948                (message 1.0 point)))
949     (draft
950      (vertical 1.0
951                (draft 1.0 point)))
952     (pipe
953      (vertical 1.0
954                (summary 0.25 point)
955                (if gnus-carpal '(summary-carpal 4))
956                ("*Shell Command Output*" 1.0)))
957     (bug
958      (vertical 1.0
959                ("*Gnus Help Bug*" 0.5)
960                ("*Gnus Bug*" 1.0 point)))
961     (compose-bounce
962      (vertical 1.0
963                (article 0.5)
964                (message 1.0 point))))
965   "Window configuration for all possible Gnus buffers.
966 This variable is a list of lists.  Each of these lists has a NAME and
967 a RULE.  The NAMEs are commonsense names like `group', which names a
968 rule used when displaying the group buffer; `summary', which names a
969 rule for what happens when you enter a group and do not display an
970 article buffer; and so on.  See the value of this variable for a
971 complete list of NAMEs.
972
973 Each RULE is a list of vectors.  The first element in this vector is
974 the name of the buffer to be displayed; the second element is the
975 percentage of the screen this buffer is to occupy (a number in the
976 0.0-0.99 range); the optional third element is `point', which should
977 be present to denote which buffer point is to go to after making this
978 buffer configuration.")
979
980 (defvar gnus-window-to-buffer
981   '((group . gnus-group-buffer)
982     (summary . gnus-summary-buffer)
983     (article . gnus-article-buffer)
984     (server . gnus-server-buffer)
985     (browse . "*Gnus Browse Server*")
986     (edit-group . gnus-group-edit-buffer)
987     (edit-server . gnus-server-edit-buffer)
988     (group-carpal . gnus-carpal-group-buffer)
989     (summary-carpal . gnus-carpal-summary-buffer)
990     (server-carpal . gnus-carpal-server-buffer)
991     (browse-carpal . gnus-carpal-browse-buffer)
992     (edit-score . gnus-score-edit-buffer)
993     (message . gnus-message-buffer)
994     (mail . gnus-message-buffer)
995     (post-news . gnus-message-buffer)
996     (faq . gnus-faq-buffer)
997     (picons . "*Picons*")
998     (tree . gnus-tree-buffer)
999     (info . gnus-info-buffer)
1000     (article-copy . gnus-article-copy)
1001     (draft . gnus-draft-buffer))
1002   "Mapping from short symbols to buffer names or buffer variables.")
1003
1004 (defvar gnus-carpal nil
1005   "*If non-nil, display clickable icons.")
1006
1007 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
1008   "*Function called with a group name when new group is detected.
1009 A few pre-made functions are supplied: `gnus-subscribe-randomly'
1010 inserts new groups at the beginning of the list of groups;
1011 `gnus-subscribe-alphabetically' inserts new groups in strict
1012 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
1013 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
1014 for your decision; `gnus-subscribe-killed' kills all new groups;
1015 `gnus-subscribe-zombies' will make all new groups into zombies.")
1016
1017 ;; Suggested by a bug report by Hallvard B Furuseth.
1018 ;; <h.b.furuseth@usit.uio.no>.
1019 (defvar gnus-subscribe-options-newsgroup-method
1020   (function gnus-subscribe-alphabetically)
1021   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
1022 If, for instance, you want to subscribe to all newsgroups in the
1023 \"no\" and \"alt\" hierarchies, you'd put the following in your
1024 .newsrc file:
1025
1026 options -n no.all alt.all
1027
1028 Gnus will the subscribe all new newsgroups in these hierarchies with
1029 the subscription method in this variable.")
1030
1031 (defvar gnus-subscribe-hierarchical-interactive nil
1032   "*If non-nil, Gnus will offer to subscribe hierarchically.
1033 When a new hierarchy appears, Gnus will ask the user:
1034
1035 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1036
1037 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1038 subscribe to all newsgroups in the hierarchy and `s' will skip this
1039 hierarchy in its entirety.")
1040
1041 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1042   "*Function used for sorting the group buffer.
1043 This function will be called with group info entries as the arguments
1044 for the groups to be sorted.  Pre-made functions include
1045 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1046 `gnus-group-sort-by-level', `gnus-group-sort-by-score',
1047 `gnus-group-sort-by-method', and `gnus-group-sort-by-rank'.
1048
1049 This variable can also be a list of sorting functions.  In that case,
1050 the most significant sort function should be the last function in the
1051 list.")
1052
1053 ;; Mark variables suggested by Thomas Michanek
1054 ;; <Thomas.Michanek@telelogic.se>.
1055 (defvar gnus-unread-mark ? 
1056   "*Mark used for unread articles.")
1057 (defvar gnus-ticked-mark ?!
1058   "*Mark used for ticked articles.")
1059 (defvar gnus-dormant-mark ??
1060   "*Mark used for dormant articles.")
1061 (defvar gnus-del-mark ?r
1062   "*Mark used for del'd articles.")
1063 (defvar gnus-read-mark ?R
1064   "*Mark used for read articles.")
1065 (defvar gnus-expirable-mark ?E
1066   "*Mark used for expirable articles.")
1067 (defvar gnus-killed-mark ?K
1068   "*Mark used for killed articles.")
1069 (defvar gnus-souped-mark ?F
1070   "*Mark used for killed articles.")
1071 (defvar gnus-kill-file-mark ?X
1072   "*Mark used for articles killed by kill files.")
1073 (defvar gnus-low-score-mark ?Y
1074   "*Mark used for articles with a low score.")
1075 (defvar gnus-catchup-mark ?C
1076   "*Mark used for articles that are caught up.")
1077 (defvar gnus-replied-mark ?A
1078   "*Mark used for articles that have been replied to.")
1079 (defvar gnus-cached-mark ?*
1080   "*Mark used for articles that are in the cache.")
1081 (defvar gnus-saved-mark ?S
1082   "*Mark used for articles that have been saved to.")
1083 (defvar gnus-process-mark ?#
1084   "*Process mark.")
1085 (defvar gnus-ancient-mark ?O
1086   "*Mark used for ancient articles.")
1087 (defvar gnus-sparse-mark ?Q
1088   "*Mark used for sparsely reffed articles.")
1089 (defvar gnus-canceled-mark ?G
1090   "*Mark used for canceled articles.")
1091 (defvar gnus-score-over-mark ?+
1092   "*Score mark used for articles with high scores.")
1093 (defvar gnus-score-below-mark ?-
1094   "*Score mark used for articles with low scores.")
1095 (defvar gnus-empty-thread-mark ? 
1096   "*There is no thread under the article.")
1097 (defvar gnus-not-empty-thread-mark ?=
1098   "*There is a thread under the article.")
1099
1100 (defvar gnus-shell-command-separator ";"
1101   "String used to separate to shell commands.")
1102
1103 (defvar gnus-view-pseudo-asynchronously nil
1104   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1105
1106 (defvar gnus-view-pseudos nil
1107   "*If `automatic', pseudo-articles will be viewed automatically.
1108 If `not-confirm', pseudos will be viewed automatically, and the user
1109 will not be asked to confirm the command.")
1110
1111 (defvar gnus-view-pseudos-separately t
1112   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1113 If nil, all files that use the same viewing command will be given as a
1114 list of parameters to that command.")
1115
1116 (defvar gnus-insert-pseudo-articles t
1117   "*If non-nil, insert pseudo-articles when decoding articles.")
1118
1119 (defvar gnus-group-line-format "%M\%S\%p\%P\%5y: %(%g%)%l\n"
1120   "*Format of group lines.
1121 It works along the same lines as a normal formatting string,
1122 with some simple extensions.
1123
1124 %M    Only marked articles (character, \"*\" or \" \")
1125 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1126 %L    Level of subscribedness (integer)
1127 %N    Number of unread articles (integer)
1128 %I    Number of dormant articles (integer)
1129 %i    Number of ticked and dormant (integer)
1130 %T    Number of ticked articles (integer)
1131 %R    Number of read articles (integer)
1132 %t    Total number of articles (integer)
1133 %y    Number of unread, unticked articles (integer)
1134 %G    Group name (string)
1135 %g    Qualified group name (string)
1136 %D    Group description (string)
1137 %s    Select method (string)
1138 %o    Moderated group (char, \"m\")
1139 %p    Process mark (char)
1140 %O    Moderated group (string, \"(m)\" or \"\")
1141 %P    Topic indentation (string)
1142 %l    Whether there are GroupLens predictions for this group (string)
1143 %n    Select from where (string)
1144 %z    A string that look like `<%s:%n>' if a foreign select method is used
1145 %u    User defined specifier.  The next character in the format string should
1146       be a letter.  Gnus will call the function gnus-user-format-function-X,
1147       where X is the letter following %u.  The function will be passed the
1148       current header as argument.  The function should return a string, which
1149       will be inserted into the buffer just like information from any other
1150       group specifier.
1151
1152 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1153 the mouse point move inside the area.  There can only be one such area.
1154
1155 Note that this format specification is not always respected.  For
1156 reasons of efficiency, when listing killed groups, this specification
1157 is ignored altogether.  If the spec is changed considerably, your
1158 output may end up looking strange when listing both alive and killed
1159 groups.
1160
1161 If you use %o or %O, reading the active file will be slower and quite
1162 a bit of extra memory will be used. %D will also worsen performance.
1163 Also note that if you change the format specification to include any
1164 of these specs, you must probably re-start Gnus to see them go into
1165 effect.")
1166
1167 (defvar gnus-summary-line-format "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
1168   "*The format specification of the lines in the summary buffer.
1169
1170 It works along the same lines as a normal formatting string,
1171 with some simple extensions.
1172
1173 %N   Article number, left padded with spaces (string)
1174 %S   Subject (string)
1175 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1176 %n   Name of the poster (string)
1177 %a   Extracted name of the poster (string)
1178 %A   Extracted address of the poster (string)
1179 %F   Contents of the From: header (string)
1180 %x   Contents of the Xref: header (string)
1181 %D   Date of the article (string)
1182 %d   Date of the article (string) in DD-MMM format
1183 %M   Message-id of the article (string)
1184 %r   References of the article (string)
1185 %c   Number of characters in the article (integer)
1186 %L   Number of lines in the article (integer)
1187 %I   Indentation based on thread level (a string of spaces)
1188 %T   A string with two possible values: 80 spaces if the article
1189      is on thread level two or larger and 0 spaces on level one
1190 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1191 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1192 %[   Opening bracket (character, \"[\" or \"<\")
1193 %]   Closing bracket (character, \"]\" or \">\")
1194 %>   Spaces of length thread-level (string)
1195 %<   Spaces of length (- 20 thread-level) (string)
1196 %i   Article score (number)
1197 %z   Article zcore (character)
1198 %t   Number of articles under the current thread (number).
1199 %e   Whether the thread is empty or not (character).
1200 %l   GroupLens score (string).
1201 %u   User defined specifier.  The next character in the format string should
1202      be a letter.  Gnus will call the function gnus-user-format-function-X,
1203      where X is the letter following %u.  The function will be passed the
1204      current header as argument.  The function should return a string, which
1205      will be inserted into the summary just like information from any other
1206      summary specifier.
1207
1208 Text between %( and %) will be highlighted with `gnus-mouse-face'
1209 when the mouse point is placed inside the area.  There can only be one
1210 such area.
1211
1212 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1213 with care.  For reasons of efficiency, Gnus will compute what column
1214 these characters will end up in, and \"hard-code\" that.  This means that
1215 it is illegal to have these specs after a variable-length spec.  Well,
1216 you might not be arrested, but your summary buffer will look strange,
1217 which is bad enough.
1218
1219 The smart choice is to have these specs as for to the left as
1220 possible.
1221
1222 This restriction may disappear in later versions of Gnus.")
1223
1224 (defvar gnus-summary-dummy-line-format
1225   "*  %(:                          :%) %S\n"
1226   "*The format specification for the dummy roots in the summary buffer.
1227 It works along the same lines as a normal formatting string,
1228 with some simple extensions.
1229
1230 %S  The subject")
1231
1232 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1233   "*The format specification for the summary mode line.
1234 It works along the same lines as a normal formatting string,
1235 with some simple extensions:
1236
1237 %G  Group name
1238 %p  Unprefixed group name
1239 %A  Current article number
1240 %V  Gnus version
1241 %U  Number of unread articles in the group
1242 %e  Number of unselected articles in the group
1243 %Z  A string with unread/unselected article counts
1244 %g  Shortish group name
1245 %S  Subject of the current article
1246 %u  User-defined spec
1247 %s  Current score file name
1248 %d  Number of dormant articles
1249 %r  Number of articles that have been marked as read in this session
1250 %E  Number of articles expunged by the score files")
1251
1252 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1253   "*The format specification for the article mode line.
1254 See `gnus-summary-mode-line-format' for a closer description.")
1255
1256 (defvar gnus-group-mode-line-format "Gnus: %%b {%M\%:%S}"
1257   "*The format specification for the group mode line.
1258 It works along the same lines as a normal formatting string,
1259 with some simple extensions:
1260
1261 %S   The native news server.
1262 %M   The native select method.
1263 %:   \":\" if %S isn't \"\".")
1264
1265 (defvar gnus-valid-select-methods
1266   '(("nntp" post address prompt-address)
1267     ("nnspool" post address)
1268     ("nnvirtual" post-mail virtual prompt-address)
1269     ("nnmbox" mail respool address)
1270     ("nnml" mail respool address)
1271     ("nnmh" mail respool address)
1272     ("nndir" post-mail prompt-address address)
1273     ("nneething" none address prompt-address)
1274     ("nndoc" none address prompt-address)
1275     ("nnbabyl" mail address respool)
1276     ("nnkiboze" post virtual)
1277     ("nnsoup" post-mail address)
1278     ("nndraft" post-mail)
1279     ("nnfolder" mail respool address))
1280   "An alist of valid select methods.
1281 The first element of each list lists should be a string with the name
1282 of the select method.  The other elements may be the category of
1283 this method (ie. `post', `mail', `none' or whatever) or other
1284 properties that this method has (like being respoolable).
1285 If you implement a new select method, all you should have to change is
1286 this variable.  I think.")
1287
1288 (defvar gnus-updated-mode-lines '(group article summary tree)
1289   "*List of buffers that should update their mode lines.
1290 The list may contain the symbols `group', `article' and `summary'.  If
1291 the corresponding symbol is present, Gnus will keep that mode line
1292 updated with information that may be pertinent.
1293 If this variable is nil, screen refresh may be quicker.")
1294
1295 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1296 (defvar gnus-mode-non-string-length nil
1297   "*Max length of mode-line non-string contents.
1298 If this is nil, Gnus will take space as is needed, leaving the rest
1299 of the modeline intact.")
1300
1301 ;see gnus-cus.el
1302 ;(defvar gnus-mouse-face 'highlight
1303 ;  "*Face used for mouse highlighting in Gnus.
1304 ;No mouse highlights will be done if `gnus-visual' is nil.")
1305
1306 (defvar gnus-summary-mark-below 0
1307   "*Mark all articles with a score below this variable as read.
1308 This variable is local to each summary buffer and usually set by the
1309 score file.")
1310
1311 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1312   "*List of functions used for sorting articles in the summary buffer.
1313 This variable is only used when not using a threaded display.")
1314
1315 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1316   "*List of functions used for sorting threads in the summary buffer.
1317 By default, threads are sorted by article number.
1318
1319 Each function takes two threads and return non-nil if the first thread
1320 should be sorted before the other.  If you use more than one function,
1321 the primary sort function should be the last.  You should probably
1322 always include `gnus-thread-sort-by-number' in the list of sorting
1323 functions -- preferably first.
1324
1325 Ready-mady functions include `gnus-thread-sort-by-number',
1326 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1327 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1328 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1329
1330 (defvar gnus-thread-score-function '+
1331   "*Function used for calculating the total score of a thread.
1332
1333 The function is called with the scores of the article and each
1334 subthread and should then return the score of the thread.
1335
1336 Some functions you can use are `+', `max', or `min'.")
1337
1338 (defvar gnus-summary-expunge-below nil
1339   "All articles that have a score less than this variable will be expunged.")
1340
1341 (defvar gnus-thread-expunge-below nil
1342   "All threads that have a total score less than this variable will be expunged.
1343 See `gnus-thread-score-function' for en explanation of what a
1344 \"thread score\" is.")
1345
1346 (defvar gnus-auto-subscribed-groups
1347   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1348   "*All new groups that match this regexp will be subscribed automatically.
1349 Note that this variable only deals with new groups.  It has no effect
1350 whatsoever on old groups.
1351
1352 New groups that match this regexp will not be handled by
1353 `gnus-subscribe-newsgroup-method'.  Instead, they will
1354 be subscribed using `gnus-subscribe-options-newsgroup-method'.")
1355
1356 (defvar gnus-options-subscribe nil
1357   "*All new groups matching this regexp will be subscribed unconditionally.
1358 Note that this variable deals only with new newsgroups.  This variable
1359 does not affect old newsgroups.
1360
1361 New groups that match this regexp will not be handled by
1362 `gnus-subscribe-newsgroup-method'.  Instead, they will
1363 be subscribed using `gnus-subscribe-options-newsgroup-method'.")
1364
1365 (defvar gnus-options-not-subscribe nil
1366   "*All new groups matching this regexp will be ignored.
1367 Note that this variable deals only with new newsgroups.  This variable
1368 does not affect old (already subscribed) newsgroups.")
1369
1370 (defvar gnus-auto-expirable-newsgroups nil
1371   "*Groups in which to automatically mark read articles as expirable.
1372 If non-nil, this should be a regexp that should match all groups in
1373 which to perform auto-expiry.  This only makes sense for mail groups.")
1374
1375 (defvar gnus-total-expirable-newsgroups nil
1376   "*Groups in which to perform expiry of all read articles.
1377 Use with extreme caution.  All groups that match this regexp will be
1378 expiring - which means that all read articles will be deleted after
1379 (say) one week.  (This only goes for mail groups and the like, of
1380 course.)")
1381
1382 (defvar gnus-group-uncollapsed-levels 1
1383   "Number of group name elements to leave alone when making a short group name.")
1384
1385 (defvar gnus-hidden-properties '(invisible t intangible t)
1386   "Property list to use for hiding text.")
1387
1388 (defvar gnus-modtime-botch nil
1389   "*Non-nil means .newsrc should be deleted prior to save.  
1390 Its use is due to the bogus appearance that .newsrc was modified on
1391 disc.")
1392
1393 ;; Hooks.
1394
1395 (defvar gnus-group-mode-hook nil
1396   "*A hook for Gnus group mode.")
1397
1398 (defvar gnus-summary-mode-hook nil
1399   "*A hook for Gnus summary mode.
1400 This hook is run before any variables are set in the summary buffer.")
1401
1402 (defvar gnus-article-mode-hook nil
1403   "*A hook for Gnus article mode.")
1404
1405 (defvar gnus-summary-prepare-exit-hook nil
1406   "*A hook called when preparing to exit from the summary buffer.
1407 It calls `gnus-summary-expire-articles' by default.")
1408 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1409
1410 (defvar gnus-summary-exit-hook nil
1411   "*A hook called on exit from the summary buffer.")
1412
1413 (defvar gnus-check-bogus-groups-hook nil
1414   "A hook run after removing bogus groups.")
1415
1416 (defvar gnus-group-catchup-group-hook nil
1417   "*A hook run when catching up a group from the group buffer.")
1418
1419 (defvar gnus-group-update-group-hook nil
1420   "*A hook called when updating group lines.")
1421
1422 (defvar gnus-open-server-hook nil
1423   "*A hook called just before opening connection to the news server.")
1424
1425 (defvar gnus-load-hook nil
1426   "*A hook run while Gnus is loaded.")
1427
1428 (defvar gnus-startup-hook nil
1429   "*A hook called at startup.
1430 This hook is called after Gnus is connected to the NNTP server.")
1431
1432 (defvar gnus-get-new-news-hook nil
1433   "*A hook run just before Gnus checks for new news.")
1434
1435 (defvar gnus-after-getting-new-news-hook nil
1436   "*A hook run after Gnus checks for new news.")
1437
1438 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1439   "*A function that is called to generate the group buffer.
1440 The function is called with three arguments: The first is a number;
1441 all group with a level less or equal to that number should be listed,
1442 if the second is non-nil, empty groups should also be displayed.  If
1443 the third is non-nil, it is a number.  No groups with a level lower
1444 than this number should be displayed.
1445
1446 The only current function implemented is `gnus-group-prepare-flat'.")
1447
1448 (defvar gnus-group-prepare-hook nil
1449   "*A hook called after the group buffer has been generated.
1450 If you want to modify the group buffer, you can use this hook.")
1451
1452 (defvar gnus-summary-prepare-hook nil
1453   "*A hook called after the summary buffer has been generated.
1454 If you want to modify the summary buffer, you can use this hook.")
1455
1456 (defvar gnus-summary-generate-hook nil
1457   "*A hook run just before generating the summary buffer.
1458 This hook is commonly used to customize threading variables and the
1459 like.")
1460
1461 (defvar gnus-article-prepare-hook nil
1462   "*A hook called after an article has been prepared in the article buffer.
1463 If you want to run a special decoding program like nkf, use this hook.")
1464
1465 ;(defvar gnus-article-display-hook nil
1466 ;  "*A hook called after the article is displayed in the article buffer.
1467 ;The hook is designed to change the contents of the article
1468 ;buffer.  Typical functions that this hook may contain are
1469 ;`gnus-article-hide-headers' (hide selected headers),
1470 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1471 ;`gnus-article-hide-signature' (hide signature) and
1472 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1473 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1474 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1475 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1476
1477 (defvar gnus-article-x-face-too-ugly nil
1478   "Regexp matching posters whose face shouldn't be shown automatically.")
1479
1480 (defvar gnus-select-group-hook nil
1481   "*A hook called when a newsgroup is selected.
1482
1483 If you'd like to simplify subjects like the
1484 `gnus-summary-next-same-subject' command does, you can use the
1485 following hook:
1486
1487  (setq gnus-select-group-hook
1488       (list
1489         (lambda ()
1490           (mapcar (lambda (header)
1491                      (mail-header-set-subject
1492                       header
1493                       (gnus-simplify-subject
1494                        (mail-header-subject header) 're-only)))
1495                   gnus-newsgroup-headers))))")
1496
1497 (defvar gnus-select-article-hook nil
1498   "*A hook called when an article is selected.")
1499
1500 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1501   "*A hook called to apply kill files to a group.
1502 This hook is intended to apply a kill file to the selected newsgroup.
1503 The function `gnus-apply-kill-file' is called by default.
1504
1505 Since a general kill file is too heavy to use only for a few
1506 newsgroups, I recommend you to use a lighter hook function.  For
1507 example, if you'd like to apply a kill file to articles which contains
1508 a string `rmgroup' in subject in newsgroup `control', you can use the
1509 following hook:
1510
1511  (setq gnus-apply-kill-hook
1512       (list
1513         (lambda ()
1514           (cond ((string-match \"control\" gnus-newsgroup-name)
1515                  (gnus-kill \"Subject\" \"rmgroup\")
1516                  (gnus-expunge \"X\"))))))")
1517
1518 (defvar gnus-visual-mark-article-hook
1519   (list 'gnus-highlight-selected-summary)
1520   "*Hook run after selecting an article in the summary buffer.
1521 It is meant to be used for highlighting the article in some way.  It
1522 is not run if `gnus-visual' is nil.")
1523
1524 (defvar gnus-parse-headers-hook nil
1525   "*A hook called before parsing the headers.")
1526 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1527
1528 (defvar gnus-exit-group-hook nil
1529   "*A hook called when exiting (not quitting) summary mode.")
1530
1531 (defvar gnus-suspend-gnus-hook nil
1532   "*A hook called when suspending (not exiting) Gnus.")
1533
1534 (defvar gnus-exit-gnus-hook nil
1535   "*A hook called when exiting Gnus.")
1536
1537 (defvar gnus-after-exiting-gnus-hook nil
1538   "*A hook called after exiting Gnus.")
1539
1540 (defvar gnus-save-newsrc-hook nil
1541   "*A hook called before saving any of the newsrc files.")
1542
1543 (defvar gnus-save-quick-newsrc-hook nil
1544   "*A hook called just before saving the quick newsrc file.
1545 Can be used to turn version control on or off.")
1546
1547 (defvar gnus-save-standard-newsrc-hook nil
1548   "*A hook called just before saving the standard newsrc file.
1549 Can be used to turn version control on or off.")
1550
1551 (defvar gnus-summary-update-hook
1552   (list 'gnus-summary-highlight-line)
1553   "*A hook called when a summary line is changed.
1554 The hook will not be called if `gnus-visual' is nil.
1555
1556 The default function `gnus-summary-highlight-line' will
1557 highlight the line according to the `gnus-summary-highlight'
1558 variable.")
1559
1560 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1561   "*A hook called when a group line is changed.
1562 The hook will not be called if `gnus-visual' is nil.
1563
1564 The default function `gnus-group-highlight-line' will
1565 highlight the line according to the `gnus-group-highlight'
1566 variable.")
1567
1568 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1569   "*A hook called when an article is selected for the first time.
1570 The hook is intended to mark an article as read (or unread)
1571 automatically when it is selected.")
1572
1573 (defvar gnus-group-change-level-function nil
1574   "Function run when a group level is changed.
1575 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1576
1577 ;; Remove any hilit infestation.
1578 (add-hook 'gnus-startup-hook
1579           (lambda ()
1580             (remove-hook 'gnus-summary-prepare-hook
1581                          'hilit-rehighlight-buffer-quietly)
1582             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1583             (setq gnus-mark-article-hook
1584                   '(gnus-summary-mark-read-and-unread-as-read))
1585             (remove-hook 'gnus-article-prepare-hook
1586                          'hilit-rehighlight-buffer-quietly)))
1587
1588 \f
1589 ;; Internal variables
1590
1591 (defvar gnus-tree-buffer "*Tree*"
1592   "Buffer where Gnus thread trees are displayed.")
1593
1594 ;; Dummy variable.
1595 (defvar gnus-use-generic-from nil)
1596
1597 (defvar gnus-thread-indent-array nil)
1598 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1599
1600 (defvar gnus-newsrc-file-version nil)
1601
1602 (defvar gnus-method-history nil)
1603 ;; Variable holding the user answers to all method prompts.
1604
1605 (defvar gnus-group-history nil)
1606 ;; Variable holding the user answers to all group prompts.
1607
1608 (defvar gnus-server-alist nil
1609   "List of available servers.")
1610
1611 (defvar gnus-group-indentation-function nil)
1612
1613 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1614
1615 (defvar gnus-goto-missing-group-function nil)
1616
1617 (defvar gnus-override-subscribe-method nil)
1618
1619 (defvar gnus-group-goto-next-group-function nil
1620   "Function to override finding the next group after listing groups.")
1621
1622 (defconst gnus-article-mark-lists
1623   '((marked . tick) (replied . reply)
1624     (expirable . expire) (killed . killed)
1625     (bookmarks . bookmark) (dormant . dormant)
1626     (scored . score) (saved . save)
1627     (cached . cache)
1628     ))
1629
1630 ;; Avoid highlighting in kill files.
1631 (defvar gnus-summary-inhibit-highlight nil)
1632 (defvar gnus-newsgroup-selected-overlay nil)
1633
1634 (defvar gnus-inhibit-hiding nil)
1635 (defvar gnus-group-indentation "")
1636 (defvar gnus-inhibit-limiting nil)
1637 (defvar gnus-created-frames nil)
1638
1639 (defvar gnus-article-mode-map nil)
1640 (defvar gnus-dribble-buffer nil)
1641 (defvar gnus-headers-retrieved-by nil)
1642 (defvar gnus-article-reply nil)
1643 (defvar gnus-override-method nil)
1644 (defvar gnus-article-check-size nil)
1645
1646 (defvar gnus-current-score-file nil)
1647 (defvar gnus-newsgroup-adaptive-score-file nil)
1648 (defvar gnus-scores-exclude-files nil)
1649
1650 (defvar gnus-opened-servers nil)
1651
1652 (defvar gnus-current-move-group nil)
1653 (defvar gnus-current-copy-group nil)
1654 (defvar gnus-current-crosspost-group nil)
1655
1656 (defvar gnus-newsgroup-dependencies nil)
1657 (defvar gnus-newsgroup-async nil)
1658 (defvar gnus-group-edit-buffer nil)
1659
1660 (defvar gnus-newsgroup-adaptive nil)
1661
1662 (defvar gnus-summary-display-table nil)
1663 (defvar gnus-summary-display-article-function nil)
1664
1665 (defvar gnus-summary-highlight-line-function nil
1666   "Function called after highlighting a summary line.")
1667
1668 (defvar gnus-group-line-format-alist
1669   `((?M gnus-tmp-marked-mark ?c)
1670     (?S gnus-tmp-subscribed ?c)
1671     (?L gnus-tmp-level ?d)
1672     (?N (cond ((eq number t) "*" )
1673               ((numberp number) 
1674                (int-to-string
1675                 (+ number
1676                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1677                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1678               (t number)) ?s)
1679     (?R gnus-tmp-number-of-read ?s)
1680     (?t gnus-tmp-number-total ?d)
1681     (?y gnus-tmp-number-of-unread ?s)
1682     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1683     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1684     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1685            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1686     (?g gnus-tmp-group ?s)
1687     (?G gnus-tmp-qualified-group ?s)
1688     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1689     (?D gnus-tmp-newsgroup-description ?s)
1690     (?o gnus-tmp-moderated ?c)
1691     (?O gnus-tmp-moderated-string ?s)
1692     (?p gnus-tmp-process-marked ?c)
1693     (?s gnus-tmp-news-server ?s)
1694     (?n gnus-tmp-news-method ?s)
1695     (?P gnus-group-indentation ?s)
1696     (?l gnus-tmp-grouplens ?s)
1697     (?z gnus-tmp-news-method-string ?s)
1698     (?u gnus-tmp-user-defined ?s)))
1699
1700 (defvar gnus-summary-line-format-alist
1701   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1702     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1703     (?s gnus-tmp-subject-or-nil ?s)
1704     (?n gnus-tmp-name ?s)
1705     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1706         ?s)
1707     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1708             gnus-tmp-from) ?s)
1709     (?F gnus-tmp-from ?s)
1710     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1711     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1712     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1713     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1714     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1715     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1716     (?L gnus-tmp-lines ?d)
1717     (?I gnus-tmp-indentation ?s)
1718     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1719     (?R gnus-tmp-replied ?c)
1720     (?\[ gnus-tmp-opening-bracket ?c)
1721     (?\] gnus-tmp-closing-bracket ?c)
1722     (?\> (make-string gnus-tmp-level ? ) ?s)
1723     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1724     (?i gnus-tmp-score ?d)
1725     (?z gnus-tmp-score-char ?c)
1726     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1727     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1728     (?U gnus-tmp-unread ?c)
1729     (?t (gnus-summary-number-of-articles-in-thread
1730          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1731         ?d)
1732     (?e (gnus-summary-number-of-articles-in-thread
1733          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1734         ?c)
1735     (?u gnus-tmp-user-defined ?s))
1736   "An alist of format specifications that can appear in summary lines,
1737 and what variables they correspond with, along with the type of the
1738 variable (string, integer, character, etc).")
1739
1740 (defvar gnus-summary-dummy-line-format-alist
1741   `((?S gnus-tmp-subject ?s)
1742     (?N gnus-tmp-number ?d)
1743     (?u gnus-tmp-user-defined ?s)))
1744
1745 (defvar gnus-summary-mode-line-format-alist
1746   `((?G gnus-tmp-group-name ?s)
1747     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1748     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1749     (?A gnus-tmp-article-number ?d)
1750     (?Z gnus-tmp-unread-and-unselected ?s)
1751     (?V gnus-version ?s)
1752     (?U gnus-tmp-unread-and-unticked ?d)
1753     (?S gnus-tmp-subject ?s)
1754     (?e gnus-tmp-unselected ?d)
1755     (?u gnus-tmp-user-defined ?s)
1756     (?d (length gnus-newsgroup-dormant) ?d)
1757     (?t (length gnus-newsgroup-marked) ?d)
1758     (?r (length gnus-newsgroup-reads) ?d)
1759     (?E gnus-newsgroup-expunged-tally ?d)
1760     (?s (gnus-current-score-file-nondirectory) ?s)))
1761
1762 (defvar gnus-article-mode-line-format-alist
1763   gnus-summary-mode-line-format-alist)
1764
1765 (defvar gnus-group-mode-line-format-alist
1766   `((?S gnus-tmp-news-server ?s)
1767     (?M gnus-tmp-news-method ?s)
1768     (?u gnus-tmp-user-defined ?s)
1769     (?: gnus-tmp-colon ?s)))
1770
1771 (defvar gnus-have-read-active-file nil)
1772
1773 (defconst gnus-maintainer
1774   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1775   "The mail address of the Gnus maintainers.")
1776
1777 (defconst gnus-version-number "5.2.38"
1778   "Version number for this version of Gnus.")
1779
1780 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1781   "Version string for this version of Gnus.")
1782
1783 (defvar gnus-info-nodes
1784   '((gnus-group-mode "(gnus)The Group Buffer")
1785     (gnus-summary-mode "(gnus)The Summary Buffer")
1786     (gnus-article-mode "(gnus)The Article Buffer")
1787     (gnus-server-mode "(gnus)The Server Buffer")
1788     (gnus-browse-mode "(gnus)Browse Foreign Server")
1789     (gnus-tree-mode "(gnus)Tree Display")
1790     )
1791   "Alist of major modes and related Info nodes.")
1792
1793 (defvar gnus-group-buffer "*Group*")
1794 (defvar gnus-summary-buffer "*Summary*")
1795 (defvar gnus-article-buffer "*Article*")
1796 (defvar gnus-server-buffer "*Server*")
1797
1798 (defvar gnus-work-buffer " *gnus work*")
1799
1800 (defvar gnus-original-article-buffer " *Original Article*")
1801 (defvar gnus-original-article nil)
1802
1803 (defvar gnus-buffer-list nil
1804   "Gnus buffers that should be killed on exit.")
1805
1806 (defvar gnus-slave nil
1807   "Whether this Gnus is a slave or not.")
1808
1809 (defvar gnus-variable-list
1810   '(gnus-newsrc-options gnus-newsrc-options-n
1811     gnus-newsrc-last-checked-date
1812     gnus-newsrc-alist gnus-server-alist
1813     gnus-killed-list gnus-zombie-list
1814     gnus-topic-topology gnus-topic-alist
1815     gnus-format-specs)
1816   "Gnus variables saved in the quick startup file.")
1817
1818 (defvar gnus-newsrc-options nil
1819   "Options line in the .newsrc file.")
1820
1821 (defvar gnus-newsrc-options-n nil
1822   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1823
1824 (defvar gnus-newsrc-last-checked-date nil
1825   "Date Gnus last asked server for new newsgroups.")
1826
1827 (defvar gnus-topic-topology nil
1828   "The complete topic hierarchy.")
1829
1830 (defvar gnus-topic-alist nil
1831   "The complete topic-group alist.")
1832
1833 (defvar gnus-newsrc-alist nil
1834   "Assoc list of read articles.
1835 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1836
1837 (defvar gnus-newsrc-hashtb nil
1838   "Hashtable of gnus-newsrc-alist.")
1839
1840 (defvar gnus-killed-list nil
1841   "List of killed newsgroups.")
1842
1843 (defvar gnus-killed-hashtb nil
1844   "Hash table equivalent of gnus-killed-list.")
1845
1846 (defvar gnus-zombie-list nil
1847   "List of almost dead newsgroups.")
1848
1849 (defvar gnus-description-hashtb nil
1850   "Descriptions of newsgroups.")
1851
1852 (defvar gnus-list-of-killed-groups nil
1853   "List of newsgroups that have recently been killed by the user.")
1854
1855 (defvar gnus-active-hashtb nil
1856   "Hashtable of active articles.")
1857
1858 (defvar gnus-moderated-list nil
1859   "List of moderated newsgroups.")
1860
1861 (defvar gnus-group-marked nil)
1862
1863 (defvar gnus-current-startup-file nil
1864   "Startup file for the current host.")
1865
1866 (defvar gnus-last-search-regexp nil
1867   "Default regexp for article search command.")
1868
1869 (defvar gnus-last-shell-command nil
1870   "Default shell command on article.")
1871
1872 (defvar gnus-current-select-method nil
1873   "The current method for selecting a newsgroup.")
1874
1875 (defvar gnus-group-list-mode nil)
1876
1877 (defvar gnus-article-internal-prepare-hook nil)
1878
1879 (defvar gnus-newsgroup-name nil)
1880 (defvar gnus-newsgroup-begin nil)
1881 (defvar gnus-newsgroup-end nil)
1882 (defvar gnus-newsgroup-last-rmail nil)
1883 (defvar gnus-newsgroup-last-mail nil)
1884 (defvar gnus-newsgroup-last-folder nil)
1885 (defvar gnus-newsgroup-last-file nil)
1886 (defvar gnus-newsgroup-auto-expire nil)
1887 (defvar gnus-newsgroup-active nil)
1888
1889 (defvar gnus-newsgroup-data nil)
1890 (defvar gnus-newsgroup-data-reverse nil)
1891 (defvar gnus-newsgroup-limit nil)
1892 (defvar gnus-newsgroup-limits nil)
1893
1894 (defvar gnus-newsgroup-unreads nil
1895   "List of unread articles in the current newsgroup.")
1896
1897 (defvar gnus-newsgroup-unselected nil
1898   "List of unselected unread articles in the current newsgroup.")
1899
1900 (defvar gnus-newsgroup-reads nil
1901   "Alist of read articles and article marks in the current newsgroup.")
1902
1903 (defvar gnus-newsgroup-expunged-tally nil)
1904
1905 (defvar gnus-newsgroup-marked nil
1906   "List of ticked articles in the current newsgroup (a subset of unread art).")
1907
1908 (defvar gnus-newsgroup-killed nil
1909   "List of ranges of articles that have been through the scoring process.")
1910
1911 (defvar gnus-newsgroup-cached nil
1912   "List of articles that come from the article cache.")
1913
1914 (defvar gnus-newsgroup-saved nil
1915   "List of articles that have been saved.")
1916
1917 (defvar gnus-newsgroup-kill-headers nil)
1918
1919 (defvar gnus-newsgroup-replied nil
1920   "List of articles that have been replied to in the current newsgroup.")
1921
1922 (defvar gnus-newsgroup-expirable nil
1923   "List of articles in the current newsgroup that can be expired.")
1924
1925 (defvar gnus-newsgroup-processable nil
1926   "List of articles in the current newsgroup that can be processed.")
1927
1928 (defvar gnus-newsgroup-bookmarks nil
1929   "List of articles in the current newsgroup that have bookmarks.")
1930
1931 (defvar gnus-newsgroup-dormant nil
1932   "List of dormant articles in the current newsgroup.")
1933
1934 (defvar gnus-newsgroup-scored nil
1935   "List of scored articles in the current newsgroup.")
1936
1937 (defvar gnus-newsgroup-headers nil
1938   "List of article headers in the current newsgroup.")
1939
1940 (defvar gnus-newsgroup-threads nil)
1941
1942 (defvar gnus-newsgroup-prepared nil
1943   "Whether the current group has been prepared properly.")
1944
1945 (defvar gnus-newsgroup-ancient nil
1946   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1947
1948 (defvar gnus-newsgroup-sparse nil)
1949
1950 (defvar gnus-current-article nil)
1951 (defvar gnus-article-current nil)
1952 (defvar gnus-current-headers nil)
1953 (defvar gnus-have-all-headers nil)
1954 (defvar gnus-last-article nil)
1955 (defvar gnus-newsgroup-history nil)
1956 (defvar gnus-current-kill-article nil)
1957
1958 ;; Save window configuration.
1959 (defvar gnus-prev-winconf nil)
1960
1961 (defvar gnus-summary-mark-positions nil)
1962 (defvar gnus-group-mark-positions nil)
1963
1964 (defvar gnus-reffed-article-number nil)
1965
1966 ;;; Let the byte-compiler know that we know about this variable.
1967 (defvar rmail-default-rmail-file)
1968
1969 (defvar gnus-cache-removable-articles nil)
1970
1971 (defvar gnus-dead-summary nil)
1972
1973 (defconst gnus-summary-local-variables
1974   '(gnus-newsgroup-name
1975     gnus-newsgroup-begin gnus-newsgroup-end
1976     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1977     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1978     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1979     gnus-newsgroup-unselected gnus-newsgroup-marked
1980     gnus-newsgroup-reads gnus-newsgroup-saved
1981     gnus-newsgroup-replied gnus-newsgroup-expirable
1982     gnus-newsgroup-processable gnus-newsgroup-killed
1983     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1984     gnus-newsgroup-headers gnus-newsgroup-threads
1985     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1986     gnus-current-article gnus-current-headers gnus-have-all-headers
1987     gnus-last-article gnus-article-internal-prepare-hook
1988     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1989     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1990     gnus-newsgroup-async gnus-thread-expunge-below
1991     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1992     (gnus-summary-mark-below . global)
1993     gnus-newsgroup-active gnus-scores-exclude-files
1994     gnus-newsgroup-history gnus-newsgroup-ancient
1995     gnus-newsgroup-sparse
1996     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1997     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1998     (gnus-newsgroup-expunged-tally . 0)
1999     gnus-cache-removable-articles gnus-newsgroup-cached
2000     gnus-newsgroup-data gnus-newsgroup-data-reverse
2001     gnus-newsgroup-limit gnus-newsgroup-limits)
2002   "Variables that are buffer-local to the summary buffers.")
2003
2004 (defconst gnus-bug-message
2005   "Sending a bug report to the Gnus Towers.
2006 ========================================
2007
2008 The buffer below is a mail buffer.  When you press `C-c C-c', it will
2009 be sent to the Gnus Bug Exterminators.
2010
2011 At the bottom of the buffer you'll see lots of variable settings.
2012 Please do not delete those.  They will tell the Bug People what your
2013 environment is, so that it will be easier to locate the bugs.
2014
2015 If you have found a bug that makes Emacs go \"beep\", set
2016 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
2017 and include the backtrace in your bug report.
2018
2019 Please describe the bug in annoying, painstaking detail.
2020
2021 Thank you for your help in stamping out bugs.
2022 ")
2023
2024 ;;; End of variables.
2025
2026 ;; Define some autoload functions Gnus might use.
2027 (eval-and-compile
2028
2029   ;; This little mapcar goes through the list below and marks the
2030   ;; symbols in question as autoloaded functions.
2031   (mapcar
2032    (lambda (package)
2033      (let ((interactive (nth 1 (memq ':interactive package))))
2034        (mapcar
2035         (lambda (function)
2036           (let (keymap)
2037             (when (consp function)
2038               (setq keymap (car (memq 'keymap function)))
2039               (setq function (car function)))
2040             (autoload function (car package) nil interactive keymap)))
2041         (if (eq (nth 1 package) ':interactive)
2042             (cdddr package)
2043           (cdr package)))))
2044    '(("metamail" metamail-buffer)
2045      ("info" Info-goto-node)
2046      ("hexl" hexl-hex-string-to-integer)
2047      ("pp" pp pp-to-string pp-eval-expression)
2048      ("mail-extr" mail-extract-address-components)
2049      ("nnmail" nnmail-split-fancy nnmail-article-group)
2050      ("nnvirtual" nnvirtual-catchup-group)
2051      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2052       timezone-make-sortable-date timezone-make-time-string)
2053      ("rmailout" rmail-output)
2054      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2055       rmail-show-message)
2056      ("gnus-soup" :interactive t
2057       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2058       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2059      ("nnsoup" nnsoup-pack-replies)
2060      ("score-mode" :interactive t gnus-score-mode)
2061      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2062       gnus-Folder-save-name gnus-folder-save-name)
2063      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2064      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2065       gnus-server-make-menu-bar gnus-article-make-menu-bar
2066       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2067       gnus-summary-highlight-line gnus-carpal-setup-buffer
2068       gnus-group-highlight-line
2069       gnus-article-add-button gnus-insert-next-page-button
2070       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2071      ("gnus-vis" :interactive t
2072       gnus-article-push-button gnus-article-press-button
2073       gnus-article-highlight gnus-article-highlight-some
2074       gnus-article-highlight-headers gnus-article-highlight-signature
2075       gnus-article-add-buttons gnus-article-add-buttons-to-head
2076       gnus-article-next-button gnus-article-prev-button)
2077      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2078       gnus-demon-add-disconnection gnus-demon-add-handler
2079       gnus-demon-remove-handler)
2080      ("gnus-demon" :interactive t
2081       gnus-demon-init gnus-demon-cancel)
2082      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2083       gnus-tree-open gnus-tree-close)
2084      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2085       gnus-nocem-unwanted-article-p)
2086      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2087      ("gnus-srvr" gnus-browse-foreign-server)
2088      ("gnus-cite" :interactive t
2089       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2090       gnus-article-hide-citation gnus-article-fill-cited-article
2091       gnus-article-hide-citation-in-followups)
2092      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2093       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2094       gnus-execute gnus-expunge)
2095      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2096       gnus-cache-possibly-remove-articles gnus-cache-request-article
2097       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2098       gnus-cache-enter-remove-article gnus-cached-article-p
2099       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2100      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2101       gnus-cache-remove-article)
2102      ("gnus-score" :interactive t
2103       gnus-summary-increase-score gnus-summary-lower-score
2104       gnus-score-flush-cache gnus-score-close
2105       gnus-score-raise-same-subject-and-select
2106       gnus-score-raise-same-subject gnus-score-default
2107       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2108       gnus-score-lower-same-subject gnus-score-lower-thread
2109       gnus-possibly-score-headers gnus-summary-raise-score 
2110       gnus-summary-set-score gnus-summary-current-score)
2111      ("gnus-score"
2112       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2113       gnus-current-score-file-nondirectory gnus-score-adaptive
2114       gnus-score-find-trace gnus-score-file-name)
2115      ("gnus-edit" :interactive t gnus-score-customize)
2116      ("gnus-topic" :interactive t gnus-topic-mode)
2117      ("gnus-topic" gnus-topic-remove-group)
2118      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2119      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2120      ("gnus-uu" :interactive t
2121       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2122       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2123       gnus-uu-mark-by-regexp gnus-uu-mark-all
2124       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2125       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2126       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2127       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2128       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2129       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2130       gnus-uu-decode-binhex-view)
2131      ("gnus-msg" (gnus-summary-send-map keymap)
2132       gnus-mail-yank-original gnus-mail-send-and-exit
2133       gnus-article-mail gnus-new-mail gnus-mail-reply
2134       gnus-copy-article-buffer)
2135      ("gnus-msg" :interactive t
2136       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2137       gnus-summary-followup gnus-summary-followup-with-original
2138       gnus-summary-cancel-article gnus-summary-supersede-article
2139       gnus-post-news gnus-inews-news 
2140       gnus-summary-reply gnus-summary-reply-with-original
2141       gnus-summary-mail-forward gnus-summary-mail-other-window
2142       gnus-bug)
2143      ("gnus-picon" :interactive t gnus-article-display-picons
2144       gnus-group-display-picons gnus-picons-article-display-x-face
2145       gnus-picons-display-x-face)
2146      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2147       gnus-grouplens-mode)
2148      ("smiley" :interactive t gnus-smiley-display)
2149      ("gnus-vm" gnus-vm-mail-setup)
2150      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2151       gnus-summary-save-article-vm))))
2152
2153 \f
2154
2155 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2156 ;; If you want the cursor to go somewhere else, set these two
2157 ;; functions in some startup hook to whatever you want.
2158 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2159 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2160
2161 ;;; Various macros and substs.
2162
2163 (defun gnus-header-from (header)
2164   (mail-header-from header))
2165
2166 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2167   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2168   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2169         (w (make-symbol "w"))
2170         (buf (make-symbol "buf")))
2171     `(let* ((,tempvar (selected-window))
2172             (,buf ,buffer)
2173             (,w (get-buffer-window ,buf 'visible)))
2174        (unwind-protect
2175            (progn
2176              (if ,w
2177                  (select-window ,w)
2178                (pop-to-buffer ,buf))
2179              ,@forms)
2180          (select-window ,tempvar)))))
2181
2182 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2183 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2184 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2185
2186 (defmacro gnus-gethash (string hashtable)
2187   "Get hash value of STRING in HASHTABLE."
2188   `(symbol-value (intern-soft ,string ,hashtable)))
2189
2190 (defmacro gnus-sethash (string value hashtable)
2191   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2192   `(set (intern ,string ,hashtable) ,value))
2193
2194 (defmacro gnus-intern-safe (string hashtable)
2195   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2196   `(let ((symbol (intern ,string ,hashtable)))
2197      (or (boundp symbol)
2198          (set symbol nil))
2199      symbol))
2200
2201 (defmacro gnus-group-unread (group)
2202   "Get the currently computed number of unread articles in GROUP."
2203   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2204
2205 (defmacro gnus-group-entry (group)
2206   "Get the newsrc entry for GROUP."
2207   `(gnus-gethash ,group gnus-newsrc-hashtb))
2208
2209 (defmacro gnus-active (group)
2210   "Get active info on GROUP."
2211   `(gnus-gethash ,group gnus-active-hashtb))
2212
2213 (defmacro gnus-set-active (group active)
2214   "Set GROUP's active info."
2215   `(gnus-sethash ,group ,active gnus-active-hashtb))
2216
2217 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2218 ;;   function `substring' might cut on a middle of multi-octet
2219 ;;   character.
2220 (defun gnus-truncate-string (str width)
2221   (substring str 0 width))
2222
2223 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2224 ;; to limit the length of a string.  This function is necessary since
2225 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2226 (defsubst gnus-limit-string (str width)
2227   (if (> (length str) width)
2228       (substring str 0 width)
2229     str))
2230
2231 (defsubst gnus-simplify-subject-re (subject)
2232   "Remove \"Re:\" from subject lines."
2233   (if (string-match "^[Rr][Ee]: *" subject)
2234       (substring subject (match-end 0))
2235     subject))
2236
2237 (defsubst gnus-functionp (form)
2238   "Return non-nil if FORM is funcallable."
2239   (or (and (symbolp form) (fboundp form))
2240       (and (listp form) (eq (car form) 'lambda))))
2241
2242 (defsubst gnus-goto-char (point)
2243   (and point (goto-char point)))
2244
2245 (defmacro gnus-buffer-exists-p (buffer)
2246   `(let ((buffer ,buffer))
2247      (and buffer
2248           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2249                    buffer))))
2250
2251 (defmacro gnus-kill-buffer (buffer)
2252   `(let ((buf ,buffer))
2253      (if (gnus-buffer-exists-p buf)
2254          (kill-buffer buf))))
2255
2256 (defsubst gnus-point-at-bol ()
2257   "Return point at the beginning of the line."
2258   (let ((p (point)))
2259     (beginning-of-line)
2260     (prog1
2261         (point)
2262       (goto-char p))))
2263
2264 (defsubst gnus-point-at-eol ()
2265   "Return point at the end of the line."
2266   (let ((p (point)))
2267     (end-of-line)
2268     (prog1
2269         (point)
2270       (goto-char p))))
2271
2272 (defun gnus-alive-p ()
2273   "Say whether Gnus is running or not."
2274   (and gnus-group-buffer
2275        (get-buffer gnus-group-buffer)))
2276
2277 (defun gnus-delete-first (elt list)
2278   "Delete by side effect the first occurrence of ELT as a member of LIST."
2279   (if (equal (car list) elt)
2280       (cdr list)
2281     (let ((total list))
2282       (while (and (cdr list)
2283                   (not (equal (cadr list) elt)))
2284         (setq list (cdr list)))
2285       (when (cdr list)
2286         (setcdr list (cddr list)))
2287       total)))
2288
2289 ;; Delete the current line (and the next N lines.);
2290 (defmacro gnus-delete-line (&optional n)
2291   `(delete-region (progn (beginning-of-line) (point))
2292                   (progn (forward-line ,(or n 1)) (point))))
2293
2294 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2295 (defvar gnus-init-inhibit nil)
2296 (defun gnus-read-init-file (&optional inhibit-next)
2297   (if gnus-init-inhibit
2298       (setq gnus-init-inhibit nil)
2299     (setq gnus-init-inhibit inhibit-next)
2300     (and gnus-init-file
2301          (or (and (file-exists-p gnus-init-file)
2302                   ;; Don't try to load a directory.
2303                   (not (file-directory-p gnus-init-file)))
2304              (file-exists-p (concat gnus-init-file ".el"))
2305              (file-exists-p (concat gnus-init-file ".elc")))
2306          (condition-case var
2307              (load gnus-init-file nil t)
2308            (error
2309             (error "Error in %s: %s" gnus-init-file var))))))
2310
2311 ;; Info access macros.
2312
2313 (defmacro gnus-info-group (info)
2314   `(nth 0 ,info))
2315 (defmacro gnus-info-rank (info)
2316   `(nth 1 ,info))
2317 (defmacro gnus-info-read (info)
2318   `(nth 2 ,info))
2319 (defmacro gnus-info-marks (info)
2320   `(nth 3 ,info))
2321 (defmacro gnus-info-method (info)
2322   `(nth 4 ,info))
2323 (defmacro gnus-info-params (info)
2324   `(nth 5 ,info))
2325
2326 (defmacro gnus-info-level (info)
2327   `(let ((rank (gnus-info-rank ,info)))
2328      (if (consp rank)
2329          (car rank)
2330        rank)))
2331 (defmacro gnus-info-score (info)
2332   `(let ((rank (gnus-info-rank ,info)))
2333      (or (and (consp rank) (cdr rank)) 0)))
2334
2335 (defmacro gnus-info-set-group (info group)
2336   `(setcar ,info ,group))
2337 (defmacro gnus-info-set-rank (info rank)
2338   `(setcar (nthcdr 1 ,info) ,rank))
2339 (defmacro gnus-info-set-read (info read)
2340   `(setcar (nthcdr 2 ,info) ,read))
2341 (defmacro gnus-info-set-marks (info marks)
2342   `(setcar (nthcdr 3 ,info) ,marks))
2343 (defmacro gnus-info-set-method (info method)
2344   `(setcar (nthcdr 4 ,info) ,method))
2345 (defmacro gnus-info-set-params (info params)
2346   `(setcar (nthcdr 5 ,info) ,params))
2347
2348 (defmacro gnus-info-set-level (info level)
2349   `(let ((rank (cdr ,info)))
2350      (if (consp (car rank))
2351          (setcar (car rank) ,level)
2352        (setcar rank ,level))))
2353 (defmacro gnus-info-set-score (info score)
2354   `(let ((rank (cdr ,info)))
2355      (if (consp (car rank))
2356          (setcdr (car rank) ,score)
2357        (setcar rank (cons (car rank) ,score)))))
2358
2359 (defmacro gnus-get-info (group)
2360   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2361
2362 (defun gnus-byte-code (func)
2363   "Return a form that can be `eval'ed based on FUNC."
2364   (let ((fval (symbol-function func)))
2365     (if (byte-code-function-p fval)
2366         (let ((flist (append fval nil)))
2367           (setcar flist 'byte-code)
2368           flist)
2369       (cons 'progn (cddr fval)))))
2370
2371 ;; Find out whether the gnus-visual TYPE is wanted.
2372 (defun gnus-visual-p (&optional type class)
2373   (and gnus-visual                      ; Has to be non-nil, at least.
2374        (if (not type)                   ; We don't care about type.
2375            gnus-visual
2376          (if (listp gnus-visual)        ; It's a list, so we check it.
2377              (or (memq type gnus-visual)
2378                  (memq class gnus-visual))
2379            t))))
2380
2381 ;;; Load the compatability functions.
2382
2383 (require 'gnus-cus)
2384 (require 'gnus-ems)
2385
2386 \f
2387 ;;;
2388 ;;; Shutdown
2389 ;;;
2390
2391 (defvar gnus-shutdown-alist nil)
2392
2393 (defun gnus-add-shutdown (function &rest symbols)
2394   "Run FUNCTION whenever one of SYMBOLS is shut down."
2395   (push (cons function symbols) gnus-shutdown-alist))
2396
2397 (defun gnus-shutdown (symbol)
2398   "Shut down everything that waits for SYMBOL."
2399   (let ((alist gnus-shutdown-alist)
2400         entry)
2401     (while (setq entry (pop alist))
2402       (when (memq symbol (cdr entry))
2403         (funcall (car entry))))))
2404
2405 \f
2406
2407 ;; Format specs.  The chunks below are the machine-generated forms
2408 ;; that are to be evaled as the result of the default format strings.
2409 ;; We write them in here to get them byte-compiled.  That way the
2410 ;; default actions will be quite fast, while still retaining the full
2411 ;; flexibility of the user-defined format specs.
2412
2413 ;; First we have lots of dummy defvars to let the compiler know these
2414 ;; are really dynamic variables.
2415
2416 (defvar gnus-tmp-unread)
2417 (defvar gnus-tmp-replied)
2418 (defvar gnus-tmp-score-char)
2419 (defvar gnus-tmp-indentation)
2420 (defvar gnus-tmp-opening-bracket)
2421 (defvar gnus-tmp-lines)
2422 (defvar gnus-tmp-name)
2423 (defvar gnus-tmp-closing-bracket)
2424 (defvar gnus-tmp-subject-or-nil)
2425 (defvar gnus-tmp-subject)
2426 (defvar gnus-tmp-marked)
2427 (defvar gnus-tmp-marked-mark)
2428 (defvar gnus-tmp-subscribed)
2429 (defvar gnus-tmp-process-marked)
2430 (defvar gnus-tmp-number-of-unread)
2431 (defvar gnus-tmp-group-name)
2432 (defvar gnus-tmp-group)
2433 (defvar gnus-tmp-article-number)
2434 (defvar gnus-tmp-unread-and-unselected)
2435 (defvar gnus-tmp-news-method)
2436 (defvar gnus-tmp-news-server)
2437 (defvar gnus-tmp-article-number)
2438 (defvar gnus-mouse-face)
2439 (defvar gnus-mouse-face-prop)
2440
2441 (defun gnus-summary-line-format-spec ()
2442   (insert gnus-tmp-unread gnus-tmp-replied
2443           gnus-tmp-score-char gnus-tmp-indentation)
2444   (gnus-put-text-property
2445    (point)
2446    (progn
2447      (insert
2448       gnus-tmp-opening-bracket
2449       (format "%4d: %-20s"
2450               gnus-tmp-lines
2451               (if (> (length gnus-tmp-name) 20)
2452                   (substring gnus-tmp-name 0 20)
2453                 gnus-tmp-name))
2454       gnus-tmp-closing-bracket)
2455      (point))
2456    gnus-mouse-face-prop gnus-mouse-face)
2457   (insert " " gnus-tmp-subject-or-nil "\n"))
2458
2459 (defvar gnus-summary-line-format-spec
2460   (gnus-byte-code 'gnus-summary-line-format-spec))
2461
2462 (defun gnus-summary-dummy-line-format-spec ()
2463   (insert "*  ")
2464   (gnus-put-text-property
2465    (point)
2466    (progn
2467      (insert ":                          :")
2468      (point))
2469    gnus-mouse-face-prop gnus-mouse-face)
2470   (insert " " gnus-tmp-subject "\n"))
2471
2472 (defvar gnus-summary-dummy-line-format-spec
2473   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2474
2475 (defun gnus-group-line-format-spec ()
2476   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2477           gnus-tmp-process-marked
2478           gnus-group-indentation
2479           (format "%5s: " gnus-tmp-number-of-unread))
2480   (gnus-put-text-property
2481    (point)
2482    (progn
2483      (insert gnus-tmp-group "\n")
2484      (1- (point)))
2485    gnus-mouse-face-prop gnus-mouse-face))
2486 (defvar gnus-group-line-format-spec
2487   (gnus-byte-code 'gnus-group-line-format-spec))
2488
2489 (defvar gnus-format-specs
2490   `((version . ,emacs-version)
2491     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2492     (summary-dummy ,gnus-summary-dummy-line-format
2493                    ,gnus-summary-dummy-line-format-spec)
2494     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2495
2496 (defvar gnus-article-mode-line-format-spec nil)
2497 (defvar gnus-summary-mode-line-format-spec nil)
2498 (defvar gnus-group-mode-line-format-spec nil)
2499
2500 ;;; Phew.  All that gruft is over, fortunately.
2501
2502 \f
2503 ;;;
2504 ;;; Gnus Utility Functions
2505 ;;;
2506
2507 (defun gnus-extract-address-components (from)
2508   (let (name address)
2509     ;; First find the address - the thing with the @ in it.  This may
2510     ;; not be accurate in mail addresses, but does the trick most of
2511     ;; the time in news messages.
2512     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2513         (setq address (substring from (match-beginning 0) (match-end 0))))
2514     ;; Then we check whether the "name <address>" format is used.
2515     (and address
2516          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2517          ;; Linear white space is not required.
2518          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2519          (and (setq name (substring from 0 (match-beginning 0)))
2520               ;; Strip any quotes from the name.
2521               (string-match "\".*\"" name)
2522               (setq name (substring name 1 (1- (match-end 0))))))
2523     ;; If not, then "address (name)" is used.
2524     (or name
2525         (and (string-match "(.+)" from)
2526              (setq name (substring from (1+ (match-beginning 0))
2527                                    (1- (match-end 0)))))
2528         (and (string-match "()" from)
2529              (setq name address))
2530         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2531         ;; XOVER might not support folded From headers.
2532         (and (string-match "(.*" from)
2533              (setq name (substring from (1+ (match-beginning 0))
2534                                    (match-end 0)))))
2535     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2536     (list (or name from) (or address from))))
2537
2538 (defun gnus-fetch-field (field)
2539   "Return the value of the header FIELD of current article."
2540   (save-excursion
2541     (save-restriction
2542       (let ((case-fold-search t)
2543             (inhibit-point-motion-hooks t))
2544         (nnheader-narrow-to-headers)
2545         (message-fetch-field field)))))
2546
2547 (defun gnus-goto-colon ()
2548   (beginning-of-line)
2549   (search-forward ":" (gnus-point-at-eol) t))
2550
2551 ;;;###autoload
2552 (defun gnus-update-format (var)
2553   "Update the format specification near point."
2554   (interactive
2555    (list
2556     (save-excursion
2557       (eval-defun nil)
2558       ;; Find the end of the current word.
2559       (re-search-forward "[ \t\n]" nil t)
2560       ;; Search backward.
2561       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2562         (match-string 1)))))
2563   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2564                               (match-string 1 var))))
2565          (entry (assq type gnus-format-specs))
2566          value spec)
2567     (when entry
2568       (setq gnus-format-specs (delq entry gnus-format-specs)))
2569     (set
2570      (intern (format "%s-spec" var))
2571      (gnus-parse-format (setq value (symbol-value (intern var)))
2572                         (symbol-value (intern (format "%s-alist" var)))
2573                         (not (string-match "mode" var))))
2574     (setq spec (symbol-value (intern (format "%s-spec" var))))
2575     (push (list type value spec) gnus-format-specs)
2576
2577     (pop-to-buffer "*Gnus Format*")
2578     (erase-buffer)
2579     (lisp-interaction-mode)
2580     (insert (pp-to-string spec))))
2581
2582 (defun gnus-update-format-specifications (&optional force)
2583   "Update all (necessary) format specifications."
2584   ;; Make the indentation array.
2585   (gnus-make-thread-indent-array)
2586
2587   ;; See whether all the stored info needs to be flushed.
2588   (when (or force
2589             (not (equal emacs-version
2590                         (cdr (assq 'version gnus-format-specs)))))
2591     (setq gnus-format-specs nil))
2592
2593   ;; Go through all the formats and see whether they need updating.
2594   (let ((types '(summary summary-dummy group
2595                          summary-mode group-mode article-mode))
2596         new-format entry type val)
2597     (while (setq type (pop types))
2598       ;; Jump to the proper buffer to find out the value of
2599       ;; the variable, if possible.  (It may be buffer-local.)
2600       (save-excursion
2601         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2602               val)
2603           (when (and (boundp buffer)
2604                      (setq val (symbol-value buffer))
2605                      (get-buffer val)
2606                      (buffer-name (get-buffer val)))
2607             (set-buffer (get-buffer val)))
2608           (setq new-format (symbol-value
2609                             (intern (format "gnus-%s-line-format" type))))))
2610       (setq entry (cdr (assq type gnus-format-specs)))
2611       (if (and entry
2612                (equal (car entry) new-format))
2613           ;; Use the old format.
2614           (set (intern (format "gnus-%s-line-format-spec" type))
2615                (cadr entry))
2616         ;; This is a new format.
2617         (setq val
2618               (if (not (stringp new-format))
2619                   ;; This is a function call or something.
2620                   new-format
2621                 ;; This is a "real" format.
2622                 (gnus-parse-format
2623                  new-format
2624                  (symbol-value
2625                   (intern (format "gnus-%s-line-format-alist"
2626                                   (if (eq type 'article-mode)
2627                                       'summary-mode type))))
2628                  (not (string-match "mode$" (symbol-name type))))))
2629         ;; Enter the new format spec into the list.
2630         (if entry
2631             (progn
2632               (setcar (cdr entry) val)
2633               (setcar entry new-format))
2634           (push (list type new-format val) gnus-format-specs))
2635         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2636
2637   (unless (assq 'version gnus-format-specs)
2638     (push (cons 'version emacs-version) gnus-format-specs))
2639
2640   (gnus-update-group-mark-positions)
2641   (gnus-update-summary-mark-positions))
2642
2643 (defun gnus-update-summary-mark-positions ()
2644   "Compute where the summary marks are to go."
2645   (save-excursion
2646     (when (and gnus-summary-buffer
2647                (get-buffer gnus-summary-buffer)
2648                (buffer-name (get-buffer gnus-summary-buffer)))
2649       (set-buffer gnus-summary-buffer))
2650     (let ((gnus-replied-mark 129)
2651           (gnus-score-below-mark 130)
2652           (gnus-score-over-mark 130)
2653           (thread nil)
2654           (gnus-visual nil)
2655           (spec gnus-summary-line-format-spec)
2656           pos)
2657       (save-excursion
2658         (gnus-set-work-buffer)
2659         (let ((gnus-summary-line-format-spec spec))
2660           (gnus-summary-insert-line
2661            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2662           (goto-char (point-min))
2663           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2664                                              (- (point) 2)))))
2665           (goto-char (point-min))
2666           (push (cons 'replied (and (search-forward "\201" nil t) 
2667                                     (- (point) 2)))
2668                 pos)
2669           (goto-char (point-min))
2670           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2671                 pos)))
2672       (setq gnus-summary-mark-positions pos))))
2673
2674 (defun gnus-update-group-mark-positions ()
2675   (save-excursion
2676     (let ((gnus-process-mark 128)
2677           (gnus-group-marked '("dummy.group"))
2678           (gnus-active-hashtb (make-vector 10 0)))
2679       (gnus-set-active "dummy.group" '(0 . 0))
2680       (gnus-set-work-buffer)
2681       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2682       (goto-char (point-min))
2683       (setq gnus-group-mark-positions
2684             (list (cons 'process (and (search-forward "\200" nil t)
2685                                       (- (point) 2))))))))
2686
2687 (defvar gnus-mouse-face-0 'highlight)
2688 (defvar gnus-mouse-face-1 'highlight)
2689 (defvar gnus-mouse-face-2 'highlight)
2690 (defvar gnus-mouse-face-3 'highlight)
2691 (defvar gnus-mouse-face-4 'highlight)
2692
2693 (defun gnus-mouse-face-function (form type)
2694   `(gnus-put-text-property
2695     (point) (progn ,@form (point))
2696     gnus-mouse-face-prop
2697     ,(if (equal type 0)
2698          'gnus-mouse-face
2699        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2700
2701 (defvar gnus-face-0 'bold)
2702 (defvar gnus-face-1 'italic)
2703 (defvar gnus-face-2 'bold-italic)
2704 (defvar gnus-face-3 'bold)
2705 (defvar gnus-face-4 'bold)
2706
2707 (defun gnus-face-face-function (form type)
2708   `(gnus-put-text-property
2709     (point) (progn ,@form (point))
2710     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2711
2712 (defun gnus-max-width-function (el max-width)
2713   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2714   (if (symbolp el)
2715       `(if (> (length ,el) ,max-width)
2716            (substring ,el 0 ,max-width)
2717          ,el)
2718     `(let ((val (eval ,el)))
2719        (if (numberp val)
2720            (setq val (int-to-string val)))
2721        (if (> (length val) ,max-width)
2722            (substring val 0 ,max-width)
2723          val))))
2724
2725 (defun gnus-parse-format (format spec-alist &optional insert)
2726   ;; This function parses the FORMAT string with the help of the
2727   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2728   ;; string.  If the FORMAT string contains the specifiers %( and %)
2729   ;; the text between them will have the mouse-face text property.
2730   (if (string-match
2731        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2732        format)
2733       (gnus-parse-complex-format format spec-alist)
2734     ;; This is a simple format.
2735     (gnus-parse-simple-format format spec-alist insert)))
2736
2737 (defun gnus-parse-complex-format (format spec-alist)
2738   (save-excursion
2739     (gnus-set-work-buffer)
2740     (insert format)
2741     (goto-char (point-min))
2742     (while (re-search-forward "\"" nil t)
2743       (replace-match "\\\"" nil t))
2744     (goto-char (point-min))
2745     (insert "(\"")
2746     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2747       (let ((number (if (match-beginning 1)
2748                         (match-string 1) "0"))
2749             (delim (aref (match-string 2) 0)))
2750         (if (or (= delim ?\() (= delim ?\{))
2751             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2752                                    " " number " \""))
2753           (replace-match "\")\""))))
2754     (goto-char (point-max))
2755     (insert "\")")
2756     (goto-char (point-min))
2757     (let ((form (read (current-buffer))))
2758       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2759
2760 (defun gnus-complex-form-to-spec (form spec-alist)
2761   (delq nil
2762         (mapcar
2763          (lambda (sform)
2764            (if (stringp sform)
2765                (gnus-parse-simple-format sform spec-alist t)
2766              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2767                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2768                       (nth 1 sform))))
2769          form)))
2770
2771 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2772   ;; This function parses the FORMAT string with the help of the
2773   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2774   ;; string.
2775   (let ((max-width 0)
2776         spec flist fstring newspec elem beg result dontinsert)
2777     (save-excursion
2778       (gnus-set-work-buffer)
2779       (insert format)
2780       (goto-char (point-min))
2781       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2782                                 nil t)
2783         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2784               (setq newspec "%"
2785                     beg (1+ (match-beginning 0)))
2786           ;; First check if there are any specs that look anything like
2787           ;; "%12,12A", ie. with a "max width specification".  These have
2788           ;; to be treated specially.
2789           (if (setq beg (match-beginning 1))
2790               (setq max-width
2791                     (string-to-int
2792                      (buffer-substring
2793                       (1+ (match-beginning 1)) (match-end 1))))
2794             (setq max-width 0)
2795             (setq beg (match-beginning 2)))
2796           ;; Find the specification from `spec-alist'.
2797           (unless (setq elem (cdr (assq spec spec-alist)))
2798             (setq elem '("*" ?s)))
2799           ;; Treat user defined format specifiers specially.
2800           (when (eq (car elem) 'gnus-tmp-user-defined)
2801             (setq elem
2802                   (list
2803                    (list (intern (concat "gnus-user-format-function-"
2804                                          (match-string 3)))
2805                          'gnus-tmp-header) ?s))
2806             (delete-region (match-beginning 3) (match-end 3)))
2807           (if (not (zerop max-width))
2808               (let ((el (car elem)))
2809                 (cond ((= (cadr elem) ?c)
2810                        (setq el (list 'char-to-string el)))
2811                       ((= (cadr elem) ?d)
2812                        (setq el (list 'int-to-string el))))
2813                 (setq flist (cons (gnus-max-width-function el max-width)
2814                                   flist))
2815                 (setq newspec ?s))
2816             (progn
2817               (setq flist (cons (car elem) flist))
2818               (setq newspec (cadr elem)))))
2819         ;; Remove the old specification (and possibly a ",12" string).
2820         (delete-region beg (match-end 2))
2821         ;; Insert the new specification.
2822         (goto-char beg)
2823         (insert newspec))
2824       (setq fstring (buffer-substring 1 (point-max))))
2825     ;; Do some postprocessing to increase efficiency.
2826     (setq
2827      result
2828      (cond
2829       ;; Emptyness.
2830       ((string= fstring "")
2831        nil)
2832       ;; Not a format string.
2833       ((not (string-match "%" fstring))
2834        (list fstring))
2835       ;; A format string with just a single string spec.
2836       ((string= fstring "%s")
2837        (list (car flist)))
2838       ;; A single character.
2839       ((string= fstring "%c")
2840        (list (car flist)))
2841       ;; A single number.
2842       ((string= fstring "%d")
2843        (setq dontinsert)
2844        (if insert
2845            (list `(princ ,(car flist)))
2846          (list `(int-to-string ,(car flist)))))
2847       ;; Just lots of chars and strings.
2848       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2849        (nreverse flist))
2850       ;; A single string spec at the beginning of the spec.
2851       ((string-match "\\`%[sc][^%]+\\'" fstring)
2852        (list (car flist) (substring fstring 2)))
2853       ;; A single string spec in the middle of the spec.
2854       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2855        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2856       ;; A single string spec in the end of the spec.
2857       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2858        (list (match-string 1 fstring) (car flist)))
2859       ;; A more complex spec.
2860       (t
2861        (list (cons 'format (cons fstring (nreverse flist)))))))
2862
2863     (if insert
2864         (when result
2865           (if dontinsert
2866               result
2867             (cons 'insert result)))
2868       (cond ((stringp result)
2869              result)
2870             ((consp result)
2871              (cons 'concat result))
2872             (t "")))))
2873
2874 (defun gnus-eval-format (format &optional alist props)
2875   "Eval the format variable FORMAT, using ALIST.
2876 If PROPS, insert the result."
2877   (let ((form (gnus-parse-format format alist props)))
2878     (if props
2879         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2880       (eval form))))
2881
2882 (defun gnus-remove-text-with-property (prop)
2883   "Delete all text in the current buffer with text property PROP."
2884   (save-excursion
2885     (goto-char (point-min))
2886     (while (not (eobp))
2887       (while (get-text-property (point) prop)
2888         (delete-char 1))
2889       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2890
2891 (defun gnus-set-work-buffer ()
2892   (if (get-buffer gnus-work-buffer)
2893       (progn
2894         (set-buffer gnus-work-buffer)
2895         (erase-buffer))
2896     (set-buffer (get-buffer-create gnus-work-buffer))
2897     (kill-all-local-variables)
2898     (buffer-disable-undo (current-buffer))
2899     (gnus-add-current-to-buffer-list)))
2900
2901 ;; Article file names when saving.
2902
2903 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2904   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2905 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2906 Otherwise, it is like ~/News/news/group/num."
2907   (let ((default
2908           (expand-file-name
2909            (concat (if (gnus-use-long-file-name 'not-save)
2910                        (gnus-capitalize-newsgroup newsgroup)
2911                      (gnus-newsgroup-directory-form newsgroup))
2912                    "/" (int-to-string (mail-header-number headers)))
2913            gnus-article-save-directory)))
2914     (if (and last-file
2915              (string-equal (file-name-directory default)
2916                            (file-name-directory last-file))
2917              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2918         default
2919       (or last-file default))))
2920
2921 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2922   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2923 If variable `gnus-use-long-file-name' is non-nil, it is
2924 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2925   (let ((default
2926           (expand-file-name
2927            (concat (if (gnus-use-long-file-name 'not-save)
2928                        newsgroup
2929                      (gnus-newsgroup-directory-form newsgroup))
2930                    "/" (int-to-string (mail-header-number headers)))
2931            gnus-article-save-directory)))
2932     (if (and last-file
2933              (string-equal (file-name-directory default)
2934                            (file-name-directory last-file))
2935              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2936         default
2937       (or last-file default))))
2938
2939 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2940   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2941 If variable `gnus-use-long-file-name' is non-nil, it is
2942 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2943   (or last-file
2944       (expand-file-name
2945        (if (gnus-use-long-file-name 'not-save)
2946            (gnus-capitalize-newsgroup newsgroup)
2947          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2948        gnus-article-save-directory)))
2949
2950 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2951   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2952 If variable `gnus-use-long-file-name' is non-nil, it is
2953 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2954   (or last-file
2955       (expand-file-name
2956        (if (gnus-use-long-file-name 'not-save)
2957            newsgroup
2958          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2959        gnus-article-save-directory)))
2960
2961 ;; For subscribing new newsgroup
2962
2963 (defun gnus-subscribe-hierarchical-interactive (groups)
2964   (let ((groups (sort groups 'string<))
2965         prefixes prefix start ans group starts)
2966     (while groups
2967       (setq prefixes (list "^"))
2968       (while (and groups prefixes)
2969         (while (not (string-match (car prefixes) (car groups)))
2970           (setq prefixes (cdr prefixes)))
2971         (setq prefix (car prefixes))
2972         (setq start (1- (length prefix)))
2973         (if (and (string-match "[^\\.]\\." (car groups) start)
2974                  (cdr groups)
2975                  (setq prefix
2976                        (concat "^" (substring (car groups) 0 (match-end 0))))
2977                  (string-match prefix (cadr groups)))
2978             (progn
2979               (setq prefixes (cons prefix prefixes))
2980               (message "Descend hierarchy %s? ([y]nsq): "
2981                        (substring prefix 1 (1- (length prefix))))
2982               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2983                 (ding)
2984                 (message "Descend hierarchy %s? ([y]nsq): "
2985                          (substring prefix 1 (1- (length prefix)))))
2986               (cond ((= ans ?n)
2987                      (while (and groups
2988                                  (string-match prefix
2989                                                (setq group (car groups))))
2990                        (setq gnus-killed-list
2991                              (cons group gnus-killed-list))
2992                        (gnus-sethash group group gnus-killed-hashtb)
2993                        (setq groups (cdr groups)))
2994                      (setq starts (cdr starts)))
2995                     ((= ans ?s)
2996                      (while (and groups
2997                                  (string-match prefix
2998                                                (setq group (car groups))))
2999                        (gnus-sethash group group gnus-killed-hashtb)
3000                        (gnus-subscribe-alphabetically (car groups))
3001                        (setq groups (cdr groups)))
3002                      (setq starts (cdr starts)))
3003                     ((= ans ?q)
3004                      (while groups
3005                        (setq group (car groups))
3006                        (setq gnus-killed-list (cons group gnus-killed-list))
3007                        (gnus-sethash group group gnus-killed-hashtb)
3008                        (setq groups (cdr groups))))
3009                     (t nil)))
3010           (message "Subscribe %s? ([n]yq)" (car groups))
3011           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
3012             (ding)
3013             (message "Subscribe %s? ([n]yq)" (car groups)))
3014           (setq group (car groups))
3015           (cond ((= ans ?y)
3016                  (gnus-subscribe-alphabetically (car groups))
3017                  (gnus-sethash group group gnus-killed-hashtb))
3018                 ((= ans ?q)
3019                  (while groups
3020                    (setq group (car groups))
3021                    (setq gnus-killed-list (cons group gnus-killed-list))
3022                    (gnus-sethash group group gnus-killed-hashtb)
3023                    (setq groups (cdr groups))))
3024                 (t
3025                  (setq gnus-killed-list (cons group gnus-killed-list))
3026                  (gnus-sethash group group gnus-killed-hashtb)))
3027           (setq groups (cdr groups)))))))
3028
3029 (defun gnus-subscribe-randomly (newsgroup)
3030   "Subscribe new NEWSGROUP by making it the first newsgroup."
3031   (gnus-subscribe-newsgroup newsgroup))
3032
3033 (defun gnus-subscribe-alphabetically (newgroup)
3034   "Subscribe new NEWSGROUP and insert it in alphabetical order."
3035   (let ((groups (cdr gnus-newsrc-alist))
3036         before)
3037     (while (and (not before) groups)
3038       (if (string< newgroup (caar groups))
3039           (setq before (caar groups))
3040         (setq groups (cdr groups))))
3041     (gnus-subscribe-newsgroup newgroup before)))
3042
3043 (defun gnus-subscribe-hierarchically (newgroup)
3044   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
3045   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
3046   (save-excursion
3047     (set-buffer (find-file-noselect gnus-current-startup-file))
3048     (let ((groupkey newgroup)
3049           before)
3050       (while (and (not before) groupkey)
3051         (goto-char (point-min))
3052         (let ((groupkey-re
3053                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3054           (while (and (re-search-forward groupkey-re nil t)
3055                       (progn
3056                         (setq before (match-string 1))
3057                         (string< before newgroup)))))
3058         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3059         (setq groupkey
3060               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3061                   (substring groupkey (match-beginning 1) (match-end 1)))))
3062       (gnus-subscribe-newsgroup newgroup before))
3063     (kill-buffer (current-buffer))))
3064
3065 (defun gnus-subscribe-interactively (group)
3066   "Subscribe the new GROUP interactively.
3067 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3068 it is killed."
3069   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3070       (gnus-subscribe-hierarchically group)
3071     (push group gnus-killed-list)))
3072
3073 (defun gnus-subscribe-zombies (group)
3074   "Make the new GROUP into a zombie group."
3075   (push group gnus-zombie-list))
3076
3077 (defun gnus-subscribe-killed (group)
3078   "Make the new GROUP a killed group."
3079   (push group gnus-killed-list))
3080
3081 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3082   "Subscribe new NEWSGROUP.
3083 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3084 the first newsgroup."
3085   (save-excursion
3086     (goto-char (point-min))
3087     ;; We subscribe the group by changing its level to `subscribed'.
3088     (gnus-group-change-level
3089      newsgroup gnus-level-default-subscribed
3090      gnus-level-killed (gnus-gethash (or next "dummy.group")
3091                                      gnus-newsrc-hashtb))
3092     (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)))
3093
3094 ;; For directories
3095
3096 (defun gnus-newsgroup-directory-form (newsgroup)
3097   "Make hierarchical directory name from NEWSGROUP name."
3098   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3099         (len (length newsgroup))
3100         idx)
3101     ;; If this is a foreign group, we don't want to translate the
3102     ;; entire name.
3103     (if (setq idx (string-match ":" newsgroup))
3104         (aset newsgroup idx ?/)
3105       (setq idx 0))
3106     ;; Replace all occurrences of `.' with `/'.
3107     (while (< idx len)
3108       (if (= (aref newsgroup idx) ?.)
3109           (aset newsgroup idx ?/))
3110       (setq idx (1+ idx)))
3111     newsgroup))
3112
3113 (defun gnus-newsgroup-savable-name (group)
3114   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3115   ;; with dots.
3116   (nnheader-replace-chars-in-string group ?/ ?.))
3117
3118 (defun gnus-make-directory (dir)
3119   "Make DIRECTORY recursively."
3120   (unless dir
3121     (error "No directory to make"))
3122   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3123   ;; of the many mysteries of the universe.
3124   (let* ((dir (expand-file-name dir default-directory))
3125          dirs err)
3126     (if (string-match "/$" dir)
3127         (setq dir (substring dir 0 (match-beginning 0))))
3128     ;; First go down the path until we find a directory that exists.
3129     (while (not (file-exists-p dir))
3130       (setq dirs (cons dir dirs))
3131       (string-match "/[^/]+$" dir)
3132       (setq dir (substring dir 0 (match-beginning 0))))
3133     ;; Then create all the subdirs.
3134     (while (and dirs (not err))
3135       (condition-case ()
3136           (make-directory (car dirs))
3137         (error (setq err t)))
3138       (setq dirs (cdr dirs)))
3139     ;; We return whether we were successful or not.
3140     (not dirs)))
3141
3142 (defun gnus-capitalize-newsgroup (newsgroup)
3143   "Capitalize NEWSGROUP name."
3144   (and (not (zerop (length newsgroup)))
3145        (concat (char-to-string (upcase (aref newsgroup 0)))
3146                (substring newsgroup 1))))
3147
3148 ;; Various... things.
3149
3150 (defun gnus-simplify-subject (subject &optional re-only)
3151   "Remove `Re:' and words in parentheses.
3152 If RE-ONLY is non-nil, strip leading `Re:'s only."
3153   (let ((case-fold-search t))           ;Ignore case.
3154     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3155     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3156       (setq subject (substring subject (match-end 0))))
3157     ;; Remove uninteresting prefixes.
3158     (if (and (not re-only)
3159              gnus-simplify-ignored-prefixes
3160              (string-match gnus-simplify-ignored-prefixes subject))
3161         (setq subject (substring subject (match-end 0))))
3162     ;; Remove words in parentheses from end.
3163     (unless re-only
3164       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3165         (setq subject (substring subject 0 (match-beginning 0)))))
3166     ;; Return subject string.
3167     subject))
3168
3169 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3170 ;; all whitespace.
3171 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3172 (defun gnus-simplify-buffer-fuzzy ()
3173   (let ((case-fold-search t))
3174     (goto-char (point-min))
3175     (while (search-forward "\t" nil t)
3176       (replace-match " " t t))
3177     (goto-char (point-min))
3178     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3179     (goto-char (match-beginning 0))
3180     (while (or
3181             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3182             (looking-at "^[[].*: .*[]]$"))
3183       (goto-char (point-min))
3184       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3185                                 nil t)
3186         (replace-match "" t t))
3187       (goto-char (point-min))
3188       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3189         (goto-char (match-end 0))
3190         (delete-char -1)
3191         (delete-region
3192          (progn (goto-char (match-beginning 0)))
3193          (re-search-forward ":"))))
3194     (goto-char (point-min))
3195     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3196       (replace-match "" t t))
3197     (goto-char (point-min))
3198     (while (re-search-forward "  +" nil t)
3199       (replace-match " " t t))
3200     (goto-char (point-min))
3201     (while (re-search-forward " $" nil t)
3202       (replace-match "" t t))
3203     (goto-char (point-min))
3204     (while (re-search-forward "^ +" nil t)
3205       (replace-match "" t t))
3206     (goto-char (point-min))
3207     (when gnus-simplify-subject-fuzzy-regexp
3208       (if (listp gnus-simplify-subject-fuzzy-regexp)
3209           (let ((list gnus-simplify-subject-fuzzy-regexp))
3210             (while list
3211               (goto-char (point-min))
3212               (while (re-search-forward (car list) nil t)
3213                 (replace-match "" t t))
3214               (setq list (cdr list))))
3215         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3216           (replace-match "" t t))))))
3217
3218 (defun gnus-simplify-subject-fuzzy (subject)
3219   "Siplify a subject string fuzzily."
3220   (save-excursion
3221     (gnus-set-work-buffer)
3222     (let ((case-fold-search t))
3223       (insert subject)
3224       (inline (gnus-simplify-buffer-fuzzy))
3225       (buffer-string))))
3226
3227 ;; Add the current buffer to the list of buffers to be killed on exit.
3228 (defun gnus-add-current-to-buffer-list ()
3229   (or (memq (current-buffer) gnus-buffer-list)
3230       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3231
3232 (defun gnus-string> (s1 s2)
3233   (not (or (string< s1 s2)
3234            (string= s1 s2))))
3235
3236 (defun gnus-read-active-file-p ()
3237   "Say whether the active file has been read from `gnus-select-method'."
3238   (memq gnus-select-method gnus-have-read-active-file))
3239
3240 ;;; General various misc type functions.
3241
3242 (defun gnus-clear-system ()
3243   "Clear all variables and buffers."
3244   ;; Clear Gnus variables.
3245   (let ((variables gnus-variable-list))
3246     (while variables
3247       (set (car variables) nil)
3248       (setq variables (cdr variables))))
3249   ;; Clear other internal variables.
3250   (setq gnus-list-of-killed-groups nil
3251         gnus-have-read-active-file nil
3252         gnus-newsrc-alist nil
3253         gnus-newsrc-hashtb nil
3254         gnus-killed-list nil
3255         gnus-zombie-list nil
3256         gnus-killed-hashtb nil
3257         gnus-active-hashtb nil
3258         gnus-moderated-list nil
3259         gnus-description-hashtb nil
3260         gnus-current-headers nil
3261         gnus-thread-indent-array nil
3262         gnus-newsgroup-headers nil
3263         gnus-newsgroup-name nil
3264         gnus-server-alist nil
3265         gnus-group-list-mode nil
3266         gnus-opened-servers nil
3267         gnus-group-mark-positions nil
3268         gnus-newsgroup-data nil
3269         gnus-newsgroup-unreads nil
3270         nnoo-state-alist nil
3271         gnus-current-select-method nil)
3272   (gnus-shutdown 'gnus)
3273   ;; Kill the startup file.
3274   (and gnus-current-startup-file
3275        (get-file-buffer gnus-current-startup-file)
3276        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3277   ;; Clear the dribble buffer.
3278   (gnus-dribble-clear)
3279   ;; Kill global KILL file buffer.
3280   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3281     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3282   (gnus-kill-buffer nntp-server-buffer)
3283   ;; Kill Gnus buffers.
3284   (while gnus-buffer-list
3285     (gnus-kill-buffer (pop gnus-buffer-list)))
3286   ;; Remove Gnus frames.
3287   (gnus-kill-gnus-frames))
3288
3289 (defun gnus-kill-gnus-frames ()
3290   "Kill all frames Gnus has created."
3291   (while gnus-created-frames
3292     (when (frame-live-p (car gnus-created-frames))
3293       ;; We slap a condition-case around this `delete-frame' to ensure 
3294       ;; against errors if we try do delete the single frame that's left.
3295       (condition-case ()
3296           (delete-frame (car gnus-created-frames))
3297         (error nil)))
3298     (pop gnus-created-frames)))
3299
3300 (defun gnus-windows-old-to-new (setting)
3301   ;; First we take care of the really, really old Gnus 3 actions.
3302   (when (symbolp setting)
3303     (setq setting
3304           ;; Take care of ooold GNUS 3.x values.
3305           (cond ((eq setting 'SelectArticle) 'article)
3306                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3307                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3308                 (t setting))))
3309   (if (or (listp setting)
3310           (not (and gnus-window-configuration
3311                     (memq setting '(group summary article)))))
3312       setting
3313     (let* ((setting (if (eq setting 'group)
3314                         (if (assq 'newsgroup gnus-window-configuration)
3315                             'newsgroup
3316                           'newsgroups) setting))
3317            (elem (cadr (assq setting gnus-window-configuration)))
3318            (total (apply '+ elem))
3319            (types '(group summary article))
3320            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3321            (i 0)
3322            perc
3323            out)
3324       (while (< i 3)
3325         (or (not (numberp (nth i elem)))
3326             (zerop (nth i elem))
3327             (progn
3328               (setq perc (if (= i 2)
3329                              1.0
3330                            (/ (float (nth 0 elem)) total)))
3331               (setq out (cons (if (eq pbuf (nth i types))
3332                                   (list (nth i types) perc 'point)
3333                                 (list (nth i types) perc))
3334                               out))))
3335         (setq i (1+ i)))
3336       `(vertical 1.0 ,@(nreverse out)))))
3337
3338 ;;;###autoload
3339 (defun gnus-add-configuration (conf)
3340   "Add the window configuration CONF to `gnus-buffer-configuration'."
3341   (setq gnus-buffer-configuration
3342         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3343                          gnus-buffer-configuration))))
3344
3345 (defvar gnus-frame-list nil)
3346
3347 (defun gnus-configure-frame (split &optional window)
3348   "Split WINDOW according to SPLIT."
3349   (unless window
3350     (setq window (get-buffer-window (current-buffer))))
3351   (select-window window)
3352   ;; This might be an old-stylee buffer config.
3353   (when (vectorp split)
3354     (setq split (append split nil)))
3355   (when (or (consp (car split))
3356             (vectorp (car split)))
3357     (push 1.0 split)
3358     (push 'vertical split))
3359   ;; The SPLIT might be something that is to be evaled to
3360   ;; return a new SPLIT.
3361   (while (and (not (assq (car split) gnus-window-to-buffer))
3362               (gnus-functionp (car split)))
3363     (setq split (eval split)))
3364   (let* ((type (car split))
3365          (subs (cddr split))
3366          (len (if (eq type 'horizontal) (window-width) (window-height)))
3367          (total 0)
3368          (window-min-width (or gnus-window-min-width window-min-width))
3369          (window-min-height (or gnus-window-min-height window-min-height))
3370          s result new-win rest comp-subs size sub)
3371     (cond
3372      ;; Nothing to do here.
3373      ((null split))
3374      ;; Don't switch buffers.
3375      ((null type)
3376       (and (memq 'point split) window))
3377      ;; This is a buffer to be selected.
3378      ((not (memq type '(frame horizontal vertical)))
3379       (let ((buffer (cond ((stringp type) type)
3380                           (t (cdr (assq type gnus-window-to-buffer)))))
3381             buf)
3382         (unless buffer
3383           (error "Illegal buffer type: %s" type))
3384         (unless (setq buf (get-buffer (if (symbolp buffer)
3385                                           (symbol-value buffer) buffer)))
3386           (setq buf (get-buffer-create (if (symbolp buffer)
3387                                            (symbol-value buffer) buffer))))
3388         (switch-to-buffer buf)
3389         ;; We return the window if it has the `point' spec.
3390         (and (memq 'point split) window)))
3391      ;; This is a frame split.
3392      ((eq type 'frame)
3393       (unless gnus-frame-list
3394         (setq gnus-frame-list (list (window-frame
3395                                      (get-buffer-window (current-buffer))))))
3396       (let ((i 0)
3397             params frame fresult)
3398         (while (< i (length subs))
3399           ;; Frame parameter is gotten from the sub-split.
3400           (setq params (cadr (elt subs i)))
3401           ;; It should be a list.
3402           (unless (listp params)
3403             (setq params nil))
3404           ;; Create a new frame?
3405           (unless (setq frame (elt gnus-frame-list i))
3406             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3407             (push frame gnus-created-frames))
3408           ;; Is the old frame still alive?
3409           (unless (frame-live-p frame)
3410             (setcar (nthcdr i gnus-frame-list)
3411                     (setq frame (make-frame params))))
3412           ;; Select the frame in question and do more splits there.
3413           (select-frame frame)
3414           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3415           (incf i))
3416         ;; Select the frame that has the selected buffer.
3417         (when fresult
3418           (select-frame (window-frame fresult)))))
3419      ;; This is a normal split.
3420      (t
3421       (when (> (length subs) 0)
3422         ;; First we have to compute the sizes of all new windows.
3423         (while subs
3424           (setq sub (append (pop subs) nil))
3425           (while (and (not (assq (car sub) gnus-window-to-buffer))
3426                       (gnus-functionp (car sub)))
3427             (setq sub (eval sub)))
3428           (when sub
3429             (push sub comp-subs)
3430             (setq size (cadar comp-subs))
3431             (cond ((equal size 1.0)
3432                    (setq rest (car comp-subs))
3433                    (setq s 0))
3434                   ((floatp size)
3435                    (setq s (floor (* size len))))
3436                   ((integerp size)
3437                    (setq s size))
3438                   (t
3439                    (error "Illegal size: %s" size)))
3440             ;; Try to make sure that we are inside the safe limits.
3441             (cond ((zerop s))
3442                   ((eq type 'horizontal)
3443                    (setq s (max s window-min-width)))
3444                   ((eq type 'vertical)
3445                    (setq s (max s window-min-height))))
3446             (setcar (cdar comp-subs) s)
3447             (incf total s)))
3448         ;; Take care of the "1.0" spec.
3449         (if rest
3450             (setcar (cdr rest) (- len total))
3451           (error "No 1.0 specs in %s" split))
3452         ;; The we do the actual splitting in a nice recursive
3453         ;; fashion.
3454         (setq comp-subs (nreverse comp-subs))
3455         (while comp-subs
3456           (if (null (cdr comp-subs))
3457               (setq new-win window)
3458             (setq new-win
3459                   (split-window window (cadar comp-subs)
3460                                 (eq type 'horizontal))))
3461           (setq result (or (gnus-configure-frame
3462                             (car comp-subs) window) result))
3463           (select-window new-win)
3464           (setq window new-win)
3465           (setq comp-subs (cdr comp-subs))))
3466       ;; Return the proper window, if any.
3467       (when result
3468         (select-window result))))))
3469
3470 (defvar gnus-frame-split-p nil)
3471
3472 (defun gnus-configure-windows (setting &optional force)
3473   (setq setting (gnus-windows-old-to-new setting))
3474   (let ((split (if (symbolp setting)
3475                    (cadr (assq setting gnus-buffer-configuration))
3476                  setting))
3477         all-visible)
3478
3479     (setq gnus-frame-split-p nil)
3480
3481     (unless split
3482       (error "No such setting: %s" setting))
3483
3484     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3485              (not force))
3486         ;; All the windows mentioned are already visible, so we just
3487         ;; put point in the assigned buffer, and do not touch the
3488         ;; winconf.
3489         (select-window all-visible)
3490
3491       ;; Either remove all windows or just remove all Gnus windows.
3492       (let ((frame (selected-frame)))
3493         (unwind-protect
3494             (if gnus-use-full-window
3495                 ;; We want to remove all other windows.
3496                 (if (not gnus-frame-split-p)
3497                     ;; This is not a `frame' split, so we ignore the
3498                     ;; other frames.  
3499                     (delete-other-windows)
3500                   ;; This is a `frame' split, so we delete all windows
3501                   ;; on all frames.
3502                   (mapcar 
3503                    (lambda (frame)
3504                      (unless (eq (cdr (assq 'minibuffer
3505                                             (frame-parameters frame)))
3506                                  'only)
3507                        (select-frame frame)
3508                        (delete-other-windows)))
3509                    (frame-list)))
3510               ;; Just remove some windows.
3511               (gnus-remove-some-windows)
3512               (switch-to-buffer nntp-server-buffer))
3513           (select-frame frame)))
3514
3515       (switch-to-buffer nntp-server-buffer)
3516       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3517
3518 (defun gnus-all-windows-visible-p (split)
3519   "Say whether all buffers in SPLIT are currently visible.
3520 In particular, the value returned will be the window that
3521 should have point."
3522   (let ((stack (list split))
3523         (all-visible t)
3524         type buffer win buf)
3525     (while (and (setq split (pop stack))
3526                 all-visible)
3527       ;; Be backwards compatible.
3528       (when (vectorp split)
3529         (setq split (append split nil)))
3530       (when (or (consp (car split))
3531                 (vectorp (car split)))
3532         (push 1.0 split)
3533         (push 'vertical split))
3534       ;; The SPLIT might be something that is to be evaled to
3535       ;; return a new SPLIT.
3536       (while (and (not (assq (car split) gnus-window-to-buffer))
3537                   (gnus-functionp (car split)))
3538         (setq split (eval split)))
3539
3540       (setq type (elt split 0))
3541       (cond
3542        ;; Nothing here.
3543        ((null split) t)
3544        ;; A buffer.
3545        ((not (memq type '(horizontal vertical frame)))
3546         (setq buffer (cond ((stringp type) type)
3547                            (t (cdr (assq type gnus-window-to-buffer)))))
3548         (unless buffer
3549           (error "Illegal buffer type: %s" type))
3550         (when (setq buf (get-buffer (if (symbolp buffer)
3551                                         (symbol-value buffer)
3552                                       buffer)))
3553           (setq win (get-buffer-window buf t)))
3554         (if win
3555             (when (memq 'point split)
3556                 (setq all-visible win))
3557           (setq all-visible nil)))
3558        (t
3559         (when (eq type 'frame)
3560           (setq gnus-frame-split-p t))
3561         (setq stack (append (cddr split) stack)))))
3562     (unless (eq all-visible t)
3563       all-visible)))
3564
3565 (defun gnus-window-top-edge (&optional window)
3566   (nth 1 (window-edges window)))
3567
3568 (defun gnus-remove-some-windows ()
3569   (let ((buffers gnus-window-to-buffer)
3570         buf bufs lowest-buf lowest)
3571     (save-excursion
3572       ;; Remove windows on all known Gnus buffers.
3573       (while buffers
3574         (setq buf (cdar buffers))
3575         (if (symbolp buf)
3576             (setq buf (and (boundp buf) (symbol-value buf))))
3577         (and buf
3578              (get-buffer-window buf)
3579              (progn
3580                (setq bufs (cons buf bufs))
3581                (pop-to-buffer buf)
3582                (if (or (not lowest)
3583                        (< (gnus-window-top-edge) lowest))
3584                    (progn
3585                      (setq lowest (gnus-window-top-edge))
3586                      (setq lowest-buf buf)))))
3587         (setq buffers (cdr buffers)))
3588       ;; Remove windows on *all* summary buffers.
3589       (walk-windows
3590        (lambda (win)
3591          (let ((buf (window-buffer win)))
3592            (if (string-match    "^\\*Summary" (buffer-name buf))
3593                (progn
3594                  (setq bufs (cons buf bufs))
3595                  (pop-to-buffer buf)
3596                  (if (or (not lowest)
3597                          (< (gnus-window-top-edge) lowest))
3598                      (progn
3599                        (setq lowest-buf buf)
3600                        (setq lowest (gnus-window-top-edge)))))))))
3601       (and lowest-buf
3602            (progn
3603              (pop-to-buffer lowest-buf)
3604              (switch-to-buffer nntp-server-buffer)))
3605       (while bufs
3606         (and (not (eq (car bufs) lowest-buf))
3607              (delete-windows-on (car bufs)))
3608         (setq bufs (cdr bufs))))))
3609
3610 (defun gnus-version (&optional arg)
3611   "Version number of this version of Gnus.
3612 If ARG, insert string at point."
3613   (interactive "P")
3614   (let ((methods gnus-valid-select-methods)
3615         (mess gnus-version)
3616         meth)
3617     ;; Go through all the legal select methods and add their version
3618     ;; numbers to the total version string.  Only the backends that are
3619     ;; currently in use will have their message numbers taken into
3620     ;; consideration.
3621     (while methods
3622       (setq meth (intern (concat (caar methods) "-version")))
3623       (and (boundp meth)
3624            (stringp (symbol-value meth))
3625            (setq mess (concat mess "; " (symbol-value meth))))
3626       (setq methods (cdr methods)))
3627     (if arg
3628         (insert (message mess))
3629       (message mess))))
3630
3631 (defun gnus-info-find-node ()
3632   "Find Info documentation of Gnus."
3633   (interactive)
3634   ;; Enlarge info window if needed.
3635   (let ((mode major-mode)
3636         gnus-info-buffer)
3637     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3638     (setq gnus-info-buffer (current-buffer))
3639     (gnus-configure-windows 'info)))
3640
3641 (defun gnus-days-between (date1 date2)
3642   ;; Return the number of days between date1 and date2.
3643   (- (gnus-day-number date1) (gnus-day-number date2)))
3644
3645 (defun gnus-day-number (date)
3646   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3647                      (timezone-parse-date date))))
3648     (timezone-absolute-from-gregorian
3649      (nth 1 dat) (nth 2 dat) (car dat))))
3650
3651 (defun gnus-encode-date (date)
3652   "Convert DATE to internal time."
3653   (let* ((parse (timezone-parse-date date))
3654          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3655          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3656     (encode-time (caddr time) (cadr time) (car time)
3657                  (caddr date) (cadr date) (car date) (nth 4 date))))
3658
3659 (defun gnus-time-minus (t1 t2)
3660   "Subtract two internal times."
3661   (let ((borrow (< (cadr t1) (cadr t2))))
3662     (list (- (car t1) (car t2) (if borrow 1 0))
3663           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3664
3665 (defun gnus-file-newer-than (file date)
3666   (let ((fdate (nth 5 (file-attributes file))))
3667     (or (> (car fdate) (car date))
3668         (and (= (car fdate) (car date))
3669              (> (nth 1 fdate) (nth 1 date))))))
3670
3671 (defmacro gnus-local-set-keys (&rest plist)
3672   "Set the keys in PLIST in the current keymap."
3673   `(gnus-define-keys-1 (current-local-map) ',plist))
3674
3675 (defmacro gnus-define-keys (keymap &rest plist)
3676   "Define all keys in PLIST in KEYMAP."
3677   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3678
3679 (put 'gnus-define-keys 'lisp-indent-function 1)
3680 (put 'gnus-define-keys 'lisp-indent-hook 1)
3681 (put 'gnus-define-keymap 'lisp-indent-function 1)
3682 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3683
3684 (defmacro gnus-define-keymap (keymap &rest plist)
3685   "Define all keys in PLIST in KEYMAP."
3686   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3687
3688 (defun gnus-define-keys-1 (keymap plist)
3689   (when (null keymap)
3690     (error "Can't set keys in a null keymap"))
3691   (cond ((symbolp keymap)
3692          (setq keymap (symbol-value keymap)))
3693         ((keymapp keymap))
3694         ((listp keymap)
3695          (set (car keymap) nil)
3696          (define-prefix-command (car keymap))
3697          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3698          (setq keymap (symbol-value (car keymap)))))
3699   (let (key)
3700     (while plist
3701       (when (symbolp (setq key (pop plist)))
3702         (setq key (symbol-value key)))
3703       (define-key keymap key (pop plist)))))
3704
3705 (defun gnus-group-read-only-p (&optional group)
3706   "Check whether GROUP supports editing or not.
3707 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3708 that that variable is buffer-local to the summary buffers."
3709   (let ((group (or group gnus-newsgroup-name)))
3710     (not (gnus-check-backend-function 'request-replace-article group))))
3711
3712 (defun gnus-group-total-expirable-p (group)
3713   "Check whether GROUP is total-expirable or not."
3714   (let ((params (gnus-info-params (gnus-get-info group))))
3715     (or (memq 'total-expire params)
3716         (cdr (assq 'total-expire params)) ; (total-expire . t)
3717         (and gnus-total-expirable-newsgroups ; Check var.
3718              (string-match gnus-total-expirable-newsgroups group)))))
3719
3720 (defun gnus-group-auto-expirable-p (group)
3721   "Check whether GROUP is total-expirable or not."
3722   (let ((params (gnus-info-params (gnus-get-info group))))
3723     (or (memq 'auto-expire params)
3724         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3725         (and gnus-auto-expirable-newsgroups ; Check var.
3726              (string-match gnus-auto-expirable-newsgroups group)))))
3727
3728 (defun gnus-virtual-group-p (group)
3729   "Say whether GROUP is virtual or not."
3730   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3731                         gnus-valid-select-methods)))
3732
3733 (defun gnus-news-group-p (group &optional article)
3734   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3735   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3736       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3737            (eq (gnus-request-type group article) 'news))))
3738
3739 (defsubst gnus-simplify-subject-fully (subject)
3740   "Simplify a subject string according to the user's wishes."
3741   (cond
3742    ((null gnus-summary-gather-subject-limit)
3743     (gnus-simplify-subject-re subject))
3744    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3745     (gnus-simplify-subject-fuzzy subject))
3746    ((numberp gnus-summary-gather-subject-limit)
3747     (gnus-limit-string (gnus-simplify-subject-re subject)
3748                        gnus-summary-gather-subject-limit))
3749    (t
3750     subject)))
3751
3752 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3753   "Check whether two subjects are equal.  If optional argument
3754 simple-first is t, first argument is already simplified."
3755   (cond
3756    ((null simple-first)
3757     (equal (gnus-simplify-subject-fully s1)
3758            (gnus-simplify-subject-fully s2)))
3759    (t
3760     (equal s1
3761            (gnus-simplify-subject-fully s2)))))
3762
3763 ;; Returns a list of writable groups.
3764 (defun gnus-writable-groups ()
3765   (let ((alist gnus-newsrc-alist)
3766         groups group)
3767     (while (setq group (car (pop alist)))
3768       (unless (gnus-group-read-only-p group)
3769         (push group groups)))
3770     (nreverse groups)))
3771
3772 (defun gnus-completing-read (default prompt &rest args)
3773   ;; Like `completing-read', except that DEFAULT is the default argument.
3774   (let* ((prompt (if default 
3775                      (concat prompt " (default " default ") ")
3776                    (concat prompt " ")))
3777          (answer (apply 'completing-read prompt args)))
3778     (if (or (null answer) (zerop (length answer)))
3779         default
3780       answer)))
3781
3782 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3783 ;; the echo area.
3784 (defun gnus-y-or-n-p (prompt)
3785   (prog1
3786       (y-or-n-p prompt)
3787     (message "")))
3788
3789 (defun gnus-yes-or-no-p (prompt)
3790   (prog1
3791       (yes-or-no-p prompt)
3792     (message "")))
3793
3794 ;; Check whether to use long file names.
3795 (defun gnus-use-long-file-name (symbol)
3796   ;; The variable has to be set...
3797   (and gnus-use-long-file-name
3798        ;; If it isn't a list, then we return t.
3799        (or (not (listp gnus-use-long-file-name))
3800            ;; If it is a list, and the list contains `symbol', we
3801            ;; return nil.
3802            (not (memq symbol gnus-use-long-file-name)))))
3803
3804 ;; I suspect there's a better way, but I haven't taken the time to do
3805 ;; it yet. -erik selberg@cs.washington.edu
3806 (defun gnus-dd-mmm (messy-date)
3807   "Return a string like DD-MMM from a big messy string"
3808   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3809                    (error nil))))
3810     (if (not datevec)
3811         "??-???"
3812       (format "%2s-%s"
3813               (condition-case ()
3814                   ;; Make sure leading zeroes are stripped.
3815                   (number-to-string (string-to-number (aref datevec 2)))
3816                 (error "??"))
3817               (capitalize
3818                (or (car
3819                     (nth (1- (string-to-number (aref datevec 1)))
3820                          timezone-months-assoc))
3821                    "???"))))))
3822
3823 (defun gnus-mode-string-quote (string)
3824   "Quote all \"%\" in STRING."
3825   (save-excursion
3826     (gnus-set-work-buffer)
3827     (insert string)
3828     (goto-char (point-min))
3829     (while (search-forward "%" nil t)
3830       (insert "%"))
3831     (buffer-string)))
3832
3833 ;; Make a hash table (default and minimum size is 255).
3834 ;; Optional argument HASHSIZE specifies the table size.
3835 (defun gnus-make-hashtable (&optional hashsize)
3836   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3837
3838 ;; Make a number that is suitable for hashing; bigger than MIN and one
3839 ;; less than 2^x.
3840 (defun gnus-create-hash-size (min)
3841   (let ((i 1))
3842     (while (< i min)
3843       (setq i (* 2 i)))
3844     (1- i)))
3845
3846 ;; Show message if message has a lower level than `gnus-verbose'.
3847 ;; Guideline for numbers:
3848 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3849 ;; for things that take a long time, 7 - not very important messages
3850 ;; on stuff, 9 - messages inside loops.
3851 (defun gnus-message (level &rest args)
3852   (if (<= level gnus-verbose)
3853       (apply 'message args)
3854     ;; We have to do this format thingy here even if the result isn't
3855     ;; shown - the return value has to be the same as the return value
3856     ;; from `message'.
3857     (apply 'format args)))
3858
3859 (defun gnus-error (level &rest args)
3860   "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
3861   (when (<= (floor level) gnus-verbose)
3862     (apply 'message args)
3863     (ding)
3864     (let (duration)
3865       (when (and (floatp level)
3866                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3867         (sit-for duration))))
3868   nil)
3869
3870 ;; Generate a unique new group name.
3871 (defun gnus-generate-new-group-name (leaf)
3872   (let ((name leaf)
3873         (num 0))
3874     (while (gnus-gethash name gnus-newsrc-hashtb)
3875       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3876     name))
3877
3878 (defsubst gnus-hide-text (b e props)
3879   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3880   (gnus-add-text-properties b e props)
3881   (when (memq 'intangible props)
3882     (gnus-put-text-property (max (1- b) (point-min))
3883                        b 'intangible (cddr (memq 'intangible props)))))
3884
3885 (defsubst gnus-unhide-text (b e)
3886   "Remove hidden text properties from region between B and E."
3887   (remove-text-properties b e gnus-hidden-properties)
3888   (when (memq 'intangible gnus-hidden-properties)
3889     (gnus-put-text-property (max (1- b) (point-min))
3890                             b 'intangible nil)))
3891
3892 (defun gnus-hide-text-type (b e type)
3893   "Hide text of TYPE between B and E."
3894   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3895
3896 (defun gnus-parent-headers (headers &optional generation)
3897   "Return the headers of the GENERATIONeth parent of HEADERS."
3898   (unless generation 
3899     (setq generation 1))
3900   (let (references parent)
3901     (while (and headers (not (zerop generation)))
3902       (setq references (mail-header-references headers))
3903       (when (and references
3904                  (setq parent (gnus-parent-id references))
3905                  (setq headers (car (gnus-id-to-thread parent))))
3906         (decf generation)))
3907     headers))
3908
3909 (defun gnus-parent-id (references)
3910   "Return the last Message-ID in REFERENCES."
3911   (when (and references
3912              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3913     (substring references (match-beginning 1) (match-end 1))))
3914
3915 (defun gnus-split-references (references)
3916   "Return a list of Message-IDs in REFERENCES."
3917   (let ((beg 0)
3918         ids)
3919     (while (string-match "<[^>]+>" references beg)
3920       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3921             ids))
3922     (nreverse ids)))
3923
3924 (defun gnus-buffer-live-p (buffer)
3925   "Say whether BUFFER is alive or not."
3926   (and buffer
3927        (get-buffer buffer)
3928        (buffer-name (get-buffer buffer))))
3929
3930 (defun gnus-ephemeral-group-p (group)
3931   "Say whether GROUP is ephemeral or not."
3932   (gnus-group-get-parameter group 'quit-config))
3933
3934 (defun gnus-group-quit-config (group)
3935   "Return the quit-config of GROUP."
3936   (gnus-group-get-parameter group 'quit-config))
3937
3938 (defun gnus-simplify-mode-line ()
3939   "Make mode lines a bit simpler."
3940   (setq mode-line-modified "-- ")
3941   (when (listp mode-line-format)
3942     (make-local-variable 'mode-line-format)
3943     (setq mode-line-format (copy-sequence mode-line-format))
3944     (when (equal (nth 3 mode-line-format) "   ")
3945       (setcar (nthcdr 3 mode-line-format) " "))))
3946
3947 ;;; List and range functions
3948
3949 (defun gnus-last-element (list)
3950   "Return last element of LIST."
3951   (while (cdr list)
3952     (setq list (cdr list)))
3953   (car list))
3954
3955 (defun gnus-copy-sequence (list)
3956   "Do a complete, total copy of a list."
3957   (if (and (consp list) (not (consp (cdr list))))
3958       (cons (car list) (cdr list))
3959     (mapcar (lambda (elem) (if (consp elem)
3960                                (if (consp (cdr elem))
3961                                    (gnus-copy-sequence elem)
3962                                  (cons (car elem) (cdr elem)))
3963                              elem))
3964             list)))
3965
3966 (defun gnus-set-difference (list1 list2)
3967   "Return a list of elements of LIST1 that do not appear in LIST2."
3968   (let ((list1 (copy-sequence list1)))
3969     (while list2
3970       (setq list1 (delq (car list2) list1))
3971       (setq list2 (cdr list2)))
3972     list1))
3973
3974 (defun gnus-sorted-complement (list1 list2)
3975   "Return a list of elements of LIST1 that do not appear in LIST2.
3976 Both lists have to be sorted over <."
3977   (let (out)
3978     (if (or (null list1) (null list2))
3979         (or list1 list2)
3980       (while (and list1 list2)
3981         (cond ((= (car list1) (car list2))
3982                (setq list1 (cdr list1)
3983                      list2 (cdr list2)))
3984               ((< (car list1) (car list2))
3985                (setq out (cons (car list1) out))
3986                (setq list1 (cdr list1)))
3987               (t
3988                (setq out (cons (car list2) out))
3989                (setq list2 (cdr list2)))))
3990       (nconc (nreverse out) (or list1 list2)))))
3991
3992 (defun gnus-intersection (list1 list2)
3993   (let ((result nil))
3994     (while list2
3995       (if (memq (car list2) list1)
3996           (setq result (cons (car list2) result)))
3997       (setq list2 (cdr list2)))
3998     result))
3999
4000 (defun gnus-sorted-intersection (list1 list2)
4001   ;; LIST1 and LIST2 have to be sorted over <.
4002   (let (out)
4003     (while (and list1 list2)
4004       (cond ((= (car list1) (car list2))
4005              (setq out (cons (car list1) out)
4006                    list1 (cdr list1)
4007                    list2 (cdr list2)))
4008             ((< (car list1) (car list2))
4009              (setq list1 (cdr list1)))
4010             (t
4011              (setq list2 (cdr list2)))))
4012     (nreverse out)))
4013
4014 (defun gnus-set-sorted-intersection (list1 list2)
4015   ;; LIST1 and LIST2 have to be sorted over <.
4016   ;; This function modifies LIST1.
4017   (let* ((top (cons nil list1))
4018          (prev top))
4019     (while (and list1 list2)
4020       (cond ((= (car list1) (car list2))
4021              (setq prev list1
4022                    list1 (cdr list1)
4023                    list2 (cdr list2)))
4024             ((< (car list1) (car list2))
4025              (setcdr prev (cdr list1))
4026              (setq list1 (cdr list1)))
4027             (t
4028              (setq list2 (cdr list2)))))
4029     (setcdr prev nil)
4030     (cdr top)))
4031
4032 (defun gnus-compress-sequence (numbers &optional always-list)
4033   "Convert list of numbers to a list of ranges or a single range.
4034 If ALWAYS-LIST is non-nil, this function will always release a list of
4035 ranges."
4036   (let* ((first (car numbers))
4037          (last (car numbers))
4038          result)
4039     (if (null numbers)
4040         nil
4041       (if (not (listp (cdr numbers)))
4042           numbers
4043         (while numbers
4044           (cond ((= last (car numbers)) nil) ;Omit duplicated number
4045                 ((= (1+ last) (car numbers)) ;Still in sequence
4046                  (setq last (car numbers)))
4047                 (t                      ;End of one sequence
4048                  (setq result
4049                        (cons (if (= first last) first
4050                                (cons first last)) result))
4051                  (setq first (car numbers))
4052                  (setq last  (car numbers))))
4053           (setq numbers (cdr numbers)))
4054         (if (and (not always-list) (null result))
4055             (if (= first last) (list first) (cons first last))
4056           (nreverse (cons (if (= first last) first (cons first last))
4057                           result)))))))
4058
4059 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
4060 (defun gnus-uncompress-range (ranges)
4061   "Expand a list of ranges into a list of numbers.
4062 RANGES is either a single range on the form `(num . num)' or a list of
4063 these ranges."
4064   (let (first last result)
4065     (cond
4066      ((null ranges)
4067       nil)
4068      ((not (listp (cdr ranges)))
4069       (setq first (car ranges))
4070       (setq last (cdr ranges))
4071       (while (<= first last)
4072         (setq result (cons first result))
4073         (setq first (1+ first)))
4074       (nreverse result))
4075      (t
4076       (while ranges
4077         (if (atom (car ranges))
4078             (if (numberp (car ranges))
4079                 (setq result (cons (car ranges) result)))
4080           (setq first (caar ranges))
4081           (setq last  (cdar ranges))
4082           (while (<= first last)
4083             (setq result (cons first result))
4084             (setq first (1+ first))))
4085         (setq ranges (cdr ranges)))
4086       (nreverse result)))))
4087
4088 (defun gnus-add-to-range (ranges list)
4089   "Return a list of ranges that has all articles from both RANGES and LIST.
4090 Note: LIST has to be sorted over `<'."
4091   (if (not ranges)
4092       (gnus-compress-sequence list t)
4093     (setq list (copy-sequence list))
4094     (or (listp (cdr ranges))
4095         (setq ranges (list ranges)))
4096     (let ((out ranges)
4097           ilist lowest highest temp)
4098       (while (and ranges list)
4099         (setq ilist list)
4100         (setq lowest (or (and (atom (car ranges)) (car ranges))
4101                          (caar ranges)))
4102         (while (and list (cdr list) (< (cadr list) lowest))
4103           (setq list (cdr list)))
4104         (if (< (car ilist) lowest)
4105             (progn
4106               (setq temp list)
4107               (setq list (cdr list))
4108               (setcdr temp nil)
4109               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4110         (setq highest (or (and (atom (car ranges)) (car ranges))
4111                           (cdar ranges)))
4112         (while (and list (<= (car list) highest))
4113           (setq list (cdr list)))
4114         (setq ranges (cdr ranges)))
4115       (if list
4116           (setq out (nconc (gnus-compress-sequence list t) out)))
4117       (setq out (sort out (lambda (r1 r2)
4118                             (< (or (and (atom r1) r1) (car r1))
4119                                (or (and (atom r2) r2) (car r2))))))
4120       (setq ranges out)
4121       (while ranges
4122         (if (atom (car ranges))
4123             (if (cdr ranges)
4124                 (if (atom (cadr ranges))
4125                     (if (= (1+ (car ranges)) (cadr ranges))
4126                         (progn
4127                           (setcar ranges (cons (car ranges)
4128                                                (cadr ranges)))
4129                           (setcdr ranges (cddr ranges))))
4130                   (if (= (1+ (car ranges)) (caadr ranges))
4131                       (progn
4132                         (setcar (cadr ranges) (car ranges))
4133                         (setcar ranges (cadr ranges))
4134                         (setcdr ranges (cddr ranges))))))
4135           (if (cdr ranges)
4136               (if (atom (cadr ranges))
4137                   (if (= (1+ (cdar ranges)) (cadr ranges))
4138                       (progn
4139                         (setcdr (car ranges) (cadr ranges))
4140                         (setcdr ranges (cddr ranges))))
4141                 (if (= (1+ (cdar ranges)) (caadr ranges))
4142                     (progn
4143                       (setcdr (car ranges) (cdadr ranges))
4144                       (setcdr ranges (cddr ranges)))))))
4145         (setq ranges (cdr ranges)))
4146       out)))
4147
4148 (defun gnus-remove-from-range (ranges list)
4149   "Return a list of ranges that has all articles from LIST removed from RANGES.
4150 Note: LIST has to be sorted over `<'."
4151   ;; !!! This function shouldn't look like this, but I've got a headache.
4152   (gnus-compress-sequence
4153    (gnus-sorted-complement
4154     (gnus-uncompress-range ranges) list)))
4155
4156 (defun gnus-member-of-range (number ranges)
4157   (if (not (listp (cdr ranges)))
4158       (and (>= number (car ranges))
4159            (<= number (cdr ranges)))
4160     (let ((not-stop t))
4161       (while (and ranges
4162                   (if (numberp (car ranges))
4163                       (>= number (car ranges))
4164                     (>= number (caar ranges)))
4165                   not-stop)
4166         (if (if (numberp (car ranges))
4167                 (= number (car ranges))
4168               (and (>= number (caar ranges))
4169                    (<= number (cdar ranges))))
4170             (setq not-stop nil))
4171         (setq ranges (cdr ranges)))
4172       (not not-stop))))
4173
4174 (defun gnus-range-length (range)
4175   "Return the length RANGE would have if uncompressed."
4176   (length (gnus-uncompress-range range)))
4177
4178 (defun gnus-sublist-p (list sublist)
4179   "Test whether all elements in SUBLIST are members of LIST."
4180   (let ((sublistp t))
4181     (while sublist
4182       (unless (memq (pop sublist) list)
4183         (setq sublistp nil
4184               sublist nil)))
4185     sublistp))
4186
4187 \f
4188 ;;;
4189 ;;; Gnus group mode
4190 ;;;
4191
4192 (defvar gnus-group-mode-map nil)
4193 (put 'gnus-group-mode 'mode-class 'special)
4194
4195 (unless gnus-group-mode-map
4196   (setq gnus-group-mode-map (make-keymap))
4197   (suppress-keymap gnus-group-mode-map)
4198
4199   (gnus-define-keys gnus-group-mode-map
4200     " " gnus-group-read-group
4201     "=" gnus-group-select-group
4202     "\r" gnus-group-select-group
4203     "\M-\r" gnus-group-quick-select-group
4204     "j" gnus-group-jump-to-group
4205     "n" gnus-group-next-unread-group
4206     "p" gnus-group-prev-unread-group
4207     "\177" gnus-group-prev-unread-group
4208     [delete] gnus-group-prev-unread-group
4209     "N" gnus-group-next-group
4210     "P" gnus-group-prev-group
4211     "\M-n" gnus-group-next-unread-group-same-level
4212     "\M-p" gnus-group-prev-unread-group-same-level
4213     "," gnus-group-best-unread-group
4214     "." gnus-group-first-unread-group
4215     "u" gnus-group-unsubscribe-current-group
4216     "U" gnus-group-unsubscribe-group
4217     "c" gnus-group-catchup-current
4218     "C" gnus-group-catchup-current-all
4219     "l" gnus-group-list-groups
4220     "L" gnus-group-list-all-groups
4221     "m" gnus-group-mail
4222     "g" gnus-group-get-new-news
4223     "\M-g" gnus-group-get-new-news-this-group
4224     "R" gnus-group-restart
4225     "r" gnus-group-read-init-file
4226     "B" gnus-group-browse-foreign-server
4227     "b" gnus-group-check-bogus-groups
4228     "F" gnus-find-new-newsgroups
4229     "\C-c\C-d" gnus-group-describe-group
4230     "\M-d" gnus-group-describe-all-groups
4231     "\C-c\C-a" gnus-group-apropos
4232     "\C-c\M-\C-a" gnus-group-description-apropos
4233     "a" gnus-group-post-news
4234     "\ek" gnus-group-edit-local-kill
4235     "\eK" gnus-group-edit-global-kill
4236     "\C-k" gnus-group-kill-group
4237     "\C-y" gnus-group-yank-group
4238     "\C-w" gnus-group-kill-region
4239     "\C-x\C-t" gnus-group-transpose-groups
4240     "\C-c\C-l" gnus-group-list-killed
4241     "\C-c\C-x" gnus-group-expire-articles
4242     "\C-c\M-\C-x" gnus-group-expire-all-groups
4243     "V" gnus-version
4244     "s" gnus-group-save-newsrc
4245     "z" gnus-group-suspend
4246 ;    "Z" gnus-group-clear-dribble
4247     "q" gnus-group-exit
4248     "Q" gnus-group-quit
4249     "?" gnus-group-describe-briefly
4250     "\C-c\C-i" gnus-info-find-node
4251     "\M-e" gnus-group-edit-group-method
4252     "^" gnus-group-enter-server-mode
4253     gnus-mouse-2 gnus-mouse-pick-group
4254     "<" beginning-of-buffer
4255     ">" end-of-buffer
4256     "\C-c\C-b" gnus-bug
4257     "\C-c\C-s" gnus-group-sort-groups
4258     "t" gnus-topic-mode
4259     "\C-c\M-g" gnus-activate-all-groups
4260     "\M-&" gnus-group-universal-argument
4261     "#" gnus-group-mark-group
4262     "\M-#" gnus-group-unmark-group)
4263
4264   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4265     "m" gnus-group-mark-group
4266     "u" gnus-group-unmark-group
4267     "w" gnus-group-mark-region
4268     "m" gnus-group-mark-buffer
4269     "r" gnus-group-mark-regexp
4270     "U" gnus-group-unmark-all-groups)
4271
4272   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4273     "d" gnus-group-make-directory-group
4274     "h" gnus-group-make-help-group
4275     "a" gnus-group-make-archive-group
4276     "k" gnus-group-make-kiboze-group
4277     "m" gnus-group-make-group
4278     "E" gnus-group-edit-group
4279     "e" gnus-group-edit-group-method
4280     "p" gnus-group-edit-group-parameters
4281     "v" gnus-group-add-to-virtual
4282     "V" gnus-group-make-empty-virtual
4283     "D" gnus-group-enter-directory
4284     "f" gnus-group-make-doc-group
4285     "r" gnus-group-rename-group
4286     "\177" gnus-group-delete-group
4287     [delete] gnus-group-delete-group)
4288
4289    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4290      "b" gnus-group-brew-soup
4291      "w" gnus-soup-save-areas
4292      "s" gnus-soup-send-replies
4293      "p" gnus-soup-pack-packet
4294      "r" nnsoup-pack-replies)
4295
4296    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4297      "s" gnus-group-sort-groups
4298      "a" gnus-group-sort-groups-by-alphabet
4299      "u" gnus-group-sort-groups-by-unread
4300      "l" gnus-group-sort-groups-by-level
4301      "v" gnus-group-sort-groups-by-score
4302      "r" gnus-group-sort-groups-by-rank
4303      "m" gnus-group-sort-groups-by-method)
4304
4305    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4306      "k" gnus-group-list-killed
4307      "z" gnus-group-list-zombies
4308      "s" gnus-group-list-groups
4309      "u" gnus-group-list-all-groups
4310      "A" gnus-group-list-active
4311      "a" gnus-group-apropos
4312      "d" gnus-group-description-apropos
4313      "m" gnus-group-list-matching
4314      "M" gnus-group-list-all-matching
4315      "l" gnus-group-list-level)
4316
4317    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4318      "f" gnus-score-flush-cache)
4319
4320    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4321      "f" gnus-group-fetch-faq)
4322
4323    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4324      "l" gnus-group-set-current-level
4325      "t" gnus-group-unsubscribe-current-group
4326      "s" gnus-group-unsubscribe-group
4327      "k" gnus-group-kill-group
4328      "y" gnus-group-yank-group
4329      "w" gnus-group-kill-region
4330      "\C-k" gnus-group-kill-level
4331      "z" gnus-group-kill-all-zombies))
4332
4333 (defun gnus-group-mode ()
4334   "Major mode for reading news.
4335
4336 All normal editing commands are switched off.
4337 \\<gnus-group-mode-map>
4338 The group buffer lists (some of) the groups available.  For instance,
4339 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4340 lists all zombie groups.
4341
4342 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4343 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4344
4345 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4346
4347 The following commands are available:
4348
4349 \\{gnus-group-mode-map}"
4350   (interactive)
4351   (when (and menu-bar-mode
4352              (gnus-visual-p 'group-menu 'menu))
4353     (gnus-group-make-menu-bar))
4354   (kill-all-local-variables)
4355   (gnus-simplify-mode-line)
4356   (setq major-mode 'gnus-group-mode)
4357   (setq mode-name "Group")
4358   (gnus-group-set-mode-line)
4359   (setq mode-line-process nil)
4360   (use-local-map gnus-group-mode-map)
4361   (buffer-disable-undo (current-buffer))
4362   (setq truncate-lines t)
4363   (setq buffer-read-only t)
4364   (gnus-make-local-hook 'post-command-hook)
4365   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4366   (run-hooks 'gnus-group-mode-hook))
4367
4368 (defun gnus-clear-inboxes-moved ()
4369   (setq nnmail-moved-inboxes nil))
4370
4371 (defun gnus-mouse-pick-group (e)
4372   "Enter the group under the mouse pointer."
4373   (interactive "e")
4374   (mouse-set-point e)
4375   (gnus-group-read-group nil))
4376
4377 ;; Look at LEVEL and find out what the level is really supposed to be.
4378 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4379 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4380 (defun gnus-group-default-level (&optional level number-or-nil)
4381   (cond
4382    (gnus-group-use-permanent-levels
4383     (or (setq gnus-group-use-permanent-levels
4384               (or level (if (numberp gnus-group-use-permanent-levels)
4385                             gnus-group-use-permanent-levels
4386                           (or gnus-group-default-list-level
4387                               gnus-level-subscribed))))
4388         gnus-group-default-list-level gnus-level-subscribed))
4389    (number-or-nil
4390     level)
4391    (t
4392     (or level gnus-group-default-list-level gnus-level-subscribed))))
4393
4394 ;;;###autoload
4395 (defun gnus-slave-no-server (&optional arg)
4396   "Read network news as a slave, without connecting to local server"
4397   (interactive "P")
4398   (gnus-no-server arg t))
4399
4400 ;;;###autoload
4401 (defun gnus-no-server (&optional arg slave)
4402   "Read network news.
4403 If ARG is a positive number, Gnus will use that as the
4404 startup level.  If ARG is nil, Gnus will be started at level 2.
4405 If ARG is non-nil and not a positive number, Gnus will
4406 prompt the user for the name of an NNTP server to use.
4407 As opposed to `gnus', this command will not connect to the local server."
4408   (interactive "P")
4409   (let ((val (or arg (1- gnus-level-default-subscribed))))
4410     (gnus val t slave)
4411     (make-local-variable 'gnus-group-use-permanent-levels)
4412     (setq gnus-group-use-permanent-levels val)))
4413
4414 ;;;###autoload
4415 (defun gnus-slave (&optional arg)
4416   "Read news as a slave."
4417   (interactive "P")
4418   (gnus arg nil 'slave))
4419
4420 ;;;###autoload
4421 (defun gnus-other-frame (&optional arg)
4422   "Pop up a frame to read news."
4423   (interactive "P")
4424   (if (get-buffer gnus-group-buffer)
4425       (let ((pop-up-frames t))
4426         (gnus arg))
4427     (select-frame (make-frame))
4428     (gnus arg)))
4429
4430 ;;;###autoload
4431 (defun gnus (&optional arg dont-connect slave)
4432   "Read network news.
4433 If ARG is non-nil and a positive number, Gnus will use that as the
4434 startup level.  If ARG is non-nil and not a positive number, Gnus will
4435 prompt the user for the name of an NNTP server to use."
4436   (interactive "P")
4437
4438   (if (get-buffer gnus-group-buffer)
4439       (progn
4440         (switch-to-buffer gnus-group-buffer)
4441         (gnus-group-get-new-news))
4442
4443     (gnus-clear-system)
4444     (nnheader-init-server-buffer)
4445     (gnus-read-init-file)
4446     (setq gnus-slave slave)
4447
4448     (gnus-group-setup-buffer)
4449     (let ((buffer-read-only nil))
4450       (erase-buffer)
4451       (if (not gnus-inhibit-startup-message)
4452           (progn
4453             (gnus-group-startup-message)
4454             (sit-for 0))))
4455
4456     (let ((level (and (numberp arg) (> arg 0) arg))
4457           did-connect)
4458       (unwind-protect
4459           (progn
4460             (or dont-connect
4461                 (setq did-connect
4462                       (gnus-start-news-server (and arg (not level))))))
4463         (if (and (not dont-connect)
4464                  (not did-connect))
4465             (gnus-group-quit)
4466           (run-hooks 'gnus-startup-hook)
4467           ;; NNTP server is successfully open.
4468
4469           ;; Find the current startup file name.
4470           (setq gnus-current-startup-file
4471                 (gnus-make-newsrc-file gnus-startup-file))
4472
4473           ;; Read the dribble file.
4474           (when (or gnus-slave gnus-use-dribble-file)
4475             (gnus-dribble-read-file))
4476
4477           ;; Allow using GroupLens predictions.
4478           (when gnus-use-grouplens
4479             (bbb-login)
4480             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4481
4482           (gnus-summary-make-display-table)
4483           ;; Do the actual startup.
4484           (gnus-setup-news nil level dont-connect)
4485           ;; Generate the group buffer.
4486           (gnus-group-list-groups level)
4487           (gnus-group-first-unread-group)
4488           (gnus-configure-windows 'group)
4489           (gnus-group-set-mode-line))))))
4490
4491 (defun gnus-unload ()
4492   "Unload all Gnus features."
4493   (interactive)
4494   (or (boundp 'load-history)
4495       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4496   (let ((history load-history)
4497         feature)
4498     (while history
4499       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4500            (setq feature (cdr (assq 'provide (car history))))
4501            (unload-feature feature 'force))
4502       (setq history (cdr history)))))
4503
4504 (defun gnus-compile ()
4505   "Byte-compile the user-defined format specs."
4506   (interactive)
4507   (let ((entries gnus-format-specs)
4508         entry gnus-tmp-func)
4509     (save-excursion
4510       (gnus-message 7 "Compiling format specs...")
4511
4512       (while entries
4513         (setq entry (pop entries))
4514         (if (eq (car entry) 'version)
4515             (setq gnus-format-specs (delq entry gnus-format-specs))
4516           (when (and (listp (caddr entry))
4517                      (not (eq 'byte-code (caaddr entry))))
4518             (fset 'gnus-tmp-func
4519                   `(lambda () ,(caddr entry)))
4520             (byte-compile 'gnus-tmp-func)
4521             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4522
4523       (push (cons 'version emacs-version) gnus-format-specs)
4524       ;; Mark the .newsrc.eld file as "dirty".
4525       (gnus-dribble-enter " ")
4526       (gnus-message 7 "Compiling user specs...done"))))
4527
4528 (defun gnus-indent-rigidly (start end arg)
4529   "Indent rigidly using only spaces and no tabs."
4530   (save-excursion
4531     (save-restriction
4532       (narrow-to-region start end)
4533       (indent-rigidly start end arg)
4534       (goto-char (point-min))
4535       (while (search-forward "\t" nil t)
4536         (replace-match "        " t t)))))
4537
4538 (defun gnus-group-startup-message (&optional x y)
4539   "Insert startup message in current buffer."
4540   ;; Insert the message.
4541   (erase-buffer)
4542   (insert
4543    (format "              %s
4544           _    ___ _             _
4545           _ ___ __ ___  __    _ ___
4546           __   _     ___    __  ___
4547               _           ___     _
4548              _  _ __             _
4549              ___   __            _
4550                    __           _
4551                     _      _   _
4552                    _      _    _
4553                       _  _    _
4554                   __  ___
4555                  _   _ _     _
4556                 _   _
4557               _    _
4558              _    _
4559             _
4560           __
4561
4562 "
4563            ""))
4564   ;; And then hack it.
4565   (gnus-indent-rigidly (point-min) (point-max)
4566                        (/ (max (- (window-width) (or x 46)) 0) 2))
4567   (goto-char (point-min))
4568   (forward-line 1)
4569   (let* ((pheight (count-lines (point-min) (point-max)))
4570          (wheight (window-height))
4571          (rest (- wheight pheight)))
4572     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4573   ;; Fontify some.
4574   (goto-char (point-min))
4575   (and (search-forward "Praxis" nil t)
4576        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4577   (goto-char (point-min))
4578   (let* ((mode-string (gnus-group-set-mode-line)))
4579     (setq mode-line-buffer-identification
4580           (list (concat gnus-version (substring (car mode-string) 4))))
4581     (set-buffer-modified-p t)))
4582
4583 (defun gnus-group-setup-buffer ()
4584   (or (get-buffer gnus-group-buffer)
4585       (progn
4586         (switch-to-buffer gnus-group-buffer)
4587         (gnus-add-current-to-buffer-list)
4588         (gnus-group-mode)
4589         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4590
4591 (defun gnus-group-list-groups (&optional level unread lowest)
4592   "List newsgroups with level LEVEL or lower that have unread articles.
4593 Default is all subscribed groups.
4594 If argument UNREAD is non-nil, groups with no unread articles are also
4595 listed."
4596   (interactive (list (if current-prefix-arg
4597                          (prefix-numeric-value current-prefix-arg)
4598                        (or
4599                         (gnus-group-default-level nil t)
4600                         gnus-group-default-list-level
4601                         gnus-level-subscribed))))
4602   (or level
4603       (setq level (car gnus-group-list-mode)
4604             unread (cdr gnus-group-list-mode)))
4605   (setq level (gnus-group-default-level level))
4606   (gnus-group-setup-buffer)             ;May call from out of group buffer
4607   (gnus-update-format-specifications)
4608   (let ((case-fold-search nil)
4609         (props (text-properties-at (gnus-point-at-bol)))
4610         (group (gnus-group-group-name)))
4611     (set-buffer gnus-group-buffer)
4612     (funcall gnus-group-prepare-function level unread lowest)
4613     (if (zerop (buffer-size))
4614         (gnus-message 5 gnus-no-groups-message)
4615       (goto-char (point-max))
4616       (when (or (not gnus-group-goto-next-group-function)
4617                 (not (funcall gnus-group-goto-next-group-function 
4618                               group props)))
4619         (if (not group)
4620             ;; Go to the first group with unread articles.
4621             (gnus-group-search-forward t)
4622           ;; Find the right group to put point on.  If the current group
4623           ;; has disappeared in the new listing, try to find the next
4624           ;; one.        If no next one can be found, just leave point at the
4625           ;; first newsgroup in the buffer.
4626           (if (not (gnus-goto-char
4627                     (text-property-any
4628                      (point-min) (point-max)
4629                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4630               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4631                 (while (and newsrc
4632                             (not (gnus-goto-char
4633                                   (text-property-any
4634                                    (point-min) (point-max) 'gnus-group
4635                                    (gnus-intern-safe
4636                                     (caar newsrc) gnus-active-hashtb)))))
4637                   (setq newsrc (cdr newsrc)))
4638                 (or newsrc (progn (goto-char (point-max))
4639                                   (forward-line -1)))))))
4640       ;; Adjust cursor point.
4641       (gnus-group-position-point))))
4642
4643 (defun gnus-group-list-level (level &optional all)
4644   "List groups on LEVEL.
4645 If ALL (the prefix), also list groups that have no unread articles."
4646   (interactive "nList groups on level: \nP")
4647   (gnus-group-list-groups level all level))
4648
4649 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4650   "List all newsgroups with unread articles of level LEVEL or lower.
4651 If ALL is non-nil, list groups that have no unread articles.
4652 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4653 If REGEXP, only list groups matching REGEXP."
4654   (set-buffer gnus-group-buffer)
4655   (let ((buffer-read-only nil)
4656         (newsrc (cdr gnus-newsrc-alist))
4657         (lowest (or lowest 1))
4658         info clevel unread group params)
4659     (erase-buffer)
4660     (if (< lowest gnus-level-zombie)
4661         ;; List living groups.
4662         (while newsrc
4663           (setq info (car newsrc)
4664                 group (gnus-info-group info)
4665                 params (gnus-info-params info)
4666                 newsrc (cdr newsrc)
4667                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4668           (and unread                   ; This group might be bogus
4669                (or (not regexp)
4670                    (string-match regexp group))
4671                (<= (setq clevel (gnus-info-level info)) level)
4672                (>= clevel lowest)
4673                (or all                  ; We list all groups?
4674                    (if (eq unread t)    ; Unactivated?
4675                        gnus-group-list-inactive-groups ; We list unactivated 
4676                      (> unread 0))      ; We list groups with unread articles
4677                    (and gnus-list-groups-with-ticked-articles
4678                         (cdr (assq 'tick (gnus-info-marks info))))
4679                                         ; And groups with tickeds
4680                    ;; Check for permanent visibility.
4681                    (and gnus-permanently-visible-groups
4682                         (string-match gnus-permanently-visible-groups
4683                                       group))
4684                    (memq 'visible params)
4685                    (cdr (assq 'visible params)))
4686                (gnus-group-insert-group-line
4687                 group (gnus-info-level info)
4688                 (gnus-info-marks info) unread (gnus-info-method info)))))
4689
4690     ;; List dead groups.
4691     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4692          (gnus-group-prepare-flat-list-dead
4693           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4694           gnus-level-zombie ?Z
4695           regexp))
4696     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4697          (gnus-group-prepare-flat-list-dead
4698           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4699           gnus-level-killed ?K regexp))
4700
4701     (gnus-group-set-mode-line)
4702     (setq gnus-group-list-mode (cons level all))
4703     (run-hooks 'gnus-group-prepare-hook)))
4704
4705 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4706   ;; List zombies and killed lists somewhat faster, which was
4707   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4708   ;; this by ignoring the group format specification altogether.
4709   (let (group)
4710     (if regexp
4711         ;; This loop is used when listing groups that match some
4712         ;; regexp.
4713         (while groups
4714           (setq group (pop groups))
4715           (when (string-match regexp group)
4716             (gnus-add-text-properties
4717              (point) (prog1 (1+ (point))
4718                        (insert " " mark "     *: " group "\n"))
4719              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4720                    'gnus-unread t
4721                    'gnus-level level))))
4722       ;; This loop is used when listing all groups.
4723       (while groups
4724         (gnus-add-text-properties
4725          (point) (prog1 (1+ (point))
4726                    (insert " " mark "     *: "
4727                            (setq group (pop groups)) "\n"))
4728          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4729                'gnus-unread t
4730                'gnus-level level))))))
4731
4732 (defmacro gnus-group-real-name (group)
4733   "Find the real name of a foreign newsgroup."
4734   `(let ((gname ,group))
4735      (if (string-match ":[^:]+$" gname)
4736          (substring gname (1+ (match-beginning 0)))
4737        gname)))
4738
4739 (defsubst gnus-server-add-address (method)
4740   (let ((method-name (symbol-name (car method))))
4741     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4742              (not (assq (intern (concat method-name "-address")) method)))
4743         (append method (list (list (intern (concat method-name "-address"))
4744                                    (nth 1 method))))
4745       method)))
4746
4747 (defsubst gnus-server-get-method (group method)
4748   ;; Input either a server name, and extended server name, or a
4749   ;; select method, and return a select method.
4750   (cond ((stringp method)
4751          (gnus-server-to-method method))
4752         ((equal method gnus-select-method)
4753          gnus-select-method)
4754         ((and (stringp (car method)) group)
4755          (gnus-server-extend-method group method))
4756         ((and method (not group)
4757               (equal (cadr method) ""))
4758          method)
4759         (t
4760          (gnus-server-add-address method))))
4761
4762 (defun gnus-server-to-method (server)
4763   "Map virtual server names to select methods."
4764   (or 
4765    ;; Is this a method, perhaps?
4766    (and server (listp server) server)
4767    ;; Perhaps this is the native server?
4768    (and (equal server "native") gnus-select-method)
4769    ;; It should be in the server alist.
4770    (cdr (assoc server gnus-server-alist))
4771    ;; If not, we look through all the opened server
4772    ;; to see whether we can find it there.
4773    (let ((opened gnus-opened-servers))
4774      (while (and opened
4775                  (not (equal server (format "%s:%s" (caaar opened)
4776                                             (cadaar opened)))))
4777        (pop opened))
4778      (caar opened))))
4779
4780 (defmacro gnus-method-equal (ss1 ss2)
4781   "Say whether two servers are equal."
4782   `(let ((s1 ,ss1)
4783          (s2 ,ss2))
4784      (or (equal s1 s2)
4785          (and (= (length s1) (length s2))
4786               (progn
4787                 (while (and s1 (member (car s1) s2))
4788                   (setq s1 (cdr s1)))
4789                 (null s1))))))
4790
4791 (defun gnus-server-equal (m1 m2)
4792   "Say whether two methods are equal."
4793   (let ((m1 (cond ((null m1) gnus-select-method)
4794                   ((stringp m1) (gnus-server-to-method m1))
4795                   (t m1)))
4796         (m2 (cond ((null m2) gnus-select-method)
4797                   ((stringp m2) (gnus-server-to-method m2))
4798                   (t m2))))
4799     (gnus-method-equal m1 m2)))
4800
4801 (defun gnus-servers-using-backend (backend)
4802   "Return a list of known servers using BACKEND."
4803   (let ((opened gnus-opened-servers)
4804         out)
4805     (while opened
4806       (when (eq backend (caaar opened))
4807         (push (caar opened) out))
4808       (pop opened))
4809     out))
4810
4811 (defun gnus-archive-server-wanted-p ()
4812   "Say whether the user wants to use the archive server."
4813   (cond 
4814    ((or (not gnus-message-archive-method)
4815         (not gnus-message-archive-group))
4816     nil)
4817    ((and gnus-message-archive-method gnus-message-archive-group)
4818     t)
4819    (t
4820     (let ((active (cadr (assq 'nnfolder-active-file
4821                               gnus-message-archive-method))))
4822       (and active
4823            (file-exists-p active))))))
4824
4825 (defun gnus-group-prefixed-name (group method)
4826   "Return the whole name from GROUP and METHOD."
4827   (and (stringp method) (setq method (gnus-server-to-method method)))
4828   (if (not method)
4829       group
4830     (concat (format "%s" (car method))
4831             (if (and
4832                  (or (assoc (format "%s" (car method)) 
4833                             (gnus-methods-using 'address))
4834                      (gnus-server-equal method gnus-message-archive-method))
4835                  (nth 1 method)
4836                  (not (string= (nth 1 method) "")))
4837                 (concat "+" (nth 1 method)))
4838             ":" group)))
4839
4840 (defun gnus-group-real-prefix (group)
4841   "Return the prefix of the current group name."
4842   (if (string-match "^[^:]+:" group)
4843       (substring group 0 (match-end 0))
4844     ""))
4845
4846 (defun gnus-group-method (group)
4847   "Return the server or method used for selecting GROUP."
4848   (let ((prefix (gnus-group-real-prefix group)))
4849     (if (equal prefix "")
4850         gnus-select-method
4851       (let ((servers gnus-opened-servers)
4852             (server "")
4853             backend possible found)
4854         (if (string-match "^[^\\+]+\\+" prefix)
4855             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4856                   server (substring prefix (match-end 0) (1- (length prefix))))
4857           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4858         (while servers
4859           (when (eq (caaar servers) backend)
4860             (setq possible (caar servers))
4861             (when (equal (cadaar servers) server)
4862               (setq found (caar servers))))
4863           (pop servers))
4864         (or (car (rassoc found gnus-server-alist))
4865             found
4866             (car (rassoc possible gnus-server-alist))
4867             possible
4868             (list backend server))))))
4869
4870 (defsubst gnus-secondary-method-p (method)
4871   "Return whether METHOD is a secondary select method."
4872   (let ((methods gnus-secondary-select-methods)
4873         (gmethod (gnus-server-get-method nil method)))
4874     (while (and methods
4875                 (not (equal (gnus-server-get-method nil (car methods))
4876                             gmethod)))
4877       (setq methods (cdr methods)))
4878     methods))
4879
4880 (defun gnus-group-foreign-p (group)
4881   "Say whether a group is foreign or not."
4882   (and (not (gnus-group-native-p group))
4883        (not (gnus-group-secondary-p group))))
4884
4885 (defun gnus-group-native-p (group)
4886   "Say whether the group is native or not."
4887   (not (string-match ":" group)))
4888
4889 (defun gnus-group-secondary-p (group)
4890   "Say whether the group is secondary or not."
4891   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4892
4893 (defun gnus-group-get-parameter (group &optional symbol)
4894   "Returns the group parameters for GROUP.
4895 If SYMBOL, return the value of that symbol in the group parameters."
4896   (let ((params (gnus-info-params (gnus-get-info group))))
4897     (if symbol
4898         (gnus-group-parameter-value params symbol)
4899       params)))
4900
4901 (defun gnus-group-parameter-value (params symbol)
4902   "Return the value of SYMBOL in group PARAMS."
4903   (or (car (memq symbol params))        ; It's either a simple symbol
4904       (cdr (assq symbol params))))      ; or a cons.
4905
4906 (defun gnus-group-add-parameter (group param)
4907   "Add parameter PARAM to GROUP."
4908   (let ((info (gnus-get-info group)))
4909     (if (not info)
4910         () ; This is a dead group.  We just ignore it.
4911       ;; Cons the new param to the old one and update.
4912       (gnus-group-set-info (cons param (gnus-info-params info))
4913                            group 'params))))
4914
4915 (defun gnus-group-set-parameter (group name value)
4916   "Set parameter NAME to VALUE in GROUP."
4917   (let ((info (gnus-get-info group)))
4918     (if (not info)
4919         () ; This is a dead group.  We just ignore it.
4920       (let ((old-params (gnus-info-params info))
4921             (new-params (list (cons name value))))
4922         (while old-params
4923           (if (or (not (listp (car old-params)))
4924                   (not (eq (caar old-params) name)))
4925               (setq new-params (append new-params (list (car old-params)))))
4926           (setq old-params (cdr old-params)))
4927         (gnus-group-set-info new-params group 'params)))))
4928
4929 (defun gnus-group-add-score (group &optional score)
4930   "Add SCORE to the GROUP score.
4931 If SCORE is nil, add 1 to the score of GROUP."
4932   (let ((info (gnus-get-info group)))
4933     (when info
4934       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4935
4936 (defun gnus-summary-bubble-group ()
4937   "Increase the score of the current group.
4938 This is a handy function to add to `gnus-summary-exit-hook' to
4939 increase the score of each group you read."
4940   (gnus-group-add-score gnus-newsgroup-name))
4941
4942 (defun gnus-group-set-info (info &optional method-only-group part)
4943   (let* ((entry (gnus-gethash
4944                  (or method-only-group (gnus-info-group info))
4945                  gnus-newsrc-hashtb))
4946          (part-info info)
4947          (info (if method-only-group (nth 2 entry) info))
4948          method)
4949     (when method-only-group
4950       (unless entry
4951         (error "Trying to change non-existent group %s" method-only-group))
4952       ;; We have received parts of the actual group info - either the
4953       ;; select method or the group parameters.  We first check
4954       ;; whether we have to extend the info, and if so, do that.
4955       (let ((len (length info))
4956             (total (if (eq part 'method) 5 6)))
4957         (when (< len total)
4958           (setcdr (nthcdr (1- len) info)
4959                   (make-list (- total len) nil)))
4960         ;; Then we enter the new info.
4961         (setcar (nthcdr (1- total) info) part-info)))
4962     (unless entry
4963       ;; This is a new group, so we just create it.
4964       (save-excursion
4965         (set-buffer gnus-group-buffer)
4966         (setq method (gnus-info-method info))
4967         (when (gnus-server-equal method "native")
4968           (setq method nil))
4969         (save-excursion
4970           (set-buffer gnus-group-buffer)
4971           (if method
4972               ;; It's a foreign group...
4973               (gnus-group-make-group
4974                (gnus-group-real-name (gnus-info-group info))
4975                (if (stringp method) method
4976                  (prin1-to-string (car method)))
4977                (and (consp method)
4978                     (nth 1 (gnus-info-method info))))
4979             ;; It's a native group.
4980             (gnus-group-make-group (gnus-info-group info))))
4981         (gnus-message 6 "Note: New group created")
4982         (setq entry
4983               (gnus-gethash (gnus-group-prefixed-name
4984                              (gnus-group-real-name (gnus-info-group info))
4985                              (or (gnus-info-method info) gnus-select-method))
4986                             gnus-newsrc-hashtb))))
4987     ;; Whether it was a new group or not, we now have the entry, so we
4988     ;; can do the update.
4989     (if entry
4990         (progn
4991           (setcar (nthcdr 2 entry) info)
4992           (when (and (not (eq (car entry) t))
4993                      (gnus-active (gnus-info-group info)))
4994             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4995       (error "No such group: %s" (gnus-info-group info)))))
4996
4997 (defun gnus-group-set-method-info (group select-method)
4998   (gnus-group-set-info select-method group 'method))
4999
5000 (defun gnus-group-set-params-info (group params)
5001   (gnus-group-set-info params group 'params))
5002
5003 (defun gnus-group-update-group-line ()
5004   "Update the current line in the group buffer."
5005   (let* ((buffer-read-only nil)
5006          (group (gnus-group-group-name))
5007          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
5008          gnus-group-indentation)
5009     (when group
5010       (and entry
5011            (not (gnus-ephemeral-group-p group))
5012            (gnus-dribble-enter
5013             (concat "(gnus-group-set-info '"
5014                     (prin1-to-string (nth 2 entry)) ")")))
5015       (setq gnus-group-indentation (gnus-group-group-indentation))
5016       (gnus-delete-line)
5017       (gnus-group-insert-group-line-info group)
5018       (forward-line -1)
5019       (gnus-group-position-point))))
5020
5021 (defun gnus-group-insert-group-line-info (group)
5022   "Insert GROUP on the current line."
5023   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
5024         active info)
5025     (if entry
5026         (progn
5027           ;; (Un)subscribed group.
5028           (setq info (nth 2 entry))
5029           (gnus-group-insert-group-line
5030            group (gnus-info-level info) (gnus-info-marks info)
5031            (or (car entry) t) (gnus-info-method info)))
5032       ;; This group is dead.
5033       (gnus-group-insert-group-line
5034        group
5035        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
5036        nil
5037        (if (setq active (gnus-active group))
5038            (- (1+ (cdr active)) (car active)) 0)
5039        nil))))
5040
5041 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
5042                                                     gnus-tmp-marked number
5043                                                     gnus-tmp-method)
5044   "Insert a group line in the group buffer."
5045   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
5046          (gnus-tmp-number-total
5047           (if gnus-tmp-active
5048               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
5049             0))
5050          (gnus-tmp-number-of-unread
5051           (if (numberp number) (int-to-string (max 0 number))
5052             "*"))
5053          (gnus-tmp-number-of-read
5054           (if (numberp number)
5055               (int-to-string (max 0 (- gnus-tmp-number-total number)))
5056             "*"))
5057          (gnus-tmp-subscribed
5058           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
5059                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
5060                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
5061                 (t ?K)))
5062          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
5063          (gnus-tmp-newsgroup-description
5064           (if gnus-description-hashtb
5065               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
5066             ""))
5067          (gnus-tmp-moderated
5068           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
5069          (gnus-tmp-moderated-string
5070           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
5071          (gnus-tmp-method
5072           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
5073          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
5074          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
5075          (gnus-tmp-news-method-string
5076           (if gnus-tmp-method
5077               (format "(%s:%s)" (car gnus-tmp-method)
5078                       (cadr gnus-tmp-method)) ""))
5079          (gnus-tmp-marked-mark
5080           (if (and (numberp number)
5081                    (zerop number)
5082                    (cdr (assq 'tick gnus-tmp-marked)))
5083               ?* ? ))
5084          (gnus-tmp-process-marked
5085           (if (member gnus-tmp-group gnus-group-marked)
5086               gnus-process-mark ? ))
5087          (gnus-tmp-grouplens
5088           (or (and gnus-use-grouplens
5089                    (bbb-grouplens-group-p gnus-tmp-group))
5090               ""))
5091          (buffer-read-only nil)
5092          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5093     (beginning-of-line)
5094     (gnus-add-text-properties
5095      (point)
5096      (prog1 (1+ (point))
5097        ;; Insert the text.
5098        (eval gnus-group-line-format-spec))
5099      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5100        gnus-unread ,(if (numberp number)
5101                         (string-to-int gnus-tmp-number-of-unread)
5102                       t)
5103        gnus-marked ,gnus-tmp-marked-mark
5104        gnus-indentation ,gnus-group-indentation
5105        gnus-level ,gnus-tmp-level))
5106     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5107       (forward-line -1)
5108       (run-hooks 'gnus-group-update-hook)
5109       (forward-line))
5110     ;; Allow XEmacs to remove front-sticky text properties.
5111     (gnus-group-remove-excess-properties)))
5112
5113 (defun gnus-group-update-group (group &optional visible-only)
5114   "Update all lines where GROUP appear.
5115 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5116 already."
5117   (save-excursion
5118     (set-buffer gnus-group-buffer)
5119     ;; The buffer may be narrowed.
5120     (save-restriction
5121       (widen)
5122       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5123             (loc (point-min))
5124             found buffer-read-only)
5125         ;; Enter the current status into the dribble buffer.
5126         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5127           (if (and entry (not (gnus-ephemeral-group-p group)))
5128               (gnus-dribble-enter
5129                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5130                        ")"))))
5131         ;; Find all group instances.  If topics are in use, each group
5132         ;; may be listed in more than once.
5133         (while (setq loc (text-property-any
5134                           loc (point-max) 'gnus-group ident))
5135           (setq found t)
5136           (goto-char loc)
5137           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5138             (gnus-delete-line)
5139             (gnus-group-insert-group-line-info group)
5140             (save-excursion
5141               (forward-line -1)
5142               (run-hooks 'gnus-group-update-group-hook)))
5143           (setq loc (1+ loc)))
5144         (unless (or found visible-only)
5145           ;; No such line in the buffer, find out where it's supposed to
5146           ;; go, and insert it there (or at the end of the buffer).
5147           (if gnus-goto-missing-group-function
5148               (funcall gnus-goto-missing-group-function group)
5149             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5150               (while (and entry (car entry)
5151                           (not
5152                            (gnus-goto-char
5153                             (text-property-any
5154                              (point-min) (point-max)
5155                              'gnus-group (gnus-intern-safe
5156                                           (caar entry) gnus-active-hashtb)))))
5157                 (setq entry (cdr entry)))
5158               (or entry (goto-char (point-max)))))
5159           ;; Finally insert the line.
5160           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5161             (gnus-group-insert-group-line-info group)
5162             (save-excursion
5163               (forward-line -1)
5164               (run-hooks 'gnus-group-update-group-hook))))
5165         (gnus-group-set-mode-line)))))
5166
5167 (defun gnus-group-set-mode-line ()
5168   "Update the mode line in the group buffer."
5169   (when (memq 'group gnus-updated-mode-lines)
5170     ;; Yes, we want to keep this mode line updated.
5171     (save-excursion
5172       (set-buffer gnus-group-buffer)
5173       (let* ((gformat (or gnus-group-mode-line-format-spec
5174                           (setq gnus-group-mode-line-format-spec
5175                                 (gnus-parse-format
5176                                  gnus-group-mode-line-format
5177                                  gnus-group-mode-line-format-alist))))
5178              (gnus-tmp-news-server (cadr gnus-select-method))
5179              (gnus-tmp-news-method (car gnus-select-method))
5180              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5181              (max-len 60)
5182              gnus-tmp-header            ;Dummy binding for user-defined formats
5183              ;; Get the resulting string.
5184              (modified 
5185               (and gnus-dribble-buffer
5186                    (buffer-name gnus-dribble-buffer)
5187                    (buffer-modified-p gnus-dribble-buffer)
5188                    (save-excursion
5189                      (set-buffer gnus-dribble-buffer)
5190                      (not (zerop (buffer-size))))))
5191              (mode-string (eval gformat)))
5192         ;; Say whether the dribble buffer has been modified.
5193         (setq mode-line-modified
5194               (if modified "---*- " "----- "))
5195         ;; If the line is too long, we chop it off.
5196         (when (> (length mode-string) max-len)
5197           (setq mode-string (substring mode-string 0 (- max-len 4))))
5198         (prog1
5199             (setq mode-line-buffer-identification 
5200                   (gnus-mode-line-buffer-identification
5201                    (list mode-string)))
5202           (set-buffer-modified-p modified))))))
5203
5204 (defun gnus-group-group-name ()
5205   "Get the name of the newsgroup on the current line."
5206   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5207     (and group (symbol-name group))))
5208
5209 (defun gnus-group-group-level ()
5210   "Get the level of the newsgroup on the current line."
5211   (get-text-property (gnus-point-at-bol) 'gnus-level))
5212
5213 (defun gnus-group-group-indentation ()
5214   "Get the indentation of the newsgroup on the current line."
5215   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5216       (and gnus-group-indentation-function
5217            (funcall gnus-group-indentation-function))
5218       ""))
5219
5220 (defun gnus-group-group-unread ()
5221   "Get the number of unread articles of the newsgroup on the current line."
5222   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5223
5224 (defun gnus-group-search-forward (&optional backward all level first-too)
5225   "Find the next newsgroup with unread articles.
5226 If BACKWARD is non-nil, find the previous newsgroup instead.
5227 If ALL is non-nil, just find any newsgroup.
5228 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5229 group exists.
5230 If FIRST-TOO, the current line is also eligible as a target."
5231   (let ((way (if backward -1 1))
5232         (low gnus-level-killed)
5233         (beg (point))
5234         pos found lev)
5235     (if (and backward (progn (beginning-of-line)) (bobp))
5236         nil
5237       (or first-too (forward-line way))
5238       (while (and
5239               (not (eobp))
5240               (not (setq
5241                     found
5242                     (and (or all
5243                              (and
5244                               (let ((unread
5245                                      (get-text-property (point) 'gnus-unread)))
5246                                 (and (numberp unread) (> unread 0)))
5247                               (setq lev (get-text-property (point)
5248                                                            'gnus-level))
5249                               (<= lev gnus-level-subscribed)))
5250                          (or (not level)
5251                              (and (setq lev (get-text-property (point)
5252                                                                'gnus-level))
5253                                   (or (= lev level)
5254                                       (and (< lev low)
5255                                            (< level lev)
5256                                            (progn
5257                                              (setq low lev)
5258                                              (setq pos (point))
5259                                              nil))))))))
5260               (zerop (forward-line way)))))
5261     (if found
5262         (progn (gnus-group-position-point) t)
5263       (goto-char (or pos beg))
5264       (and pos t))))
5265
5266 ;;; Gnus group mode commands
5267
5268 ;; Group marking.
5269
5270 (defun gnus-group-mark-group (n &optional unmark no-advance)
5271   "Mark the current group."
5272   (interactive "p")
5273   (let ((buffer-read-only nil)
5274         group)
5275     (while (and (> n 0)
5276                 (not (eobp)))
5277       (when (setq group (gnus-group-group-name))
5278         ;; Update the mark.
5279         (beginning-of-line)
5280         (forward-char
5281          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5282         (delete-char 1)
5283         (if unmark
5284             (progn
5285               (insert " ")
5286               (setq gnus-group-marked (delete group gnus-group-marked)))
5287           (insert "#")
5288           (setq gnus-group-marked
5289                 (cons group (delete group gnus-group-marked)))))
5290       (or no-advance (gnus-group-next-group 1))
5291       (decf n))
5292     (gnus-summary-position-point)
5293     n))
5294
5295 (defun gnus-group-unmark-group (n)
5296   "Remove the mark from the current group."
5297   (interactive "p")
5298   (gnus-group-mark-group n 'unmark)
5299   (gnus-group-position-point))
5300
5301 (defun gnus-group-unmark-all-groups ()
5302   "Unmark all groups."
5303   (interactive)
5304   (let ((groups gnus-group-marked))
5305     (save-excursion
5306       (while groups
5307         (gnus-group-remove-mark (pop groups)))))
5308   (gnus-group-position-point))
5309
5310 (defun gnus-group-mark-region (unmark beg end)
5311   "Mark all groups between point and mark.
5312 If UNMARK, remove the mark instead."
5313   (interactive "P\nr")
5314   (let ((num (count-lines beg end)))
5315     (save-excursion
5316       (goto-char beg)
5317       (- num (gnus-group-mark-group num unmark)))))
5318
5319 (defun gnus-group-mark-buffer (&optional unmark)
5320   "Mark all groups in the buffer.
5321 If UNMARK, remove the mark instead."
5322   (interactive "P")
5323   (gnus-group-mark-region unmark (point-min) (point-max)))
5324
5325 (defun gnus-group-mark-regexp (regexp)
5326   "Mark all groups that match some regexp."
5327   (interactive "sMark (regexp): ")
5328   (let ((alist (cdr gnus-newsrc-alist))
5329         group)
5330     (while alist
5331       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5332         (gnus-group-set-mark group))))
5333   (gnus-group-position-point))
5334
5335 (defun gnus-group-remove-mark (group)
5336   "Remove the process mark from GROUP and move point there.
5337 Return nil if the group isn't displayed."
5338   (if (gnus-group-goto-group group)
5339       (save-excursion
5340         (gnus-group-mark-group 1 'unmark t)
5341         t)
5342     (setq gnus-group-marked
5343           (delete group gnus-group-marked))
5344     nil))
5345
5346 (defun gnus-group-set-mark (group)
5347   "Set the process mark on GROUP."
5348   (if (gnus-group-goto-group group) 
5349       (save-excursion
5350         (gnus-group-mark-group 1 nil t))
5351     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5352
5353 (defun gnus-group-universal-argument (arg &optional groups func)
5354   "Perform any command on all groups accoring to the process/prefix convention."
5355   (interactive "P")
5356   (let ((groups (or groups (gnus-group-process-prefix arg)))
5357         group func)
5358     (if (eq (setq func (or func
5359                            (key-binding
5360                             (read-key-sequence
5361                              (substitute-command-keys
5362                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5363             'undefined)
5364         (gnus-error 1 "Undefined key")
5365       (while groups
5366         (gnus-group-remove-mark (setq group (pop groups)))
5367         (command-execute func))))
5368   (gnus-group-position-point))
5369
5370 (defun gnus-group-process-prefix (n)
5371   "Return a list of groups to work on.
5372 Take into consideration N (the prefix) and the list of marked groups."
5373   (cond
5374    (n
5375     (setq n (prefix-numeric-value n))
5376     ;; There is a prefix, so we return a list of the N next
5377     ;; groups.
5378     (let ((way (if (< n 0) -1 1))
5379           (n (abs n))
5380           group groups)
5381       (save-excursion
5382         (while (and (> n 0)
5383                     (setq group (gnus-group-group-name)))
5384           (setq groups (cons group groups))
5385           (setq n (1- n))
5386           (gnus-group-next-group way)))
5387       (nreverse groups)))
5388    ((and (boundp 'transient-mark-mode)
5389          transient-mark-mode
5390          (boundp 'mark-active)
5391          mark-active)
5392     ;; Work on the region between point and mark.
5393     (let ((max (max (point) (mark)))
5394           groups)
5395       (save-excursion
5396         (goto-char (min (point) (mark)))
5397         (while
5398             (and
5399              (push (gnus-group-group-name) groups)
5400              (zerop (gnus-group-next-group 1))
5401              (< (point) max)))
5402         (nreverse groups))))
5403    (gnus-group-marked
5404     ;; No prefix, but a list of marked articles.
5405     (reverse gnus-group-marked))
5406    (t
5407     ;; Neither marked articles or a prefix, so we return the
5408     ;; current group.
5409     (let ((group (gnus-group-group-name)))
5410       (and group (list group))))))
5411
5412 ;; Selecting groups.
5413
5414 (defun gnus-group-read-group (&optional all no-article group)
5415   "Read news in this newsgroup.
5416 If the prefix argument ALL is non-nil, already read articles become
5417 readable.  IF ALL is a number, fetch this number of articles.  If the
5418 optional argument NO-ARTICLE is non-nil, no article will be
5419 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5420 group."
5421   (interactive "P")
5422   (let ((group (or group (gnus-group-group-name)))
5423         number active marked entry)
5424     (or group (error "No group on current line"))
5425     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5426                                             group gnus-newsrc-hashtb)))))
5427     ;; This group might be a dead group.  In that case we have to get
5428     ;; the number of unread articles from `gnus-active-hashtb'.
5429     (setq number
5430           (cond ((numberp all) all)
5431                 (entry (car entry))
5432                 ((setq active (gnus-active group))
5433                  (- (1+ (cdr active)) (car active)))))
5434     (gnus-summary-read-group
5435      group (or all (and (numberp number)
5436                         (zerop (+ number (length (cdr (assq 'tick marked)))
5437                                   (length (cdr (assq 'dormant marked)))))))
5438      no-article)))
5439
5440 (defun gnus-group-select-group (&optional all)
5441   "Select this newsgroup.
5442 No article is selected automatically.
5443 If ALL is non-nil, already read articles become readable.
5444 If ALL is a number, fetch this number of articles."
5445   (interactive "P")
5446   (gnus-group-read-group all t))
5447
5448 (defun gnus-group-quick-select-group (&optional all)
5449   "Select the current group \"quickly\".
5450 This means that no highlighting or scoring will be performed."
5451   (interactive "P")
5452   (let (gnus-visual
5453         gnus-score-find-score-files-function
5454         gnus-apply-kill-hook
5455         gnus-summary-expunge-below)
5456     (gnus-group-read-group all t)))
5457
5458 (defun gnus-group-visible-select-group (&optional all)
5459   "Select the current group without hiding any articles."
5460   (interactive "P")
5461   (let ((gnus-inhibit-limiting t))
5462     (gnus-group-read-group all t)))
5463
5464 ;;;###autoload
5465 (defun gnus-fetch-group (group)
5466   "Start Gnus if necessary and enter GROUP.
5467 Returns whether the fetching was successful or not."
5468   (interactive "sGroup name: ")
5469   (or (get-buffer gnus-group-buffer)
5470       (gnus))
5471   (gnus-group-read-group nil nil group))
5472
5473 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5474 ;; if selection was successful.
5475 (defun gnus-group-read-ephemeral-group
5476   (group method &optional activate quit-config)
5477   (let ((group (if (gnus-group-foreign-p group) group
5478                  (gnus-group-prefixed-name group method))))
5479     (gnus-sethash
5480      group
5481      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5482                      ((quit-config . ,(if quit-config quit-config
5483                                         (cons (current-buffer) 'summary))))))
5484      gnus-newsrc-hashtb)
5485     (set-buffer gnus-group-buffer)
5486     (or (gnus-check-server method)
5487         (error "Unable to contact server: %s" (gnus-status-message method)))
5488     (if activate (or (gnus-request-group group)
5489                      (error "Couldn't request group")))
5490     (condition-case ()
5491         (gnus-group-read-group t t group)
5492       (error nil)
5493       (quit nil))))
5494
5495 (defun gnus-group-jump-to-group (group)
5496   "Jump to newsgroup GROUP."
5497   (interactive
5498    (list (completing-read
5499           "Group: " gnus-active-hashtb nil
5500           (gnus-read-active-file-p)
5501           nil
5502           'gnus-group-history)))
5503
5504   (when (equal group "")
5505     (error "Empty group name"))
5506
5507   (when (string-match "[\000-\032]" group)
5508     (error "Control characters in group: %s" group))
5509
5510   (let ((b (text-property-any
5511             (point-min) (point-max)
5512             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5513     (unless (gnus-ephemeral-group-p group)
5514       (if b
5515           ;; Either go to the line in the group buffer...
5516           (goto-char b)
5517         ;; ... or insert the line.
5518         (or
5519          t ;; Don't activate group.
5520          (gnus-active group)
5521          (gnus-activate-group group)
5522          (error "%s error: %s" group (gnus-status-message group)))
5523
5524         (gnus-group-update-group group)
5525         (goto-char (text-property-any
5526                     (point-min) (point-max)
5527                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5528     ;; Adjust cursor point.
5529     (gnus-group-position-point)))
5530
5531 (defun gnus-group-goto-group (group)
5532   "Goto to newsgroup GROUP."
5533   (when group
5534     ;; It's quite likely that we are on the right line, so
5535     ;; we check the current line first.
5536     (beginning-of-line)
5537     (if (eq (get-text-property (point) 'gnus-group)
5538             (gnus-intern-safe group gnus-active-hashtb))
5539         (point)
5540       ;; Search through the entire buffer.
5541       (let ((b (text-property-any 
5542                 (point-min) (point-max)
5543                 'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5544         (when b 
5545           (goto-char b))))))
5546
5547 (defun gnus-group-next-group (n &optional silent)
5548   "Go to next N'th newsgroup.
5549 If N is negative, search backward instead.
5550 Returns the difference between N and the number of skips actually
5551 done."
5552   (interactive "p")
5553   (gnus-group-next-unread-group n t nil silent))
5554
5555 (defun gnus-group-next-unread-group (n &optional all level silent)
5556   "Go to next N'th unread newsgroup.
5557 If N is negative, search backward instead.
5558 If ALL is non-nil, choose any newsgroup, unread or not.
5559 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5560 such group can be found, the next group with a level higher than
5561 LEVEL.
5562 Returns the difference between N and the number of skips actually
5563 made."
5564   (interactive "p")
5565   (let ((backward (< n 0))
5566         (n (abs n)))
5567     (while (and (> n 0)
5568                 (gnus-group-search-forward
5569                  backward (or (not gnus-group-goto-unread) all) level))
5570       (setq n (1- n)))
5571     (when (and (/= 0 n)
5572                (not silent))
5573       (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5574                     (if level " on this level or higher" "")))
5575     n))
5576
5577 (defun gnus-group-prev-group (n)
5578   "Go to previous N'th newsgroup.
5579 Returns the difference between N and the number of skips actually
5580 done."
5581   (interactive "p")
5582   (gnus-group-next-unread-group (- n) t))
5583
5584 (defun gnus-group-prev-unread-group (n)
5585   "Go to previous N'th unread newsgroup.
5586 Returns the difference between N and the number of skips actually
5587 done."
5588   (interactive "p")
5589   (gnus-group-next-unread-group (- n)))
5590
5591 (defun gnus-group-next-unread-group-same-level (n)
5592   "Go to next N'th unread newsgroup on the same level.
5593 If N is negative, search backward instead.
5594 Returns the difference between N and the number of skips actually
5595 done."
5596   (interactive "p")
5597   (gnus-group-next-unread-group n t (gnus-group-group-level))
5598   (gnus-group-position-point))
5599
5600 (defun gnus-group-prev-unread-group-same-level (n)
5601   "Go to next N'th unread newsgroup on the same level.
5602 Returns the difference between N and the number of skips actually
5603 done."
5604   (interactive "p")
5605   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5606   (gnus-group-position-point))
5607
5608 (defun gnus-group-best-unread-group (&optional exclude-group)
5609   "Go to the group with the highest level.
5610 If EXCLUDE-GROUP, do not go to that group."
5611   (interactive)
5612   (goto-char (point-min))
5613   (let ((best 100000)
5614         unread best-point)
5615     (while (not (eobp))
5616       (setq unread (get-text-property (point) 'gnus-unread))
5617       (if (and (numberp unread) (> unread 0))
5618           (progn
5619             (if (and (get-text-property (point) 'gnus-level)
5620                      (< (get-text-property (point) 'gnus-level) best)
5621                      (or (not exclude-group)
5622                          (not (equal exclude-group (gnus-group-group-name)))))
5623                 (progn
5624                   (setq best (get-text-property (point) 'gnus-level))
5625                   (setq best-point (point))))))
5626       (forward-line 1))
5627     (if best-point (goto-char best-point))
5628     (gnus-summary-position-point)
5629     (and best-point (gnus-group-group-name))))
5630
5631 (defun gnus-group-first-unread-group ()
5632   "Go to the first group with unread articles."
5633   (interactive)
5634   (prog1
5635       (let ((opoint (point))
5636             unread)
5637         (goto-char (point-min))
5638         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5639                 (and (numberp unread)   ; Not a topic.
5640                      (not (zerop unread))) ; Has unread articles.
5641                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5642             (point)                     ; Success.
5643           (goto-char opoint)
5644           nil))                         ; Not success.
5645     (gnus-group-position-point)))
5646
5647 (defun gnus-group-enter-server-mode ()
5648   "Jump to the server buffer."
5649   (interactive)
5650   (gnus-enter-server-buffer))
5651
5652 (defun gnus-group-make-group (name &optional method address)
5653   "Add a new newsgroup.
5654 The user will be prompted for a NAME, for a select METHOD, and an
5655 ADDRESS."
5656   (interactive
5657    (cons
5658     (read-string "Group name: ")
5659     (let ((method
5660            (completing-read
5661             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5662             nil t nil 'gnus-method-history)))
5663       (cond 
5664        ((equal method "")
5665         (setq method gnus-select-method))
5666        ((assoc method gnus-valid-select-methods)
5667         (list method
5668               (if (memq 'prompt-address
5669                         (assoc method gnus-valid-select-methods))
5670                   (read-string "Address: ")
5671                 "")))
5672        ((assoc method gnus-server-alist)
5673         (list method))
5674        (t
5675         (list method ""))))))
5676
5677   (let* ((meth (when (and method
5678                           (not (gnus-server-equal method gnus-select-method)))
5679                  (if address (list (intern method) address)
5680                    method)))
5681          (nname (if method (gnus-group-prefixed-name name meth) name))
5682          backend info)
5683     (when (gnus-gethash nname gnus-newsrc-hashtb)
5684       (error "Group %s already exists" nname))
5685     ;; Subscribe to the new group.
5686     (gnus-group-change-level
5687      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5688      gnus-level-default-subscribed gnus-level-killed
5689      (and (gnus-group-group-name)
5690           (gnus-gethash (gnus-group-group-name)
5691                         gnus-newsrc-hashtb))
5692      t)
5693     ;; Make it active.
5694     (gnus-set-active nname (cons 1 0))
5695     (or (gnus-ephemeral-group-p name)
5696         (gnus-dribble-enter
5697          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5698     ;; Insert the line.
5699     (gnus-group-insert-group-line-info nname)
5700     (forward-line -1)
5701     (gnus-group-position-point)
5702
5703     ;; Load the backend and try to make the backend create
5704     ;; the group as well.
5705     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5706                                                   nil meth))))
5707                  gnus-valid-select-methods)
5708       (require backend))
5709     (gnus-check-server meth)
5710     (and (gnus-check-backend-function 'request-create-group nname)
5711          (gnus-request-create-group nname))
5712     t))
5713
5714 (defun gnus-group-delete-group (group &optional force)
5715   "Delete the current group.  Only meaningful with mail groups.
5716 If FORCE (the prefix) is non-nil, all the articles in the group will
5717 be deleted.  This is \"deleted\" as in \"removed forever from the face
5718 of the Earth\".  There is no undo.  The user will be prompted before
5719 doing the deletion."
5720   (interactive
5721    (list (gnus-group-group-name)
5722          current-prefix-arg))
5723   (or group (error "No group to rename"))
5724   (or (gnus-check-backend-function 'request-delete-group group)
5725       (error "This backend does not support group deletion"))
5726   (prog1
5727       (if (not (gnus-yes-or-no-p
5728                 (format
5729                  "Do you really want to delete %s%s? "
5730                  group (if force " and all its contents" ""))))
5731           () ; Whew!
5732         (gnus-message 6 "Deleting group %s..." group)
5733         (if (not (gnus-request-delete-group group force))
5734             (gnus-error 3 "Couldn't delete group %s" group)
5735           (gnus-message 6 "Deleting group %s...done" group)
5736           (gnus-group-goto-group group)
5737           (gnus-group-kill-group 1 t)
5738           (gnus-sethash group nil gnus-active-hashtb)
5739           t))
5740     (gnus-group-position-point)))
5741
5742 (defun gnus-group-rename-group (group new-name)
5743   (interactive
5744    (list
5745     (gnus-group-group-name)
5746     (progn
5747       (or (gnus-check-backend-function
5748            'request-rename-group (gnus-group-group-name))
5749           (error "This backend does not support renaming groups"))
5750       (read-string "New group name: "))))
5751
5752   (or (gnus-check-backend-function 'request-rename-group group)
5753       (error "This backend does not support renaming groups"))
5754
5755   (or group (error "No group to rename"))
5756   (and (string-match "^[ \t]*$" new-name)
5757        (error "Not a valid group name"))
5758
5759   ;; We find the proper prefixed name.
5760   (setq new-name
5761         (if (equal (gnus-group-real-name new-name) new-name)
5762             ;; Native group.
5763             new-name
5764           ;; Foreign group.
5765           (gnus-group-prefixed-name
5766            (gnus-group-real-name new-name)
5767            (gnus-info-method (gnus-get-info group)))))
5768
5769   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5770   (prog1
5771       (if (not (gnus-request-rename-group group new-name))
5772           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5773         ;; We rename the group internally by killing it...
5774         (gnus-group-goto-group group)
5775         (gnus-group-kill-group)
5776         ;; ... changing its name ...
5777         (setcar (cdar gnus-list-of-killed-groups) new-name)
5778         ;; ... and then yanking it.  Magic!
5779         (gnus-group-yank-group)
5780         (gnus-set-active new-name (gnus-active group))
5781         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5782         new-name)
5783     (gnus-group-position-point)))
5784
5785 (defun gnus-group-edit-group (group &optional part)
5786   "Edit the group on the current line."
5787   (interactive (list (gnus-group-group-name)))
5788   (let* ((part (or part 'info))
5789          (done-func `(lambda ()
5790                        "Exit editing mode and update the information."
5791                        (interactive)
5792                        (gnus-group-edit-group-done ',part ,group)))
5793          (winconf (current-window-configuration))
5794          info)
5795     (or group (error "No group on current line"))
5796     (or (setq info (gnus-get-info group))
5797         (error "Killed group; can't be edited"))
5798     (set-buffer (setq gnus-group-edit-buffer 
5799                       (get-buffer-create
5800                        (format "*Gnus edit %s*" group))))
5801     (gnus-configure-windows 'edit-group)
5802     (gnus-add-current-to-buffer-list)
5803     (emacs-lisp-mode)
5804     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5805     (use-local-map (copy-keymap emacs-lisp-mode-map))
5806     (local-set-key "\C-c\C-c" done-func)
5807     (make-local-variable 'gnus-prev-winconf)
5808     (setq gnus-prev-winconf winconf)
5809     (erase-buffer)
5810     (insert
5811      (cond
5812       ((eq part 'method)
5813        ";; Type `C-c C-c' after editing the select method.\n\n")
5814       ((eq part 'params)
5815        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5816       ((eq part 'info)
5817        ";; Type `C-c C-c' after editing the group info.\n\n")))
5818     (insert
5819      (pp-to-string
5820       (cond ((eq part 'method)
5821              (or (gnus-info-method info) "native"))
5822             ((eq part 'params)
5823              (gnus-info-params info))
5824             (t info)))
5825      "\n")))
5826
5827 (defun gnus-group-edit-group-method (group)
5828   "Edit the select method of GROUP."
5829   (interactive (list (gnus-group-group-name)))
5830   (gnus-group-edit-group group 'method))
5831
5832 (defun gnus-group-edit-group-parameters (group)
5833   "Edit the group parameters of GROUP."
5834   (interactive (list (gnus-group-group-name)))
5835   (gnus-group-edit-group group 'params))
5836
5837 (defun gnus-group-edit-group-done (part group)
5838   "Get info from buffer, update variables and jump to the group buffer."
5839   (when (and gnus-group-edit-buffer
5840              (buffer-name gnus-group-edit-buffer))
5841     (set-buffer gnus-group-edit-buffer)
5842     (goto-char (point-min))
5843     (let* ((form (read (current-buffer)))
5844            (winconf gnus-prev-winconf)
5845            (method (cond ((eq part 'info) (nth 4 form))
5846                          ((eq part 'method) form)
5847                          (t nil)))
5848            (info (cond ((eq part 'info) form)
5849                        ((eq part 'method) (gnus-get-info group))
5850                        (t nil)))
5851            (new-group (if info
5852                           (if (or (not method)
5853                                   (gnus-server-equal
5854                                    gnus-select-method method))
5855                               (gnus-group-real-name (car info))
5856                             (gnus-group-prefixed-name
5857                              (gnus-group-real-name (car info)) method))
5858                         nil)))
5859       (when (and new-group
5860                  (not (equal new-group group)))
5861         (when (gnus-group-goto-group group)
5862           (gnus-group-kill-group 1))
5863         (gnus-activate-group new-group))
5864       ;; Set the info.
5865       (if (and info new-group)
5866           (progn
5867             (setq info (gnus-copy-sequence info))
5868             (setcar info new-group)
5869             (unless (gnus-server-equal method "native")
5870               (unless (nthcdr 3 info)
5871                 (nconc info (list nil nil)))
5872               (unless (nthcdr 4 info)
5873                 (nconc info (list nil)))
5874               (gnus-info-set-method info method))
5875             (gnus-group-set-info info))
5876         (gnus-group-set-info form (or new-group group) part))
5877       (kill-buffer (current-buffer))
5878       (and winconf (set-window-configuration winconf))
5879       (set-buffer gnus-group-buffer)
5880       (gnus-group-update-group (or new-group group))
5881       (gnus-group-position-point))))
5882
5883 (defun gnus-group-make-help-group ()
5884   "Create the Gnus documentation group."
5885   (interactive)
5886   (let ((path load-path)
5887         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5888         file dir)
5889     (and (gnus-gethash name gnus-newsrc-hashtb)
5890          (error "Documentation group already exists"))
5891     (while path
5892       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5893             file nil)
5894       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5895                 (file-exists-p
5896                  (setq file (concat (file-name-directory
5897                                      (directory-file-name dir))
5898                                     "etc/gnus-tut.txt"))))
5899         (setq path nil)))
5900     (if (not file)
5901         (gnus-message 1 "Couldn't find doc group")
5902       (gnus-group-make-group
5903        (gnus-group-real-name name)
5904        (list 'nndoc "gnus-help"
5905              (list 'nndoc-address file)
5906              (list 'nndoc-article-type 'mbox)))))
5907   (gnus-group-position-point))
5908
5909 (defun gnus-group-make-doc-group (file type)
5910   "Create a group that uses a single file as the source."
5911   (interactive
5912    (list (read-file-name "File name: ")
5913          (and current-prefix-arg 'ask)))
5914   (when (eq type 'ask)
5915     (let ((err "")
5916           char found)
5917       (while (not found)
5918         (message
5919          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5920          err)
5921         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5922                           ((= char ?b) 'babyl)
5923                           ((= char ?d) 'digest)
5924                           ((= char ?f) 'forward)
5925                           ((= char ?a) 'mmfd)
5926                           (t (setq err (format "%c unknown. " char))
5927                              nil))))
5928       (setq type found)))
5929   (let* ((file (expand-file-name file))
5930          (name (gnus-generate-new-group-name
5931                 (gnus-group-prefixed-name
5932                  (file-name-nondirectory file) '(nndoc "")))))
5933     (gnus-group-make-group
5934      (gnus-group-real-name name)
5935      (list 'nndoc (file-name-nondirectory file)
5936            (list 'nndoc-address file)
5937            (list 'nndoc-article-type (or type 'guess))))))
5938
5939 (defun gnus-group-make-archive-group (&optional all)
5940   "Create the (ding) Gnus archive group of the most recent articles.
5941 Given a prefix, create a full group."
5942   (interactive "P")
5943   (let ((group (gnus-group-prefixed-name
5944                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5945     (when (gnus-gethash group gnus-newsrc-hashtb)
5946       (error "Archive group already exists"))
5947     (gnus-group-make-group
5948      (gnus-group-real-name group)
5949      (list 'nndir (if all "hpc" "edu")
5950            (list 'nndir-directory
5951                  (if all gnus-group-archive-directory
5952                    gnus-group-recent-archive-directory))))
5953     (gnus-group-add-parameter group (cons 'to-address "ding@ifi.uio.no"))))
5954
5955 (defun gnus-group-make-directory-group (dir)
5956   "Create an nndir group.
5957 The user will be prompted for a directory.  The contents of this
5958 directory will be used as a newsgroup.  The directory should contain
5959 mail messages or news articles in files that have numeric names."
5960   (interactive
5961    (list (read-file-name "Create group from directory: ")))
5962   (or (file-exists-p dir) (error "No such directory"))
5963   (or (file-directory-p dir) (error "Not a directory"))
5964   (let ((ext "")
5965         (i 0)
5966         group)
5967     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5968       (setq group
5969             (gnus-group-prefixed-name
5970              (concat (file-name-as-directory (directory-file-name dir))
5971                      ext)
5972              '(nndir "")))
5973       (setq ext (format "<%d>" (setq i (1+ i)))))
5974     (gnus-group-make-group
5975      (gnus-group-real-name group)
5976      (list 'nndir (gnus-group-real-name group) (list 'nndir-directory dir)))))
5977
5978 (defun gnus-group-make-kiboze-group (group address scores)
5979   "Create an nnkiboze group.
5980 The user will be prompted for a name, a regexp to match groups, and
5981 score file entries for articles to include in the group."
5982   (interactive
5983    (list
5984     (read-string "nnkiboze group name: ")
5985     (read-string "Source groups (regexp): ")
5986     (let ((headers (mapcar (lambda (group) (list group))
5987                            '("subject" "from" "number" "date" "message-id"
5988                              "references" "chars" "lines" "xref"
5989                              "followup" "all" "body" "head")))
5990           scores header regexp regexps)
5991       (while (not (equal "" (setq header (completing-read
5992                                           "Match on header: " headers nil t))))
5993         (setq regexps nil)
5994         (while (not (equal "" (setq regexp (read-string
5995                                             (format "Match on %s (string): "
5996                                                     header)))))
5997           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5998         (setq scores (cons (cons header regexps) scores)))
5999       scores)))
6000   (gnus-group-make-group group "nnkiboze" address)
6001   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
6002     (let (emacs-lisp-mode-hook)
6003       (pp scores (current-buffer)))))
6004
6005 (defun gnus-group-add-to-virtual (n vgroup)
6006   "Add the current group to a virtual group."
6007   (interactive
6008    (list current-prefix-arg
6009          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
6010                           "nnvirtual:")))
6011   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
6012       (error "%s is not an nnvirtual group" vgroup))
6013   (let* ((groups (gnus-group-process-prefix n))
6014          (method (gnus-info-method (gnus-get-info vgroup))))
6015     (setcar (cdr method)
6016             (concat
6017              (nth 1 method) "\\|"
6018              (mapconcat
6019               (lambda (s)
6020                 (gnus-group-remove-mark s)
6021                 (concat "\\(^" (regexp-quote s) "$\\)"))
6022               groups "\\|"))))
6023   (gnus-group-position-point))
6024
6025 (defun gnus-group-make-empty-virtual (group)
6026   "Create a new, fresh, empty virtual group."
6027   (interactive "sCreate new, empty virtual group: ")
6028   (let* ((method (list 'nnvirtual "^$"))
6029          (pgroup (gnus-group-prefixed-name group method)))
6030     ;; Check whether it exists already.
6031     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
6032          (error "Group %s already exists." pgroup))
6033     ;; Subscribe the new group after the group on the current line.
6034     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
6035     (gnus-group-update-group pgroup)
6036     (forward-line -1)
6037     (gnus-group-position-point)))
6038
6039 (defun gnus-group-enter-directory (dir)
6040   "Enter an ephemeral nneething group."
6041   (interactive "DDirectory to read: ")
6042   (let* ((method (list 'nneething dir))
6043          (leaf (gnus-group-prefixed-name
6044                 (file-name-nondirectory (directory-file-name dir))
6045                 method))
6046          (name (gnus-generate-new-group-name leaf)))
6047     (let ((nneething-read-only t))
6048       (or (gnus-group-read-ephemeral-group
6049            name method t
6050            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
6051                                       'summary 'group)))
6052           (error "Couldn't enter %s" dir)))))
6053
6054 ;; Group sorting commands
6055 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
6056
6057 (defun gnus-group-sort-groups (func &optional reverse)
6058   "Sort the group buffer according to FUNC.
6059 If REVERSE, reverse the sorting order."
6060   (interactive (list gnus-group-sort-function
6061                      current-prefix-arg))
6062   (let ((func (cond 
6063                ((not (listp func)) func)
6064                ((null func) func)
6065                ((= 1 (length func)) (car func))
6066                (t `(lambda (t1 t2)
6067                      ,(gnus-make-sort-function 
6068                        (reverse func)))))))
6069     ;; We peel off the dummy group from the alist.
6070     (when func
6071       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
6072         (pop gnus-newsrc-alist))
6073       ;; Do the sorting.
6074       (setq gnus-newsrc-alist
6075             (sort gnus-newsrc-alist func))
6076       (when reverse
6077         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
6078       ;; Regenerate the hash table.
6079       (gnus-make-hashtable-from-newsrc-alist)
6080       (gnus-group-list-groups))))
6081
6082 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
6083   "Sort the group buffer alphabetically by group name.
6084 If REVERSE, sort in reverse order."
6085   (interactive "P")
6086   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
6087
6088 (defun gnus-group-sort-groups-by-unread (&optional reverse)
6089   "Sort the group buffer by number of unread articles.
6090 If REVERSE, sort in reverse order."
6091   (interactive "P")
6092   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
6093
6094 (defun gnus-group-sort-groups-by-level (&optional reverse)
6095   "Sort the group buffer by group level.
6096 If REVERSE, sort in reverse order."
6097   (interactive "P")
6098   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
6099
6100 (defun gnus-group-sort-groups-by-score (&optional reverse)
6101   "Sort the group buffer by group score.
6102 If REVERSE, sort in reverse order."
6103   (interactive "P")
6104   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
6105
6106 (defun gnus-group-sort-groups-by-rank (&optional reverse)
6107   "Sort the group buffer by group rank.
6108 If REVERSE, sort in reverse order."
6109   (interactive "P")
6110   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
6111
6112 (defun gnus-group-sort-groups-by-method (&optional reverse)
6113   "Sort the group buffer alphabetically by backend name.
6114 If REVERSE, sort in reverse order."
6115   (interactive "P")
6116   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6117
6118 (defun gnus-group-sort-by-alphabet (info1 info2)
6119   "Sort alphabetically."
6120   (string< (gnus-info-group info1) (gnus-info-group info2)))
6121
6122 (defun gnus-group-sort-by-unread (info1 info2)
6123   "Sort by number of unread articles."
6124   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6125         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6126     (< (or (and (numberp n1) n1) 0)
6127        (or (and (numberp n2) n2) 0))))
6128
6129 (defun gnus-group-sort-by-level (info1 info2)
6130   "Sort by level."
6131   (< (gnus-info-level info1) (gnus-info-level info2)))
6132
6133 (defun gnus-group-sort-by-method (info1 info2)
6134   "Sort alphabetically by backend name."
6135   (string< (symbol-name (car (gnus-find-method-for-group
6136                               (gnus-info-group info1) info1)))
6137            (symbol-name (car (gnus-find-method-for-group
6138                               (gnus-info-group info2) info2)))))
6139
6140 (defun gnus-group-sort-by-score (info1 info2)
6141   "Sort by group score."
6142   (< (gnus-info-score info1) (gnus-info-score info2)))
6143
6144 (defun gnus-group-sort-by-rank (info1 info2)
6145   "Sort by level and score."
6146   (let ((level1 (gnus-info-level info1))
6147         (level2 (gnus-info-level info2)))
6148     (or (< level1 level2)
6149         (and (= level1 level2)
6150              (> (gnus-info-score info1) (gnus-info-score info2))))))
6151
6152 ;; Group catching up.
6153
6154 (defun gnus-group-clear-data (n)
6155   "Clear all marks and read ranges from the current group."
6156   (interactive "P")
6157   (let ((groups (gnus-group-process-prefix n))
6158         group info)
6159     (while (setq group (pop groups))
6160       (setq info (gnus-get-info group))
6161       (gnus-info-set-read info nil)
6162       (when (gnus-info-marks info)
6163         (gnus-info-set-marks info nil))
6164       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6165       (when (gnus-group-goto-group group)
6166         (gnus-group-remove-mark group)
6167         (gnus-group-update-group-line)))))
6168
6169 (defun gnus-group-catchup-current (&optional n all)
6170   "Mark all articles not marked as unread in current newsgroup as read.
6171 If prefix argument N is numeric, the ARG next newsgroups will be
6172 caught up.  If ALL is non-nil, marked articles will also be marked as
6173 read.  Cross references (Xref: header) of articles are ignored.
6174 The difference between N and actual number of newsgroups that were
6175 caught up is returned."
6176   (interactive "P")
6177   (unless (gnus-group-group-name)
6178     (error "No group on the current line"))
6179   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6180                gnus-expert-user
6181                (gnus-y-or-n-p
6182                 (if all
6183                     "Do you really want to mark all articles as read? "
6184                   "Mark all unread articles as read? "))))
6185       n
6186     (let ((groups (gnus-group-process-prefix n))
6187           (ret 0))
6188       (while groups
6189         ;; Virtual groups have to be given special treatment.
6190         (let ((method (gnus-find-method-for-group (car groups))))
6191           (if (eq 'nnvirtual (car method))
6192               (nnvirtual-catchup-group
6193                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6194         (gnus-group-remove-mark (car groups))
6195         (if (>= (gnus-group-group-level) gnus-level-zombie)
6196             (gnus-message 2 "Dead groups can't be caught up")
6197           (if (prog1
6198                   (gnus-group-goto-group (car groups))
6199                 (gnus-group-catchup (car groups) all))
6200               (gnus-group-update-group-line)
6201             (setq ret (1+ ret))))
6202         (setq groups (cdr groups)))
6203       (gnus-group-next-unread-group 1)
6204       ret)))
6205
6206 (defun gnus-group-catchup-current-all (&optional n)
6207   "Mark all articles in current newsgroup as read.
6208 Cross references (Xref: header) of articles are ignored."
6209   (interactive "P")
6210   (gnus-group-catchup-current n 'all))
6211
6212 (defun gnus-group-catchup (group &optional all)
6213   "Mark all articles in GROUP as read.
6214 If ALL is non-nil, all articles are marked as read.
6215 The return value is the number of articles that were marked as read,
6216 or nil if no action could be taken."
6217   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6218          (num (car entry)))
6219     ;; Do the updating only if the newsgroup isn't killed.
6220     (if (not (numberp (car entry)))
6221         (gnus-message 1 "Can't catch up; non-active group")
6222       ;; Do auto-expirable marks if that's required.
6223       (when (gnus-group-auto-expirable-p group)
6224         (gnus-add-marked-articles
6225          group 'expire (gnus-list-of-unread-articles group))
6226         (when all
6227           (let ((marks (nth 3 (nth 2 entry))))
6228             (gnus-add-marked-articles
6229              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6230             (gnus-add-marked-articles
6231              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6232       (when entry
6233         (gnus-update-read-articles group nil)
6234         ;; Also nix out the lists of marks and dormants.
6235         (when all
6236           (gnus-add-marked-articles group 'tick nil nil 'force)
6237           (gnus-add-marked-articles group 'dormant nil nil 'force))
6238         (run-hooks 'gnus-group-catchup-group-hook)
6239         num))))
6240
6241 (defun gnus-group-expire-articles (&optional n)
6242   "Expire all expirable articles in the current newsgroup."
6243   (interactive "P")
6244   (let ((groups (gnus-group-process-prefix n))
6245         group)
6246     (unless groups
6247       (error "No groups to expire"))
6248     (while (setq group (pop groups))
6249       (gnus-group-remove-mark group)
6250       (when (gnus-check-backend-function 'request-expire-articles group)
6251         (gnus-message 6 "Expiring articles in %s..." group)
6252         (let* ((info (gnus-get-info group))
6253                (expirable (if (gnus-group-total-expirable-p group)
6254                               (cons nil (gnus-list-of-read-articles group))
6255                             (assq 'expire (gnus-info-marks info))))
6256                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6257           (when expirable
6258             (setcdr
6259              expirable
6260              (gnus-compress-sequence
6261               (if expiry-wait
6262                   ;; We set the expiry variables to the groupp
6263                   ;; parameter. 
6264                   (let ((nnmail-expiry-wait-function nil)
6265                         (nnmail-expiry-wait expiry-wait))
6266                     (gnus-request-expire-articles
6267                      (gnus-uncompress-sequence (cdr expirable)) group))
6268                 ;; Just expire using the normal expiry values.
6269                 (gnus-request-expire-articles
6270                  (gnus-uncompress-sequence (cdr expirable)) group))))
6271             (gnus-close-group group))
6272           (gnus-message 6 "Expiring articles in %s...done" group)))
6273       (gnus-group-position-point))))
6274
6275 (defun gnus-group-expire-all-groups ()
6276   "Expire all expirable articles in all newsgroups."
6277   (interactive)
6278   (save-excursion
6279     (gnus-message 5 "Expiring...")
6280     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6281                                      (cdr gnus-newsrc-alist))))
6282       (gnus-group-expire-articles nil)))
6283   (gnus-group-position-point)
6284   (gnus-message 5 "Expiring...done"))
6285
6286 (defun gnus-group-set-current-level (n level)
6287   "Set the level of the next N groups to LEVEL."
6288   (interactive
6289    (list
6290     current-prefix-arg
6291     (string-to-int
6292      (let ((s (read-string
6293                (format "Level (default %s): "
6294                        (or (gnus-group-group-level) 
6295                            gnus-level-default-subscribed)))))
6296        (if (string-match "^\\s-*$" s)
6297            (int-to-string (or (gnus-group-group-level) 
6298                               gnus-level-default-subscribed))
6299          s)))))
6300   (or (and (>= level 1) (<= level gnus-level-killed))
6301       (error "Illegal level: %d" level))
6302   (let ((groups (gnus-group-process-prefix n))
6303         group)
6304     (while (setq group (pop groups))
6305       (gnus-group-remove-mark group)
6306       (gnus-message 6 "Changed level of %s from %d to %d"
6307                     group (or (gnus-group-group-level) gnus-level-killed)
6308                     level)
6309       (gnus-group-change-level
6310        group level (or (gnus-group-group-level) gnus-level-killed))
6311       (gnus-group-update-group-line)))
6312   (gnus-group-position-point))
6313
6314 (defun gnus-group-unsubscribe-current-group (&optional n)
6315   "Toggle subscription of the current group.
6316 If given numerical prefix, toggle the N next groups."
6317   (interactive "P")
6318   (let ((groups (gnus-group-process-prefix n))
6319         group)
6320     (while groups
6321       (setq group (car groups)
6322             groups (cdr groups))
6323       (gnus-group-remove-mark group)
6324       (gnus-group-unsubscribe-group
6325        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6326                  gnus-level-default-unsubscribed
6327                gnus-level-default-subscribed) t)
6328       (gnus-group-update-group-line))
6329     (gnus-group-next-group 1)))
6330
6331 (defun gnus-group-unsubscribe-group (group &optional level silent)
6332   "Toggle subscription to GROUP.
6333 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6334 group line."
6335   (interactive
6336    (list (completing-read
6337           "Group: " gnus-active-hashtb nil
6338           (gnus-read-active-file-p)
6339           nil 
6340           'gnus-group-history)))
6341   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6342     (cond
6343      ((string-match "^[ \t]$" group)
6344       (error "Empty group name"))
6345      (newsrc
6346       ;; Toggle subscription flag.
6347       (gnus-group-change-level
6348        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6349                                       gnus-level-subscribed)
6350                                   (1+ gnus-level-subscribed)
6351                                 gnus-level-default-subscribed)))
6352       (unless silent
6353         (gnus-group-update-group group)))
6354      ((and (stringp group)
6355            (or (not (gnus-read-active-file-p))
6356                (gnus-active group)))
6357       ;; Add new newsgroup.
6358       (gnus-group-change-level
6359        group
6360        (if level level gnus-level-default-subscribed)
6361        (or (and (member group gnus-zombie-list)
6362                 gnus-level-zombie)
6363            gnus-level-killed)
6364        (and (gnus-group-group-name)
6365             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6366       (unless silent
6367         (gnus-group-update-group group)))
6368      (t (error "No such newsgroup: %s" group)))
6369     (gnus-group-position-point)))
6370
6371 (defun gnus-group-transpose-groups (n)
6372   "Move the current newsgroup up N places.
6373 If given a negative prefix, move down instead.  The difference between
6374 N and the number of steps taken is returned."
6375   (interactive "p")
6376   (or (gnus-group-group-name)
6377       (error "No group on current line"))
6378   (gnus-group-kill-group 1)
6379   (prog1
6380       (forward-line (- n))
6381     (gnus-group-yank-group)
6382     (gnus-group-position-point)))
6383
6384 (defun gnus-group-kill-all-zombies ()
6385   "Kill all zombie newsgroups."
6386   (interactive)
6387   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6388   (setq gnus-zombie-list nil)
6389   (gnus-group-list-groups))
6390
6391 (defun gnus-group-kill-region (begin end)
6392   "Kill newsgroups in current region (excluding current point).
6393 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6394   (interactive "r")
6395   (let ((lines
6396          ;; Count lines.
6397          (save-excursion
6398            (count-lines
6399             (progn
6400               (goto-char begin)
6401               (beginning-of-line)
6402               (point))
6403             (progn
6404               (goto-char end)
6405               (beginning-of-line)
6406               (point))))))
6407     (goto-char begin)
6408     (beginning-of-line)                 ;Important when LINES < 1
6409     (gnus-group-kill-group lines)))
6410
6411 (defun gnus-group-kill-group (&optional n discard)
6412   "Kill the next N groups.
6413 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6414 However, only groups that were alive can be yanked; already killed
6415 groups or zombie groups can't be yanked.
6416 The return value is the name of the group that was killed, or a list
6417 of groups killed."
6418   (interactive "P")
6419   (let ((buffer-read-only nil)
6420         (groups (gnus-group-process-prefix n))
6421         group entry level out)
6422     (if (< (length groups) 10)
6423         ;; This is faster when there are few groups.
6424         (while groups
6425           (push (setq group (pop groups)) out)
6426           (gnus-group-remove-mark group)
6427           (setq level (gnus-group-group-level))
6428           (gnus-delete-line)
6429           (when (and (not discard)
6430                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6431             (push (cons (car entry) (nth 2 entry))
6432                   gnus-list-of-killed-groups))
6433           (gnus-group-change-level
6434            (if entry entry group) gnus-level-killed (if entry nil level)))
6435       ;; If there are lots and lots of groups to be killed, we use
6436       ;; this thing instead.
6437       (let (entry)
6438         (setq groups (nreverse groups))
6439         (while groups
6440           (gnus-group-remove-mark (setq group (pop groups)))
6441           (gnus-delete-line)
6442           (push group gnus-killed-list)
6443           (setq gnus-newsrc-alist
6444                 (delq (assoc group gnus-newsrc-alist)
6445                       gnus-newsrc-alist))
6446           (when gnus-group-change-level-function
6447             (funcall gnus-group-change-level-function group 9 3))
6448           (cond
6449            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6450             (push (cons (car entry) (nth 2 entry))
6451                   gnus-list-of-killed-groups)
6452             (setcdr (cdr entry) (cdddr entry)))
6453            ((member group gnus-zombie-list)
6454             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6455         (gnus-make-hashtable-from-newsrc-alist)))
6456
6457     (gnus-group-position-point)
6458     (if (< (length out) 2) (car out) (nreverse out))))
6459
6460 (defun gnus-group-yank-group (&optional arg)
6461   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6462 inserting it before the current newsgroup.  The numeric ARG specifies
6463 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6464 is returned, or (if several groups are yanked) a list of yanked groups
6465 is returned."
6466   (interactive "p")
6467   (setq arg (or arg 1))
6468   (let (info group prev out)
6469     (while (>= (decf arg) 0)
6470       (if (not (setq info (pop gnus-list-of-killed-groups)))
6471           (error "No more newsgroups to yank"))
6472       (push (setq group (nth 1 info)) out)
6473       ;; Find which newsgroup to insert this one before - search
6474       ;; backward until something suitable is found.  If there are no
6475       ;; other newsgroups in this buffer, just make this newsgroup the
6476       ;; first newsgroup.
6477       (setq prev (gnus-group-group-name))
6478       (gnus-group-change-level
6479        info (gnus-info-level (cdr info)) gnus-level-killed
6480        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6481        t)
6482       (gnus-group-insert-group-line-info group))
6483     (forward-line -1)
6484     (gnus-group-position-point)
6485     (if (< (length out) 2) (car out) (nreverse out))))
6486
6487 (defun gnus-group-kill-level (level)
6488   "Kill all groups that is on a certain LEVEL."
6489   (interactive "nKill all groups on level: ")
6490   (cond
6491    ((= level gnus-level-zombie)
6492     (setq gnus-killed-list
6493           (nconc gnus-zombie-list gnus-killed-list))
6494     (setq gnus-zombie-list nil))
6495    ((and (< level gnus-level-zombie)
6496          (> level 0)
6497          (or gnus-expert-user
6498              (gnus-yes-or-no-p
6499               (format
6500                "Do you really want to kill all groups on level %d? "
6501                level))))
6502     (let* ((prev gnus-newsrc-alist)
6503            (alist (cdr prev)))
6504       (while alist
6505         (if (= (gnus-info-level (car alist)) level)
6506             (progn
6507               (push (gnus-info-group (car alist)) gnus-killed-list)
6508               (setcdr prev (cdr alist)))
6509           (setq prev alist))
6510         (setq alist (cdr alist)))
6511       (gnus-make-hashtable-from-newsrc-alist)
6512       (gnus-group-list-groups)))
6513    (t
6514     (error "Can't kill; illegal level: %d" level))))
6515
6516 (defun gnus-group-list-all-groups (&optional arg)
6517   "List all newsgroups with level ARG or lower.
6518 Default is gnus-level-unsubscribed, which lists all subscribed and most
6519 unsubscribed groups."
6520   (interactive "P")
6521   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6522
6523 ;; Redefine this to list ALL killed groups if prefix arg used.
6524 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6525 (defun gnus-group-list-killed (&optional arg)
6526   "List all killed newsgroups in the group buffer.
6527 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6528 entail asking the server for the groups."
6529   (interactive "P")
6530   ;; Find all possible killed newsgroups if arg.
6531   (when arg
6532     (gnus-get-killed-groups))
6533   (if (not gnus-killed-list)
6534       (gnus-message 6 "No killed groups")
6535     (let (gnus-group-list-mode)
6536       (funcall gnus-group-prepare-function
6537                gnus-level-killed t gnus-level-killed))
6538     (goto-char (point-min)))
6539   (gnus-group-position-point))
6540
6541 (defun gnus-group-list-zombies ()
6542   "List all zombie newsgroups in the group buffer."
6543   (interactive)
6544   (if (not gnus-zombie-list)
6545       (gnus-message 6 "No zombie groups")
6546     (let (gnus-group-list-mode)
6547       (funcall gnus-group-prepare-function
6548                gnus-level-zombie t gnus-level-zombie))
6549     (goto-char (point-min)))
6550   (gnus-group-position-point))
6551
6552 (defun gnus-group-list-active ()
6553   "List all groups that are available from the server(s)."
6554   (interactive)
6555   ;; First we make sure that we have really read the active file.
6556   (unless (gnus-read-active-file-p)
6557     (let ((gnus-read-active-file t))
6558       (gnus-read-active-file)))
6559   ;; Find all groups and sort them.
6560   (let ((groups
6561          (sort
6562           (let (list)
6563             (mapatoms
6564              (lambda (sym)
6565                (and (boundp sym)
6566                     (symbol-value sym)
6567                     (setq list (cons (symbol-name sym) list))))
6568              gnus-active-hashtb)
6569             list)
6570           'string<))
6571         (buffer-read-only nil))
6572     (erase-buffer)
6573     (while groups
6574       (gnus-group-insert-group-line-info (pop groups)))
6575     (goto-char (point-min))))
6576
6577 (defun gnus-activate-all-groups (level)
6578   "Activate absolutely all groups."
6579   (interactive (list 7))
6580   (let ((gnus-activate-level level)
6581         (gnus-activate-foreign-newsgroups level))
6582     (gnus-group-get-new-news)))
6583
6584 (defun gnus-group-get-new-news (&optional arg)
6585   "Get newly arrived articles.
6586 If ARG is a number, it specifies which levels you are interested in
6587 re-scanning.  If ARG is non-nil and not a number, this will force
6588 \"hard\" re-reading of the active files from all servers."
6589   (interactive "P")
6590   (run-hooks 'gnus-get-new-news-hook)
6591   ;; We might read in new NoCeM messages here.
6592   (when (and gnus-use-nocem 
6593              (null arg))
6594     (gnus-nocem-scan-groups))
6595   ;; If ARG is not a number, then we read the active file.
6596   (when (and arg (not (numberp arg)))
6597     (let ((gnus-read-active-file t))
6598       (gnus-read-active-file))
6599     (setq arg nil))
6600
6601   (setq arg (gnus-group-default-level arg t))
6602   (if (and gnus-read-active-file (not arg))
6603       (progn
6604         (gnus-read-active-file)
6605         (gnus-get-unread-articles arg))
6606     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6607       (gnus-get-unread-articles arg)))
6608   (run-hooks 'gnus-after-getting-new-news-hook)
6609   (gnus-group-list-groups))
6610
6611 (defun gnus-group-get-new-news-this-group (&optional n)
6612   "Check for newly arrived news in the current group (and the N-1 next groups).
6613 The difference between N and the number of newsgroup checked is returned.
6614 If N is negative, this group and the N-1 previous groups will be checked."
6615   (interactive "P")
6616   (let* ((groups (gnus-group-process-prefix n))
6617          (ret (if (numberp n) (- n (length groups)) 0))
6618          (beg (unless n (point)))
6619          group)
6620     (while (setq group (pop groups))
6621       (gnus-group-remove-mark group)
6622       (if (gnus-activate-group group 'scan)
6623           (progn
6624             (gnus-get-unread-articles-in-group
6625              (gnus-get-info group) (gnus-active group) t)
6626             (unless (gnus-virtual-group-p group)
6627               (gnus-close-group group))
6628             (gnus-group-update-group group))
6629         (if (eq (gnus-server-status (gnus-find-method-for-group group))
6630                 'denied)
6631             (gnus-error "Server denied access")
6632           (gnus-error 3 "%s error: %s" group (gnus-status-message group)))))
6633     (when beg (goto-char beg))
6634     (when gnus-goto-next-group-when-activating
6635       (gnus-group-next-unread-group 1 t))
6636     (gnus-summary-position-point)
6637     ret))
6638
6639 (defun gnus-group-fetch-faq (group &optional faq-dir)
6640   "Fetch the FAQ for the current group."
6641   (interactive
6642    (list
6643     (and (gnus-group-group-name)
6644          (gnus-group-real-name (gnus-group-group-name)))
6645     (cond (current-prefix-arg
6646            (completing-read
6647             "Faq dir: " (and (listp gnus-group-faq-directory)
6648                              (mapcar (lambda (file) (list file))
6649                                      gnus-group-faq-directory)))))))
6650   (or faq-dir
6651       (setq faq-dir (if (listp gnus-group-faq-directory)
6652                         (car gnus-group-faq-directory)
6653                       gnus-group-faq-directory)))
6654   (or group (error "No group name given"))
6655   (let ((file (concat (file-name-as-directory faq-dir)
6656                       (gnus-group-real-name group))))
6657     (if (not (file-exists-p file))
6658         (error "No such file: %s" file)
6659       (find-file file))))
6660
6661 (defun gnus-group-describe-group (force &optional group)
6662   "Display a description of the current newsgroup."
6663   (interactive (list current-prefix-arg (gnus-group-group-name)))
6664   (let* ((method (gnus-find-method-for-group group))
6665          (mname (gnus-group-prefixed-name "" method))
6666          desc)
6667     (when (and force
6668                gnus-description-hashtb)
6669       (gnus-sethash mname nil gnus-description-hashtb))
6670     (or group (error "No group name given"))
6671     (and (or (and gnus-description-hashtb
6672                   ;; We check whether this group's method has been
6673                   ;; queried for a description file.
6674                   (gnus-gethash mname gnus-description-hashtb))
6675              (setq desc (gnus-group-get-description group))
6676              (gnus-read-descriptions-file method))
6677          (gnus-message 1
6678           (or desc (gnus-gethash group gnus-description-hashtb)
6679               "No description available")))))
6680
6681 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6682 (defun gnus-group-describe-all-groups (&optional force)
6683   "Pop up a buffer with descriptions of all newsgroups."
6684   (interactive "P")
6685   (and force (setq gnus-description-hashtb nil))
6686   (if (not (or gnus-description-hashtb
6687                (gnus-read-all-descriptions-files)))
6688       (error "Couldn't request descriptions file"))
6689   (let ((buffer-read-only nil)
6690         b)
6691     (erase-buffer)
6692     (mapatoms
6693      (lambda (group)
6694        (setq b (point))
6695        (insert (format "      *: %-20s %s\n" (symbol-name group)
6696                        (symbol-value group)))
6697        (gnus-add-text-properties
6698         b (1+ b) (list 'gnus-group group
6699                        'gnus-unread t 'gnus-marked nil
6700                        'gnus-level (1+ gnus-level-subscribed))))
6701      gnus-description-hashtb)
6702     (goto-char (point-min))
6703     (gnus-group-position-point)))
6704
6705 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6706 (defun gnus-group-apropos (regexp &optional search-description)
6707   "List all newsgroups that have names that match a regexp."
6708   (interactive "sGnus apropos (regexp): ")
6709   (let ((prev "")
6710         (obuf (current-buffer))
6711         groups des)
6712     ;; Go through all newsgroups that are known to Gnus.
6713     (mapatoms
6714      (lambda (group)
6715        (and (symbol-name group)
6716             (string-match regexp (symbol-name group))
6717             (setq groups (cons (symbol-name group) groups))))
6718      gnus-active-hashtb)
6719     ;; Also go through all descriptions that are known to Gnus.
6720     (when search-description
6721       (mapatoms
6722        (lambda (group)
6723          (and (string-match regexp (symbol-value group))
6724               (gnus-active (symbol-name group))
6725               (setq groups (cons (symbol-name group) groups))))
6726        gnus-description-hashtb))
6727     (if (not groups)
6728         (gnus-message 3 "No groups matched \"%s\"." regexp)
6729       ;; Print out all the groups.
6730       (save-excursion
6731         (pop-to-buffer "*Gnus Help*")
6732         (buffer-disable-undo (current-buffer))
6733         (erase-buffer)
6734         (setq groups (sort groups 'string<))
6735         (while groups
6736           ;; Groups may be entered twice into the list of groups.
6737           (if (not (string= (car groups) prev))
6738               (progn
6739                 (insert (setq prev (car groups)) "\n")
6740                 (if (and gnus-description-hashtb
6741                          (setq des (gnus-gethash (car groups)
6742                                                  gnus-description-hashtb)))
6743                     (insert "  " des "\n"))))
6744           (setq groups (cdr groups)))
6745         (goto-char (point-min))))
6746     (pop-to-buffer obuf)))
6747
6748 (defun gnus-group-description-apropos (regexp)
6749   "List all newsgroups that have names or descriptions that match a regexp."
6750   (interactive "sGnus description apropos (regexp): ")
6751   (if (not (or gnus-description-hashtb
6752                (gnus-read-all-descriptions-files)))
6753       (error "Couldn't request descriptions file"))
6754   (gnus-group-apropos regexp t))
6755
6756 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6757 (defun gnus-group-list-matching (level regexp &optional all lowest)
6758   "List all groups with unread articles that match REGEXP.
6759 If the prefix LEVEL is non-nil, it should be a number that says which
6760 level to cut off listing groups.
6761 If ALL, also list groups with no unread articles.
6762 If LOWEST, don't list groups with level lower than LOWEST.
6763
6764 This command may read the active file."
6765   (interactive "P\nsList newsgroups matching: ")
6766   ;; First make sure active file has been read.
6767   (when (and level
6768              (> (prefix-numeric-value level) gnus-level-killed))
6769     (gnus-get-killed-groups))
6770   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6771                            all (or lowest 1) regexp)
6772   (goto-char (point-min))
6773   (gnus-group-position-point))
6774
6775 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6776   "List all groups that match REGEXP.
6777 If the prefix LEVEL is non-nil, it should be a number that says which
6778 level to cut off listing groups.
6779 If LOWEST, don't list groups with level lower than LOWEST."
6780   (interactive "P\nsList newsgroups matching: ")
6781   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6782
6783 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6784 (defun gnus-group-save-newsrc (&optional force)
6785   "Save the Gnus startup files.
6786 If FORCE, force saving whether it is necessary or not."
6787   (interactive "P")
6788   (gnus-save-newsrc-file force))
6789
6790 (defun gnus-group-restart (&optional arg)
6791   "Force Gnus to read the .newsrc file."
6792   (interactive "P")
6793   (when (gnus-yes-or-no-p
6794          (format "Are you sure you want to read %s? "
6795                  gnus-current-startup-file))
6796     (gnus-save-newsrc-file)
6797     (gnus-setup-news 'force)
6798     (gnus-group-list-groups arg)))
6799
6800 (defun gnus-group-read-init-file ()
6801   "Read the Gnus elisp init file."
6802   (interactive)
6803   (gnus-read-init-file))
6804
6805 (defun gnus-group-check-bogus-groups (&optional silent)
6806   "Check bogus newsgroups.
6807 If given a prefix, don't ask for confirmation before removing a bogus
6808 group."
6809   (interactive "P")
6810   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6811   (gnus-group-list-groups))
6812
6813 (defun gnus-group-edit-global-kill (&optional article group)
6814   "Edit the global kill file.
6815 If GROUP, edit that local kill file instead."
6816   (interactive "P")
6817   (setq gnus-current-kill-article article)
6818   (gnus-kill-file-edit-file group)
6819   (gnus-message
6820    6
6821    (substitute-command-keys
6822     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6823             (if group "local" "global")))))
6824
6825 (defun gnus-group-edit-local-kill (article group)
6826   "Edit a local kill file."
6827   (interactive (list nil (gnus-group-group-name)))
6828   (gnus-group-edit-global-kill article group))
6829
6830 (defun gnus-group-force-update ()
6831   "Update `.newsrc' file."
6832   (interactive)
6833   (gnus-save-newsrc-file))
6834
6835 (defun gnus-group-suspend ()
6836   "Suspend the current Gnus session.
6837 In fact, cleanup buffers except for group mode buffer.
6838 The hook gnus-suspend-gnus-hook is called before actually suspending."
6839   (interactive)
6840   (run-hooks 'gnus-suspend-gnus-hook)
6841   ;; Kill Gnus buffers except for group mode buffer.
6842   (let* ((group-buf (get-buffer gnus-group-buffer))
6843          ;; Do this on a separate list in case the user does a ^G before we finish
6844          (gnus-buffer-list
6845           (delete group-buf (delete gnus-dribble-buffer
6846                                     (append gnus-buffer-list nil)))))
6847     (while gnus-buffer-list
6848       (gnus-kill-buffer (pop gnus-buffer-list)))
6849     (gnus-kill-gnus-frames)
6850     (when group-buf
6851       (setq gnus-buffer-list (list group-buf))
6852       (bury-buffer group-buf)
6853       (delete-windows-on group-buf t))))
6854
6855 (defun gnus-group-clear-dribble ()
6856   "Clear all information from the dribble buffer."
6857   (interactive)
6858   (gnus-dribble-clear)
6859   (gnus-message 7 "Cleared dribble buffer"))
6860
6861 (defun gnus-group-exit ()
6862   "Quit reading news after updating .newsrc.eld and .newsrc.
6863 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6864   (interactive)
6865   (when 
6866       (or noninteractive                ;For gnus-batch-kill
6867           (not gnus-interactive-exit)   ;Without confirmation
6868           gnus-expert-user
6869           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6870     (run-hooks 'gnus-exit-gnus-hook)
6871     ;; Offer to save data from non-quitted summary buffers.
6872     (gnus-offer-save-summaries)
6873     ;; Save the newsrc file(s).
6874     (gnus-save-newsrc-file)
6875     ;; Kill-em-all.
6876     (gnus-close-backends)
6877     ;; Reset everything.
6878     (gnus-clear-system)
6879     ;; Allow the user to do things after cleaning up.
6880     (run-hooks 'gnus-after-exiting-gnus-hook)))
6881
6882 (defun gnus-close-backends ()
6883   ;; Send a close request to all backends that support such a request.
6884   (let ((methods gnus-valid-select-methods)
6885         func)
6886     (while methods
6887       (if (fboundp (setq func (intern (concat (caar methods)
6888                                               "-request-close"))))
6889           (funcall func))
6890       (setq methods (cdr methods)))))
6891
6892 (defun gnus-group-quit ()
6893   "Quit reading news without updating .newsrc.eld or .newsrc.
6894 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6895   (interactive)
6896   (when (or noninteractive              ;For gnus-batch-kill
6897             (zerop (buffer-size))
6898             (not (gnus-server-opened gnus-select-method))
6899             gnus-expert-user
6900             (not gnus-current-startup-file)
6901             (gnus-yes-or-no-p
6902              (format "Quit reading news without saving %s? "
6903                      (file-name-nondirectory gnus-current-startup-file))))
6904     (run-hooks 'gnus-exit-gnus-hook)
6905     (if gnus-use-full-window
6906         (delete-other-windows)
6907       (gnus-remove-some-windows))
6908     (gnus-dribble-save)
6909     (gnus-close-backends)
6910     (gnus-clear-system)
6911     ;; Allow the user to do things after cleaning up.
6912     (run-hooks 'gnus-after-exiting-gnus-hook)))
6913
6914 (defun gnus-offer-save-summaries ()
6915   "Offer to save all active summary buffers."
6916   (save-excursion
6917     (let ((buflist (buffer-list))
6918           buffers bufname)
6919       ;; Go through all buffers and find all summaries.
6920       (while buflist
6921         (and (setq bufname (buffer-name (car buflist)))
6922              (string-match "Summary" bufname)
6923              (save-excursion
6924                (set-buffer bufname)
6925                ;; We check that this is, indeed, a summary buffer.
6926                (and (eq major-mode 'gnus-summary-mode)
6927                     ;; Also make sure this isn't bogus.
6928                     gnus-newsgroup-prepared))
6929              (push bufname buffers))
6930         (setq buflist (cdr buflist)))
6931       ;; Go through all these summary buffers and offer to save them.
6932       (when buffers
6933         (map-y-or-n-p
6934          "Update summary buffer %s? "
6935          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6936          buffers)))))
6937
6938 (defun gnus-group-describe-briefly ()
6939   "Give a one line description of the group mode commands."
6940   (interactive)
6941   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
6942
6943 (defun gnus-group-browse-foreign-server (method)
6944   "Browse a foreign news server.
6945 If called interactively, this function will ask for a select method
6946  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6947 If not, METHOD should be a list where the first element is the method
6948 and the second element is the address."
6949   (interactive
6950    (list (let ((how (completing-read
6951                      "Which backend: "
6952                      (append gnus-valid-select-methods gnus-server-alist)
6953                      nil t (cons "nntp" 0) 'gnus-method-history)))
6954            ;; We either got a backend name or a virtual server name.
6955            ;; If the first, we also need an address.
6956            (if (assoc how gnus-valid-select-methods)
6957                (list (intern how)
6958                      ;; Suggested by mapjph@bath.ac.uk.
6959                      (completing-read
6960                       "Address: "
6961                       (mapcar (lambda (server) (list server))
6962                               gnus-secondary-servers)))
6963              ;; We got a server name, so we find the method.
6964              (gnus-server-to-method how)))))
6965   (gnus-browse-foreign-server method))
6966
6967 \f
6968 ;;;
6969 ;;; Gnus summary mode
6970 ;;;
6971
6972 (defvar gnus-summary-mode-map nil)
6973
6974 (put 'gnus-summary-mode 'mode-class 'special)
6975
6976 (unless gnus-summary-mode-map
6977   (setq gnus-summary-mode-map (make-keymap))
6978   (suppress-keymap gnus-summary-mode-map)
6979
6980   ;; Non-orthogonal keys
6981
6982   (gnus-define-keys gnus-summary-mode-map
6983     " " gnus-summary-next-page
6984     "\177" gnus-summary-prev-page
6985     [delete] gnus-summary-prev-page
6986     "\r" gnus-summary-scroll-up
6987     "n" gnus-summary-next-unread-article
6988     "p" gnus-summary-prev-unread-article
6989     "N" gnus-summary-next-article
6990     "P" gnus-summary-prev-article
6991     "\M-\C-n" gnus-summary-next-same-subject
6992     "\M-\C-p" gnus-summary-prev-same-subject
6993     "\M-n" gnus-summary-next-unread-subject
6994     "\M-p" gnus-summary-prev-unread-subject
6995     "." gnus-summary-first-unread-article
6996     "," gnus-summary-best-unread-article
6997     "\M-s" gnus-summary-search-article-forward
6998     "\M-r" gnus-summary-search-article-backward
6999     "<" gnus-summary-beginning-of-article
7000     ">" gnus-summary-end-of-article
7001     "j" gnus-summary-goto-article
7002     "^" gnus-summary-refer-parent-article
7003     "\M-^" gnus-summary-refer-article
7004     "u" gnus-summary-tick-article-forward
7005     "!" gnus-summary-tick-article-forward
7006     "U" gnus-summary-tick-article-backward
7007     "d" gnus-summary-mark-as-read-forward
7008     "D" gnus-summary-mark-as-read-backward
7009     "E" gnus-summary-mark-as-expirable
7010     "\M-u" gnus-summary-clear-mark-forward
7011     "\M-U" gnus-summary-clear-mark-backward
7012     "k" gnus-summary-kill-same-subject-and-select
7013     "\C-k" gnus-summary-kill-same-subject
7014     "\M-\C-k" gnus-summary-kill-thread
7015     "\M-\C-l" gnus-summary-lower-thread
7016     "e" gnus-summary-edit-article
7017     "#" gnus-summary-mark-as-processable
7018     "\M-#" gnus-summary-unmark-as-processable
7019     "\M-\C-t" gnus-summary-toggle-threads
7020     "\M-\C-s" gnus-summary-show-thread
7021     "\M-\C-h" gnus-summary-hide-thread
7022     "\M-\C-f" gnus-summary-next-thread
7023     "\M-\C-b" gnus-summary-prev-thread
7024     "\M-\C-u" gnus-summary-up-thread
7025     "\M-\C-d" gnus-summary-down-thread
7026     "&" gnus-summary-execute-command
7027     "c" gnus-summary-catchup-and-exit
7028     "\C-w" gnus-summary-mark-region-as-read
7029     "\C-t" gnus-summary-toggle-truncation
7030     "?" gnus-summary-mark-as-dormant
7031     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
7032     "\C-c\C-s\C-n" gnus-summary-sort-by-number
7033     "\C-c\C-s\C-a" gnus-summary-sort-by-author
7034     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
7035     "\C-c\C-s\C-d" gnus-summary-sort-by-date
7036     "\C-c\C-s\C-i" gnus-summary-sort-by-score
7037     "=" gnus-summary-expand-window
7038     "\C-x\C-s" gnus-summary-reselect-current-group
7039     "\M-g" gnus-summary-rescan-group
7040     "w" gnus-summary-stop-page-breaking
7041     "\C-c\C-r" gnus-summary-caesar-message
7042     "\M-t" gnus-summary-toggle-mime
7043     "f" gnus-summary-followup
7044     "F" gnus-summary-followup-with-original
7045     "C" gnus-summary-cancel-article
7046     "r" gnus-summary-reply
7047     "R" gnus-summary-reply-with-original
7048     "\C-c\C-f" gnus-summary-mail-forward
7049     "o" gnus-summary-save-article
7050     "\C-o" gnus-summary-save-article-mail
7051     "|" gnus-summary-pipe-output
7052     "\M-k" gnus-summary-edit-local-kill
7053     "\M-K" gnus-summary-edit-global-kill
7054     "V" gnus-version
7055     "\C-c\C-d" gnus-summary-describe-group
7056     "q" gnus-summary-exit
7057     "Q" gnus-summary-exit-no-update
7058     "\C-c\C-i" gnus-info-find-node
7059     gnus-mouse-2 gnus-mouse-pick-article
7060     "m" gnus-summary-mail-other-window
7061     "a" gnus-summary-post-news
7062     "x" gnus-summary-limit-to-unread
7063     "s" gnus-summary-isearch-article
7064     "t" gnus-article-hide-headers
7065     "g" gnus-summary-show-article
7066     "l" gnus-summary-goto-last-article
7067     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
7068     "\C-d" gnus-summary-enter-digest-group
7069     "\C-c\C-b" gnus-bug
7070     "*" gnus-cache-enter-article
7071     "\M-*" gnus-cache-remove-article
7072     "\M-&" gnus-summary-universal-argument
7073     "\C-l" gnus-recenter
7074     "I" gnus-summary-increase-score
7075     "L" gnus-summary-lower-score
7076
7077     "V" gnus-summary-score-map
7078     "X" gnus-uu-extract-map
7079     "S" gnus-summary-send-map)
7080
7081   ;; Sort of orthogonal keymap
7082   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
7083     "t" gnus-summary-tick-article-forward
7084     "!" gnus-summary-tick-article-forward
7085     "d" gnus-summary-mark-as-read-forward
7086     "r" gnus-summary-mark-as-read-forward
7087     "c" gnus-summary-clear-mark-forward
7088     " " gnus-summary-clear-mark-forward
7089     "e" gnus-summary-mark-as-expirable
7090     "x" gnus-summary-mark-as-expirable
7091     "?" gnus-summary-mark-as-dormant
7092     "b" gnus-summary-set-bookmark
7093     "B" gnus-summary-remove-bookmark
7094     "#" gnus-summary-mark-as-processable
7095     "\M-#" gnus-summary-unmark-as-processable
7096     "S" gnus-summary-limit-include-expunged
7097     "C" gnus-summary-catchup
7098     "H" gnus-summary-catchup-to-here
7099     "\C-c" gnus-summary-catchup-all
7100     "k" gnus-summary-kill-same-subject-and-select
7101     "K" gnus-summary-kill-same-subject
7102     "P" gnus-uu-mark-map)
7103
7104   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
7105     "c" gnus-summary-clear-above
7106     "u" gnus-summary-tick-above
7107     "m" gnus-summary-mark-above
7108     "k" gnus-summary-kill-below)
7109
7110   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
7111     "/" gnus-summary-limit-to-subject
7112     "n" gnus-summary-limit-to-articles
7113     "w" gnus-summary-pop-limit
7114     "s" gnus-summary-limit-to-subject
7115     "a" gnus-summary-limit-to-author
7116     "u" gnus-summary-limit-to-unread
7117     "m" gnus-summary-limit-to-marks
7118     "v" gnus-summary-limit-to-score
7119     "D" gnus-summary-limit-include-dormant
7120     "d" gnus-summary-limit-exclude-dormant
7121     ;;  "t" gnus-summary-limit-exclude-thread
7122     "E" gnus-summary-limit-include-expunged
7123     "c" gnus-summary-limit-exclude-childless-dormant
7124     "C" gnus-summary-limit-mark-excluded-as-read)
7125
7126   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
7127     "n" gnus-summary-next-unread-article
7128     "p" gnus-summary-prev-unread-article
7129     "N" gnus-summary-next-article
7130     "P" gnus-summary-prev-article
7131     "\C-n" gnus-summary-next-same-subject
7132     "\C-p" gnus-summary-prev-same-subject
7133     "\M-n" gnus-summary-next-unread-subject
7134     "\M-p" gnus-summary-prev-unread-subject
7135     "f" gnus-summary-first-unread-article
7136     "b" gnus-summary-best-unread-article
7137     "j" gnus-summary-goto-article
7138     "g" gnus-summary-goto-subject
7139     "l" gnus-summary-goto-last-article
7140     "p" gnus-summary-pop-article)
7141
7142   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7143     "k" gnus-summary-kill-thread
7144     "l" gnus-summary-lower-thread
7145     "i" gnus-summary-raise-thread
7146     "T" gnus-summary-toggle-threads
7147     "t" gnus-summary-rethread-current
7148     "^" gnus-summary-reparent-thread
7149     "s" gnus-summary-show-thread
7150     "S" gnus-summary-show-all-threads
7151     "h" gnus-summary-hide-thread
7152     "H" gnus-summary-hide-all-threads
7153     "n" gnus-summary-next-thread
7154     "p" gnus-summary-prev-thread
7155     "u" gnus-summary-up-thread
7156     "o" gnus-summary-top-thread
7157     "d" gnus-summary-down-thread
7158     "#" gnus-uu-mark-thread
7159     "\M-#" gnus-uu-unmark-thread)
7160
7161   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7162     "c" gnus-summary-catchup-and-exit
7163     "C" gnus-summary-catchup-all-and-exit
7164     "E" gnus-summary-exit-no-update
7165     "Q" gnus-summary-exit
7166     "Z" gnus-summary-exit
7167     "n" gnus-summary-catchup-and-goto-next-group
7168     "R" gnus-summary-reselect-current-group
7169     "G" gnus-summary-rescan-group
7170     "N" gnus-summary-next-group
7171     "P" gnus-summary-prev-group)
7172
7173   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7174     " " gnus-summary-next-page
7175     "n" gnus-summary-next-page
7176     "\177" gnus-summary-prev-page
7177     [delete] gnus-summary-prev-page
7178     "p" gnus-summary-prev-page
7179     "\r" gnus-summary-scroll-up
7180     "<" gnus-summary-beginning-of-article
7181     ">" gnus-summary-end-of-article
7182     "b" gnus-summary-beginning-of-article
7183     "e" gnus-summary-end-of-article
7184     "^" gnus-summary-refer-parent-article
7185     "r" gnus-summary-refer-parent-article
7186     "R" gnus-summary-refer-references
7187     "g" gnus-summary-show-article
7188     "s" gnus-summary-isearch-article)
7189
7190   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7191     "b" gnus-article-add-buttons
7192     "B" gnus-article-add-buttons-to-head
7193     "o" gnus-article-treat-overstrike
7194     ;;  "w" gnus-article-word-wrap
7195     "w" gnus-article-fill-cited-article
7196     "c" gnus-article-remove-cr
7197     "L" gnus-article-remove-trailing-blank-lines
7198     "q" gnus-article-de-quoted-unreadable
7199     "f" gnus-article-display-x-face
7200     "l" gnus-summary-stop-page-breaking
7201     "r" gnus-summary-caesar-message
7202     "t" gnus-article-hide-headers
7203     "v" gnus-summary-verbose-headers
7204     "m" gnus-summary-toggle-mime)
7205
7206   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7207     "a" gnus-article-hide
7208     "h" gnus-article-hide-headers
7209     "b" gnus-article-hide-boring-headers
7210     "s" gnus-article-hide-signature
7211     "c" gnus-article-hide-citation
7212     "p" gnus-article-hide-pgp
7213     "P" gnus-article-hide-pem
7214     "\C-c" gnus-article-hide-citation-maybe)
7215
7216   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7217     "a" gnus-article-highlight
7218     "h" gnus-article-highlight-headers
7219     "c" gnus-article-highlight-citation
7220     "s" gnus-article-highlight-signature)
7221
7222   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7223     "z" gnus-article-date-ut
7224     "u" gnus-article-date-ut
7225     "l" gnus-article-date-local
7226     "e" gnus-article-date-lapsed
7227     "o" gnus-article-date-original)
7228
7229   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7230     "v" gnus-version
7231     "f" gnus-summary-fetch-faq
7232     "d" gnus-summary-describe-group
7233     "h" gnus-summary-describe-briefly
7234     "i" gnus-info-find-node)
7235
7236   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7237     "e" gnus-summary-expire-articles
7238     "\M-\C-e" gnus-summary-expire-articles-now
7239     "\177" gnus-summary-delete-article
7240     [delete] gnus-summary-delete-article
7241     "m" gnus-summary-move-article
7242     "r" gnus-summary-respool-article
7243     "w" gnus-summary-edit-article
7244     "c" gnus-summary-copy-article
7245     "B" gnus-summary-crosspost-article
7246     "q" gnus-summary-respool-query
7247     "i" gnus-summary-import-article)
7248
7249   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7250     "o" gnus-summary-save-article
7251     "m" gnus-summary-save-article-mail
7252     "r" gnus-summary-save-article-rmail
7253     "f" gnus-summary-save-article-file
7254     "b" gnus-summary-save-article-body-file
7255     "h" gnus-summary-save-article-folder
7256     "v" gnus-summary-save-article-vm
7257     "p" gnus-summary-pipe-output
7258     "s" gnus-soup-add-article)
7259   )
7260
7261 \f
7262
7263 (defun gnus-summary-mode (&optional group)
7264   "Major mode for reading articles.
7265
7266 All normal editing commands are switched off.
7267 \\<gnus-summary-mode-map>
7268 Each line in this buffer represents one article.  To read an
7269 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7270 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7271 respectively.
7272
7273 You can also post articles and send mail from this buffer.  To
7274 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7275 of an article, type `\\[gnus-summary-reply]'.
7276
7277 There are approx. one gazillion commands you can execute in this
7278 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7279
7280 The following commands are available:
7281
7282 \\{gnus-summary-mode-map}"
7283   (interactive)
7284   (when (and menu-bar-mode
7285              (gnus-visual-p 'summary-menu 'menu))
7286     (gnus-summary-make-menu-bar))
7287   (kill-all-local-variables)
7288   (gnus-summary-make-local-variables)
7289   (gnus-make-thread-indent-array)
7290   (gnus-simplify-mode-line)
7291   (setq major-mode 'gnus-summary-mode)
7292   (setq mode-name "Summary")
7293   (make-local-variable 'minor-mode-alist)
7294   (use-local-map gnus-summary-mode-map)
7295   (buffer-disable-undo (current-buffer))
7296   (setq buffer-read-only t)             ;Disable modification
7297   (setq truncate-lines t)
7298   (setq selective-display t)
7299   (setq selective-display-ellipses t)   ;Display `...'
7300   (setq buffer-display-table gnus-summary-display-table)
7301   (setq gnus-newsgroup-name group)
7302   (make-local-variable 'gnus-summary-line-format)
7303   (make-local-variable 'gnus-summary-line-format-spec)
7304   (make-local-variable 'gnus-summary-mark-positions)
7305   (gnus-make-local-hook 'post-command-hook)
7306   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
7307   (run-hooks 'gnus-summary-mode-hook))
7308
7309 (defun gnus-summary-make-local-variables ()
7310   "Make all the local summary buffer variables."
7311   (let ((locals gnus-summary-local-variables)
7312         global local)
7313     (while (setq local (pop locals))
7314       (if (consp local)
7315           (progn
7316             (if (eq (cdr local) 'global)
7317                 ;; Copy the global value of the variable.
7318                 (setq global (symbol-value (car local)))
7319               ;; Use the value from the list.
7320               (setq global (eval (cdr local))))
7321             (make-local-variable (car local))
7322             (set (car local) global))
7323         ;; Simple nil-valued local variable.
7324         (make-local-variable local)
7325         (set local nil)))))
7326
7327 (defun gnus-summary-make-display-table ()
7328   ;; Change the display table.  Odd characters have a tendency to mess
7329   ;; up nicely formatted displays - we make all possible glyphs
7330   ;; display only a single character.
7331
7332   ;; We start from the standard display table, if any.
7333   (setq gnus-summary-display-table
7334         (or (copy-sequence standard-display-table)
7335             (make-display-table)))
7336   ;; Nix out all the control chars...
7337   (let ((i 32))
7338     (while (>= (setq i (1- i)) 0)
7339       (aset gnus-summary-display-table i [??])))
7340   ;; ... but not newline and cr, of course. (cr is necessary for the
7341   ;; selective display).
7342   (aset gnus-summary-display-table ?\n nil)
7343   (aset gnus-summary-display-table ?\r nil)
7344   ;; We nix out any glyphs over 126 that are not set already.
7345   (let ((i 256))
7346     (while (>= (setq i (1- i)) 127)
7347       ;; Only modify if the entry is nil.
7348       (or (aref gnus-summary-display-table i)
7349           (aset gnus-summary-display-table i [??])))))
7350
7351 (defun gnus-summary-clear-local-variables ()
7352   (let ((locals gnus-summary-local-variables))
7353     (while locals
7354       (if (consp (car locals))
7355           (and (vectorp (caar locals))
7356                (set (caar locals) nil))
7357         (and (vectorp (car locals))
7358              (set (car locals) nil)))
7359       (setq locals (cdr locals)))))
7360
7361 ;; Summary data functions.
7362
7363 (defmacro gnus-data-number (data)
7364   `(car ,data))
7365
7366 (defmacro gnus-data-set-number (data number)
7367   `(setcar ,data ,number))
7368
7369 (defmacro gnus-data-mark (data)
7370   `(nth 1 ,data))
7371
7372 (defmacro gnus-data-set-mark (data mark)
7373   `(setcar (nthcdr 1 ,data) ,mark))
7374
7375 (defmacro gnus-data-pos (data)
7376   `(nth 2 ,data))
7377
7378 (defmacro gnus-data-set-pos (data pos)
7379   `(setcar (nthcdr 2 ,data) ,pos))
7380
7381 (defmacro gnus-data-header (data)
7382   `(nth 3 ,data))
7383
7384 (defmacro gnus-data-level (data)
7385   `(nth 4 ,data))
7386
7387 (defmacro gnus-data-unread-p (data)
7388   `(= (nth 1 ,data) gnus-unread-mark))
7389
7390 (defmacro gnus-data-pseudo-p (data)
7391   `(consp (nth 3 ,data)))
7392
7393 (defmacro gnus-data-find (number)
7394   `(assq ,number gnus-newsgroup-data))
7395
7396 (defmacro gnus-data-find-list (number &optional data)
7397   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7398      (memq (assq ,number bdata)
7399            bdata)))
7400
7401 (defmacro gnus-data-make (number mark pos header level)
7402   `(list ,number ,mark ,pos ,header ,level))
7403
7404 (defun gnus-data-enter (after-article number mark pos header level offset)
7405   (let ((data (gnus-data-find-list after-article)))
7406     (or data (error "No such article: %d" after-article))
7407     (setcdr data (cons (gnus-data-make number mark pos header level)
7408                        (cdr data)))
7409     (setq gnus-newsgroup-data-reverse nil)
7410     (gnus-data-update-list (cddr data) offset)))
7411
7412 (defun gnus-data-enter-list (after-article list &optional offset)
7413   (when list
7414     (let ((data (and after-article (gnus-data-find-list after-article)))
7415           (ilist list))
7416       (or data (not after-article) (error "No such article: %d" after-article))
7417       ;; Find the last element in the list to be spliced into the main
7418       ;; list.
7419       (while (cdr list)
7420         (setq list (cdr list)))
7421       (if (not data)
7422           (progn
7423             (setcdr list gnus-newsgroup-data)
7424             (setq gnus-newsgroup-data ilist)
7425             (and offset (gnus-data-update-list (cdr list) offset)))
7426         (setcdr list (cdr data))
7427         (setcdr data ilist)
7428         (and offset (gnus-data-update-list (cdr data) offset)))
7429       (setq gnus-newsgroup-data-reverse nil))))
7430
7431 (defun gnus-data-remove (article &optional offset)
7432   (let ((data gnus-newsgroup-data))
7433     (if (= (gnus-data-number (car data)) article)
7434         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7435               gnus-newsgroup-data-reverse nil)
7436       (while (cdr data)
7437         (and (= (gnus-data-number (cadr data)) article)
7438              (progn
7439                (setcdr data (cddr data))
7440                (and offset (gnus-data-update-list (cdr data) offset))
7441                (setq data nil
7442                      gnus-newsgroup-data-reverse nil)))
7443         (setq data (cdr data))))))
7444
7445 (defmacro gnus-data-list (backward)
7446   `(if ,backward
7447        (or gnus-newsgroup-data-reverse
7448            (setq gnus-newsgroup-data-reverse
7449                  (reverse gnus-newsgroup-data)))
7450      gnus-newsgroup-data))
7451
7452 (defun gnus-data-update-list (data offset)
7453   "Add OFFSET to the POS of all data entries in DATA."
7454   (while data
7455     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7456     (setq data (cdr data))))
7457
7458 (defun gnus-data-compute-positions ()
7459   "Compute the positions of all articles."
7460   (let ((data gnus-newsgroup-data)
7461         pos)
7462     (while data
7463       (when (setq pos (text-property-any
7464                        (point-min) (point-max)
7465                        'gnus-number (gnus-data-number (car data))))
7466         (gnus-data-set-pos (car data) (+ pos 3)))
7467       (setq data (cdr data)))))
7468
7469 (defun gnus-summary-article-pseudo-p (article)
7470   "Say whether this article is a pseudo article or not."
7471   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7472
7473 (defun gnus-article-parent-p (number)
7474   "Say whether this article is a parent or not."
7475   (let ((data (gnus-data-find-list number)))
7476     (and (cdr data)                     ; There has to be an article after...
7477          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7478             (gnus-data-level (nth 1 data))))))
7479
7480 (defun gnus-article-children (number)
7481   "Return a list of all children to NUMBER."
7482   (let* ((data (gnus-data-find-list number))
7483          (level (gnus-data-level (car data)))
7484          children)
7485     (setq data (cdr data))
7486     (while (and data            
7487                 (= (gnus-data-level (car data)) (1+ level)))
7488       (push (gnus-data-number (car data)) children)
7489       (setq data (cdr data)))
7490     children))
7491
7492 (defmacro gnus-summary-skip-intangible ()
7493   "If the current article is intangible, then jump to a different article."
7494   '(let ((to (get-text-property (point) 'gnus-intangible)))
7495     (and to (gnus-summary-goto-subject to))))
7496
7497 (defmacro gnus-summary-article-intangible-p ()
7498   "Say whether this article is intangible or not."
7499   '(get-text-property (point) 'gnus-intangible))
7500
7501 ;; Some summary mode macros.
7502
7503 (defmacro gnus-summary-article-number ()
7504   "The article number of the article on the current line.
7505 If there isn's an article number here, then we return the current
7506 article number."
7507   '(progn
7508      (gnus-summary-skip-intangible)
7509      (or (get-text-property (point) 'gnus-number)
7510          (gnus-summary-last-subject))))
7511
7512 (defmacro gnus-summary-article-header (&optional number)
7513   `(gnus-data-header (gnus-data-find
7514                       ,(or number '(gnus-summary-article-number)))))
7515
7516 (defmacro gnus-summary-thread-level (&optional number)
7517   `(if (and (eq gnus-summary-make-false-root 'dummy)
7518             (get-text-property (point) 'gnus-intangible))
7519        0
7520      (gnus-data-level (gnus-data-find
7521                        ,(or number '(gnus-summary-article-number))))))
7522
7523 (defmacro gnus-summary-article-mark (&optional number)
7524   `(gnus-data-mark (gnus-data-find
7525                     ,(or number '(gnus-summary-article-number)))))
7526
7527 (defmacro gnus-summary-article-pos (&optional number)
7528   `(gnus-data-pos (gnus-data-find
7529                    ,(or number '(gnus-summary-article-number)))))
7530
7531 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7532 (defmacro gnus-summary-article-subject (&optional number)
7533   "Return current subject string or nil if nothing."
7534   `(let ((headers
7535           ,(if number
7536                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7537              '(gnus-data-header (assq (gnus-summary-article-number)
7538                                       gnus-newsgroup-data)))))
7539      (and headers
7540           (vectorp headers)
7541           (mail-header-subject headers))))
7542
7543 (defmacro gnus-summary-article-score (&optional number)
7544   "Return current article score."
7545   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7546                   gnus-newsgroup-scored))
7547        gnus-summary-default-score 0))
7548
7549 (defun gnus-summary-article-children (&optional number)
7550   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7551          (level (gnus-data-level (car data)))
7552          l children)
7553     (while (and (setq data (cdr data))
7554                 (> (setq l (gnus-data-level (car data))) level))
7555       (and (= (1+ level) l)
7556            (setq children (cons (gnus-data-number (car data))
7557                                 children))))
7558     (nreverse children)))
7559
7560 (defun gnus-summary-article-parent (&optional number)
7561   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7562                                     (gnus-data-list t)))
7563          (level (gnus-data-level (car data))))
7564     (if (zerop level)
7565         () ; This is a root.
7566       ;; We search until we find an article with a level less than
7567       ;; this one.  That function has to be the parent.
7568       (while (and (setq data (cdr data))
7569                   (not (< (gnus-data-level (car data)) level))))
7570       (and data (gnus-data-number (car data))))))
7571
7572 (defun gnus-unread-mark-p (mark)
7573   "Say whether MARK is the unread mark."
7574   (= mark gnus-unread-mark))
7575
7576 (defun gnus-read-mark-p (mark)
7577   "Say whether MARK is one of the marks that mark as read.
7578 This is all marks except unread, ticked, dormant, and expirable."
7579   (not (or (= mark gnus-unread-mark)
7580            (= mark gnus-ticked-mark)
7581            (= mark gnus-dormant-mark)
7582            (= mark gnus-expirable-mark))))
7583
7584 ;; Saving hidden threads.
7585
7586 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7587 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7588 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7589
7590 (defmacro gnus-save-hidden-threads (&rest forms)
7591   "Save hidden threads, eval FORMS, and restore the hidden threads."
7592   (let ((config (make-symbol "config")))
7593     `(let ((,config (gnus-hidden-threads-configuration)))
7594        (unwind-protect
7595            (progn
7596              ,@forms)
7597          (gnus-restore-hidden-threads-configuration ,config)))))
7598
7599 (defun gnus-hidden-threads-configuration ()
7600   "Return the current hidden threads configuration."
7601   (save-excursion
7602     (let (config)
7603       (goto-char (point-min))
7604       (while (search-forward "\r" nil t)
7605         (push (1- (point)) config))
7606       config)))
7607
7608 (defun gnus-restore-hidden-threads-configuration (config)
7609   "Restore hidden threads configuration from CONFIG."
7610   (let (point buffer-read-only)
7611     (while (setq point (pop config))
7612       (when (and (< point (point-max))
7613                  (goto-char point)
7614                  (= (following-char) ?\n))
7615         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7616
7617 ;; Various summary mode internalish functions.
7618
7619 (defun gnus-mouse-pick-article (e)
7620   (interactive "e")
7621   (mouse-set-point e)
7622   (gnus-summary-next-page nil t))
7623
7624 (defun gnus-summary-setup-buffer (group)
7625   "Initialize summary buffer."
7626   (let ((buffer (concat "*Summary " group "*")))
7627     (if (get-buffer buffer)
7628         (progn
7629           (set-buffer buffer)
7630           (setq gnus-summary-buffer (current-buffer))
7631           (not gnus-newsgroup-prepared))
7632       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7633       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7634       (gnus-add-current-to-buffer-list)
7635       (gnus-summary-mode group)
7636       (when gnus-carpal
7637         (gnus-carpal-setup-buffer 'summary))
7638       (unless gnus-single-article-buffer
7639         (make-local-variable 'gnus-article-buffer)
7640         (make-local-variable 'gnus-article-current)
7641         (make-local-variable 'gnus-original-article-buffer))
7642       (setq gnus-newsgroup-name group)
7643       t)))
7644
7645 (defun gnus-set-global-variables ()
7646   ;; Set the global equivalents of the summary buffer-local variables
7647   ;; to the latest values they had.  These reflect the summary buffer
7648   ;; that was in action when the last article was fetched.
7649   (when (eq major-mode 'gnus-summary-mode)
7650     (setq gnus-summary-buffer (current-buffer))
7651     (let ((name gnus-newsgroup-name)
7652           (marked gnus-newsgroup-marked)
7653           (unread gnus-newsgroup-unreads)
7654           (headers gnus-current-headers)
7655           (data gnus-newsgroup-data)
7656           (summary gnus-summary-buffer)
7657           (article-buffer gnus-article-buffer)
7658           (original gnus-original-article-buffer)
7659           (gac gnus-article-current)
7660           (reffed gnus-reffed-article-number)
7661           (score-file gnus-current-score-file))
7662       (save-excursion
7663         (set-buffer gnus-group-buffer)
7664         (setq gnus-newsgroup-name name)
7665         (setq gnus-newsgroup-marked marked)
7666         (setq gnus-newsgroup-unreads unread)
7667         (setq gnus-current-headers headers)
7668         (setq gnus-newsgroup-data data)
7669         (setq gnus-article-current gac)
7670         (setq gnus-summary-buffer summary)
7671         (setq gnus-article-buffer article-buffer)
7672         (setq gnus-original-article-buffer original)
7673         (setq gnus-reffed-article-number reffed)
7674         (setq gnus-current-score-file score-file)))))
7675
7676 (defun gnus-summary-last-article-p (&optional article)
7677   "Return whether ARTICLE is the last article in the buffer."
7678   (if (not (setq article (or article (gnus-summary-article-number))))
7679       t ; All non-existant numbers are the last article. :-)
7680     (not (cdr (gnus-data-find-list article)))))
7681
7682 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7683   "Insert a dummy root in the summary buffer."
7684   (beginning-of-line)
7685   (gnus-add-text-properties
7686    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7687    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7688
7689 (defun gnus-make-thread-indent-array ()
7690   (let ((n 200))
7691     (unless (and gnus-thread-indent-array
7692                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7693       (setq gnus-thread-indent-array (make-vector 201 "")
7694             gnus-thread-indent-array-level gnus-thread-indent-level)
7695       (while (>= n 0)
7696         (aset gnus-thread-indent-array n
7697               (make-string (* n gnus-thread-indent-level) ? ))
7698         (setq n (1- n))))))
7699
7700 (defun gnus-summary-insert-line
7701   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7702                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7703                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7704   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7705          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7706          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7707          (gnus-tmp-score-char
7708           (if (or (null gnus-summary-default-score)
7709                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7710                       gnus-summary-zcore-fuzz)) ? 
7711             (if (< gnus-tmp-score gnus-summary-default-score)
7712                 gnus-score-below-mark gnus-score-over-mark)))
7713          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7714                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7715                                   gnus-cached-mark)
7716                                  (gnus-tmp-replied gnus-replied-mark)
7717                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7718                                   gnus-saved-mark)
7719                                  (t gnus-unread-mark)))
7720          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7721          (gnus-tmp-name
7722           (cond
7723            ((string-match "(.+)" gnus-tmp-from)
7724             (substring gnus-tmp-from
7725                        (1+ (match-beginning 0)) (1- (match-end 0))))
7726            ((string-match "<[^>]+> *$" gnus-tmp-from)
7727             (let ((beg (match-beginning 0)))
7728               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7729                        (substring gnus-tmp-from (1+ (match-beginning 0))
7730                                   (1- (match-end 0))))
7731                   (substring gnus-tmp-from 0 beg))))
7732            (t gnus-tmp-from)))
7733          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7734          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7735          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7736          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7737          (buffer-read-only nil))
7738     (when (string= gnus-tmp-name "")
7739       (setq gnus-tmp-name gnus-tmp-from))
7740     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7741     (gnus-put-text-property
7742      (point)
7743      (progn (eval gnus-summary-line-format-spec) (point))
7744      'gnus-number gnus-tmp-number)
7745     (when (gnus-visual-p 'summary-highlight 'highlight)
7746       (forward-line -1)
7747       (run-hooks 'gnus-summary-update-hook)
7748       (forward-line 1))))
7749
7750 (defun gnus-summary-update-line (&optional dont-update)
7751   ;; Update summary line after change.
7752   (when (and gnus-summary-default-score
7753              (not gnus-summary-inhibit-highlight))
7754     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7755            (article (gnus-summary-article-number))
7756            (score (gnus-summary-article-score article)))
7757       (unless dont-update
7758         (if (and gnus-summary-mark-below
7759                  (< (gnus-summary-article-score)
7760                     gnus-summary-mark-below))
7761             ;; This article has a low score, so we mark it as read.
7762             (when (memq article gnus-newsgroup-unreads)
7763               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7764           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7765             ;; This article was previously marked as read on account
7766             ;; of a low score, but now it has risen, so we mark it as
7767             ;; unread.
7768             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7769         (gnus-summary-update-mark
7770          (if (or (null gnus-summary-default-score)
7771                  (<= (abs (- score gnus-summary-default-score))
7772                      gnus-summary-zcore-fuzz)) ? 
7773            (if (< score gnus-summary-default-score)
7774                gnus-score-below-mark gnus-score-over-mark)) 'score))
7775       ;; Do visual highlighting.
7776       (when (gnus-visual-p 'summary-highlight 'highlight)
7777         (run-hooks 'gnus-summary-update-hook)))))
7778
7779 (defvar gnus-tmp-new-adopts nil)
7780
7781 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7782   ;; Sum up all elements (and sub-elements) in a list.
7783   (let* ((number
7784           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7785           (cond
7786            ((and (consp thread) (cdr thread))
7787             (apply
7788              '+ 1 (mapcar
7789                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7790            ((null thread)
7791             1)
7792            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7793             1)
7794            (t 0))))
7795     (when (and level (zerop level) gnus-tmp-new-adopts)
7796       (incf number
7797             (apply '+ (mapcar
7798                        'gnus-summary-number-of-articles-in-thread
7799                        gnus-tmp-new-adopts))))
7800     (if char
7801         (if (> number 1) gnus-not-empty-thread-mark
7802           gnus-empty-thread-mark)
7803       number)))
7804
7805 (defun gnus-summary-set-local-parameters (group)
7806  "Go through the local params of GROUP and set all variable specs in that list."
7807   (let ((params (gnus-info-params (gnus-get-info group)))
7808         elem)
7809     (while params
7810       (setq elem (car params)
7811             params (cdr params))
7812       (and (consp elem)                 ; Has to be a cons.
7813            (consp (cdr elem))           ; The cdr has to be a list.
7814            (symbolp (car elem))         ; Has to be a symbol in there.
7815            (not (memq (car elem) 
7816                       '(quit-config to-address to-list to-group)))
7817            (progn                       ; So we set it.
7818              (make-local-variable (car elem))
7819              (set (car elem) (eval (nth 1 elem))))))))
7820
7821 (defun gnus-summary-read-group (group &optional show-all no-article
7822                                       kill-buffer no-display)
7823   "Start reading news in newsgroup GROUP.
7824 If SHOW-ALL is non-nil, already read articles are also listed.
7825 If NO-ARTICLE is non-nil, no article is selected initially.
7826 If NO-DISPLAY, don't generate a summary buffer."
7827   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7828   (let* ((new-group (gnus-summary-setup-buffer group))
7829          (quit-config (gnus-group-quit-config group))
7830          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7831     (cond
7832      ;; This summary buffer exists already, so we just select it.
7833      ((not new-group)
7834       (gnus-set-global-variables)
7835       (when kill-buffer
7836         (gnus-kill-or-deaden-summary kill-buffer))
7837       (gnus-configure-windows 'summary 'force)
7838       (gnus-set-mode-line 'summary)
7839       (gnus-summary-position-point)
7840       (message "")
7841       t)
7842      ;; We couldn't select this group.
7843      ((null did-select)
7844       (when (and (eq major-mode 'gnus-summary-mode)
7845                  (not (equal (current-buffer) kill-buffer)))
7846         (kill-buffer (current-buffer))
7847         (if (not quit-config)
7848             (progn
7849               (set-buffer gnus-group-buffer)
7850               (gnus-group-jump-to-group group)
7851               (gnus-group-next-unread-group 1))
7852           (if (not (buffer-name (car quit-config)))
7853               (gnus-configure-windows 'group 'force)
7854             (set-buffer (car quit-config))
7855             (and (eq major-mode 'gnus-summary-mode)
7856                  (gnus-set-global-variables))
7857             (gnus-configure-windows (cdr quit-config)))))
7858       (gnus-message 3 "Can't select group")
7859       nil)
7860      ;; The user did a `C-g' while prompting for number of articles,
7861      ;; so we exit this group.
7862      ((eq did-select 'quit)
7863       (and (eq major-mode 'gnus-summary-mode)
7864            (not (equal (current-buffer) kill-buffer))
7865            (kill-buffer (current-buffer)))
7866       (when kill-buffer
7867         (gnus-kill-or-deaden-summary kill-buffer))
7868       (if (not quit-config)
7869           (progn
7870             (set-buffer gnus-group-buffer)
7871             (gnus-group-jump-to-group group)
7872             (gnus-group-next-unread-group 1)
7873             (gnus-configure-windows 'group 'force))
7874         (if (not (buffer-name (car quit-config)))
7875             (gnus-configure-windows 'group 'force)
7876           (set-buffer (car quit-config))
7877           (and (eq major-mode 'gnus-summary-mode)
7878                (gnus-set-global-variables))
7879           (gnus-configure-windows (cdr quit-config))))
7880       ;; Finally signal the quit.
7881       (signal 'quit nil))
7882      ;; The group was successfully selected.
7883      (t
7884       (gnus-set-global-variables)
7885       ;; Save the active value in effect when the group was entered.
7886       (setq gnus-newsgroup-active
7887             (gnus-copy-sequence
7888              (gnus-active gnus-newsgroup-name)))
7889       ;; You can change the summary buffer in some way with this hook.
7890       (run-hooks 'gnus-select-group-hook)
7891       ;; Set any local variables in the group parameters.
7892       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7893       (gnus-update-format-specifications)
7894       ;; Do score processing.
7895       (when gnus-use-scoring
7896         (gnus-possibly-score-headers))
7897       ;; Check whether to fill in the gaps in the threads.
7898       (when gnus-build-sparse-threads
7899         (gnus-build-sparse-threads))
7900       ;; Find the initial limit.
7901       (if gnus-show-threads
7902           (if show-all
7903               (let ((gnus-newsgroup-dormant nil))
7904                 (gnus-summary-initial-limit show-all))
7905             (gnus-summary-initial-limit show-all))
7906         (setq gnus-newsgroup-limit 
7907               (mapcar 
7908                (lambda (header) (mail-header-number header))
7909                gnus-newsgroup-headers)))
7910       ;; Generate the summary buffer.
7911       (unless no-display
7912         (gnus-summary-prepare))
7913       (when gnus-use-trees
7914         (gnus-tree-open group)
7915         (setq gnus-summary-highlight-line-function
7916               'gnus-tree-highlight-article))
7917       ;; If the summary buffer is empty, but there are some low-scored
7918       ;; articles or some excluded dormants, we include these in the
7919       ;; buffer.
7920       (when (and (zerop (buffer-size))
7921                  (not no-display))
7922         (cond (gnus-newsgroup-dormant
7923                (gnus-summary-limit-include-dormant))
7924               ((and gnus-newsgroup-scored show-all)
7925                (gnus-summary-limit-include-expunged t))))
7926       ;; Function `gnus-apply-kill-file' must be called in this hook.
7927       (run-hooks 'gnus-apply-kill-hook)
7928       (if (and (zerop (buffer-size))
7929                (not no-display))
7930           (progn
7931             ;; This newsgroup is empty.
7932             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7933             (gnus-message 6 "No unread news")
7934             (when kill-buffer
7935               (gnus-kill-or-deaden-summary kill-buffer))
7936             ;; Return nil from this function.
7937             nil)
7938         ;; Hide conversation thread subtrees.  We cannot do this in
7939         ;; gnus-summary-prepare-hook since kill processing may not
7940         ;; work with hidden articles.
7941         (and gnus-show-threads
7942              gnus-thread-hide-subtree
7943              (gnus-summary-hide-all-threads))
7944         ;; Show first unread article if requested.
7945         (if (and (not no-article)
7946                  (not no-display)
7947                  gnus-newsgroup-unreads
7948                  gnus-auto-select-first)
7949             (unless (if (eq gnus-auto-select-first 'best)
7950                         (gnus-summary-best-unread-article)
7951                       (gnus-summary-first-unread-article))
7952               (gnus-configure-windows 'summary))
7953           ;; Don't select any articles, just move point to the first
7954           ;; article in the group.
7955           (goto-char (point-min))
7956           (gnus-summary-position-point)
7957           (gnus-set-mode-line 'summary)
7958           (gnus-configure-windows 'summary 'force))
7959         ;; If we are in async mode, we send some info to the backend.
7960         (when gnus-newsgroup-async
7961           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7962         (when kill-buffer
7963           (gnus-kill-or-deaden-summary kill-buffer))
7964         (when (get-buffer-window gnus-group-buffer t)
7965           ;; Gotta use windows, because recenter does wierd stuff if
7966           ;; the current buffer ain't the displayed window.
7967           (let ((owin (selected-window)))
7968             (select-window (get-buffer-window gnus-group-buffer t))
7969             (when (gnus-group-goto-group group)
7970               (recenter))
7971             (select-window owin))))
7972       ;; Mark this buffer as "prepared".
7973       (setq gnus-newsgroup-prepared t)
7974       t))))
7975
7976 (defun gnus-summary-prepare ()
7977   "Generate the summary buffer."
7978   (let ((buffer-read-only nil))
7979     (erase-buffer)
7980     (setq gnus-newsgroup-data nil
7981           gnus-newsgroup-data-reverse nil)
7982     (run-hooks 'gnus-summary-generate-hook)
7983     ;; Generate the buffer, either with threads or without.
7984     (when gnus-newsgroup-headers
7985       (gnus-summary-prepare-threads
7986        (if gnus-show-threads
7987            (gnus-sort-gathered-threads
7988             (funcall gnus-summary-thread-gathering-function
7989                      (gnus-sort-threads
7990                       (gnus-cut-threads (gnus-make-threads)))))
7991          ;; Unthreaded display.
7992          (gnus-sort-articles gnus-newsgroup-headers))))
7993     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7994     ;; Call hooks for modifying summary buffer.
7995     (goto-char (point-min))
7996     (run-hooks 'gnus-summary-prepare-hook)))
7997
7998 (defun gnus-gather-threads-by-subject (threads)
7999   "Gather threads by looking at Subject headers."
8000   (if (not gnus-summary-make-false-root)
8001       threads
8002     (let ((hashtb (gnus-make-hashtable 1023))
8003           (prev threads)
8004           (result threads)
8005           subject hthread whole-subject)
8006       (while threads
8007         (setq whole-subject (mail-header-subject (caar threads)))
8008         (setq subject
8009               (cond
8010                ;; Truncate the subject.
8011                ((numberp gnus-summary-gather-subject-limit)
8012                 (setq subject (gnus-simplify-subject-re whole-subject))
8013                 (if (> (length subject) gnus-summary-gather-subject-limit)
8014                     (substring subject 0 gnus-summary-gather-subject-limit)
8015                   subject))
8016                ;; Fuzzily simplify it.
8017                ((eq 'fuzzy gnus-summary-gather-subject-limit)
8018                 (gnus-simplify-subject-fuzzy whole-subject))
8019                ;; Just remove the leading "Re:".
8020                (t
8021                 (gnus-simplify-subject-re whole-subject))))
8022
8023         (if (and gnus-summary-gather-exclude-subject
8024                  (string-match gnus-summary-gather-exclude-subject
8025                                subject))
8026             ()          ; We don't want to do anything with this article.
8027           ;; We simplify the subject before looking it up in the
8028           ;; hash table.
8029
8030           (if (setq hthread (gnus-gethash subject hashtb))
8031               (progn
8032                 ;; We enter a dummy root into the thread, if we
8033                 ;; haven't done that already.
8034                 (unless (stringp (caar hthread))
8035                   (setcar hthread (list whole-subject (car hthread))))
8036                 ;; We add this new gathered thread to this gathered
8037                 ;; thread.
8038                 (setcdr (car hthread)
8039                         (nconc (cdar hthread) (list (car threads))))
8040                 ;; Remove it from the list of threads.
8041                 (setcdr prev (cdr threads))
8042                 (setq threads prev))
8043             ;; Enter this thread into the hash table.
8044             (gnus-sethash subject threads hashtb)))
8045         (setq prev threads)
8046         (setq threads (cdr threads)))
8047       result)))
8048
8049 (defun gnus-gather-threads-by-references (threads)
8050   "Gather threads by looking at References headers."
8051   (let ((idhashtb (gnus-make-hashtable 1023))
8052         (thhashtb (gnus-make-hashtable 1023))
8053         (prev threads)
8054         (result threads)
8055         ids references id gthread gid entered)
8056     (while threads
8057       (when (setq references (mail-header-references (caar threads)))
8058         (setq id (mail-header-id (caar threads)))
8059         (setq ids (gnus-split-references references))
8060         (setq entered nil)
8061         (while ids
8062           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
8063               (progn
8064                 (gnus-sethash (car ids) id idhashtb)
8065                 (gnus-sethash id threads thhashtb))
8066             (setq gthread (gnus-gethash gid thhashtb))
8067             (unless entered
8068               ;; We enter a dummy root into the thread, if we
8069               ;; haven't done that already.
8070               (unless (stringp (caar gthread))
8071                 (setcar gthread (list (mail-header-subject (caar gthread))
8072                                       (car gthread))))
8073               ;; We add this new gathered thread to this gathered
8074               ;; thread.
8075               (setcdr (car gthread)
8076                       (nconc (cdar gthread) (list (car threads)))))
8077             ;; Add it into the thread hash table.
8078             (gnus-sethash id gthread thhashtb)
8079             (setq entered t)
8080             ;; Remove it from the list of threads.
8081             (setcdr prev (cdr threads))
8082             (setq threads prev))
8083           (setq ids (cdr ids))))
8084       (setq prev threads)
8085       (setq threads (cdr threads)))
8086     result))
8087
8088 (defun gnus-sort-gathered-threads (threads)
8089   "Sort subtreads inside each gathered thread by article number."
8090   (let ((result threads))
8091     (while threads
8092       (when (stringp (caar threads))
8093         (setcdr (car threads)
8094                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
8095       (setq threads (cdr threads)))
8096     result))
8097
8098 (defun gnus-make-threads ()
8099   "Go through the dependency hashtb and find the roots.  Return all threads."
8100   (let (threads)
8101     (mapatoms
8102      (lambda (refs)
8103        (unless (car (symbol-value refs))
8104          ;; These threads do not refer back to any other articles,
8105          ;; so they're roots.
8106          (setq threads (append (cdr (symbol-value refs)) threads))))
8107      gnus-newsgroup-dependencies)
8108     threads))
8109
8110 (defun gnus-build-sparse-threads ()
8111   (let ((headers gnus-newsgroup-headers)
8112         (deps gnus-newsgroup-dependencies)
8113         header references generation relations 
8114         cthread subject child end pthread relation)
8115     ;; First we create an alist of generations/relations, where 
8116     ;; generations is how much we trust the ralation, and the relation
8117     ;; is parent/child.
8118     (gnus-message 7 "Making sparse threads...")
8119     (save-excursion
8120       (nnheader-set-temp-buffer " *gnus sparse threads*")
8121       (while (setq header (pop headers))
8122         (when (and (setq references (mail-header-references header))
8123                    (not (string= references "")))
8124           (insert references)
8125           (setq child (mail-header-id header)
8126                 subject (mail-header-subject header))
8127           (setq generation 0)
8128           (while (search-backward ">" nil t)
8129             (setq end (1+ (point)))
8130             (when (search-backward "<" nil t)
8131               (push (list (incf generation) 
8132                           child (setq child (buffer-substring (point) end))
8133                           subject)
8134                     relations)))
8135           (push (list (1+ generation) child nil subject) relations)
8136           (erase-buffer)))
8137       (kill-buffer (current-buffer)))
8138     ;; Sort over trustworthiness.
8139     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8140     (while (setq relation (pop relations))
8141       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8142                 (unless (car (symbol-value cthread))
8143                   ;; Make this article the parent of these threads.
8144                   (setcar (symbol-value cthread)
8145                           (vector gnus-reffed-article-number 
8146                                   (cadddr relation) 
8147                                   "" ""
8148                                   (cadr relation) 
8149                                   (or (caddr relation) "") 0 0 "")))
8150               (set cthread (list (vector gnus-reffed-article-number
8151                                          (cadddr relation) 
8152                                          "" "" (cadr relation) 
8153                                          (or (caddr relation) "") 0 0 ""))))
8154         (push gnus-reffed-article-number gnus-newsgroup-limit)
8155         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8156         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8157               gnus-newsgroup-reads)
8158         (decf gnus-reffed-article-number)
8159         ;; Make this new thread the child of its parent.
8160         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8161             (setcdr (symbol-value pthread)
8162                     (nconc (cdr (symbol-value pthread))
8163                            (list (symbol-value cthread))))
8164           (set pthread (list nil (symbol-value cthread))))))
8165     (gnus-message 7 "Making sparse threads...done")))
8166
8167 (defun gnus-build-old-threads ()
8168   ;; Look at all the articles that refer back to old articles, and
8169   ;; fetch the headers for the articles that aren't there.  This will
8170   ;; build complete threads - if the roots haven't been expired by the
8171   ;; server, that is.
8172   (let (id heads)
8173     (mapatoms
8174      (lambda (refs)
8175        (when (not (car (symbol-value refs)))
8176          (setq heads (cdr (symbol-value refs)))
8177          (while heads
8178            (if (memq (mail-header-number (caar heads))
8179                      gnus-newsgroup-dormant)
8180                (setq heads (cdr heads))
8181              (setq id (symbol-name refs))
8182              (while (and (setq id (gnus-build-get-header id))
8183                          (not (car (gnus-gethash
8184                                     id gnus-newsgroup-dependencies)))))
8185              (setq heads nil)))))
8186      gnus-newsgroup-dependencies)))
8187
8188 (defun gnus-build-get-header (id)
8189   ;; Look through the buffer of NOV lines and find the header to
8190   ;; ID.  Enter this line into the dependencies hash table, and return
8191   ;; the id of the parent article (if any).
8192   (let ((deps gnus-newsgroup-dependencies)
8193         found header)
8194     (prog1
8195         (save-excursion
8196           (set-buffer nntp-server-buffer)
8197           (goto-char (point-min))
8198           (while (and (not found) (search-forward id nil t))
8199             (beginning-of-line)
8200             (setq found (looking-at
8201                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8202                                  (regexp-quote id))))
8203             (or found (beginning-of-line 2)))
8204           (when found
8205             (beginning-of-line)
8206             (and
8207              (setq header (gnus-nov-parse-line
8208                            (read (current-buffer)) deps))
8209              (gnus-parent-id (mail-header-references header)))))
8210       (when header
8211         (let ((number (mail-header-number header)))
8212           (push number gnus-newsgroup-limit)
8213           (push header gnus-newsgroup-headers)
8214           (if (memq number gnus-newsgroup-unselected)
8215               (progn
8216                 (push number gnus-newsgroup-unreads)
8217                 (setq gnus-newsgroup-unselected
8218                       (delq number gnus-newsgroup-unselected)))
8219             (push number gnus-newsgroup-ancient)))))))
8220
8221 (defun gnus-summary-update-article (article &optional iheader)
8222   "Update ARTICLE in the summary buffer."
8223   (set-buffer gnus-summary-buffer)
8224   (let* ((header (or iheader (gnus-summary-article-header article)))
8225          (id (mail-header-id header))
8226          (data (gnus-data-find article))
8227          (thread (gnus-id-to-thread id))
8228          (references (mail-header-references header))
8229          (parent
8230           (gnus-id-to-thread
8231            (or (gnus-parent-id 
8232                 (if (and references
8233                          (not (equal "" references)))
8234                     references))
8235                "none")))
8236          (buffer-read-only nil)
8237          (old (car thread))
8238          (number (mail-header-number header))
8239          pos)
8240     (when thread
8241       ;; !!! Should this be in or not?
8242       (unless iheader
8243         (setcar thread nil))
8244       (when parent
8245         (delq thread parent))
8246       (if (gnus-summary-insert-subject id header iheader)
8247           ;; Set the (possibly) new article number in the data structure.
8248           (gnus-data-set-number data (gnus-id-to-article id))
8249         (setcar thread old)
8250         nil))))
8251
8252 (defun gnus-rebuild-thread (id)
8253   "Rebuild the thread containing ID."
8254   (let ((buffer-read-only nil)
8255         current thread data)
8256     (if (not gnus-show-threads)
8257         (setq thread (list (car (gnus-id-to-thread id))))
8258       ;; Get the thread this article is part of.
8259       (setq thread (gnus-remove-thread id)))
8260     (setq current (save-excursion
8261                     (and (zerop (forward-line -1))
8262                          (gnus-summary-article-number))))
8263     ;; If this is a gathered thread, we have to go some re-gathering.
8264     (when (stringp (car thread))
8265       (let ((subject (car thread))
8266             roots thr)
8267         (setq thread (cdr thread))
8268         (while thread
8269           (unless (memq (setq thr (gnus-id-to-thread
8270                                       (gnus-root-id
8271                                        (mail-header-id (caar thread)))))
8272                         roots)
8273             (push thr roots))
8274           (setq thread (cdr thread)))
8275         ;; We now have all (unique) roots.
8276         (if (= (length roots) 1)
8277             ;; All the loose roots are now one solid root.
8278             (setq thread (car roots))
8279           (setq thread (cons subject (gnus-sort-threads roots))))))
8280     (let (threads)
8281       ;; We then insert this thread into the summary buffer.
8282       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8283         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8284         (setq data (nreverse gnus-newsgroup-data))
8285         (setq threads gnus-newsgroup-threads))
8286       ;; We splice the new data into the data structure.
8287       (gnus-data-enter-list current data)
8288       (gnus-data-compute-positions)
8289       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8290
8291 (defun gnus-number-to-header (number)
8292   "Return the header for article NUMBER."
8293   (let ((headers gnus-newsgroup-headers))
8294     (while (and headers
8295                 (not (= number (mail-header-number (car headers)))))
8296       (pop headers))
8297     (when headers
8298       (car headers))))
8299
8300 (defun gnus-id-to-thread (id)
8301   "Return the (sub-)thread where ID appears."
8302   (gnus-gethash id gnus-newsgroup-dependencies))
8303
8304 (defun gnus-id-to-article (id)
8305   "Return the article number of ID."
8306   (let ((thread (gnus-id-to-thread id)))
8307     (when (and thread
8308                (car thread))
8309       (mail-header-number (car thread)))))
8310
8311 (defun gnus-id-to-header (id)
8312   "Return the article headers of ID."
8313   (car (gnus-id-to-thread id)))
8314
8315 (defun gnus-article-displayed-root-p (article)
8316   "Say whether ARTICLE is a root(ish) article."
8317   (let ((level (gnus-summary-thread-level article))
8318         (refs (mail-header-references  (gnus-summary-article-header article)))
8319         particle)
8320     (cond 
8321      ((null level) nil)
8322      ((zerop level) t)
8323      ((null refs) t)
8324      ((null (gnus-parent-id refs)) t)
8325      ((and (= 1 level)
8326            (null (setq particle (gnus-id-to-article
8327                                  (gnus-parent-id refs))))
8328            (null (gnus-summary-thread-level particle)))))))
8329
8330 (defun gnus-root-id (id)
8331   "Return the id of the root of the thread where ID appears."
8332   (let (last-id prev)
8333     (while (and id (setq prev (car (gnus-gethash 
8334                                     id gnus-newsgroup-dependencies))))
8335       (setq last-id id
8336             id (gnus-parent-id (mail-header-references prev))))
8337     last-id))
8338
8339 (defun gnus-remove-thread (id &optional dont-remove)
8340   "Remove the thread that has ID in it."
8341   (let ((dep gnus-newsgroup-dependencies)
8342         headers thread last-id)
8343     ;; First go up in this thread until we find the root.
8344     (setq last-id (gnus-root-id id))
8345     (setq headers (list (car (gnus-id-to-thread last-id))
8346                         (caadr (gnus-id-to-thread last-id))))
8347     ;; We have now found the real root of this thread.  It might have
8348     ;; been gathered into some loose thread, so we have to search
8349     ;; through the threads to find the thread we wanted.
8350     (let ((threads gnus-newsgroup-threads)
8351           sub)
8352       (while threads
8353         (setq sub (car threads))
8354         (if (stringp (car sub))
8355             ;; This is a gathered thread, so we look at the roots
8356             ;; below it to find whether this article is in this
8357             ;; gathered root.
8358             (progn
8359               (setq sub (cdr sub))
8360               (while sub
8361                 (when (member (caar sub) headers)
8362                   (setq thread (car threads)
8363                         threads nil
8364                         sub nil))
8365                 (setq sub (cdr sub))))
8366           ;; It's an ordinary thread, so we check it.
8367           (when (eq (car sub) (car headers))
8368             (setq thread sub
8369                   threads nil)))
8370         (setq threads (cdr threads)))
8371       ;; If this article is in no thread, then it's a root.
8372       (if thread
8373           (unless dont-remove
8374             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8375         (setq thread (gnus-gethash last-id dep)))
8376       (when thread
8377         (prog1
8378             thread ; We return this thread.
8379           (unless dont-remove
8380             (if (stringp (car thread))
8381                 (progn
8382                   ;; If we use dummy roots, then we have to remove the
8383                   ;; dummy root as well.
8384                   (when (eq gnus-summary-make-false-root 'dummy)
8385                     ;; Uhm.
8386                     )
8387                   (setq thread (cdr thread))
8388                   (while thread
8389                     (gnus-remove-thread-1 (car thread))
8390                     (setq thread (cdr thread))))
8391               (gnus-remove-thread-1 thread))))))))
8392
8393 (defun gnus-remove-thread-1 (thread)
8394   "Remove the thread THREAD recursively."
8395   (let ((number (mail-header-number (car thread)))
8396         pos)
8397     (when (setq pos (text-property-any
8398                      (point-min) (point-max) 'gnus-number number))
8399       (goto-char pos)
8400       (gnus-delete-line)
8401       (gnus-data-remove number))
8402     (setq thread (cdr thread))
8403     (while thread
8404       (gnus-remove-thread-1 (pop thread)))))
8405
8406 (defun gnus-sort-threads (threads)
8407   "Sort THREADS."
8408   (if (not gnus-thread-sort-functions)
8409       threads
8410     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8411                     (car gnus-thread-sort-functions)
8412                   `(lambda (t1 t2)
8413                      ,(gnus-make-sort-function 
8414                        (reverse gnus-thread-sort-functions))))))
8415       (gnus-message 7 "Sorting threads...")
8416       (prog1
8417           (sort threads func)
8418         (gnus-message 7 "Sorting threads...done")))))
8419
8420 (defun gnus-sort-articles (articles)
8421   "Sort ARTICLES."
8422   (when gnus-article-sort-functions
8423     (let ((func (if (= 1 (length gnus-article-sort-functions))
8424                     (car gnus-article-sort-functions)
8425                   `(lambda (t1 t2)
8426                      ,(gnus-make-sort-function 
8427                        (reverse gnus-article-sort-functions))))))
8428       (gnus-message 7 "Sorting articles...")
8429       (prog1
8430           (setq gnus-newsgroup-headers (sort articles func))
8431         (gnus-message 7 "Sorting articles...done")))))
8432
8433 (defun gnus-make-sort-function (funs)
8434   "Return a composite sort condition based on the functions in FUNC."
8435   (if (cdr funs)
8436       `(or (,(car funs) t1 t2)
8437            (and (not (,(car funs) t2 t1))
8438                 ,(gnus-make-sort-function (cdr funs))))
8439     `(,(car funs) t1 t2)))
8440                  
8441 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8442 (defmacro gnus-thread-header (thread)
8443   ;; Return header of first article in THREAD.
8444   ;; Note that THREAD must never, ever be anything else than a variable -
8445   ;; using some other form will lead to serious barfage.
8446   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8447   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8448   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8449         (vector thread) 2))
8450
8451 (defsubst gnus-article-sort-by-number (h1 h2)
8452   "Sort articles by article number."
8453   (< (mail-header-number h1)
8454      (mail-header-number h2)))
8455
8456 (defun gnus-thread-sort-by-number (h1 h2)
8457   "Sort threads by root article number."
8458   (gnus-article-sort-by-number
8459    (gnus-thread-header h1) (gnus-thread-header h2)))
8460
8461 (defsubst gnus-article-sort-by-author (h1 h2)
8462   "Sort articles by root author."
8463   (string-lessp
8464    (let ((extract (funcall
8465                    gnus-extract-address-components
8466                    (mail-header-from h1))))
8467      (or (car extract) (cdr extract)))
8468    (let ((extract (funcall
8469                    gnus-extract-address-components
8470                    (mail-header-from h2))))
8471      (or (car extract) (cdr extract)))))
8472
8473 (defun gnus-thread-sort-by-author (h1 h2)
8474   "Sort threads by root author."
8475   (gnus-article-sort-by-author
8476    (gnus-thread-header h1)  (gnus-thread-header h2)))
8477
8478 (defsubst gnus-article-sort-by-subject (h1 h2)
8479   "Sort articles by root subject."
8480   (string-lessp
8481    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8482    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8483
8484 (defun gnus-thread-sort-by-subject (h1 h2)
8485   "Sort threads by root subject."
8486   (gnus-article-sort-by-subject
8487    (gnus-thread-header h1) (gnus-thread-header h2)))
8488
8489 (defsubst gnus-article-sort-by-date (h1 h2)
8490   "Sort articles by root article date."
8491   (string-lessp
8492    (inline (gnus-sortable-date (mail-header-date h1)))
8493    (inline (gnus-sortable-date (mail-header-date h2)))))
8494
8495 (defun gnus-thread-sort-by-date (h1 h2)
8496   "Sort threads by root article date."
8497   (gnus-article-sort-by-date
8498    (gnus-thread-header h1) (gnus-thread-header h2)))
8499
8500 (defsubst gnus-article-sort-by-score (h1 h2)
8501   "Sort articles by root article score.
8502 Unscored articles will be counted as having a score of zero."
8503   (> (or (cdr (assq (mail-header-number h1)
8504                     gnus-newsgroup-scored))
8505          gnus-summary-default-score 0)
8506      (or (cdr (assq (mail-header-number h2)
8507                     gnus-newsgroup-scored))
8508          gnus-summary-default-score 0)))
8509
8510 (defun gnus-thread-sort-by-score (h1 h2)
8511   "Sort threads by root article score."
8512   (gnus-article-sort-by-score
8513    (gnus-thread-header h1) (gnus-thread-header h2)))
8514
8515 (defun gnus-thread-sort-by-total-score (h1 h2)
8516   "Sort threads by the sum of all scores in the thread.
8517 Unscored articles will be counted as having a score of zero."
8518   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8519
8520 (defun gnus-thread-total-score (thread)
8521   ;;  This function find the total score of THREAD.
8522   (cond ((null thread)
8523          0)
8524         ((consp thread)
8525          (if (stringp (car thread))
8526              (apply gnus-thread-score-function 0
8527                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8528            (gnus-thread-total-score-1 thread)))
8529         (t
8530          (gnus-thread-total-score-1 (list thread)))))
8531
8532 (defun gnus-thread-total-score-1 (root)
8533   ;; This function find the total score of the thread below ROOT.
8534   (setq root (car root))
8535   (apply gnus-thread-score-function
8536          (or (append
8537               (mapcar 'gnus-thread-total-score
8538                       (cdr (gnus-gethash (mail-header-id root)
8539                                          gnus-newsgroup-dependencies)))
8540                  (if (> (mail-header-number root) 0)
8541                      (list (or (cdr (assq (mail-header-number root) 
8542                                           gnus-newsgroup-scored))
8543                                gnus-summary-default-score 0))))
8544              (list gnus-summary-default-score)
8545              '(0))))
8546
8547 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8548 (defvar gnus-tmp-prev-subject nil)
8549 (defvar gnus-tmp-false-parent nil)
8550 (defvar gnus-tmp-root-expunged nil)
8551 (defvar gnus-tmp-dummy-line nil)
8552
8553 (defun gnus-summary-prepare-threads (threads)
8554   "Prepare summary buffer from THREADS and indentation LEVEL.
8555 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8556 or a straight list of headers."
8557   (gnus-message 7 "Generating summary...")
8558
8559   (setq gnus-newsgroup-threads threads)
8560   (beginning-of-line)
8561
8562   (let ((gnus-tmp-level 0)
8563         (default-score (or gnus-summary-default-score 0))
8564         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8565         thread number subject stack state gnus-tmp-gathered beg-match
8566         new-roots gnus-tmp-new-adopts thread-end
8567         gnus-tmp-header gnus-tmp-unread
8568         gnus-tmp-replied gnus-tmp-subject-or-nil
8569         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8570         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8571         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8572
8573     (setq gnus-tmp-prev-subject nil)
8574
8575     (if (vectorp (car threads))
8576         ;; If this is a straight (sic) list of headers, then a
8577         ;; threaded summary display isn't required, so we just create
8578         ;; an unthreaded one.
8579         (gnus-summary-prepare-unthreaded threads)
8580
8581       ;; Do the threaded display.
8582
8583       (while (or threads stack gnus-tmp-new-adopts new-roots)
8584
8585         (if (and (= gnus-tmp-level 0)
8586                  (not (setq gnus-tmp-dummy-line nil))
8587                  (or (not stack)
8588                      (= (caar stack) 0))
8589                  (not gnus-tmp-false-parent)
8590                  (or gnus-tmp-new-adopts new-roots))
8591             (if gnus-tmp-new-adopts
8592                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8593                       thread (list (car gnus-tmp-new-adopts))
8594                       gnus-tmp-header (caar thread)
8595                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8596               (if new-roots
8597                   (setq thread (list (car new-roots))
8598                         gnus-tmp-header (caar thread)
8599                         new-roots (cdr new-roots))))
8600
8601           (if threads
8602               ;; If there are some threads, we do them before the
8603               ;; threads on the stack.
8604               (setq thread threads
8605                     gnus-tmp-header (caar thread))
8606             ;; There were no current threads, so we pop something off
8607             ;; the stack.
8608             (setq state (car stack)
8609                   gnus-tmp-level (car state)
8610                   thread (cdr state)
8611                   stack (cdr stack)
8612                   gnus-tmp-header (caar thread))))
8613
8614         (setq gnus-tmp-false-parent nil)
8615         (setq gnus-tmp-root-expunged nil)
8616         (setq thread-end nil)
8617
8618         (if (stringp gnus-tmp-header)
8619             ;; The header is a dummy root.
8620             (cond
8621              ((eq gnus-summary-make-false-root 'adopt)
8622               ;; We let the first article adopt the rest.
8623               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8624                                                (cddar thread)))
8625               (setq gnus-tmp-gathered
8626                     (nconc (mapcar
8627                             (lambda (h) (mail-header-number (car h)))
8628                             (cddar thread))
8629                            gnus-tmp-gathered))
8630               (setq thread (cons (list (caar thread)
8631                                        (cadar thread))
8632                                  (cdr thread)))
8633               (setq gnus-tmp-level -1
8634                     gnus-tmp-false-parent t))
8635              ((eq gnus-summary-make-false-root 'empty)
8636               ;; We print adopted articles with empty subject fields.
8637               (setq gnus-tmp-gathered
8638                     (nconc (mapcar
8639                             (lambda (h) (mail-header-number (car h)))
8640                             (cddar thread))
8641                            gnus-tmp-gathered))
8642               (setq gnus-tmp-level -1))
8643              ((eq gnus-summary-make-false-root 'dummy)
8644               ;; We remember that we probably want to output a dummy
8645               ;; root.
8646               (setq gnus-tmp-dummy-line gnus-tmp-header)
8647               (setq gnus-tmp-prev-subject gnus-tmp-header))
8648              (t
8649               ;; We do not make a root for the gathered
8650               ;; sub-threads at all.
8651               (setq gnus-tmp-level -1)))
8652
8653           (setq number (mail-header-number gnus-tmp-header)
8654                 subject (mail-header-subject gnus-tmp-header))
8655
8656           (cond
8657            ;; If the thread has changed subject, we might want to make
8658            ;; this subthread into a root.
8659            ((and (null gnus-thread-ignore-subject)
8660                  (not (zerop gnus-tmp-level))
8661                  gnus-tmp-prev-subject
8662                  (not (inline
8663                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8664             (setq new-roots (nconc new-roots (list (car thread)))
8665                   thread-end t
8666                   gnus-tmp-header nil))
8667            ;; If the article lies outside the current limit,
8668            ;; then we do not display it.
8669            ((and (not (memq number gnus-newsgroup-limit))
8670                  (not gnus-tmp-dummy-line))
8671             (setq gnus-tmp-gathered
8672                   (nconc (mapcar
8673                           (lambda (h) (mail-header-number (car h)))
8674                           (cdar thread))
8675                          gnus-tmp-gathered))
8676             (setq gnus-tmp-new-adopts (if (cdar thread)
8677                                           (append gnus-tmp-new-adopts
8678                                                   (cdar thread))
8679                                         gnus-tmp-new-adopts)
8680                   thread-end t
8681                   gnus-tmp-header nil)
8682             (when (zerop gnus-tmp-level)
8683               (setq gnus-tmp-root-expunged t)))
8684            ;; Perhaps this article is to be marked as read?
8685            ((and gnus-summary-mark-below
8686                  (< (or (cdr (assq number gnus-newsgroup-scored))
8687                         default-score)
8688                     gnus-summary-mark-below)
8689                  ;; Don't touch sparse articles.
8690                  (not (memq number gnus-newsgroup-sparse))
8691                  (not (memq number gnus-newsgroup-ancient)))
8692             (setq gnus-newsgroup-unreads
8693                   (delq number gnus-newsgroup-unreads))
8694             (if gnus-newsgroup-auto-expire
8695                 (push number gnus-newsgroup-expirable)
8696               (push (cons number gnus-low-score-mark)
8697                     gnus-newsgroup-reads))))
8698
8699           (when gnus-tmp-header
8700             ;; We may have an old dummy line to output before this
8701             ;; article.
8702             (when gnus-tmp-dummy-line
8703               (gnus-summary-insert-dummy-line
8704                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8705               (setq gnus-tmp-dummy-line nil))
8706
8707             ;; Compute the mark.
8708             (setq
8709              gnus-tmp-unread
8710              (cond
8711               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8712               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8713               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8714               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8715               (t (or (cdr (assq number gnus-newsgroup-reads))
8716                      gnus-ancient-mark))))
8717
8718             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8719                                   gnus-tmp-header gnus-tmp-level)
8720                   gnus-newsgroup-data)
8721
8722             ;; Actually insert the line.
8723             (setq
8724              gnus-tmp-subject-or-nil
8725              (cond
8726               ((and gnus-thread-ignore-subject
8727                     gnus-tmp-prev-subject
8728                     (not (inline (gnus-subject-equal
8729                                   gnus-tmp-prev-subject subject))))
8730                subject)
8731               ((zerop gnus-tmp-level)
8732                (if (and (eq gnus-summary-make-false-root 'empty)
8733                         (memq number gnus-tmp-gathered)
8734                         gnus-tmp-prev-subject
8735                         (inline (gnus-subject-equal
8736                                  gnus-tmp-prev-subject subject)))
8737                    gnus-summary-same-subject
8738                  subject))
8739               (t gnus-summary-same-subject)))
8740             (if (and (eq gnus-summary-make-false-root 'adopt)
8741                      (= gnus-tmp-level 1)
8742                      (memq number gnus-tmp-gathered))
8743                 (setq gnus-tmp-opening-bracket ?\<
8744                       gnus-tmp-closing-bracket ?\>)
8745               (setq gnus-tmp-opening-bracket ?\[
8746                     gnus-tmp-closing-bracket ?\]))
8747             (setq
8748              gnus-tmp-indentation
8749              (aref gnus-thread-indent-array gnus-tmp-level)
8750              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8751              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8752                                 gnus-summary-default-score 0)
8753              gnus-tmp-score-char
8754              (if (or (null gnus-summary-default-score)
8755                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8756                          gnus-summary-zcore-fuzz)) ? 
8757                (if (< gnus-tmp-score gnus-summary-default-score)
8758                    gnus-score-below-mark gnus-score-over-mark))
8759              gnus-tmp-replied
8760              (cond ((memq number gnus-newsgroup-processable)
8761                     gnus-process-mark)
8762                    ((memq number gnus-newsgroup-cached)
8763                     gnus-cached-mark)
8764                    ((memq number gnus-newsgroup-replied)
8765                     gnus-replied-mark)
8766                    ((memq number gnus-newsgroup-saved)
8767                     gnus-saved-mark)
8768                    (t gnus-unread-mark))
8769              gnus-tmp-from (mail-header-from gnus-tmp-header)
8770              gnus-tmp-name
8771              (cond
8772               ((string-match "(.+)" gnus-tmp-from)
8773                (substring gnus-tmp-from
8774                           (1+ (match-beginning 0)) (1- (match-end 0))))
8775               ((string-match "<[^>]+> *$" gnus-tmp-from)
8776                (setq beg-match (match-beginning 0))
8777                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8778                         (substring gnus-tmp-from (1+ (match-beginning 0))
8779                                    (1- (match-end 0))))
8780                    (substring gnus-tmp-from 0 beg-match)))
8781               (t gnus-tmp-from)))
8782             (when (string= gnus-tmp-name "")
8783               (setq gnus-tmp-name gnus-tmp-from))
8784             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8785             (gnus-put-text-property
8786              (point)
8787              (progn (eval gnus-summary-line-format-spec) (point))
8788              'gnus-number number)
8789             (when gnus-visual-p
8790               (forward-line -1)
8791               (run-hooks 'gnus-summary-update-hook)
8792               (forward-line 1))
8793
8794             (setq gnus-tmp-prev-subject subject)))
8795
8796         (when (nth 1 thread)
8797           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8798         (incf gnus-tmp-level)
8799         (setq threads (if thread-end nil (cdar thread)))
8800         (unless threads
8801           (setq gnus-tmp-level 0)))))
8802   (gnus-message 7 "Generating summary...done"))
8803
8804 (defun gnus-summary-prepare-unthreaded (headers)
8805   "Generate an unthreaded summary buffer based on HEADERS."
8806   (let (header number mark)
8807
8808     (while headers
8809       ;; We may have to root out some bad articles...
8810       (when (memq (setq number (mail-header-number
8811                                 (setq header (pop headers))))
8812                   gnus-newsgroup-limit)
8813         ;; Mark article as read when it has a low score.
8814         (when (and gnus-summary-mark-below
8815                    (< (or (cdr (assq number gnus-newsgroup-scored))
8816                           gnus-summary-default-score 0)
8817                       gnus-summary-mark-below)
8818                    (not (memq number gnus-newsgroup-ancient)))
8819           (setq gnus-newsgroup-unreads
8820                 (delq number gnus-newsgroup-unreads))
8821           (if gnus-newsgroup-auto-expire
8822               (push number gnus-newsgroup-expirable)
8823             (push (cons number gnus-low-score-mark)
8824                   gnus-newsgroup-reads)))
8825
8826         (setq mark
8827               (cond
8828                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8829                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8830                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8831                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8832                (t (or (cdr (assq number gnus-newsgroup-reads))
8833                       gnus-ancient-mark))))
8834         (setq gnus-newsgroup-data
8835               (cons (gnus-data-make number mark (1+ (point)) header 0)
8836                     gnus-newsgroup-data))
8837         (gnus-summary-insert-line
8838          header 0 nil mark (memq number gnus-newsgroup-replied)
8839          (memq number gnus-newsgroup-expirable)
8840          (mail-header-subject header) nil
8841          (cdr (assq number gnus-newsgroup-scored))
8842          (memq number gnus-newsgroup-processable))))))
8843
8844 (defun gnus-select-newsgroup (group &optional read-all)
8845   "Select newsgroup GROUP.
8846 If READ-ALL is non-nil, all articles in the group are selected."
8847   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8848          (info (nth 2 entry))
8849          articles fetched-articles cached)
8850
8851     (or (gnus-check-server
8852          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8853         (error "Couldn't open server"))
8854
8855     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8856         (gnus-activate-group group)     ; Or we can activate it...
8857         (progn                          ; Or we bug out.
8858           (when (equal major-mode 'gnus-summary-mode)
8859             (kill-buffer (current-buffer)))
8860           (error "Couldn't request group %s: %s"
8861                  group (gnus-status-message group))))
8862
8863     (unless (gnus-request-group group t)
8864       (when (equal major-mode 'gnus-summary-mode)
8865         (kill-buffer (current-buffer)))
8866       (error "Couldn't request group %s: %s"
8867              group (gnus-status-message group)))      
8868
8869     (setq gnus-newsgroup-name group)
8870     (setq gnus-newsgroup-unselected nil)
8871     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8872
8873     (and gnus-asynchronous
8874          (gnus-check-backend-function
8875           'request-asynchronous gnus-newsgroup-name)
8876          (setq gnus-newsgroup-async
8877                (gnus-request-asynchronous gnus-newsgroup-name)))
8878
8879     ;; Adjust and set lists of article marks.
8880     (when info
8881       (gnus-adjust-marked-articles info))
8882
8883     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8884     (when (gnus-virtual-group-p group)
8885       (setq cached gnus-newsgroup-cached))
8886
8887     (setq gnus-newsgroup-unreads
8888           (gnus-set-difference
8889            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8890            gnus-newsgroup-dormant))
8891
8892     (setq gnus-newsgroup-processable nil)
8893
8894     (setq articles (gnus-articles-to-read group read-all))
8895
8896     (cond
8897      ((null articles)
8898       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8899       'quit)
8900      ((eq articles 0) nil)
8901      (t
8902       ;; Init the dependencies hash table.
8903       (setq gnus-newsgroup-dependencies
8904             (gnus-make-hashtable (length articles)))
8905       ;; Retrieve the headers and read them in.
8906       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8907       (setq gnus-newsgroup-headers
8908             (if (eq 'nov
8909                     (setq gnus-headers-retrieved-by
8910                           (gnus-retrieve-headers
8911                            articles gnus-newsgroup-name
8912                            ;; We might want to fetch old headers, but
8913                            ;; not if there is only 1 article.
8914                            (and gnus-fetch-old-headers
8915                                 (or (and
8916                                      (not (eq gnus-fetch-old-headers 'some))
8917                                      (not (numberp gnus-fetch-old-headers)))
8918                                     (> (length articles) 1))))))
8919                 (gnus-get-newsgroup-headers-xover articles)
8920               (gnus-get-newsgroup-headers)))
8921       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8922
8923       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8924       (when cached
8925         (setq gnus-newsgroup-cached cached))
8926
8927       ;; Set the initial limit.
8928       (setq gnus-newsgroup-limit (copy-sequence articles))
8929       ;; Remove canceled articles from the list of unread articles.
8930       (setq gnus-newsgroup-unreads
8931             (gnus-set-sorted-intersection
8932              gnus-newsgroup-unreads
8933              (setq fetched-articles
8934                    (mapcar (lambda (headers) (mail-header-number headers))
8935                            gnus-newsgroup-headers))))
8936       ;; Removed marked articles that do not exist.
8937       (gnus-update-missing-marks
8938        (gnus-sorted-complement fetched-articles articles))
8939       ;; We might want to build some more threads first.
8940       (and gnus-fetch-old-headers
8941            (eq gnus-headers-retrieved-by 'nov)
8942            (gnus-build-old-threads))
8943       ;; Check whether auto-expire is to be done in this group.
8944       (setq gnus-newsgroup-auto-expire
8945             (gnus-group-auto-expirable-p group))
8946       ;; Set up the article buffer now, if necessary.
8947       (unless gnus-single-article-buffer
8948         (gnus-article-setup-buffer))
8949       ;; First and last article in this newsgroup.
8950       (when gnus-newsgroup-headers
8951         (setq gnus-newsgroup-begin
8952               (mail-header-number (car gnus-newsgroup-headers))
8953               gnus-newsgroup-end
8954               (mail-header-number
8955                (gnus-last-element gnus-newsgroup-headers))))
8956       ;; GROUP is successfully selected.
8957       (or gnus-newsgroup-headers t)))))
8958
8959 (defun gnus-articles-to-read (group read-all)
8960   ;; Find out what articles the user wants to read.
8961   (let* ((articles
8962           ;; Select all articles if `read-all' is non-nil, or if there
8963           ;; are no unread articles.
8964           (if (or read-all
8965                   (and (zerop (length gnus-newsgroup-marked))
8966                        (zerop (length gnus-newsgroup-unreads))))
8967               (gnus-uncompress-range (gnus-active group))
8968             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8969                           (copy-sequence gnus-newsgroup-unreads))
8970                   '<)))
8971          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8972          (scored (length scored-list))
8973          (number (length articles))
8974          (marked (+ (length gnus-newsgroup-marked)
8975                     (length gnus-newsgroup-dormant)))
8976          (select
8977           (cond
8978            ((numberp read-all)
8979             read-all)
8980            (t
8981             (condition-case ()
8982                 (cond
8983                  ((and (or (<= scored marked) (= scored number))
8984                        (numberp gnus-large-newsgroup)
8985                        (> number gnus-large-newsgroup))
8986                   (let ((input
8987                          (read-string
8988                           (format
8989                            "How many articles from %s (default %d): "
8990                            gnus-newsgroup-name number))))
8991                     (if (string-match "^[ \t]*$" input) number input)))
8992                  ((and (> scored marked) (< scored number)
8993                        (> (- scored number) 20))
8994                   (let ((input
8995                          (read-string
8996                           (format "%s %s (%d scored, %d total): "
8997                                   "How many articles from"
8998                                   group scored number))))
8999                     (if (string-match "^[ \t]*$" input)
9000                         number input)))
9001                  (t number))
9002               (quit nil))))))
9003     (setq select (if (stringp select) (string-to-number select) select))
9004     (if (or (null select) (zerop select))
9005         select
9006       (if (and (not (zerop scored)) (<= (abs select) scored))
9007           (progn
9008             (setq articles (sort scored-list '<))
9009             (setq number (length articles)))
9010         (setq articles (copy-sequence articles)))
9011
9012       (if (< (abs select) number)
9013           (if (< select 0)
9014               ;; Select the N oldest articles.
9015               (setcdr (nthcdr (1- (abs select)) articles) nil)
9016             ;; Select the N most recent articles.
9017             (setq articles (nthcdr (- number select) articles))))
9018       (setq gnus-newsgroup-unselected
9019             (gnus-sorted-intersection
9020              gnus-newsgroup-unreads
9021              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
9022       articles)))
9023
9024 (defun gnus-killed-articles (killed articles)
9025   (let (out)
9026     (while articles
9027       (if (inline (gnus-member-of-range (car articles) killed))
9028           (setq out (cons (car articles) out)))
9029       (setq articles (cdr articles)))
9030     out))
9031
9032 (defun gnus-uncompress-marks (marks)
9033   "Uncompress the mark ranges in MARKS."
9034   (let ((uncompressed '(score bookmark))
9035         out)
9036     (while marks
9037       (if (memq (caar marks) uncompressed)
9038           (push (car marks) out)
9039         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
9040       (setq marks (cdr marks)))
9041     out))
9042
9043 (defun gnus-adjust-marked-articles (info)
9044   "Set all article lists and remove all marks that are no longer legal."
9045   (let* ((marked-lists (gnus-info-marks info))
9046          (active (gnus-active (gnus-info-group info)))
9047          (min (car active))
9048          (max (cdr active))
9049          (types gnus-article-mark-lists)
9050          (uncompressed '(score bookmark killed))
9051          marks var articles article mark)
9052
9053     (while marked-lists
9054       (setq marks (pop marked-lists))
9055       (set (setq var (intern (format "gnus-newsgroup-%s"
9056                                      (car (rassq (setq mark (car marks))
9057                                                  types)))))
9058            (if (memq (car marks) uncompressed) (cdr marks)
9059              (gnus-uncompress-range (cdr marks))))
9060
9061       (setq articles (symbol-value var))
9062
9063       ;; All articles have to be subsets of the active articles.
9064       (cond
9065        ;; Adjust "simple" lists.
9066        ((memq mark '(tick dormant expirable reply save))
9067         (while articles
9068           (when (or (< (setq article (pop articles)) min) (> article max))
9069             (set var (delq article (symbol-value var))))))
9070        ;; Adjust assocs.
9071        ((memq mark uncompressed)
9072         (while articles
9073           (when (or (not (consp (setq article (pop articles))))
9074                     (< (car article) min)
9075                     (> (car article) max))
9076             (set var (delq article (symbol-value var))))))))))
9077
9078 (defun gnus-update-missing-marks (missing)
9079   "Go through the list of MISSING articles and remove them mark lists."
9080   (when missing
9081     (let ((types gnus-article-mark-lists)
9082           var m)
9083       ;; Go through all types.
9084       (while types
9085         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
9086         (when (symbol-value var)
9087           ;; This list has articles.  So we delete all missing articles
9088           ;; from it.
9089           (setq m missing)
9090           (while m
9091             (set var (delq (pop m) (symbol-value var)))))))))
9092
9093 (defun gnus-update-marks ()
9094   "Enter the various lists of marked articles into the newsgroup info list."
9095   (let ((types gnus-article-mark-lists)
9096         (info (gnus-get-info gnus-newsgroup-name))
9097         (uncompressed '(score bookmark killed))
9098         type list newmarked symbol)
9099     (when info
9100       ;; Add all marks lists that are non-nil to the list of marks lists.
9101       (while types
9102         (setq type (pop types))
9103         (when (setq list (symbol-value
9104                           (setq symbol
9105                                 (intern (format "gnus-newsgroup-%s"
9106                                                 (car type))))))
9107           (push (cons (cdr type)
9108                       (if (memq (cdr type) uncompressed) list
9109                         (gnus-compress-sequence 
9110                          (set symbol (sort list '<)) t)))
9111                 newmarked)))
9112
9113       ;; Enter these new marks into the info of the group.
9114       (if (nthcdr 3 info)
9115           (setcar (nthcdr 3 info) newmarked)
9116         ;; Add the marks lists to the end of the info.
9117         (when newmarked
9118           (setcdr (nthcdr 2 info) (list newmarked))))
9119
9120       ;; Cut off the end of the info if there's nothing else there.
9121       (let ((i 5))
9122         (while (and (> i 2)
9123                     (not (nth i info)))
9124           (when (nthcdr (decf i) info)
9125             (setcdr (nthcdr i info) nil)))))))
9126
9127 (defun gnus-add-marked-articles (group type articles &optional info force)
9128   ;; Add ARTICLES of TYPE to the info of GROUP.
9129   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
9130   ;; add, but replace marked articles of TYPE with ARTICLES.
9131   (let ((info (or info (gnus-get-info group)))
9132         (uncompressed '(score bookmark killed))
9133         marked m)
9134     (or (not info)
9135         (and (not (setq marked (nthcdr 3 info)))
9136              (or (null articles)
9137                  (setcdr (nthcdr 2 info)
9138                          (list (list (cons type (gnus-compress-sequence
9139                                                  articles t)))))))
9140         (and (not (setq m (assq type (car marked))))
9141              (or (null articles)
9142                  (setcar marked
9143                          (cons (cons type (gnus-compress-sequence articles t) )
9144                                (car marked)))))
9145         (if force
9146             (if (null articles)
9147                 (setcar (nthcdr 3 info)
9148                         (delq (assq type (car marked)) (car marked)))
9149               (setcdr m (gnus-compress-sequence articles t)))
9150           (setcdr m (gnus-compress-sequence
9151                      (sort (nconc (gnus-uncompress-range (cdr m))
9152                                   (copy-sequence articles)) '<) t))))))
9153
9154 (defun gnus-set-mode-line (where)
9155   "This function sets the mode line of the article or summary buffers.
9156 If WHERE is `summary', the summary mode line format will be used."
9157   ;; Is this mode line one we keep updated?
9158   (when (memq where gnus-updated-mode-lines)
9159     (let (mode-string)
9160       (save-excursion
9161         ;; We evaluate this in the summary buffer since these
9162         ;; variables are buffer-local to that buffer.
9163         (set-buffer gnus-summary-buffer)
9164         ;; We bind all these variables that are used in the `eval' form
9165         ;; below.
9166         (let* ((mformat (symbol-value
9167                          (intern
9168                           (format "gnus-%s-mode-line-format-spec" where))))
9169                (gnus-tmp-group-name gnus-newsgroup-name)
9170                (gnus-tmp-article-number (or gnus-current-article 0))
9171                (gnus-tmp-unread gnus-newsgroup-unreads)
9172                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9173                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9174                (gnus-tmp-unread-and-unselected
9175                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9176                             (zerop gnus-tmp-unselected)) "")
9177                       ((zerop gnus-tmp-unselected)
9178                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9179                       (t (format "{%d(+%d) more}"
9180                                  gnus-tmp-unread-and-unticked
9181                                  gnus-tmp-unselected))))
9182                (gnus-tmp-subject
9183                 (if (and gnus-current-headers
9184                          (vectorp gnus-current-headers))
9185                     (gnus-mode-string-quote
9186                      (mail-header-subject gnus-current-headers)) ""))
9187                max-len
9188                gnus-tmp-header);; passed as argument to any user-format-funcs
9189           (setq mode-string (eval mformat))
9190           (setq max-len (max 4 (if gnus-mode-non-string-length
9191                                    (- (window-width)
9192                                       gnus-mode-non-string-length)
9193                                  (length mode-string))))
9194           ;; We might have to chop a bit of the string off...
9195           (when (> (length mode-string) max-len)
9196             (setq mode-string
9197                   (concat (gnus-truncate-string mode-string (- max-len 3))
9198                           "...")))
9199           ;; Pad the mode string a bit.
9200           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9201       ;; Update the mode line.
9202       (setq mode-line-buffer-identification 
9203             (gnus-mode-line-buffer-identification
9204              (list mode-string)))
9205       (set-buffer-modified-p t))))
9206
9207 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9208   "Go through the HEADERS list and add all Xrefs to a hash table.
9209 The resulting hash table is returned, or nil if no Xrefs were found."
9210   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9211          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9212          (xref-hashtb (make-vector 63 0))
9213          start group entry number xrefs header)
9214     (while headers
9215       (setq header (pop headers))
9216       (when (and (setq xrefs (mail-header-xref header))
9217                  (not (memq (setq number (mail-header-number header))
9218                             unreads)))
9219         (setq start 0)
9220         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9221           (setq start (match-end 0))
9222           (setq group (if prefix
9223                           (concat prefix (substring xrefs (match-beginning 1)
9224                                                     (match-end 1)))
9225                         (substring xrefs (match-beginning 1) (match-end 1))))
9226           (setq number
9227                 (string-to-int (substring xrefs (match-beginning 2)
9228                                           (match-end 2))))
9229           (if (setq entry (gnus-gethash group xref-hashtb))
9230               (setcdr entry (cons number (cdr entry)))
9231             (gnus-sethash group (cons number nil) xref-hashtb)))))
9232     (and start xref-hashtb)))
9233
9234 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9235   "Look through all the headers and mark the Xrefs as read."
9236   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9237         name entry info xref-hashtb idlist method nth4)
9238     (save-excursion
9239       (set-buffer gnus-group-buffer)
9240       (when (setq xref-hashtb
9241                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9242         (mapatoms
9243          (lambda (group)
9244            (unless (string= from-newsgroup (setq name (symbol-name group)))
9245              (setq idlist (symbol-value group))
9246              ;; Dead groups are not updated.
9247              (and (prog1
9248                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9249                             info (nth 2 entry))
9250                     (if (stringp (setq nth4 (gnus-info-method info)))
9251                         (setq nth4 (gnus-server-to-method nth4))))
9252                   ;; Only do the xrefs if the group has the same
9253                   ;; select method as the group we have just read.
9254                   (or (gnus-methods-equal-p
9255                        nth4 (gnus-find-method-for-group from-newsgroup))
9256                       virtual
9257                       (equal nth4 (setq method (gnus-find-method-for-group
9258                                                 from-newsgroup)))
9259                       (and (equal (car nth4) (car method))
9260                            (equal (nth 1 nth4) (nth 1 method))))
9261                   gnus-use-cross-reference
9262                   (or (not (eq gnus-use-cross-reference t))
9263                       virtual
9264                       ;; Only do cross-references on subscribed
9265                       ;; groups, if that is what is wanted.
9266                       (<= (gnus-info-level info) gnus-level-subscribed))
9267                   (gnus-group-make-articles-read name idlist))))
9268          xref-hashtb)))))
9269
9270 (defun gnus-group-make-articles-read (group articles)
9271   (let* ((num 0)
9272          (entry (gnus-gethash group gnus-newsrc-hashtb))
9273          (info (nth 2 entry))
9274          (active (gnus-active group))
9275          range)
9276     ;; First peel off all illegal article numbers.
9277     (when active
9278       (let ((ids articles)
9279             id first)
9280         (while (setq id (pop ids))
9281           (when (and first (> id (cdr active)))
9282             ;; We'll end up in this situation in one particular
9283             ;; obscure situation.  If you re-scan a group and get
9284             ;; a new article that is cross-posted to a different
9285             ;; group that has not been re-scanned, you might get
9286             ;; crossposted article that has a higher number than
9287             ;; Gnus believes possible.  So we re-activate this
9288             ;; group as well.  This might mean doing the
9289             ;; crossposting thingy will *increase* the number
9290             ;; of articles in some groups.  Tsk, tsk.
9291             (setq active (or (gnus-activate-group group) active)))
9292           (when (or (> id (cdr active))
9293                     (< id (car active)))
9294             (setq articles (delq id articles))))))
9295     ;; If the read list is nil, we init it.
9296     (and active
9297          (null (gnus-info-read info))
9298          (> (car active) 1)
9299          (gnus-info-set-read info (cons 1 (1- (car active)))))
9300     ;; Then we add the read articles to the range.
9301     (gnus-info-set-read
9302      info
9303      (setq range
9304            (gnus-add-to-range
9305             (gnus-info-read info) (setq articles (sort articles '<)))))
9306     ;; Then we have to re-compute how many unread
9307     ;; articles there are in this group.
9308     (if active
9309         (progn
9310           (cond
9311            ((not range)
9312             (setq num (- (1+ (cdr active)) (car active))))
9313            ((not (listp (cdr range)))
9314             (setq num (- (cdr active) (- (1+ (cdr range))
9315                                          (car range)))))
9316            (t
9317             (while range
9318               (if (numberp (car range))
9319                   (setq num (1+ num))
9320                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9321               (setq range (cdr range)))
9322             (setq num (- (cdr active) num))))
9323           ;; Update the number of unread articles.
9324           (setcar entry num)
9325           ;; Update the group buffer.
9326           (gnus-group-update-group group t)))))
9327
9328 (defun gnus-methods-equal-p (m1 m2)
9329   (let ((m1 (or m1 gnus-select-method))
9330         (m2 (or m2 gnus-select-method)))
9331     (or (equal m1 m2)
9332         (and (eq (car m1) (car m2))
9333              (or (not (memq 'address (assoc (symbol-name (car m1))
9334                                             gnus-valid-select-methods)))
9335                  (equal (nth 1 m1) (nth 1 m2)))))))
9336
9337 (defsubst gnus-header-value ()
9338   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9339
9340 (defvar gnus-newsgroup-none-id 0)
9341
9342 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9343   (let ((cur nntp-server-buffer)
9344         (dependencies
9345          (or dependencies
9346              (save-excursion (set-buffer gnus-summary-buffer)
9347                              gnus-newsgroup-dependencies)))
9348         headers id id-dep ref-dep end ref)
9349     (save-excursion
9350       (set-buffer nntp-server-buffer)
9351       (run-hooks 'gnus-parse-headers-hook)
9352       (let ((case-fold-search t)
9353             in-reply-to header p lines)
9354         (goto-char (point-min))
9355         ;; Search to the beginning of the next header.  Error messages
9356         ;; do not begin with 2 or 3.
9357         (while (re-search-forward "^[23][0-9]+ " nil t)
9358           (setq id nil
9359                 ref nil)
9360           ;; This implementation of this function, with nine
9361           ;; search-forwards instead of the one re-search-forward and
9362           ;; a case (which basically was the old function) is actually
9363           ;; about twice as fast, even though it looks messier.  You
9364           ;; can't have everything, I guess.  Speed and elegance
9365           ;; doesn't always go hand in hand.
9366           (setq
9367            header
9368            (vector
9369             ;; Number.
9370             (prog1
9371                 (read cur)
9372               (end-of-line)
9373               (setq p (point))
9374               (narrow-to-region (point)
9375                                 (or (and (search-forward "\n.\n" nil t)
9376                                          (- (point) 2))
9377                                     (point))))
9378             ;; Subject.
9379             (progn
9380               (goto-char p)
9381               (if (search-forward "\nsubject: " nil t)
9382                   (gnus-header-value) "(none)"))
9383             ;; From.
9384             (progn
9385               (goto-char p)
9386               (if (search-forward "\nfrom: " nil t)
9387                   (gnus-header-value) "(nobody)"))
9388             ;; Date.
9389             (progn
9390               (goto-char p)
9391               (if (search-forward "\ndate: " nil t)
9392                   (gnus-header-value) ""))
9393             ;; Message-ID.
9394             (progn
9395               (goto-char p)
9396               (if (search-forward "\nmessage-id: " nil t)
9397                   (setq id (gnus-header-value))
9398                 ;; If there was no message-id, we just fake one to make
9399                 ;; subsequent routines simpler.
9400                 (setq id (concat "none+"
9401                                  (int-to-string
9402                                   (setq gnus-newsgroup-none-id
9403                                         (1+ gnus-newsgroup-none-id)))))))
9404             ;; References.
9405             (progn
9406               (goto-char p)
9407               (if (search-forward "\nreferences: " nil t)
9408                   (progn
9409                     (setq end (point))
9410                     (prog1
9411                         (gnus-header-value)
9412                       (setq ref
9413                             (buffer-substring
9414                              (progn
9415                                (end-of-line)
9416                                (search-backward ">" end t)
9417                                (1+ (point)))
9418                              (progn
9419                                (search-backward "<" end t)
9420                                (point))))))
9421                 ;; Get the references from the in-reply-to header if there
9422                 ;; were no references and the in-reply-to header looks
9423                 ;; promising.
9424                 (if (and (search-forward "\nin-reply-to: " nil t)
9425                          (setq in-reply-to (gnus-header-value))
9426                          (string-match "<[^>]+>" in-reply-to))
9427                     (setq ref (substring in-reply-to (match-beginning 0)
9428                                          (match-end 0)))
9429                   (setq ref ""))))
9430             ;; Chars.
9431             0
9432             ;; Lines.
9433             (progn
9434               (goto-char p)
9435               (if (search-forward "\nlines: " nil t)
9436                   (if (numberp (setq lines (read cur)))
9437                       lines 0)
9438                 0))
9439             ;; Xref.
9440             (progn
9441               (goto-char p)
9442               (and (search-forward "\nxref: " nil t)
9443                    (gnus-header-value)))))
9444           ;; We do the threading while we read the headers.  The
9445           ;; message-id and the last reference are both entered into
9446           ;; the same hash table.  Some tippy-toeing around has to be
9447           ;; done in case an article has arrived before the article
9448           ;; which it refers to.
9449           (if (boundp (setq id-dep (intern id dependencies)))
9450               (if (and (car (symbol-value id-dep))
9451                        (not force-new))
9452                   ;; An article with this Message-ID has already
9453                   ;; been seen, so we ignore this one, except we add
9454                   ;; any additional Xrefs (in case the two articles
9455                   ;; came from different servers).
9456                   (progn
9457                     (mail-header-set-xref
9458                      (car (symbol-value id-dep))
9459                      (concat (or (mail-header-xref
9460                                   (car (symbol-value id-dep))) "")
9461                              (or (mail-header-xref header) "")))
9462                     (setq header nil))
9463                 (setcar (symbol-value id-dep) header))
9464             (set id-dep (list header)))
9465           (when header
9466             (if (boundp (setq ref-dep (intern ref dependencies)))
9467                 (setcdr (symbol-value ref-dep)
9468                         (nconc (cdr (symbol-value ref-dep))
9469                                (list (symbol-value id-dep))))
9470               (set ref-dep (list nil (symbol-value id-dep))))
9471             (setq headers (cons header headers)))
9472           (goto-char (point-max))
9473           (widen))
9474         (nreverse headers)))))
9475
9476 ;; The following macros and functions were written by Felix Lee
9477 ;; <flee@cse.psu.edu>.
9478
9479 (defmacro gnus-nov-read-integer ()
9480   '(prog1
9481        (if (= (following-char) ?\t)
9482            0
9483          (let ((num (condition-case nil (read buffer) (error nil))))
9484            (if (numberp num) num 0)))
9485      (or (eobp) (forward-char 1))))
9486
9487 (defmacro gnus-nov-skip-field ()
9488   '(search-forward "\t" eol 'move))
9489
9490 (defmacro gnus-nov-field ()
9491   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9492
9493 ;; Goes through the xover lines and returns a list of vectors
9494 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9495                                                   force-new dependencies)
9496   "Parse the news overview data in the server buffer, and return a
9497 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9498   ;; Get the Xref when the users reads the articles since most/some
9499   ;; NNTP servers do not include Xrefs when using XOVER.
9500   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9501   (let ((cur nntp-server-buffer)
9502         (dependencies (or dependencies gnus-newsgroup-dependencies))
9503         number headers header)
9504     (save-excursion
9505       (set-buffer nntp-server-buffer)
9506       ;; Allow the user to mangle the headers before parsing them.
9507       (run-hooks 'gnus-parse-headers-hook)
9508       (goto-char (point-min))
9509       (while (and sequence (not (eobp)))
9510         (setq number (read cur))
9511         (while (and sequence (< (car sequence) number))
9512           (setq sequence (cdr sequence)))
9513         (and sequence
9514              (eq number (car sequence))
9515              (progn
9516                (setq sequence (cdr sequence))
9517                (if (setq header
9518                          (inline (gnus-nov-parse-line
9519                                   number dependencies force-new)))
9520                    (setq headers (cons header headers)))))
9521         (forward-line 1))
9522       (setq headers (nreverse headers)))
9523     headers))
9524
9525 ;; This function has to be called with point after the article number
9526 ;; on the beginning of the line.
9527 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9528   (let ((none 0)
9529         (eol (gnus-point-at-eol))
9530         (buffer (current-buffer))
9531         header ref id id-dep ref-dep)
9532
9533     ;; overview: [num subject from date id refs chars lines misc]
9534     (narrow-to-region (point) eol)
9535     (or (eobp) (forward-char))
9536
9537     (condition-case nil
9538         (setq header
9539               (vector
9540                number                   ; number
9541                (gnus-nov-field)         ; subject
9542                (gnus-nov-field)         ; from
9543                (gnus-nov-field)         ; date
9544                (setq id (or (gnus-nov-field)
9545                             (concat "none+"
9546                                     (int-to-string
9547                                      (setq none (1+ none)))))) ; id
9548                (progn
9549                  (save-excursion
9550                    (let ((beg (point)))
9551                      (search-forward "\t" eol)
9552                      (if (search-backward ">" beg t)
9553                          (setq ref
9554                                (buffer-substring
9555                                 (1+ (point))
9556                                 (search-backward "<" beg t)))
9557                        (setq ref nil))))
9558                  (gnus-nov-field))      ; refs
9559                (gnus-nov-read-integer)  ; chars
9560                (gnus-nov-read-integer)  ; lines
9561                (if (= (following-char) ?\n)
9562                    nil
9563                  (gnus-nov-field))      ; misc
9564                ))
9565       (error (progn
9566                (gnus-error 4 "Strange nov line")
9567                (setq header nil)
9568                (goto-char eol))))
9569
9570     (widen)
9571
9572     ;; We build the thread tree.
9573     (when header
9574       (if (boundp (setq id-dep (intern id dependencies)))
9575           (if (and (car (symbol-value id-dep))
9576                    (not force-new))
9577               ;; An article with this Message-ID has already been seen,
9578               ;; so we ignore this one, except we add any additional
9579               ;; Xrefs (in case the two articles came from different
9580               ;; servers.
9581               (progn
9582                 (mail-header-set-xref
9583                  (car (symbol-value id-dep))
9584                  (concat (or (mail-header-xref
9585                               (car (symbol-value id-dep))) "")
9586                          (or (mail-header-xref header) "")))
9587                 (setq header nil))
9588             (setcar (symbol-value id-dep) header))
9589         (set id-dep (list header))))
9590     (when header
9591       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9592           (setcdr (symbol-value ref-dep)
9593                   (nconc (cdr (symbol-value ref-dep))
9594                          (list (symbol-value id-dep))))
9595         (set ref-dep (list nil (symbol-value id-dep)))))
9596     header))
9597
9598 (defun gnus-article-get-xrefs ()
9599   "Fill in the Xref value in `gnus-current-headers', if necessary.
9600 This is meant to be called in `gnus-article-internal-prepare-hook'."
9601   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9602                                  gnus-current-headers)))
9603     (or (not gnus-use-cross-reference)
9604         (not headers)
9605         (and (mail-header-xref headers)
9606              (not (string= (mail-header-xref headers) "")))
9607         (let ((case-fold-search t)
9608               xref)
9609           (save-restriction
9610             (nnheader-narrow-to-headers)
9611             (goto-char (point-min))
9612             (if (or (and (eq (downcase (following-char)) ?x)
9613                          (looking-at "Xref:"))
9614                     (search-forward "\nXref:" nil t))
9615                 (progn
9616                   (goto-char (1+ (match-end 0)))
9617                   (setq xref (buffer-substring (point)
9618                                                (progn (end-of-line) (point))))
9619                   (mail-header-set-xref headers xref))))))))
9620
9621 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9622   "Find article ID and insert the summary line for that article."
9623   (let ((header (if (and old-header use-old-header)
9624                     old-header (gnus-read-header id)))
9625         (number (and (numberp id) id))
9626         pos)
9627     (when header
9628       ;; Rebuild the thread that this article is part of and go to the
9629       ;; article we have fetched.
9630       (when (and (not gnus-show-threads)
9631                  old-header)
9632         (when (setq pos (text-property-any
9633                          (point-min) (point-max) 'gnus-number 
9634                          (mail-header-number old-header)))
9635           (goto-char pos)
9636           (gnus-delete-line)
9637           (gnus-data-remove (mail-header-number old-header))))
9638       (when old-header
9639         (mail-header-set-number header (mail-header-number old-header)))
9640       (setq gnus-newsgroup-sparse
9641             (delq (setq number (mail-header-number header)) 
9642                   gnus-newsgroup-sparse))
9643       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9644       (gnus-rebuild-thread (mail-header-id header))
9645       (gnus-summary-goto-subject number nil t))
9646     (when (and (numberp number)
9647                (> number 0))
9648       ;; We have to update the boundaries even if we can't fetch the
9649       ;; article if ID is a number -- so that the next `P' or `N'
9650       ;; command will fetch the previous (or next) article even
9651       ;; if the one we tried to fetch this time has been canceled.
9652       (and (> number gnus-newsgroup-end)
9653            (setq gnus-newsgroup-end number))
9654       (and (< number gnus-newsgroup-begin)
9655            (setq gnus-newsgroup-begin number))
9656       (setq gnus-newsgroup-unselected
9657             (delq number gnus-newsgroup-unselected)))
9658     ;; Report back a success?
9659     (and header (mail-header-number header))))
9660
9661 (defun gnus-summary-work-articles (n)
9662   "Return a list of articles to be worked upon.  The prefix argument,
9663 the list of process marked articles, and the current article will be
9664 taken into consideration."
9665   (cond
9666    (n
9667     ;; A numerical prefix has been given.
9668     (let ((backward (< n 0))
9669           (n (abs (prefix-numeric-value n)))
9670           articles article)
9671       (save-excursion
9672         (while
9673             (and (> n 0)
9674                  (push (setq article (gnus-summary-article-number))
9675                        articles)
9676                  (if backward
9677                      (gnus-summary-find-prev nil article)
9678                    (gnus-summary-find-next nil article)))
9679           (decf n)))
9680       (nreverse articles)))
9681    ((and (boundp 'transient-mark-mode)
9682          transient-mark-mode
9683          mark-active)
9684     ;; Work on the region between point and mark.
9685     (let ((max (max (point) (mark)))
9686           articles article)
9687       (save-excursion
9688         (goto-char (min (point) (mark)))
9689         (while
9690             (and
9691              (push (setq article (gnus-summary-article-number)) articles)
9692              (gnus-summary-find-next nil article)
9693              (< (point) max)))
9694         (nreverse articles))))
9695    (gnus-newsgroup-processable
9696     ;; There are process-marked articles present.
9697     (reverse gnus-newsgroup-processable))
9698    (t
9699     ;; Just return the current article.
9700     (list (gnus-summary-article-number)))))
9701
9702 (defun gnus-summary-search-group (&optional backward use-level)
9703   "Search for next unread newsgroup.
9704 If optional argument BACKWARD is non-nil, search backward instead."
9705   (save-excursion
9706     (set-buffer gnus-group-buffer)
9707     (if (gnus-group-search-forward
9708          backward nil (if use-level (gnus-group-group-level) nil))
9709         (gnus-group-group-name))))
9710
9711 (defun gnus-summary-best-group (&optional exclude-group)
9712   "Find the name of the best unread group.
9713 If EXCLUDE-GROUP, do not go to this group."
9714   (save-excursion
9715     (set-buffer gnus-group-buffer)
9716     (save-excursion
9717       (gnus-group-best-unread-group exclude-group))))
9718
9719 (defun gnus-summary-find-next (&optional unread article backward)
9720   (if backward (gnus-summary-find-prev)
9721     (let* ((dummy (gnus-summary-article-intangible-p))
9722            (article (or article (gnus-summary-article-number)))
9723            (arts (gnus-data-find-list article))
9724            result)
9725       (when (and (not dummy)
9726                  (or (not gnus-summary-check-current)
9727                      (not unread)
9728                      (not (gnus-data-unread-p (car arts)))))
9729         (setq arts (cdr arts)))
9730       (when (setq result
9731                   (if unread
9732                       (progn
9733                         (while arts
9734                           (when (gnus-data-unread-p (car arts))
9735                             (setq result (car arts)
9736                                   arts nil))
9737                           (setq arts (cdr arts)))
9738                         result)
9739                     (car arts)))
9740         (goto-char (gnus-data-pos result))
9741         (gnus-data-number result)))))
9742
9743 (defun gnus-summary-find-prev (&optional unread article)
9744   (let* ((eobp (eobp))
9745          (article (or article (gnus-summary-article-number)))
9746          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9747          result)
9748     (when (and (not eobp)
9749                (or (not gnus-summary-check-current)
9750                    (not unread)
9751                    (not (gnus-data-unread-p (car arts)))))
9752       (setq arts (cdr arts)))
9753     (if (setq result
9754               (if unread
9755                   (progn
9756                     (while arts
9757                       (and (gnus-data-unread-p (car arts))
9758                            (setq result (car arts)
9759                                  arts nil))
9760                       (setq arts (cdr arts)))
9761                     result)
9762                 (car arts)))
9763         (progn
9764           (goto-char (gnus-data-pos result))
9765           (gnus-data-number result)))))
9766
9767 (defun gnus-summary-find-subject (subject &optional unread backward article)
9768   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9769          (article (or article (gnus-summary-article-number)))
9770          (articles (gnus-data-list backward))
9771          (arts (gnus-data-find-list article articles))
9772          result)
9773     (when (or (not gnus-summary-check-current)
9774               (not unread)
9775               (not (gnus-data-unread-p (car arts))))
9776       (setq arts (cdr arts)))
9777     (while arts
9778       (and (or (not unread)
9779                (gnus-data-unread-p (car arts)))
9780            (vectorp (gnus-data-header (car arts)))
9781            (gnus-subject-equal
9782             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9783            (setq result (car arts)
9784                  arts nil))
9785       (setq arts (cdr arts)))
9786     (and result
9787          (goto-char (gnus-data-pos result))
9788          (gnus-data-number result))))
9789
9790 (defun gnus-summary-search-forward (&optional unread subject backward)
9791   "Search forward for an article.
9792 If UNREAD, look for unread articles.  If SUBJECT, look for
9793 articles with that subject.  If BACKWARD, search backward instead."
9794   (cond (subject (gnus-summary-find-subject subject unread backward))
9795         (backward (gnus-summary-find-prev unread))
9796         (t (gnus-summary-find-next unread))))
9797
9798 (defun gnus-recenter (&optional n)
9799   "Center point in window and redisplay frame.
9800 Also do horizontal recentering."
9801   (interactive "P")
9802   (when (and gnus-auto-center-summary
9803              (not (eq gnus-auto-center-summary 'vertical)))
9804     (gnus-horizontal-recenter))
9805   (recenter n))
9806
9807 (defun gnus-summary-recenter ()
9808   "Center point in the summary window.
9809 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9810 displayed, no centering will be performed."
9811   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9812   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9813   (let* ((top (cond ((< (window-height) 4) 0)
9814                     ((< (window-height) 7) 1)
9815                     (t 2)))
9816          (height (1- (window-height)))
9817          (bottom (save-excursion (goto-char (point-max))
9818                                  (forward-line (- height))
9819                                  (point)))
9820          (window (get-buffer-window (current-buffer))))
9821     ;; The user has to want it.
9822     (when gnus-auto-center-summary
9823       (when (get-buffer-window gnus-article-buffer)
9824        ;; Only do recentering when the article buffer is displayed,
9825        ;; Set the window start to either `bottom', which is the biggest
9826        ;; possible valid number, or the second line from the top,
9827        ;; whichever is the least.
9828        (set-window-start
9829         window (min bottom (save-excursion 
9830                              (forward-line (- top)) (point)))))
9831       ;; Do horizontal recentering while we're at it.
9832       (when (and (get-buffer-window (current-buffer) t)
9833                  (not (eq gnus-auto-center-summary 'vertical)))
9834         (let ((selected (selected-window)))
9835           (select-window (get-buffer-window (current-buffer) t))
9836           (gnus-summary-position-point)
9837           (gnus-horizontal-recenter)
9838           (select-window selected))))))
9839
9840 (defun gnus-horizontal-recenter ()
9841   "Recenter the current buffer horizontally."
9842   (if (< (current-column) (/ (window-width) 2))
9843       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9844     (let* ((orig (point))
9845            (end (window-end (get-buffer-window (current-buffer) t)))
9846            (max 0))
9847       ;; Find the longest line currently displayed in the window.
9848       (goto-char (window-start))
9849       (while (and (not (eobp)) 
9850                   (< (point) end))
9851         (end-of-line)
9852         (setq max (max max (current-column)))
9853         (forward-line 1))
9854       (goto-char orig)
9855       ;; Scroll horizontally to center (sort of) the point.
9856       (if (> max (window-width))
9857           (set-window-hscroll 
9858            (get-buffer-window (current-buffer) t)
9859            (min (- (current-column) (/ (window-width) 3))
9860                 (+ 2 (- max (window-width)))))
9861         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9862       max)))
9863
9864 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9865 (defun gnus-short-group-name (group &optional levels)
9866   "Collapse GROUP name LEVELS."
9867   (let* ((name "") 
9868          (foreign "")
9869          (depth 0) 
9870          (skip 1)
9871          (levels (or levels
9872                      (progn
9873                        (while (string-match "\\." group skip)
9874                          (setq skip (match-end 0)
9875                                depth (+ depth 1)))
9876                        depth))))
9877     (if (string-match ":" group)
9878         (setq foreign (substring group 0 (match-end 0))
9879               group (substring group (match-end 0))))
9880     (while group
9881       (if (and (string-match "\\." group)
9882                (> levels (- gnus-group-uncollapsed-levels 1)))
9883           (setq name (concat name (substring group 0 1))
9884                 group (substring group (match-end 0))
9885                 levels (- levels 1)
9886                 name (concat name "."))
9887         (setq name (concat foreign name group)
9888               group nil)))
9889     name))
9890
9891 (defun gnus-summary-jump-to-group (newsgroup)
9892   "Move point to NEWSGROUP in group mode buffer."
9893   ;; Keep update point of group mode buffer if visible.
9894   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9895       (save-window-excursion
9896         ;; Take care of tree window mode.
9897         (if (get-buffer-window gnus-group-buffer)
9898             (pop-to-buffer gnus-group-buffer))
9899         (gnus-group-jump-to-group newsgroup))
9900     (save-excursion
9901       ;; Take care of tree window mode.
9902       (if (get-buffer-window gnus-group-buffer)
9903           (pop-to-buffer gnus-group-buffer)
9904         (set-buffer gnus-group-buffer))
9905       (gnus-group-jump-to-group newsgroup))))
9906
9907 ;; This function returns a list of article numbers based on the
9908 ;; difference between the ranges of read articles in this group and
9909 ;; the range of active articles.
9910 (defun gnus-list-of-unread-articles (group)
9911   (let* ((read (gnus-info-read (gnus-get-info group)))
9912          (active (gnus-active group))
9913          (last (cdr active))
9914          first nlast unread)
9915     ;; If none are read, then all are unread.
9916     (if (not read)
9917         (setq first (car active))
9918       ;; If the range of read articles is a single range, then the
9919       ;; first unread article is the article after the last read
9920       ;; article.  Sounds logical, doesn't it?
9921       (if (not (listp (cdr read)))
9922           (setq first (1+ (cdr read)))
9923         ;; `read' is a list of ranges.
9924         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9925                                 (caar read))) 1)
9926             (setq first 1))
9927         (while read
9928           (if first
9929               (while (< first nlast)
9930                 (setq unread (cons first unread))
9931                 (setq first (1+ first))))
9932           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9933           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9934           (setq read (cdr read)))))
9935     ;; And add the last unread articles.
9936     (while (<= first last)
9937       (setq unread (cons first unread))
9938       (setq first (1+ first)))
9939     ;; Return the list of unread articles.
9940     (nreverse unread)))
9941
9942 (defun gnus-list-of-read-articles (group)
9943   "Return a list of unread, unticked and non-dormant articles."
9944   (let* ((info (gnus-get-info group))
9945          (marked (gnus-info-marks info))
9946          (active (gnus-active group)))
9947     (and info active
9948          (gnus-set-difference
9949           (gnus-sorted-complement
9950            (gnus-uncompress-range active)
9951            (gnus-list-of-unread-articles group))
9952           (append
9953            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9954            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9955
9956 ;; Various summary commands
9957
9958 (defun gnus-summary-universal-argument (arg)
9959   "Perform any operation on all articles that are process/prefixed."
9960   (interactive "P")
9961   (gnus-set-global-variables)
9962   (let ((articles (gnus-summary-work-articles arg))
9963         func article)
9964     (if (eq
9965          (setq
9966           func
9967           (key-binding
9968            (read-key-sequence
9969             (substitute-command-keys
9970              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9971              ))))
9972          'undefined)
9973         (gnus-error 1 "Undefined key")
9974       (save-excursion
9975         (while articles
9976           (gnus-summary-goto-subject (setq article (pop articles)))
9977           (command-execute func)
9978           (gnus-summary-remove-process-mark article)))))
9979   (gnus-summary-position-point))
9980
9981 (defun gnus-summary-toggle-truncation (&optional arg)
9982   "Toggle truncation of summary lines.
9983 With arg, turn line truncation on iff arg is positive."
9984   (interactive "P")
9985   (setq truncate-lines
9986         (if (null arg) (not truncate-lines)
9987           (> (prefix-numeric-value arg) 0)))
9988   (redraw-display))
9989
9990 (defun gnus-summary-reselect-current-group (&optional all rescan)
9991   "Exit and then reselect the current newsgroup.
9992 The prefix argument ALL means to select all articles."
9993   (interactive "P")
9994   (gnus-set-global-variables)
9995   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
9996     (error "Ephemeral groups can't be reselected"))
9997   (let ((current-subject (gnus-summary-article-number))
9998         (group gnus-newsgroup-name))
9999     (setq gnus-newsgroup-begin nil)
10000     (gnus-summary-exit)
10001     ;; We have to adjust the point of group mode buffer because the
10002     ;; current point was moved to the next unread newsgroup by
10003     ;; exiting.
10004     (gnus-summary-jump-to-group group)
10005     (when rescan
10006       (save-excursion
10007         (gnus-group-get-new-news-this-group 1)))
10008     (gnus-group-read-group all t)
10009     (gnus-summary-goto-subject current-subject nil t)))
10010
10011 (defun gnus-summary-rescan-group (&optional all)
10012   "Exit the newsgroup, ask for new articles, and select the newsgroup."
10013   (interactive "P")
10014   (gnus-summary-reselect-current-group all t))
10015
10016 (defun gnus-summary-update-info ()
10017   (let* ((group gnus-newsgroup-name))
10018     (when gnus-newsgroup-kill-headers
10019       (setq gnus-newsgroup-killed
10020             (gnus-compress-sequence
10021              (nconc
10022               (gnus-set-sorted-intersection
10023                (gnus-uncompress-range gnus-newsgroup-killed)
10024                (setq gnus-newsgroup-unselected
10025                      (sort gnus-newsgroup-unselected '<)))
10026               (setq gnus-newsgroup-unreads
10027                     (sort gnus-newsgroup-unreads '<))) t)))
10028     (unless (listp (cdr gnus-newsgroup-killed))
10029       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
10030     (let ((headers gnus-newsgroup-headers))
10031       (run-hooks 'gnus-exit-group-hook)
10032       (unless gnus-save-score
10033         (setq gnus-newsgroup-scored nil))
10034       ;; Set the new ranges of read articles.
10035       (gnus-update-read-articles
10036        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
10037       ;; Set the current article marks.
10038       (gnus-update-marks)
10039       ;; Do the cross-ref thing.
10040       (when gnus-use-cross-reference
10041         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
10042       ;; Do adaptive scoring, and possibly save score files.
10043       (when gnus-newsgroup-adaptive
10044         (gnus-score-adaptive))
10045       (when gnus-use-scoring
10046         (gnus-score-save))
10047       ;; Do not switch windows but change the buffer to work.
10048       (set-buffer gnus-group-buffer)
10049       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10050           (gnus-group-update-group group)))))
10051
10052 (defun gnus-summary-exit (&optional temporary)
10053   "Exit reading current newsgroup, and then return to group selection mode.
10054 gnus-exit-group-hook is called with no arguments if that value is non-nil."
10055   (interactive)
10056   (gnus-set-global-variables)
10057   (gnus-kill-save-kill-buffer)
10058   (let* ((group gnus-newsgroup-name)
10059          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
10060          (mode major-mode)
10061          (buf (current-buffer)))
10062     (run-hooks 'gnus-summary-prepare-exit-hook)
10063     ;; If we have several article buffers, we kill them at exit.
10064     (unless gnus-single-article-buffer
10065       (gnus-kill-buffer gnus-original-article-buffer)
10066       (setq gnus-article-current nil))
10067     (when gnus-use-cache
10068       (gnus-cache-possibly-remove-articles)
10069       (gnus-cache-save-buffers))
10070     (when gnus-use-trees
10071       (gnus-tree-close group))
10072     ;; Make all changes in this group permanent.
10073     (unless quit-config
10074       (gnus-summary-update-info))
10075     (gnus-close-group group)
10076     ;; Make sure where I was, and go to next newsgroup.
10077     (set-buffer gnus-group-buffer)
10078     (unless quit-config
10079       (gnus-group-jump-to-group group))
10080     (run-hooks 'gnus-summary-exit-hook)
10081     (unless quit-config
10082       (gnus-group-next-unread-group 1))
10083     (if temporary
10084         nil                             ;Nothing to do.
10085       ;; If we have several article buffers, we kill them at exit.
10086       (unless gnus-single-article-buffer
10087         (gnus-kill-buffer gnus-article-buffer)
10088         (gnus-kill-buffer gnus-original-article-buffer)
10089         (setq gnus-article-current nil))
10090       (set-buffer buf)
10091       (if (not gnus-kill-summary-on-exit)
10092           (gnus-deaden-summary)
10093         ;; We set all buffer-local variables to nil.  It is unclear why
10094         ;; this is needed, but if we don't, buffer-local variables are
10095         ;; not garbage-collected, it seems.  This would the lead to en
10096         ;; ever-growing Emacs.
10097         (gnus-summary-clear-local-variables)
10098         (when (get-buffer gnus-article-buffer)
10099           (bury-buffer gnus-article-buffer))
10100         ;; We clear the global counterparts of the buffer-local
10101         ;; variables as well, just to be on the safe side.
10102         (gnus-configure-windows 'group 'force)
10103         (gnus-summary-clear-local-variables)
10104         ;; Return to group mode buffer.
10105         (if (eq mode 'gnus-summary-mode)
10106             (gnus-kill-buffer buf)))
10107       (setq gnus-current-select-method gnus-select-method)
10108       (pop-to-buffer gnus-group-buffer)
10109       ;; Clear the current group name.
10110       (if (not quit-config)
10111           (progn
10112             (gnus-group-jump-to-group group)
10113             (gnus-group-next-unread-group 1)
10114             (gnus-configure-windows 'group 'force))
10115         (if (not (buffer-name (car quit-config)))
10116             (gnus-configure-windows 'group 'force)
10117           (set-buffer (car quit-config))
10118           (and (eq major-mode 'gnus-summary-mode)
10119                (gnus-set-global-variables))
10120           (gnus-configure-windows (cdr quit-config))))
10121       (unless quit-config
10122         (setq gnus-newsgroup-name nil)))))
10123
10124 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
10125 (defun gnus-summary-exit-no-update (&optional no-questions)
10126   "Quit reading current newsgroup without updating read article info."
10127   (interactive)
10128   (gnus-set-global-variables)
10129   (let* ((group gnus-newsgroup-name)
10130          (quit-config (gnus-group-quit-config group)))
10131     (when (or no-questions
10132               gnus-expert-user
10133               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
10134       ;; If we have several article buffers, we kill them at exit.
10135       (unless gnus-single-article-buffer
10136         (gnus-kill-buffer gnus-article-buffer)
10137         (gnus-kill-buffer gnus-original-article-buffer)
10138         (setq gnus-article-current nil))
10139       (if (not gnus-kill-summary-on-exit)
10140           (gnus-deaden-summary)
10141         (gnus-close-group group)
10142         (gnus-summary-clear-local-variables)
10143         (set-buffer gnus-group-buffer)
10144         (gnus-summary-clear-local-variables)
10145         (when (get-buffer gnus-summary-buffer)
10146           (kill-buffer gnus-summary-buffer)))
10147       (unless gnus-single-article-buffer
10148         (setq gnus-article-current nil))
10149       (when gnus-use-trees
10150         (gnus-tree-close group))
10151       (when (get-buffer gnus-article-buffer)
10152         (bury-buffer gnus-article-buffer))
10153       ;; Return to the group buffer.
10154       (gnus-configure-windows 'group 'force)
10155       ;; Clear the current group name.
10156       (setq gnus-newsgroup-name nil)
10157       (when (equal (gnus-group-group-name) group)
10158         (gnus-group-next-unread-group 1))
10159       (when quit-config
10160         (if (not (buffer-name (car quit-config)))
10161             (gnus-configure-windows 'group 'force)
10162           (set-buffer (car quit-config))
10163           (when (eq major-mode 'gnus-summary-mode)
10164             (gnus-set-global-variables))
10165           (gnus-configure-windows (cdr quit-config)))))))
10166
10167 ;;; Dead summaries.
10168
10169 (defvar gnus-dead-summary-mode-map nil)
10170
10171 (if gnus-dead-summary-mode-map
10172     nil
10173   (setq gnus-dead-summary-mode-map (make-keymap))
10174   (suppress-keymap gnus-dead-summary-mode-map)
10175   (substitute-key-definition
10176    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10177   (let ((keys '("\C-d" "\r" "\177")))
10178     (while keys
10179       (define-key gnus-dead-summary-mode-map
10180         (pop keys) 'gnus-summary-wake-up-the-dead))))
10181
10182 (defvar gnus-dead-summary-mode nil
10183   "Minor mode for Gnus summary buffers.")
10184
10185 (defun gnus-dead-summary-mode (&optional arg)
10186   "Minor mode for Gnus summary buffers."
10187   (interactive "P")
10188   (when (eq major-mode 'gnus-summary-mode)
10189     (make-local-variable 'gnus-dead-summary-mode)
10190     (setq gnus-dead-summary-mode
10191           (if (null arg) (not gnus-dead-summary-mode)
10192             (> (prefix-numeric-value arg) 0)))
10193     (when gnus-dead-summary-mode
10194       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10195         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10196       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10197         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10198               minor-mode-map-alist)))))
10199
10200 (defun gnus-deaden-summary ()
10201   "Make the current summary buffer into a dead summary buffer."
10202   ;; Kill any previous dead summary buffer.
10203   (when (and gnus-dead-summary
10204              (buffer-name gnus-dead-summary))
10205     (save-excursion
10206       (set-buffer gnus-dead-summary)
10207       (when gnus-dead-summary-mode
10208         (kill-buffer (current-buffer)))))
10209   ;; Make this the current dead summary.
10210   (setq gnus-dead-summary (current-buffer))
10211   (gnus-dead-summary-mode 1)
10212   (let ((name (buffer-name)))
10213     (when (string-match "Summary" name)
10214       (rename-buffer
10215        (concat (substring name 0 (match-beginning 0)) "Dead "
10216                (substring name (match-beginning 0))) t))))
10217
10218 (defun gnus-kill-or-deaden-summary (buffer)
10219   "Kill or deaden the summary BUFFER."
10220   (when (and (buffer-name buffer)
10221              (not gnus-single-article-buffer))
10222     (save-excursion
10223       (set-buffer buffer)
10224       (gnus-kill-buffer gnus-article-buffer)
10225       (gnus-kill-buffer gnus-original-article-buffer)))
10226   (cond (gnus-kill-summary-on-exit
10227          (when (and gnus-use-trees
10228                     (and (get-buffer buffer)
10229                          (buffer-name (get-buffer buffer))))
10230            (save-excursion
10231              (set-buffer (get-buffer buffer))
10232              (gnus-tree-close gnus-newsgroup-name)))
10233          (gnus-kill-buffer buffer))
10234         ((and (get-buffer buffer)
10235               (buffer-name (get-buffer buffer)))
10236          (save-excursion
10237            (set-buffer buffer)
10238            (gnus-deaden-summary)))))
10239
10240 (defun gnus-summary-wake-up-the-dead (&rest args)
10241   "Wake up the dead summary buffer."
10242   (interactive)
10243   (gnus-dead-summary-mode -1)
10244   (let ((name (buffer-name)))
10245     (when (string-match "Dead " name)
10246       (rename-buffer
10247        (concat (substring name 0 (match-beginning 0))
10248                (substring name (match-end 0))) t)))
10249   (gnus-message 3 "This dead summary is now alive again"))
10250
10251 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10252 (defun gnus-summary-fetch-faq (&optional faq-dir)
10253   "Fetch the FAQ for the current group.
10254 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10255 in."
10256   (interactive
10257    (list
10258     (if current-prefix-arg
10259         (completing-read
10260          "Faq dir: " (and (listp gnus-group-faq-directory)
10261                           gnus-group-faq-directory)))))
10262   (let (gnus-faq-buffer)
10263     (and (setq gnus-faq-buffer
10264                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10265          (gnus-configure-windows 'summary-faq))))
10266
10267 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10268 (defun gnus-summary-describe-group (&optional force)
10269   "Describe the current newsgroup."
10270   (interactive "P")
10271   (gnus-group-describe-group force gnus-newsgroup-name))
10272
10273 (defun gnus-summary-describe-briefly ()
10274   "Describe summary mode commands briefly."
10275   (interactive)
10276   (gnus-message 6
10277                 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-summary-describe-briefly]:This help")))
10278
10279 ;; Walking around group mode buffer from summary mode.
10280
10281 (defun gnus-summary-next-group (&optional no-article target-group backward)
10282   "Exit current newsgroup and then select next unread newsgroup.
10283 If prefix argument NO-ARTICLE is non-nil, no article is selected
10284 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10285 previous group instead."
10286   (interactive "P")
10287   (gnus-set-global-variables)
10288   (let ((current-group gnus-newsgroup-name)
10289         (current-buffer (current-buffer))
10290         entered)
10291     ;; First we semi-exit this group to update Xrefs and all variables.
10292     ;; We can't do a real exit, because the window conf must remain
10293     ;; the same in case the user is prompted for info, and we don't
10294     ;; want the window conf to change before that...
10295     (gnus-summary-exit t)
10296     (while (not entered)
10297       ;; Then we find what group we are supposed to enter.
10298       (set-buffer gnus-group-buffer)
10299       (gnus-group-jump-to-group current-group)
10300       (setq target-group
10301             (or target-group
10302                 (if (eq gnus-keep-same-level 'best)
10303                     (gnus-summary-best-group gnus-newsgroup-name)
10304                   (gnus-summary-search-group backward gnus-keep-same-level))))
10305       (if (not target-group)
10306           ;; There are no further groups, so we return to the group
10307           ;; buffer.
10308           (progn
10309             (gnus-message 5 "Returning to the group buffer")
10310             (setq entered t)
10311             (set-buffer current-buffer)
10312             (gnus-summary-exit))
10313         ;; We try to enter the target group.
10314         (gnus-group-jump-to-group target-group)
10315         (let ((unreads (gnus-group-group-unread)))
10316           (if (and (or (eq t unreads)
10317                        (and unreads (not (zerop unreads))))
10318                    (gnus-summary-read-group
10319                     target-group nil no-article current-buffer))
10320               (setq entered t)
10321             (setq current-group target-group
10322                   target-group nil)))))))
10323
10324 (defun gnus-summary-prev-group (&optional no-article)
10325   "Exit current newsgroup and then select previous unread newsgroup.
10326 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10327   (interactive "P")
10328   (gnus-summary-next-group no-article nil t))
10329
10330 ;; Walking around summary lines.
10331
10332 (defun gnus-summary-first-subject (&optional unread)
10333   "Go to the first unread subject.
10334 If UNREAD is non-nil, go to the first unread article.
10335 Returns the article selected or nil if there are no unread articles."
10336   (interactive "P")
10337   (prog1
10338       (cond
10339        ;; Empty summary.
10340        ((null gnus-newsgroup-data)
10341         (gnus-message 3 "No articles in the group")
10342         nil)
10343        ;; Pick the first article.
10344        ((not unread)
10345         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10346         (gnus-data-number (car gnus-newsgroup-data)))
10347        ;; No unread articles.
10348        ((null gnus-newsgroup-unreads)
10349         (gnus-message 3 "No more unread articles")
10350         nil)
10351        ;; Find the first unread article.
10352        (t
10353         (let ((data gnus-newsgroup-data))
10354           (while (and data
10355                       (not (gnus-data-unread-p (car data))))
10356             (setq data (cdr data)))
10357           (if data
10358               (progn
10359                 (goto-char (gnus-data-pos (car data)))
10360                 (gnus-data-number (car data)))))))
10361     (gnus-summary-position-point)))
10362
10363 (defun gnus-summary-next-subject (n &optional unread dont-display)
10364   "Go to next N'th summary line.
10365 If N is negative, go to the previous N'th subject line.
10366 If UNREAD is non-nil, only unread articles are selected.
10367 The difference between N and the actual number of steps taken is
10368 returned."
10369   (interactive "p")
10370   (let ((backward (< n 0))
10371         (n (abs n)))
10372     (while (and (> n 0)
10373                 (if backward
10374                     (gnus-summary-find-prev unread)
10375                   (gnus-summary-find-next unread)))
10376       (setq n (1- n)))
10377     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10378                                (if unread " unread" "")))
10379     (unless dont-display
10380       (gnus-summary-recenter)
10381       (gnus-summary-position-point))
10382     n))
10383
10384 (defun gnus-summary-next-unread-subject (n)
10385   "Go to next N'th unread summary line."
10386   (interactive "p")
10387   (gnus-summary-next-subject n t))
10388
10389 (defun gnus-summary-prev-subject (n &optional unread)
10390   "Go to previous N'th summary line.
10391 If optional argument UNREAD is non-nil, only unread article is selected."
10392   (interactive "p")
10393   (gnus-summary-next-subject (- n) unread))
10394
10395 (defun gnus-summary-prev-unread-subject (n)
10396   "Go to previous N'th unread summary line."
10397   (interactive "p")
10398   (gnus-summary-next-subject (- n) t))
10399
10400 (defun gnus-summary-goto-subject (article &optional force silent)
10401   "Go the subject line of ARTICLE.
10402 If FORCE, also allow jumping to articles not currently shown."
10403   (let ((b (point))
10404         (data (gnus-data-find article)))
10405     ;; We read in the article if we have to.
10406     (and (not data)
10407          force
10408          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10409          (setq data (gnus-data-find article)))
10410     (goto-char b)
10411     (if (not data)
10412         (progn
10413           (unless silent
10414             (gnus-message 3 "Can't find article %d" article))
10415           nil)
10416       (goto-char (gnus-data-pos data))
10417       article)))
10418
10419 ;; Walking around summary lines with displaying articles.
10420
10421 (defun gnus-summary-expand-window (&optional arg)
10422   "Make the summary buffer take up the entire Emacs frame.
10423 Given a prefix, will force an `article' buffer configuration."
10424   (interactive "P")
10425   (gnus-set-global-variables)
10426   (if arg
10427       (gnus-configure-windows 'article 'force)
10428     (gnus-configure-windows 'summary 'force)))
10429
10430 (defun gnus-summary-display-article (article &optional all-header)
10431   "Display ARTICLE in article buffer."
10432   (gnus-set-global-variables)
10433   (if (null article)
10434       nil
10435     (prog1
10436         (if gnus-summary-display-article-function
10437             (funcall gnus-summary-display-article-function article all-header)
10438           (gnus-article-prepare article all-header))
10439       (run-hooks 'gnus-select-article-hook)
10440       (unless (zerop gnus-current-article)
10441         (gnus-summary-goto-subject gnus-current-article))
10442       (gnus-summary-recenter)
10443       (when gnus-use-trees
10444         (gnus-possibly-generate-tree article)
10445         (gnus-highlight-selected-tree article))
10446       ;; Successfully display article.
10447       (gnus-article-set-window-start
10448        (cdr (assq article gnus-newsgroup-bookmarks))))))
10449
10450 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10451   "Select the current article.
10452 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10453 non-nil, the article will be re-fetched even if it already present in
10454 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10455 be displayed."
10456   ;; Make sure we are in the summary buffer to work around bbdb bug.
10457   (unless (eq major-mode 'gnus-summary-mode)
10458     (set-buffer gnus-summary-buffer))
10459   (let ((article (or article (gnus-summary-article-number)))
10460         (all-headers (not (not all-headers))) ;Must be T or NIL.
10461         gnus-summary-display-article-function
10462         did)
10463     (and (not pseudo)
10464          (gnus-summary-article-pseudo-p article)
10465          (error "This is a pseudo-article."))
10466     (prog1
10467         (save-excursion
10468           (set-buffer gnus-summary-buffer)
10469           (if (or (and gnus-single-article-buffer
10470                        (or (null gnus-current-article)
10471                            (null gnus-article-current)
10472                            (null (get-buffer gnus-article-buffer))
10473                            (not (eq article (cdr gnus-article-current)))
10474                            (not (equal (car gnus-article-current)
10475                                        gnus-newsgroup-name))))
10476                   (and (not gnus-single-article-buffer)
10477                        (or (null gnus-current-article)
10478                            (not (eq gnus-current-article article))))
10479                   force)
10480               ;; The requested article is different from the current article.
10481               (prog1
10482                   (gnus-summary-display-article article all-headers)
10483                 (setq did article))
10484             (if (or all-headers gnus-show-all-headers)
10485                 (gnus-article-show-all-headers))
10486             'old))
10487       (if did
10488           (gnus-article-set-window-start
10489            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10490
10491 (defun gnus-summary-set-current-mark (&optional current-mark)
10492   "Obsolete function."
10493   nil)
10494
10495 (defun gnus-summary-next-article (&optional unread subject backward push)
10496   "Select the next article.
10497 If UNREAD, only unread articles are selected.
10498 If SUBJECT, only articles with SUBJECT are selected.
10499 If BACKWARD, the previous article is selected instead of the next."
10500   (interactive "P")
10501   (gnus-set-global-variables)
10502   (cond
10503    ;; Is there such an article?
10504    ((and (gnus-summary-search-forward unread subject backward)
10505          (or (gnus-summary-display-article (gnus-summary-article-number))
10506              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10507     (gnus-summary-position-point))
10508    ;; If not, we try the first unread, if that is wanted.
10509    ((and subject
10510          gnus-auto-select-same
10511          (gnus-summary-first-unread-article))
10512     (gnus-summary-position-point)
10513     (gnus-message 6 "Wrapped"))
10514    ;; Try to get next/previous article not displayed in this group.
10515    ((and gnus-auto-extend-newsgroup
10516          (not unread) (not subject))
10517     (gnus-summary-goto-article
10518      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10519      nil t))
10520    ;; Go to next/previous group.
10521    (t
10522     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10523         (gnus-summary-jump-to-group gnus-newsgroup-name))
10524     (let ((cmd last-command-char)
10525           (group
10526            (if (eq gnus-keep-same-level 'best)
10527                (gnus-summary-best-group gnus-newsgroup-name)
10528              (gnus-summary-search-group backward gnus-keep-same-level))))
10529       ;; For some reason, the group window gets selected.  We change
10530       ;; it back.
10531       (select-window (get-buffer-window (current-buffer)))
10532       ;; Select next unread newsgroup automagically.
10533       (cond
10534        ((or (not gnus-auto-select-next)
10535             (not cmd))
10536         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10537        ((or (eq gnus-auto-select-next 'quietly)
10538             (and (eq gnus-auto-select-next 'slightly-quietly)
10539                  push)
10540             (and (eq gnus-auto-select-next 'almost-quietly)
10541                  (gnus-summary-last-article-p)))
10542         ;; Select quietly.
10543         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10544             (gnus-summary-exit)
10545           (gnus-message 7 "No more%s articles (%s)..."
10546                         (if unread " unread" "")
10547                         (if group (concat "selecting " group)
10548                           "exiting"))
10549           (gnus-summary-next-group nil group backward)))
10550        (t
10551         (gnus-summary-walk-group-buffer
10552          gnus-newsgroup-name cmd unread backward)))))))
10553
10554 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10555   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10556                       (?\C-p (gnus-group-prev-unread-group 1))))
10557         keve key group ended)
10558     (save-excursion
10559       (set-buffer gnus-group-buffer)
10560       (gnus-summary-jump-to-group from-group)
10561       (setq group
10562             (if (eq gnus-keep-same-level 'best)
10563                 (gnus-summary-best-group gnus-newsgroup-name)
10564               (gnus-summary-search-group backward gnus-keep-same-level))))
10565     (while (not ended)
10566       (gnus-message
10567        5 "No more%s articles%s" (if unread " unread" "")
10568        (if (and group
10569                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10570            (format " (Type %s for %s [%s])"
10571                    (single-key-description cmd) group
10572                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10573          (format " (Type %s to exit %s)"
10574                  (single-key-description cmd)
10575                  gnus-newsgroup-name)))
10576       ;; Confirm auto selection.
10577       (setq key (car (setq keve (gnus-read-event-char))))
10578       (setq ended t)
10579       (cond
10580        ((assq key keystrokes)
10581         (let ((obuf (current-buffer)))
10582           (switch-to-buffer gnus-group-buffer)
10583           (and group
10584                (gnus-group-jump-to-group group))
10585           (eval (cadr (assq key keystrokes)))
10586           (setq group (gnus-group-group-name))
10587           (switch-to-buffer obuf))
10588         (setq ended nil))
10589        ((equal key cmd)
10590         (if (or (not group)
10591                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10592             (gnus-summary-exit)
10593           (gnus-summary-next-group nil group backward)))
10594        (t
10595         (push (cdr keve) unread-command-events))))))
10596
10597 (defun gnus-read-event-char ()
10598   "Get the next event."
10599   (let ((event (read-event)))
10600     (cons (and (numberp event) event) event)))
10601
10602 (defun gnus-summary-next-unread-article ()
10603   "Select unread article after current one."
10604   (interactive)
10605   (gnus-summary-next-article t (and gnus-auto-select-same
10606                                     (gnus-summary-article-subject))))
10607
10608 (defun gnus-summary-prev-article (&optional unread subject)
10609   "Select the article after the current one.
10610 If UNREAD is non-nil, only unread articles are selected."
10611   (interactive "P")
10612   (gnus-summary-next-article unread subject t))
10613
10614 (defun gnus-summary-prev-unread-article ()
10615   "Select unred article before current one."
10616   (interactive)
10617   (gnus-summary-prev-article t (and gnus-auto-select-same
10618                                     (gnus-summary-article-subject))))
10619
10620 (defun gnus-summary-next-page (&optional lines circular)
10621   "Show next page of the selected article.
10622 If at the end of the current article, select the next article.
10623 LINES says how many lines should be scrolled up.
10624
10625 If CIRCULAR is non-nil, go to the start of the article instead of
10626 selecting the next article when reaching the end of the current
10627 article."
10628   (interactive "P")
10629   (setq gnus-summary-buffer (current-buffer))
10630   (gnus-set-global-variables)
10631   (let ((article (gnus-summary-article-number))
10632         (endp nil))
10633     (gnus-configure-windows 'article)
10634     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10635         (if (and (eq gnus-summary-goto-unread 'never)
10636                  (not (gnus-summary-last-article-p article)))
10637             (gnus-summary-next-article)
10638           (gnus-summary-next-unread-article))
10639       (if (or (null gnus-current-article)
10640               (null gnus-article-current)
10641               (/= article (cdr gnus-article-current))
10642               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10643           ;; Selected subject is different from current article's.
10644           (gnus-summary-display-article article)
10645         (gnus-eval-in-buffer-window gnus-article-buffer
10646           (setq endp (gnus-article-next-page lines)))
10647         (if endp
10648             (cond (circular
10649                    (gnus-summary-beginning-of-article))
10650                   (lines
10651                    (gnus-message 3 "End of message"))
10652                   ((null lines)
10653                    (if (and (eq gnus-summary-goto-unread 'never)
10654                             (not (gnus-summary-last-article-p article)))
10655                        (gnus-summary-next-article)
10656                      (gnus-summary-next-unread-article)))))))
10657     (gnus-summary-recenter)
10658     (gnus-summary-position-point)))
10659
10660 (defun gnus-summary-prev-page (&optional lines)
10661   "Show previous page of selected article.
10662 Argument LINES specifies lines to be scrolled down."
10663   (interactive "P")
10664   (gnus-set-global-variables)
10665   (let ((article (gnus-summary-article-number)))
10666     (gnus-configure-windows 'article)
10667     (if (or (null gnus-current-article)
10668             (null gnus-article-current)
10669             (/= article (cdr gnus-article-current))
10670             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10671         ;; Selected subject is different from current article's.
10672         (gnus-summary-display-article article)
10673       (gnus-summary-recenter)
10674       (gnus-eval-in-buffer-window gnus-article-buffer
10675         (gnus-article-prev-page lines))))
10676   (gnus-summary-position-point))
10677
10678 (defun gnus-summary-scroll-up (lines)
10679   "Scroll up (or down) one line current article.
10680 Argument LINES specifies lines to be scrolled up (or down if negative)."
10681   (interactive "p")
10682   (gnus-set-global-variables)
10683   (gnus-configure-windows 'article)
10684   (gnus-summary-show-thread)
10685   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10686     (gnus-eval-in-buffer-window gnus-article-buffer
10687       (cond ((> lines 0)
10688              (if (gnus-article-next-page lines)
10689                  (gnus-message 3 "End of message")))
10690             ((< lines 0)
10691              (gnus-article-prev-page (- lines))))))
10692   (gnus-summary-recenter)
10693   (gnus-summary-position-point))
10694
10695 (defun gnus-summary-next-same-subject ()
10696   "Select next article which has the same subject as current one."
10697   (interactive)
10698   (gnus-set-global-variables)
10699   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10700
10701 (defun gnus-summary-prev-same-subject ()
10702   "Select previous article which has the same subject as current one."
10703   (interactive)
10704   (gnus-set-global-variables)
10705   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10706
10707 (defun gnus-summary-next-unread-same-subject ()
10708   "Select next unread article which has the same subject as current one."
10709   (interactive)
10710   (gnus-set-global-variables)
10711   (gnus-summary-next-article t (gnus-summary-article-subject)))
10712
10713 (defun gnus-summary-prev-unread-same-subject ()
10714   "Select previous unread article which has the same subject as current one."
10715   (interactive)
10716   (gnus-set-global-variables)
10717   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10718
10719 (defun gnus-summary-first-unread-article ()
10720   "Select the first unread article.
10721 Return nil if there are no unread articles."
10722   (interactive)
10723   (gnus-set-global-variables)
10724   (prog1
10725       (if (gnus-summary-first-subject t)
10726           (progn
10727             (gnus-summary-show-thread)
10728             (gnus-summary-first-subject t)
10729             (gnus-summary-display-article (gnus-summary-article-number))))
10730     (gnus-summary-position-point)))
10731
10732 (defun gnus-summary-best-unread-article ()
10733   "Select the unread article with the highest score."
10734   (interactive)
10735   (gnus-set-global-variables)
10736   (let ((best -1000000)
10737         (data gnus-newsgroup-data)
10738         article score)
10739     (while data
10740       (and (gnus-data-unread-p (car data))
10741            (> (setq score
10742                     (gnus-summary-article-score (gnus-data-number (car data))))
10743               best)
10744            (setq best score
10745                  article (gnus-data-number (car data))))
10746       (setq data (cdr data)))
10747     (prog1
10748         (if article
10749             (gnus-summary-goto-article article)
10750           (error "No unread articles"))
10751       (gnus-summary-position-point))))
10752
10753 (defun gnus-summary-last-subject ()
10754   "Go to the last displayed subject line in the group."
10755   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10756     (when article
10757       (gnus-summary-goto-subject article))))
10758
10759 (defun gnus-summary-goto-article (article &optional all-headers force)
10760   "Fetch ARTICLE and display it if it exists.
10761 If ALL-HEADERS is non-nil, no header lines are hidden."
10762   (interactive
10763    (list
10764     (string-to-int
10765      (completing-read
10766       "Article number: "
10767       (mapcar (lambda (number) (list (int-to-string number)))
10768               gnus-newsgroup-limit)))
10769     current-prefix-arg
10770     t))
10771   (prog1
10772       (if (gnus-summary-goto-subject article force)
10773           (gnus-summary-display-article article all-headers)
10774         (gnus-message 4 "Couldn't go to article %s" article) nil)
10775     (gnus-summary-position-point)))
10776
10777 (defun gnus-summary-goto-last-article ()
10778   "Go to the previously read article."
10779   (interactive)
10780   (prog1
10781       (and gnus-last-article
10782            (gnus-summary-goto-article gnus-last-article))
10783     (gnus-summary-position-point)))
10784
10785 (defun gnus-summary-pop-article (number)
10786   "Pop one article off the history and go to the previous.
10787 NUMBER articles will be popped off."
10788   (interactive "p")
10789   (let (to)
10790     (setq gnus-newsgroup-history
10791           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10792     (if to
10793         (gnus-summary-goto-article (car to))
10794       (error "Article history empty")))
10795   (gnus-summary-position-point))
10796
10797 ;; Summary commands and functions for limiting the summary buffer.
10798
10799 (defun gnus-summary-limit-to-articles (n)
10800   "Limit the summary buffer to the next N articles.
10801 If not given a prefix, use the process marked articles instead."
10802   (interactive "P")
10803   (gnus-set-global-variables)
10804   (prog1
10805       (let ((articles (gnus-summary-work-articles n)))
10806         (setq gnus-newsgroup-processable nil)
10807         (gnus-summary-limit articles))
10808     (gnus-summary-position-point)))
10809
10810 (defun gnus-summary-pop-limit (&optional total)
10811   "Restore the previous limit.
10812 If given a prefix, remove all limits."
10813   (interactive "P")
10814   (gnus-set-global-variables)
10815   (when total 
10816     (setq gnus-newsgroup-limits
10817           (list (mapcar (lambda (h) (mail-header-number h))
10818                         gnus-newsgroup-headers))))
10819   (unless gnus-newsgroup-limits
10820     (error "No limit to pop"))
10821   (prog1
10822       (gnus-summary-limit nil 'pop)
10823     (gnus-summary-position-point)))
10824
10825 (defun gnus-summary-limit-to-subject (subject &optional header)
10826   "Limit the summary buffer to articles that have subjects that match a regexp."
10827   (interactive "sRegexp: ")
10828   (unless header
10829     (setq header "subject"))
10830   (when (not (equal "" subject))
10831     (prog1
10832         (let ((articles (gnus-summary-find-matching
10833                          (or header "subject") subject 'all)))
10834           (or articles (error "Found no matches for \"%s\"" subject))
10835           (gnus-summary-limit articles))
10836       (gnus-summary-position-point))))
10837
10838 (defun gnus-summary-limit-to-author (from)
10839   "Limit the summary buffer to articles that have authors that match a regexp."
10840   (interactive "sRegexp: ")
10841   (gnus-summary-limit-to-subject from "from"))
10842
10843 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10844 (make-obsolete
10845  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10846
10847 (defun gnus-summary-limit-to-unread (&optional all)
10848   "Limit the summary buffer to articles that are not marked as read.
10849 If ALL is non-nil, limit strictly to unread articles."
10850   (interactive "P")
10851   (if all
10852       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10853     (gnus-summary-limit-to-marks
10854      ;; Concat all the marks that say that an article is read and have
10855      ;; those removed.
10856      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10857            gnus-killed-mark gnus-kill-file-mark
10858            gnus-low-score-mark gnus-expirable-mark
10859            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10860      'reverse)))
10861
10862 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10863 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10864
10865 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10866   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10867 If REVERSE, limit the summary buffer to articles that are not marked
10868 with MARKS.  MARKS can either be a string of marks or a list of marks.
10869 Returns how many articles were removed."
10870   (interactive "sMarks: ")
10871   (gnus-set-global-variables)
10872   (prog1
10873       (let ((data gnus-newsgroup-data)
10874             (marks (if (listp marks) marks
10875                      (append marks nil))) ; Transform to list.
10876             articles)
10877         (while data
10878           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10879                  (memq (gnus-data-mark (car data)) marks))
10880                (setq articles (cons (gnus-data-number (car data)) articles)))
10881           (setq data (cdr data)))
10882         (gnus-summary-limit articles))
10883     (gnus-summary-position-point)))
10884
10885 (defun gnus-summary-limit-to-score (&optional score)
10886   "Limit to articles with score at or above SCORE."
10887   (interactive "P")
10888   (gnus-set-global-variables)
10889   (setq score (if score
10890                   (prefix-numeric-value score)
10891                 (or gnus-summary-default-score 0)))
10892   (let ((data gnus-newsgroup-data)
10893         articles)
10894     (while data
10895       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10896                 score)
10897         (push (gnus-data-number (car data)) articles))
10898       (setq data (cdr data)))
10899     (prog1
10900         (gnus-summary-limit articles)
10901       (gnus-summary-position-point))))
10902
10903 (defun gnus-summary-limit-include-dormant ()
10904   "Display all the hidden articles that are marked as dormant."
10905   (interactive)
10906   (gnus-set-global-variables)
10907   (or gnus-newsgroup-dormant
10908       (error "There are no dormant articles in this group"))
10909   (prog1
10910       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10911     (gnus-summary-position-point)))
10912
10913 (defun gnus-summary-limit-exclude-dormant ()
10914   "Hide all dormant articles."
10915   (interactive)
10916   (gnus-set-global-variables)
10917   (prog1
10918       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10919     (gnus-summary-position-point)))
10920
10921 (defun gnus-summary-limit-exclude-childless-dormant ()
10922   "Hide all dormant articles that have no children."
10923   (interactive)
10924   (gnus-set-global-variables)
10925   (let ((data (gnus-data-list t))
10926         articles d children)
10927     ;; Find all articles that are either not dormant or have
10928     ;; children.
10929     (while (setq d (pop data))
10930       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10931                 (and (setq children 
10932                            (gnus-article-children (gnus-data-number d)))
10933                      (let (found)
10934                        (while children
10935                          (when (memq (car children) articles)
10936                            (setq children nil
10937                                  found t))
10938                          (pop children))
10939                        found)))
10940         (push (gnus-data-number d) articles)))
10941     ;; Do the limiting.
10942     (prog1
10943         (gnus-summary-limit articles)
10944       (gnus-summary-position-point))))
10945
10946 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10947   "Mark all unread excluded articles as read.
10948 If ALL, mark even excluded ticked and dormants as read."
10949   (interactive "P")
10950   (let ((articles (gnus-sorted-complement
10951                    (sort
10952                     (mapcar (lambda (h) (mail-header-number h))
10953                             gnus-newsgroup-headers)
10954                     '<)
10955                    (sort gnus-newsgroup-limit '<)))
10956         article)
10957     (setq gnus-newsgroup-unreads nil)
10958     (if all
10959         (setq gnus-newsgroup-dormant nil
10960               gnus-newsgroup-marked nil
10961               gnus-newsgroup-reads
10962               (nconc
10963                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10964                gnus-newsgroup-reads))
10965       (while (setq article (pop articles))
10966         (unless (or (memq article gnus-newsgroup-dormant)
10967                     (memq article gnus-newsgroup-marked))
10968           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10969
10970 (defun gnus-summary-limit (articles &optional pop)
10971   (if pop
10972       ;; We pop the previous limit off the stack and use that.
10973       (setq articles (car gnus-newsgroup-limits)
10974             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10975     ;; We use the new limit, so we push the old limit on the stack.
10976     (setq gnus-newsgroup-limits
10977           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10978   ;; Set the limit.
10979   (setq gnus-newsgroup-limit articles)
10980   (let ((total (length gnus-newsgroup-data))
10981         (data (gnus-data-find-list (gnus-summary-article-number)))
10982         (gnus-summary-mark-below nil)   ; Inhibit this.
10983         found)
10984     ;; This will do all the work of generating the new summary buffer
10985     ;; according to the new limit.
10986     (gnus-summary-prepare)
10987     ;; Hide any threads, possibly.
10988     (and gnus-show-threads
10989          gnus-thread-hide-subtree
10990          (gnus-summary-hide-all-threads))
10991     ;; Try to return to the article you were at, or one in the
10992     ;; neighborhood.
10993     (if data
10994         ;; We try to find some article after the current one.
10995         (while data
10996           (and (gnus-summary-goto-subject
10997                 (gnus-data-number (car data)) nil t)
10998                (setq data nil
10999                      found t))
11000           (setq data (cdr data))))
11001     (or found
11002         ;; If there is no data, that means that we were after the last
11003         ;; article.  The same goes when we can't find any articles
11004         ;; after the current one.
11005         (progn
11006           (goto-char (point-max))
11007           (gnus-summary-find-prev)))
11008     ;; We return how many articles were removed from the summary
11009     ;; buffer as a result of the new limit.
11010     (- total (length gnus-newsgroup-data))))
11011
11012 (defsubst gnus-invisible-cut-children (threads)
11013   (let ((num 0))
11014     (while threads
11015       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
11016         (incf num))
11017       (pop threads))
11018     (< num 2)))
11019
11020 (defsubst gnus-cut-thread (thread)
11021   "Go forwards in the thread until we find an article that we want to display."
11022   (when (or (eq gnus-fetch-old-headers 'some)
11023             (eq gnus-build-sparse-threads 'some)
11024             (eq gnus-build-sparse-threads 'more))
11025     ;; Deal with old-fetched headers and sparse threads.
11026     (while (and
11027             thread
11028             (or
11029              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
11030              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
11031             (or (<= (length (cdr thread)) 1)
11032                 (gnus-invisible-cut-children (cdr thread))))
11033       (setq thread (cadr thread))))
11034   thread)
11035
11036 (defun gnus-cut-threads (threads)
11037   "Cut off all uninteresting articles from the beginning of threads."
11038   (when (or (eq gnus-fetch-old-headers 'some)
11039             (eq gnus-build-sparse-threads 'some)
11040             (eq gnus-build-sparse-threads 'more))
11041     (let ((th threads))
11042       (while th
11043         (setcar th (gnus-cut-thread (car th)))
11044         (setq th (cdr th)))))
11045   ;; Remove nixed out threads.
11046   (delq nil threads))
11047
11048 (defun gnus-summary-initial-limit (&optional show-if-empty)
11049   "Figure out what the initial limit is supposed to be on group entry.
11050 This entails weeding out unwanted dormants, low-scored articles,
11051 fetch-old-headers verbiage, and so on."
11052   ;; Most groups have nothing to remove.
11053   (if (or gnus-inhibit-limiting
11054           (and (null gnus-newsgroup-dormant)
11055                (not (eq gnus-fetch-old-headers 'some))
11056                (null gnus-summary-expunge-below)
11057                (not (eq gnus-build-sparse-threads 'some))
11058                (not (eq gnus-build-sparse-threads 'more))
11059                (null gnus-thread-expunge-below)
11060                (not gnus-use-nocem)))
11061       () ; Do nothing.
11062     (push gnus-newsgroup-limit gnus-newsgroup-limits)
11063     (setq gnus-newsgroup-limit nil)
11064     (mapatoms
11065      (lambda (node)
11066        (unless (car (symbol-value node))
11067          ;; These threads have no parents -- they are roots.
11068          (let ((nodes (cdr (symbol-value node)))
11069                thread)
11070            (while nodes
11071              (if (and gnus-thread-expunge-below
11072                       (< (gnus-thread-total-score (car nodes))
11073                          gnus-thread-expunge-below))
11074                  (gnus-expunge-thread (pop nodes))
11075                (setq thread (pop nodes))
11076                (gnus-summary-limit-children thread))))))
11077      gnus-newsgroup-dependencies)
11078     ;; If this limitation resulted in an empty group, we might
11079     ;; pop the previous limit and use it instead.
11080     (when (and (not gnus-newsgroup-limit)
11081                show-if-empty)
11082       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
11083     gnus-newsgroup-limit))
11084
11085 (defun gnus-summary-limit-children (thread)
11086   "Return 1 if this subthread is visible and 0 if it is not."
11087   ;; First we get the number of visible children to this thread.  This
11088   ;; is done by recursing down the thread using this function, so this
11089   ;; will really go down to a leaf article first, before slowly
11090   ;; working its way up towards the root.
11091   (when thread
11092     (let ((children
11093            (if (cdr thread)
11094                (apply '+ (mapcar 'gnus-summary-limit-children
11095                                  (cdr thread)))
11096              0))
11097           (number (mail-header-number (car thread)))
11098           score)
11099       (if (or
11100            ;; If this article is dormant and has absolutely no visible
11101            ;; children, then this article isn't visible.
11102            (and (memq number gnus-newsgroup-dormant)
11103                 (= children 0))
11104            ;; If this is "fetch-old-headered" and there is only one
11105            ;; visible child (or less), then we don't want this article.
11106            (and (eq gnus-fetch-old-headers 'some)
11107                 (memq number gnus-newsgroup-ancient)
11108                 (zerop children))
11109            ;; If this is a sparsely inserted article with no children,
11110            ;; we don't want it.
11111            (and (eq gnus-build-sparse-threads 'some)
11112                 (memq number gnus-newsgroup-sparse)
11113                 (zerop children))
11114            ;; If we use expunging, and this article is really
11115            ;; low-scored, then we don't want this article.
11116            (when (and gnus-summary-expunge-below
11117                       (< (setq score
11118                                (or (cdr (assq number gnus-newsgroup-scored))
11119                                    gnus-summary-default-score))
11120                          gnus-summary-expunge-below))
11121              ;; We increase the expunge-tally here, but that has
11122              ;; nothing to do with the limits, really.
11123              (incf gnus-newsgroup-expunged-tally)
11124              ;; We also mark as read here, if that's wanted.
11125              (when (and gnus-summary-mark-below
11126                         (< score gnus-summary-mark-below))
11127                (setq gnus-newsgroup-unreads
11128                      (delq number gnus-newsgroup-unreads))
11129                (if gnus-newsgroup-auto-expire
11130                    (push number gnus-newsgroup-expirable)
11131                  (push (cons number gnus-low-score-mark)
11132                        gnus-newsgroup-reads)))
11133              t)
11134            (and gnus-use-nocem
11135                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11136           ;; Nope, invisible article.
11137           0
11138         ;; Ok, this article is to be visible, so we add it to the limit
11139         ;; and return 1.
11140         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11141         1))))
11142
11143 (defun gnus-expunge-thread (thread)
11144   "Mark all articles in THREAD as read."
11145   (let* ((number (mail-header-number (car thread))))
11146     (incf gnus-newsgroup-expunged-tally)
11147     ;; We also mark as read here, if that's wanted.
11148     (setq gnus-newsgroup-unreads
11149           (delq number gnus-newsgroup-unreads))
11150     (if gnus-newsgroup-auto-expire
11151         (push number gnus-newsgroup-expirable)
11152       (push (cons number gnus-low-score-mark)
11153             gnus-newsgroup-reads)))
11154   ;; Go recursively through all subthreads.
11155   (mapcar 'gnus-expunge-thread (cdr thread)))
11156
11157 ;; Summary article oriented commands
11158
11159 (defun gnus-summary-refer-parent-article (n)
11160   "Refer parent article N times.
11161 The difference between N and the number of articles fetched is returned."
11162   (interactive "p")
11163   (gnus-set-global-variables)
11164   (while
11165       (and
11166        (> n 0)
11167        (let* ((header (gnus-summary-article-header))
11168               (ref
11169                ;; If we try to find the parent of the currently
11170                ;; displayed article, then we take a look at the actual
11171                ;; References header, since this is slightly more
11172                ;; reliable than the References field we got from the
11173                ;; server.
11174                (if (and (eq (mail-header-number header)
11175                             (cdr gnus-article-current))
11176                         (equal gnus-newsgroup-name
11177                                (car gnus-article-current)))
11178                    (save-excursion
11179                      (set-buffer gnus-original-article-buffer)
11180                      (nnheader-narrow-to-headers)
11181                      (prog1
11182                          (message-fetch-field "references")
11183                        (widen)))
11184                  ;; It's not the current article, so we take a bet on
11185                  ;; the value we got from the server.
11186                  (mail-header-references header))))
11187          (if (setq ref (or ref (mail-header-references header)))
11188              (or (gnus-summary-refer-article (gnus-parent-id ref))
11189                  (gnus-message 1 "Couldn't find parent"))
11190            (gnus-message 1 "No references in article %d"
11191                          (gnus-summary-article-number))
11192            nil)))
11193     (setq n (1- n)))
11194   (gnus-summary-position-point)
11195   n)
11196
11197 (defun gnus-summary-refer-references ()
11198   "Fetch all articles mentioned in the References header.
11199 Return how many articles were fetched."
11200   (interactive)
11201   (gnus-set-global-variables)
11202   (let ((ref (mail-header-references (gnus-summary-article-header)))
11203         (current (gnus-summary-article-number))
11204         (n 0))
11205     ;; For each Message-ID in the References header...
11206     (while (string-match "<[^>]*>" ref)
11207       (incf n)
11208       ;; ... fetch that article.
11209       (gnus-summary-refer-article
11210        (prog1 (match-string 0 ref)
11211          (setq ref (substring ref (match-end 0))))))
11212     (gnus-summary-goto-subject current)
11213     (gnus-summary-position-point)
11214     n))
11215
11216 (defun gnus-summary-refer-article (message-id)
11217   "Fetch an article specified by MESSAGE-ID."
11218   (interactive "sMessage-ID: ")
11219   (when (and (stringp message-id)
11220              (not (zerop (length message-id))))
11221     ;; Construct the correct Message-ID if necessary.
11222     ;; Suggested by tale@pawl.rpi.edu.
11223     (unless (string-match "^<" message-id)
11224       (setq message-id (concat "<" message-id)))
11225     (unless (string-match ">$" message-id)
11226       (setq message-id (concat message-id ">")))
11227     (let* ((header (gnus-id-to-header message-id))
11228            (sparse (and header
11229                         (memq (mail-header-number header)
11230                               gnus-newsgroup-sparse))))
11231       (if header
11232           (prog1
11233               ;; The article is present in the buffer, to we just go to it.
11234               (gnus-summary-goto-article 
11235                (mail-header-number header) nil header)
11236             (when sparse
11237               (gnus-summary-update-article (mail-header-number header))))
11238         ;; We fetch the article
11239         (let ((gnus-override-method 
11240                (and (gnus-news-group-p gnus-newsgroup-name)
11241                     gnus-refer-article-method))
11242               number)
11243           ;; Start the special refer-article method, if necessary.
11244           (when (and gnus-refer-article-method
11245                      (gnus-news-group-p gnus-newsgroup-name))
11246             (gnus-check-server gnus-refer-article-method))
11247           ;; Fetch the header, and display the article.
11248           (if (setq number (gnus-summary-insert-subject message-id))
11249               (gnus-summary-select-article nil nil nil number)
11250             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11251
11252 (defun gnus-summary-enter-digest-group (&optional force)
11253   "Enter a digest group based on the current article."
11254   (interactive "P")
11255   (gnus-set-global-variables)
11256   (gnus-summary-select-article)
11257   (let ((name (format "%s-%d"
11258                       (gnus-group-prefixed-name
11259                        gnus-newsgroup-name (list 'nndoc ""))
11260                       gnus-current-article))
11261         (ogroup gnus-newsgroup-name)
11262         (case-fold-search t)
11263         (buf (current-buffer))
11264         dig)
11265     (save-excursion
11266       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11267       (insert-buffer-substring gnus-original-article-buffer)
11268       (narrow-to-region
11269        (goto-char (point-min))
11270        (or (search-forward "\n\n" nil t) (point)))
11271       (goto-char (point-min))
11272       (delete-matching-lines "^\\(Path\\):\\|^From ")
11273       (widen))
11274     (unwind-protect
11275         (if (gnus-group-read-ephemeral-group
11276              name `(nndoc ,name (nndoc-address
11277                                  ,(get-buffer dig))
11278                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11279             ;; Make all postings to this group go to the parent group.
11280             (nconc (gnus-info-params (gnus-get-info name))
11281                    (list (cons 'to-group ogroup)))
11282           ;; Couldn't select this doc group.
11283           (switch-to-buffer buf)
11284           (gnus-set-global-variables)
11285           (gnus-configure-windows 'summary)
11286           (gnus-message 3 "Article couldn't be entered?"))
11287       (kill-buffer dig))))
11288
11289 (defun gnus-summary-isearch-article (&optional regexp-p)
11290   "Do incremental search forward on the current article.
11291 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11292   (interactive "P")
11293   (gnus-set-global-variables)
11294   (gnus-summary-select-article)
11295   (gnus-configure-windows 'article)
11296   (gnus-eval-in-buffer-window gnus-article-buffer
11297     ;;(goto-char (point-min))
11298     (isearch-forward regexp-p)))
11299
11300 (defun gnus-summary-search-article-forward (regexp &optional backward)
11301   "Search for an article containing REGEXP forward.
11302 If BACKWARD, search backward instead."
11303   (interactive
11304    (list (read-string
11305           (format "Search article %s (regexp%s): "
11306                   (if current-prefix-arg "backward" "forward")
11307                   (if gnus-last-search-regexp
11308                       (concat ", default " gnus-last-search-regexp)
11309                     "")))
11310          current-prefix-arg))
11311   (gnus-set-global-variables)
11312   (if (string-equal regexp "")
11313       (setq regexp (or gnus-last-search-regexp ""))
11314     (setq gnus-last-search-regexp regexp))
11315   (unless (gnus-summary-search-article regexp backward)
11316     (error "Search failed: \"%s\"" regexp)))
11317
11318 (defun gnus-summary-search-article-backward (regexp)
11319   "Search for an article containing REGEXP backward."
11320   (interactive
11321    (list (read-string
11322           (format "Search article backward (regexp%s): "
11323                   (if gnus-last-search-regexp
11324                       (concat ", default " gnus-last-search-regexp)
11325                     "")))))
11326   (gnus-summary-search-article-forward regexp 'backward))
11327
11328 (defun gnus-summary-search-article (regexp &optional backward)
11329   "Search for an article containing REGEXP.
11330 Optional argument BACKWARD means do search for backward.
11331 `gnus-select-article-hook' is not called during the search."
11332   (let ((gnus-select-article-hook nil)  ;Disable hook.
11333         (gnus-article-display-hook nil)
11334         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11335         (re-search
11336          (if backward
11337              're-search-backward 're-search-forward))
11338         (sum (current-buffer))
11339         (found nil))
11340     (gnus-save-hidden-threads
11341       (gnus-summary-select-article)
11342       (set-buffer gnus-article-buffer)
11343       (when backward
11344         (forward-line -1))
11345       (while (not found)
11346         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11347         (if (if backward
11348                 (re-search-backward regexp nil t)
11349               (re-search-forward regexp nil t))
11350             ;; We found the regexp.
11351             (progn
11352               (setq found 'found)
11353               (beginning-of-line)
11354               (set-window-start
11355                (get-buffer-window (current-buffer))
11356                (point))
11357               (forward-line 1)
11358               (set-buffer sum))
11359           ;; We didn't find it, so we go to the next article.
11360           (set-buffer sum)
11361           (if (not (if backward (gnus-summary-find-prev)
11362                      (gnus-summary-find-next)))
11363               ;; No more articles.
11364               (setq found t)
11365             ;; Select the next article and adjust point.
11366             (gnus-summary-select-article)
11367             (set-buffer gnus-article-buffer)
11368             (widen)
11369             (goto-char (if backward (point-max) (point-min))))))
11370       (gnus-message 7 ""))
11371     ;; Return whether we found the regexp.
11372     (when (eq found 'found)
11373       (gnus-summary-show-thread)
11374       (gnus-summary-goto-subject gnus-current-article)
11375       (gnus-summary-position-point)
11376       t)))
11377
11378 (defun gnus-summary-find-matching (header regexp &optional backward unread
11379                                           not-case-fold)
11380   "Return a list of all articles that match REGEXP on HEADER.
11381 The search stars on the current article and goes forwards unless
11382 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11383 If UNREAD is non-nil, only unread articles will
11384 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11385 in the comparisons."
11386   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11387                 (gnus-data-find-list
11388                  (gnus-summary-article-number) (gnus-data-list backward))))
11389         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11390         (case-fold-search (not not-case-fold))
11391         articles d)
11392     (or (fboundp (intern (concat "mail-header-" header)))
11393         (error "%s is not a valid header" header))
11394     (while data
11395       (setq d (car data))
11396       (and (or (not unread)             ; We want all articles...
11397                (gnus-data-unread-p d))  ; Or just unreads.
11398            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11399            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11400            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11401       (setq data (cdr data)))
11402     (nreverse articles)))
11403
11404 (defun gnus-summary-execute-command (header regexp command &optional backward)
11405   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11406 If HEADER is an empty string (or nil), the match is done on the entire
11407 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11408   (interactive
11409    (list (let ((completion-ignore-case t))
11410            (completing-read
11411             "Header name: "
11412             (mapcar (lambda (string) (list string))
11413                     '("Number" "Subject" "From" "Lines" "Date"
11414                       "Message-ID" "Xref" "References" "Body"))
11415             nil 'require-match))
11416          (read-string "Regexp: ")
11417          (read-key-sequence "Command: ")
11418          current-prefix-arg))
11419   (when (equal header "Body")
11420     (setq header ""))
11421   (gnus-set-global-variables)
11422   ;; Hidden thread subtrees must be searched as well.
11423   (gnus-summary-show-all-threads)
11424   ;; We don't want to change current point nor window configuration.
11425   (save-excursion
11426     (save-window-excursion
11427       (gnus-message 6 "Executing %s..." (key-description command))
11428       ;; We'd like to execute COMMAND interactively so as to give arguments.
11429       (gnus-execute header regexp
11430                     `(lambda () (call-interactively ',(key-binding command)))
11431                     backward)
11432       (gnus-message 6 "Executing %s...done" (key-description command)))))
11433
11434 (defun gnus-summary-beginning-of-article ()
11435   "Scroll the article back to the beginning."
11436   (interactive)
11437   (gnus-set-global-variables)
11438   (gnus-summary-select-article)
11439   (gnus-configure-windows 'article)
11440   (gnus-eval-in-buffer-window gnus-article-buffer
11441     (widen)
11442     (goto-char (point-min))
11443     (and gnus-break-pages (gnus-narrow-to-page))))
11444
11445 (defun gnus-summary-end-of-article ()
11446   "Scroll to the end of the article."
11447   (interactive)
11448   (gnus-set-global-variables)
11449   (gnus-summary-select-article)
11450   (gnus-configure-windows 'article)
11451   (gnus-eval-in-buffer-window gnus-article-buffer
11452     (widen)
11453     (goto-char (point-max))
11454     (recenter -3)
11455     (and gnus-break-pages (gnus-narrow-to-page))))
11456
11457 (defun gnus-summary-show-article (&optional arg)
11458   "Force re-fetching of the current article.
11459 If ARG (the prefix) is non-nil, show the raw article without any
11460 article massaging functions being run."
11461   (interactive "P")
11462   (gnus-set-global-variables)
11463   (if (not arg)
11464       ;; Select the article the normal way.
11465       (gnus-summary-select-article nil 'force)
11466     ;; Bind the article treatment functions to nil.
11467     (let ((gnus-have-all-headers t)
11468           gnus-article-display-hook
11469           gnus-article-prepare-hook
11470           gnus-break-pages
11471           gnus-visual)
11472       (gnus-summary-select-article nil 'force)))
11473   (gnus-summary-goto-subject gnus-current-article)
11474 ;  (gnus-configure-windows 'article)
11475   (gnus-summary-position-point))
11476
11477 (defun gnus-summary-verbose-headers (&optional arg)
11478   "Toggle permanent full header display.
11479 If ARG is a positive number, turn header display on.
11480 If ARG is a negative number, turn header display off."
11481   (interactive "P")
11482   (gnus-set-global-variables)
11483   (gnus-summary-toggle-header arg)
11484   (setq gnus-show-all-headers
11485         (cond ((or (not (numberp arg))
11486                    (zerop arg))
11487                (not gnus-show-all-headers))
11488               ((natnump arg)
11489                t))))
11490
11491 (defun gnus-summary-toggle-header (&optional arg)
11492   "Show the headers if they are hidden, or hide them if they are shown.
11493 If ARG is a positive number, show the entire header.
11494 If ARG is a negative number, hide the unwanted header lines."
11495   (interactive "P")
11496   (gnus-set-global-variables)
11497   (save-excursion
11498     (set-buffer gnus-article-buffer)
11499     (let* ((buffer-read-only nil)
11500            (inhibit-point-motion-hooks t)
11501            (hidden (text-property-any
11502                     (goto-char (point-min)) (search-forward "\n\n")
11503                     'invisible t))
11504            e)
11505       (goto-char (point-min))
11506       (when (search-forward "\n\n" nil t)
11507         (delete-region (point-min) (1- (point))))
11508       (goto-char (point-min))
11509       (save-excursion
11510         (set-buffer gnus-original-article-buffer)
11511         (goto-char (point-min))
11512         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11513       (insert-buffer-substring gnus-original-article-buffer 1 e)
11514       (let ((gnus-inhibit-hiding t))
11515         (run-hooks 'gnus-article-display-hook))
11516       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11517           (gnus-article-hide-headers)))))
11518
11519 (defun gnus-summary-show-all-headers ()
11520   "Make all header lines visible."
11521   (interactive)
11522   (gnus-set-global-variables)
11523   (gnus-article-show-all-headers))
11524
11525 (defun gnus-summary-toggle-mime (&optional arg)
11526   "Toggle MIME processing.
11527 If ARG is a positive number, turn MIME processing on."
11528   (interactive "P")
11529   (gnus-set-global-variables)
11530   (setq gnus-show-mime
11531         (if (null arg) (not gnus-show-mime)
11532           (> (prefix-numeric-value arg) 0)))
11533   (gnus-summary-select-article t 'force))
11534
11535 (defun gnus-summary-caesar-message (&optional arg)
11536   "Caesar rotate the current article by 13.
11537 The numerical prefix specifies how manu places to rotate each letter
11538 forward."
11539   (interactive "P")
11540   (gnus-set-global-variables)
11541   (gnus-summary-select-article)
11542   (let ((mail-header-separator ""))
11543     (gnus-eval-in-buffer-window gnus-article-buffer
11544       (save-restriction
11545         (widen)
11546         (let ((start (window-start))
11547               buffer-read-only)
11548           (message-caesar-buffer-body arg)
11549           (set-window-start (get-buffer-window (current-buffer)) start))))))
11550
11551 (defun gnus-summary-stop-page-breaking ()
11552   "Stop page breaking in the current article."
11553   (interactive)
11554   (gnus-set-global-variables)
11555   (gnus-summary-select-article)
11556   (gnus-eval-in-buffer-window gnus-article-buffer
11557     (widen)))
11558
11559 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11560   "Move the current article to a different newsgroup.
11561 If N is a positive number, move the N next articles.
11562 If N is a negative number, move the N previous articles.
11563 If N is nil and any articles have been marked with the process mark,
11564 move those articles instead.
11565 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11566 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11567 re-spool using this method.
11568
11569 For this function to work, both the current newsgroup and the
11570 newsgroup that you want to move to have to support the `request-move'
11571 and `request-accept' functions."
11572   (interactive "P")
11573   (unless action (setq action 'move))
11574   (gnus-set-global-variables)
11575   ;; Check whether the source group supports the required functions.
11576   (cond ((and (eq action 'move)
11577               (not (gnus-check-backend-function
11578                     'request-move-article gnus-newsgroup-name)))
11579          (error "The current group does not support article moving"))
11580         ((and (eq action 'crosspost)
11581               (not (gnus-check-backend-function
11582                     'request-replace-article gnus-newsgroup-name)))
11583          (error "The current group does not support article editing")))
11584   (let ((articles (gnus-summary-work-articles n))
11585         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11586         (names '((move "Move" "Moving")
11587                  (copy "Copy" "Copying")
11588                  (crosspost "Crosspost" "Crossposting")))
11589         (copy-buf (save-excursion
11590                     (nnheader-set-temp-buffer " *copy article*")))
11591         art-group to-method new-xref article to-groups)
11592     (unless (assq action names)
11593       (error "Unknown action %s" action))
11594     ;; Read the newsgroup name.
11595     (when (and (not to-newsgroup)
11596                (not select-method))
11597       (setq to-newsgroup
11598             (gnus-read-move-group-name
11599              (cadr (assq action names))
11600              (symbol-value (intern (format "gnus-current-%s-group" action)))
11601              articles prefix))
11602       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11603     (setq to-method (or select-method 
11604                         (gnus-group-name-to-method to-newsgroup)))
11605     ;; Check the method we are to move this article to...
11606     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11607         (error "%s does not support article copying" (car to-method)))
11608     (or (gnus-check-server to-method)
11609         (error "Can't open server %s" (car to-method)))
11610     (gnus-message 6 "%s to %s: %s..."
11611                   (caddr (assq action names))
11612                   (or (car select-method) to-newsgroup) articles)
11613     (while articles
11614       (setq article (pop articles))
11615       (setq
11616        art-group
11617        (cond
11618         ;; Move the article.
11619         ((eq action 'move)
11620          (gnus-request-move-article
11621           article                       ; Article to move
11622           gnus-newsgroup-name           ; From newsgrouo
11623           (nth 1 (gnus-find-method-for-group
11624                   gnus-newsgroup-name)) ; Server
11625           (list 'gnus-request-accept-article
11626                 to-newsgroup (list 'quote select-method)
11627                 (not articles))         ; Accept form
11628           (not articles)))              ; Only save nov last time
11629         ;; Copy the article.
11630         ((eq action 'copy)
11631          (save-excursion
11632            (set-buffer copy-buf)
11633            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11634            (gnus-request-accept-article
11635             to-newsgroup select-method (not articles))))
11636         ;; Crosspost the article.
11637         ((eq action 'crosspost)
11638          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11639            (setq new-xref (concat gnus-newsgroup-name ":" article))
11640            (if (and xref (not (string= xref "")))
11641                (progn
11642                  (when (string-match "^Xref: " xref)
11643                    (setq xref (substring xref (match-end 0))))
11644                  (setq new-xref (concat xref " " new-xref)))
11645              (setq new-xref (concat (system-name) " " new-xref)))
11646            (save-excursion
11647              (set-buffer copy-buf)
11648              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11649              (nnheader-replace-header "xref" new-xref)
11650              (gnus-request-accept-article
11651               to-newsgroup select-method (not articles)))))))
11652       (if (not art-group)
11653           (gnus-message 1 "Couldn't %s article %s"
11654                         (cadr (assq action names)) article)
11655         (let* ((entry
11656                 (or
11657                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11658                  (gnus-gethash
11659                   (gnus-group-prefixed-name
11660                    (car art-group)
11661                    (or select-method 
11662                        (gnus-find-method-for-group to-newsgroup)))
11663                   gnus-newsrc-hashtb)))
11664                (info (nth 2 entry))
11665                (to-group (gnus-info-group info)))
11666           ;; Update the group that has been moved to.
11667           (when (and info
11668                      (memq action '(move copy)))
11669             (unless (member to-group to-groups)
11670               (push to-group to-groups))
11671
11672             (unless (memq article gnus-newsgroup-unreads)
11673               (gnus-info-set-read
11674                info (gnus-add-to-range (gnus-info-read info)
11675                                        (list (cdr art-group)))))
11676
11677             ;; Copy any marks over to the new group.
11678             (let ((marks gnus-article-mark-lists)
11679                   (to-article (cdr art-group)))
11680
11681               ;; See whether the article is to be put in the cache.
11682               (when gnus-use-cache
11683                 (gnus-cache-possibly-enter-article
11684                  to-group to-article
11685                  (let ((header (copy-sequence
11686                                 (gnus-summary-article-header article))))
11687                    (mail-header-set-number header to-article)
11688                    header)
11689                  (memq article gnus-newsgroup-marked)
11690                  (memq article gnus-newsgroup-dormant)
11691                  (memq article gnus-newsgroup-unreads)))
11692
11693               (while marks
11694                 (when (memq article (symbol-value
11695                                      (intern (format "gnus-newsgroup-%s"
11696                                                      (caar marks)))))
11697                   ;; If the other group is the same as this group,
11698                   ;; then we have to add the mark to the list.
11699                   (when (equal to-group gnus-newsgroup-name)
11700                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11701                          (cons to-article
11702                                (symbol-value
11703                                 (intern (format "gnus-newsgroup-%s"
11704                                                 (caar marks)))))))
11705                   ;; Copy mark to other group.
11706                   (gnus-add-marked-articles
11707                    to-group (cdar marks) (list to-article) info))
11708                 (setq marks (cdr marks)))))
11709
11710           ;; Update the Xref header in this article to point to
11711           ;; the new crossposted article we have just created.
11712           (when (eq action 'crosspost)
11713             (save-excursion
11714               (set-buffer copy-buf)
11715               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11716               (nnheader-replace-header
11717                "xref" (concat new-xref " " (gnus-group-prefixed-name
11718                                             (car art-group) to-method)
11719                               ":" (cdr art-group)))
11720               (gnus-request-replace-article
11721                article gnus-newsgroup-name (current-buffer)))))
11722
11723         (gnus-summary-goto-subject article)
11724         (when (eq action 'move)
11725           (gnus-summary-mark-article article gnus-canceled-mark)))
11726       (gnus-summary-remove-process-mark article))
11727     ;; Re-activate all groups that have been moved to.
11728     (while to-groups
11729       (gnus-activate-group (pop to-groups)))
11730     
11731     (gnus-kill-buffer copy-buf)
11732     (gnus-summary-position-point)
11733     (gnus-set-mode-line 'summary)))
11734
11735 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11736   "Move the current article to a different newsgroup.
11737 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11738 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11739 re-spool using this method."
11740   (interactive "P")
11741   (gnus-summary-move-article n nil select-method 'copy))
11742
11743 (defun gnus-summary-crosspost-article (&optional n)
11744   "Crosspost the current article to some other group."
11745   (interactive "P")
11746   (gnus-summary-move-article n nil nil 'crosspost))
11747
11748 (defvar gnus-summary-respool-default-method nil
11749   "Default method for respooling an article.  
11750 If nil, use to the current newsgroup method.")
11751
11752 (defun gnus-summary-respool-article (&optional n method)
11753   "Respool the current article.
11754 The article will be squeezed through the mail spooling process again,
11755 which means that it will be put in some mail newsgroup or other
11756 depending on `nnmail-split-methods'.
11757 If N is a positive number, respool the N next articles.
11758 If N is a negative number, respool the N previous articles.
11759 If N is nil and any articles have been marked with the process mark,
11760 respool those articles instead.
11761
11762 Respooling can be done both from mail groups and \"real\" newsgroups.
11763 In the former case, the articles in question will be moved from the
11764 current group into whatever groups they are destined to.  In the
11765 latter case, they will be copied into the relevant groups."
11766   (interactive 
11767    (list current-prefix-arg
11768          (let* ((methods (gnus-methods-using 'respool))
11769                 (methname
11770                  (symbol-name (or gnus-summary-respool-default-method
11771                                   (car (gnus-find-method-for-group
11772                                         gnus-newsgroup-name)))))
11773                 (method
11774                  (gnus-completing-read 
11775                   methname "What backend do you want to use when respooling?"
11776                   methods nil t nil 'gnus-method-history))
11777                 ms)
11778            (cond
11779             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11780              (list (intern method) ""))
11781             ((= 1 (length ms))
11782              (car ms))
11783             (t
11784              (cdr (completing-read 
11785                    "Server name: "
11786                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11787   (gnus-set-global-variables)
11788   (unless method
11789     (error "No method given for respooling"))
11790   (if (assoc (symbol-name
11791               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11792              (gnus-methods-using 'respool))
11793       (gnus-summary-move-article n nil method)
11794     (gnus-summary-copy-article n nil method)))
11795
11796 (defun gnus-summary-import-article (file)
11797   "Import a random file into a mail newsgroup."
11798   (interactive "fImport file: ")
11799   (gnus-set-global-variables)
11800   (let ((group gnus-newsgroup-name)
11801         (now (current-time))
11802         atts lines)
11803     (or (gnus-check-backend-function 'request-accept-article group)
11804         (error "%s does not support article importing" group))
11805     (or (file-readable-p file)
11806         (not (file-regular-p file))
11807         (error "Can't read %s" file))
11808     (save-excursion
11809       (set-buffer (get-buffer-create " *import file*"))
11810       (buffer-disable-undo (current-buffer))
11811       (erase-buffer)
11812       (insert-file-contents file)
11813       (goto-char (point-min))
11814       (unless (nnheader-article-p)
11815         ;; This doesn't look like an article, so we fudge some headers.
11816         (setq atts (file-attributes file)
11817               lines (count-lines (point-min) (point-max)))
11818         (insert "From: " (read-string "From: ") "\n"
11819                 "Subject: " (read-string "Subject: ") "\n"
11820                 "Date: " (timezone-make-date-arpa-standard
11821                           (current-time-string (nth 5 atts))
11822                           (current-time-zone now)
11823                           (current-time-zone now)) "\n"
11824                 "Message-ID: " (message-make-message-id) "\n"
11825                 "Lines: " (int-to-string lines) "\n"
11826                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11827       (gnus-request-accept-article group nil t)
11828       (kill-buffer (current-buffer)))))
11829
11830 (defun gnus-summary-expire-articles (&optional now)
11831   "Expire all articles that are marked as expirable in the current group."
11832   (interactive)
11833   (gnus-set-global-variables)
11834   (when (gnus-check-backend-function
11835          'request-expire-articles gnus-newsgroup-name)
11836     ;; This backend supports expiry.
11837     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11838            (expirable (if total
11839                           (gnus-list-of-read-articles gnus-newsgroup-name)
11840                         (setq gnus-newsgroup-expirable
11841                               (sort gnus-newsgroup-expirable '<))))
11842            (expiry-wait (if now 'immediate
11843                           (gnus-group-get-parameter
11844                            gnus-newsgroup-name 'expiry-wait)))
11845            es)
11846       (when expirable
11847         ;; There are expirable articles in this group, so we run them
11848         ;; through the expiry process.
11849         (gnus-message 6 "Expiring articles...")
11850         ;; The list of articles that weren't expired is returned.
11851         (if expiry-wait
11852             (let ((nnmail-expiry-wait-function nil)
11853                   (nnmail-expiry-wait expiry-wait))
11854               (setq es (gnus-request-expire-articles
11855                         expirable gnus-newsgroup-name)))
11856           (setq es (gnus-request-expire-articles
11857                     expirable gnus-newsgroup-name)))
11858         (or total (setq gnus-newsgroup-expirable es))
11859         ;; We go through the old list of expirable, and mark all
11860         ;; really expired articles as nonexistent.
11861         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11862           (let ((gnus-use-cache nil))
11863             (while expirable
11864               (unless (memq (car expirable) es)
11865                 (when (gnus-data-find (car expirable))
11866                   (gnus-summary-mark-article
11867                    (car expirable) gnus-canceled-mark)))
11868               (setq expirable (cdr expirable)))))
11869         (gnus-message 6 "Expiring articles...done")))))
11870
11871 (defun gnus-summary-expire-articles-now ()
11872   "Expunge all expirable articles in the current group.
11873 This means that *all* articles that are marked as expirable will be
11874 deleted forever, right now."
11875   (interactive)
11876   (gnus-set-global-variables)
11877   (or gnus-expert-user
11878       (gnus-y-or-n-p
11879        "Are you really, really, really sure you want to delete all these messages? ")
11880       (error "Phew!"))
11881   (gnus-summary-expire-articles t))
11882
11883 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11884 (defun gnus-summary-delete-article (&optional n)
11885   "Delete the N next (mail) articles.
11886 This command actually deletes articles.  This is not a marking
11887 command.  The article will disappear forever from your life, never to
11888 return.
11889 If N is negative, delete backwards.
11890 If N is nil and articles have been marked with the process mark,
11891 delete these instead."
11892   (interactive "P")
11893   (gnus-set-global-variables)
11894   (or (gnus-check-backend-function 'request-expire-articles
11895                                    gnus-newsgroup-name)
11896       (error "The current newsgroup does not support article deletion."))
11897   ;; Compute the list of articles to delete.
11898   (let ((articles (gnus-summary-work-articles n))
11899         not-deleted)
11900     (if (and gnus-novice-user
11901              (not (gnus-y-or-n-p
11902                    (format "Do you really want to delete %s forever? "
11903                            (if (> (length articles) 1) 
11904                                (format "these %s articles" (length articles))
11905                              "this article")))))
11906         ()
11907       ;; Delete the articles.
11908       (setq not-deleted (gnus-request-expire-articles
11909                          articles gnus-newsgroup-name 'force))
11910       (while articles
11911         (gnus-summary-remove-process-mark (car articles))
11912         ;; The backend might not have been able to delete the article
11913         ;; after all.
11914         (or (memq (car articles) not-deleted)
11915             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11916         (setq articles (cdr articles))))
11917     (gnus-summary-position-point)
11918     (gnus-set-mode-line 'summary)
11919     not-deleted))
11920
11921 (defun gnus-summary-edit-article (&optional force)
11922   "Enter into a buffer and edit the current article.
11923 This will have permanent effect only in mail groups.
11924 If FORCE is non-nil, allow editing of articles even in read-only
11925 groups."
11926   (interactive "P")
11927   (save-excursion
11928     (set-buffer gnus-summary-buffer)
11929     (gnus-set-global-variables)
11930     (when (and (not force)
11931                (gnus-group-read-only-p))
11932       (error "The current newsgroup does not support article editing."))
11933     (gnus-summary-select-article t nil t)
11934     (gnus-configure-windows 'article)
11935     (select-window (get-buffer-window gnus-article-buffer))
11936     (gnus-message 6 "C-c C-c to end edits")
11937     (setq buffer-read-only nil)
11938     (text-mode)
11939     (use-local-map (copy-keymap (current-local-map)))
11940     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11941     (buffer-enable-undo)
11942     (widen)
11943     (goto-char (point-min))
11944     (search-forward "\n\n" nil t)))
11945
11946 (defun gnus-summary-edit-article-done ()
11947   "Make edits to the current article permanent."
11948   (interactive)
11949   (if (gnus-group-read-only-p)
11950       (progn
11951         (let ((beep (not (eq major-mode 'text-mode))))
11952           (gnus-summary-edit-article-postpone)
11953           (when beep
11954             (gnus-error
11955              3 "The current newsgroup does not support article editing."))))
11956     (let ((buf (format "%s" (buffer-string))))
11957       (erase-buffer)
11958       (insert buf)
11959       (if (not (gnus-request-replace-article
11960                 (cdr gnus-article-current) (car gnus-article-current)
11961                 (current-buffer)))
11962           (error "Couldn't replace article.")
11963         (gnus-article-mode)
11964         (use-local-map gnus-article-mode-map)
11965         (setq buffer-read-only t)
11966         (buffer-disable-undo (current-buffer))
11967         (gnus-configure-windows 'summary)
11968         (gnus-summary-update-article (cdr gnus-article-current))
11969         (when gnus-use-cache
11970           (gnus-cache-update-article    
11971            (car gnus-article-current) (cdr gnus-article-current)))
11972         (when gnus-keep-backlog
11973           (gnus-backlog-remove-article 
11974            (car gnus-article-current) (cdr gnus-article-current))))
11975       (save-excursion
11976         (when (get-buffer gnus-original-article-buffer)
11977           (set-buffer gnus-original-article-buffer)
11978           (setq gnus-original-article nil)))
11979       (setq gnus-article-current nil
11980             gnus-current-article nil)
11981       (run-hooks 'gnus-article-display-hook)
11982       (and (gnus-visual-p 'summary-highlight 'highlight)
11983            (run-hooks 'gnus-visual-mark-article-hook)))))
11984
11985 (defun gnus-summary-edit-article-postpone ()
11986   "Postpone changes to the current article."
11987   (interactive)
11988   (gnus-article-mode)
11989   (use-local-map gnus-article-mode-map)
11990   (setq buffer-read-only t)
11991   (buffer-disable-undo (current-buffer))
11992   (gnus-configure-windows 'summary)
11993   (and (gnus-visual-p 'summary-highlight 'highlight)
11994        (run-hooks 'gnus-visual-mark-article-hook)))
11995
11996 (defun gnus-summary-respool-query ()
11997   "Query where the respool algorithm would put this article."
11998   (interactive)
11999   (gnus-set-global-variables)
12000   (gnus-summary-select-article)
12001   (save-excursion
12002     (set-buffer gnus-article-buffer)
12003     (save-restriction
12004       (goto-char (point-min))
12005       (search-forward "\n\n")
12006       (narrow-to-region (point-min) (point))
12007       (pp-eval-expression
12008        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
12009
12010 ;; Summary marking commands.
12011
12012 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
12013   "Mark articles which has the same subject as read, and then select the next.
12014 If UNMARK is positive, remove any kind of mark.
12015 If UNMARK is negative, tick articles."
12016   (interactive "P")
12017   (gnus-set-global-variables)
12018   (if unmark
12019       (setq unmark (prefix-numeric-value unmark)))
12020   (let ((count
12021          (gnus-summary-mark-same-subject
12022           (gnus-summary-article-subject) unmark)))
12023     ;; Select next unread article.  If auto-select-same mode, should
12024     ;; select the first unread article.
12025     (gnus-summary-next-article t (and gnus-auto-select-same
12026                                       (gnus-summary-article-subject)))
12027     (gnus-message 7 "%d article%s marked as %s"
12028                   count (if (= count 1) " is" "s are")
12029                   (if unmark "unread" "read"))))
12030
12031 (defun gnus-summary-kill-same-subject (&optional unmark)
12032   "Mark articles which has the same subject as read.
12033 If UNMARK is positive, remove any kind of mark.
12034 If UNMARK is negative, tick articles."
12035   (interactive "P")
12036   (gnus-set-global-variables)
12037   (if unmark
12038       (setq unmark (prefix-numeric-value unmark)))
12039   (let ((count
12040          (gnus-summary-mark-same-subject
12041           (gnus-summary-article-subject) unmark)))
12042     ;; If marked as read, go to next unread subject.
12043     (if (null unmark)
12044         ;; Go to next unread subject.
12045         (gnus-summary-next-subject 1 t))
12046     (gnus-message 7 "%d articles are marked as %s"
12047                   count (if unmark "unread" "read"))))
12048
12049 (defun gnus-summary-mark-same-subject (subject &optional unmark)
12050   "Mark articles with same SUBJECT as read, and return marked number.
12051 If optional argument UNMARK is positive, remove any kinds of marks.
12052 If optional argument UNMARK is negative, mark articles as unread instead."
12053   (let ((count 1))
12054     (save-excursion
12055       (cond
12056        ((null unmark)                   ; Mark as read.
12057         (while (and
12058                 (progn
12059                   (gnus-summary-mark-article-as-read gnus-killed-mark)
12060                   (gnus-summary-show-thread) t)
12061                 (gnus-summary-find-subject subject))
12062           (setq count (1+ count))))
12063        ((> unmark 0)                    ; Tick.
12064         (while (and
12065                 (progn
12066                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
12067                   (gnus-summary-show-thread) t)
12068                 (gnus-summary-find-subject subject))
12069           (setq count (1+ count))))
12070        (t                               ; Mark as unread.
12071         (while (and
12072                 (progn
12073                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
12074                   (gnus-summary-show-thread) t)
12075                 (gnus-summary-find-subject subject))
12076           (setq count (1+ count)))))
12077       (gnus-set-mode-line 'summary)
12078       ;; Return the number of marked articles.
12079       count)))
12080
12081 (defun gnus-summary-mark-as-processable (n &optional unmark)
12082   "Set the process mark on the next N articles.
12083 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
12084 the process mark instead.  The difference between N and the actual
12085 number of articles marked is returned."
12086   (interactive "p")
12087   (gnus-set-global-variables)
12088   (let ((backward (< n 0))
12089         (n (abs n)))
12090     (while (and
12091             (> n 0)
12092             (if unmark
12093                 (gnus-summary-remove-process-mark
12094                  (gnus-summary-article-number))
12095               (gnus-summary-set-process-mark (gnus-summary-article-number)))
12096             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
12097       (setq n (1- n)))
12098     (if (/= 0 n) (gnus-message 7 "No more articles"))
12099     (gnus-summary-recenter)
12100     (gnus-summary-position-point)
12101     n))
12102
12103 (defun gnus-summary-unmark-as-processable (n)
12104   "Remove the process mark from the next N articles.
12105 If N is negative, mark backward instead.  The difference between N and
12106 the actual number of articles marked is returned."
12107   (interactive "p")
12108   (gnus-set-global-variables)
12109   (gnus-summary-mark-as-processable n t))
12110
12111 (defun gnus-summary-unmark-all-processable ()
12112   "Remove the process mark from all articles."
12113   (interactive)
12114   (gnus-set-global-variables)
12115   (save-excursion
12116     (while gnus-newsgroup-processable
12117       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
12118   (gnus-summary-position-point))
12119
12120 (defun gnus-summary-mark-as-expirable (n)
12121   "Mark N articles forward as expirable.
12122 If N is negative, mark backward instead.  The difference between N and
12123 the actual number of articles marked is returned."
12124   (interactive "p")
12125   (gnus-set-global-variables)
12126   (gnus-summary-mark-forward n gnus-expirable-mark))
12127
12128 (defun gnus-summary-mark-article-as-replied (article)
12129   "Mark ARTICLE replied and update the summary line."
12130   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12131   (let ((buffer-read-only nil))
12132     (when (gnus-summary-goto-subject article)
12133       (gnus-summary-update-secondary-mark article))))
12134
12135 (defun gnus-summary-set-bookmark (article)
12136   "Set a bookmark in current article."
12137   (interactive (list (gnus-summary-article-number)))
12138   (gnus-set-global-variables)
12139   (if (or (not (get-buffer gnus-article-buffer))
12140           (not gnus-current-article)
12141           (not gnus-article-current)
12142           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12143       (error "No current article selected"))
12144   ;; Remove old bookmark, if one exists.
12145   (let ((old (assq article gnus-newsgroup-bookmarks)))
12146     (if old (setq gnus-newsgroup-bookmarks
12147                   (delq old gnus-newsgroup-bookmarks))))
12148   ;; Set the new bookmark, which is on the form
12149   ;; (article-number . line-number-in-body).
12150   (setq gnus-newsgroup-bookmarks
12151         (cons
12152          (cons article
12153                (save-excursion
12154                  (set-buffer gnus-article-buffer)
12155                  (count-lines
12156                   (min (point)
12157                        (save-excursion
12158                          (goto-char (point-min))
12159                          (search-forward "\n\n" nil t)
12160                          (point)))
12161                   (point))))
12162          gnus-newsgroup-bookmarks))
12163   (gnus-message 6 "A bookmark has been added to the current article."))
12164
12165 (defun gnus-summary-remove-bookmark (article)
12166   "Remove the bookmark from the current article."
12167   (interactive (list (gnus-summary-article-number)))
12168   (gnus-set-global-variables)
12169   ;; Remove old bookmark, if one exists.
12170   (let ((old (assq article gnus-newsgroup-bookmarks)))
12171     (if old
12172         (progn
12173           (setq gnus-newsgroup-bookmarks
12174                 (delq old gnus-newsgroup-bookmarks))
12175           (gnus-message 6 "Removed bookmark."))
12176       (gnus-message 6 "No bookmark in current article."))))
12177
12178 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12179 (defun gnus-summary-mark-as-dormant (n)
12180   "Mark N articles forward as dormant.
12181 If N is negative, mark backward instead.  The difference between N and
12182 the actual number of articles marked is returned."
12183   (interactive "p")
12184   (gnus-set-global-variables)
12185   (gnus-summary-mark-forward n gnus-dormant-mark))
12186
12187 (defun gnus-summary-set-process-mark (article)
12188   "Set the process mark on ARTICLE and update the summary line."
12189   (setq gnus-newsgroup-processable
12190         (cons article
12191               (delq article gnus-newsgroup-processable)))
12192   (when (gnus-summary-goto-subject article)
12193     (gnus-summary-show-thread)
12194     (gnus-summary-update-secondary-mark article)))
12195
12196 (defun gnus-summary-remove-process-mark (article)
12197   "Remove the process mark from ARTICLE and update the summary line."
12198   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12199   (when (gnus-summary-goto-subject article)
12200     (gnus-summary-show-thread)
12201     (gnus-summary-update-secondary-mark article)))
12202
12203 (defun gnus-summary-set-saved-mark (article)
12204   "Set the process mark on ARTICLE and update the summary line."
12205   (push article gnus-newsgroup-saved)
12206   (when (gnus-summary-goto-subject article)
12207     (gnus-summary-update-secondary-mark article)))
12208
12209 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12210   "Mark N articles as read forwards.
12211 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
12212 The difference between N and the actual number of articles marked is
12213 returned."
12214   (interactive "p")
12215   (gnus-set-global-variables)
12216   (let ((backward (< n 0))
12217         (gnus-summary-goto-unread
12218          (and gnus-summary-goto-unread
12219               (not (eq gnus-summary-goto-unread 'never))
12220               (not (memq mark (list gnus-unread-mark
12221                                     gnus-ticked-mark gnus-dormant-mark)))))
12222         (n (abs n))
12223         (mark (or mark gnus-del-mark)))
12224     (while (and (> n 0)
12225                 (gnus-summary-mark-article nil mark no-expire)
12226                 (zerop (gnus-summary-next-subject
12227                         (if backward -1 1)
12228                         (and gnus-summary-goto-unread
12229                              (not (eq gnus-summary-goto-unread 'never)))
12230                         t)))
12231       (setq n (1- n)))
12232     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12233     (gnus-summary-recenter)
12234     (gnus-summary-position-point)
12235     (gnus-set-mode-line 'summary)
12236     n))
12237
12238 (defun gnus-summary-mark-article-as-read (mark)
12239   "Mark the current article quickly as read with MARK."
12240   (let ((article (gnus-summary-article-number)))
12241     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12242     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12243     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12244     (setq gnus-newsgroup-reads
12245           (cons (cons article mark) gnus-newsgroup-reads))
12246     ;; Possibly remove from cache, if that is used.
12247     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12248     ;; Allow the backend to change the mark.
12249     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12250     ;; Check for auto-expiry.
12251     (when (and gnus-newsgroup-auto-expire
12252                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12253                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12254                    (= mark gnus-ancient-mark)
12255                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12256       (setq mark gnus-expirable-mark)
12257       (push article gnus-newsgroup-expirable))
12258     ;; Set the mark in the buffer.
12259     (gnus-summary-update-mark mark 'unread)
12260     t))
12261
12262 (defun gnus-summary-mark-article-as-unread (mark)
12263   "Mark the current article quickly as unread with MARK."
12264   (let ((article (gnus-summary-article-number)))
12265     (if (< article 0)
12266         (gnus-error 1 "Unmarkable article")
12267       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12268       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12269       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12270       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12271       (cond ((= mark gnus-ticked-mark)
12272              (push article gnus-newsgroup-marked))
12273             ((= mark gnus-dormant-mark)
12274              (push article gnus-newsgroup-dormant))
12275             (t
12276              (push article gnus-newsgroup-unreads)))
12277       (setq gnus-newsgroup-reads
12278             (delq (assq article gnus-newsgroup-reads)
12279                   gnus-newsgroup-reads))
12280
12281       ;; See whether the article is to be put in the cache.
12282       (and gnus-use-cache
12283            (vectorp (gnus-summary-article-header article))
12284            (save-excursion
12285              (gnus-cache-possibly-enter-article
12286               gnus-newsgroup-name article
12287               (gnus-summary-article-header article)
12288               (= mark gnus-ticked-mark)
12289               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12290
12291       ;; Fix the mark.
12292       (gnus-summary-update-mark mark 'unread))
12293     t))
12294
12295 (defun gnus-summary-mark-article (&optional article mark no-expire)
12296   "Mark ARTICLE with MARK.  MARK can be any character.
12297 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12298 `??' (dormant) and `?E' (expirable).
12299 If MARK is nil, then the default character `?D' is used.
12300 If ARTICLE is nil, then the article on the current line will be
12301 marked."
12302   ;; The mark might be a string.
12303   (and (stringp mark)
12304        (setq mark (aref mark 0)))
12305   ;; If no mark is given, then we check auto-expiring.
12306   (and (not no-expire)
12307        gnus-newsgroup-auto-expire
12308        (or (not mark)
12309            (and (numberp mark)
12310                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12311                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12312                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12313        (setq mark gnus-expirable-mark))
12314   (let* ((mark (or mark gnus-del-mark))
12315          (article (or article (gnus-summary-article-number))))
12316     (or article (error "No article on current line"))
12317     (if (or (= mark gnus-unread-mark)
12318             (= mark gnus-ticked-mark)
12319             (= mark gnus-dormant-mark))
12320         (gnus-mark-article-as-unread article mark)
12321       (gnus-mark-article-as-read article mark))
12322
12323     ;; See whether the article is to be put in the cache.
12324     (and gnus-use-cache
12325          (not (= mark gnus-canceled-mark))
12326          (vectorp (gnus-summary-article-header article))
12327          (save-excursion
12328            (gnus-cache-possibly-enter-article
12329             gnus-newsgroup-name article
12330             (gnus-summary-article-header article)
12331             (= mark gnus-ticked-mark)
12332             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12333
12334     (if (gnus-summary-goto-subject article nil t)
12335         (let ((buffer-read-only nil))
12336           (gnus-summary-show-thread)
12337           ;; Fix the mark.
12338           (gnus-summary-update-mark mark 'unread)
12339           t))))
12340
12341 (defun gnus-summary-update-secondary-mark (article)
12342   "Update the secondary (read, process, cache) mark."
12343   (gnus-summary-update-mark
12344    (cond ((memq article gnus-newsgroup-processable)
12345           gnus-process-mark)
12346          ((memq article gnus-newsgroup-cached)
12347           gnus-cached-mark)
12348          ((memq article gnus-newsgroup-replied)
12349           gnus-replied-mark)
12350          ((memq article gnus-newsgroup-saved)
12351           gnus-saved-mark)
12352          (t gnus-unread-mark))
12353    'replied)
12354   (when (gnus-visual-p 'summary-highlight 'highlight)
12355     (run-hooks 'gnus-summary-update-hook))
12356   t)
12357
12358 (defun gnus-summary-update-mark (mark type)
12359   (beginning-of-line)
12360   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12361         (buffer-read-only nil))
12362     (when (and forward
12363                (<= (+ forward (point)) (point-max)))
12364       ;; Go to the right position on the line.
12365       (goto-char (+ forward (point)))
12366       ;; Replace the old mark with the new mark.
12367       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12368       ;; Optionally update the marks by some user rule.
12369       (when (eq type 'unread)
12370         (gnus-data-set-mark
12371          (gnus-data-find (gnus-summary-article-number)) mark)
12372         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12373
12374 (defun gnus-mark-article-as-read (article &optional mark)
12375   "Enter ARTICLE in the pertinent lists and remove it from others."
12376   ;; Make the article expirable.
12377   (let ((mark (or mark gnus-del-mark)))
12378     (if (= mark gnus-expirable-mark)
12379         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12380       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12381     ;; Remove from unread and marked lists.
12382     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12383     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12384     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12385     (push (cons article mark) gnus-newsgroup-reads)
12386     ;; Possibly remove from cache, if that is used.
12387     (when gnus-use-cache
12388       (gnus-cache-enter-remove-article article))))
12389
12390 (defun gnus-mark-article-as-unread (article &optional mark)
12391   "Enter ARTICLE in the pertinent lists and remove it from others."
12392   (let ((mark (or mark gnus-ticked-mark)))
12393     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12394     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12395     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12396     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12397     (cond ((= mark gnus-ticked-mark)
12398            (push article gnus-newsgroup-marked))
12399           ((= mark gnus-dormant-mark)
12400            (push article gnus-newsgroup-dormant))
12401           (t
12402            (push article gnus-newsgroup-unreads)))
12403     (setq gnus-newsgroup-reads
12404           (delq (assq article gnus-newsgroup-reads)
12405                 gnus-newsgroup-reads))))
12406
12407 (defalias 'gnus-summary-mark-as-unread-forward
12408   'gnus-summary-tick-article-forward)
12409 (make-obsolete 'gnus-summary-mark-as-unread-forward
12410                'gnus-summary-tick-article-forward)
12411 (defun gnus-summary-tick-article-forward (n)
12412   "Tick N articles forwards.
12413 If N is negative, tick backwards instead.
12414 The difference between N and the number of articles ticked is returned."
12415   (interactive "p")
12416   (gnus-summary-mark-forward n gnus-ticked-mark))
12417
12418 (defalias 'gnus-summary-mark-as-unread-backward
12419   'gnus-summary-tick-article-backward)
12420 (make-obsolete 'gnus-summary-mark-as-unread-backward
12421                'gnus-summary-tick-article-backward)
12422 (defun gnus-summary-tick-article-backward (n)
12423   "Tick N articles backwards.
12424 The difference between N and the number of articles ticked is returned."
12425   (interactive "p")
12426   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12427
12428 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12429 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12430 (defun gnus-summary-tick-article (&optional article clear-mark)
12431   "Mark current article as unread.
12432 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12433 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12434   (interactive)
12435   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12436                                        gnus-ticked-mark)))
12437
12438 (defun gnus-summary-mark-as-read-forward (n)
12439   "Mark N articles as read forwards.
12440 If N is negative, mark backwards instead.
12441 The difference between N and the actual number of articles marked is
12442 returned."
12443   (interactive "p")
12444   (gnus-summary-mark-forward n gnus-del-mark t))
12445
12446 (defun gnus-summary-mark-as-read-backward (n)
12447   "Mark the N articles as read backwards.
12448 The difference between N and the actual number of articles marked is
12449 returned."
12450   (interactive "p")
12451   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12452
12453 (defun gnus-summary-mark-as-read (&optional article mark)
12454   "Mark current article as read.
12455 ARTICLE specifies the article to be marked as read.
12456 MARK specifies a string to be inserted at the beginning of the line."
12457   (gnus-summary-mark-article article mark))
12458
12459 (defun gnus-summary-clear-mark-forward (n)
12460   "Clear marks from N articles forward.
12461 If N is negative, clear backward instead.
12462 The difference between N and the number of marks cleared is returned."
12463   (interactive "p")
12464   (gnus-summary-mark-forward n gnus-unread-mark))
12465
12466 (defun gnus-summary-clear-mark-backward (n)
12467   "Clear marks from N articles backward.
12468 The difference between N and the number of marks cleared is returned."
12469   (interactive "p")
12470   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12471
12472 (defun gnus-summary-mark-unread-as-read ()
12473   "Intended to be used by `gnus-summary-mark-article-hook'."
12474   (when (memq gnus-current-article gnus-newsgroup-unreads)
12475     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12476
12477 (defun gnus-summary-mark-read-and-unread-as-read ()
12478   "Intended to be used by `gnus-summary-mark-article-hook'."
12479   (let ((mark (gnus-summary-article-mark)))
12480     (when (or (gnus-unread-mark-p mark)
12481               (gnus-read-mark-p mark))
12482       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12483
12484 (defun gnus-summary-mark-region-as-read (point mark all)
12485   "Mark all unread articles between point and mark as read.
12486 If given a prefix, mark all articles between point and mark as read,
12487 even ticked and dormant ones."
12488   (interactive "r\nP")
12489   (save-excursion
12490     (let (article)
12491       (goto-char point)
12492       (beginning-of-line)
12493       (while (and
12494               (< (point) mark)
12495               (progn
12496                 (when (or all
12497                           (memq (setq article (gnus-summary-article-number))
12498                                 gnus-newsgroup-unreads))
12499                   (gnus-summary-mark-article article gnus-del-mark))
12500                 t)
12501               (gnus-summary-find-next))))))
12502
12503 (defun gnus-summary-mark-below (score mark)
12504   "Mark articles with score less than SCORE with MARK."
12505   (interactive "P\ncMark: ")
12506   (gnus-set-global-variables)
12507   (setq score (if score
12508                   (prefix-numeric-value score)
12509                 (or gnus-summary-default-score 0)))
12510   (save-excursion
12511     (set-buffer gnus-summary-buffer)
12512     (goto-char (point-min))
12513     (while 
12514         (progn
12515           (and (< (gnus-summary-article-score) score)
12516                (gnus-summary-mark-article nil mark))
12517           (gnus-summary-find-next)))))
12518
12519 (defun gnus-summary-kill-below (&optional score)
12520   "Mark articles with score below SCORE as read."
12521   (interactive "P")
12522   (gnus-set-global-variables)
12523   (gnus-summary-mark-below score gnus-killed-mark))
12524
12525 (defun gnus-summary-clear-above (&optional score)
12526   "Clear all marks from articles with score above SCORE."
12527   (interactive "P")
12528   (gnus-set-global-variables)
12529   (gnus-summary-mark-above score gnus-unread-mark))
12530
12531 (defun gnus-summary-tick-above (&optional score)
12532   "Tick all articles with score above SCORE."
12533   (interactive "P")
12534   (gnus-set-global-variables)
12535   (gnus-summary-mark-above score gnus-ticked-mark))
12536
12537 (defun gnus-summary-mark-above (score mark)
12538   "Mark articles with score over SCORE with MARK."
12539   (interactive "P\ncMark: ")
12540   (gnus-set-global-variables)
12541   (setq score (if score
12542                   (prefix-numeric-value score)
12543                 (or gnus-summary-default-score 0)))
12544   (save-excursion
12545     (set-buffer gnus-summary-buffer)
12546     (goto-char (point-min))
12547     (while (and (progn
12548                   (if (> (gnus-summary-article-score) score)
12549                       (gnus-summary-mark-article nil mark))
12550                   t)
12551                 (gnus-summary-find-next)))))
12552
12553 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12554 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12555 (defun gnus-summary-limit-include-expunged (&optional no-error)
12556   "Display all the hidden articles that were expunged for low scores."
12557   (interactive)
12558   (gnus-set-global-variables)
12559   (let ((buffer-read-only nil))
12560     (let ((scored gnus-newsgroup-scored)
12561           headers h)
12562       (while scored
12563         (or (gnus-summary-goto-subject (caar scored))
12564             (and (setq h (gnus-summary-article-header (caar scored)))
12565                  (< (cdar scored) gnus-summary-expunge-below)
12566                  (setq headers (cons h headers))))
12567         (setq scored (cdr scored)))
12568       (if (not headers)
12569           (when (not no-error)
12570             (error "No expunged articles hidden."))
12571         (goto-char (point-min))
12572         (gnus-summary-prepare-unthreaded (nreverse headers))
12573         (goto-char (point-min))
12574         (gnus-summary-position-point)
12575         t))))
12576
12577 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12578   "Mark all articles not marked as unread in this newsgroup as read.
12579 If prefix argument ALL is non-nil, all articles are marked as read.
12580 If QUIETLY is non-nil, no questions will be asked.
12581 If TO-HERE is non-nil, it should be a point in the buffer.  All
12582 articles before this point will be marked as read.
12583 The number of articles marked as read is returned."
12584   (interactive "P")
12585   (gnus-set-global-variables)
12586   (prog1
12587       (if (or quietly
12588               (not gnus-interactive-catchup) ;Without confirmation?
12589               gnus-expert-user
12590               (gnus-y-or-n-p
12591                (if all
12592                    "Mark absolutely all articles as read? "
12593                  "Mark all unread articles as read? ")))
12594           (if (and not-mark
12595                    (not gnus-newsgroup-adaptive)
12596                    (not gnus-newsgroup-auto-expire))
12597               (progn
12598                 (when all
12599                   (setq gnus-newsgroup-marked nil
12600                         gnus-newsgroup-dormant nil))
12601                 (setq gnus-newsgroup-unreads nil))
12602             ;; We actually mark all articles as canceled, which we
12603             ;; have to do when using auto-expiry or adaptive scoring.
12604             (gnus-summary-show-all-threads)
12605             (if (gnus-summary-first-subject (not all))
12606                 (while (and
12607                         (if to-here (< (point) to-here) t)
12608                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12609                         (gnus-summary-find-next (not all)))))
12610             (unless to-here
12611               (setq gnus-newsgroup-unreads nil))
12612             (gnus-set-mode-line 'summary)))
12613     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12614       (if (and (not to-here) (eq 'nnvirtual (car method)))
12615           (nnvirtual-catchup-group
12616            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12617     (gnus-summary-position-point)))
12618
12619 (defun gnus-summary-catchup-to-here (&optional all)
12620   "Mark all unticked articles before the current one as read.
12621 If ALL is non-nil, also mark ticked and dormant articles as read."
12622   (interactive "P")
12623   (gnus-set-global-variables)
12624   (save-excursion
12625     (gnus-save-hidden-threads
12626       (let ((beg (point)))
12627         ;; We check that there are unread articles.
12628         (when (or all (gnus-summary-find-prev))
12629           (gnus-summary-catchup all t beg)))))
12630   (gnus-summary-position-point))
12631
12632 (defun gnus-summary-catchup-all (&optional quietly)
12633   "Mark all articles in this newsgroup as read."
12634   (interactive "P")
12635   (gnus-set-global-variables)
12636   (gnus-summary-catchup t quietly))
12637
12638 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12639   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12640 If prefix argument ALL is non-nil, all articles are marked as read."
12641   (interactive "P")
12642   (gnus-set-global-variables)
12643   (gnus-summary-catchup all quietly nil 'fast)
12644   ;; Select next newsgroup or exit.
12645   (if (eq gnus-auto-select-next 'quietly)
12646       (gnus-summary-next-group nil)
12647     (gnus-summary-exit)))
12648
12649 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12650   "Mark all articles in this newsgroup as read, and then exit."
12651   (interactive "P")
12652   (gnus-set-global-variables)
12653   (gnus-summary-catchup-and-exit t quietly))
12654
12655 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12656 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12657   "Mark all articles in this group as read and select the next group.
12658 If given a prefix, mark all articles, unread as well as ticked, as
12659 read."
12660   (interactive "P")
12661   (gnus-set-global-variables)
12662   (save-excursion
12663     (gnus-summary-catchup all))
12664   (gnus-summary-next-article t nil nil t))
12665
12666 ;; Thread-based commands.
12667
12668 (defun gnus-summary-articles-in-thread (&optional article)
12669   "Return a list of all articles in the current thread.
12670 If ARTICLE is non-nil, return all articles in the thread that starts
12671 with that article."
12672   (let* ((article (or article (gnus-summary-article-number)))
12673          (data (gnus-data-find-list article))
12674          (top-level (gnus-data-level (car data)))
12675          (top-subject
12676           (cond ((null gnus-thread-operation-ignore-subject)
12677                  (gnus-simplify-subject-re
12678                   (mail-header-subject (gnus-data-header (car data)))))
12679                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12680                  (gnus-simplify-subject-fuzzy
12681                   (mail-header-subject (gnus-data-header (car data)))))
12682                 (t nil)))
12683          (end-point (save-excursion
12684                       (if (gnus-summary-go-to-next-thread) 
12685                           (point) (point-max))))
12686          articles)
12687     (while (and data
12688                 (< (gnus-data-pos (car data)) end-point))
12689       (when (or (not top-subject)
12690                 (string= top-subject
12691                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12692                              (gnus-simplify-subject-fuzzy
12693                               (mail-header-subject
12694                                (gnus-data-header (car data))))
12695                            (gnus-simplify-subject-re
12696                             (mail-header-subject
12697                              (gnus-data-header (car data)))))))
12698         (push (gnus-data-number (car data)) articles))
12699       (unless (and (setq data (cdr data))
12700                    (> (gnus-data-level (car data)) top-level))
12701         (setq data nil)))
12702     ;; Return the list of articles.
12703     (nreverse articles)))
12704
12705 (defun gnus-summary-rethread-current ()
12706   "Rethread the thread the current article is part of."
12707   (interactive)
12708   (gnus-set-global-variables)
12709   (let* ((gnus-show-threads t)
12710          (article (gnus-summary-article-number))
12711          (id (mail-header-id (gnus-summary-article-header)))
12712          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12713     (unless id
12714       (error "No article on the current line"))
12715     (gnus-rebuild-thread id)
12716     (gnus-summary-goto-subject article)))
12717
12718 (defun gnus-summary-reparent-thread ()
12719   "Make current article child of the marked (or previous) article.
12720
12721 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12722 is non-nil or the Subject: of both articles are the same."
12723   (interactive)
12724   (or (not (gnus-group-read-only-p))
12725       (error "The current newsgroup does not support article editing."))
12726   (or (<= (length gnus-newsgroup-processable) 1)
12727       (error "No more than one article may be marked."))
12728   (save-window-excursion
12729     (let ((gnus-article-buffer " *reparent*")
12730           (current-article (gnus-summary-article-number))
12731           ; first grab the marked article, otherwise one line up.
12732           (parent-article (if (not (null gnus-newsgroup-processable))
12733                               (car gnus-newsgroup-processable)
12734                             (save-excursion
12735                               (if (eq (forward-line -1) 0)
12736                                   (gnus-summary-article-number)
12737                                 (error "Beginning of summary buffer."))))))
12738       (or (not (eq current-article parent-article))
12739           (error "An article may not be self-referential."))
12740       (let ((message-id (mail-header-id 
12741                          (gnus-summary-article-header parent-article))))
12742         (or (and message-id (not (equal message-id "")))
12743             (error "No message-id in desired parent."))
12744         (gnus-summary-select-article t t nil current-article)
12745         (set-buffer gnus-article-buffer)
12746         (setq buffer-read-only nil)
12747         (let ((buf (format "%s" (buffer-string))))
12748           (erase-buffer)
12749           (insert buf))
12750         (goto-char (point-min))
12751         (if (search-forward-regexp "^References: " nil t)
12752             (insert message-id " " )
12753           (insert "References: " message-id "\n"))
12754         (or (gnus-request-replace-article current-article
12755                                           (car gnus-article-current)
12756                                           gnus-article-buffer)
12757             (error "Couldn't replace article."))
12758         (set-buffer gnus-summary-buffer)
12759         (gnus-summary-unmark-all-processable)
12760         (gnus-summary-rethread-current)
12761         (gnus-message 3 "Article %d is now the child of article %d."
12762                       current-article parent-article)))))
12763
12764 (defun gnus-summary-toggle-threads (&optional arg)
12765   "Toggle showing conversation threads.
12766 If ARG is positive number, turn showing conversation threads on."
12767   (interactive "P")
12768   (gnus-set-global-variables)
12769   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12770     (setq gnus-show-threads
12771           (if (null arg) (not gnus-show-threads)
12772             (> (prefix-numeric-value arg) 0)))
12773     (gnus-summary-prepare)
12774     (gnus-summary-goto-subject current)
12775     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
12776     (gnus-summary-position-point)))
12777
12778 (defun gnus-summary-show-all-threads ()
12779   "Show all threads."
12780   (interactive)
12781   (gnus-set-global-variables)
12782   (save-excursion
12783     (let ((buffer-read-only nil))
12784       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12785   (gnus-summary-position-point))
12786
12787 (defun gnus-summary-show-thread ()
12788   "Show thread subtrees.
12789 Returns nil if no thread was there to be shown."
12790   (interactive)
12791   (gnus-set-global-variables)
12792   (let ((buffer-read-only nil)
12793         (orig (point))
12794         ;; first goto end then to beg, to have point at beg after let
12795         (end (progn (end-of-line) (point)))
12796         (beg (progn (beginning-of-line) (point))))
12797     (prog1
12798         ;; Any hidden lines here?
12799         (search-forward "\r" end t)
12800       (subst-char-in-region beg end ?\^M ?\n t)
12801       (goto-char orig)
12802       (gnus-summary-position-point))))
12803
12804 (defun gnus-summary-hide-all-threads ()
12805   "Hide all thread subtrees."
12806   (interactive)
12807   (gnus-set-global-variables)
12808   (save-excursion
12809     (goto-char (point-min))
12810     (gnus-summary-hide-thread)
12811     (while (zerop (gnus-summary-next-thread 1 t))
12812       (gnus-summary-hide-thread)))
12813   (gnus-summary-position-point))
12814
12815 (defun gnus-summary-hide-thread ()
12816   "Hide thread subtrees.
12817 Returns nil if no threads were there to be hidden."
12818   (interactive)
12819   (gnus-set-global-variables)
12820   (let ((buffer-read-only nil)
12821         (start (point))
12822         (article (gnus-summary-article-number)))
12823     (goto-char start)
12824     ;; Go forward until either the buffer ends or the subthread
12825     ;; ends.
12826     (when (and (not (eobp))
12827                (or (zerop (gnus-summary-next-thread 1 t))
12828                    (goto-char (point-max))))
12829       (prog1
12830           (if (and (> (point) start)
12831                    (search-backward "\n" start t))
12832               (progn
12833                 (subst-char-in-region start (point) ?\n ?\^M)
12834                 (gnus-summary-goto-subject article))
12835             (goto-char start)
12836             nil)
12837         ;;(gnus-summary-position-point)
12838         ))))
12839
12840 (defun gnus-summary-go-to-next-thread (&optional previous)
12841   "Go to the same level (or less) next thread.
12842 If PREVIOUS is non-nil, go to previous thread instead.
12843 Return the article number moved to, or nil if moving was impossible."
12844   (let ((level (gnus-summary-thread-level))
12845         (way (if previous -1 1))
12846         (beg (point)))
12847     (forward-line way)
12848     (while (and (not (eobp))
12849                 (< level (gnus-summary-thread-level)))
12850       (forward-line way))
12851     (if (eobp)
12852         (progn
12853           (goto-char beg)
12854           nil)
12855       (setq beg (point))
12856       (prog1
12857           (gnus-summary-article-number)
12858         (goto-char beg)))))
12859
12860 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12861   "Go to the same level (or less) next thread.
12862 If PREVIOUS is non-nil, go to previous thread instead.
12863 Return the article number moved to, or nil if moving was impossible."
12864   (if (and (eq gnus-summary-make-false-root 'dummy)
12865            (gnus-summary-article-intangible-p))
12866       (let ((beg (point)))
12867         (while (and (zerop (forward-line 1))
12868                     (not (gnus-summary-article-intangible-p))
12869                     (not (zerop (save-excursion 
12870                                   (gnus-summary-thread-level))))))
12871         (if (eobp)
12872             (progn
12873               (goto-char beg)
12874               nil)
12875           (point)))
12876     (let* ((level (gnus-summary-thread-level))
12877            (article (gnus-summary-article-number))
12878            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12879            oart)
12880       (while data
12881         (if (<= (gnus-data-level (car data)) level)
12882             (setq oart (gnus-data-number (car data))
12883                   data nil)
12884           (setq data (cdr data))))
12885       (and oart
12886            (gnus-summary-goto-subject oart)))))
12887
12888 (defun gnus-summary-next-thread (n &optional silent)
12889   "Go to the same level next N'th thread.
12890 If N is negative, search backward instead.
12891 Returns the difference between N and the number of skips actually
12892 done.
12893
12894 If SILENT, don't output messages."
12895   (interactive "p")
12896   (gnus-set-global-variables)
12897   (let ((backward (< n 0))
12898         (n (abs n))
12899         old dum int)
12900     (while (and (> n 0)
12901                 (gnus-summary-go-to-next-thread backward))
12902       (decf n))
12903     (unless silent 
12904       (gnus-summary-position-point))
12905     (when (and (not silent) (/= 0 n))
12906       (gnus-message 7 "No more threads"))
12907     n))
12908
12909 (defun gnus-summary-prev-thread (n)
12910   "Go to the same level previous N'th thread.
12911 Returns the difference between N and the number of skips actually
12912 done."
12913   (interactive "p")
12914   (gnus-set-global-variables)
12915   (gnus-summary-next-thread (- n)))
12916
12917 (defun gnus-summary-go-down-thread ()
12918   "Go down one level in the current thread."
12919   (let ((children (gnus-summary-article-children)))
12920     (and children
12921          (gnus-summary-goto-subject (car children)))))
12922
12923 (defun gnus-summary-go-up-thread ()
12924   "Go up one level in the current thread."
12925   (let ((parent (gnus-summary-article-parent)))
12926     (and parent
12927          (gnus-summary-goto-subject parent))))
12928
12929 (defun gnus-summary-down-thread (n)
12930   "Go down thread N steps.
12931 If N is negative, go up instead.
12932 Returns the difference between N and how many steps down that were
12933 taken."
12934   (interactive "p")
12935   (gnus-set-global-variables)
12936   (let ((up (< n 0))
12937         (n (abs n)))
12938     (while (and (> n 0)
12939                 (if up (gnus-summary-go-up-thread)
12940                   (gnus-summary-go-down-thread)))
12941       (setq n (1- n)))
12942     (gnus-summary-position-point)
12943     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12944     n))
12945
12946 (defun gnus-summary-up-thread (n)
12947   "Go up thread N steps.
12948 If N is negative, go up instead.
12949 Returns the difference between N and how many steps down that were
12950 taken."
12951   (interactive "p")
12952   (gnus-set-global-variables)
12953   (gnus-summary-down-thread (- n)))
12954
12955 (defun gnus-summary-top-thread ()
12956   "Go to the top of the thread."
12957   (interactive)
12958   (gnus-set-global-variables)
12959   (while (gnus-summary-go-up-thread))
12960   (gnus-summary-article-number))
12961
12962 (defun gnus-summary-kill-thread (&optional unmark)
12963   "Mark articles under current thread as read.
12964 If the prefix argument is positive, remove any kinds of marks.
12965 If the prefix argument is negative, tick articles instead."
12966   (interactive "P")
12967   (gnus-set-global-variables)
12968   (when unmark
12969     (setq unmark (prefix-numeric-value unmark)))
12970   (let ((articles (gnus-summary-articles-in-thread)))
12971     (save-excursion
12972       ;; Expand the thread.
12973       (gnus-summary-show-thread)
12974       ;; Mark all the articles.
12975       (while articles
12976         (gnus-summary-goto-subject (car articles))
12977         (cond ((null unmark)
12978                (gnus-summary-mark-article-as-read gnus-killed-mark))
12979               ((> unmark 0)
12980                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12981               (t
12982                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12983         (setq articles (cdr articles))))
12984     ;; Hide killed subtrees.
12985     (and (null unmark)
12986          gnus-thread-hide-killed
12987          (gnus-summary-hide-thread))
12988     ;; If marked as read, go to next unread subject.
12989     (if (null unmark)
12990         ;; Go to next unread subject.
12991         (gnus-summary-next-subject 1 t)))
12992   (gnus-set-mode-line 'summary))
12993
12994 ;; Summary sorting commands
12995
12996 (defun gnus-summary-sort-by-number (&optional reverse)
12997   "Sort summary buffer by article number.
12998 Argument REVERSE means reverse order."
12999   (interactive "P")
13000   (gnus-summary-sort 'number reverse))
13001
13002 (defun gnus-summary-sort-by-author (&optional reverse)
13003   "Sort summary buffer by author name alphabetically.
13004 If case-fold-search is non-nil, case of letters is ignored.
13005 Argument REVERSE means reverse order."
13006   (interactive "P")
13007   (gnus-summary-sort 'author reverse))
13008
13009 (defun gnus-summary-sort-by-subject (&optional reverse)
13010   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
13011 If case-fold-search is non-nil, case of letters is ignored.
13012 Argument REVERSE means reverse order."
13013   (interactive "P")
13014   (gnus-summary-sort 'subject reverse))
13015
13016 (defun gnus-summary-sort-by-date (&optional reverse)
13017   "Sort summary buffer by date.
13018 Argument REVERSE means reverse order."
13019   (interactive "P")
13020   (gnus-summary-sort 'date reverse))
13021
13022 (defun gnus-summary-sort-by-score (&optional reverse)
13023   "Sort summary buffer by score.
13024 Argument REVERSE means reverse order."
13025   (interactive "P")
13026   (gnus-summary-sort 'score reverse))
13027
13028 (defun gnus-summary-sort (predicate reverse)
13029   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
13030   (gnus-set-global-variables)
13031   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
13032          (article (intern (format "gnus-article-sort-by-%s" predicate)))
13033          (gnus-thread-sort-functions
13034           (list
13035            (if (not reverse)
13036                thread
13037              `(lambda (t1 t2)
13038                 (,thread t2 t1)))))
13039          (gnus-article-sort-functions
13040           (list
13041            (if (not reverse)
13042                article
13043              `(lambda (t1 t2)
13044                 (,article t2 t1)))))
13045          (buffer-read-only)
13046          (gnus-summary-prepare-hook nil))
13047     ;; We do the sorting by regenerating the threads.
13048     (gnus-summary-prepare)
13049     ;; Hide subthreads if needed.
13050     (when (and gnus-show-threads gnus-thread-hide-subtree)
13051       (gnus-summary-hide-all-threads)))
13052   ;; If in async mode, we send some info to the backend.
13053   (when gnus-newsgroup-async
13054     (gnus-request-asynchronous
13055      gnus-newsgroup-name gnus-newsgroup-data)))
13056
13057 (defun gnus-sortable-date (date)
13058   "Make sortable string by string-lessp from DATE.
13059 Timezone package is used."
13060   (condition-case ()
13061       (progn
13062         (setq date (inline (timezone-fix-time 
13063                             date nil 
13064                             (aref (inline (timezone-parse-date date)) 4))))
13065         (inline
13066           (timezone-make-sortable-date
13067            (aref date 0) (aref date 1) (aref date 2)
13068            (inline
13069              (timezone-make-time-string
13070               (aref date 3) (aref date 4) (aref date 5))))))
13071     (error "")))
13072   
13073 ;; Summary saving commands.
13074
13075 (defun gnus-summary-save-article (&optional n not-saved)
13076   "Save the current article using the default saver function.
13077 If N is a positive number, save the N next articles.
13078 If N is a negative number, save the N previous articles.
13079 If N is nil and any articles have been marked with the process mark,
13080 save those articles instead.
13081 The variable `gnus-default-article-saver' specifies the saver function."
13082   (interactive "P")
13083   (gnus-set-global-variables)
13084   (let ((articles (gnus-summary-work-articles n))
13085         (save-buffer (save-excursion 
13086                        (nnheader-set-temp-buffer " *Gnus Save*")))
13087         file header article)
13088     (while articles
13089       (setq header (gnus-summary-article-header
13090                     (setq article (pop articles))))
13091       (if (not (vectorp header))
13092           ;; This is a pseudo-article.
13093           (if (assq 'name header)
13094               (gnus-copy-file (cdr (assq 'name header)))
13095             (gnus-message 1 "Article %d is unsaveable" article))
13096         ;; This is a real article.
13097         (save-window-excursion
13098           (gnus-summary-select-article t nil nil article))
13099         (save-excursion
13100           (set-buffer save-buffer)
13101           (erase-buffer)
13102           (insert-buffer-substring gnus-original-article-buffer))
13103         (unless gnus-save-all-headers
13104           ;; Remove headers accoring to `gnus-saved-headers'.
13105           (let ((gnus-visible-headers
13106                  (or gnus-saved-headers gnus-visible-headers))
13107                 (gnus-article-buffer save-buffer))
13108             (gnus-article-hide-headers 1 t)))
13109         (save-window-excursion
13110           (if (not gnus-default-article-saver)
13111               (error "No default saver is defined.")
13112             ;; !!! Magic!  The saving functions all save
13113             ;; `gnus-original-article-buffer' (or so they think),
13114             ;; but we bind that variable to our save-buffer.
13115             (set-buffer gnus-article-buffer)
13116             (let ((gnus-original-article-buffer save-buffer))
13117               (set-buffer gnus-summary-buffer)
13118               (setq file (funcall
13119                           gnus-default-article-saver
13120                           (cond
13121                            ((not gnus-prompt-before-saving)
13122                             'default)
13123                            ((eq gnus-prompt-before-saving 'always)
13124                             nil)
13125                            (t file)))))))
13126         (gnus-summary-remove-process-mark article)
13127         (unless not-saved
13128           (gnus-summary-set-saved-mark article))))
13129     (gnus-kill-buffer save-buffer)
13130     (gnus-summary-position-point)
13131     n))
13132
13133 (defun gnus-summary-pipe-output (&optional arg)
13134   "Pipe the current article to a subprocess.
13135 If N is a positive number, pipe the N next articles.
13136 If N is a negative number, pipe the N previous articles.
13137 If N is nil and any articles have been marked with the process mark,
13138 pipe those articles instead."
13139   (interactive "P")
13140   (gnus-set-global-variables)
13141   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13142     (gnus-summary-save-article arg t))
13143   (gnus-configure-windows 'pipe))
13144
13145 (defun gnus-summary-save-article-mail (&optional arg)
13146   "Append the current article to an mail file.
13147 If N is a positive number, save the N next articles.
13148 If N is a negative number, save the N previous articles.
13149 If N is nil and any articles have been marked with the process mark,
13150 save those articles instead."
13151   (interactive "P")
13152   (gnus-set-global-variables)
13153   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13154     (gnus-summary-save-article arg)))
13155
13156 (defun gnus-summary-save-article-rmail (&optional arg)
13157   "Append the current article to an rmail file.
13158 If N is a positive number, save the N next articles.
13159 If N is a negative number, save the N previous articles.
13160 If N is nil and any articles have been marked with the process mark,
13161 save those articles instead."
13162   (interactive "P")
13163   (gnus-set-global-variables)
13164   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13165     (gnus-summary-save-article arg)))
13166
13167 (defun gnus-summary-save-article-file (&optional arg)
13168   "Append the current article to a file.
13169 If N is a positive number, save the N next articles.
13170 If N is a negative number, save the N previous articles.
13171 If N is nil and any articles have been marked with the process mark,
13172 save those articles instead."
13173   (interactive "P")
13174   (gnus-set-global-variables)
13175   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13176     (gnus-summary-save-article arg)))
13177
13178 (defun gnus-summary-save-article-body-file (&optional arg)
13179   "Append the current article body to a file.
13180 If N is a positive number, save the N next articles.
13181 If N is a negative number, save the N previous articles.
13182 If N is nil and any articles have been marked with the process mark,
13183 save those articles instead."
13184   (interactive "P")
13185   (gnus-set-global-variables)
13186   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13187     (gnus-summary-save-article arg)))
13188
13189 (defun gnus-get-split-value (methods)
13190   "Return a value based on the split METHODS."
13191   (let (split-name method result match)
13192     (when methods
13193       (save-excursion
13194         (set-buffer gnus-original-article-buffer)
13195         (save-restriction
13196           (nnheader-narrow-to-headers)
13197           (while methods
13198             (goto-char (point-min))
13199             (setq method (pop methods))
13200             (setq match (car method))
13201             (when (cond
13202                    ((stringp match)
13203                     ;; Regular expression.
13204                     (condition-case ()
13205                         (re-search-forward match nil t)
13206                       (error nil)))
13207                    ((gnus-functionp match)
13208                     ;; Function.
13209                     (save-restriction
13210                       (widen)
13211                       (setq result (funcall match gnus-newsgroup-name))))
13212                    ((consp match)
13213                     ;; Form.
13214                     (save-restriction
13215                       (widen)
13216                       (setq result (eval match)))))
13217               (setq split-name (append (cdr method) split-name))
13218               (cond ((stringp result)
13219                      (push result split-name))
13220                     ((consp result)
13221                      (setq split-name (append result split-name)))))))))
13222     split-name))
13223
13224 (defun gnus-read-move-group-name (prompt default articles prefix)
13225   "Read a group name."
13226   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13227          (minibuffer-confirm-incomplete nil) ; XEmacs
13228          group-map
13229          (dum (mapatoms
13230                (lambda (g) 
13231                  (and (boundp g)
13232                       (symbol-name g)
13233                       (memq 'respool
13234                             (assoc (symbol-name
13235                                     (car (gnus-find-method-for-group
13236                                           (symbol-name g))))
13237                                    gnus-valid-select-methods))
13238                       (push (list (symbol-name g)) group-map)))
13239                gnus-active-hashtb))
13240          (prom
13241           (format "%s %s to:"
13242                   prompt
13243                   (if (> (length articles) 1)
13244                       (format "these %d articles" (length articles))
13245                     "this article")))
13246          (to-newsgroup
13247           (cond
13248            ((null split-name)
13249             (gnus-completing-read default prom
13250                                   group-map nil nil prefix
13251                                   'gnus-group-history))
13252            ((= 1 (length split-name))
13253             (gnus-completing-read (car split-name) prom group-map
13254                                   nil nil nil
13255                                   'gnus-group-history))
13256            (t
13257             (gnus-completing-read nil prom 
13258                                   (mapcar (lambda (el) (list el))
13259                                           (nreverse split-name))
13260                                   nil nil nil
13261                                   'gnus-group-history)))))
13262     (when to-newsgroup
13263       (if (or (string= to-newsgroup "")
13264               (string= to-newsgroup prefix))
13265           (setq to-newsgroup (or default "")))
13266       (or (gnus-active to-newsgroup)
13267           (gnus-activate-group to-newsgroup)
13268           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13269                                      to-newsgroup))
13270               (or (and (gnus-request-create-group 
13271                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
13272                        (gnus-activate-group to-newsgroup nil nil
13273                                             (gnus-group-name-to-method
13274                                              to-newsgroup)))
13275                   (error "Couldn't create group %s" to-newsgroup)))
13276           (error "No such group: %s" to-newsgroup)))
13277     to-newsgroup))
13278
13279 (defun gnus-read-save-file-name (prompt default-name)
13280   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13281          (file
13282           ;; Let the split methods have their say.
13283           (cond
13284            ;; No split name was found.
13285            ((null split-name)
13286             (read-file-name
13287              (concat prompt " (default "
13288                      (file-name-nondirectory default-name) ") ")
13289              (file-name-directory default-name)
13290              default-name))
13291            ;; A single split name was found
13292            ((= 1 (length split-name))
13293             (let* ((name (car split-name))
13294                    (dir (cond ((file-directory-p name)
13295                                (file-name-as-directory name))
13296                               ((file-exists-p name) name)
13297                               (t gnus-article-save-directory))))
13298               (read-file-name
13299                (concat prompt " (default " name ") ")
13300                dir name)))
13301            ;; A list of splits was found.
13302            (t
13303             (setq split-name (nreverse split-name))
13304             (let (result)
13305               (let ((file-name-history (nconc split-name file-name-history)))
13306                 (setq result
13307                       (read-file-name
13308                        (concat prompt " (`M-p' for defaults) ")
13309                        gnus-article-save-directory
13310                        (car split-name))))
13311               (car (push result file-name-history)))))))
13312     ;; Create the directory.
13313     (unless (equal (directory-file-name file) file)
13314       (make-directory (file-name-directory file) t))
13315     ;; If we have read a directory, we append the default file name.
13316     (when (file-directory-p file)
13317       (setq file (concat (file-name-as-directory file)
13318                          (file-name-nondirectory default-name))))
13319     ;; Possibly translate some characters.
13320     (nnheader-translate-file-chars file)))
13321
13322 (defun gnus-article-archive-name (group)
13323   "Return the first instance of an \"Archive-name\" in the current buffer."
13324   (let ((case-fold-search t))
13325     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13326       (nnheader-concat gnus-article-save-directory
13327                        (match-string 1)))))
13328
13329 (defun gnus-summary-save-in-rmail (&optional filename)
13330   "Append this article to Rmail file.
13331 Optional argument FILENAME specifies file name.
13332 Directory to save to is default to `gnus-article-save-directory'."
13333   (interactive)
13334   (gnus-set-global-variables)
13335   (let ((default-name
13336           (funcall gnus-rmail-save-name gnus-newsgroup-name
13337                    gnus-current-headers gnus-newsgroup-last-rmail)))
13338     (setq filename
13339           (cond ((eq filename 'default)
13340                  default-name)
13341                 (filename filename)
13342                 (t (gnus-read-save-file-name
13343                     "Save in rmail file:" default-name))))
13344     (gnus-make-directory (file-name-directory filename))
13345     (gnus-eval-in-buffer-window gnus-original-article-buffer
13346       (save-excursion
13347         (save-restriction
13348           (widen)
13349           (gnus-output-to-rmail filename))))
13350     ;; Remember the directory name to save articles
13351     (setq gnus-newsgroup-last-rmail filename)))
13352
13353 (defun gnus-summary-save-in-mail (&optional filename)
13354   "Append this article to Unix mail file.
13355 Optional argument FILENAME specifies file name.
13356 Directory to save to is default to `gnus-article-save-directory'."
13357   (interactive)
13358   (gnus-set-global-variables)
13359   (let ((default-name
13360           (funcall gnus-mail-save-name gnus-newsgroup-name
13361                    gnus-current-headers gnus-newsgroup-last-mail)))
13362     (setq filename
13363           (cond ((eq filename 'default)
13364                  default-name)
13365                 (filename filename)
13366                 (t (gnus-read-save-file-name
13367                     "Save in Unix mail file:" default-name))))
13368     (setq filename
13369           (expand-file-name filename
13370                             (and default-name
13371                                  (file-name-directory default-name))))
13372     (gnus-make-directory (file-name-directory filename))
13373     (gnus-eval-in-buffer-window gnus-original-article-buffer
13374       (save-excursion
13375         (save-restriction
13376           (widen)
13377           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13378               (gnus-output-to-rmail filename)
13379             (let ((mail-use-rfc822 t))
13380               (rmail-output filename 1 t t))))))
13381     ;; Remember the directory name to save articles.
13382     (setq gnus-newsgroup-last-mail filename)))
13383
13384 (defun gnus-summary-save-in-file (&optional filename)
13385   "Append this article to file.
13386 Optional argument FILENAME specifies file name.
13387 Directory to save to is default to `gnus-article-save-directory'."
13388   (interactive)
13389   (gnus-set-global-variables)
13390   (let ((default-name
13391           (funcall gnus-file-save-name gnus-newsgroup-name
13392                    gnus-current-headers gnus-newsgroup-last-file)))
13393     (setq filename
13394           (cond ((eq filename 'default)
13395                  default-name)
13396                 (filename filename)
13397                 (t (gnus-read-save-file-name
13398                     "Save in file:" default-name))))
13399     (gnus-make-directory (file-name-directory filename))
13400     (gnus-eval-in-buffer-window gnus-original-article-buffer
13401       (save-excursion
13402         (save-restriction
13403           (widen)
13404           (gnus-output-to-file filename))))
13405     ;; Remember the directory name to save articles.
13406     (setq gnus-newsgroup-last-file filename)))
13407
13408 (defun gnus-summary-save-body-in-file (&optional filename)
13409   "Append this article body to a file.
13410 Optional argument FILENAME specifies file name.
13411 The directory to save in defaults to `gnus-article-save-directory'."
13412   (interactive)
13413   (gnus-set-global-variables)
13414   (let ((default-name
13415           (funcall gnus-file-save-name gnus-newsgroup-name
13416                    gnus-current-headers gnus-newsgroup-last-file)))
13417     (setq filename
13418           (cond ((eq filename 'default)
13419                  default-name)
13420                 (filename filename)
13421                 (t (gnus-read-save-file-name
13422                     "Save body in file:" default-name))))
13423     (gnus-make-directory (file-name-directory filename))
13424     (gnus-eval-in-buffer-window gnus-original-article-buffer
13425       (save-excursion
13426         (save-restriction
13427           (widen)
13428           (goto-char (point-min))
13429           (and (search-forward "\n\n" nil t)
13430                (narrow-to-region (point) (point-max)))
13431           (gnus-output-to-file filename))))
13432     ;; Remember the directory name to save articles.
13433     (setq gnus-newsgroup-last-file filename)))
13434
13435 (defun gnus-summary-save-in-pipe (&optional command)
13436   "Pipe this article to subprocess."
13437   (interactive)
13438   (gnus-set-global-variables)
13439   (setq command
13440         (cond ((eq command 'default)
13441                gnus-last-shell-command)
13442               (command command)
13443               (t (read-string "Shell command on article: "
13444                               gnus-last-shell-command))))
13445   (if (string-equal command "")
13446       (setq command gnus-last-shell-command))
13447   (gnus-eval-in-buffer-window gnus-article-buffer
13448     (save-restriction
13449       (widen)
13450       (shell-command-on-region (point-min) (point-max) command nil)))
13451   (setq gnus-last-shell-command command))
13452
13453 ;; Summary extract commands
13454
13455 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13456   (let ((buffer-read-only nil)
13457         (article (gnus-summary-article-number))
13458         after-article b e)
13459     (or (gnus-summary-goto-subject article)
13460         (error (format "No such article: %d" article)))
13461     (gnus-summary-position-point)
13462     ;; If all commands are to be bunched up on one line, we collect
13463     ;; them here.
13464     (if gnus-view-pseudos-separately
13465         ()
13466       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13467             files action)
13468         (while ps
13469           (setq action (cdr (assq 'action (car ps))))
13470           (setq files (list (cdr (assq 'name (car ps)))))
13471           (while (and ps (cdr ps)
13472                       (string= (or action "1")
13473                                (or (cdr (assq 'action (cadr ps))) "2")))
13474             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13475             (setcdr ps (cddr ps)))
13476           (if (not files)
13477               ()
13478             (if (not (string-match "%s" action))
13479                 (setq files (cons " " files)))
13480             (setq files (cons " " files))
13481             (and (assq 'execute (car ps))
13482                  (setcdr (assq 'execute (car ps))
13483                          (funcall (if (string-match "%s" action)
13484                                       'format 'concat)
13485                                   action
13486                                   (mapconcat (lambda (f) f) files " ")))))
13487           (setq ps (cdr ps)))))
13488     (if (and gnus-view-pseudos (not not-view))
13489         (while pslist
13490           (and (assq 'execute (car pslist))
13491                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13492                                      (eq gnus-view-pseudos 'not-confirm)))
13493           (setq pslist (cdr pslist)))
13494       (save-excursion
13495         (while pslist
13496           (setq after-article (or (cdr (assq 'article (car pslist)))
13497                                   (gnus-summary-article-number)))
13498           (gnus-summary-goto-subject after-article)
13499           (forward-line 1)
13500           (setq b (point))
13501           (insert "    " (file-name-nondirectory
13502                                 (cdr (assq 'name (car pslist))))
13503                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13504           (setq e (point))
13505           (forward-line -1)             ; back to `b'
13506           (gnus-add-text-properties
13507            b (1- e) (list 'gnus-number gnus-reffed-article-number
13508                           gnus-mouse-face-prop gnus-mouse-face))
13509           (gnus-data-enter
13510            after-article gnus-reffed-article-number
13511            gnus-unread-mark b (car pslist) 0 (- e b))
13512           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13513           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13514           (setq pslist (cdr pslist)))))))
13515
13516 (defun gnus-pseudos< (p1 p2)
13517   (let ((c1 (cdr (assq 'action p1)))
13518         (c2 (cdr (assq 'action p2))))
13519     (and c1 c2 (string< c1 c2))))
13520
13521 (defun gnus-request-pseudo-article (props)
13522   (cond ((assq 'execute props)
13523          (gnus-execute-command (cdr (assq 'execute props)))))
13524   (let ((gnus-current-article (gnus-summary-article-number)))
13525     (run-hooks 'gnus-mark-article-hook)))
13526
13527 (defun gnus-execute-command (command &optional automatic)
13528   (save-excursion
13529     (gnus-article-setup-buffer)
13530     (set-buffer gnus-article-buffer)
13531     (setq buffer-read-only nil)
13532     (let ((command (if automatic command (read-string "Command: " command)))
13533           ;; Just binding this here doesn't help, because there might
13534           ;; be output from the process after exiting the scope of 
13535           ;; this `let'.
13536           ;; (buffer-read-only nil)
13537           )
13538       (erase-buffer)
13539       (insert "$ " command "\n\n")
13540       (if gnus-view-pseudo-asynchronously
13541           (start-process "gnus-execute" nil shell-file-name
13542                          shell-command-switch command)
13543         (call-process shell-file-name nil t nil
13544                       shell-command-switch command)))))
13545
13546 (defun gnus-copy-file (file &optional to)
13547   "Copy FILE to TO."
13548   (interactive
13549    (list (read-file-name "Copy file: " default-directory)
13550          (read-file-name "Copy file to: " default-directory)))
13551   (gnus-set-global-variables)
13552   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13553   (and (file-directory-p to)
13554        (setq to (concat (file-name-as-directory to)
13555                         (file-name-nondirectory file))))
13556   (copy-file file to))
13557
13558 ;; Summary kill commands.
13559
13560 (defun gnus-summary-edit-global-kill (article)
13561   "Edit the \"global\" kill file."
13562   (interactive (list (gnus-summary-article-number)))
13563   (gnus-set-global-variables)
13564   (gnus-group-edit-global-kill article))
13565
13566 (defun gnus-summary-edit-local-kill ()
13567   "Edit a local kill file applied to the current newsgroup."
13568   (interactive)
13569   (gnus-set-global-variables)
13570   (setq gnus-current-headers (gnus-summary-article-header))
13571   (gnus-set-global-variables)
13572   (gnus-group-edit-local-kill
13573    (gnus-summary-article-number) gnus-newsgroup-name))
13574
13575 \f
13576 ;;;
13577 ;;; Gnus article mode
13578 ;;;
13579
13580 (put 'gnus-article-mode 'mode-class 'special)
13581
13582 (if gnus-article-mode-map
13583     nil
13584   (setq gnus-article-mode-map (make-keymap))
13585   (suppress-keymap gnus-article-mode-map)
13586
13587   (gnus-define-keys gnus-article-mode-map
13588     " " gnus-article-goto-next-page
13589     "\177" gnus-article-goto-prev-page
13590     [delete] gnus-article-goto-prev-page
13591     "\C-c^" gnus-article-refer-article
13592     "h" gnus-article-show-summary
13593     "s" gnus-article-show-summary
13594     "\C-c\C-m" gnus-article-mail
13595     "?" gnus-article-describe-briefly
13596     gnus-mouse-2 gnus-article-push-button
13597     "\r" gnus-article-press-button
13598     "\t" gnus-article-next-button
13599     "\M-\t" gnus-article-prev-button
13600     "<" beginning-of-buffer
13601     ">" end-of-buffer
13602     "\C-c\C-i" gnus-info-find-node
13603     "\C-c\C-b" gnus-bug)
13604
13605   (substitute-key-definition
13606    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13607
13608 (defun gnus-article-mode ()
13609   "Major mode for displaying an article.
13610
13611 All normal editing commands are switched off.
13612
13613 The following commands are available:
13614
13615 \\<gnus-article-mode-map>
13616 \\[gnus-article-next-page]\t Scroll the article one page forwards
13617 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13618 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13619 \\[gnus-article-show-summary]\t Display the summary buffer
13620 \\[gnus-article-mail]\t Send a reply to the address near point
13621 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13622 \\[gnus-info-find-node]\t Go to the Gnus info node"
13623   (interactive)
13624   (when (and menu-bar-mode
13625              (gnus-visual-p 'article-menu 'menu))
13626     (gnus-article-make-menu-bar))
13627   (kill-all-local-variables)
13628   (gnus-simplify-mode-line)
13629   (setq mode-name "Article")
13630   (setq major-mode 'gnus-article-mode)
13631   (make-local-variable 'minor-mode-alist)
13632   (or (assq 'gnus-show-mime minor-mode-alist)
13633       (setq minor-mode-alist
13634             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13635   (use-local-map gnus-article-mode-map)
13636   (make-local-variable 'page-delimiter)
13637   (setq page-delimiter gnus-page-delimiter)
13638   (buffer-disable-undo (current-buffer))
13639   (setq buffer-read-only t)             ;Disable modification
13640   (run-hooks 'gnus-article-mode-hook))
13641
13642 (defun gnus-article-setup-buffer ()
13643   "Initialize the article buffer."
13644   (let* ((name (if gnus-single-article-buffer "*Article*"
13645                  (concat "*Article " gnus-newsgroup-name "*")))
13646          (original
13647           (progn (string-match "\\*Article" name)
13648                  (concat " *Original Article"
13649                          (substring name (match-end 0))))))
13650     (setq gnus-article-buffer name)
13651     (setq gnus-original-article-buffer original)
13652     ;; This might be a variable local to the summary buffer.
13653     (unless gnus-single-article-buffer
13654       (save-excursion
13655         (set-buffer gnus-summary-buffer)
13656         (setq gnus-article-buffer name)
13657         (setq gnus-original-article-buffer original)
13658         (gnus-set-global-variables))
13659       (make-local-variable 'gnus-summary-buffer))
13660     ;; Init original article buffer.
13661     (save-excursion
13662       (set-buffer (get-buffer-create gnus-original-article-buffer))
13663       (buffer-disable-undo (current-buffer))
13664       (setq major-mode 'gnus-original-article-mode)
13665       (gnus-add-current-to-buffer-list)
13666       (make-local-variable 'gnus-original-article))
13667     (if (get-buffer name)
13668         (save-excursion
13669           (set-buffer name)
13670           (buffer-disable-undo (current-buffer))
13671           (setq buffer-read-only t)
13672           (gnus-add-current-to-buffer-list)
13673           (or (eq major-mode 'gnus-article-mode)
13674               (gnus-article-mode))
13675           (current-buffer))
13676       (save-excursion
13677         (set-buffer (get-buffer-create name))
13678         (gnus-add-current-to-buffer-list)
13679         (gnus-article-mode)
13680         (current-buffer)))))
13681
13682 ;; Set article window start at LINE, where LINE is the number of lines
13683 ;; from the head of the article.
13684 (defun gnus-article-set-window-start (&optional line)
13685   (set-window-start
13686    (get-buffer-window gnus-article-buffer t)
13687    (save-excursion
13688      (set-buffer gnus-article-buffer)
13689      (goto-char (point-min))
13690      (if (not line)
13691          (point-min)
13692        (gnus-message 6 "Moved to bookmark")
13693        (search-forward "\n\n" nil t)
13694        (forward-line line)
13695        (point)))))
13696
13697 (defun gnus-kill-all-overlays ()
13698   "Delete all overlays in the current buffer."
13699   (when (fboundp 'overlay-lists)
13700     (let* ((overlayss (overlay-lists))
13701            (buffer-read-only nil)
13702            (overlays (nconc (car overlayss) (cdr overlayss))))
13703       (while overlays
13704         (delete-overlay (pop overlays))))))
13705
13706 (defun gnus-request-article-this-buffer (article group)
13707   "Get an article and insert it into this buffer."
13708   (let (do-update-line)
13709     (prog1
13710         (save-excursion
13711           (erase-buffer)
13712           (gnus-kill-all-overlays)
13713           (setq group (or group gnus-newsgroup-name))
13714
13715           ;; Open server if it has closed.
13716           (gnus-check-server (gnus-find-method-for-group group))
13717
13718           ;; Using `gnus-request-article' directly will insert the article into
13719           ;; `nntp-server-buffer' - so we'll save some time by not having to
13720           ;; copy it from the server buffer into the article buffer.
13721
13722           ;; We only request an article by message-id when we do not have the
13723           ;; headers for it, so we'll have to get those.
13724           (when (stringp article)
13725             (let ((gnus-override-method gnus-refer-article-method))
13726               (gnus-read-header article)))
13727
13728           ;; If the article number is negative, that means that this article
13729           ;; doesn't belong in this newsgroup (possibly), so we find its
13730           ;; message-id and request it by id instead of number.
13731           (when (and (numberp article)
13732                      gnus-summary-buffer
13733                      (get-buffer gnus-summary-buffer)
13734                      (buffer-name (get-buffer gnus-summary-buffer)))
13735             (save-excursion
13736               (set-buffer gnus-summary-buffer)
13737               (let ((header (gnus-summary-article-header article)))
13738                 (if (< article 0)
13739                     (cond 
13740                      ((memq article gnus-newsgroup-sparse)
13741                       ;; This is a sparse gap article.
13742                       (setq do-update-line article)
13743                       (setq article (mail-header-id header))
13744                       (let ((gnus-override-method gnus-refer-article-method))
13745                         (gnus-read-header article))
13746                       (setq gnus-newsgroup-sparse
13747                             (delq article gnus-newsgroup-sparse)))
13748                      ((vectorp header)
13749                       ;; It's a real article.
13750                       (setq article (mail-header-id header)))
13751                      (t
13752                       ;; It is an extracted pseudo-article.
13753                       (setq article 'pseudo)
13754                       (gnus-request-pseudo-article header))))
13755                 
13756                 (let ((method (gnus-find-method-for-group 
13757                                gnus-newsgroup-name)))
13758                   (if (not (eq (car method) 'nneething))
13759                       ()
13760                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13761                                        (mail-header-subject header))))
13762                       (if (file-directory-p dir)
13763                           (progn
13764                             (setq article 'nneething)
13765                             (gnus-group-enter-directory dir)))))))))
13766
13767           (cond
13768            ;; Refuse to select canceled articles.
13769            ((and (numberp article)
13770                  gnus-summary-buffer
13771                  (get-buffer gnus-summary-buffer)
13772                  (buffer-name (get-buffer gnus-summary-buffer))
13773                  (eq (cdr (save-excursion
13774                             (set-buffer gnus-summary-buffer)
13775                             (assq article gnus-newsgroup-reads)))
13776                      gnus-canceled-mark))
13777             nil)
13778            ;; We first check `gnus-original-article-buffer'.
13779            ((and (get-buffer gnus-original-article-buffer)
13780                  (numberp article)
13781                  (save-excursion
13782                    (set-buffer gnus-original-article-buffer)
13783                    (and (equal (car gnus-original-article) group)
13784                         (eq (cdr gnus-original-article) article))))
13785             (insert-buffer-substring gnus-original-article-buffer)
13786             'article)
13787            ;; Check the backlog.
13788            ((and gnus-keep-backlog
13789                  (gnus-backlog-request-article group article (current-buffer)))
13790             'article)
13791            ;; Check the cache.
13792            ((and gnus-use-cache
13793                  (numberp article)
13794                  (gnus-cache-request-article article group))
13795             'article)
13796            ;; Get the article and put into the article buffer.
13797            ((or (stringp article) (numberp article))
13798             (let ((gnus-override-method
13799                    (and (stringp article) gnus-refer-article-method))
13800                   (buffer-read-only nil))
13801               (erase-buffer)
13802               (gnus-kill-all-overlays)
13803               (if (gnus-request-article article group (current-buffer))
13804                   (progn
13805                     (and gnus-keep-backlog
13806                          (numberp article)
13807                          (gnus-backlog-enter-article
13808                           group article (current-buffer)))
13809                     'article))))
13810            ;; It was a pseudo.
13811            (t article)))
13812
13813       ;; Take the article from the original article buffer
13814       ;; and place it in the buffer it's supposed to be in.
13815       (when (and (get-buffer gnus-article-buffer)
13816                  ;;(numberp article)
13817                  (equal (buffer-name (current-buffer))
13818                         (buffer-name (get-buffer gnus-article-buffer))))
13819         (save-excursion
13820           (if (get-buffer gnus-original-article-buffer)
13821               (set-buffer (get-buffer gnus-original-article-buffer))
13822             (set-buffer (get-buffer-create gnus-original-article-buffer))
13823             (buffer-disable-undo (current-buffer))
13824             (setq major-mode 'gnus-original-article-mode)
13825             (setq buffer-read-only t)
13826             (gnus-add-current-to-buffer-list))
13827           (let (buffer-read-only)
13828             (erase-buffer)
13829             (insert-buffer-substring gnus-article-buffer))
13830           (setq gnus-original-article (cons group article))))
13831     
13832       ;; Update sparse articles.
13833       (when (and do-update-line
13834                  (or (numberp article)
13835                      (stringp article)))
13836         (let ((buf (current-buffer)))
13837           (set-buffer gnus-summary-buffer)
13838           (gnus-summary-update-article do-update-line)
13839           (gnus-summary-goto-subject do-update-line nil t)
13840           (set-window-point (get-buffer-window (current-buffer) t)
13841                             (point))
13842           (set-buffer buf))))))
13843
13844 (defun gnus-read-header (id &optional header)
13845   "Read the headers of article ID and enter them into the Gnus system."
13846   (let ((group gnus-newsgroup-name)
13847         (gnus-override-method 
13848          (and (gnus-news-group-p gnus-newsgroup-name)
13849               gnus-refer-article-method))       
13850         where)
13851     ;; First we check to see whether the header in question is already
13852     ;; fetched.
13853     (if (stringp id)
13854         ;; This is a Message-ID.
13855         (setq header (or header (gnus-id-to-header id)))
13856       ;; This is an article number.
13857       (setq header (or header (gnus-summary-article-header id))))
13858     (if (and header
13859              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13860         ;; We have found the header.
13861         header
13862       ;; We have to really fetch the header to this article.
13863       (when (setq where (gnus-request-head id group))
13864         (save-excursion
13865           (set-buffer nntp-server-buffer)
13866           (goto-char (point-max))
13867           (insert ".\n")
13868           (goto-char (point-min))
13869           (insert "211 ")
13870           (princ (cond
13871                   ((numberp id) id)
13872                   ((cdr where) (cdr where))
13873                   (header (mail-header-number header))
13874                   (t gnus-reffed-article-number))
13875                  (current-buffer))
13876           (insert " Article retrieved.\n"))
13877         ;(when (and header
13878         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13879         ;  (setcar (gnus-id-to-thread id) nil))
13880         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13881             ()                          ; Malformed head.
13882           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13883             (if (and (stringp id)
13884                      (not (string= (gnus-group-real-name group)
13885                                    (car where))))
13886                 ;; If we fetched by Message-ID and the article came
13887                 ;; from a different group, we fudge some bogus article
13888                 ;; numbers for this article.
13889                 (mail-header-set-number header gnus-reffed-article-number))
13890             (decf gnus-reffed-article-number)
13891             (gnus-remove-header (mail-header-number header))
13892             (push header gnus-newsgroup-headers)
13893             (setq gnus-current-headers header)
13894             (push (mail-header-number header) gnus-newsgroup-limit))
13895           header)))))
13896
13897 (defun gnus-remove-header (number)
13898   "Remove header NUMBER from `gnus-newsgroup-headers'."
13899   (if (and gnus-newsgroup-headers
13900            (= number (mail-header-number (car gnus-newsgroup-headers))))
13901       (pop gnus-newsgroup-headers)
13902     (let ((headers gnus-newsgroup-headers))
13903       (while (and (cdr headers)
13904                   (not (= number (mail-header-number (cadr headers)))))
13905         (pop headers))
13906       (when (cdr headers)
13907         (setcdr headers (cddr headers))))))
13908
13909 (defun gnus-article-prepare (article &optional all-headers header)
13910   "Prepare ARTICLE in article mode buffer.
13911 ARTICLE should either be an article number or a Message-ID.
13912 If ARTICLE is an id, HEADER should be the article headers.
13913 If ALL-HEADERS is non-nil, no headers are hidden."
13914   (save-excursion
13915     ;; Make sure we start in a summary buffer.
13916     (unless (eq major-mode 'gnus-summary-mode)
13917       (set-buffer gnus-summary-buffer))
13918     (setq gnus-summary-buffer (current-buffer))
13919     ;; Make sure the connection to the server is alive.
13920     (unless (gnus-server-opened
13921              (gnus-find-method-for-group gnus-newsgroup-name))
13922       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13923       (gnus-request-group gnus-newsgroup-name t))
13924     (let* ((article (if header (mail-header-number header) article))
13925            (summary-buffer (current-buffer))
13926            (internal-hook gnus-article-internal-prepare-hook)
13927            (group gnus-newsgroup-name)
13928            result)
13929       (save-excursion
13930         (gnus-article-setup-buffer)
13931         (set-buffer gnus-article-buffer)
13932         ;; Deactivate active regions.
13933         (when (and (boundp 'transient-mark-mode)
13934                    transient-mark-mode)
13935           (setq mark-active nil))
13936         (if (not (setq result (let ((buffer-read-only nil))
13937                                 (gnus-request-article-this-buffer
13938                                  article group))))
13939             ;; There is no such article.
13940             (save-excursion
13941               (when (and (numberp article)
13942                          (not (memq article gnus-newsgroup-sparse)))
13943                 (setq gnus-article-current
13944                       (cons gnus-newsgroup-name article))
13945                 (set-buffer gnus-summary-buffer)
13946                 (setq gnus-current-article article)
13947                 (gnus-summary-mark-article article gnus-canceled-mark))
13948               (unless (memq article gnus-newsgroup-sparse)
13949                 (gnus-error
13950                  1 "No such article (may have expired or been canceled)")))
13951           (if (or (eq result 'pseudo) (eq result 'nneething))
13952               (progn
13953                 (save-excursion
13954                   (set-buffer summary-buffer)
13955                   (setq gnus-last-article gnus-current-article
13956                         gnus-newsgroup-history (cons gnus-current-article
13957                                                      gnus-newsgroup-history)
13958                         gnus-current-article 0
13959                         gnus-current-headers nil
13960                         gnus-article-current nil)
13961                   (if (eq result 'nneething)
13962                       (gnus-configure-windows 'summary)
13963                     (gnus-configure-windows 'article))
13964                   (gnus-set-global-variables))
13965                 (gnus-set-mode-line 'article))
13966             ;; The result from the `request' was an actual article -
13967             ;; or at least some text that is now displayed in the
13968             ;; article buffer.
13969             (if (and (numberp article)
13970                      (not (eq article gnus-current-article)))
13971                 ;; Seems like a new article has been selected.
13972                 ;; `gnus-current-article' must be an article number.
13973                 (save-excursion
13974                   (set-buffer summary-buffer)
13975                   (setq gnus-last-article gnus-current-article
13976                         gnus-newsgroup-history (cons gnus-current-article
13977                                                      gnus-newsgroup-history)
13978                         gnus-current-article article
13979                         gnus-current-headers
13980                         (gnus-summary-article-header gnus-current-article)
13981                         gnus-article-current
13982                         (cons gnus-newsgroup-name gnus-current-article))
13983                   (unless (vectorp gnus-current-headers)
13984                     (setq gnus-current-headers nil))
13985                   (gnus-summary-show-thread)
13986                   (run-hooks 'gnus-mark-article-hook)
13987                   (gnus-set-mode-line 'summary)
13988                   (and (gnus-visual-p 'article-highlight 'highlight)
13989                        (run-hooks 'gnus-visual-mark-article-hook))
13990                   ;; Set the global newsgroup variables here.
13991                   ;; Suggested by Jim Sisolak
13992                   ;; <sisolak@trans4.neep.wisc.edu>.
13993                   (gnus-set-global-variables)
13994                   (setq gnus-have-all-headers
13995                         (or all-headers gnus-show-all-headers))
13996                   (and gnus-use-cache
13997                        (vectorp (gnus-summary-article-header article))
13998                        (gnus-cache-possibly-enter-article
13999                         group article
14000                         (gnus-summary-article-header article)
14001                         (memq article gnus-newsgroup-marked)
14002                         (memq article gnus-newsgroup-dormant)
14003                         (memq article gnus-newsgroup-unreads)))))
14004             (when (or (numberp article)
14005                       (stringp article))
14006               ;; Hooks for getting information from the article.
14007               ;; This hook must be called before being narrowed.
14008               (let (buffer-read-only)
14009                 (run-hooks 'internal-hook)
14010                 (run-hooks 'gnus-article-prepare-hook)
14011                 ;; Decode MIME message.
14012                 (if gnus-show-mime
14013                     (if (or (not gnus-strict-mime)
14014                             (gnus-fetch-field "Mime-Version"))
14015                         (funcall gnus-show-mime-method)
14016                       (funcall gnus-decode-encoded-word-method)))
14017                 ;; Perform the article display hooks.
14018                 (run-hooks 'gnus-article-display-hook))
14019               ;; Do page break.
14020               (goto-char (point-min))
14021               (and gnus-break-pages (gnus-narrow-to-page)))
14022             (gnus-set-mode-line 'article)
14023             (gnus-configure-windows 'article)
14024             (goto-char (point-min))
14025             t))))))
14026
14027 (defun gnus-article-show-all-headers ()
14028   "Show all article headers in article mode buffer."
14029   (save-excursion
14030     (gnus-article-setup-buffer)
14031     (set-buffer gnus-article-buffer)
14032     (let ((buffer-read-only nil))
14033       (gnus-unhide-text (point-min) (point-max)))))
14034
14035 (defun gnus-article-hide-headers-if-wanted ()
14036   "Hide unwanted headers if `gnus-have-all-headers' is nil.
14037 Provided for backwards compatibility."
14038   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
14039       gnus-inhibit-hiding
14040       (gnus-article-hide-headers)))
14041
14042 (defsubst gnus-article-header-rank ()
14043   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
14044   (let ((list gnus-sorted-header-list)
14045         (i 0))
14046     (while list
14047       (when (looking-at (car list))
14048         (setq list nil))
14049       (setq list (cdr list))
14050       (incf i))
14051     i))
14052
14053 (defun gnus-article-hide-headers (&optional arg delete)
14054   "Toggle whether to hide unwanted headers and possibly sort them as well.
14055 If given a negative prefix, always show; if given a positive prefix,
14056 always hide."
14057   (interactive (gnus-hidden-arg))
14058   (if (gnus-article-check-hidden-text 'headers arg)
14059       ;; Show boring headers as well.
14060       (gnus-article-show-hidden-text 'boring-headers)
14061     ;; This function might be inhibited.
14062     (unless gnus-inhibit-hiding
14063       (save-excursion
14064         (set-buffer gnus-article-buffer)
14065         (save-restriction
14066           (let ((buffer-read-only nil)
14067                 (props (nconc (list 'gnus-type 'headers)
14068                               gnus-hidden-properties))
14069                 (max (1+ (length gnus-sorted-header-list)))
14070                 (ignored (when (not (stringp gnus-visible-headers))
14071                            (cond ((stringp gnus-ignored-headers)
14072                                   gnus-ignored-headers)
14073                                  ((listp gnus-ignored-headers)
14074                                   (mapconcat 'identity gnus-ignored-headers
14075                                              "\\|")))))
14076                 (visible
14077                  (cond ((stringp gnus-visible-headers)
14078                         gnus-visible-headers)
14079                        ((and gnus-visible-headers
14080                              (listp gnus-visible-headers))
14081                         (mapconcat 'identity gnus-visible-headers "\\|"))))
14082                 (inhibit-point-motion-hooks t)
14083                 want-list beg)
14084             ;; First we narrow to just the headers.
14085             (widen)
14086             (goto-char (point-min))
14087             ;; Hide any "From " lines at the beginning of (mail) articles.
14088             (while (looking-at "From ")
14089               (forward-line 1))
14090             (unless (bobp)
14091               (if delete
14092                   (delete-region (point-min) (point))
14093                 (gnus-hide-text (point-min) (point) props)))
14094             ;; Then treat the rest of the header lines.
14095             (narrow-to-region
14096              (point)
14097              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
14098             ;; Then we use the two regular expressions
14099             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
14100             ;; select which header lines is to remain visible in the
14101             ;; article buffer.
14102             (goto-char (point-min))
14103             (while (re-search-forward "^[^ \t]*:" nil t)
14104               (beginning-of-line)
14105               ;; We add the headers we want to keep to a list and delete
14106               ;; them from the buffer.
14107               (gnus-put-text-property 
14108                (point) (1+ (point)) 'message-rank
14109                (if (or (and visible (looking-at visible))
14110                        (and ignored
14111                             (not (looking-at ignored))))
14112                    (gnus-article-header-rank) 
14113                  (+ 2 max)))
14114               (forward-line 1))
14115             (message-sort-headers-1)
14116             (when (setq beg (text-property-any 
14117                              (point-min) (point-max) 'message-rank (+ 2 max)))
14118               ;; We make the unwanted headers invisible.
14119               (if delete
14120                   (delete-region beg (point-max))
14121                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
14122                 (gnus-hide-text-type beg (point-max) 'headers))
14123               ;; Work around XEmacs lossage.
14124               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
14125
14126 (defun gnus-article-hide-boring-headers (&optional arg)
14127   "Toggle hiding of headers that aren't very interesting.
14128 If given a negative prefix, always show; if given a positive prefix,
14129 always hide."
14130   (interactive (gnus-hidden-arg))
14131   (unless (gnus-article-check-hidden-text 'boring-headers arg)
14132     (save-excursion
14133       (set-buffer gnus-article-buffer)
14134       (save-restriction
14135         (let ((buffer-read-only nil)
14136               (list gnus-boring-article-headers)
14137               (inhibit-point-motion-hooks t)
14138               elem)
14139           (nnheader-narrow-to-headers)
14140           (while list
14141             (setq elem (pop list))
14142             (goto-char (point-min))
14143             (cond
14144              ;; Hide empty headers.
14145              ((eq elem 'empty)
14146               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14147                 (forward-line -1)
14148                 (gnus-hide-text-type
14149                  (progn (beginning-of-line) (point))
14150                  (progn 
14151                    (end-of-line)
14152                    (if (re-search-forward "^[^ \t]" nil t)
14153                        (match-beginning 0)
14154                      (point-max)))
14155                  'boring-headers)))
14156              ;; Hide boring Newsgroups header.
14157              ((eq elem 'newsgroups)
14158               (when (equal (message-fetch-field "newsgroups")
14159                            (gnus-group-real-name gnus-newsgroup-name))
14160                 (gnus-article-hide-header "newsgroups")))
14161              ((eq elem 'followup-to)
14162               (when (equal (message-fetch-field "followup-to")
14163                            (message-fetch-field "newsgroups"))
14164                 (gnus-article-hide-header "followup-to")))
14165              ((eq elem 'reply-to)
14166               (let ((from (message-fetch-field "from"))
14167                     (reply-to (message-fetch-field "reply-to")))
14168                 (when (and
14169                        from reply-to
14170                        (equal 
14171                         (nth 1 (funcall gnus-extract-address-components from))
14172                         (nth 1 (funcall gnus-extract-address-components
14173                                         reply-to))))
14174                   (gnus-article-hide-header "reply-to"))))
14175              ((eq elem 'date)
14176               (let ((date (message-fetch-field "date")))
14177                 (when (and date
14178                            (< (gnus-days-between date (current-time-string))
14179                               4))
14180                   (gnus-article-hide-header "date")))))))))))
14181
14182 (defun gnus-article-hide-header (header)
14183   (save-excursion
14184     (goto-char (point-min))
14185     (when (re-search-forward (concat "^" header ":") nil t)
14186       (gnus-hide-text-type
14187        (progn (beginning-of-line) (point))
14188        (progn 
14189          (end-of-line)
14190          (if (re-search-forward "^[^ \t]" nil t)
14191              (match-beginning 0)
14192            (point-max)))
14193        'boring-headers))))
14194
14195 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14196 (defun gnus-article-treat-overstrike ()
14197   "Translate overstrikes into bold text."
14198   (interactive)
14199   (save-excursion
14200     (set-buffer gnus-article-buffer)
14201     (let ((buffer-read-only nil))
14202       (while (search-forward "\b" nil t)
14203         (let ((next (following-char))
14204               (previous (char-after (- (point) 2))))
14205           (cond 
14206            ((eq next previous)
14207             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14208             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14209            ((eq next ?_)
14210             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14211             (gnus-put-text-property
14212              (- (point) 2) (1- (point)) 'face 'underline))
14213            ((eq previous ?_)
14214             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14215             (gnus-put-text-property
14216              (point) (1+ (point))       'face 'underline))))))))
14217
14218 (defun gnus-article-word-wrap ()
14219   "Format too long lines."
14220   (interactive)
14221   (save-excursion
14222     (set-buffer gnus-article-buffer)
14223     (let ((buffer-read-only nil))
14224       (widen)
14225       (goto-char (point-min))
14226       (search-forward "\n\n" nil t)
14227       (end-of-line 1)
14228       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14229             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14230             (adaptive-fill-mode t))
14231         (while (not (eobp))
14232           (and (>= (current-column) (min fill-column (window-width)))
14233                (/= (preceding-char) ?:)
14234                (fill-paragraph nil))
14235           (end-of-line 2))))))
14236
14237 (defun gnus-article-remove-cr ()
14238   "Remove carriage returns from an article."
14239   (interactive)
14240   (save-excursion
14241     (set-buffer gnus-article-buffer)
14242     (let ((buffer-read-only nil))
14243       (goto-char (point-min))
14244       (while (search-forward "\r" nil t)
14245         (replace-match "" t t)))))
14246
14247 (defun gnus-article-remove-trailing-blank-lines ()
14248   "Remove all trailing blank lines from the article."
14249   (interactive)
14250   (save-excursion
14251     (set-buffer gnus-article-buffer)
14252     (let ((buffer-read-only nil))
14253       (goto-char (point-max))
14254       (delete-region
14255        (point)
14256        (progn
14257          (while (looking-at "^[ \t]*$")
14258            (forward-line -1))
14259          (forward-line 1)
14260          (point))))))
14261
14262 (defun gnus-article-display-x-face (&optional force)
14263   "Look for an X-Face header and display it if present."
14264   (interactive (list 'force))
14265   (save-excursion
14266     (set-buffer gnus-article-buffer)
14267     ;; Delete the old process, if any.
14268     (when (process-status "gnus-x-face")
14269       (delete-process "gnus-x-face"))
14270     (let ((inhibit-point-motion-hooks t)
14271           (case-fold-search nil)
14272           from)
14273       (save-restriction
14274         (nnheader-narrow-to-headers)
14275         (setq from (message-fetch-field "from"))
14276         (goto-char (point-min))
14277         (when (and gnus-article-x-face-command
14278                    (or force
14279                        ;; Check whether this face is censored.
14280                        (not gnus-article-x-face-too-ugly)
14281                        (and gnus-article-x-face-too-ugly from
14282                             (not (string-match gnus-article-x-face-too-ugly
14283                                                from))))
14284                    ;; Has to be present.
14285                    (re-search-forward "^X-Face: " nil t))
14286           ;; We now have the area of the buffer where the X-Face is stored.
14287           (let ((beg (point))
14288                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14289             ;; We display the face.
14290             (if (symbolp gnus-article-x-face-command)
14291                 ;; The command is a lisp function, so we call it.
14292                 (if (gnus-functionp gnus-article-x-face-command)
14293                     (funcall gnus-article-x-face-command beg end)
14294                   (error "%s is not a function" gnus-article-x-face-command))
14295               ;; The command is a string, so we interpret the command
14296               ;; as a, well, command, and fork it off.
14297               (let ((process-connection-type nil))
14298                 (process-kill-without-query
14299                  (start-process
14300                   "gnus-x-face" nil shell-file-name shell-command-switch
14301                   gnus-article-x-face-command))
14302                 (process-send-region "gnus-x-face" beg end)
14303                 (process-send-eof "gnus-x-face")))))))))
14304
14305 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14306 (defun gnus-decode-rfc1522 ()
14307   "Hack to remove QP encoding from headers."
14308   (let ((case-fold-search t)
14309         (inhibit-point-motion-hooks t)
14310         (buffer-read-only nil)
14311         string)
14312     (save-restriction
14313       (narrow-to-region
14314        (goto-char (point-min))
14315        (or (search-forward "\n\n" nil t) (point-max)))
14316
14317       (goto-char (point-min))
14318       (while (re-search-forward 
14319               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14320         (setq string (match-string 1))
14321         (narrow-to-region (match-beginning 0) (match-end 0))
14322         (delete-region (point-min) (point-max))
14323         (insert string)
14324         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14325         (subst-char-in-region (point-min) (point-max) ?_ ? )
14326         (widen)
14327         (goto-char (point-min))))))
14328
14329 (defun gnus-article-de-quoted-unreadable (&optional force)
14330   "Do a naive translation of a quoted-printable-encoded article.
14331 This is in no way, shape or form meant as a replacement for real MIME
14332 processing, but is simply a stop-gap measure until MIME support is
14333 written.
14334 If FORCE, decode the article whether it is marked as quoted-printable
14335 or not."
14336   (interactive (list 'force))
14337   (save-excursion
14338     (set-buffer gnus-article-buffer)
14339     (let ((case-fold-search t)
14340           (buffer-read-only nil)
14341           (type (gnus-fetch-field "content-transfer-encoding")))
14342       (gnus-decode-rfc1522)
14343       (when (or force
14344                 (and type (string-match "quoted-printable" (downcase type))))
14345         (goto-char (point-min))
14346         (search-forward "\n\n" nil 'move)
14347         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14348
14349 (defun gnus-mime-decode-quoted-printable (from to)
14350   "Decode Quoted-Printable in the region between FROM and TO."
14351   (interactive "r")
14352   (goto-char from)
14353   (while (search-forward "=" to t)
14354     (cond ((eq (following-char) ?\n)
14355            (delete-char -1)
14356            (delete-char 1))
14357           ((looking-at "[0-9A-F][0-9A-F]")
14358            (subst-char-in-region
14359             (1- (point)) (point) ?=
14360             (hexl-hex-string-to-integer
14361              (buffer-substring (point) (+ 2 (point)))))
14362            (delete-char 2))
14363           ((looking-at "=")
14364            (delete-char 1))
14365           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14366
14367 (defun gnus-article-hide-pgp (&optional arg)
14368   "Toggle hiding of any PGP headers and signatures in the current article.
14369 If given a negative prefix, always show; if given a positive prefix,
14370 always hide."
14371   (interactive (gnus-hidden-arg))
14372   (unless (gnus-article-check-hidden-text 'pgp arg)
14373     (save-excursion
14374       (set-buffer gnus-article-buffer)
14375       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14376             buffer-read-only beg end)
14377         (widen)
14378         (goto-char (point-min))
14379         ;; Hide the "header".
14380         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14381              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14382         (setq beg (point))
14383         ;; Hide the actual signature.
14384         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14385              (setq end (1+ (match-beginning 0)))
14386              (gnus-hide-text
14387               end
14388               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14389                   (match-end 0)
14390                 ;; Perhaps we shouldn't hide to the end of the buffer
14391                 ;; if there is no end to the signature?
14392                 (point-max))
14393               props))
14394         ;; Hide "- " PGP quotation markers.
14395         (when (and beg end)
14396           (narrow-to-region beg end)
14397           (goto-char (point-min))
14398           (while (re-search-forward "^- " nil t)
14399             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14400           (widen))))))
14401
14402 (defun gnus-article-hide-pem (&optional arg)
14403   "Toggle hiding of any PEM headers and signatures in the current article.
14404 If given a negative prefix, always show; if given a positive prefix,
14405 always hide."
14406   (interactive (gnus-hidden-arg))
14407   (unless (gnus-article-check-hidden-text 'pem arg)
14408     (save-excursion
14409       (set-buffer gnus-article-buffer)
14410       (let ((props (nconc (list 'gnus-type 'pem) gnus-hidden-properties))
14411             buffer-read-only end)
14412         (widen)
14413         (goto-char (point-min))
14414         ;; hide the horrendously ugly "header".
14415         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
14416                              nil
14417                              t)
14418              (setq end (1+ (match-beginning 0)))
14419              (gnus-hide-text
14420               end
14421               (if (search-forward "\n\n" nil t)
14422                   (match-end 0)
14423                 (point-max))
14424               props))
14425         ;; hide the trailer as well
14426         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
14427                              nil
14428                              t)
14429              (gnus-hide-text (match-beginning 0) (match-end 0) props))))))
14430
14431 (defun gnus-article-hide-signature (&optional arg)
14432   "Hide the signature in the current article.
14433 If given a negative prefix, always show; if given a positive prefix,
14434 always hide."
14435   (interactive (gnus-hidden-arg))
14436   (unless (gnus-article-check-hidden-text 'signature arg)
14437     (save-excursion
14438       (set-buffer gnus-article-buffer)
14439       (save-restriction
14440         (let ((buffer-read-only nil))
14441           (when (gnus-narrow-to-signature)
14442             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14443
14444 (defun gnus-article-strip-leading-blank-lines ()
14445   "Remove all blank lines from the beginning of the article."
14446   (interactive)
14447   (save-excursion
14448     (set-buffer gnus-article-buffer)
14449     (let (buffer-read-only)
14450       (goto-char (point-min))
14451       (when (search-forward "\n\n" nil t)
14452         (while (looking-at "[ \t]$")
14453           (gnus-delete-line))))))
14454
14455 (defvar mime::preview/content-list)
14456 (defvar mime::preview-content-info/point-min)
14457 (defun gnus-narrow-to-signature ()
14458   "Narrow to the signature."
14459   (widen)
14460   (if (and (boundp 'mime::preview/content-list)
14461            mime::preview/content-list)
14462       (let ((pcinfo (car (last mime::preview/content-list))))
14463         (condition-case ()
14464             (narrow-to-region
14465              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14466              (point-max))
14467           (error nil))))
14468   (goto-char (point-max))
14469   (when (re-search-backward gnus-signature-separator nil t)
14470     (forward-line 1)
14471     (when (or (null gnus-signature-limit)
14472               (and (numberp gnus-signature-limit)
14473                    (< (- (point-max) (point)) gnus-signature-limit))
14474               (and (gnus-functionp gnus-signature-limit)
14475                    (funcall gnus-signature-limit))
14476               (and (stringp gnus-signature-limit)
14477                    (not (re-search-forward gnus-signature-limit nil t))))
14478       (narrow-to-region (point) (point-max))
14479       t)))
14480
14481 (defun gnus-hidden-arg ()
14482   "Return the current prefix arg as a number, or 0 if no prefix."
14483   (list (if current-prefix-arg
14484             (prefix-numeric-value current-prefix-arg)
14485           0)))
14486
14487 (defun gnus-article-check-hidden-text (type arg)
14488   "Return nil if hiding is necessary.
14489 Arg can be nil or a number.  Nil and positive means hide, negative
14490 means show, 0 means toggle."
14491   (save-excursion
14492     (set-buffer gnus-article-buffer)
14493     (let ((hide (gnus-article-hidden-text-p type)))
14494       (cond
14495        ((or (null arg)
14496             (> arg 0))
14497         nil)
14498        ((< arg 0)
14499         (gnus-article-show-hidden-text type))
14500        (t
14501         (if (eq hide 'hidden)
14502             (gnus-article-show-hidden-text type)
14503           nil))))))
14504
14505 (defun gnus-article-hidden-text-p (type)
14506   "Say whether the current buffer contains hidden text of type TYPE."
14507   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14508     (when pos
14509       (if (get-text-property pos 'invisible)
14510           'hidden
14511         'shown))))
14512
14513 (defun gnus-article-hide (&optional arg force)
14514   "Hide all the gruft in the current article.
14515 This means that PGP stuff, signatures, cited text and (some)
14516 headers will be hidden.
14517 If given a prefix, show the hidden text instead."
14518   (interactive (list current-prefix-arg 'force))
14519   (gnus-article-hide-headers arg)
14520   (gnus-article-hide-pgp arg)
14521   (gnus-article-hide-citation-maybe arg force)
14522   (gnus-article-hide-signature arg))
14523
14524 (defun gnus-article-show-hidden-text (type &optional hide)
14525   "Show all hidden text of type TYPE.
14526 If HIDE, hide the text instead."
14527   (save-excursion
14528     (set-buffer gnus-article-buffer)
14529     (let ((buffer-read-only nil)
14530           (inhibit-point-motion-hooks t)
14531           (beg (point-min)))
14532       (while (gnus-goto-char (text-property-any
14533                               beg (point-max) 'gnus-type type))
14534         (setq beg (point))
14535         (forward-char)
14536         (if hide
14537             (gnus-hide-text beg (point) gnus-hidden-properties)
14538           (gnus-unhide-text beg (point)))
14539         (setq beg (point)))
14540       t)))
14541
14542 (defvar gnus-article-time-units
14543   `((year . ,(* 365.25 24 60 60))
14544     (week . ,(* 7 24 60 60))
14545     (day . ,(* 24 60 60))
14546     (hour . ,(* 60 60))
14547     (minute . 60)
14548     (second . 1))
14549   "Mapping from time units to seconds.")
14550
14551 (defun gnus-article-date-ut (&optional type highlight)
14552   "Convert DATE date to universal time in the current article.
14553 If TYPE is `local', convert to local time; if it is `lapsed', output
14554 how much time has lapsed since DATE."
14555   (interactive (list 'ut t))
14556   (let* ((header (or gnus-current-headers
14557                      (gnus-summary-article-header) ""))
14558          (date (and (vectorp header) (mail-header-date header)))
14559          (date-regexp "^Date: \\|^X-Sent: ")
14560          (now (current-time))
14561          (inhibit-point-motion-hooks t)
14562          bface eface)
14563     (when (and date (not (string= date "")))
14564       (save-excursion
14565         (set-buffer gnus-article-buffer)
14566         (save-restriction
14567           (nnheader-narrow-to-headers)
14568           (let ((buffer-read-only nil))
14569             ;; Delete any old Date headers.
14570             (if (re-search-forward date-regexp nil t)
14571                 (progn
14572                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14573                         eface (get-text-property (1- (gnus-point-at-eol))
14574                                                  'face))
14575                   (message-remove-header date-regexp t)
14576                   (beginning-of-line))
14577               (goto-char (point-max)))
14578             (insert (gnus-make-date-line date type))
14579             ;; Do highlighting.
14580             (forward-line -1)
14581             (when (and (gnus-visual-p 'article-highlight 'highlight)
14582                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14583               (gnus-put-text-property (match-beginning 1) (match-end 1)
14584                                  'face bface)
14585               (gnus-put-text-property (match-beginning 2) (match-end 2)
14586                                  'face eface))))))))
14587
14588 (defun gnus-make-date-line (date type)
14589   "Return a DATE line of TYPE."
14590   (cond
14591    ;; Convert to the local timezone.  We have to slap a
14592    ;; `condition-case' round the calls to the timezone
14593    ;; functions since they aren't particularly resistant to
14594    ;; buggy dates.
14595    ((eq type 'local)
14596     (concat "Date: " (condition-case ()
14597                          (timezone-make-date-arpa-standard date)
14598                        (error date))
14599             "\n"))
14600    ;; Convert to Universal Time.
14601    ((eq type 'ut)
14602     (concat "Date: "
14603             (condition-case ()
14604                 (timezone-make-date-arpa-standard date nil "UT")
14605               (error date))
14606             "\n"))
14607    ;; Get the original date from the article.
14608    ((eq type 'original)
14609     (concat "Date: " date "\n"))
14610    ;; Do an X-Sent lapsed format.
14611    ((eq type 'lapsed)
14612     ;; If the date is seriously mangled, the timezone
14613     ;; functions are liable to bug out, so we condition-case
14614     ;; the entire thing.
14615     (let* ((now (current-time))
14616            (real-time
14617             (condition-case ()
14618                 (gnus-time-minus
14619                  (gnus-encode-date
14620                   (timezone-make-date-arpa-standard
14621                    (current-time-string now)
14622                    (current-time-zone now) "UT"))
14623                  (gnus-encode-date
14624                   (timezone-make-date-arpa-standard
14625                    date nil "UT")))
14626               (error '(0 0))))
14627            (real-sec (+ (* (float (car real-time)) 65536)
14628                         (cadr real-time)))
14629            (sec (abs real-sec))
14630            num prev)
14631       (cond
14632        ((equal real-time '(0 0))
14633         "X-Sent: Unknown\n")
14634        ((zerop sec)
14635         "X-Sent: Now\n")
14636        (t
14637         (concat
14638          "X-Sent: "
14639          ;; This is a bit convoluted, but basically we go
14640          ;; through the time units for years, weeks, etc,
14641          ;; and divide things to see whether that results
14642          ;; in positive answers.
14643          (mapconcat
14644           (lambda (unit)
14645             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14646                 ;; The (remaining) seconds are too few to
14647                 ;; be divided into this time unit.
14648                 ""
14649               ;; It's big enough, so we output it.
14650               (setq sec (- sec (* num (cdr unit))))
14651               (prog1
14652                   (concat (if prev ", " "") (int-to-string
14653                                              (floor num))
14654                           " " (symbol-name (car unit)) 
14655                           (if (> num 1) "s" ""))
14656                 (setq prev t))))
14657           gnus-article-time-units "")
14658          ;; If dates are odd, then it might appear like the
14659          ;; article was sent in the future.
14660          (if (> real-sec 0)
14661              " ago\n"
14662            " in the future\n"))))))
14663    (t
14664     (error "Unknown conversion type: %s" type))))
14665
14666 (defun gnus-article-date-local (&optional highlight)
14667   "Convert the current article date to the local timezone."
14668   (interactive (list t))
14669   (gnus-article-date-ut 'local highlight))
14670
14671 (defun gnus-article-date-original (&optional highlight)
14672   "Convert the current article date to what it was originally.
14673 This is only useful if you have used some other date conversion
14674 function and want to see what the date was before converting."
14675   (interactive (list t))
14676   (gnus-article-date-ut 'original highlight))
14677
14678 (defun gnus-article-date-lapsed (&optional highlight)
14679   "Convert the current article date to time lapsed since it was sent."
14680   (interactive (list t))
14681   (gnus-article-date-ut 'lapsed highlight))
14682
14683 (defun gnus-article-maybe-highlight ()
14684   "Do some article highlighting if `gnus-visual' is non-nil."
14685   (if (gnus-visual-p 'article-highlight 'highlight)
14686       (gnus-article-highlight-some)))
14687
14688 ;;; Article savers.
14689
14690 (defun gnus-output-to-rmail (file-name)
14691   "Append the current article to an Rmail file named FILE-NAME."
14692   (require 'rmail)
14693   ;; Most of these codes are borrowed from rmailout.el.
14694   (setq file-name (expand-file-name file-name))
14695   (setq rmail-default-rmail-file file-name)
14696   (let ((artbuf (current-buffer))
14697         (tmpbuf (get-buffer-create " *Gnus-output*")))
14698     (save-excursion
14699       (or (get-file-buffer file-name)
14700           (file-exists-p file-name)
14701           (if (gnus-yes-or-no-p
14702                (concat "\"" file-name "\" does not exist, create it? "))
14703               (let ((file-buffer (create-file-buffer file-name)))
14704                 (save-excursion
14705                   (set-buffer file-buffer)
14706                   (rmail-insert-rmail-file-header)
14707                   (let ((require-final-newline nil))
14708                     (write-region (point-min) (point-max) file-name t 1)))
14709                 (kill-buffer file-buffer))
14710             (error "Output file does not exist")))
14711       (set-buffer tmpbuf)
14712       (buffer-disable-undo (current-buffer))
14713       (erase-buffer)
14714       (insert-buffer-substring artbuf)
14715       (gnus-convert-article-to-rmail)
14716       ;; Decide whether to append to a file or to an Emacs buffer.
14717       (let ((outbuf (get-file-buffer file-name)))
14718         (if (not outbuf)
14719             (append-to-file (point-min) (point-max) file-name)
14720           ;; File has been visited, in buffer OUTBUF.
14721           (set-buffer outbuf)
14722           (let ((buffer-read-only nil)
14723                 (msg (and (boundp 'rmail-current-message)
14724                           (symbol-value 'rmail-current-message))))
14725             ;; If MSG is non-nil, buffer is in RMAIL mode.
14726             (if msg
14727                 (progn (widen)
14728                        (narrow-to-region (point-max) (point-max))))
14729             (insert-buffer-substring tmpbuf)
14730             (if msg
14731                 (progn
14732                   (goto-char (point-min))
14733                   (widen)
14734                   (search-backward "\^_")
14735                   (narrow-to-region (point) (point-max))
14736                   (goto-char (1+ (point-min)))
14737                   (rmail-count-new-messages t)
14738                   (rmail-show-message msg)))))))
14739     (kill-buffer tmpbuf)))
14740
14741 (defun gnus-output-to-file (file-name)
14742   "Append the current article to a file named FILE-NAME."
14743   (let ((artbuf (current-buffer)))
14744     (nnheader-temp-write nil
14745       (insert-buffer-substring artbuf)
14746       ;; Append newline at end of the buffer as separator, and then
14747       ;; save it to file.
14748       (goto-char (point-max))
14749       (insert "\n")
14750       (append-to-file (point-min) (point-max) file-name))))
14751
14752 (defun gnus-convert-article-to-rmail ()
14753   "Convert article in current buffer to Rmail message format."
14754   (let ((buffer-read-only nil))
14755     ;; Convert article directly into Babyl format.
14756     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14757     (goto-char (point-min))
14758     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14759     (while (search-forward "\n\^_" nil t) ;single char
14760       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14761     (goto-char (point-max))
14762     (insert "\^_")))
14763
14764 (defun gnus-narrow-to-page (&optional arg)
14765   "Narrow the article buffer to a page.
14766 If given a numerical ARG, move forward ARG pages."
14767   (interactive "P")
14768   (setq arg (if arg (prefix-numeric-value arg) 0))
14769   (save-excursion
14770     (set-buffer gnus-article-buffer)
14771     (goto-char (point-min))
14772     (widen)
14773     (when (gnus-visual-p 'page-marker)
14774       (let ((buffer-read-only nil))
14775         (gnus-remove-text-with-property 'gnus-prev)
14776         (gnus-remove-text-with-property 'gnus-next)))
14777     (when
14778         (cond ((< arg 0)
14779                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14780               ((> arg 0)
14781                (re-search-forward page-delimiter nil 'move arg)))
14782       (goto-char (match-end 0)))
14783     (narrow-to-region
14784      (point)
14785      (if (re-search-forward page-delimiter nil 'move)
14786          (match-beginning 0)
14787        (point)))
14788     (when (and (gnus-visual-p 'page-marker)
14789                (not (= (point-min) 1)))
14790       (save-excursion
14791         (goto-char (point-min))
14792         (gnus-insert-prev-page-button)))
14793     (when (and (gnus-visual-p 'page-marker)
14794                (not (= (1- (point-max)) (buffer-size))))
14795       (save-excursion
14796         (goto-char (point-max))
14797         (gnus-insert-next-page-button)))))
14798
14799 ;; Article mode commands
14800
14801 (defun gnus-article-goto-next-page ()
14802   "Show the next page of the article."
14803   (interactive)
14804   (when (gnus-article-next-page)
14805     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14806
14807 (defun gnus-article-goto-prev-page ()
14808   "Show the next page of the article."
14809   (interactive)
14810   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14811     (gnus-article-prev-page nil)))
14812
14813 (defun gnus-article-next-page (&optional lines)
14814   "Show the next page of the current article.
14815 If end of article, return non-nil.  Otherwise return nil.
14816 Argument LINES specifies lines to be scrolled up."
14817   (interactive "p")
14818   (move-to-window-line -1)
14819   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14820   (if (save-excursion
14821         (end-of-line)
14822         (and (pos-visible-in-window-p)  ;Not continuation line.
14823              (eobp)))
14824       ;; Nothing in this page.
14825       (if (or (not gnus-break-pages)
14826               (save-excursion
14827                 (save-restriction
14828                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14829           t                             ;Nothing more.
14830         (gnus-narrow-to-page 1)         ;Go to next page.
14831         nil)
14832     ;; More in this page.
14833     (condition-case ()
14834         (scroll-up lines)
14835       (end-of-buffer
14836        ;; Long lines may cause an end-of-buffer error.
14837        (goto-char (point-max))))
14838     (move-to-window-line 0)
14839     nil))
14840
14841 (defun gnus-article-prev-page (&optional lines)
14842   "Show previous page of current article.
14843 Argument LINES specifies lines to be scrolled down."
14844   (interactive "p")
14845   (move-to-window-line 0)
14846   (if (and gnus-break-pages
14847            (bobp)
14848            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14849       (progn
14850         (gnus-narrow-to-page -1)        ;Go to previous page.
14851         (goto-char (point-max))
14852         (recenter -1))
14853     (prog1
14854         (condition-case ()
14855             (scroll-down lines)
14856           (error nil))
14857       (move-to-window-line 0))))
14858
14859 (defun gnus-article-refer-article ()
14860   "Read article specified by message-id around point."
14861   (interactive)
14862   (let ((point (point)))
14863     (search-forward ">" nil t)          ;Move point to end of "<....>".
14864     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14865         (let ((message-id (match-string 1)))
14866           (goto-char point)
14867           (set-buffer gnus-summary-buffer)
14868           (gnus-summary-refer-article message-id))
14869       (goto-char (point))
14870       (error "No references around point"))))
14871
14872 (defun gnus-article-show-summary ()
14873   "Reconfigure windows to show summary buffer."
14874   (interactive)
14875   (gnus-configure-windows 'article)
14876   (gnus-summary-goto-subject gnus-current-article))
14877
14878 (defun gnus-article-describe-briefly ()
14879   "Describe article mode commands briefly."
14880   (interactive)
14881   (gnus-message 6
14882                 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page     \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
14883
14884 (defun gnus-article-summary-command ()
14885   "Execute the last keystroke in the summary buffer."
14886   (interactive)
14887   (let ((obuf (current-buffer))
14888         (owin (current-window-configuration))
14889         func)
14890     (switch-to-buffer gnus-summary-buffer 'norecord)
14891     (setq func (lookup-key (current-local-map) (this-command-keys)))
14892     (call-interactively func)
14893     (set-buffer obuf)
14894     (set-window-configuration owin)
14895     (set-window-point (get-buffer-window (current-buffer)) (point))))
14896
14897 (defun gnus-article-summary-command-nosave ()
14898   "Execute the last keystroke in the summary buffer."
14899   (interactive)
14900   (let (func)
14901     (pop-to-buffer gnus-summary-buffer 'norecord)
14902     (setq func (lookup-key (current-local-map) (this-command-keys)))
14903     (call-interactively func)))
14904
14905 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14906   "Read a summary buffer key sequence and execute it from the article buffer."
14907   (interactive "P")
14908   (let ((nosaves
14909          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14910            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14911            "=" "^" "\M-^" "|"))
14912         (nosave-but-article
14913          '("A\r"))
14914         keys)
14915     (save-excursion
14916       (set-buffer gnus-summary-buffer)
14917       (push (or key last-command-event) unread-command-events)
14918       (setq keys (read-key-sequence nil)))
14919     (message "")
14920
14921     (if (or (member keys nosaves)
14922             (member keys nosave-but-article))
14923         (let (func)
14924           (save-window-excursion
14925             (pop-to-buffer gnus-summary-buffer 'norecord)
14926             (setq func (lookup-key (current-local-map) keys)))
14927           (if (not func)
14928               (ding)
14929             (set-buffer gnus-summary-buffer)
14930             (call-interactively func))
14931           (when (member keys nosave-but-article)
14932             (pop-to-buffer gnus-article-buffer 'norecord)))
14933       (let ((obuf (current-buffer))
14934             (owin (current-window-configuration))
14935             (opoint (point))
14936             func in-buffer)
14937         (if not-restore-window
14938             (pop-to-buffer gnus-summary-buffer 'norecord)
14939           (switch-to-buffer gnus-summary-buffer 'norecord))
14940         (setq in-buffer (current-buffer))
14941         (if (setq func (lookup-key (current-local-map) keys))
14942             (call-interactively func)
14943           (ding))
14944         (when (eq in-buffer (current-buffer))
14945           (set-buffer obuf)
14946           (unless not-restore-window
14947             (set-window-configuration owin))
14948           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14949
14950 \f
14951 ;;;
14952 ;;; Kill file handling.
14953 ;;;
14954
14955 ;;;###autoload
14956 (defalias 'gnus-batch-kill 'gnus-batch-score)
14957 ;;;###autoload
14958 (defun gnus-batch-score ()
14959   "Run batched scoring.
14960 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14961 Newsgroups is a list of strings in Bnews format.  If you want to score
14962 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14963 score the alt hierarchy, you'd say \"!alt.all\"."
14964   (interactive)
14965   (let* ((yes-and-no
14966           (gnus-newsrc-parse-options
14967            (apply (function concat)
14968                   (mapcar (lambda (g) (concat g " "))
14969                           command-line-args-left))))
14970          (gnus-expert-user t)
14971          (nnmail-spool-file nil)
14972          (gnus-use-dribble-file nil)
14973          (yes (car yes-and-no))
14974          (no (cdr yes-and-no))
14975          group newsrc entry
14976          ;; Disable verbose message.
14977          gnus-novice-user gnus-large-newsgroup)
14978     ;; Eat all arguments.
14979     (setq command-line-args-left nil)
14980     ;; Start Gnus.
14981     (gnus)
14982     ;; Apply kills to specified newsgroups in command line arguments.
14983     (setq newsrc (cdr gnus-newsrc-alist))
14984     (while newsrc
14985       (setq group (caar newsrc))
14986       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14987       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14988                (and (car entry)
14989                     (or (eq (car entry) t)
14990                         (not (zerop (car entry)))))
14991                (if yes (string-match yes group) t)
14992                (or (null no) (not (string-match no group))))
14993           (progn
14994             (gnus-summary-read-group group nil t nil t)
14995             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14996                  (gnus-summary-exit))))
14997       (setq newsrc (cdr newsrc)))
14998     ;; Exit Emacs.
14999     (switch-to-buffer gnus-group-buffer)
15000     (gnus-group-save-newsrc)))
15001
15002 (defun gnus-apply-kill-file ()
15003   "Apply a kill file to the current newsgroup.
15004 Returns the number of articles marked as read."
15005   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
15006           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
15007       (gnus-apply-kill-file-internal)
15008     0))
15009
15010 (defun gnus-kill-save-kill-buffer ()
15011   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
15012     (when (get-file-buffer file)
15013       (save-excursion
15014         (set-buffer (get-file-buffer file))
15015         (and (buffer-modified-p) (save-buffer))
15016         (kill-buffer (current-buffer))))))
15017
15018 (defvar gnus-kill-file-name "KILL"
15019   "Suffix of the kill files.")
15020
15021 (defun gnus-newsgroup-kill-file (newsgroup)
15022   "Return the name of a kill file name for NEWSGROUP.
15023 If NEWSGROUP is nil, return the global kill file name instead."
15024   (cond 
15025    ;; The global KILL file is placed at top of the directory.
15026    ((or (null newsgroup)
15027         (string-equal newsgroup ""))
15028     (expand-file-name gnus-kill-file-name
15029                       gnus-kill-files-directory))
15030    ;; Append ".KILL" to newsgroup name.
15031    ((gnus-use-long-file-name 'not-kill)
15032     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
15033                               "." gnus-kill-file-name)
15034                       gnus-kill-files-directory))
15035    ;; Place "KILL" under the hierarchical directory.
15036    (t
15037     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
15038                               "/" gnus-kill-file-name)
15039                       gnus-kill-files-directory))))
15040
15041 \f
15042 ;;;
15043 ;;; Dribble file
15044 ;;;
15045
15046 (defvar gnus-dribble-ignore nil)
15047 (defvar gnus-dribble-eval-file nil)
15048
15049 (defun gnus-dribble-file-name ()
15050   "Return the dribble file for the current .newsrc."
15051   (concat
15052    (if gnus-dribble-directory
15053        (concat (file-name-as-directory gnus-dribble-directory)
15054                (file-name-nondirectory gnus-current-startup-file))
15055      gnus-current-startup-file)
15056    "-dribble"))
15057
15058 (defun gnus-dribble-enter (string)
15059   "Enter STRING into the dribble buffer."
15060   (if (and (not gnus-dribble-ignore)
15061            gnus-dribble-buffer
15062            (buffer-name gnus-dribble-buffer))
15063       (let ((obuf (current-buffer)))
15064         (set-buffer gnus-dribble-buffer)
15065         (insert string "\n")
15066         (set-window-point (get-buffer-window (current-buffer)) (point-max))
15067         (bury-buffer gnus-dribble-buffer)
15068         (set-buffer obuf))))
15069
15070 (defun gnus-dribble-read-file ()
15071   "Read the dribble file from disk."
15072   (let ((dribble-file (gnus-dribble-file-name)))
15073     (save-excursion
15074       (set-buffer (setq gnus-dribble-buffer
15075                         (get-buffer-create
15076                          (file-name-nondirectory dribble-file))))
15077       (gnus-add-current-to-buffer-list)
15078       (erase-buffer)
15079       (setq buffer-file-name dribble-file)
15080       (auto-save-mode t)
15081       (buffer-disable-undo (current-buffer))
15082       (bury-buffer (current-buffer))
15083       (set-buffer-modified-p nil)
15084       (let ((auto (make-auto-save-file-name))
15085             (gnus-dribble-ignore t)
15086             modes)
15087         (when (or (file-exists-p auto) (file-exists-p dribble-file))
15088           ;; Load whichever file is newest -- the auto save file
15089           ;; or the "real" file.
15090           (if (file-newer-than-file-p auto dribble-file)
15091               (insert-file-contents auto)
15092             (insert-file-contents dribble-file))
15093           (unless (zerop (buffer-size))
15094             (set-buffer-modified-p t))
15095           ;; Set the file modes to reflect the .newsrc file modes.
15096           (save-buffer)
15097           (when (and (file-exists-p gnus-current-startup-file)
15098                      (setq modes (file-modes gnus-current-startup-file)))
15099             (set-file-modes dribble-file modes))
15100           ;; Possibly eval the file later.
15101           (when (gnus-y-or-n-p
15102                  "Auto-save file exists.  Do you want to read it? ")
15103             (setq gnus-dribble-eval-file t)))))))
15104
15105 (defun gnus-dribble-eval-file ()
15106   (when gnus-dribble-eval-file
15107     (setq gnus-dribble-eval-file nil)
15108     (save-excursion
15109       (let ((gnus-dribble-ignore t))
15110         (set-buffer gnus-dribble-buffer)
15111         (eval-buffer (current-buffer))))))
15112
15113 (defun gnus-dribble-delete-file ()
15114   (when (file-exists-p (gnus-dribble-file-name))
15115     (delete-file (gnus-dribble-file-name)))
15116   (when gnus-dribble-buffer
15117     (save-excursion
15118       (set-buffer gnus-dribble-buffer)
15119       (let ((auto (make-auto-save-file-name)))
15120         (if (file-exists-p auto)
15121             (delete-file auto))
15122         (erase-buffer)
15123         (set-buffer-modified-p nil)))))
15124
15125 (defun gnus-dribble-save ()
15126   (when (and gnus-dribble-buffer
15127              (buffer-name gnus-dribble-buffer))
15128     (save-excursion
15129       (set-buffer gnus-dribble-buffer)
15130       (save-buffer))))
15131
15132 (defun gnus-dribble-clear ()
15133   (when (gnus-buffer-exists-p gnus-dribble-buffer)
15134     (save-excursion
15135       (set-buffer gnus-dribble-buffer)
15136       (erase-buffer)
15137       (set-buffer-modified-p nil)
15138       (setq buffer-saved-size (buffer-size)))))
15139
15140 \f
15141 ;;;
15142 ;;; Server Communication
15143 ;;;
15144
15145 (defun gnus-start-news-server (&optional confirm)
15146   "Open a method for getting news.
15147 If CONFIRM is non-nil, the user will be asked for an NNTP server."
15148   (let (how)
15149     (if gnus-current-select-method
15150         ;; Stream is already opened.
15151         nil
15152       ;; Open NNTP server.
15153       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
15154       (if confirm
15155           (progn
15156             ;; Read server name with completion.
15157             (setq gnus-nntp-server
15158                   (completing-read "NNTP server: "
15159                                    (mapcar (lambda (server) (list server))
15160                                            (cons (list gnus-nntp-server)
15161                                                  gnus-secondary-servers))
15162                                    nil nil gnus-nntp-server))))
15163
15164       (if (and gnus-nntp-server
15165                (stringp gnus-nntp-server)
15166                (not (string= gnus-nntp-server "")))
15167           (setq gnus-select-method
15168                 (cond ((or (string= gnus-nntp-server "")
15169                            (string= gnus-nntp-server "::"))
15170                        (list 'nnspool (system-name)))
15171                       ((string-match "^:" gnus-nntp-server)
15172                        (list 'nnmh gnus-nntp-server
15173                              (list 'nnmh-directory
15174                                    (file-name-as-directory
15175                                     (expand-file-name
15176                                      (concat "~/" (substring
15177                                                    gnus-nntp-server 1)))))
15178                              (list 'nnmh-get-new-mail nil)))
15179                       (t
15180                        (list 'nntp gnus-nntp-server)))))
15181
15182       (setq how (car gnus-select-method))
15183       (cond ((eq how 'nnspool)
15184              (require 'nnspool)
15185              (gnus-message 5 "Looking up local news spool..."))
15186             ((eq how 'nnmh)
15187              (require 'nnmh)
15188              (gnus-message 5 "Looking up mh spool..."))
15189             (t
15190              (require 'nntp)))
15191       (setq gnus-current-select-method gnus-select-method)
15192       (run-hooks 'gnus-open-server-hook)
15193       (or
15194        ;; gnus-open-server-hook might have opened it
15195        (gnus-server-opened gnus-select-method)
15196        (gnus-open-server gnus-select-method)
15197        (gnus-y-or-n-p
15198         (format
15199          "%s (%s) open error: '%s'.     Continue? "
15200          (car gnus-select-method) (cadr gnus-select-method)
15201          (gnus-status-message gnus-select-method)))
15202        (gnus-error 1 "Couldn't open server on %s"
15203                    (nth 1 gnus-select-method))))))
15204
15205 (defun gnus-check-group (group)
15206   "Try to make sure that the server where GROUP exists is alive."
15207   (let ((method (gnus-find-method-for-group group)))
15208     (or (gnus-server-opened method)
15209         (gnus-open-server method))))
15210
15211 (defun gnus-check-server (&optional method silent)
15212   "Check whether the connection to METHOD is down.
15213 If METHOD is nil, use `gnus-select-method'.
15214 If it is down, start it up (again)."
15215   (let ((method (or method gnus-select-method)))
15216     ;; Transform virtual server names into select methods.
15217     (when (stringp method)
15218       (setq method (gnus-server-to-method method)))
15219     (if (gnus-server-opened method)
15220         ;; The stream is already opened.
15221         t
15222       ;; Open the server.
15223       (unless silent
15224         (gnus-message 5 "Opening %s server%s..." (car method)
15225                       (if (equal (nth 1 method) "") ""
15226                         (format " on %s" (nth 1 method)))))
15227       (run-hooks 'gnus-open-server-hook)
15228       (prog1
15229           (gnus-open-server method)
15230         (unless silent
15231           (message ""))))))
15232
15233 (defun gnus-get-function (method function &optional noerror)
15234   "Return a function symbol based on METHOD and FUNCTION."
15235   ;; Translate server names into methods.
15236   (unless method
15237     (error "Attempted use of a nil select method"))
15238   (when (stringp method)
15239     (setq method (gnus-server-to-method method)))
15240   (let ((func (intern (format "%s-%s" (car method) function))))
15241     ;; If the functions isn't bound, we require the backend in
15242     ;; question.
15243     (unless (fboundp func)
15244       (require (car method))
15245       (when (and (not (fboundp func))
15246                  (not noerror))
15247         ;; This backend doesn't implement this function.
15248         (error "No such function: %s" func)))
15249     func))
15250
15251 \f
15252 ;;;
15253 ;;; Interface functions to the backends.
15254 ;;;
15255
15256 (defun gnus-open-server (method)
15257   "Open a connection to METHOD."
15258   (when (stringp method)
15259     (setq method (gnus-server-to-method method)))
15260   (let ((elem (assoc method gnus-opened-servers)))
15261     ;; If this method was previously denied, we just return nil.
15262     (if (eq (nth 1 elem) 'denied)
15263         (progn
15264           (gnus-message 1 "Denied server")
15265           nil)
15266       ;; Open the server.
15267       (let ((result
15268              (funcall (gnus-get-function method 'open-server)
15269                       (nth 1 method) (nthcdr 2 method))))
15270         ;; If this hasn't been opened before, we add it to the list.
15271         (unless elem
15272           (setq elem (list method nil)
15273                 gnus-opened-servers (cons elem gnus-opened-servers)))
15274         ;; Set the status of this server.
15275         (setcar (cdr elem) (if result 'ok 'denied))
15276         ;; Return the result from the "open" call.
15277         result))))
15278
15279 (defun gnus-close-server (method)
15280   "Close the connection to METHOD."
15281   (when (stringp method)
15282     (setq method (gnus-server-to-method method)))
15283   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15284
15285 (defun gnus-request-list (method)
15286   "Request the active file from METHOD."
15287   (when (stringp method)
15288     (setq method (gnus-server-to-method method)))
15289   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15290
15291 (defun gnus-request-list-newsgroups (method)
15292   "Request the newsgroups file from METHOD."
15293   (when (stringp method)
15294     (setq method (gnus-server-to-method method)))
15295   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15296
15297 (defun gnus-request-newgroups (date method)
15298   "Request all new groups since DATE from METHOD."
15299   (when (stringp method)
15300     (setq method (gnus-server-to-method method)))
15301   (funcall (gnus-get-function method 'request-newgroups)
15302            date (nth 1 method)))
15303
15304 (defun gnus-server-opened (method)
15305   "Check whether a connection to METHOD has been opened."
15306   (when (stringp method)
15307     (setq method (gnus-server-to-method method)))
15308   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15309
15310 (defun gnus-status-message (method)
15311   "Return the status message from METHOD.
15312 If METHOD is a string, it is interpreted as a group name.   The method
15313 this group uses will be queried."
15314   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15315                   method)))
15316     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15317
15318 (defun gnus-request-group (group &optional dont-check method)
15319   "Request GROUP.  If DONT-CHECK, no information is required."
15320   (let ((method (or method (gnus-find-method-for-group group))))
15321     (when (stringp method)
15322       (setq method (gnus-server-to-method method)))
15323     (funcall (gnus-get-function method 'request-group)
15324              (gnus-group-real-name group) (nth 1 method) dont-check)))
15325
15326 (defun gnus-request-asynchronous (group &optional articles)
15327   "Request that GROUP behave asynchronously.
15328 ARTICLES is the `data' of the group."
15329   (let ((method (gnus-find-method-for-group group)))
15330     (funcall (gnus-get-function method 'request-asynchronous)
15331              (gnus-group-real-name group) (nth 1 method) articles)))
15332
15333 (defun gnus-list-active-group (group)
15334   "Request active information on GROUP."
15335   (let ((method (gnus-find-method-for-group group))
15336         (func 'list-active-group))
15337     (when (gnus-check-backend-function func group)
15338       (funcall (gnus-get-function method func)
15339                (gnus-group-real-name group) (nth 1 method)))))
15340
15341 (defun gnus-request-group-description (group)
15342   "Request a description of GROUP."
15343   (let ((method (gnus-find-method-for-group group))
15344         (func 'request-group-description))
15345     (when (gnus-check-backend-function func group)
15346       (funcall (gnus-get-function method func)
15347                (gnus-group-real-name group) (nth 1 method)))))
15348
15349 (defun gnus-close-group (group)
15350   "Request the GROUP be closed."
15351   (let ((method (gnus-find-method-for-group group)))
15352     (funcall (gnus-get-function method 'close-group)
15353              (gnus-group-real-name group) (nth 1 method))))
15354
15355 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15356   "Request headers for ARTICLES in GROUP.
15357 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15358   (let ((method (gnus-find-method-for-group group)))
15359     (if (and gnus-use-cache (numberp (car articles)))
15360         (gnus-cache-retrieve-headers articles group fetch-old)
15361       (funcall (gnus-get-function method 'retrieve-headers)
15362                articles (gnus-group-real-name group) (nth 1 method)
15363                fetch-old))))
15364
15365 (defun gnus-retrieve-groups (groups method)
15366   "Request active information on GROUPS from METHOD."
15367   (when (stringp method)
15368     (setq method (gnus-server-to-method method)))
15369   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15370
15371 (defun gnus-request-type (group &optional article)
15372   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15373   (let ((method (gnus-find-method-for-group group)))
15374     (if (not (gnus-check-backend-function 'request-type (car method)))
15375         'unknown
15376       (funcall (gnus-get-function method 'request-type)
15377                (gnus-group-real-name group) article))))
15378
15379 (defun gnus-request-update-mark (group article mark)
15380   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15381   (let ((method (gnus-find-method-for-group group)))
15382     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15383         mark
15384       (funcall (gnus-get-function method 'request-update-mark)
15385                (gnus-group-real-name group) article mark))))
15386
15387 (defun gnus-request-article (article group &optional buffer)
15388   "Request the ARTICLE in GROUP.
15389 ARTICLE can either be an article number or an article Message-ID.
15390 If BUFFER, insert the article in that group."
15391   (let ((method (gnus-find-method-for-group group)))
15392     (funcall (gnus-get-function method 'request-article)
15393              article (gnus-group-real-name group) (nth 1 method) buffer)))
15394
15395 (defun gnus-request-head (article group)
15396   "Request the head of ARTICLE in GROUP."
15397   (let* ((method (gnus-find-method-for-group group))
15398          (head (gnus-get-function method 'request-head t)))
15399     (if (fboundp head)
15400         (funcall head article (gnus-group-real-name group) (nth 1 method))
15401       (let ((res (gnus-request-article article group)))
15402         (when res
15403           (save-excursion
15404             (set-buffer nntp-server-buffer)
15405             (goto-char (point-min))
15406             (when (search-forward "\n\n" nil t)
15407               (delete-region (1- (point)) (point-max)))
15408             (nnheader-fold-continuation-lines)))
15409         res))))
15410
15411 (defun gnus-request-body (article group)
15412   "Request the body of ARTICLE in GROUP."
15413   (let ((method (gnus-find-method-for-group group)))
15414     (funcall (gnus-get-function method 'request-body)
15415              article (gnus-group-real-name group) (nth 1 method))))
15416
15417 (defun gnus-request-post (method)
15418   "Post the current buffer using METHOD."
15419   (when (stringp method)
15420     (setq method (gnus-server-to-method method)))
15421   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15422
15423 (defun gnus-request-scan (group method)
15424   "Request a SCAN being performed in GROUP from METHOD.
15425 If GROUP is nil, all groups on METHOD are scanned."
15426   (let ((method (if group (gnus-find-method-for-group group) method)))
15427     (funcall (gnus-get-function method 'request-scan)
15428              (and group (gnus-group-real-name group)) (nth 1 method))))
15429
15430 (defsubst gnus-request-update-info (info method)
15431   "Request that METHOD update INFO."
15432   (when (stringp method)
15433     (setq method (gnus-server-to-method method)))
15434   (when (gnus-check-backend-function 'request-update-info (car method))
15435     (funcall (gnus-get-function method 'request-update-info)
15436              (gnus-group-real-name (gnus-info-group info))
15437              info (nth 1 method))))
15438
15439 (defun gnus-request-expire-articles (articles group &optional force)
15440   (let ((method (gnus-find-method-for-group group)))
15441     (funcall (gnus-get-function method 'request-expire-articles)
15442              articles (gnus-group-real-name group) (nth 1 method)
15443              force)))
15444
15445 (defun gnus-request-move-article
15446   (article group server accept-function &optional last)
15447   (let ((method (gnus-find-method-for-group group)))
15448     (funcall (gnus-get-function method 'request-move-article)
15449              article (gnus-group-real-name group)
15450              (nth 1 method) accept-function last)))
15451
15452 (defun gnus-request-accept-article (group method &optional last)
15453   ;; Make sure there's a newline at the end of the article.
15454   (when (stringp method)
15455     (setq method (gnus-server-to-method method)))
15456   (when (and (not method)
15457              (stringp group))
15458     (setq method (gnus-group-name-to-method group)))
15459   (goto-char (point-max))
15460   (unless (bolp)
15461     (insert "\n"))
15462   (let ((func (car (or method (gnus-find-method-for-group group)))))
15463     (funcall (intern (format "%s-request-accept-article" func))
15464              (if (stringp group) (gnus-group-real-name group) group)
15465              (cadr method)
15466              last)))
15467
15468 (defun gnus-request-replace-article (article group buffer)
15469   (let ((func (car (gnus-find-method-for-group group))))
15470     (funcall (intern (format "%s-request-replace-article" func))
15471              article (gnus-group-real-name group) buffer)))
15472
15473 (defun gnus-request-associate-buffer (group)
15474   (let ((method (gnus-find-method-for-group group)))
15475     (funcall (gnus-get-function method 'request-associate-buffer)
15476              (gnus-group-real-name group))))
15477
15478 (defun gnus-request-restore-buffer (article group)
15479   "Request a new buffer restored to the state of ARTICLE."
15480   (let ((method (gnus-find-method-for-group group)))
15481     (funcall (gnus-get-function method 'request-restore-buffer)
15482              article (gnus-group-real-name group) (nth 1 method))))
15483
15484 (defun gnus-request-create-group (group &optional method)
15485   (when (stringp method)
15486     (setq method (gnus-server-to-method method)))
15487   (let ((method (or method (gnus-find-method-for-group group))))
15488     (funcall (gnus-get-function method 'request-create-group)
15489              (gnus-group-real-name group) (nth 1 method))))
15490
15491 (defun gnus-request-delete-group (group &optional force)
15492   (let ((method (gnus-find-method-for-group group)))
15493     (funcall (gnus-get-function method 'request-delete-group)
15494              (gnus-group-real-name group) force (nth 1 method))))
15495
15496 (defun gnus-request-rename-group (group new-name)
15497   (let ((method (gnus-find-method-for-group group)))
15498     (funcall (gnus-get-function method 'request-rename-group)
15499              (gnus-group-real-name group)
15500              (gnus-group-real-name new-name) (nth 1 method))))
15501
15502 (defun gnus-member-of-valid (symbol group)
15503   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15504   (memq symbol (assoc
15505                 (symbol-name (car (gnus-find-method-for-group group)))
15506                 gnus-valid-select-methods)))
15507
15508 (defun gnus-method-option-p (method option)
15509   "Return non-nil if select METHOD has OPTION as a parameter."
15510   (when (stringp method)
15511     (setq method (gnus-server-to-method method)))
15512   (memq option (assoc (format "%s" (car method))
15513                       gnus-valid-select-methods)))
15514
15515 (defun gnus-server-extend-method (group method)
15516   ;; This function "extends" a virtual server.  If the server is
15517   ;; "hello", and the select method is ("hello" (my-var "something"))
15518   ;; in the group "alt.alt", this will result in a new virtual server
15519   ;; called "hello+alt.alt".
15520   (let ((entry
15521          (gnus-copy-sequence
15522           (if (equal (car method) "native") gnus-select-method
15523             (cdr (assoc (car method) gnus-server-alist))))))
15524     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15525     (nconc entry (cdr method))))
15526
15527 (defun gnus-server-status (method)
15528   "Return the status of METHOD."
15529   (nth 1 (assoc method gnus-opened-servers)))
15530
15531 (defun gnus-group-name-to-method (group)
15532   "Return a select method suitable for GROUP."
15533   (if (string-match ":" group)
15534       (let ((server (substring group 0 (match-beginning 0))))
15535         (if (string-match "\\+" server)
15536             (list (intern (substring server 0 (match-beginning 0)))
15537                   (substring server (match-end 0)))
15538           (list (intern server) "")))
15539     gnus-select-method))
15540
15541 (defun gnus-find-method-for-group (group &optional info)
15542   "Find the select method that GROUP uses."
15543   (or gnus-override-method
15544       (and (not group)
15545            gnus-select-method)
15546       (let ((info (or info (gnus-get-info group)))
15547             method)
15548         (if (or (not info)
15549                 (not (setq method (gnus-info-method info)))
15550                 (equal method "native"))
15551             gnus-select-method
15552           (setq method
15553                 (cond ((stringp method)
15554                        (gnus-server-to-method method))
15555                       ((stringp (car method))
15556                        (gnus-server-extend-method group method))
15557                       (t
15558                        method)))
15559           (cond ((equal (cadr method) "")
15560                  method)
15561                 ((null (cadr method))
15562                  (list (car method) ""))
15563                 (t
15564                  (gnus-server-add-address method)))))))
15565
15566 (defun gnus-check-backend-function (func group)
15567   "Check whether GROUP supports function FUNC."
15568   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15569                   group)))
15570     (fboundp (intern (format "%s-%s" method func)))))
15571
15572 (defun gnus-methods-using (feature)
15573   "Find all methods that have FEATURE."
15574   (let ((valids gnus-valid-select-methods)
15575         outs)
15576     (while valids
15577       (if (memq feature (car valids))
15578           (setq outs (cons (car valids) outs)))
15579       (setq valids (cdr valids)))
15580     outs))
15581
15582 \f
15583 ;;;
15584 ;;; Active & Newsrc File Handling
15585 ;;;
15586
15587 (defun gnus-setup-news (&optional rawfile level dont-connect)
15588   "Setup news information.
15589 If RAWFILE is non-nil, the .newsrc file will also be read.
15590 If LEVEL is non-nil, the news will be set up at level LEVEL."
15591   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15592
15593     (when init 
15594       ;; Clear some variables to re-initialize news information.
15595       (setq gnus-newsrc-alist nil
15596             gnus-active-hashtb nil)
15597       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15598       (gnus-read-newsrc-file rawfile))
15599
15600     (when (and (not (assoc "archive" gnus-server-alist))
15601                (gnus-archive-server-wanted-p))
15602       (push (cons "archive" gnus-message-archive-method)
15603             gnus-server-alist))
15604
15605     ;; If we don't read the complete active file, we fill in the
15606     ;; hashtb here.
15607     (if (or (null gnus-read-active-file)
15608             (eq gnus-read-active-file 'some))
15609         (gnus-update-active-hashtb-from-killed))
15610
15611     ;; Read the active file and create `gnus-active-hashtb'.
15612     ;; If `gnus-read-active-file' is nil, then we just create an empty
15613     ;; hash table.  The partial filling out of the hash table will be
15614     ;; done in `gnus-get-unread-articles'.
15615     (and gnus-read-active-file
15616          (not level)
15617          (gnus-read-active-file))
15618
15619     (or gnus-active-hashtb
15620         (setq gnus-active-hashtb (make-vector 4095 0)))
15621
15622     ;; Initialize the cache.
15623     (when gnus-use-cache
15624       (gnus-cache-open))
15625
15626     ;; Possibly eval the dribble file.
15627     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15628
15629     ;; Slave Gnusii should then clear the dribble buffer.
15630     (when (and init gnus-slave)
15631       (gnus-dribble-clear))
15632
15633     (gnus-update-format-specifications)
15634
15635     ;; See whether we need to read the description file.
15636     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15637              (not gnus-description-hashtb)
15638              (not dont-connect)
15639              gnus-read-active-file)
15640         (gnus-read-all-descriptions-files))
15641
15642     ;; Find new newsgroups and treat them.
15643     (if (and init gnus-check-new-newsgroups (not level)
15644              (gnus-check-server gnus-select-method))
15645         (gnus-find-new-newsgroups))
15646
15647     ;; We might read in new NoCeM messages here.
15648     (when (and gnus-use-nocem 
15649                (not level)
15650                (not dont-connect))
15651       (gnus-nocem-scan-groups))
15652
15653     ;; Find the number of unread articles in each non-dead group.
15654     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15655       (gnus-get-unread-articles level))
15656
15657     (if (and init gnus-check-bogus-newsgroups
15658              gnus-read-active-file (not level)
15659              (gnus-server-opened gnus-select-method))
15660         (gnus-check-bogus-newsgroups))))
15661
15662 (defun gnus-find-new-newsgroups (&optional arg)
15663   "Search for new newsgroups and add them.
15664 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15665 The `-n' option line from .newsrc is respected.
15666 If ARG (the prefix), use the `ask-server' method to query
15667 the server for new groups."
15668   (interactive "P")
15669   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15670                        (null gnus-read-active-file)
15671                        (eq gnus-read-active-file 'some))
15672                    'ask-server gnus-check-new-newsgroups)))
15673     (unless (gnus-check-first-time-used)
15674       (if (or (consp check)
15675               (eq check 'ask-server))
15676           ;; Ask the server for new groups.
15677           (gnus-ask-server-for-new-groups)
15678         ;; Go through the active hashtb and look for new groups.
15679         (let ((groups 0)
15680               group new-newsgroups)
15681           (gnus-message 5 "Looking for new newsgroups...")
15682           (unless gnus-have-read-active-file
15683             (gnus-read-active-file))
15684           (setq gnus-newsrc-last-checked-date (current-time-string))
15685           (unless gnus-killed-hashtb
15686             (gnus-make-hashtable-from-killed))
15687           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15688           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15689           (mapatoms
15690            (lambda (sym)
15691              (if (or (null (setq group (symbol-name sym)))
15692                      (not (boundp sym))
15693                      (null (symbol-value sym))
15694                      (gnus-gethash group gnus-killed-hashtb)
15695                      (gnus-gethash group gnus-newsrc-hashtb))
15696                  ()
15697                (let ((do-sub (gnus-matches-options-n group)))
15698                  (cond
15699                   ((eq do-sub 'subscribe)
15700                    (setq groups (1+ groups))
15701                    (gnus-sethash group group gnus-killed-hashtb)
15702                    (funcall gnus-subscribe-options-newsgroup-method group))
15703                   ((eq do-sub 'ignore)
15704                    nil)
15705                   (t
15706                    (setq groups (1+ groups))
15707                    (gnus-sethash group group gnus-killed-hashtb)
15708                    (if gnus-subscribe-hierarchical-interactive
15709                        (setq new-newsgroups (cons group new-newsgroups))
15710                      (funcall gnus-subscribe-newsgroup-method group)))))))
15711            gnus-active-hashtb)
15712           (when new-newsgroups
15713             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15714           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15715           (if (> groups 0)
15716               (gnus-message 6 "%d new newsgroup%s arrived."
15717                             groups (if (> groups 1) "s have" " has"))
15718             (gnus-message 6 "No new newsgroups.")))))))
15719
15720 (defun gnus-matches-options-n (group)
15721   ;; Returns `subscribe' if the group is to be unconditionally
15722   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15723   ;; no match for the group.
15724
15725   ;; First we check the two user variables.
15726   (cond
15727    ((and gnus-options-subscribe
15728          (string-match gnus-options-subscribe group))
15729     'subscribe)
15730    ((and gnus-auto-subscribed-groups
15731          (string-match gnus-auto-subscribed-groups group))
15732     'subscribe)
15733    ((and gnus-options-not-subscribe
15734          (string-match gnus-options-not-subscribe group))
15735     'ignore)
15736    ;; Then we go through the list that was retrieved from the .newsrc
15737    ;; file.  This list has elements on the form
15738    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15739    ;; is in the reverse order of the options line) is returned.
15740    (t
15741     (let ((regs gnus-newsrc-options-n))
15742       (while (and regs
15743                   (not (string-match (caar regs) group)))
15744         (setq regs (cdr regs)))
15745       (and regs (cdar regs))))))
15746
15747 (defun gnus-ask-server-for-new-groups ()
15748   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15749          (methods (cons gnus-select-method
15750                         (nconc
15751                          (when (gnus-archive-server-wanted-p)
15752                            (list "archive"))
15753                          (append
15754                           (and (consp gnus-check-new-newsgroups)
15755                                gnus-check-new-newsgroups)
15756                           gnus-secondary-select-methods))))
15757          (groups 0)
15758          (new-date (current-time-string))
15759          group new-newsgroups got-new method hashtb
15760          gnus-override-subscribe-method)
15761     ;; Go through both primary and secondary select methods and
15762     ;; request new newsgroups.
15763     (while (setq method (gnus-server-get-method nil (pop methods)))
15764       (setq new-newsgroups nil)
15765       (setq gnus-override-subscribe-method method)
15766       (when (and (gnus-check-server method)
15767                  (gnus-request-newgroups date method))
15768         (save-excursion
15769           (setq got-new t)
15770           (setq hashtb (gnus-make-hashtable 100))
15771           (set-buffer nntp-server-buffer)
15772           ;; Enter all the new groups into a hashtable.
15773           (gnus-active-to-gnus-format method hashtb 'ignore))
15774         ;; Now all new groups from `method' are in `hashtb'.
15775         (mapatoms
15776          (lambda (group-sym)
15777            (if (or (null (setq group (symbol-name group-sym)))
15778                    (not (boundp group-sym))
15779                    (null (symbol-value group-sym))
15780                    (gnus-gethash group gnus-newsrc-hashtb)
15781                    (member group gnus-zombie-list)
15782                    (member group gnus-killed-list))
15783                ;; The group is already known.
15784                ()
15785              ;; Make this group active.
15786              (when (symbol-value group-sym)
15787                (gnus-set-active group (symbol-value group-sym)))
15788              ;; Check whether we want it or not.
15789              (let ((do-sub (gnus-matches-options-n group)))
15790                (cond
15791                 ((eq do-sub 'subscribe)
15792                  (incf groups)
15793                  (gnus-sethash group group gnus-killed-hashtb)
15794                  (funcall gnus-subscribe-options-newsgroup-method group))
15795                 ((eq do-sub 'ignore)
15796                  nil)
15797                 (t
15798                  (incf groups)
15799                  (gnus-sethash group group gnus-killed-hashtb)
15800                  (if gnus-subscribe-hierarchical-interactive
15801                      (push group new-newsgroups)
15802                    (funcall gnus-subscribe-newsgroup-method group)))))))
15803          hashtb))
15804       (when new-newsgroups
15805         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15806     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15807     (when (> groups 0)
15808       (gnus-message 6 "%d new newsgroup%s arrived."
15809                     groups (if (> groups 1) "s have" " has")))
15810     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15811     got-new))
15812
15813 (defun gnus-check-first-time-used ()
15814   (if (or (> (length gnus-newsrc-alist) 1)
15815           (file-exists-p gnus-startup-file)
15816           (file-exists-p (concat gnus-startup-file ".el"))
15817           (file-exists-p (concat gnus-startup-file ".eld")))
15818       nil
15819     (gnus-message 6 "First time user; subscribing you to default groups")
15820     (unless (gnus-read-active-file-p)
15821       (gnus-read-active-file))
15822     (setq gnus-newsrc-last-checked-date (current-time-string))
15823     (let ((groups gnus-default-subscribed-newsgroups)
15824           group)
15825       (if (eq groups t)
15826           nil
15827         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15828         (mapatoms
15829          (lambda (sym)
15830            (if (null (setq group (symbol-name sym)))
15831                ()
15832              (let ((do-sub (gnus-matches-options-n group)))
15833                (cond
15834                 ((eq do-sub 'subscribe)
15835                  (gnus-sethash group group gnus-killed-hashtb)
15836                  (funcall gnus-subscribe-options-newsgroup-method group))
15837                 ((eq do-sub 'ignore)
15838                  nil)
15839                 (t
15840                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15841          gnus-active-hashtb)
15842         (while groups
15843           (if (gnus-active (car groups))
15844               (gnus-group-change-level
15845                (car groups) gnus-level-default-subscribed gnus-level-killed))
15846           (setq groups (cdr groups)))
15847         (gnus-group-make-help-group)
15848         (and gnus-novice-user
15849              (gnus-message 7 "`A k' to list killed groups"))))))
15850
15851 (defun gnus-subscribe-group (group previous &optional method)
15852   (gnus-group-change-level
15853    (if method
15854        (list t group gnus-level-default-subscribed nil nil method)
15855      group)
15856    gnus-level-default-subscribed gnus-level-killed previous t))
15857
15858 ;; `gnus-group-change-level' is the fundamental function for changing
15859 ;; subscription levels of newsgroups.  This might mean just changing
15860 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15861 ;; again, which subscribes/unsubscribes a group, which is equally
15862 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15863 ;; from 8-9 to 1-7 means that you remove the group from the list of
15864 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15865 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15866 ;; which is trivial.
15867 ;; ENTRY can either be a string (newsgroup name) or a list (if
15868 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15869 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15870 ;; entries.
15871 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15872 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15873 ;; after.
15874 (defun gnus-group-change-level (entry level &optional oldlevel
15875                                       previous fromkilled)
15876   (let (group info active num)
15877     ;; Glean what info we can from the arguments
15878     (if (consp entry)
15879         (if fromkilled (setq group (nth 1 entry))
15880           (setq group (car (nth 2 entry))))
15881       (setq group entry))
15882     (if (and (stringp entry)
15883              oldlevel
15884              (< oldlevel gnus-level-zombie))
15885         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15886     (if (and (not oldlevel)
15887              (consp entry))
15888         (setq oldlevel (gnus-info-level (nth 2 entry)))
15889       (setq oldlevel (or oldlevel 9)))
15890     (if (stringp previous)
15891         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15892
15893     (if (and (>= oldlevel gnus-level-zombie)
15894              (gnus-gethash group gnus-newsrc-hashtb))
15895         ;; We are trying to subscribe a group that is already
15896         ;; subscribed.
15897         ()                              ; Do nothing.
15898
15899       (or (gnus-ephemeral-group-p group)
15900           (gnus-dribble-enter
15901            (format "(gnus-group-change-level %S %S %S %S %S)"
15902                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15903
15904       ;; Then we remove the newgroup from any old structures, if needed.
15905       ;; If the group was killed, we remove it from the killed or zombie
15906       ;; list.  If not, and it is in fact going to be killed, we remove
15907       ;; it from the newsrc hash table and assoc.
15908       (cond
15909        ((>= oldlevel gnus-level-zombie)
15910         (if (= oldlevel gnus-level-zombie)
15911             (setq gnus-zombie-list (delete group gnus-zombie-list))
15912           (setq gnus-killed-list (delete group gnus-killed-list))))
15913        (t
15914         (if (and (>= level gnus-level-zombie)
15915                  entry)
15916             (progn
15917               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15918               (if (nth 3 entry)
15919                   (setcdr (gnus-gethash (car (nth 3 entry))
15920                                         gnus-newsrc-hashtb)
15921                           (cdr entry)))
15922               (setcdr (cdr entry) (cdddr entry))))))
15923
15924       ;; Finally we enter (if needed) the list where it is supposed to
15925       ;; go, and change the subscription level.  If it is to be killed,
15926       ;; we enter it into the killed or zombie list.
15927       (cond 
15928        ((>= level gnus-level-zombie)
15929         ;; Remove from the hash table.
15930         (gnus-sethash group nil gnus-newsrc-hashtb)
15931         ;; We do not enter foreign groups into the list of dead
15932         ;; groups.
15933         (unless (gnus-group-foreign-p group)
15934           (if (= level gnus-level-zombie)
15935               (setq gnus-zombie-list (cons group gnus-zombie-list))
15936             (setq gnus-killed-list (cons group gnus-killed-list)))))
15937        (t
15938         ;; If the list is to be entered into the newsrc assoc, and
15939         ;; it was killed, we have to create an entry in the newsrc
15940         ;; hashtb format and fix the pointers in the newsrc assoc.
15941         (if (< oldlevel gnus-level-zombie)
15942             ;; It was alive, and it is going to stay alive, so we
15943             ;; just change the level and don't change any pointers or
15944             ;; hash table entries.
15945             (setcar (cdaddr entry) level)
15946           (if (listp entry)
15947               (setq info (cdr entry)
15948                     num (car entry))
15949             (setq active (gnus-active group))
15950             (setq num
15951                   (if active (- (1+ (cdr active)) (car active)) t))
15952             ;; Check whether the group is foreign.  If so, the
15953             ;; foreign select method has to be entered into the
15954             ;; info.
15955             (let ((method (or gnus-override-subscribe-method
15956                               (gnus-group-method group))))
15957               (if (eq method gnus-select-method)
15958                   (setq info (list group level nil))
15959                 (setq info (list group level nil nil method)))))
15960           (unless previous
15961             (setq previous
15962                   (let ((p gnus-newsrc-alist))
15963                     (while (cddr p)
15964                       (setq p (cdr p)))
15965                     p)))
15966           (setq entry (cons info (cddr previous)))
15967           (if (cdr previous)
15968               (progn
15969                 (setcdr (cdr previous) entry)
15970                 (gnus-sethash group (cons num (cdr previous))
15971                               gnus-newsrc-hashtb))
15972             (setcdr previous entry)
15973             (gnus-sethash group (cons num previous)
15974                           gnus-newsrc-hashtb))
15975           (when (cdr entry)
15976             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15977       (when gnus-group-change-level-function
15978         (funcall gnus-group-change-level-function group level oldlevel)))))
15979
15980 (defun gnus-kill-newsgroup (newsgroup)
15981   "Obsolete function.  Kills a newsgroup."
15982   (gnus-group-change-level
15983    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15984
15985 (defun gnus-check-bogus-newsgroups (&optional confirm)
15986   "Remove bogus newsgroups.
15987 If CONFIRM is non-nil, the user has to confirm the deletion of every
15988 newsgroup."
15989   (let ((newsrc (cdr gnus-newsrc-alist))
15990         bogus group entry info)
15991     (gnus-message 5 "Checking bogus newsgroups...")
15992     (unless (gnus-read-active-file-p)
15993       (gnus-read-active-file))
15994     (when (gnus-read-active-file-p)
15995       ;; Find all bogus newsgroup that are subscribed.
15996       (while newsrc
15997         (setq info (pop newsrc)
15998               group (gnus-info-group info))
15999         (unless (or (gnus-active group) ; Active
16000                     (gnus-info-method info) ; Foreign
16001                     (and confirm
16002                          (not (gnus-y-or-n-p
16003                                (format "Remove bogus newsgroup: %s " group)))))
16004           ;; Found a bogus newsgroup.
16005           (push group bogus)))
16006       ;; Remove all bogus subscribed groups by first killing them, and
16007       ;; then removing them from the list of killed groups.
16008       (while bogus
16009         (when (setq entry (gnus-gethash (setq group (pop bogus))
16010                                         gnus-newsrc-hashtb))
16011           (gnus-group-change-level entry gnus-level-killed)
16012           (setq gnus-killed-list (delete group gnus-killed-list))))
16013       ;; Then we remove all bogus groups from the list of killed and
16014       ;; zombie groups.  They are removed without confirmation.
16015       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
16016             killed)
16017         (while dead-lists
16018           (setq killed (symbol-value (car dead-lists)))
16019           (while killed
16020             (unless (gnus-active (setq group (pop killed)))
16021               ;; The group is bogus.
16022               ;; !!!Slow as hell.
16023               (set (car dead-lists)
16024                    (delete group (symbol-value (car dead-lists))))))
16025           (setq dead-lists (cdr dead-lists))))
16026       (run-hooks 'gnus-check-bogus-groups-hook)
16027       (gnus-message 5 "Checking bogus newsgroups...done"))))
16028
16029 (defun gnus-check-duplicate-killed-groups ()
16030   "Remove duplicates from the list of killed groups."
16031   (interactive)
16032   (let ((killed gnus-killed-list))
16033     (while killed
16034       (gnus-message 9 "%d" (length killed))
16035       (setcdr killed (delete (car killed) (cdr killed)))
16036       (setq killed (cdr killed)))))
16037
16038 ;; We want to inline a function from gnus-cache, so we cheat here:
16039 (eval-when-compile
16040   (provide 'gnus)
16041   (setq gnus-directory (or (getenv "SAVEDIR") "~/News/"))
16042   (require 'gnus-cache))
16043
16044 (defun gnus-get-unread-articles-in-group (info active &optional update)
16045   (when active
16046     ;; Allow the backend to update the info in the group.
16047     (when (and update 
16048                (gnus-request-update-info
16049                 info (gnus-find-method-for-group (gnus-info-group info))))
16050       (gnus-activate-group (gnus-info-group info) nil t))
16051     (let* ((range (gnus-info-read info))
16052            (num 0))
16053       ;; If a cache is present, we may have to alter the active info.
16054       (when (and gnus-use-cache info)
16055         (inline (gnus-cache-possibly-alter-active 
16056                  (gnus-info-group info) active)))
16057       ;; Modify the list of read articles according to what articles
16058       ;; are available; then tally the unread articles and add the
16059       ;; number to the group hash table entry.
16060       (cond
16061        ((zerop (cdr active))
16062         (setq num 0))
16063        ((not range)
16064         (setq num (- (1+ (cdr active)) (car active))))
16065        ((not (listp (cdr range)))
16066         ;; Fix a single (num . num) range according to the
16067         ;; active hash table.
16068         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
16069         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
16070         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
16071         ;; Compute number of unread articles.
16072         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
16073        (t
16074         ;; The read list is a list of ranges.  Fix them according to
16075         ;; the active hash table.
16076         ;; First peel off any elements that are below the lower
16077         ;; active limit.
16078         (while (and (cdr range)
16079                     (>= (car active)
16080                         (or (and (atom (cadr range)) (cadr range))
16081                             (caadr range))))
16082           (if (numberp (car range))
16083               (setcar range
16084                       (cons (car range)
16085                             (or (and (numberp (cadr range))
16086                                      (cadr range))
16087                                 (cdadr range))))
16088             (setcdr (car range)
16089                     (or (and (numberp (nth 1 range)) (nth 1 range))
16090                         (cdadr range))))
16091           (setcdr range (cddr range)))
16092         ;; Adjust the first element to be the same as the lower limit.
16093         (if (and (not (atom (car range)))
16094                  (< (cdar range) (car active)))
16095             (setcdr (car range) (1- (car active))))
16096         ;; Then we want to peel off any elements that are higher
16097         ;; than the upper active limit.
16098         (let ((srange range))
16099           ;; Go past all legal elements.
16100           (while (and (cdr srange)
16101                       (<= (or (and (atom (cadr srange))
16102                                    (cadr srange))
16103                               (caadr srange)) (cdr active)))
16104             (setq srange (cdr srange)))
16105           (if (cdr srange)
16106               ;; Nuke all remaining illegal elements.
16107               (setcdr srange nil))
16108
16109           ;; Adjust the final element.
16110           (if (and (not (atom (car srange)))
16111                    (> (cdar srange) (cdr active)))
16112               (setcdr (car srange) (cdr active))))
16113         ;; Compute the number of unread articles.
16114         (while range
16115           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
16116                                       (cdar range)))
16117                               (or (and (atom (car range)) (car range))
16118                                   (caar range)))))
16119           (setq range (cdr range)))
16120         (setq num (max 0 (- (cdr active) num)))))
16121       ;; Set the number of unread articles.
16122       (when info
16123         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
16124       num)))
16125
16126 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
16127 ;; and compute how many unread articles there are in each group.
16128 (defun gnus-get-unread-articles (&optional level)
16129   (let* ((newsrc (cdr gnus-newsrc-alist))
16130          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
16131          (foreign-level
16132           (min
16133            (cond ((and gnus-activate-foreign-newsgroups
16134                        (not (numberp gnus-activate-foreign-newsgroups)))
16135                   (1+ gnus-level-subscribed))
16136                  ((numberp gnus-activate-foreign-newsgroups)
16137                   gnus-activate-foreign-newsgroups)
16138                  (t 0))
16139            level))
16140          info group active method)
16141     (gnus-message 5 "Checking new news...")
16142
16143     (while newsrc
16144       (setq active (gnus-active (setq group (gnus-info-group
16145                                              (setq info (pop newsrc))))))
16146
16147       ;; Check newsgroups.  If the user doesn't want to check them, or
16148       ;; they can't be checked (for instance, if the news server can't
16149       ;; be reached) we just set the number of unread articles in this
16150       ;; newsgroup to t.  This means that Gnus thinks that there are
16151       ;; unread articles, but it has no idea how many.
16152       (if (and (setq method (gnus-info-method info))
16153                (not (gnus-server-equal
16154                      gnus-select-method
16155                      (setq method (gnus-server-get-method nil method))))
16156                (not (gnus-secondary-method-p method)))
16157           ;; These groups are foreign.  Check the level.
16158           (when (<= (gnus-info-level info) foreign-level)
16159             (setq active (gnus-activate-group group 'scan))
16160             (unless (inline (gnus-virtual-group-p group))
16161               (inline (gnus-close-group group)))
16162             (when (fboundp (intern (concat (symbol-name (car method))
16163                                            "-request-update-info")))
16164               (inline (gnus-request-update-info info method))))
16165         ;; These groups are native or secondary.
16166         (when (and (<= (gnus-info-level info) level)
16167                    (not gnus-read-active-file))
16168           (setq active (gnus-activate-group group 'scan))
16169           (inline (gnus-close-group group))))
16170
16171       ;; Get the number of unread articles in the group.
16172       (if active
16173           (inline (gnus-get-unread-articles-in-group info active))
16174         ;; The group couldn't be reached, so we nix out the number of
16175         ;; unread articles and stuff.
16176         (gnus-set-active group nil)
16177         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
16178
16179     (gnus-message 5 "Checking new news...done")))
16180
16181 ;; Create a hash table out of the newsrc alist.  The `car's of the
16182 ;; alist elements are used as keys.
16183 (defun gnus-make-hashtable-from-newsrc-alist ()
16184   (let ((alist gnus-newsrc-alist)
16185         (ohashtb gnus-newsrc-hashtb)
16186         prev)
16187     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
16188     (setq alist
16189           (setq prev (setq gnus-newsrc-alist
16190                            (if (equal (caar gnus-newsrc-alist)
16191                                       "dummy.group")
16192                                gnus-newsrc-alist
16193                              (cons (list "dummy.group" 0 nil) alist)))))
16194     (while alist
16195       (gnus-sethash
16196        (caar alist)
16197        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
16198              prev)
16199        gnus-newsrc-hashtb)
16200       (setq prev alist
16201             alist (cdr alist)))))
16202
16203 (defun gnus-make-hashtable-from-killed ()
16204   "Create a hash table from the killed and zombie lists."
16205   (let ((lists '(gnus-killed-list gnus-zombie-list))
16206         list)
16207     (setq gnus-killed-hashtb
16208           (gnus-make-hashtable
16209            (+ (length gnus-killed-list) (length gnus-zombie-list))))
16210     (while (setq list (pop lists))
16211       (setq list (symbol-value list))
16212       (while list
16213         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
16214
16215 (defun gnus-activate-group (group &optional scan dont-check method)
16216   ;; Check whether a group has been activated or not.
16217   ;; If SCAN, request a scan of that group as well.
16218   (let ((method (or method (gnus-find-method-for-group group)))
16219         active)
16220     (and (gnus-check-server method)
16221          ;; We escape all bugs and quit here to make it possible to
16222          ;; continue if a group is so out-there that it reports bugs
16223          ;; and stuff.
16224          (progn
16225            (and scan
16226                 (gnus-check-backend-function 'request-scan (car method))
16227                 (gnus-request-scan group method))
16228            t)
16229          (condition-case ()
16230              (gnus-request-group group dont-check method)
16231         ;   (error nil)
16232            (quit nil))
16233          (save-excursion
16234            (set-buffer nntp-server-buffer)
16235            (goto-char (point-min))
16236            ;; Parse the result we got from `gnus-request-group'.
16237            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16238                 (progn
16239                   (goto-char (match-beginning 1))
16240                   (gnus-set-active
16241                    group (setq active (cons (read (current-buffer))
16242                                             (read (current-buffer)))))
16243                   ;; Return the new active info.
16244                   active))))))
16245
16246 (defun gnus-update-read-articles (group unread)
16247   "Update the list of read and ticked articles in GROUP using the
16248 UNREAD and TICKED lists.
16249 Note: UNSELECTED has to be sorted over `<'.
16250 Returns whether the updating was successful."
16251   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16252          (entry (gnus-gethash group gnus-newsrc-hashtb))
16253          (info (nth 2 entry))
16254          (prev 1)
16255          (unread (sort (copy-sequence unread) '<))
16256          read)
16257     (if (or (not info) (not active))
16258         ;; There is no info on this group if it was, in fact,
16259         ;; killed.  Gnus stores no information on killed groups, so
16260         ;; there's nothing to be done.
16261         ;; One could store the information somewhere temporarily,
16262         ;; perhaps...  Hmmm...
16263         ()
16264       ;; Remove any negative articles numbers.
16265       (while (and unread (< (car unread) 0))
16266         (setq unread (cdr unread)))
16267       ;; Remove any expired article numbers
16268       (while (and unread (< (car unread) (car active)))
16269         (setq unread (cdr unread)))
16270       ;; Compute the ranges of read articles by looking at the list of
16271       ;; unread articles.
16272       (while unread
16273         (if (/= (car unread) prev)
16274             (setq read (cons (if (= prev (1- (car unread))) prev
16275                                (cons prev (1- (car unread)))) read)))
16276         (setq prev (1+ (car unread)))
16277         (setq unread (cdr unread)))
16278       (when (<= prev (cdr active))
16279         (setq read (cons (cons prev (cdr active)) read)))
16280       ;; Enter this list into the group info.
16281       (gnus-info-set-read
16282        info (if (> (length read) 1) (nreverse read) read))
16283       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16284       (gnus-get-unread-articles-in-group info (gnus-active group))
16285       t)))
16286
16287 (defun gnus-make-articles-unread (group articles)
16288   "Mark ARTICLES in GROUP as unread."
16289   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16290                           (gnus-gethash (gnus-group-real-name group)
16291                                         gnus-newsrc-hashtb))))
16292          (ranges (gnus-info-read info))
16293          news article)
16294     (while articles
16295       (when (gnus-member-of-range
16296              (setq article (pop articles)) ranges)
16297         (setq news (cons article news))))
16298     (when news
16299       (gnus-info-set-read
16300        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16301       (gnus-group-update-group group t))))
16302
16303 ;; Enter all dead groups into the hashtb.
16304 (defun gnus-update-active-hashtb-from-killed ()
16305   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16306         (lists (list gnus-killed-list gnus-zombie-list))
16307         killed)
16308     (while lists
16309       (setq killed (car lists))
16310       (while killed
16311         (gnus-sethash (car killed) nil hashtb)
16312         (setq killed (cdr killed)))
16313       (setq lists (cdr lists)))))
16314
16315 (defun gnus-get-killed-groups ()
16316   "Go through the active hashtb and mark all unknown groups as killed."
16317   ;; First make sure active file has been read.
16318   (unless (gnus-read-active-file-p)
16319     (let ((gnus-read-active-file t))
16320       (gnus-read-active-file)))
16321   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16322   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16323   (mapatoms
16324    (lambda (sym)
16325      (let ((groups 0)
16326            (group (symbol-name sym)))
16327        (if (or (null group)
16328                (gnus-gethash group gnus-killed-hashtb)
16329                (gnus-gethash group gnus-newsrc-hashtb))
16330            ()
16331          (let ((do-sub (gnus-matches-options-n group)))
16332            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16333                ()
16334              (setq groups (1+ groups))
16335              (setq gnus-killed-list
16336                    (cons group gnus-killed-list))
16337              (gnus-sethash group group gnus-killed-hashtb))))))
16338    gnus-active-hashtb))
16339
16340 ;; Get the active file(s) from the backend(s).
16341 (defun gnus-read-active-file ()
16342   (gnus-group-set-mode-line)
16343   (let ((methods 
16344          (append
16345           (if (gnus-check-server gnus-select-method)
16346               ;; The native server is available.
16347               (cons gnus-select-method gnus-secondary-select-methods)
16348             ;; The native server is down, so we just do the
16349             ;; secondary ones.
16350             gnus-secondary-select-methods)
16351           ;; Also read from the archive server.
16352           (when (gnus-archive-server-wanted-p)
16353             (list "archive"))))
16354         list-type)
16355     (setq gnus-have-read-active-file nil)
16356     (save-excursion
16357       (set-buffer nntp-server-buffer)
16358       (while methods
16359         (let* ((method (if (stringp (car methods))
16360                            (gnus-server-get-method nil (car methods))
16361                          (car methods)))
16362                (where (nth 1 method))
16363                (mesg (format "Reading active file%s via %s..."
16364                              (if (and where (not (zerop (length where))))
16365                                  (concat " from " where) "")
16366                              (car method))))
16367           (gnus-message 5 mesg)
16368           (when (gnus-check-server method)
16369             ;; Request that the backend scan its incoming messages.
16370             (and (gnus-check-backend-function 'request-scan (car method))
16371                  (gnus-request-scan nil method))
16372             (cond
16373              ((and (eq gnus-read-active-file 'some)
16374                    (gnus-check-backend-function 'retrieve-groups (car method)))
16375               (let ((newsrc (cdr gnus-newsrc-alist))
16376                     (gmethod (gnus-server-get-method nil method))
16377                     groups info)
16378                 (while (setq info (pop newsrc))
16379                   (when (gnus-server-equal
16380                          (gnus-find-method-for-group 
16381                           (gnus-info-group info) info)
16382                          gmethod)
16383                     (push (gnus-group-real-name (gnus-info-group info)) 
16384                           groups)))
16385                 (when groups
16386                   (gnus-check-server method)
16387                   (setq list-type (gnus-retrieve-groups groups method))
16388                   (cond
16389                    ((not list-type)
16390                     (gnus-error
16391                      1.2 "Cannot read partial active file from %s server."
16392                      (car method)))
16393                    ((eq list-type 'active)
16394                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16395                    (t
16396                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16397              (t
16398               (if (not (gnus-request-list method))
16399                   (unless (equal method gnus-message-archive-method)
16400                     (gnus-error 1 "Cannot read active file from %s server."
16401                                 (car method)))
16402                 (gnus-message 5 mesg)
16403                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16404                 ;; We mark this active file as read.
16405                 (push method gnus-have-read-active-file)
16406                 (gnus-message 5 "%sdone" mesg))))))
16407         (setq methods (cdr methods))))))
16408
16409 ;; Read an active file and place the results in `gnus-active-hashtb'.
16410 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16411   (unless method
16412     (setq method gnus-select-method))
16413   (let ((cur (current-buffer))
16414         (hashtb (or hashtb
16415                     (if (and gnus-active-hashtb
16416                              (not (equal method gnus-select-method)))
16417                         gnus-active-hashtb
16418                       (setq gnus-active-hashtb
16419                             (if (equal method gnus-select-method)
16420                                 (gnus-make-hashtable
16421                                  (count-lines (point-min) (point-max)))
16422                               (gnus-make-hashtable 4096)))))))
16423     ;; Delete unnecessary lines.
16424     (goto-char (point-min))
16425     (while (search-forward "\nto." nil t)
16426       (delete-region (1+ (match-beginning 0))
16427                      (progn (forward-line 1) (point))))
16428     (or (string= gnus-ignored-newsgroups "")
16429         (progn
16430           (goto-char (point-min))
16431           (delete-matching-lines gnus-ignored-newsgroups)))
16432     ;; Make the group names readable as a lisp expression even if they
16433     ;; contain special characters.
16434     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16435     (goto-char (point-max))
16436     (while (re-search-backward "[][';?()#]" nil t)
16437       (insert ?\\))
16438     ;; If these are groups from a foreign select method, we insert the
16439     ;; group prefix in front of the group names.
16440     (and method (not (gnus-server-equal
16441                       (gnus-server-get-method nil method)
16442                       (gnus-server-get-method nil gnus-select-method)))
16443          (let ((prefix (gnus-group-prefixed-name "" method)))
16444            (goto-char (point-min))
16445            (while (and (not (eobp))
16446                        (progn (insert prefix)
16447                               (zerop (forward-line 1)))))))
16448     ;; Store the active file in a hash table.
16449     (goto-char (point-min))
16450     (if (string-match "%[oO]" gnus-group-line-format)
16451         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16452         ;; If we want information on moderated groups, we use this
16453         ;; loop...
16454         (let* ((mod-hashtb (make-vector 7 0))
16455                (m (intern "m" mod-hashtb))
16456                group max min)
16457           (while (not (eobp))
16458             (condition-case nil
16459                 (progn
16460                   (narrow-to-region (point) (gnus-point-at-eol))
16461                   (setq group (let ((obarray hashtb)) (read cur)))
16462                   (if (and (numberp (setq max (read cur)))
16463                            (numberp (setq min (read cur)))
16464                            (progn
16465                              (skip-chars-forward " \t")
16466                              (not
16467                               (or (= (following-char) ?=)
16468                                   (= (following-char) ?x)
16469                                   (= (following-char) ?j)))))
16470                       (set group (cons min max))
16471                     (set group nil))
16472                   ;; Enter moderated groups into a list.
16473                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16474                       (setq gnus-moderated-list
16475                             (cons (symbol-name group) gnus-moderated-list))))
16476               (error
16477                (and group
16478                     (symbolp group)
16479                     (set group nil))))
16480             (widen)
16481             (forward-line 1)))
16482       ;; And if we do not care about moderation, we use this loop,
16483       ;; which is faster.
16484       (let (group max min)
16485         (while (not (eobp))
16486           (condition-case ()
16487               (progn
16488                 (narrow-to-region (point) (gnus-point-at-eol))
16489                 ;; group gets set to a symbol interned in the hash table
16490                 ;; (what a hack!!) - jwz
16491                 (setq group (let ((obarray hashtb)) (read cur)))
16492                 (if (and (numberp (setq max (read cur)))
16493                          (numberp (setq min (read cur)))
16494                          (progn
16495                            (skip-chars-forward " \t")
16496                            (not
16497                             (or (= (following-char) ?=)
16498                                 (= (following-char) ?x)
16499                                 (= (following-char) ?j)))))
16500                     (set group (cons min max))
16501                   (set group nil)))
16502             (error
16503              (progn
16504                (and group
16505                     (symbolp group)
16506                     (set group nil))
16507                (or ignore-errors
16508                    (gnus-message 3 "Warning - illegal active: %s"
16509                                  (buffer-substring
16510                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16511           (widen)
16512           (forward-line 1))))))
16513
16514 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16515   ;; Parse a "groups" active file.
16516   (let ((cur (current-buffer))
16517         (hashtb (or hashtb
16518                     (if (and method gnus-active-hashtb)
16519                         gnus-active-hashtb
16520                       (setq gnus-active-hashtb
16521                             (gnus-make-hashtable
16522                              (count-lines (point-min) (point-max)))))))
16523         (prefix (and method
16524                      (not (gnus-server-equal
16525                            (gnus-server-get-method nil method)
16526                            (gnus-server-get-method nil gnus-select-method)))
16527                      (gnus-group-prefixed-name "" method))))
16528
16529     (goto-char (point-min))
16530     ;; We split this into to separate loops, one with the prefix
16531     ;; and one without to speed the reading up somewhat.
16532     (if prefix
16533         (let (min max opoint group)
16534           (while (not (eobp))
16535             (condition-case ()
16536                 (progn
16537                   (read cur) (read cur)
16538                   (setq min (read cur)
16539                         max (read cur)
16540                         opoint (point))
16541                   (skip-chars-forward " \t")
16542                   (insert prefix)
16543                   (goto-char opoint)
16544                   (set (let ((obarray hashtb)) (read cur))
16545                        (cons min max)))
16546               (error (and group (symbolp group) (set group nil))))
16547             (forward-line 1)))
16548       (let (min max group)
16549         (while (not (eobp))
16550           (condition-case ()
16551               (if (= (following-char) ?2)
16552                   (progn
16553                     (read cur) (read cur)
16554                     (setq min (read cur)
16555                           max (read cur))
16556                     (set (setq group (let ((obarray hashtb)) (read cur)))
16557                          (cons min max))))
16558             (error (and group (symbolp group) (set group nil))))
16559           (forward-line 1))))))
16560
16561 (defun gnus-read-newsrc-file (&optional force)
16562   "Read startup file.
16563 If FORCE is non-nil, the .newsrc file is read."
16564   ;; Reset variables that might be defined in the .newsrc.eld file.
16565   (let ((variables gnus-variable-list))
16566     (while variables
16567       (set (car variables) nil)
16568       (setq variables (cdr variables))))
16569   (let* ((newsrc-file gnus-current-startup-file)
16570          (quick-file (concat newsrc-file ".el")))
16571     (save-excursion
16572       ;; We always load the .newsrc.eld file.  If always contains
16573       ;; much information that can not be gotten from the .newsrc
16574       ;; file (ticked articles, killed groups, foreign methods, etc.)
16575       (gnus-read-newsrc-el-file quick-file)
16576
16577       (if (and (file-exists-p gnus-current-startup-file)
16578                (or force
16579                    (and (file-newer-than-file-p newsrc-file quick-file)
16580                         (file-newer-than-file-p newsrc-file
16581                                                 (concat quick-file "d")))
16582                    (not gnus-newsrc-alist)))
16583           ;; We read the .newsrc file.  Note that if there if a
16584           ;; .newsrc.eld file exists, it has already been read, and
16585           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16586           ;; the .newsrc file, Gnus will only use the information it
16587           ;; can find there for changing the data already read -
16588           ;; ie. reading the .newsrc file will not trash the data
16589           ;; already read (except for read articles).
16590           (save-excursion
16591             (gnus-message 5 "Reading %s..." newsrc-file)
16592             (set-buffer (find-file-noselect newsrc-file))
16593             (buffer-disable-undo (current-buffer))
16594             (gnus-newsrc-to-gnus-format)
16595             (kill-buffer (current-buffer))
16596             (gnus-message 5 "Reading %s...done" newsrc-file)))
16597
16598       ;; Read any slave files.
16599       (unless gnus-slave
16600         (gnus-master-read-slave-newsrc))
16601       
16602       ;; Convert old to new.
16603       (gnus-convert-old-newsrc))))
16604
16605 (defun gnus-continuum-version (version)
16606   "Return VERSION as a floating point number."
16607   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16608             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16609     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16610            (number (match-string 2 version))
16611            major minor least)
16612       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16613       (setq major (string-to-number (match-string 1 number)))
16614       (setq minor (string-to-number (match-string 2 number)))
16615       (setq least (if (match-beginning 3)
16616                       (string-to-number (match-string 3 number))
16617                     0))
16618       (string-to-number
16619        (if (zerop major)
16620            (format "%s00%02d%02d"
16621                    (cond 
16622                     ((member alpha '("(ding)" "d")) "4.99")
16623                     ((member alpha '("September" "s")) "5.01")
16624                     ((member alpha '("Red" "r")) "5.03"))
16625                    minor least)
16626          (format "%d.%02d%02d" major minor least))))))
16627
16628 (defun gnus-convert-old-newsrc ()
16629   "Convert old newsrc into the new format, if needed."
16630   (let ((fcv (and gnus-newsrc-file-version
16631                   (gnus-continuum-version gnus-newsrc-file-version))))
16632     (cond
16633      ;; No .newsrc.eld file was loaded.
16634      ((null fcv) nil)
16635      ;; Gnus 5 .newsrc.eld was loaded.
16636      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16637       (gnus-convert-old-ticks)))))
16638
16639 (defun gnus-convert-old-ticks ()
16640   (let ((newsrc (cdr gnus-newsrc-alist))
16641         marks info dormant ticked)
16642     (while (setq info (pop newsrc))
16643       (when (setq marks (gnus-info-marks info))
16644         (setq dormant (cdr (assq 'dormant marks))
16645               ticked (cdr (assq 'tick marks)))
16646         (when (or dormant ticked)
16647           (gnus-info-set-read
16648            info
16649            (gnus-add-to-range
16650             (gnus-info-read info)
16651             (nconc (gnus-uncompress-range dormant)
16652                    (gnus-uncompress-range ticked)))))))))
16653
16654 (defun gnus-read-newsrc-el-file (file)
16655   (let ((ding-file (concat file "d")))
16656     ;; We always, always read the .eld file.
16657     (gnus-message 5 "Reading %s..." ding-file)
16658     (let (gnus-newsrc-assoc)
16659       (condition-case nil
16660           (load ding-file t t t)
16661         (error
16662          (gnus-error 1 "Error in %s" ding-file)))
16663       (when gnus-newsrc-assoc
16664         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16665     (gnus-make-hashtable-from-newsrc-alist)
16666     (when (file-newer-than-file-p file ding-file)
16667       ;; Old format quick file
16668       (gnus-message 5 "Reading %s..." file)
16669       ;; The .el file is newer than the .eld file, so we read that one
16670       ;; as well.
16671       (gnus-read-old-newsrc-el-file file))))
16672
16673 ;; Parse the old-style quick startup file
16674 (defun gnus-read-old-newsrc-el-file (file)
16675   (let (newsrc killed marked group m info)
16676     (prog1
16677         (let ((gnus-killed-assoc nil)
16678               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16679           (prog1
16680               (condition-case nil
16681                   (load file t t t)
16682                 (error nil))
16683             (setq newsrc gnus-newsrc-assoc
16684                   killed gnus-killed-assoc
16685                   marked gnus-marked-assoc)))
16686       (setq gnus-newsrc-alist nil)
16687       (while (setq group (pop newsrc))
16688         (if (setq info (gnus-get-info (car group)))
16689             (progn
16690               (gnus-info-set-read info (cddr group))
16691               (gnus-info-set-level
16692                info (if (nth 1 group) gnus-level-default-subscribed
16693                       gnus-level-default-unsubscribed))
16694               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16695           (push (setq info
16696                       (list (car group)
16697                             (if (nth 1 group) gnus-level-default-subscribed
16698                               gnus-level-default-unsubscribed)
16699                             (cddr group)))
16700                 gnus-newsrc-alist))
16701         ;; Copy marks into info.
16702         (when (setq m (assoc (car group) marked))
16703           (unless (nthcdr 3 info)
16704             (nconc info (list nil)))
16705           (gnus-info-set-marks
16706            info (list (cons 'tick (gnus-compress-sequence 
16707                                    (sort (cdr m) '<) t))))))
16708       (setq newsrc killed)
16709       (while newsrc
16710         (setcar newsrc (caar newsrc))
16711         (setq newsrc (cdr newsrc)))
16712       (setq gnus-killed-list killed))
16713     ;; The .el file version of this variable does not begin with
16714     ;; "options", while the .eld version does, so we just add it if it
16715     ;; isn't there.
16716     (and
16717      gnus-newsrc-options
16718      (progn
16719        (and (not (string-match "^ *options" gnus-newsrc-options))
16720             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16721        (and (not (string-match "\n$" gnus-newsrc-options))
16722             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16723        ;; Finally, if we read some options lines, we parse them.
16724        (or (string= gnus-newsrc-options "")
16725            (gnus-newsrc-parse-options gnus-newsrc-options))))
16726
16727     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16728     (gnus-make-hashtable-from-newsrc-alist)))
16729
16730 (defun gnus-make-newsrc-file (file)
16731   "Make server dependent file name by catenating FILE and server host name."
16732   (let* ((file (expand-file-name file nil))
16733          (real-file (concat file "-" (nth 1 gnus-select-method))))
16734     (if (or (file-exists-p real-file)
16735             (file-exists-p (concat real-file ".el"))
16736             (file-exists-p (concat real-file ".eld")))
16737         real-file file)))
16738
16739 (defun gnus-newsrc-to-gnus-format ()
16740   (setq gnus-newsrc-options "")
16741   (setq gnus-newsrc-options-n nil)
16742
16743   (or gnus-active-hashtb
16744       (setq gnus-active-hashtb (make-vector 4095 0)))
16745   (let ((buf (current-buffer))
16746         (already-read (> (length gnus-newsrc-alist) 1))
16747         group subscribed options-symbol newsrc Options-symbol
16748         symbol reads num1)
16749     (goto-char (point-min))
16750     ;; We intern the symbol `options' in the active hashtb so that we
16751     ;; can `eq' against it later.
16752     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16753     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16754
16755     (while (not (eobp))
16756       ;; We first read the first word on the line by narrowing and
16757       ;; then reading into `gnus-active-hashtb'.  Most groups will
16758       ;; already exist in that hashtb, so this will save some string
16759       ;; space.
16760       (narrow-to-region
16761        (point)
16762        (progn (skip-chars-forward "^ \t!:\n") (point)))
16763       (goto-char (point-min))
16764       (setq symbol
16765             (and (/= (point-min) (point-max))
16766                  (let ((obarray gnus-active-hashtb)) (read buf))))
16767       (widen)
16768       ;; Now, the symbol we have read is either `options' or a group
16769       ;; name.  If it is an options line, we just add it to a string.
16770       (cond
16771        ((or (eq symbol options-symbol)
16772             (eq symbol Options-symbol))
16773         (setq gnus-newsrc-options
16774               ;; This concating is quite inefficient, but since our
16775               ;; thorough studies show that approx 99.37% of all
16776               ;; .newsrc files only contain a single options line, we
16777               ;; don't give a damn, frankly, my dear.
16778               (concat gnus-newsrc-options
16779                       (buffer-substring
16780                        (gnus-point-at-bol)
16781                        ;; Options may continue on the next line.
16782                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16783                                 (progn (beginning-of-line) (point)))
16784                            (point)))))
16785         (forward-line -1))
16786        (symbol
16787         ;; Group names can be just numbers.  
16788         (when (numberp symbol) 
16789           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16790         (or (boundp symbol) (set symbol nil))
16791         ;; It was a group name.
16792         (setq subscribed (= (following-char) ?:)
16793               group (symbol-name symbol)
16794               reads nil)
16795         (if (eolp)
16796             ;; If the line ends here, this is clearly a buggy line, so
16797             ;; we put point a the beginning of line and let the cond
16798             ;; below do the error handling.
16799             (beginning-of-line)
16800           ;; We skip to the beginning of the ranges.
16801           (skip-chars-forward "!: \t"))
16802         ;; We are now at the beginning of the list of read articles.
16803         ;; We read them range by range.
16804         (while
16805             (cond
16806              ((looking-at "[0-9]+")
16807               ;; We narrow and read a number instead of buffer-substring/
16808               ;; string-to-int because it's faster.  narrow/widen is
16809               ;; faster than save-restriction/narrow, and save-restriction
16810               ;; produces a garbage object.
16811               (setq num1 (progn
16812                            (narrow-to-region (match-beginning 0) (match-end 0))
16813                            (read buf)))
16814               (widen)
16815               ;; If the next character is a dash, then this is a range.
16816               (if (= (following-char) ?-)
16817                   (progn
16818                     ;; We read the upper bound of the range.
16819                     (forward-char 1)
16820                     (if (not (looking-at "[0-9]+"))
16821                         ;; This is a buggy line, by we pretend that
16822                         ;; it's kinda OK.  Perhaps the user should be
16823                         ;; dinged?
16824                         (setq reads (cons num1 reads))
16825                       (setq reads
16826                             (cons
16827                              (cons num1
16828                                    (progn
16829                                      (narrow-to-region (match-beginning 0)
16830                                                        (match-end 0))
16831                                      (read buf)))
16832                              reads))
16833                       (widen)))
16834                 ;; It was just a simple number, so we add it to the
16835                 ;; list of ranges.
16836                 (setq reads (cons num1 reads)))
16837               ;; If the next char in ?\n, then we have reached the end
16838               ;; of the line and return nil.
16839               (/= (following-char) ?\n))
16840              ((= (following-char) ?\n)
16841               ;; End of line, so we end.
16842               nil)
16843              (t
16844               ;; Not numbers and not eol, so this might be a buggy
16845               ;; line...
16846               (or (eobp)
16847                   ;; If it was eob instead of ?\n, we allow it.
16848                   (progn
16849                     ;; The line was buggy.
16850                     (setq group nil)
16851                     (gnus-error 3.1 "Mangled line: %s"
16852                                 (buffer-substring (gnus-point-at-bol)
16853                                                   (gnus-point-at-eol)))))
16854               nil))
16855           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16856           ;; we allow them, because it's a common mistake to put a
16857           ;; space after the comma.
16858           (skip-chars-forward ", "))
16859
16860         ;; We have already read .newsrc.eld, so we gently update the
16861         ;; data in the hash table with the information we have just
16862         ;; read.
16863         (when group
16864           (let ((info (gnus-get-info group))
16865                 level)
16866             (if info
16867                 ;; There is an entry for this file in the alist.
16868                 (progn
16869                   (gnus-info-set-read info (nreverse reads))
16870                   ;; We update the level very gently.  In fact, we
16871                   ;; only change it if there's been a status change
16872                   ;; from subscribed to unsubscribed, or vice versa.
16873                   (setq level (gnus-info-level info))
16874                   (cond ((and (<= level gnus-level-subscribed)
16875                               (not subscribed))
16876                          (setq level (if reads
16877                                          gnus-level-default-unsubscribed
16878                                        (1+ gnus-level-default-unsubscribed))))
16879                         ((and (> level gnus-level-subscribed) subscribed)
16880                          (setq level gnus-level-default-subscribed)))
16881                   (gnus-info-set-level info level))
16882               ;; This is a new group.
16883               (setq info (list group
16884                                (if subscribed
16885                                    gnus-level-default-subscribed
16886                                  (if reads
16887                                      (1+ gnus-level-subscribed)
16888                                    gnus-level-default-unsubscribed))
16889                                (nreverse reads))))
16890             (setq newsrc (cons info newsrc))))))
16891       (forward-line 1))
16892
16893     (setq newsrc (nreverse newsrc))
16894
16895     (if (not already-read)
16896         ()
16897       ;; We now have two newsrc lists - `newsrc', which is what we
16898       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16899       ;; what we've read from .newsrc.eld.  We have to merge these
16900       ;; lists.  We do this by "attaching" any (foreign) groups in the
16901       ;; gnus-newsrc-alist to the (native) group that precedes them.
16902       (let ((rc (cdr gnus-newsrc-alist))
16903             (prev gnus-newsrc-alist)
16904             entry mentry)
16905         (while rc
16906           (or (null (nth 4 (car rc)))   ; It's a native group.
16907               (assoc (caar rc) newsrc) ; It's already in the alist.
16908               (if (setq entry (assoc (caar prev) newsrc))
16909                   (setcdr (setq mentry (memq entry newsrc))
16910                           (cons (car rc) (cdr mentry)))
16911                 (setq newsrc (cons (car rc) newsrc))))
16912           (setq prev rc
16913                 rc (cdr rc)))))
16914
16915     (setq gnus-newsrc-alist newsrc)
16916     ;; We make the newsrc hashtb.
16917     (gnus-make-hashtable-from-newsrc-alist)
16918
16919     ;; Finally, if we read some options lines, we parse them.
16920     (or (string= gnus-newsrc-options "")
16921         (gnus-newsrc-parse-options gnus-newsrc-options))))
16922
16923 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16924 ;; The return value will be a list on the form
16925 ;; ((regexp1 . ignore)
16926 ;;  (regexp2 . subscribe)...)
16927 ;; When handling new newsgroups, groups that match a `ignore' regexp
16928 ;; will be ignored, and groups that match a `subscribe' regexp will be
16929 ;; subscribed.  A line like
16930 ;; options -n !all rec.all
16931 ;; will lead to a list that looks like
16932 ;; (("^rec\\..+" . subscribe)
16933 ;;  ("^.+" . ignore))
16934 ;; So all "rec.*" groups will be subscribed, while all the other
16935 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16936 ;; different from "options -n rec.all !all".
16937 (defun gnus-newsrc-parse-options (options)
16938   (let (out eol)
16939     (save-excursion
16940       (gnus-set-work-buffer)
16941       (insert (regexp-quote options))
16942       ;; First we treat all continuation lines.
16943       (goto-char (point-min))
16944       (while (re-search-forward "\n[ \t]+" nil t)
16945         (replace-match " " t t))
16946       ;; Then we transform all "all"s into ".+"s.
16947       (goto-char (point-min))
16948       (while (re-search-forward "\\ball\\b" nil t)
16949         (replace-match ".+" t t))
16950       (goto-char (point-min))
16951       ;; We remove all other options than the "-n" ones.
16952       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16953         (replace-match " ")
16954         (forward-char -1))
16955       (goto-char (point-min))
16956
16957       ;; We are only interested in "options -n" lines - we
16958       ;; ignore the other option lines.
16959       (while (re-search-forward "[ \t]-n" nil t)
16960         (setq eol
16961               (or (save-excursion
16962                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16963                          (- (point) 2)))
16964                   (gnus-point-at-eol)))
16965         ;; Search for all "words"...
16966         (while (re-search-forward "[^ \t,\n]+" eol t)
16967           (if (= (char-after (match-beginning 0)) ?!)
16968               ;; If the word begins with a bang (!), this is a "not"
16969               ;; spec.  We put this spec (minus the bang) and the
16970               ;; symbol `ignore' into the list.
16971               (setq out (cons (cons (concat
16972                                      "^" (buffer-substring
16973                                           (1+ (match-beginning 0))
16974                                           (match-end 0)))
16975                                     'ignore) out))
16976             ;; There was no bang, so this is a "yes" spec.
16977             (setq out (cons (cons (concat "^" (match-string 0))
16978                                   'subscribe) out)))))
16979
16980       (setq gnus-newsrc-options-n out))))
16981
16982 (defun gnus-save-newsrc-file (&optional force)
16983   "Save .newsrc file."
16984   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16985   ;; from the variable gnus-newsrc-alist.
16986   (when (and (or gnus-newsrc-alist gnus-killed-list)
16987              gnus-current-startup-file)
16988     (save-excursion
16989       (if (and (or gnus-use-dribble-file gnus-slave)
16990                (not force)
16991                (or (not gnus-dribble-buffer)
16992                    (not (buffer-name gnus-dribble-buffer))
16993                    (zerop (save-excursion
16994                             (set-buffer gnus-dribble-buffer)
16995                             (buffer-size)))))
16996           (gnus-message 4 "(No changes need to be saved)")
16997         (run-hooks 'gnus-save-newsrc-hook)
16998         (if gnus-slave
16999             (gnus-slave-save-newsrc)
17000           ;; Save .newsrc.
17001           (when gnus-save-newsrc-file
17002             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
17003             (gnus-gnus-to-newsrc-format)
17004             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
17005           ;; Save .newsrc.eld.
17006           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
17007           (make-local-variable 'version-control)
17008           (setq version-control 'never)
17009           (setq buffer-file-name
17010                 (concat gnus-current-startup-file ".eld"))
17011           (setq default-directory (file-name-directory buffer-file-name))
17012           (gnus-add-current-to-buffer-list)
17013           (buffer-disable-undo (current-buffer))
17014           (erase-buffer)
17015           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
17016           (gnus-gnus-to-quick-newsrc-format)
17017           (run-hooks 'gnus-save-quick-newsrc-hook)
17018           (save-buffer)
17019           (kill-buffer (current-buffer))
17020           (gnus-message
17021            5 "Saving %s.eld...done" gnus-current-startup-file))
17022         (gnus-dribble-delete-file)
17023         (gnus-group-set-mode-line)))))
17024
17025 (defun gnus-gnus-to-quick-newsrc-format ()
17026   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
17027   (insert ";; Gnus startup file.\n")
17028   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
17029   (insert ";; to read .newsrc.\n")
17030   (insert "(setq gnus-newsrc-file-version "
17031           (prin1-to-string gnus-version) ")\n")
17032   (let ((variables
17033          (if gnus-save-killed-list gnus-variable-list
17034            ;; Remove the `gnus-killed-list' from the list of variables
17035            ;; to be saved, if required.
17036            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
17037         ;; Peel off the "dummy" group.
17038         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
17039         variable)
17040     ;; Insert the variables into the file.
17041     (while variables
17042       (when (and (boundp (setq variable (pop variables)))
17043                  (symbol-value variable))
17044         (insert "(setq " (symbol-name variable) " '")
17045         (prin1 (symbol-value variable) (current-buffer))
17046         (insert ")\n")))))
17047
17048 (defun gnus-gnus-to-newsrc-format ()
17049   ;; Generate and save the .newsrc file.
17050   (save-excursion
17051     (set-buffer (create-file-buffer gnus-current-startup-file))
17052     (let ((newsrc (cdr gnus-newsrc-alist))
17053           (standard-output (current-buffer))
17054           info ranges range method)
17055       (setq buffer-file-name gnus-current-startup-file)
17056       (setq default-directory (file-name-directory buffer-file-name))
17057       (buffer-disable-undo (current-buffer))
17058       (erase-buffer)
17059       ;; Write options.
17060       (if gnus-newsrc-options (insert gnus-newsrc-options))
17061       ;; Write subscribed and unsubscribed.
17062       (while (setq info (pop newsrc))
17063         ;; Don't write foreign groups to .newsrc.
17064         (when (or (null (setq method (gnus-info-method info)))
17065                   (equal method "native")
17066                   (gnus-server-equal method gnus-select-method))
17067           (insert (gnus-info-group info)
17068                   (if (> (gnus-info-level info) gnus-level-subscribed)
17069                       "!" ":"))
17070           (when (setq ranges (gnus-info-read info))
17071             (insert " ")
17072             (if (not (listp (cdr ranges)))
17073                 (if (= (car ranges) (cdr ranges))
17074                     (princ (car ranges))
17075                   (princ (car ranges))
17076                   (insert "-")
17077                   (princ (cdr ranges)))
17078               (while (setq range (pop ranges))
17079                 (if (or (atom range) (= (car range) (cdr range)))
17080                     (princ (or (and (atom range) range) (car range)))
17081                   (princ (car range))
17082                   (insert "-")
17083                   (princ (cdr range)))
17084                 (if ranges (insert ",")))))
17085           (insert "\n")))
17086       (make-local-variable 'version-control)
17087       (setq version-control 'never)
17088       ;; It has been reported that sometime the modtime on the .newsrc
17089       ;; file seems to be off.  We really do want to overwrite it, so
17090       ;; we clear the modtime here before saving.  It's a bit odd,
17091       ;; though...
17092       ;; sometimes the modtime clear isn't sufficient.  most brute force:
17093       ;; delete the silly thing entirely first.  but this fails to provide
17094       ;; such niceties as .newsrc~ creation.
17095       (if gnus-modtime-botch
17096           (delete-file gnus-startup-file)
17097         (clear-visited-file-modtime))
17098       (run-hooks 'gnus-save-standard-newsrc-hook)
17099       (save-buffer)
17100       (kill-buffer (current-buffer)))))
17101
17102 \f
17103 ;;;
17104 ;;; Slave functions.
17105 ;;;
17106
17107 (defun gnus-slave-save-newsrc ()
17108   (save-excursion
17109     (set-buffer gnus-dribble-buffer)
17110     (let ((slave-name
17111            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
17112       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
17113
17114 (defun gnus-master-read-slave-newsrc ()
17115   (let ((slave-files
17116          (directory-files
17117           (file-name-directory gnus-current-startup-file)
17118           t (concat
17119              "^" (regexp-quote
17120                   (concat
17121                    (file-name-nondirectory gnus-current-startup-file)
17122                    "-slave-")))
17123           t))
17124         file)
17125     (if (not slave-files)
17126         ()                              ; There are no slave files to read.
17127       (gnus-message 7 "Reading slave newsrcs...")
17128       (save-excursion
17129         (set-buffer (get-buffer-create " *gnus slave*"))
17130         (buffer-disable-undo (current-buffer))
17131         (setq slave-files
17132               (sort (mapcar (lambda (file)
17133                               (list (nth 5 (file-attributes file)) file))
17134                             slave-files)
17135                     (lambda (f1 f2)
17136                       (or (< (caar f1) (caar f2))
17137                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
17138         (while slave-files
17139           (erase-buffer)
17140           (setq file (nth 1 (car slave-files)))
17141           (insert-file-contents file)
17142           (if (condition-case ()
17143                   (progn
17144                     (eval-buffer (current-buffer))
17145                     t)
17146                 (error
17147                  (gnus-error 3.2 "Possible error in %s" file)
17148                  nil))
17149               (or gnus-slave ; Slaves shouldn't delete these files.
17150                   (condition-case ()
17151                       (delete-file file)
17152                     (error nil))))
17153           (setq slave-files (cdr slave-files))))
17154       (gnus-message 7 "Reading slave newsrcs...done"))))
17155
17156 \f
17157 ;;;
17158 ;;; Group description.
17159 ;;;
17160
17161 (defun gnus-read-all-descriptions-files ()
17162   (let ((methods (cons gnus-select-method 
17163                        (nconc
17164                         (when (gnus-archive-server-wanted-p)
17165                           (list "archive"))
17166                         gnus-secondary-select-methods))))
17167     (while methods
17168       (gnus-read-descriptions-file (car methods))
17169       (setq methods (cdr methods)))
17170     t))
17171
17172 (defun gnus-read-descriptions-file (&optional method)
17173   (let ((method (or method gnus-select-method))
17174         group)
17175     (when (stringp method)
17176       (setq method (gnus-server-to-method method)))
17177     ;; We create the hashtable whether we manage to read the desc file
17178     ;; to avoid trying to re-read after a failed read.
17179     (or gnus-description-hashtb
17180         (setq gnus-description-hashtb
17181               (gnus-make-hashtable (length gnus-active-hashtb))))
17182     ;; Mark this method's desc file as read.
17183     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
17184                   gnus-description-hashtb)
17185
17186     (gnus-message 5 "Reading descriptions file via %s..." (car method))
17187     (cond
17188      ((not (gnus-check-server method))
17189       (gnus-message 1 "Couldn't open server")
17190       nil)
17191      ((not (gnus-request-list-newsgroups method))
17192       (gnus-message 1 "Couldn't read newsgroups descriptions")
17193       nil)
17194      (t
17195       (save-excursion
17196         (save-restriction
17197           (set-buffer nntp-server-buffer)
17198           (goto-char (point-min))
17199           (when (or (search-forward "\n.\n" nil t)
17200                     (goto-char (point-max)))
17201             (beginning-of-line)
17202             (narrow-to-region (point-min) (point)))
17203           ;; If these are groups from a foreign select method, we insert the
17204           ;; group prefix in front of the group names.
17205           (and method (not (gnus-server-equal
17206                             (gnus-server-get-method nil method)
17207                             (gnus-server-get-method nil gnus-select-method)))
17208                (let ((prefix (gnus-group-prefixed-name "" method)))
17209                  (goto-char (point-min))
17210                  (while (and (not (eobp))
17211                              (progn (insert prefix)
17212                                     (zerop (forward-line 1)))))))
17213           (goto-char (point-min))
17214           (while (not (eobp))
17215             ;; If we get an error, we set group to 0, which is not a
17216             ;; symbol...
17217             (setq group
17218                   (condition-case ()
17219                       (let ((obarray gnus-description-hashtb))
17220                         ;; Group is set to a symbol interned in this
17221                         ;; hash table.
17222                         (read nntp-server-buffer))
17223                     (error 0)))
17224             (skip-chars-forward " \t")
17225             ;; ...  which leads to this line being effectively ignored.
17226             (and (symbolp group)
17227                  (set group (buffer-substring
17228                              (point) (progn (end-of-line) (point)))))
17229             (forward-line 1))))
17230       (gnus-message 5 "Reading descriptions file...done")
17231       t))))
17232
17233 (defun gnus-group-get-description (group)
17234   "Get the description of a group by sending XGTITLE to the server."
17235   (when (gnus-request-group-description group)
17236     (save-excursion
17237       (set-buffer nntp-server-buffer)
17238       (goto-char (point-min))
17239       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17240         (match-string 1)))))
17241
17242 \f
17243 ;;;
17244 ;;; Buffering of read articles.
17245 ;;;
17246
17247 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17248 (defvar gnus-backlog-articles nil)
17249 (defvar gnus-backlog-hashtb nil)
17250
17251 (defun gnus-backlog-buffer ()
17252   "Return the backlog buffer."
17253   (or (get-buffer gnus-backlog-buffer)
17254       (save-excursion
17255         (set-buffer (get-buffer-create gnus-backlog-buffer))
17256         (buffer-disable-undo (current-buffer))
17257         (setq buffer-read-only t)
17258         (gnus-add-current-to-buffer-list)
17259         (get-buffer gnus-backlog-buffer))))
17260
17261 (defun gnus-backlog-setup ()
17262   "Initialize backlog variables."
17263   (unless gnus-backlog-hashtb
17264     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17265
17266 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17267
17268 (defun gnus-backlog-shutdown ()
17269   "Clear all backlog variables and buffers."
17270   (when (get-buffer gnus-backlog-buffer)
17271     (kill-buffer gnus-backlog-buffer))
17272   (setq gnus-backlog-hashtb nil
17273         gnus-backlog-articles nil))
17274
17275 (defun gnus-backlog-enter-article (group number buffer)
17276   (gnus-backlog-setup)
17277   (let ((ident (intern (concat group ":" (int-to-string number))
17278                        gnus-backlog-hashtb))
17279         b)
17280     (if (memq ident gnus-backlog-articles)
17281         () ; It's already kept.
17282       ;; Remove the oldest article, if necessary.
17283       (and (numberp gnus-keep-backlog)
17284            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17285            (gnus-backlog-remove-oldest-article))
17286       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17287       ;; Insert the new article.
17288       (save-excursion
17289         (set-buffer (gnus-backlog-buffer))
17290         (let (buffer-read-only)
17291           (goto-char (point-max))
17292           (or (bolp) (insert "\n"))
17293           (setq b (point))
17294           (insert-buffer-substring buffer)
17295           ;; Tag the beginning of the article with the ident.
17296           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17297
17298 (defun gnus-backlog-remove-oldest-article ()
17299   (save-excursion
17300     (set-buffer (gnus-backlog-buffer))
17301     (goto-char (point-min))
17302     (if (zerop (buffer-size))
17303         () ; The buffer is empty.
17304       (let ((ident (get-text-property (point) 'gnus-backlog))
17305             buffer-read-only)
17306         ;; Remove the ident from the list of articles.
17307         (when ident
17308           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17309         ;; Delete the article itself.
17310         (delete-region
17311          (point) (next-single-property-change
17312                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17313
17314 (defun gnus-backlog-remove-article (group number)
17315   "Remove article NUMBER in GROUP from the backlog."
17316   (when (numberp number)
17317     (gnus-backlog-setup)
17318     (let ((ident (intern (concat group ":" (int-to-string number))
17319                          gnus-backlog-hashtb))
17320           beg end)
17321       (when (memq ident gnus-backlog-articles)
17322         ;; It was in the backlog.
17323         (save-excursion
17324           (set-buffer (gnus-backlog-buffer))
17325           (let (buffer-read-only)
17326             (when (setq beg (text-property-any
17327                              (point-min) (point-max) 'gnus-backlog
17328                              ident))
17329               ;; Find the end (i. e., the beginning of the next article).
17330               (setq end
17331                     (next-single-property-change
17332                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17333               (delete-region beg end)
17334               ;; Return success.
17335               t)))))))
17336
17337 (defun gnus-backlog-request-article (group number buffer)
17338   (when (numberp number)
17339     (gnus-backlog-setup)
17340     (let ((ident (intern (concat group ":" (int-to-string number))
17341                          gnus-backlog-hashtb))
17342           beg end)
17343       (when (memq ident gnus-backlog-articles)
17344         ;; It was in the backlog.
17345         (save-excursion
17346           (set-buffer (gnus-backlog-buffer))
17347           (if (not (setq beg (text-property-any
17348                               (point-min) (point-max) 'gnus-backlog
17349                               ident)))
17350               ;; It wasn't in the backlog after all.
17351               (ignore
17352                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17353             ;; Find the end (i. e., the beginning of the next article).
17354             (setq end
17355                   (next-single-property-change
17356                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17357         (let ((buffer-read-only nil))
17358           (erase-buffer)
17359           (insert-buffer-substring gnus-backlog-buffer beg end)
17360           t)))))
17361
17362 ;; Allow redefinition of Gnus functions.
17363
17364 (gnus-ems-redefine)
17365
17366 (provide 'gnus)
17367
17368 ;;; gnus.el ends here