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