*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus-load)
29 (require 'gnus-group)
30 (require 'gnus-spec)
31 (require 'gnus-range)
32 (require 'gnus-int)
33 (require 'gnus)
34
35
36 (defvar gnus-kill-summary-on-exit t
37   "*If non-nil, kill the summary buffer when you exit from it.
38 If nil, the summary will become a \"*Dead Summary*\" buffer, and
39 it will be killed sometime later.")
40
41 (defvar gnus-fetch-old-headers nil
42   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
43 If an unread article in the group refers to an older, already read (or
44 just marked as read) article, the old article will not normally be
45 displayed in the Summary buffer.  If this variable is non-nil, Gnus
46 will attempt to grab the headers to the old articles, and thereby
47 build complete threads.  If it has the value `some', only enough
48 headers to connect otherwise loose threads will be displayed.
49 This variable can also be a number.  In that case, no more than that
50 number of old headers will be fetched.
51
52 The server has to support NOV for any of this to work.")
53
54 (defvar gnus-summary-make-false-root 'adopt
55   "*nil means that Gnus won't gather loose threads.
56 If the root of a thread has expired or been read in a previous
57 session, the information necessary to build a complete thread has been
58 lost.  Instead of having many small sub-threads from this original thread
59 scattered all over the summary buffer, Gnus can gather them.
60
61 If non-nil, Gnus will try to gather all loose sub-threads from an
62 original thread into one large thread.
63
64 If this variable is non-nil, it should be one of `none', `adopt',
65 `dummy' or `empty'.
66
67 If this variable is `none', Gnus will not make a false root, but just
68 present the sub-threads after another.
69 If this variable is `dummy', Gnus will create a dummy root that will
70 have all the sub-threads as children.
71 If this variable is `adopt', Gnus will make one of the \"children\"
72 the parent and mark all the step-children as such.
73 If this variable is `empty', the \"children\" are printed with empty
74 subject fields.  (Or rather, they will be printed with a string
75 given by the `gnus-summary-same-subject' variable.)")
76
77 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
78   "*A regexp to match subjects to be excluded from loose thread gathering.
79 As loose thread gathering is done on subjects only, that means that
80 there can be many false gatherings performed.  By rooting out certain
81 common subjects, gathering might become saner.")
82
83 (defvar gnus-summary-gather-subject-limit nil
84   "*Maximum length of subject comparisons when gathering loose threads.
85 Use nil to compare full subjects.  Setting this variable to a low
86 number will help gather threads that have been corrupted by
87 newsreaders chopping off subject lines, but it might also mean that
88 unrelated articles that have subject that happen to begin with the
89 same few characters will be incorrectly gathered.
90
91 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
92 comparing subjects.")
93
94 (defvar gnus-simplify-ignored-prefixes nil
95   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily.")
96
97 (defvar gnus-build-sparse-threads nil
98   "*If non-nil, fill in the gaps in threads.
99 If `some', only fill in the gaps that are needed to tie loose threads
100 together.  If `more', fill in all leaf nodes that Gnus can find.  If
101 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
102
103 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
104   "Function used for gathering loose threads.
105 There are two pre-defined functions: `gnus-gather-threads-by-subject',
106 which only takes Subjects into consideration; and
107 `gnus-gather-threads-by-references', which compared the References
108 headers of the articles to find matches.")
109
110 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
111 (defvar gnus-summary-same-subject ""
112   "*String indicating that the current article has the same subject as the previous.
113 This variable will only be used if the value of
114 `gnus-summary-make-false-root' is `empty'.")
115
116 (defvar gnus-summary-goto-unread t
117   "*If non-nil, marking commands will go to the next unread article.
118 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
119 whether it is read or not.")
120
121 (defvar gnus-summary-default-score 0
122   "*Default article score level.
123 If this variable is nil, scoring will be disabled.")
124
125 (defvar gnus-summary-zcore-fuzz 0
126   "*Fuzziness factor for the zcore in the summary buffer.
127 Articles with scores closer than this to `gnus-summary-default-score'
128 will not be marked.")
129
130 (defvar gnus-simplify-subject-fuzzy-regexp nil
131   "*Strings to be removed when doing fuzzy matches.
132 This can either be a regular expression or list of regular expressions
133 that will be removed from subject strings if fuzzy subject
134 simplification is selected.")
135
136 (defvar gnus-show-threads t
137   "*If non-nil, display threads in summary mode.")
138
139 (defvar gnus-thread-hide-subtree nil
140   "*If non-nil, hide all threads initially.
141 If threads are hidden, you have to run the command
142 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
143 to expose hidden threads.")
144
145 (defvar gnus-thread-hide-killed t
146   "*If non-nil, hide killed threads automatically.")
147
148 (defvar gnus-thread-ignore-subject nil
149   "*If non-nil, ignore subjects and do all threading based on the Reference header.
150 If nil, which is the default, articles that have different subjects
151 from their parents will start separate threads.")
152
153 (defvar gnus-thread-operation-ignore-subject t
154   "*If non-nil, subjects will be ignored when doing thread commands.
155 This affects commands like `gnus-summary-kill-thread' and
156 `gnus-summary-lower-thread'.
157
158 If this variable is nil, articles in the same thread with different
159 subjects will not be included in the operation in question.  If this
160 variable is `fuzzy', only articles that have subjects that are fuzzily
161 equal will be included.")
162
163 (defvar gnus-thread-indent-level 4
164   "*Number that says how much each sub-thread should be indented.")
165
166 (defvar gnus-auto-extend-newsgroup t
167   "*If non-nil, extend newsgroup forward and backward when requested.")
168
169 (defvar gnus-auto-select-first t
170   "*If nil, don't select the first unread article when entering a group.
171 If this variable is `best', select the highest-scored unread article
172 in the group.  If neither nil nor `best', select the first unread
173 article.
174
175 If you want to prevent automatic selection of the first unread article
176 in some newsgroups, set the variable to nil in
177 `gnus-select-group-hook'.")
178
179 (defvar gnus-auto-select-next t
180   "*If non-nil, offer to go to the next group from the end of the previous.
181 If the value is t and the next newsgroup is empty, Gnus will exit
182 summary mode and go back to group mode.  If the value is neither nil
183 nor t, Gnus will select the following unread newsgroup.  In
184 particular, if the value is the symbol `quietly', the next unread
185 newsgroup will be selected without any confirmation, and if it is
186 `almost-quietly', the next group will be selected without any
187 confirmation if you are located on the last article in the group.
188 Finally, if this variable is `slightly-quietly', the `Z n' command
189 will go to the next group without confirmation.")
190
191 (defvar gnus-auto-select-same nil
192   "*If non-nil, select the next article with the same subject.")
193
194 (defvar gnus-summary-check-current nil
195   "*If non-nil, consider the current article when moving.
196 The \"unread\" movement commands will stay on the same line if the
197 current article is unread.")
198
199 (defvar gnus-auto-center-summary t
200   "*If non-nil, always center the current summary buffer.
201 In particular, if `vertical' do only vertical recentering.  If non-nil
202 and non-`vertical', do both horizontal and vertical recentering.")
203
204 (defvar gnus-show-all-headers nil
205   "*If non-nil, don't hide any headers.")
206
207 (defvar gnus-single-article-buffer t
208   "*If non-nil, display all articles in the same buffer.
209 If nil, each group will get its own article buffer.")
210
211 (defvar gnus-break-pages t
212   "*If non-nil, do page breaking on articles.
213 The page delimiter is specified by the `gnus-page-delimiter'
214 variable.")
215
216 (defvar gnus-show-mime nil
217   "*If non-nil, do mime processing of articles.
218 The articles will simply be fed to the function given by
219 `gnus-show-mime-method'.")
220
221 (defvar gnus-move-split-methods nil
222   "*Variable used to suggest where articles are to be moved to.
223 It uses the same syntax as the `gnus-split-methods' variable.")
224
225 ;; Mark variables suggested by Thomas Michanek
226 ;; <Thomas.Michanek@telelogic.se>.
227 (defvar gnus-unread-mark ? 
228   "*Mark used for unread articles.")
229 (defvar gnus-ticked-mark ?!
230   "*Mark used for ticked articles.")
231 (defvar gnus-dormant-mark ??
232   "*Mark used for dormant articles.")
233 (defvar gnus-del-mark ?r
234   "*Mark used for del'd articles.")
235 (defvar gnus-read-mark ?R
236   "*Mark used for read articles.")
237 (defvar gnus-expirable-mark ?E
238   "*Mark used for expirable articles.")
239 (defvar gnus-killed-mark ?K
240   "*Mark used for killed articles.")
241 (defvar gnus-souped-mark ?F
242   "*Mark used for killed articles.")
243 (defvar gnus-kill-file-mark ?X
244   "*Mark used for articles killed by kill files.")
245 (defvar gnus-low-score-mark ?Y
246   "*Mark used for articles with a low score.")
247 (defvar gnus-catchup-mark ?C
248   "*Mark used for articles that are caught up.")
249 (defvar gnus-replied-mark ?A
250   "*Mark used for articles that have been replied to.")
251 (defvar gnus-cached-mark ?*
252   "*Mark used for articles that are in the cache.")
253 (defvar gnus-saved-mark ?S
254   "*Mark used for articles that have been saved to.")
255 (defvar gnus-ancient-mark ?O
256   "*Mark used for ancient articles.")
257 (defvar gnus-sparse-mark ?Q
258   "*Mark used for sparsely reffed articles.")
259 (defvar gnus-canceled-mark ?G
260   "*Mark used for canceled articles.")
261 (defvar gnus-score-over-mark ?+
262   "*Score mark used for articles with high scores.")
263 (defvar gnus-score-below-mark ?-
264   "*Score mark used for articles with low scores.")
265 (defvar gnus-empty-thread-mark ? 
266   "*There is no thread under the article.")
267 (defvar gnus-not-empty-thread-mark ?=
268   "*There is a thread under the article.")
269
270 (defvar gnus-view-pseudo-asynchronously nil
271   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
272
273 (defvar gnus-view-pseudos nil
274   "*If `automatic', pseudo-articles will be viewed automatically.
275 If `not-confirm', pseudos will be viewed automatically, and the user
276 will not be asked to confirm the command.")
277
278 (defvar gnus-view-pseudos-separately t
279   "*If non-nil, one pseudo-article will be created for each file to be viewed.
280 If nil, all files that use the same viewing command will be given as a
281 list of parameters to that command.")
282
283 (defvar gnus-insert-pseudo-articles t
284   "*If non-nil, insert pseudo-articles when decoding articles.")
285
286 (defvar gnus-summary-line-format "%U\%R\%z\%I\%(%[%4L: %-20,20n%]%) %s\n"
287   "*The format specification of the lines in the summary buffer.
288
289 It works along the same lines as a normal formatting string,
290 with some simple extensions.
291
292 %N   Article number, left padded with spaces (string)
293 %S   Subject (string)
294 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
295 %n   Name of the poster (string)
296 %a   Extracted name of the poster (string)
297 %A   Extracted address of the poster (string)
298 %F   Contents of the From: header (string)
299 %x   Contents of the Xref: header (string)
300 %D   Date of the article (string)
301 %d   Date of the article (string) in DD-MMM format
302 %M   Message-id of the article (string)
303 %r   References of the article (string)
304 %c   Number of characters in the article (integer)
305 %L   Number of lines in the article (integer)
306 %I   Indentation based on thread level (a string of spaces)
307 %T   A string with two possible values: 80 spaces if the article
308      is on thread level two or larger and 0 spaces on level one
309 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
310 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
311 %[   Opening bracket (character, \"[\" or \"<\")
312 %]   Closing bracket (character, \"]\" or \">\")
313 %>   Spaces of length thread-level (string)
314 %<   Spaces of length (- 20 thread-level) (string)
315 %i   Article score (number)
316 %z   Article zcore (character)
317 %t   Number of articles under the current thread (number).
318 %e   Whether the thread is empty or not (character).
319 %l   GroupLens score (string).
320 %u   User defined specifier.  The next character in the format string should
321      be a letter.  Gnus will call the function gnus-user-format-function-X,
322      where X is the letter following %u.  The function will be passed the
323      current header as argument.  The function should return a string, which
324      will be inserted into the summary just like information from any other
325      summary specifier.
326
327 Text between %( and %) will be highlighted with `gnus-mouse-face'
328 when the mouse point is placed inside the area.  There can only be one
329 such area.
330
331 The %U (status), %R (replied) and %z (zcore) specs have to be handled
332 with care.  For reasons of efficiency, Gnus will compute what column
333 these characters will end up in, and \"hard-code\" that.  This means that
334 it is illegal to have these specs after a variable-length spec.  Well,
335 you might not be arrested, but your summary buffer will look strange,
336 which is bad enough.
337
338 The smart choice is to have these specs as for to the left as
339 possible.
340
341 This restriction may disappear in later versions of Gnus.")
342
343 (defvar gnus-summary-dummy-line-format
344   "*  %(:                          :%) %S\n"
345   "*The format specification for the dummy roots in the summary buffer.
346 It works along the same lines as a normal formatting string,
347 with some simple extensions.
348
349 %S  The subject")
350
351 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
352   "*The format specification for the summary mode line.
353 It works along the same lines as a normal formatting string,
354 with some simple extensions:
355
356 %G  Group name
357 %p  Unprefixed group name
358 %A  Current article number
359 %V  Gnus version
360 %U  Number of unread articles in the group
361 %e  Number of unselected articles in the group
362 %Z  A string with unread/unselected article counts
363 %g  Shortish group name
364 %S  Subject of the current article
365 %u  User-defined spec
366 %s  Current score file name
367 %d  Number of dormant articles
368 %r  Number of articles that have been marked as read in this session
369 %E  Number of articles expunged by the score files")
370
371 (defvar gnus-summary-mark-below 0
372   "*Mark all articles with a score below this variable as read.
373 This variable is local to each summary buffer and usually set by the
374 score file.")
375
376 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
377   "*List of functions used for sorting articles in the summary buffer.
378 This variable is only used when not using a threaded display.")
379
380 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
381   "*List of functions used for sorting threads in the summary buffer.
382 By default, threads are sorted by article number.
383
384 Each function takes two threads and return non-nil if the first thread
385 should be sorted before the other.  If you use more than one function,
386 the primary sort function should be the last.  You should probably
387 always include `gnus-thread-sort-by-number' in the list of sorting
388 functions -- preferably first.
389
390 Ready-mady functions include `gnus-thread-sort-by-number',
391 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
392 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
393 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
394
395 (defvar gnus-thread-score-function '+
396   "*Function used for calculating the total score of a thread.
397
398 The function is called with the scores of the article and each
399 subthread and should then return the score of the thread.
400
401 Some functions you can use are `+', `max', or `min'.")
402
403 (defvar gnus-summary-expunge-below nil
404   "All articles that have a score less than this variable will be expunged.")
405
406 (defvar gnus-thread-expunge-below nil
407   "All threads that have a total score less than this variable will be expunged.
408 See `gnus-thread-score-function' for en explanation of what a
409 \"thread score\" is.")
410
411 (defvar gnus-summary-mode-hook nil
412   "*A hook for Gnus summary mode.
413 This hook is run before any variables are set in the summary buffer.")
414
415 (defvar gnus-summary-exit-hook nil
416   "*A hook called on exit from the summary buffer.")
417
418 (defvar gnus-summary-prepare-hook nil
419   "*A hook called after the summary buffer has been generated.
420 If you want to modify the summary buffer, you can use this hook.")
421
422 (defvar gnus-summary-generate-hook nil
423   "*A hook run just before generating the summary buffer.
424 This hook is commonly used to customize threading variables and the
425 like.")
426
427 (defvar gnus-select-group-hook nil
428   "*A hook called when a newsgroup is selected.
429
430 If you'd like to simplify subjects like the
431 `gnus-summary-next-same-subject' command does, you can use the
432 following hook:
433
434  (setq gnus-select-group-hook
435       (list
436         (lambda ()
437           (mapcar (lambda (header)
438                      (mail-header-set-subject
439                       header
440                       (gnus-simplify-subject
441                        (mail-header-subject header) 're-only)))
442                   gnus-newsgroup-headers))))")
443
444 (defvar gnus-select-article-hook nil
445   "*A hook called when an article is selected.")
446
447 (defvar gnus-visual-mark-article-hook
448   (list 'gnus-highlight-selected-summary)
449   "*Hook run after selecting an article in the summary buffer.
450 It is meant to be used for highlighting the article in some way.  It
451 is not run if `gnus-visual' is nil.")
452
453 (defvar gnus-parse-headers-hook nil
454   "*A hook called before parsing the headers.")
455 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
456
457 (defvar gnus-exit-group-hook nil
458   "*A hook called when exiting (not quitting) summary mode.")
459
460 (defvar gnus-summary-update-hook
461   (list 'gnus-summary-highlight-line)
462   "*A hook called when a summary line is changed.
463 The hook will not be called if `gnus-visual' is nil.
464
465 The default function `gnus-summary-highlight-line' will
466 highlight the line according to the `gnus-summary-highlight'
467 variable.")
468
469 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
470   "*A hook called when an article is selected for the first time.
471 The hook is intended to mark an article as read (or unread)
472 automatically when it is selected.")
473
474 (defvar gnus-summary-display-table 
475   ;; Change the display table.  Odd characters have a tendency to mess
476   ;; up nicely formatted displays - we make all possible glyphs
477   ;; display only a single character.
478
479   ;; We start from the standard display table, if any.
480   (let ((table (or (copy-sequence standard-display-table)
481                    (make-display-table)))
482         ;; Nix out all the control chars...
483         (i 32))
484     (while (>= (setq i (1- i)) 0)
485       (aset table i [??]))
486     ;; ... but not newline and cr, of course. (cr is necessary for the
487     ;; selective display).
488     (aset table ?\n nil)
489     (aset table ?\r nil)
490     ;; We nix out any glyphs over 126 that are not set already.
491     (let ((i 256))
492       (while (>= (setq i (1- i)) 127)
493         ;; Only modify if the entry is nil.
494         (or (aref table i)
495             (aset table i [??]))))
496     table)
497   "Display table used in summary mode buffers.")
498
499 ;;; Internal variables
500
501 (defvar gnus-original-article-buffer " *Original Article*")
502 (defvar gnus-original-article nil)
503 (defvar gnus-article-internal-prepare-hook nil)
504
505 (defvar gnus-thread-indent-array nil)
506 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
507
508 ;; Avoid highlighting in kill files.
509 (defvar gnus-summary-inhibit-highlight nil)
510 (defvar gnus-newsgroup-selected-overlay nil)
511 (defvar gnus-inhibit-limiting nil)
512 (defvar gnus-newsgroup-adaptive-score-file nil)
513 (defvar gnus-current-score-file nil)
514 (defvar gnus-current-move-group nil)
515 (defvar gnus-current-copy-group nil)
516 (defvar gnus-current-crosspost-group nil)
517
518 (defvar gnus-newsgroup-dependencies nil)
519 (defvar gnus-newsgroup-adaptive nil)
520 (defvar gnus-summary-display-article-function nil)
521 (defvar gnus-summary-highlight-line-function nil
522   "Function called after highlighting a summary line.")
523
524 (defvar gnus-summary-line-format-alist
525   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
526     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
527     (?s gnus-tmp-subject-or-nil ?s)
528     (?n gnus-tmp-name ?s)
529     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
530         ?s)
531     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
532             gnus-tmp-from) ?s)
533     (?F gnus-tmp-from ?s)
534     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
535     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
536     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
537     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
538     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
539     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
540     (?L gnus-tmp-lines ?d)
541     (?I gnus-tmp-indentation ?s)
542     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
543     (?R gnus-tmp-replied ?c)
544     (?\[ gnus-tmp-opening-bracket ?c)
545     (?\] gnus-tmp-closing-bracket ?c)
546     (?\> (make-string gnus-tmp-level ? ) ?s)
547     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
548     (?i gnus-tmp-score ?d)
549     (?z gnus-tmp-score-char ?c)
550     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
551     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
552     (?U gnus-tmp-unread ?c)
553     (?t (gnus-summary-number-of-articles-in-thread
554          (and (boundp 'thread) (car thread)) gnus-tmp-level)
555         ?d)
556     (?e (gnus-summary-number-of-articles-in-thread
557          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
558         ?c)
559     (?u gnus-tmp-user-defined ?s))
560   "An alist of format specifications that can appear in summary lines,
561 and what variables they correspond with, along with the type of the
562 variable (string, integer, character, etc).")
563
564 (defvar gnus-summary-dummy-line-format-alist
565   `((?S gnus-tmp-subject ?s)
566     (?N gnus-tmp-number ?d)
567     (?u gnus-tmp-user-defined ?s)))
568
569 (defvar gnus-summary-mode-line-format-alist
570   `((?G gnus-tmp-group-name ?s)
571     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
572     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
573     (?A gnus-tmp-article-number ?d)
574     (?Z gnus-tmp-unread-and-unselected ?s)
575     (?V gnus-version ?s)
576     (?U gnus-tmp-unread-and-unticked ?d)
577     (?S gnus-tmp-subject ?s)
578     (?e gnus-tmp-unselected ?d)
579     (?u gnus-tmp-user-defined ?s)
580     (?d (length gnus-newsgroup-dormant) ?d)
581     (?t (length gnus-newsgroup-marked) ?d)
582     (?r (length gnus-newsgroup-reads) ?d)
583     (?E gnus-newsgroup-expunged-tally ?d)
584     (?s (gnus-current-score-file-nondirectory) ?s)))
585
586 (defvar gnus-last-search-regexp nil
587   "Default regexp for article search command.")
588
589 (defvar gnus-last-shell-command nil
590   "Default shell command on article.")
591
592 (defvar gnus-newsgroup-begin nil)
593 (defvar gnus-newsgroup-end nil)
594 (defvar gnus-newsgroup-last-rmail nil)
595 (defvar gnus-newsgroup-last-mail nil)
596 (defvar gnus-newsgroup-last-folder nil)
597 (defvar gnus-newsgroup-last-file nil)
598 (defvar gnus-newsgroup-auto-expire nil)
599 (defvar gnus-newsgroup-active nil)
600
601 (defvar gnus-newsgroup-data nil)
602 (defvar gnus-newsgroup-data-reverse nil)
603 (defvar gnus-newsgroup-limit nil)
604 (defvar gnus-newsgroup-limits nil)
605
606 (defvar gnus-newsgroup-unreads nil
607   "List of unread articles in the current newsgroup.")
608
609 (defvar gnus-newsgroup-unselected nil
610   "List of unselected unread articles in the current newsgroup.")
611
612 (defvar gnus-newsgroup-reads nil
613   "Alist of read articles and article marks in the current newsgroup.")
614
615 (defvar gnus-newsgroup-expunged-tally nil)
616
617 (defvar gnus-newsgroup-marked nil
618   "List of ticked articles in the current newsgroup (a subset of unread art).")
619
620 (defvar gnus-newsgroup-killed nil
621   "List of ranges of articles that have been through the scoring process.")
622
623 (defvar gnus-newsgroup-cached nil
624   "List of articles that come from the article cache.")
625
626 (defvar gnus-newsgroup-saved nil
627   "List of articles that have been saved.")
628
629 (defvar gnus-newsgroup-kill-headers nil)
630
631 (defvar gnus-newsgroup-replied nil
632   "List of articles that have been replied to in the current newsgroup.")
633
634 (defvar gnus-newsgroup-expirable nil
635   "List of articles in the current newsgroup that can be expired.")
636
637 (defvar gnus-newsgroup-processable nil
638   "List of articles in the current newsgroup that can be processed.")
639
640 (defvar gnus-newsgroup-bookmarks nil
641   "List of articles in the current newsgroup that have bookmarks.")
642
643 (defvar gnus-newsgroup-dormant nil
644   "List of dormant articles in the current newsgroup.")
645
646 (defvar gnus-newsgroup-scored nil
647   "List of scored articles in the current newsgroup.")
648
649 (defvar gnus-newsgroup-headers nil
650   "List of article headers in the current newsgroup.")
651
652 (defvar gnus-newsgroup-threads nil)
653
654 (defvar gnus-newsgroup-prepared nil
655   "Whether the current group has been prepared properly.")
656
657 (defvar gnus-newsgroup-ancient nil
658   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
659
660 (defvar gnus-newsgroup-sparse nil)
661
662 (defvar gnus-current-article nil)
663 (defvar gnus-article-current nil)
664 (defvar gnus-current-headers nil)
665 (defvar gnus-have-all-headers nil)
666 (defvar gnus-last-article nil)
667 (defvar gnus-newsgroup-history nil)
668
669 (defconst gnus-summary-local-variables
670   '(gnus-newsgroup-name
671     gnus-newsgroup-begin gnus-newsgroup-end
672     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
673     gnus-newsgroup-last-folder gnus-newsgroup-last-file
674     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
675     gnus-newsgroup-unselected gnus-newsgroup-marked
676     gnus-newsgroup-reads gnus-newsgroup-saved
677     gnus-newsgroup-replied gnus-newsgroup-expirable
678     gnus-newsgroup-processable gnus-newsgroup-killed
679     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
680     gnus-newsgroup-headers gnus-newsgroup-threads
681     gnus-newsgroup-prepared gnus-summary-highlight-line-function
682     gnus-current-article gnus-current-headers gnus-have-all-headers
683     gnus-last-article gnus-article-internal-prepare-hook
684     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
685     gnus-newsgroup-scored gnus-newsgroup-kill-headers
686     gnus-thread-expunge-below
687     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
688     (gnus-summary-mark-below . global)
689     gnus-newsgroup-active gnus-scores-exclude-files
690     gnus-newsgroup-history gnus-newsgroup-ancient
691     gnus-newsgroup-sparse
692     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
693     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
694     (gnus-newsgroup-expunged-tally . 0)
695     gnus-cache-removable-articles gnus-newsgroup-cached
696     gnus-newsgroup-data gnus-newsgroup-data-reverse
697     gnus-newsgroup-limit gnus-newsgroup-limits)
698   "Variables that are buffer-local to the summary buffers.")
699
700 ;; Byte-compiler warning.
701 (defvar gnus-article-mode-map)
702
703 ;; Subject simplification.
704
705 (defsubst gnus-simplify-subject-re (subject)
706   "Remove \"Re:\" from subject lines."
707   (if (string-match "^[Rr][Ee]: *" subject)
708       (substring subject (match-end 0))
709     subject))
710
711 (defun gnus-simplify-subject (subject &optional re-only)
712   "Remove `Re:' and words in parentheses.
713 If RE-ONLY is non-nil, strip leading `Re:'s only."
714   (let ((case-fold-search t))           ;Ignore case.
715     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
716     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
717       (setq subject (substring subject (match-end 0))))
718     ;; Remove uninteresting prefixes.
719     (if (and (not re-only)
720              gnus-simplify-ignored-prefixes
721              (string-match gnus-simplify-ignored-prefixes subject))
722         (setq subject (substring subject (match-end 0))))
723     ;; Remove words in parentheses from end.
724     (unless re-only
725       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
726         (setq subject (substring subject 0 (match-beginning 0)))))
727     ;; Return subject string.
728     subject))
729
730 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
731 ;; all whitespace.
732 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
733 (defun gnus-simplify-buffer-fuzzy ()
734   (let ((case-fold-search t))
735     (goto-char (point-min))
736     (while (search-forward "\t" nil t)
737       (replace-match " " t t))
738     (goto-char (point-min))
739     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
740     (goto-char (match-beginning 0))
741     (while (or
742             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
743             (looking-at "^[[].*: .*[]]$"))
744       (goto-char (point-min))
745       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
746                                 nil t)
747         (replace-match "" t t))
748       (goto-char (point-min))
749       (while (re-search-forward "^[[].*: .*[]]$" nil t)
750         (goto-char (match-end 0))
751         (delete-char -1)
752         (delete-region
753          (progn (goto-char (match-beginning 0)))
754          (re-search-forward ":"))))
755     (goto-char (point-min))
756     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
757       (replace-match "" t t))
758     (goto-char (point-min))
759     (while (re-search-forward "  +" nil t)
760       (replace-match " " t t))
761     (goto-char (point-min))
762     (while (re-search-forward " $" nil t)
763       (replace-match "" t t))
764     (goto-char (point-min))
765     (while (re-search-forward "^ +" nil t)
766       (replace-match "" t t))
767     (goto-char (point-min))
768     (when gnus-simplify-subject-fuzzy-regexp
769       (if (listp gnus-simplify-subject-fuzzy-regexp)
770           (let ((list gnus-simplify-subject-fuzzy-regexp))
771             (while list
772               (goto-char (point-min))
773               (while (re-search-forward (car list) nil t)
774                 (replace-match "" t t))
775               (setq list (cdr list))))
776         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
777           (replace-match "" t t))))))
778
779 (defun gnus-simplify-subject-fuzzy (subject)
780   "Siplify a subject string fuzzily."
781   (save-excursion
782     (gnus-set-work-buffer)
783     (let ((case-fold-search t))
784       (insert subject)
785       (inline (gnus-simplify-buffer-fuzzy))
786       (buffer-string))))
787
788 (defsubst gnus-simplify-subject-fully (subject)
789   "Simplify a subject string according to the user's wishes."
790   (cond
791    ((null gnus-summary-gather-subject-limit)
792     (gnus-simplify-subject-re subject))
793    ((eq gnus-summary-gather-subject-limit 'fuzzy)
794     (gnus-simplify-subject-fuzzy subject))
795    ((numberp gnus-summary-gather-subject-limit)
796     (gnus-limit-string (gnus-simplify-subject-re subject)
797                        gnus-summary-gather-subject-limit))
798    (t
799     subject)))
800
801 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
802   "Check whether two subjects are equal.  If optional argument
803 simple-first is t, first argument is already simplified."
804   (cond
805    ((null simple-first)
806     (equal (gnus-simplify-subject-fully s1)
807            (gnus-simplify-subject-fully s2)))
808    (t
809     (equal s1
810            (gnus-simplify-subject-fully s2)))))
811
812 (defun gnus-offer-save-summaries ()
813   "Offer to save all active summary buffers."
814   (save-excursion
815     (let ((buflist (buffer-list))
816           buffers bufname)
817       ;; Go through all buffers and find all summaries.
818       (while buflist
819         (and (setq bufname (buffer-name (car buflist)))
820              (string-match "Summary" bufname)
821              (save-excursion
822                (set-buffer bufname)
823                ;; We check that this is, indeed, a summary buffer.
824                (and (eq major-mode 'gnus-summary-mode)
825                     ;; Also make sure this isn't bogus.
826                     gnus-newsgroup-prepared))
827              (push bufname buffers))
828         (setq buflist (cdr buflist)))
829       ;; Go through all these summary buffers and offer to save them.
830       (when buffers
831         (map-y-or-n-p
832          "Update summary buffer %s? "
833          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
834          buffers)))))
835
836 (defun gnus-summary-bubble-group ()
837   "Increase the score of the current group.
838 This is a handy function to add to `gnus-summary-exit-hook' to
839 increase the score of each group you read."
840   (gnus-group-add-score gnus-newsgroup-name))
841
842 \f
843 ;;;
844 ;;; Gnus summary mode
845 ;;;
846
847 (put 'gnus-summary-mode 'mode-class 'special)
848
849 (when t
850   ;; Non-orthogonal keys
851
852   (gnus-define-keys gnus-summary-mode-map
853     " " gnus-summary-next-page
854     "\177" gnus-summary-prev-page
855     [delete] gnus-summary-prev-page
856     "\r" gnus-summary-scroll-up
857     "n" gnus-summary-next-unread-article
858     "p" gnus-summary-prev-unread-article
859     "N" gnus-summary-next-article
860     "P" gnus-summary-prev-article
861     "\M-\C-n" gnus-summary-next-same-subject
862     "\M-\C-p" gnus-summary-prev-same-subject
863     "\M-n" gnus-summary-next-unread-subject
864     "\M-p" gnus-summary-prev-unread-subject
865     "." gnus-summary-first-unread-article
866     "," gnus-summary-best-unread-article
867     "\M-s" gnus-summary-search-article-forward
868     "\M-r" gnus-summary-search-article-backward
869     "<" gnus-summary-beginning-of-article
870     ">" gnus-summary-end-of-article
871     "j" gnus-summary-goto-article
872     "^" gnus-summary-refer-parent-article
873     "\M-^" gnus-summary-refer-article
874     "u" gnus-summary-tick-article-forward
875     "!" gnus-summary-tick-article-forward
876     "U" gnus-summary-tick-article-backward
877     "d" gnus-summary-mark-as-read-forward
878     "D" gnus-summary-mark-as-read-backward
879     "E" gnus-summary-mark-as-expirable
880     "\M-u" gnus-summary-clear-mark-forward
881     "\M-U" gnus-summary-clear-mark-backward
882     "k" gnus-summary-kill-same-subject-and-select
883     "\C-k" gnus-summary-kill-same-subject
884     "\M-\C-k" gnus-summary-kill-thread
885     "\M-\C-l" gnus-summary-lower-thread
886     "e" gnus-summary-edit-article
887     "#" gnus-summary-mark-as-processable
888     "\M-#" gnus-summary-unmark-as-processable
889     "\M-\C-t" gnus-summary-toggle-threads
890     "\M-\C-s" gnus-summary-show-thread
891     "\M-\C-h" gnus-summary-hide-thread
892     "\M-\C-f" gnus-summary-next-thread
893     "\M-\C-b" gnus-summary-prev-thread
894     "\M-\C-u" gnus-summary-up-thread
895     "\M-\C-d" gnus-summary-down-thread
896     "&" gnus-summary-execute-command
897     "c" gnus-summary-catchup-and-exit
898     "\C-w" gnus-summary-mark-region-as-read
899     "\C-t" gnus-summary-toggle-truncation
900     "?" gnus-summary-mark-as-dormant
901     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
902     "\C-c\C-s\C-n" gnus-summary-sort-by-number
903     "\C-c\C-s\C-a" gnus-summary-sort-by-author
904     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
905     "\C-c\C-s\C-d" gnus-summary-sort-by-date
906     "\C-c\C-s\C-i" gnus-summary-sort-by-score
907     "=" gnus-summary-expand-window
908     "\C-x\C-s" gnus-summary-reselect-current-group
909     "\M-g" gnus-summary-rescan-group
910     "w" gnus-summary-stop-page-breaking
911     "\C-c\C-r" gnus-summary-caesar-message
912     "\M-t" gnus-summary-toggle-mime
913     "f" gnus-summary-followup
914     "F" gnus-summary-followup-with-original
915     "C" gnus-summary-cancel-article
916     "r" gnus-summary-reply
917     "R" gnus-summary-reply-with-original
918     "\C-c\C-f" gnus-summary-mail-forward
919     "o" gnus-summary-save-article
920     "\C-o" gnus-summary-save-article-mail
921     "|" gnus-summary-pipe-output
922     "\M-k" gnus-summary-edit-local-kill
923     "\M-K" gnus-summary-edit-global-kill
924     "V" gnus-version
925     "\C-c\C-d" gnus-summary-describe-group
926     "q" gnus-summary-exit
927     "Q" gnus-summary-exit-no-update
928     "\C-c\C-i" gnus-info-find-node
929     gnus-mouse-2 gnus-mouse-pick-article
930     "m" gnus-summary-mail-other-window
931     "a" gnus-summary-post-news
932     "x" gnus-summary-limit-to-unread
933     "s" gnus-summary-isearch-article
934     "t" gnus-article-hide-headers
935     "g" gnus-summary-show-article
936     "l" gnus-summary-goto-last-article
937     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
938     "\C-d" gnus-summary-enter-digest-group
939     "\C-c\C-b" gnus-bug
940     "*" gnus-cache-enter-article
941     "\M-*" gnus-cache-remove-article
942     "\M-&" gnus-summary-universal-argument
943     "\C-l" gnus-recenter
944     "I" gnus-summary-increase-score
945     "L" gnus-summary-lower-score
946
947     "V" gnus-summary-score-map
948     "X" gnus-uu-extract-map
949     "S" gnus-summary-send-map)
950
951   ;; Sort of orthogonal keymap
952   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
953     "t" gnus-summary-tick-article-forward
954     "!" gnus-summary-tick-article-forward
955     "d" gnus-summary-mark-as-read-forward
956     "r" gnus-summary-mark-as-read-forward
957     "c" gnus-summary-clear-mark-forward
958     " " gnus-summary-clear-mark-forward
959     "e" gnus-summary-mark-as-expirable
960     "x" gnus-summary-mark-as-expirable
961     "?" gnus-summary-mark-as-dormant
962     "b" gnus-summary-set-bookmark
963     "B" gnus-summary-remove-bookmark
964     "#" gnus-summary-mark-as-processable
965     "\M-#" gnus-summary-unmark-as-processable
966     "S" gnus-summary-limit-include-expunged
967     "C" gnus-summary-catchup
968     "H" gnus-summary-catchup-to-here
969     "\C-c" gnus-summary-catchup-all
970     "k" gnus-summary-kill-same-subject-and-select
971     "K" gnus-summary-kill-same-subject
972     "P" gnus-uu-mark-map)
973
974   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
975     "c" gnus-summary-clear-above
976     "u" gnus-summary-tick-above
977     "m" gnus-summary-mark-above
978     "k" gnus-summary-kill-below)
979
980   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
981     "/" gnus-summary-limit-to-subject
982     "n" gnus-summary-limit-to-articles
983     "w" gnus-summary-pop-limit
984     "s" gnus-summary-limit-to-subject
985     "a" gnus-summary-limit-to-author
986     "u" gnus-summary-limit-to-unread
987     "m" gnus-summary-limit-to-marks
988     "v" gnus-summary-limit-to-score
989     "D" gnus-summary-limit-include-dormant
990     "d" gnus-summary-limit-exclude-dormant
991     ;;  "t" gnus-summary-limit-exclude-thread
992     "E" gnus-summary-limit-include-expunged
993     "c" gnus-summary-limit-exclude-childless-dormant
994     "C" gnus-summary-limit-mark-excluded-as-read)
995
996   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
997     "n" gnus-summary-next-unread-article
998     "p" gnus-summary-prev-unread-article
999     "N" gnus-summary-next-article
1000     "P" gnus-summary-prev-article
1001     "\C-n" gnus-summary-next-same-subject
1002     "\C-p" gnus-summary-prev-same-subject
1003     "\M-n" gnus-summary-next-unread-subject
1004     "\M-p" gnus-summary-prev-unread-subject
1005     "f" gnus-summary-first-unread-article
1006     "b" gnus-summary-best-unread-article
1007     "j" gnus-summary-goto-article
1008     "g" gnus-summary-goto-subject
1009     "l" gnus-summary-goto-last-article
1010     "p" gnus-summary-pop-article)
1011
1012   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1013     "k" gnus-summary-kill-thread
1014     "l" gnus-summary-lower-thread
1015     "i" gnus-summary-raise-thread
1016     "T" gnus-summary-toggle-threads
1017     "t" gnus-summary-rethread-current
1018     "^" gnus-summary-reparent-thread
1019     "s" gnus-summary-show-thread
1020     "S" gnus-summary-show-all-threads
1021     "h" gnus-summary-hide-thread
1022     "H" gnus-summary-hide-all-threads
1023     "n" gnus-summary-next-thread
1024     "p" gnus-summary-prev-thread
1025     "u" gnus-summary-up-thread
1026     "o" gnus-summary-top-thread
1027     "d" gnus-summary-down-thread
1028     "#" gnus-uu-mark-thread
1029     "\M-#" gnus-uu-unmark-thread)
1030
1031   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1032     "c" gnus-summary-catchup-and-exit
1033     "C" gnus-summary-catchup-all-and-exit
1034     "E" gnus-summary-exit-no-update
1035     "Q" gnus-summary-exit
1036     "Z" gnus-summary-exit
1037     "n" gnus-summary-catchup-and-goto-next-group
1038     "R" gnus-summary-reselect-current-group
1039     "G" gnus-summary-rescan-group
1040     "N" gnus-summary-next-group
1041     "P" gnus-summary-prev-group)
1042
1043   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1044     " " gnus-summary-next-page
1045     "n" gnus-summary-next-page
1046     "\177" gnus-summary-prev-page
1047     [delete] gnus-summary-prev-page
1048     "p" gnus-summary-prev-page
1049     "\r" gnus-summary-scroll-up
1050     "<" gnus-summary-beginning-of-article
1051     ">" gnus-summary-end-of-article
1052     "b" gnus-summary-beginning-of-article
1053     "e" gnus-summary-end-of-article
1054     "^" gnus-summary-refer-parent-article
1055     "r" gnus-summary-refer-parent-article
1056     "R" gnus-summary-refer-references
1057     "g" gnus-summary-show-article
1058     "s" gnus-summary-isearch-article)
1059
1060   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1061     "b" gnus-article-add-buttons
1062     "B" gnus-article-add-buttons-to-head
1063     "o" gnus-article-treat-overstrike
1064     "e" gnus-article-emphasize
1065     "w" gnus-article-fill-cited-article
1066     "c" gnus-article-remove-cr
1067     "L" gnus-article-remove-trailing-blank-lines
1068     "q" gnus-article-de-quoted-unreadable
1069     "f" gnus-article-display-x-face
1070     "l" gnus-summary-stop-page-breaking
1071     "r" gnus-summary-caesar-message
1072     "t" gnus-article-hide-headers
1073     "v" gnus-summary-verbose-headers
1074     "m" gnus-summary-toggle-mime)
1075
1076   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1077     "a" gnus-article-hide
1078     "h" gnus-article-hide-headers
1079     "b" gnus-article-hide-boring-headers
1080     "s" gnus-article-hide-signature
1081     "c" gnus-article-hide-citation
1082     "p" gnus-article-hide-pgp
1083     "P" gnus-article-hide-pem
1084     "\C-c" gnus-article-hide-citation-maybe)
1085
1086   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1087     "a" gnus-article-highlight
1088     "h" gnus-article-highlight-headers
1089     "c" gnus-article-highlight-citation
1090     "s" gnus-article-highlight-signature)
1091
1092   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1093     "z" gnus-article-date-ut
1094     "u" gnus-article-date-ut
1095     "l" gnus-article-date-local
1096     "e" gnus-article-date-lapsed
1097     "o" gnus-article-date-original)
1098
1099   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1100     "v" gnus-version
1101     "f" gnus-summary-fetch-faq
1102     "d" gnus-summary-describe-group
1103     "h" gnus-summary-describe-briefly
1104     "i" gnus-info-find-node)
1105
1106   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1107     "e" gnus-summary-expire-articles
1108     "\M-\C-e" gnus-summary-expire-articles-now
1109     "\177" gnus-summary-delete-article
1110     [delete] gnus-summary-delete-article
1111     "m" gnus-summary-move-article
1112     "r" gnus-summary-respool-article
1113     "w" gnus-summary-edit-article
1114     "c" gnus-summary-copy-article
1115     "B" gnus-summary-crosspost-article
1116     "q" gnus-summary-respool-query
1117     "i" gnus-summary-import-article)
1118
1119   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1120     "o" gnus-summary-save-article
1121     "m" gnus-summary-save-article-mail
1122     "r" gnus-summary-save-article-rmail
1123     "f" gnus-summary-save-article-file
1124     "b" gnus-summary-save-article-body-file
1125     "h" gnus-summary-save-article-folder
1126     "v" gnus-summary-save-article-vm
1127     "p" gnus-summary-pipe-output
1128     "s" gnus-soup-add-article)
1129   )
1130
1131 \f
1132
1133 (defun gnus-summary-mode (&optional group)
1134   "Major mode for reading articles.
1135
1136 All normal editing commands are switched off.
1137 \\<gnus-summary-mode-map>
1138 Each line in this buffer represents one article.  To read an
1139 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1140 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1141 respectively.
1142
1143 You can also post articles and send mail from this buffer.  To
1144 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1145 of an article, type `\\[gnus-summary-reply]'.
1146
1147 There are approx. one gazillion commands you can execute in this
1148 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1149
1150 The following commands are available:
1151
1152 \\{gnus-summary-mode-map}"
1153   (interactive)
1154   (when (and menu-bar-mode
1155              (gnus-visual-p 'summary-menu 'menu))
1156     (gnus-summary-make-menu-bar))
1157   (kill-all-local-variables)
1158   (gnus-summary-make-local-variables)
1159   (gnus-make-thread-indent-array)
1160   (gnus-simplify-mode-line)
1161   (setq major-mode 'gnus-summary-mode)
1162   (setq mode-name "Summary")
1163   (make-local-variable 'minor-mode-alist)
1164   (use-local-map gnus-summary-mode-map)
1165   (buffer-disable-undo (current-buffer))
1166   (setq buffer-read-only t)             ;Disable modification
1167   (setq truncate-lines t)
1168   (setq selective-display t)
1169   (setq selective-display-ellipses t)   ;Display `...'
1170   (setq buffer-display-table gnus-summary-display-table)
1171   (setq gnus-newsgroup-name group)
1172   (make-local-variable 'gnus-summary-line-format)
1173   (make-local-variable 'gnus-summary-line-format-spec)
1174   (make-local-variable 'gnus-summary-mark-positions)
1175   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1176   (gnus-update-summary-mark-positions)
1177   (gnus-make-local-hook 'post-command-hook)
1178   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1179   (run-hooks 'gnus-summary-mode-hook))
1180
1181 (defun gnus-summary-make-local-variables ()
1182   "Make all the local summary buffer variables."
1183   (let ((locals gnus-summary-local-variables)
1184         global local)
1185     (while (setq local (pop locals))
1186       (if (consp local)
1187           (progn
1188             (if (eq (cdr local) 'global)
1189                 ;; Copy the global value of the variable.
1190                 (setq global (symbol-value (car local)))
1191               ;; Use the value from the list.
1192               (setq global (eval (cdr local))))
1193             (make-local-variable (car local))
1194             (set (car local) global))
1195         ;; Simple nil-valued local variable.
1196         (make-local-variable local)
1197         (set local nil)))))
1198
1199 (defun gnus-summary-clear-local-variables ()
1200   (let ((locals gnus-summary-local-variables))
1201     (while locals
1202       (if (consp (car locals))
1203           (and (vectorp (caar locals))
1204                (set (caar locals) nil))
1205         (and (vectorp (car locals))
1206              (set (car locals) nil)))
1207       (setq locals (cdr locals)))))
1208
1209 ;; Summary data functions.
1210
1211 (defmacro gnus-data-number (data)
1212   `(car ,data))
1213
1214 (defmacro gnus-data-set-number (data number)
1215   `(setcar ,data ,number))
1216
1217 (defmacro gnus-data-mark (data)
1218   `(nth 1 ,data))
1219
1220 (defmacro gnus-data-set-mark (data mark)
1221   `(setcar (nthcdr 1 ,data) ,mark))
1222
1223 (defmacro gnus-data-pos (data)
1224   `(nth 2 ,data))
1225
1226 (defmacro gnus-data-set-pos (data pos)
1227   `(setcar (nthcdr 2 ,data) ,pos))
1228
1229 (defmacro gnus-data-header (data)
1230   `(nth 3 ,data))
1231
1232 (defmacro gnus-data-level (data)
1233   `(nth 4 ,data))
1234
1235 (defmacro gnus-data-unread-p (data)
1236   `(= (nth 1 ,data) gnus-unread-mark))
1237
1238 (defmacro gnus-data-pseudo-p (data)
1239   `(consp (nth 3 ,data)))
1240
1241 (defmacro gnus-data-find (number)
1242   `(assq ,number gnus-newsgroup-data))
1243
1244 (defmacro gnus-data-find-list (number &optional data)
1245   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
1246      (memq (assq ,number bdata)
1247            bdata)))
1248
1249 (defmacro gnus-data-make (number mark pos header level)
1250   `(list ,number ,mark ,pos ,header ,level))
1251
1252 (defun gnus-data-enter (after-article number mark pos header level offset)
1253   (let ((data (gnus-data-find-list after-article)))
1254     (or data (error "No such article: %d" after-article))
1255     (setcdr data (cons (gnus-data-make number mark pos header level)
1256                        (cdr data)))
1257     (setq gnus-newsgroup-data-reverse nil)
1258     (gnus-data-update-list (cddr data) offset)))
1259
1260 (defun gnus-data-enter-list (after-article list &optional offset)
1261   (when list
1262     (let ((data (and after-article (gnus-data-find-list after-article)))
1263           (ilist list))
1264       (or data (not after-article) (error "No such article: %d" after-article))
1265       ;; Find the last element in the list to be spliced into the main
1266       ;; list.
1267       (while (cdr list)
1268         (setq list (cdr list)))
1269       (if (not data)
1270           (progn
1271             (setcdr list gnus-newsgroup-data)
1272             (setq gnus-newsgroup-data ilist)
1273             (and offset (gnus-data-update-list (cdr list) offset)))
1274         (setcdr list (cdr data))
1275         (setcdr data ilist)
1276         (and offset (gnus-data-update-list (cdr data) offset)))
1277       (setq gnus-newsgroup-data-reverse nil))))
1278
1279 (defun gnus-data-remove (article &optional offset)
1280   (let ((data gnus-newsgroup-data))
1281     (if (= (gnus-data-number (car data)) article)
1282         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
1283               gnus-newsgroup-data-reverse nil)
1284       (while (cdr data)
1285         (and (= (gnus-data-number (cadr data)) article)
1286              (progn
1287                (setcdr data (cddr data))
1288                (and offset (gnus-data-update-list (cdr data) offset))
1289                (setq data nil
1290                      gnus-newsgroup-data-reverse nil)))
1291         (setq data (cdr data))))))
1292
1293 (defmacro gnus-data-list (backward)
1294   `(if ,backward
1295        (or gnus-newsgroup-data-reverse
1296            (setq gnus-newsgroup-data-reverse
1297                  (reverse gnus-newsgroup-data)))
1298      gnus-newsgroup-data))
1299
1300 (defun gnus-data-update-list (data offset)
1301   "Add OFFSET to the POS of all data entries in DATA."
1302   (while data
1303     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
1304     (setq data (cdr data))))
1305
1306 (defun gnus-data-compute-positions ()
1307   "Compute the positions of all articles."
1308   (let ((data gnus-newsgroup-data)
1309         pos)
1310     (while data
1311       (when (setq pos (text-property-any
1312                        (point-min) (point-max)
1313                        'gnus-number (gnus-data-number (car data))))
1314         (gnus-data-set-pos (car data) (+ pos 3)))
1315       (setq data (cdr data)))))
1316
1317 (defun gnus-summary-article-pseudo-p (article)
1318   "Say whether this article is a pseudo article or not."
1319   (not (vectorp (gnus-data-header (gnus-data-find article)))))
1320
1321 (defun gnus-article-parent-p (number)
1322   "Say whether this article is a parent or not."
1323   (let ((data (gnus-data-find-list number)))
1324     (and (cdr data)                     ; There has to be an article after...
1325          (< (gnus-data-level (car data)) ; And it has to have a higher level.
1326             (gnus-data-level (nth 1 data))))))
1327
1328 (defun gnus-article-children (number)
1329   "Return a list of all children to NUMBER."
1330   (let* ((data (gnus-data-find-list number))
1331          (level (gnus-data-level (car data)))
1332          children)
1333     (setq data (cdr data))
1334     (while (and data            
1335                 (= (gnus-data-level (car data)) (1+ level)))
1336       (push (gnus-data-number (car data)) children)
1337       (setq data (cdr data)))
1338     children))
1339
1340 (defmacro gnus-summary-skip-intangible ()
1341   "If the current article is intangible, then jump to a different article."
1342   '(let ((to (get-text-property (point) 'gnus-intangible)))
1343     (and to (gnus-summary-goto-subject to))))
1344
1345 (defmacro gnus-summary-article-intangible-p ()
1346   "Say whether this article is intangible or not."
1347   '(get-text-property (point) 'gnus-intangible))
1348
1349 ;; Some summary mode macros.
1350
1351 (defmacro gnus-summary-article-number ()
1352   "The article number of the article on the current line.
1353 If there isn's an article number here, then we return the current
1354 article number."
1355   '(progn
1356      (gnus-summary-skip-intangible)
1357      (or (get-text-property (point) 'gnus-number)
1358          (gnus-summary-last-subject))))
1359
1360 (defmacro gnus-summary-article-header (&optional number)
1361   `(gnus-data-header (gnus-data-find
1362                       ,(or number '(gnus-summary-article-number)))))
1363
1364 (defmacro gnus-summary-thread-level (&optional number)
1365   `(if (and (eq gnus-summary-make-false-root 'dummy)
1366             (get-text-property (point) 'gnus-intangible))
1367        0
1368      (gnus-data-level (gnus-data-find
1369                        ,(or number '(gnus-summary-article-number))))))
1370
1371 (defmacro gnus-summary-article-mark (&optional number)
1372   `(gnus-data-mark (gnus-data-find
1373                     ,(or number '(gnus-summary-article-number)))))
1374
1375 (defmacro gnus-summary-article-pos (&optional number)
1376   `(gnus-data-pos (gnus-data-find
1377                    ,(or number '(gnus-summary-article-number)))))
1378
1379 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
1380 (defmacro gnus-summary-article-subject (&optional number)
1381   "Return current subject string or nil if nothing."
1382   `(let ((headers
1383           ,(if number
1384                `(gnus-data-header (assq ,number gnus-newsgroup-data))
1385              '(gnus-data-header (assq (gnus-summary-article-number)
1386                                       gnus-newsgroup-data)))))
1387      (and headers
1388           (vectorp headers)
1389           (mail-header-subject headers))))
1390
1391 (defmacro gnus-summary-article-score (&optional number)
1392   "Return current article score."
1393   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
1394                   gnus-newsgroup-scored))
1395        gnus-summary-default-score 0))
1396
1397 (defun gnus-summary-article-children (&optional number)
1398   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
1399          (level (gnus-data-level (car data)))
1400          l children)
1401     (while (and (setq data (cdr data))
1402                 (> (setq l (gnus-data-level (car data))) level))
1403       (and (= (1+ level) l)
1404            (setq children (cons (gnus-data-number (car data))
1405                                 children))))
1406     (nreverse children)))
1407
1408 (defun gnus-summary-article-parent (&optional number)
1409   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
1410                                     (gnus-data-list t)))
1411          (level (gnus-data-level (car data))))
1412     (if (zerop level)
1413         () ; This is a root.
1414       ;; We search until we find an article with a level less than
1415       ;; this one.  That function has to be the parent.
1416       (while (and (setq data (cdr data))
1417                   (not (< (gnus-data-level (car data)) level))))
1418       (and data (gnus-data-number (car data))))))
1419
1420 (defun gnus-unread-mark-p (mark)
1421   "Say whether MARK is the unread mark."
1422   (= mark gnus-unread-mark))
1423
1424 (defun gnus-read-mark-p (mark)
1425   "Say whether MARK is one of the marks that mark as read.
1426 This is all marks except unread, ticked, dormant, and expirable."
1427   (not (or (= mark gnus-unread-mark)
1428            (= mark gnus-ticked-mark)
1429            (= mark gnus-dormant-mark)
1430            (= mark gnus-expirable-mark))))
1431
1432 ;; Saving hidden threads.
1433
1434 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
1435 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
1436 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
1437
1438 (defmacro gnus-save-hidden-threads (&rest forms)
1439   "Save hidden threads, eval FORMS, and restore the hidden threads."
1440   (let ((config (make-symbol "config")))
1441     `(let ((,config (gnus-hidden-threads-configuration)))
1442        (unwind-protect
1443            (progn
1444              ,@forms)
1445          (gnus-restore-hidden-threads-configuration ,config)))))
1446
1447 (defun gnus-hidden-threads-configuration ()
1448   "Return the current hidden threads configuration."
1449   (save-excursion
1450     (let (config)
1451       (goto-char (point-min))
1452       (while (search-forward "\r" nil t)
1453         (push (1- (point)) config))
1454       config)))
1455
1456 (defun gnus-restore-hidden-threads-configuration (config)
1457   "Restore hidden threads configuration from CONFIG."
1458   (let (point buffer-read-only)
1459     (while (setq point (pop config))
1460       (when (and (< point (point-max))
1461                  (goto-char point)
1462                  (= (following-char) ?\n))
1463         (subst-char-in-region point (1+ point) ?\n ?\r)))))
1464
1465 ;; Various summary mode internalish functions.
1466
1467 (defun gnus-mouse-pick-article (e)
1468   (interactive "e")
1469   (mouse-set-point e)
1470   (gnus-summary-next-page nil t))
1471
1472 (defun gnus-summary-setup-buffer (group)
1473   "Initialize summary buffer."
1474   (let ((buffer (concat "*Summary " group "*")))
1475     (if (get-buffer buffer)
1476         (progn
1477           (set-buffer buffer)
1478           (setq gnus-summary-buffer (current-buffer))
1479           (not gnus-newsgroup-prepared))
1480       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
1481       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
1482       (gnus-add-current-to-buffer-list)
1483       (gnus-summary-mode group)
1484       (when gnus-carpal
1485         (gnus-carpal-setup-buffer 'summary))
1486       (unless gnus-single-article-buffer
1487         (make-local-variable 'gnus-article-buffer)
1488         (make-local-variable 'gnus-article-current)
1489         (make-local-variable 'gnus-original-article-buffer))
1490       (setq gnus-newsgroup-name group)
1491       t)))
1492
1493 (defun gnus-set-global-variables ()
1494   ;; Set the global equivalents of the summary buffer-local variables
1495   ;; to the latest values they had.  These reflect the summary buffer
1496   ;; that was in action when the last article was fetched.
1497   (when (eq major-mode 'gnus-summary-mode)
1498     (setq gnus-summary-buffer (current-buffer))
1499     (let ((name gnus-newsgroup-name)
1500           (marked gnus-newsgroup-marked)
1501           (unread gnus-newsgroup-unreads)
1502           (headers gnus-current-headers)
1503           (data gnus-newsgroup-data)
1504           (summary gnus-summary-buffer)
1505           (article-buffer gnus-article-buffer)
1506           (original gnus-original-article-buffer)
1507           (gac gnus-article-current)
1508           (reffed gnus-reffed-article-number)
1509           (score-file gnus-current-score-file))
1510       (save-excursion
1511         (set-buffer gnus-group-buffer)
1512         (setq gnus-newsgroup-name name)
1513         (setq gnus-newsgroup-marked marked)
1514         (setq gnus-newsgroup-unreads unread)
1515         (setq gnus-current-headers headers)
1516         (setq gnus-newsgroup-data data)
1517         (setq gnus-article-current gac)
1518         (setq gnus-summary-buffer summary)
1519         (setq gnus-article-buffer article-buffer)
1520         (setq gnus-original-article-buffer original)
1521         (setq gnus-reffed-article-number reffed)
1522         (setq gnus-current-score-file score-file)))))
1523
1524 (defun gnus-summary-last-article-p (&optional article)
1525   "Return whether ARTICLE is the last article in the buffer."
1526   (if (not (setq article (or article (gnus-summary-article-number))))
1527       t ; All non-existant numbers are the last article. :-)
1528     (not (cdr (gnus-data-find-list article)))))
1529
1530 (defun gnus-make-thread-indent-array ()
1531   (let ((n 200))
1532     (unless (and gnus-thread-indent-array
1533                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
1534       (setq gnus-thread-indent-array (make-vector 201 "")
1535             gnus-thread-indent-array-level gnus-thread-indent-level)
1536       (while (>= n 0)
1537         (aset gnus-thread-indent-array n
1538               (make-string (* n gnus-thread-indent-level) ? ))
1539         (setq n (1- n))))))
1540
1541 (defun gnus-update-summary-mark-positions ()
1542   "Compute where the summary marks are to go."
1543   (save-excursion
1544     (when (and gnus-summary-buffer
1545                (get-buffer gnus-summary-buffer)
1546                (buffer-name (get-buffer gnus-summary-buffer)))
1547       (set-buffer gnus-summary-buffer))
1548     (let ((gnus-replied-mark 129)
1549           (gnus-score-below-mark 130)
1550           (gnus-score-over-mark 130)
1551           (thread nil)
1552           (gnus-visual nil)
1553           (spec gnus-summary-line-format-spec)
1554           pos)
1555       (save-excursion
1556         (gnus-set-work-buffer)
1557         (let ((gnus-summary-line-format-spec spec))
1558           (gnus-summary-insert-line
1559            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1560           (goto-char (point-min))
1561           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1562                                              (- (point) 2)))))
1563           (goto-char (point-min))
1564           (push (cons 'replied (and (search-forward "\201" nil t) 
1565                                     (- (point) 2)))
1566                 pos)
1567           (goto-char (point-min))
1568           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
1569                 pos)))
1570       (setq gnus-summary-mark-positions pos))))
1571
1572 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
1573   "Insert a dummy root in the summary buffer."
1574   (beginning-of-line)
1575   (gnus-add-text-properties
1576    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
1577    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
1578
1579 (defun gnus-summary-insert-line
1580   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
1581                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
1582                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
1583   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
1584          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
1585          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
1586          (gnus-tmp-score-char
1587           (if (or (null gnus-summary-default-score)
1588                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
1589                       gnus-summary-zcore-fuzz)) ? 
1590             (if (< gnus-tmp-score gnus-summary-default-score)
1591                 gnus-score-below-mark gnus-score-over-mark)))
1592          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
1593                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
1594                                   gnus-cached-mark)
1595                                  (gnus-tmp-replied gnus-replied-mark)
1596                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
1597                                   gnus-saved-mark)
1598                                  (t gnus-unread-mark)))
1599          (gnus-tmp-from (mail-header-from gnus-tmp-header))
1600          (gnus-tmp-name
1601           (cond
1602            ((string-match "(.+)" gnus-tmp-from)
1603             (substring gnus-tmp-from
1604                        (1+ (match-beginning 0)) (1- (match-end 0))))
1605            ((string-match "<[^>]+> *$" gnus-tmp-from)
1606             (let ((beg (match-beginning 0)))
1607               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
1608                        (substring gnus-tmp-from (1+ (match-beginning 0))
1609                                   (1- (match-end 0))))
1610                   (substring gnus-tmp-from 0 beg))))
1611            (t gnus-tmp-from)))
1612          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
1613          (gnus-tmp-number (mail-header-number gnus-tmp-header))
1614          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
1615          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
1616          (buffer-read-only nil))
1617     (when (string= gnus-tmp-name "")
1618       (setq gnus-tmp-name gnus-tmp-from))
1619     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
1620     (gnus-put-text-property
1621      (point)
1622      (progn (eval gnus-summary-line-format-spec) (point))
1623      'gnus-number gnus-tmp-number)
1624     (when (gnus-visual-p 'summary-highlight 'highlight)
1625       (forward-line -1)
1626       (run-hooks 'gnus-summary-update-hook)
1627       (forward-line 1))))
1628
1629 (defun gnus-summary-update-line (&optional dont-update)
1630   ;; Update summary line after change.
1631   (when (and gnus-summary-default-score
1632              (not gnus-summary-inhibit-highlight))
1633     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
1634            (article (gnus-summary-article-number))
1635            (score (gnus-summary-article-score article)))
1636       (unless dont-update
1637         (if (and gnus-summary-mark-below
1638                  (< (gnus-summary-article-score)
1639                     gnus-summary-mark-below))
1640             ;; This article has a low score, so we mark it as read.
1641             (when (memq article gnus-newsgroup-unreads)
1642               (gnus-summary-mark-article-as-read gnus-low-score-mark))
1643           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
1644             ;; This article was previously marked as read on account
1645             ;; of a low score, but now it has risen, so we mark it as
1646             ;; unread.
1647             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
1648         (gnus-summary-update-mark
1649          (if (or (null gnus-summary-default-score)
1650                  (<= (abs (- score gnus-summary-default-score))
1651                      gnus-summary-zcore-fuzz)) ? 
1652            (if (< score gnus-summary-default-score)
1653                gnus-score-below-mark gnus-score-over-mark)) 'score))
1654       ;; Do visual highlighting.
1655       (when (gnus-visual-p 'summary-highlight 'highlight)
1656         (run-hooks 'gnus-summary-update-hook)))))
1657
1658 (defvar gnus-tmp-new-adopts nil)
1659
1660 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
1661   ;; Sum up all elements (and sub-elements) in a list.
1662   (let* ((number
1663           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
1664           (cond
1665            ((and (consp thread) (cdr thread))
1666             (apply
1667              '+ 1 (mapcar
1668                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
1669            ((null thread)
1670             1)
1671            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
1672             1)
1673            (t 0))))
1674     (when (and level (zerop level) gnus-tmp-new-adopts)
1675       (incf number
1676             (apply '+ (mapcar
1677                        'gnus-summary-number-of-articles-in-thread
1678                        gnus-tmp-new-adopts))))
1679     (if char
1680         (if (> number 1) gnus-not-empty-thread-mark
1681           gnus-empty-thread-mark)
1682       number)))
1683
1684 (defun gnus-summary-set-local-parameters (group)
1685  "Go through the local params of GROUP and set all variable specs in that list."
1686   (let ((params (gnus-info-params (gnus-get-info group)))
1687         elem)
1688     (while params
1689       (setq elem (car params)
1690             params (cdr params))
1691       (and (consp elem)                 ; Has to be a cons.
1692            (consp (cdr elem))           ; The cdr has to be a list.
1693            (symbolp (car elem))         ; Has to be a symbol in there.
1694            (not (memq (car elem) 
1695                       '(quit-config to-address to-list to-group)))
1696            (progn                       ; So we set it.
1697              (make-local-variable (car elem))
1698              (set (car elem) (eval (nth 1 elem))))))))
1699
1700 (defun gnus-summary-read-group (group &optional show-all no-article
1701                                       kill-buffer no-display)
1702   "Start reading news in newsgroup GROUP.
1703 If SHOW-ALL is non-nil, already read articles are also listed.
1704 If NO-ARTICLE is non-nil, no article is selected initially.
1705 If NO-DISPLAY, don't generate a summary buffer."
1706   (gnus-message 5 "Retrieving newsgroup: %s..." group)
1707   (let* ((new-group (gnus-summary-setup-buffer group))
1708          (quit-config (gnus-group-quit-config group))
1709          (did-select (and new-group (gnus-select-newsgroup group show-all))))
1710     (cond
1711      ;; This summary buffer exists already, so we just select it.
1712      ((not new-group)
1713       (gnus-set-global-variables)
1714       (when kill-buffer
1715         (gnus-kill-or-deaden-summary kill-buffer))
1716       (gnus-configure-windows 'summary 'force)
1717       (gnus-set-mode-line 'summary)
1718       (gnus-summary-position-point)
1719       (message "")
1720       t)
1721      ;; We couldn't select this group.
1722      ((null did-select)
1723       (when (and (eq major-mode 'gnus-summary-mode)
1724                  (not (equal (current-buffer) kill-buffer)))
1725         (kill-buffer (current-buffer))
1726         (if (not quit-config)
1727             (progn
1728               (set-buffer gnus-group-buffer)
1729               (gnus-group-jump-to-group group)
1730               (gnus-group-next-unread-group 1))
1731           (if (not (buffer-name (car quit-config)))
1732               (gnus-configure-windows 'group 'force)
1733             (set-buffer (car quit-config))
1734             (and (eq major-mode 'gnus-summary-mode)
1735                  (gnus-set-global-variables))
1736             (gnus-configure-windows (cdr quit-config)))))
1737       (gnus-message 3 "Can't select group")
1738       nil)
1739      ;; The user did a `C-g' while prompting for number of articles,
1740      ;; so we exit this group.
1741      ((eq did-select 'quit)
1742       (and (eq major-mode 'gnus-summary-mode)
1743            (not (equal (current-buffer) kill-buffer))
1744            (kill-buffer (current-buffer)))
1745       (when kill-buffer
1746         (gnus-kill-or-deaden-summary kill-buffer))
1747       (if (not quit-config)
1748           (progn
1749             (set-buffer gnus-group-buffer)
1750             (gnus-group-jump-to-group group)
1751             (gnus-group-next-unread-group 1)
1752             (gnus-configure-windows 'group 'force))
1753         (if (not (buffer-name (car quit-config)))
1754             (gnus-configure-windows 'group 'force)
1755           (set-buffer (car quit-config))
1756           (and (eq major-mode 'gnus-summary-mode)
1757                (gnus-set-global-variables))
1758           (gnus-configure-windows (cdr quit-config))))
1759       ;; Finally signal the quit.
1760       (signal 'quit nil))
1761      ;; The group was successfully selected.
1762      (t
1763       (gnus-set-global-variables)
1764       ;; Save the active value in effect when the group was entered.
1765       (setq gnus-newsgroup-active
1766             (gnus-copy-sequence
1767              (gnus-active gnus-newsgroup-name)))
1768       ;; You can change the summary buffer in some way with this hook.
1769       (run-hooks 'gnus-select-group-hook)
1770       ;; Set any local variables in the group parameters.
1771       (gnus-summary-set-local-parameters gnus-newsgroup-name)
1772       (gnus-update-format-specifications)
1773       ;; Do score processing.
1774       (when gnus-use-scoring
1775         (gnus-possibly-score-headers))
1776       ;; Check whether to fill in the gaps in the threads.
1777       (when gnus-build-sparse-threads
1778         (gnus-build-sparse-threads))
1779       ;; Find the initial limit.
1780       (if gnus-show-threads
1781           (if show-all
1782               (let ((gnus-newsgroup-dormant nil))
1783                 (gnus-summary-initial-limit show-all))
1784             (gnus-summary-initial-limit show-all))
1785         (setq gnus-newsgroup-limit 
1786               (mapcar 
1787                (lambda (header) (mail-header-number header))
1788                gnus-newsgroup-headers)))
1789       ;; Generate the summary buffer.
1790       (unless no-display
1791         (gnus-summary-prepare))
1792       (when gnus-use-trees
1793         (gnus-tree-open group)
1794         (setq gnus-summary-highlight-line-function
1795               'gnus-tree-highlight-article))
1796       ;; If the summary buffer is empty, but there are some low-scored
1797       ;; articles or some excluded dormants, we include these in the
1798       ;; buffer.
1799       (when (and (zerop (buffer-size))
1800                  (not no-display))
1801         (cond (gnus-newsgroup-dormant
1802                (gnus-summary-limit-include-dormant))
1803               ((and gnus-newsgroup-scored show-all)
1804                (gnus-summary-limit-include-expunged t))))
1805       ;; Function `gnus-apply-kill-file' must be called in this hook.
1806       (run-hooks 'gnus-apply-kill-hook)
1807       (if (and (zerop (buffer-size))
1808                (not no-display))
1809           (progn
1810             ;; This newsgroup is empty.
1811             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
1812             (gnus-message 6 "No unread news")
1813             (when kill-buffer
1814               (gnus-kill-or-deaden-summary kill-buffer))
1815             ;; Return nil from this function.
1816             nil)
1817         ;; Hide conversation thread subtrees.  We cannot do this in
1818         ;; gnus-summary-prepare-hook since kill processing may not
1819         ;; work with hidden articles.
1820         (and gnus-show-threads
1821              gnus-thread-hide-subtree
1822              (gnus-summary-hide-all-threads))
1823         ;; Show first unread article if requested.
1824         (if (and (not no-article)
1825                  (not no-display)
1826                  gnus-newsgroup-unreads
1827                  gnus-auto-select-first)
1828             (unless (if (eq gnus-auto-select-first 'best)
1829                         (gnus-summary-best-unread-article)
1830                       (gnus-summary-first-unread-article))
1831               (gnus-configure-windows 'summary))
1832           ;; Don't select any articles, just move point to the first
1833           ;; article in the group.
1834           (goto-char (point-min))
1835           (gnus-summary-position-point)
1836           (gnus-set-mode-line 'summary)
1837           (gnus-configure-windows 'summary 'force))
1838         (when kill-buffer
1839           (gnus-kill-or-deaden-summary kill-buffer))
1840         (when (get-buffer-window gnus-group-buffer t)
1841           ;; Gotta use windows, because recenter does wierd stuff if
1842           ;; the current buffer ain't the displayed window.
1843           (let ((owin (selected-window)))
1844             (select-window (get-buffer-window gnus-group-buffer t))
1845             (when (gnus-group-goto-group group)
1846               (recenter))
1847             (select-window owin))))
1848       ;; Mark this buffer as "prepared".
1849       (setq gnus-newsgroup-prepared t)
1850       t))))
1851
1852 (defun gnus-summary-prepare ()
1853   "Generate the summary buffer."
1854   (let ((buffer-read-only nil))
1855     (erase-buffer)
1856     (setq gnus-newsgroup-data nil
1857           gnus-newsgroup-data-reverse nil)
1858     (run-hooks 'gnus-summary-generate-hook)
1859     ;; Generate the buffer, either with threads or without.
1860     (when gnus-newsgroup-headers
1861       (gnus-summary-prepare-threads
1862        (if gnus-show-threads
1863            (gnus-sort-gathered-threads
1864             (funcall gnus-summary-thread-gathering-function
1865                      (gnus-sort-threads
1866                       (gnus-cut-threads (gnus-make-threads)))))
1867          ;; Unthreaded display.
1868          (gnus-sort-articles gnus-newsgroup-headers))))
1869     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
1870     ;; Call hooks for modifying summary buffer.
1871     (goto-char (point-min))
1872     (run-hooks 'gnus-summary-prepare-hook)))
1873
1874 (defsubst gnus-general-simplify-subject (subject)
1875   "Simply subject by the same rules as gnus-gather-threads-by-subject."
1876   (setq subject
1877         (cond
1878          ;; Truncate the subject.
1879          ((numberp gnus-summary-gather-subject-limit)
1880           (setq subject (gnus-simplify-subject-re subject))
1881           (if (> (length subject) gnus-summary-gather-subject-limit)
1882               (substring subject 0 gnus-summary-gather-subject-limit)
1883             subject))
1884          ;; Fuzzily simplify it.
1885          ((eq 'fuzzy gnus-summary-gather-subject-limit)
1886           (gnus-simplify-subject-fuzzy subject))
1887          ;; Just remove the leading "Re:".
1888          (t
1889           (gnus-simplify-subject-re subject))))
1890   
1891   (if (and gnus-summary-gather-exclude-subject
1892            (string-match gnus-summary-gather-exclude-subject subject))
1893       nil                               ; This article shouldn't be gathered
1894     subject))
1895
1896 (defun gnus-summary-simplify-subject-query ()
1897   "Query where the respool algorithm would put this article."
1898   (interactive)
1899   (gnus-set-global-variables)
1900   (gnus-summary-select-article)
1901   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
1902
1903 (defun gnus-gather-threads-by-subject (threads)
1904   "Gather threads by looking at Subject headers."
1905   (if (not gnus-summary-make-false-root)
1906       threads
1907     (let ((hashtb (gnus-make-hashtable 1023))
1908           (prev threads)
1909           (result threads)
1910           subject hthread whole-subject)
1911       (while threads
1912         (setq subject (gnus-general-simplify-subject
1913                        (setq whole-subject (mail-header-subject 
1914                                             (caar threads)))))
1915         (if subject
1916             (if (setq hthread (gnus-gethash subject hashtb))
1917                 (progn
1918                   ;; We enter a dummy root into the thread, if we
1919                   ;; haven't done that already.
1920                   (unless (stringp (caar hthread))
1921                     (setcar hthread (list whole-subject (car hthread))))
1922                   ;; We add this new gathered thread to this gathered
1923                   ;; thread.
1924                   (setcdr (car hthread)
1925                           (nconc (cdar hthread) (list (car threads))))
1926                   ;; Remove it from the list of threads.
1927                   (setcdr prev (cdr threads))
1928                   (setq threads prev))
1929               ;; Enter this thread into the hash table.
1930               (gnus-sethash subject threads hashtb)))
1931         (setq prev threads)
1932         (setq threads (cdr threads)))
1933       result)))
1934
1935 (defun gnus-gather-threads-by-references (threads)
1936   "Gather threads by looking at References headers."
1937   (let ((idhashtb (gnus-make-hashtable 1023))
1938         (thhashtb (gnus-make-hashtable 1023))
1939         (prev threads)
1940         (result threads)
1941         ids references id gthread gid entered)
1942     (while threads
1943       (when (setq references (mail-header-references (caar threads)))
1944         (setq id (mail-header-id (caar threads)))
1945         (setq ids (gnus-split-references references))
1946         (setq entered nil)
1947         (while ids
1948           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
1949               (progn
1950                 (gnus-sethash (car ids) id idhashtb)
1951                 (gnus-sethash id threads thhashtb))
1952             (setq gthread (gnus-gethash gid thhashtb))
1953             (unless entered
1954               ;; We enter a dummy root into the thread, if we
1955               ;; haven't done that already.
1956               (unless (stringp (caar gthread))
1957                 (setcar gthread (list (mail-header-subject (caar gthread))
1958                                       (car gthread))))
1959               ;; We add this new gathered thread to this gathered
1960               ;; thread.
1961               (setcdr (car gthread)
1962                       (nconc (cdar gthread) (list (car threads)))))
1963             ;; Add it into the thread hash table.
1964             (gnus-sethash id gthread thhashtb)
1965             (setq entered t)
1966             ;; Remove it from the list of threads.
1967             (setcdr prev (cdr threads))
1968             (setq threads prev))
1969           (setq ids (cdr ids))))
1970       (setq prev threads)
1971       (setq threads (cdr threads)))
1972     result))
1973
1974 (defun gnus-sort-gathered-threads (threads)
1975   "Sort subtreads inside each gathered thread by article number."
1976   (let ((result threads))
1977     (while threads
1978       (when (stringp (caar threads))
1979         (setcdr (car threads)
1980                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
1981       (setq threads (cdr threads)))
1982     result))
1983
1984 (defun gnus-make-threads ()
1985   "Go through the dependency hashtb and find the roots.  Return all threads."
1986   (let (threads)
1987     (mapatoms
1988      (lambda (refs)
1989        (unless (car (symbol-value refs))
1990          ;; These threads do not refer back to any other articles,
1991          ;; so they're roots.
1992          (setq threads (append (cdr (symbol-value refs)) threads))))
1993      gnus-newsgroup-dependencies)
1994     threads))
1995
1996 (defun gnus-build-sparse-threads ()
1997   (let ((headers gnus-newsgroup-headers)
1998         (deps gnus-newsgroup-dependencies)
1999         header references generation relations 
2000         cthread subject child end pthread relation)
2001     ;; First we create an alist of generations/relations, where 
2002     ;; generations is how much we trust the ralation, and the relation
2003     ;; is parent/child.
2004     (gnus-message 7 "Making sparse threads...")
2005     (save-excursion
2006       (nnheader-set-temp-buffer " *gnus sparse threads*")
2007       (while (setq header (pop headers))
2008         (when (and (setq references (mail-header-references header))
2009                    (not (string= references "")))
2010           (insert references)
2011           (setq child (mail-header-id header)
2012                 subject (mail-header-subject header))
2013           (setq generation 0)
2014           (while (search-backward ">" nil t)
2015             (setq end (1+ (point)))
2016             (when (search-backward "<" nil t)
2017               (push (list (incf generation) 
2018                           child (setq child (buffer-substring (point) end))
2019                           subject)
2020                     relations)))
2021           (push (list (1+ generation) child nil subject) relations)
2022           (erase-buffer)))
2023       (kill-buffer (current-buffer)))
2024     ;; Sort over trustworthiness.
2025     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
2026     (while (setq relation (pop relations))
2027       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
2028                 (unless (car (symbol-value cthread))
2029                   ;; Make this article the parent of these threads.
2030                   (setcar (symbol-value cthread)
2031                           (vector gnus-reffed-article-number 
2032                                   (cadddr relation) 
2033                                   "" ""
2034                                   (cadr relation) 
2035                                   (or (caddr relation) "") 0 0 "")))
2036               (set cthread (list (vector gnus-reffed-article-number
2037                                          (cadddr relation) 
2038                                          "" "" (cadr relation) 
2039                                          (or (caddr relation) "") 0 0 ""))))
2040         (push gnus-reffed-article-number gnus-newsgroup-limit)
2041         (push gnus-reffed-article-number gnus-newsgroup-sparse)
2042         (push (cons gnus-reffed-article-number gnus-sparse-mark)
2043               gnus-newsgroup-reads)
2044         (decf gnus-reffed-article-number)
2045         ;; Make this new thread the child of its parent.
2046         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
2047             (setcdr (symbol-value pthread)
2048                     (nconc (cdr (symbol-value pthread))
2049                            (list (symbol-value cthread))))
2050           (set pthread (list nil (symbol-value cthread))))))
2051     (gnus-message 7 "Making sparse threads...done")))
2052
2053 (defun gnus-build-old-threads ()
2054   ;; Look at all the articles that refer back to old articles, and
2055   ;; fetch the headers for the articles that aren't there.  This will
2056   ;; build complete threads - if the roots haven't been expired by the
2057   ;; server, that is.
2058   (let (id heads)
2059     (mapatoms
2060      (lambda (refs)
2061        (when (not (car (symbol-value refs)))
2062          (setq heads (cdr (symbol-value refs)))
2063          (while heads
2064            (if (memq (mail-header-number (caar heads))
2065                      gnus-newsgroup-dormant)
2066                (setq heads (cdr heads))
2067              (setq id (symbol-name refs))
2068              (while (and (setq id (gnus-build-get-header id))
2069                          (not (car (gnus-gethash
2070                                     id gnus-newsgroup-dependencies)))))
2071              (setq heads nil)))))
2072      gnus-newsgroup-dependencies)))
2073
2074 (defun gnus-build-get-header (id)
2075   ;; Look through the buffer of NOV lines and find the header to
2076   ;; ID.  Enter this line into the dependencies hash table, and return
2077   ;; the id of the parent article (if any).
2078   (let ((deps gnus-newsgroup-dependencies)
2079         found header)
2080     (prog1
2081         (save-excursion
2082           (set-buffer nntp-server-buffer)
2083           (goto-char (point-min))
2084           (while (and (not found) (search-forward id nil t))
2085             (beginning-of-line)
2086             (setq found (looking-at
2087                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
2088                                  (regexp-quote id))))
2089             (or found (beginning-of-line 2)))
2090           (when found
2091             (beginning-of-line)
2092             (and
2093              (setq header (gnus-nov-parse-line
2094                            (read (current-buffer)) deps))
2095              (gnus-parent-id (mail-header-references header)))))
2096       (when header
2097         (let ((number (mail-header-number header)))
2098           (push number gnus-newsgroup-limit)
2099           (push header gnus-newsgroup-headers)
2100           (if (memq number gnus-newsgroup-unselected)
2101               (progn
2102                 (push number gnus-newsgroup-unreads)
2103                 (setq gnus-newsgroup-unselected
2104                       (delq number gnus-newsgroup-unselected)))
2105             (push number gnus-newsgroup-ancient)))))))
2106
2107 (defun gnus-summary-update-article (article &optional iheader)
2108   "Update ARTICLE in the summary buffer."
2109   (set-buffer gnus-summary-buffer)
2110   (let* ((header (or iheader (gnus-summary-article-header article)))
2111          (id (mail-header-id header))
2112          (data (gnus-data-find article))
2113          (thread (gnus-id-to-thread id))
2114          (references (mail-header-references header))
2115          (parent
2116           (gnus-id-to-thread
2117            (or (gnus-parent-id 
2118                 (if (and references
2119                          (not (equal "" references)))
2120                     references))
2121                "none")))
2122          (buffer-read-only nil)
2123          (old (car thread))
2124          (number (mail-header-number header))
2125          pos)
2126     (when thread
2127       ;; !!! Should this be in or not?
2128       (unless iheader
2129         (setcar thread nil))
2130       (when parent
2131         (delq thread parent))
2132       (if (gnus-summary-insert-subject id header iheader)
2133           ;; Set the (possibly) new article number in the data structure.
2134           (gnus-data-set-number data (gnus-id-to-article id))
2135         (setcar thread old)
2136         nil))))
2137
2138 (defun gnus-rebuild-thread (id)
2139   "Rebuild the thread containing ID."
2140   (let ((buffer-read-only nil)
2141         current thread data)
2142     (if (not gnus-show-threads)
2143         (setq thread (list (car (gnus-id-to-thread id))))
2144       ;; Get the thread this article is part of.
2145       (setq thread (gnus-remove-thread id)))
2146     (setq current (save-excursion
2147                     (and (zerop (forward-line -1))
2148                          (gnus-summary-article-number))))
2149     ;; If this is a gathered thread, we have to go some re-gathering.
2150     (when (stringp (car thread))
2151       (let ((subject (car thread))
2152             roots thr)
2153         (setq thread (cdr thread))
2154         (while thread
2155           (unless (memq (setq thr (gnus-id-to-thread
2156                                       (gnus-root-id
2157                                        (mail-header-id (caar thread)))))
2158                         roots)
2159             (push thr roots))
2160           (setq thread (cdr thread)))
2161         ;; We now have all (unique) roots.
2162         (if (= (length roots) 1)
2163             ;; All the loose roots are now one solid root.
2164             (setq thread (car roots))
2165           (setq thread (cons subject (gnus-sort-threads roots))))))
2166     (let (threads)
2167       ;; We then insert this thread into the summary buffer.
2168       (let (gnus-newsgroup-data gnus-newsgroup-threads)
2169         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
2170         (setq data (nreverse gnus-newsgroup-data))
2171         (setq threads gnus-newsgroup-threads))
2172       ;; We splice the new data into the data structure.
2173       (gnus-data-enter-list current data)
2174       (gnus-data-compute-positions)
2175       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
2176
2177 (defun gnus-number-to-header (number)
2178   "Return the header for article NUMBER."
2179   (let ((headers gnus-newsgroup-headers))
2180     (while (and headers
2181                 (not (= number (mail-header-number (car headers)))))
2182       (pop headers))
2183     (when headers
2184       (car headers))))
2185
2186 (defun gnus-parent-headers (headers &optional generation)
2187   "Return the headers of the GENERATIONeth parent of HEADERS."
2188   (unless generation 
2189     (setq generation 1))
2190   (let (references parent)
2191     (while (and headers (not (zerop generation)))
2192       (setq references (mail-header-references headers))
2193       (when (and references
2194                  (setq parent (gnus-parent-id references))
2195                  (setq headers (car (gnus-id-to-thread parent))))
2196         (decf generation)))
2197     headers))
2198
2199 (defun gnus-id-to-thread (id)
2200   "Return the (sub-)thread where ID appears."
2201   (gnus-gethash id gnus-newsgroup-dependencies))
2202
2203 (defun gnus-id-to-article (id)
2204   "Return the article number of ID."
2205   (let ((thread (gnus-id-to-thread id)))
2206     (when (and thread
2207                (car thread))
2208       (mail-header-number (car thread)))))
2209
2210 (defun gnus-id-to-header (id)
2211   "Return the article headers of ID."
2212   (car (gnus-id-to-thread id)))
2213
2214 (defun gnus-article-displayed-root-p (article)
2215   "Say whether ARTICLE is a root(ish) article."
2216   (let ((level (gnus-summary-thread-level article))
2217         (refs (mail-header-references  (gnus-summary-article-header article)))
2218         particle)
2219     (cond 
2220      ((null level) nil)
2221      ((zerop level) t)
2222      ((null refs) t)
2223      ((null (gnus-parent-id refs)) t)
2224      ((and (= 1 level)
2225            (null (setq particle (gnus-id-to-article
2226                                  (gnus-parent-id refs))))
2227            (null (gnus-summary-thread-level particle)))))))
2228
2229 (defun gnus-root-id (id)
2230   "Return the id of the root of the thread where ID appears."
2231   (let (last-id prev)
2232     (while (and id (setq prev (car (gnus-gethash 
2233                                     id gnus-newsgroup-dependencies))))
2234       (setq last-id id
2235             id (gnus-parent-id (mail-header-references prev))))
2236     last-id))
2237
2238 (defun gnus-remove-thread (id &optional dont-remove)
2239   "Remove the thread that has ID in it."
2240   (let ((dep gnus-newsgroup-dependencies)
2241         headers thread last-id)
2242     ;; First go up in this thread until we find the root.
2243     (setq last-id (gnus-root-id id))
2244     (setq headers (list (car (gnus-id-to-thread last-id))
2245                         (caadr (gnus-id-to-thread last-id))))
2246     ;; We have now found the real root of this thread.  It might have
2247     ;; been gathered into some loose thread, so we have to search
2248     ;; through the threads to find the thread we wanted.
2249     (let ((threads gnus-newsgroup-threads)
2250           sub)
2251       (while threads
2252         (setq sub (car threads))
2253         (if (stringp (car sub))
2254             ;; This is a gathered thread, so we look at the roots
2255             ;; below it to find whether this article is in this
2256             ;; gathered root.
2257             (progn
2258               (setq sub (cdr sub))
2259               (while sub
2260                 (when (member (caar sub) headers)
2261                   (setq thread (car threads)
2262                         threads nil
2263                         sub nil))
2264                 (setq sub (cdr sub))))
2265           ;; It's an ordinary thread, so we check it.
2266           (when (eq (car sub) (car headers))
2267             (setq thread sub
2268                   threads nil)))
2269         (setq threads (cdr threads)))
2270       ;; If this article is in no thread, then it's a root.
2271       (if thread
2272           (unless dont-remove
2273             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
2274         (setq thread (gnus-gethash last-id dep)))
2275       (when thread
2276         (prog1
2277             thread ; We return this thread.
2278           (unless dont-remove
2279             (if (stringp (car thread))
2280                 (progn
2281                   ;; If we use dummy roots, then we have to remove the
2282                   ;; dummy root as well.
2283                   (when (eq gnus-summary-make-false-root 'dummy)
2284                     ;; Uhm.
2285                     )
2286                   (setq thread (cdr thread))
2287                   (while thread
2288                     (gnus-remove-thread-1 (car thread))
2289                     (setq thread (cdr thread))))
2290               (gnus-remove-thread-1 thread))))))))
2291
2292 (defun gnus-remove-thread-1 (thread)
2293   "Remove the thread THREAD recursively."
2294   (let ((number (mail-header-number (car thread)))
2295         pos)
2296     (when (setq pos (text-property-any
2297                      (point-min) (point-max) 'gnus-number number))
2298       (goto-char pos)
2299       (gnus-delete-line)
2300       (gnus-data-remove number))
2301     (setq thread (cdr thread))
2302     (while thread
2303       (gnus-remove-thread-1 (pop thread)))))
2304
2305 (defun gnus-sort-threads (threads)
2306   "Sort THREADS."
2307   (if (not gnus-thread-sort-functions)
2308       threads
2309     (let ((func (if (= 1 (length gnus-thread-sort-functions))
2310                     (car gnus-thread-sort-functions)
2311                   `(lambda (t1 t2)
2312                      ,(gnus-make-sort-function 
2313                        (reverse gnus-thread-sort-functions))))))
2314       (gnus-message 7 "Sorting threads...")
2315       (prog1
2316           (sort threads func)
2317         (gnus-message 7 "Sorting threads...done")))))
2318
2319 (defun gnus-sort-articles (articles)
2320   "Sort ARTICLES."
2321   (when gnus-article-sort-functions
2322     (let ((func (if (= 1 (length gnus-article-sort-functions))
2323                     (car gnus-article-sort-functions)
2324                   `(lambda (t1 t2)
2325                      ,(gnus-make-sort-function 
2326                        (reverse gnus-article-sort-functions))))))
2327       (gnus-message 7 "Sorting articles...")
2328       (prog1
2329           (setq gnus-newsgroup-headers (sort articles func))
2330         (gnus-message 7 "Sorting articles...done")))))
2331
2332 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2333 (defmacro gnus-thread-header (thread)
2334   ;; Return header of first article in THREAD.
2335   ;; Note that THREAD must never, ever be anything else than a variable -
2336   ;; using some other form will lead to serious barfage.
2337   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
2338   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
2339   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
2340         (vector thread) 2))
2341
2342 (defsubst gnus-article-sort-by-number (h1 h2)
2343   "Sort articles by article number."
2344   (< (mail-header-number h1)
2345      (mail-header-number h2)))
2346
2347 (defun gnus-thread-sort-by-number (h1 h2)
2348   "Sort threads by root article number."
2349   (gnus-article-sort-by-number
2350    (gnus-thread-header h1) (gnus-thread-header h2)))
2351
2352 (defsubst gnus-article-sort-by-author (h1 h2)
2353   "Sort articles by root author."
2354   (string-lessp
2355    (let ((extract (funcall
2356                    gnus-extract-address-components
2357                    (mail-header-from h1))))
2358      (or (car extract) (cdr extract)))
2359    (let ((extract (funcall
2360                    gnus-extract-address-components
2361                    (mail-header-from h2))))
2362      (or (car extract) (cdr extract)))))
2363
2364 (defun gnus-thread-sort-by-author (h1 h2)
2365   "Sort threads by root author."
2366   (gnus-article-sort-by-author
2367    (gnus-thread-header h1)  (gnus-thread-header h2)))
2368
2369 (defsubst gnus-article-sort-by-subject (h1 h2)
2370   "Sort articles by root subject."
2371   (string-lessp
2372    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
2373    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
2374
2375 (defun gnus-thread-sort-by-subject (h1 h2)
2376   "Sort threads by root subject."
2377   (gnus-article-sort-by-subject
2378    (gnus-thread-header h1) (gnus-thread-header h2)))
2379
2380 (defsubst gnus-article-sort-by-date (h1 h2)
2381   "Sort articles by root article date."
2382   (string-lessp
2383    (inline (gnus-sortable-date (mail-header-date h1)))
2384    (inline (gnus-sortable-date (mail-header-date h2)))))
2385
2386 (defun gnus-thread-sort-by-date (h1 h2)
2387   "Sort threads by root article date."
2388   (gnus-article-sort-by-date
2389    (gnus-thread-header h1) (gnus-thread-header h2)))
2390
2391 (defsubst gnus-article-sort-by-score (h1 h2)
2392   "Sort articles by root article score.
2393 Unscored articles will be counted as having a score of zero."
2394   (> (or (cdr (assq (mail-header-number h1)
2395                     gnus-newsgroup-scored))
2396          gnus-summary-default-score 0)
2397      (or (cdr (assq (mail-header-number h2)
2398                     gnus-newsgroup-scored))
2399          gnus-summary-default-score 0)))
2400
2401 (defun gnus-thread-sort-by-score (h1 h2)
2402   "Sort threads by root article score."
2403   (gnus-article-sort-by-score
2404    (gnus-thread-header h1) (gnus-thread-header h2)))
2405
2406 (defun gnus-thread-sort-by-total-score (h1 h2)
2407   "Sort threads by the sum of all scores in the thread.
2408 Unscored articles will be counted as having a score of zero."
2409   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
2410
2411 (defun gnus-thread-total-score (thread)
2412   ;;  This function find the total score of THREAD.
2413   (cond ((null thread)
2414          0)
2415         ((consp thread)
2416          (if (stringp (car thread))
2417              (apply gnus-thread-score-function 0
2418                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
2419            (gnus-thread-total-score-1 thread)))
2420         (t
2421          (gnus-thread-total-score-1 (list thread)))))
2422
2423 (defun gnus-thread-total-score-1 (root)
2424   ;; This function find the total score of the thread below ROOT.
2425   (setq root (car root))
2426   (apply gnus-thread-score-function
2427          (or (append
2428               (mapcar 'gnus-thread-total-score
2429                       (cdr (gnus-gethash (mail-header-id root)
2430                                          gnus-newsgroup-dependencies)))
2431                  (if (> (mail-header-number root) 0)
2432                      (list (or (cdr (assq (mail-header-number root) 
2433                                           gnus-newsgroup-scored))
2434                                gnus-summary-default-score 0))))
2435              (list gnus-summary-default-score)
2436              '(0))))
2437
2438 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
2439 (defvar gnus-tmp-prev-subject nil)
2440 (defvar gnus-tmp-false-parent nil)
2441 (defvar gnus-tmp-root-expunged nil)
2442 (defvar gnus-tmp-dummy-line nil)
2443
2444 (defun gnus-summary-prepare-threads (threads)
2445   "Prepare summary buffer from THREADS and indentation LEVEL.
2446 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
2447 or a straight list of headers."
2448   (gnus-message 7 "Generating summary...")
2449
2450   (setq gnus-newsgroup-threads threads)
2451   (beginning-of-line)
2452
2453   (let ((gnus-tmp-level 0)
2454         (default-score (or gnus-summary-default-score 0))
2455         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
2456         thread number subject stack state gnus-tmp-gathered beg-match
2457         new-roots gnus-tmp-new-adopts thread-end
2458         gnus-tmp-header gnus-tmp-unread
2459         gnus-tmp-replied gnus-tmp-subject-or-nil
2460         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
2461         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
2462         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
2463
2464     (setq gnus-tmp-prev-subject nil)
2465
2466     (if (vectorp (car threads))
2467         ;; If this is a straight (sic) list of headers, then a
2468         ;; threaded summary display isn't required, so we just create
2469         ;; an unthreaded one.
2470         (gnus-summary-prepare-unthreaded threads)
2471
2472       ;; Do the threaded display.
2473
2474       (while (or threads stack gnus-tmp-new-adopts new-roots)
2475
2476         (if (and (= gnus-tmp-level 0)
2477                  (not (setq gnus-tmp-dummy-line nil))
2478                  (or (not stack)
2479                      (= (caar stack) 0))
2480                  (not gnus-tmp-false-parent)
2481                  (or gnus-tmp-new-adopts new-roots))
2482             (if gnus-tmp-new-adopts
2483                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
2484                       thread (list (car gnus-tmp-new-adopts))
2485                       gnus-tmp-header (caar thread)
2486                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
2487               (if new-roots
2488                   (setq thread (list (car new-roots))
2489                         gnus-tmp-header (caar thread)
2490                         new-roots (cdr new-roots))))
2491
2492           (if threads
2493               ;; If there are some threads, we do them before the
2494               ;; threads on the stack.
2495               (setq thread threads
2496                     gnus-tmp-header (caar thread))
2497             ;; There were no current threads, so we pop something off
2498             ;; the stack.
2499             (setq state (car stack)
2500                   gnus-tmp-level (car state)
2501                   thread (cdr state)
2502                   stack (cdr stack)
2503                   gnus-tmp-header (caar thread))))
2504
2505         (setq gnus-tmp-false-parent nil)
2506         (setq gnus-tmp-root-expunged nil)
2507         (setq thread-end nil)
2508
2509         (if (stringp gnus-tmp-header)
2510             ;; The header is a dummy root.
2511             (cond
2512              ((eq gnus-summary-make-false-root 'adopt)
2513               ;; We let the first article adopt the rest.
2514               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
2515                                                (cddar thread)))
2516               (setq gnus-tmp-gathered
2517                     (nconc (mapcar
2518                             (lambda (h) (mail-header-number (car h)))
2519                             (cddar thread))
2520                            gnus-tmp-gathered))
2521               (setq thread (cons (list (caar thread)
2522                                        (cadar thread))
2523                                  (cdr thread)))
2524               (setq gnus-tmp-level -1
2525                     gnus-tmp-false-parent t))
2526              ((eq gnus-summary-make-false-root 'empty)
2527               ;; We print adopted articles with empty subject fields.
2528               (setq gnus-tmp-gathered
2529                     (nconc (mapcar
2530                             (lambda (h) (mail-header-number (car h)))
2531                             (cddar thread))
2532                            gnus-tmp-gathered))
2533               (setq gnus-tmp-level -1))
2534              ((eq gnus-summary-make-false-root 'dummy)
2535               ;; We remember that we probably want to output a dummy
2536               ;; root.
2537               (setq gnus-tmp-dummy-line gnus-tmp-header)
2538               (setq gnus-tmp-prev-subject gnus-tmp-header))
2539              (t
2540               ;; We do not make a root for the gathered
2541               ;; sub-threads at all.
2542               (setq gnus-tmp-level -1)))
2543
2544           (setq number (mail-header-number gnus-tmp-header)
2545                 subject (mail-header-subject gnus-tmp-header))
2546
2547           (cond
2548            ;; If the thread has changed subject, we might want to make
2549            ;; this subthread into a root.
2550            ((and (null gnus-thread-ignore-subject)
2551                  (not (zerop gnus-tmp-level))
2552                  gnus-tmp-prev-subject
2553                  (not (inline
2554                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
2555             (setq new-roots (nconc new-roots (list (car thread)))
2556                   thread-end t
2557                   gnus-tmp-header nil))
2558            ;; If the article lies outside the current limit,
2559            ;; then we do not display it.
2560            ((and (not (memq number gnus-newsgroup-limit))
2561                  (not gnus-tmp-dummy-line))
2562             (setq gnus-tmp-gathered
2563                   (nconc (mapcar
2564                           (lambda (h) (mail-header-number (car h)))
2565                           (cdar thread))
2566                          gnus-tmp-gathered))
2567             (setq gnus-tmp-new-adopts (if (cdar thread)
2568                                           (append gnus-tmp-new-adopts
2569                                                   (cdar thread))
2570                                         gnus-tmp-new-adopts)
2571                   thread-end t
2572                   gnus-tmp-header nil)
2573             (when (zerop gnus-tmp-level)
2574               (setq gnus-tmp-root-expunged t)))
2575            ;; Perhaps this article is to be marked as read?
2576            ((and gnus-summary-mark-below
2577                  (< (or (cdr (assq number gnus-newsgroup-scored))
2578                         default-score)
2579                     gnus-summary-mark-below)
2580                  ;; Don't touch sparse articles.
2581                  (not (memq number gnus-newsgroup-sparse))
2582                  (not (memq number gnus-newsgroup-ancient)))
2583             (setq gnus-newsgroup-unreads
2584                   (delq number gnus-newsgroup-unreads))
2585             (if gnus-newsgroup-auto-expire
2586                 (push number gnus-newsgroup-expirable)
2587               (push (cons number gnus-low-score-mark)
2588                     gnus-newsgroup-reads))))
2589
2590           (when gnus-tmp-header
2591             ;; We may have an old dummy line to output before this
2592             ;; article.
2593             (when gnus-tmp-dummy-line
2594               (gnus-summary-insert-dummy-line
2595                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
2596               (setq gnus-tmp-dummy-line nil))
2597
2598             ;; Compute the mark.
2599             (setq
2600              gnus-tmp-unread
2601              (cond
2602               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
2603               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
2604               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
2605               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
2606               (t (or (cdr (assq number gnus-newsgroup-reads))
2607                      gnus-ancient-mark))))
2608
2609             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
2610                                   gnus-tmp-header gnus-tmp-level)
2611                   gnus-newsgroup-data)
2612
2613             ;; Actually insert the line.
2614             (setq
2615              gnus-tmp-subject-or-nil
2616              (cond
2617               ((and gnus-thread-ignore-subject
2618                     gnus-tmp-prev-subject
2619                     (not (inline (gnus-subject-equal
2620                                   gnus-tmp-prev-subject subject))))
2621                subject)
2622               ((zerop gnus-tmp-level)
2623                (if (and (eq gnus-summary-make-false-root 'empty)
2624                         (memq number gnus-tmp-gathered)
2625                         gnus-tmp-prev-subject
2626                         (inline (gnus-subject-equal
2627                                  gnus-tmp-prev-subject subject)))
2628                    gnus-summary-same-subject
2629                  subject))
2630               (t gnus-summary-same-subject)))
2631             (if (and (eq gnus-summary-make-false-root 'adopt)
2632                      (= gnus-tmp-level 1)
2633                      (memq number gnus-tmp-gathered))
2634                 (setq gnus-tmp-opening-bracket ?\<
2635                       gnus-tmp-closing-bracket ?\>)
2636               (setq gnus-tmp-opening-bracket ?\[
2637                     gnus-tmp-closing-bracket ?\]))
2638             (setq
2639              gnus-tmp-indentation
2640              (aref gnus-thread-indent-array gnus-tmp-level)
2641              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
2642              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
2643                                 gnus-summary-default-score 0)
2644              gnus-tmp-score-char
2645              (if (or (null gnus-summary-default-score)
2646                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2647                          gnus-summary-zcore-fuzz)) ? 
2648                (if (< gnus-tmp-score gnus-summary-default-score)
2649                    gnus-score-below-mark gnus-score-over-mark))
2650              gnus-tmp-replied
2651              (cond ((memq number gnus-newsgroup-processable)
2652                     gnus-process-mark)
2653                    ((memq number gnus-newsgroup-cached)
2654                     gnus-cached-mark)
2655                    ((memq number gnus-newsgroup-replied)
2656                     gnus-replied-mark)
2657                    ((memq number gnus-newsgroup-saved)
2658                     gnus-saved-mark)
2659                    (t gnus-unread-mark))
2660              gnus-tmp-from (mail-header-from gnus-tmp-header)
2661              gnus-tmp-name
2662              (cond
2663               ((string-match "(.+)" gnus-tmp-from)
2664                (substring gnus-tmp-from
2665                           (1+ (match-beginning 0)) (1- (match-end 0))))
2666               ((string-match "<[^>]+> *$" gnus-tmp-from)
2667                (setq beg-match (match-beginning 0))
2668                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2669                         (substring gnus-tmp-from (1+ (match-beginning 0))
2670                                    (1- (match-end 0))))
2671                    (substring gnus-tmp-from 0 beg-match)))
2672               (t gnus-tmp-from)))
2673             (when (string= gnus-tmp-name "")
2674               (setq gnus-tmp-name gnus-tmp-from))
2675             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
2676             (gnus-put-text-property
2677              (point)
2678              (progn (eval gnus-summary-line-format-spec) (point))
2679              'gnus-number number)
2680             (when gnus-visual-p
2681               (forward-line -1)
2682               (run-hooks 'gnus-summary-update-hook)
2683               (forward-line 1))
2684
2685             (setq gnus-tmp-prev-subject subject)))
2686
2687         (when (nth 1 thread)
2688           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
2689         (incf gnus-tmp-level)
2690         (setq threads (if thread-end nil (cdar thread)))
2691         (unless threads
2692           (setq gnus-tmp-level 0)))))
2693   (gnus-message 7 "Generating summary...done"))
2694
2695 (defun gnus-summary-prepare-unthreaded (headers)
2696   "Generate an unthreaded summary buffer based on HEADERS."
2697   (let (header number mark)
2698
2699     (while headers
2700       ;; We may have to root out some bad articles...
2701       (when (memq (setq number (mail-header-number
2702                                 (setq header (pop headers))))
2703                   gnus-newsgroup-limit)
2704         ;; Mark article as read when it has a low score.
2705         (when (and gnus-summary-mark-below
2706                    (< (or (cdr (assq number gnus-newsgroup-scored))
2707                           gnus-summary-default-score 0)
2708                       gnus-summary-mark-below)
2709                    (not (memq number gnus-newsgroup-ancient)))
2710           (setq gnus-newsgroup-unreads
2711                 (delq number gnus-newsgroup-unreads))
2712           (if gnus-newsgroup-auto-expire
2713               (push number gnus-newsgroup-expirable)
2714             (push (cons number gnus-low-score-mark)
2715                   gnus-newsgroup-reads)))
2716
2717         (setq mark
2718               (cond
2719                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
2720                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
2721                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
2722                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
2723                (t (or (cdr (assq number gnus-newsgroup-reads))
2724                       gnus-ancient-mark))))
2725         (setq gnus-newsgroup-data
2726               (cons (gnus-data-make number mark (1+ (point)) header 0)
2727                     gnus-newsgroup-data))
2728         (gnus-summary-insert-line
2729          header 0 nil mark (memq number gnus-newsgroup-replied)
2730          (memq number gnus-newsgroup-expirable)
2731          (mail-header-subject header) nil
2732          (cdr (assq number gnus-newsgroup-scored))
2733          (memq number gnus-newsgroup-processable))))))
2734
2735 (defun gnus-select-newsgroup (group &optional read-all)
2736   "Select newsgroup GROUP.
2737 If READ-ALL is non-nil, all articles in the group are selected."
2738   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
2739          (info (nth 2 entry))
2740          articles fetched-articles cached)
2741
2742     (or (gnus-check-server
2743          (setq gnus-current-select-method (gnus-find-method-for-group group)))
2744         (error "Couldn't open server"))
2745
2746     (or (and entry (not (eq (car entry) t))) ; Either it's active...
2747         (gnus-activate-group group)     ; Or we can activate it...
2748         (progn                          ; Or we bug out.
2749           (when (equal major-mode 'gnus-summary-mode)
2750             (kill-buffer (current-buffer)))
2751           (error "Couldn't request group %s: %s"
2752                  group (gnus-status-message group))))
2753
2754     (unless (gnus-request-group group t)
2755       (when (equal major-mode 'gnus-summary-mode)
2756         (kill-buffer (current-buffer)))
2757       (error "Couldn't request group %s: %s"
2758              group (gnus-status-message group)))      
2759
2760     (setq gnus-newsgroup-name group)
2761     (setq gnus-newsgroup-unselected nil)
2762     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
2763
2764     ;; Adjust and set lists of article marks.
2765     (when info
2766       (gnus-adjust-marked-articles info))
2767
2768     ;; Kludge to avoid having cached articles nixed out in virtual groups.
2769     (when (gnus-virtual-group-p group)
2770       (setq cached gnus-newsgroup-cached))
2771
2772     (setq gnus-newsgroup-unreads
2773           (gnus-set-difference
2774            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
2775            gnus-newsgroup-dormant))
2776
2777     (setq gnus-newsgroup-processable nil)
2778
2779     (setq articles (gnus-articles-to-read group read-all))
2780
2781     (cond
2782      ((null articles)
2783       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
2784       'quit)
2785      ((eq articles 0) nil)
2786      (t
2787       ;; Init the dependencies hash table.
2788       (setq gnus-newsgroup-dependencies
2789             (gnus-make-hashtable (length articles)))
2790       ;; Retrieve the headers and read them in.
2791       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
2792       (setq gnus-newsgroup-headers
2793             (if (eq 'nov
2794                     (setq gnus-headers-retrieved-by
2795                           (gnus-retrieve-headers
2796                            articles gnus-newsgroup-name
2797                            ;; We might want to fetch old headers, but
2798                            ;; not if there is only 1 article.
2799                            (and gnus-fetch-old-headers
2800                                 (or (and
2801                                      (not (eq gnus-fetch-old-headers 'some))
2802                                      (not (numberp gnus-fetch-old-headers)))
2803                                     (> (length articles) 1))))))
2804                 (gnus-get-newsgroup-headers-xover articles)
2805               (gnus-get-newsgroup-headers)))
2806       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
2807
2808       ;; Kludge to avoid having cached articles nixed out in virtual groups.
2809       (when cached
2810         (setq gnus-newsgroup-cached cached))
2811
2812       ;; Set the initial limit.
2813       (setq gnus-newsgroup-limit (copy-sequence articles))
2814       ;; Remove canceled articles from the list of unread articles.
2815       (setq gnus-newsgroup-unreads
2816             (gnus-set-sorted-intersection
2817              gnus-newsgroup-unreads
2818              (setq fetched-articles
2819                    (mapcar (lambda (headers) (mail-header-number headers))
2820                            gnus-newsgroup-headers))))
2821       ;; Removed marked articles that do not exist.
2822       (gnus-update-missing-marks
2823        (gnus-sorted-complement fetched-articles articles))
2824       ;; We might want to build some more threads first.
2825       (and gnus-fetch-old-headers
2826            (eq gnus-headers-retrieved-by 'nov)
2827            (gnus-build-old-threads))
2828       ;; Check whether auto-expire is to be done in this group.
2829       (setq gnus-newsgroup-auto-expire
2830             (gnus-group-auto-expirable-p group))
2831       ;; Set up the article buffer now, if necessary.
2832       (unless gnus-single-article-buffer
2833         (gnus-article-setup-buffer))
2834       ;; First and last article in this newsgroup.
2835       (when gnus-newsgroup-headers
2836         (setq gnus-newsgroup-begin
2837               (mail-header-number (car gnus-newsgroup-headers))
2838               gnus-newsgroup-end
2839               (mail-header-number
2840                (gnus-last-element gnus-newsgroup-headers))))
2841       ;; GROUP is successfully selected.
2842       (or gnus-newsgroup-headers t)))))
2843
2844 (defun gnus-articles-to-read (group read-all)
2845   ;; Find out what articles the user wants to read.
2846   (let* ((articles
2847           ;; Select all articles if `read-all' is non-nil, or if there
2848           ;; are no unread articles.
2849           (if (or read-all
2850                   (and (zerop (length gnus-newsgroup-marked))
2851                        (zerop (length gnus-newsgroup-unreads))))
2852               (gnus-uncompress-range (gnus-active group))
2853             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
2854                           (copy-sequence gnus-newsgroup-unreads))
2855                   '<)))
2856          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
2857          (scored (length scored-list))
2858          (number (length articles))
2859          (marked (+ (length gnus-newsgroup-marked)
2860                     (length gnus-newsgroup-dormant)))
2861          (select
2862           (cond
2863            ((numberp read-all)
2864             read-all)
2865            (t
2866             (condition-case ()
2867                 (cond
2868                  ((and (or (<= scored marked) (= scored number))
2869                        (numberp gnus-large-newsgroup)
2870                        (> number gnus-large-newsgroup))
2871                   (let ((input
2872                          (read-string
2873                           (format
2874                            "How many articles from %s (default %d): "
2875                            gnus-newsgroup-name number))))
2876                     (if (string-match "^[ \t]*$" input) number input)))
2877                  ((and (> scored marked) (< scored number)
2878                        (> (- scored number) 20))
2879                   (let ((input
2880                          (read-string
2881                           (format "%s %s (%d scored, %d total): "
2882                                   "How many articles from"
2883                                   group scored number))))
2884                     (if (string-match "^[ \t]*$" input)
2885                         number input)))
2886                  (t number))
2887               (quit nil))))))
2888     (setq select (if (stringp select) (string-to-number select) select))
2889     (if (or (null select) (zerop select))
2890         select
2891       (if (and (not (zerop scored)) (<= (abs select) scored))
2892           (progn
2893             (setq articles (sort scored-list '<))
2894             (setq number (length articles)))
2895         (setq articles (copy-sequence articles)))
2896
2897       (if (< (abs select) number)
2898           (if (< select 0)
2899               ;; Select the N oldest articles.
2900               (setcdr (nthcdr (1- (abs select)) articles) nil)
2901             ;; Select the N most recent articles.
2902             (setq articles (nthcdr (- number select) articles))))
2903       (setq gnus-newsgroup-unselected
2904             (gnus-sorted-intersection
2905              gnus-newsgroup-unreads
2906              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
2907       articles)))
2908
2909 (defun gnus-killed-articles (killed articles)
2910   (let (out)
2911     (while articles
2912       (if (inline (gnus-member-of-range (car articles) killed))
2913           (setq out (cons (car articles) out)))
2914       (setq articles (cdr articles)))
2915     out))
2916
2917 (defun gnus-uncompress-marks (marks)
2918   "Uncompress the mark ranges in MARKS."
2919   (let ((uncompressed '(score bookmark))
2920         out)
2921     (while marks
2922       (if (memq (caar marks) uncompressed)
2923           (push (car marks) out)
2924         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
2925       (setq marks (cdr marks)))
2926     out))
2927
2928 (defun gnus-adjust-marked-articles (info)
2929   "Set all article lists and remove all marks that are no longer legal."
2930   (let* ((marked-lists (gnus-info-marks info))
2931          (active (gnus-active (gnus-info-group info)))
2932          (min (car active))
2933          (max (cdr active))
2934          (types gnus-article-mark-lists)
2935          (uncompressed '(score bookmark killed))
2936          marks var articles article mark)
2937
2938     (while marked-lists
2939       (setq marks (pop marked-lists))
2940       (set (setq var (intern (format "gnus-newsgroup-%s"
2941                                      (car (rassq (setq mark (car marks))
2942                                                  types)))))
2943            (if (memq (car marks) uncompressed) (cdr marks)
2944              (gnus-uncompress-range (cdr marks))))
2945
2946       (setq articles (symbol-value var))
2947
2948       ;; All articles have to be subsets of the active articles.
2949       (cond
2950        ;; Adjust "simple" lists.
2951        ((memq mark '(tick dormant expirable reply save))
2952         (while articles
2953           (when (or (< (setq article (pop articles)) min) (> article max))
2954             (set var (delq article (symbol-value var))))))
2955        ;; Adjust assocs.
2956        ((memq mark uncompressed)
2957         (while articles
2958           (when (or (not (consp (setq article (pop articles))))
2959                     (< (car article) min)
2960                     (> (car article) max))
2961             (set var (delq article (symbol-value var))))))))))
2962
2963 (defun gnus-update-missing-marks (missing)
2964   "Go through the list of MISSING articles and remove them mark lists."
2965   (when missing
2966     (let ((types gnus-article-mark-lists)
2967           var m)
2968       ;; Go through all types.
2969       (while types
2970         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
2971         (when (symbol-value var)
2972           ;; This list has articles.  So we delete all missing articles
2973           ;; from it.
2974           (setq m missing)
2975           (while m
2976             (set var (delq (pop m) (symbol-value var)))))))))
2977
2978 (defun gnus-update-marks ()
2979   "Enter the various lists of marked articles into the newsgroup info list."
2980   (let ((types gnus-article-mark-lists)
2981         (info (gnus-get-info gnus-newsgroup-name))
2982         (uncompressed '(score bookmark killed))
2983         type list newmarked symbol)
2984     (when info
2985       ;; Add all marks lists that are non-nil to the list of marks lists.
2986       (while types
2987         (setq type (pop types))
2988         (when (setq list (symbol-value
2989                           (setq symbol
2990                                 (intern (format "gnus-newsgroup-%s"
2991                                                 (car type))))))
2992           (push (cons (cdr type)
2993                       (if (memq (cdr type) uncompressed) list
2994                         (gnus-compress-sequence 
2995                          (set symbol (sort list '<)) t)))
2996                 newmarked)))
2997
2998       ;; Enter these new marks into the info of the group.
2999       (if (nthcdr 3 info)
3000           (setcar (nthcdr 3 info) newmarked)
3001         ;; Add the marks lists to the end of the info.
3002         (when newmarked
3003           (setcdr (nthcdr 2 info) (list newmarked))))
3004
3005       ;; Cut off the end of the info if there's nothing else there.
3006       (let ((i 5))
3007         (while (and (> i 2)
3008                     (not (nth i info)))
3009           (when (nthcdr (decf i) info)
3010             (setcdr (nthcdr i info) nil)))))))
3011
3012 (defun gnus-add-marked-articles (group type articles &optional info force)
3013   ;; Add ARTICLES of TYPE to the info of GROUP.
3014   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
3015   ;; add, but replace marked articles of TYPE with ARTICLES.
3016   (let ((info (or info (gnus-get-info group)))
3017         (uncompressed '(score bookmark killed))
3018         marked m)
3019     (or (not info)
3020         (and (not (setq marked (nthcdr 3 info)))
3021              (or (null articles)
3022                  (setcdr (nthcdr 2 info)
3023                          (list (list (cons type (gnus-compress-sequence
3024                                                  articles t)))))))
3025         (and (not (setq m (assq type (car marked))))
3026              (or (null articles)
3027                  (setcar marked
3028                          (cons (cons type (gnus-compress-sequence articles t) )
3029                                (car marked)))))
3030         (if force
3031             (if (null articles)
3032                 (setcar (nthcdr 3 info)
3033                         (delq (assq type (car marked)) (car marked)))
3034               (setcdr m (gnus-compress-sequence articles t)))
3035           (setcdr m (gnus-compress-sequence
3036                      (sort (nconc (gnus-uncompress-range (cdr m))
3037                                   (copy-sequence articles)) '<) t))))))
3038
3039 (defun gnus-set-mode-line (where)
3040   "This function sets the mode line of the article or summary buffers.
3041 If WHERE is `summary', the summary mode line format will be used."
3042   ;; Is this mode line one we keep updated?
3043   (when (memq where gnus-updated-mode-lines)
3044     (let (mode-string)
3045       (save-excursion
3046         ;; We evaluate this in the summary buffer since these
3047         ;; variables are buffer-local to that buffer.
3048         (set-buffer gnus-summary-buffer)
3049         ;; We bind all these variables that are used in the `eval' form
3050         ;; below.
3051         (let* ((mformat (symbol-value
3052                          (intern
3053                           (format "gnus-%s-mode-line-format-spec" where))))
3054                (gnus-tmp-group-name gnus-newsgroup-name)
3055                (gnus-tmp-article-number (or gnus-current-article 0))
3056                (gnus-tmp-unread gnus-newsgroup-unreads)
3057                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
3058                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
3059                (gnus-tmp-unread-and-unselected
3060                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
3061                             (zerop gnus-tmp-unselected)) "")
3062                       ((zerop gnus-tmp-unselected)
3063                        (format "{%d more}" gnus-tmp-unread-and-unticked))
3064                       (t (format "{%d(+%d) more}"
3065                                  gnus-tmp-unread-and-unticked
3066                                  gnus-tmp-unselected))))
3067                (gnus-tmp-subject
3068                 (if (and gnus-current-headers
3069                          (vectorp gnus-current-headers))
3070                     (gnus-mode-string-quote
3071                      (mail-header-subject gnus-current-headers)) ""))
3072                max-len
3073                gnus-tmp-header);; passed as argument to any user-format-funcs
3074           (setq mode-string (eval mformat))
3075           (setq max-len (max 4 (if gnus-mode-non-string-length
3076                                    (- (window-width)
3077                                       gnus-mode-non-string-length)
3078                                  (length mode-string))))
3079           ;; We might have to chop a bit of the string off...
3080           (when (> (length mode-string) max-len)
3081             (setq mode-string
3082                   (concat (gnus-truncate-string mode-string (- max-len 3))
3083                           "...")))
3084           ;; Pad the mode string a bit.
3085           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
3086       ;; Update the mode line.
3087       (setq mode-line-buffer-identification 
3088             (gnus-mode-line-buffer-identification
3089              (list mode-string)))
3090       (set-buffer-modified-p t))))
3091
3092 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
3093   "Go through the HEADERS list and add all Xrefs to a hash table.
3094 The resulting hash table is returned, or nil if no Xrefs were found."
3095   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
3096          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
3097          (xref-hashtb (make-vector 63 0))
3098          start group entry number xrefs header)
3099     (while headers
3100       (setq header (pop headers))
3101       (when (and (setq xrefs (mail-header-xref header))
3102                  (not (memq (setq number (mail-header-number header))
3103                             unreads)))
3104         (setq start 0)
3105         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
3106           (setq start (match-end 0))
3107           (setq group (if prefix
3108                           (concat prefix (substring xrefs (match-beginning 1)
3109                                                     (match-end 1)))
3110                         (substring xrefs (match-beginning 1) (match-end 1))))
3111           (setq number
3112                 (string-to-int (substring xrefs (match-beginning 2)
3113                                           (match-end 2))))
3114           (if (setq entry (gnus-gethash group xref-hashtb))
3115               (setcdr entry (cons number (cdr entry)))
3116             (gnus-sethash group (cons number nil) xref-hashtb)))))
3117     (and start xref-hashtb)))
3118
3119 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
3120   "Look through all the headers and mark the Xrefs as read."
3121   (let ((virtual (gnus-virtual-group-p from-newsgroup))
3122         name entry info xref-hashtb idlist method nth4)
3123     (save-excursion
3124       (set-buffer gnus-group-buffer)
3125       (when (setq xref-hashtb
3126                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
3127         (mapatoms
3128          (lambda (group)
3129            (unless (string= from-newsgroup (setq name (symbol-name group)))
3130              (setq idlist (symbol-value group))
3131              ;; Dead groups are not updated.
3132              (and (prog1
3133                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
3134                             info (nth 2 entry))
3135                     (if (stringp (setq nth4 (gnus-info-method info)))
3136                         (setq nth4 (gnus-server-to-method nth4))))
3137                   ;; Only do the xrefs if the group has the same
3138                   ;; select method as the group we have just read.
3139                   (or (gnus-methods-equal-p
3140                        nth4 (gnus-find-method-for-group from-newsgroup))
3141                       virtual
3142                       (equal nth4 (setq method (gnus-find-method-for-group
3143                                                 from-newsgroup)))
3144                       (and (equal (car nth4) (car method))
3145                            (equal (nth 1 nth4) (nth 1 method))))
3146                   gnus-use-cross-reference
3147                   (or (not (eq gnus-use-cross-reference t))
3148                       virtual
3149                       ;; Only do cross-references on subscribed
3150                       ;; groups, if that is what is wanted.
3151                       (<= (gnus-info-level info) gnus-level-subscribed))
3152                   (gnus-group-make-articles-read name idlist))))
3153          xref-hashtb)))))
3154
3155 (defun gnus-group-make-articles-read (group articles)
3156   (let* ((num 0)
3157          (entry (gnus-gethash group gnus-newsrc-hashtb))
3158          (info (nth 2 entry))
3159          (active (gnus-active group))
3160          range)
3161     ;; First peel off all illegal article numbers.
3162     (when active
3163       (let ((ids articles)
3164             id first)
3165         (while (setq id (pop ids))
3166           (when (and first (> id (cdr active)))
3167             ;; We'll end up in this situation in one particular
3168             ;; obscure situation.  If you re-scan a group and get
3169             ;; a new article that is cross-posted to a different
3170             ;; group that has not been re-scanned, you might get
3171             ;; crossposted article that has a higher number than
3172             ;; Gnus believes possible.  So we re-activate this
3173             ;; group as well.  This might mean doing the
3174             ;; crossposting thingy will *increase* the number
3175             ;; of articles in some groups.  Tsk, tsk.
3176             (setq active (or (gnus-activate-group group) active)))
3177           (when (or (> id (cdr active))
3178                     (< id (car active)))
3179             (setq articles (delq id articles))))))
3180     ;; If the read list is nil, we init it.
3181     (and active
3182          (null (gnus-info-read info))
3183          (> (car active) 1)
3184          (gnus-info-set-read info (cons 1 (1- (car active)))))
3185     ;; Then we add the read articles to the range.
3186     (gnus-info-set-read
3187      info
3188      (setq range
3189            (gnus-add-to-range
3190             (gnus-info-read info) (setq articles (sort articles '<)))))
3191     ;; Then we have to re-compute how many unread
3192     ;; articles there are in this group.
3193     (if active
3194         (progn
3195           (cond
3196            ((not range)
3197             (setq num (- (1+ (cdr active)) (car active))))
3198            ((not (listp (cdr range)))
3199             (setq num (- (cdr active) (- (1+ (cdr range))
3200                                          (car range)))))
3201            (t
3202             (while range
3203               (if (numberp (car range))
3204                   (setq num (1+ num))
3205                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
3206               (setq range (cdr range)))
3207             (setq num (- (cdr active) num))))
3208           ;; Update the number of unread articles.
3209           (setcar entry num)
3210           ;; Update the group buffer.
3211           (gnus-group-update-group group t)))))
3212
3213 (defun gnus-methods-equal-p (m1 m2)
3214   (let ((m1 (or m1 gnus-select-method))
3215         (m2 (or m2 gnus-select-method)))
3216     (or (equal m1 m2)
3217         (and (eq (car m1) (car m2))
3218              (or (not (memq 'address (assoc (symbol-name (car m1))
3219                                             gnus-valid-select-methods)))
3220                  (equal (nth 1 m1) (nth 1 m2)))))))
3221
3222 (defsubst gnus-header-value ()
3223   (buffer-substring (match-end 0) (gnus-point-at-eol)))
3224
3225 (defvar gnus-newsgroup-none-id 0)
3226
3227 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
3228   (let ((cur nntp-server-buffer)
3229         (dependencies
3230          (or dependencies
3231              (save-excursion (set-buffer gnus-summary-buffer)
3232                              gnus-newsgroup-dependencies)))
3233         headers id id-dep ref-dep end ref)
3234     (save-excursion
3235       (set-buffer nntp-server-buffer)
3236       (run-hooks 'gnus-parse-headers-hook)
3237       (let ((case-fold-search t)
3238             in-reply-to header p lines)
3239         (goto-char (point-min))
3240         ;; Search to the beginning of the next header.  Error messages
3241         ;; do not begin with 2 or 3.
3242         (while (re-search-forward "^[23][0-9]+ " nil t)
3243           (setq id nil
3244                 ref nil)
3245           ;; This implementation of this function, with nine
3246           ;; search-forwards instead of the one re-search-forward and
3247           ;; a case (which basically was the old function) is actually
3248           ;; about twice as fast, even though it looks messier.  You
3249           ;; can't have everything, I guess.  Speed and elegance
3250           ;; doesn't always go hand in hand.
3251           (setq
3252            header
3253            (vector
3254             ;; Number.
3255             (prog1
3256                 (read cur)
3257               (end-of-line)
3258               (setq p (point))
3259               (narrow-to-region (point)
3260                                 (or (and (search-forward "\n.\n" nil t)
3261                                          (- (point) 2))
3262                                     (point))))
3263             ;; Subject.
3264             (progn
3265               (goto-char p)
3266               (if (search-forward "\nsubject: " nil t)
3267                   (gnus-header-value) "(none)"))
3268             ;; From.
3269             (progn
3270               (goto-char p)
3271               (if (search-forward "\nfrom: " nil t)
3272                   (gnus-header-value) "(nobody)"))
3273             ;; Date.
3274             (progn
3275               (goto-char p)
3276               (if (search-forward "\ndate: " nil t)
3277                   (gnus-header-value) ""))
3278             ;; Message-ID.
3279             (progn
3280               (goto-char p)
3281               (if (search-forward "\nmessage-id: " nil t)
3282                   (setq id (gnus-header-value))
3283                 ;; If there was no message-id, we just fake one to make
3284                 ;; subsequent routines simpler.
3285                 (setq id (concat "none+"
3286                                  (int-to-string
3287                                   (setq gnus-newsgroup-none-id
3288                                         (1+ gnus-newsgroup-none-id)))))))
3289             ;; References.
3290             (progn
3291               (goto-char p)
3292               (if (search-forward "\nreferences: " nil t)
3293                   (progn
3294                     (setq end (point))
3295                     (prog1
3296                         (gnus-header-value)
3297                       (setq ref
3298                             (buffer-substring
3299                              (progn
3300                                (end-of-line)
3301                                (search-backward ">" end t)
3302                                (1+ (point)))
3303                              (progn
3304                                (search-backward "<" end t)
3305                                (point))))))
3306                 ;; Get the references from the in-reply-to header if there
3307                 ;; were no references and the in-reply-to header looks
3308                 ;; promising.
3309                 (if (and (search-forward "\nin-reply-to: " nil t)
3310                          (setq in-reply-to (gnus-header-value))
3311                          (string-match "<[^>]+>" in-reply-to))
3312                     (setq ref (substring in-reply-to (match-beginning 0)
3313                                          (match-end 0)))
3314                   (setq ref ""))))
3315             ;; Chars.
3316             0
3317             ;; Lines.
3318             (progn
3319               (goto-char p)
3320               (if (search-forward "\nlines: " nil t)
3321                   (if (numberp (setq lines (read cur)))
3322                       lines 0)
3323                 0))
3324             ;; Xref.
3325             (progn
3326               (goto-char p)
3327               (and (search-forward "\nxref: " nil t)
3328                    (gnus-header-value)))))
3329           ;; We do the threading while we read the headers.  The
3330           ;; message-id and the last reference are both entered into
3331           ;; the same hash table.  Some tippy-toeing around has to be
3332           ;; done in case an article has arrived before the article
3333           ;; which it refers to.
3334           (if (boundp (setq id-dep (intern id dependencies)))
3335               (if (and (car (symbol-value id-dep))
3336                        (not force-new))
3337                   ;; An article with this Message-ID has already
3338                   ;; been seen, so we ignore this one, except we add
3339                   ;; any additional Xrefs (in case the two articles
3340                   ;; came from different servers).
3341                   (progn
3342                     (mail-header-set-xref
3343                      (car (symbol-value id-dep))
3344                      (concat (or (mail-header-xref
3345                                   (car (symbol-value id-dep))) "")
3346                              (or (mail-header-xref header) "")))
3347                     (setq header nil))
3348                 (setcar (symbol-value id-dep) header))
3349             (set id-dep (list header)))
3350           (when header
3351             (if (boundp (setq ref-dep (intern ref dependencies)))
3352                 (setcdr (symbol-value ref-dep)
3353                         (nconc (cdr (symbol-value ref-dep))
3354                                (list (symbol-value id-dep))))
3355               (set ref-dep (list nil (symbol-value id-dep))))
3356             (setq headers (cons header headers)))
3357           (goto-char (point-max))
3358           (widen))
3359         (nreverse headers)))))
3360
3361 ;; The following macros and functions were written by Felix Lee
3362 ;; <flee@cse.psu.edu>.
3363
3364 (defmacro gnus-nov-read-integer ()
3365   '(prog1
3366        (if (= (following-char) ?\t)
3367            0
3368          (let ((num (condition-case nil (read buffer) (error nil))))
3369            (if (numberp num) num 0)))
3370      (or (eobp) (forward-char 1))))
3371
3372 (defmacro gnus-nov-skip-field ()
3373   '(search-forward "\t" eol 'move))
3374
3375 (defmacro gnus-nov-field ()
3376   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3377
3378 ;; Goes through the xover lines and returns a list of vectors
3379 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
3380                                                   force-new dependencies)
3381   "Parse the news overview data in the server buffer, and return a
3382 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
3383   ;; Get the Xref when the users reads the articles since most/some
3384   ;; NNTP servers do not include Xrefs when using XOVER.
3385   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
3386   (let ((cur nntp-server-buffer)
3387         (dependencies (or dependencies gnus-newsgroup-dependencies))
3388         number headers header)
3389     (save-excursion
3390       (set-buffer nntp-server-buffer)
3391       ;; Allow the user to mangle the headers before parsing them.
3392       (run-hooks 'gnus-parse-headers-hook)
3393       (goto-char (point-min))
3394       (while (and sequence (not (eobp)))
3395         (setq number (read cur))
3396         (while (and sequence (< (car sequence) number))
3397           (setq sequence (cdr sequence)))
3398         (and sequence
3399              (eq number (car sequence))
3400              (progn
3401                (setq sequence (cdr sequence))
3402                (if (setq header
3403                          (inline (gnus-nov-parse-line
3404                                   number dependencies force-new)))
3405                    (setq headers (cons header headers)))))
3406         (forward-line 1))
3407       (setq headers (nreverse headers)))
3408     headers))
3409
3410 ;; This function has to be called with point after the article number
3411 ;; on the beginning of the line.
3412 (defun gnus-nov-parse-line (number dependencies &optional force-new)
3413   (let ((none 0)
3414         (eol (gnus-point-at-eol))
3415         (buffer (current-buffer))
3416         header ref id id-dep ref-dep)
3417
3418     ;; overview: [num subject from date id refs chars lines misc]
3419     (narrow-to-region (point) eol)
3420     (or (eobp) (forward-char))
3421
3422     (condition-case nil
3423         (setq header
3424               (vector
3425                number                   ; number
3426                (gnus-nov-field)         ; subject
3427                (gnus-nov-field)         ; from
3428                (gnus-nov-field)         ; date
3429                (setq id (or (gnus-nov-field)
3430                             (concat "none+"
3431                                     (int-to-string
3432                                      (setq none (1+ none)))))) ; id
3433                (progn
3434                  (save-excursion
3435                    (let ((beg (point)))
3436                      (search-forward "\t" eol)
3437                      (if (search-backward ">" beg t)
3438                          (setq ref
3439                                (buffer-substring
3440                                 (1+ (point))
3441                                 (search-backward "<" beg t)))
3442                        (setq ref nil))))
3443                  (gnus-nov-field))      ; refs
3444                (gnus-nov-read-integer)  ; chars
3445                (gnus-nov-read-integer)  ; lines
3446                (if (= (following-char) ?\n)
3447                    nil
3448                  (gnus-nov-field))      ; misc
3449                ))
3450       (error (progn
3451                (gnus-error 4 "Strange nov line")
3452                (setq header nil)
3453                (goto-char eol))))
3454
3455     (widen)
3456
3457     ;; We build the thread tree.
3458     (when header
3459       (if (boundp (setq id-dep (intern id dependencies)))
3460           (if (and (car (symbol-value id-dep))
3461                    (not force-new))
3462               ;; An article with this Message-ID has already been seen,
3463               ;; so we ignore this one, except we add any additional
3464               ;; Xrefs (in case the two articles came from different
3465               ;; servers.
3466               (progn
3467                 (mail-header-set-xref
3468                  (car (symbol-value id-dep))
3469                  (concat (or (mail-header-xref
3470                               (car (symbol-value id-dep))) "")
3471                          (or (mail-header-xref header) "")))
3472                 (setq header nil))
3473             (setcar (symbol-value id-dep) header))
3474         (set id-dep (list header))))
3475     (when header
3476       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
3477           (setcdr (symbol-value ref-dep)
3478                   (nconc (cdr (symbol-value ref-dep))
3479                          (list (symbol-value id-dep))))
3480         (set ref-dep (list nil (symbol-value id-dep)))))
3481     header))
3482
3483 (defun gnus-article-get-xrefs ()
3484   "Fill in the Xref value in `gnus-current-headers', if necessary.
3485 This is meant to be called in `gnus-article-internal-prepare-hook'."
3486   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
3487                                  gnus-current-headers)))
3488     (or (not gnus-use-cross-reference)
3489         (not headers)
3490         (and (mail-header-xref headers)
3491              (not (string= (mail-header-xref headers) "")))
3492         (let ((case-fold-search t)
3493               xref)
3494           (save-restriction
3495             (nnheader-narrow-to-headers)
3496             (goto-char (point-min))
3497             (if (or (and (eq (downcase (following-char)) ?x)
3498                          (looking-at "Xref:"))
3499                     (search-forward "\nXref:" nil t))
3500                 (progn
3501                   (goto-char (1+ (match-end 0)))
3502                   (setq xref (buffer-substring (point)
3503                                                (progn (end-of-line) (point))))
3504                   (mail-header-set-xref headers xref))))))))
3505
3506 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
3507   "Find article ID and insert the summary line for that article."
3508   (let ((header (if (and old-header use-old-header)
3509                     old-header (gnus-read-header id)))
3510         (number (and (numberp id) id))
3511         pos)
3512     (when header
3513       ;; Rebuild the thread that this article is part of and go to the
3514       ;; article we have fetched.
3515       (when (and (not gnus-show-threads)
3516                  old-header)
3517         (when (setq pos (text-property-any
3518                          (point-min) (point-max) 'gnus-number 
3519                          (mail-header-number old-header)))
3520           (goto-char pos)
3521           (gnus-delete-line)
3522           (gnus-data-remove (mail-header-number old-header))))
3523       (when old-header
3524         (mail-header-set-number header (mail-header-number old-header)))
3525       (setq gnus-newsgroup-sparse
3526             (delq (setq number (mail-header-number header)) 
3527                   gnus-newsgroup-sparse))
3528       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
3529       (gnus-rebuild-thread (mail-header-id header))
3530       (gnus-summary-goto-subject number nil t))
3531     (when (and (numberp number)
3532                (> number 0))
3533       ;; We have to update the boundaries even if we can't fetch the
3534       ;; article if ID is a number -- so that the next `P' or `N'
3535       ;; command will fetch the previous (or next) article even
3536       ;; if the one we tried to fetch this time has been canceled.
3537       (and (> number gnus-newsgroup-end)
3538            (setq gnus-newsgroup-end number))
3539       (and (< number gnus-newsgroup-begin)
3540            (setq gnus-newsgroup-begin number))
3541       (setq gnus-newsgroup-unselected
3542             (delq number gnus-newsgroup-unselected)))
3543     ;; Report back a success?
3544     (and header (mail-header-number header))))
3545
3546 (defun gnus-summary-work-articles (n)
3547   "Return a list of articles to be worked upon.  The prefix argument,
3548 the list of process marked articles, and the current article will be
3549 taken into consideration."
3550   (cond
3551    (n
3552     ;; A numerical prefix has been given.
3553     (let ((backward (< n 0))
3554           (n (abs (prefix-numeric-value n)))
3555           articles article)
3556       (save-excursion
3557         (while
3558             (and (> n 0)
3559                  (push (setq article (gnus-summary-article-number))
3560                        articles)
3561                  (if backward
3562                      (gnus-summary-find-prev nil article)
3563                    (gnus-summary-find-next nil article)))
3564           (decf n)))
3565       (nreverse articles)))
3566    ((and (boundp 'transient-mark-mode)
3567          transient-mark-mode
3568          mark-active)
3569     ;; Work on the region between point and mark.
3570     (let ((max (max (point) (mark)))
3571           articles article)
3572       (save-excursion
3573         (goto-char (min (point) (mark)))
3574         (while
3575             (and
3576              (push (setq article (gnus-summary-article-number)) articles)
3577              (gnus-summary-find-next nil article)
3578              (< (point) max)))
3579         (nreverse articles))))
3580    (gnus-newsgroup-processable
3581     ;; There are process-marked articles present.
3582     (reverse gnus-newsgroup-processable))
3583    (t
3584     ;; Just return the current article.
3585     (list (gnus-summary-article-number)))))
3586
3587 (defun gnus-summary-search-group (&optional backward use-level)
3588   "Search for next unread newsgroup.
3589 If optional argument BACKWARD is non-nil, search backward instead."
3590   (save-excursion
3591     (set-buffer gnus-group-buffer)
3592     (if (gnus-group-search-forward
3593          backward nil (if use-level (gnus-group-group-level) nil))
3594         (gnus-group-group-name))))
3595
3596 (defun gnus-summary-best-group (&optional exclude-group)
3597   "Find the name of the best unread group.
3598 If EXCLUDE-GROUP, do not go to this group."
3599   (save-excursion
3600     (set-buffer gnus-group-buffer)
3601     (save-excursion
3602       (gnus-group-best-unread-group exclude-group))))
3603
3604 (defun gnus-summary-find-next (&optional unread article backward)
3605   (if backward (gnus-summary-find-prev)
3606     (let* ((dummy (gnus-summary-article-intangible-p))
3607            (article (or article (gnus-summary-article-number)))
3608            (arts (gnus-data-find-list article))
3609            result)
3610       (when (and (not dummy)
3611                  (or (not gnus-summary-check-current)
3612                      (not unread)
3613                      (not (gnus-data-unread-p (car arts)))))
3614         (setq arts (cdr arts)))
3615       (when (setq result
3616                   (if unread
3617                       (progn
3618                         (while arts
3619                           (when (gnus-data-unread-p (car arts))
3620                             (setq result (car arts)
3621                                   arts nil))
3622                           (setq arts (cdr arts)))
3623                         result)
3624                     (car arts)))
3625         (goto-char (gnus-data-pos result))
3626         (gnus-data-number result)))))
3627
3628 (defun gnus-summary-find-prev (&optional unread article)
3629   (let* ((eobp (eobp))
3630          (article (or article (gnus-summary-article-number)))
3631          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
3632          result)
3633     (when (and (not eobp)
3634                (or (not gnus-summary-check-current)
3635                    (not unread)
3636                    (not (gnus-data-unread-p (car arts)))))
3637       (setq arts (cdr arts)))
3638     (if (setq result
3639               (if unread
3640                   (progn
3641                     (while arts
3642                       (and (gnus-data-unread-p (car arts))
3643                            (setq result (car arts)
3644                                  arts nil))
3645                       (setq arts (cdr arts)))
3646                     result)
3647                 (car arts)))
3648         (progn
3649           (goto-char (gnus-data-pos result))
3650           (gnus-data-number result)))))
3651
3652 (defun gnus-summary-find-subject (subject &optional unread backward article)
3653   (let* ((simp-subject (gnus-simplify-subject-fully subject))
3654          (article (or article (gnus-summary-article-number)))
3655          (articles (gnus-data-list backward))
3656          (arts (gnus-data-find-list article articles))
3657          result)
3658     (when (or (not gnus-summary-check-current)
3659               (not unread)
3660               (not (gnus-data-unread-p (car arts))))
3661       (setq arts (cdr arts)))
3662     (while arts
3663       (and (or (not unread)
3664                (gnus-data-unread-p (car arts)))
3665            (vectorp (gnus-data-header (car arts)))
3666            (gnus-subject-equal
3667             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
3668            (setq result (car arts)
3669                  arts nil))
3670       (setq arts (cdr arts)))
3671     (and result
3672          (goto-char (gnus-data-pos result))
3673          (gnus-data-number result))))
3674
3675 (defun gnus-summary-search-forward (&optional unread subject backward)
3676   "Search forward for an article.
3677 If UNREAD, look for unread articles.  If SUBJECT, look for
3678 articles with that subject.  If BACKWARD, search backward instead."
3679   (cond (subject (gnus-summary-find-subject subject unread backward))
3680         (backward (gnus-summary-find-prev unread))
3681         (t (gnus-summary-find-next unread))))
3682
3683 (defun gnus-recenter (&optional n)
3684   "Center point in window and redisplay frame.
3685 Also do horizontal recentering."
3686   (interactive "P")
3687   (when (and gnus-auto-center-summary
3688              (not (eq gnus-auto-center-summary 'vertical)))
3689     (gnus-horizontal-recenter))
3690   (recenter n))
3691
3692 (defun gnus-summary-recenter ()
3693   "Center point in the summary window.
3694 If `gnus-auto-center-summary' is nil, or the article buffer isn't
3695 displayed, no centering will be performed."
3696   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
3697   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
3698   (let* ((top (cond ((< (window-height) 4) 0)
3699                     ((< (window-height) 7) 1)
3700                     (t 2)))
3701          (height (1- (window-height)))
3702          (bottom (save-excursion (goto-char (point-max))
3703                                  (forward-line (- height))
3704                                  (point)))
3705          (window (get-buffer-window (current-buffer))))
3706     ;; The user has to want it.
3707     (when gnus-auto-center-summary
3708       (when (get-buffer-window gnus-article-buffer)
3709        ;; Only do recentering when the article buffer is displayed,
3710        ;; Set the window start to either `bottom', which is the biggest
3711        ;; possible valid number, or the second line from the top,
3712        ;; whichever is the least.
3713        (set-window-start
3714         window (min bottom (save-excursion 
3715                              (forward-line (- top)) (point)))))
3716       ;; Do horizontal recentering while we're at it.
3717       (when (and (get-buffer-window (current-buffer) t)
3718                  (not (eq gnus-auto-center-summary 'vertical)))
3719         (let ((selected (selected-window)))
3720           (select-window (get-buffer-window (current-buffer) t))
3721           (gnus-summary-position-point)
3722           (gnus-horizontal-recenter)
3723           (select-window selected))))))
3724
3725 (defun gnus-summary-jump-to-group (newsgroup)
3726   "Move point to NEWSGROUP in group mode buffer."
3727   ;; Keep update point of group mode buffer if visible.
3728   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
3729       (save-window-excursion
3730         ;; Take care of tree window mode.
3731         (if (get-buffer-window gnus-group-buffer)
3732             (pop-to-buffer gnus-group-buffer))
3733         (gnus-group-jump-to-group newsgroup))
3734     (save-excursion
3735       ;; Take care of tree window mode.
3736       (if (get-buffer-window gnus-group-buffer)
3737           (pop-to-buffer gnus-group-buffer)
3738         (set-buffer gnus-group-buffer))
3739       (gnus-group-jump-to-group newsgroup))))
3740
3741 ;; This function returns a list of article numbers based on the
3742 ;; difference between the ranges of read articles in this group and
3743 ;; the range of active articles.
3744 (defun gnus-list-of-unread-articles (group)
3745   (let* ((read (gnus-info-read (gnus-get-info group)))
3746          (active (gnus-active group))
3747          (last (cdr active))
3748          first nlast unread)
3749     ;; If none are read, then all are unread.
3750     (if (not read)
3751         (setq first (car active))
3752       ;; If the range of read articles is a single range, then the
3753       ;; first unread article is the article after the last read
3754       ;; article.  Sounds logical, doesn't it?
3755       (if (not (listp (cdr read)))
3756           (setq first (1+ (cdr read)))
3757         ;; `read' is a list of ranges.
3758         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
3759                                 (caar read))) 1)
3760             (setq first 1))
3761         (while read
3762           (if first
3763               (while (< first nlast)
3764                 (setq unread (cons first unread))
3765                 (setq first (1+ first))))
3766           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
3767           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
3768           (setq read (cdr read)))))
3769     ;; And add the last unread articles.
3770     (while (<= first last)
3771       (setq unread (cons first unread))
3772       (setq first (1+ first)))
3773     ;; Return the list of unread articles.
3774     (nreverse unread)))
3775
3776 (defun gnus-list-of-read-articles (group)
3777   "Return a list of unread, unticked and non-dormant articles."
3778   (let* ((info (gnus-get-info group))
3779          (marked (gnus-info-marks info))
3780          (active (gnus-active group)))
3781     (and info active
3782          (gnus-set-difference
3783           (gnus-sorted-complement
3784            (gnus-uncompress-range active)
3785            (gnus-list-of-unread-articles group))
3786           (append
3787            (gnus-uncompress-range (cdr (assq 'dormant marked)))
3788            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
3789
3790 ;; Various summary commands
3791
3792 (defun gnus-summary-universal-argument (arg)
3793   "Perform any operation on all articles that are process/prefixed."
3794   (interactive "P")
3795   (gnus-set-global-variables)
3796   (let ((articles (gnus-summary-work-articles arg))
3797         func article)
3798     (if (eq
3799          (setq
3800           func
3801           (key-binding
3802            (read-key-sequence
3803             (substitute-command-keys
3804              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
3805              ))))
3806          'undefined)
3807         (gnus-error 1 "Undefined key")
3808       (save-excursion
3809         (while articles
3810           (gnus-summary-goto-subject (setq article (pop articles)))
3811           (command-execute func)
3812           (gnus-summary-remove-process-mark article)))))
3813   (gnus-summary-position-point))
3814
3815 (defun gnus-summary-toggle-truncation (&optional arg)
3816   "Toggle truncation of summary lines.
3817 With arg, turn line truncation on iff arg is positive."
3818   (interactive "P")
3819   (setq truncate-lines
3820         (if (null arg) (not truncate-lines)
3821           (> (prefix-numeric-value arg) 0)))
3822   (redraw-display))
3823
3824 (defun gnus-summary-reselect-current-group (&optional all rescan)
3825   "Exit and then reselect the current newsgroup.
3826 The prefix argument ALL means to select all articles."
3827   (interactive "P")
3828   (gnus-set-global-variables)
3829   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
3830     (error "Ephemeral groups can't be reselected"))
3831   (let ((current-subject (gnus-summary-article-number))
3832         (group gnus-newsgroup-name))
3833     (setq gnus-newsgroup-begin nil)
3834     (gnus-summary-exit)
3835     ;; We have to adjust the point of group mode buffer because the
3836     ;; current point was moved to the next unread newsgroup by
3837     ;; exiting.
3838     (gnus-summary-jump-to-group group)
3839     (when rescan
3840       (save-excursion
3841         (gnus-group-get-new-news-this-group 1)))
3842     (gnus-group-read-group all t)
3843     (gnus-summary-goto-subject current-subject nil t)))
3844
3845 (defun gnus-summary-rescan-group (&optional all)
3846   "Exit the newsgroup, ask for new articles, and select the newsgroup."
3847   (interactive "P")
3848   (gnus-summary-reselect-current-group all t))
3849
3850 (defun gnus-summary-update-info ()
3851   (let* ((group gnus-newsgroup-name))
3852     (when gnus-newsgroup-kill-headers
3853       (setq gnus-newsgroup-killed
3854             (gnus-compress-sequence
3855              (nconc
3856               (gnus-set-sorted-intersection
3857                (gnus-uncompress-range gnus-newsgroup-killed)
3858                (setq gnus-newsgroup-unselected
3859                      (sort gnus-newsgroup-unselected '<)))
3860               (setq gnus-newsgroup-unreads
3861                     (sort gnus-newsgroup-unreads '<))) t)))
3862     (unless (listp (cdr gnus-newsgroup-killed))
3863       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
3864     (let ((headers gnus-newsgroup-headers))
3865       (run-hooks 'gnus-exit-group-hook)
3866       (unless gnus-save-score
3867         (setq gnus-newsgroup-scored nil))
3868       ;; Set the new ranges of read articles.
3869       (gnus-update-read-articles
3870        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
3871       ;; Set the current article marks.
3872       (gnus-update-marks)
3873       ;; Do the cross-ref thing.
3874       (when gnus-use-cross-reference
3875         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
3876       ;; Do adaptive scoring, and possibly save score files.
3877       (when gnus-newsgroup-adaptive
3878         (gnus-score-adaptive))
3879       (when gnus-use-scoring
3880         (gnus-score-save))
3881       ;; Do not switch windows but change the buffer to work.
3882       (set-buffer gnus-group-buffer)
3883       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
3884           (gnus-group-update-group group)))))
3885
3886 (defun gnus-summary-exit (&optional temporary)
3887   "Exit reading current newsgroup, and then return to group selection mode.
3888 gnus-exit-group-hook is called with no arguments if that value is non-nil."
3889   (interactive)
3890   (gnus-set-global-variables)
3891   (gnus-kill-save-kill-buffer)
3892   (let* ((group gnus-newsgroup-name)
3893          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
3894          (mode major-mode)
3895          (buf (current-buffer)))
3896     (run-hooks 'gnus-summary-prepare-exit-hook)
3897     ;; If we have several article buffers, we kill them at exit.
3898     (unless gnus-single-article-buffer
3899       (gnus-kill-buffer gnus-original-article-buffer)
3900       (setq gnus-article-current nil))
3901     (when gnus-use-cache
3902       (gnus-cache-possibly-remove-articles)
3903       (gnus-cache-save-buffers))
3904     (gnus-async-prefetch-remove-group group)
3905     (when gnus-use-trees
3906       (gnus-tree-close group))
3907     ;; Make all changes in this group permanent.
3908     (unless quit-config
3909       (gnus-summary-update-info))
3910     (gnus-close-group group)
3911     ;; Make sure where I was, and go to next newsgroup.
3912     (set-buffer gnus-group-buffer)
3913     (unless quit-config
3914       (gnus-group-jump-to-group group))
3915     (run-hooks 'gnus-summary-exit-hook)
3916     (unless quit-config
3917       (gnus-group-next-unread-group 1))
3918     (if temporary
3919         nil                             ;Nothing to do.
3920       ;; If we have several article buffers, we kill them at exit.
3921       (unless gnus-single-article-buffer
3922         (gnus-kill-buffer gnus-article-buffer)
3923         (gnus-kill-buffer gnus-original-article-buffer)
3924         (setq gnus-article-current nil))
3925       (set-buffer buf)
3926       (if (not gnus-kill-summary-on-exit)
3927           (gnus-deaden-summary)
3928         ;; We set all buffer-local variables to nil.  It is unclear why
3929         ;; this is needed, but if we don't, buffer-local variables are
3930         ;; not garbage-collected, it seems.  This would the lead to en
3931         ;; ever-growing Emacs.
3932         (gnus-summary-clear-local-variables)
3933         (when (get-buffer gnus-article-buffer)
3934           (bury-buffer gnus-article-buffer))
3935         ;; We clear the global counterparts of the buffer-local
3936         ;; variables as well, just to be on the safe side.
3937         (gnus-configure-windows 'group 'force)
3938         (gnus-summary-clear-local-variables)
3939         ;; Return to group mode buffer.
3940         (if (eq mode 'gnus-summary-mode)
3941             (gnus-kill-buffer buf)))
3942       (setq gnus-current-select-method gnus-select-method)
3943       (pop-to-buffer gnus-group-buffer)
3944       ;; Clear the current group name.
3945       (if (not quit-config)
3946           (progn
3947             (gnus-group-jump-to-group group)
3948             (gnus-group-next-unread-group 1)
3949             (gnus-configure-windows 'group 'force))
3950         (if (not (buffer-name (car quit-config)))
3951             (gnus-configure-windows 'group 'force)
3952           (set-buffer (car quit-config))
3953           (and (eq major-mode 'gnus-summary-mode)
3954                (gnus-set-global-variables))
3955           (gnus-configure-windows (cdr quit-config))))
3956       (unless quit-config
3957         (setq gnus-newsgroup-name nil)))))
3958
3959 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
3960 (defun gnus-summary-exit-no-update (&optional no-questions)
3961   "Quit reading current newsgroup without updating read article info."
3962   (interactive)
3963   (gnus-set-global-variables)
3964   (let* ((group gnus-newsgroup-name)
3965          (quit-config (gnus-group-quit-config group)))
3966     (when (or no-questions
3967               gnus-expert-user
3968               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
3969       ;; If we have several article buffers, we kill them at exit.
3970       (unless gnus-single-article-buffer
3971         (gnus-kill-buffer gnus-article-buffer)
3972         (gnus-kill-buffer gnus-original-article-buffer)
3973         (setq gnus-article-current nil))
3974       (if (not gnus-kill-summary-on-exit)
3975           (gnus-deaden-summary)
3976         (gnus-close-group group)
3977         (gnus-summary-clear-local-variables)
3978         (set-buffer gnus-group-buffer)
3979         (gnus-summary-clear-local-variables)
3980         (when (get-buffer gnus-summary-buffer)
3981           (kill-buffer gnus-summary-buffer)))
3982       (unless gnus-single-article-buffer
3983         (setq gnus-article-current nil))
3984       (when gnus-use-trees
3985         (gnus-tree-close group))
3986       (gnus-async-prefetch-remove-group group)
3987       (when (get-buffer gnus-article-buffer)
3988         (bury-buffer gnus-article-buffer))
3989       ;; Return to the group buffer.
3990       (gnus-configure-windows 'group 'force)
3991       ;; Clear the current group name.
3992       (setq gnus-newsgroup-name nil)
3993       (when (equal (gnus-group-group-name) group)
3994         (gnus-group-next-unread-group 1))
3995       (when quit-config
3996         (if (not (buffer-name (car quit-config)))
3997             (gnus-configure-windows 'group 'force)
3998           (set-buffer (car quit-config))
3999           (when (eq major-mode 'gnus-summary-mode)
4000             (gnus-set-global-variables))
4001           (gnus-configure-windows (cdr quit-config)))))))
4002
4003 ;;; Dead summaries.
4004
4005 (defvar gnus-dead-summary-mode-map nil)
4006
4007 (if gnus-dead-summary-mode-map
4008     nil
4009   (setq gnus-dead-summary-mode-map (make-keymap))
4010   (suppress-keymap gnus-dead-summary-mode-map)
4011   (substitute-key-definition
4012    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
4013   (let ((keys '("\C-d" "\r" "\177")))
4014     (while keys
4015       (define-key gnus-dead-summary-mode-map
4016         (pop keys) 'gnus-summary-wake-up-the-dead))))
4017
4018 (defvar gnus-dead-summary-mode nil
4019   "Minor mode for Gnus summary buffers.")
4020
4021 (defun gnus-dead-summary-mode (&optional arg)
4022   "Minor mode for Gnus summary buffers."
4023   (interactive "P")
4024   (when (eq major-mode 'gnus-summary-mode)
4025     (make-local-variable 'gnus-dead-summary-mode)
4026     (setq gnus-dead-summary-mode
4027           (if (null arg) (not gnus-dead-summary-mode)
4028             (> (prefix-numeric-value arg) 0)))
4029     (when gnus-dead-summary-mode
4030       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
4031         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
4032       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
4033         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
4034               minor-mode-map-alist)))))
4035
4036 (defun gnus-deaden-summary ()
4037   "Make the current summary buffer into a dead summary buffer."
4038   ;; Kill any previous dead summary buffer.
4039   (when (and gnus-dead-summary
4040              (buffer-name gnus-dead-summary))
4041     (save-excursion
4042       (set-buffer gnus-dead-summary)
4043       (when gnus-dead-summary-mode
4044         (kill-buffer (current-buffer)))))
4045   ;; Make this the current dead summary.
4046   (setq gnus-dead-summary (current-buffer))
4047   (gnus-dead-summary-mode 1)
4048   (let ((name (buffer-name)))
4049     (when (string-match "Summary" name)
4050       (rename-buffer
4051        (concat (substring name 0 (match-beginning 0)) "Dead "
4052                (substring name (match-beginning 0))) t))))
4053
4054 (defun gnus-kill-or-deaden-summary (buffer)
4055   "Kill or deaden the summary BUFFER."
4056   (when (and (buffer-name buffer)
4057              (not gnus-single-article-buffer))
4058     (save-excursion
4059       (set-buffer buffer)
4060       (gnus-kill-buffer gnus-article-buffer)
4061       (gnus-kill-buffer gnus-original-article-buffer)))
4062   (cond (gnus-kill-summary-on-exit
4063          (when (and gnus-use-trees
4064                     (and (get-buffer buffer)
4065                          (buffer-name (get-buffer buffer))))
4066            (save-excursion
4067              (set-buffer (get-buffer buffer))
4068              (gnus-tree-close gnus-newsgroup-name)))
4069          (gnus-kill-buffer buffer))
4070         ((and (get-buffer buffer)
4071               (buffer-name (get-buffer buffer)))
4072          (save-excursion
4073            (set-buffer buffer)
4074            (gnus-deaden-summary)))))
4075
4076 (defun gnus-summary-wake-up-the-dead (&rest args)
4077   "Wake up the dead summary buffer."
4078   (interactive)
4079   (gnus-dead-summary-mode -1)
4080   (let ((name (buffer-name)))
4081     (when (string-match "Dead " name)
4082       (rename-buffer
4083        (concat (substring name 0 (match-beginning 0))
4084                (substring name (match-end 0))) t)))
4085   (gnus-message 3 "This dead summary is now alive again"))
4086
4087 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
4088 (defun gnus-summary-fetch-faq (&optional faq-dir)
4089   "Fetch the FAQ for the current group.
4090 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
4091 in."
4092   (interactive
4093    (list
4094     (if current-prefix-arg
4095         (completing-read
4096          "Faq dir: " (and (listp gnus-group-faq-directory)
4097                           gnus-group-faq-directory)))))
4098   (let (gnus-faq-buffer)
4099     (and (setq gnus-faq-buffer
4100                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
4101          (gnus-configure-windows 'summary-faq))))
4102
4103 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4104 (defun gnus-summary-describe-group (&optional force)
4105   "Describe the current newsgroup."
4106   (interactive "P")
4107   (gnus-group-describe-group force gnus-newsgroup-name))
4108
4109 (defun gnus-summary-describe-briefly ()
4110   "Describe summary mode commands briefly."
4111   (interactive)
4112   (gnus-message 6
4113                 (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")))
4114
4115 ;; Walking around group mode buffer from summary mode.
4116
4117 (defun gnus-summary-next-group (&optional no-article target-group backward)
4118   "Exit current newsgroup and then select next unread newsgroup.
4119 If prefix argument NO-ARTICLE is non-nil, no article is selected
4120 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
4121 previous group instead."
4122   (interactive "P")
4123   (gnus-set-global-variables)
4124   (let ((current-group gnus-newsgroup-name)
4125         (current-buffer (current-buffer))
4126         entered)
4127     ;; First we semi-exit this group to update Xrefs and all variables.
4128     ;; We can't do a real exit, because the window conf must remain
4129     ;; the same in case the user is prompted for info, and we don't
4130     ;; want the window conf to change before that...
4131     (gnus-summary-exit t)
4132     (while (not entered)
4133       ;; Then we find what group we are supposed to enter.
4134       (set-buffer gnus-group-buffer)
4135       (gnus-group-jump-to-group current-group)
4136       (setq target-group
4137             (or target-group
4138                 (if (eq gnus-keep-same-level 'best)
4139                     (gnus-summary-best-group gnus-newsgroup-name)
4140                   (gnus-summary-search-group backward gnus-keep-same-level))))
4141       (if (not target-group)
4142           ;; There are no further groups, so we return to the group
4143           ;; buffer.
4144           (progn
4145             (gnus-message 5 "Returning to the group buffer")
4146             (setq entered t)
4147             (set-buffer current-buffer)
4148             (gnus-summary-exit))
4149         ;; We try to enter the target group.
4150         (gnus-group-jump-to-group target-group)
4151         (let ((unreads (gnus-group-group-unread)))
4152           (if (and (or (eq t unreads)
4153                        (and unreads (not (zerop unreads))))
4154                    (gnus-summary-read-group
4155                     target-group nil no-article current-buffer))
4156               (setq entered t)
4157             (setq current-group target-group
4158                   target-group nil)))))))
4159
4160 (defun gnus-summary-prev-group (&optional no-article)
4161   "Exit current newsgroup and then select previous unread newsgroup.
4162 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
4163   (interactive "P")
4164   (gnus-summary-next-group no-article nil t))
4165
4166 ;; Walking around summary lines.
4167
4168 (defun gnus-summary-first-subject (&optional unread)
4169   "Go to the first unread subject.
4170 If UNREAD is non-nil, go to the first unread article.
4171 Returns the article selected or nil if there are no unread articles."
4172   (interactive "P")
4173   (prog1
4174       (cond
4175        ;; Empty summary.
4176        ((null gnus-newsgroup-data)
4177         (gnus-message 3 "No articles in the group")
4178         nil)
4179        ;; Pick the first article.
4180        ((not unread)
4181         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
4182         (gnus-data-number (car gnus-newsgroup-data)))
4183        ;; No unread articles.
4184        ((null gnus-newsgroup-unreads)
4185         (gnus-message 3 "No more unread articles")
4186         nil)
4187        ;; Find the first unread article.
4188        (t
4189         (let ((data gnus-newsgroup-data))
4190           (while (and data
4191                       (not (gnus-data-unread-p (car data))))
4192             (setq data (cdr data)))
4193           (if data
4194               (progn
4195                 (goto-char (gnus-data-pos (car data)))
4196                 (gnus-data-number (car data)))))))
4197     (gnus-summary-position-point)))
4198
4199 (defun gnus-summary-next-subject (n &optional unread dont-display)
4200   "Go to next N'th summary line.
4201 If N is negative, go to the previous N'th subject line.
4202 If UNREAD is non-nil, only unread articles are selected.
4203 The difference between N and the actual number of steps taken is
4204 returned."
4205   (interactive "p")
4206   (let ((backward (< n 0))
4207         (n (abs n)))
4208     (while (and (> n 0)
4209                 (if backward
4210                     (gnus-summary-find-prev unread)
4211                   (gnus-summary-find-next unread)))
4212       (setq n (1- n)))
4213     (if (/= 0 n) (gnus-message 7 "No more%s articles"
4214                                (if unread " unread" "")))
4215     (unless dont-display
4216       (gnus-summary-recenter)
4217       (gnus-summary-position-point))
4218     n))
4219
4220 (defun gnus-summary-next-unread-subject (n)
4221   "Go to next N'th unread summary line."
4222   (interactive "p")
4223   (gnus-summary-next-subject n t))
4224
4225 (defun gnus-summary-prev-subject (n &optional unread)
4226   "Go to previous N'th summary line.
4227 If optional argument UNREAD is non-nil, only unread article is selected."
4228   (interactive "p")
4229   (gnus-summary-next-subject (- n) unread))
4230
4231 (defun gnus-summary-prev-unread-subject (n)
4232   "Go to previous N'th unread summary line."
4233   (interactive "p")
4234   (gnus-summary-next-subject (- n) t))
4235
4236 (defun gnus-summary-goto-subject (article &optional force silent)
4237   "Go the subject line of ARTICLE.
4238 If FORCE, also allow jumping to articles not currently shown."
4239   (let ((b (point))
4240         (data (gnus-data-find article)))
4241     ;; We read in the article if we have to.
4242     (and (not data)
4243          force
4244          (gnus-summary-insert-subject article (and (vectorp force) force) t)
4245          (setq data (gnus-data-find article)))
4246     (goto-char b)
4247     (if (not data)
4248         (progn
4249           (unless silent
4250             (gnus-message 3 "Can't find article %d" article))
4251           nil)
4252       (goto-char (gnus-data-pos data))
4253       article)))
4254
4255 ;; Walking around summary lines with displaying articles.
4256
4257 (defun gnus-summary-expand-window (&optional arg)
4258   "Make the summary buffer take up the entire Emacs frame.
4259 Given a prefix, will force an `article' buffer configuration."
4260   (interactive "P")
4261   (gnus-set-global-variables)
4262   (if arg
4263       (gnus-configure-windows 'article 'force)
4264     (gnus-configure-windows 'summary 'force)))
4265
4266 (defun gnus-summary-display-article (article &optional all-header)
4267   "Display ARTICLE in article buffer."
4268   (gnus-set-global-variables)
4269   (if (null article)
4270       nil
4271     (prog1
4272         (if gnus-summary-display-article-function
4273             (funcall gnus-summary-display-article-function article all-header)
4274           (gnus-article-prepare article all-header))
4275       (run-hooks 'gnus-select-article-hook)
4276       (unless (zerop gnus-current-article)
4277         (gnus-summary-goto-subject gnus-current-article))
4278       (gnus-summary-recenter)
4279       (when gnus-use-trees
4280         (gnus-possibly-generate-tree article)
4281         (gnus-highlight-selected-tree article))
4282       ;; Successfully display article.
4283       (gnus-article-set-window-start
4284        (cdr (assq article gnus-newsgroup-bookmarks))))))
4285
4286 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
4287   "Select the current article.
4288 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
4289 non-nil, the article will be re-fetched even if it already present in
4290 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
4291 be displayed."
4292   ;; Make sure we are in the summary buffer to work around bbdb bug.
4293   (unless (eq major-mode 'gnus-summary-mode)
4294     (set-buffer gnus-summary-buffer))
4295   (let ((article (or article (gnus-summary-article-number)))
4296         (all-headers (not (not all-headers))) ;Must be T or NIL.
4297         gnus-summary-display-article-function
4298         did)
4299     (and (not pseudo)
4300          (gnus-summary-article-pseudo-p article)
4301          (error "This is a pseudo-article."))
4302     (prog1
4303         (save-excursion
4304           (set-buffer gnus-summary-buffer)
4305           (if (or (and gnus-single-article-buffer
4306                        (or (null gnus-current-article)
4307                            (null gnus-article-current)
4308                            (null (get-buffer gnus-article-buffer))
4309                            (not (eq article (cdr gnus-article-current)))
4310                            (not (equal (car gnus-article-current)
4311                                        gnus-newsgroup-name))))
4312                   (and (not gnus-single-article-buffer)
4313                        (or (null gnus-current-article)
4314                            (not (eq gnus-current-article article))))
4315                   force)
4316               ;; The requested article is different from the current article.
4317               (prog1
4318                   (gnus-summary-display-article article all-headers)
4319                 (setq did article))
4320             (if (or all-headers gnus-show-all-headers)
4321                 (gnus-article-show-all-headers))
4322             'old))
4323       (if did
4324           (gnus-article-set-window-start
4325            (cdr (assq article gnus-newsgroup-bookmarks)))))))
4326
4327 (defun gnus-summary-set-current-mark (&optional current-mark)
4328   "Obsolete function."
4329   nil)
4330
4331 (defun gnus-summary-next-article (&optional unread subject backward push)
4332   "Select the next article.
4333 If UNREAD, only unread articles are selected.
4334 If SUBJECT, only articles with SUBJECT are selected.
4335 If BACKWARD, the previous article is selected instead of the next."
4336   (interactive "P")
4337   (gnus-set-global-variables)
4338   (cond
4339    ;; Is there such an article?
4340    ((and (gnus-summary-search-forward unread subject backward)
4341          (or (gnus-summary-display-article (gnus-summary-article-number))
4342              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
4343     (gnus-summary-position-point))
4344    ;; If not, we try the first unread, if that is wanted.
4345    ((and subject
4346          gnus-auto-select-same
4347          (gnus-summary-first-unread-article))
4348     (gnus-summary-position-point)
4349     (gnus-message 6 "Wrapped"))
4350    ;; Try to get next/previous article not displayed in this group.
4351    ((and gnus-auto-extend-newsgroup
4352          (not unread) (not subject))
4353     (gnus-summary-goto-article
4354      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
4355      nil t))
4356    ;; Go to next/previous group.
4357    (t
4358     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
4359         (gnus-summary-jump-to-group gnus-newsgroup-name))
4360     (let ((cmd last-command-char)
4361           (group
4362            (if (eq gnus-keep-same-level 'best)
4363                (gnus-summary-best-group gnus-newsgroup-name)
4364              (gnus-summary-search-group backward gnus-keep-same-level))))
4365       ;; For some reason, the group window gets selected.  We change
4366       ;; it back.
4367       (select-window (get-buffer-window (current-buffer)))
4368       ;; Select next unread newsgroup automagically.
4369       (cond
4370        ((or (not gnus-auto-select-next)
4371             (not cmd))
4372         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
4373        ((or (eq gnus-auto-select-next 'quietly)
4374             (and (eq gnus-auto-select-next 'slightly-quietly)
4375                  push)
4376             (and (eq gnus-auto-select-next 'almost-quietly)
4377                  (gnus-summary-last-article-p)))
4378         ;; Select quietly.
4379         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
4380             (gnus-summary-exit)
4381           (gnus-message 7 "No more%s articles (%s)..."
4382                         (if unread " unread" "")
4383                         (if group (concat "selecting " group)
4384                           "exiting"))
4385           (gnus-summary-next-group nil group backward)))
4386        (t
4387         (gnus-summary-walk-group-buffer
4388          gnus-newsgroup-name cmd unread backward)))))))
4389
4390 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
4391   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
4392                       (?\C-p (gnus-group-prev-unread-group 1))))
4393         keve key group ended)
4394     (save-excursion
4395       (set-buffer gnus-group-buffer)
4396       (gnus-summary-jump-to-group from-group)
4397       (setq group
4398             (if (eq gnus-keep-same-level 'best)
4399                 (gnus-summary-best-group gnus-newsgroup-name)
4400               (gnus-summary-search-group backward gnus-keep-same-level))))
4401     (while (not ended)
4402       (gnus-message
4403        5 "No more%s articles%s" (if unread " unread" "")
4404        (if (and group
4405                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
4406            (format " (Type %s for %s [%s])"
4407                    (single-key-description cmd) group
4408                    (car (gnus-gethash group gnus-newsrc-hashtb)))
4409          (format " (Type %s to exit %s)"
4410                  (single-key-description cmd)
4411                  gnus-newsgroup-name)))
4412       ;; Confirm auto selection.
4413       (setq key (car (setq keve (gnus-read-event-char))))
4414       (setq ended t)
4415       (cond
4416        ((assq key keystrokes)
4417         (let ((obuf (current-buffer)))
4418           (switch-to-buffer gnus-group-buffer)
4419           (and group
4420                (gnus-group-jump-to-group group))
4421           (eval (cadr (assq key keystrokes)))
4422           (setq group (gnus-group-group-name))
4423           (switch-to-buffer obuf))
4424         (setq ended nil))
4425        ((equal key cmd)
4426         (if (or (not group)
4427                 (gnus-ephemeral-group-p gnus-newsgroup-name))
4428             (gnus-summary-exit)
4429           (gnus-summary-next-group nil group backward)))
4430        (t
4431         (push (cdr keve) unread-command-events))))))
4432
4433 (defun gnus-summary-next-unread-article ()
4434   "Select unread article after current one."
4435   (interactive)
4436   (gnus-summary-next-article t (and gnus-auto-select-same
4437                                     (gnus-summary-article-subject))))
4438
4439 (defun gnus-summary-prev-article (&optional unread subject)
4440   "Select the article after the current one.
4441 If UNREAD is non-nil, only unread articles are selected."
4442   (interactive "P")
4443   (gnus-summary-next-article unread subject t))
4444
4445 (defun gnus-summary-prev-unread-article ()
4446   "Select unred article before current one."
4447   (interactive)
4448   (gnus-summary-prev-article t (and gnus-auto-select-same
4449                                     (gnus-summary-article-subject))))
4450
4451 (defun gnus-summary-next-page (&optional lines circular)
4452   "Show next page of the selected article.
4453 If at the end of the current article, select the next article.
4454 LINES says how many lines should be scrolled up.
4455
4456 If CIRCULAR is non-nil, go to the start of the article instead of
4457 selecting the next article when reaching the end of the current
4458 article."
4459   (interactive "P")
4460   (setq gnus-summary-buffer (current-buffer))
4461   (gnus-set-global-variables)
4462   (let ((article (gnus-summary-article-number))
4463         (endp nil))
4464     (gnus-configure-windows 'article)
4465     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
4466         (if (and (eq gnus-summary-goto-unread 'never)
4467                  (not (gnus-summary-last-article-p article)))
4468             (gnus-summary-next-article)
4469           (gnus-summary-next-unread-article))
4470       (if (or (null gnus-current-article)
4471               (null gnus-article-current)
4472               (/= article (cdr gnus-article-current))
4473               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
4474           ;; Selected subject is different from current article's.
4475           (gnus-summary-display-article article)
4476         (gnus-eval-in-buffer-window gnus-article-buffer
4477           (setq endp (gnus-article-next-page lines)))
4478         (if endp
4479             (cond (circular
4480                    (gnus-summary-beginning-of-article))
4481                   (lines
4482                    (gnus-message 3 "End of message"))
4483                   ((null lines)
4484                    (if (and (eq gnus-summary-goto-unread 'never)
4485                             (not (gnus-summary-last-article-p article)))
4486                        (gnus-summary-next-article)
4487                      (gnus-summary-next-unread-article)))))))
4488     (gnus-summary-recenter)
4489     (gnus-summary-position-point)))
4490
4491 (defun gnus-summary-prev-page (&optional lines)
4492   "Show previous page of selected article.
4493 Argument LINES specifies lines to be scrolled down."
4494   (interactive "P")
4495   (gnus-set-global-variables)
4496   (let ((article (gnus-summary-article-number)))
4497     (gnus-configure-windows 'article)
4498     (if (or (null gnus-current-article)
4499             (null gnus-article-current)
4500             (/= article (cdr gnus-article-current))
4501             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
4502         ;; Selected subject is different from current article's.
4503         (gnus-summary-display-article article)
4504       (gnus-summary-recenter)
4505       (gnus-eval-in-buffer-window gnus-article-buffer
4506         (gnus-article-prev-page lines))))
4507   (gnus-summary-position-point))
4508
4509 (defun gnus-summary-scroll-up (lines)
4510   "Scroll up (or down) one line current article.
4511 Argument LINES specifies lines to be scrolled up (or down if negative)."
4512   (interactive "p")
4513   (gnus-set-global-variables)
4514   (gnus-configure-windows 'article)
4515   (gnus-summary-show-thread)
4516   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
4517     (gnus-eval-in-buffer-window gnus-article-buffer
4518       (cond ((> lines 0)
4519              (if (gnus-article-next-page lines)
4520                  (gnus-message 3 "End of message")))
4521             ((< lines 0)
4522              (gnus-article-prev-page (- lines))))))
4523   (gnus-summary-recenter)
4524   (gnus-summary-position-point))
4525
4526 (defun gnus-summary-next-same-subject ()
4527   "Select next article which has the same subject as current one."
4528   (interactive)
4529   (gnus-set-global-variables)
4530   (gnus-summary-next-article nil (gnus-summary-article-subject)))
4531
4532 (defun gnus-summary-prev-same-subject ()
4533   "Select previous article which has the same subject as current one."
4534   (interactive)
4535   (gnus-set-global-variables)
4536   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
4537
4538 (defun gnus-summary-next-unread-same-subject ()
4539   "Select next unread article which has the same subject as current one."
4540   (interactive)
4541   (gnus-set-global-variables)
4542   (gnus-summary-next-article t (gnus-summary-article-subject)))
4543
4544 (defun gnus-summary-prev-unread-same-subject ()
4545   "Select previous unread article which has the same subject as current one."
4546   (interactive)
4547   (gnus-set-global-variables)
4548   (gnus-summary-prev-article t (gnus-summary-article-subject)))
4549
4550 (defun gnus-summary-first-unread-article ()
4551   "Select the first unread article.
4552 Return nil if there are no unread articles."
4553   (interactive)
4554   (gnus-set-global-variables)
4555   (prog1
4556       (if (gnus-summary-first-subject t)
4557           (progn
4558             (gnus-summary-show-thread)
4559             (gnus-summary-first-subject t)
4560             (gnus-summary-display-article (gnus-summary-article-number))))
4561     (gnus-summary-position-point)))
4562
4563 (defun gnus-summary-best-unread-article ()
4564   "Select the unread article with the highest score."
4565   (interactive)
4566   (gnus-set-global-variables)
4567   (let ((best -1000000)
4568         (data gnus-newsgroup-data)
4569         article score)
4570     (while data
4571       (and (gnus-data-unread-p (car data))
4572            (> (setq score
4573                     (gnus-summary-article-score (gnus-data-number (car data))))
4574               best)
4575            (setq best score
4576                  article (gnus-data-number (car data))))
4577       (setq data (cdr data)))
4578     (prog1
4579         (if article
4580             (gnus-summary-goto-article article)
4581           (error "No unread articles"))
4582       (gnus-summary-position-point))))
4583
4584 (defun gnus-summary-last-subject ()
4585   "Go to the last displayed subject line in the group."
4586   (let ((article (gnus-data-number (car (gnus-data-list t)))))
4587     (when article
4588       (gnus-summary-goto-subject article))))
4589
4590 (defun gnus-summary-goto-article (article &optional all-headers force)
4591   "Fetch ARTICLE and display it if it exists.
4592 If ALL-HEADERS is non-nil, no header lines are hidden."
4593   (interactive
4594    (list
4595     (string-to-int
4596      (completing-read
4597       "Article number: "
4598       (mapcar (lambda (number) (list (int-to-string number)))
4599               gnus-newsgroup-limit)))
4600     current-prefix-arg
4601     t))
4602   (prog1
4603       (if (gnus-summary-goto-subject article force)
4604           (gnus-summary-display-article article all-headers)
4605         (gnus-message 4 "Couldn't go to article %s" article) nil)
4606     (gnus-summary-position-point)))
4607
4608 (defun gnus-summary-goto-last-article ()
4609   "Go to the previously read article."
4610   (interactive)
4611   (prog1
4612       (and gnus-last-article
4613            (gnus-summary-goto-article gnus-last-article))
4614     (gnus-summary-position-point)))
4615
4616 (defun gnus-summary-pop-article (number)
4617   "Pop one article off the history and go to the previous.
4618 NUMBER articles will be popped off."
4619   (interactive "p")
4620   (let (to)
4621     (setq gnus-newsgroup-history
4622           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
4623     (if to
4624         (gnus-summary-goto-article (car to))
4625       (error "Article history empty")))
4626   (gnus-summary-position-point))
4627
4628 ;; Summary commands and functions for limiting the summary buffer.
4629
4630 (defun gnus-summary-limit-to-articles (n)
4631   "Limit the summary buffer to the next N articles.
4632 If not given a prefix, use the process marked articles instead."
4633   (interactive "P")
4634   (gnus-set-global-variables)
4635   (prog1
4636       (let ((articles (gnus-summary-work-articles n)))
4637         (setq gnus-newsgroup-processable nil)
4638         (gnus-summary-limit articles))
4639     (gnus-summary-position-point)))
4640
4641 (defun gnus-summary-pop-limit (&optional total)
4642   "Restore the previous limit.
4643 If given a prefix, remove all limits."
4644   (interactive "P")
4645   (gnus-set-global-variables)
4646   (when total 
4647     (setq gnus-newsgroup-limits
4648           (list (mapcar (lambda (h) (mail-header-number h))
4649                         gnus-newsgroup-headers))))
4650   (unless gnus-newsgroup-limits
4651     (error "No limit to pop"))
4652   (prog1
4653       (gnus-summary-limit nil 'pop)
4654     (gnus-summary-position-point)))
4655
4656 (defun gnus-summary-limit-to-subject (subject &optional header)
4657   "Limit the summary buffer to articles that have subjects that match a regexp."
4658   (interactive "sRegexp: ")
4659   (unless header
4660     (setq header "subject"))
4661   (when (not (equal "" subject))
4662     (prog1
4663         (let ((articles (gnus-summary-find-matching
4664                          (or header "subject") subject 'all)))
4665           (or articles (error "Found no matches for \"%s\"" subject))
4666           (gnus-summary-limit articles))
4667       (gnus-summary-position-point))))
4668
4669 (defun gnus-summary-limit-to-author (from)
4670   "Limit the summary buffer to articles that have authors that match a regexp."
4671   (interactive "sRegexp: ")
4672   (gnus-summary-limit-to-subject from "from"))
4673
4674 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
4675 (make-obsolete
4676  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
4677
4678 (defun gnus-summary-limit-to-unread (&optional all)
4679   "Limit the summary buffer to articles that are not marked as read.
4680 If ALL is non-nil, limit strictly to unread articles."
4681   (interactive "P")
4682   (if all
4683       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
4684     (gnus-summary-limit-to-marks
4685      ;; Concat all the marks that say that an article is read and have
4686      ;; those removed.
4687      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
4688            gnus-killed-mark gnus-kill-file-mark
4689            gnus-low-score-mark gnus-expirable-mark
4690            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
4691      'reverse)))
4692
4693 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
4694 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
4695
4696 (defun gnus-summary-limit-to-marks (marks &optional reverse)
4697   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
4698 If REVERSE, limit the summary buffer to articles that are not marked
4699 with MARKS.  MARKS can either be a string of marks or a list of marks.
4700 Returns how many articles were removed."
4701   (interactive "sMarks: ")
4702   (gnus-set-global-variables)
4703   (prog1
4704       (let ((data gnus-newsgroup-data)
4705             (marks (if (listp marks) marks
4706                      (append marks nil))) ; Transform to list.
4707             articles)
4708         (while data
4709           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
4710                  (memq (gnus-data-mark (car data)) marks))
4711                (setq articles (cons (gnus-data-number (car data)) articles)))
4712           (setq data (cdr data)))
4713         (gnus-summary-limit articles))
4714     (gnus-summary-position-point)))
4715
4716 (defun gnus-summary-limit-to-score (&optional score)
4717   "Limit to articles with score at or above SCORE."
4718   (interactive "P")
4719   (gnus-set-global-variables)
4720   (setq score (if score
4721                   (prefix-numeric-value score)
4722                 (or gnus-summary-default-score 0)))
4723   (let ((data gnus-newsgroup-data)
4724         articles)
4725     (while data
4726       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
4727                 score)
4728         (push (gnus-data-number (car data)) articles))
4729       (setq data (cdr data)))
4730     (prog1
4731         (gnus-summary-limit articles)
4732       (gnus-summary-position-point))))
4733
4734 (defun gnus-summary-limit-include-dormant ()
4735   "Display all the hidden articles that are marked as dormant."
4736   (interactive)
4737   (gnus-set-global-variables)
4738   (or gnus-newsgroup-dormant
4739       (error "There are no dormant articles in this group"))
4740   (prog1
4741       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
4742     (gnus-summary-position-point)))
4743
4744 (defun gnus-summary-limit-exclude-dormant ()
4745   "Hide all dormant articles."
4746   (interactive)
4747   (gnus-set-global-variables)
4748   (prog1
4749       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
4750     (gnus-summary-position-point)))
4751
4752 (defun gnus-summary-limit-exclude-childless-dormant ()
4753   "Hide all dormant articles that have no children."
4754   (interactive)
4755   (gnus-set-global-variables)
4756   (let ((data (gnus-data-list t))
4757         articles d children)
4758     ;; Find all articles that are either not dormant or have
4759     ;; children.
4760     (while (setq d (pop data))
4761       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
4762                 (and (setq children 
4763                            (gnus-article-children (gnus-data-number d)))
4764                      (let (found)
4765                        (while children
4766                          (when (memq (car children) articles)
4767                            (setq children nil
4768                                  found t))
4769                          (pop children))
4770                        found)))
4771         (push (gnus-data-number d) articles)))
4772     ;; Do the limiting.
4773     (prog1
4774         (gnus-summary-limit articles)
4775       (gnus-summary-position-point))))
4776
4777 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
4778   "Mark all unread excluded articles as read.
4779 If ALL, mark even excluded ticked and dormants as read."
4780   (interactive "P")
4781   (let ((articles (gnus-sorted-complement
4782                    (sort
4783                     (mapcar (lambda (h) (mail-header-number h))
4784                             gnus-newsgroup-headers)
4785                     '<)
4786                    (sort gnus-newsgroup-limit '<)))
4787         article)
4788     (setq gnus-newsgroup-unreads nil)
4789     (if all
4790         (setq gnus-newsgroup-dormant nil
4791               gnus-newsgroup-marked nil
4792               gnus-newsgroup-reads
4793               (nconc
4794                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
4795                gnus-newsgroup-reads))
4796       (while (setq article (pop articles))
4797         (unless (or (memq article gnus-newsgroup-dormant)
4798                     (memq article gnus-newsgroup-marked))
4799           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
4800
4801 (defun gnus-summary-limit (articles &optional pop)
4802   (if pop
4803       ;; We pop the previous limit off the stack and use that.
4804       (setq articles (car gnus-newsgroup-limits)
4805             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
4806     ;; We use the new limit, so we push the old limit on the stack.
4807     (setq gnus-newsgroup-limits
4808           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
4809   ;; Set the limit.
4810   (setq gnus-newsgroup-limit articles)
4811   (let ((total (length gnus-newsgroup-data))
4812         (data (gnus-data-find-list (gnus-summary-article-number)))
4813         (gnus-summary-mark-below nil)   ; Inhibit this.
4814         found)
4815     ;; This will do all the work of generating the new summary buffer
4816     ;; according to the new limit.
4817     (gnus-summary-prepare)
4818     ;; Hide any threads, possibly.
4819     (and gnus-show-threads
4820          gnus-thread-hide-subtree
4821          (gnus-summary-hide-all-threads))
4822     ;; Try to return to the article you were at, or one in the
4823     ;; neighborhood.
4824     (if data
4825         ;; We try to find some article after the current one.
4826         (while data
4827           (and (gnus-summary-goto-subject
4828                 (gnus-data-number (car data)) nil t)
4829                (setq data nil
4830                      found t))
4831           (setq data (cdr data))))
4832     (or found
4833         ;; If there is no data, that means that we were after the last
4834         ;; article.  The same goes when we can't find any articles
4835         ;; after the current one.
4836         (progn
4837           (goto-char (point-max))
4838           (gnus-summary-find-prev)))
4839     ;; We return how many articles were removed from the summary
4840     ;; buffer as a result of the new limit.
4841     (- total (length gnus-newsgroup-data))))
4842
4843 (defsubst gnus-invisible-cut-children (threads)
4844   (let ((num 0))
4845     (while threads
4846       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
4847         (incf num))
4848       (pop threads))
4849     (< num 2)))
4850
4851 (defsubst gnus-cut-thread (thread)
4852   "Go forwards in the thread until we find an article that we want to display."
4853   (when (or (eq gnus-fetch-old-headers 'some)
4854             (eq gnus-build-sparse-threads 'some)
4855             (eq gnus-build-sparse-threads 'more))
4856     ;; Deal with old-fetched headers and sparse threads.
4857     (while (and
4858             thread
4859             (or
4860              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
4861              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
4862             (or (<= (length (cdr thread)) 1)
4863                 (gnus-invisible-cut-children (cdr thread))))
4864       (setq thread (cadr thread))))
4865   thread)
4866
4867 (defun gnus-cut-threads (threads)
4868   "Cut off all uninteresting articles from the beginning of threads."
4869   (when (or (eq gnus-fetch-old-headers 'some)
4870             (eq gnus-build-sparse-threads 'some)
4871             (eq gnus-build-sparse-threads 'more))
4872     (let ((th threads))
4873       (while th
4874         (setcar th (gnus-cut-thread (car th)))
4875         (setq th (cdr th)))))
4876   ;; Remove nixed out threads.
4877   (delq nil threads))
4878
4879 (defun gnus-summary-initial-limit (&optional show-if-empty)
4880   "Figure out what the initial limit is supposed to be on group entry.
4881 This entails weeding out unwanted dormants, low-scored articles,
4882 fetch-old-headers verbiage, and so on."
4883   ;; Most groups have nothing to remove.
4884   (if (or gnus-inhibit-limiting
4885           (and (null gnus-newsgroup-dormant)
4886                (not (eq gnus-fetch-old-headers 'some))
4887                (null gnus-summary-expunge-below)
4888                (not (eq gnus-build-sparse-threads 'some))
4889                (not (eq gnus-build-sparse-threads 'more))
4890                (null gnus-thread-expunge-below)
4891                (not gnus-use-nocem)))
4892       () ; Do nothing.
4893     (push gnus-newsgroup-limit gnus-newsgroup-limits)
4894     (setq gnus-newsgroup-limit nil)
4895     (mapatoms
4896      (lambda (node)
4897        (unless (car (symbol-value node))
4898          ;; These threads have no parents -- they are roots.
4899          (let ((nodes (cdr (symbol-value node)))
4900                thread)
4901            (while nodes
4902              (if (and gnus-thread-expunge-below
4903                       (< (gnus-thread-total-score (car nodes))
4904                          gnus-thread-expunge-below))
4905                  (gnus-expunge-thread (pop nodes))
4906                (setq thread (pop nodes))
4907                (gnus-summary-limit-children thread))))))
4908      gnus-newsgroup-dependencies)
4909     ;; If this limitation resulted in an empty group, we might
4910     ;; pop the previous limit and use it instead.
4911     (when (and (not gnus-newsgroup-limit)
4912                show-if-empty)
4913       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
4914     gnus-newsgroup-limit))
4915
4916 (defun gnus-summary-limit-children (thread)
4917   "Return 1 if this subthread is visible and 0 if it is not."
4918   ;; First we get the number of visible children to this thread.  This
4919   ;; is done by recursing down the thread using this function, so this
4920   ;; will really go down to a leaf article first, before slowly
4921   ;; working its way up towards the root.
4922   (when thread
4923     (let ((children
4924            (if (cdr thread)
4925                (apply '+ (mapcar 'gnus-summary-limit-children
4926                                  (cdr thread)))
4927              0))
4928           (number (mail-header-number (car thread)))
4929           score)
4930       (if (or
4931            ;; If this article is dormant and has absolutely no visible
4932            ;; children, then this article isn't visible.
4933            (and (memq number gnus-newsgroup-dormant)
4934                 (= children 0))
4935            ;; If this is "fetch-old-headered" and there is only one
4936            ;; visible child (or less), then we don't want this article.
4937            (and (eq gnus-fetch-old-headers 'some)
4938                 (memq number gnus-newsgroup-ancient)
4939                 (zerop children))
4940            ;; If this is a sparsely inserted article with no children,
4941            ;; we don't want it.
4942            (and (eq gnus-build-sparse-threads 'some)
4943                 (memq number gnus-newsgroup-sparse)
4944                 (zerop children))
4945            ;; If we use expunging, and this article is really
4946            ;; low-scored, then we don't want this article.
4947            (when (and gnus-summary-expunge-below
4948                       (< (setq score
4949                                (or (cdr (assq number gnus-newsgroup-scored))
4950                                    gnus-summary-default-score))
4951                          gnus-summary-expunge-below))
4952              ;; We increase the expunge-tally here, but that has
4953              ;; nothing to do with the limits, really.
4954              (incf gnus-newsgroup-expunged-tally)
4955              ;; We also mark as read here, if that's wanted.
4956              (when (and gnus-summary-mark-below
4957                         (< score gnus-summary-mark-below))
4958                (setq gnus-newsgroup-unreads
4959                      (delq number gnus-newsgroup-unreads))
4960                (if gnus-newsgroup-auto-expire
4961                    (push number gnus-newsgroup-expirable)
4962                  (push (cons number gnus-low-score-mark)
4963                        gnus-newsgroup-reads)))
4964              t)
4965            (and gnus-use-nocem
4966                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
4967           ;; Nope, invisible article.
4968           0
4969         ;; Ok, this article is to be visible, so we add it to the limit
4970         ;; and return 1.
4971         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
4972         1))))
4973
4974 (defun gnus-expunge-thread (thread)
4975   "Mark all articles in THREAD as read."
4976   (let* ((number (mail-header-number (car thread))))
4977     (incf gnus-newsgroup-expunged-tally)
4978     ;; We also mark as read here, if that's wanted.
4979     (setq gnus-newsgroup-unreads
4980           (delq number gnus-newsgroup-unreads))
4981     (if gnus-newsgroup-auto-expire
4982         (push number gnus-newsgroup-expirable)
4983       (push (cons number gnus-low-score-mark)
4984             gnus-newsgroup-reads)))
4985   ;; Go recursively through all subthreads.
4986   (mapcar 'gnus-expunge-thread (cdr thread)))
4987
4988 ;; Summary article oriented commands
4989
4990 (defun gnus-summary-refer-parent-article (n)
4991   "Refer parent article N times.
4992 The difference between N and the number of articles fetched is returned."
4993   (interactive "p")
4994   (gnus-set-global-variables)
4995   (while
4996       (and
4997        (> n 0)
4998        (let* ((header (gnus-summary-article-header))
4999               (ref
5000                ;; If we try to find the parent of the currently
5001                ;; displayed article, then we take a look at the actual
5002                ;; References header, since this is slightly more
5003                ;; reliable than the References field we got from the
5004                ;; server.
5005                (if (and (eq (mail-header-number header)
5006                             (cdr gnus-article-current))
5007                         (equal gnus-newsgroup-name
5008                                (car gnus-article-current)))
5009                    (save-excursion
5010                      (set-buffer gnus-original-article-buffer)
5011                      (nnheader-narrow-to-headers)
5012                      (prog1
5013                          (message-fetch-field "references")
5014                        (widen)))
5015                  ;; It's not the current article, so we take a bet on
5016                  ;; the value we got from the server.
5017                  (mail-header-references header))))
5018          (if (setq ref (or ref (mail-header-references header)))
5019              (or (gnus-summary-refer-article (gnus-parent-id ref))
5020                  (gnus-message 1 "Couldn't find parent"))
5021            (gnus-message 1 "No references in article %d"
5022                          (gnus-summary-article-number))
5023            nil)))
5024     (setq n (1- n)))
5025   (gnus-summary-position-point)
5026   n)
5027
5028 (defun gnus-summary-refer-references ()
5029   "Fetch all articles mentioned in the References header.
5030 Return how many articles were fetched."
5031   (interactive)
5032   (gnus-set-global-variables)
5033   (let ((ref (mail-header-references (gnus-summary-article-header)))
5034         (current (gnus-summary-article-number))
5035         (n 0))
5036     ;; For each Message-ID in the References header...
5037     (while (string-match "<[^>]*>" ref)
5038       (incf n)
5039       ;; ... fetch that article.
5040       (gnus-summary-refer-article
5041        (prog1 (match-string 0 ref)
5042          (setq ref (substring ref (match-end 0))))))
5043     (gnus-summary-goto-subject current)
5044     (gnus-summary-position-point)
5045     n))
5046
5047 (defun gnus-summary-refer-article (message-id)
5048   "Fetch an article specified by MESSAGE-ID."
5049   (interactive "sMessage-ID: ")
5050   (when (and (stringp message-id)
5051              (not (zerop (length message-id))))
5052     ;; Construct the correct Message-ID if necessary.
5053     ;; Suggested by tale@pawl.rpi.edu.
5054     (unless (string-match "^<" message-id)
5055       (setq message-id (concat "<" message-id)))
5056     (unless (string-match ">$" message-id)
5057       (setq message-id (concat message-id ">")))
5058     (let* ((header (gnus-id-to-header message-id))
5059            (sparse (and header
5060                         (memq (mail-header-number header)
5061                               gnus-newsgroup-sparse))))
5062       (if header
5063           (prog1
5064               ;; The article is present in the buffer, to we just go to it.
5065               (gnus-summary-goto-article 
5066                (mail-header-number header) nil header)
5067             (when sparse
5068               (gnus-summary-update-article (mail-header-number header))))
5069         ;; We fetch the article
5070         (let ((gnus-override-method 
5071                (and (gnus-news-group-p gnus-newsgroup-name)
5072                     gnus-refer-article-method))
5073               number)
5074           ;; Start the special refer-article method, if necessary.
5075           (when (and gnus-refer-article-method
5076                      (gnus-news-group-p gnus-newsgroup-name))
5077             (gnus-check-server gnus-refer-article-method))
5078           ;; Fetch the header, and display the article.
5079           (if (setq number (gnus-summary-insert-subject message-id))
5080               (gnus-summary-select-article nil nil nil number)
5081             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
5082
5083 (defun gnus-summary-enter-digest-group (&optional force)
5084   "Enter a digest group based on the current article."
5085   (interactive "P")
5086   (gnus-set-global-variables)
5087   (gnus-summary-select-article)
5088   (let ((name (format "%s-%d"
5089                       (gnus-group-prefixed-name
5090                        gnus-newsgroup-name (list 'nndoc ""))
5091                       gnus-current-article))
5092         (ogroup gnus-newsgroup-name)
5093         (case-fold-search t)
5094         (buf (current-buffer))
5095         dig)
5096     (save-excursion
5097       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
5098       (insert-buffer-substring gnus-original-article-buffer)
5099       (narrow-to-region
5100        (goto-char (point-min))
5101        (or (search-forward "\n\n" nil t) (point)))
5102       (goto-char (point-min))
5103       (delete-matching-lines "^\\(Path\\):\\|^From ")
5104       (widen))
5105     (unwind-protect
5106         (if (gnus-group-read-ephemeral-group
5107              name `(nndoc ,name (nndoc-address
5108                                  ,(get-buffer dig))
5109                           (nndoc-article-type ,(if force 'digest 'guess))) t)
5110             ;; Make all postings to this group go to the parent group.
5111             (nconc (gnus-info-params (gnus-get-info name))
5112                    (list (cons 'to-group ogroup)))
5113           ;; Couldn't select this doc group.
5114           (switch-to-buffer buf)
5115           (gnus-set-global-variables)
5116           (gnus-configure-windows 'summary)
5117           (gnus-message 3 "Article couldn't be entered?"))
5118       (kill-buffer dig))))
5119
5120 (defun gnus-summary-isearch-article (&optional regexp-p)
5121   "Do incremental search forward on the current article.
5122 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
5123   (interactive "P")
5124   (gnus-set-global-variables)
5125   (gnus-summary-select-article)
5126   (gnus-configure-windows 'article)
5127   (gnus-eval-in-buffer-window gnus-article-buffer
5128     ;;(goto-char (point-min))
5129     (isearch-forward regexp-p)))
5130
5131 (defun gnus-summary-search-article-forward (regexp &optional backward)
5132   "Search for an article containing REGEXP forward.
5133 If BACKWARD, search backward instead."
5134   (interactive
5135    (list (read-string
5136           (format "Search article %s (regexp%s): "
5137                   (if current-prefix-arg "backward" "forward")
5138                   (if gnus-last-search-regexp
5139                       (concat ", default " gnus-last-search-regexp)
5140                     "")))
5141          current-prefix-arg))
5142   (gnus-set-global-variables)
5143   (if (string-equal regexp "")
5144       (setq regexp (or gnus-last-search-regexp ""))
5145     (setq gnus-last-search-regexp regexp))
5146   (unless (gnus-summary-search-article regexp backward)
5147     (error "Search failed: \"%s\"" regexp)))
5148
5149 (defun gnus-summary-search-article-backward (regexp)
5150   "Search for an article containing REGEXP backward."
5151   (interactive
5152    (list (read-string
5153           (format "Search article backward (regexp%s): "
5154                   (if gnus-last-search-regexp
5155                       (concat ", default " gnus-last-search-regexp)
5156                     "")))))
5157   (gnus-summary-search-article-forward regexp 'backward))
5158
5159 (defun gnus-summary-search-article (regexp &optional backward)
5160   "Search for an article containing REGEXP.
5161 Optional argument BACKWARD means do search for backward.
5162 `gnus-select-article-hook' is not called during the search."
5163   (let ((gnus-select-article-hook nil)  ;Disable hook.
5164         (gnus-article-display-hook nil)
5165         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
5166         (re-search
5167          (if backward
5168              're-search-backward 're-search-forward))
5169         (sum (current-buffer))
5170         (found nil))
5171     (gnus-save-hidden-threads
5172       (gnus-summary-select-article)
5173       (set-buffer gnus-article-buffer)
5174       (when backward
5175         (forward-line -1))
5176       (while (not found)
5177         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
5178         (if (if backward
5179                 (re-search-backward regexp nil t)
5180               (re-search-forward regexp nil t))
5181             ;; We found the regexp.
5182             (progn
5183               (setq found 'found)
5184               (beginning-of-line)
5185               (set-window-start
5186                (get-buffer-window (current-buffer))
5187                (point))
5188               (forward-line 1)
5189               (set-buffer sum))
5190           ;; We didn't find it, so we go to the next article.
5191           (set-buffer sum)
5192           (if (not (if backward (gnus-summary-find-prev)
5193                      (gnus-summary-find-next)))
5194               ;; No more articles.
5195               (setq found t)
5196             ;; Select the next article and adjust point.
5197             (gnus-summary-select-article)
5198             (set-buffer gnus-article-buffer)
5199             (widen)
5200             (goto-char (if backward (point-max) (point-min))))))
5201       (gnus-message 7 ""))
5202     ;; Return whether we found the regexp.
5203     (when (eq found 'found)
5204       (gnus-summary-show-thread)
5205       (gnus-summary-goto-subject gnus-current-article)
5206       (gnus-summary-position-point)
5207       t)))
5208
5209 (defun gnus-summary-find-matching (header regexp &optional backward unread
5210                                           not-case-fold)
5211   "Return a list of all articles that match REGEXP on HEADER.
5212 The search stars on the current article and goes forwards unless
5213 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
5214 If UNREAD is non-nil, only unread articles will
5215 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
5216 in the comparisons."
5217   (let ((data (if (eq backward 'all) gnus-newsgroup-data
5218                 (gnus-data-find-list
5219                  (gnus-summary-article-number) (gnus-data-list backward))))
5220         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
5221         (case-fold-search (not not-case-fold))
5222         articles d)
5223     (or (fboundp (intern (concat "mail-header-" header)))
5224         (error "%s is not a valid header" header))
5225     (while data
5226       (setq d (car data))
5227       (and (or (not unread)             ; We want all articles...
5228                (gnus-data-unread-p d))  ; Or just unreads.
5229            (vectorp (gnus-data-header d)) ; It's not a pseudo.
5230            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
5231            (setq articles (cons (gnus-data-number d) articles))) ; Success!
5232       (setq data (cdr data)))
5233     (nreverse articles)))
5234
5235 (defun gnus-summary-execute-command (header regexp command &optional backward)
5236   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
5237 If HEADER is an empty string (or nil), the match is done on the entire
5238 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
5239   (interactive
5240    (list (let ((completion-ignore-case t))
5241            (completing-read
5242             "Header name: "
5243             (mapcar (lambda (string) (list string))
5244                     '("Number" "Subject" "From" "Lines" "Date"
5245                       "Message-ID" "Xref" "References" "Body"))
5246             nil 'require-match))
5247          (read-string "Regexp: ")
5248          (read-key-sequence "Command: ")
5249          current-prefix-arg))
5250   (when (equal header "Body")
5251     (setq header ""))
5252   (gnus-set-global-variables)
5253   ;; Hidden thread subtrees must be searched as well.
5254   (gnus-summary-show-all-threads)
5255   ;; We don't want to change current point nor window configuration.
5256   (save-excursion
5257     (save-window-excursion
5258       (gnus-message 6 "Executing %s..." (key-description command))
5259       ;; We'd like to execute COMMAND interactively so as to give arguments.
5260       (gnus-execute header regexp
5261                     `(lambda () (call-interactively ',(key-binding command)))
5262                     backward)
5263       (gnus-message 6 "Executing %s...done" (key-description command)))))
5264
5265 (defun gnus-summary-beginning-of-article ()
5266   "Scroll the article back to the beginning."
5267   (interactive)
5268   (gnus-set-global-variables)
5269   (gnus-summary-select-article)
5270   (gnus-configure-windows 'article)
5271   (gnus-eval-in-buffer-window gnus-article-buffer
5272     (widen)
5273     (goto-char (point-min))
5274     (and gnus-break-pages (gnus-narrow-to-page))))
5275
5276 (defun gnus-summary-end-of-article ()
5277   "Scroll to the end of the article."
5278   (interactive)
5279   (gnus-set-global-variables)
5280   (gnus-summary-select-article)
5281   (gnus-configure-windows 'article)
5282   (gnus-eval-in-buffer-window gnus-article-buffer
5283     (widen)
5284     (goto-char (point-max))
5285     (recenter -3)
5286     (and gnus-break-pages (gnus-narrow-to-page))))
5287
5288 (defun gnus-summary-show-article (&optional arg)
5289   "Force re-fetching of the current article.
5290 If ARG (the prefix) is non-nil, show the raw article without any
5291 article massaging functions being run."
5292   (interactive "P")
5293   (gnus-set-global-variables)
5294   (if (not arg)
5295       ;; Select the article the normal way.
5296       (gnus-summary-select-article nil 'force)
5297     ;; Bind the article treatment functions to nil.
5298     (let ((gnus-have-all-headers t)
5299           gnus-article-display-hook
5300           gnus-article-prepare-hook
5301           gnus-break-pages
5302           gnus-visual)
5303       (gnus-summary-select-article nil 'force)))
5304   (gnus-summary-goto-subject gnus-current-article)
5305 ;  (gnus-configure-windows 'article)
5306   (gnus-summary-position-point))
5307
5308 (defun gnus-summary-verbose-headers (&optional arg)
5309   "Toggle permanent full header display.
5310 If ARG is a positive number, turn header display on.
5311 If ARG is a negative number, turn header display off."
5312   (interactive "P")
5313   (gnus-set-global-variables)
5314   (gnus-summary-toggle-header arg)
5315   (setq gnus-show-all-headers
5316         (cond ((or (not (numberp arg))
5317                    (zerop arg))
5318                (not gnus-show-all-headers))
5319               ((natnump arg)
5320                t))))
5321
5322 (defun gnus-summary-toggle-header (&optional arg)
5323   "Show the headers if they are hidden, or hide them if they are shown.
5324 If ARG is a positive number, show the entire header.
5325 If ARG is a negative number, hide the unwanted header lines."
5326   (interactive "P")
5327   (gnus-set-global-variables)
5328   (save-excursion
5329     (set-buffer gnus-article-buffer)
5330     (let* ((buffer-read-only nil)
5331            (inhibit-point-motion-hooks t)
5332            (hidden (text-property-any
5333                     (goto-char (point-min)) (search-forward "\n\n")
5334                     'invisible t))
5335            e)
5336       (goto-char (point-min))
5337       (when (search-forward "\n\n" nil t)
5338         (delete-region (point-min) (1- (point))))
5339       (goto-char (point-min))
5340       (save-excursion
5341         (set-buffer gnus-original-article-buffer)
5342         (goto-char (point-min))
5343         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
5344       (insert-buffer-substring gnus-original-article-buffer 1 e)
5345       (let ((article-inhibit-hiding t))
5346         (run-hooks 'gnus-article-display-hook))
5347       (if (or (not hidden) (and (numberp arg) (< arg 0)))
5348           (gnus-article-hide-headers)))))
5349
5350 (defun gnus-summary-show-all-headers ()
5351   "Make all header lines visible."
5352   (interactive)
5353   (gnus-set-global-variables)
5354   (gnus-article-show-all-headers))
5355
5356 (defun gnus-summary-toggle-mime (&optional arg)
5357   "Toggle MIME processing.
5358 If ARG is a positive number, turn MIME processing on."
5359   (interactive "P")
5360   (gnus-set-global-variables)
5361   (setq gnus-show-mime
5362         (if (null arg) (not gnus-show-mime)
5363           (> (prefix-numeric-value arg) 0)))
5364   (gnus-summary-select-article t 'force))
5365
5366 (defun gnus-summary-caesar-message (&optional arg)
5367   "Caesar rotate the current article by 13.
5368 The numerical prefix specifies how manu places to rotate each letter
5369 forward."
5370   (interactive "P")
5371   (gnus-set-global-variables)
5372   (gnus-summary-select-article)
5373   (let ((mail-header-separator ""))
5374     (gnus-eval-in-buffer-window gnus-article-buffer
5375       (save-restriction
5376         (widen)
5377         (let ((start (window-start))
5378               buffer-read-only)
5379           (message-caesar-buffer-body arg)
5380           (set-window-start (get-buffer-window (current-buffer)) start))))))
5381
5382 (defun gnus-summary-stop-page-breaking ()
5383   "Stop page breaking in the current article."
5384   (interactive)
5385   (gnus-set-global-variables)
5386   (gnus-summary-select-article)
5387   (gnus-eval-in-buffer-window gnus-article-buffer
5388     (widen)))
5389
5390 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
5391   "Move the current article to a different newsgroup.
5392 If N is a positive number, move the N next articles.
5393 If N is a negative number, move the N previous articles.
5394 If N is nil and any articles have been marked with the process mark,
5395 move those articles instead.
5396 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
5397 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
5398 re-spool using this method.
5399
5400 For this function to work, both the current newsgroup and the
5401 newsgroup that you want to move to have to support the `request-move'
5402 and `request-accept' functions."
5403   (interactive "P")
5404   (unless action (setq action 'move))
5405   (gnus-set-global-variables)
5406   ;; Check whether the source group supports the required functions.
5407   (cond ((and (eq action 'move)
5408               (not (gnus-check-backend-function
5409                     'request-move-article gnus-newsgroup-name)))
5410          (error "The current group does not support article moving"))
5411         ((and (eq action 'crosspost)
5412               (not (gnus-check-backend-function
5413                     'request-replace-article gnus-newsgroup-name)))
5414          (error "The current group does not support article editing")))
5415   (let ((articles (gnus-summary-work-articles n))
5416         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
5417         (names '((move "Move" "Moving")
5418                  (copy "Copy" "Copying")
5419                  (crosspost "Crosspost" "Crossposting")))
5420         (copy-buf (save-excursion
5421                     (nnheader-set-temp-buffer " *copy article*")))
5422         art-group to-method new-xref article to-groups)
5423     (unless (assq action names)
5424       (error "Unknown action %s" action))
5425     ;; Read the newsgroup name.
5426     (when (and (not to-newsgroup)
5427                (not select-method))
5428       (setq to-newsgroup
5429             (gnus-read-move-group-name
5430              (cadr (assq action names))
5431              (symbol-value (intern (format "gnus-current-%s-group" action)))
5432              articles prefix))
5433       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
5434     (setq to-method (or select-method 
5435                         (gnus-group-name-to-method to-newsgroup)))
5436     ;; Check the method we are to move this article to...
5437     (or (gnus-check-backend-function 'request-accept-article (car to-method))
5438         (error "%s does not support article copying" (car to-method)))
5439     (or (gnus-check-server to-method)
5440         (error "Can't open server %s" (car to-method)))
5441     (gnus-message 6 "%s to %s: %s..."
5442                   (caddr (assq action names))
5443                   (or (car select-method) to-newsgroup) articles)
5444     (while articles
5445       (setq article (pop articles))
5446       (setq
5447        art-group
5448        (cond
5449         ;; Move the article.
5450         ((eq action 'move)
5451          (gnus-request-move-article
5452           article                       ; Article to move
5453           gnus-newsgroup-name           ; From newsgrouo
5454           (nth 1 (gnus-find-method-for-group
5455                   gnus-newsgroup-name)) ; Server
5456           (list 'gnus-request-accept-article
5457                 to-newsgroup (list 'quote select-method)
5458                 (not articles))         ; Accept form
5459           (not articles)))              ; Only save nov last time
5460         ;; Copy the article.
5461         ((eq action 'copy)
5462          (save-excursion
5463            (set-buffer copy-buf)
5464            (gnus-request-article-this-buffer article gnus-newsgroup-name)
5465            (gnus-request-accept-article
5466             to-newsgroup select-method (not articles))))
5467         ;; Crosspost the article.
5468         ((eq action 'crosspost)
5469          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
5470            (setq new-xref (concat gnus-newsgroup-name ":" article))
5471            (if (and xref (not (string= xref "")))
5472                (progn
5473                  (when (string-match "^Xref: " xref)
5474                    (setq xref (substring xref (match-end 0))))
5475                  (setq new-xref (concat xref " " new-xref)))
5476              (setq new-xref (concat (system-name) " " new-xref)))
5477            (save-excursion
5478              (set-buffer copy-buf)
5479              (gnus-request-article-this-buffer article gnus-newsgroup-name)
5480              (nnheader-replace-header "xref" new-xref)
5481              (gnus-request-accept-article
5482               to-newsgroup select-method (not articles)))))))
5483       (if (not art-group)
5484           (gnus-message 1 "Couldn't %s article %s"
5485                         (cadr (assq action names)) article)
5486         (let* ((entry
5487                 (or
5488                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
5489                  (gnus-gethash
5490                   (gnus-group-prefixed-name
5491                    (car art-group)
5492                    (or select-method 
5493                        (gnus-find-method-for-group to-newsgroup)))
5494                   gnus-newsrc-hashtb)))
5495                (info (nth 2 entry))
5496                (to-group (gnus-info-group info)))
5497           ;; Update the group that has been moved to.
5498           (when (and info
5499                      (memq action '(move copy)))
5500             (unless (member to-group to-groups)
5501               (push to-group to-groups))
5502
5503             (unless (memq article gnus-newsgroup-unreads)
5504               (gnus-info-set-read
5505                info (gnus-add-to-range (gnus-info-read info)
5506                                        (list (cdr art-group)))))
5507
5508             ;; Copy any marks over to the new group.
5509             (let ((marks gnus-article-mark-lists)
5510                   (to-article (cdr art-group)))
5511
5512               ;; See whether the article is to be put in the cache.
5513               (when gnus-use-cache
5514                 (gnus-cache-possibly-enter-article
5515                  to-group to-article
5516                  (let ((header (copy-sequence
5517                                 (gnus-summary-article-header article))))
5518                    (mail-header-set-number header to-article)
5519                    header)
5520                  (memq article gnus-newsgroup-marked)
5521                  (memq article gnus-newsgroup-dormant)
5522                  (memq article gnus-newsgroup-unreads)))
5523
5524               (while marks
5525                 (when (memq article (symbol-value
5526                                      (intern (format "gnus-newsgroup-%s"
5527                                                      (caar marks)))))
5528                   ;; If the other group is the same as this group,
5529                   ;; then we have to add the mark to the list.
5530                   (when (equal to-group gnus-newsgroup-name)
5531                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
5532                          (cons to-article
5533                                (symbol-value
5534                                 (intern (format "gnus-newsgroup-%s"
5535                                                 (caar marks)))))))
5536                   ;; Copy mark to other group.
5537                   (gnus-add-marked-articles
5538                    to-group (cdar marks) (list to-article) info))
5539                 (setq marks (cdr marks)))))
5540
5541           ;; Update the Xref header in this article to point to
5542           ;; the new crossposted article we have just created.
5543           (when (eq action 'crosspost)
5544             (save-excursion
5545               (set-buffer copy-buf)
5546               (gnus-request-article-this-buffer article gnus-newsgroup-name)
5547               (nnheader-replace-header
5548                "xref" (concat new-xref " " (gnus-group-prefixed-name
5549                                             (car art-group) to-method)
5550                               ":" (cdr art-group)))
5551               (gnus-request-replace-article
5552                article gnus-newsgroup-name (current-buffer)))))
5553
5554         (gnus-summary-goto-subject article)
5555         (when (eq action 'move)
5556           (gnus-summary-mark-article article gnus-canceled-mark)))
5557       (gnus-summary-remove-process-mark article))
5558     ;; Re-activate all groups that have been moved to.
5559     (while to-groups
5560       (gnus-activate-group (pop to-groups)))
5561     
5562     (gnus-kill-buffer copy-buf)
5563     (gnus-summary-position-point)
5564     (gnus-set-mode-line 'summary)))
5565
5566 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
5567   "Move the current article to a different newsgroup.
5568 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
5569 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
5570 re-spool using this method."
5571   (interactive "P")
5572   (gnus-summary-move-article n nil select-method 'copy))
5573
5574 (defun gnus-summary-crosspost-article (&optional n)
5575   "Crosspost the current article to some other group."
5576   (interactive "P")
5577   (gnus-summary-move-article n nil nil 'crosspost))
5578
5579 (defvar gnus-summary-respool-default-method nil
5580   "Default method for respooling an article.  
5581 If nil, use to the current newsgroup method.")
5582
5583 (defun gnus-summary-respool-article (&optional n method)
5584   "Respool the current article.
5585 The article will be squeezed through the mail spooling process again,
5586 which means that it will be put in some mail newsgroup or other
5587 depending on `nnmail-split-methods'.
5588 If N is a positive number, respool the N next articles.
5589 If N is a negative number, respool the N previous articles.
5590 If N is nil and any articles have been marked with the process mark,
5591 respool those articles instead.
5592
5593 Respooling can be done both from mail groups and \"real\" newsgroups.
5594 In the former case, the articles in question will be moved from the
5595 current group into whatever groups they are destined to.  In the
5596 latter case, they will be copied into the relevant groups."
5597   (interactive 
5598    (list current-prefix-arg
5599          (let* ((methods (gnus-methods-using 'respool))
5600                 (methname
5601                  (symbol-name (or gnus-summary-respool-default-method
5602                                   (car (gnus-find-method-for-group
5603                                         gnus-newsgroup-name)))))
5604                 (method
5605                  (gnus-completing-read 
5606                   methname "What backend do you want to use when respooling?"
5607                   methods nil t nil 'gnus-method-history))
5608                 ms)
5609            (cond
5610             ((zerop (length (setq ms (gnus-servers-using-backend method))))
5611              (list (intern method) ""))
5612             ((= 1 (length ms))
5613              (car ms))
5614             (t
5615              (cdr (completing-read 
5616                    "Server name: "
5617                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
5618   (gnus-set-global-variables)
5619   (unless method
5620     (error "No method given for respooling"))
5621   (if (assoc (symbol-name
5622               (car (gnus-find-method-for-group gnus-newsgroup-name)))
5623              (gnus-methods-using 'respool))
5624       (gnus-summary-move-article n nil method)
5625     (gnus-summary-copy-article n nil method)))
5626
5627 (defun gnus-summary-import-article (file)
5628   "Import a random file into a mail newsgroup."
5629   (interactive "fImport file: ")
5630   (gnus-set-global-variables)
5631   (let ((group gnus-newsgroup-name)
5632         (now (current-time))
5633         atts lines)
5634     (or (gnus-check-backend-function 'request-accept-article group)
5635         (error "%s does not support article importing" group))
5636     (or (file-readable-p file)
5637         (not (file-regular-p file))
5638         (error "Can't read %s" file))
5639     (save-excursion
5640       (set-buffer (get-buffer-create " *import file*"))
5641       (buffer-disable-undo (current-buffer))
5642       (erase-buffer)
5643       (insert-file-contents file)
5644       (goto-char (point-min))
5645       (unless (nnheader-article-p)
5646         ;; This doesn't look like an article, so we fudge some headers.
5647         (setq atts (file-attributes file)
5648               lines (count-lines (point-min) (point-max)))
5649         (insert "From: " (read-string "From: ") "\n"
5650                 "Subject: " (read-string "Subject: ") "\n"
5651                 "Date: " (timezone-make-date-arpa-standard
5652                           (current-time-string (nth 5 atts))
5653                           (current-time-zone now)
5654                           (current-time-zone now)) "\n"
5655                 "Message-ID: " (message-make-message-id) "\n"
5656                 "Lines: " (int-to-string lines) "\n"
5657                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
5658       (gnus-request-accept-article group nil t)
5659       (kill-buffer (current-buffer)))))
5660
5661 (defun gnus-summary-expire-articles (&optional now)
5662   "Expire all articles that are marked as expirable in the current group."
5663   (interactive)
5664   (gnus-set-global-variables)
5665   (when (gnus-check-backend-function
5666          'request-expire-articles gnus-newsgroup-name)
5667     ;; This backend supports expiry.
5668     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
5669            (expirable (if total
5670                           (gnus-list-of-read-articles gnus-newsgroup-name)
5671                         (setq gnus-newsgroup-expirable
5672                               (sort gnus-newsgroup-expirable '<))))
5673            (expiry-wait (if now 'immediate
5674                           (gnus-group-get-parameter
5675                            gnus-newsgroup-name 'expiry-wait)))
5676            es)
5677       (when expirable
5678         ;; There are expirable articles in this group, so we run them
5679         ;; through the expiry process.
5680         (gnus-message 6 "Expiring articles...")
5681         ;; The list of articles that weren't expired is returned.
5682         (if expiry-wait
5683             (let ((nnmail-expiry-wait-function nil)
5684                   (nnmail-expiry-wait expiry-wait))
5685               (setq es (gnus-request-expire-articles
5686                         expirable gnus-newsgroup-name)))
5687           (setq es (gnus-request-expire-articles
5688                     expirable gnus-newsgroup-name)))
5689         (or total (setq gnus-newsgroup-expirable es))
5690         ;; We go through the old list of expirable, and mark all
5691         ;; really expired articles as nonexistent.
5692         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
5693           (let ((gnus-use-cache nil))
5694             (while expirable
5695               (unless (memq (car expirable) es)
5696                 (when (gnus-data-find (car expirable))
5697                   (gnus-summary-mark-article
5698                    (car expirable) gnus-canceled-mark)))
5699               (setq expirable (cdr expirable)))))
5700         (gnus-message 6 "Expiring articles...done")))))
5701
5702 (defun gnus-summary-expire-articles-now ()
5703   "Expunge all expirable articles in the current group.
5704 This means that *all* articles that are marked as expirable will be
5705 deleted forever, right now."
5706   (interactive)
5707   (gnus-set-global-variables)
5708   (or gnus-expert-user
5709       (gnus-y-or-n-p
5710        "Are you really, really, really sure you want to delete all these messages? ")
5711       (error "Phew!"))
5712   (gnus-summary-expire-articles t))
5713
5714 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
5715 (defun gnus-summary-delete-article (&optional n)
5716   "Delete the N next (mail) articles.
5717 This command actually deletes articles.  This is not a marking
5718 command.  The article will disappear forever from your life, never to
5719 return.
5720 If N is negative, delete backwards.
5721 If N is nil and articles have been marked with the process mark,
5722 delete these instead."
5723   (interactive "P")
5724   (gnus-set-global-variables)
5725   (or (gnus-check-backend-function 'request-expire-articles
5726                                    gnus-newsgroup-name)
5727       (error "The current newsgroup does not support article deletion."))
5728   ;; Compute the list of articles to delete.
5729   (let ((articles (gnus-summary-work-articles n))
5730         not-deleted)
5731     (if (and gnus-novice-user
5732              (not (gnus-y-or-n-p
5733                    (format "Do you really want to delete %s forever? "
5734                            (if (> (length articles) 1) 
5735                                (format "these %s articles" (length articles))
5736                              "this article")))))
5737         ()
5738       ;; Delete the articles.
5739       (setq not-deleted (gnus-request-expire-articles
5740                          articles gnus-newsgroup-name 'force))
5741       (while articles
5742         (gnus-summary-remove-process-mark (car articles))
5743         ;; The backend might not have been able to delete the article
5744         ;; after all.
5745         (or (memq (car articles) not-deleted)
5746             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
5747         (setq articles (cdr articles))))
5748     (gnus-summary-position-point)
5749     (gnus-set-mode-line 'summary)
5750     not-deleted))
5751
5752 (defun gnus-summary-edit-article (&optional force)
5753   "Enter into a buffer and edit the current article.
5754 This will have permanent effect only in mail groups.
5755 If FORCE is non-nil, allow editing of articles even in read-only
5756 groups."
5757   (interactive "P")
5758   (save-excursion
5759     (set-buffer gnus-summary-buffer)
5760     (gnus-set-global-variables)
5761     (when (and (not force)
5762                (gnus-group-read-only-p))
5763       (error "The current newsgroup does not support article editing."))
5764     (gnus-summary-select-article t nil t)
5765     (gnus-configure-windows 'article)
5766     (select-window (get-buffer-window gnus-article-buffer))
5767     (gnus-message 6 "C-c C-c to end edits")
5768     (setq buffer-read-only nil)
5769     (gnus-article-edit-mode)
5770     (buffer-enable-undo)
5771     (widen)
5772     (goto-char (point-min))
5773     (search-forward "\n\n" nil t)))
5774
5775 (defun gnus-summary-edit-article-done ()
5776   "Make edits to the current article permanent."
5777   (interactive)
5778   (if (gnus-group-read-only-p)
5779       (progn
5780         (let ((beep (not (eq major-mode 'text-mode))))
5781           (gnus-summary-edit-article-postpone)
5782           (when beep
5783             (gnus-error
5784              3 "The current newsgroup does not support article editing."))))
5785     (let ((buf (format "%s" (buffer-string))))
5786       (erase-buffer)
5787       (insert buf)
5788       (if (not (gnus-request-replace-article
5789                 (cdr gnus-article-current) (car gnus-article-current)
5790                 (current-buffer)))
5791           (error "Couldn't replace article.")
5792         (gnus-article-mode)
5793         (use-local-map gnus-article-mode-map)
5794         (setq buffer-read-only t)
5795         (buffer-disable-undo (current-buffer))
5796         (gnus-configure-windows 'summary)
5797         (gnus-summary-update-article (cdr gnus-article-current))
5798         (when gnus-use-cache
5799           (gnus-cache-update-article    
5800            (car gnus-article-current) (cdr gnus-article-current)))
5801         (when gnus-keep-backlog
5802           (gnus-backlog-remove-article 
5803            (car gnus-article-current) (cdr gnus-article-current))))
5804       (save-excursion
5805         (when (get-buffer gnus-original-article-buffer)
5806           (set-buffer gnus-original-article-buffer)
5807           (setq gnus-original-article nil)))
5808       (setq gnus-article-current nil
5809             gnus-current-article nil)
5810       (run-hooks 'gnus-article-display-hook)
5811       (and (gnus-visual-p 'summary-highlight 'highlight)
5812            (run-hooks 'gnus-visual-mark-article-hook)))))
5813
5814 (defun gnus-summary-edit-article-postpone ()
5815   "Postpone changes to the current article."
5816   (interactive)
5817   (gnus-article-mode)
5818   (use-local-map gnus-article-mode-map)
5819   (setq buffer-read-only t)
5820   (buffer-disable-undo (current-buffer))
5821   (gnus-configure-windows 'summary)
5822   (and (gnus-visual-p 'summary-highlight 'highlight)
5823        (run-hooks 'gnus-visual-mark-article-hook)))
5824
5825 (defun gnus-summary-edit-wash (key)
5826   "Perform editing command in the article buffer."
5827   (interactive 
5828    (list
5829     (progn
5830       (message "%s" (concat (this-command-keys) "- "))
5831       (read-char))))
5832   (message "")
5833   (gnus-summary-edit-article)
5834   (execute-kbd-macro (concat (this-command-keys) key))
5835   (gnus-summary-edit-article-done))
5836
5837 ;;; Respooling
5838
5839 (defun gnus-summary-respool-query ()
5840   "Query where the respool algorithm would put this article."
5841   (interactive)
5842   (gnus-set-global-variables)
5843   (gnus-summary-select-article)
5844   (save-excursion
5845     (set-buffer gnus-article-buffer)
5846     (save-restriction
5847       (goto-char (point-min))
5848       (search-forward "\n\n")
5849       (narrow-to-region (point-min) (point))
5850       (pp-eval-expression
5851        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
5852
5853 ;; Summary marking commands.
5854
5855 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
5856   "Mark articles which has the same subject as read, and then select the next.
5857 If UNMARK is positive, remove any kind of mark.
5858 If UNMARK is negative, tick articles."
5859   (interactive "P")
5860   (gnus-set-global-variables)
5861   (if unmark
5862       (setq unmark (prefix-numeric-value unmark)))
5863   (let ((count
5864          (gnus-summary-mark-same-subject
5865           (gnus-summary-article-subject) unmark)))
5866     ;; Select next unread article.  If auto-select-same mode, should
5867     ;; select the first unread article.
5868     (gnus-summary-next-article t (and gnus-auto-select-same
5869                                       (gnus-summary-article-subject)))
5870     (gnus-message 7 "%d article%s marked as %s"
5871                   count (if (= count 1) " is" "s are")
5872                   (if unmark "unread" "read"))))
5873
5874 (defun gnus-summary-kill-same-subject (&optional unmark)
5875   "Mark articles which has the same subject as read.
5876 If UNMARK is positive, remove any kind of mark.
5877 If UNMARK is negative, tick articles."
5878   (interactive "P")
5879   (gnus-set-global-variables)
5880   (if unmark
5881       (setq unmark (prefix-numeric-value unmark)))
5882   (let ((count
5883          (gnus-summary-mark-same-subject
5884           (gnus-summary-article-subject) unmark)))
5885     ;; If marked as read, go to next unread subject.
5886     (if (null unmark)
5887         ;; Go to next unread subject.
5888         (gnus-summary-next-subject 1 t))
5889     (gnus-message 7 "%d articles are marked as %s"
5890                   count (if unmark "unread" "read"))))
5891
5892 (defun gnus-summary-mark-same-subject (subject &optional unmark)
5893   "Mark articles with same SUBJECT as read, and return marked number.
5894 If optional argument UNMARK is positive, remove any kinds of marks.
5895 If optional argument UNMARK is negative, mark articles as unread instead."
5896   (let ((count 1))
5897     (save-excursion
5898       (cond
5899        ((null unmark)                   ; Mark as read.
5900         (while (and
5901                 (progn
5902                   (gnus-summary-mark-article-as-read gnus-killed-mark)
5903                   (gnus-summary-show-thread) t)
5904                 (gnus-summary-find-subject subject))
5905           (setq count (1+ count))))
5906        ((> unmark 0)                    ; Tick.
5907         (while (and
5908                 (progn
5909                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
5910                   (gnus-summary-show-thread) t)
5911                 (gnus-summary-find-subject subject))
5912           (setq count (1+ count))))
5913        (t                               ; Mark as unread.
5914         (while (and
5915                 (progn
5916                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
5917                   (gnus-summary-show-thread) t)
5918                 (gnus-summary-find-subject subject))
5919           (setq count (1+ count)))))
5920       (gnus-set-mode-line 'summary)
5921       ;; Return the number of marked articles.
5922       count)))
5923
5924 (defun gnus-summary-mark-as-processable (n &optional unmark)
5925   "Set the process mark on the next N articles.
5926 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
5927 the process mark instead.  The difference between N and the actual
5928 number of articles marked is returned."
5929   (interactive "p")
5930   (gnus-set-global-variables)
5931   (let ((backward (< n 0))
5932         (n (abs n)))
5933     (while (and
5934             (> n 0)
5935             (if unmark
5936                 (gnus-summary-remove-process-mark
5937                  (gnus-summary-article-number))
5938               (gnus-summary-set-process-mark (gnus-summary-article-number)))
5939             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
5940       (setq n (1- n)))
5941     (if (/= 0 n) (gnus-message 7 "No more articles"))
5942     (gnus-summary-recenter)
5943     (gnus-summary-position-point)
5944     n))
5945
5946 (defun gnus-summary-unmark-as-processable (n)
5947   "Remove the process mark from the next N articles.
5948 If N is negative, mark backward instead.  The difference between N and
5949 the actual number of articles marked is returned."
5950   (interactive "p")
5951   (gnus-set-global-variables)
5952   (gnus-summary-mark-as-processable n t))
5953
5954 (defun gnus-summary-unmark-all-processable ()
5955   "Remove the process mark from all articles."
5956   (interactive)
5957   (gnus-set-global-variables)
5958   (save-excursion
5959     (while gnus-newsgroup-processable
5960       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
5961   (gnus-summary-position-point))
5962
5963 (defun gnus-summary-mark-as-expirable (n)
5964   "Mark N articles forward as expirable.
5965 If N is negative, mark backward instead.  The difference between N and
5966 the actual number of articles marked is returned."
5967   (interactive "p")
5968   (gnus-set-global-variables)
5969   (gnus-summary-mark-forward n gnus-expirable-mark))
5970
5971 (defun gnus-summary-mark-article-as-replied (article)
5972   "Mark ARTICLE replied and update the summary line."
5973   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
5974   (let ((buffer-read-only nil))
5975     (when (gnus-summary-goto-subject article)
5976       (gnus-summary-update-secondary-mark article))))
5977
5978 (defun gnus-summary-set-bookmark (article)
5979   "Set a bookmark in current article."
5980   (interactive (list (gnus-summary-article-number)))
5981   (gnus-set-global-variables)
5982   (if (or (not (get-buffer gnus-article-buffer))
5983           (not gnus-current-article)
5984           (not gnus-article-current)
5985           (not (equal gnus-newsgroup-name (car gnus-article-current))))
5986       (error "No current article selected"))
5987   ;; Remove old bookmark, if one exists.
5988   (let ((old (assq article gnus-newsgroup-bookmarks)))
5989     (if old (setq gnus-newsgroup-bookmarks
5990                   (delq old gnus-newsgroup-bookmarks))))
5991   ;; Set the new bookmark, which is on the form
5992   ;; (article-number . line-number-in-body).
5993   (setq gnus-newsgroup-bookmarks
5994         (cons
5995          (cons article
5996                (save-excursion
5997                  (set-buffer gnus-article-buffer)
5998                  (count-lines
5999                   (min (point)
6000                        (save-excursion
6001                          (goto-char (point-min))
6002                          (search-forward "\n\n" nil t)
6003                          (point)))
6004                   (point))))
6005          gnus-newsgroup-bookmarks))
6006   (gnus-message 6 "A bookmark has been added to the current article."))
6007
6008 (defun gnus-summary-remove-bookmark (article)
6009   "Remove the bookmark from the current article."
6010   (interactive (list (gnus-summary-article-number)))
6011   (gnus-set-global-variables)
6012   ;; Remove old bookmark, if one exists.
6013   (let ((old (assq article gnus-newsgroup-bookmarks)))
6014     (if old
6015         (progn
6016           (setq gnus-newsgroup-bookmarks
6017                 (delq old gnus-newsgroup-bookmarks))
6018           (gnus-message 6 "Removed bookmark."))
6019       (gnus-message 6 "No bookmark in current article."))))
6020
6021 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6022 (defun gnus-summary-mark-as-dormant (n)
6023   "Mark N articles forward as dormant.
6024 If N is negative, mark backward instead.  The difference between N and
6025 the actual number of articles marked is returned."
6026   (interactive "p")
6027   (gnus-set-global-variables)
6028   (gnus-summary-mark-forward n gnus-dormant-mark))
6029
6030 (defun gnus-summary-set-process-mark (article)
6031   "Set the process mark on ARTICLE and update the summary line."
6032   (setq gnus-newsgroup-processable
6033         (cons article
6034               (delq article gnus-newsgroup-processable)))
6035   (when (gnus-summary-goto-subject article)
6036     (gnus-summary-show-thread)
6037     (gnus-summary-update-secondary-mark article)))
6038
6039 (defun gnus-summary-remove-process-mark (article)
6040   "Remove the process mark from ARTICLE and update the summary line."
6041   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
6042   (when (gnus-summary-goto-subject article)
6043     (gnus-summary-show-thread)
6044     (gnus-summary-update-secondary-mark article)))
6045
6046 (defun gnus-summary-set-saved-mark (article)
6047   "Set the process mark on ARTICLE and update the summary line."
6048   (push article gnus-newsgroup-saved)
6049   (when (gnus-summary-goto-subject article)
6050     (gnus-summary-update-secondary-mark article)))
6051
6052 (defun gnus-summary-mark-forward (n &optional mark no-expire)
6053   "Mark N articles as read forwards.
6054 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
6055 The difference between N and the actual number of articles marked is
6056 returned."
6057   (interactive "p")
6058   (gnus-set-global-variables)
6059   (let ((backward (< n 0))
6060         (gnus-summary-goto-unread
6061          (and gnus-summary-goto-unread
6062               (not (eq gnus-summary-goto-unread 'never))
6063               (not (memq mark (list gnus-unread-mark
6064                                     gnus-ticked-mark gnus-dormant-mark)))))
6065         (n (abs n))
6066         (mark (or mark gnus-del-mark)))
6067     (while (and (> n 0)
6068                 (gnus-summary-mark-article nil mark no-expire)
6069                 (zerop (gnus-summary-next-subject
6070                         (if backward -1 1)
6071                         (and gnus-summary-goto-unread
6072                              (not (eq gnus-summary-goto-unread 'never)))
6073                         t)))
6074       (setq n (1- n)))
6075     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
6076     (gnus-summary-recenter)
6077     (gnus-summary-position-point)
6078     (gnus-set-mode-line 'summary)
6079     n))
6080
6081 (defun gnus-summary-mark-article-as-read (mark)
6082   "Mark the current article quickly as read with MARK."
6083   (let ((article (gnus-summary-article-number)))
6084     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6085     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6086     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6087     (setq gnus-newsgroup-reads
6088           (cons (cons article mark) gnus-newsgroup-reads))
6089     ;; Possibly remove from cache, if that is used.
6090     (and gnus-use-cache (gnus-cache-enter-remove-article article))
6091     ;; Allow the backend to change the mark.
6092     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
6093     ;; Check for auto-expiry.
6094     (when (and gnus-newsgroup-auto-expire
6095                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6096                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6097                    (= mark gnus-ancient-mark)
6098                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
6099       (setq mark gnus-expirable-mark)
6100       (push article gnus-newsgroup-expirable))
6101     ;; Set the mark in the buffer.
6102     (gnus-summary-update-mark mark 'unread)
6103     t))
6104
6105 (defun gnus-summary-mark-article-as-unread (mark)
6106   "Mark the current article quickly as unread with MARK."
6107   (let ((article (gnus-summary-article-number)))
6108     (if (< article 0)
6109         (gnus-error 1 "Unmarkable article")
6110       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6111       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6112       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
6113       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
6114       (cond ((= mark gnus-ticked-mark)
6115              (push article gnus-newsgroup-marked))
6116             ((= mark gnus-dormant-mark)
6117              (push article gnus-newsgroup-dormant))
6118             (t
6119              (push article gnus-newsgroup-unreads)))
6120       (setq gnus-newsgroup-reads
6121             (delq (assq article gnus-newsgroup-reads)
6122                   gnus-newsgroup-reads))
6123
6124       ;; See whether the article is to be put in the cache.
6125       (and gnus-use-cache
6126            (vectorp (gnus-summary-article-header article))
6127            (save-excursion
6128              (gnus-cache-possibly-enter-article
6129               gnus-newsgroup-name article
6130               (gnus-summary-article-header article)
6131               (= mark gnus-ticked-mark)
6132               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6133
6134       ;; Fix the mark.
6135       (gnus-summary-update-mark mark 'unread))
6136     t))
6137
6138 (defun gnus-summary-mark-article (&optional article mark no-expire)
6139   "Mark ARTICLE with MARK.  MARK can be any character.
6140 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
6141 `??' (dormant) and `?E' (expirable).
6142 If MARK is nil, then the default character `?D' is used.
6143 If ARTICLE is nil, then the article on the current line will be
6144 marked."
6145   ;; The mark might be a string.
6146   (and (stringp mark)
6147        (setq mark (aref mark 0)))
6148   ;; If no mark is given, then we check auto-expiring.
6149   (and (not no-expire)
6150        gnus-newsgroup-auto-expire
6151        (or (not mark)
6152            (and (numberp mark)
6153                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6154                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6155                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
6156        (setq mark gnus-expirable-mark))
6157   (let* ((mark (or mark gnus-del-mark))
6158          (article (or article (gnus-summary-article-number))))
6159     (or article (error "No article on current line"))
6160     (if (or (= mark gnus-unread-mark)
6161             (= mark gnus-ticked-mark)
6162             (= mark gnus-dormant-mark))
6163         (gnus-mark-article-as-unread article mark)
6164       (gnus-mark-article-as-read article mark))
6165
6166     ;; See whether the article is to be put in the cache.
6167     (and gnus-use-cache
6168          (not (= mark gnus-canceled-mark))
6169          (vectorp (gnus-summary-article-header article))
6170          (save-excursion
6171            (gnus-cache-possibly-enter-article
6172             gnus-newsgroup-name article
6173             (gnus-summary-article-header article)
6174             (= mark gnus-ticked-mark)
6175             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6176
6177     (if (gnus-summary-goto-subject article nil t)
6178         (let ((buffer-read-only nil))
6179           (gnus-summary-show-thread)
6180           ;; Fix the mark.
6181           (gnus-summary-update-mark mark 'unread)
6182           t))))
6183
6184 (defun gnus-summary-update-secondary-mark (article)
6185   "Update the secondary (read, process, cache) mark."
6186   (gnus-summary-update-mark
6187    (cond ((memq article gnus-newsgroup-processable)
6188           gnus-process-mark)
6189          ((memq article gnus-newsgroup-cached)
6190           gnus-cached-mark)
6191          ((memq article gnus-newsgroup-replied)
6192           gnus-replied-mark)
6193          ((memq article gnus-newsgroup-saved)
6194           gnus-saved-mark)
6195          (t gnus-unread-mark))
6196    'replied)
6197   (when (gnus-visual-p 'summary-highlight 'highlight)
6198     (run-hooks 'gnus-summary-update-hook))
6199   t)
6200
6201 (defun gnus-summary-update-mark (mark type)
6202   (beginning-of-line)
6203   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
6204         (buffer-read-only nil))
6205     (when (and forward
6206                (<= (+ forward (point)) (point-max)))
6207       ;; Go to the right position on the line.
6208       (goto-char (+ forward (point)))
6209       ;; Replace the old mark with the new mark.
6210       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
6211       ;; Optionally update the marks by some user rule.
6212       (when (eq type 'unread)
6213         (gnus-data-set-mark
6214          (gnus-data-find (gnus-summary-article-number)) mark)
6215         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
6216
6217 (defun gnus-mark-article-as-read (article &optional mark)
6218   "Enter ARTICLE in the pertinent lists and remove it from others."
6219   ;; Make the article expirable.
6220   (let ((mark (or mark gnus-del-mark)))
6221     (if (= mark gnus-expirable-mark)
6222         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
6223       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
6224     ;; Remove from unread and marked lists.
6225     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6226     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6227     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6228     (push (cons article mark) gnus-newsgroup-reads)
6229     ;; Possibly remove from cache, if that is used.
6230     (when gnus-use-cache
6231       (gnus-cache-enter-remove-article article))))
6232
6233 (defun gnus-mark-article-as-unread (article &optional mark)
6234   "Enter ARTICLE in the pertinent lists and remove it from others."
6235   (let ((mark (or mark gnus-ticked-mark)))
6236     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6237     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6238     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
6239     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6240     (cond ((= mark gnus-ticked-mark)
6241            (push article gnus-newsgroup-marked))
6242           ((= mark gnus-dormant-mark)
6243            (push article gnus-newsgroup-dormant))
6244           (t
6245            (push article gnus-newsgroup-unreads)))
6246     (setq gnus-newsgroup-reads
6247           (delq (assq article gnus-newsgroup-reads)
6248                 gnus-newsgroup-reads))))
6249
6250 (defalias 'gnus-summary-mark-as-unread-forward
6251   'gnus-summary-tick-article-forward)
6252 (make-obsolete 'gnus-summary-mark-as-unread-forward
6253                'gnus-summary-tick-article-forward)
6254 (defun gnus-summary-tick-article-forward (n)
6255   "Tick N articles forwards.
6256 If N is negative, tick backwards instead.
6257 The difference between N and the number of articles ticked is returned."
6258   (interactive "p")
6259   (gnus-summary-mark-forward n gnus-ticked-mark))
6260
6261 (defalias 'gnus-summary-mark-as-unread-backward
6262   'gnus-summary-tick-article-backward)
6263 (make-obsolete 'gnus-summary-mark-as-unread-backward
6264                'gnus-summary-tick-article-backward)
6265 (defun gnus-summary-tick-article-backward (n)
6266   "Tick N articles backwards.
6267 The difference between N and the number of articles ticked is returned."
6268   (interactive "p")
6269   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
6270
6271 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6272 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6273 (defun gnus-summary-tick-article (&optional article clear-mark)
6274   "Mark current article as unread.
6275 Optional 1st argument ARTICLE specifies article number to be marked as unread.
6276 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
6277   (interactive)
6278   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
6279                                        gnus-ticked-mark)))
6280
6281 (defun gnus-summary-mark-as-read-forward (n)
6282   "Mark N articles as read forwards.
6283 If N is negative, mark backwards instead.
6284 The difference between N and the actual number of articles marked is
6285 returned."
6286   (interactive "p")
6287   (gnus-summary-mark-forward n gnus-del-mark t))
6288
6289 (defun gnus-summary-mark-as-read-backward (n)
6290   "Mark the N articles as read backwards.
6291 The difference between N and the actual number of articles marked is
6292 returned."
6293   (interactive "p")
6294   (gnus-summary-mark-forward (- n) gnus-del-mark t))
6295
6296 (defun gnus-summary-mark-as-read (&optional article mark)
6297   "Mark current article as read.
6298 ARTICLE specifies the article to be marked as read.
6299 MARK specifies a string to be inserted at the beginning of the line."
6300   (gnus-summary-mark-article article mark))
6301
6302 (defun gnus-summary-clear-mark-forward (n)
6303   "Clear marks from N articles forward.
6304 If N is negative, clear backward instead.
6305 The difference between N and the number of marks cleared is returned."
6306   (interactive "p")
6307   (gnus-summary-mark-forward n gnus-unread-mark))
6308
6309 (defun gnus-summary-clear-mark-backward (n)
6310   "Clear marks from N articles backward.
6311 The difference between N and the number of marks cleared is returned."
6312   (interactive "p")
6313   (gnus-summary-mark-forward (- n) gnus-unread-mark))
6314
6315 (defun gnus-summary-mark-unread-as-read ()
6316   "Intended to be used by `gnus-summary-mark-article-hook'."
6317   (when (memq gnus-current-article gnus-newsgroup-unreads)
6318     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
6319
6320 (defun gnus-summary-mark-read-and-unread-as-read ()
6321   "Intended to be used by `gnus-summary-mark-article-hook'."
6322   (let ((mark (gnus-summary-article-mark)))
6323     (when (or (gnus-unread-mark-p mark)
6324               (gnus-read-mark-p mark))
6325       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
6326
6327 (defun gnus-summary-mark-region-as-read (point mark all)
6328   "Mark all unread articles between point and mark as read.
6329 If given a prefix, mark all articles between point and mark as read,
6330 even ticked and dormant ones."
6331   (interactive "r\nP")
6332   (save-excursion
6333     (let (article)
6334       (goto-char point)
6335       (beginning-of-line)
6336       (while (and
6337               (< (point) mark)
6338               (progn
6339                 (when (or all
6340                           (memq (setq article (gnus-summary-article-number))
6341                                 gnus-newsgroup-unreads))
6342                   (gnus-summary-mark-article article gnus-del-mark))
6343                 t)
6344               (gnus-summary-find-next))))))
6345
6346 (defun gnus-summary-mark-below (score mark)
6347   "Mark articles with score less than SCORE with MARK."
6348   (interactive "P\ncMark: ")
6349   (gnus-set-global-variables)
6350   (setq score (if score
6351                   (prefix-numeric-value score)
6352                 (or gnus-summary-default-score 0)))
6353   (save-excursion
6354     (set-buffer gnus-summary-buffer)
6355     (goto-char (point-min))
6356     (while 
6357         (progn
6358           (and (< (gnus-summary-article-score) score)
6359                (gnus-summary-mark-article nil mark))
6360           (gnus-summary-find-next)))))
6361
6362 (defun gnus-summary-kill-below (&optional score)
6363   "Mark articles with score below SCORE as read."
6364   (interactive "P")
6365   (gnus-set-global-variables)
6366   (gnus-summary-mark-below score gnus-killed-mark))
6367
6368 (defun gnus-summary-clear-above (&optional score)
6369   "Clear all marks from articles with score above SCORE."
6370   (interactive "P")
6371   (gnus-set-global-variables)
6372   (gnus-summary-mark-above score gnus-unread-mark))
6373
6374 (defun gnus-summary-tick-above (&optional score)
6375   "Tick all articles with score above SCORE."
6376   (interactive "P")
6377   (gnus-set-global-variables)
6378   (gnus-summary-mark-above score gnus-ticked-mark))
6379
6380 (defun gnus-summary-mark-above (score mark)
6381   "Mark articles with score over SCORE with MARK."
6382   (interactive "P\ncMark: ")
6383   (gnus-set-global-variables)
6384   (setq score (if score
6385                   (prefix-numeric-value score)
6386                 (or gnus-summary-default-score 0)))
6387   (save-excursion
6388     (set-buffer gnus-summary-buffer)
6389     (goto-char (point-min))
6390     (while (and (progn
6391                   (if (> (gnus-summary-article-score) score)
6392                       (gnus-summary-mark-article nil mark))
6393                   t)
6394                 (gnus-summary-find-next)))))
6395
6396 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6397 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
6398 (defun gnus-summary-limit-include-expunged (&optional no-error)
6399   "Display all the hidden articles that were expunged for low scores."
6400   (interactive)
6401   (gnus-set-global-variables)
6402   (let ((buffer-read-only nil))
6403     (let ((scored gnus-newsgroup-scored)
6404           headers h)
6405       (while scored
6406         (or (gnus-summary-goto-subject (caar scored))
6407             (and (setq h (gnus-summary-article-header (caar scored)))
6408                  (< (cdar scored) gnus-summary-expunge-below)
6409                  (setq headers (cons h headers))))
6410         (setq scored (cdr scored)))
6411       (if (not headers)
6412           (when (not no-error)
6413             (error "No expunged articles hidden."))
6414         (goto-char (point-min))
6415         (gnus-summary-prepare-unthreaded (nreverse headers))
6416         (goto-char (point-min))
6417         (gnus-summary-position-point)
6418         t))))
6419
6420 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
6421   "Mark all articles not marked as unread in this newsgroup as read.
6422 If prefix argument ALL is non-nil, all articles are marked as read.
6423 If QUIETLY is non-nil, no questions will be asked.
6424 If TO-HERE is non-nil, it should be a point in the buffer.  All
6425 articles before this point will be marked as read.
6426 The number of articles marked as read is returned."
6427   (interactive "P")
6428   (gnus-set-global-variables)
6429   (prog1
6430       (if (or quietly
6431               (not gnus-interactive-catchup) ;Without confirmation?
6432               gnus-expert-user
6433               (gnus-y-or-n-p
6434                (if all
6435                    "Mark absolutely all articles as read? "
6436                  "Mark all unread articles as read? ")))
6437           (if (and not-mark
6438                    (not gnus-newsgroup-adaptive)
6439                    (not gnus-newsgroup-auto-expire))
6440               (progn
6441                 (when all
6442                   (setq gnus-newsgroup-marked nil
6443                         gnus-newsgroup-dormant nil))
6444                 (setq gnus-newsgroup-unreads nil))
6445             ;; We actually mark all articles as canceled, which we
6446             ;; have to do when using auto-expiry or adaptive scoring.
6447             (gnus-summary-show-all-threads)
6448             (if (gnus-summary-first-subject (not all))
6449                 (while (and
6450                         (if to-here (< (point) to-here) t)
6451                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
6452                         (gnus-summary-find-next (not all)))))
6453             (unless to-here
6454               (setq gnus-newsgroup-unreads nil))
6455             (gnus-set-mode-line 'summary)))
6456     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
6457       (if (and (not to-here) (eq 'nnvirtual (car method)))
6458           (nnvirtual-catchup-group
6459            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
6460     (gnus-summary-position-point)))
6461
6462 (defun gnus-summary-catchup-to-here (&optional all)
6463   "Mark all unticked articles before the current one as read.
6464 If ALL is non-nil, also mark ticked and dormant articles as read."
6465   (interactive "P")
6466   (gnus-set-global-variables)
6467   (save-excursion
6468     (gnus-save-hidden-threads
6469       (let ((beg (point)))
6470         ;; We check that there are unread articles.
6471         (when (or all (gnus-summary-find-prev))
6472           (gnus-summary-catchup all t beg)))))
6473   (gnus-summary-position-point))
6474
6475 (defun gnus-summary-catchup-all (&optional quietly)
6476   "Mark all articles in this newsgroup as read."
6477   (interactive "P")
6478   (gnus-set-global-variables)
6479   (gnus-summary-catchup t quietly))
6480
6481 (defun gnus-summary-catchup-and-exit (&optional all quietly)
6482   "Mark all articles not marked as unread in this newsgroup as read, then exit.
6483 If prefix argument ALL is non-nil, all articles are marked as read."
6484   (interactive "P")
6485   (gnus-set-global-variables)
6486   (gnus-summary-catchup all quietly nil 'fast)
6487   ;; Select next newsgroup or exit.
6488   (if (eq gnus-auto-select-next 'quietly)
6489       (gnus-summary-next-group nil)
6490     (gnus-summary-exit)))
6491
6492 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
6493   "Mark all articles in this newsgroup as read, and then exit."
6494   (interactive "P")
6495   (gnus-set-global-variables)
6496   (gnus-summary-catchup-and-exit t quietly))
6497
6498 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
6499 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
6500   "Mark all articles in this group as read and select the next group.
6501 If given a prefix, mark all articles, unread as well as ticked, as
6502 read."
6503   (interactive "P")
6504   (gnus-set-global-variables)
6505   (save-excursion
6506     (gnus-summary-catchup all))
6507   (gnus-summary-next-article t nil nil t))
6508
6509 ;; Thread-based commands.
6510
6511 (defun gnus-summary-articles-in-thread (&optional article)
6512   "Return a list of all articles in the current thread.
6513 If ARTICLE is non-nil, return all articles in the thread that starts
6514 with that article."
6515   (let* ((article (or article (gnus-summary-article-number)))
6516          (data (gnus-data-find-list article))
6517          (top-level (gnus-data-level (car data)))
6518          (top-subject
6519           (cond ((null gnus-thread-operation-ignore-subject)
6520                  (gnus-simplify-subject-re
6521                   (mail-header-subject (gnus-data-header (car data)))))
6522                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
6523                  (gnus-simplify-subject-fuzzy
6524                   (mail-header-subject (gnus-data-header (car data)))))
6525                 (t nil)))
6526          (end-point (save-excursion
6527                       (if (gnus-summary-go-to-next-thread) 
6528                           (point) (point-max))))
6529          articles)
6530     (while (and data
6531                 (< (gnus-data-pos (car data)) end-point))
6532       (when (or (not top-subject)
6533                 (string= top-subject
6534                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
6535                              (gnus-simplify-subject-fuzzy
6536                               (mail-header-subject
6537                                (gnus-data-header (car data))))
6538                            (gnus-simplify-subject-re
6539                             (mail-header-subject
6540                              (gnus-data-header (car data)))))))
6541         (push (gnus-data-number (car data)) articles))
6542       (unless (and (setq data (cdr data))
6543                    (> (gnus-data-level (car data)) top-level))
6544         (setq data nil)))
6545     ;; Return the list of articles.
6546     (nreverse articles)))
6547
6548 (defun gnus-summary-rethread-current ()
6549   "Rethread the thread the current article is part of."
6550   (interactive)
6551   (gnus-set-global-variables)
6552   (let* ((gnus-show-threads t)
6553          (article (gnus-summary-article-number))
6554          (id (mail-header-id (gnus-summary-article-header)))
6555          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
6556     (unless id
6557       (error "No article on the current line"))
6558     (gnus-rebuild-thread id)
6559     (gnus-summary-goto-subject article)))
6560
6561 (defun gnus-summary-reparent-thread ()
6562   "Make current article child of the marked (or previous) article.
6563
6564 Note that the re-threading will only work if `gnus-thread-ignore-subject'
6565 is non-nil or the Subject: of both articles are the same."
6566   (interactive)
6567   (or (not (gnus-group-read-only-p))
6568       (error "The current newsgroup does not support article editing."))
6569   (or (<= (length gnus-newsgroup-processable) 1)
6570       (error "No more than one article may be marked."))
6571   (save-window-excursion
6572     (let ((gnus-article-buffer " *reparent*")
6573           (current-article (gnus-summary-article-number))
6574           ; first grab the marked article, otherwise one line up.
6575           (parent-article (if (not (null gnus-newsgroup-processable))
6576                               (car gnus-newsgroup-processable)
6577                             (save-excursion
6578                               (if (eq (forward-line -1) 0)
6579                                   (gnus-summary-article-number)
6580                                 (error "Beginning of summary buffer."))))))
6581       (or (not (eq current-article parent-article))
6582           (error "An article may not be self-referential."))
6583       (let ((message-id (mail-header-id 
6584                          (gnus-summary-article-header parent-article))))
6585         (or (and message-id (not (equal message-id "")))
6586             (error "No message-id in desired parent."))
6587         (gnus-summary-select-article t t nil current-article)
6588         (set-buffer gnus-article-buffer)
6589         (setq buffer-read-only nil)
6590         (let ((buf (format "%s" (buffer-string))))
6591           (erase-buffer)
6592           (insert buf))
6593         (goto-char (point-min))
6594         (if (search-forward-regexp "^References: " nil t)
6595             (insert message-id " " )
6596           (insert "References: " message-id "\n"))
6597         (or (gnus-request-replace-article current-article
6598                                           (car gnus-article-current)
6599                                           gnus-article-buffer)
6600             (error "Couldn't replace article."))
6601         (set-buffer gnus-summary-buffer)
6602         (gnus-summary-unmark-all-processable)
6603         (gnus-summary-rethread-current)
6604         (gnus-message 3 "Article %d is now the child of article %d."
6605                       current-article parent-article)))))
6606
6607 (defun gnus-summary-toggle-threads (&optional arg)
6608   "Toggle showing conversation threads.
6609 If ARG is positive number, turn showing conversation threads on."
6610   (interactive "P")
6611   (gnus-set-global-variables)
6612   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
6613     (setq gnus-show-threads
6614           (if (null arg) (not gnus-show-threads)
6615             (> (prefix-numeric-value arg) 0)))
6616     (gnus-summary-prepare)
6617     (gnus-summary-goto-subject current)
6618     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
6619     (gnus-summary-position-point)))
6620
6621 (defun gnus-summary-show-all-threads ()
6622   "Show all threads."
6623   (interactive)
6624   (gnus-set-global-variables)
6625   (save-excursion
6626     (let ((buffer-read-only nil))
6627       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
6628   (gnus-summary-position-point))
6629
6630 (defun gnus-summary-show-thread ()
6631   "Show thread subtrees.
6632 Returns nil if no thread was there to be shown."
6633   (interactive)
6634   (gnus-set-global-variables)
6635   (let ((buffer-read-only nil)
6636         (orig (point))
6637         ;; first goto end then to beg, to have point at beg after let
6638         (end (progn (end-of-line) (point)))
6639         (beg (progn (beginning-of-line) (point))))
6640     (prog1
6641         ;; Any hidden lines here?
6642         (search-forward "\r" end t)
6643       (subst-char-in-region beg end ?\^M ?\n t)
6644       (goto-char orig)
6645       (gnus-summary-position-point))))
6646
6647 (defun gnus-summary-hide-all-threads ()
6648   "Hide all thread subtrees."
6649   (interactive)
6650   (gnus-set-global-variables)
6651   (save-excursion
6652     (goto-char (point-min))
6653     (gnus-summary-hide-thread)
6654     (while (zerop (gnus-summary-next-thread 1 t))
6655       (gnus-summary-hide-thread)))
6656   (gnus-summary-position-point))
6657
6658 (defun gnus-summary-hide-thread ()
6659   "Hide thread subtrees.
6660 Returns nil if no threads were there to be hidden."
6661   (interactive)
6662   (gnus-set-global-variables)
6663   (let ((buffer-read-only nil)
6664         (start (point))
6665         (article (gnus-summary-article-number)))
6666     (goto-char start)
6667     ;; Go forward until either the buffer ends or the subthread
6668     ;; ends.
6669     (when (and (not (eobp))
6670                (or (zerop (gnus-summary-next-thread 1 t))
6671                    (goto-char (point-max))))
6672       (prog1
6673           (if (and (> (point) start)
6674                    (search-backward "\n" start t))
6675               (progn
6676                 (subst-char-in-region start (point) ?\n ?\^M)
6677                 (gnus-summary-goto-subject article))
6678             (goto-char start)
6679             nil)
6680         ;;(gnus-summary-position-point)
6681         ))))
6682
6683 (defun gnus-summary-go-to-next-thread (&optional previous)
6684   "Go to the same level (or less) next thread.
6685 If PREVIOUS is non-nil, go to previous thread instead.
6686 Return the article number moved to, or nil if moving was impossible."
6687   (let ((level (gnus-summary-thread-level))
6688         (way (if previous -1 1))
6689         (beg (point)))
6690     (forward-line way)
6691     (while (and (not (eobp))
6692                 (< level (gnus-summary-thread-level)))
6693       (forward-line way))
6694     (if (eobp)
6695         (progn
6696           (goto-char beg)
6697           nil)
6698       (setq beg (point))
6699       (prog1
6700           (gnus-summary-article-number)
6701         (goto-char beg)))))
6702
6703 (defun gnus-summary-go-to-next-thread-old (&optional previous)
6704   "Go to the same level (or less) next thread.
6705 If PREVIOUS is non-nil, go to previous thread instead.
6706 Return the article number moved to, or nil if moving was impossible."
6707   (if (and (eq gnus-summary-make-false-root 'dummy)
6708            (gnus-summary-article-intangible-p))
6709       (let ((beg (point)))
6710         (while (and (zerop (forward-line 1))
6711                     (not (gnus-summary-article-intangible-p))
6712                     (not (zerop (save-excursion 
6713                                   (gnus-summary-thread-level))))))
6714         (if (eobp)
6715             (progn
6716               (goto-char beg)
6717               nil)
6718           (point)))
6719     (let* ((level (gnus-summary-thread-level))
6720            (article (gnus-summary-article-number))
6721            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
6722            oart)
6723       (while data
6724         (if (<= (gnus-data-level (car data)) level)
6725             (setq oart (gnus-data-number (car data))
6726                   data nil)
6727           (setq data (cdr data))))
6728       (and oart
6729            (gnus-summary-goto-subject oart)))))
6730
6731 (defun gnus-summary-next-thread (n &optional silent)
6732   "Go to the same level next N'th thread.
6733 If N is negative, search backward instead.
6734 Returns the difference between N and the number of skips actually
6735 done.
6736
6737 If SILENT, don't output messages."
6738   (interactive "p")
6739   (gnus-set-global-variables)
6740   (let ((backward (< n 0))
6741         (n (abs n))
6742         old dum int)
6743     (while (and (> n 0)
6744                 (gnus-summary-go-to-next-thread backward))
6745       (decf n))
6746     (unless silent 
6747       (gnus-summary-position-point))
6748     (when (and (not silent) (/= 0 n))
6749       (gnus-message 7 "No more threads"))
6750     n))
6751
6752 (defun gnus-summary-prev-thread (n)
6753   "Go to the same level previous N'th thread.
6754 Returns the difference between N and the number of skips actually
6755 done."
6756   (interactive "p")
6757   (gnus-set-global-variables)
6758   (gnus-summary-next-thread (- n)))
6759
6760 (defun gnus-summary-go-down-thread ()
6761   "Go down one level in the current thread."
6762   (let ((children (gnus-summary-article-children)))
6763     (and children
6764          (gnus-summary-goto-subject (car children)))))
6765
6766 (defun gnus-summary-go-up-thread ()
6767   "Go up one level in the current thread."
6768   (let ((parent (gnus-summary-article-parent)))
6769     (and parent
6770          (gnus-summary-goto-subject parent))))
6771
6772 (defun gnus-summary-down-thread (n)
6773   "Go down thread N steps.
6774 If N is negative, go up instead.
6775 Returns the difference between N and how many steps down that were
6776 taken."
6777   (interactive "p")
6778   (gnus-set-global-variables)
6779   (let ((up (< n 0))
6780         (n (abs n)))
6781     (while (and (> n 0)
6782                 (if up (gnus-summary-go-up-thread)
6783                   (gnus-summary-go-down-thread)))
6784       (setq n (1- n)))
6785     (gnus-summary-position-point)
6786     (if (/= 0 n) (gnus-message 7 "Can't go further"))
6787     n))
6788
6789 (defun gnus-summary-up-thread (n)
6790   "Go up thread N steps.
6791 If N is negative, go up instead.
6792 Returns the difference between N and how many steps down that were
6793 taken."
6794   (interactive "p")
6795   (gnus-set-global-variables)
6796   (gnus-summary-down-thread (- n)))
6797
6798 (defun gnus-summary-top-thread ()
6799   "Go to the top of the thread."
6800   (interactive)
6801   (gnus-set-global-variables)
6802   (while (gnus-summary-go-up-thread))
6803   (gnus-summary-article-number))
6804
6805 (defun gnus-summary-kill-thread (&optional unmark)
6806   "Mark articles under current thread as read.
6807 If the prefix argument is positive, remove any kinds of marks.
6808 If the prefix argument is negative, tick articles instead."
6809   (interactive "P")
6810   (gnus-set-global-variables)
6811   (when unmark
6812     (setq unmark (prefix-numeric-value unmark)))
6813   (let ((articles (gnus-summary-articles-in-thread)))
6814     (save-excursion
6815       ;; Expand the thread.
6816       (gnus-summary-show-thread)
6817       ;; Mark all the articles.
6818       (while articles
6819         (gnus-summary-goto-subject (car articles))
6820         (cond ((null unmark)
6821                (gnus-summary-mark-article-as-read gnus-killed-mark))
6822               ((> unmark 0)
6823                (gnus-summary-mark-article-as-unread gnus-unread-mark))
6824               (t
6825                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
6826         (setq articles (cdr articles))))
6827     ;; Hide killed subtrees.
6828     (and (null unmark)
6829          gnus-thread-hide-killed
6830          (gnus-summary-hide-thread))
6831     ;; If marked as read, go to next unread subject.
6832     (if (null unmark)
6833         ;; Go to next unread subject.
6834         (gnus-summary-next-subject 1 t)))
6835   (gnus-set-mode-line 'summary))
6836
6837 ;; Summary sorting commands
6838
6839 (defun gnus-summary-sort-by-number (&optional reverse)
6840   "Sort summary buffer by article number.
6841 Argument REVERSE means reverse order."
6842   (interactive "P")
6843   (gnus-summary-sort 'number reverse))
6844
6845 (defun gnus-summary-sort-by-author (&optional reverse)
6846   "Sort summary buffer by author name alphabetically.
6847 If case-fold-search is non-nil, case of letters is ignored.
6848 Argument REVERSE means reverse order."
6849   (interactive "P")
6850   (gnus-summary-sort 'author reverse))
6851
6852 (defun gnus-summary-sort-by-subject (&optional reverse)
6853   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
6854 If case-fold-search is non-nil, case of letters is ignored.
6855 Argument REVERSE means reverse order."
6856   (interactive "P")
6857   (gnus-summary-sort 'subject reverse))
6858
6859 (defun gnus-summary-sort-by-date (&optional reverse)
6860   "Sort summary buffer by date.
6861 Argument REVERSE means reverse order."
6862   (interactive "P")
6863   (gnus-summary-sort 'date reverse))
6864
6865 (defun gnus-summary-sort-by-score (&optional reverse)
6866   "Sort summary buffer by score.
6867 Argument REVERSE means reverse order."
6868   (interactive "P")
6869   (gnus-summary-sort 'score reverse))
6870
6871 (defun gnus-summary-sort (predicate reverse)
6872   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
6873   (gnus-set-global-variables)
6874   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
6875          (article (intern (format "gnus-article-sort-by-%s" predicate)))
6876          (gnus-thread-sort-functions
6877           (list
6878            (if (not reverse)
6879                thread
6880              `(lambda (t1 t2)
6881                 (,thread t2 t1)))))
6882          (gnus-article-sort-functions
6883           (list
6884            (if (not reverse)
6885                article
6886              `(lambda (t1 t2)
6887                 (,article t2 t1)))))
6888          (buffer-read-only)
6889          (gnus-summary-prepare-hook nil))
6890     ;; We do the sorting by regenerating the threads.
6891     (gnus-summary-prepare)
6892     ;; Hide subthreads if needed.
6893     (when (and gnus-show-threads gnus-thread-hide-subtree)
6894       (gnus-summary-hide-all-threads))))
6895
6896 ;; Summary saving commands.
6897
6898 (defun gnus-summary-save-article (&optional n not-saved)
6899   "Save the current article using the default saver function.
6900 If N is a positive number, save the N next articles.
6901 If N is a negative number, save the N previous articles.
6902 If N is nil and any articles have been marked with the process mark,
6903 save those articles instead.
6904 The variable `gnus-default-article-saver' specifies the saver function."
6905   (interactive "P")
6906   (gnus-set-global-variables)
6907   (let ((articles (gnus-summary-work-articles n))
6908         (save-buffer (save-excursion 
6909                        (nnheader-set-temp-buffer " *Gnus Save*")))
6910         header article file)
6911     (while articles
6912       (setq header (gnus-summary-article-header
6913                     (setq article (pop articles))))
6914       (if (not (vectorp header))
6915           ;; This is a pseudo-article.
6916           (if (assq 'name header)
6917               (gnus-copy-file (cdr (assq 'name header)))
6918             (gnus-message 1 "Article %d is unsaveable" article))
6919         ;; This is a real article.
6920         (save-window-excursion
6921           (gnus-summary-select-article t nil nil article))
6922         (save-excursion
6923           (set-buffer save-buffer)
6924           (erase-buffer)
6925           (insert-buffer-substring gnus-original-article-buffer))
6926         (setq file (gnus-article-save save-buffer file))
6927         (gnus-summary-remove-process-mark article)
6928         (unless not-saved
6929           (gnus-summary-set-saved-mark article))))
6930     (gnus-kill-buffer save-buffer)
6931     (gnus-summary-position-point)
6932     n))
6933
6934 (defun gnus-summary-pipe-output (&optional arg)
6935   "Pipe the current article to a subprocess.
6936 If N is a positive number, pipe the N next articles.
6937 If N is a negative number, pipe the N previous articles.
6938 If N is nil and any articles have been marked with the process mark,
6939 pipe those articles instead."
6940   (interactive "P")
6941   (gnus-set-global-variables)
6942   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
6943     (gnus-summary-save-article arg t))
6944   (gnus-configure-windows 'pipe))
6945
6946 (defun gnus-summary-save-article-mail (&optional arg)
6947   "Append the current article to an mail file.
6948 If N is a positive number, save the N next articles.
6949 If N is a negative number, save the N previous articles.
6950 If N is nil and any articles have been marked with the process mark,
6951 save those articles instead."
6952   (interactive "P")
6953   (gnus-set-global-variables)
6954   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
6955     (gnus-summary-save-article arg)))
6956
6957 (defun gnus-summary-save-article-rmail (&optional arg)
6958   "Append the current article to an rmail file.
6959 If N is a positive number, save the N next articles.
6960 If N is a negative number, save the N previous articles.
6961 If N is nil and any articles have been marked with the process mark,
6962 save those articles instead."
6963   (interactive "P")
6964   (gnus-set-global-variables)
6965   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
6966     (gnus-summary-save-article arg)))
6967
6968 (defun gnus-summary-save-article-file (&optional arg)
6969   "Append the current article to a file.
6970 If N is a positive number, save the N next articles.
6971 If N is a negative number, save the N previous articles.
6972 If N is nil and any articles have been marked with the process mark,
6973 save those articles instead."
6974   (interactive "P")
6975   (gnus-set-global-variables)
6976   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
6977     (gnus-summary-save-article arg)))
6978
6979 (defun gnus-summary-save-article-body-file (&optional arg)
6980   "Append the current article body to a file.
6981 If N is a positive number, save the N next articles.
6982 If N is a negative number, save the N previous articles.
6983 If N is nil and any articles have been marked with the process mark,
6984 save those articles instead."
6985   (interactive "P")
6986   (gnus-set-global-variables)
6987   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
6988     (gnus-summary-save-article arg)))
6989
6990 (defun gnus-get-split-value (methods)
6991   "Return a value based on the split METHODS."
6992   (let (split-name method result match)
6993     (when methods
6994       (save-excursion
6995         (set-buffer gnus-original-article-buffer)
6996         (save-restriction
6997           (nnheader-narrow-to-headers)
6998           (while methods
6999             (goto-char (point-min))
7000             (setq method (pop methods))
7001             (setq match (car method))
7002             (when (cond
7003                    ((stringp match)
7004                     ;; Regular expression.
7005                     (condition-case ()
7006                         (re-search-forward match nil t)
7007                       (error nil)))
7008                    ((gnus-functionp match)
7009                     ;; Function.
7010                     (save-restriction
7011                       (widen)
7012                       (setq result (funcall match gnus-newsgroup-name))))
7013                    ((consp match)
7014                     ;; Form.
7015                     (save-restriction
7016                       (widen)
7017                       (setq result (eval match)))))
7018               (setq split-name (append (cdr method) split-name))
7019               (cond ((stringp result)
7020                      (push result split-name))
7021                     ((consp result)
7022                      (setq split-name (append result split-name)))))))))
7023     split-name))
7024
7025 (defun gnus-read-move-group-name (prompt default articles prefix)
7026   "Read a group name."
7027   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
7028          (minibuffer-confirm-incomplete nil) ; XEmacs
7029          group-map
7030          (dum (mapatoms
7031                (lambda (g) 
7032                  (and (boundp g)
7033                       (symbol-name g)
7034                       (memq 'respool
7035                             (assoc (symbol-name
7036                                     (car (gnus-find-method-for-group
7037                                           (symbol-name g))))
7038                                    gnus-valid-select-methods))
7039                       (push (list (symbol-name g)) group-map)))
7040                gnus-active-hashtb))
7041          (prom
7042           (format "%s %s to:"
7043                   prompt
7044                   (if (> (length articles) 1)
7045                       (format "these %d articles" (length articles))
7046                     "this article")))
7047          (to-newsgroup
7048           (cond
7049            ((null split-name)
7050             (gnus-completing-read default prom
7051                                   group-map nil nil prefix
7052                                   'gnus-group-history))
7053            ((= 1 (length split-name))
7054             (gnus-completing-read (car split-name) prom group-map
7055                                   nil nil nil
7056                                   'gnus-group-history))
7057            (t
7058             (gnus-completing-read nil prom 
7059                                   (mapcar (lambda (el) (list el))
7060                                           (nreverse split-name))
7061                                   nil nil nil
7062                                   'gnus-group-history)))))
7063     (when to-newsgroup
7064       (if (or (string= to-newsgroup "")
7065               (string= to-newsgroup prefix))
7066           (setq to-newsgroup (or default "")))
7067       (or (gnus-active to-newsgroup)
7068           (gnus-activate-group to-newsgroup)
7069           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
7070                                      to-newsgroup))
7071               (or (and (gnus-request-create-group 
7072                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
7073                        (gnus-activate-group to-newsgroup nil nil
7074                                             (gnus-group-name-to-method
7075                                              to-newsgroup)))
7076                   (error "Couldn't create group %s" to-newsgroup)))
7077           (error "No such group: %s" to-newsgroup)))
7078     to-newsgroup))
7079
7080 ;; Summary extract commands
7081
7082 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
7083   (let ((buffer-read-only nil)
7084         (article (gnus-summary-article-number))
7085         after-article b e)
7086     (or (gnus-summary-goto-subject article)
7087         (error (format "No such article: %d" article)))
7088     (gnus-summary-position-point)
7089     ;; If all commands are to be bunched up on one line, we collect
7090     ;; them here.
7091     (if gnus-view-pseudos-separately
7092         ()
7093       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
7094             files action)
7095         (while ps
7096           (setq action (cdr (assq 'action (car ps))))
7097           (setq files (list (cdr (assq 'name (car ps)))))
7098           (while (and ps (cdr ps)
7099                       (string= (or action "1")
7100                                (or (cdr (assq 'action (cadr ps))) "2")))
7101             (setq files (cons (cdr (assq 'name (cadr ps))) files))
7102             (setcdr ps (cddr ps)))
7103           (if (not files)
7104               ()
7105             (if (not (string-match "%s" action))
7106                 (setq files (cons " " files)))
7107             (setq files (cons " " files))
7108             (and (assq 'execute (car ps))
7109                  (setcdr (assq 'execute (car ps))
7110                          (funcall (if (string-match "%s" action)
7111                                       'format 'concat)
7112                                   action
7113                                   (mapconcat (lambda (f) f) files " ")))))
7114           (setq ps (cdr ps)))))
7115     (if (and gnus-view-pseudos (not not-view))
7116         (while pslist
7117           (and (assq 'execute (car pslist))
7118                (gnus-execute-command (cdr (assq 'execute (car pslist)))
7119                                      (eq gnus-view-pseudos 'not-confirm)))
7120           (setq pslist (cdr pslist)))
7121       (save-excursion
7122         (while pslist
7123           (setq after-article (or (cdr (assq 'article (car pslist)))
7124                                   (gnus-summary-article-number)))
7125           (gnus-summary-goto-subject after-article)
7126           (forward-line 1)
7127           (setq b (point))
7128           (insert "    " (file-name-nondirectory
7129                                 (cdr (assq 'name (car pslist))))
7130                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
7131           (setq e (point))
7132           (forward-line -1)             ; back to `b'
7133           (gnus-add-text-properties
7134            b (1- e) (list 'gnus-number gnus-reffed-article-number
7135                           gnus-mouse-face-prop gnus-mouse-face))
7136           (gnus-data-enter
7137            after-article gnus-reffed-article-number
7138            gnus-unread-mark b (car pslist) 0 (- e b))
7139           (push gnus-reffed-article-number gnus-newsgroup-unreads)
7140           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
7141           (setq pslist (cdr pslist)))))))
7142
7143 (defun gnus-pseudos< (p1 p2)
7144   (let ((c1 (cdr (assq 'action p1)))
7145         (c2 (cdr (assq 'action p2))))
7146     (and c1 c2 (string< c1 c2))))
7147
7148 (defun gnus-request-pseudo-article (props)
7149   (cond ((assq 'execute props)
7150          (gnus-execute-command (cdr (assq 'execute props)))))
7151   (let ((gnus-current-article (gnus-summary-article-number)))
7152     (run-hooks 'gnus-mark-article-hook)))
7153
7154 (defun gnus-execute-command (command &optional automatic)
7155   (save-excursion
7156     (gnus-article-setup-buffer)
7157     (set-buffer gnus-article-buffer)
7158     (setq buffer-read-only nil)
7159     (let ((command (if automatic command (read-string "Command: " command)))
7160           ;; Just binding this here doesn't help, because there might
7161           ;; be output from the process after exiting the scope of 
7162           ;; this `let'.
7163           ;; (buffer-read-only nil)
7164           )
7165       (erase-buffer)
7166       (insert "$ " command "\n\n")
7167       (if gnus-view-pseudo-asynchronously
7168           (start-process "gnus-execute" nil shell-file-name
7169                          shell-command-switch command)
7170         (call-process shell-file-name nil t nil
7171                       shell-command-switch command)))))
7172
7173 ;; Summary kill commands.
7174
7175 (defun gnus-summary-edit-global-kill (article)
7176   "Edit the \"global\" kill file."
7177   (interactive (list (gnus-summary-article-number)))
7178   (gnus-set-global-variables)
7179   (gnus-group-edit-global-kill article))
7180
7181 (defun gnus-summary-edit-local-kill ()
7182   "Edit a local kill file applied to the current newsgroup."
7183   (interactive)
7184   (gnus-set-global-variables)
7185   (setq gnus-current-headers (gnus-summary-article-header))
7186   (gnus-set-global-variables)
7187   (gnus-group-edit-local-kill
7188    (gnus-summary-article-number) gnus-newsgroup-name))
7189
7190 ;;; Header reading.
7191
7192 (defun gnus-read-header (id &optional header)
7193   "Read the headers of article ID and enter them into the Gnus system."
7194   (let ((group gnus-newsgroup-name)
7195         (gnus-override-method 
7196          (and (gnus-news-group-p gnus-newsgroup-name)
7197               gnus-refer-article-method))       
7198         where)
7199     ;; First we check to see whether the header in question is already
7200     ;; fetched.
7201     (if (stringp id)
7202         ;; This is a Message-ID.
7203         (setq header (or header (gnus-id-to-header id)))
7204       ;; This is an article number.
7205       (setq header (or header (gnus-summary-article-header id))))
7206     (if (and header
7207              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
7208         ;; We have found the header.
7209         header
7210       ;; We have to really fetch the header to this article.
7211       (when (setq where (gnus-request-head id group))
7212         (save-excursion
7213           (set-buffer nntp-server-buffer)
7214           (goto-char (point-max))
7215           (insert ".\n")
7216           (goto-char (point-min))
7217           (insert "211 ")
7218           (princ (cond
7219                   ((numberp id) id)
7220                   ((cdr where) (cdr where))
7221                   (header (mail-header-number header))
7222                   (t gnus-reffed-article-number))
7223                  (current-buffer))
7224           (insert " Article retrieved.\n"))
7225         ;(when (and header
7226         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
7227         ;  (setcar (gnus-id-to-thread id) nil))
7228         (if (not (setq header (car (gnus-get-newsgroup-headers))))
7229             ()                          ; Malformed head.
7230           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
7231             (if (and (stringp id)
7232                      (not (string= (gnus-group-real-name group)
7233                                    (car where))))
7234                 ;; If we fetched by Message-ID and the article came
7235                 ;; from a different group, we fudge some bogus article
7236                 ;; numbers for this article.
7237                 (mail-header-set-number header gnus-reffed-article-number))
7238             (decf gnus-reffed-article-number)
7239             (gnus-remove-header (mail-header-number header))
7240             (push header gnus-newsgroup-headers)
7241             (setq gnus-current-headers header)
7242             (push (mail-header-number header) gnus-newsgroup-limit))
7243           header)))))
7244
7245 (defun gnus-remove-header (number)
7246   "Remove header NUMBER from `gnus-newsgroup-headers'."
7247   (if (and gnus-newsgroup-headers
7248            (= number (mail-header-number (car gnus-newsgroup-headers))))
7249       (pop gnus-newsgroup-headers)
7250     (let ((headers gnus-newsgroup-headers))
7251       (while (and (cdr headers)
7252                   (not (= number (mail-header-number (cadr headers)))))
7253         (pop headers))
7254       (when (cdr headers)
7255         (setcdr headers (cddr headers))))))
7256
7257 (provide 'gnus-sum)
7258
7259 ;;; gnus-sum.el ends here