Importing Pterodactyl Gnus v0.95.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-threading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-group-charset-alist
801   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
802     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
803     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
804     ("^relcom\\>" koi8-r)
805     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
806     ("^israel\\>" iso-8859-1)
807     ("^han\\>" euc-kr)
808     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
809     (".*" iso-8859-1))
810   "Alist of regexps (to match group names) and default charsets to be used when reading."
811   :type '(repeat (list (regexp :tag "Group")
812                        (symbol :tag "Charset")))
813   :group 'gnus-charset)
814
815 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
816   "List of charsets that should be ignored.
817 When these charsets are used in the \"charset\" parameter, the
818 default charset will be used instead."
819   :type '(repeat symbol)
820   :group 'gnus-charset)
821
822 (defcustom gnus-group-ignored-charsets-alist 
823   '(("alt\\.chinese\\.text" iso-8859-1))
824   "Alist of regexps (to match group names) and charsets that should be ignored.
825 When these charsets are used in the \"charset\" parameter, the
826 default charset will be used instead."
827   :type '(repeat (cons (regexp :tag "Group")
828                        (repeat symbol)))
829   :group 'gnus-charset)
830
831 (defcustom gnus-group-highlight-words-alist nil
832   "Alist of group regexps and highlight regexps.
833 This variable uses the same syntax as `gnus-emphasis-alist'."
834   :type '(repeat (cons (regexp :tag "Group")
835                        (repeat (list (regexp :tag "Highlight regexp")
836                                      (number :tag "Group for entire word" 0)
837                                      (number :tag "Group for displayed part" 0)
838                                      (symbol :tag "Face" 
839                                              gnus-emphasis-highlight-words)))))
840   :group 'gnus-summary-visual)
841
842 ;;; Internal variables
843
844 (defvar gnus-article-mime-handles nil)
845 (defvar gnus-article-decoded-p nil)
846 (defvar gnus-scores-exclude-files nil)
847 (defvar gnus-page-broken nil)
848 (defvar gnus-inhibit-mime-unbuttonizing nil)
849
850 (defvar gnus-original-article nil)
851 (defvar gnus-article-internal-prepare-hook nil)
852 (defvar gnus-newsgroup-process-stack nil)
853
854 (defvar gnus-thread-indent-array nil)
855 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
856
857 ;; Avoid highlighting in kill files.
858 (defvar gnus-summary-inhibit-highlight nil)
859 (defvar gnus-newsgroup-selected-overlay nil)
860 (defvar gnus-inhibit-limiting nil)
861 (defvar gnus-newsgroup-adaptive-score-file nil)
862 (defvar gnus-current-score-file nil)
863 (defvar gnus-current-move-group nil)
864 (defvar gnus-current-copy-group nil)
865 (defvar gnus-current-crosspost-group nil)
866
867 (defvar gnus-newsgroup-dependencies nil)
868 (defvar gnus-newsgroup-adaptive nil)
869 (defvar gnus-summary-display-article-function nil)
870 (defvar gnus-summary-highlight-line-function nil
871   "Function called after highlighting a summary line.")
872
873 (defvar gnus-summary-line-format-alist
874   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
875     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
876     (?s gnus-tmp-subject-or-nil ?s)
877     (?n gnus-tmp-name ?s)
878     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
879         ?s)
880     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
881             gnus-tmp-from) ?s)
882     (?F gnus-tmp-from ?s)
883     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
884     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
885     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
886     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
887     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
888     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
889     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
890     (?L gnus-tmp-lines ?d)
891     (?I gnus-tmp-indentation ?s)
892     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
893     (?R gnus-tmp-replied ?c)
894     (?\[ gnus-tmp-opening-bracket ?c)
895     (?\] gnus-tmp-closing-bracket ?c)
896     (?\> (make-string gnus-tmp-level ? ) ?s)
897     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
898     (?i gnus-tmp-score ?d)
899     (?z gnus-tmp-score-char ?c)
900     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
901     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
902     (?U gnus-tmp-unread ?c)
903     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
904     (?t (gnus-summary-number-of-articles-in-thread
905          (and (boundp 'thread) (car thread)) gnus-tmp-level)
906         ?d)
907     (?e (gnus-summary-number-of-articles-in-thread
908          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
909         ?c)
910     (?u gnus-tmp-user-defined ?s)
911     (?P (gnus-pick-line-number) ?d))
912   "An alist of format specifications that can appear in summary lines,
913 and what variables they correspond with, along with the type of the
914 variable (string, integer, character, etc).")
915
916 (defvar gnus-summary-dummy-line-format-alist
917   `((?S gnus-tmp-subject ?s)
918     (?N gnus-tmp-number ?d)
919     (?u gnus-tmp-user-defined ?s)))
920
921 (defvar gnus-summary-mode-line-format-alist
922   `((?G gnus-tmp-group-name ?s)
923     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
924     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
925     (?A gnus-tmp-article-number ?d)
926     (?Z gnus-tmp-unread-and-unselected ?s)
927     (?V gnus-version ?s)
928     (?U gnus-tmp-unread-and-unticked ?d)
929     (?S gnus-tmp-subject ?s)
930     (?e gnus-tmp-unselected ?d)
931     (?u gnus-tmp-user-defined ?s)
932     (?d (length gnus-newsgroup-dormant) ?d)
933     (?t (length gnus-newsgroup-marked) ?d)
934     (?r (length gnus-newsgroup-reads) ?d)
935     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
936     (?E gnus-newsgroup-expunged-tally ?d)
937     (?s (gnus-current-score-file-nondirectory) ?s)))
938
939 (defvar gnus-last-search-regexp nil
940   "Default regexp for article search command.")
941
942 (defvar gnus-last-shell-command nil
943   "Default shell command on article.")
944
945 (defvar gnus-newsgroup-begin nil)
946 (defvar gnus-newsgroup-end nil)
947 (defvar gnus-newsgroup-last-rmail nil)
948 (defvar gnus-newsgroup-last-mail nil)
949 (defvar gnus-newsgroup-last-folder nil)
950 (defvar gnus-newsgroup-last-file nil)
951 (defvar gnus-newsgroup-auto-expire nil)
952 (defvar gnus-newsgroup-active nil)
953
954 (defvar gnus-newsgroup-data nil)
955 (defvar gnus-newsgroup-data-reverse nil)
956 (defvar gnus-newsgroup-limit nil)
957 (defvar gnus-newsgroup-limits nil)
958
959 (defvar gnus-newsgroup-unreads nil
960   "List of unread articles in the current newsgroup.")
961
962 (defvar gnus-newsgroup-unselected nil
963   "List of unselected unread articles in the current newsgroup.")
964
965 (defvar gnus-newsgroup-reads nil
966   "Alist of read articles and article marks in the current newsgroup.")
967
968 (defvar gnus-newsgroup-expunged-tally nil)
969
970 (defvar gnus-newsgroup-marked nil
971   "List of ticked articles in the current newsgroup (a subset of unread art).")
972
973 (defvar gnus-newsgroup-killed nil
974   "List of ranges of articles that have been through the scoring process.")
975
976 (defvar gnus-newsgroup-cached nil
977   "List of articles that come from the article cache.")
978
979 (defvar gnus-newsgroup-saved nil
980   "List of articles that have been saved.")
981
982 (defvar gnus-newsgroup-kill-headers nil)
983
984 (defvar gnus-newsgroup-replied nil
985   "List of articles that have been replied to in the current newsgroup.")
986
987 (defvar gnus-newsgroup-expirable nil
988   "List of articles in the current newsgroup that can be expired.")
989
990 (defvar gnus-newsgroup-processable nil
991   "List of articles in the current newsgroup that can be processed.")
992
993 (defvar gnus-newsgroup-downloadable nil
994   "List of articles in the current newsgroup that can be processed.")
995
996 (defvar gnus-newsgroup-undownloaded nil
997   "List of articles in the current newsgroup that haven't been downloaded..")
998
999 (defvar gnus-newsgroup-unsendable nil
1000   "List of articles in the current newsgroup that won't be sent.")
1001
1002 (defvar gnus-newsgroup-bookmarks nil
1003   "List of articles in the current newsgroup that have bookmarks.")
1004
1005 (defvar gnus-newsgroup-dormant nil
1006   "List of dormant articles in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-scored nil
1009   "List of scored articles in the current newsgroup.")
1010
1011 (defvar gnus-newsgroup-headers nil
1012   "List of article headers in the current newsgroup.")
1013
1014 (defvar gnus-newsgroup-threads nil)
1015
1016 (defvar gnus-newsgroup-prepared nil
1017   "Whether the current group has been prepared properly.")
1018
1019 (defvar gnus-newsgroup-ancient nil
1020   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1021
1022 (defvar gnus-newsgroup-sparse nil)
1023
1024 (defvar gnus-current-article nil)
1025 (defvar gnus-article-current nil)
1026 (defvar gnus-current-headers nil)
1027 (defvar gnus-have-all-headers nil)
1028 (defvar gnus-last-article nil)
1029 (defvar gnus-newsgroup-history nil)
1030 (defvar gnus-newsgroup-charset nil)
1031 (defvar gnus-newsgroup-ephemeral-charset nil)
1032 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1033
1034 (defconst gnus-summary-local-variables
1035   '(gnus-newsgroup-name
1036     gnus-newsgroup-begin gnus-newsgroup-end
1037     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1038     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1039     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1040     gnus-newsgroup-unselected gnus-newsgroup-marked
1041     gnus-newsgroup-reads gnus-newsgroup-saved
1042     gnus-newsgroup-replied gnus-newsgroup-expirable
1043     gnus-newsgroup-processable gnus-newsgroup-killed
1044     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1045     gnus-newsgroup-unsendable
1046     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1047     gnus-newsgroup-headers gnus-newsgroup-threads
1048     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1049     gnus-current-article gnus-current-headers gnus-have-all-headers
1050     gnus-last-article gnus-article-internal-prepare-hook
1051     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1052     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1053     gnus-thread-expunge-below
1054     gnus-score-alist gnus-current-score-file
1055     (gnus-summary-expunge-below . global)
1056     (gnus-summary-mark-below . global)
1057     gnus-newsgroup-active gnus-scores-exclude-files
1058     gnus-newsgroup-history gnus-newsgroup-ancient
1059     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1060     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1061     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1062     (gnus-newsgroup-expunged-tally . 0)
1063     gnus-cache-removable-articles gnus-newsgroup-cached
1064     gnus-newsgroup-data gnus-newsgroup-data-reverse
1065     gnus-newsgroup-limit gnus-newsgroup-limits
1066     gnus-newsgroup-charset)
1067   "Variables that are buffer-local to the summary buffers.")
1068
1069 ;; Byte-compiler warning.
1070 (defvar gnus-article-mode-map)
1071
1072 ;; MIME stuff.
1073
1074 (defvar gnus-decode-encoded-word-methods
1075   '(mail-decode-encoded-word-string)
1076   "List of methods used to decode encoded words.
1077
1078 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1079 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1080 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1081 whose names match REGEXP.
1082
1083 For example:
1084 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1085  mail-decode-encoded-word-string
1086  (\"chinese\" . rfc1843-decode-string))
1087 ")
1088
1089 (defvar gnus-decode-encoded-word-methods-cache nil)
1090
1091 (defun gnus-multi-decode-encoded-word-string (string)
1092   "Apply the functions from `gnus-encoded-word-methods' that match."
1093   (unless (and gnus-decode-encoded-word-methods-cache
1094                (eq gnus-newsgroup-name
1095                    (car gnus-decode-encoded-word-methods-cache)))
1096     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1097     (mapc '(lambda (x)
1098              (if (symbolp x)
1099                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1100                (if (and gnus-newsgroup-name
1101                         (string-match (car x) gnus-newsgroup-name))
1102                    (nconc gnus-decode-encoded-word-methods-cache
1103                           (list (cdr x))))))
1104           gnus-decode-encoded-word-methods))
1105   (let ((xlist gnus-decode-encoded-word-methods-cache))
1106     (pop xlist)
1107     (while xlist
1108       (setq string (funcall (pop xlist) string))))
1109   string)
1110
1111 ;; Subject simplification.
1112
1113 (defun gnus-simplify-whitespace (str)
1114   "Remove excessive whitespace."
1115   (let ((mystr str))
1116     ;; Multiple spaces.
1117     (while (string-match "[ \t][ \t]+" mystr)
1118       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1119                           " "
1120                           (substring mystr (match-end 0)))))
1121     ;; Leading spaces.
1122     (when (string-match "^[ \t]+" mystr)
1123       (setq mystr (substring mystr (match-end 0))))
1124     ;; Trailing spaces.
1125     (when (string-match "[ \t]+$" mystr)
1126       (setq mystr (substring mystr 0 (match-beginning 0))))
1127     mystr))
1128
1129 (defsubst gnus-simplify-subject-re (subject)
1130   "Remove \"Re:\" from subject lines."
1131   (if (string-match "^[Rr][Ee]: *" subject)
1132       (substring subject (match-end 0))
1133     subject))
1134
1135 (defun gnus-simplify-subject (subject &optional re-only)
1136   "Remove `Re:' and words in parentheses.
1137 If RE-ONLY is non-nil, strip leading `Re:'s only."
1138   (let ((case-fold-search t))           ;Ignore case.
1139     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1140     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1141       (setq subject (substring subject (match-end 0))))
1142     ;; Remove uninteresting prefixes.
1143     (when (and (not re-only)
1144                gnus-simplify-ignored-prefixes
1145                (string-match gnus-simplify-ignored-prefixes subject))
1146       (setq subject (substring subject (match-end 0))))
1147     ;; Remove words in parentheses from end.
1148     (unless re-only
1149       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1150         (setq subject (substring subject 0 (match-beginning 0)))))
1151     ;; Return subject string.
1152     subject))
1153
1154 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1155 ;; all whitespace.
1156 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1157   (goto-char (point-min))
1158   (while (re-search-forward regexp nil t)
1159       (replace-match (or newtext ""))))
1160
1161 (defun gnus-simplify-buffer-fuzzy ()
1162   "Simplify string in the buffer fuzzily.
1163 The string in the accessible portion of the current buffer is simplified.
1164 It is assumed to be a single-line subject.
1165 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1166 matter is removed.  Additional things can be deleted by setting
1167 gnus-simplify-subject-fuzzy-regexp."
1168   (let ((case-fold-search t)
1169         (modified-tick))
1170     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1171
1172     (while (not (eq modified-tick (buffer-modified-tick)))
1173       (setq modified-tick (buffer-modified-tick))
1174       (cond
1175        ((listp gnus-simplify-subject-fuzzy-regexp)
1176         (mapcar 'gnus-simplify-buffer-fuzzy-step
1177                 gnus-simplify-subject-fuzzy-regexp))
1178        (gnus-simplify-subject-fuzzy-regexp
1179         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1180       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1181       (gnus-simplify-buffer-fuzzy-step
1182        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1183       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1184
1185     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1186     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1187     (gnus-simplify-buffer-fuzzy-step " $")
1188     (gnus-simplify-buffer-fuzzy-step "^ +")))
1189
1190 (defun gnus-simplify-subject-fuzzy (subject)
1191   "Simplify a subject string fuzzily.
1192 See `gnus-simplify-buffer-fuzzy' for details."
1193   (save-excursion
1194     (gnus-set-work-buffer)
1195     (let ((case-fold-search t))
1196       ;; Remove uninteresting prefixes.
1197       (when (and gnus-simplify-ignored-prefixes
1198                  (string-match gnus-simplify-ignored-prefixes subject))
1199         (setq subject (substring subject (match-end 0))))
1200       (insert subject)
1201       (inline (gnus-simplify-buffer-fuzzy))
1202       (buffer-string))))
1203
1204 (defsubst gnus-simplify-subject-fully (subject)
1205   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1206   (cond
1207    (gnus-simplify-subject-functions
1208     (gnus-map-function gnus-simplify-subject-functions subject))
1209    ((null gnus-summary-gather-subject-limit)
1210     (gnus-simplify-subject-re subject))
1211    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1212     (gnus-simplify-subject-fuzzy subject))
1213    ((numberp gnus-summary-gather-subject-limit)
1214     (gnus-limit-string (gnus-simplify-subject-re subject)
1215                        gnus-summary-gather-subject-limit))
1216    (t
1217     subject)))
1218
1219 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1220   "Check whether two subjects are equal.
1221 If optional argument simple-first is t, first argument is already
1222 simplified."
1223   (cond
1224    ((null simple-first)
1225     (equal (gnus-simplify-subject-fully s1)
1226            (gnus-simplify-subject-fully s2)))
1227    (t
1228     (equal s1
1229            (gnus-simplify-subject-fully s2)))))
1230
1231 (defun gnus-summary-bubble-group ()
1232   "Increase the score of the current group.
1233 This is a handy function to add to `gnus-summary-exit-hook' to
1234 increase the score of each group you read."
1235   (gnus-group-add-score gnus-newsgroup-name))
1236
1237 \f
1238 ;;;
1239 ;;; Gnus summary mode
1240 ;;;
1241
1242 (put 'gnus-summary-mode 'mode-class 'special)
1243
1244 (when t
1245   ;; Non-orthogonal keys
1246
1247   (gnus-define-keys gnus-summary-mode-map
1248     " " gnus-summary-next-page
1249     "\177" gnus-summary-prev-page
1250     [delete] gnus-summary-prev-page
1251     [backspace] gnus-summary-prev-page
1252     "\r" gnus-summary-scroll-up
1253     "\M-\r" gnus-summary-scroll-down
1254     "n" gnus-summary-next-unread-article
1255     "p" gnus-summary-prev-unread-article
1256     "N" gnus-summary-next-article
1257     "P" gnus-summary-prev-article
1258     "\M-\C-n" gnus-summary-next-same-subject
1259     "\M-\C-p" gnus-summary-prev-same-subject
1260     "\M-n" gnus-summary-next-unread-subject
1261     "\M-p" gnus-summary-prev-unread-subject
1262     "." gnus-summary-first-unread-article
1263     "," gnus-summary-best-unread-article
1264     "\M-s" gnus-summary-search-article-forward
1265     "\M-r" gnus-summary-search-article-backward
1266     "<" gnus-summary-beginning-of-article
1267     ">" gnus-summary-end-of-article
1268     "j" gnus-summary-goto-article
1269     "^" gnus-summary-refer-parent-article
1270     "\M-^" gnus-summary-refer-article
1271     "u" gnus-summary-tick-article-forward
1272     "!" gnus-summary-tick-article-forward
1273     "U" gnus-summary-tick-article-backward
1274     "d" gnus-summary-mark-as-read-forward
1275     "D" gnus-summary-mark-as-read-backward
1276     "E" gnus-summary-mark-as-expirable
1277     "\M-u" gnus-summary-clear-mark-forward
1278     "\M-U" gnus-summary-clear-mark-backward
1279     "k" gnus-summary-kill-same-subject-and-select
1280     "\C-k" gnus-summary-kill-same-subject
1281     "\M-\C-k" gnus-summary-kill-thread
1282     "\M-\C-l" gnus-summary-lower-thread
1283     "e" gnus-summary-edit-article
1284     "#" gnus-summary-mark-as-processable
1285     "\M-#" gnus-summary-unmark-as-processable
1286     "\M-\C-t" gnus-summary-toggle-threads
1287     "\M-\C-s" gnus-summary-show-thread
1288     "\M-\C-h" gnus-summary-hide-thread
1289     "\M-\C-f" gnus-summary-next-thread
1290     "\M-\C-b" gnus-summary-prev-thread
1291     "\M-\C-u" gnus-summary-up-thread
1292     "\M-\C-d" gnus-summary-down-thread
1293     "&" gnus-summary-execute-command
1294     "c" gnus-summary-catchup-and-exit
1295     "\C-w" gnus-summary-mark-region-as-read
1296     "\C-t" gnus-summary-toggle-truncation
1297     "?" gnus-summary-mark-as-dormant
1298     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1299     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1300     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1301     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1302     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1303     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1304     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1305     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1306     "=" gnus-summary-expand-window
1307     "\C-x\C-s" gnus-summary-reselect-current-group
1308     "\M-g" gnus-summary-rescan-group
1309     "w" gnus-summary-stop-page-breaking
1310     "\C-c\C-r" gnus-summary-caesar-message
1311     "f" gnus-summary-followup
1312     "F" gnus-summary-followup-with-original
1313     "C" gnus-summary-cancel-article
1314     "r" gnus-summary-reply
1315     "R" gnus-summary-reply-with-original
1316     "\C-c\C-f" gnus-summary-mail-forward
1317     "o" gnus-summary-save-article
1318     "\C-o" gnus-summary-save-article-mail
1319     "|" gnus-summary-pipe-output
1320     "\M-k" gnus-summary-edit-local-kill
1321     "\M-K" gnus-summary-edit-global-kill
1322     ;; "V" gnus-version
1323     "\C-c\C-d" gnus-summary-describe-group
1324     "q" gnus-summary-exit
1325     "Q" gnus-summary-exit-no-update
1326     "\C-c\C-i" gnus-info-find-node
1327     gnus-mouse-2 gnus-mouse-pick-article
1328     "m" gnus-summary-mail-other-window
1329     "a" gnus-summary-post-news
1330     "x" gnus-summary-limit-to-unread
1331     "s" gnus-summary-isearch-article
1332     "t" gnus-summary-toggle-header
1333     "g" gnus-summary-show-article
1334     "l" gnus-summary-goto-last-article
1335     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1336     "\C-d" gnus-summary-enter-digest-group
1337     "\M-\C-d" gnus-summary-read-document
1338     "\M-\C-e" gnus-summary-edit-parameters
1339     "\M-\C-g" gnus-summary-customize-parameters
1340     "\C-c\C-b" gnus-bug
1341     "*" gnus-cache-enter-article
1342     "\M-*" gnus-cache-remove-article
1343     "\M-&" gnus-summary-universal-argument
1344     "\C-l" gnus-recenter
1345     "I" gnus-summary-increase-score
1346     "L" gnus-summary-lower-score
1347     "\M-i" gnus-symbolic-argument
1348     "h" gnus-summary-select-article-buffer
1349
1350     "b" gnus-article-view-part
1351     "\M-t" gnus-summary-toggle-display-buttonized
1352
1353     "V" gnus-summary-score-map
1354     "X" gnus-uu-extract-map
1355     "S" gnus-summary-send-map)
1356
1357   ;; Sort of orthogonal keymap
1358   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1359     "t" gnus-summary-tick-article-forward
1360     "!" gnus-summary-tick-article-forward
1361     "d" gnus-summary-mark-as-read-forward
1362     "r" gnus-summary-mark-as-read-forward
1363     "c" gnus-summary-clear-mark-forward
1364     " " gnus-summary-clear-mark-forward
1365     "e" gnus-summary-mark-as-expirable
1366     "x" gnus-summary-mark-as-expirable
1367     "?" gnus-summary-mark-as-dormant
1368     "b" gnus-summary-set-bookmark
1369     "B" gnus-summary-remove-bookmark
1370     "#" gnus-summary-mark-as-processable
1371     "\M-#" gnus-summary-unmark-as-processable
1372     "S" gnus-summary-limit-include-expunged
1373     "C" gnus-summary-catchup
1374     "H" gnus-summary-catchup-to-here
1375     "\C-c" gnus-summary-catchup-all
1376     "k" gnus-summary-kill-same-subject-and-select
1377     "K" gnus-summary-kill-same-subject
1378     "P" gnus-uu-mark-map)
1379
1380   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1381     "c" gnus-summary-clear-above
1382     "u" gnus-summary-tick-above
1383     "m" gnus-summary-mark-above
1384     "k" gnus-summary-kill-below)
1385
1386   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1387     "/" gnus-summary-limit-to-subject
1388     "n" gnus-summary-limit-to-articles
1389     "w" gnus-summary-pop-limit
1390     "s" gnus-summary-limit-to-subject
1391     "a" gnus-summary-limit-to-author
1392     "u" gnus-summary-limit-to-unread
1393     "m" gnus-summary-limit-to-marks
1394     "M" gnus-summary-limit-exclude-marks
1395     "v" gnus-summary-limit-to-score
1396     "*" gnus-summary-limit-include-cached
1397     "D" gnus-summary-limit-include-dormant
1398     "T" gnus-summary-limit-include-thread
1399     "d" gnus-summary-limit-exclude-dormant
1400     "t" gnus-summary-limit-to-age
1401     "x" gnus-summary-limit-to-extra 
1402     "E" gnus-summary-limit-include-expunged
1403     "c" gnus-summary-limit-exclude-childless-dormant
1404     "C" gnus-summary-limit-mark-excluded-as-read)
1405
1406   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1407     "n" gnus-summary-next-unread-article
1408     "p" gnus-summary-prev-unread-article
1409     "N" gnus-summary-next-article
1410     "P" gnus-summary-prev-article
1411     "\C-n" gnus-summary-next-same-subject
1412     "\C-p" gnus-summary-prev-same-subject
1413     "\M-n" gnus-summary-next-unread-subject
1414     "\M-p" gnus-summary-prev-unread-subject
1415     "f" gnus-summary-first-unread-article
1416     "b" gnus-summary-best-unread-article
1417     "j" gnus-summary-goto-article
1418     "g" gnus-summary-goto-subject
1419     "l" gnus-summary-goto-last-article
1420     "o" gnus-summary-pop-article)
1421
1422   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1423     "k" gnus-summary-kill-thread
1424     "l" gnus-summary-lower-thread
1425     "i" gnus-summary-raise-thread
1426     "T" gnus-summary-toggle-threads
1427     "t" gnus-summary-rethread-current
1428     "^" gnus-summary-reparent-thread
1429     "s" gnus-summary-show-thread
1430     "S" gnus-summary-show-all-threads
1431     "h" gnus-summary-hide-thread
1432     "H" gnus-summary-hide-all-threads
1433     "n" gnus-summary-next-thread
1434     "p" gnus-summary-prev-thread
1435     "u" gnus-summary-up-thread
1436     "o" gnus-summary-top-thread
1437     "d" gnus-summary-down-thread
1438     "#" gnus-uu-mark-thread
1439     "\M-#" gnus-uu-unmark-thread)
1440
1441   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1442     "g" gnus-summary-prepare
1443     "c" gnus-summary-insert-cached-articles)
1444
1445   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1446     "c" gnus-summary-catchup-and-exit
1447     "C" gnus-summary-catchup-all-and-exit
1448     "E" gnus-summary-exit-no-update
1449     "Q" gnus-summary-exit
1450     "Z" gnus-summary-exit
1451     "n" gnus-summary-catchup-and-goto-next-group
1452     "R" gnus-summary-reselect-current-group
1453     "G" gnus-summary-rescan-group
1454     "N" gnus-summary-next-group
1455     "s" gnus-summary-save-newsrc
1456     "P" gnus-summary-prev-group)
1457
1458   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1459     " " gnus-summary-next-page
1460     "n" gnus-summary-next-page
1461     "\177" gnus-summary-prev-page
1462     [delete] gnus-summary-prev-page
1463     "p" gnus-summary-prev-page
1464     "\r" gnus-summary-scroll-up
1465     "\M-\r" gnus-summary-scroll-down
1466     "<" gnus-summary-beginning-of-article
1467     ">" gnus-summary-end-of-article
1468     "b" gnus-summary-beginning-of-article
1469     "e" gnus-summary-end-of-article
1470     "^" gnus-summary-refer-parent-article
1471     "r" gnus-summary-refer-parent-article
1472     "D" gnus-summary-enter-digest-group
1473     "R" gnus-summary-refer-references
1474     "T" gnus-summary-refer-thread
1475     "g" gnus-summary-show-article
1476     "s" gnus-summary-isearch-article
1477     "P" gnus-summary-print-article
1478     "t" gnus-article-babel)
1479
1480   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1481     "b" gnus-article-add-buttons
1482     "B" gnus-article-add-buttons-to-head
1483     "o" gnus-article-treat-overstrike
1484     "e" gnus-article-emphasize
1485     "w" gnus-article-fill-cited-article
1486     "Q" gnus-article-fill-long-lines
1487     "C" gnus-article-capitalize-sentences
1488     "c" gnus-article-remove-cr
1489     "q" gnus-article-de-quoted-unreadable
1490     "f" gnus-article-display-x-face
1491     "l" gnus-summary-stop-page-breaking
1492     "r" gnus-summary-caesar-message
1493     "t" gnus-article-hide-headers
1494     "v" gnus-summary-verbose-headers
1495     "H" gnus-article-strip-headers-in-body
1496     "d" gnus-article-treat-dumbquotes)
1497
1498   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1499     "a" gnus-article-hide
1500     "h" gnus-article-hide-headers
1501     "b" gnus-article-hide-boring-headers
1502     "s" gnus-article-hide-signature
1503     "c" gnus-article-hide-citation
1504     "C" gnus-article-hide-citation-in-followups
1505     "p" gnus-article-hide-pgp
1506     "B" gnus-article-strip-banner
1507     "P" gnus-article-hide-pem
1508     "\C-c" gnus-article-hide-citation-maybe)
1509
1510   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1511     "a" gnus-article-highlight
1512     "h" gnus-article-highlight-headers
1513     "c" gnus-article-highlight-citation
1514     "s" gnus-article-highlight-signature)
1515
1516   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1517     "w" gnus-article-decode-mime-words
1518     "c" gnus-article-decode-charset
1519     "v" gnus-mime-view-all-parts
1520     "b" gnus-article-view-part)
1521
1522   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1523     "z" gnus-article-date-ut
1524     "u" gnus-article-date-ut
1525     "l" gnus-article-date-local
1526     "e" gnus-article-date-lapsed
1527     "o" gnus-article-date-original
1528     "i" gnus-article-date-iso8601
1529     "s" gnus-article-date-user)
1530
1531   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1532     "t" gnus-article-remove-trailing-blank-lines
1533     "l" gnus-article-strip-leading-blank-lines
1534     "m" gnus-article-strip-multiple-blank-lines
1535     "a" gnus-article-strip-blank-lines
1536     "A" gnus-article-strip-all-blank-lines
1537     "s" gnus-article-strip-leading-space
1538     "e" gnus-article-strip-trailing-space)
1539
1540   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1541     "v" gnus-version
1542     "f" gnus-summary-fetch-faq
1543     "d" gnus-summary-describe-group
1544     "h" gnus-summary-describe-briefly
1545     "i" gnus-info-find-node)
1546
1547   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1548     "e" gnus-summary-expire-articles
1549     "\M-\C-e" gnus-summary-expire-articles-now
1550     "\177" gnus-summary-delete-article
1551     [delete] gnus-summary-delete-article
1552     [backspace] gnus-summary-delete-article
1553     "m" gnus-summary-move-article
1554     "r" gnus-summary-respool-article
1555     "w" gnus-summary-edit-article
1556     "c" gnus-summary-copy-article
1557     "B" gnus-summary-crosspost-article
1558     "q" gnus-summary-respool-query
1559     "t" gnus-summary-respool-trace
1560     "i" gnus-summary-import-article
1561     "p" gnus-summary-article-posted-p)
1562
1563   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1564     "o" gnus-summary-save-article
1565     "m" gnus-summary-save-article-mail
1566     "F" gnus-summary-write-article-file
1567     "r" gnus-summary-save-article-rmail
1568     "f" gnus-summary-save-article-file
1569     "b" gnus-summary-save-article-body-file
1570     "h" gnus-summary-save-article-folder
1571     "v" gnus-summary-save-article-vm
1572     "p" gnus-summary-pipe-output
1573     "s" gnus-soup-add-article)
1574
1575   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1576     "b" gnus-summary-display-buttonized
1577     "m" gnus-summary-repair-multipart
1578     "v" gnus-article-view-part
1579     "o" gnus-article-save-part
1580     "c" gnus-article-copy-part
1581     "e" gnus-article-externalize-part
1582     "i" gnus-article-inline-part
1583     "|" gnus-article-pipe-part)
1584   )
1585
1586 (defun gnus-summary-make-menu-bar ()
1587   (gnus-turn-off-edit-menu 'summary)
1588
1589   (unless (boundp 'gnus-summary-misc-menu)
1590
1591     (easy-menu-define
1592      gnus-summary-kill-menu gnus-summary-mode-map ""
1593      (cons
1594       "Score"
1595       (nconc
1596        (list
1597         ["Enter score..." gnus-summary-score-entry t]
1598         ["Customize" gnus-score-customize t])
1599        (gnus-make-score-map 'increase)
1600        (gnus-make-score-map 'lower)
1601        '(("Mark"
1602           ["Kill below" gnus-summary-kill-below t]
1603           ["Mark above" gnus-summary-mark-above t]
1604           ["Tick above" gnus-summary-tick-above t]
1605           ["Clear above" gnus-summary-clear-above t])
1606          ["Current score" gnus-summary-current-score t]
1607          ["Set score" gnus-summary-set-score t]
1608          ["Switch current score file..." gnus-score-change-score-file t]
1609          ["Set mark below..." gnus-score-set-mark-below t]
1610          ["Set expunge below..." gnus-score-set-expunge-below t]
1611          ["Edit current score file" gnus-score-edit-current-scores t]
1612          ["Edit score file" gnus-score-edit-file t]
1613          ["Trace score" gnus-score-find-trace t]
1614          ["Find words" gnus-score-find-favourite-words t]
1615          ["Rescore buffer" gnus-summary-rescore t]
1616          ["Increase score..." gnus-summary-increase-score t]
1617          ["Lower score..." gnus-summary-lower-score t]))))
1618
1619     ;; Define both the Article menu in the summary buffer and the equivalent
1620     ;; Commands menu in the article buffer here for consistency.
1621     (let ((innards
1622            '(("Hide"
1623               ["All" gnus-article-hide t]
1624               ["Headers" gnus-article-hide-headers t]
1625               ["Signature" gnus-article-hide-signature t]
1626               ["Citation" gnus-article-hide-citation t]
1627               ["PGP" gnus-article-hide-pgp t]
1628               ["Banner" gnus-article-strip-banner t]
1629               ["Boring headers" gnus-article-hide-boring-headers t])
1630              ("Highlight"
1631               ["All" gnus-article-highlight t]
1632               ["Headers" gnus-article-highlight-headers t]
1633               ["Signature" gnus-article-highlight-signature t]
1634               ["Citation" gnus-article-highlight-citation t])
1635              ("MIME"
1636               ["Words" gnus-article-decode-mime-words t]
1637               ["Charset" gnus-article-decode-charset t]
1638               ["QP" gnus-article-de-quoted-unreadable t]
1639               ["View all" gnus-mime-view-all-parts t])
1640              ("Date"
1641               ["Local" gnus-article-date-local t]
1642               ["ISO8601" gnus-article-date-iso8601 t]
1643               ["UT" gnus-article-date-ut t]
1644               ["Original" gnus-article-date-original t]
1645               ["Lapsed" gnus-article-date-lapsed t]
1646               ["User-defined" gnus-article-date-user t])
1647              ("Washing"
1648               ("Remove Blanks"
1649                ["Leading" gnus-article-strip-leading-blank-lines t]
1650                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1651                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1652                ["All of the above" gnus-article-strip-blank-lines t]
1653                ["All" gnus-article-strip-all-blank-lines t]
1654                ["Leading space" gnus-article-strip-leading-space t]
1655                ["Trailing space" gnus-article-strip-trailing-space t])
1656               ["Overstrike" gnus-article-treat-overstrike t]
1657               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1658               ["Emphasis" gnus-article-emphasize t]
1659               ["Word wrap" gnus-article-fill-cited-article t]
1660               ["Fill long lines" gnus-article-fill-long-lines t]
1661               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1662               ["CR" gnus-article-remove-cr t]
1663               ["Show X-Face" gnus-article-display-x-face t]
1664               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1665               ["Rot 13" gnus-summary-caesar-message t]
1666               ["Unix pipe" gnus-summary-pipe-message t]
1667               ["Add buttons" gnus-article-add-buttons t]
1668               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1669               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1670               ["Verbose header" gnus-summary-verbose-headers t]
1671               ["Toggle header" gnus-summary-toggle-header t])
1672              ("Output"
1673               ["Save in default format" gnus-summary-save-article t]
1674               ["Save in file" gnus-summary-save-article-file t]
1675               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1676               ["Save in MH folder" gnus-summary-save-article-folder t]
1677               ["Save in VM folder" gnus-summary-save-article-vm t]
1678               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1679               ["Save body in file" gnus-summary-save-article-body-file t]
1680               ["Pipe through a filter" gnus-summary-pipe-output t]
1681               ["Add to SOUP packet" gnus-soup-add-article t]
1682               ["Print" gnus-summary-print-article t])
1683              ("Backend"
1684               ["Respool article..." gnus-summary-respool-article t]
1685               ["Move article..." gnus-summary-move-article
1686                (gnus-check-backend-function
1687                 'request-move-article gnus-newsgroup-name)]
1688               ["Copy article..." gnus-summary-copy-article t]
1689               ["Crosspost article..." gnus-summary-crosspost-article
1690                (gnus-check-backend-function
1691                 'request-replace-article gnus-newsgroup-name)]
1692               ["Import file..." gnus-summary-import-article t]
1693               ["Check if posted" gnus-summary-article-posted-p t]
1694               ["Edit article" gnus-summary-edit-article
1695                (not (gnus-group-read-only-p))]
1696               ["Delete article" gnus-summary-delete-article
1697                (gnus-check-backend-function
1698                 'request-expire-articles gnus-newsgroup-name)]
1699               ["Query respool" gnus-summary-respool-query t]
1700               ["Trace respool" gnus-summary-respool-trace t]
1701               ["Delete expirable articles" gnus-summary-expire-articles-now
1702                (gnus-check-backend-function
1703                 'request-expire-articles gnus-newsgroup-name)])
1704              ("Extract"
1705               ["Uudecode" gnus-uu-decode-uu t]
1706               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1707               ["Unshar" gnus-uu-decode-unshar t]
1708               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1709               ["Save" gnus-uu-decode-save t]
1710               ["Binhex" gnus-uu-decode-binhex t]
1711               ["Postscript" gnus-uu-decode-postscript t])
1712              ("Cache"
1713               ["Enter article" gnus-cache-enter-article t]
1714               ["Remove article" gnus-cache-remove-article t])
1715              ["Translate" gnus-article-babel t]
1716              ["Select article buffer" gnus-summary-select-article-buffer t]
1717              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1718              ["Isearch article..." gnus-summary-isearch-article t]
1719              ["Beginning of the article" gnus-summary-beginning-of-article t]
1720              ["End of the article" gnus-summary-end-of-article t]
1721              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1722              ["Fetch referenced articles" gnus-summary-refer-references t]
1723              ["Fetch current thread" gnus-summary-refer-thread t]
1724              ["Fetch article with id..." gnus-summary-refer-article t]
1725              ["Redisplay" gnus-summary-show-article t])))
1726       (easy-menu-define
1727        gnus-summary-article-menu gnus-summary-mode-map ""
1728        (cons "Article" innards))
1729
1730       (easy-menu-define
1731        gnus-article-commands-menu gnus-article-mode-map ""
1732        (cons "Commands" innards)))
1733
1734     (easy-menu-define
1735      gnus-summary-thread-menu gnus-summary-mode-map ""
1736      '("Threads"
1737        ["Toggle threading" gnus-summary-toggle-threads t]
1738        ["Hide threads" gnus-summary-hide-all-threads t]
1739        ["Show threads" gnus-summary-show-all-threads t]
1740        ["Hide thread" gnus-summary-hide-thread t]
1741        ["Show thread" gnus-summary-show-thread t]
1742        ["Go to next thread" gnus-summary-next-thread t]
1743        ["Go to previous thread" gnus-summary-prev-thread t]
1744        ["Go down thread" gnus-summary-down-thread t]
1745        ["Go up thread" gnus-summary-up-thread t]
1746        ["Top of thread" gnus-summary-top-thread t]
1747        ["Mark thread as read" gnus-summary-kill-thread t]
1748        ["Lower thread score" gnus-summary-lower-thread t]
1749        ["Raise thread score" gnus-summary-raise-thread t]
1750        ["Rethread current" gnus-summary-rethread-current t]
1751        ))
1752
1753     (easy-menu-define
1754      gnus-summary-post-menu gnus-summary-mode-map ""
1755      '("Post"
1756        ["Post an article" gnus-summary-post-news t]
1757        ["Followup" gnus-summary-followup t]
1758        ["Followup and yank" gnus-summary-followup-with-original t]
1759        ["Supersede article" gnus-summary-supersede-article t]
1760        ["Cancel article" gnus-summary-cancel-article t]
1761        ["Reply" gnus-summary-reply t]
1762        ["Reply and yank" gnus-summary-reply-with-original t]
1763        ["Wide reply" gnus-summary-wide-reply t]
1764        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1765        ["Mail forward" gnus-summary-mail-forward t]
1766        ["Post forward" gnus-summary-post-forward t]
1767        ["Digest and mail" gnus-uu-digest-mail-forward t]
1768        ["Digest and post" gnus-uu-digest-post-forward t]
1769        ["Resend message" gnus-summary-resend-message t]
1770        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1771        ["Send a mail" gnus-summary-mail-other-window t]
1772        ["Uuencode and post" gnus-uu-post-news t]
1773        ["Followup via news" gnus-summary-followup-to-mail t]
1774        ["Followup via news and yank"
1775         gnus-summary-followup-to-mail-with-original t]
1776        ;;("Draft"
1777        ;;["Send" gnus-summary-send-draft t]
1778        ;;["Send bounced" gnus-resend-bounced-mail t])
1779        ))
1780
1781     (easy-menu-define
1782      gnus-summary-misc-menu gnus-summary-mode-map ""
1783      '("Misc"
1784        ("Mark Read"
1785         ["Mark as read" gnus-summary-mark-as-read-forward t]
1786         ["Mark same subject and select"
1787          gnus-summary-kill-same-subject-and-select t]
1788         ["Mark same subject" gnus-summary-kill-same-subject t]
1789         ["Catchup" gnus-summary-catchup t]
1790         ["Catchup all" gnus-summary-catchup-all t]
1791         ["Catchup to here" gnus-summary-catchup-to-here t]
1792         ["Catchup region" gnus-summary-mark-region-as-read t]
1793         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1794        ("Mark Various"
1795         ["Tick" gnus-summary-tick-article-forward t]
1796         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1797         ["Remove marks" gnus-summary-clear-mark-forward t]
1798         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1799         ["Set bookmark" gnus-summary-set-bookmark t]
1800         ["Remove bookmark" gnus-summary-remove-bookmark t])
1801        ("Mark Limit"
1802         ["Marks..." gnus-summary-limit-to-marks t]
1803         ["Subject..." gnus-summary-limit-to-subject t]
1804         ["Author..." gnus-summary-limit-to-author t]
1805         ["Age..." gnus-summary-limit-to-age t]
1806         ["Extra..." gnus-summary-limit-to-extra t]
1807         ["Score" gnus-summary-limit-to-score t]
1808         ["Unread" gnus-summary-limit-to-unread t]
1809         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1810         ["Articles" gnus-summary-limit-to-articles t]
1811         ["Pop limit" gnus-summary-pop-limit t]
1812         ["Show dormant" gnus-summary-limit-include-dormant t]
1813         ["Hide childless dormant"
1814          gnus-summary-limit-exclude-childless-dormant t]
1815         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1816         ["Hide marked" gnus-summary-limit-exclude-marks t]
1817         ["Show expunged" gnus-summary-show-all-expunged t])
1818        ("Process Mark"
1819         ["Set mark" gnus-summary-mark-as-processable t]
1820         ["Remove mark" gnus-summary-unmark-as-processable t]
1821         ["Remove all marks" gnus-summary-unmark-all-processable t]
1822         ["Mark above" gnus-uu-mark-over t]
1823         ["Mark series" gnus-uu-mark-series t]
1824         ["Mark region" gnus-uu-mark-region t]
1825         ["Unmark region" gnus-uu-unmark-region t]
1826         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1827         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1828         ["Mark all" gnus-uu-mark-all t]
1829         ["Mark buffer" gnus-uu-mark-buffer t]
1830         ["Mark sparse" gnus-uu-mark-sparse t]
1831         ["Mark thread" gnus-uu-mark-thread t]
1832         ["Unmark thread" gnus-uu-unmark-thread t]
1833         ("Process Mark Sets"
1834          ["Kill" gnus-summary-kill-process-mark t]
1835          ["Yank" gnus-summary-yank-process-mark
1836           gnus-newsgroup-process-stack]
1837          ["Save" gnus-summary-save-process-mark t]))
1838        ("Scroll article"
1839         ["Page forward" gnus-summary-next-page t]
1840         ["Page backward" gnus-summary-prev-page t]
1841         ["Line forward" gnus-summary-scroll-up t])
1842        ("Move"
1843         ["Next unread article" gnus-summary-next-unread-article t]
1844         ["Previous unread article" gnus-summary-prev-unread-article t]
1845         ["Next article" gnus-summary-next-article t]
1846         ["Previous article" gnus-summary-prev-article t]
1847         ["Next unread subject" gnus-summary-next-unread-subject t]
1848         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1849         ["Next article same subject" gnus-summary-next-same-subject t]
1850         ["Previous article same subject" gnus-summary-prev-same-subject t]
1851         ["First unread article" gnus-summary-first-unread-article t]
1852         ["Best unread article" gnus-summary-best-unread-article t]
1853         ["Go to subject number..." gnus-summary-goto-subject t]
1854         ["Go to article number..." gnus-summary-goto-article t]
1855         ["Go to the last article" gnus-summary-goto-last-article t]
1856         ["Pop article off history" gnus-summary-pop-article t])
1857        ("Sort"
1858         ["Sort by number" gnus-summary-sort-by-number t]
1859         ["Sort by author" gnus-summary-sort-by-author t]
1860         ["Sort by subject" gnus-summary-sort-by-subject t]
1861         ["Sort by date" gnus-summary-sort-by-date t]
1862         ["Sort by score" gnus-summary-sort-by-score t]
1863         ["Sort by lines" gnus-summary-sort-by-lines t]
1864         ["Sort by characters" gnus-summary-sort-by-chars t])
1865        ("Help"
1866         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1867         ["Describe group" gnus-summary-describe-group t]
1868         ["Read manual" gnus-info-find-node t])
1869        ("Modes"
1870         ["Pick and read" gnus-pick-mode t]
1871         ["Binary" gnus-binary-mode t])
1872        ("Regeneration"
1873         ["Regenerate" gnus-summary-prepare t]
1874         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1875         ["Toggle threading" gnus-summary-toggle-threads t])
1876        ["Filter articles..." gnus-summary-execute-command t]
1877        ["Run command on subjects..." gnus-summary-universal-argument t]
1878        ["Search articles forward..." gnus-summary-search-article-forward t]
1879        ["Search articles backward..." gnus-summary-search-article-backward t]
1880        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1881        ["Expand window" gnus-summary-expand-window t]
1882        ["Expire expirable articles" gnus-summary-expire-articles
1883         (gnus-check-backend-function
1884          'request-expire-articles gnus-newsgroup-name)]
1885        ["Edit local kill file" gnus-summary-edit-local-kill t]
1886        ["Edit main kill file" gnus-summary-edit-global-kill t]
1887        ["Edit group parameters" gnus-summary-edit-parameters t]
1888        ["Customize group parameters" gnus-summary-customize-parameters t]
1889        ["Send a bug report" gnus-bug t]
1890        ("Exit"
1891         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1892         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1893         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1894         ["Exit group" gnus-summary-exit t]
1895         ["Exit group without updating" gnus-summary-exit-no-update t]
1896         ["Exit and goto next group" gnus-summary-next-group t]
1897         ["Exit and goto prev group" gnus-summary-prev-group t]
1898         ["Reselect group" gnus-summary-reselect-current-group t]
1899         ["Rescan group" gnus-summary-rescan-group t]
1900         ["Update dribble" gnus-summary-save-newsrc t])))
1901
1902     (gnus-run-hooks 'gnus-summary-menu-hook)))
1903
1904 (defun gnus-score-set-default (var value)
1905   "A version of set that updates the GNU Emacs menu-bar."
1906   (set var value)
1907   ;; It is the message that forces the active status to be updated.
1908   (message ""))
1909
1910 (defun gnus-make-score-map (type)
1911   "Make a summary score map of type TYPE."
1912   (if t
1913       nil
1914     (let ((headers '(("author" "from" string)
1915                      ("subject" "subject" string)
1916                      ("article body" "body" string)
1917                      ("article head" "head" string)
1918                      ("xref" "xref" string)
1919                      ("extra header" "extra" string)
1920                      ("lines" "lines" number)
1921                      ("followups to author" "followup" string)))
1922           (types '((number ("less than" <)
1923                            ("greater than" >)
1924                            ("equal" =))
1925                    (string ("substring" s)
1926                            ("exact string" e)
1927                            ("fuzzy string" f)
1928                            ("regexp" r))))
1929           (perms '(("temporary" (current-time-string))
1930                    ("permanent" nil)
1931                    ("immediate" now)))
1932           header)
1933       (list
1934        (apply
1935         'nconc
1936         (list
1937          (if (eq type 'lower)
1938              "Lower score"
1939            "Increase score"))
1940         (let (outh)
1941           (while headers
1942             (setq header (car headers))
1943             (setq outh
1944                   (cons
1945                    (apply
1946                     'nconc
1947                     (list (car header))
1948                     (let ((ts (cdr (assoc (nth 2 header) types)))
1949                           outt)
1950                       (while ts
1951                         (setq outt
1952                               (cons
1953                                (apply
1954                                 'nconc
1955                                 (list (caar ts))
1956                                 (let ((ps perms)
1957                                       outp)
1958                                   (while ps
1959                                     (setq outp
1960                                           (cons
1961                                            (vector
1962                                             (caar ps)
1963                                             (list
1964                                              'gnus-summary-score-entry
1965                                              (nth 1 header)
1966                                              (if (or (string= (nth 1 header)
1967                                                               "head")
1968                                                      (string= (nth 1 header)
1969                                                               "body"))
1970                                                  ""
1971                                                (list 'gnus-summary-header
1972                                                      (nth 1 header)))
1973                                              (list 'quote (nth 1 (car ts)))
1974                                              (list 'gnus-score-default nil)
1975                                              (nth 1 (car ps))
1976                                              t)
1977                                             t)
1978                                            outp))
1979                                     (setq ps (cdr ps)))
1980                                   (list (nreverse outp))))
1981                                outt))
1982                         (setq ts (cdr ts)))
1983                       (list (nreverse outt))))
1984                    outh))
1985             (setq headers (cdr headers)))
1986           (list (nreverse outh))))))))
1987
1988 \f
1989
1990 (defun gnus-summary-mode (&optional group)
1991   "Major mode for reading articles.
1992
1993 All normal editing commands are switched off.
1994 \\<gnus-summary-mode-map>
1995 Each line in this buffer represents one article.  To read an
1996 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1997 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1998 respectively.
1999
2000 You can also post articles and send mail from this buffer.  To
2001 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2002 of an article, type `\\[gnus-summary-reply]'.
2003
2004 There are approx. one gazillion commands you can execute in this
2005 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2006
2007 The following commands are available:
2008
2009 \\{gnus-summary-mode-map}"
2010   (interactive)
2011   (when (gnus-visual-p 'summary-menu 'menu)
2012     (gnus-summary-make-menu-bar))
2013   (kill-all-local-variables)
2014   (gnus-summary-make-local-variables)
2015   (gnus-make-thread-indent-array)
2016   (gnus-simplify-mode-line)
2017   (setq major-mode 'gnus-summary-mode)
2018   (setq mode-name "Summary")
2019   (make-local-variable 'minor-mode-alist)
2020   (use-local-map gnus-summary-mode-map)
2021   (buffer-disable-undo)
2022   (setq buffer-read-only t)             ;Disable modification
2023   (setq truncate-lines t)
2024   (setq selective-display t)
2025   (setq selective-display-ellipses t)   ;Display `...'
2026   (gnus-summary-set-display-table)
2027   (gnus-set-default-directory)
2028   (setq gnus-newsgroup-name group)
2029   (make-local-variable 'gnus-summary-line-format)
2030   (make-local-variable 'gnus-summary-line-format-spec)
2031   (make-local-variable 'gnus-summary-dummy-line-format)
2032   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2033   (make-local-variable 'gnus-summary-mark-positions)
2034   (make-local-hook 'pre-command-hook)
2035   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2036   (gnus-run-hooks 'gnus-summary-mode-hook)
2037   (mm-enable-multibyte)
2038   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2039   (gnus-update-summary-mark-positions))
2040
2041 (defun gnus-summary-make-local-variables ()
2042   "Make all the local summary buffer variables."
2043   (let (global)
2044     (dolist (local gnus-summary-local-variables)
2045       (if (consp local)
2046           (progn
2047             (if (eq (cdr local) 'global)
2048                 ;; Copy the global value of the variable.
2049                 (setq global (symbol-value (car local)))
2050               ;; Use the value from the list.
2051               (setq global (eval (cdr local))))
2052             (set (make-local-variable (car local)) global))
2053         ;; Simple nil-valued local variable.
2054         (set (make-local-variable local) nil)))))
2055
2056 (defun gnus-summary-clear-local-variables ()
2057   (let ((locals gnus-summary-local-variables))
2058     (while locals
2059       (if (consp (car locals))
2060           (and (vectorp (caar locals))
2061                (set (caar locals) nil))
2062         (and (vectorp (car locals))
2063              (set (car locals) nil)))
2064       (setq locals (cdr locals)))))
2065
2066 ;; Summary data functions.
2067
2068 (defmacro gnus-data-number (data)
2069   `(car ,data))
2070
2071 (defmacro gnus-data-set-number (data number)
2072   `(setcar ,data ,number))
2073
2074 (defmacro gnus-data-mark (data)
2075   `(nth 1 ,data))
2076
2077 (defmacro gnus-data-set-mark (data mark)
2078   `(setcar (nthcdr 1 ,data) ,mark))
2079
2080 (defmacro gnus-data-pos (data)
2081   `(nth 2 ,data))
2082
2083 (defmacro gnus-data-set-pos (data pos)
2084   `(setcar (nthcdr 2 ,data) ,pos))
2085
2086 (defmacro gnus-data-header (data)
2087   `(nth 3 ,data))
2088
2089 (defmacro gnus-data-set-header (data header)
2090   `(setf (nth 3 ,data) ,header))
2091
2092 (defmacro gnus-data-level (data)
2093   `(nth 4 ,data))
2094
2095 (defmacro gnus-data-unread-p (data)
2096   `(= (nth 1 ,data) gnus-unread-mark))
2097
2098 (defmacro gnus-data-read-p (data)
2099   `(/= (nth 1 ,data) gnus-unread-mark))
2100
2101 (defmacro gnus-data-pseudo-p (data)
2102   `(consp (nth 3 ,data)))
2103
2104 (defmacro gnus-data-find (number)
2105   `(assq ,number gnus-newsgroup-data))
2106
2107 (defmacro gnus-data-find-list (number &optional data)
2108   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2109      (memq (assq ,number bdata)
2110            bdata)))
2111
2112 (defmacro gnus-data-make (number mark pos header level)
2113   `(list ,number ,mark ,pos ,header ,level))
2114
2115 (defun gnus-data-enter (after-article number mark pos header level offset)
2116   (let ((data (gnus-data-find-list after-article)))
2117     (unless data
2118       (error "No such article: %d" after-article))
2119     (setcdr data (cons (gnus-data-make number mark pos header level)
2120                        (cdr data)))
2121     (setq gnus-newsgroup-data-reverse nil)
2122     (gnus-data-update-list (cddr data) offset)))
2123
2124 (defun gnus-data-enter-list (after-article list &optional offset)
2125   (when list
2126     (let ((data (and after-article (gnus-data-find-list after-article)))
2127           (ilist list))
2128       (if (not (or data
2129                    after-article))
2130           (let ((odata gnus-newsgroup-data))
2131             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2132             (when offset
2133               (gnus-data-update-list odata offset)))
2134         ;; Find the last element in the list to be spliced into the main
2135         ;; list.
2136         (while (cdr list)
2137           (setq list (cdr list)))
2138         (if (not data)
2139             (progn
2140               (setcdr list gnus-newsgroup-data)
2141               (setq gnus-newsgroup-data ilist)
2142               (when offset
2143                 (gnus-data-update-list (cdr list) offset)))
2144           (setcdr list (cdr data))
2145           (setcdr data ilist)
2146           (when offset
2147             (gnus-data-update-list (cdr list) offset))))
2148       (setq gnus-newsgroup-data-reverse nil))))
2149
2150 (defun gnus-data-remove (article &optional offset)
2151   (let ((data gnus-newsgroup-data))
2152     (if (= (gnus-data-number (car data)) article)
2153         (progn
2154           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2155                 gnus-newsgroup-data-reverse nil)
2156           (when offset
2157             (gnus-data-update-list gnus-newsgroup-data offset)))
2158       (while (cdr data)
2159         (when (= (gnus-data-number (cadr data)) article)
2160           (setcdr data (cddr data))
2161           (when offset
2162             (gnus-data-update-list (cdr data) offset))
2163           (setq data nil
2164                 gnus-newsgroup-data-reverse nil))
2165         (setq data (cdr data))))))
2166
2167 (defmacro gnus-data-list (backward)
2168   `(if ,backward
2169        (or gnus-newsgroup-data-reverse
2170            (setq gnus-newsgroup-data-reverse
2171                  (reverse gnus-newsgroup-data)))
2172      gnus-newsgroup-data))
2173
2174 (defun gnus-data-update-list (data offset)
2175   "Add OFFSET to the POS of all data entries in DATA."
2176   (setq gnus-newsgroup-data-reverse nil)
2177   (while data
2178     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2179     (setq data (cdr data))))
2180
2181 (defun gnus-summary-article-pseudo-p (article)
2182   "Say whether this article is a pseudo article or not."
2183   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2184
2185 (defmacro gnus-summary-article-sparse-p (article)
2186   "Say whether this article is a sparse article or not."
2187   `(memq ,article gnus-newsgroup-sparse))
2188
2189 (defmacro gnus-summary-article-ancient-p (article)
2190   "Say whether this article is a sparse article or not."
2191   `(memq ,article gnus-newsgroup-ancient))
2192
2193 (defun gnus-article-parent-p (number)
2194   "Say whether this article is a parent or not."
2195   (let ((data (gnus-data-find-list number)))
2196     (and (cdr data)                     ; There has to be an article after...
2197          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2198             (gnus-data-level (nth 1 data))))))
2199
2200 (defun gnus-article-children (number)
2201   "Return a list of all children to NUMBER."
2202   (let* ((data (gnus-data-find-list number))
2203          (level (gnus-data-level (car data)))
2204          children)
2205     (setq data (cdr data))
2206     (while (and data
2207                 (= (gnus-data-level (car data)) (1+ level)))
2208       (push (gnus-data-number (car data)) children)
2209       (setq data (cdr data)))
2210     children))
2211
2212 (defmacro gnus-summary-skip-intangible ()
2213   "If the current article is intangible, then jump to a different article."
2214   '(let ((to (get-text-property (point) 'gnus-intangible)))
2215      (and to (gnus-summary-goto-subject to))))
2216
2217 (defmacro gnus-summary-article-intangible-p ()
2218   "Say whether this article is intangible or not."
2219   '(get-text-property (point) 'gnus-intangible))
2220
2221 (defun gnus-article-read-p (article)
2222   "Say whether ARTICLE is read or not."
2223   (not (or (memq article gnus-newsgroup-marked)
2224            (memq article gnus-newsgroup-unreads)
2225            (memq article gnus-newsgroup-unselected)
2226            (memq article gnus-newsgroup-dormant))))
2227
2228 ;; Some summary mode macros.
2229
2230 (defmacro gnus-summary-article-number ()
2231   "The article number of the article on the current line.
2232 If there isn's an article number here, then we return the current
2233 article number."
2234   '(progn
2235      (gnus-summary-skip-intangible)
2236      (or (get-text-property (point) 'gnus-number)
2237          (gnus-summary-last-subject))))
2238
2239 (defmacro gnus-summary-article-header (&optional number)
2240   "Return the header of article NUMBER."
2241   `(gnus-data-header (gnus-data-find
2242                       ,(or number '(gnus-summary-article-number)))))
2243
2244 (defmacro gnus-summary-thread-level (&optional number)
2245   "Return the level of thread that starts with article NUMBER."
2246   `(if (and (eq gnus-summary-make-false-root 'dummy)
2247             (get-text-property (point) 'gnus-intangible))
2248        0
2249      (gnus-data-level (gnus-data-find
2250                        ,(or number '(gnus-summary-article-number))))))
2251
2252 (defmacro gnus-summary-article-mark (&optional number)
2253   "Return the mark of article NUMBER."
2254   `(gnus-data-mark (gnus-data-find
2255                     ,(or number '(gnus-summary-article-number)))))
2256
2257 (defmacro gnus-summary-article-pos (&optional number)
2258   "Return the position of the line of article NUMBER."
2259   `(gnus-data-pos (gnus-data-find
2260                    ,(or number '(gnus-summary-article-number)))))
2261
2262 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2263 (defmacro gnus-summary-article-subject (&optional number)
2264   "Return current subject string or nil if nothing."
2265   `(let ((headers
2266           ,(if number
2267                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2268              '(gnus-data-header (assq (gnus-summary-article-number)
2269                                       gnus-newsgroup-data)))))
2270      (and headers
2271           (vectorp headers)
2272           (mail-header-subject headers))))
2273
2274 (defmacro gnus-summary-article-score (&optional number)
2275   "Return current article score."
2276   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2277                   gnus-newsgroup-scored))
2278        gnus-summary-default-score 0))
2279
2280 (defun gnus-summary-article-children (&optional number)
2281   "Return a list of article numbers that are children of article NUMBER."
2282   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2283          (level (gnus-data-level (car data)))
2284          l children)
2285     (while (and (setq data (cdr data))
2286                 (> (setq l (gnus-data-level (car data))) level))
2287       (and (= (1+ level) l)
2288            (push (gnus-data-number (car data))
2289                  children)))
2290     (nreverse children)))
2291
2292 (defun gnus-summary-article-parent (&optional number)
2293   "Return the article number of the parent of article NUMBER."
2294   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2295                                     (gnus-data-list t)))
2296          (level (gnus-data-level (car data))))
2297     (if (zerop level)
2298         ()                              ; This is a root.
2299       ;; We search until we find an article with a level less than
2300       ;; this one.  That function has to be the parent.
2301       (while (and (setq data (cdr data))
2302                   (not (< (gnus-data-level (car data)) level))))
2303       (and data (gnus-data-number (car data))))))
2304
2305 (defun gnus-unread-mark-p (mark)
2306   "Say whether MARK is the unread mark."
2307   (= mark gnus-unread-mark))
2308
2309 (defun gnus-read-mark-p (mark)
2310   "Say whether MARK is one of the marks that mark as read.
2311 This is all marks except unread, ticked, dormant, and expirable."
2312   (not (or (= mark gnus-unread-mark)
2313            (= mark gnus-ticked-mark)
2314            (= mark gnus-dormant-mark)
2315            (= mark gnus-expirable-mark))))
2316
2317 (defmacro gnus-article-mark (number)
2318   "Return the MARK of article NUMBER.
2319 This macro should only be used when computing the mark the \"first\"
2320 time; i.e., when generating the summary lines.  After that,
2321 `gnus-summary-article-mark' should be used to examine the
2322 marks of articles."
2323   `(cond
2324     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2325     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2326     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2327     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2328     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2329     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2330     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2331     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2332            gnus-ancient-mark))))
2333
2334 ;; Saving hidden threads.
2335
2336 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2337 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2338
2339 (defmacro gnus-save-hidden-threads (&rest forms)
2340   "Save hidden threads, eval FORMS, and restore the hidden threads."
2341   (let ((config (make-symbol "config")))
2342     `(let ((,config (gnus-hidden-threads-configuration)))
2343        (unwind-protect
2344            (save-excursion
2345              ,@forms)
2346          (gnus-restore-hidden-threads-configuration ,config)))))
2347
2348 (defun gnus-data-compute-positions ()
2349   "Compute the positions of all articles."
2350   (setq gnus-newsgroup-data-reverse nil)
2351   (let ((data gnus-newsgroup-data))
2352     (save-excursion
2353       (gnus-save-hidden-threads
2354         (gnus-summary-show-all-threads)
2355         (goto-char (point-min))
2356         (while data
2357           (while (get-text-property (point) 'gnus-intangible)
2358             (forward-line 1))
2359           (gnus-data-set-pos (car data) (+ (point) 3))
2360           (setq data (cdr data))
2361           (forward-line 1))))))
2362
2363 (defun gnus-hidden-threads-configuration ()
2364   "Return the current hidden threads configuration."
2365   (save-excursion
2366     (let (config)
2367       (goto-char (point-min))
2368       (while (search-forward "\r" nil t)
2369         (push (1- (point)) config))
2370       config)))
2371
2372 (defun gnus-restore-hidden-threads-configuration (config)
2373   "Restore hidden threads configuration from CONFIG."
2374   (let (point buffer-read-only)
2375     (while (setq point (pop config))
2376       (when (and (< point (point-max))
2377                  (goto-char point)
2378                  (eq (char-after) ?\n))
2379         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2380
2381 ;; Various summary mode internalish functions.
2382
2383 (defun gnus-mouse-pick-article (e)
2384   (interactive "e")
2385   (mouse-set-point e)
2386   (gnus-summary-next-page nil t))
2387
2388 (defun gnus-summary-set-display-table ()
2389   ;; Change the display table.  Odd characters have a tendency to mess
2390   ;; up nicely formatted displays - we make all possible glyphs
2391   ;; display only a single character.
2392
2393   ;; We start from the standard display table, if any.
2394   (let ((table (or (copy-sequence standard-display-table)
2395                    (make-display-table)))
2396         (i 32))
2397     ;; Nix out all the control chars...
2398     (while (>= (setq i (1- i)) 0)
2399       (aset table i [??]))
2400     ;; ... but not newline and cr, of course.  (cr is necessary for the
2401     ;; selective display).
2402     (aset table ?\n nil)
2403     (aset table ?\r nil)
2404     ;; We keep TAB as well.
2405     (aset table ?\t nil)
2406     ;; We nix out any glyphs over 126 that are not set already.
2407     (let ((i 256))
2408       (while (>= (setq i (1- i)) 127)
2409         ;; Only modify if the entry is nil.
2410         (unless (aref table i)
2411           (aset table i [??]))))
2412     (setq buffer-display-table table)))
2413
2414 (defun gnus-summary-setup-buffer (group)
2415   "Initialize summary buffer."
2416   (let ((buffer (concat "*Summary " group "*")))
2417     (if (get-buffer buffer)
2418         (progn
2419           (set-buffer buffer)
2420           (setq gnus-summary-buffer (current-buffer))
2421           (not gnus-newsgroup-prepared))
2422       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2423       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2424       (gnus-summary-mode group)
2425       (when gnus-carpal
2426         (gnus-carpal-setup-buffer 'summary))
2427       (unless gnus-single-article-buffer
2428         (make-local-variable 'gnus-article-buffer)
2429         (make-local-variable 'gnus-article-current)
2430         (make-local-variable 'gnus-original-article-buffer))
2431       (setq gnus-newsgroup-name group)
2432       t)))
2433
2434 (defun gnus-set-global-variables ()
2435   ;; Set the global equivalents of the summary buffer-local variables
2436   ;; to the latest values they had.  These reflect the summary buffer
2437   ;; that was in action when the last article was fetched.
2438   (when (eq major-mode 'gnus-summary-mode)
2439     (setq gnus-summary-buffer (current-buffer))
2440     (let ((name gnus-newsgroup-name)
2441           (marked gnus-newsgroup-marked)
2442           (unread gnus-newsgroup-unreads)
2443           (headers gnus-current-headers)
2444           (data gnus-newsgroup-data)
2445           (summary gnus-summary-buffer)
2446           (article-buffer gnus-article-buffer)
2447           (original gnus-original-article-buffer)
2448           (gac gnus-article-current)
2449           (reffed gnus-reffed-article-number)
2450           (score-file gnus-current-score-file)
2451           (default-charset gnus-newsgroup-charset))
2452       (save-excursion
2453         (set-buffer gnus-group-buffer)
2454         (setq gnus-newsgroup-name name
2455               gnus-newsgroup-marked marked
2456               gnus-newsgroup-unreads unread
2457               gnus-current-headers headers
2458               gnus-newsgroup-data data
2459               gnus-article-current gac
2460               gnus-summary-buffer summary
2461               gnus-article-buffer article-buffer
2462               gnus-original-article-buffer original
2463               gnus-reffed-article-number reffed
2464               gnus-current-score-file score-file
2465               gnus-newsgroup-charset default-charset)
2466         ;; The article buffer also has local variables.
2467         (when (gnus-buffer-live-p gnus-article-buffer)
2468           (set-buffer gnus-article-buffer)
2469           (setq gnus-summary-buffer summary))))))
2470
2471 (defun gnus-summary-article-unread-p (article)
2472   "Say whether ARTICLE is unread or not."
2473   (memq article gnus-newsgroup-unreads))
2474
2475 (defun gnus-summary-first-article-p (&optional article)
2476   "Return whether ARTICLE is the first article in the buffer."
2477   (if (not (setq article (or article (gnus-summary-article-number))))
2478       nil
2479     (eq article (caar gnus-newsgroup-data))))
2480
2481 (defun gnus-summary-last-article-p (&optional article)
2482   "Return whether ARTICLE is the last article in the buffer."
2483   (if (not (setq article (or article (gnus-summary-article-number))))
2484       ;; All non-existent numbers are the last article.  :-)
2485       t
2486     (not (cdr (gnus-data-find-list article)))))
2487
2488 (defun gnus-make-thread-indent-array ()
2489   (let ((n 200))
2490     (unless (and gnus-thread-indent-array
2491                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2492       (setq gnus-thread-indent-array (make-vector 201 "")
2493             gnus-thread-indent-array-level gnus-thread-indent-level)
2494       (while (>= n 0)
2495         (aset gnus-thread-indent-array n
2496               (make-string (* n gnus-thread-indent-level) ? ))
2497         (setq n (1- n))))))
2498
2499 (defun gnus-update-summary-mark-positions ()
2500   "Compute where the summary marks are to go."
2501   (save-excursion
2502     (when (gnus-buffer-exists-p gnus-summary-buffer)
2503       (set-buffer gnus-summary-buffer))
2504     (let ((gnus-replied-mark 129)
2505           (gnus-score-below-mark 130)
2506           (gnus-score-over-mark 130)
2507           (gnus-download-mark 131)
2508           (spec gnus-summary-line-format-spec)
2509           gnus-visual pos)
2510       (save-excursion
2511         (gnus-set-work-buffer)
2512         (let ((gnus-summary-line-format-spec spec)
2513               (gnus-newsgroup-downloadable '((0 . t))))
2514           (gnus-summary-insert-line
2515            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2516           (goto-char (point-min))
2517           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2518                                              (- (point) 2)))))
2519           (goto-char (point-min))
2520           (push (cons 'replied (and (search-forward "\201" nil t)
2521                                     (- (point) 2)))
2522                 pos)
2523           (goto-char (point-min))
2524           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2525                 pos)
2526           (goto-char (point-min))
2527           (push (cons 'download
2528                       (and (search-forward "\203" nil t) (- (point) 2)))
2529                 pos)))
2530       (setq gnus-summary-mark-positions pos))))
2531
2532 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2533   "Insert a dummy root in the summary buffer."
2534   (beginning-of-line)
2535   (gnus-add-text-properties
2536    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2537    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2538
2539 (defun gnus-summary-from-or-to-or-newsgroups (header)
2540   (let ((to (cdr (assq 'To (mail-header-extra header))))
2541         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2542         (mail-parse-charset gnus-newsgroup-charset)
2543         (mail-parse-ignored-charsets 
2544          (save-excursion (set-buffer gnus-summary-buffer)
2545                          gnus-newsgroup-ignored-charsets)))
2546     (cond
2547      ((and to
2548            gnus-ignored-from-addresses
2549            (string-match gnus-ignored-from-addresses
2550                          (mail-header-from header)))
2551       (concat "-> "
2552               (or (car (funcall gnus-extract-address-components
2553                                 (funcall
2554                                  gnus-decode-encoded-word-function to)))
2555                   (funcall gnus-decode-encoded-word-function to))))
2556      ((and newsgroups
2557            gnus-ignored-from-addresses
2558            (string-match gnus-ignored-from-addresses
2559                          (mail-header-from header)))
2560       (concat "=> " newsgroups))
2561      (t
2562       (or (car (funcall gnus-extract-address-components
2563                         (mail-header-from header)))
2564           (mail-header-from header))))))
2565
2566 (defun gnus-summary-insert-line (gnus-tmp-header
2567                                  gnus-tmp-level gnus-tmp-current
2568                                  gnus-tmp-unread gnus-tmp-replied
2569                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2570                                  &optional gnus-tmp-dummy gnus-tmp-score
2571                                  gnus-tmp-process)
2572   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2573          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2574          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2575          (gnus-tmp-score-char
2576           (if (or (null gnus-summary-default-score)
2577                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2578                       gnus-summary-zcore-fuzz))
2579               ? ;Whitespace
2580             (if (< gnus-tmp-score gnus-summary-default-score)
2581                 gnus-score-below-mark gnus-score-over-mark)))
2582          (gnus-tmp-replied
2583           (cond (gnus-tmp-process gnus-process-mark)
2584                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2585                  gnus-cached-mark)
2586                 (gnus-tmp-replied gnus-replied-mark)
2587                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2588                  gnus-saved-mark)
2589                 (t gnus-unread-mark)))
2590          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2591          (gnus-tmp-name
2592           (cond
2593            ((string-match "<[^>]+> *$" gnus-tmp-from)
2594             (let ((beg (match-beginning 0)))
2595               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2596                        (substring gnus-tmp-from (1+ (match-beginning 0))
2597                                   (1- (match-end 0))))
2598                   (substring gnus-tmp-from 0 beg))))
2599            ((string-match "(.+)" gnus-tmp-from)
2600             (substring gnus-tmp-from
2601                        (1+ (match-beginning 0)) (1- (match-end 0))))
2602            (t gnus-tmp-from)))
2603          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2604          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2605          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2606          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2607          (buffer-read-only nil))
2608     (when (string= gnus-tmp-name "")
2609       (setq gnus-tmp-name gnus-tmp-from))
2610     (unless (numberp gnus-tmp-lines)
2611       (setq gnus-tmp-lines 0))
2612     (gnus-put-text-property
2613      (point)
2614      (progn (eval gnus-summary-line-format-spec) (point))
2615      'gnus-number gnus-tmp-number)
2616     (when (gnus-visual-p 'summary-highlight 'highlight)
2617       (forward-line -1)
2618       (gnus-run-hooks 'gnus-summary-update-hook)
2619       (forward-line 1))))
2620
2621 (defun gnus-summary-update-line (&optional dont-update)
2622   ;; Update summary line after change.
2623   (when (and gnus-summary-default-score
2624              (not gnus-summary-inhibit-highlight))
2625     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2626            (article (gnus-summary-article-number))
2627            (score (gnus-summary-article-score article)))
2628       (unless dont-update
2629         (if (and gnus-summary-mark-below
2630                  (< (gnus-summary-article-score)
2631                     gnus-summary-mark-below))
2632             ;; This article has a low score, so we mark it as read.
2633             (when (memq article gnus-newsgroup-unreads)
2634               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2635           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2636             ;; This article was previously marked as read on account
2637             ;; of a low score, but now it has risen, so we mark it as
2638             ;; unread.
2639             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2640         (gnus-summary-update-mark
2641          (if (or (null gnus-summary-default-score)
2642                  (<= (abs (- score gnus-summary-default-score))
2643                      gnus-summary-zcore-fuzz))
2644              ? ;Whitespace
2645            (if (< score gnus-summary-default-score)
2646                gnus-score-below-mark gnus-score-over-mark))
2647          'score))
2648       ;; Do visual highlighting.
2649       (when (gnus-visual-p 'summary-highlight 'highlight)
2650         (gnus-run-hooks 'gnus-summary-update-hook)))))
2651
2652 (defvar gnus-tmp-new-adopts nil)
2653
2654 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2655   "Return the number of articles in THREAD.
2656 This may be 0 in some cases -- if none of the articles in
2657 the thread are to be displayed."
2658   (let* ((number
2659           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2660           (cond
2661            ((not (listp thread))
2662             1)
2663            ((and (consp thread) (cdr thread))
2664             (apply
2665              '+ 1 (mapcar
2666                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2667            ((null thread)
2668             1)
2669            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2670             1)
2671            (t 0))))
2672     (when (and level (zerop level) gnus-tmp-new-adopts)
2673       (incf number
2674             (apply '+ (mapcar
2675                        'gnus-summary-number-of-articles-in-thread
2676                        gnus-tmp-new-adopts))))
2677     (if char
2678         (if (> number 1) gnus-not-empty-thread-mark
2679           gnus-empty-thread-mark)
2680       number)))
2681
2682 (defun gnus-summary-set-local-parameters (group)
2683   "Go through the local params of GROUP and set all variable specs in that list."
2684   (let ((params (gnus-group-find-parameter group))
2685         elem)
2686     (while params
2687       (setq elem (car params)
2688             params (cdr params))
2689       (and (consp elem)                 ; Has to be a cons.
2690            (consp (cdr elem))           ; The cdr has to be a list.
2691            (symbolp (car elem))         ; Has to be a symbol in there.
2692            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2693            (ignore-errors               ; So we set it.
2694              (make-local-variable (car elem))
2695              (set (car elem) (eval (nth 1 elem))))))))
2696
2697 (defun gnus-summary-read-group (group &optional show-all no-article
2698                                       kill-buffer no-display backward
2699                                       select-articles)
2700   "Start reading news in newsgroup GROUP.
2701 If SHOW-ALL is non-nil, already read articles are also listed.
2702 If NO-ARTICLE is non-nil, no article is selected initially.
2703 If NO-DISPLAY, don't generate a summary buffer."
2704   (let (result)
2705     (while (and group
2706                 (null (setq result
2707                             (let ((gnus-auto-select-next nil))
2708                               (or (gnus-summary-read-group-1
2709                                    group show-all no-article
2710                                    kill-buffer no-display
2711                                    select-articles)
2712                                   (setq show-all nil
2713                                         select-articles nil)))))
2714                 (eq gnus-auto-select-next 'quietly))
2715       (set-buffer gnus-group-buffer)
2716       ;; The entry function called above goes to the next
2717       ;; group automatically, so we go two groups back
2718       ;; if we are searching for the previous group.
2719       (when backward
2720         (gnus-group-prev-unread-group 2))
2721       (if (not (equal group (gnus-group-group-name)))
2722           (setq group (gnus-group-group-name))
2723         (setq group nil)))
2724     result))
2725
2726 (defun gnus-summary-read-group-1 (group show-all no-article
2727                                         kill-buffer no-display
2728                                         &optional select-articles)
2729   ;; Killed foreign groups can't be entered.
2730   (when (and (not (gnus-group-native-p group))
2731              (not (gnus-gethash group gnus-newsrc-hashtb)))
2732     (error "Dead non-native groups can't be entered"))
2733   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2734   (let* ((new-group (gnus-summary-setup-buffer group))
2735          (quit-config (gnus-group-quit-config group))
2736          (did-select (and new-group (gnus-select-newsgroup
2737                                      group show-all select-articles))))
2738     (cond
2739      ;; This summary buffer exists already, so we just select it.
2740      ((not new-group)
2741       (gnus-set-global-variables)
2742       (when kill-buffer
2743         (gnus-kill-or-deaden-summary kill-buffer))
2744       (gnus-configure-windows 'summary 'force)
2745       (gnus-set-mode-line 'summary)
2746       (gnus-summary-position-point)
2747       (message "")
2748       t)
2749      ;; We couldn't select this group.
2750      ((null did-select)
2751       (when (and (eq major-mode 'gnus-summary-mode)
2752                  (not (equal (current-buffer) kill-buffer)))
2753         (kill-buffer (current-buffer))
2754         (if (not quit-config)
2755             (progn
2756               ;; Update the info -- marks might need to be removed,
2757               ;; for instance.
2758               (gnus-summary-update-info)
2759               (set-buffer gnus-group-buffer)
2760               (gnus-group-jump-to-group group)
2761               (gnus-group-next-unread-group 1))
2762           (gnus-handle-ephemeral-exit quit-config)))
2763       (gnus-message 3 "Can't select group")
2764       nil)
2765      ;; The user did a `C-g' while prompting for number of articles,
2766      ;; so we exit this group.
2767      ((eq did-select 'quit)
2768       (and (eq major-mode 'gnus-summary-mode)
2769            (not (equal (current-buffer) kill-buffer))
2770            (kill-buffer (current-buffer)))
2771       (when kill-buffer
2772         (gnus-kill-or-deaden-summary kill-buffer))
2773       (if (not quit-config)
2774           (progn
2775             (set-buffer gnus-group-buffer)
2776             (gnus-group-jump-to-group group)
2777             (gnus-group-next-unread-group 1)
2778             (gnus-configure-windows 'group 'force))
2779         (gnus-handle-ephemeral-exit quit-config))
2780       ;; Finally signal the quit.
2781       (signal 'quit nil))
2782      ;; The group was successfully selected.
2783      (t
2784       (gnus-set-global-variables)
2785       ;; Save the active value in effect when the group was entered.
2786       (setq gnus-newsgroup-active
2787             (gnus-copy-sequence
2788              (gnus-active gnus-newsgroup-name)))
2789       ;; You can change the summary buffer in some way with this hook.
2790       (gnus-run-hooks 'gnus-select-group-hook)
2791       ;; Set any local variables in the group parameters.
2792       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2793       (gnus-update-format-specifications
2794        nil 'summary 'summary-mode 'summary-dummy)
2795       (gnus-update-summary-mark-positions)
2796       ;; Do score processing.
2797       (when gnus-use-scoring
2798         (gnus-possibly-score-headers))
2799       ;; Check whether to fill in the gaps in the threads.
2800       (when gnus-build-sparse-threads
2801         (gnus-build-sparse-threads))
2802       ;; Find the initial limit.
2803       (if gnus-show-threads
2804           (if show-all
2805               (let ((gnus-newsgroup-dormant nil))
2806                 (gnus-summary-initial-limit show-all))
2807             (gnus-summary-initial-limit show-all))
2808         (setq gnus-newsgroup-limit
2809               (mapcar
2810                (lambda (header) (mail-header-number header))
2811                gnus-newsgroup-headers)))
2812       ;; Generate the summary buffer.
2813       (unless no-display
2814         (gnus-summary-prepare))
2815       (when gnus-use-trees
2816         (gnus-tree-open group)
2817         (setq gnus-summary-highlight-line-function
2818               'gnus-tree-highlight-article))
2819       ;; If the summary buffer is empty, but there are some low-scored
2820       ;; articles or some excluded dormants, we include these in the
2821       ;; buffer.
2822       (when (and (zerop (buffer-size))
2823                  (not no-display))
2824         (cond (gnus-newsgroup-dormant
2825                (gnus-summary-limit-include-dormant))
2826               ((and gnus-newsgroup-scored show-all)
2827                (gnus-summary-limit-include-expunged t))))
2828       ;; Function `gnus-apply-kill-file' must be called in this hook.
2829       (gnus-run-hooks 'gnus-apply-kill-hook)
2830       (if (and (zerop (buffer-size))
2831                (not no-display))
2832           (progn
2833             ;; This newsgroup is empty.
2834             (gnus-summary-catchup-and-exit nil t)
2835             (gnus-message 6 "No unread news")
2836             (when kill-buffer
2837               (gnus-kill-or-deaden-summary kill-buffer))
2838             ;; Return nil from this function.
2839             nil)
2840         ;; Hide conversation thread subtrees.  We cannot do this in
2841         ;; gnus-summary-prepare-hook since kill processing may not
2842         ;; work with hidden articles.
2843         (and gnus-show-threads
2844              gnus-thread-hide-subtree
2845              (gnus-summary-hide-all-threads))
2846         (when kill-buffer
2847           (gnus-kill-or-deaden-summary kill-buffer))
2848         ;; Show first unread article if requested.
2849         (if (and (not no-article)
2850                  (not no-display)
2851                  gnus-newsgroup-unreads
2852                  gnus-auto-select-first)
2853             (progn
2854               (gnus-configure-windows 'summary)
2855               (cond
2856                ((eq gnus-auto-select-first 'best)
2857                 (gnus-summary-best-unread-article))
2858                ((eq gnus-auto-select-first t)
2859                 (gnus-summary-first-unread-article))
2860                ((gnus-functionp gnus-auto-select-first)
2861                 (funcall gnus-auto-select-first))))
2862           ;; Don't select any articles, just move point to the first
2863           ;; article in the group.
2864           (goto-char (point-min))
2865           (gnus-summary-position-point)
2866           (gnus-configure-windows 'summary 'force)
2867           (gnus-set-mode-line 'summary))
2868         (when (get-buffer-window gnus-group-buffer t)
2869           ;; Gotta use windows, because recenter does weird stuff if
2870           ;; the current buffer ain't the displayed window.
2871           (let ((owin (selected-window)))
2872             (select-window (get-buffer-window gnus-group-buffer t))
2873             (when (gnus-group-goto-group group)
2874               (recenter))
2875             (select-window owin)))
2876         ;; Mark this buffer as "prepared".
2877         (setq gnus-newsgroup-prepared t)
2878         (gnus-run-hooks 'gnus-summary-prepared-hook)
2879         t)))))
2880
2881 (defun gnus-summary-prepare ()
2882   "Generate the summary buffer."
2883   (interactive)
2884   (let ((buffer-read-only nil))
2885     (erase-buffer)
2886     (setq gnus-newsgroup-data nil
2887           gnus-newsgroup-data-reverse nil)
2888     (gnus-run-hooks 'gnus-summary-generate-hook)
2889     ;; Generate the buffer, either with threads or without.
2890     (when gnus-newsgroup-headers
2891       (gnus-summary-prepare-threads
2892        (if gnus-show-threads
2893            (gnus-sort-gathered-threads
2894             (funcall gnus-summary-thread-gathering-function
2895                      (gnus-sort-threads
2896                       (gnus-cut-threads (gnus-make-threads)))))
2897          ;; Unthreaded display.
2898          (gnus-sort-articles gnus-newsgroup-headers))))
2899     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2900     ;; Call hooks for modifying summary buffer.
2901     (goto-char (point-min))
2902     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2903
2904 (defsubst gnus-general-simplify-subject (subject)
2905   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2906   (setq subject
2907         (cond
2908          ;; Truncate the subject.
2909          (gnus-simplify-subject-functions
2910           (gnus-map-function gnus-simplify-subject-functions subject))
2911          ((numberp gnus-summary-gather-subject-limit)
2912           (setq subject (gnus-simplify-subject-re subject))
2913           (if (> (length subject) gnus-summary-gather-subject-limit)
2914               (substring subject 0 gnus-summary-gather-subject-limit)
2915             subject))
2916          ;; Fuzzily simplify it.
2917          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2918           (gnus-simplify-subject-fuzzy subject))
2919          ;; Just remove the leading "Re:".
2920          (t
2921           (gnus-simplify-subject-re subject))))
2922
2923   (if (and gnus-summary-gather-exclude-subject
2924            (string-match gnus-summary-gather-exclude-subject subject))
2925       nil                               ; This article shouldn't be gathered
2926     subject))
2927
2928 (defun gnus-summary-simplify-subject-query ()
2929   "Query where the respool algorithm would put this article."
2930   (interactive)
2931   (gnus-summary-select-article)
2932   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2933
2934 (defun gnus-gather-threads-by-subject (threads)
2935   "Gather threads by looking at Subject headers."
2936   (if (not gnus-summary-make-false-root)
2937       threads
2938     (let ((hashtb (gnus-make-hashtable 1024))
2939           (prev threads)
2940           (result threads)
2941           subject hthread whole-subject)
2942       (while threads
2943         (setq subject (gnus-general-simplify-subject
2944                        (setq whole-subject (mail-header-subject
2945                                             (caar threads)))))
2946         (when subject
2947           (if (setq hthread (gnus-gethash subject hashtb))
2948               (progn
2949                 ;; We enter a dummy root into the thread, if we
2950                 ;; haven't done that already.
2951                 (unless (stringp (caar hthread))
2952                   (setcar hthread (list whole-subject (car hthread))))
2953                 ;; We add this new gathered thread to this gathered
2954                 ;; thread.
2955                 (setcdr (car hthread)
2956                         (nconc (cdar hthread) (list (car threads))))
2957                 ;; Remove it from the list of threads.
2958                 (setcdr prev (cdr threads))
2959                 (setq threads prev))
2960             ;; Enter this thread into the hash table.
2961             (gnus-sethash subject threads hashtb)))
2962         (setq prev threads)
2963         (setq threads (cdr threads)))
2964       result)))
2965
2966 (defun gnus-gather-threads-by-references (threads)
2967   "Gather threads by looking at References headers."
2968   (let ((idhashtb (gnus-make-hashtable 1024))
2969         (thhashtb (gnus-make-hashtable 1024))
2970         (prev threads)
2971         (result threads)
2972         ids references id gthread gid entered ref)
2973     (while threads
2974       (when (setq references (mail-header-references (caar threads)))
2975         (setq id (mail-header-id (caar threads))
2976               ids (gnus-split-references references)
2977               entered nil)
2978         (while (setq ref (pop ids))
2979           (setq ids (delete ref ids))
2980           (if (not (setq gid (gnus-gethash ref idhashtb)))
2981               (progn
2982                 (gnus-sethash ref id idhashtb)
2983                 (gnus-sethash id threads thhashtb))
2984             (setq gthread (gnus-gethash gid thhashtb))
2985             (unless entered
2986               ;; We enter a dummy root into the thread, if we
2987               ;; haven't done that already.
2988               (unless (stringp (caar gthread))
2989                 (setcar gthread (list (mail-header-subject (caar gthread))
2990                                       (car gthread))))
2991               ;; We add this new gathered thread to this gathered
2992               ;; thread.
2993               (setcdr (car gthread)
2994                       (nconc (cdar gthread) (list (car threads)))))
2995             ;; Add it into the thread hash table.
2996             (gnus-sethash id gthread thhashtb)
2997             (setq entered t)
2998             ;; Remove it from the list of threads.
2999             (setcdr prev (cdr threads))
3000             (setq threads prev))))
3001       (setq prev threads)
3002       (setq threads (cdr threads)))
3003     result))
3004
3005 (defun gnus-sort-gathered-threads (threads)
3006   "Sort subtreads inside each gathered thread by article number."
3007   (let ((result threads))
3008     (while threads
3009       (when (stringp (caar threads))
3010         (setcdr (car threads)
3011                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
3012       (setq threads (cdr threads)))
3013     result))
3014
3015 (defun gnus-thread-loop-p (root thread)
3016   "Say whether ROOT is in THREAD."
3017   (let ((stack (list thread))
3018         (infloop 0)
3019         th)
3020     (while (setq thread (pop stack))
3021       (setq th (cdr thread))
3022       (while (and th
3023                   (not (eq (caar th) root)))
3024         (pop th))
3025       (if th
3026           ;; We have found a loop.
3027           (let (ref-dep)
3028             (setcdr thread (delq (car th) (cdr thread)))
3029             (if (boundp (setq ref-dep (intern "none"
3030                                               gnus-newsgroup-dependencies)))
3031                 (setcdr (symbol-value ref-dep)
3032                         (nconc (cdr (symbol-value ref-dep))
3033                                (list (car th))))
3034               (set ref-dep (list nil (car th))))
3035             (setq infloop 1
3036                   stack nil))
3037         ;; Push all the subthreads onto the stack.
3038         (push (cdr thread) stack)))
3039     infloop))
3040
3041 (defun gnus-make-threads ()
3042   "Go through the dependency hashtb and find the roots.  Return all threads."
3043   (let (threads)
3044     (while (catch 'infloop
3045              (mapatoms
3046               (lambda (refs)
3047                 ;; Deal with self-referencing References loops.
3048                 (when (and (car (symbol-value refs))
3049                            (not (zerop
3050                                  (apply
3051                                   '+
3052                                   (mapcar
3053                                    (lambda (thread)
3054                                      (gnus-thread-loop-p
3055                                       (car (symbol-value refs)) thread))
3056                                    (cdr (symbol-value refs)))))))
3057                   (setq threads nil)
3058                   (throw 'infloop t))
3059                 (unless (car (symbol-value refs))
3060                   ;; These threads do not refer back to any other articles,
3061                   ;; so they're roots.
3062                   (setq threads (append (cdr (symbol-value refs)) threads))))
3063               gnus-newsgroup-dependencies)))
3064     threads))
3065
3066 ;; Build the thread tree.
3067 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3068   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3069
3070 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3071 if it was already present.
3072
3073 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3074 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3075 Message-IDs will be renamed be renamed to a unique Message-ID before
3076 being entered.
3077
3078 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3079   (let* ((id (mail-header-id header))
3080          (id-dep (and id (intern id dependencies)))
3081          ref ref-dep ref-header)
3082     ;; Enter this `header' in the `dependencies' table.
3083     (cond
3084      ((not id-dep)
3085       (setq header nil))
3086      ;; The first two cases do the normal part: enter a new `header'
3087      ;; in the `dependencies' table.
3088      ((not (boundp id-dep))
3089       (set id-dep (list header)))
3090      ((null (car (symbol-value id-dep)))
3091       (setcar (symbol-value id-dep) header))
3092
3093      ;; From here the `header' was already present in the
3094      ;; `dependencies' table.
3095      (force-new
3096       ;; Overrides an existing entry;
3097       ;; just set the header part of the entry.
3098       (setcar (symbol-value id-dep) header))
3099
3100      ;; Renames the existing `header' to a unique Message-ID.
3101      ((not gnus-summary-ignore-duplicates)
3102       ;; An article with this Message-ID has already been seen.
3103       ;; We rename the Message-ID.
3104       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3105            (list header))
3106       (mail-header-set-id header id))
3107
3108      ;; The last case ignores an existing entry, except it adds any
3109      ;; additional Xrefs (in case the two articles came from different
3110      ;; servers.
3111      ;; Also sets `header' to `nil' meaning that the `dependencies'
3112      ;; table was *not* modified.
3113      (t
3114       (mail-header-set-xref
3115        (car (symbol-value id-dep))
3116        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3117                    "")
3118                (or (mail-header-xref header) "")))
3119       (setq header nil)))
3120
3121     (when header
3122       ;; First check if that we are not creating a References loop.
3123       (setq ref (gnus-parent-id (mail-header-references header)))
3124       (while (and ref
3125                   (setq ref-dep (intern-soft ref dependencies))
3126                   (boundp ref-dep)
3127                   (setq ref-header (car (symbol-value ref-dep))))
3128         (if (string= id ref)
3129             ;; Yuk!  This is a reference loop.  Make the article be a
3130             ;; root article.
3131             (progn
3132               (mail-header-set-references (car (symbol-value id-dep)) "none")
3133               (setq ref nil))
3134           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3135       (setq ref (gnus-parent-id (mail-header-references header)))
3136       (setq ref-dep (intern (or ref "none") dependencies))
3137       (if (boundp ref-dep)
3138           (setcdr (symbol-value ref-dep)
3139                   (nconc (cdr (symbol-value ref-dep))
3140                          (list (symbol-value id-dep))))
3141         (set ref-dep (list nil (symbol-value id-dep)))))
3142     header))
3143
3144 (defun gnus-build-sparse-threads ()
3145   (let ((headers gnus-newsgroup-headers)
3146         (gnus-summary-ignore-duplicates t)
3147         header references generation relations
3148         subject child end new-child date)
3149     ;; First we create an alist of generations/relations, where
3150     ;; generations is how much we trust the relation, and the relation
3151     ;; is parent/child.
3152     (gnus-message 7 "Making sparse threads...")
3153     (save-excursion
3154       (nnheader-set-temp-buffer " *gnus sparse threads*")
3155       (while (setq header (pop headers))
3156         (when (and (setq references (mail-header-references header))
3157                    (not (string= references "")))
3158           (insert references)
3159           (setq child (mail-header-id header)
3160                 subject (mail-header-subject header)
3161                 date (mail-header-date header)
3162                 generation 0)
3163           (while (search-backward ">" nil t)
3164             (setq end (1+ (point)))
3165             (when (search-backward "<" nil t)
3166               (setq new-child (buffer-substring (point) end))
3167               (push (list (incf generation)
3168                           child (setq child new-child)
3169                           subject date)
3170                     relations)))
3171           (when child
3172             (push (list (1+ generation) child nil subject) relations))
3173           (erase-buffer)))
3174       (kill-buffer (current-buffer)))
3175     ;; Sort over trustworthiness.
3176     (mapcar
3177      (lambda (relation)
3178        (when (gnus-dependencies-add-header
3179               (make-full-mail-header
3180                gnus-reffed-article-number
3181                (nth 3 relation) "" (or (nth 4 relation) "")
3182                (nth 1 relation)
3183                (or (nth 2 relation) "") 0 0 "")
3184               gnus-newsgroup-dependencies nil)
3185          (push gnus-reffed-article-number gnus-newsgroup-limit)
3186          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3187          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3188                gnus-newsgroup-reads)
3189          (decf gnus-reffed-article-number)))
3190      (sort relations 'car-less-than-car))
3191     (gnus-message 7 "Making sparse threads...done")))
3192
3193 (defun gnus-build-old-threads ()
3194   ;; Look at all the articles that refer back to old articles, and
3195   ;; fetch the headers for the articles that aren't there.  This will
3196   ;; build complete threads - if the roots haven't been expired by the
3197   ;; server, that is.
3198   (let (id heads)
3199     (mapatoms
3200      (lambda (refs)
3201        (when (not (car (symbol-value refs)))
3202          (setq heads (cdr (symbol-value refs)))
3203          (while heads
3204            (if (memq (mail-header-number (caar heads))
3205                      gnus-newsgroup-dormant)
3206                (setq heads (cdr heads))
3207              (setq id (symbol-name refs))
3208              (while (and (setq id (gnus-build-get-header id))
3209                          (not (car (gnus-id-to-thread id)))))
3210              (setq heads nil)))))
3211      gnus-newsgroup-dependencies)))
3212
3213 ;; This function has to be called with point after the article number
3214 ;; on the beginning of the line.
3215 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3216   (let ((eol (gnus-point-at-eol))
3217         (buffer (current-buffer))
3218         header)
3219
3220     ;; overview: [num subject from date id refs chars lines misc]
3221     (unwind-protect
3222         (progn
3223           (narrow-to-region (point) eol)
3224           (unless (eobp)
3225             (forward-char))
3226
3227           (setq header
3228                 (make-full-mail-header
3229                  number                         ; number
3230                  (funcall gnus-decode-encoded-word-function
3231                           (nnheader-nov-field)) ; subject
3232                  (funcall gnus-decode-encoded-word-function
3233                           (nnheader-nov-field)) ; from
3234                  (nnheader-nov-field)           ; date
3235                  (nnheader-nov-read-message-id) ; id
3236                  (nnheader-nov-field)           ; refs
3237                  (nnheader-nov-read-integer)    ; chars
3238                  (nnheader-nov-read-integer)    ; lines
3239                  (unless (eobp)
3240                    (nnheader-nov-field))                ; misc
3241                  (nnheader-nov-parse-extra))))  ; extra
3242
3243       (widen))
3244
3245     (when gnus-alter-header-function
3246       (funcall gnus-alter-header-function header))
3247     (gnus-dependencies-add-header header dependencies force-new)))
3248
3249 (defun gnus-build-get-header (id)
3250   ;; Look through the buffer of NOV lines and find the header to
3251   ;; ID.  Enter this line into the dependencies hash table, and return
3252   ;; the id of the parent article (if any).
3253   (let ((deps gnus-newsgroup-dependencies)
3254         found header)
3255     (prog1
3256         (save-excursion
3257           (set-buffer nntp-server-buffer)
3258           (let ((case-fold-search nil))
3259             (goto-char (point-min))
3260             (while (and (not found)
3261                         (search-forward id nil t))
3262               (beginning-of-line)
3263               (setq found (looking-at
3264                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3265                                    (regexp-quote id))))
3266               (or found (beginning-of-line 2)))
3267             (when found
3268               (beginning-of-line)
3269               (and
3270                (setq header (gnus-nov-parse-line
3271                              (read (current-buffer)) deps))
3272                (gnus-parent-id (mail-header-references header))))))
3273       (when header
3274         (let ((number (mail-header-number header)))
3275           (push number gnus-newsgroup-limit)
3276           (push header gnus-newsgroup-headers)
3277           (if (memq number gnus-newsgroup-unselected)
3278               (progn
3279                 (push number gnus-newsgroup-unreads)
3280                 (setq gnus-newsgroup-unselected
3281                       (delq number gnus-newsgroup-unselected)))
3282             (push number gnus-newsgroup-ancient)))))))
3283
3284 (defun gnus-build-all-threads ()
3285   "Read all the headers."
3286   (let ((gnus-summary-ignore-duplicates t)
3287         (dependencies gnus-newsgroup-dependencies)
3288         header article)
3289     (save-excursion
3290       (set-buffer nntp-server-buffer)
3291       (let ((case-fold-search nil))
3292         (goto-char (point-min))
3293         (while (not (eobp))
3294           (ignore-errors
3295             (setq article (read (current-buffer))
3296                   header (gnus-nov-parse-line
3297                           article dependencies)))
3298           (when header
3299             (save-excursion
3300               (set-buffer gnus-summary-buffer)
3301               (push header gnus-newsgroup-headers)
3302               (if (memq (setq article (mail-header-number header))
3303                         gnus-newsgroup-unselected)
3304                   (progn
3305                     (push article gnus-newsgroup-unreads)
3306                     (setq gnus-newsgroup-unselected
3307                           (delq article gnus-newsgroup-unselected)))
3308                 (push article gnus-newsgroup-ancient)))
3309             (forward-line 1)))))))
3310
3311 (defun gnus-summary-update-article-line (article header)
3312   "Update the line for ARTICLE using HEADERS."
3313   (let* ((id (mail-header-id header))
3314          (thread (gnus-id-to-thread id)))
3315     (unless thread
3316       (error "Article in no thread"))
3317     ;; Update the thread.
3318     (setcar thread header)
3319     (gnus-summary-goto-subject article)
3320     (let* ((datal (gnus-data-find-list article))
3321            (data (car datal))
3322            (length (when (cdr datal)
3323                      (- (gnus-data-pos data)
3324                         (gnus-data-pos (cadr datal)))))
3325            (buffer-read-only nil)
3326            (level (gnus-summary-thread-level)))
3327       (gnus-delete-line)
3328       (gnus-summary-insert-line
3329        header level nil (gnus-article-mark article)
3330        (memq article gnus-newsgroup-replied)
3331        (memq article gnus-newsgroup-expirable)
3332        ;; Only insert the Subject string when it's different
3333        ;; from the previous Subject string.
3334        (if (gnus-subject-equal
3335             (condition-case ()
3336                 (mail-header-subject
3337                  (gnus-data-header
3338                   (cadr
3339                    (gnus-data-find-list
3340                     article
3341                     (gnus-data-list t)))))
3342               ;; Error on the side of excessive subjects.
3343               (error ""))
3344             (mail-header-subject header))
3345            ""
3346          (mail-header-subject header))
3347        nil (cdr (assq article gnus-newsgroup-scored))
3348        (memq article gnus-newsgroup-processable))
3349       (when length
3350         (gnus-data-update-list
3351          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3352
3353 (defun gnus-summary-update-article (article &optional iheader)
3354   "Update ARTICLE in the summary buffer."
3355   (set-buffer gnus-summary-buffer)
3356   (let* ((header (gnus-summary-article-header article))
3357          (id (mail-header-id header))
3358          (data (gnus-data-find article))
3359          (thread (gnus-id-to-thread id))
3360          (references (mail-header-references header))
3361          (parent
3362           (gnus-id-to-thread
3363            (or (gnus-parent-id
3364                 (when (and references
3365                            (not (equal "" references)))
3366                   references))
3367                "none")))
3368          (buffer-read-only nil)
3369          (old (car thread)))
3370     (when thread
3371       (unless iheader
3372         (setcar thread nil)
3373         (when parent
3374           (delq thread parent)))
3375       (if (gnus-summary-insert-subject id header)
3376           ;; Set the (possibly) new article number in the data structure.
3377           (gnus-data-set-number data (gnus-id-to-article id))
3378         (setcar thread old)
3379         nil))))
3380
3381 (defun gnus-rebuild-thread (id &optional line)
3382   "Rebuild the thread containing ID.
3383 If LINE, insert the rebuilt thread starting on line LINE."
3384   (let ((buffer-read-only nil)
3385         old-pos current thread data)
3386     (if (not gnus-show-threads)
3387         (setq thread (list (car (gnus-id-to-thread id))))
3388       ;; Get the thread this article is part of.
3389       (setq thread (gnus-remove-thread id)))
3390     (setq old-pos (gnus-point-at-bol))
3391     (setq current (save-excursion
3392                     (and (zerop (forward-line -1))
3393                          (gnus-summary-article-number))))
3394     ;; If this is a gathered thread, we have to go some re-gathering.
3395     (when (stringp (car thread))
3396       (let ((subject (car thread))
3397             roots thr)
3398         (setq thread (cdr thread))
3399         (while thread
3400           (unless (memq (setq thr (gnus-id-to-thread
3401                                    (gnus-root-id
3402                                     (mail-header-id (caar thread)))))
3403                         roots)
3404             (push thr roots))
3405           (setq thread (cdr thread)))
3406         ;; We now have all (unique) roots.
3407         (if (= (length roots) 1)
3408             ;; All the loose roots are now one solid root.
3409             (setq thread (car roots))
3410           (setq thread (cons subject (gnus-sort-threads roots))))))
3411     (let (threads)
3412       ;; We then insert this thread into the summary buffer.
3413       (when line
3414         (goto-char (point-min))
3415         (forward-line (1- line)))
3416       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3417         (if gnus-show-threads
3418             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3419           (gnus-summary-prepare-unthreaded thread))
3420         (setq data (nreverse gnus-newsgroup-data))
3421         (setq threads gnus-newsgroup-threads))
3422       ;; We splice the new data into the data structure.
3423       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3424       ;;!!! then we want to insert at the beginning of the buffer.
3425       ;;!!! That happens to be true with Gnus now, but that may
3426       ;;!!! change in the future.  Perhaps.
3427       (gnus-data-enter-list
3428        (if line nil current) data (- (point) old-pos))
3429       (setq gnus-newsgroup-threads
3430             (nconc threads gnus-newsgroup-threads))
3431       (gnus-data-compute-positions))))
3432
3433 (defun gnus-number-to-header (number)
3434   "Return the header for article NUMBER."
3435   (let ((headers gnus-newsgroup-headers))
3436     (while (and headers
3437                 (not (= number (mail-header-number (car headers)))))
3438       (pop headers))
3439     (when headers
3440       (car headers))))
3441
3442 (defun gnus-parent-headers (in-headers &optional generation)
3443   "Return the headers of the GENERATIONeth parent of HEADERS."
3444   (unless generation
3445     (setq generation 1))
3446   (let ((parent t)
3447         (headers in-headers)
3448         references)
3449     (while (and parent
3450                 (not (zerop generation))
3451                 (setq references (mail-header-references headers)))
3452       (setq headers (if (and references
3453                              (setq parent (gnus-parent-id references)))
3454                         (car (gnus-id-to-thread parent))
3455                       nil))
3456       (decf generation))
3457     (and (not (eq headers in-headers))
3458          headers)))
3459
3460 (defun gnus-id-to-thread (id)
3461   "Return the (sub-)thread where ID appears."
3462   (gnus-gethash id gnus-newsgroup-dependencies))
3463
3464 (defun gnus-id-to-article (id)
3465   "Return the article number of ID."
3466   (let ((thread (gnus-id-to-thread id)))
3467     (when (and thread
3468                (car thread))
3469       (mail-header-number (car thread)))))
3470
3471 (defun gnus-id-to-header (id)
3472   "Return the article headers of ID."
3473   (car (gnus-id-to-thread id)))
3474
3475 (defun gnus-article-displayed-root-p (article)
3476   "Say whether ARTICLE is a root(ish) article."
3477   (let ((level (gnus-summary-thread-level article))
3478         (refs (mail-header-references  (gnus-summary-article-header article)))
3479         particle)
3480     (cond
3481      ((null level) nil)
3482      ((zerop level) t)
3483      ((null refs) t)
3484      ((null (gnus-parent-id refs)) t)
3485      ((and (= 1 level)
3486            (null (setq particle (gnus-id-to-article
3487                                  (gnus-parent-id refs))))
3488            (null (gnus-summary-thread-level particle)))))))
3489
3490 (defun gnus-root-id (id)
3491   "Return the id of the root of the thread where ID appears."
3492   (let (last-id prev)
3493     (while (and id (setq prev (car (gnus-id-to-thread id))))
3494       (setq last-id id
3495             id (gnus-parent-id (mail-header-references prev))))
3496     last-id))
3497
3498 (defun gnus-articles-in-thread (thread)
3499   "Return the list of articles in THREAD."
3500   (cons (mail-header-number (car thread))
3501         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3502
3503 (defun gnus-remove-thread (id &optional dont-remove)
3504   "Remove the thread that has ID in it."
3505   (let (headers thread last-id)
3506     ;; First go up in this thread until we find the root.
3507     (setq last-id (gnus-root-id id)
3508           headers (message-flatten-list (gnus-id-to-thread last-id)))
3509     ;; We have now found the real root of this thread.  It might have
3510     ;; been gathered into some loose thread, so we have to search
3511     ;; through the threads to find the thread we wanted.
3512     (let ((threads gnus-newsgroup-threads)
3513           sub)
3514       (while threads
3515         (setq sub (car threads))
3516         (if (stringp (car sub))
3517             ;; This is a gathered thread, so we look at the roots
3518             ;; below it to find whether this article is in this
3519             ;; gathered root.
3520             (progn
3521               (setq sub (cdr sub))
3522               (while sub
3523                 (when (member (caar sub) headers)
3524                   (setq thread (car threads)
3525                         threads nil
3526                         sub nil))
3527                 (setq sub (cdr sub))))
3528           ;; It's an ordinary thread, so we check it.
3529           (when (eq (car sub) (car headers))
3530             (setq thread sub
3531                   threads nil)))
3532         (setq threads (cdr threads)))
3533       ;; If this article is in no thread, then it's a root.
3534       (if thread
3535           (unless dont-remove
3536             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3537         (setq thread (gnus-id-to-thread last-id)))
3538       (when thread
3539         (prog1
3540             thread                      ; We return this thread.
3541           (unless dont-remove
3542             (if (stringp (car thread))
3543                 (progn
3544                   ;; If we use dummy roots, then we have to remove the
3545                   ;; dummy root as well.
3546                   (when (eq gnus-summary-make-false-root 'dummy)
3547                     ;; We go to the dummy root by going to
3548                     ;; the first sub-"thread", and then one line up.
3549                     (gnus-summary-goto-article
3550                      (mail-header-number (caadr thread)))
3551                     (forward-line -1)
3552                     (gnus-delete-line)
3553                     (gnus-data-compute-positions))
3554                   (setq thread (cdr thread))
3555                   (while thread
3556                     (gnus-remove-thread-1 (car thread))
3557                     (setq thread (cdr thread))))
3558               (gnus-summary-show-all-threads)
3559               (gnus-remove-thread-1 thread))))))))
3560
3561 (defun gnus-remove-thread-1 (thread)
3562   "Remove the thread THREAD recursively."
3563   (let ((number (mail-header-number (pop thread)))
3564         d)
3565     (setq thread (reverse thread))
3566     (while thread
3567       (gnus-remove-thread-1 (pop thread)))
3568     (when (setq d (gnus-data-find number))
3569       (goto-char (gnus-data-pos d))
3570       (gnus-data-remove
3571        number
3572        (- (gnus-point-at-bol)
3573           (prog1
3574               (1+ (gnus-point-at-eol))
3575             (gnus-delete-line)))))))
3576
3577 (defun gnus-sort-threads (threads)
3578   "Sort THREADS."
3579   (if (not gnus-thread-sort-functions)
3580       threads
3581     (gnus-message 8 "Sorting threads...")
3582     (prog1
3583         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3584       (gnus-message 8 "Sorting threads...done"))))
3585
3586 (defun gnus-sort-articles (articles)
3587   "Sort ARTICLES."
3588   (when gnus-article-sort-functions
3589     (gnus-message 7 "Sorting articles...")
3590     (prog1
3591         (setq gnus-newsgroup-headers
3592               (sort articles (gnus-make-sort-function
3593                               gnus-article-sort-functions)))
3594       (gnus-message 7 "Sorting articles...done"))))
3595
3596 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3597 (defmacro gnus-thread-header (thread)
3598   ;; Return header of first article in THREAD.
3599   ;; Note that THREAD must never, ever be anything else than a variable -
3600   ;; using some other form will lead to serious barfage.
3601   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3602   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3603   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3604         (vector thread) 2))
3605
3606 (defsubst gnus-article-sort-by-number (h1 h2)
3607   "Sort articles by article number."
3608   (< (mail-header-number h1)
3609      (mail-header-number h2)))
3610
3611 (defun gnus-thread-sort-by-number (h1 h2)
3612   "Sort threads by root article number."
3613   (gnus-article-sort-by-number
3614    (gnus-thread-header h1) (gnus-thread-header h2)))
3615
3616 (defsubst gnus-article-sort-by-lines (h1 h2)
3617   "Sort articles by article Lines header."
3618   (< (mail-header-lines h1)
3619      (mail-header-lines h2)))
3620
3621 (defun gnus-thread-sort-by-lines (h1 h2)
3622   "Sort threads by root article Lines header."
3623   (gnus-article-sort-by-lines
3624    (gnus-thread-header h1) (gnus-thread-header h2)))
3625
3626 (defsubst gnus-article-sort-by-chars (h1 h2)
3627   "Sort articles by octet length."
3628   (< (mail-header-chars h1)
3629      (mail-header-chars h2)))
3630
3631 (defun gnus-thread-sort-by-chars (h1 h2)
3632   "Sort threads by root article octet length."
3633   (gnus-article-sort-by-chars
3634    (gnus-thread-header h1) (gnus-thread-header h2)))
3635
3636 (defsubst gnus-article-sort-by-author (h1 h2)
3637   "Sort articles by root author."
3638   (string-lessp
3639    (let ((extract (funcall
3640                    gnus-extract-address-components
3641                    (mail-header-from h1))))
3642      (or (car extract) (cadr extract) ""))
3643    (let ((extract (funcall
3644                    gnus-extract-address-components
3645                    (mail-header-from h2))))
3646      (or (car extract) (cadr extract) ""))))
3647
3648 (defun gnus-thread-sort-by-author (h1 h2)
3649   "Sort threads by root author."
3650   (gnus-article-sort-by-author
3651    (gnus-thread-header h1)  (gnus-thread-header h2)))
3652
3653 (defsubst gnus-article-sort-by-subject (h1 h2)
3654   "Sort articles by root subject."
3655   (string-lessp
3656    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3657    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3658
3659 (defun gnus-thread-sort-by-subject (h1 h2)
3660   "Sort threads by root subject."
3661   (gnus-article-sort-by-subject
3662    (gnus-thread-header h1) (gnus-thread-header h2)))
3663
3664 (defsubst gnus-article-sort-by-date (h1 h2)
3665   "Sort articles by root article date."
3666   (time-less-p
3667    (gnus-date-get-time (mail-header-date h1))
3668    (gnus-date-get-time (mail-header-date h2))))
3669
3670 (defun gnus-thread-sort-by-date (h1 h2)
3671   "Sort threads by root article date."
3672   (gnus-article-sort-by-date
3673    (gnus-thread-header h1) (gnus-thread-header h2)))
3674
3675 (defsubst gnus-article-sort-by-score (h1 h2)
3676   "Sort articles by root article score.
3677 Unscored articles will be counted as having a score of zero."
3678   (> (or (cdr (assq (mail-header-number h1)
3679                     gnus-newsgroup-scored))
3680          gnus-summary-default-score 0)
3681      (or (cdr (assq (mail-header-number h2)
3682                     gnus-newsgroup-scored))
3683          gnus-summary-default-score 0)))
3684
3685 (defun gnus-thread-sort-by-score (h1 h2)
3686   "Sort threads by root article score."
3687   (gnus-article-sort-by-score
3688    (gnus-thread-header h1) (gnus-thread-header h2)))
3689
3690 (defun gnus-thread-sort-by-total-score (h1 h2)
3691   "Sort threads by the sum of all scores in the thread.
3692 Unscored articles will be counted as having a score of zero."
3693   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3694
3695 (defun gnus-thread-total-score (thread)
3696   ;; This function find the total score of THREAD.
3697   (cond ((null thread)
3698          0)
3699         ((consp thread)
3700          (if (stringp (car thread))
3701              (apply gnus-thread-score-function 0
3702                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3703            (gnus-thread-total-score-1 thread)))
3704         (t
3705          (gnus-thread-total-score-1 (list thread)))))
3706
3707 (defun gnus-thread-total-score-1 (root)
3708   ;; This function find the total score of the thread below ROOT.
3709   (setq root (car root))
3710   (apply gnus-thread-score-function
3711          (or (append
3712               (mapcar 'gnus-thread-total-score
3713                       (cdr (gnus-id-to-thread (mail-header-id root))))
3714               (when (> (mail-header-number root) 0)
3715                 (list (or (cdr (assq (mail-header-number root)
3716                                      gnus-newsgroup-scored))
3717                           gnus-summary-default-score 0))))
3718              (list gnus-summary-default-score)
3719              '(0))))
3720
3721 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3722 (defvar gnus-tmp-prev-subject nil)
3723 (defvar gnus-tmp-false-parent nil)
3724 (defvar gnus-tmp-root-expunged nil)
3725 (defvar gnus-tmp-dummy-line nil)
3726
3727 (defvar gnus-tmp-header)
3728 (defun gnus-extra-header (type &optional header)
3729   "Return the extra header of TYPE."
3730   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3731       ""))
3732
3733 (defun gnus-summary-prepare-threads (threads)
3734   "Prepare summary buffer from THREADS and indentation LEVEL.
3735 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3736 or a straight list of headers."
3737   (gnus-message 7 "Generating summary...")
3738
3739   (setq gnus-newsgroup-threads threads)
3740   (beginning-of-line)
3741
3742   (let ((gnus-tmp-level 0)
3743         (default-score (or gnus-summary-default-score 0))
3744         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3745         thread number subject stack state gnus-tmp-gathered beg-match
3746         new-roots gnus-tmp-new-adopts thread-end
3747         gnus-tmp-header gnus-tmp-unread
3748         gnus-tmp-replied gnus-tmp-subject-or-nil
3749         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3750         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3751         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3752
3753     (setq gnus-tmp-prev-subject nil)
3754
3755     (if (vectorp (car threads))
3756         ;; If this is a straight (sic) list of headers, then a
3757         ;; threaded summary display isn't required, so we just create
3758         ;; an unthreaded one.
3759         (gnus-summary-prepare-unthreaded threads)
3760
3761       ;; Do the threaded display.
3762
3763       (while (or threads stack gnus-tmp-new-adopts new-roots)
3764
3765         (if (and (= gnus-tmp-level 0)
3766                  (or (not stack)
3767                      (= (caar stack) 0))
3768                  (not gnus-tmp-false-parent)
3769                  (or gnus-tmp-new-adopts new-roots))
3770             (if gnus-tmp-new-adopts
3771                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3772                       thread (list (car gnus-tmp-new-adopts))
3773                       gnus-tmp-header (caar thread)
3774                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3775               (when new-roots
3776                 (setq thread (list (car new-roots))
3777                       gnus-tmp-header (caar thread)
3778                       new-roots (cdr new-roots))))
3779
3780           (if threads
3781               ;; If there are some threads, we do them before the
3782               ;; threads on the stack.
3783               (setq thread threads
3784                     gnus-tmp-header (caar thread))
3785             ;; There were no current threads, so we pop something off
3786             ;; the stack.
3787             (setq state (car stack)
3788                   gnus-tmp-level (car state)
3789                   thread (cdr state)
3790                   stack (cdr stack)
3791                   gnus-tmp-header (caar thread))))
3792
3793         (setq gnus-tmp-false-parent nil)
3794         (setq gnus-tmp-root-expunged nil)
3795         (setq thread-end nil)
3796
3797         (if (stringp gnus-tmp-header)
3798             ;; The header is a dummy root.
3799             (cond
3800              ((eq gnus-summary-make-false-root 'adopt)
3801               ;; We let the first article adopt the rest.
3802               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3803                                                (cddar thread)))
3804               (setq gnus-tmp-gathered
3805                     (nconc (mapcar
3806                             (lambda (h) (mail-header-number (car h)))
3807                             (cddar thread))
3808                            gnus-tmp-gathered))
3809               (setq thread (cons (list (caar thread)
3810                                        (cadar thread))
3811                                  (cdr thread)))
3812               (setq gnus-tmp-level -1
3813                     gnus-tmp-false-parent t))
3814              ((eq gnus-summary-make-false-root 'empty)
3815               ;; We print adopted articles with empty subject fields.
3816               (setq gnus-tmp-gathered
3817                     (nconc (mapcar
3818                             (lambda (h) (mail-header-number (car h)))
3819                             (cddar thread))
3820                            gnus-tmp-gathered))
3821               (setq gnus-tmp-level -1))
3822              ((eq gnus-summary-make-false-root 'dummy)
3823               ;; We remember that we probably want to output a dummy
3824               ;; root.
3825               (setq gnus-tmp-dummy-line gnus-tmp-header)
3826               (setq gnus-tmp-prev-subject gnus-tmp-header))
3827              (t
3828               ;; We do not make a root for the gathered
3829               ;; sub-threads at all.
3830               (setq gnus-tmp-level -1)))
3831
3832           (setq number (mail-header-number gnus-tmp-header)
3833                 subject (mail-header-subject gnus-tmp-header))
3834
3835           (cond
3836            ;; If the thread has changed subject, we might want to make
3837            ;; this subthread into a root.
3838            ((and (null gnus-thread-ignore-subject)
3839                  (not (zerop gnus-tmp-level))
3840                  gnus-tmp-prev-subject
3841                  (not (inline
3842                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3843             (setq new-roots (nconc new-roots (list (car thread)))
3844                   thread-end t
3845                   gnus-tmp-header nil))
3846            ;; If the article lies outside the current limit,
3847            ;; then we do not display it.
3848            ((not (memq number gnus-newsgroup-limit))
3849             (setq gnus-tmp-gathered
3850                   (nconc (mapcar
3851                           (lambda (h) (mail-header-number (car h)))
3852                           (cdar thread))
3853                          gnus-tmp-gathered))
3854             (setq gnus-tmp-new-adopts (if (cdar thread)
3855                                           (append gnus-tmp-new-adopts
3856                                                   (cdar thread))
3857                                         gnus-tmp-new-adopts)
3858                   thread-end t
3859                   gnus-tmp-header nil)
3860             (when (zerop gnus-tmp-level)
3861               (setq gnus-tmp-root-expunged t)))
3862            ;; Perhaps this article is to be marked as read?
3863            ((and gnus-summary-mark-below
3864                  (< (or (cdr (assq number gnus-newsgroup-scored))
3865                         default-score)
3866                     gnus-summary-mark-below)
3867                  ;; Don't touch sparse articles.
3868                  (not (gnus-summary-article-sparse-p number))
3869                  (not (gnus-summary-article-ancient-p number)))
3870             (setq gnus-newsgroup-unreads
3871                   (delq number gnus-newsgroup-unreads))
3872             (if gnus-newsgroup-auto-expire
3873                 (push number gnus-newsgroup-expirable)
3874               (push (cons number gnus-low-score-mark)
3875                     gnus-newsgroup-reads))))
3876
3877           (when gnus-tmp-header
3878             ;; We may have an old dummy line to output before this
3879             ;; article.
3880             (when (and gnus-tmp-dummy-line
3881                        (gnus-subject-equal
3882                         gnus-tmp-dummy-line
3883                         (mail-header-subject gnus-tmp-header)))
3884               (gnus-summary-insert-dummy-line
3885                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3886               (setq gnus-tmp-dummy-line nil))
3887
3888             ;; Compute the mark.
3889             (setq gnus-tmp-unread (gnus-article-mark number))
3890
3891             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3892                                   gnus-tmp-header gnus-tmp-level)
3893                   gnus-newsgroup-data)
3894
3895             ;; Actually insert the line.
3896             (setq
3897              gnus-tmp-subject-or-nil
3898              (cond
3899               ((and gnus-thread-ignore-subject
3900                     gnus-tmp-prev-subject
3901                     (not (inline (gnus-subject-equal
3902                                   gnus-tmp-prev-subject subject))))
3903                subject)
3904               ((zerop gnus-tmp-level)
3905                (if (and (eq gnus-summary-make-false-root 'empty)
3906                         (memq number gnus-tmp-gathered)
3907                         gnus-tmp-prev-subject
3908                         (inline (gnus-subject-equal
3909                                  gnus-tmp-prev-subject subject)))
3910                    gnus-summary-same-subject
3911                  subject))
3912               (t gnus-summary-same-subject)))
3913             (if (and (eq gnus-summary-make-false-root 'adopt)
3914                      (= gnus-tmp-level 1)
3915                      (memq number gnus-tmp-gathered))
3916                 (setq gnus-tmp-opening-bracket ?\<
3917                       gnus-tmp-closing-bracket ?\>)
3918               (setq gnus-tmp-opening-bracket ?\[
3919                     gnus-tmp-closing-bracket ?\]))
3920             (setq
3921              gnus-tmp-indentation
3922              (aref gnus-thread-indent-array gnus-tmp-level)
3923              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3924              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3925                                 gnus-summary-default-score 0)
3926              gnus-tmp-score-char
3927              (if (or (null gnus-summary-default-score)
3928                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3929                          gnus-summary-zcore-fuzz))
3930                  ? ;Whitespace
3931                (if (< gnus-tmp-score gnus-summary-default-score)
3932                    gnus-score-below-mark gnus-score-over-mark))
3933              gnus-tmp-replied
3934              (cond ((memq number gnus-newsgroup-processable)
3935                     gnus-process-mark)
3936                    ((memq number gnus-newsgroup-cached)
3937                     gnus-cached-mark)
3938                    ((memq number gnus-newsgroup-replied)
3939                     gnus-replied-mark)
3940                    ((memq number gnus-newsgroup-saved)
3941                     gnus-saved-mark)
3942                    (t gnus-unread-mark))
3943              gnus-tmp-from (mail-header-from gnus-tmp-header)
3944              gnus-tmp-name
3945              (cond
3946               ((string-match "<[^>]+> *$" gnus-tmp-from)
3947                (setq beg-match (match-beginning 0))
3948                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3949                         (substring gnus-tmp-from (1+ (match-beginning 0))
3950                                    (1- (match-end 0))))
3951                    (substring gnus-tmp-from 0 beg-match)))
3952               ((string-match "(.+)" gnus-tmp-from)
3953                (substring gnus-tmp-from
3954                           (1+ (match-beginning 0)) (1- (match-end 0))))
3955               (t gnus-tmp-from)))
3956             (when (string= gnus-tmp-name "")
3957               (setq gnus-tmp-name gnus-tmp-from))
3958             (unless (numberp gnus-tmp-lines)
3959               (setq gnus-tmp-lines 0))
3960             (gnus-put-text-property
3961              (point)
3962              (progn (eval gnus-summary-line-format-spec) (point))
3963              'gnus-number number)
3964             (when gnus-visual-p
3965               (forward-line -1)
3966               (gnus-run-hooks 'gnus-summary-update-hook)
3967               (forward-line 1))
3968
3969             (setq gnus-tmp-prev-subject subject)))
3970
3971         (when (nth 1 thread)
3972           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3973         (incf gnus-tmp-level)
3974         (setq threads (if thread-end nil (cdar thread)))
3975         (unless threads
3976           (setq gnus-tmp-level 0)))))
3977   (gnus-message 7 "Generating summary...done"))
3978
3979 (defun gnus-summary-prepare-unthreaded (headers)
3980   "Generate an unthreaded summary buffer based on HEADERS."
3981   (let (header number mark)
3982
3983     (beginning-of-line)
3984
3985     (while headers
3986       ;; We may have to root out some bad articles...
3987       (when (memq (setq number (mail-header-number
3988                                 (setq header (pop headers))))
3989                   gnus-newsgroup-limit)
3990         ;; Mark article as read when it has a low score.
3991         (when (and gnus-summary-mark-below
3992                    (< (or (cdr (assq number gnus-newsgroup-scored))
3993                           gnus-summary-default-score 0)
3994                       gnus-summary-mark-below)
3995                    (not (gnus-summary-article-ancient-p number)))
3996           (setq gnus-newsgroup-unreads
3997                 (delq number gnus-newsgroup-unreads))
3998           (if gnus-newsgroup-auto-expire
3999               (push number gnus-newsgroup-expirable)
4000             (push (cons number gnus-low-score-mark)
4001                   gnus-newsgroup-reads)))
4002
4003         (setq mark (gnus-article-mark number))
4004         (push (gnus-data-make number mark (1+ (point)) header 0)
4005               gnus-newsgroup-data)
4006         (gnus-summary-insert-line
4007          header 0 number
4008          mark (memq number gnus-newsgroup-replied)
4009          (memq number gnus-newsgroup-expirable)
4010          (mail-header-subject header) nil
4011          (cdr (assq number gnus-newsgroup-scored))
4012          (memq number gnus-newsgroup-processable))))))
4013
4014 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4015   "Select newsgroup GROUP.
4016 If READ-ALL is non-nil, all articles in the group are selected.
4017 If SELECT-ARTICLES, only select those articles from GROUP."
4018   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4019          ;;!!! Dirty hack; should be removed.
4020          (gnus-summary-ignore-duplicates
4021           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4022               t
4023             gnus-summary-ignore-duplicates))
4024          (info (nth 2 entry))
4025          articles fetched-articles cached)
4026
4027     (unless (gnus-check-server
4028              (setq gnus-current-select-method
4029                    (gnus-find-method-for-group group)))
4030       (error "Couldn't open server"))
4031
4032     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4033         (gnus-activate-group group)     ; Or we can activate it...
4034         (progn                          ; Or we bug out.
4035           (when (equal major-mode 'gnus-summary-mode)
4036             (kill-buffer (current-buffer)))
4037           (error "Couldn't request group %s: %s"
4038                  group (gnus-status-message group))))
4039
4040     (unless (gnus-request-group group t)
4041       (when (equal major-mode 'gnus-summary-mode)
4042         (kill-buffer (current-buffer)))
4043       (error "Couldn't request group %s: %s"
4044              group (gnus-status-message group)))
4045
4046     (setq gnus-newsgroup-name group)
4047     (setq gnus-newsgroup-unselected nil)
4048     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4049     (gnus-summary-setup-default-charset)
4050
4051     ;; Adjust and set lists of article marks.
4052     (when info
4053       (gnus-adjust-marked-articles info))
4054
4055     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4056     (when (gnus-virtual-group-p group)
4057       (setq cached gnus-newsgroup-cached))
4058
4059     (setq gnus-newsgroup-unreads
4060           (gnus-set-difference
4061            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4062            gnus-newsgroup-dormant))
4063
4064     (setq gnus-newsgroup-processable nil)
4065
4066     (gnus-update-read-articles group gnus-newsgroup-unreads)
4067
4068     (if (setq articles select-articles)
4069         (setq gnus-newsgroup-unselected
4070               (gnus-sorted-intersection
4071                gnus-newsgroup-unreads
4072                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4073       (setq articles (gnus-articles-to-read group read-all)))
4074
4075     (cond
4076      ((null articles)
4077       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4078       'quit)
4079      ((eq articles 0) nil)
4080      (t
4081       ;; Init the dependencies hash table.
4082       (setq gnus-newsgroup-dependencies
4083             (gnus-make-hashtable (length articles)))
4084       (gnus-set-global-variables)
4085       ;; Retrieve the headers and read them in.
4086       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4087       (setq gnus-newsgroup-headers
4088             (if (eq 'nov
4089                     (setq gnus-headers-retrieved-by
4090                           (gnus-retrieve-headers
4091                            articles gnus-newsgroup-name
4092                            ;; We might want to fetch old headers, but
4093                            ;; not if there is only 1 article.
4094                            (and (or (and
4095                                      (not (eq gnus-fetch-old-headers 'some))
4096                                      (not (numberp gnus-fetch-old-headers)))
4097                                     (> (length articles) 1))
4098                                 gnus-fetch-old-headers))))
4099                 (gnus-get-newsgroup-headers-xover
4100                  articles nil nil gnus-newsgroup-name t)
4101               (gnus-get-newsgroup-headers)))
4102       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4103
4104       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4105       (when cached
4106         (setq gnus-newsgroup-cached cached))
4107
4108       ;; Suppress duplicates?
4109       (when gnus-suppress-duplicates
4110         (gnus-dup-suppress-articles))
4111
4112       ;; Set the initial limit.
4113       (setq gnus-newsgroup-limit (copy-sequence articles))
4114       ;; Remove canceled articles from the list of unread articles.
4115       (setq gnus-newsgroup-unreads
4116             (gnus-set-sorted-intersection
4117              gnus-newsgroup-unreads
4118              (setq fetched-articles
4119                    (mapcar (lambda (headers) (mail-header-number headers))
4120                            gnus-newsgroup-headers))))
4121       ;; Removed marked articles that do not exist.
4122       (gnus-update-missing-marks
4123        (gnus-sorted-complement fetched-articles articles))
4124       ;; We might want to build some more threads first.
4125       (when (and gnus-fetch-old-headers
4126                  (eq gnus-headers-retrieved-by 'nov))
4127         (if (eq gnus-fetch-old-headers 'invisible)
4128             (gnus-build-all-threads)
4129           (gnus-build-old-threads)))
4130       ;; Let the Gnus agent mark articles as read.
4131       (when gnus-agent
4132         (gnus-agent-get-undownloaded-list))
4133       ;; Check whether auto-expire is to be done in this group.
4134       (setq gnus-newsgroup-auto-expire
4135             (gnus-group-auto-expirable-p group))
4136       ;; Set up the article buffer now, if necessary.
4137       (unless gnus-single-article-buffer
4138         (gnus-article-setup-buffer))
4139       ;; First and last article in this newsgroup.
4140       (when gnus-newsgroup-headers
4141         (setq gnus-newsgroup-begin
4142               (mail-header-number (car gnus-newsgroup-headers))
4143               gnus-newsgroup-end
4144               (mail-header-number
4145                (gnus-last-element gnus-newsgroup-headers))))
4146       ;; GROUP is successfully selected.
4147       (or gnus-newsgroup-headers t)))))
4148
4149 (defun gnus-articles-to-read (group &optional read-all)
4150   ;; Find out what articles the user wants to read.
4151   (let* ((articles
4152           ;; Select all articles if `read-all' is non-nil, or if there
4153           ;; are no unread articles.
4154           (if (or read-all
4155                   (and (zerop (length gnus-newsgroup-marked))
4156                        (zerop (length gnus-newsgroup-unreads)))
4157                   (eq (gnus-group-find-parameter group 'display)
4158                       'all))
4159               (gnus-uncompress-range (gnus-active group))
4160             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4161                           (copy-sequence gnus-newsgroup-unreads))
4162                   '<)))
4163          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4164          (scored (length scored-list))
4165          (number (length articles))
4166          (marked (+ (length gnus-newsgroup-marked)
4167                     (length gnus-newsgroup-dormant)))
4168          (select
4169           (cond
4170            ((numberp read-all)
4171             read-all)
4172            (t
4173             (condition-case ()
4174                 (cond
4175                  ((and (or (<= scored marked) (= scored number))
4176                        (numberp gnus-large-newsgroup)
4177                        (> number gnus-large-newsgroup))
4178                   (let ((input
4179                          (read-string
4180                           (format
4181                            "How many articles from %s (default %d): "
4182                            (gnus-limit-string gnus-newsgroup-name 35)
4183                            number))))
4184                     (if (string-match "^[ \t]*$" input) number input)))
4185                  ((and (> scored marked) (< scored number)
4186                        (> (- scored number) 20))
4187                   (let ((input
4188                          (read-string
4189                           (format "%s %s (%d scored, %d total): "
4190                                   "How many articles from"
4191                                   group scored number))))
4192                     (if (string-match "^[ \t]*$" input)
4193                         number input)))
4194                  (t number))
4195               (quit nil))))))
4196     (setq select (if (stringp select) (string-to-number select) select))
4197     (if (or (null select) (zerop select))
4198         select
4199       (if (and (not (zerop scored)) (<= (abs select) scored))
4200           (progn
4201             (setq articles (sort scored-list '<))
4202             (setq number (length articles)))
4203         (setq articles (copy-sequence articles)))
4204
4205       (when (< (abs select) number)
4206         (if (< select 0)
4207             ;; Select the N oldest articles.
4208             (setcdr (nthcdr (1- (abs select)) articles) nil)
4209           ;; Select the N most recent articles.
4210           (setq articles (nthcdr (- number select) articles))))
4211       (setq gnus-newsgroup-unselected
4212             (gnus-sorted-intersection
4213              gnus-newsgroup-unreads
4214              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4215       articles)))
4216
4217 (defun gnus-killed-articles (killed articles)
4218   (let (out)
4219     (while articles
4220       (when (inline (gnus-member-of-range (car articles) killed))
4221         (push (car articles) out))
4222       (setq articles (cdr articles)))
4223     out))
4224
4225 (defun gnus-uncompress-marks (marks)
4226   "Uncompress the mark ranges in MARKS."
4227   (let ((uncompressed '(score bookmark))
4228         out)
4229     (while marks
4230       (if (memq (caar marks) uncompressed)
4231           (push (car marks) out)
4232         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4233       (setq marks (cdr marks)))
4234     out))
4235
4236 (defun gnus-adjust-marked-articles (info)
4237   "Set all article lists and remove all marks that are no longer valid."
4238   (let* ((marked-lists (gnus-info-marks info))
4239          (active (gnus-active (gnus-info-group info)))
4240          (min (car active))
4241          (max (cdr active))
4242          (types gnus-article-mark-lists)
4243          (uncompressed '(score bookmark killed))
4244          marks var articles article mark)
4245
4246     (while marked-lists
4247       (setq marks (pop marked-lists))
4248       (set (setq var (intern (format "gnus-newsgroup-%s"
4249                                      (car (rassq (setq mark (car marks))
4250                                                  types)))))
4251            (if (memq (car marks) uncompressed) (cdr marks)
4252              (gnus-uncompress-range (cdr marks))))
4253
4254       (setq articles (symbol-value var))
4255
4256       ;; All articles have to be subsets of the active articles.
4257       (cond
4258        ;; Adjust "simple" lists.
4259        ((memq mark '(tick dormant expire reply save))
4260         (while articles
4261           (when (or (< (setq article (pop articles)) min) (> article max))
4262             (set var (delq article (symbol-value var))))))
4263        ;; Adjust assocs.
4264        ((memq mark uncompressed)
4265         (when (not (listp (cdr (symbol-value var))))
4266           (set var (list (symbol-value var))))
4267         (when (not (listp (cdr articles)))
4268           (setq articles (list articles)))
4269         (while articles
4270           (when (or (not (consp (setq article (pop articles))))
4271                     (< (car article) min)
4272                     (> (car article) max))
4273             (set var (delq article (symbol-value var))))))))))
4274
4275 (defun gnus-update-missing-marks (missing)
4276   "Go through the list of MISSING articles and remove them from the mark lists."
4277   (when missing
4278     (let ((types gnus-article-mark-lists)
4279           var m)
4280       ;; Go through all types.
4281       (while types
4282         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4283         (when (symbol-value var)
4284           ;; This list has articles.  So we delete all missing articles
4285           ;; from it.
4286           (setq m missing)
4287           (while m
4288             (set var (delq (pop m) (symbol-value var)))))))))
4289
4290 (defun gnus-update-marks ()
4291   "Enter the various lists of marked articles into the newsgroup info list."
4292   (let ((types gnus-article-mark-lists)
4293         (info (gnus-get-info gnus-newsgroup-name))
4294         (uncompressed '(score bookmark killed))
4295         type list newmarked symbol delta-marks)
4296     (when info
4297       ;; Add all marks lists to the list of marks lists.
4298       (while (setq type (pop types))
4299         (setq list (symbol-value
4300                           (setq symbol
4301                                 (intern (format "gnus-newsgroup-%s"
4302                                                 (car type))))))
4303
4304         (when list
4305           ;; Get rid of the entries of the articles that have the
4306           ;; default score.
4307           (when (and (eq (cdr type) 'score)
4308                      gnus-save-score
4309                      list)
4310             (let* ((arts list)
4311                    (prev (cons nil list))
4312                    (all prev))
4313               (while arts
4314                 (if (or (not (consp (car arts)))
4315                         (= (cdar arts) gnus-summary-default-score))
4316                     (setcdr prev (cdr arts))
4317                   (setq prev arts))
4318                 (setq arts (cdr arts)))
4319               (setq list (cdr all)))))
4320
4321        (or (memq (cdr type) uncompressed)
4322            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4323        
4324        (when (gnus-check-backend-function 'request-set-mark
4325                                           gnus-newsgroup-name)
4326          ;; uncompressed:s are not proper flags (they are cons cells)
4327          ;; cache is a internal gnus flag
4328          (unless (memq (cdr type) (cons 'cache uncompressed))
4329            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4330                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4331                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4332              (if add
4333                  (push (list add 'add (list (cdr type))) delta-marks))
4334              (if del
4335                  (push (list del 'del (list (cdr type))) delta-marks)))))
4336           
4337         (when list
4338          (push (cons (cdr type) list) newmarked)))
4339
4340       (when delta-marks
4341         (unless (gnus-check-group gnus-newsgroup-name)
4342           (error "Can't open server for %s" gnus-newsgroup-name))
4343         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4344           
4345       ;; Enter these new marks into the info of the group.
4346       (if (nthcdr 3 info)
4347           (setcar (nthcdr 3 info) newmarked)
4348         ;; Add the marks lists to the end of the info.
4349         (when newmarked
4350           (setcdr (nthcdr 2 info) (list newmarked))))
4351
4352       ;; Cut off the end of the info if there's nothing else there.
4353       (let ((i 5))
4354         (while (and (> i 2)
4355                     (not (nth i info)))
4356           (when (nthcdr (decf i) info)
4357             (setcdr (nthcdr i info) nil)))))))
4358
4359 (defun gnus-set-mode-line (where)
4360   "This function sets the mode line of the article or summary buffers.
4361 If WHERE is `summary', the summary mode line format will be used."
4362   ;; Is this mode line one we keep updated?
4363   (when (and (memq where gnus-updated-mode-lines)
4364              (symbol-value
4365               (intern (format "gnus-%s-mode-line-format-spec" where))))
4366     (let (mode-string)
4367       (save-excursion
4368         ;; We evaluate this in the summary buffer since these
4369         ;; variables are buffer-local to that buffer.
4370         (set-buffer gnus-summary-buffer)
4371         ;; We bind all these variables that are used in the `eval' form
4372         ;; below.
4373         (let* ((mformat (symbol-value
4374                          (intern
4375                           (format "gnus-%s-mode-line-format-spec" where))))
4376                (gnus-tmp-group-name gnus-newsgroup-name)
4377                (gnus-tmp-article-number (or gnus-current-article 0))
4378                (gnus-tmp-unread gnus-newsgroup-unreads)
4379                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4380                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4381                (gnus-tmp-unread-and-unselected
4382                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4383                             (zerop gnus-tmp-unselected))
4384                        "")
4385                       ((zerop gnus-tmp-unselected)
4386                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4387                       (t (format "{%d(+%d) more}"
4388                                  gnus-tmp-unread-and-unticked
4389                                  gnus-tmp-unselected))))
4390                (gnus-tmp-subject
4391                 (if (and gnus-current-headers
4392                          (vectorp gnus-current-headers))
4393                     (gnus-mode-string-quote
4394                      (mail-header-subject gnus-current-headers))
4395                   ""))
4396                bufname-length max-len
4397                gnus-tmp-header);; passed as argument to any user-format-funcs
4398           (setq mode-string (eval mformat))
4399           (setq bufname-length (if (string-match "%b" mode-string)
4400                                    (- (length
4401                                        (buffer-name
4402                                         (if (eq where 'summary)
4403                                             nil
4404                                           (get-buffer gnus-article-buffer))))
4405                                       2)
4406                                  0))
4407           (setq max-len (max 4 (if gnus-mode-non-string-length
4408                                    (- (window-width)
4409                                       gnus-mode-non-string-length
4410                                       bufname-length)
4411                                  (length mode-string))))
4412           ;; We might have to chop a bit of the string off...
4413           (when (> (length mode-string) max-len)
4414             (setq mode-string
4415                   (concat (truncate-string-to-width mode-string (- max-len 3))
4416                           "...")))
4417           ;; Pad the mode string a bit.
4418           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4419       ;; Update the mode line.
4420       (setq mode-line-buffer-identification
4421             (gnus-mode-line-buffer-identification (list mode-string)))
4422       (set-buffer-modified-p t))))
4423
4424 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4425   "Go through the HEADERS list and add all Xrefs to a hash table.
4426 The resulting hash table is returned, or nil if no Xrefs were found."
4427   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4428          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4429          (xref-hashtb (gnus-make-hashtable))
4430          start group entry number xrefs header)
4431     (while headers
4432       (setq header (pop headers))
4433       (when (and (setq xrefs (mail-header-xref header))
4434                  (not (memq (setq number (mail-header-number header))
4435                             unreads)))
4436         (setq start 0)
4437         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4438           (setq start (match-end 0))
4439           (setq group (if prefix
4440                           (concat prefix (substring xrefs (match-beginning 1)
4441                                                     (match-end 1)))
4442                         (substring xrefs (match-beginning 1) (match-end 1))))
4443           (setq number
4444                 (string-to-int (substring xrefs (match-beginning 2)
4445                                           (match-end 2))))
4446           (if (setq entry (gnus-gethash group xref-hashtb))
4447               (setcdr entry (cons number (cdr entry)))
4448             (gnus-sethash group (cons number nil) xref-hashtb)))))
4449     (and start xref-hashtb)))
4450
4451 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4452   "Look through all the headers and mark the Xrefs as read."
4453   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4454         name entry info xref-hashtb idlist method nth4)
4455     (save-excursion
4456       (set-buffer gnus-group-buffer)
4457       (when (setq xref-hashtb
4458                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4459         (mapatoms
4460          (lambda (group)
4461            (unless (string= from-newsgroup (setq name (symbol-name group)))
4462              (setq idlist (symbol-value group))
4463              ;; Dead groups are not updated.
4464              (and (prog1
4465                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4466                             info (nth 2 entry))
4467                     (when (stringp (setq nth4 (gnus-info-method info)))
4468                       (setq nth4 (gnus-server-to-method nth4))))
4469                   ;; Only do the xrefs if the group has the same
4470                   ;; select method as the group we have just read.
4471                   (or (gnus-methods-equal-p
4472                        nth4 (gnus-find-method-for-group from-newsgroup))
4473                       virtual
4474                       (equal nth4 (setq method (gnus-find-method-for-group
4475                                                 from-newsgroup)))
4476                       (and (equal (car nth4) (car method))
4477                            (equal (nth 1 nth4) (nth 1 method))))
4478                   gnus-use-cross-reference
4479                   (or (not (eq gnus-use-cross-reference t))
4480                       virtual
4481                       ;; Only do cross-references on subscribed
4482                       ;; groups, if that is what is wanted.
4483                       (<= (gnus-info-level info) gnus-level-subscribed))
4484                   (gnus-group-make-articles-read name idlist))))
4485          xref-hashtb)))))
4486
4487 (defun gnus-compute-read-articles (group articles)
4488   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4489          (info (nth 2 entry))
4490          (active (gnus-active group))
4491          ninfo)
4492     (when entry
4493       ;; First peel off all invalid article numbers.
4494       (when active
4495         (let ((ids articles)
4496               id first)
4497           (while (setq id (pop ids))
4498             (when (and first (> id (cdr active)))
4499               ;; We'll end up in this situation in one particular
4500               ;; obscure situation.  If you re-scan a group and get
4501               ;; a new article that is cross-posted to a different
4502               ;; group that has not been re-scanned, you might get
4503               ;; crossposted article that has a higher number than
4504               ;; Gnus believes possible.  So we re-activate this
4505               ;; group as well.  This might mean doing the
4506               ;; crossposting thingy will *increase* the number
4507               ;; of articles in some groups.  Tsk, tsk.
4508               (setq active (or (gnus-activate-group group) active)))
4509             (when (or (> id (cdr active))
4510                       (< id (car active)))
4511               (setq articles (delq id articles))))))
4512       ;; If the read list is nil, we init it.
4513       (if (and active
4514                (null (gnus-info-read info))
4515                (> (car active) 1))
4516           (setq ninfo (cons 1 (1- (car active))))
4517         (setq ninfo (gnus-info-read info)))
4518       ;; Then we add the read articles to the range.
4519       (gnus-add-to-range
4520        ninfo (setq articles (sort articles '<))))))
4521
4522 (defun gnus-group-make-articles-read (group articles)
4523   "Update the info of GROUP to say that ARTICLES are read."
4524   (let* ((num 0)
4525          (entry (gnus-gethash group gnus-newsrc-hashtb))
4526          (info (nth 2 entry))
4527          (active (gnus-active group))
4528          range)
4529     (when entry
4530       (setq range (gnus-compute-read-articles group articles))
4531       (save-excursion
4532         (set-buffer gnus-group-buffer)
4533         (gnus-undo-register
4534           `(progn
4535              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4536              (gnus-info-set-read ',info ',(gnus-info-read info))
4537              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4538              (gnus-group-update-group ,group t))))
4539       ;; Add the read articles to the range.
4540       (gnus-info-set-read info range)
4541       ;; Then we have to re-compute how many unread
4542       ;; articles there are in this group.
4543       (when active
4544         (cond
4545          ((not range)
4546           (setq num (- (1+ (cdr active)) (car active))))
4547          ((not (listp (cdr range)))
4548           (setq num (- (cdr active) (- (1+ (cdr range))
4549                                        (car range)))))
4550          (t
4551           (while range
4552             (if (numberp (car range))
4553                 (setq num (1+ num))
4554               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4555             (setq range (cdr range)))
4556           (setq num (- (cdr active) num))))
4557         ;; Update the number of unread articles.
4558         (setcar entry num)
4559         ;; Update the group buffer.
4560         (gnus-group-update-group group t)))))
4561
4562 (defvar gnus-newsgroup-none-id 0)
4563
4564 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4565   (let ((cur nntp-server-buffer)
4566         (dependencies
4567          (or dependencies
4568              (save-excursion (set-buffer gnus-summary-buffer)
4569                              gnus-newsgroup-dependencies)))
4570         headers id end ref
4571         (mail-parse-charset gnus-newsgroup-charset)
4572         (mail-parse-ignored-charsets 
4573              (save-excursion (condition-case nil
4574                                  (set-buffer gnus-summary-buffer)
4575                                (error))
4576                              gnus-newsgroup-ignored-charsets)))
4577     (save-excursion
4578       (set-buffer nntp-server-buffer)
4579       ;; Translate all TAB characters into SPACE characters.
4580       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4581       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4582       (gnus-run-hooks 'gnus-parse-headers-hook)
4583       (let ((case-fold-search t)
4584             in-reply-to header p lines chars)
4585         (goto-char (point-min))
4586         ;; Search to the beginning of the next header.  Error messages
4587         ;; do not begin with 2 or 3.
4588         (while (re-search-forward "^[23][0-9]+ " nil t)
4589           (setq id nil
4590                 ref nil)
4591           ;; This implementation of this function, with nine
4592           ;; search-forwards instead of the one re-search-forward and
4593           ;; a case (which basically was the old function) is actually
4594           ;; about twice as fast, even though it looks messier.  You
4595           ;; can't have everything, I guess.  Speed and elegance
4596           ;; doesn't always go hand in hand.
4597           (setq
4598            header
4599            (vector
4600             ;; Number.
4601             (prog1
4602                 (read cur)
4603               (end-of-line)
4604               (setq p (point))
4605               (narrow-to-region (point)
4606                                 (or (and (search-forward "\n.\n" nil t)
4607                                          (- (point) 2))
4608                                     (point))))
4609             ;; Subject.
4610             (progn
4611               (goto-char p)
4612               (if (search-forward "\nsubject: " nil t)
4613                   (funcall gnus-decode-encoded-word-function
4614                            (nnheader-header-value))
4615                 "(none)"))
4616             ;; From.
4617             (progn
4618               (goto-char p)
4619               (if (search-forward "\nfrom: " nil t)
4620                   (funcall gnus-decode-encoded-word-function
4621                            (nnheader-header-value))
4622                 "(nobody)"))
4623             ;; Date.
4624             (progn
4625               (goto-char p)
4626               (if (search-forward "\ndate: " nil t)
4627                   (nnheader-header-value) ""))
4628             ;; Message-ID.
4629             (progn
4630               (goto-char p)
4631               (setq id (if (re-search-forward
4632                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4633                            ;; We do it this way to make sure the Message-ID
4634                            ;; is (somewhat) syntactically valid.
4635                            (buffer-substring (match-beginning 1)
4636                                              (match-end 1))
4637                          ;; If there was no message-id, we just fake one
4638                          ;; to make subsequent routines simpler.
4639                          (nnheader-generate-fake-message-id))))
4640             ;; References.
4641             (progn
4642               (goto-char p)
4643               (if (search-forward "\nreferences: " nil t)
4644                   (progn
4645                     (setq end (point))
4646                     (prog1
4647                         (nnheader-header-value)
4648                       (setq ref
4649                             (buffer-substring
4650                              (progn
4651                                (end-of-line)
4652                                (search-backward ">" end t)
4653                                (1+ (point)))
4654                              (progn
4655                                (search-backward "<" end t)
4656                                (point))))))
4657                 ;; Get the references from the in-reply-to header if there
4658                 ;; were no references and the in-reply-to header looks
4659                 ;; promising.
4660                 (if (and (search-forward "\nin-reply-to: " nil t)
4661                          (setq in-reply-to (nnheader-header-value))
4662                          (string-match "<[^>]+>" in-reply-to))
4663                     (let (ref2)
4664                       (setq ref (substring in-reply-to (match-beginning 0)
4665                                            (match-end 0)))
4666                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4667                         (setq ref2 (substring in-reply-to (match-beginning 0)
4668                                               (match-end 0)))
4669                         (when (> (length ref2) (length ref))
4670                           (setq ref ref2)))
4671                       ref)
4672                   (setq ref nil))))
4673             ;; Chars.
4674             (progn
4675               (goto-char p)
4676               (if (search-forward "\nchars: " nil t)
4677                   (if (numberp (setq chars (ignore-errors (read cur))))
4678                       chars 0)
4679                 0))
4680             ;; Lines.
4681             (progn
4682               (goto-char p)
4683               (if (search-forward "\nlines: " nil t)
4684                   (if (numberp (setq lines (ignore-errors (read cur))))
4685                       lines 0)
4686                 0))
4687             ;; Xref.
4688             (progn
4689               (goto-char p)
4690               (and (search-forward "\nxref: " nil t)
4691                    (nnheader-header-value)))
4692             ;; Extra.
4693             (when gnus-extra-headers
4694               (let ((extra gnus-extra-headers)
4695                     out)
4696                 (while extra
4697                   (goto-char p)
4698                   (when (search-forward
4699                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4700                     (push (cons (car extra) (nnheader-header-value))
4701                           out))
4702                   (pop extra))
4703                 out))))
4704           (when (equal id ref)
4705             (setq ref nil))
4706
4707           (when gnus-alter-header-function
4708             (funcall gnus-alter-header-function header)
4709             (setq id (mail-header-id header)
4710                   ref (gnus-parent-id (mail-header-references header))))
4711
4712           (when (setq header
4713                       (gnus-dependencies-add-header
4714                        header dependencies force-new))
4715             (push header headers))
4716           (goto-char (point-max))
4717           (widen))
4718         (nreverse headers)))))
4719
4720 ;; Goes through the xover lines and returns a list of vectors
4721 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4722                                                   force-new dependencies
4723                                                   group also-fetch-heads)
4724   "Parse the news overview data in the server buffer, and return a
4725 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4726   ;; Get the Xref when the users reads the articles since most/some
4727   ;; NNTP servers do not include Xrefs when using XOVER.
4728   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4729   (let ((mail-parse-charset gnus-newsgroup-charset)
4730         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4731         (cur nntp-server-buffer)
4732         (dependencies (or dependencies gnus-newsgroup-dependencies))
4733         number headers header)
4734     (save-excursion
4735       (set-buffer nntp-server-buffer)
4736       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4737       ;; Allow the user to mangle the headers before parsing them.
4738       (gnus-run-hooks 'gnus-parse-headers-hook)
4739       (goto-char (point-min))
4740       (while (not (eobp))
4741         (condition-case ()
4742             (while (and sequence (not (eobp)))
4743               (setq number (read cur))
4744               (while (and sequence
4745                           (< (car sequence) number))
4746                 (setq sequence (cdr sequence)))
4747               (and sequence
4748                    (eq number (car sequence))
4749                    (progn
4750                      (setq sequence (cdr sequence))
4751                      (setq header (inline
4752                                     (gnus-nov-parse-line
4753                                      number dependencies force-new))))
4754                    (push header headers))
4755               (forward-line 1))
4756           (error
4757            (gnus-error 4 "Strange nov line (%d)"
4758                        (count-lines (point-min) (point)))))
4759         (forward-line 1))
4760       ;; A common bug in inn is that if you have posted an article and
4761       ;; then retrieves the active file, it will answer correctly --
4762       ;; the new article is included.  However, a NOV entry for the
4763       ;; article may not have been generated yet, so this may fail.
4764       ;; We work around this problem by retrieving the last few
4765       ;; headers using HEAD.
4766       (if (or (not also-fetch-heads)
4767               (not sequence))
4768           ;; We (probably) got all the headers.
4769           (nreverse headers)
4770         (let ((gnus-nov-is-evil t))
4771           (nconc
4772            (nreverse headers)
4773            (when (gnus-retrieve-headers sequence group)
4774              (gnus-get-newsgroup-headers))))))))
4775
4776 (defun gnus-article-get-xrefs ()
4777   "Fill in the Xref value in `gnus-current-headers', if necessary.
4778 This is meant to be called in `gnus-article-internal-prepare-hook'."
4779   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4780                                  gnus-current-headers)))
4781     (or (not gnus-use-cross-reference)
4782         (not headers)
4783         (and (mail-header-xref headers)
4784              (not (string= (mail-header-xref headers) "")))
4785         (let ((case-fold-search t)
4786               xref)
4787           (save-restriction
4788             (nnheader-narrow-to-headers)
4789             (goto-char (point-min))
4790             (when (or (and (eq (downcase (char-after)) ?x)
4791                            (looking-at "Xref:"))
4792                       (search-forward "\nXref:" nil t))
4793               (goto-char (1+ (match-end 0)))
4794               (setq xref (buffer-substring (point)
4795                                            (progn (end-of-line) (point))))
4796               (mail-header-set-xref headers xref)))))))
4797
4798 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4799   "Find article ID and insert the summary line for that article.
4800 OLD-HEADER can either be a header or a line number to insert
4801 the subject line on."
4802   (let* ((line (and (numberp old-header) old-header))
4803          (old-header (and (vectorp old-header) old-header))
4804          (header (cond ((and old-header use-old-header)
4805                         old-header)
4806                        ((and (numberp id)
4807                              (gnus-number-to-header id))
4808                         (gnus-number-to-header id))
4809                        (t
4810                         (gnus-read-header id))))
4811          (number (and (numberp id) id))
4812          d)
4813     (when header
4814       ;; Rebuild the thread that this article is part of and go to the
4815       ;; article we have fetched.
4816       (when (and (not gnus-show-threads)
4817                  old-header)
4818         (when (and number
4819                    (setq d (gnus-data-find (mail-header-number old-header))))
4820           (goto-char (gnus-data-pos d))
4821           (gnus-data-remove
4822            number
4823            (- (gnus-point-at-bol)
4824               (prog1
4825                   (1+ (gnus-point-at-eol))
4826                 (gnus-delete-line))))))
4827       (when old-header
4828         (mail-header-set-number header (mail-header-number old-header)))
4829       (setq gnus-newsgroup-sparse
4830             (delq (setq number (mail-header-number header))
4831                   gnus-newsgroup-sparse))
4832       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4833       (push number gnus-newsgroup-limit)
4834       (gnus-rebuild-thread (mail-header-id header) line)
4835       (gnus-summary-goto-subject number nil t))
4836     (when (and (numberp number)
4837                (> number 0))
4838       ;; We have to update the boundaries even if we can't fetch the
4839       ;; article if ID is a number -- so that the next `P' or `N'
4840       ;; command will fetch the previous (or next) article even
4841       ;; if the one we tried to fetch this time has been canceled.
4842       (when (> number gnus-newsgroup-end)
4843         (setq gnus-newsgroup-end number))
4844       (when (< number gnus-newsgroup-begin)
4845         (setq gnus-newsgroup-begin number))
4846       (setq gnus-newsgroup-unselected
4847             (delq number gnus-newsgroup-unselected)))
4848     ;; Report back a success?
4849     (and header (mail-header-number header))))
4850
4851 ;;; Process/prefix in the summary buffer
4852
4853 (defun gnus-summary-work-articles (n)
4854   "Return a list of articles to be worked upon.
4855 The prefix argument, the list of process marked articles, and the
4856 current article will be taken into consideration."
4857   (save-excursion
4858     (set-buffer gnus-summary-buffer)
4859     (cond
4860      (n
4861       ;; A numerical prefix has been given.
4862       (setq n (prefix-numeric-value n))
4863       (let ((backward (< n 0))
4864             (n (abs (prefix-numeric-value n)))
4865             articles article)
4866         (save-excursion
4867           (while
4868               (and (> n 0)
4869                    (push (setq article (gnus-summary-article-number))
4870                          articles)
4871                    (if backward
4872                        (gnus-summary-find-prev nil article)
4873                      (gnus-summary-find-next nil article)))
4874             (decf n)))
4875         (nreverse articles)))
4876      ((and (gnus-region-active-p) (mark))
4877       (message "region active")
4878       ;; Work on the region between point and mark.
4879       (let ((max (max (point) (mark)))
4880             articles article)
4881         (save-excursion
4882           (goto-char (min (min (point) (mark))))
4883           (while
4884               (and
4885                (push (setq article (gnus-summary-article-number)) articles)
4886                (gnus-summary-find-next nil article)
4887                (< (point) max)))
4888           (nreverse articles))))
4889      (gnus-newsgroup-processable
4890       ;; There are process-marked articles present.
4891       ;; Save current state.
4892       (gnus-summary-save-process-mark)
4893       ;; Return the list.
4894       (reverse gnus-newsgroup-processable))
4895      (t
4896       ;; Just return the current article.
4897       (list (gnus-summary-article-number))))))
4898
4899 (defmacro gnus-summary-iterate (arg &rest forms)
4900   "Iterate over the process/prefixed articles and do FORMS.
4901 ARG is the interactive prefix given to the command.  FORMS will be
4902 executed with point over the summary line of the articles."
4903   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4904     `(let ((,articles (gnus-summary-work-articles ,arg)))
4905        (while ,articles
4906          (gnus-summary-goto-subject (car ,articles))
4907          ,@forms
4908          (pop ,articles)))))
4909
4910 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4911 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4912
4913 (defun gnus-summary-save-process-mark ()
4914   "Push the current set of process marked articles on the stack."
4915   (interactive)
4916   (push (copy-sequence gnus-newsgroup-processable)
4917         gnus-newsgroup-process-stack))
4918
4919 (defun gnus-summary-kill-process-mark ()
4920   "Push the current set of process marked articles on the stack and unmark."
4921   (interactive)
4922   (gnus-summary-save-process-mark)
4923   (gnus-summary-unmark-all-processable))
4924
4925 (defun gnus-summary-yank-process-mark ()
4926   "Pop the last process mark state off the stack and restore it."
4927   (interactive)
4928   (unless gnus-newsgroup-process-stack
4929     (error "Empty mark stack"))
4930   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4931
4932 (defun gnus-summary-process-mark-set (set)
4933   "Make SET into the current process marked articles."
4934   (gnus-summary-unmark-all-processable)
4935   (while set
4936     (gnus-summary-set-process-mark (pop set))))
4937
4938 ;;; Searching and stuff
4939
4940 (defun gnus-summary-search-group (&optional backward use-level)
4941   "Search for next unread newsgroup.
4942 If optional argument BACKWARD is non-nil, search backward instead."
4943   (save-excursion
4944     (set-buffer gnus-group-buffer)
4945     (when (gnus-group-search-forward
4946            backward nil (if use-level (gnus-group-group-level) nil))
4947       (gnus-group-group-name))))
4948
4949 (defun gnus-summary-best-group (&optional exclude-group)
4950   "Find the name of the best unread group.
4951 If EXCLUDE-GROUP, do not go to this group."
4952   (save-excursion
4953     (set-buffer gnus-group-buffer)
4954     (save-excursion
4955       (gnus-group-best-unread-group exclude-group))))
4956
4957 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4958   (if backward (gnus-summary-find-prev)
4959     (let* ((dummy (gnus-summary-article-intangible-p))
4960            (article (or article (gnus-summary-article-number)))
4961            (arts (gnus-data-find-list article))
4962            result)
4963       (when (and (not dummy)
4964                  (or (not gnus-summary-check-current)
4965                      (not unread)
4966                      (not (gnus-data-unread-p (car arts)))))
4967         (setq arts (cdr arts)))
4968       (when (setq result
4969                   (if unread
4970                       (progn
4971                         (while arts
4972                           (when (or (and undownloaded
4973                                          (eq gnus-undownloaded-mark
4974                                              (gnus-data-mark (car arts))))
4975                                     (gnus-data-unread-p (car arts)))
4976                             (setq result (car arts)
4977                                   arts nil))
4978                           (setq arts (cdr arts)))
4979                         result)
4980                     (car arts)))
4981         (goto-char (gnus-data-pos result))
4982         (gnus-data-number result)))))
4983
4984 (defun gnus-summary-find-prev (&optional unread article)
4985   (let* ((eobp (eobp))
4986          (article (or article (gnus-summary-article-number)))
4987          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4988          result)
4989     (when (and (not eobp)
4990                (or (not gnus-summary-check-current)
4991                    (not unread)
4992                    (not (gnus-data-unread-p (car arts)))))
4993       (setq arts (cdr arts)))
4994     (when (setq result
4995                 (if unread
4996                     (progn
4997                       (while arts
4998                         (when (gnus-data-unread-p (car arts))
4999                           (setq result (car arts)
5000                                 arts nil))
5001                         (setq arts (cdr arts)))
5002                       result)
5003                   (car arts)))
5004       (goto-char (gnus-data-pos result))
5005       (gnus-data-number result))))
5006
5007 (defun gnus-summary-find-subject (subject &optional unread backward article)
5008   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5009          (article (or article (gnus-summary-article-number)))
5010          (articles (gnus-data-list backward))
5011          (arts (gnus-data-find-list article articles))
5012          result)
5013     (when (or (not gnus-summary-check-current)
5014               (not unread)
5015               (not (gnus-data-unread-p (car arts))))
5016       (setq arts (cdr arts)))
5017     (while arts
5018       (and (or (not unread)
5019                (gnus-data-unread-p (car arts)))
5020            (vectorp (gnus-data-header (car arts)))
5021            (gnus-subject-equal
5022             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5023            (setq result (car arts)
5024                  arts nil))
5025       (setq arts (cdr arts)))
5026     (and result
5027          (goto-char (gnus-data-pos result))
5028          (gnus-data-number result))))
5029
5030 (defun gnus-summary-search-forward (&optional unread subject backward)
5031   "Search forward for an article.
5032 If UNREAD, look for unread articles.  If SUBJECT, look for
5033 articles with that subject.  If BACKWARD, search backward instead."
5034   (cond (subject (gnus-summary-find-subject subject unread backward))
5035         (backward (gnus-summary-find-prev unread))
5036         (t (gnus-summary-find-next unread))))
5037
5038 (defun gnus-recenter (&optional n)
5039   "Center point in window and redisplay frame.
5040 Also do horizontal recentering."
5041   (interactive "P")
5042   (when (and gnus-auto-center-summary
5043              (not (eq gnus-auto-center-summary 'vertical)))
5044     (gnus-horizontal-recenter))
5045   (recenter n))
5046
5047 (defun gnus-summary-recenter ()
5048   "Center point in the summary window.
5049 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5050 displayed, no centering will be performed."
5051   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5052   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5053   (let* ((top (cond ((< (window-height) 4) 0)
5054                     ((< (window-height) 7) 1)
5055                     (t (if (numberp gnus-auto-center-summary)
5056                            gnus-auto-center-summary
5057                          2))))
5058          (height (1- (window-height)))
5059          (bottom (save-excursion (goto-char (point-max))
5060                                  (forward-line (- height))
5061                                  (point)))
5062          (window (get-buffer-window (current-buffer))))
5063     ;; The user has to want it.
5064     (when gnus-auto-center-summary
5065       (when (get-buffer-window gnus-article-buffer)
5066         ;; Only do recentering when the article buffer is displayed,
5067         ;; Set the window start to either `bottom', which is the biggest
5068         ;; possible valid number, or the second line from the top,
5069         ;; whichever is the least.
5070         (set-window-start
5071          window (min bottom (save-excursion
5072                               (forward-line (- top)) (point)))))
5073       ;; Do horizontal recentering while we're at it.
5074       (when (and (get-buffer-window (current-buffer) t)
5075                  (not (eq gnus-auto-center-summary 'vertical)))
5076         (let ((selected (selected-window)))
5077           (select-window (get-buffer-window (current-buffer) t))
5078           (gnus-summary-position-point)
5079           (gnus-horizontal-recenter)
5080           (select-window selected))))))
5081
5082 (defun gnus-summary-jump-to-group (newsgroup)
5083   "Move point to NEWSGROUP in group mode buffer."
5084   ;; Keep update point of group mode buffer if visible.
5085   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5086       (save-window-excursion
5087         ;; Take care of tree window mode.
5088         (when (get-buffer-window gnus-group-buffer)
5089           (pop-to-buffer gnus-group-buffer))
5090         (gnus-group-jump-to-group newsgroup))
5091     (save-excursion
5092       ;; Take care of tree window mode.
5093       (if (get-buffer-window gnus-group-buffer)
5094           (pop-to-buffer gnus-group-buffer)
5095         (set-buffer gnus-group-buffer))
5096       (gnus-group-jump-to-group newsgroup))))
5097
5098 ;; This function returns a list of article numbers based on the
5099 ;; difference between the ranges of read articles in this group and
5100 ;; the range of active articles.
5101 (defun gnus-list-of-unread-articles (group)
5102   (let* ((read (gnus-info-read (gnus-get-info group)))
5103          (active (or (gnus-active group) (gnus-activate-group group)))
5104          (last (cdr active))
5105          first nlast unread)
5106     ;; If none are read, then all are unread.
5107     (if (not read)
5108         (setq first (car active))
5109       ;; If the range of read articles is a single range, then the
5110       ;; first unread article is the article after the last read
5111       ;; article.  Sounds logical, doesn't it?
5112       (if (not (listp (cdr read)))
5113           (setq first (max (car active) (1+ (cdr read))))
5114         ;; `read' is a list of ranges.
5115         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5116                                   (caar read)))
5117                   1)
5118           (setq first (car active)))
5119         (while read
5120           (when first
5121             (while (< first nlast)
5122               (push first unread)
5123               (setq first (1+ first))))
5124           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5125           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5126           (setq read (cdr read)))))
5127     ;; And add the last unread articles.
5128     (while (<= first last)
5129       (push first unread)
5130       (setq first (1+ first)))
5131     ;; Return the list of unread articles.
5132     (delq 0 (nreverse unread))))
5133
5134 (defun gnus-list-of-read-articles (group)
5135   "Return a list of unread, unticked and non-dormant articles."
5136   (let* ((info (gnus-get-info group))
5137          (marked (gnus-info-marks info))
5138          (active (gnus-active group)))
5139     (and info active
5140          (gnus-set-difference
5141           (gnus-sorted-complement
5142            (gnus-uncompress-range active)
5143            (gnus-list-of-unread-articles group))
5144           (append
5145            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5146            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5147
5148 ;; Various summary commands
5149
5150 (defun gnus-summary-select-article-buffer ()
5151   "Reconfigure windows to show article buffer."
5152   (interactive)
5153   (if (not (gnus-buffer-live-p gnus-article-buffer))
5154       (error "There is no article buffer for this summary buffer")
5155     (gnus-configure-windows 'article)
5156     (select-window (get-buffer-window gnus-article-buffer))))
5157
5158 (defun gnus-summary-universal-argument (arg)
5159   "Perform any operation on all articles that are process/prefixed."
5160   (interactive "P")
5161   (let ((articles (gnus-summary-work-articles arg))
5162         func article)
5163     (if (eq
5164          (setq
5165           func
5166           (key-binding
5167            (read-key-sequence
5168             (substitute-command-keys
5169              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5170              ))))
5171          'undefined)
5172         (gnus-error 1 "Undefined key")
5173       (save-excursion
5174         (while articles
5175           (gnus-summary-goto-subject (setq article (pop articles)))
5176           (let (gnus-newsgroup-processable)
5177             (command-execute func))
5178           (gnus-summary-remove-process-mark article)))))
5179   (gnus-summary-position-point))
5180
5181 (defun gnus-summary-toggle-truncation (&optional arg)
5182   "Toggle truncation of summary lines.
5183 With arg, turn line truncation on iff arg is positive."
5184   (interactive "P")
5185   (setq truncate-lines
5186         (if (null arg) (not truncate-lines)
5187           (> (prefix-numeric-value arg) 0)))
5188   (redraw-display))
5189
5190 (defun gnus-summary-reselect-current-group (&optional all rescan)
5191   "Exit and then reselect the current newsgroup.
5192 The prefix argument ALL means to select all articles."
5193   (interactive "P")
5194   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5195     (error "Ephemeral groups can't be reselected"))
5196   (let ((current-subject (gnus-summary-article-number))
5197         (group gnus-newsgroup-name))
5198     (setq gnus-newsgroup-begin nil)
5199     (gnus-summary-exit)
5200     ;; We have to adjust the point of group mode buffer because
5201     ;; point was moved to the next unread newsgroup by exiting.
5202     (gnus-summary-jump-to-group group)
5203     (when rescan
5204       (save-excursion
5205         (gnus-group-get-new-news-this-group 1)))
5206     (gnus-group-read-group all t)
5207     (gnus-summary-goto-subject current-subject nil t)))
5208
5209 (defun gnus-summary-rescan-group (&optional all)
5210   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5211   (interactive "P")
5212   (gnus-summary-reselect-current-group all t))
5213
5214 (defun gnus-summary-update-info (&optional non-destructive)
5215   (save-excursion
5216     (let ((group gnus-newsgroup-name))
5217       (when group
5218         (when gnus-newsgroup-kill-headers
5219           (setq gnus-newsgroup-killed
5220                 (gnus-compress-sequence
5221                  (nconc
5222                   (gnus-set-sorted-intersection
5223                    (gnus-uncompress-range gnus-newsgroup-killed)
5224                    (setq gnus-newsgroup-unselected
5225                          (sort gnus-newsgroup-unselected '<)))
5226                   (setq gnus-newsgroup-unreads
5227                         (sort gnus-newsgroup-unreads '<)))
5228                  t)))
5229         (unless (listp (cdr gnus-newsgroup-killed))
5230           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5231         (let ((headers gnus-newsgroup-headers))
5232           ;; Set the new ranges of read articles.
5233           (save-excursion
5234             (set-buffer gnus-group-buffer)
5235             (gnus-undo-force-boundary))
5236           (gnus-update-read-articles
5237            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5238           ;; Set the current article marks.
5239           (let ((gnus-newsgroup-scored
5240                  (if (and (not gnus-save-score)
5241                           (not non-destructive))
5242                      nil
5243                    gnus-newsgroup-scored)))
5244             (save-excursion
5245               (gnus-update-marks)))
5246           ;; Do the cross-ref thing.
5247           (when gnus-use-cross-reference
5248             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5249           ;; Do not switch windows but change the buffer to work.
5250           (set-buffer gnus-group-buffer)
5251           (unless (gnus-ephemeral-group-p group)
5252             (gnus-group-update-group group)))))))
5253
5254 (defun gnus-summary-save-newsrc (&optional force)
5255   "Save the current number of read/marked articles in the dribble buffer.
5256 The dribble buffer will then be saved.
5257 If FORCE (the prefix), also save the .newsrc file(s)."
5258   (interactive "P")
5259   (gnus-summary-update-info t)
5260   (if force
5261       (gnus-save-newsrc-file)
5262     (gnus-dribble-save)))
5263
5264 (defun gnus-summary-exit (&optional temporary)
5265   "Exit reading current newsgroup, and then return to group selection mode.
5266 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5267   (interactive)
5268   (gnus-set-global-variables)
5269   (when (gnus-buffer-live-p gnus-article-buffer)
5270     (save-excursion
5271       (set-buffer gnus-article-buffer)
5272       (mm-destroy-parts gnus-article-mime-handles)))
5273   (gnus-kill-save-kill-buffer)
5274   (gnus-async-halt-prefetch)
5275   (let* ((group gnus-newsgroup-name)
5276          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5277          (mode major-mode)
5278          (group-point nil)
5279          (buf (current-buffer)))
5280     (unless quit-config
5281       ;; Do adaptive scoring, and possibly save score files.
5282       (when gnus-newsgroup-adaptive
5283         (gnus-score-adaptive))
5284       (when gnus-use-scoring
5285         (gnus-score-save)))
5286     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5287     ;; If we have several article buffers, we kill them at exit.
5288     (unless gnus-single-article-buffer
5289       (gnus-kill-buffer gnus-original-article-buffer)
5290       (setq gnus-article-current nil))
5291     (when gnus-use-cache
5292       (gnus-cache-possibly-remove-articles)
5293       (gnus-cache-save-buffers))
5294     (gnus-async-prefetch-remove-group group)
5295     (when gnus-suppress-duplicates
5296       (gnus-dup-enter-articles))
5297     (when gnus-use-trees
5298       (gnus-tree-close group))
5299     ;; Remove entries for this group.
5300     (nnmail-purge-split-history (gnus-group-real-name group))
5301     ;; Make all changes in this group permanent.
5302     (unless quit-config
5303       (gnus-run-hooks 'gnus-exit-group-hook)
5304       (gnus-summary-update-info))
5305     (gnus-close-group group)
5306     ;; Make sure where we were, and go to next newsgroup.
5307     (set-buffer gnus-group-buffer)
5308     (unless quit-config
5309       (gnus-group-jump-to-group group))
5310     (gnus-run-hooks 'gnus-summary-exit-hook)
5311     (unless (or quit-config
5312                 ;; If this group has disappeared from the summary
5313                 ;; buffer, don't skip forwards.
5314                 (not (string= group (gnus-group-group-name))))
5315       (gnus-group-next-unread-group 1))
5316     (setq group-point (point))
5317     (if temporary
5318         nil                             ;Nothing to do.
5319       ;; If we have several article buffers, we kill them at exit.
5320       (unless gnus-single-article-buffer
5321         (gnus-kill-buffer gnus-article-buffer)
5322         (gnus-kill-buffer gnus-original-article-buffer)
5323         (setq gnus-article-current nil))
5324       (set-buffer buf)
5325       (if (not gnus-kill-summary-on-exit)
5326           (gnus-deaden-summary)
5327         ;; We set all buffer-local variables to nil.  It is unclear why
5328         ;; this is needed, but if we don't, buffer-local variables are
5329         ;; not garbage-collected, it seems.  This would the lead to en
5330         ;; ever-growing Emacs.
5331         (gnus-summary-clear-local-variables)
5332         (when (get-buffer gnus-article-buffer)
5333           (bury-buffer gnus-article-buffer))
5334         ;; We clear the global counterparts of the buffer-local
5335         ;; variables as well, just to be on the safe side.
5336         (set-buffer gnus-group-buffer)
5337         (gnus-summary-clear-local-variables)
5338         ;; Return to group mode buffer.
5339         (when (eq mode 'gnus-summary-mode)
5340           (gnus-kill-buffer buf)))
5341       (setq gnus-current-select-method gnus-select-method)
5342       (pop-to-buffer gnus-group-buffer)
5343       (if (not quit-config)
5344           (progn
5345             (goto-char group-point)
5346             (gnus-configure-windows 'group 'force))
5347         (gnus-handle-ephemeral-exit quit-config))
5348       ;; Clear the current group name.
5349       (unless quit-config
5350         (setq gnus-newsgroup-name nil)))))
5351
5352 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5353 (defun gnus-summary-exit-no-update (&optional no-questions)
5354   "Quit reading current newsgroup without updating read article info."
5355   (interactive)
5356   (let* ((group gnus-newsgroup-name)
5357          (quit-config (gnus-group-quit-config group)))
5358     (when (or no-questions
5359               gnus-expert-user
5360               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5361       (gnus-async-halt-prefetch)
5362       (mapcar 'funcall
5363               (delq 'gnus-summary-expire-articles
5364                     (copy-list gnus-summary-prepare-exit-hook)))
5365       (when (gnus-buffer-live-p gnus-article-buffer)
5366         (save-excursion
5367           (set-buffer gnus-article-buffer)
5368           (mm-destroy-parts gnus-article-mime-handles)))
5369       ;; If we have several article buffers, we kill them at exit.
5370       (unless gnus-single-article-buffer
5371         (gnus-kill-buffer gnus-article-buffer)
5372         (gnus-kill-buffer gnus-original-article-buffer)
5373         (setq gnus-article-current nil))
5374       (if (not gnus-kill-summary-on-exit)
5375           (gnus-deaden-summary)
5376         (gnus-close-group group)
5377         (gnus-summary-clear-local-variables)
5378         (set-buffer gnus-group-buffer)
5379         (gnus-summary-clear-local-variables)
5380         (when (get-buffer gnus-summary-buffer)
5381           (kill-buffer gnus-summary-buffer)))
5382       (unless gnus-single-article-buffer
5383         (setq gnus-article-current nil))
5384       (when gnus-use-trees
5385         (gnus-tree-close group))
5386       (gnus-async-prefetch-remove-group group)
5387       (when (get-buffer gnus-article-buffer)
5388         (bury-buffer gnus-article-buffer))
5389       ;; Return to the group buffer.
5390       (gnus-configure-windows 'group 'force)
5391       ;; Clear the current group name.
5392       (setq gnus-newsgroup-name nil)
5393       (when (equal (gnus-group-group-name) group)
5394         (gnus-group-next-unread-group 1))
5395       (when quit-config
5396         (gnus-handle-ephemeral-exit quit-config)))))
5397
5398 (defun gnus-handle-ephemeral-exit (quit-config)
5399   "Handle movement when leaving an ephemeral group.
5400 The state which existed when entering the ephemeral is reset."
5401   (if (not (buffer-name (car quit-config)))
5402       (gnus-configure-windows 'group 'force)
5403     (set-buffer (car quit-config))
5404     (cond ((eq major-mode 'gnus-summary-mode)
5405            (gnus-set-global-variables))
5406           ((eq major-mode 'gnus-article-mode)
5407            (save-excursion
5408              ;; The `gnus-summary-buffer' variable may point
5409              ;; to the old summary buffer when using a single
5410              ;; article buffer.
5411              (unless (gnus-buffer-live-p gnus-summary-buffer)
5412                (set-buffer gnus-group-buffer))
5413              (set-buffer gnus-summary-buffer)
5414              (gnus-set-global-variables))))
5415     (if (or (eq (cdr quit-config) 'article)
5416             (eq (cdr quit-config) 'pick))
5417         (progn
5418           ;; The current article may be from the ephemeral group
5419           ;; thus it is best that we reload this article
5420           (gnus-summary-show-article)
5421           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5422               (gnus-configure-windows 'pick 'force)
5423             (gnus-configure-windows (cdr quit-config) 'force)))
5424       (gnus-configure-windows (cdr quit-config) 'force))
5425     (when (eq major-mode 'gnus-summary-mode)
5426       (gnus-summary-next-subject 1 nil t)
5427       (gnus-summary-recenter)
5428       (gnus-summary-position-point))))
5429
5430 ;;; Dead summaries.
5431
5432 (defvar gnus-dead-summary-mode-map nil)
5433
5434 (unless gnus-dead-summary-mode-map
5435   (setq gnus-dead-summary-mode-map (make-keymap))
5436   (suppress-keymap gnus-dead-summary-mode-map)
5437   (substitute-key-definition
5438    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5439   (let ((keys '("\C-d" "\r" "\177" [delete])))
5440     (while keys
5441       (define-key gnus-dead-summary-mode-map
5442         (pop keys) 'gnus-summary-wake-up-the-dead))))
5443
5444 (defvar gnus-dead-summary-mode nil
5445   "Minor mode for Gnus summary buffers.")
5446
5447 (defun gnus-dead-summary-mode (&optional arg)
5448   "Minor mode for Gnus summary buffers."
5449   (interactive "P")
5450   (when (eq major-mode 'gnus-summary-mode)
5451     (make-local-variable 'gnus-dead-summary-mode)
5452     (setq gnus-dead-summary-mode
5453           (if (null arg) (not gnus-dead-summary-mode)
5454             (> (prefix-numeric-value arg) 0)))
5455     (when gnus-dead-summary-mode
5456       (gnus-add-minor-mode
5457        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5458
5459 (defun gnus-deaden-summary ()
5460   "Make the current summary buffer into a dead summary buffer."
5461   ;; Kill any previous dead summary buffer.
5462   (when (and gnus-dead-summary
5463              (buffer-name gnus-dead-summary))
5464     (save-excursion
5465       (set-buffer gnus-dead-summary)
5466       (when gnus-dead-summary-mode
5467         (kill-buffer (current-buffer)))))
5468   ;; Make this the current dead summary.
5469   (setq gnus-dead-summary (current-buffer))
5470   (gnus-dead-summary-mode 1)
5471   (let ((name (buffer-name)))
5472     (when (string-match "Summary" name)
5473       (rename-buffer
5474        (concat (substring name 0 (match-beginning 0)) "Dead "
5475                (substring name (match-beginning 0)))
5476        t))))
5477
5478 (defun gnus-kill-or-deaden-summary (buffer)
5479   "Kill or deaden the summary BUFFER."
5480   (save-excursion
5481     (when (and (buffer-name buffer)
5482                (not gnus-single-article-buffer))
5483       (save-excursion
5484         (set-buffer buffer)
5485         (gnus-kill-buffer gnus-article-buffer)
5486         (gnus-kill-buffer gnus-original-article-buffer)))
5487     (cond (gnus-kill-summary-on-exit
5488            (when (and gnus-use-trees
5489                       (gnus-buffer-exists-p buffer))
5490              (save-excursion
5491                (set-buffer buffer)
5492                (gnus-tree-close gnus-newsgroup-name)))
5493            (gnus-kill-buffer buffer))
5494           ((gnus-buffer-exists-p buffer)
5495            (save-excursion
5496              (set-buffer buffer)
5497              (gnus-deaden-summary))))))
5498
5499 (defun gnus-summary-wake-up-the-dead (&rest args)
5500   "Wake up the dead summary buffer."
5501   (interactive)
5502   (gnus-dead-summary-mode -1)
5503   (let ((name (buffer-name)))
5504     (when (string-match "Dead " name)
5505       (rename-buffer
5506        (concat (substring name 0 (match-beginning 0))
5507                (substring name (match-end 0)))
5508        t)))
5509   (gnus-message 3 "This dead summary is now alive again"))
5510
5511 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5512 (defun gnus-summary-fetch-faq (&optional faq-dir)
5513   "Fetch the FAQ for the current group.
5514 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5515 in."
5516   (interactive
5517    (list
5518     (when current-prefix-arg
5519       (completing-read
5520        "Faq dir: " (and (listp gnus-group-faq-directory)
5521                         (mapcar (lambda (file) (list file))
5522                                 gnus-group-faq-directory))))))
5523   (let (gnus-faq-buffer)
5524     (when (setq gnus-faq-buffer
5525                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5526       (gnus-configure-windows 'summary-faq))))
5527
5528 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5529 (defun gnus-summary-describe-group (&optional force)
5530   "Describe the current newsgroup."
5531   (interactive "P")
5532   (gnus-group-describe-group force gnus-newsgroup-name))
5533
5534 (defun gnus-summary-describe-briefly ()
5535   "Describe summary mode commands briefly."
5536   (interactive)
5537   (gnus-message 6 (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")))
5538
5539 ;; Walking around group mode buffer from summary mode.
5540
5541 (defun gnus-summary-next-group (&optional no-article target-group backward)
5542   "Exit current newsgroup and then select next unread newsgroup.
5543 If prefix argument NO-ARTICLE is non-nil, no article is selected
5544 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5545 previous group instead."
5546   (interactive "P")
5547   ;; Stop pre-fetching.
5548   (gnus-async-halt-prefetch)
5549   (let ((current-group gnus-newsgroup-name)
5550         (current-buffer (current-buffer))
5551         entered)
5552     ;; First we semi-exit this group to update Xrefs and all variables.
5553     ;; We can't do a real exit, because the window conf must remain
5554     ;; the same in case the user is prompted for info, and we don't
5555     ;; want the window conf to change before that...
5556     (gnus-summary-exit t)
5557     (while (not entered)
5558       ;; Then we find what group we are supposed to enter.
5559       (set-buffer gnus-group-buffer)
5560       (gnus-group-jump-to-group current-group)
5561       (setq target-group
5562             (or target-group
5563                 (if (eq gnus-keep-same-level 'best)
5564                     (gnus-summary-best-group gnus-newsgroup-name)
5565                   (gnus-summary-search-group backward gnus-keep-same-level))))
5566       (if (not target-group)
5567           ;; There are no further groups, so we return to the group
5568           ;; buffer.
5569           (progn
5570             (gnus-message 5 "Returning to the group buffer")
5571             (setq entered t)
5572             (when (gnus-buffer-live-p current-buffer)
5573               (set-buffer current-buffer)
5574               (gnus-summary-exit))
5575             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5576         ;; We try to enter the target group.
5577         (gnus-group-jump-to-group target-group)
5578         (let ((unreads (gnus-group-group-unread)))
5579           (if (and (or (eq t unreads)
5580                        (and unreads (not (zerop unreads))))
5581                    (gnus-summary-read-group
5582                     target-group nil no-article
5583                     (and (buffer-name current-buffer) current-buffer)
5584                     nil backward))
5585               (setq entered t)
5586             (setq current-group target-group
5587                   target-group nil)))))))
5588
5589 (defun gnus-summary-prev-group (&optional no-article)
5590   "Exit current newsgroup and then select previous unread newsgroup.
5591 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5592   (interactive "P")
5593   (gnus-summary-next-group no-article nil t))
5594
5595 ;; Walking around summary lines.
5596
5597 (defun gnus-summary-first-subject (&optional unread undownloaded)
5598   "Go to the first unread subject.
5599 If UNREAD is non-nil, go to the first unread article.
5600 Returns the article selected or nil if there are no unread articles."
5601   (interactive "P")
5602   (prog1
5603       (cond
5604        ;; Empty summary.
5605        ((null gnus-newsgroup-data)
5606         (gnus-message 3 "No articles in the group")
5607         nil)
5608        ;; Pick the first article.
5609        ((not unread)
5610         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5611         (gnus-data-number (car gnus-newsgroup-data)))
5612        ;; No unread articles.
5613        ((null gnus-newsgroup-unreads)
5614         (gnus-message 3 "No more unread articles")
5615         nil)
5616        ;; Find the first unread article.
5617        (t
5618         (let ((data gnus-newsgroup-data))
5619           (while (and data
5620                       (and (not (and undownloaded
5621                                      (eq gnus-undownloaded-mark
5622                                          (gnus-data-mark (car data)))))
5623                            (not (gnus-data-unread-p (car data)))))
5624             (setq data (cdr data)))
5625           (when data
5626             (goto-char (gnus-data-pos (car data)))
5627             (gnus-data-number (car data))))))
5628     (gnus-summary-position-point)))
5629
5630 (defun gnus-summary-next-subject (n &optional unread dont-display)
5631   "Go to next N'th summary line.
5632 If N is negative, go to the previous N'th subject line.
5633 If UNREAD is non-nil, only unread articles are selected.
5634 The difference between N and the actual number of steps taken is
5635 returned."
5636   (interactive "p")
5637   (let ((backward (< n 0))
5638         (n (abs n)))
5639     (while (and (> n 0)
5640                 (if backward
5641                     (gnus-summary-find-prev unread)
5642                   (gnus-summary-find-next unread)))
5643       (gnus-summary-show-thread)
5644       (setq n (1- n)))
5645     (when (/= 0 n)
5646       (gnus-message 7 "No more%s articles"
5647                     (if unread " unread" "")))
5648     (unless dont-display
5649       (gnus-summary-recenter)
5650       (gnus-summary-position-point))
5651     n))
5652
5653 (defun gnus-summary-next-unread-subject (n)
5654   "Go to next N'th unread summary line."
5655   (interactive "p")
5656   (gnus-summary-next-subject n t))
5657
5658 (defun gnus-summary-prev-subject (n &optional unread)
5659   "Go to previous N'th summary line.
5660 If optional argument UNREAD is non-nil, only unread article is selected."
5661   (interactive "p")
5662   (gnus-summary-next-subject (- n) unread))
5663
5664 (defun gnus-summary-prev-unread-subject (n)
5665   "Go to previous N'th unread summary line."
5666   (interactive "p")
5667   (gnus-summary-next-subject (- n) t))
5668
5669 (defun gnus-summary-goto-subject (article &optional force silent)
5670   "Go the subject line of ARTICLE.
5671 If FORCE, also allow jumping to articles not currently shown."
5672   (interactive "nArticle number: ")
5673   (let ((b (point))
5674         (data (gnus-data-find article)))
5675     ;; We read in the article if we have to.
5676     (and (not data)
5677          force
5678          (gnus-summary-insert-subject
5679           article
5680           (if (or (numberp force) (vectorp force)) force)
5681           t)
5682          (setq data (gnus-data-find article)))
5683     (goto-char b)
5684     (if (not data)
5685         (progn
5686           (unless silent
5687             (gnus-message 3 "Can't find article %d" article))
5688           nil)
5689       (goto-char (gnus-data-pos data))
5690       (gnus-summary-position-point)
5691       article)))
5692
5693 ;; Walking around summary lines with displaying articles.
5694
5695 (defun gnus-summary-expand-window (&optional arg)
5696   "Make the summary buffer take up the entire Emacs frame.
5697 Given a prefix, will force an `article' buffer configuration."
5698   (interactive "P")
5699   (if arg
5700       (gnus-configure-windows 'article 'force)
5701     (gnus-configure-windows 'summary 'force)))
5702
5703 (defun gnus-summary-display-article (article &optional all-header)
5704   "Display ARTICLE in article buffer."
5705   (gnus-set-global-variables)
5706   (if (null article)
5707       nil
5708     (prog1
5709         (if gnus-summary-display-article-function
5710             (funcall gnus-summary-display-article-function article all-header)
5711           (gnus-article-prepare article all-header))
5712       (gnus-run-hooks 'gnus-select-article-hook)
5713       (when (and gnus-current-article
5714                  (not (zerop gnus-current-article)))
5715         (gnus-summary-goto-subject gnus-current-article))
5716       (gnus-summary-recenter)
5717       (when (and gnus-use-trees gnus-show-threads)
5718         (gnus-possibly-generate-tree article)
5719         (gnus-highlight-selected-tree article))
5720       ;; Successfully display article.
5721       (gnus-article-set-window-start
5722        (cdr (assq article gnus-newsgroup-bookmarks))))))
5723
5724 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5725   "Select the current article.
5726 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5727 non-nil, the article will be re-fetched even if it already present in
5728 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5729 be displayed."
5730   ;; Make sure we are in the summary buffer to work around bbdb bug.
5731   (unless (eq major-mode 'gnus-summary-mode)
5732     (set-buffer gnus-summary-buffer))
5733   (let ((article (or article (gnus-summary-article-number)))
5734         (all-headers (not (not all-headers))) ;Must be T or NIL.
5735         gnus-summary-display-article-function
5736         did)
5737     (and (not pseudo)
5738          (gnus-summary-article-pseudo-p article)
5739          (error "This is a pseudo-article"))
5740     (prog1
5741         (save-excursion
5742           (set-buffer gnus-summary-buffer)
5743           (if (or (and gnus-single-article-buffer
5744                        (or (null gnus-current-article)
5745                            (null gnus-article-current)
5746                            (null (get-buffer gnus-article-buffer))
5747                            (not (eq article (cdr gnus-article-current)))
5748                            (not (equal (car gnus-article-current)
5749                                        gnus-newsgroup-name))))
5750                   (and (not gnus-single-article-buffer)
5751                        (or (null gnus-current-article)
5752                            (not (eq gnus-current-article article))))
5753                   force)
5754               ;; The requested article is different from the current article.
5755               (prog1
5756                   (gnus-summary-display-article article all-headers)
5757                 (setq did article)
5758                 (when (or all-headers gnus-show-all-headers)
5759                   (gnus-article-show-all-headers)))
5760             (when (or all-headers gnus-show-all-headers)
5761               (gnus-article-show-all-headers))
5762             'old))
5763       (when did
5764         (gnus-article-set-window-start
5765          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5766
5767 (defun gnus-summary-set-current-mark (&optional current-mark)
5768   "Obsolete function."
5769   nil)
5770
5771 (defun gnus-summary-next-article (&optional unread subject backward push)
5772   "Select the next article.
5773 If UNREAD, only unread articles are selected.
5774 If SUBJECT, only articles with SUBJECT are selected.
5775 If BACKWARD, the previous article is selected instead of the next."
5776   (interactive "P")
5777   (cond
5778    ;; Is there such an article?
5779    ((and (gnus-summary-search-forward unread subject backward)
5780          (or (gnus-summary-display-article (gnus-summary-article-number))
5781              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5782     (gnus-summary-position-point))
5783    ;; If not, we try the first unread, if that is wanted.
5784    ((and subject
5785          gnus-auto-select-same
5786          (gnus-summary-first-unread-article))
5787     (gnus-summary-position-point)
5788     (gnus-message 6 "Wrapped"))
5789    ;; Try to get next/previous article not displayed in this group.
5790    ((and gnus-auto-extend-newsgroup
5791          (not unread) (not subject))
5792     (gnus-summary-goto-article
5793      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5794      nil (count-lines (point-min) (point))))
5795    ;; Go to next/previous group.
5796    (t
5797     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5798       (gnus-summary-jump-to-group gnus-newsgroup-name))
5799     (let ((cmd last-command-char)
5800           (point
5801            (save-excursion
5802              (set-buffer gnus-group-buffer)
5803              (point)))
5804           (group
5805            (if (eq gnus-keep-same-level 'best)
5806                (gnus-summary-best-group gnus-newsgroup-name)
5807              (gnus-summary-search-group backward gnus-keep-same-level))))
5808       ;; For some reason, the group window gets selected.  We change
5809       ;; it back.
5810       (select-window (get-buffer-window (current-buffer)))
5811       ;; Select next unread newsgroup automagically.
5812       (cond
5813        ((or (not gnus-auto-select-next)
5814             (not cmd))
5815         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5816        ((or (eq gnus-auto-select-next 'quietly)
5817             (and (eq gnus-auto-select-next 'slightly-quietly)
5818                  push)
5819             (and (eq gnus-auto-select-next 'almost-quietly)
5820                  (gnus-summary-last-article-p)))
5821         ;; Select quietly.
5822         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5823             (gnus-summary-exit)
5824           (gnus-message 7 "No more%s articles (%s)..."
5825                         (if unread " unread" "")
5826                         (if group (concat "selecting " group)
5827                           "exiting"))
5828           (gnus-summary-next-group nil group backward)))
5829        (t
5830         (when (gnus-key-press-event-p last-input-event)
5831           (gnus-summary-walk-group-buffer
5832            gnus-newsgroup-name cmd unread backward point))))))))
5833
5834 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5835   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5836                       (?\C-p (gnus-group-prev-unread-group 1))))
5837         (cursor-in-echo-area t)
5838         keve key group ended)
5839     (save-excursion
5840       (set-buffer gnus-group-buffer)
5841       (goto-char start)
5842       (setq group
5843             (if (eq gnus-keep-same-level 'best)
5844                 (gnus-summary-best-group gnus-newsgroup-name)
5845               (gnus-summary-search-group backward gnus-keep-same-level))))
5846     (while (not ended)
5847       (gnus-message
5848        5 "No more%s articles%s" (if unread " unread" "")
5849        (if (and group
5850                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5851            (format " (Type %s for %s [%s])"
5852                    (single-key-description cmd) group
5853                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5854          (format " (Type %s to exit %s)"
5855                  (single-key-description cmd)
5856                  gnus-newsgroup-name)))
5857       ;; Confirm auto selection.
5858       (setq key (car (setq keve (gnus-read-event-char))))
5859       (setq ended t)
5860       (cond
5861        ((assq key keystrokes)
5862         (let ((obuf (current-buffer)))
5863           (switch-to-buffer gnus-group-buffer)
5864           (when group
5865             (gnus-group-jump-to-group group))
5866           (eval (cadr (assq key keystrokes)))
5867           (setq group (gnus-group-group-name))
5868           (switch-to-buffer obuf))
5869         (setq ended nil))
5870        ((equal key cmd)
5871         (if (or (not group)
5872                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5873             (gnus-summary-exit)
5874           (gnus-summary-next-group nil group backward)))
5875        (t
5876         (push (cdr keve) unread-command-events))))))
5877
5878 (defun gnus-summary-next-unread-article ()
5879   "Select unread article after current one."
5880   (interactive)
5881   (gnus-summary-next-article
5882    (or (not (eq gnus-summary-goto-unread 'never))
5883        (gnus-summary-last-article-p (gnus-summary-article-number)))
5884    (and gnus-auto-select-same
5885         (gnus-summary-article-subject))))
5886
5887 (defun gnus-summary-prev-article (&optional unread subject)
5888   "Select the article after the current one.
5889 If UNREAD is non-nil, only unread articles are selected."
5890   (interactive "P")
5891   (gnus-summary-next-article unread subject t))
5892
5893 (defun gnus-summary-prev-unread-article ()
5894   "Select unread article before current one."
5895   (interactive)
5896   (gnus-summary-prev-article
5897    (or (not (eq gnus-summary-goto-unread 'never))
5898        (gnus-summary-first-article-p (gnus-summary-article-number)))
5899    (and gnus-auto-select-same
5900         (gnus-summary-article-subject))))
5901
5902 (defun gnus-summary-next-page (&optional lines circular)
5903   "Show next page of the selected article.
5904 If at the end of the current article, select the next article.
5905 LINES says how many lines should be scrolled up.
5906
5907 If CIRCULAR is non-nil, go to the start of the article instead of
5908 selecting the next article when reaching the end of the current
5909 article."
5910   (interactive "P")
5911   (setq gnus-summary-buffer (current-buffer))
5912   (gnus-set-global-variables)
5913   (let ((article (gnus-summary-article-number))
5914         (article-window (get-buffer-window gnus-article-buffer t))
5915         endp)
5916     ;; If the buffer is empty, we have no article.
5917     (unless article
5918       (error "No article to select"))
5919     (gnus-configure-windows 'article)
5920     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5921         (if (and (eq gnus-summary-goto-unread 'never)
5922                  (not (gnus-summary-last-article-p article)))
5923             (gnus-summary-next-article)
5924           (gnus-summary-next-unread-article))
5925       (if (or (null gnus-current-article)
5926               (null gnus-article-current)
5927               (/= article (cdr gnus-article-current))
5928               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5929           ;; Selected subject is different from current article's.
5930           (gnus-summary-display-article article)
5931         (when article-window
5932           (gnus-eval-in-buffer-window gnus-article-buffer
5933             (setq endp (gnus-article-next-page lines)))
5934           (when endp
5935             (cond (circular
5936                    (gnus-summary-beginning-of-article))
5937                   (lines
5938                    (gnus-message 3 "End of message"))
5939                   ((null lines)
5940                    (if (and (eq gnus-summary-goto-unread 'never)
5941                             (not (gnus-summary-last-article-p article)))
5942                        (gnus-summary-next-article)
5943                      (gnus-summary-next-unread-article))))))))
5944     (gnus-summary-recenter)
5945     (gnus-summary-position-point)))
5946
5947 (defun gnus-summary-prev-page (&optional lines move)
5948   "Show previous page of selected article.
5949 Argument LINES specifies lines to be scrolled down.
5950 If MOVE, move to the previous unread article if point is at
5951 the beginning of the buffer."
5952   (interactive "P")
5953   (let ((article (gnus-summary-article-number))
5954         (article-window (get-buffer-window gnus-article-buffer t))
5955         endp)
5956     (gnus-configure-windows 'article)
5957     (if (or (null gnus-current-article)
5958             (null gnus-article-current)
5959             (/= article (cdr gnus-article-current))
5960             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5961         ;; Selected subject is different from current article's.
5962         (gnus-summary-display-article article)
5963       (gnus-summary-recenter)
5964       (when article-window
5965         (gnus-eval-in-buffer-window gnus-article-buffer
5966           (setq endp (gnus-article-prev-page lines)))
5967         (when (and move endp)
5968           (cond (lines
5969                  (gnus-message 3 "Beginning of message"))
5970                 ((null lines)
5971                  (if (and (eq gnus-summary-goto-unread 'never)
5972                           (not (gnus-summary-first-article-p article)))
5973                      (gnus-summary-prev-article)
5974                    (gnus-summary-prev-unread-article))))))))
5975   (gnus-summary-position-point))
5976
5977 (defun gnus-summary-prev-page-or-article (&optional lines)
5978   "Show previous page of selected article.
5979 Argument LINES specifies lines to be scrolled down.
5980 If at the beginning of the article, go to the next article."
5981   (interactive "P")
5982   (gnus-summary-prev-page lines t))
5983
5984 (defun gnus-summary-scroll-up (lines)
5985   "Scroll up (or down) one line current article.
5986 Argument LINES specifies lines to be scrolled up (or down if negative)."
5987   (interactive "p")
5988   (gnus-configure-windows 'article)
5989   (gnus-summary-show-thread)
5990   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5991     (gnus-eval-in-buffer-window gnus-article-buffer
5992       (cond ((> lines 0)
5993              (when (gnus-article-next-page lines)
5994                (gnus-message 3 "End of message")))
5995             ((< lines 0)
5996              (gnus-article-prev-page (- lines))))))
5997   (gnus-summary-recenter)
5998   (gnus-summary-position-point))
5999
6000 (defun gnus-summary-scroll-down (lines)
6001   "Scroll down (or up) one line current article.
6002 Argument LINES specifies lines to be scrolled down (or up if negative)."
6003   (interactive "p")
6004   (gnus-summary-scroll-up (- lines)))
6005
6006 (defun gnus-summary-next-same-subject ()
6007   "Select next article which has the same subject as current one."
6008   (interactive)
6009   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6010
6011 (defun gnus-summary-prev-same-subject ()
6012   "Select previous article which has the same subject as current one."
6013   (interactive)
6014   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6015
6016 (defun gnus-summary-next-unread-same-subject ()
6017   "Select next unread article which has the same subject as current one."
6018   (interactive)
6019   (gnus-summary-next-article t (gnus-summary-article-subject)))
6020
6021 (defun gnus-summary-prev-unread-same-subject ()
6022   "Select previous unread article which has the same subject as current one."
6023   (interactive)
6024   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6025
6026 (defun gnus-summary-first-unread-article ()
6027   "Select the first unread article.
6028 Return nil if there are no unread articles."
6029   (interactive)
6030   (prog1
6031       (when (gnus-summary-first-subject t)
6032         (gnus-summary-show-thread)
6033         (gnus-summary-first-subject t)
6034         (gnus-summary-display-article (gnus-summary-article-number)))
6035     (gnus-summary-position-point)))
6036
6037 (defun gnus-summary-first-unread-subject ()
6038   "Place the point on the subject line of the first unread article.
6039 Return nil if there are no unread articles."
6040   (interactive)
6041   (prog1
6042       (when (gnus-summary-first-subject t)
6043         (gnus-summary-show-thread)
6044         (gnus-summary-first-subject t))
6045     (gnus-summary-position-point)))
6046
6047 (defun gnus-summary-first-article ()
6048   "Select the first article.
6049 Return nil if there are no articles."
6050   (interactive)
6051   (prog1
6052       (when (gnus-summary-first-subject)
6053         (gnus-summary-show-thread)
6054         (gnus-summary-first-subject)
6055         (gnus-summary-display-article (gnus-summary-article-number)))
6056     (gnus-summary-position-point)))
6057
6058 (defun gnus-summary-best-unread-article ()
6059   "Select the unread article with the highest score."
6060   (interactive)
6061   (let ((best -1000000)
6062         (data gnus-newsgroup-data)
6063         article score)
6064     (while data
6065       (and (gnus-data-unread-p (car data))
6066            (> (setq score
6067                     (gnus-summary-article-score (gnus-data-number (car data))))
6068               best)
6069            (setq best score
6070                  article (gnus-data-number (car data))))
6071       (setq data (cdr data)))
6072     (prog1
6073         (if article
6074             (gnus-summary-goto-article article)
6075           (error "No unread articles"))
6076       (gnus-summary-position-point))))
6077
6078 (defun gnus-summary-last-subject ()
6079   "Go to the last displayed subject line in the group."
6080   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6081     (when article
6082       (gnus-summary-goto-subject article))))
6083
6084 (defun gnus-summary-goto-article (article &optional all-headers force)
6085   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6086 If ALL-HEADERS is non-nil, no header lines are hidden.
6087 If FORCE, go to the article even if it isn't displayed.  If FORCE
6088 is a number, it is the line the article is to be displayed on."
6089   (interactive
6090    (list
6091     (completing-read
6092      "Article number or Message-ID: "
6093      (mapcar (lambda (number) (list (int-to-string number)))
6094              gnus-newsgroup-limit))
6095     current-prefix-arg
6096     t))
6097   (prog1
6098       (if (and (stringp article)
6099                (string-match "@" article))
6100           (gnus-summary-refer-article article)
6101         (when (stringp article)
6102           (setq article (string-to-number article)))
6103         (if (gnus-summary-goto-subject article force)
6104             (gnus-summary-display-article article all-headers)
6105           (gnus-message 4 "Couldn't go to article %s" article) nil))
6106     (gnus-summary-position-point)))
6107
6108 (defun gnus-summary-goto-last-article ()
6109   "Go to the previously read article."
6110   (interactive)
6111   (prog1
6112       (when gnus-last-article
6113         (gnus-summary-goto-article gnus-last-article nil t))
6114     (gnus-summary-position-point)))
6115
6116 (defun gnus-summary-pop-article (number)
6117   "Pop one article off the history and go to the previous.
6118 NUMBER articles will be popped off."
6119   (interactive "p")
6120   (let (to)
6121     (setq gnus-newsgroup-history
6122           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6123     (if to
6124         (gnus-summary-goto-article (car to) nil t)
6125       (error "Article history empty")))
6126   (gnus-summary-position-point))
6127
6128 ;; Summary commands and functions for limiting the summary buffer.
6129
6130 (defun gnus-summary-limit-to-articles (n)
6131   "Limit the summary buffer to the next N articles.
6132 If not given a prefix, use the process marked articles instead."
6133   (interactive "P")
6134   (prog1
6135       (let ((articles (gnus-summary-work-articles n)))
6136         (setq gnus-newsgroup-processable nil)
6137         (gnus-summary-limit articles))
6138     (gnus-summary-position-point)))
6139
6140 (defun gnus-summary-pop-limit (&optional total)
6141   "Restore the previous limit.
6142 If given a prefix, remove all limits."
6143   (interactive "P")
6144   (when total
6145     (setq gnus-newsgroup-limits
6146           (list (mapcar (lambda (h) (mail-header-number h))
6147                         gnus-newsgroup-headers))))
6148   (unless gnus-newsgroup-limits
6149     (error "No limit to pop"))
6150   (prog1
6151       (gnus-summary-limit nil 'pop)
6152     (gnus-summary-position-point)))
6153
6154 (defun gnus-summary-limit-to-subject (subject &optional header)
6155   "Limit the summary buffer to articles that have subjects that match a regexp."
6156   (interactive "sLimit to subject (regexp): ")
6157   (unless header
6158     (setq header "subject"))
6159   (when (not (equal "" subject))
6160     (prog1
6161         (let ((articles (gnus-summary-find-matching
6162                          (or header "subject") subject 'all)))
6163           (unless articles
6164             (error "Found no matches for \"%s\"" subject))
6165           (gnus-summary-limit articles))
6166       (gnus-summary-position-point))))
6167
6168 (defun gnus-summary-limit-to-author (from)
6169   "Limit the summary buffer to articles that have authors that match a regexp."
6170   (interactive "sLimit to author (regexp): ")
6171   (gnus-summary-limit-to-subject from "from"))
6172
6173 (defun gnus-summary-limit-to-age (age &optional younger-p)
6174   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6175 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6176 articles that are younger than AGE days."
6177   (interactive "nLimit to articles older than (in days): \nP")
6178   (prog1
6179       (let ((data gnus-newsgroup-data)
6180             (cutoff (days-to-time age))
6181             articles d date is-younger)
6182         (while (setq d (pop data))
6183           (when (and (vectorp (gnus-data-header d))
6184                      (setq date (mail-header-date (gnus-data-header d))))
6185             (setq is-younger (time-less-p
6186                               (time-since (date-to-time date))
6187                               cutoff))
6188             (when (if younger-p
6189                       is-younger
6190                     (not is-younger))
6191               (push (gnus-data-number d) articles))))
6192         (gnus-summary-limit (nreverse articles)))
6193     (gnus-summary-position-point)))
6194
6195 (defun gnus-summary-limit-to-extra (header regexp)
6196   "Limit the summary buffer to articles that match an 'extra' header."
6197   (interactive
6198    (let ((header
6199           (intern
6200            (gnus-completing-read
6201             (symbol-name (car gnus-extra-headers))      
6202             "Limit extra header:"       
6203             (mapcar (lambda (x) 
6204                       (cons (symbol-name x) x))
6205                     gnus-extra-headers)
6206             nil                 
6207             t))))
6208      (list header
6209            (read-string (format "Limit to header %s (regexp): " header)))))
6210   (when (not (equal "" regexp))
6211     (prog1
6212         (let ((articles (gnus-summary-find-matching
6213                          (cons 'extra header) regexp 'all)))
6214           (unless articles
6215             (error "Found no matches for \"%s\"" regexp))
6216           (gnus-summary-limit articles))
6217       (gnus-summary-position-point))))
6218
6219 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6220 (make-obsolete
6221  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6222
6223 (defun gnus-summary-limit-to-unread (&optional all)
6224   "Limit the summary buffer to articles that are not marked as read.
6225 If ALL is non-nil, limit strictly to unread articles."
6226   (interactive "P")
6227   (if all
6228       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6229     (gnus-summary-limit-to-marks
6230      ;; Concat all the marks that say that an article is read and have
6231      ;; those removed.
6232      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6233            gnus-killed-mark gnus-kill-file-mark
6234            gnus-low-score-mark gnus-expirable-mark
6235            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6236            gnus-duplicate-mark gnus-souped-mark)
6237      'reverse)))
6238
6239 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6240 (make-obsolete 'gnus-summary-delete-marked-with
6241                'gnus-summary-limit-exlude-marks)
6242
6243 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6244   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6245 If REVERSE, limit the summary buffer to articles that are marked
6246 with MARKS.  MARKS can either be a string of marks or a list of marks.
6247 Returns how many articles were removed."
6248   (interactive "sMarks: ")
6249   (gnus-summary-limit-to-marks marks t))
6250
6251 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6252   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6253 If REVERSE (the prefix), limit the summary buffer to articles that are
6254 not marked with MARKS.  MARKS can either be a string of marks or a
6255 list of marks.
6256 Returns how many articles were removed."
6257   (interactive "sMarks: \nP")
6258   (prog1
6259       (let ((data gnus-newsgroup-data)
6260             (marks (if (listp marks) marks
6261                      (append marks nil))) ; Transform to list.
6262             articles)
6263         (while data
6264           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6265                   (memq (gnus-data-mark (car data)) marks))
6266             (push (gnus-data-number (car data)) articles))
6267           (setq data (cdr data)))
6268         (gnus-summary-limit articles))
6269     (gnus-summary-position-point)))
6270
6271 (defun gnus-summary-limit-to-score (&optional score)
6272   "Limit to articles with score at or above SCORE."
6273   (interactive "P")
6274   (setq score (if score
6275                   (prefix-numeric-value score)
6276                 (or gnus-summary-default-score 0)))
6277   (let ((data gnus-newsgroup-data)
6278         articles)
6279     (while data
6280       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6281                 score)
6282         (push (gnus-data-number (car data)) articles))
6283       (setq data (cdr data)))
6284     (prog1
6285         (gnus-summary-limit articles)
6286       (gnus-summary-position-point))))
6287
6288 (defun gnus-summary-limit-include-thread (id)
6289   "Display all the hidden articles that in the current thread."
6290   (interactive (list (mail-header-id (gnus-summary-article-header))))
6291   (let ((articles (gnus-articles-in-thread
6292                    (gnus-id-to-thread (gnus-root-id id)))))
6293     (prog1
6294         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6295       (gnus-summary-position-point))))
6296
6297 (defun gnus-summary-limit-include-dormant ()
6298   "Display all the hidden articles that are marked as dormant.
6299 Note that this command only works on a subset of the articles currently
6300 fetched for this group."
6301   (interactive)
6302   (unless gnus-newsgroup-dormant
6303     (error "There are no dormant articles in this group"))
6304   (prog1
6305       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6306     (gnus-summary-position-point)))
6307
6308 (defun gnus-summary-limit-exclude-dormant ()
6309   "Hide all dormant articles."
6310   (interactive)
6311   (prog1
6312       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6313     (gnus-summary-position-point)))
6314
6315 (defun gnus-summary-limit-exclude-childless-dormant ()
6316   "Hide all dormant articles that have no children."
6317   (interactive)
6318   (let ((data (gnus-data-list t))
6319         articles d children)
6320     ;; Find all articles that are either not dormant or have
6321     ;; children.
6322     (while (setq d (pop data))
6323       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6324                 (and (setq children
6325                            (gnus-article-children (gnus-data-number d)))
6326                      (let (found)
6327                        (while children
6328                          (when (memq (car children) articles)
6329                            (setq children nil
6330                                  found t))
6331                          (pop children))
6332                        found)))
6333         (push (gnus-data-number d) articles)))
6334     ;; Do the limiting.
6335     (prog1
6336         (gnus-summary-limit articles)
6337       (gnus-summary-position-point))))
6338
6339 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6340   "Mark all unread excluded articles as read.
6341 If ALL, mark even excluded ticked and dormants as read."
6342   (interactive "P")
6343   (let ((articles (gnus-sorted-complement
6344                    (sort
6345                     (mapcar (lambda (h) (mail-header-number h))
6346                             gnus-newsgroup-headers)
6347                     '<)
6348                    (sort gnus-newsgroup-limit '<)))
6349         article)
6350     (setq gnus-newsgroup-unreads
6351           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6352     (if all
6353         (setq gnus-newsgroup-dormant nil
6354               gnus-newsgroup-marked nil
6355               gnus-newsgroup-reads
6356               (nconc
6357                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6358                gnus-newsgroup-reads))
6359       (while (setq article (pop articles))
6360         (unless (or (memq article gnus-newsgroup-dormant)
6361                     (memq article gnus-newsgroup-marked))
6362           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6363
6364 (defun gnus-summary-limit (articles &optional pop)
6365   (if pop
6366       ;; We pop the previous limit off the stack and use that.
6367       (setq articles (car gnus-newsgroup-limits)
6368             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6369     ;; We use the new limit, so we push the old limit on the stack.
6370     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6371   ;; Set the limit.
6372   (setq gnus-newsgroup-limit articles)
6373   (let ((total (length gnus-newsgroup-data))
6374         (data (gnus-data-find-list (gnus-summary-article-number)))
6375         (gnus-summary-mark-below nil)   ; Inhibit this.
6376         found)
6377     ;; This will do all the work of generating the new summary buffer
6378     ;; according to the new limit.
6379     (gnus-summary-prepare)
6380     ;; Hide any threads, possibly.
6381     (and gnus-show-threads
6382          gnus-thread-hide-subtree
6383          (gnus-summary-hide-all-threads))
6384     ;; Try to return to the article you were at, or one in the
6385     ;; neighborhood.
6386     (when data
6387       ;; We try to find some article after the current one.
6388       (while data
6389         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6390           (setq data nil
6391                 found t))
6392         (setq data (cdr data))))
6393     (unless found
6394       ;; If there is no data, that means that we were after the last
6395       ;; article.  The same goes when we can't find any articles
6396       ;; after the current one.
6397       (goto-char (point-max))
6398       (gnus-summary-find-prev))
6399     (gnus-set-mode-line 'summary)
6400     ;; We return how many articles were removed from the summary
6401     ;; buffer as a result of the new limit.
6402     (- total (length gnus-newsgroup-data))))
6403
6404 (defsubst gnus-invisible-cut-children (threads)
6405   (let ((num 0))
6406     (while threads
6407       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6408         (incf num))
6409       (pop threads))
6410     (< num 2)))
6411
6412 (defsubst gnus-cut-thread (thread)
6413   "Go forwards in the thread until we find an article that we want to display."
6414   (when (or (eq gnus-fetch-old-headers 'some)
6415             (eq gnus-fetch-old-headers 'invisible)
6416             (eq gnus-build-sparse-threads 'some)
6417             (eq gnus-build-sparse-threads 'more))
6418     ;; Deal with old-fetched headers and sparse threads.
6419     (while (and
6420             thread
6421             (or
6422              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6423              (gnus-summary-article-ancient-p
6424               (mail-header-number (car thread))))
6425             (if (or (<= (length (cdr thread)) 1)
6426                     (eq gnus-fetch-old-headers 'invisible))
6427                 (setq gnus-newsgroup-limit
6428                       (delq (mail-header-number (car thread))
6429                             gnus-newsgroup-limit)
6430                       thread (cadr thread))
6431               (when (gnus-invisible-cut-children (cdr thread))
6432                 (let ((th (cdr thread)))
6433                   (while th
6434                     (if (memq (mail-header-number (caar th))
6435                               gnus-newsgroup-limit)
6436                         (setq thread (car th)
6437                               th nil)
6438                       (setq th (cdr th))))))))))
6439   thread)
6440
6441 (defun gnus-cut-threads (threads)
6442   "Cut off all uninteresting articles from the beginning of threads."
6443   (when (or (eq gnus-fetch-old-headers 'some)
6444             (eq gnus-fetch-old-headers 'invisible)
6445             (eq gnus-build-sparse-threads 'some)
6446             (eq gnus-build-sparse-threads 'more))
6447     (let ((th threads))
6448       (while th
6449         (setcar th (gnus-cut-thread (car th)))
6450         (setq th (cdr th)))))
6451   ;; Remove nixed out threads.
6452   (delq nil threads))
6453
6454 (defun gnus-summary-initial-limit (&optional show-if-empty)
6455   "Figure out what the initial limit is supposed to be on group entry.
6456 This entails weeding out unwanted dormants, low-scored articles,
6457 fetch-old-headers verbiage, and so on."
6458   ;; Most groups have nothing to remove.
6459   (if (or gnus-inhibit-limiting
6460           (and (null gnus-newsgroup-dormant)
6461                (not (eq gnus-fetch-old-headers 'some))
6462                (not (eq gnus-fetch-old-headers 'invisible))
6463                (null gnus-summary-expunge-below)
6464                (not (eq gnus-build-sparse-threads 'some))
6465                (not (eq gnus-build-sparse-threads 'more))
6466                (null gnus-thread-expunge-below)
6467                (not gnus-use-nocem)))
6468       ()                                ; Do nothing.
6469     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6470     (setq gnus-newsgroup-limit nil)
6471     (mapatoms
6472      (lambda (node)
6473        (unless (car (symbol-value node))
6474          ;; These threads have no parents -- they are roots.
6475          (let ((nodes (cdr (symbol-value node)))
6476                thread)
6477            (while nodes
6478              (if (and gnus-thread-expunge-below
6479                       (< (gnus-thread-total-score (car nodes))
6480                          gnus-thread-expunge-below))
6481                  (gnus-expunge-thread (pop nodes))
6482                (setq thread (pop nodes))
6483                (gnus-summary-limit-children thread))))))
6484      gnus-newsgroup-dependencies)
6485     ;; If this limitation resulted in an empty group, we might
6486     ;; pop the previous limit and use it instead.
6487     (when (and (not gnus-newsgroup-limit)
6488                show-if-empty)
6489       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6490     gnus-newsgroup-limit))
6491
6492 (defun gnus-summary-limit-children (thread)
6493   "Return 1 if this subthread is visible and 0 if it is not."
6494   ;; First we get the number of visible children to this thread.  This
6495   ;; is done by recursing down the thread using this function, so this
6496   ;; will really go down to a leaf article first, before slowly
6497   ;; working its way up towards the root.
6498   (when thread
6499     (let ((children
6500            (if (cdr thread)
6501                (apply '+ (mapcar 'gnus-summary-limit-children
6502                                  (cdr thread)))
6503              0))
6504           (number (mail-header-number (car thread)))
6505           score)
6506       (if (and
6507            (not (memq number gnus-newsgroup-marked))
6508            (or
6509             ;; If this article is dormant and has absolutely no visible
6510             ;; children, then this article isn't visible.
6511             (and (memq number gnus-newsgroup-dormant)
6512                  (zerop children))
6513             ;; If this is "fetch-old-headered" and there is no
6514             ;; visible children, then we don't want this article.
6515             (and (eq gnus-fetch-old-headers 'some)
6516                  (gnus-summary-article-ancient-p number)
6517                  (zerop children))
6518             ;; If this is "fetch-old-headered" and `invisible', then
6519             ;; we don't want this article.
6520             (and (eq gnus-fetch-old-headers 'invisible)
6521                  (gnus-summary-article-ancient-p number))
6522             ;; If this is a sparsely inserted article with no children,
6523             ;; we don't want it.
6524             (and (eq gnus-build-sparse-threads 'some)
6525                  (gnus-summary-article-sparse-p number)
6526                  (zerop children))
6527             ;; If we use expunging, and this article is really
6528             ;; low-scored, then we don't want this article.
6529             (when (and gnus-summary-expunge-below
6530                        (< (setq score
6531                                 (or (cdr (assq number gnus-newsgroup-scored))
6532                                     gnus-summary-default-score))
6533                           gnus-summary-expunge-below))
6534               ;; We increase the expunge-tally here, but that has
6535               ;; nothing to do with the limits, really.
6536               (incf gnus-newsgroup-expunged-tally)
6537               ;; We also mark as read here, if that's wanted.
6538               (when (and gnus-summary-mark-below
6539                          (< score gnus-summary-mark-below))
6540                 (setq gnus-newsgroup-unreads
6541                       (delq number gnus-newsgroup-unreads))
6542                 (if gnus-newsgroup-auto-expire
6543                     (push number gnus-newsgroup-expirable)
6544                   (push (cons number gnus-low-score-mark)
6545                         gnus-newsgroup-reads)))
6546               t)
6547             ;; Check NoCeM things.
6548             (if (and gnus-use-nocem
6549                      (gnus-nocem-unwanted-article-p
6550                       (mail-header-id (car thread))))
6551                 (progn
6552                   (setq gnus-newsgroup-unreads
6553                         (delq number gnus-newsgroup-unreads))
6554                   t))))
6555           ;; Nope, invisible article.
6556           0
6557         ;; Ok, this article is to be visible, so we add it to the limit
6558         ;; and return 1.
6559         (push number gnus-newsgroup-limit)
6560         1))))
6561
6562 (defun gnus-expunge-thread (thread)
6563   "Mark all articles in THREAD as read."
6564   (let* ((number (mail-header-number (car thread))))
6565     (incf gnus-newsgroup-expunged-tally)
6566     ;; We also mark as read here, if that's wanted.
6567     (setq gnus-newsgroup-unreads
6568           (delq number gnus-newsgroup-unreads))
6569     (if gnus-newsgroup-auto-expire
6570         (push number gnus-newsgroup-expirable)
6571       (push (cons number gnus-low-score-mark)
6572             gnus-newsgroup-reads)))
6573   ;; Go recursively through all subthreads.
6574   (mapcar 'gnus-expunge-thread (cdr thread)))
6575
6576 ;; Summary article oriented commands
6577
6578 (defun gnus-summary-refer-parent-article (n)
6579   "Refer parent article N times.
6580 If N is negative, go to ancestor -N instead.
6581 The difference between N and the number of articles fetched is returned."
6582   (interactive "p")
6583   (let ((skip 1)
6584         error header ref)
6585     (when (not (natnump n))
6586       (setq skip (abs n)
6587             n 1))
6588     (while (and (> n 0)
6589                 (not error))
6590       (setq header (gnus-summary-article-header))
6591       (if (and (eq (mail-header-number header)
6592                    (cdr gnus-article-current))
6593                (equal gnus-newsgroup-name
6594                       (car gnus-article-current)))
6595           ;; If we try to find the parent of the currently
6596           ;; displayed article, then we take a look at the actual
6597           ;; References header, since this is slightly more
6598           ;; reliable than the References field we got from the
6599           ;; server.
6600           (save-excursion
6601             (set-buffer gnus-original-article-buffer)
6602             (nnheader-narrow-to-headers)
6603             (unless (setq ref (message-fetch-field "references"))
6604               (setq ref (message-fetch-field "in-reply-to")))
6605             (widen))
6606         (setq ref
6607               ;; It's not the current article, so we take a bet on
6608               ;; the value we got from the server.
6609               (mail-header-references header)))
6610       (if (and ref
6611                (not (equal ref "")))
6612           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6613             (gnus-message 1 "Couldn't find parent"))
6614         (gnus-message 1 "No references in article %d"
6615                       (gnus-summary-article-number))
6616         (setq error t))
6617       (decf n))
6618     (gnus-summary-position-point)
6619     n))
6620
6621 (defun gnus-summary-refer-references ()
6622   "Fetch all articles mentioned in the References header.
6623 Return the number of articles fetched."
6624   (interactive)
6625   (let ((ref (mail-header-references (gnus-summary-article-header)))
6626         (current (gnus-summary-article-number))
6627         (n 0))
6628     (if (or (not ref)
6629             (equal ref ""))
6630         (error "No References in the current article")
6631       ;; For each Message-ID in the References header...
6632       (while (string-match "<[^>]*>" ref)
6633         (incf n)
6634         ;; ... fetch that article.
6635         (gnus-summary-refer-article
6636          (prog1 (match-string 0 ref)
6637            (setq ref (substring ref (match-end 0))))))
6638       (gnus-summary-goto-subject current)
6639       (gnus-summary-position-point)
6640       n)))
6641
6642 (defun gnus-summary-refer-thread (&optional limit)
6643   "Fetch all articles in the current thread.
6644 If LIMIT (the numerical prefix), fetch that many old headers instead
6645 of what's specified by the `gnus-refer-thread-limit' variable."
6646   (interactive "P")
6647   (let ((id (mail-header-id (gnus-summary-article-header)))
6648         (limit (if limit (prefix-numeric-value limit)
6649                  gnus-refer-thread-limit)))
6650     ;; We want to fetch LIMIT *old* headers, but we also have to
6651     ;; re-fetch all the headers in the current buffer, because many of
6652     ;; them may be undisplayed.  So we adjust LIMIT.
6653     (when (numberp limit)
6654       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6655     (unless (eq gnus-fetch-old-headers 'invisible)
6656       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6657       ;; Retrieve the headers and read them in.
6658       (if (eq (gnus-retrieve-headers
6659                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6660               'nov)
6661           (gnus-build-all-threads)
6662         (error "Can't fetch thread from backends that don't support NOV"))
6663       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6664     (gnus-summary-limit-include-thread id)))
6665
6666 (defun gnus-summary-refer-article (message-id &optional arg)
6667   "Fetch an article specified by MESSAGE-ID.
6668 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6669 or `gnus-select-method', no matter what backend the article comes from."
6670   (interactive "sMessage-ID: \nP")
6671   (when (and (stringp message-id)
6672              (not (zerop (length message-id))))
6673     ;; Construct the correct Message-ID if necessary.
6674     ;; Suggested by tale@pawl.rpi.edu.
6675     (unless (string-match "^<" message-id)
6676       (setq message-id (concat "<" message-id)))
6677     (unless (string-match ">$" message-id)
6678       (setq message-id (concat message-id ">")))
6679     (let* ((header (gnus-id-to-header message-id))
6680            (sparse (and header
6681                         (gnus-summary-article-sparse-p
6682                          (mail-header-number header))
6683                         (memq (mail-header-number header)
6684                               gnus-newsgroup-limit))))
6685       (cond
6686        ;; If the article is present in the buffer we just go to it.
6687        ((and header
6688              (or (not (gnus-summary-article-sparse-p
6689                        (mail-header-number header)))
6690                  sparse))
6691         (prog1
6692             (gnus-summary-goto-article
6693              (mail-header-number header) nil t)
6694           (when sparse
6695             (gnus-summary-update-article (mail-header-number header)))))
6696        (t
6697         ;; We fetch the article
6698         (let ((gnus-override-method
6699                (cond ((gnus-news-group-p gnus-newsgroup-name)
6700                       gnus-refer-article-method)
6701                      (arg
6702                       (or gnus-refer-article-method gnus-select-method))
6703                      (t nil)))
6704               number)
6705           ;; Start the special refer-article method, if necessary.
6706           (when (and gnus-refer-article-method
6707                      (gnus-news-group-p gnus-newsgroup-name))
6708             (gnus-check-server gnus-refer-article-method))
6709           ;; Fetch the header, and display the article.
6710           (if (setq number (gnus-summary-insert-subject message-id))
6711               (gnus-summary-select-article nil nil nil number)
6712             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6713
6714 (defun gnus-summary-edit-parameters ()
6715   "Edit the group parameters of the current group."
6716   (interactive)
6717   (gnus-group-edit-group gnus-newsgroup-name 'params))
6718
6719 (defun gnus-summary-customize-parameters ()
6720   "Customize the group parameters of the current group."
6721   (interactive)
6722   (gnus-group-customize gnus-newsgroup-name))
6723
6724 (defun gnus-summary-enter-digest-group (&optional force)
6725   "Enter an nndoc group based on the current article.
6726 If FORCE, force a digest interpretation.  If not, try
6727 to guess what the document format is."
6728   (interactive "P")
6729   (let ((conf gnus-current-window-configuration))
6730     (save-excursion
6731       (gnus-summary-select-article))
6732     (setq gnus-current-window-configuration conf)
6733     (let* ((name (format "%s-%d"
6734                          (gnus-group-prefixed-name
6735                           gnus-newsgroup-name (list 'nndoc ""))
6736                          (save-excursion
6737                            (set-buffer gnus-summary-buffer)
6738                            gnus-current-article)))
6739            (ogroup gnus-newsgroup-name)
6740            (params (append (gnus-info-params (gnus-get-info ogroup))
6741                            (list (cons 'to-group ogroup))
6742                            (list (cons 'save-article-group ogroup))))
6743            (case-fold-search t)
6744            (buf (current-buffer))
6745            dig)
6746       (save-excursion
6747         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6748         (insert-buffer-substring gnus-original-article-buffer)
6749         ;; Remove lines that may lead nndoc to misinterpret the
6750         ;; document type.
6751         (narrow-to-region
6752          (goto-char (point-min))
6753          (or (search-forward "\n\n" nil t) (point)))
6754         (goto-char (point-min))
6755         (delete-matching-lines "^Path:\\|^From ")
6756         (widen))
6757       (unwind-protect
6758           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6759                     (gnus-newsgroup-ephemeral-ignored-charsets
6760                      gnus-newsgroup-ignored-charsets))
6761                 (gnus-group-read-ephemeral-group
6762                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6763                               (nndoc-article-type
6764                                ,(if force 'digest 'guess))) t))
6765               ;; Make all postings to this group go to the parent group.
6766               (nconc (gnus-info-params (gnus-get-info name))
6767                      params)
6768             ;; Couldn't select this doc group.
6769             (switch-to-buffer buf)
6770             (gnus-set-global-variables)
6771             (gnus-configure-windows 'summary)
6772             (gnus-message 3 "Article couldn't be entered?"))
6773         (kill-buffer dig)))))
6774
6775 (defun gnus-summary-read-document (n)
6776   "Open a new group based on the current article(s).
6777 This will allow you to read digests and other similar
6778 documents as newsgroups.
6779 Obeys the standard process/prefix convention."
6780   (interactive "P")
6781   (let* ((articles (gnus-summary-work-articles n))
6782          (ogroup gnus-newsgroup-name)
6783          (params (append (gnus-info-params (gnus-get-info ogroup))
6784                          (list (cons 'to-group ogroup))))
6785          article group egroup groups vgroup)
6786     (while (setq article (pop articles))
6787       (setq group (format "%s-%d" gnus-newsgroup-name article))
6788       (gnus-summary-remove-process-mark article)
6789       (when (gnus-summary-display-article article)
6790         (save-excursion
6791           (with-temp-buffer
6792             (insert-buffer-substring gnus-original-article-buffer)
6793             ;; Remove some headers that may lead nndoc to make
6794             ;; the wrong guess.
6795             (message-narrow-to-head)
6796             (goto-char (point-min))
6797             (delete-matching-lines "^\\(Path\\):\\|^From ")
6798             (widen)
6799             (if (setq egroup
6800                       (gnus-group-read-ephemeral-group
6801                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6802                                      (nndoc-article-type guess))
6803                        t nil t))
6804                 (progn
6805                   ;; Make all postings to this group go to the parent group.
6806                   (nconc (gnus-info-params (gnus-get-info egroup))
6807                          params)
6808                   (push egroup groups))
6809               ;; Couldn't select this doc group.
6810               (gnus-error 3 "Article couldn't be entered"))))))
6811     ;; Now we have selected all the documents.
6812     (cond
6813      ((not groups)
6814       (error "None of the articles could be interpreted as documents"))
6815      ((gnus-group-read-ephemeral-group
6816        (setq vgroup (format
6817                      "nnvirtual:%s-%s" gnus-newsgroup-name
6818                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6819        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6820        t
6821        (cons (current-buffer) 'summary)))
6822      (t
6823       (error "Couldn't select virtual nndoc group")))))
6824
6825 (defun gnus-summary-isearch-article (&optional regexp-p)
6826   "Do incremental search forward on the current article.
6827 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6828   (interactive "P")
6829   (gnus-summary-select-article)
6830   (gnus-configure-windows 'article)
6831   (gnus-eval-in-buffer-window gnus-article-buffer
6832     (save-restriction
6833       (widen)
6834       (isearch-forward regexp-p))))
6835
6836 (defun gnus-summary-search-article-forward (regexp &optional backward)
6837   "Search for an article containing REGEXP forward.
6838 If BACKWARD, search backward instead."
6839   (interactive
6840    (list (read-string
6841           (format "Search article %s (regexp%s): "
6842                   (if current-prefix-arg "backward" "forward")
6843                   (if gnus-last-search-regexp
6844                       (concat ", default " gnus-last-search-regexp)
6845                     "")))
6846          current-prefix-arg))
6847   (if (string-equal regexp "")
6848       (setq regexp (or gnus-last-search-regexp ""))
6849     (setq gnus-last-search-regexp regexp))
6850   (if (gnus-summary-search-article regexp backward)
6851       (gnus-summary-show-thread)
6852     (error "Search failed: \"%s\"" regexp)))
6853
6854 (defun gnus-summary-search-article-backward (regexp)
6855   "Search for an article containing REGEXP backward."
6856   (interactive
6857    (list (read-string
6858           (format "Search article backward (regexp%s): "
6859                   (if gnus-last-search-regexp
6860                       (concat ", default " gnus-last-search-regexp)
6861                     "")))))
6862   (gnus-summary-search-article-forward regexp 'backward))
6863
6864 (defun gnus-summary-search-article (regexp &optional backward)
6865   "Search for an article containing REGEXP.
6866 Optional argument BACKWARD means do search for backward.
6867 `gnus-select-article-hook' is not called during the search."
6868   ;; We have to require this here to make sure that the following
6869   ;; dynamic binding isn't shadowed by autoloading.
6870   (require 'gnus-async)
6871   (require 'gnus-art)
6872   (let ((gnus-select-article-hook nil)  ;Disable hook.
6873         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6874         (gnus-use-article-prefetch nil)
6875         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6876         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6877         (sum (current-buffer))
6878         (gnus-display-mime-function nil)
6879         (found nil)
6880         point)
6881     (gnus-save-hidden-threads
6882       (gnus-summary-select-article)
6883       (set-buffer gnus-article-buffer)
6884       (when backward
6885         (forward-line -1))
6886       (while (not found)
6887         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6888         (if (if backward
6889                 (re-search-backward regexp nil t)
6890               (re-search-forward regexp nil t))
6891             ;; We found the regexp.
6892             (progn
6893               (setq found 'found)
6894               (beginning-of-line)
6895               (set-window-start
6896                (get-buffer-window (current-buffer))
6897                (point))
6898               (forward-line 1)
6899               (set-buffer sum)
6900               (setq point (point)))
6901           ;; We didn't find it, so we go to the next article.
6902           (set-buffer sum)
6903           (setq found 'not)
6904           (while (eq found 'not)
6905             (if (not (if backward (gnus-summary-find-prev)
6906                        (gnus-summary-find-next)))
6907                 ;; No more articles.
6908                 (setq found t)
6909               ;; Select the next article and adjust point.
6910               (unless (gnus-summary-article-sparse-p
6911                        (gnus-summary-article-number))
6912                 (setq found nil)
6913                 (gnus-summary-select-article)
6914                 (set-buffer gnus-article-buffer)
6915                 (widen)
6916                 (goto-char (if backward (point-max) (point-min))))))))
6917       (gnus-message 7 ""))
6918     ;; Return whether we found the regexp.
6919     (when (eq found 'found)
6920       (goto-char point)
6921       (gnus-summary-show-thread)
6922       (gnus-summary-goto-subject gnus-current-article)
6923       (gnus-summary-position-point)
6924       t)))
6925
6926 (defun gnus-summary-find-matching (header regexp &optional backward unread
6927                                           not-case-fold)
6928   "Return a list of all articles that match REGEXP on HEADER.
6929 The search stars on the current article and goes forwards unless
6930 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6931 If UNREAD is non-nil, only unread articles will
6932 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6933 in the comparisons."
6934   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6935                 (gnus-data-find-list
6936                  (gnus-summary-article-number) (gnus-data-list backward))))
6937         (case-fold-search (not not-case-fold))
6938         articles d func)
6939     (if (consp header)
6940         (if (eq (car header) 'extra)
6941             (setq func
6942                   `(lambda (h)
6943                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6944                          "")))
6945           (error "%s is an invalid header" header))
6946       (unless (fboundp (intern (concat "mail-header-" header)))
6947         (error "%s is not a valid header" header))
6948       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6949     (while data
6950       (setq d (car data))
6951       (and (or (not unread)             ; We want all articles...
6952                (gnus-data-unread-p d))  ; Or just unreads.
6953            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6954            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6955            (push (gnus-data-number d) articles)) ; Success!
6956       (setq data (cdr data)))
6957     (nreverse articles)))
6958
6959 (defun gnus-summary-execute-command (header regexp command &optional backward)
6960   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6961 If HEADER is an empty string (or nil), the match is done on the entire
6962 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6963   (interactive
6964    (list (let ((completion-ignore-case t))
6965            (completing-read
6966             "Header name: "
6967             (mapcar (lambda (string) (list string))
6968                     '("Number" "Subject" "From" "Lines" "Date"
6969                       "Message-ID" "Xref" "References" "Body"))
6970             nil 'require-match))
6971          (read-string "Regexp: ")
6972          (read-key-sequence "Command: ")
6973          current-prefix-arg))
6974   (when (equal header "Body")
6975     (setq header ""))
6976   ;; Hidden thread subtrees must be searched as well.
6977   (gnus-summary-show-all-threads)
6978   ;; We don't want to change current point nor window configuration.
6979   (save-excursion
6980     (save-window-excursion
6981       (gnus-message 6 "Executing %s..." (key-description command))
6982       ;; We'd like to execute COMMAND interactively so as to give arguments.
6983       (gnus-execute header regexp
6984                     `(call-interactively ',(key-binding command))
6985                     backward)
6986       (gnus-message 6 "Executing %s...done" (key-description command)))))
6987
6988 (defun gnus-summary-beginning-of-article ()
6989   "Scroll the article back to the beginning."
6990   (interactive)
6991   (gnus-summary-select-article)
6992   (gnus-configure-windows 'article)
6993   (gnus-eval-in-buffer-window gnus-article-buffer
6994     (widen)
6995     (goto-char (point-min))
6996     (when gnus-page-broken
6997       (gnus-narrow-to-page))))
6998
6999 (defun gnus-summary-end-of-article ()
7000   "Scroll to the end of the article."
7001   (interactive)
7002   (gnus-summary-select-article)
7003   (gnus-configure-windows 'article)
7004   (gnus-eval-in-buffer-window gnus-article-buffer
7005     (widen)
7006     (goto-char (point-max))
7007     (recenter -3)
7008     (when gnus-page-broken
7009       (gnus-narrow-to-page))))
7010
7011 (defun gnus-summary-print-article (&optional filename n)
7012   "Generate and print a PostScript image of the N next (mail) articles.
7013
7014 If N is negative, print the N previous articles.  If N is nil and articles
7015 have been marked with the process mark, print these instead.
7016
7017 If the optional first argument FILENAME is nil, send the image to the
7018 printer.  If FILENAME is a string, save the PostScript image in a file with
7019 that name.  If FILENAME is a number, prompt the user for the name of the file
7020 to save in."
7021   (interactive (list (ps-print-preprint current-prefix-arg)
7022                      current-prefix-arg))
7023   (dolist (article (gnus-summary-work-articles n))
7024     (gnus-summary-select-article nil nil 'pseudo article)
7025     (gnus-eval-in-buffer-window gnus-article-buffer
7026       (let ((buffer (generate-new-buffer " *print*")))
7027         (unwind-protect
7028             (progn
7029               (copy-to-buffer buffer (point-min) (point-max))
7030               (set-buffer buffer)
7031               (gnus-article-delete-invisible-text)
7032               (let ((ps-left-header
7033                      (list
7034                       (concat "("
7035                               (mail-header-subject gnus-current-headers) ")")
7036                       (concat "("
7037                               (mail-header-from gnus-current-headers) ")")))
7038                     (ps-right-header
7039                      (list
7040                       "/pagenumberstring load"
7041                       (concat "("
7042                               (mail-header-date gnus-current-headers) ")"))))
7043                 (gnus-run-hooks 'gnus-ps-print-hook)
7044                 (save-excursion
7045                   (ps-print-buffer-with-faces filename))))
7046           (kill-buffer buffer))))))
7047
7048 (defun gnus-summary-show-article (&optional arg)
7049   "Force re-fetching of the current article.
7050 If ARG (the prefix) is non-nil, show the raw article without any
7051 article massaging functions being run."
7052   (interactive "P")
7053   (if (not arg)
7054       ;; Select the article the normal way.
7055       (gnus-summary-select-article nil 'force)
7056     ;; We have to require this here to make sure that the following
7057     ;; dynamic binding isn't shadowed by autoloading.
7058     (require 'gnus-async)
7059     (require 'gnus-art)
7060     ;; Bind the article treatment functions to nil.
7061     (let ((gnus-have-all-headers t)
7062           gnus-article-prepare-hook
7063           gnus-article-decode-hook
7064           gnus-display-mime-function
7065           gnus-break-pages
7066           gnus-visual)
7067       ;; Destroy any MIME parts.
7068       (when (gnus-buffer-live-p gnus-article-buffer)
7069         (save-excursion
7070           (set-buffer gnus-article-buffer)
7071           (mm-destroy-parts gnus-article-mime-handles)))
7072       (gnus-summary-select-article nil 'force)))
7073   (gnus-summary-goto-subject gnus-current-article)
7074   (gnus-summary-position-point))
7075
7076 (defun gnus-summary-verbose-headers (&optional arg)
7077   "Toggle permanent full header display.
7078 If ARG is a positive number, turn header display on.
7079 If ARG is a negative number, turn header display off."
7080   (interactive "P")
7081   (setq gnus-show-all-headers
7082         (cond ((or (not (numberp arg))
7083                    (zerop arg))
7084                (not gnus-show-all-headers))
7085               ((natnump arg)
7086                t)))
7087   (gnus-summary-show-article))
7088
7089 (defun gnus-summary-toggle-header (&optional arg)
7090   "Show the headers if they are hidden, or hide them if they are shown.
7091 If ARG is a positive number, show the entire header.
7092 If ARG is a negative number, hide the unwanted header lines."
7093   (interactive "P")
7094   (save-excursion
7095     (set-buffer gnus-article-buffer)
7096     (save-restriction
7097       (let* ((buffer-read-only nil)
7098              (inhibit-point-motion-hooks t)
7099              hidden e)
7100         (save-restriction 
7101           (article-narrow-to-head)
7102           (setq hidden (gnus-article-hidden-text-p 'headers)))
7103         (goto-char (point-min))
7104         (when (search-forward "\n\n" nil t)
7105           (delete-region (point-min) (1- (point))))
7106         (goto-char (point-min))
7107         (save-excursion
7108           (set-buffer gnus-original-article-buffer)
7109           (goto-char (point-min))
7110           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7111         (insert-buffer-substring gnus-original-article-buffer 1 e)
7112         (save-restriction
7113           (narrow-to-region (point-min) (point))
7114           (article-decode-encoded-words)
7115           (if (or hidden
7116                   (and (numberp arg) (< arg 0)))
7117               (let ((gnus-treat-hide-headers nil)
7118                     (gnus-treat-hide-boring-headers nil))
7119                 (gnus-treat-article 'head))
7120             (gnus-treat-article 'head)))))))
7121
7122 (defun gnus-summary-show-all-headers ()
7123   "Make all header lines visible."
7124   (interactive)
7125   (gnus-article-show-all-headers))
7126
7127 (defun gnus-summary-caesar-message (&optional arg)
7128   "Caesar rotate the current article by 13.
7129 The numerical prefix specifies how many places to rotate each letter
7130 forward."
7131   (interactive "P")
7132   (gnus-summary-select-article)
7133   (let ((mail-header-separator ""))
7134     (gnus-eval-in-buffer-window gnus-article-buffer
7135       (save-restriction
7136         (widen)
7137         (let ((start (window-start))
7138               buffer-read-only)
7139           (message-caesar-buffer-body arg)
7140           (set-window-start (get-buffer-window (current-buffer)) start))))))
7141
7142 (defun gnus-summary-stop-page-breaking ()
7143   "Stop page breaking in the current article."
7144   (interactive)
7145   (gnus-summary-select-article)
7146   (gnus-eval-in-buffer-window gnus-article-buffer
7147     (widen)
7148     (when (gnus-visual-p 'page-marker)
7149       (let ((buffer-read-only nil))
7150         (gnus-remove-text-with-property 'gnus-prev)
7151         (gnus-remove-text-with-property 'gnus-next))
7152       (setq gnus-page-broken nil))))
7153
7154 (defun gnus-summary-move-article (&optional n to-newsgroup
7155                                             select-method action)
7156   "Move the current article to a different newsgroup.
7157 If N is a positive number, move the N next articles.
7158 If N is a negative number, move the N previous articles.
7159 If N is nil and any articles have been marked with the process mark,
7160 move those articles instead.
7161 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7162 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7163 re-spool using this method.
7164
7165 For this function to work, both the current newsgroup and the
7166 newsgroup that you want to move to have to support the `request-move'
7167 and `request-accept' functions."
7168   (interactive "P")
7169   (unless action
7170     (setq action 'move))
7171   ;; Disable marking as read.
7172   (let (gnus-mark-article-hook)
7173     (save-window-excursion
7174       (gnus-summary-select-article)))
7175   ;; Check whether the source group supports the required functions.
7176   (cond ((and (eq action 'move)
7177               (not (gnus-check-backend-function
7178                     'request-move-article gnus-newsgroup-name)))
7179          (error "The current group does not support article moving"))
7180         ((and (eq action 'crosspost)
7181               (not (gnus-check-backend-function
7182                     'request-replace-article gnus-newsgroup-name)))
7183          (error "The current group does not support article editing")))
7184   (let ((articles (gnus-summary-work-articles n))
7185         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7186         (names '((move "Move" "Moving")
7187                  (copy "Copy" "Copying")
7188                  (crosspost "Crosspost" "Crossposting")))
7189         (copy-buf (save-excursion
7190                     (nnheader-set-temp-buffer " *copy article*")))
7191         art-group to-method new-xref article to-groups)
7192     (unless (assq action names)
7193       (error "Unknown action %s" action))
7194     ;; Read the newsgroup name.
7195     (when (and (not to-newsgroup)
7196                (not select-method))
7197       (setq to-newsgroup
7198             (gnus-read-move-group-name
7199              (cadr (assq action names))
7200              (symbol-value (intern (format "gnus-current-%s-group" action)))
7201              articles prefix))
7202       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7203     (setq to-method (or select-method
7204                         (gnus-group-name-to-method to-newsgroup)))
7205     ;; Check the method we are to move this article to...
7206     (unless (gnus-check-backend-function
7207              'request-accept-article (car to-method))
7208       (error "%s does not support article copying" (car to-method)))
7209     (unless (gnus-check-server to-method)
7210       (error "Can't open server %s" (car to-method)))
7211     (gnus-message 6 "%s to %s: %s..."
7212                   (caddr (assq action names))
7213                   (or (car select-method) to-newsgroup) articles)
7214     (while articles
7215       (setq article (pop articles))
7216       (setq
7217        art-group
7218        (cond
7219         ;; Move the article.
7220         ((eq action 'move)
7221          ;; Remove this article from future suppression.
7222          (gnus-dup-unsuppress-article article)
7223          (gnus-request-move-article
7224           article                       ; Article to move
7225           gnus-newsgroup-name           ; From newsgroup
7226           (nth 1 (gnus-find-method-for-group
7227                   gnus-newsgroup-name)) ; Server
7228           (list 'gnus-request-accept-article
7229                 to-newsgroup (list 'quote select-method)
7230                 (not articles) t)               ; Accept form
7231           (not articles)))              ; Only save nov last time
7232         ;; Copy the article.
7233         ((eq action 'copy)
7234          (save-excursion
7235            (set-buffer copy-buf)
7236            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7237              (gnus-request-accept-article
7238               to-newsgroup select-method (not articles) t))))
7239         ;; Crosspost the article.
7240         ((eq action 'crosspost)
7241          (let ((xref (message-tokenize-header
7242                       (mail-header-xref (gnus-summary-article-header article))
7243                       " ")))
7244            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7245                                   ":" article))
7246            (unless xref
7247              (setq xref (list (system-name))))
7248            (setq new-xref
7249                  (concat
7250                   (mapconcat 'identity
7251                              (delete "Xref:" (delete new-xref xref))
7252                              " ")
7253                   " " new-xref))
7254            (save-excursion
7255              (set-buffer copy-buf)
7256              ;; First put the article in the destination group.
7257              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7258              (when (consp (setq art-group
7259                                 (gnus-request-accept-article
7260                                  to-newsgroup select-method (not articles))))
7261                (setq new-xref (concat new-xref " " (car art-group)
7262                                       ":" (cdr art-group)))
7263                ;; Now we have the new Xrefs header, so we insert
7264                ;; it and replace the new article.
7265                (nnheader-replace-header "Xref" new-xref)
7266                (gnus-request-replace-article
7267                 (cdr art-group) to-newsgroup (current-buffer))
7268                art-group))))))
7269       (cond
7270        ((not art-group)
7271         (gnus-message 1 "Couldn't %s article %s"
7272                       (cadr (assq action names)) article))
7273        ((and (eq art-group 'junk)
7274              (eq action 'move))
7275         (gnus-summary-mark-article article gnus-canceled-mark)
7276         (gnus-message 4 "Deleted article %s" article))
7277        (t
7278         (let* ((pto-group (gnus-group-prefixed-name
7279                            (car art-group) to-method))
7280                (entry
7281                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7282                (info (nth 2 entry))
7283                (to-group (gnus-info-group info))
7284                to-marks)
7285           ;; Update the group that has been moved to.
7286           (when (and info
7287                      (memq action '(move copy)))
7288             (unless (member to-group to-groups)
7289               (push to-group to-groups))
7290
7291             (unless (memq article gnus-newsgroup-unreads)
7292               (push 'read to-marks)
7293               (gnus-info-set-read
7294                info (gnus-add-to-range (gnus-info-read info)
7295                                        (list (cdr art-group)))))
7296
7297             ;; Copy any marks over to the new group.
7298             (let ((marks gnus-article-mark-lists)
7299                   (to-article (cdr art-group)))
7300
7301               ;; See whether the article is to be put in the cache.
7302               (when gnus-use-cache
7303                 (gnus-cache-possibly-enter-article
7304                  to-group to-article
7305                  (memq article gnus-newsgroup-marked)
7306                  (memq article gnus-newsgroup-dormant)
7307                  (memq article gnus-newsgroup-unreads)))
7308
7309               (when (and (equal to-group gnus-newsgroup-name)
7310                          (not (memq article gnus-newsgroup-unreads)))
7311                 ;; Mark this article as read in this group.
7312                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7313                 (setcdr (gnus-active to-group) to-article)
7314                 (setcdr gnus-newsgroup-active to-article))
7315
7316               (while marks
7317                 (when (memq article (symbol-value
7318                                      (intern (format "gnus-newsgroup-%s"
7319                                                      (caar marks)))))
7320                   (push (cdar marks) to-marks)
7321                   ;; If the other group is the same as this group,
7322                   ;; then we have to add the mark to the list.
7323                   (when (equal to-group gnus-newsgroup-name)
7324                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7325                          (cons to-article
7326                                (symbol-value
7327                                 (intern (format "gnus-newsgroup-%s"
7328                                                 (caar marks)))))))
7329                   ;; Copy the marks to other group.
7330                   (gnus-add-marked-articles
7331                    to-group (cdar marks) (list to-article) info))
7332                 (setq marks (cdr marks)))
7333
7334               (gnus-request-set-mark to-group (list (list (list to-article)
7335                                                           'set
7336                                                           to-marks)))
7337
7338               (gnus-dribble-enter
7339                (concat "(gnus-group-set-info '"
7340                        (gnus-prin1-to-string (gnus-get-info to-group))
7341                        ")"))))
7342
7343           ;; Update the Xref header in this article to point to
7344           ;; the new crossposted article we have just created.
7345           (when (eq action 'crosspost)
7346             (save-excursion
7347               (set-buffer copy-buf)
7348               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7349               (nnheader-replace-header "Xref" new-xref)
7350               (gnus-request-replace-article
7351                article gnus-newsgroup-name (current-buffer)))))
7352
7353         ;;;!!!Why is this necessary?
7354         (set-buffer gnus-summary-buffer)
7355
7356         (gnus-summary-goto-subject article)
7357         (when (eq action 'move)
7358           (gnus-summary-mark-article article gnus-canceled-mark))))
7359       (gnus-summary-remove-process-mark article))
7360     ;; Re-activate all groups that have been moved to.
7361     (while to-groups
7362       (save-excursion
7363         (set-buffer gnus-group-buffer)
7364         (when (gnus-group-goto-group (car to-groups) t)
7365           (gnus-group-get-new-news-this-group 1 t))
7366         (pop to-groups)))
7367
7368     (gnus-kill-buffer copy-buf)
7369     (gnus-summary-position-point)
7370     (gnus-set-mode-line 'summary)))
7371
7372 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7373   "Move the current article to a different newsgroup.
7374 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7375 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7376 re-spool using this method."
7377   (interactive "P")
7378   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7379
7380 (defun gnus-summary-crosspost-article (&optional n)
7381   "Crosspost the current article to some other group."
7382   (interactive "P")
7383   (gnus-summary-move-article n nil nil 'crosspost))
7384
7385 (defcustom gnus-summary-respool-default-method nil
7386   "Default method for respooling an article.
7387 If nil, use to the current newsgroup method."
7388   :type '(choice (gnus-select-method :value (nnml ""))
7389                  (const nil))
7390   :group 'gnus-summary-mail)
7391
7392 (defun gnus-summary-respool-article (&optional n method)
7393   "Respool the current article.
7394 The article will be squeezed through the mail spooling process again,
7395 which means that it will be put in some mail newsgroup or other
7396 depending on `nnmail-split-methods'.
7397 If N is a positive number, respool the N next articles.
7398 If N is a negative number, respool the N previous articles.
7399 If N is nil and any articles have been marked with the process mark,
7400 respool those articles instead.
7401
7402 Respooling can be done both from mail groups and \"real\" newsgroups.
7403 In the former case, the articles in question will be moved from the
7404 current group into whatever groups they are destined to.  In the
7405 latter case, they will be copied into the relevant groups."
7406   (interactive
7407    (list current-prefix-arg
7408          (let* ((methods (gnus-methods-using 'respool))
7409                 (methname
7410                  (symbol-name (or gnus-summary-respool-default-method
7411                                   (car (gnus-find-method-for-group
7412                                         gnus-newsgroup-name)))))
7413                 (method
7414                  (gnus-completing-read
7415                   methname "What backend do you want to use when respooling?"
7416                   methods nil t nil 'gnus-mail-method-history))
7417                 ms)
7418            (cond
7419             ((zerop (length (setq ms (gnus-servers-using-backend
7420                                       (intern method)))))
7421              (list (intern method) ""))
7422             ((= 1 (length ms))
7423              (car ms))
7424             (t
7425              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7426                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7427                            ms-alist))))))))
7428   (unless method
7429     (error "No method given for respooling"))
7430   (if (assoc (symbol-name
7431               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7432              (gnus-methods-using 'respool))
7433       (gnus-summary-move-article n nil method)
7434     (gnus-summary-copy-article n nil method)))
7435
7436 (defun gnus-summary-import-article (file)
7437   "Import an arbitrary file into a mail newsgroup."
7438   (interactive "fImport file: ")
7439   (let ((group gnus-newsgroup-name)
7440         (now (current-time))
7441         atts lines)
7442     (unless (gnus-check-backend-function 'request-accept-article group)
7443       (error "%s does not support article importing" group))
7444     (or (file-readable-p file)
7445         (not (file-regular-p file))
7446         (error "Can't read %s" file))
7447     (save-excursion
7448       (set-buffer (gnus-get-buffer-create " *import file*"))
7449       (erase-buffer)
7450       (insert-file-contents file)
7451       (goto-char (point-min))
7452       (unless (nnheader-article-p)
7453         ;; This doesn't look like an article, so we fudge some headers.
7454         (setq atts (file-attributes file)
7455               lines (count-lines (point-min) (point-max)))
7456         (insert "From: " (read-string "From: ") "\n"
7457                 "Subject: " (read-string "Subject: ") "\n"
7458                 "Date: " (message-make-date (nth 5 atts))
7459                 "\n"
7460                 "Message-ID: " (message-make-message-id) "\n"
7461                 "Lines: " (int-to-string lines) "\n"
7462                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7463       (gnus-request-accept-article group nil t)
7464       (kill-buffer (current-buffer)))))
7465
7466 (defun gnus-summary-article-posted-p ()
7467   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7468 This will be the case if the article has both been mailed and posted."
7469   (interactive)
7470   (let ((id (mail-header-references (gnus-summary-article-header)))
7471         (gnus-override-method
7472          (or gnus-refer-article-method gnus-select-method)))
7473     (if (gnus-request-head id "")
7474         (gnus-message 2 "The current message was found on %s"
7475                       gnus-override-method)
7476       (gnus-message 2 "The current message couldn't be found on %s"
7477                     gnus-override-method)
7478       nil)))
7479
7480 (defun gnus-summary-expire-articles (&optional now)
7481   "Expire all articles that are marked as expirable in the current group."
7482   (interactive)
7483   (when (gnus-check-backend-function
7484          'request-expire-articles gnus-newsgroup-name)
7485     ;; This backend supports expiry.
7486     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7487            (expirable (if total
7488                           (progn
7489                             ;; We need to update the info for
7490                             ;; this group for `gnus-list-of-read-articles'
7491                             ;; to give us the right answer.
7492                             (gnus-run-hooks 'gnus-exit-group-hook)
7493                             (gnus-summary-update-info)
7494                             (gnus-list-of-read-articles gnus-newsgroup-name))
7495                         (setq gnus-newsgroup-expirable
7496                               (sort gnus-newsgroup-expirable '<))))
7497            (expiry-wait (if now 'immediate
7498                           (gnus-group-find-parameter
7499                            gnus-newsgroup-name 'expiry-wait)))
7500            es)
7501       (when expirable
7502         ;; There are expirable articles in this group, so we run them
7503         ;; through the expiry process.
7504         (gnus-message 6 "Expiring articles...")
7505         (unless (gnus-check-group gnus-newsgroup-name)
7506           (error "Can't open server for %s" gnus-newsgroup-name))
7507         ;; The list of articles that weren't expired is returned.
7508         (save-excursion
7509           (if expiry-wait
7510               (let ((nnmail-expiry-wait-function nil)
7511                     (nnmail-expiry-wait expiry-wait))
7512                 (setq es (gnus-request-expire-articles
7513                           expirable gnus-newsgroup-name)))
7514             (setq es (gnus-request-expire-articles
7515                       expirable gnus-newsgroup-name))))
7516         (unless total
7517           (setq gnus-newsgroup-expirable es))
7518         ;; We go through the old list of expirable, and mark all
7519         ;; really expired articles as nonexistent.
7520         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7521           (let ((gnus-use-cache nil))
7522             (while expirable
7523               (unless (memq (car expirable) es)
7524                 (when (gnus-data-find (car expirable))
7525                   (gnus-summary-mark-article
7526                    (car expirable) gnus-canceled-mark)))
7527               (setq expirable (cdr expirable)))))
7528         (gnus-message 6 "Expiring articles...done")))))
7529
7530 (defun gnus-summary-expire-articles-now ()
7531   "Expunge all expirable articles in the current group.
7532 This means that *all* articles that are marked as expirable will be
7533 deleted forever, right now."
7534   (interactive)
7535   (or gnus-expert-user
7536       (gnus-yes-or-no-p
7537        "Are you really, really, really sure you want to delete all these messages? ")
7538       (error "Phew!"))
7539   (gnus-summary-expire-articles t))
7540
7541 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7542 (defun gnus-summary-delete-article (&optional n)
7543   "Delete the N next (mail) articles.
7544 This command actually deletes articles.  This is not a marking
7545 command.  The article will disappear forever from your life, never to
7546 return.
7547 If N is negative, delete backwards.
7548 If N is nil and articles have been marked with the process mark,
7549 delete these instead."
7550   (interactive "P")
7551   (unless (gnus-check-backend-function 'request-expire-articles
7552                                        gnus-newsgroup-name)
7553     (error "The current newsgroup does not support article deletion"))
7554   ;; Compute the list of articles to delete.
7555   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7556         not-deleted)
7557     (if (and gnus-novice-user
7558              (not (gnus-yes-or-no-p
7559                    (format "Do you really want to delete %s forever? "
7560                            (if (> (length articles) 1)
7561                                (format "these %s articles" (length articles))
7562                              "this article")))))
7563         ()
7564       ;; Delete the articles.
7565       (setq not-deleted (gnus-request-expire-articles
7566                          articles gnus-newsgroup-name 'force))
7567       (while articles
7568         (gnus-summary-remove-process-mark (car articles))
7569         ;; The backend might not have been able to delete the article
7570         ;; after all.
7571         (unless (memq (car articles) not-deleted)
7572           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7573         (setq articles (cdr articles)))
7574       (when not-deleted
7575         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7576     (gnus-summary-position-point)
7577     (gnus-set-mode-line 'summary)
7578     not-deleted))
7579
7580 (defun gnus-summary-edit-article (&optional force)
7581   "Edit the current article.
7582 This will have permanent effect only in mail groups.
7583 If FORCE is non-nil, allow editing of articles even in read-only
7584 groups."
7585   (interactive "P")
7586   (save-excursion
7587     (set-buffer gnus-summary-buffer)
7588     (let ((mail-parse-charset gnus-newsgroup-charset)
7589           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7590       (gnus-set-global-variables)
7591       (when (and (not force)
7592                  (gnus-group-read-only-p))
7593         (error "The current newsgroup does not support article editing"))
7594       (gnus-summary-show-article t)
7595       (gnus-article-edit-article
7596        'mime-to-mml
7597        `(lambda (no-highlight)
7598           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7599                 (mail-parse-ignored-charsets 
7600                  ',gnus-newsgroup-ignored-charsets))
7601             (mml-to-mime)
7602             (gnus-summary-edit-article-done
7603              ,(or (mail-header-references gnus-current-headers) "")
7604              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7605
7606 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7607
7608 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7609                                                  no-highlight)
7610   "Make edits to the current article permanent."
7611   (interactive)
7612   ;; Replace the article.
7613   (let ((buf (current-buffer)))
7614     (with-temp-buffer
7615       (insert-buffer-substring buf)
7616       (if (and (not read-only)
7617                (not (gnus-request-replace-article
7618                      (cdr gnus-article-current) (car gnus-article-current)
7619                      (current-buffer) t)))
7620           (error "Couldn't replace article")
7621         ;; Update the summary buffer.
7622         (if (and references
7623                  (equal (message-tokenize-header references " ")
7624                         (message-tokenize-header
7625                          (or (message-fetch-field "references") "") " ")))
7626             ;; We only have to update this line.
7627             (save-excursion
7628               (save-restriction
7629                 (message-narrow-to-head)
7630                 (let ((head (buffer-string))
7631                       header)
7632                   (with-temp-buffer
7633                     (insert (format "211 %d Article retrieved.\n"
7634                                     (cdr gnus-article-current)))
7635                     (insert head)
7636                     (insert ".\n")
7637                     (let ((nntp-server-buffer (current-buffer)))
7638                       (setq header (car (gnus-get-newsgroup-headers
7639                                          (save-excursion
7640                                            (set-buffer gnus-summary-buffer)
7641                                            gnus-newsgroup-dependencies)
7642                                          t))))
7643                     (save-excursion
7644                       (set-buffer gnus-summary-buffer)
7645                       (gnus-data-set-header
7646                        (gnus-data-find (cdr gnus-article-current))
7647                        header)
7648                       (gnus-summary-update-article-line
7649                        (cdr gnus-article-current) header))))))
7650           ;; Update threads.
7651           (set-buffer (or buffer gnus-summary-buffer))
7652           (gnus-summary-update-article (cdr gnus-article-current)))
7653         ;; Prettify the article buffer again.
7654         (unless no-highlight
7655           (save-excursion
7656             (set-buffer gnus-article-buffer)
7657             ;;;!!! Fix this -- article should be rehighlighted.
7658             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7659             (set-buffer gnus-original-article-buffer)
7660             (gnus-request-article
7661              (cdr gnus-article-current)
7662              (car gnus-article-current) (current-buffer))))
7663         ;; Prettify the summary buffer line.
7664         (when (gnus-visual-p 'summary-highlight 'highlight)
7665           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7666
7667 (defun gnus-summary-edit-wash (key)
7668   "Perform editing command KEY in the article buffer."
7669   (interactive
7670    (list
7671     (progn
7672       (message "%s" (concat (this-command-keys) "- "))
7673       (read-char))))
7674   (message "")
7675   (gnus-summary-edit-article)
7676   (execute-kbd-macro (concat (this-command-keys) key))
7677   (gnus-article-edit-done))
7678
7679 ;;; Respooling
7680
7681 (defun gnus-summary-respool-query (&optional silent trace)
7682   "Query where the respool algorithm would put this article."
7683   (interactive)
7684   (let (gnus-mark-article-hook)
7685     (gnus-summary-select-article)
7686     (save-excursion
7687       (set-buffer gnus-original-article-buffer)
7688       (save-restriction
7689         (message-narrow-to-head)
7690         (let ((groups (nnmail-article-group 'identity trace)))
7691           (unless silent
7692             (if groups
7693                 (message "This message would go to %s"
7694                          (mapconcat 'car groups ", "))
7695               (message "This message would go to no groups"))
7696             groups))))))
7697
7698 (defun gnus-summary-respool-trace ()
7699   "Trace where the respool algorithm would put this article.
7700 Display a buffer showing all fancy splitting patterns which matched."
7701   (interactive)
7702   (gnus-summary-respool-query nil t))
7703
7704 ;; Summary marking commands.
7705
7706 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7707   "Mark articles which has the same subject as read, and then select the next.
7708 If UNMARK is positive, remove any kind of mark.
7709 If UNMARK is negative, tick articles."
7710   (interactive "P")
7711   (when unmark
7712     (setq unmark (prefix-numeric-value unmark)))
7713   (let ((count
7714          (gnus-summary-mark-same-subject
7715           (gnus-summary-article-subject) unmark)))
7716     ;; Select next unread article.  If auto-select-same mode, should
7717     ;; select the first unread article.
7718     (gnus-summary-next-article t (and gnus-auto-select-same
7719                                       (gnus-summary-article-subject)))
7720     (gnus-message 7 "%d article%s marked as %s"
7721                   count (if (= count 1) " is" "s are")
7722                   (if unmark "unread" "read"))))
7723
7724 (defun gnus-summary-kill-same-subject (&optional unmark)
7725   "Mark articles which has the same subject as read.
7726 If UNMARK is positive, remove any kind of mark.
7727 If UNMARK is negative, tick articles."
7728   (interactive "P")
7729   (when unmark
7730     (setq unmark (prefix-numeric-value unmark)))
7731   (let ((count
7732          (gnus-summary-mark-same-subject
7733           (gnus-summary-article-subject) unmark)))
7734     ;; If marked as read, go to next unread subject.
7735     (when (null unmark)
7736       ;; Go to next unread subject.
7737       (gnus-summary-next-subject 1 t))
7738     (gnus-message 7 "%d articles are marked as %s"
7739                   count (if unmark "unread" "read"))))
7740
7741 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7742   "Mark articles with same SUBJECT as read, and return marked number.
7743 If optional argument UNMARK is positive, remove any kinds of marks.
7744 If optional argument UNMARK is negative, mark articles as unread instead."
7745   (let ((count 1))
7746     (save-excursion
7747       (cond
7748        ((null unmark)                   ; Mark as read.
7749         (while (and
7750                 (progn
7751                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7752                   (gnus-summary-show-thread) t)
7753                 (gnus-summary-find-subject subject))
7754           (setq count (1+ count))))
7755        ((> unmark 0)                    ; Tick.
7756         (while (and
7757                 (progn
7758                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7759                   (gnus-summary-show-thread) t)
7760                 (gnus-summary-find-subject subject))
7761           (setq count (1+ count))))
7762        (t                               ; Mark as unread.
7763         (while (and
7764                 (progn
7765                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7766                   (gnus-summary-show-thread) t)
7767                 (gnus-summary-find-subject subject))
7768           (setq count (1+ count)))))
7769       (gnus-set-mode-line 'summary)
7770       ;; Return the number of marked articles.
7771       count)))
7772
7773 (defun gnus-summary-mark-as-processable (n &optional unmark)
7774   "Set the process mark on the next N articles.
7775 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7776 the process mark instead.  The difference between N and the actual
7777 number of articles marked is returned."
7778   (interactive "p")
7779   (let ((backward (< n 0))
7780         (n (abs n)))
7781     (while (and
7782             (> n 0)
7783             (if unmark
7784                 (gnus-summary-remove-process-mark
7785                  (gnus-summary-article-number))
7786               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7787             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7788       (setq n (1- n)))
7789     (when (/= 0 n)
7790       (gnus-message 7 "No more articles"))
7791     (gnus-summary-recenter)
7792     (gnus-summary-position-point)
7793     n))
7794
7795 (defun gnus-summary-unmark-as-processable (n)
7796   "Remove the process mark from the next N articles.
7797 If N is negative, unmark backward instead.  The difference between N and
7798 the actual number of articles unmarked is returned."
7799   (interactive "p")
7800   (gnus-summary-mark-as-processable n t))
7801
7802 (defun gnus-summary-unmark-all-processable ()
7803   "Remove the process mark from all articles."
7804   (interactive)
7805   (save-excursion
7806     (while gnus-newsgroup-processable
7807       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7808   (gnus-summary-position-point))
7809
7810 (defun gnus-summary-mark-as-expirable (n)
7811   "Mark N articles forward as expirable.
7812 If N is negative, mark backward instead.  The difference between N and
7813 the actual number of articles marked is returned."
7814   (interactive "p")
7815   (gnus-summary-mark-forward n gnus-expirable-mark))
7816
7817 (defun gnus-summary-mark-article-as-replied (article)
7818   "Mark ARTICLE replied and update the summary line."
7819   (push article gnus-newsgroup-replied)
7820   (let ((buffer-read-only nil))
7821     (when (gnus-summary-goto-subject article nil t)
7822       (gnus-summary-update-secondary-mark article))))
7823
7824 (defun gnus-summary-set-bookmark (article)
7825   "Set a bookmark in current article."
7826   (interactive (list (gnus-summary-article-number)))
7827   (when (or (not (get-buffer gnus-article-buffer))
7828             (not gnus-current-article)
7829             (not gnus-article-current)
7830             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7831     (error "No current article selected"))
7832   ;; Remove old bookmark, if one exists.
7833   (let ((old (assq article gnus-newsgroup-bookmarks)))
7834     (when old
7835       (setq gnus-newsgroup-bookmarks
7836             (delq old gnus-newsgroup-bookmarks))))
7837   ;; Set the new bookmark, which is on the form
7838   ;; (article-number . line-number-in-body).
7839   (push
7840    (cons article
7841          (save-excursion
7842            (set-buffer gnus-article-buffer)
7843            (count-lines
7844             (min (point)
7845                  (save-excursion
7846                    (goto-char (point-min))
7847                    (search-forward "\n\n" nil t)
7848                    (point)))
7849             (point))))
7850    gnus-newsgroup-bookmarks)
7851   (gnus-message 6 "A bookmark has been added to the current article."))
7852
7853 (defun gnus-summary-remove-bookmark (article)
7854   "Remove the bookmark from the current article."
7855   (interactive (list (gnus-summary-article-number)))
7856   ;; Remove old bookmark, if one exists.
7857   (let ((old (assq article gnus-newsgroup-bookmarks)))
7858     (if old
7859         (progn
7860           (setq gnus-newsgroup-bookmarks
7861                 (delq old gnus-newsgroup-bookmarks))
7862           (gnus-message 6 "Removed bookmark."))
7863       (gnus-message 6 "No bookmark in current article."))))
7864
7865 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7866 (defun gnus-summary-mark-as-dormant (n)
7867   "Mark N articles forward as dormant.
7868 If N is negative, mark backward instead.  The difference between N and
7869 the actual number of articles marked is returned."
7870   (interactive "p")
7871   (gnus-summary-mark-forward n gnus-dormant-mark))
7872
7873 (defun gnus-summary-set-process-mark (article)
7874   "Set the process mark on ARTICLE and update the summary line."
7875   (setq gnus-newsgroup-processable
7876         (cons article
7877               (delq article gnus-newsgroup-processable)))
7878   (when (gnus-summary-goto-subject article)
7879     (gnus-summary-show-thread)
7880     (gnus-summary-goto-subject article)
7881     (gnus-summary-update-secondary-mark article)))
7882
7883 (defun gnus-summary-remove-process-mark (article)
7884   "Remove the process mark from ARTICLE and update the summary line."
7885   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7886   (when (gnus-summary-goto-subject article)
7887     (gnus-summary-show-thread)
7888     (gnus-summary-goto-subject article)
7889     (gnus-summary-update-secondary-mark article)))
7890
7891 (defun gnus-summary-set-saved-mark (article)
7892   "Set the process mark on ARTICLE and update the summary line."
7893   (push article gnus-newsgroup-saved)
7894   (when (gnus-summary-goto-subject article)
7895     (gnus-summary-update-secondary-mark article)))
7896
7897 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7898   "Mark N articles as read forwards.
7899 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7900 The difference between N and the actual number of articles marked is
7901 returned."
7902   (interactive "p")
7903   (gnus-summary-show-thread)
7904   (let ((backward (< n 0))
7905         (gnus-summary-goto-unread
7906          (and gnus-summary-goto-unread
7907               (not (eq gnus-summary-goto-unread 'never))
7908               (not (memq mark (list gnus-unread-mark
7909                                     gnus-ticked-mark gnus-dormant-mark)))))
7910         (n (abs n))
7911         (mark (or mark gnus-del-mark)))
7912     (while (and (> n 0)
7913                 (gnus-summary-mark-article nil mark no-expire)
7914                 (zerop (gnus-summary-next-subject
7915                         (if backward -1 1)
7916                         (and gnus-summary-goto-unread
7917                              (not (eq gnus-summary-goto-unread 'never)))
7918                         t)))
7919       (setq n (1- n)))
7920     (when (/= 0 n)
7921       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7922     (gnus-summary-recenter)
7923     (gnus-summary-position-point)
7924     (gnus-set-mode-line 'summary)
7925     n))
7926
7927 (defun gnus-summary-mark-article-as-read (mark)
7928   "Mark the current article quickly as read with MARK."
7929   (let ((article (gnus-summary-article-number)))
7930     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7931     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7932     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7933     (push (cons article mark) gnus-newsgroup-reads)
7934     ;; Possibly remove from cache, if that is used.
7935     (when gnus-use-cache
7936       (gnus-cache-enter-remove-article article))
7937     ;; Allow the backend to change the mark.
7938     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7939     ;; Check for auto-expiry.
7940     (when (and gnus-newsgroup-auto-expire
7941                (memq mark gnus-auto-expirable-marks))
7942       (setq mark gnus-expirable-mark)
7943       ;; Let the backend know about the mark change.
7944       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7945       (push article gnus-newsgroup-expirable))
7946     ;; Set the mark in the buffer.
7947     (gnus-summary-update-mark mark 'unread)
7948     t))
7949
7950 (defun gnus-summary-mark-article-as-unread (mark)
7951   "Mark the current article quickly as unread with MARK."
7952   (let* ((article (gnus-summary-article-number))
7953          (old-mark (gnus-summary-article-mark article)))
7954     ;; Allow the backend to change the mark.
7955     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7956     (if (eq mark old-mark)
7957         t
7958       (if (<= article 0)
7959           (progn
7960             (gnus-error 1 "Can't mark negative article numbers")
7961             nil)
7962         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7963         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7964         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7965         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7966         (cond ((= mark gnus-ticked-mark)
7967                (push article gnus-newsgroup-marked))
7968               ((= mark gnus-dormant-mark)
7969                (push article gnus-newsgroup-dormant))
7970               (t
7971                (push article gnus-newsgroup-unreads)))
7972         (gnus-pull article gnus-newsgroup-reads)
7973
7974         ;; See whether the article is to be put in the cache.
7975         (and gnus-use-cache
7976              (vectorp (gnus-summary-article-header article))
7977              (save-excursion
7978                (gnus-cache-possibly-enter-article
7979                 gnus-newsgroup-name article
7980                 (= mark gnus-ticked-mark)
7981                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7982
7983         ;; Fix the mark.
7984         (gnus-summary-update-mark mark 'unread)
7985         t))))
7986
7987 (defun gnus-summary-mark-article (&optional article mark no-expire)
7988   "Mark ARTICLE with MARK.  MARK can be any character.
7989 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7990 `??' (dormant) and `?E' (expirable).
7991 If MARK is nil, then the default character `?r' is used.
7992 If ARTICLE is nil, then the article on the current line will be
7993 marked."
7994   ;; The mark might be a string.
7995   (when (stringp mark)
7996     (setq mark (aref mark 0)))
7997   ;; If no mark is given, then we check auto-expiring.
7998   (when (null mark)
7999     (setq mark gnus-del-mark))
8000   (when (and (not no-expire)
8001              gnus-newsgroup-auto-expire
8002              (memq mark gnus-auto-expirable-marks))
8003     (setq mark gnus-expirable-mark))
8004   (let ((article (or article (gnus-summary-article-number)))
8005         (old-mark (gnus-summary-article-mark article)))
8006     ;; Allow the backend to change the mark.
8007     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8008     (if (eq mark old-mark)
8009         t
8010       (unless article
8011         (error "No article on current line"))
8012       (if (not (if (or (= mark gnus-unread-mark)
8013                        (= mark gnus-ticked-mark)
8014                        (= mark gnus-dormant-mark))
8015                    (gnus-mark-article-as-unread article mark)
8016                  (gnus-mark-article-as-read article mark)))
8017           t
8018         ;; See whether the article is to be put in the cache.
8019         (and gnus-use-cache
8020              (not (= mark gnus-canceled-mark))
8021              (vectorp (gnus-summary-article-header article))
8022              (save-excursion
8023                (gnus-cache-possibly-enter-article
8024                 gnus-newsgroup-name article
8025                 (= mark gnus-ticked-mark)
8026                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8027
8028         (when (gnus-summary-goto-subject article nil t)
8029           (let ((buffer-read-only nil))
8030             (gnus-summary-show-thread)
8031             ;; Fix the mark.
8032             (gnus-summary-update-mark mark 'unread)
8033             t))))))
8034
8035 (defun gnus-summary-update-secondary-mark (article)
8036   "Update the secondary (read, process, cache) mark."
8037   (gnus-summary-update-mark
8038    (cond ((memq article gnus-newsgroup-processable)
8039           gnus-process-mark)
8040          ((memq article gnus-newsgroup-cached)
8041           gnus-cached-mark)
8042          ((memq article gnus-newsgroup-replied)
8043           gnus-replied-mark)
8044          ((memq article gnus-newsgroup-saved)
8045           gnus-saved-mark)
8046          (t gnus-unread-mark))
8047    'replied)
8048   (when (gnus-visual-p 'summary-highlight 'highlight)
8049     (gnus-run-hooks 'gnus-summary-update-hook))
8050   t)
8051
8052 (defun gnus-summary-update-mark (mark type)
8053   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8054         (buffer-read-only nil))
8055     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8056     (when forward
8057       (when (looking-at "\r")
8058         (incf forward))
8059       (when (<= (+ forward (point)) (point-max))
8060         ;; Go to the right position on the line.
8061         (goto-char (+ forward (point)))
8062         ;; Replace the old mark with the new mark.
8063         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8064         ;; Optionally update the marks by some user rule.
8065         (when (eq type 'unread)
8066           (gnus-data-set-mark
8067            (gnus-data-find (gnus-summary-article-number)) mark)
8068           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8069
8070 (defun gnus-mark-article-as-read (article &optional mark)
8071   "Enter ARTICLE in the pertinent lists and remove it from others."
8072   ;; Make the article expirable.
8073   (let ((mark (or mark gnus-del-mark)))
8074     (if (= mark gnus-expirable-mark)
8075         (push article gnus-newsgroup-expirable)
8076       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8077     ;; Remove from unread and marked lists.
8078     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8079     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8080     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8081     (push (cons article mark) gnus-newsgroup-reads)
8082     ;; Possibly remove from cache, if that is used.
8083     (when gnus-use-cache
8084       (gnus-cache-enter-remove-article article))
8085     t))
8086
8087 (defun gnus-mark-article-as-unread (article &optional mark)
8088   "Enter ARTICLE in the pertinent lists and remove it from others."
8089   (let ((mark (or mark gnus-ticked-mark)))
8090     (if (<= article 0)
8091         (progn
8092           (gnus-error 1 "Can't mark negative article numbers")
8093           nil)
8094       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8095             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8096             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8097             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8098
8099       ;; Unsuppress duplicates?
8100       (when gnus-suppress-duplicates
8101         (gnus-dup-unsuppress-article article))
8102
8103       (cond ((= mark gnus-ticked-mark)
8104              (push article gnus-newsgroup-marked))
8105             ((= mark gnus-dormant-mark)
8106              (push article gnus-newsgroup-dormant))
8107             (t
8108              (push article gnus-newsgroup-unreads)))
8109       (gnus-pull article gnus-newsgroup-reads)
8110       t)))
8111
8112 (defalias 'gnus-summary-mark-as-unread-forward
8113   'gnus-summary-tick-article-forward)
8114 (make-obsolete 'gnus-summary-mark-as-unread-forward
8115                'gnus-summary-tick-article-forward)
8116 (defun gnus-summary-tick-article-forward (n)
8117   "Tick N articles forwards.
8118 If N is negative, tick backwards instead.
8119 The difference between N and the number of articles ticked is returned."
8120   (interactive "p")
8121   (gnus-summary-mark-forward n gnus-ticked-mark))
8122
8123 (defalias 'gnus-summary-mark-as-unread-backward
8124   'gnus-summary-tick-article-backward)
8125 (make-obsolete 'gnus-summary-mark-as-unread-backward
8126                'gnus-summary-tick-article-backward)
8127 (defun gnus-summary-tick-article-backward (n)
8128   "Tick N articles backwards.
8129 The difference between N and the number of articles ticked is returned."
8130   (interactive "p")
8131   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8132
8133 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8134 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8135 (defun gnus-summary-tick-article (&optional article clear-mark)
8136   "Mark current article as unread.
8137 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8138 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8139   (interactive)
8140   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8141                                        gnus-ticked-mark)))
8142
8143 (defun gnus-summary-mark-as-read-forward (n)
8144   "Mark N articles as read forwards.
8145 If N is negative, mark backwards instead.
8146 The difference between N and the actual number of articles marked is
8147 returned."
8148   (interactive "p")
8149   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8150
8151 (defun gnus-summary-mark-as-read-backward (n)
8152   "Mark the N articles as read backwards.
8153 The difference between N and the actual number of articles marked is
8154 returned."
8155   (interactive "p")
8156   (gnus-summary-mark-forward
8157    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8158
8159 (defun gnus-summary-mark-as-read (&optional article mark)
8160   "Mark current article as read.
8161 ARTICLE specifies the article to be marked as read.
8162 MARK specifies a string to be inserted at the beginning of the line."
8163   (gnus-summary-mark-article article mark))
8164
8165 (defun gnus-summary-clear-mark-forward (n)
8166   "Clear marks from N articles forward.
8167 If N is negative, clear backward instead.
8168 The difference between N and the number of marks cleared is returned."
8169   (interactive "p")
8170   (gnus-summary-mark-forward n gnus-unread-mark))
8171
8172 (defun gnus-summary-clear-mark-backward (n)
8173   "Clear marks from N articles backward.
8174 The difference between N and the number of marks cleared is returned."
8175   (interactive "p")
8176   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8177
8178 (defun gnus-summary-mark-unread-as-read ()
8179   "Intended to be used by `gnus-summary-mark-article-hook'."
8180   (when (memq gnus-current-article gnus-newsgroup-unreads)
8181     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8182
8183 (defun gnus-summary-mark-read-and-unread-as-read ()
8184   "Intended to be used by `gnus-summary-mark-article-hook'."
8185   (let ((mark (gnus-summary-article-mark)))
8186     (when (or (gnus-unread-mark-p mark)
8187               (gnus-read-mark-p mark))
8188       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8189
8190 (defun gnus-summary-mark-region-as-read (point mark all)
8191   "Mark all unread articles between point and mark as read.
8192 If given a prefix, mark all articles between point and mark as read,
8193 even ticked and dormant ones."
8194   (interactive "r\nP")
8195   (save-excursion
8196     (let (article)
8197       (goto-char point)
8198       (beginning-of-line)
8199       (while (and
8200               (< (point) mark)
8201               (progn
8202                 (when (or all
8203                           (memq (setq article (gnus-summary-article-number))
8204                                 gnus-newsgroup-unreads))
8205                   (gnus-summary-mark-article article gnus-del-mark))
8206                 t)
8207               (gnus-summary-find-next))))))
8208
8209 (defun gnus-summary-mark-below (score mark)
8210   "Mark articles with score less than SCORE with MARK."
8211   (interactive "P\ncMark: ")
8212   (setq score (if score
8213                   (prefix-numeric-value score)
8214                 (or gnus-summary-default-score 0)))
8215   (save-excursion
8216     (set-buffer gnus-summary-buffer)
8217     (goto-char (point-min))
8218     (while
8219         (progn
8220           (and (< (gnus-summary-article-score) score)
8221                (gnus-summary-mark-article nil mark))
8222           (gnus-summary-find-next)))))
8223
8224 (defun gnus-summary-kill-below (&optional score)
8225   "Mark articles with score below SCORE as read."
8226   (interactive "P")
8227   (gnus-summary-mark-below score gnus-killed-mark))
8228
8229 (defun gnus-summary-clear-above (&optional score)
8230   "Clear all marks from articles with score above SCORE."
8231   (interactive "P")
8232   (gnus-summary-mark-above score gnus-unread-mark))
8233
8234 (defun gnus-summary-tick-above (&optional score)
8235   "Tick all articles with score above SCORE."
8236   (interactive "P")
8237   (gnus-summary-mark-above score gnus-ticked-mark))
8238
8239 (defun gnus-summary-mark-above (score mark)
8240   "Mark articles with score over SCORE with MARK."
8241   (interactive "P\ncMark: ")
8242   (setq score (if score
8243                   (prefix-numeric-value score)
8244                 (or gnus-summary-default-score 0)))
8245   (save-excursion
8246     (set-buffer gnus-summary-buffer)
8247     (goto-char (point-min))
8248     (while (and (progn
8249                   (when (> (gnus-summary-article-score) score)
8250                     (gnus-summary-mark-article nil mark))
8251                   t)
8252                 (gnus-summary-find-next)))))
8253
8254 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8255 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8256 (defun gnus-summary-limit-include-expunged (&optional no-error)
8257   "Display all the hidden articles that were expunged for low scores."
8258   (interactive)
8259   (let ((buffer-read-only nil))
8260     (let ((scored gnus-newsgroup-scored)
8261           headers h)
8262       (while scored
8263         (unless (gnus-summary-goto-subject (caar scored))
8264           (and (setq h (gnus-summary-article-header (caar scored)))
8265                (< (cdar scored) gnus-summary-expunge-below)
8266                (push h headers)))
8267         (setq scored (cdr scored)))
8268       (if (not headers)
8269           (when (not no-error)
8270             (error "No expunged articles hidden"))
8271         (goto-char (point-min))
8272         (gnus-summary-prepare-unthreaded (nreverse headers))
8273         (goto-char (point-min))
8274         (gnus-summary-position-point)
8275         t))))
8276
8277 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8278   "Mark all unread articles in this newsgroup as read.
8279 If prefix argument ALL is non-nil, ticked and dormant articles will
8280 also be marked as read.
8281 If QUIETLY is non-nil, no questions will be asked.
8282 If TO-HERE is non-nil, it should be a point in the buffer.  All
8283 articles before this point will be marked as read.
8284 Note that this function will only catch up the unread article
8285 in the current summary buffer limitation.
8286 The number of articles marked as read is returned."
8287   (interactive "P")
8288   (prog1
8289       (save-excursion
8290         (when (or quietly
8291                   (not gnus-interactive-catchup) ;Without confirmation?
8292                   gnus-expert-user
8293                   (gnus-y-or-n-p
8294                    (if all
8295                        "Mark absolutely all articles as read? "
8296                      "Mark all unread articles as read? ")))
8297           (if (and not-mark
8298                    (not gnus-newsgroup-adaptive)
8299                    (not gnus-newsgroup-auto-expire)
8300                    (not gnus-suppress-duplicates)
8301                    (or (not gnus-use-cache)
8302                        (eq gnus-use-cache 'passive)))
8303               (progn
8304                 (when all
8305                   (setq gnus-newsgroup-marked nil
8306                         gnus-newsgroup-dormant nil))
8307                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8308             ;; We actually mark all articles as canceled, which we
8309             ;; have to do when using auto-expiry or adaptive scoring.
8310             (gnus-summary-show-all-threads)
8311             (when (gnus-summary-first-subject (not all) t)
8312               (while (and
8313                       (if to-here (< (point) to-here) t)
8314                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8315                       (gnus-summary-find-next (not all) nil nil t))))
8316             (gnus-set-mode-line 'summary))
8317           t))
8318     (gnus-summary-position-point)))
8319
8320 (defun gnus-summary-catchup-to-here (&optional all)
8321   "Mark all unticked articles before the current one as read.
8322 If ALL is non-nil, also mark ticked and dormant articles as read."
8323   (interactive "P")
8324   (save-excursion
8325     (gnus-save-hidden-threads
8326       (let ((beg (point)))
8327         ;; We check that there are unread articles.
8328         (when (or all (gnus-summary-find-prev))
8329           (gnus-summary-catchup all t beg)))))
8330   (gnus-summary-position-point))
8331
8332 (defun gnus-summary-catchup-all (&optional quietly)
8333   "Mark all articles in this newsgroup as read."
8334   (interactive "P")
8335   (gnus-summary-catchup t quietly))
8336
8337 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8338   "Mark all unread articles in this group as read, then exit.
8339 If prefix argument ALL is non-nil, all articles are marked as read."
8340   (interactive "P")
8341   (when (gnus-summary-catchup all quietly nil 'fast)
8342     ;; Select next newsgroup or exit.
8343     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8344              (eq gnus-auto-select-next 'quietly))
8345         (gnus-summary-next-group nil)
8346       (gnus-summary-exit))))
8347
8348 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8349   "Mark all articles in this newsgroup as read, and then exit."
8350   (interactive "P")
8351   (gnus-summary-catchup-and-exit t quietly))
8352
8353 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8354 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8355   "Mark all articles in this group as read and select the next group.
8356 If given a prefix, mark all articles, unread as well as ticked, as
8357 read."
8358   (interactive "P")
8359   (save-excursion
8360     (gnus-summary-catchup all))
8361   (gnus-summary-next-article t nil nil t))
8362
8363 ;; Thread-based commands.
8364
8365 (defun gnus-summary-articles-in-thread (&optional article)
8366   "Return a list of all articles in the current thread.
8367 If ARTICLE is non-nil, return all articles in the thread that starts
8368 with that article."
8369   (let* ((article (or article (gnus-summary-article-number)))
8370          (data (gnus-data-find-list article))
8371          (top-level (gnus-data-level (car data)))
8372          (top-subject
8373           (cond ((null gnus-thread-operation-ignore-subject)
8374                  (gnus-simplify-subject-re
8375                   (mail-header-subject (gnus-data-header (car data)))))
8376                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8377                  (gnus-simplify-subject-fuzzy
8378                   (mail-header-subject (gnus-data-header (car data)))))
8379                 (t nil)))
8380          (end-point (save-excursion
8381                       (if (gnus-summary-go-to-next-thread)
8382                           (point) (point-max))))
8383          articles)
8384     (while (and data
8385                 (< (gnus-data-pos (car data)) end-point))
8386       (when (or (not top-subject)
8387                 (string= top-subject
8388                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8389                              (gnus-simplify-subject-fuzzy
8390                               (mail-header-subject
8391                                (gnus-data-header (car data))))
8392                            (gnus-simplify-subject-re
8393                             (mail-header-subject
8394                              (gnus-data-header (car data)))))))
8395         (push (gnus-data-number (car data)) articles))
8396       (unless (and (setq data (cdr data))
8397                    (> (gnus-data-level (car data)) top-level))
8398         (setq data nil)))
8399     ;; Return the list of articles.
8400     (nreverse articles)))
8401
8402 (defun gnus-summary-rethread-current ()
8403   "Rethread the thread the current article is part of."
8404   (interactive)
8405   (let* ((gnus-show-threads t)
8406          (article (gnus-summary-article-number))
8407          (id (mail-header-id (gnus-summary-article-header)))
8408          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8409     (unless id
8410       (error "No article on the current line"))
8411     (gnus-rebuild-thread id)
8412     (gnus-summary-goto-subject article)))
8413
8414 (defun gnus-summary-reparent-thread ()
8415   "Make the current article child of the marked (or previous) article.
8416
8417 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8418 is non-nil or the Subject: of both articles are the same."
8419   (interactive)
8420   (unless (not (gnus-group-read-only-p))
8421     (error "The current newsgroup does not support article editing"))
8422   (unless (<= (length gnus-newsgroup-processable) 1)
8423     (error "No more than one article may be marked"))
8424   (save-window-excursion
8425     (let ((gnus-article-buffer " *reparent*")
8426           (current-article (gnus-summary-article-number))
8427           ;; First grab the marked article, otherwise one line up.
8428           (parent-article (if (not (null gnus-newsgroup-processable))
8429                               (car gnus-newsgroup-processable)
8430                             (save-excursion
8431                               (if (eq (forward-line -1) 0)
8432                                   (gnus-summary-article-number)
8433                                 (error "Beginning of summary buffer"))))))
8434       (unless (not (eq current-article parent-article))
8435         (error "An article may not be self-referential"))
8436       (let ((message-id (mail-header-id
8437                          (gnus-summary-article-header parent-article))))
8438         (unless (and message-id (not (equal message-id "")))
8439           (error "No message-id in desired parent"))
8440         (gnus-with-article current-article
8441           (goto-char (point-min))
8442           (if (re-search-forward "^References: " nil t)
8443               (progn
8444                 (re-search-forward "^[^ \t]" nil t)
8445                 (forward-line -1)
8446                 (end-of-line)
8447                 (insert " " message-id))
8448             (insert "References: " message-id "\n")))
8449         (set-buffer gnus-summary-buffer)
8450         (gnus-summary-unmark-all-processable)
8451         (gnus-summary-update-article current-article)
8452         (gnus-summary-rethread-current)
8453         (gnus-message 3 "Article %d is now the child of article %d"
8454                       current-article parent-article)))))
8455
8456 (defun gnus-summary-toggle-threads (&optional arg)
8457   "Toggle showing conversation threads.
8458 If ARG is positive number, turn showing conversation threads on."
8459   (interactive "P")
8460   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8461     (setq gnus-show-threads
8462           (if (null arg) (not gnus-show-threads)
8463             (> (prefix-numeric-value arg) 0)))
8464     (gnus-summary-prepare)
8465     (gnus-summary-goto-subject current)
8466     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8467     (gnus-summary-position-point)))
8468
8469 (defun gnus-summary-show-all-threads ()
8470   "Show all threads."
8471   (interactive)
8472   (save-excursion
8473     (let ((buffer-read-only nil))
8474       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8475   (gnus-summary-position-point))
8476
8477 (defun gnus-summary-show-thread ()
8478   "Show thread subtrees.
8479 Returns nil if no thread was there to be shown."
8480   (interactive)
8481   (let ((buffer-read-only nil)
8482         (orig (point))
8483         ;; first goto end then to beg, to have point at beg after let
8484         (end (progn (end-of-line) (point)))
8485         (beg (progn (beginning-of-line) (point))))
8486     (prog1
8487         ;; Any hidden lines here?
8488         (search-forward "\r" end t)
8489       (subst-char-in-region beg end ?\^M ?\n t)
8490       (goto-char orig)
8491       (gnus-summary-position-point))))
8492
8493 (defun gnus-summary-hide-all-threads ()
8494   "Hide all thread subtrees."
8495   (interactive)
8496   (save-excursion
8497     (goto-char (point-min))
8498     (gnus-summary-hide-thread)
8499     (while (zerop (gnus-summary-next-thread 1 t))
8500       (gnus-summary-hide-thread)))
8501   (gnus-summary-position-point))
8502
8503 (defun gnus-summary-hide-thread ()
8504   "Hide thread subtrees.
8505 Returns nil if no threads were there to be hidden."
8506   (interactive)
8507   (let ((buffer-read-only nil)
8508         (start (point))
8509         (article (gnus-summary-article-number)))
8510     (goto-char start)
8511     ;; Go forward until either the buffer ends or the subthread
8512     ;; ends.
8513     (when (and (not (eobp))
8514                (or (zerop (gnus-summary-next-thread 1 t))
8515                    (goto-char (point-max))))
8516       (prog1
8517           (if (and (> (point) start)
8518                    (search-backward "\n" start t))
8519               (progn
8520                 (subst-char-in-region start (point) ?\n ?\^M)
8521                 (gnus-summary-goto-subject article))
8522             (goto-char start)
8523             nil)
8524         ;;(gnus-summary-position-point)
8525         ))))
8526
8527 (defun gnus-summary-go-to-next-thread (&optional previous)
8528   "Go to the same level (or less) next thread.
8529 If PREVIOUS is non-nil, go to previous thread instead.
8530 Return the article number moved to, or nil if moving was impossible."
8531   (let ((level (gnus-summary-thread-level))
8532         (way (if previous -1 1))
8533         (beg (point)))
8534     (forward-line way)
8535     (while (and (not (eobp))
8536                 (< level (gnus-summary-thread-level)))
8537       (forward-line way))
8538     (if (eobp)
8539         (progn
8540           (goto-char beg)
8541           nil)
8542       (setq beg (point))
8543       (prog1
8544           (gnus-summary-article-number)
8545         (goto-char beg)))))
8546
8547 (defun gnus-summary-next-thread (n &optional silent)
8548   "Go to the same level next N'th thread.
8549 If N is negative, search backward instead.
8550 Returns the difference between N and the number of skips actually
8551 done.
8552
8553 If SILENT, don't output messages."
8554   (interactive "p")
8555   (let ((backward (< n 0))
8556         (n (abs n)))
8557     (while (and (> n 0)
8558                 (gnus-summary-go-to-next-thread backward))
8559       (decf n))
8560     (unless silent
8561       (gnus-summary-position-point))
8562     (when (and (not silent) (/= 0 n))
8563       (gnus-message 7 "No more threads"))
8564     n))
8565
8566 (defun gnus-summary-prev-thread (n)
8567   "Go to the same level previous N'th thread.
8568 Returns the difference between N and the number of skips actually
8569 done."
8570   (interactive "p")
8571   (gnus-summary-next-thread (- n)))
8572
8573 (defun gnus-summary-go-down-thread ()
8574   "Go down one level in the current thread."
8575   (let ((children (gnus-summary-article-children)))
8576     (when children
8577       (gnus-summary-goto-subject (car children)))))
8578
8579 (defun gnus-summary-go-up-thread ()
8580   "Go up one level in the current thread."
8581   (let ((parent (gnus-summary-article-parent)))
8582     (when parent
8583       (gnus-summary-goto-subject parent))))
8584
8585 (defun gnus-summary-down-thread (n)
8586   "Go down thread N steps.
8587 If N is negative, go up instead.
8588 Returns the difference between N and how many steps down that were
8589 taken."
8590   (interactive "p")
8591   (let ((up (< n 0))
8592         (n (abs n)))
8593     (while (and (> n 0)
8594                 (if up (gnus-summary-go-up-thread)
8595                   (gnus-summary-go-down-thread)))
8596       (setq n (1- n)))
8597     (gnus-summary-position-point)
8598     (when (/= 0 n)
8599       (gnus-message 7 "Can't go further"))
8600     n))
8601
8602 (defun gnus-summary-up-thread (n)
8603   "Go up thread N steps.
8604 If N is negative, go up instead.
8605 Returns the difference between N and how many steps down that were
8606 taken."
8607   (interactive "p")
8608   (gnus-summary-down-thread (- n)))
8609
8610 (defun gnus-summary-top-thread ()
8611   "Go to the top of the thread."
8612   (interactive)
8613   (while (gnus-summary-go-up-thread))
8614   (gnus-summary-article-number))
8615
8616 (defun gnus-summary-kill-thread (&optional unmark)
8617   "Mark articles under current thread as read.
8618 If the prefix argument is positive, remove any kinds of marks.
8619 If the prefix argument is negative, tick articles instead."
8620   (interactive "P")
8621   (when unmark
8622     (setq unmark (prefix-numeric-value unmark)))
8623   (let ((articles (gnus-summary-articles-in-thread)))
8624     (save-excursion
8625       ;; Expand the thread.
8626       (gnus-summary-show-thread)
8627       ;; Mark all the articles.
8628       (while articles
8629         (gnus-summary-goto-subject (car articles))
8630         (cond ((null unmark)
8631                (gnus-summary-mark-article-as-read gnus-killed-mark))
8632               ((> unmark 0)
8633                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8634               (t
8635                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8636         (setq articles (cdr articles))))
8637     ;; Hide killed subtrees.
8638     (and (null unmark)
8639          gnus-thread-hide-killed
8640          (gnus-summary-hide-thread))
8641     ;; If marked as read, go to next unread subject.
8642     (when (null unmark)
8643       ;; Go to next unread subject.
8644       (gnus-summary-next-subject 1 t)))
8645   (gnus-set-mode-line 'summary))
8646
8647 ;; Summary sorting commands
8648
8649 (defun gnus-summary-sort-by-number (&optional reverse)
8650   "Sort the summary buffer by article number.
8651 Argument REVERSE means reverse order."
8652   (interactive "P")
8653   (gnus-summary-sort 'number reverse))
8654
8655 (defun gnus-summary-sort-by-author (&optional reverse)
8656   "Sort the summary buffer by author name alphabetically.
8657 If case-fold-search is non-nil, case of letters is ignored.
8658 Argument REVERSE means reverse order."
8659   (interactive "P")
8660   (gnus-summary-sort 'author reverse))
8661
8662 (defun gnus-summary-sort-by-subject (&optional reverse)
8663   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8664 If case-fold-search is non-nil, case of letters is ignored.
8665 Argument REVERSE means reverse order."
8666   (interactive "P")
8667   (gnus-summary-sort 'subject reverse))
8668
8669 (defun gnus-summary-sort-by-date (&optional reverse)
8670   "Sort the summary buffer by date.
8671 Argument REVERSE means reverse order."
8672   (interactive "P")
8673   (gnus-summary-sort 'date reverse))
8674
8675 (defun gnus-summary-sort-by-score (&optional reverse)
8676   "Sort the summary buffer by score.
8677 Argument REVERSE means reverse order."
8678   (interactive "P")
8679   (gnus-summary-sort 'score reverse))
8680
8681 (defun gnus-summary-sort-by-lines (&optional reverse)
8682   "Sort the summary buffer by the number of lines.
8683 Argument REVERSE means reverse order."
8684   (interactive "P")
8685   (gnus-summary-sort 'lines reverse))
8686
8687 (defun gnus-summary-sort-by-chars (&optional reverse)
8688   "Sort the summary buffer by article length.
8689 Argument REVERSE means reverse order."
8690   (interactive "P")
8691   (gnus-summary-sort 'chars reverse))   
8692
8693 (defun gnus-summary-sort (predicate reverse)
8694   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8695   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8696          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8697          (gnus-thread-sort-functions
8698           (if (not reverse)
8699               thread
8700             `(lambda (t1 t2)
8701                (,thread t2 t1))))
8702          (gnus-article-sort-functions
8703           (if (not reverse)
8704               article
8705             `(lambda (t1 t2)
8706                (,article t2 t1))))
8707          (buffer-read-only)
8708          (gnus-summary-prepare-hook nil))
8709     ;; We do the sorting by regenerating the threads.
8710     (gnus-summary-prepare)
8711     ;; Hide subthreads if needed.
8712     (when (and gnus-show-threads gnus-thread-hide-subtree)
8713       (gnus-summary-hide-all-threads))))
8714
8715 ;; Summary saving commands.
8716
8717 (defun gnus-summary-save-article (&optional n not-saved)
8718   "Save the current article using the default saver function.
8719 If N is a positive number, save the N next articles.
8720 If N is a negative number, save the N previous articles.
8721 If N is nil and any articles have been marked with the process mark,
8722 save those articles instead.
8723 The variable `gnus-default-article-saver' specifies the saver function."
8724   (interactive "P")
8725   (let* ((articles (gnus-summary-work-articles n))
8726          (save-buffer (save-excursion
8727                         (nnheader-set-temp-buffer " *Gnus Save*")))
8728          (num (length articles))
8729          header file)
8730     (dolist (article articles)
8731       (setq header (gnus-summary-article-header article))
8732       (if (not (vectorp header))
8733           ;; This is a pseudo-article.
8734           (if (assq 'name header)
8735               (gnus-copy-file (cdr (assq 'name header)))
8736             (gnus-message 1 "Article %d is unsaveable" article))
8737         ;; This is a real article.
8738         (save-window-excursion
8739           (gnus-summary-select-article t nil nil article))
8740         (save-excursion
8741           (set-buffer save-buffer)
8742           (erase-buffer)
8743           (insert-buffer-substring gnus-original-article-buffer))
8744         (setq file (gnus-article-save save-buffer file num))
8745         (gnus-summary-remove-process-mark article)
8746         (unless not-saved
8747           (gnus-summary-set-saved-mark article))))
8748     (gnus-kill-buffer save-buffer)
8749     (gnus-summary-position-point)
8750     (gnus-set-mode-line 'summary)
8751     n))
8752
8753 (defun gnus-summary-pipe-output (&optional arg)
8754   "Pipe the current article to a subprocess.
8755 If N is a positive number, pipe the N next articles.
8756 If N is a negative number, pipe the N previous articles.
8757 If N is nil and any articles have been marked with the process mark,
8758 pipe those articles instead."
8759   (interactive "P")
8760   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8761     (gnus-summary-save-article arg t))
8762   (gnus-configure-windows 'pipe))
8763
8764 (defun gnus-summary-save-article-mail (&optional arg)
8765   "Append the current article to an mail file.
8766 If N is a positive number, save the N next articles.
8767 If N is a negative number, save the N previous articles.
8768 If N is nil and any articles have been marked with the process mark,
8769 save those articles instead."
8770   (interactive "P")
8771   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8772     (gnus-summary-save-article arg)))
8773
8774 (defun gnus-summary-save-article-rmail (&optional arg)
8775   "Append the current article to an rmail file.
8776 If N is a positive number, save the N next articles.
8777 If N is a negative number, save the N previous articles.
8778 If N is nil and any articles have been marked with the process mark,
8779 save those articles instead."
8780   (interactive "P")
8781   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8782     (gnus-summary-save-article arg)))
8783
8784 (defun gnus-summary-save-article-file (&optional arg)
8785   "Append the current article to a file.
8786 If N is a positive number, save the N next articles.
8787 If N is a negative number, save the N previous articles.
8788 If N is nil and any articles have been marked with the process mark,
8789 save those articles instead."
8790   (interactive "P")
8791   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8792     (gnus-summary-save-article arg)))
8793
8794 (defun gnus-summary-write-article-file (&optional arg)
8795   "Write the current article to a file, deleting the previous file.
8796 If N is a positive number, save the N next articles.
8797 If N is a negative number, save the N previous articles.
8798 If N is nil and any articles have been marked with the process mark,
8799 save those articles instead."
8800   (interactive "P")
8801   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8802     (gnus-summary-save-article arg)))
8803
8804 (defun gnus-summary-save-article-body-file (&optional arg)
8805   "Append the current article body to a file.
8806 If N is a positive number, save the N next articles.
8807 If N is a negative number, save the N previous articles.
8808 If N is nil and any articles have been marked with the process mark,
8809 save those articles instead."
8810   (interactive "P")
8811   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8812     (gnus-summary-save-article arg)))
8813
8814 (defun gnus-summary-pipe-message (program)
8815   "Pipe the current article through PROGRAM."
8816   (interactive "sProgram: ")
8817   (gnus-summary-select-article)
8818   (let ((mail-header-separator ""))
8819     (gnus-eval-in-buffer-window gnus-article-buffer
8820       (save-restriction
8821         (widen)
8822         (let ((start (window-start))
8823               buffer-read-only)
8824           (message-pipe-buffer-body program)
8825           (set-window-start (get-buffer-window (current-buffer)) start))))))
8826
8827 (defun gnus-get-split-value (methods)
8828   "Return a value based on the split METHODS."
8829   (let (split-name method result match)
8830     (when methods
8831       (save-excursion
8832         (set-buffer gnus-original-article-buffer)
8833         (save-restriction
8834           (nnheader-narrow-to-headers)
8835           (while methods
8836             (goto-char (point-min))
8837             (setq method (pop methods))
8838             (setq match (car method))
8839             (when (cond
8840                    ((stringp match)
8841                     ;; Regular expression.
8842                     (ignore-errors
8843                       (re-search-forward match nil t)))
8844                    ((gnus-functionp match)
8845                     ;; Function.
8846                     (save-restriction
8847                       (widen)
8848                       (setq result (funcall match gnus-newsgroup-name))))
8849                    ((consp match)
8850                     ;; Form.
8851                     (save-restriction
8852                       (widen)
8853                       (setq result (eval match)))))
8854               (setq split-name (append (cdr method) split-name))
8855               (cond ((stringp result)
8856                      (push (expand-file-name
8857                             result gnus-article-save-directory)
8858                            split-name))
8859                     ((consp result)
8860                      (setq split-name (append result split-name)))))))))
8861     (nreverse split-name)))
8862
8863 (defun gnus-valid-move-group-p (group)
8864   (and (boundp group)
8865        (symbol-name group)
8866        (symbol-value group)
8867        (gnus-get-function (gnus-find-method-for-group
8868                            (symbol-name group)) 'request-accept-article t)))
8869
8870 (defun gnus-read-move-group-name (prompt default articles prefix)
8871   "Read a group name."
8872   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8873          (minibuffer-confirm-incomplete nil) ; XEmacs
8874          (prom
8875           (format "%s %s to:"
8876                   prompt
8877                   (if (> (length articles) 1)
8878                       (format "these %d articles" (length articles))
8879                     "this article")))
8880          (to-newsgroup
8881           (cond
8882            ((null split-name)
8883             (gnus-completing-read default prom
8884                                   gnus-active-hashtb
8885                                   'gnus-valid-move-group-p
8886                                   nil prefix
8887                                   'gnus-group-history))
8888            ((= 1 (length split-name))
8889             (gnus-completing-read (car split-name) prom
8890                                   gnus-active-hashtb
8891                                   'gnus-valid-move-group-p
8892                                   nil nil
8893                                   'gnus-group-history))
8894            (t
8895             (gnus-completing-read nil prom
8896                                   (mapcar (lambda (el) (list el))
8897                                           (nreverse split-name))
8898                                   nil nil nil
8899                                   'gnus-group-history)))))
8900     (when to-newsgroup
8901       (if (or (string= to-newsgroup "")
8902               (string= to-newsgroup prefix))
8903           (setq to-newsgroup default))
8904       (unless to-newsgroup
8905         (error "No group name entered"))
8906       (or (gnus-active to-newsgroup)
8907           (gnus-activate-group to-newsgroup)
8908           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8909                                      to-newsgroup))
8910               (or (and (gnus-request-create-group
8911                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8912                        (gnus-activate-group to-newsgroup nil nil
8913                                             (gnus-group-name-to-method
8914                                              to-newsgroup)))
8915                   (error "Couldn't create group %s" to-newsgroup)))
8916           (error "No such group: %s" to-newsgroup)))
8917     to-newsgroup))
8918
8919 (defun gnus-summary-save-parts (type dir n reverse)
8920   "Save parts matching TYPE to DIR.
8921 If REVERSE, save parts that do not match TYPE."
8922   (interactive
8923    (list (read-string "Save parts of type: " "image/.*")
8924          (read-file-name "Save to directory: " t nil t)
8925          current-prefix-arg))
8926   (gnus-summary-iterate n
8927     (let ((gnus-display-mime-function nil)
8928           (gnus-inhibit-treatment t))
8929       (gnus-summary-select-article))
8930     (save-excursion
8931       (set-buffer gnus-article-buffer)
8932       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8933         (when handles
8934           (gnus-summary-save-parts-1 type dir handles reverse)
8935           (mm-destroy-parts handles))))))
8936
8937 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8938   (if (stringp (car handle))
8939       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8940               (cdr handle))
8941     (when (if reverse
8942               (not (string-match type (mm-handle-media-type handle)))
8943             (string-match type (mm-handle-media-type handle)))
8944       (let ((file (expand-file-name
8945                    (file-name-nondirectory
8946                     (or
8947                      (mail-content-type-get
8948                       (mm-handle-disposition handle) 'filename)
8949                      (concat gnus-newsgroup-name "." gnus-current-article)))
8950                    dir)))
8951         (unless (file-exists-p file)
8952           (mm-save-part-to-file handle file))))))
8953
8954 ;; Summary extract commands
8955
8956 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8957   (let ((buffer-read-only nil)
8958         (article (gnus-summary-article-number))
8959         after-article b e)
8960     (unless (gnus-summary-goto-subject article)
8961       (error "No such article: %d" article))
8962     (gnus-summary-position-point)
8963     ;; If all commands are to be bunched up on one line, we collect
8964     ;; them here.
8965     (unless gnus-view-pseudos-separately
8966       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8967             files action)
8968         (while ps
8969           (setq action (cdr (assq 'action (car ps))))
8970           (setq files (list (cdr (assq 'name (car ps)))))
8971           (while (and ps (cdr ps)
8972                       (string= (or action "1")
8973                                (or (cdr (assq 'action (cadr ps))) "2")))
8974             (push (cdr (assq 'name (cadr ps))) files)
8975             (setcdr ps (cddr ps)))
8976           (when files
8977             (when (not (string-match "%s" action))
8978               (push " " files))
8979             (push " " files)
8980             (when (assq 'execute (car ps))
8981               (setcdr (assq 'execute (car ps))
8982                       (funcall (if (string-match "%s" action)
8983                                    'format 'concat)
8984                                action
8985                                (mapconcat
8986                                 (lambda (f)
8987                                   (if (equal f " ")
8988                                       f
8989                                     (mm-quote-arg f)))
8990                                 files " ")))))
8991           (setq ps (cdr ps)))))
8992     (if (and gnus-view-pseudos (not not-view))
8993         (while pslist
8994           (when (assq 'execute (car pslist))
8995             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8996                                   (eq gnus-view-pseudos 'not-confirm)))
8997           (setq pslist (cdr pslist)))
8998       (save-excursion
8999         (while pslist
9000           (setq after-article (or (cdr (assq 'article (car pslist)))
9001                                   (gnus-summary-article-number)))
9002           (gnus-summary-goto-subject after-article)
9003           (forward-line 1)
9004           (setq b (point))
9005           (insert "    " (file-name-nondirectory
9006                           (cdr (assq 'name (car pslist))))
9007                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9008           (setq e (point))
9009           (forward-line -1)             ; back to `b'
9010           (gnus-add-text-properties
9011            b (1- e) (list 'gnus-number gnus-reffed-article-number
9012                           gnus-mouse-face-prop gnus-mouse-face))
9013           (gnus-data-enter
9014            after-article gnus-reffed-article-number
9015            gnus-unread-mark b (car pslist) 0 (- e b))
9016           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9017           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9018           (setq pslist (cdr pslist)))))))
9019
9020 (defun gnus-pseudos< (p1 p2)
9021   (let ((c1 (cdr (assq 'action p1)))
9022         (c2 (cdr (assq 'action p2))))
9023     (and c1 c2 (string< c1 c2))))
9024
9025 (defun gnus-request-pseudo-article (props)
9026   (cond ((assq 'execute props)
9027          (gnus-execute-command (cdr (assq 'execute props)))))
9028   (let ((gnus-current-article (gnus-summary-article-number)))
9029     (gnus-run-hooks 'gnus-mark-article-hook)))
9030
9031 (defun gnus-execute-command (command &optional automatic)
9032   (save-excursion
9033     (gnus-article-setup-buffer)
9034     (set-buffer gnus-article-buffer)
9035     (setq buffer-read-only nil)
9036     (let ((command (if automatic command
9037                      (read-string "Command: " (cons command 0)))))
9038       (erase-buffer)
9039       (insert "$ " command "\n\n")
9040       (if gnus-view-pseudo-asynchronously
9041           (start-process "gnus-execute" (current-buffer) shell-file-name
9042                          shell-command-switch command)
9043         (call-process shell-file-name nil t nil
9044                       shell-command-switch command)))))
9045
9046 ;; Summary kill commands.
9047
9048 (defun gnus-summary-edit-global-kill (article)
9049   "Edit the \"global\" kill file."
9050   (interactive (list (gnus-summary-article-number)))
9051   (gnus-group-edit-global-kill article))
9052
9053 (defun gnus-summary-edit-local-kill ()
9054   "Edit a local kill file applied to the current newsgroup."
9055   (interactive)
9056   (setq gnus-current-headers (gnus-summary-article-header))
9057   (gnus-group-edit-local-kill
9058    (gnus-summary-article-number) gnus-newsgroup-name))
9059
9060 ;;; Header reading.
9061
9062 (defun gnus-read-header (id &optional header)
9063   "Read the headers of article ID and enter them into the Gnus system."
9064   (let ((group gnus-newsgroup-name)
9065         (gnus-override-method
9066          (and (gnus-news-group-p gnus-newsgroup-name)
9067               gnus-refer-article-method))
9068         where)
9069     ;; First we check to see whether the header in question is already
9070     ;; fetched.
9071     (if (stringp id)
9072         ;; This is a Message-ID.
9073         (setq header (or header (gnus-id-to-header id)))
9074       ;; This is an article number.
9075       (setq header (or header (gnus-summary-article-header id))))
9076     (if (and header
9077              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9078         ;; We have found the header.
9079         header
9080       ;; If this is a sparse article, we have to nix out its
9081       ;; previous entry in the thread hashtb.
9082       (when (and header
9083                  (gnus-summary-article-sparse-p (mail-header-number header)))
9084         (let* ((parent (gnus-parent-id (mail-header-references header)))
9085                (thread (and parent (gnus-id-to-thread parent))))
9086           (when thread
9087             (delq (assq header thread) thread))))
9088       ;; We have to really fetch the header to this article.
9089       (save-excursion
9090         (set-buffer nntp-server-buffer)
9091         (when (setq where (gnus-request-head id group))
9092           (nnheader-fold-continuation-lines)
9093           (goto-char (point-max))
9094           (insert ".\n")
9095           (goto-char (point-min))
9096           (insert "211 ")
9097           (princ (cond
9098                   ((numberp id) id)
9099                   ((cdr where) (cdr where))
9100                   (header (mail-header-number header))
9101                   (t gnus-reffed-article-number))
9102                  (current-buffer))
9103           (insert " Article retrieved.\n"))
9104         (if (or (not where)
9105                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9106             ()                          ; Malformed head.
9107           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9108             (when (and (stringp id)
9109                        (not (string= (gnus-group-real-name group)
9110                                      (car where))))
9111               ;; If we fetched by Message-ID and the article came
9112               ;; from a different group, we fudge some bogus article
9113               ;; numbers for this article.
9114               (mail-header-set-number header gnus-reffed-article-number))
9115             (save-excursion
9116               (set-buffer gnus-summary-buffer)
9117               (decf gnus-reffed-article-number)
9118               (gnus-remove-header (mail-header-number header))
9119               (push header gnus-newsgroup-headers)
9120               (setq gnus-current-headers header)
9121               (push (mail-header-number header) gnus-newsgroup-limit)))
9122           header)))))
9123
9124 (defun gnus-remove-header (number)
9125   "Remove header NUMBER from `gnus-newsgroup-headers'."
9126   (if (and gnus-newsgroup-headers
9127            (= number (mail-header-number (car gnus-newsgroup-headers))))
9128       (pop gnus-newsgroup-headers)
9129     (let ((headers gnus-newsgroup-headers))
9130       (while (and (cdr headers)
9131                   (not (= number (mail-header-number (cadr headers)))))
9132         (pop headers))
9133       (when (cdr headers)
9134         (setcdr headers (cddr headers))))))
9135
9136 ;;;
9137 ;;; summary highlights
9138 ;;;
9139
9140 (defun gnus-highlight-selected-summary ()
9141   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9142   ;; Highlight selected article in summary buffer
9143   (when gnus-summary-selected-face
9144     (save-excursion
9145       (let* ((beg (progn (beginning-of-line) (point)))
9146              (end (progn (end-of-line) (point)))
9147              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9148              (from (if (get-text-property beg gnus-mouse-face-prop)
9149                        beg
9150                      (or (next-single-property-change
9151                           beg gnus-mouse-face-prop nil end)
9152                          beg)))
9153              (to
9154               (if (= from end)
9155                   (- from 2)
9156                 (or (next-single-property-change
9157                      from gnus-mouse-face-prop nil end)
9158                     end))))
9159         ;; If no mouse-face prop on line we will have to = from = end,
9160         ;; so we highlight the entire line instead.
9161         (when (= (+ to 2) from)
9162           (setq from beg)
9163           (setq to end))
9164         (if gnus-newsgroup-selected-overlay
9165             ;; Move old overlay.
9166             (gnus-move-overlay
9167              gnus-newsgroup-selected-overlay from to (current-buffer))
9168           ;; Create new overlay.
9169           (gnus-overlay-put
9170            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9171            'face gnus-summary-selected-face))))))
9172
9173 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9174 (defun gnus-summary-highlight-line ()
9175   "Highlight current line according to `gnus-summary-highlight'."
9176   (let* ((list gnus-summary-highlight)
9177          (p (point))
9178          (end (progn (end-of-line) (point)))
9179          ;; now find out where the line starts and leave point there.
9180          (beg (progn (beginning-of-line) (point)))
9181          (article (gnus-summary-article-number))
9182          (score (or (cdr (assq (or article gnus-current-article)
9183                                gnus-newsgroup-scored))
9184                     gnus-summary-default-score 0))
9185          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9186          (inhibit-read-only t))
9187     ;; Eval the cars of the lists until we find a match.
9188     (let ((default gnus-summary-default-score))
9189       (while (and list
9190                   (not (eval (caar list))))
9191         (setq list (cdr list))))
9192     (let ((face (cdar list)))
9193       (unless (eq face (get-text-property beg 'face))
9194         (gnus-put-text-property-excluding-characters-with-faces
9195          beg end 'face
9196          (setq face (if (boundp face) (symbol-value face) face)))
9197         (when gnus-summary-highlight-line-function
9198           (funcall gnus-summary-highlight-line-function article face))))
9199     (goto-char p)))
9200
9201 (defun gnus-update-read-articles (group unread &optional compute)
9202   "Update the list of read articles in GROUP."
9203   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9204          (entry (gnus-gethash group gnus-newsrc-hashtb))
9205          (info (nth 2 entry))
9206          (prev 1)
9207          (unread (sort (copy-sequence unread) '<))
9208          read)
9209     (if (or (not info) (not active))
9210         ;; There is no info on this group if it was, in fact,
9211         ;; killed.  Gnus stores no information on killed groups, so
9212         ;; there's nothing to be done.
9213         ;; One could store the information somewhere temporarily,
9214         ;; perhaps...  Hmmm...
9215         ()
9216       ;; Remove any negative articles numbers.
9217       (while (and unread (< (car unread) 0))
9218         (setq unread (cdr unread)))
9219       ;; Remove any expired article numbers
9220       (while (and unread (< (car unread) (car active)))
9221         (setq unread (cdr unread)))
9222       ;; Compute the ranges of read articles by looking at the list of
9223       ;; unread articles.
9224       (while unread
9225         (when (/= (car unread) prev)
9226           (push (if (= prev (1- (car unread))) prev
9227                   (cons prev (1- (car unread))))
9228                 read))
9229         (setq prev (1+ (car unread)))
9230         (setq unread (cdr unread)))
9231       (when (<= prev (cdr active))
9232         (push (cons prev (cdr active)) read))
9233       (setq read (if (> (length read) 1) (nreverse read) read))
9234       (if compute
9235           read
9236         (save-excursion
9237           (set-buffer gnus-group-buffer)
9238           (gnus-undo-register
9239             `(progn
9240                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9241                (gnus-info-set-read ',info ',(gnus-info-read info))
9242                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9243                (gnus-group-update-group ,group t))))
9244        ;; Propagate the read marks to the backend.
9245        (if (gnus-check-backend-function 'request-set-mark group)
9246            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9247                  (add (gnus-remove-from-range read (gnus-info-read info))))
9248              (when (or add del)
9249                (unless (gnus-check-group group)
9250                  (error "Can't open server for %s" group))
9251                (gnus-request-set-mark
9252                 group (delq nil (list (if add (list add 'add '(read)))
9253                                       (if del (list del 'del '(read)))))))))
9254         ;; Enter this list into the group info.
9255         (gnus-info-set-read info read)
9256         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9257         (gnus-get-unread-articles-in-group info (gnus-active group))
9258         t))))
9259
9260 (defun gnus-offer-save-summaries ()
9261   "Offer to save all active summary buffers."
9262   (save-excursion
9263     (let ((buflist (buffer-list))
9264           buffers bufname)
9265       ;; Go through all buffers and find all summaries.
9266       (while buflist
9267         (and (setq bufname (buffer-name (car buflist)))
9268              (string-match "Summary" bufname)
9269              (save-excursion
9270                (set-buffer bufname)
9271                ;; We check that this is, indeed, a summary buffer.
9272                (and (eq major-mode 'gnus-summary-mode)
9273                     ;; Also make sure this isn't bogus.
9274                     gnus-newsgroup-prepared
9275                     ;; Also make sure that this isn't a dead summary buffer.
9276                     (not gnus-dead-summary-mode)))
9277              (push bufname buffers))
9278         (setq buflist (cdr buflist)))
9279       ;; Go through all these summary buffers and offer to save them.
9280       (when buffers
9281         (map-y-or-n-p
9282          "Update summary buffer %s? "
9283          (lambda (buf)
9284            (switch-to-buffer buf)
9285            (gnus-summary-exit))
9286          buffers)))))
9287
9288 (defun gnus-summary-setup-default-charset ()
9289   "Setup newsgroup default charset."
9290   (if (equal gnus-newsgroup-name "nndraft:drafts")
9291       (setq gnus-newsgroup-charset nil)
9292   (let* ((name (and gnus-newsgroup-name
9293                    (gnus-group-real-name gnus-newsgroup-name)))
9294          (ignored-charsets 
9295           (or gnus-newsgroup-ephemeral-ignored-charsets
9296               (append
9297                (and gnus-newsgroup-name
9298                     (or (gnus-group-find-parameter gnus-newsgroup-name
9299                                                    'ignored-charsets t)
9300                         (let ((alist gnus-group-ignored-charsets-alist)
9301                            elem (charsets nil))
9302                           (while (setq elem (pop alist))
9303                             (when (and name
9304                                        (string-match (car elem) name))
9305                               (setq alist nil
9306                                     charsets (cdr elem))))
9307                           charsets))))
9308               gnus-newsgroup-ignored-charsets)))
9309     (setq gnus-newsgroup-charset
9310           (or gnus-newsgroup-ephemeral-charset
9311               (and gnus-newsgroup-name
9312                    (or (gnus-group-find-parameter gnus-newsgroup-name
9313                                                   'charset)
9314                        (let ((alist gnus-group-charset-alist)
9315                              elem (charset nil))
9316                          (while (setq elem (pop alist))
9317                            (when (and name
9318                                       (string-match (car elem) name))
9319                              (setq alist nil
9320                                    charset (cadr elem))))
9321                          charset)))
9322               gnus-default-charset))
9323     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9324          ignored-charsets))))
9325
9326 ;;;
9327 ;;; Mime Commands
9328 ;;;
9329
9330 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9331   "Display the current article buffer fully MIME-buttonized.
9332 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9333 treated as multipart/mixed."
9334   (interactive "P")
9335   (require 'gnus-art)
9336   (let ((gnus-unbuttonized-mime-types nil)
9337         (gnus-mime-display-multipart-as-mixed show-all-parts))
9338     (gnus-summary-show-article)))
9339
9340 (defun gnus-summary-repair-multipart (article)
9341   "Add a Content-Type header to a multipart article without one."
9342   (interactive (list (gnus-summary-article-number)))
9343   (gnus-with-article article
9344     (message-narrow-to-head)
9345     (goto-char (point-max))
9346     (widen)
9347     (when (search-forward "\n--" nil t)
9348       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9349         (message-narrow-to-head)
9350         (message-remove-header "Mime-Version")
9351         (message-remove-header "Content-Type")
9352         (goto-char (point-max))
9353         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9354                         separator))
9355         (insert "Mime-Version: 1.0\n")
9356         (widen))))
9357   (let (gnus-mark-article-hook)
9358     (gnus-summary-select-article t t nil article)))
9359
9360 (defun gnus-summary-toggle-display-buttonized ()
9361   "Toggle the buttonizing of the article buffer."
9362   (interactive)
9363   (require 'gnus-art)
9364   (if (setq gnus-inhibit-mime-unbuttonizing
9365             (not gnus-inhibit-mime-unbuttonizing))
9366       (let ((gnus-unbuttonized-mime-types nil))
9367         (gnus-summary-show-article))
9368     (gnus-summary-show-article)))
9369
9370 ;;;
9371 ;;; with article
9372 ;;;
9373
9374 (defmacro gnus-with-article (article &rest forms)
9375   "Select ARTICLE and perform FORMS in the original article buffer.
9376 Then replace the article with the result."
9377   `(progn
9378      ;; We don't want the article to be marked as read.
9379      (let (gnus-mark-article-hook)
9380        (gnus-summary-select-article t t nil ,article))
9381      (set-buffer gnus-original-article-buffer)
9382      ,@forms
9383      (if (not (gnus-check-backend-function
9384                'request-replace-article (car gnus-article-current)))
9385          (gnus-message 5 "Read-only group; not replacing")
9386        (unless (gnus-request-replace-article
9387                 ,article (car gnus-article-current)
9388                 (current-buffer) t)
9389          (error "Couldn't replace article")))
9390      ;; The cache and backlog have to be flushed somewhat.
9391      (when gnus-keep-backlog
9392        (gnus-backlog-remove-article
9393         (car gnus-article-current) (cdr gnus-article-current)))
9394      (when gnus-use-cache
9395        (gnus-cache-update-article
9396         (car gnus-article-current) (cdr gnus-article-current)))))
9397
9398 (put 'gnus-with-article 'lisp-indent-function 1)
9399 (put 'gnus-with-article 'edebug-form-spec '(form body))
9400
9401 ;;;
9402 ;;; Generic summary marking commands
9403 ;;;
9404
9405 (defvar gnus-summary-marking-alist
9406   '((read gnus-del-mark "d")
9407     (unread gnus-unread-mark "u")
9408     (ticked gnus-ticked-mark "!")
9409     (dormant gnus-dormant-mark "?")
9410     (expirable gnus-expirable-mark "e"))
9411   "An alist of names/marks/keystrokes.")
9412
9413 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9414 (defvar gnus-summary-mark-map)
9415
9416 (defun gnus-summary-make-all-marking-commands ()
9417   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9418   (dolist (elem gnus-summary-marking-alist)
9419     (apply 'gnus-summary-make-marking-command elem)))
9420
9421 (defun gnus-summary-make-marking-command (name mark keystroke)
9422   (let ((map (make-sparse-keymap)))
9423     (define-key gnus-summary-generic-mark-map keystroke map)
9424     (dolist (lway `((next "next" next nil "n")
9425                     (next-unread "next unread" next t "N")
9426                     (prev "previous" prev nil "p")
9427                     (prev-unread "previous unread" prev t "P")
9428                     (nomove "" nil nil ,keystroke)))
9429       (let ((func (gnus-summary-make-marking-command-1
9430                    mark (car lway) lway name)))
9431         (setq func (eval func))
9432         (define-key map (nth 4 lway) func)))))
9433       
9434 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9435   `(defun ,(intern
9436             (format "gnus-summary-put-mark-as-%s%s"
9437                     name (if (eq way 'nomove)
9438                              ""
9439                            (concat "-" (symbol-name way)))))
9440      (n)
9441      ,(format
9442        "Mark the current article as %s%s.
9443 If N, the prefix, then repeat N times.
9444 If N is negative, move in reverse order.
9445 The difference between N and the actual number of articles marked is
9446 returned."
9447        name (cadr lway))
9448      (interactive "p")
9449      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9450     
9451 (defun gnus-summary-generic-mark (n mark move unread)
9452   "Mark N articles with MARK."
9453   (unless (eq major-mode 'gnus-summary-mode)
9454     (error "This command can only be used in the summary buffer"))
9455   (gnus-summary-show-thread)
9456   (let ((nummove
9457          (cond
9458           ((eq move 'next) 1)
9459           ((eq move 'prev) -1)
9460           (t 0))))
9461     (if (zerop nummove)
9462         (setq n 1)
9463       (when (< n 0)
9464         (setq n (abs n)
9465               nummove (* -1 nummove))))
9466     (while (and (> n 0)
9467                 (gnus-summary-mark-article nil mark)
9468                 (zerop (gnus-summary-next-subject nummove unread t)))
9469       (setq n (1- n)))
9470     (when (/= 0 n)
9471       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9472     (gnus-summary-recenter)
9473     (gnus-summary-position-point)
9474     (gnus-set-mode-line 'summary)
9475     n))
9476
9477 (gnus-summary-make-all-marking-commands)
9478
9479 (gnus-ems-redefine)
9480
9481 (provide 'gnus-sum)
9482
9483 (run-hooks 'gnus-sum-load-hook)
9484
9485 ;;; gnus-sum.el ends here