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