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