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