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